2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
6 * Portions of this software were developed by Edward Tomasz Napierala
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16 * substantially similar to the "NO WARRANTY" disclaimer below
17 * ("Disclaimer") and any redistribution must be conditioned upon
18 * including a substantially similar Disclaimer requirement for further
19 * binary redistribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
31 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGES.
34 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl.c#8 $
37 * CAM Target Layer, a SCSI device emulation subsystem.
39 * Author: Ken Merry <ken@FreeBSD.org>
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/types.h>
51 #include <sys/kthread.h>
53 #include <sys/fcntl.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/malloc.h>
60 #include <sys/ioccom.h>
61 #include <sys/queue.h>
63 #include <sys/endian.h>
64 #include <sys/sysctl.h>
67 #include <cam/scsi/scsi_all.h>
68 #include <cam/scsi/scsi_da.h>
69 #include <cam/ctl/ctl_io.h>
70 #include <cam/ctl/ctl.h>
71 #include <cam/ctl/ctl_frontend.h>
72 #include <cam/ctl/ctl_frontend_internal.h>
73 #include <cam/ctl/ctl_util.h>
74 #include <cam/ctl/ctl_backend.h>
75 #include <cam/ctl/ctl_ioctl.h>
76 #include <cam/ctl/ctl_ha.h>
77 #include <cam/ctl/ctl_private.h>
78 #include <cam/ctl/ctl_debug.h>
79 #include <cam/ctl/ctl_scsi_all.h>
80 #include <cam/ctl/ctl_error.h>
82 struct ctl_softc *control_softc = NULL;
85 * The default is to run with CTL_DONE_THREAD turned on. Completed
86 * transactions are queued for processing by the CTL work thread. When
87 * CTL_DONE_THREAD is not defined, completed transactions are processed in
88 * the caller's context.
90 #define CTL_DONE_THREAD
93 * Use the serial number and device ID provided by the backend, rather than
96 #define CTL_USE_BACKEND_SN
99 * Size and alignment macros needed for Copan-specific HA hardware. These
100 * can go away when the HA code is re-written, and uses busdma for any
103 #define CTL_ALIGN_8B(target, source, type) \
104 if (((uint32_t)source & 0x7) != 0) \
105 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
107 target = (type)source;
109 #define CTL_SIZE_8B(target, size) \
110 if ((size & 0x7) != 0) \
111 target = size + (0x8 - (size & 0x7)); \
115 #define CTL_ALIGN_8B_MARGIN 16
118 * Template mode pages.
122 * Note that these are default values only. The actual values will be
123 * filled in when the user does a mode sense.
125 static struct copan_power_subpage power_page_default = {
126 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
127 /*subpage*/ PWR_SUBPAGE_CODE,
128 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
129 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
130 /*page_version*/ PWR_VERSION,
132 /* max_active_luns*/ PWR_DFLT_MAX_LUNS,
133 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
134 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
138 static struct copan_power_subpage power_page_changeable = {
139 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
140 /*subpage*/ PWR_SUBPAGE_CODE,
141 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
142 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
145 /* max_active_luns*/ 0,
146 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
147 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
151 static struct copan_aps_subpage aps_page_default = {
152 APS_PAGE_CODE | SMPH_SPF, //page_code
153 APS_SUBPAGE_CODE, //subpage
154 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
155 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
156 APS_VERSION, //page_version
158 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0} //reserved
163 static struct copan_aps_subpage aps_page_changeable = {
164 APS_PAGE_CODE | SMPH_SPF, //page_code
165 APS_SUBPAGE_CODE, //subpage
166 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
167 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
170 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172 0, 0, 0, 0, 0} //reserved
175 static struct copan_debugconf_subpage debugconf_page_default = {
176 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
177 DBGCNF_SUBPAGE_CODE, /* subpage */
178 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
179 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
180 DBGCNF_VERSION, /* page_version */
181 {CTL_TIME_IO_DEFAULT_SECS>>8,
182 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
185 static struct copan_debugconf_subpage debugconf_page_changeable = {
186 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
187 DBGCNF_SUBPAGE_CODE, /* subpage */
188 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
189 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
190 0, /* page_version */
191 {0xff,0xff}, /* ctl_time_io_secs */
194 static struct scsi_format_page format_page_default = {
195 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
196 /*page_length*/sizeof(struct scsi_format_page) - 2,
197 /*tracks_per_zone*/ {0, 0},
198 /*alt_sectors_per_zone*/ {0, 0},
199 /*alt_tracks_per_zone*/ {0, 0},
200 /*alt_tracks_per_lun*/ {0, 0},
201 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
202 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
203 /*bytes_per_sector*/ {0, 0},
204 /*interleave*/ {0, 0},
205 /*track_skew*/ {0, 0},
206 /*cylinder_skew*/ {0, 0},
208 /*reserved*/ {0, 0, 0}
211 static struct scsi_format_page format_page_changeable = {
212 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
213 /*page_length*/sizeof(struct scsi_format_page) - 2,
214 /*tracks_per_zone*/ {0, 0},
215 /*alt_sectors_per_zone*/ {0, 0},
216 /*alt_tracks_per_zone*/ {0, 0},
217 /*alt_tracks_per_lun*/ {0, 0},
218 /*sectors_per_track*/ {0, 0},
219 /*bytes_per_sector*/ {0, 0},
220 /*interleave*/ {0, 0},
221 /*track_skew*/ {0, 0},
222 /*cylinder_skew*/ {0, 0},
224 /*reserved*/ {0, 0, 0}
227 static struct scsi_rigid_disk_page rigid_disk_page_default = {
228 /*page_code*/SMS_RIGID_DISK_PAGE,
229 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
230 /*cylinders*/ {0, 0, 0},
231 /*heads*/ CTL_DEFAULT_HEADS,
232 /*start_write_precomp*/ {0, 0, 0},
233 /*start_reduced_current*/ {0, 0, 0},
234 /*step_rate*/ {0, 0},
235 /*landing_zone_cylinder*/ {0, 0, 0},
236 /*rpl*/ SRDP_RPL_DISABLED,
237 /*rotational_offset*/ 0,
239 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
240 CTL_DEFAULT_ROTATION_RATE & 0xff},
244 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
245 /*page_code*/SMS_RIGID_DISK_PAGE,
246 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
247 /*cylinders*/ {0, 0, 0},
249 /*start_write_precomp*/ {0, 0, 0},
250 /*start_reduced_current*/ {0, 0, 0},
251 /*step_rate*/ {0, 0},
252 /*landing_zone_cylinder*/ {0, 0, 0},
254 /*rotational_offset*/ 0,
256 /*rotation_rate*/ {0, 0},
260 static struct scsi_caching_page caching_page_default = {
261 /*page_code*/SMS_CACHING_PAGE,
262 /*page_length*/sizeof(struct scsi_caching_page) - 2,
263 /*flags1*/ SCP_DISC | SCP_WCE,
265 /*disable_pf_transfer_len*/ {0xff, 0xff},
266 /*min_prefetch*/ {0, 0},
267 /*max_prefetch*/ {0xff, 0xff},
268 /*max_pf_ceiling*/ {0xff, 0xff},
270 /*cache_segments*/ 0,
271 /*cache_seg_size*/ {0, 0},
273 /*non_cache_seg_size*/ {0, 0, 0}
276 static struct scsi_caching_page caching_page_changeable = {
277 /*page_code*/SMS_CACHING_PAGE,
278 /*page_length*/sizeof(struct scsi_caching_page) - 2,
281 /*disable_pf_transfer_len*/ {0, 0},
282 /*min_prefetch*/ {0, 0},
283 /*max_prefetch*/ {0, 0},
284 /*max_pf_ceiling*/ {0, 0},
286 /*cache_segments*/ 0,
287 /*cache_seg_size*/ {0, 0},
289 /*non_cache_seg_size*/ {0, 0, 0}
292 static struct scsi_control_page control_page_default = {
293 /*page_code*/SMS_CONTROL_MODE_PAGE,
294 /*page_length*/sizeof(struct scsi_control_page) - 2,
299 /*aen_holdoff_period*/{0, 0}
302 static struct scsi_control_page control_page_changeable = {
303 /*page_code*/SMS_CONTROL_MODE_PAGE,
304 /*page_length*/sizeof(struct scsi_control_page) - 2,
309 /*aen_holdoff_period*/{0, 0}
314 * XXX KDM move these into the softc.
316 static int rcv_sync_msg;
317 static int persis_offset;
318 static uint8_t ctl_pause_rtr;
319 static int ctl_is_single = 1;
320 static int index_to_aps_page;
322 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
325 * Serial number (0x80), device id (0x83), and supported pages (0x00)
327 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 3
329 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
331 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
332 static int ctl_init(void);
333 void ctl_shutdown(void);
334 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
335 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
336 static void ctl_ioctl_online(void *arg);
337 static void ctl_ioctl_offline(void *arg);
338 static int ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id);
339 static int ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id);
340 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
341 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
342 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
343 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock);
344 static int ctl_ioctl_submit_wait(union ctl_io *io);
345 static void ctl_ioctl_datamove(union ctl_io *io);
346 static void ctl_ioctl_done(union ctl_io *io);
347 static void ctl_ioctl_hard_startstop_callback(void *arg,
348 struct cfi_metatask *metatask);
349 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
350 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
351 struct ctl_ooa *ooa_hdr,
352 struct ctl_ooa_entry *kern_entries);
353 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
355 uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
356 uint32_t ctl_port_idx(int port_num);
358 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
359 uint32_t targ_target, uint32_t targ_lun,
361 static void ctl_kfree_io(union ctl_io *io);
363 static void ctl_free_io_internal(union ctl_io *io, int have_lock);
364 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
365 struct ctl_be_lun *be_lun, struct ctl_id target_id);
366 static int ctl_free_lun(struct ctl_lun *lun);
367 static void ctl_create_lun(struct ctl_be_lun *be_lun);
369 static void ctl_failover_change_pages(struct ctl_softc *softc,
370 struct ctl_scsiio *ctsio, int master);
373 static int ctl_do_mode_select(union ctl_io *io);
374 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
375 uint64_t res_key, uint64_t sa_res_key,
376 uint8_t type, uint32_t residx,
377 struct ctl_scsiio *ctsio,
378 struct scsi_per_res_out *cdb,
379 struct scsi_per_res_out_parms* param);
380 static void ctl_pro_preempt_other(struct ctl_lun *lun,
381 union ctl_ha_msg *msg);
382 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
383 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
384 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
385 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
386 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
387 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
388 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
389 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
390 static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
391 union ctl_io *ooa_io);
392 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
393 union ctl_io *starting_io);
394 static int ctl_check_blocked(struct ctl_lun *lun);
395 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
397 struct ctl_cmd_entry *entry,
398 struct ctl_scsiio *ctsio);
399 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
400 static void ctl_failover(void);
401 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
402 struct ctl_scsiio *ctsio);
403 static int ctl_scsiio(struct ctl_scsiio *ctsio);
405 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
406 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
407 ctl_ua_type ua_type);
408 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
409 ctl_ua_type ua_type);
410 static int ctl_abort_task(union ctl_io *io);
411 static void ctl_run_task_queue(struct ctl_softc *ctl_softc);
413 static void ctl_datamove_timer_wakeup(void *arg);
414 static void ctl_done_timer_wakeup(void *arg);
415 #endif /* CTL_IO_DELAY */
417 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
418 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
419 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
420 static void ctl_datamove_remote_write(union ctl_io *io);
421 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
422 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
423 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
424 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
425 ctl_ha_dt_cb callback);
426 static void ctl_datamove_remote_read(union ctl_io *io);
427 static void ctl_datamove_remote(union ctl_io *io);
428 static int ctl_process_done(union ctl_io *io, int have_lock);
429 static void ctl_work_thread(void *arg);
432 * Load the serialization table. This isn't very pretty, but is probably
433 * the easiest way to do it.
435 #include "ctl_ser_table.c"
438 * We only need to define open, close and ioctl routines for this driver.
440 static struct cdevsw ctl_cdevsw = {
441 .d_version = D_VERSION,
444 .d_close = ctl_close,
445 .d_ioctl = ctl_ioctl,
450 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
452 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
454 static moduledata_t ctl_moduledata = {
456 ctl_module_event_handler,
460 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
461 MODULE_VERSION(ctl, 1);
464 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
465 union ctl_ha_msg *msg_info)
467 struct ctl_scsiio *ctsio;
469 if (msg_info->hdr.original_sc == NULL) {
470 printf("%s: original_sc == NULL!\n", __func__);
471 /* XXX KDM now what? */
475 ctsio = &msg_info->hdr.original_sc->scsiio;
476 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
477 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
478 ctsio->io_hdr.status = msg_info->hdr.status;
479 ctsio->scsi_status = msg_info->scsi.scsi_status;
480 ctsio->sense_len = msg_info->scsi.sense_len;
481 ctsio->sense_residual = msg_info->scsi.sense_residual;
482 ctsio->residual = msg_info->scsi.residual;
483 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
484 sizeof(ctsio->sense_data));
485 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
486 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
487 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
492 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
493 union ctl_ha_msg *msg_info)
495 struct ctl_scsiio *ctsio;
497 if (msg_info->hdr.serializing_sc == NULL) {
498 printf("%s: serializing_sc == NULL!\n", __func__);
499 /* XXX KDM now what? */
503 ctsio = &msg_info->hdr.serializing_sc->scsiio;
506 * Attempt to catch the situation where an I/O has
507 * been freed, and we're using it again.
509 if (ctsio->io_hdr.io_type == 0xff) {
510 union ctl_io *tmp_io;
511 tmp_io = (union ctl_io *)ctsio;
512 printf("%s: %p use after free!\n", __func__,
514 printf("%s: type %d msg %d cdb %x iptl: "
515 "%d:%d:%d:%d tag 0x%04x "
516 "flag %#x status %x\n",
518 tmp_io->io_hdr.io_type,
519 tmp_io->io_hdr.msg_type,
520 tmp_io->scsiio.cdb[0],
521 tmp_io->io_hdr.nexus.initid.id,
522 tmp_io->io_hdr.nexus.targ_port,
523 tmp_io->io_hdr.nexus.targ_target.id,
524 tmp_io->io_hdr.nexus.targ_lun,
525 (tmp_io->io_hdr.io_type ==
527 tmp_io->taskio.tag_num :
528 tmp_io->scsiio.tag_num,
529 tmp_io->io_hdr.flags,
530 tmp_io->io_hdr.status);
533 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
534 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue, &ctsio->io_hdr, links);
539 * ISC (Inter Shelf Communication) event handler. Events from the HA
540 * subsystem come in here.
543 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
545 struct ctl_softc *ctl_softc;
547 struct ctl_prio *presio;
548 ctl_ha_status isc_status;
550 ctl_softc = control_softc;
555 printf("CTL: Isc Msg event %d\n", event);
557 if (event == CTL_HA_EVT_MSG_RECV) {
558 union ctl_ha_msg msg_info;
560 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
561 sizeof(msg_info), /*wait*/ 0);
563 printf("CTL: msg_type %d\n", msg_info.msg_type);
565 if (isc_status != 0) {
566 printf("Error receiving message, status = %d\n",
570 mtx_lock(&ctl_softc->ctl_lock);
572 switch (msg_info.hdr.msg_type) {
573 case CTL_MSG_SERIALIZE:
575 printf("Serialize\n");
577 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
579 printf("ctl_isc_event_handler: can't allocate "
582 /* Need to set busy and send msg back */
583 mtx_unlock(&ctl_softc->ctl_lock);
584 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
585 msg_info.hdr.status = CTL_SCSI_ERROR;
586 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
587 msg_info.scsi.sense_len = 0;
588 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
589 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
594 // populate ctsio from msg_info
595 io->io_hdr.io_type = CTL_IO_SCSI;
596 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
597 io->io_hdr.original_sc = msg_info.hdr.original_sc;
599 printf("pOrig %x\n", (int)msg_info.original_sc);
601 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
604 * If we're in serialization-only mode, we don't
605 * want to go through full done processing. Thus
608 * XXX KDM add another flag that is more specific.
610 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
611 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
612 io->io_hdr.nexus = msg_info.hdr.nexus;
614 printf("targ %d, port %d, iid %d, lun %d\n",
615 io->io_hdr.nexus.targ_target.id,
616 io->io_hdr.nexus.targ_port,
617 io->io_hdr.nexus.initid.id,
618 io->io_hdr.nexus.targ_lun);
620 io->scsiio.tag_num = msg_info.scsi.tag_num;
621 io->scsiio.tag_type = msg_info.scsi.tag_type;
622 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
624 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
625 struct ctl_cmd_entry *entry;
628 opcode = io->scsiio.cdb[0];
629 entry = &ctl_cmd_table[opcode];
630 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
632 entry->flags & CTL_FLAG_DATA_MASK;
634 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
639 /* Performed on the Originating SC, XFER mode only */
640 case CTL_MSG_DATAMOVE: {
641 struct ctl_sg_entry *sgl;
644 io = msg_info.hdr.original_sc;
646 printf("%s: original_sc == NULL!\n", __func__);
647 /* XXX KDM do something here */
650 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
651 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
653 * Keep track of this, we need to send it back over
654 * when the datamove is complete.
656 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
658 if (msg_info.dt.sg_sequence == 0) {
660 * XXX KDM we use the preallocated S/G list
661 * here, but we'll need to change this to
662 * dynamic allocation if we need larger S/G
665 if (msg_info.dt.kern_sg_entries >
666 sizeof(io->io_hdr.remote_sglist) /
667 sizeof(io->io_hdr.remote_sglist[0])) {
668 printf("%s: number of S/G entries "
669 "needed %u > allocated num %zd\n",
671 msg_info.dt.kern_sg_entries,
672 sizeof(io->io_hdr.remote_sglist)/
673 sizeof(io->io_hdr.remote_sglist[0]));
676 * XXX KDM send a message back to
677 * the other side to shut down the
678 * DMA. The error will come back
679 * through via the normal channel.
683 sgl = io->io_hdr.remote_sglist;
685 sizeof(io->io_hdr.remote_sglist));
687 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
689 io->scsiio.kern_sg_entries =
690 msg_info.dt.kern_sg_entries;
691 io->scsiio.rem_sg_entries =
692 msg_info.dt.kern_sg_entries;
693 io->scsiio.kern_data_len =
694 msg_info.dt.kern_data_len;
695 io->scsiio.kern_total_len =
696 msg_info.dt.kern_total_len;
697 io->scsiio.kern_data_resid =
698 msg_info.dt.kern_data_resid;
699 io->scsiio.kern_rel_offset =
700 msg_info.dt.kern_rel_offset;
702 * Clear out per-DMA flags.
704 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
706 * Add per-DMA flags that are set for this
707 * particular DMA request.
709 io->io_hdr.flags |= msg_info.dt.flags &
712 sgl = (struct ctl_sg_entry *)
713 io->scsiio.kern_data_ptr;
715 for (i = msg_info.dt.sent_sg_entries, j = 0;
716 i < (msg_info.dt.sent_sg_entries +
717 msg_info.dt.cur_sg_entries); i++, j++) {
718 sgl[i].addr = msg_info.dt.sg_list[j].addr;
719 sgl[i].len = msg_info.dt.sg_list[j].len;
722 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
724 msg_info.dt.sg_list[j].addr,
725 msg_info.dt.sg_list[j].len,
726 sgl[i].addr, sgl[i].len, j, i);
730 memcpy(&sgl[msg_info.dt.sent_sg_entries],
732 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
736 * If this is the last piece of the I/O, we've got
737 * the full S/G list. Queue processing in the thread.
738 * Otherwise wait for the next piece.
740 if (msg_info.dt.sg_last != 0) {
741 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
747 /* Performed on the Serializing (primary) SC, XFER mode only */
748 case CTL_MSG_DATAMOVE_DONE: {
749 if (msg_info.hdr.serializing_sc == NULL) {
750 printf("%s: serializing_sc == NULL!\n",
752 /* XXX KDM now what? */
756 * We grab the sense information here in case
757 * there was a failure, so we can return status
758 * back to the initiator.
760 io = msg_info.hdr.serializing_sc;
761 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
762 io->io_hdr.status = msg_info.hdr.status;
763 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
764 io->scsiio.sense_len = msg_info.scsi.sense_len;
765 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
766 io->io_hdr.port_status = msg_info.scsi.fetd_status;
767 io->scsiio.residual = msg_info.scsi.residual;
768 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
769 sizeof(io->scsiio.sense_data));
771 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
777 /* Preformed on Originating SC, SER_ONLY mode */
779 io = msg_info.hdr.original_sc;
781 printf("%s: Major Bummer\n", __func__);
782 mtx_unlock(&ctl_softc->ctl_lock);
786 printf("pOrig %x\n",(int) ctsio);
789 io->io_hdr.msg_type = CTL_MSG_R2R;
790 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
791 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
797 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
799 * Performed on the Originating (i.e. secondary) SC in XFER
802 case CTL_MSG_FINISH_IO:
803 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
804 ctl_isc_handler_finish_xfer(ctl_softc,
807 ctl_isc_handler_finish_ser_only(ctl_softc,
811 /* Preformed on Originating SC */
812 case CTL_MSG_BAD_JUJU:
813 io = msg_info.hdr.original_sc;
815 printf("%s: Bad JUJU!, original_sc is NULL!\n",
819 ctl_copy_sense_data(&msg_info, io);
821 * IO should have already been cleaned up on other
822 * SC so clear this flag so we won't send a message
823 * back to finish the IO there.
825 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
826 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
828 /* io = msg_info.hdr.serializing_sc; */
829 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
830 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
835 /* Handle resets sent from the other side */
836 case CTL_MSG_MANAGE_TASKS: {
837 struct ctl_taskio *taskio;
838 taskio = (struct ctl_taskio *)ctl_alloc_io(
839 (void *)ctl_softc->othersc_pool);
840 if (taskio == NULL) {
841 printf("ctl_isc_event_handler: can't allocate "
844 /* should I just call the proper reset func
846 mtx_unlock(&ctl_softc->ctl_lock);
849 ctl_zero_io((union ctl_io *)taskio);
850 taskio->io_hdr.io_type = CTL_IO_TASK;
851 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
852 taskio->io_hdr.nexus = msg_info.hdr.nexus;
853 taskio->task_action = msg_info.task.task_action;
854 taskio->tag_num = msg_info.task.tag_num;
855 taskio->tag_type = msg_info.task.tag_type;
857 taskio->io_hdr.start_time = time_uptime;
858 getbintime(&taskio->io_hdr.start_bt);
860 cs_prof_gettime(&taskio->io_hdr.start_ticks);
862 #endif /* CTL_TIME_IO */
863 STAILQ_INSERT_TAIL(&ctl_softc->task_queue,
864 &taskio->io_hdr, links);
865 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
869 /* Persistent Reserve action which needs attention */
870 case CTL_MSG_PERS_ACTION:
871 presio = (struct ctl_prio *)ctl_alloc_io(
872 (void *)ctl_softc->othersc_pool);
873 if (presio == NULL) {
874 printf("ctl_isc_event_handler: can't allocate "
877 /* Need to set busy and send msg back */
878 mtx_unlock(&ctl_softc->ctl_lock);
881 ctl_zero_io((union ctl_io *)presio);
882 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
883 presio->pr_msg = msg_info.pr;
884 STAILQ_INSERT_TAIL(&ctl_softc->isc_queue,
885 &presio->io_hdr, links);
888 case CTL_MSG_SYNC_FE:
891 case CTL_MSG_APS_LOCK: {
892 // It's quicker to execute this then to
895 struct ctl_page_index *page_index;
896 struct copan_aps_subpage *current_sp;
899 targ_lun = msg_info.hdr.nexus.targ_lun;
900 if (msg_info.hdr.nexus.lun_map_fn != NULL)
901 targ_lun = msg_info.hdr.nexus.lun_map_fn(msg_info.hdr.nexus.lun_map_arg, targ_lun);
903 lun = ctl_softc->ctl_luns[targ_lun];
904 page_index = &lun->mode_pages.index[index_to_aps_page];
905 current_sp = (struct copan_aps_subpage *)
906 (page_index->page_data +
907 (page_index->page_len * CTL_PAGE_CURRENT));
909 current_sp->lock_active = msg_info.aps.lock_flag;
913 printf("How did I get here?\n");
915 mtx_unlock(&ctl_softc->ctl_lock);
916 } else if (event == CTL_HA_EVT_MSG_SENT) {
917 if (param != CTL_HA_STATUS_SUCCESS) {
918 printf("Bad status from ctl_ha_msg_send status %d\n",
922 } else if (event == CTL_HA_EVT_DISCONNECT) {
923 printf("CTL: Got a disconnect from Isc\n");
926 printf("ctl_isc_event_handler: Unknown event %d\n", event);
935 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
937 struct scsi_sense_data *sense;
939 sense = &dest->scsiio.sense_data;
940 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
941 dest->scsiio.scsi_status = src->scsi.scsi_status;
942 dest->scsiio.sense_len = src->scsi.sense_len;
943 dest->io_hdr.status = src->hdr.status;
949 struct ctl_softc *softc;
950 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
951 struct ctl_frontend *fe;
964 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
966 softc = control_softc;
968 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
971 softc->dev->si_drv1 = softc;
974 * By default, return a "bad LUN" peripheral qualifier for unknown
975 * LUNs. The user can override this default using the tunable or
976 * sysctl. See the comment in ctl_inquiry_std() for more details.
978 softc->inquiry_pq_no_lun = 1;
979 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
980 &softc->inquiry_pq_no_lun);
981 sysctl_ctx_init(&softc->sysctl_ctx);
982 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
983 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
984 CTLFLAG_RD, 0, "CAM Target Layer");
986 if (softc->sysctl_tree == NULL) {
987 printf("%s: unable to allocate sysctl tree\n", __func__);
988 destroy_dev(softc->dev);
989 free(control_softc, M_DEVBUF);
990 control_softc = NULL;
994 SYSCTL_ADD_INT(&softc->sysctl_ctx,
995 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
996 "inquiry_pq_no_lun", CTLFLAG_RW,
997 &softc->inquiry_pq_no_lun, 0,
998 "Report no lun possible for invalid LUNs");
1000 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1001 softc->open_count = 0;
1004 * Default to actually sending a SYNCHRONIZE CACHE command down to
1007 softc->flags = CTL_FLAG_REAL_SYNC;
1010 * In Copan's HA scheme, the "master" and "slave" roles are
1011 * figured out through the slot the controller is in. Although it
1012 * is an active/active system, someone has to be in charge.
1015 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1019 softc->flags |= CTL_FLAG_MASTER_SHELF;
1022 persis_offset = CTL_MAX_INITIATORS;
1025 * XXX KDM need to figure out where we want to get our target ID
1026 * and WWID. Is it different on each port?
1028 softc->target.id = 0;
1029 softc->target.wwid[0] = 0x12345678;
1030 softc->target.wwid[1] = 0x87654321;
1031 STAILQ_INIT(&softc->lun_list);
1032 STAILQ_INIT(&softc->pending_lun_queue);
1033 STAILQ_INIT(&softc->task_queue);
1034 STAILQ_INIT(&softc->incoming_queue);
1035 STAILQ_INIT(&softc->rtr_queue);
1036 STAILQ_INIT(&softc->done_queue);
1037 STAILQ_INIT(&softc->isc_queue);
1038 STAILQ_INIT(&softc->fe_list);
1039 STAILQ_INIT(&softc->be_list);
1040 STAILQ_INIT(&softc->io_pools);
1045 * We don't bother calling these with ctl_lock held here, because,
1046 * in theory, no one else can try to do anything while we're in our
1047 * module init routine.
1049 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1050 &internal_pool)!= 0){
1051 printf("ctl: can't allocate %d entry internal pool, "
1052 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1056 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1057 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1058 printf("ctl: can't allocate %d entry emergency pool, "
1059 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1060 ctl_pool_free(softc, internal_pool);
1064 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1067 printf("ctl: can't allocate %d entry other SC pool, "
1068 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1069 ctl_pool_free(softc, internal_pool);
1070 ctl_pool_free(softc, emergency_pool);
1074 softc->internal_pool = internal_pool;
1075 softc->emergency_pool = emergency_pool;
1076 softc->othersc_pool = other_pool;
1078 mtx_lock(&softc->ctl_lock);
1079 ctl_pool_acquire(internal_pool);
1080 ctl_pool_acquire(emergency_pool);
1081 ctl_pool_acquire(other_pool);
1082 mtx_unlock(&softc->ctl_lock);
1085 * We used to allocate a processor LUN here. The new scheme is to
1086 * just let the user allocate LUNs as he sees fit.
1089 mtx_lock(&softc->ctl_lock);
1090 ctl_alloc_lun(softc, lun, /*be_lun*/NULL, /*target*/softc->target);
1091 mtx_unlock(&softc->ctl_lock);
1094 error = kproc_create(ctl_work_thread, softc, &softc->work_thread, 0, 0,
1097 printf("error creating CTL work thread!\n");
1098 mtx_lock(&softc->ctl_lock);
1100 ctl_pool_free(softc, internal_pool);
1101 ctl_pool_free(softc, emergency_pool);
1102 ctl_pool_free(softc, other_pool);
1103 mtx_unlock(&softc->ctl_lock);
1107 printf("ctl: CAM Target Layer loaded\n");
1110 * Initialize the initiator and portname mappings
1112 memset(softc->wwpn_iid, 0, sizeof(softc->wwpn_iid));
1115 * Initialize the ioctl front end.
1117 fe = &softc->ioctl_info.fe;
1118 sprintf(softc->ioctl_info.port_name, "CTL ioctl");
1119 fe->port_type = CTL_PORT_IOCTL;
1120 fe->num_requested_ctl_io = 100;
1121 fe->port_name = softc->ioctl_info.port_name;
1122 fe->port_online = ctl_ioctl_online;
1123 fe->port_offline = ctl_ioctl_offline;
1124 fe->onoff_arg = &softc->ioctl_info;
1125 fe->targ_enable = ctl_ioctl_targ_enable;
1126 fe->targ_disable = ctl_ioctl_targ_disable;
1127 fe->lun_enable = ctl_ioctl_lun_enable;
1128 fe->lun_disable = ctl_ioctl_lun_disable;
1129 fe->targ_lun_arg = &softc->ioctl_info;
1130 fe->fe_datamove = ctl_ioctl_datamove;
1131 fe->fe_done = ctl_ioctl_done;
1132 fe->max_targets = 15;
1133 fe->max_target_id = 15;
1135 if (ctl_frontend_register(&softc->ioctl_info.fe,
1136 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1137 printf("ctl: ioctl front end registration failed, will "
1138 "continue anyway\n");
1142 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1143 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1144 sizeof(struct callout), CTL_TIMER_BYTES);
1147 #endif /* CTL_IO_DELAY */
1155 struct ctl_softc *softc;
1156 struct ctl_lun *lun, *next_lun;
1157 struct ctl_io_pool *pool, *next_pool;
1159 softc = (struct ctl_softc *)control_softc;
1161 if (ctl_frontend_deregister(&softc->ioctl_info.fe) != 0)
1162 printf("ctl: ioctl front end deregistration failed\n");
1164 mtx_lock(&softc->ctl_lock);
1169 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1170 next_lun = STAILQ_NEXT(lun, links);
1175 * This will rip the rug out from under any FETDs or anyone else
1176 * that has a pool allocated. Since we increment our module
1177 * refcount any time someone outside the main CTL module allocates
1178 * a pool, we shouldn't have any problems here. The user won't be
1179 * able to unload the CTL module until client modules have
1180 * successfully unloaded.
1182 for (pool = STAILQ_FIRST(&softc->io_pools); pool != NULL;
1184 next_pool = STAILQ_NEXT(pool, links);
1185 ctl_pool_free(softc, pool);
1188 mtx_unlock(&softc->ctl_lock);
1191 ctl_shutdown_thread(softc->work_thread);
1194 mtx_destroy(&softc->ctl_lock);
1196 destroy_dev(softc->dev);
1198 sysctl_ctx_free(&softc->sysctl_ctx);
1200 free(control_softc, M_DEVBUF);
1201 control_softc = NULL;
1204 printf("ctl: CAM Target Layer unloaded\n");
1208 ctl_module_event_handler(module_t mod, int what, void *arg)
1213 return (ctl_init());
1217 return (EOPNOTSUPP);
1222 * XXX KDM should we do some access checks here? Bump a reference count to
1223 * prevent a CTL module from being unloaded while someone has it open?
1226 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1232 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1238 ctl_port_enable(ctl_port_type port_type)
1240 struct ctl_softc *softc;
1241 struct ctl_frontend *fe;
1243 if (ctl_is_single == 0) {
1244 union ctl_ha_msg msg_info;
1248 printf("%s: HA mode, synchronizing frontend enable\n",
1251 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1252 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1253 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1254 printf("Sync msg send error retval %d\n", isc_retval);
1256 if (!rcv_sync_msg) {
1257 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1258 sizeof(msg_info), 1);
1261 printf("CTL:Frontend Enable\n");
1263 printf("%s: single mode, skipping frontend synchronization\n",
1268 softc = control_softc;
1270 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1271 if (port_type & fe->port_type)
1274 printf("port %d\n", fe->targ_port);
1276 ctl_frontend_online(fe);
1284 ctl_port_disable(ctl_port_type port_type)
1286 struct ctl_softc *softc;
1287 struct ctl_frontend *fe;
1289 softc = control_softc;
1291 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1292 if (port_type & fe->port_type)
1293 ctl_frontend_offline(fe);
1300 * Returns 0 for success, 1 for failure.
1301 * Currently the only failure mode is if there aren't enough entries
1302 * allocated. So, in case of a failure, look at num_entries_dropped,
1303 * reallocate and try again.
1306 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1307 int *num_entries_filled, int *num_entries_dropped,
1308 ctl_port_type port_type, int no_virtual)
1310 struct ctl_softc *softc;
1311 struct ctl_frontend *fe;
1312 int entries_dropped, entries_filled;
1316 softc = control_softc;
1320 entries_dropped = 0;
1323 mtx_lock(&softc->ctl_lock);
1324 STAILQ_FOREACH(fe, &softc->fe_list, links) {
1325 struct ctl_port_entry *entry;
1327 if ((fe->port_type & port_type) == 0)
1330 if ((no_virtual != 0)
1331 && (fe->virtual_port != 0))
1334 if (entries_filled >= num_entries_alloced) {
1338 entry = &entries[i];
1340 entry->port_type = fe->port_type;
1341 strlcpy(entry->port_name, fe->port_name,
1342 sizeof(entry->port_name));
1343 entry->physical_port = fe->physical_port;
1344 entry->virtual_port = fe->virtual_port;
1345 entry->wwnn = fe->wwnn;
1346 entry->wwpn = fe->wwpn;
1352 mtx_unlock(&softc->ctl_lock);
1354 if (entries_dropped > 0)
1357 *num_entries_dropped = entries_dropped;
1358 *num_entries_filled = entries_filled;
1364 ctl_ioctl_online(void *arg)
1366 struct ctl_ioctl_info *ioctl_info;
1368 ioctl_info = (struct ctl_ioctl_info *)arg;
1370 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1374 ctl_ioctl_offline(void *arg)
1376 struct ctl_ioctl_info *ioctl_info;
1378 ioctl_info = (struct ctl_ioctl_info *)arg;
1380 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1384 * Remove an initiator by port number and initiator ID.
1385 * Returns 0 for success, 1 for failure.
1388 ctl_remove_initiator(int32_t targ_port, uint32_t iid)
1390 struct ctl_softc *softc;
1392 softc = control_softc;
1394 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1397 || (targ_port > CTL_MAX_PORTS)) {
1398 printf("%s: invalid port number %d\n", __func__, targ_port);
1401 if (iid > CTL_MAX_INIT_PER_PORT) {
1402 printf("%s: initiator ID %u > maximun %u!\n",
1403 __func__, iid, CTL_MAX_INIT_PER_PORT);
1407 mtx_lock(&softc->ctl_lock);
1409 softc->wwpn_iid[targ_port][iid].in_use = 0;
1411 mtx_unlock(&softc->ctl_lock);
1417 * Add an initiator to the initiator map.
1418 * Returns 0 for success, 1 for failure.
1421 ctl_add_initiator(uint64_t wwpn, int32_t targ_port, uint32_t iid)
1423 struct ctl_softc *softc;
1426 softc = control_softc;
1428 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1433 || (targ_port > CTL_MAX_PORTS)) {
1434 printf("%s: invalid port number %d\n", __func__, targ_port);
1437 if (iid > CTL_MAX_INIT_PER_PORT) {
1438 printf("%s: WWPN %#jx initiator ID %u > maximun %u!\n",
1439 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1443 mtx_lock(&softc->ctl_lock);
1445 if (softc->wwpn_iid[targ_port][iid].in_use != 0) {
1447 * We don't treat this as an error.
1449 if (softc->wwpn_iid[targ_port][iid].wwpn == wwpn) {
1450 printf("%s: port %d iid %u WWPN %#jx arrived again?\n",
1451 __func__, targ_port, iid, (uintmax_t)wwpn);
1456 * This is an error, but what do we do about it? The
1457 * driver is telling us we have a new WWPN for this
1458 * initiator ID, so we pretty much need to use it.
1460 printf("%s: port %d iid %u WWPN %#jx arrived, WWPN %#jx is "
1461 "still at that address\n", __func__, targ_port, iid,
1463 (uintmax_t)softc->wwpn_iid[targ_port][iid].wwpn);
1466 * XXX KDM clear have_ca and ua_pending on each LUN for
1470 softc->wwpn_iid[targ_port][iid].in_use = 1;
1471 softc->wwpn_iid[targ_port][iid].iid = iid;
1472 softc->wwpn_iid[targ_port][iid].wwpn = wwpn;
1473 softc->wwpn_iid[targ_port][iid].port = targ_port;
1477 mtx_unlock(&softc->ctl_lock);
1483 * XXX KDM should we pretend to do something in the target/lun
1484 * enable/disable functions?
1487 ctl_ioctl_targ_enable(void *arg, struct ctl_id targ_id)
1493 ctl_ioctl_targ_disable(void *arg, struct ctl_id targ_id)
1499 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1505 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1511 * Data movement routine for the CTL ioctl frontend port.
1514 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1516 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1517 struct ctl_sg_entry ext_entry, kern_entry;
1518 int ext_sglen, ext_sg_entries, kern_sg_entries;
1519 int ext_sg_start, ext_offset;
1520 int len_to_copy, len_copied;
1521 int kern_watermark, ext_watermark;
1522 int ext_sglist_malloced;
1525 ext_sglist_malloced = 0;
1529 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1532 * If this flag is set, fake the data transfer.
1534 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1535 ctsio->ext_data_filled = ctsio->ext_data_len;
1540 * To simplify things here, if we have a single buffer, stick it in
1541 * a S/G entry and just make it a single entry S/G list.
1543 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1546 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1548 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1550 ext_sglist_malloced = 1;
1551 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1553 ctl_set_internal_failure(ctsio,
1558 ext_sg_entries = ctsio->ext_sg_entries;
1560 for (i = 0; i < ext_sg_entries; i++) {
1561 if ((len_seen + ext_sglist[i].len) >=
1562 ctsio->ext_data_filled) {
1564 ext_offset = ctsio->ext_data_filled - len_seen;
1567 len_seen += ext_sglist[i].len;
1570 ext_sglist = &ext_entry;
1571 ext_sglist->addr = ctsio->ext_data_ptr;
1572 ext_sglist->len = ctsio->ext_data_len;
1575 ext_offset = ctsio->ext_data_filled;
1578 if (ctsio->kern_sg_entries > 0) {
1579 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1580 kern_sg_entries = ctsio->kern_sg_entries;
1582 kern_sglist = &kern_entry;
1583 kern_sglist->addr = ctsio->kern_data_ptr;
1584 kern_sglist->len = ctsio->kern_data_len;
1585 kern_sg_entries = 1;
1590 ext_watermark = ext_offset;
1592 for (i = ext_sg_start, j = 0;
1593 i < ext_sg_entries && j < kern_sg_entries;) {
1594 uint8_t *ext_ptr, *kern_ptr;
1596 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1597 kern_sglist[j].len - kern_watermark);
1599 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1600 ext_ptr = ext_ptr + ext_watermark;
1601 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1605 panic("need to implement bus address support");
1607 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1610 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1611 kern_ptr = kern_ptr + kern_watermark;
1613 kern_watermark += len_to_copy;
1614 ext_watermark += len_to_copy;
1616 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1618 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1619 "bytes to user\n", len_to_copy));
1620 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1621 "to %p\n", kern_ptr, ext_ptr));
1622 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1623 ctl_set_internal_failure(ctsio,
1629 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1630 "bytes from user\n", len_to_copy));
1631 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1632 "to %p\n", ext_ptr, kern_ptr));
1633 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1634 ctl_set_internal_failure(ctsio,
1641 len_copied += len_to_copy;
1643 if (ext_sglist[i].len == ext_watermark) {
1648 if (kern_sglist[j].len == kern_watermark) {
1654 ctsio->ext_data_filled += len_copied;
1656 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1657 "kern_sg_entries: %d\n", ext_sg_entries,
1659 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1660 "kern_data_len = %d\n", ctsio->ext_data_len,
1661 ctsio->kern_data_len));
1664 /* XXX KDM set residual?? */
1667 if (ext_sglist_malloced != 0)
1668 free(ext_sglist, M_CTL);
1670 return (CTL_RETVAL_COMPLETE);
1674 * Serialize a command that went down the "wrong" side, and so was sent to
1675 * this controller for execution. The logic is a little different than the
1676 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1677 * sent back to the other side, but in the success case, we execute the
1678 * command on this side (XFER mode) or tell the other side to execute it
1682 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio, int have_lock)
1684 struct ctl_softc *ctl_softc;
1685 union ctl_ha_msg msg_info;
1686 struct ctl_lun *lun;
1690 ctl_softc = control_softc;
1692 mtx_lock(&ctl_softc->ctl_lock);
1694 targ_lun = ctsio->io_hdr.nexus.targ_lun;
1695 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
1696 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun);
1697 lun = ctl_softc->ctl_luns[targ_lun];
1701 * Why isn't LUN defined? The other side wouldn't
1702 * send a cmd if the LUN is undefined.
1704 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1706 /* "Logical unit not supported" */
1707 ctl_set_sense_data(&msg_info.scsi.sense_data,
1709 /*sense_format*/SSD_TYPE_NONE,
1710 /*current_error*/ 1,
1711 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1716 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1717 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1718 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1719 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1720 msg_info.hdr.serializing_sc = NULL;
1721 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1722 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1723 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1726 mtx_unlock(&ctl_softc->ctl_lock);
1731 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1733 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1734 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1736 case CTL_ACTION_BLOCK:
1737 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1738 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1741 case CTL_ACTION_PASS:
1742 case CTL_ACTION_SKIP:
1743 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1744 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1745 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
1746 &ctsio->io_hdr, links);
1749 /* send msg back to other side */
1750 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1751 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1752 msg_info.hdr.msg_type = CTL_MSG_R2R;
1754 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1756 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1757 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1761 case CTL_ACTION_OVERLAP:
1762 /* OVERLAPPED COMMANDS ATTEMPTED */
1763 ctl_set_sense_data(&msg_info.scsi.sense_data,
1765 /*sense_format*/SSD_TYPE_NONE,
1766 /*current_error*/ 1,
1767 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1772 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1773 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1774 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1775 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1776 msg_info.hdr.serializing_sc = NULL;
1777 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1779 printf("BAD JUJU:Major Bummer Overlap\n");
1781 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1783 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1784 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1787 case CTL_ACTION_OVERLAP_TAG:
1788 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1789 ctl_set_sense_data(&msg_info.scsi.sense_data,
1791 /*sense_format*/SSD_TYPE_NONE,
1792 /*current_error*/ 1,
1793 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1795 /*ascq*/ ctsio->tag_num & 0xff,
1798 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1799 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1800 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1801 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1802 msg_info.hdr.serializing_sc = NULL;
1803 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1805 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1807 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1809 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1810 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1813 case CTL_ACTION_ERROR:
1815 /* "Internal target failure" */
1816 ctl_set_sense_data(&msg_info.scsi.sense_data,
1818 /*sense_format*/SSD_TYPE_NONE,
1819 /*current_error*/ 1,
1820 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1825 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1826 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1827 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1828 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1829 msg_info.hdr.serializing_sc = NULL;
1830 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1832 printf("BAD JUJU:Major Bummer HW Error\n");
1834 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1836 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1837 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1842 mtx_unlock(&ctl_softc->ctl_lock);
1847 ctl_ioctl_submit_wait(union ctl_io *io)
1849 struct ctl_fe_ioctl_params params;
1850 ctl_fe_ioctl_state last_state;
1855 bzero(¶ms, sizeof(params));
1857 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1858 cv_init(¶ms.sem, "ctlioccv");
1859 params.state = CTL_IOCTL_INPROG;
1860 last_state = params.state;
1862 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1864 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1866 /* This shouldn't happen */
1867 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1873 mtx_lock(¶ms.ioctl_mtx);
1875 * Check the state here, and don't sleep if the state has
1876 * already changed (i.e. wakeup has already occured, but we
1877 * weren't waiting yet).
1879 if (params.state == last_state) {
1880 /* XXX KDM cv_wait_sig instead? */
1881 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1883 last_state = params.state;
1885 switch (params.state) {
1886 case CTL_IOCTL_INPROG:
1887 /* Why did we wake up? */
1888 /* XXX KDM error here? */
1889 mtx_unlock(¶ms.ioctl_mtx);
1891 case CTL_IOCTL_DATAMOVE:
1892 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1895 * change last_state back to INPROG to avoid
1896 * deadlock on subsequent data moves.
1898 params.state = last_state = CTL_IOCTL_INPROG;
1900 mtx_unlock(¶ms.ioctl_mtx);
1901 ctl_ioctl_do_datamove(&io->scsiio);
1903 * Note that in some cases, most notably writes,
1904 * this will queue the I/O and call us back later.
1905 * In other cases, generally reads, this routine
1906 * will immediately call back and wake us up,
1907 * probably using our own context.
1909 io->scsiio.be_move_done(io);
1911 case CTL_IOCTL_DONE:
1912 mtx_unlock(¶ms.ioctl_mtx);
1913 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1917 mtx_unlock(¶ms.ioctl_mtx);
1918 /* XXX KDM error here? */
1921 } while (done == 0);
1923 mtx_destroy(¶ms.ioctl_mtx);
1924 cv_destroy(¶ms.sem);
1926 return (CTL_RETVAL_COMPLETE);
1930 ctl_ioctl_datamove(union ctl_io *io)
1932 struct ctl_fe_ioctl_params *params;
1934 params = (struct ctl_fe_ioctl_params *)
1935 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1937 mtx_lock(¶ms->ioctl_mtx);
1938 params->state = CTL_IOCTL_DATAMOVE;
1939 cv_broadcast(¶ms->sem);
1940 mtx_unlock(¶ms->ioctl_mtx);
1944 ctl_ioctl_done(union ctl_io *io)
1946 struct ctl_fe_ioctl_params *params;
1948 params = (struct ctl_fe_ioctl_params *)
1949 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1951 mtx_lock(¶ms->ioctl_mtx);
1952 params->state = CTL_IOCTL_DONE;
1953 cv_broadcast(¶ms->sem);
1954 mtx_unlock(¶ms->ioctl_mtx);
1958 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
1960 struct ctl_fe_ioctl_startstop_info *sd_info;
1962 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
1964 sd_info->hs_info.status = metatask->status;
1965 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
1966 sd_info->hs_info.luns_complete =
1967 metatask->taskinfo.startstop.luns_complete;
1968 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
1970 cv_broadcast(&sd_info->sem);
1974 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
1976 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
1978 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
1980 mtx_lock(fe_bbr_info->lock);
1981 fe_bbr_info->bbr_info->status = metatask->status;
1982 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
1983 fe_bbr_info->wakeup_done = 1;
1984 mtx_unlock(fe_bbr_info->lock);
1986 cv_broadcast(&fe_bbr_info->sem);
1990 * Returns 0 for success, errno for failure.
1993 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
1994 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2001 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
2003 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2004 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2006 struct ctl_ooa_entry *entry;
2009 * If we've got more than we can fit, just count the
2010 * remaining entries.
2012 if (*cur_fill_num >= ooa_hdr->alloc_num)
2015 entry = &kern_entries[*cur_fill_num];
2017 entry->tag_num = io->scsiio.tag_num;
2018 entry->lun_num = lun->lun;
2020 entry->start_bt = io->io_hdr.start_bt;
2022 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2023 entry->cdb_len = io->scsiio.cdb_len;
2024 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2025 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2027 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2028 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2030 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2031 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2033 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2034 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2036 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2037 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2044 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2045 size_t error_str_len)
2049 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2051 if (copyin(user_addr, kptr, len) != 0) {
2052 snprintf(error_str, error_str_len, "Error copying %d bytes "
2053 "from user address %p to kernel address %p", len,
2063 ctl_free_args(int num_be_args, struct ctl_be_arg *be_args)
2067 if (be_args == NULL)
2070 for (i = 0; i < num_be_args; i++) {
2071 free(be_args[i].kname, M_CTL);
2072 free(be_args[i].kvalue, M_CTL);
2075 free(be_args, M_CTL);
2078 static struct ctl_be_arg *
2079 ctl_copyin_args(int num_be_args, struct ctl_be_arg *be_args,
2080 char *error_str, size_t error_str_len)
2082 struct ctl_be_arg *args;
2085 args = ctl_copyin_alloc(be_args, num_be_args * sizeof(*be_args),
2086 error_str, error_str_len);
2091 for (i = 0; i < num_be_args; i++) {
2092 args[i].kname = NULL;
2093 args[i].kvalue = NULL;
2096 for (i = 0; i < num_be_args; i++) {
2099 args[i].kname = ctl_copyin_alloc(args[i].name,
2100 args[i].namelen, error_str, error_str_len);
2101 if (args[i].kname == NULL)
2104 if (args[i].kname[args[i].namelen - 1] != '\0') {
2105 snprintf(error_str, error_str_len, "Argument %d "
2106 "name is not NUL-terminated", i);
2110 args[i].kvalue = NULL;
2112 tmpptr = ctl_copyin_alloc(args[i].value,
2113 args[i].vallen, error_str, error_str_len);
2117 args[i].kvalue = tmpptr;
2119 if ((args[i].flags & CTL_BEARG_ASCII)
2120 && (tmpptr[args[i].vallen - 1] != '\0')) {
2121 snprintf(error_str, error_str_len, "Argument %d "
2122 "value is not NUL-terminated", i);
2130 ctl_free_args(num_be_args, args);
2136 * Escape characters that are illegal or not recommended in XML.
2139 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2145 for (; *str; str++) {
2148 retval = sbuf_printf(sb, "&");
2151 retval = sbuf_printf(sb, ">");
2154 retval = sbuf_printf(sb, "<");
2157 retval = sbuf_putc(sb, *str);
2170 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2173 struct ctl_softc *softc;
2176 softc = control_softc;
2186 * If we haven't been "enabled", don't allow any SCSI I/O
2189 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2194 io = ctl_alloc_io(softc->ioctl_info.fe.ctl_pool_ref);
2196 printf("ctl_ioctl: can't allocate ctl_io!\n");
2202 * Need to save the pool reference so it doesn't get
2203 * spammed by the user's ctl_io.
2205 pool_tmp = io->io_hdr.pool;
2207 memcpy(io, (void *)addr, sizeof(*io));
2209 io->io_hdr.pool = pool_tmp;
2211 * No status yet, so make sure the status is set properly.
2213 io->io_hdr.status = CTL_STATUS_NONE;
2216 * The user sets the initiator ID, target and LUN IDs.
2218 io->io_hdr.nexus.targ_port = softc->ioctl_info.fe.targ_port;
2219 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2220 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2221 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2222 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2224 retval = ctl_ioctl_submit_wait(io);
2231 memcpy((void *)addr, io, sizeof(*io));
2233 /* return this to our pool */
2238 case CTL_ENABLE_PORT:
2239 case CTL_DISABLE_PORT:
2240 case CTL_SET_PORT_WWNS: {
2241 struct ctl_frontend *fe;
2242 struct ctl_port_entry *entry;
2244 entry = (struct ctl_port_entry *)addr;
2246 mtx_lock(&softc->ctl_lock);
2247 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2253 if ((entry->port_type == CTL_PORT_NONE)
2254 && (entry->targ_port == fe->targ_port)) {
2256 * If the user only wants to enable or
2257 * disable or set WWNs on a specific port,
2258 * do the operation and we're done.
2262 } else if (entry->port_type & fe->port_type) {
2264 * Compare the user's type mask with the
2265 * particular frontend type to see if we
2272 * Make sure the user isn't trying to set
2273 * WWNs on multiple ports at the same time.
2275 if (cmd == CTL_SET_PORT_WWNS) {
2276 printf("%s: Can't set WWNs on "
2277 "multiple ports\n", __func__);
2284 * XXX KDM we have to drop the lock here,
2285 * because the online/offline operations
2286 * can potentially block. We need to
2287 * reference count the frontends so they
2290 mtx_unlock(&softc->ctl_lock);
2292 if (cmd == CTL_ENABLE_PORT) {
2293 struct ctl_lun *lun;
2295 STAILQ_FOREACH(lun, &softc->lun_list,
2297 fe->lun_enable(fe->targ_lun_arg,
2302 ctl_frontend_online(fe);
2303 } else if (cmd == CTL_DISABLE_PORT) {
2304 struct ctl_lun *lun;
2306 ctl_frontend_offline(fe);
2308 STAILQ_FOREACH(lun, &softc->lun_list,
2317 mtx_lock(&softc->ctl_lock);
2319 if (cmd == CTL_SET_PORT_WWNS)
2320 ctl_frontend_set_wwns(fe,
2321 (entry->flags & CTL_PORT_WWNN_VALID) ?
2323 (entry->flags & CTL_PORT_WWPN_VALID) ?
2324 1 : 0, entry->wwpn);
2329 mtx_unlock(&softc->ctl_lock);
2332 case CTL_GET_PORT_LIST: {
2333 struct ctl_frontend *fe;
2334 struct ctl_port_list *list;
2337 list = (struct ctl_port_list *)addr;
2339 if (list->alloc_len != (list->alloc_num *
2340 sizeof(struct ctl_port_entry))) {
2341 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2342 "alloc_num %u * sizeof(struct ctl_port_entry) "
2343 "%zu\n", __func__, list->alloc_len,
2344 list->alloc_num, sizeof(struct ctl_port_entry));
2350 list->dropped_num = 0;
2352 mtx_lock(&softc->ctl_lock);
2353 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2354 struct ctl_port_entry entry, *list_entry;
2356 if (list->fill_num >= list->alloc_num) {
2357 list->dropped_num++;
2361 entry.port_type = fe->port_type;
2362 strlcpy(entry.port_name, fe->port_name,
2363 sizeof(entry.port_name));
2364 entry.targ_port = fe->targ_port;
2365 entry.physical_port = fe->physical_port;
2366 entry.virtual_port = fe->virtual_port;
2367 entry.wwnn = fe->wwnn;
2368 entry.wwpn = fe->wwpn;
2369 if (fe->status & CTL_PORT_STATUS_ONLINE)
2374 list_entry = &list->entries[i];
2376 retval = copyout(&entry, list_entry, sizeof(entry));
2378 printf("%s: CTL_GET_PORT_LIST: copyout "
2379 "returned %d\n", __func__, retval);
2384 list->fill_len += sizeof(entry);
2386 mtx_unlock(&softc->ctl_lock);
2389 * If this is non-zero, we had a copyout fault, so there's
2390 * probably no point in attempting to set the status inside
2396 if (list->dropped_num > 0)
2397 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2399 list->status = CTL_PORT_LIST_OK;
2402 case CTL_DUMP_OOA: {
2403 struct ctl_lun *lun;
2408 mtx_lock(&softc->ctl_lock);
2409 printf("Dumping OOA queues:\n");
2410 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2411 for (io = (union ctl_io *)TAILQ_FIRST(
2412 &lun->ooa_queue); io != NULL;
2413 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2415 sbuf_new(&sb, printbuf, sizeof(printbuf),
2417 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2421 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2423 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2425 CTL_FLAG_ABORT) ? " ABORT" : "",
2427 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2428 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2430 printf("%s\n", sbuf_data(&sb));
2433 printf("OOA queues dump done\n");
2434 mtx_unlock(&softc->ctl_lock);
2438 struct ctl_lun *lun;
2439 struct ctl_ooa *ooa_hdr;
2440 struct ctl_ooa_entry *entries;
2441 uint32_t cur_fill_num;
2443 ooa_hdr = (struct ctl_ooa *)addr;
2445 if ((ooa_hdr->alloc_len == 0)
2446 || (ooa_hdr->alloc_num == 0)) {
2447 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2448 "must be non-zero\n", __func__,
2449 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2454 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2455 sizeof(struct ctl_ooa_entry))) {
2456 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2457 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2458 __func__, ooa_hdr->alloc_len,
2459 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2464 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2465 if (entries == NULL) {
2466 printf("%s: could not allocate %d bytes for OOA "
2467 "dump\n", __func__, ooa_hdr->alloc_len);
2472 mtx_lock(&softc->ctl_lock);
2473 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2474 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2475 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2476 mtx_unlock(&softc->ctl_lock);
2477 free(entries, M_CTL);
2478 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2479 __func__, (uintmax_t)ooa_hdr->lun_num);
2486 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2487 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2488 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2494 mtx_unlock(&softc->ctl_lock);
2495 free(entries, M_CTL);
2499 lun = softc->ctl_luns[ooa_hdr->lun_num];
2501 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2504 mtx_unlock(&softc->ctl_lock);
2506 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2507 ooa_hdr->fill_len = ooa_hdr->fill_num *
2508 sizeof(struct ctl_ooa_entry);
2509 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2511 printf("%s: error copying out %d bytes for OOA dump\n",
2512 __func__, ooa_hdr->fill_len);
2515 getbintime(&ooa_hdr->cur_bt);
2517 if (cur_fill_num > ooa_hdr->alloc_num) {
2518 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2519 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2521 ooa_hdr->dropped_num = 0;
2522 ooa_hdr->status = CTL_OOA_OK;
2525 free(entries, M_CTL);
2528 case CTL_CHECK_OOA: {
2530 struct ctl_lun *lun;
2531 struct ctl_ooa_info *ooa_info;
2534 ooa_info = (struct ctl_ooa_info *)addr;
2536 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2537 ooa_info->status = CTL_OOA_INVALID_LUN;
2540 mtx_lock(&softc->ctl_lock);
2541 lun = softc->ctl_luns[ooa_info->lun_id];
2543 mtx_unlock(&softc->ctl_lock);
2544 ooa_info->status = CTL_OOA_INVALID_LUN;
2548 ooa_info->num_entries = 0;
2549 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2550 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2551 &io->io_hdr, ooa_links)) {
2552 ooa_info->num_entries++;
2555 mtx_unlock(&softc->ctl_lock);
2556 ooa_info->status = CTL_OOA_SUCCESS;
2560 case CTL_HARD_START:
2561 case CTL_HARD_STOP: {
2562 struct ctl_fe_ioctl_startstop_info ss_info;
2563 struct cfi_metatask *metatask;
2566 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2568 cv_init(&ss_info.sem, "hard start/stop cv" );
2570 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2571 if (metatask == NULL) {
2573 mtx_destroy(&hs_mtx);
2577 if (cmd == CTL_HARD_START)
2578 metatask->tasktype = CFI_TASK_STARTUP;
2580 metatask->tasktype = CFI_TASK_SHUTDOWN;
2582 metatask->callback = ctl_ioctl_hard_startstop_callback;
2583 metatask->callback_arg = &ss_info;
2585 cfi_action(metatask);
2587 /* Wait for the callback */
2589 cv_wait_sig(&ss_info.sem, &hs_mtx);
2590 mtx_unlock(&hs_mtx);
2593 * All information has been copied from the metatask by the
2594 * time cv_broadcast() is called, so we free the metatask here.
2596 cfi_free_metatask(metatask);
2598 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2600 mtx_destroy(&hs_mtx);
2604 struct ctl_bbrread_info *bbr_info;
2605 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2607 struct cfi_metatask *metatask;
2609 bbr_info = (struct ctl_bbrread_info *)addr;
2611 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2613 bzero(&bbr_mtx, sizeof(bbr_mtx));
2614 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2616 fe_bbr_info.bbr_info = bbr_info;
2617 fe_bbr_info.lock = &bbr_mtx;
2619 cv_init(&fe_bbr_info.sem, "BBR read cv");
2620 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2622 if (metatask == NULL) {
2623 mtx_destroy(&bbr_mtx);
2624 cv_destroy(&fe_bbr_info.sem);
2628 metatask->tasktype = CFI_TASK_BBRREAD;
2629 metatask->callback = ctl_ioctl_bbrread_callback;
2630 metatask->callback_arg = &fe_bbr_info;
2631 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2632 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2633 metatask->taskinfo.bbrread.len = bbr_info->len;
2635 cfi_action(metatask);
2638 while (fe_bbr_info.wakeup_done == 0)
2639 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2640 mtx_unlock(&bbr_mtx);
2642 bbr_info->status = metatask->status;
2643 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2644 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2645 memcpy(&bbr_info->sense_data,
2646 &metatask->taskinfo.bbrread.sense_data,
2647 ctl_min(sizeof(bbr_info->sense_data),
2648 sizeof(metatask->taskinfo.bbrread.sense_data)));
2650 cfi_free_metatask(metatask);
2652 mtx_destroy(&bbr_mtx);
2653 cv_destroy(&fe_bbr_info.sem);
2657 case CTL_DELAY_IO: {
2658 struct ctl_io_delay_info *delay_info;
2660 struct ctl_lun *lun;
2661 #endif /* CTL_IO_DELAY */
2663 delay_info = (struct ctl_io_delay_info *)addr;
2666 mtx_lock(&softc->ctl_lock);
2668 if ((delay_info->lun_id > CTL_MAX_LUNS)
2669 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2670 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2672 lun = softc->ctl_luns[delay_info->lun_id];
2674 delay_info->status = CTL_DELAY_STATUS_OK;
2676 switch (delay_info->delay_type) {
2677 case CTL_DELAY_TYPE_CONT:
2679 case CTL_DELAY_TYPE_ONESHOT:
2682 delay_info->status =
2683 CTL_DELAY_STATUS_INVALID_TYPE;
2687 switch (delay_info->delay_loc) {
2688 case CTL_DELAY_LOC_DATAMOVE:
2689 lun->delay_info.datamove_type =
2690 delay_info->delay_type;
2691 lun->delay_info.datamove_delay =
2692 delay_info->delay_secs;
2694 case CTL_DELAY_LOC_DONE:
2695 lun->delay_info.done_type =
2696 delay_info->delay_type;
2697 lun->delay_info.done_delay =
2698 delay_info->delay_secs;
2701 delay_info->status =
2702 CTL_DELAY_STATUS_INVALID_LOC;
2707 mtx_unlock(&softc->ctl_lock);
2709 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2710 #endif /* CTL_IO_DELAY */
2713 case CTL_REALSYNC_SET: {
2716 syncstate = (int *)addr;
2718 mtx_lock(&softc->ctl_lock);
2719 switch (*syncstate) {
2721 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2724 softc->flags |= CTL_FLAG_REAL_SYNC;
2730 mtx_unlock(&softc->ctl_lock);
2733 case CTL_REALSYNC_GET: {
2736 syncstate = (int*)addr;
2738 mtx_lock(&softc->ctl_lock);
2739 if (softc->flags & CTL_FLAG_REAL_SYNC)
2743 mtx_unlock(&softc->ctl_lock);
2749 struct ctl_sync_info *sync_info;
2750 struct ctl_lun *lun;
2752 sync_info = (struct ctl_sync_info *)addr;
2754 mtx_lock(&softc->ctl_lock);
2755 lun = softc->ctl_luns[sync_info->lun_id];
2757 mtx_unlock(&softc->ctl_lock);
2758 sync_info->status = CTL_GS_SYNC_NO_LUN;
2761 * Get or set the sync interval. We're not bounds checking
2762 * in the set case, hopefully the user won't do something
2765 if (cmd == CTL_GETSYNC)
2766 sync_info->sync_interval = lun->sync_interval;
2768 lun->sync_interval = sync_info->sync_interval;
2770 mtx_unlock(&softc->ctl_lock);
2772 sync_info->status = CTL_GS_SYNC_OK;
2776 case CTL_GETSTATS: {
2777 struct ctl_stats *stats;
2778 struct ctl_lun *lun;
2781 stats = (struct ctl_stats *)addr;
2783 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2785 stats->status = CTL_SS_NEED_MORE_SPACE;
2786 stats->num_luns = softc->num_luns;
2790 * XXX KDM no locking here. If the LUN list changes,
2791 * things can blow up.
2793 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2794 i++, lun = STAILQ_NEXT(lun, links)) {
2795 retval = copyout(&lun->stats, &stats->lun_stats[i],
2796 sizeof(lun->stats));
2800 stats->num_luns = softc->num_luns;
2801 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2803 stats->status = CTL_SS_OK;
2805 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2807 stats->flags = CTL_STATS_FLAG_NONE;
2809 getnanouptime(&stats->timestamp);
2812 case CTL_ERROR_INJECT: {
2813 struct ctl_error_desc *err_desc, *new_err_desc;
2814 struct ctl_lun *lun;
2816 err_desc = (struct ctl_error_desc *)addr;
2818 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2820 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2822 mtx_lock(&softc->ctl_lock);
2823 lun = softc->ctl_luns[err_desc->lun_id];
2825 mtx_unlock(&softc->ctl_lock);
2826 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2827 __func__, (uintmax_t)err_desc->lun_id);
2833 * We could do some checking here to verify the validity
2834 * of the request, but given the complexity of error
2835 * injection requests, the checking logic would be fairly
2838 * For now, if the request is invalid, it just won't get
2839 * executed and might get deleted.
2841 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2844 * XXX KDM check to make sure the serial number is unique,
2845 * in case we somehow manage to wrap. That shouldn't
2846 * happen for a very long time, but it's the right thing to
2849 new_err_desc->serial = lun->error_serial;
2850 err_desc->serial = lun->error_serial;
2851 lun->error_serial++;
2853 mtx_unlock(&softc->ctl_lock);
2856 case CTL_ERROR_INJECT_DELETE: {
2857 struct ctl_error_desc *delete_desc, *desc, *desc2;
2858 struct ctl_lun *lun;
2861 delete_desc = (struct ctl_error_desc *)addr;
2864 mtx_lock(&softc->ctl_lock);
2865 lun = softc->ctl_luns[delete_desc->lun_id];
2867 mtx_unlock(&softc->ctl_lock);
2868 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2869 __func__, (uintmax_t)delete_desc->lun_id);
2873 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2874 if (desc->serial != delete_desc->serial)
2877 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2882 mtx_unlock(&softc->ctl_lock);
2883 if (delete_done == 0) {
2884 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2885 "error serial %ju on LUN %u\n", __func__,
2886 delete_desc->serial, delete_desc->lun_id);
2892 case CTL_DUMP_STRUCTS: {
2894 struct ctl_frontend *fe;
2896 printf("CTL IID to WWPN map start:\n");
2897 for (i = 0; i < CTL_MAX_PORTS; i++) {
2898 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2899 if (softc->wwpn_iid[i][j].in_use == 0)
2902 printf("port %d iid %u WWPN %#jx\n",
2903 softc->wwpn_iid[i][j].port,
2904 softc->wwpn_iid[i][j].iid,
2905 (uintmax_t)softc->wwpn_iid[i][j].wwpn);
2908 printf("CTL IID to WWPN map end\n");
2909 printf("CTL Persistent Reservation information start:\n");
2910 for (i = 0; i < CTL_MAX_LUNS; i++) {
2911 struct ctl_lun *lun;
2913 lun = softc->ctl_luns[i];
2916 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2919 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2920 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2921 if (lun->per_res[j+k].registered == 0)
2923 printf("LUN %d port %d iid %d key "
2925 (uintmax_t)scsi_8btou64(
2926 lun->per_res[j+k].res_key.key));
2930 printf("CTL Persistent Reservation information end\n");
2931 printf("CTL Frontends:\n");
2933 * XXX KDM calling this without a lock. We'd likely want
2934 * to drop the lock before calling the frontend's dump
2937 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2938 printf("Frontend %s Type %u pport %d vport %d WWNN "
2939 "%#jx WWPN %#jx\n", fe->port_name, fe->port_type,
2940 fe->physical_port, fe->virtual_port,
2941 (uintmax_t)fe->wwnn, (uintmax_t)fe->wwpn);
2944 * Frontends are not required to support the dump
2947 if (fe->fe_dump == NULL)
2952 printf("CTL Frontend information end\n");
2956 struct ctl_lun_req *lun_req;
2957 struct ctl_backend_driver *backend;
2959 lun_req = (struct ctl_lun_req *)addr;
2961 backend = ctl_backend_find(lun_req->backend);
2962 if (backend == NULL) {
2963 lun_req->status = CTL_LUN_ERROR;
2964 snprintf(lun_req->error_str,
2965 sizeof(lun_req->error_str),
2966 "Backend \"%s\" not found.",
2970 if (lun_req->num_be_args > 0) {
2971 lun_req->kern_be_args = ctl_copyin_args(
2972 lun_req->num_be_args,
2975 sizeof(lun_req->error_str));
2976 if (lun_req->kern_be_args == NULL) {
2977 lun_req->status = CTL_LUN_ERROR;
2982 retval = backend->ioctl(dev, cmd, addr, flag, td);
2984 if (lun_req->num_be_args > 0) {
2985 ctl_free_args(lun_req->num_be_args,
2986 lun_req->kern_be_args);
2990 case CTL_LUN_LIST: {
2992 struct ctl_lun *lun;
2993 struct ctl_lun_list *list;
2994 struct ctl_be_lun_option *opt;
2996 list = (struct ctl_lun_list *)addr;
2999 * Allocate a fixed length sbuf here, based on the length
3000 * of the user's buffer. We could allocate an auto-extending
3001 * buffer, and then tell the user how much larger our
3002 * amount of data is than his buffer, but that presents
3005 * 1. The sbuf(9) routines use a blocking malloc, and so
3006 * we can't hold a lock while calling them with an
3007 * auto-extending buffer.
3009 * 2. There is not currently a LUN reference counting
3010 * mechanism, outside of outstanding transactions on
3011 * the LUN's OOA queue. So a LUN could go away on us
3012 * while we're getting the LUN number, backend-specific
3013 * information, etc. Thus, given the way things
3014 * currently work, we need to hold the CTL lock while
3015 * grabbing LUN information.
3017 * So, from the user's standpoint, the best thing to do is
3018 * allocate what he thinks is a reasonable buffer length,
3019 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3020 * double the buffer length and try again. (And repeat
3021 * that until he succeeds.)
3023 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3025 list->status = CTL_LUN_LIST_ERROR;
3026 snprintf(list->error_str, sizeof(list->error_str),
3027 "Unable to allocate %d bytes for LUN list",
3032 sbuf_printf(sb, "<ctllunlist>\n");
3034 mtx_lock(&softc->ctl_lock);
3036 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3037 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3038 (uintmax_t)lun->lun);
3041 * Bail out as soon as we see that we've overfilled
3047 retval = sbuf_printf(sb, "<backend_type>%s"
3048 "</backend_type>\n",
3049 (lun->backend == NULL) ? "none" :
3050 lun->backend->name);
3055 retval = sbuf_printf(sb, "<lun_type>%d</lun_type>\n",
3056 lun->be_lun->lun_type);
3061 if (lun->backend == NULL) {
3062 retval = sbuf_printf(sb, "</lun>\n");
3068 retval = sbuf_printf(sb, "<size>%ju</size>\n",
3069 (lun->be_lun->maxlba > 0) ?
3070 lun->be_lun->maxlba + 1 : 0);
3075 retval = sbuf_printf(sb, "<blocksize>%u</blocksize>\n",
3076 lun->be_lun->blocksize);
3081 retval = sbuf_printf(sb, "<serial_number>");
3086 retval = ctl_sbuf_printf_esc(sb,
3087 lun->be_lun->serial_num);
3092 retval = sbuf_printf(sb, "</serial_number>\n");
3097 retval = sbuf_printf(sb, "<device_id>");
3102 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3107 retval = sbuf_printf(sb, "</device_id>\n");
3112 if (lun->backend->lun_info != NULL) {
3113 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3117 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3118 retval = sbuf_printf(sb, "<%s>%s</%s>", opt->name, opt->value, opt->name);
3123 retval = sbuf_printf(sb, "</lun>\n");
3128 mtx_unlock(&softc->ctl_lock);
3131 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3134 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3135 snprintf(list->error_str, sizeof(list->error_str),
3136 "Out of space, %d bytes is too small",
3143 retval = copyout(sbuf_data(sb), list->lun_xml,
3146 list->fill_len = sbuf_len(sb) + 1;
3147 list->status = CTL_LUN_LIST_OK;
3152 struct ctl_iscsi *ci;
3153 struct ctl_frontend *fe;
3155 ci = (struct ctl_iscsi *)addr;
3157 mtx_lock(&softc->ctl_lock);
3158 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3159 if (strcmp(fe->port_name, "iscsi") == 0)
3162 mtx_unlock(&softc->ctl_lock);
3165 ci->status = CTL_ISCSI_ERROR;
3166 snprintf(ci->error_str, sizeof(ci->error_str), "Backend \"iscsi\" not found.");
3170 retval = fe->ioctl(dev, cmd, addr, flag, td);
3174 /* XXX KDM should we fix this? */
3176 struct ctl_backend_driver *backend;
3183 * We encode the backend type as the ioctl type for backend
3184 * ioctls. So parse it out here, and then search for a
3185 * backend of this type.
3187 type = _IOC_TYPE(cmd);
3189 STAILQ_FOREACH(backend, &softc->be_list, links) {
3190 if (backend->type == type) {
3196 printf("ctl: unknown ioctl command %#lx or backend "
3201 retval = backend->ioctl(dev, cmd, addr, flag, td);
3211 ctl_get_initindex(struct ctl_nexus *nexus)
3213 if (nexus->targ_port < CTL_MAX_PORTS)
3214 return (nexus->initid.id +
3215 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3217 return (nexus->initid.id +
3218 ((nexus->targ_port - CTL_MAX_PORTS) *
3219 CTL_MAX_INIT_PER_PORT));
3223 ctl_get_resindex(struct ctl_nexus *nexus)
3225 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3229 ctl_port_idx(int port_num)
3231 if (port_num < CTL_MAX_PORTS)
3234 return(port_num - CTL_MAX_PORTS);
3238 * Note: This only works for bitmask sizes that are at least 32 bits, and
3239 * that are a power of 2.
3242 ctl_ffz(uint32_t *mask, uint32_t size)
3244 uint32_t num_chunks, num_pieces;
3247 num_chunks = (size >> 5);
3248 if (num_chunks == 0)
3250 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3252 for (i = 0; i < num_chunks; i++) {
3253 for (j = 0; j < num_pieces; j++) {
3254 if ((mask[i] & (1 << j)) == 0)
3255 return ((i << 5) + j);
3263 ctl_set_mask(uint32_t *mask, uint32_t bit)
3265 uint32_t chunk, piece;
3268 piece = bit % (sizeof(uint32_t) * 8);
3270 if ((mask[chunk] & (1 << piece)) != 0)
3273 mask[chunk] |= (1 << piece);
3279 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3281 uint32_t chunk, piece;
3284 piece = bit % (sizeof(uint32_t) * 8);
3286 if ((mask[chunk] & (1 << piece)) == 0)
3289 mask[chunk] &= ~(1 << piece);
3295 ctl_is_set(uint32_t *mask, uint32_t bit)
3297 uint32_t chunk, piece;
3300 piece = bit % (sizeof(uint32_t) * 8);
3302 if ((mask[chunk] & (1 << piece)) == 0)
3310 * The bus, target and lun are optional, they can be filled in later.
3311 * can_wait is used to determine whether we can wait on the malloc or not.
3314 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3315 uint32_t targ_lun, int can_wait)
3320 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3322 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3325 io->io_hdr.io_type = io_type;
3326 io->io_hdr.targ_port = targ_port;
3328 * XXX KDM this needs to change/go away. We need to move
3329 * to a preallocated pool of ctl_scsiio structures.
3331 io->io_hdr.nexus.targ_target.id = targ_target;
3332 io->io_hdr.nexus.targ_lun = targ_lun;
3339 ctl_kfree_io(union ctl_io *io)
3346 * ctl_softc, pool_type, total_ctl_io are passed in.
3347 * npool is passed out.
3350 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3351 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3354 union ctl_io *cur_io, *next_io;
3355 struct ctl_io_pool *pool;
3360 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3367 pool->type = pool_type;
3368 pool->ctl_softc = ctl_softc;
3370 mtx_lock(&ctl_softc->ctl_lock);
3371 pool->id = ctl_softc->cur_pool_id++;
3372 mtx_unlock(&ctl_softc->ctl_lock);
3374 pool->flags = CTL_POOL_FLAG_NONE;
3375 STAILQ_INIT(&pool->free_queue);
3378 * XXX KDM other options here:
3379 * - allocate a page at a time
3380 * - allocate one big chunk of memory.
3381 * Page allocation might work well, but would take a little more
3384 for (i = 0; i < total_ctl_io; i++) {
3385 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTL,
3387 if (cur_io == NULL) {
3391 cur_io->io_hdr.pool = pool;
3392 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3393 pool->total_ctl_io++;
3394 pool->free_ctl_io++;
3398 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3399 cur_io != NULL; cur_io = next_io) {
3400 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3402 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3404 free(cur_io, M_CTL);
3410 mtx_lock(&ctl_softc->ctl_lock);
3411 ctl_softc->num_pools++;
3412 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3414 * Increment our usage count if this is an external consumer, so we
3415 * can't get unloaded until the external consumer (most likely a
3416 * FETD) unloads and frees his pool.
3418 * XXX KDM will this increment the caller's module use count, or
3422 if ((pool_type != CTL_POOL_EMERGENCY)
3423 && (pool_type != CTL_POOL_INTERNAL)
3424 && (pool_type != CTL_POOL_IOCTL)
3425 && (pool_type != CTL_POOL_4OTHERSC))
3429 mtx_unlock(&ctl_softc->ctl_lock);
3439 ctl_pool_acquire(struct ctl_io_pool *pool)
3442 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
3447 if (pool->flags & CTL_POOL_FLAG_INVALID)
3456 ctl_pool_invalidate(struct ctl_io_pool *pool)
3459 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
3464 pool->flags |= CTL_POOL_FLAG_INVALID;
3470 ctl_pool_release(struct ctl_io_pool *pool)
3473 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
3478 if ((--pool->refcount == 0)
3479 && (pool->flags & CTL_POOL_FLAG_INVALID)) {
3480 ctl_pool_free(pool->ctl_softc, pool);
3487 ctl_pool_free(struct ctl_softc *ctl_softc, struct ctl_io_pool *pool)
3489 union ctl_io *cur_io, *next_io;
3491 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
3493 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3494 cur_io != NULL; cur_io = next_io) {
3495 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3497 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr, ctl_io_hdr,
3499 free(cur_io, M_CTL);
3502 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3503 ctl_softc->num_pools--;
3506 * XXX KDM will this decrement the caller's usage count or mine?
3509 if ((pool->type != CTL_POOL_EMERGENCY)
3510 && (pool->type != CTL_POOL_INTERNAL)
3511 && (pool->type != CTL_POOL_IOCTL))
3519 * This routine does not block (except for spinlocks of course).
3520 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3524 ctl_alloc_io(void *pool_ref)
3527 struct ctl_softc *ctl_softc;
3528 struct ctl_io_pool *pool, *npool;
3529 struct ctl_io_pool *emergency_pool;
3531 pool = (struct ctl_io_pool *)pool_ref;
3534 printf("%s: pool is NULL\n", __func__);
3538 emergency_pool = NULL;
3540 ctl_softc = pool->ctl_softc;
3542 mtx_lock(&ctl_softc->ctl_lock);
3544 * First, try to get the io structure from the user's pool.
3546 if (ctl_pool_acquire(pool) == 0) {
3547 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3549 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3550 pool->total_allocated++;
3551 pool->free_ctl_io--;
3552 mtx_unlock(&ctl_softc->ctl_lock);
3555 ctl_pool_release(pool);
3558 * If he doesn't have any io structures left, search for an
3559 * emergency pool and grab one from there.
3561 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3562 if (npool->type != CTL_POOL_EMERGENCY)
3565 if (ctl_pool_acquire(npool) != 0)
3568 emergency_pool = npool;
3570 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3572 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3573 npool->total_allocated++;
3574 npool->free_ctl_io--;
3575 mtx_unlock(&ctl_softc->ctl_lock);
3578 ctl_pool_release(npool);
3581 /* Drop the spinlock before we malloc */
3582 mtx_unlock(&ctl_softc->ctl_lock);
3585 * The emergency pool (if it exists) didn't have one, so try an
3586 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3588 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3591 * If the emergency pool exists but is empty, add this
3592 * ctl_io to its list when it gets freed.
3594 if (emergency_pool != NULL) {
3595 mtx_lock(&ctl_softc->ctl_lock);
3596 if (ctl_pool_acquire(emergency_pool) == 0) {
3597 io->io_hdr.pool = emergency_pool;
3598 emergency_pool->total_ctl_io++;
3600 * Need to bump this, otherwise
3601 * total_allocated and total_freed won't
3602 * match when we no longer have anything
3605 emergency_pool->total_allocated++;
3607 mtx_unlock(&ctl_softc->ctl_lock);
3609 io->io_hdr.pool = NULL;
3616 ctl_free_io_internal(union ctl_io *io, int have_lock)
3622 * If this ctl_io has a pool, return it to that pool.
3624 if (io->io_hdr.pool != NULL) {
3625 struct ctl_io_pool *pool;
3627 struct ctl_softc *ctl_softc;
3628 union ctl_io *tmp_io;
3629 unsigned long xflags;
3632 ctl_softc = control_softc;
3635 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3638 mtx_lock(&pool->ctl_softc->ctl_lock);
3642 for (i = 0, tmp_io = (union ctl_io *)STAILQ_FIRST(
3643 &ctl_softc->task_queue); tmp_io != NULL; i++,
3644 tmp_io = (union ctl_io *)STAILQ_NEXT(&tmp_io->io_hdr,
3647 printf("%s: %p is still on the task queue!\n",
3649 printf("%s: (%d): type %d "
3650 "msg %d cdb %x iptl: "
3651 "%d:%d:%d:%d tag 0x%04x "
3654 tmp_io->io_hdr.io_type,
3655 tmp_io->io_hdr.msg_type,
3656 tmp_io->scsiio.cdb[0],
3657 tmp_io->io_hdr.nexus.initid.id,
3658 tmp_io->io_hdr.nexus.targ_port,
3659 tmp_io->io_hdr.nexus.targ_target.id,
3660 tmp_io->io_hdr.nexus.targ_lun,
3661 (tmp_io->io_hdr.io_type ==
3663 tmp_io->taskio.tag_num :
3664 tmp_io->scsiio.tag_num,
3666 panic("I/O still on the task queue!");
3670 io->io_hdr.io_type = 0xff;
3671 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3672 pool->total_freed++;
3673 pool->free_ctl_io++;
3674 ctl_pool_release(pool);
3676 mtx_unlock(&pool->ctl_softc->ctl_lock);
3679 * Otherwise, just free it. We probably malloced it and
3680 * the emergency pool wasn't available.
3688 ctl_free_io(union ctl_io *io)
3690 ctl_free_io_internal(io, /*have_lock*/ 0);
3694 ctl_zero_io(union ctl_io *io)
3702 * May need to preserve linked list pointers at some point too.
3704 pool_ref = io->io_hdr.pool;
3706 memset(io, 0, sizeof(*io));
3708 io->io_hdr.pool = pool_ref;
3712 * This routine is currently used for internal copies of ctl_ios that need
3713 * to persist for some reason after we've already returned status to the
3714 * FETD. (Thus the flag set.)
3717 * Note that this makes a blind copy of all fields in the ctl_io, except
3718 * for the pool reference. This includes any memory that has been
3719 * allocated! That memory will no longer be valid after done has been
3720 * called, so this would be VERY DANGEROUS for command that actually does
3721 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3722 * start and stop commands, which don't transfer any data, so this is not a
3723 * problem. If it is used for anything else, the caller would also need to
3724 * allocate data buffer space and this routine would need to be modified to
3725 * copy the data buffer(s) as well.
3728 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3737 * May need to preserve linked list pointers at some point too.
3739 pool_ref = dest->io_hdr.pool;
3741 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3743 dest->io_hdr.pool = pool_ref;
3745 * We need to know that this is an internal copy, and doesn't need
3746 * to get passed back to the FETD that allocated it.
3748 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3753 ctl_update_power_subpage(struct copan_power_subpage *page)
3755 int num_luns, num_partitions, config_type;
3756 struct ctl_softc *softc;
3757 cs_BOOL_t aor_present, shelf_50pct_power;
3758 cs_raidset_personality_t rs_type;
3759 int max_active_luns;
3761 softc = control_softc;
3763 /* subtract out the processor LUN */
3764 num_luns = softc->num_luns - 1;
3766 * Default to 7 LUNs active, which was the only number we allowed
3769 max_active_luns = 7;
3771 num_partitions = config_GetRsPartitionInfo();
3772 config_type = config_GetConfigType();
3773 shelf_50pct_power = config_GetShelfPowerMode();
3774 aor_present = config_IsAorRsPresent();
3776 rs_type = ddb_GetRsRaidType(1);
3777 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3778 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3779 EPRINT(0, "Unsupported RS type %d!", rs_type);
3783 page->total_luns = num_luns;
3785 switch (config_type) {
3788 * In a 40 drive configuration, it doesn't matter what DC
3789 * cards we have, whether we have AOR enabled or not,
3790 * partitioning or not, or what type of RAIDset we have.
3791 * In that scenario, we can power up every LUN we present
3794 max_active_luns = num_luns;
3798 if (shelf_50pct_power == CS_FALSE) {
3800 if (aor_present == CS_TRUE) {
3802 CS_RAIDSET_PERSONALITY_RAID5) {
3803 max_active_luns = 7;
3804 } else if (rs_type ==
3805 CS_RAIDSET_PERSONALITY_RAID1){
3806 max_active_luns = 14;
3808 /* XXX KDM now what?? */
3812 CS_RAIDSET_PERSONALITY_RAID5) {
3813 max_active_luns = 8;
3814 } else if (rs_type ==
3815 CS_RAIDSET_PERSONALITY_RAID1){
3816 max_active_luns = 16;
3818 /* XXX KDM now what?? */
3824 * With 50% power in a 64 drive configuration, we
3825 * can power all LUNs we present.
3827 max_active_luns = num_luns;
3831 if (shelf_50pct_power == CS_FALSE) {
3833 if (aor_present == CS_TRUE) {
3835 CS_RAIDSET_PERSONALITY_RAID5) {
3836 max_active_luns = 7;
3837 } else if (rs_type ==
3838 CS_RAIDSET_PERSONALITY_RAID1){
3839 max_active_luns = 14;
3841 /* XXX KDM now what?? */
3845 CS_RAIDSET_PERSONALITY_RAID5) {
3846 max_active_luns = 8;
3847 } else if (rs_type ==
3848 CS_RAIDSET_PERSONALITY_RAID1){
3849 max_active_luns = 16;
3851 /* XXX KDM now what?? */
3856 if (aor_present == CS_TRUE) {
3858 CS_RAIDSET_PERSONALITY_RAID5) {
3859 max_active_luns = 14;
3860 } else if (rs_type ==
3861 CS_RAIDSET_PERSONALITY_RAID1){
3863 * We're assuming here that disk
3864 * caching is enabled, and so we're
3865 * able to power up half of each
3866 * LUN, and cache all writes.
3868 max_active_luns = num_luns;
3870 /* XXX KDM now what?? */
3874 CS_RAIDSET_PERSONALITY_RAID5) {
3875 max_active_luns = 15;
3876 } else if (rs_type ==
3877 CS_RAIDSET_PERSONALITY_RAID1){
3878 max_active_luns = 30;
3880 /* XXX KDM now what?? */
3887 * In this case, we have an unknown configuration, so we
3888 * just use the default from above.
3893 page->max_active_luns = max_active_luns;
3895 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
3896 page->total_luns, page->max_active_luns);
3899 #endif /* NEEDTOPORT */
3902 * This routine could be used in the future to load default and/or saved
3903 * mode page parameters for a particuar lun.
3906 ctl_init_page_index(struct ctl_lun *lun)
3909 struct ctl_page_index *page_index;
3910 struct ctl_softc *softc;
3912 memcpy(&lun->mode_pages.index, page_index_template,
3913 sizeof(page_index_template));
3915 softc = lun->ctl_softc;
3917 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3919 page_index = &lun->mode_pages.index[i];
3921 * If this is a disk-only mode page, there's no point in
3922 * setting it up. For some pages, we have to have some
3923 * basic information about the disk in order to calculate the
3926 if ((lun->be_lun->lun_type != T_DIRECT)
3927 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
3930 switch (page_index->page_code & SMPH_PC_MASK) {
3931 case SMS_FORMAT_DEVICE_PAGE: {
3932 struct scsi_format_page *format_page;
3934 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3935 panic("subpage is incorrect!");
3938 * Sectors per track are set above. Bytes per
3939 * sector need to be set here on a per-LUN basis.
3941 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3942 &format_page_default,
3943 sizeof(format_page_default));
3944 memcpy(&lun->mode_pages.format_page[
3945 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3946 sizeof(format_page_changeable));
3947 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3948 &format_page_default,
3949 sizeof(format_page_default));
3950 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3951 &format_page_default,
3952 sizeof(format_page_default));
3954 format_page = &lun->mode_pages.format_page[
3956 scsi_ulto2b(lun->be_lun->blocksize,
3957 format_page->bytes_per_sector);
3959 format_page = &lun->mode_pages.format_page[
3961 scsi_ulto2b(lun->be_lun->blocksize,
3962 format_page->bytes_per_sector);
3964 format_page = &lun->mode_pages.format_page[
3966 scsi_ulto2b(lun->be_lun->blocksize,
3967 format_page->bytes_per_sector);
3969 page_index->page_data =
3970 (uint8_t *)lun->mode_pages.format_page;
3973 case SMS_RIGID_DISK_PAGE: {
3974 struct scsi_rigid_disk_page *rigid_disk_page;
3975 uint32_t sectors_per_cylinder;
3979 #endif /* !__XSCALE__ */
3981 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3982 panic("invalid subpage value %d",
3983 page_index->subpage);
3986 * Rotation rate and sectors per track are set
3987 * above. We calculate the cylinders here based on
3988 * capacity. Due to the number of heads and
3989 * sectors per track we're using, smaller arrays
3990 * may turn out to have 0 cylinders. Linux and
3991 * FreeBSD don't pay attention to these mode pages
3992 * to figure out capacity, but Solaris does. It
3993 * seems to deal with 0 cylinders just fine, and
3994 * works out a fake geometry based on the capacity.
3996 memcpy(&lun->mode_pages.rigid_disk_page[
3997 CTL_PAGE_CURRENT], &rigid_disk_page_default,
3998 sizeof(rigid_disk_page_default));
3999 memcpy(&lun->mode_pages.rigid_disk_page[
4000 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4001 sizeof(rigid_disk_page_changeable));
4002 memcpy(&lun->mode_pages.rigid_disk_page[
4003 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4004 sizeof(rigid_disk_page_default));
4005 memcpy(&lun->mode_pages.rigid_disk_page[
4006 CTL_PAGE_SAVED], &rigid_disk_page_default,
4007 sizeof(rigid_disk_page_default));
4009 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4013 * The divide method here will be more accurate,
4014 * probably, but results in floating point being
4015 * used in the kernel on i386 (__udivdi3()). On the
4016 * XScale, though, __udivdi3() is implemented in
4019 * The shift method for cylinder calculation is
4020 * accurate if sectors_per_cylinder is a power of
4021 * 2. Otherwise it might be slightly off -- you
4022 * might have a bit of a truncation problem.
4025 cylinders = (lun->be_lun->maxlba + 1) /
4026 sectors_per_cylinder;
4028 for (shift = 31; shift > 0; shift--) {
4029 if (sectors_per_cylinder & (1 << shift))
4032 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4036 * We've basically got 3 bytes, or 24 bits for the
4037 * cylinder size in the mode page. If we're over,
4038 * just round down to 2^24.
4040 if (cylinders > 0xffffff)
4041 cylinders = 0xffffff;
4043 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4045 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4047 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4049 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4051 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4053 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4055 page_index->page_data =
4056 (uint8_t *)lun->mode_pages.rigid_disk_page;
4059 case SMS_CACHING_PAGE: {
4061 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4062 panic("invalid subpage value %d",
4063 page_index->subpage);
4065 * Defaults should be okay here, no calculations
4068 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4069 &caching_page_default,
4070 sizeof(caching_page_default));
4071 memcpy(&lun->mode_pages.caching_page[
4072 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4073 sizeof(caching_page_changeable));
4074 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4075 &caching_page_default,
4076 sizeof(caching_page_default));
4077 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4078 &caching_page_default,
4079 sizeof(caching_page_default));
4080 page_index->page_data =
4081 (uint8_t *)lun->mode_pages.caching_page;
4084 case SMS_CONTROL_MODE_PAGE: {
4086 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4087 panic("invalid subpage value %d",
4088 page_index->subpage);
4091 * Defaults should be okay here, no calculations
4094 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4095 &control_page_default,
4096 sizeof(control_page_default));
4097 memcpy(&lun->mode_pages.control_page[
4098 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4099 sizeof(control_page_changeable));
4100 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4101 &control_page_default,
4102 sizeof(control_page_default));
4103 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4104 &control_page_default,
4105 sizeof(control_page_default));
4106 page_index->page_data =
4107 (uint8_t *)lun->mode_pages.control_page;
4111 case SMS_VENDOR_SPECIFIC_PAGE:{
4112 switch (page_index->subpage) {
4113 case PWR_SUBPAGE_CODE: {
4114 struct copan_power_subpage *current_page,
4117 memcpy(&lun->mode_pages.power_subpage[
4119 &power_page_default,
4120 sizeof(power_page_default));
4121 memcpy(&lun->mode_pages.power_subpage[
4122 CTL_PAGE_CHANGEABLE],
4123 &power_page_changeable,
4124 sizeof(power_page_changeable));
4125 memcpy(&lun->mode_pages.power_subpage[
4127 &power_page_default,
4128 sizeof(power_page_default));
4129 memcpy(&lun->mode_pages.power_subpage[
4131 &power_page_default,
4132 sizeof(power_page_default));
4133 page_index->page_data =
4134 (uint8_t *)lun->mode_pages.power_subpage;
4136 current_page = (struct copan_power_subpage *)
4137 (page_index->page_data +
4138 (page_index->page_len *
4140 saved_page = (struct copan_power_subpage *)
4141 (page_index->page_data +
4142 (page_index->page_len *
4146 case APS_SUBPAGE_CODE: {
4147 struct copan_aps_subpage *current_page,
4150 // This gets set multiple times but
4151 // it should always be the same. It's
4152 // only done during init so who cares.
4153 index_to_aps_page = i;
4155 memcpy(&lun->mode_pages.aps_subpage[
4158 sizeof(aps_page_default));
4159 memcpy(&lun->mode_pages.aps_subpage[
4160 CTL_PAGE_CHANGEABLE],
4161 &aps_page_changeable,
4162 sizeof(aps_page_changeable));
4163 memcpy(&lun->mode_pages.aps_subpage[
4166 sizeof(aps_page_default));
4167 memcpy(&lun->mode_pages.aps_subpage[
4170 sizeof(aps_page_default));
4171 page_index->page_data =
4172 (uint8_t *)lun->mode_pages.aps_subpage;
4174 current_page = (struct copan_aps_subpage *)
4175 (page_index->page_data +
4176 (page_index->page_len *
4178 saved_page = (struct copan_aps_subpage *)
4179 (page_index->page_data +
4180 (page_index->page_len *
4184 case DBGCNF_SUBPAGE_CODE: {
4185 struct copan_debugconf_subpage *current_page,
4188 memcpy(&lun->mode_pages.debugconf_subpage[
4190 &debugconf_page_default,
4191 sizeof(debugconf_page_default));
4192 memcpy(&lun->mode_pages.debugconf_subpage[
4193 CTL_PAGE_CHANGEABLE],
4194 &debugconf_page_changeable,
4195 sizeof(debugconf_page_changeable));
4196 memcpy(&lun->mode_pages.debugconf_subpage[
4198 &debugconf_page_default,
4199 sizeof(debugconf_page_default));
4200 memcpy(&lun->mode_pages.debugconf_subpage[
4202 &debugconf_page_default,
4203 sizeof(debugconf_page_default));
4204 page_index->page_data =
4205 (uint8_t *)lun->mode_pages.debugconf_subpage;
4207 current_page = (struct copan_debugconf_subpage *)
4208 (page_index->page_data +
4209 (page_index->page_len *
4211 saved_page = (struct copan_debugconf_subpage *)
4212 (page_index->page_data +
4213 (page_index->page_len *
4218 panic("invalid subpage value %d",
4219 page_index->subpage);
4225 panic("invalid page value %d",
4226 page_index->page_code & SMPH_PC_MASK);
4231 return (CTL_RETVAL_COMPLETE);
4238 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4239 * wants us to allocate the LUN and he can block.
4240 * - ctl_softc is always set
4241 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4243 * Returns 0 for success, non-zero (errno) for failure.
4246 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4247 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4249 struct ctl_lun *nlun, *lun;
4250 struct ctl_frontend *fe;
4251 int lun_number, i, lun_malloced;
4257 * We currently only support Direct Access or Processor LUN types.
4259 switch (be_lun->lun_type) {
4267 be_lun->lun_config_status(be_lun->be_lun,
4268 CTL_LUN_CONFIG_FAILURE);
4271 if (ctl_lun == NULL) {
4272 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4279 memset(lun, 0, sizeof(*lun));
4281 lun->flags = CTL_LUN_MALLOCED;
4283 mtx_lock(&ctl_softc->ctl_lock);
4285 * See if the caller requested a particular LUN number. If so, see
4286 * if it is available. Otherwise, allocate the first available LUN.
4288 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4289 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4290 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4291 mtx_unlock(&ctl_softc->ctl_lock);
4292 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4293 printf("ctl: requested LUN ID %d is higher "
4294 "than CTL_MAX_LUNS - 1 (%d)\n",
4295 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4298 * XXX KDM return an error, or just assign
4299 * another LUN ID in this case??
4301 printf("ctl: requested LUN ID %d is already "
4302 "in use\n", be_lun->req_lun_id);
4304 if (lun->flags & CTL_LUN_MALLOCED)
4306 be_lun->lun_config_status(be_lun->be_lun,
4307 CTL_LUN_CONFIG_FAILURE);
4310 lun_number = be_lun->req_lun_id;
4312 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4313 if (lun_number == -1) {
4314 mtx_unlock(&ctl_softc->ctl_lock);
4315 printf("ctl: can't allocate LUN on target %ju, out of "
4316 "LUNs\n", (uintmax_t)target_id.id);
4317 if (lun->flags & CTL_LUN_MALLOCED)
4319 be_lun->lun_config_status(be_lun->be_lun,
4320 CTL_LUN_CONFIG_FAILURE);
4324 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4326 lun->target = target_id;
4327 lun->lun = lun_number;
4328 lun->be_lun = be_lun;
4330 * The processor LUN is always enabled. Disk LUNs come on line
4331 * disabled, and must be enabled by the backend.
4333 lun->flags |= CTL_LUN_DISABLED;
4334 lun->backend = be_lun->be;
4335 be_lun->ctl_lun = lun;
4336 be_lun->lun_id = lun_number;
4337 atomic_add_int(&be_lun->be->num_luns, 1);
4338 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4339 lun->flags |= CTL_LUN_STOPPED;
4341 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4342 lun->flags |= CTL_LUN_INOPERABLE;
4344 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4345 lun->flags |= CTL_LUN_PRIMARY_SC;
4347 lun->ctl_softc = ctl_softc;
4348 TAILQ_INIT(&lun->ooa_queue);
4349 TAILQ_INIT(&lun->blocked_queue);
4350 STAILQ_INIT(&lun->error_list);
4353 * Initialize the mode page index.
4355 ctl_init_page_index(lun);
4358 * Set the poweron UA for all initiators on this LUN only.
4360 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4361 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4364 * Now, before we insert this lun on the lun list, set the lun
4365 * inventory changed UA for all other luns.
4367 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4368 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4369 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4373 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4375 ctl_softc->ctl_luns[lun_number] = lun;
4377 ctl_softc->num_luns++;
4379 /* Setup statistics gathering */
4380 lun->stats.device_type = be_lun->lun_type;
4381 lun->stats.lun_number = lun_number;
4382 if (lun->stats.device_type == T_DIRECT)
4383 lun->stats.blocksize = be_lun->blocksize;
4385 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4386 for (i = 0;i < CTL_MAX_PORTS;i++)
4387 lun->stats.ports[i].targ_port = i;
4389 mtx_unlock(&ctl_softc->ctl_lock);
4391 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4394 * Run through each registered FETD and bring it online if it isn't
4395 * already. Enable the target ID if it hasn't been enabled, and
4396 * enable this particular LUN.
4398 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4402 * XXX KDM this only works for ONE TARGET ID. We'll need
4403 * to do things differently if we go to a multiple target
4406 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) == 0) {
4408 retval = fe->targ_enable(fe->targ_lun_arg, target_id);
4410 printf("ctl_alloc_lun: FETD %s port %d "
4411 "returned error %d for targ_enable on "
4412 "target %ju\n", fe->port_name,
4413 fe->targ_port, retval,
4414 (uintmax_t)target_id.id);
4416 fe->status |= CTL_PORT_STATUS_TARG_ONLINE;
4419 retval = fe->lun_enable(fe->targ_lun_arg, target_id,lun_number);
4421 printf("ctl_alloc_lun: FETD %s port %d returned error "
4422 "%d for lun_enable on target %ju lun %d\n",
4423 fe->port_name, fe->targ_port, retval,
4424 (uintmax_t)target_id.id, lun_number);
4426 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4434 * - LUN has already been marked invalid and any pending I/O has been taken
4438 ctl_free_lun(struct ctl_lun *lun)
4440 struct ctl_softc *softc;
4442 struct ctl_frontend *fe;
4444 struct ctl_lun *nlun;
4445 union ctl_io *io, *next_io;
4448 softc = lun->ctl_softc;
4450 mtx_assert(&softc->ctl_lock, MA_OWNED);
4452 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4454 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4456 softc->ctl_luns[lun->lun] = NULL;
4458 if (TAILQ_FIRST(&lun->ooa_queue) != NULL) {
4459 printf("ctl_free_lun: aieee!! freeing a LUN with "
4460 "outstanding I/O!!\n");
4464 * If we have anything pending on the RtR queue, remove it.
4466 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue); io != NULL;
4470 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
4471 targ_lun = io->io_hdr.nexus.targ_lun;
4472 if (io->io_hdr.nexus.lun_map_fn != NULL)
4473 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
4474 if ((io->io_hdr.nexus.targ_target.id == lun->target.id)
4475 && (targ_lun == lun->lun))
4476 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
4481 * Then remove everything from the blocked queue.
4483 for (io = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue); io != NULL;
4485 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,blocked_links);
4486 TAILQ_REMOVE(&lun->blocked_queue, &io->io_hdr, blocked_links);
4487 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
4491 * Now clear out the OOA queue, and free all the I/O.
4492 * XXX KDM should we notify the FETD here? We probably need to
4493 * quiesce the LUN before deleting it.
4495 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); io != NULL;
4497 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, ooa_links);
4498 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
4499 ctl_free_io_internal(io, /*have_lock*/ 1);
4505 * XXX KDM this scheme only works for a single target/multiple LUN
4506 * setup. It needs to be revamped for a multiple target scheme.
4508 * XXX KDM this results in fe->lun_disable() getting called twice,
4509 * once when ctl_disable_lun() is called, and a second time here.
4510 * We really need to re-think the LUN disable semantics. There
4511 * should probably be several steps/levels to LUN removal:
4516 * Right now we only have a disable method when communicating to
4517 * the front end ports, at least for individual LUNs.
4520 STAILQ_FOREACH(fe, &softc->fe_list, links) {
4523 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4526 printf("ctl_free_lun: FETD %s port %d returned error "
4527 "%d for lun_disable on target %ju lun %jd\n",
4528 fe->port_name, fe->targ_port, retval,
4529 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4532 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4533 fe->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4535 retval = fe->targ_disable(fe->targ_lun_arg,lun->target);
4537 printf("ctl_free_lun: FETD %s port %d "
4538 "returned error %d for targ_disable on "
4539 "target %ju\n", fe->port_name,
4540 fe->targ_port, retval,
4541 (uintmax_t)lun->target.id);
4543 fe->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4545 if ((fe->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4549 fe->port_offline(fe->onoff_arg);
4550 fe->status &= ~CTL_PORT_STATUS_ONLINE;
4557 * Tell the backend to free resources, if this LUN has a backend.
4559 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4560 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4562 if (lun->flags & CTL_LUN_MALLOCED)
4565 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4566 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4567 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4575 ctl_create_lun(struct ctl_be_lun *be_lun)
4577 struct ctl_softc *ctl_softc;
4579 ctl_softc = control_softc;
4582 * ctl_alloc_lun() should handle all potential failure cases.
4584 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4588 ctl_add_lun(struct ctl_be_lun *be_lun)
4590 struct ctl_softc *ctl_softc;
4592 ctl_softc = control_softc;
4594 mtx_lock(&ctl_softc->ctl_lock);
4595 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4596 mtx_unlock(&ctl_softc->ctl_lock);
4598 ctl_wakeup_thread();
4604 ctl_enable_lun(struct ctl_be_lun *be_lun)
4606 struct ctl_softc *ctl_softc;
4607 struct ctl_frontend *fe, *nfe;
4608 struct ctl_lun *lun;
4611 ctl_softc = control_softc;
4613 lun = (struct ctl_lun *)be_lun->ctl_lun;
4615 mtx_lock(&ctl_softc->ctl_lock);
4616 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4618 * eh? Why did we get called if the LUN is already
4621 mtx_unlock(&ctl_softc->ctl_lock);
4624 lun->flags &= ~CTL_LUN_DISABLED;
4626 for (fe = STAILQ_FIRST(&ctl_softc->fe_list); fe != NULL; fe = nfe) {
4627 nfe = STAILQ_NEXT(fe, links);
4630 * Drop the lock while we call the FETD's enable routine.
4631 * This can lead to a callback into CTL (at least in the
4632 * case of the internal initiator frontend.
4634 mtx_unlock(&ctl_softc->ctl_lock);
4635 retval = fe->lun_enable(fe->targ_lun_arg, lun->target,lun->lun);
4636 mtx_lock(&ctl_softc->ctl_lock);
4638 printf("%s: FETD %s port %d returned error "
4639 "%d for lun_enable on target %ju lun %jd\n",
4640 __func__, fe->port_name, fe->targ_port, retval,
4641 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4645 /* NOTE: TODO: why does lun enable affect port status? */
4646 fe->status |= CTL_PORT_STATUS_LUN_ONLINE;
4651 mtx_unlock(&ctl_softc->ctl_lock);
4657 ctl_disable_lun(struct ctl_be_lun *be_lun)
4659 struct ctl_softc *ctl_softc;
4660 struct ctl_frontend *fe;
4661 struct ctl_lun *lun;
4664 ctl_softc = control_softc;
4666 lun = (struct ctl_lun *)be_lun->ctl_lun;
4668 mtx_lock(&ctl_softc->ctl_lock);
4670 if (lun->flags & CTL_LUN_DISABLED) {
4671 mtx_unlock(&ctl_softc->ctl_lock);
4674 lun->flags |= CTL_LUN_DISABLED;
4676 STAILQ_FOREACH(fe, &ctl_softc->fe_list, links) {
4677 mtx_unlock(&ctl_softc->ctl_lock);
4679 * Drop the lock before we call the frontend's disable
4680 * routine, to avoid lock order reversals.
4682 * XXX KDM what happens if the frontend list changes while
4683 * we're traversing it? It's unlikely, but should be handled.
4685 retval = fe->lun_disable(fe->targ_lun_arg, lun->target,
4687 mtx_lock(&ctl_softc->ctl_lock);
4689 printf("ctl_alloc_lun: FETD %s port %d returned error "
4690 "%d for lun_disable on target %ju lun %jd\n",
4691 fe->port_name, fe->targ_port, retval,
4692 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4696 mtx_unlock(&ctl_softc->ctl_lock);
4702 ctl_start_lun(struct ctl_be_lun *be_lun)
4704 struct ctl_softc *ctl_softc;
4705 struct ctl_lun *lun;
4707 ctl_softc = control_softc;
4709 lun = (struct ctl_lun *)be_lun->ctl_lun;
4711 mtx_lock(&ctl_softc->ctl_lock);
4712 lun->flags &= ~CTL_LUN_STOPPED;
4713 mtx_unlock(&ctl_softc->ctl_lock);
4719 ctl_stop_lun(struct ctl_be_lun *be_lun)
4721 struct ctl_softc *ctl_softc;
4722 struct ctl_lun *lun;
4724 ctl_softc = control_softc;
4726 lun = (struct ctl_lun *)be_lun->ctl_lun;
4728 mtx_lock(&ctl_softc->ctl_lock);
4729 lun->flags |= CTL_LUN_STOPPED;
4730 mtx_unlock(&ctl_softc->ctl_lock);
4736 ctl_lun_offline(struct ctl_be_lun *be_lun)
4738 struct ctl_softc *ctl_softc;
4739 struct ctl_lun *lun;
4741 ctl_softc = control_softc;
4743 lun = (struct ctl_lun *)be_lun->ctl_lun;
4745 mtx_lock(&ctl_softc->ctl_lock);
4746 lun->flags |= CTL_LUN_OFFLINE;
4747 mtx_unlock(&ctl_softc->ctl_lock);
4753 ctl_lun_online(struct ctl_be_lun *be_lun)
4755 struct ctl_softc *ctl_softc;
4756 struct ctl_lun *lun;
4758 ctl_softc = control_softc;
4760 lun = (struct ctl_lun *)be_lun->ctl_lun;
4762 mtx_lock(&ctl_softc->ctl_lock);
4763 lun->flags &= ~CTL_LUN_OFFLINE;
4764 mtx_unlock(&ctl_softc->ctl_lock);
4770 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4772 struct ctl_softc *ctl_softc;
4773 struct ctl_lun *lun;
4775 ctl_softc = control_softc;
4777 lun = (struct ctl_lun *)be_lun->ctl_lun;
4779 mtx_lock(&ctl_softc->ctl_lock);
4782 * The LUN needs to be disabled before it can be marked invalid.
4784 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4785 mtx_unlock(&ctl_softc->ctl_lock);
4789 * Mark the LUN invalid.
4791 lun->flags |= CTL_LUN_INVALID;
4794 * If there is nothing in the OOA queue, go ahead and free the LUN.
4795 * If we have something in the OOA queue, we'll free it when the
4796 * last I/O completes.
4798 if (TAILQ_FIRST(&lun->ooa_queue) == NULL)
4800 mtx_unlock(&ctl_softc->ctl_lock);
4806 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4808 struct ctl_softc *ctl_softc;
4809 struct ctl_lun *lun;
4811 ctl_softc = control_softc;
4812 lun = (struct ctl_lun *)be_lun->ctl_lun;
4814 mtx_lock(&ctl_softc->ctl_lock);
4815 lun->flags |= CTL_LUN_INOPERABLE;
4816 mtx_unlock(&ctl_softc->ctl_lock);
4822 ctl_lun_operable(struct ctl_be_lun *be_lun)
4824 struct ctl_softc *ctl_softc;
4825 struct ctl_lun *lun;
4827 ctl_softc = control_softc;
4828 lun = (struct ctl_lun *)be_lun->ctl_lun;
4830 mtx_lock(&ctl_softc->ctl_lock);
4831 lun->flags &= ~CTL_LUN_INOPERABLE;
4832 mtx_unlock(&ctl_softc->ctl_lock);
4838 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
4841 struct ctl_softc *softc;
4842 struct ctl_lun *lun;
4843 struct copan_aps_subpage *current_sp;
4844 struct ctl_page_index *page_index;
4847 softc = control_softc;
4849 mtx_lock(&softc->ctl_lock);
4851 lun = (struct ctl_lun *)be_lun->ctl_lun;
4854 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4855 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
4859 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
4861 page_index = &lun->mode_pages.index[i];
4864 if (page_index == NULL) {
4865 mtx_unlock(&softc->ctl_lock);
4866 printf("%s: APS subpage not found for lun %ju!\n", __func__,
4867 (uintmax_t)lun->lun);
4871 if ((softc->aps_locked_lun != 0)
4872 && (softc->aps_locked_lun != lun->lun)) {
4873 printf("%s: attempt to lock LUN %llu when %llu is already "
4875 mtx_unlock(&softc->ctl_lock);
4880 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
4881 (page_index->page_len * CTL_PAGE_CURRENT));
4884 current_sp->lock_active = APS_LOCK_ACTIVE;
4885 softc->aps_locked_lun = lun->lun;
4887 current_sp->lock_active = 0;
4888 softc->aps_locked_lun = 0;
4893 * If we're in HA mode, try to send the lock message to the other
4896 if (ctl_is_single == 0) {
4898 union ctl_ha_msg lock_msg;
4900 lock_msg.hdr.nexus = *nexus;
4901 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
4903 lock_msg.aps.lock_flag = 1;
4905 lock_msg.aps.lock_flag = 0;
4906 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
4907 sizeof(lock_msg), 0);
4908 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
4909 printf("%s: APS (lock=%d) error returned from "
4910 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
4911 mtx_unlock(&softc->ctl_lock);
4916 mtx_unlock(&softc->ctl_lock);
4922 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4924 struct ctl_lun *lun;
4925 struct ctl_softc *softc;
4928 softc = control_softc;
4930 mtx_lock(&softc->ctl_lock);
4932 lun = (struct ctl_lun *)be_lun->ctl_lun;
4934 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4935 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
4937 mtx_unlock(&softc->ctl_lock);
4941 * Backend "memory move is complete" callback for requests that never
4942 * make it down to say RAIDCore's configuration code.
4945 ctl_config_move_done(union ctl_io *io)
4949 retval = CTL_RETVAL_COMPLETE;
4952 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4954 * XXX KDM this shouldn't happen, but what if it does?
4956 if (io->io_hdr.io_type != CTL_IO_SCSI)
4957 panic("I/O type isn't CTL_IO_SCSI!");
4959 if ((io->io_hdr.port_status == 0)
4960 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4961 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
4962 io->io_hdr.status = CTL_SUCCESS;
4963 else if ((io->io_hdr.port_status != 0)
4964 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
4965 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
4967 * For hardware error sense keys, the sense key
4968 * specific value is defined to be a retry count,
4969 * but we use it to pass back an internal FETD
4970 * error code. XXX KDM Hopefully the FETD is only
4971 * using 16 bits for an error code, since that's
4972 * all the space we have in the sks field.
4974 ctl_set_internal_failure(&io->scsiio,
4977 io->io_hdr.port_status);
4978 free(io->scsiio.kern_data_ptr, M_CTL);
4983 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
4984 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
4985 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4987 * XXX KDM just assuming a single pointer here, and not a
4988 * S/G list. If we start using S/G lists for config data,
4989 * we'll need to know how to clean them up here as well.
4991 free(io->scsiio.kern_data_ptr, M_CTL);
4992 /* Hopefully the user has already set the status... */
4996 * XXX KDM now we need to continue data movement. Some
4998 * - call ctl_scsiio() again? We don't do this for data
4999 * writes, because for those at least we know ahead of
5000 * time where the write will go and how long it is. For
5001 * config writes, though, that information is largely
5002 * contained within the write itself, thus we need to
5003 * parse out the data again.
5005 * - Call some other function once the data is in?
5009 * XXX KDM call ctl_scsiio() again for now, and check flag
5010 * bits to see whether we're allocated or not.
5012 retval = ctl_scsiio(&io->scsiio);
5019 * This gets called by a backend driver when it is done with a
5020 * configuration write.
5023 ctl_config_write_done(union ctl_io *io)
5026 * If the IO_CONT flag is set, we need to call the supplied
5027 * function to continue processing the I/O, instead of completing
5030 * If there is an error, though, we don't want to keep processing.
5031 * Instead, just send status back to the initiator.
5033 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5034 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5035 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5036 io->scsiio.io_cont(io);
5040 * Since a configuration write can be done for commands that actually
5041 * have data allocated, like write buffer, and commands that have
5042 * no data, like start/stop unit, we need to check here.
5044 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
5045 free(io->scsiio.kern_data_ptr, M_CTL);
5050 * SCSI release command.
5053 ctl_scsi_release(struct ctl_scsiio *ctsio)
5055 int length, longid, thirdparty_id, resv_id;
5056 struct ctl_softc *ctl_softc;
5057 struct ctl_lun *lun;
5062 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5064 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5065 ctl_softc = control_softc;
5067 switch (ctsio->cdb[0]) {
5069 struct scsi_release *cdb;
5071 cdb = (struct scsi_release *)ctsio->cdb;
5072 if ((cdb->byte2 & 0x1f) != 0) {
5073 ctl_set_invalid_field(ctsio,
5079 ctl_done((union ctl_io *)ctsio);
5080 return (CTL_RETVAL_COMPLETE);
5085 struct scsi_release_10 *cdb;
5087 cdb = (struct scsi_release_10 *)ctsio->cdb;
5089 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5090 ctl_set_invalid_field(ctsio,
5096 ctl_done((union ctl_io *)ctsio);
5097 return (CTL_RETVAL_COMPLETE);
5101 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5102 ctl_set_invalid_field(ctsio,
5108 ctl_done((union ctl_io *)ctsio);
5109 return (CTL_RETVAL_COMPLETE);
5112 if (cdb->byte2 & SR10_LONGID)
5115 thirdparty_id = cdb->thirdparty_id;
5117 resv_id = cdb->resv_id;
5118 length = scsi_2btoul(cdb->length);
5125 * XXX KDM right now, we only support LUN reservation. We don't
5126 * support 3rd party reservations, or extent reservations, which
5127 * might actually need the parameter list. If we've gotten this
5128 * far, we've got a LUN reservation. Anything else got kicked out
5129 * above. So, according to SPC, ignore the length.
5133 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5135 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5136 ctsio->kern_data_len = length;
5137 ctsio->kern_total_len = length;
5138 ctsio->kern_data_resid = 0;
5139 ctsio->kern_rel_offset = 0;
5140 ctsio->kern_sg_entries = 0;
5141 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5142 ctsio->be_move_done = ctl_config_move_done;
5143 ctl_datamove((union ctl_io *)ctsio);
5145 return (CTL_RETVAL_COMPLETE);
5149 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5151 mtx_lock(&ctl_softc->ctl_lock);
5154 * According to SPC, it is not an error for an intiator to attempt
5155 * to release a reservation on a LUN that isn't reserved, or that
5156 * is reserved by another initiator. The reservation can only be
5157 * released, though, by the initiator who made it or by one of
5158 * several reset type events.
5160 if (lun->flags & CTL_LUN_RESERVED) {
5161 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5162 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5163 && (ctsio->io_hdr.nexus.targ_target.id ==
5164 lun->rsv_nexus.targ_target.id)) {
5165 lun->flags &= ~CTL_LUN_RESERVED;
5169 ctsio->scsi_status = SCSI_STATUS_OK;
5170 ctsio->io_hdr.status = CTL_SUCCESS;
5172 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5173 free(ctsio->kern_data_ptr, M_CTL);
5174 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5177 mtx_unlock(&ctl_softc->ctl_lock);
5179 ctl_done((union ctl_io *)ctsio);
5180 return (CTL_RETVAL_COMPLETE);
5184 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5186 int extent, thirdparty, longid;
5187 int resv_id, length;
5188 uint64_t thirdparty_id;
5189 struct ctl_softc *ctl_softc;
5190 struct ctl_lun *lun;
5199 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5201 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5202 ctl_softc = control_softc;
5204 switch (ctsio->cdb[0]) {
5206 struct scsi_reserve *cdb;
5208 cdb = (struct scsi_reserve *)ctsio->cdb;
5209 if ((cdb->byte2 & 0x1f) != 0) {
5210 ctl_set_invalid_field(ctsio,
5216 ctl_done((union ctl_io *)ctsio);
5217 return (CTL_RETVAL_COMPLETE);
5219 resv_id = cdb->resv_id;
5220 length = scsi_2btoul(cdb->length);
5224 struct scsi_reserve_10 *cdb;
5226 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5228 if ((cdb->byte2 & SR10_EXTENT) != 0) {
5229 ctl_set_invalid_field(ctsio,
5235 ctl_done((union ctl_io *)ctsio);
5236 return (CTL_RETVAL_COMPLETE);
5238 if ((cdb->byte2 & SR10_3RDPTY) != 0) {
5239 ctl_set_invalid_field(ctsio,
5245 ctl_done((union ctl_io *)ctsio);
5246 return (CTL_RETVAL_COMPLETE);
5248 if (cdb->byte2 & SR10_LONGID)
5251 thirdparty_id = cdb->thirdparty_id;
5253 resv_id = cdb->resv_id;
5254 length = scsi_2btoul(cdb->length);
5260 * XXX KDM right now, we only support LUN reservation. We don't
5261 * support 3rd party reservations, or extent reservations, which
5262 * might actually need the parameter list. If we've gotten this
5263 * far, we've got a LUN reservation. Anything else got kicked out
5264 * above. So, according to SPC, ignore the length.
5268 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5270 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5271 ctsio->kern_data_len = length;
5272 ctsio->kern_total_len = length;
5273 ctsio->kern_data_resid = 0;
5274 ctsio->kern_rel_offset = 0;
5275 ctsio->kern_sg_entries = 0;
5276 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5277 ctsio->be_move_done = ctl_config_move_done;
5278 ctl_datamove((union ctl_io *)ctsio);
5280 return (CTL_RETVAL_COMPLETE);
5284 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5286 mtx_lock(&ctl_softc->ctl_lock);
5287 if (lun->flags & CTL_LUN_RESERVED) {
5288 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5289 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5290 || (ctsio->io_hdr.nexus.targ_target.id !=
5291 lun->rsv_nexus.targ_target.id)) {
5292 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5293 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5298 lun->flags |= CTL_LUN_RESERVED;
5299 lun->rsv_nexus = ctsio->io_hdr.nexus;
5301 ctsio->scsi_status = SCSI_STATUS_OK;
5302 ctsio->io_hdr.status = CTL_SUCCESS;
5305 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5306 free(ctsio->kern_data_ptr, M_CTL);
5307 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5310 mtx_unlock(&ctl_softc->ctl_lock);
5312 ctl_done((union ctl_io *)ctsio);
5313 return (CTL_RETVAL_COMPLETE);
5317 ctl_start_stop(struct ctl_scsiio *ctsio)
5319 struct scsi_start_stop_unit *cdb;
5320 struct ctl_lun *lun;
5321 struct ctl_softc *ctl_softc;
5324 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5326 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5327 ctl_softc = control_softc;
5330 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5334 * We don't support the immediate bit on a stop unit. In order to
5335 * do that, we would need to code up a way to know that a stop is
5336 * pending, and hold off any new commands until it completes, one
5337 * way or another. Then we could accept or reject those commands
5338 * depending on its status. We would almost need to do the reverse
5339 * of what we do below for an immediate start -- return the copy of
5340 * the ctl_io to the FETD with status to send to the host (and to
5341 * free the copy!) and then free the original I/O once the stop
5342 * actually completes. That way, the OOA queue mechanism can work
5343 * to block commands that shouldn't proceed. Another alternative
5344 * would be to put the copy in the queue in place of the original,
5345 * and return the original back to the caller. That could be
5348 if ((cdb->byte2 & SSS_IMMED)
5349 && ((cdb->how & SSS_START) == 0)) {
5350 ctl_set_invalid_field(ctsio,
5356 ctl_done((union ctl_io *)ctsio);
5357 return (CTL_RETVAL_COMPLETE);
5361 * We don't support the power conditions field. We need to check
5362 * this prior to checking the load/eject and start/stop bits.
5364 if ((cdb->how & SSS_PC_MASK) != SSS_PC_START_VALID) {
5365 ctl_set_invalid_field(ctsio,
5371 ctl_done((union ctl_io *)ctsio);
5372 return (CTL_RETVAL_COMPLETE);
5376 * Media isn't removable, so we can't load or eject it.
5378 if ((cdb->how & SSS_LOEJ) != 0) {
5379 ctl_set_invalid_field(ctsio,
5385 ctl_done((union ctl_io *)ctsio);
5386 return (CTL_RETVAL_COMPLETE);
5389 if ((lun->flags & CTL_LUN_PR_RESERVED)
5390 && ((cdb->how & SSS_START)==0)) {
5393 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5394 if (!lun->per_res[residx].registered
5395 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5397 ctl_set_reservation_conflict(ctsio);
5398 ctl_done((union ctl_io *)ctsio);
5399 return (CTL_RETVAL_COMPLETE);
5404 * If there is no backend on this device, we can't start or stop
5405 * it. In theory we shouldn't get any start/stop commands in the
5406 * first place at this level if the LUN doesn't have a backend.
5407 * That should get stopped by the command decode code.
5409 if (lun->backend == NULL) {
5410 ctl_set_invalid_opcode(ctsio);
5411 ctl_done((union ctl_io *)ctsio);
5412 return (CTL_RETVAL_COMPLETE);
5416 * XXX KDM Copan-specific offline behavior.
5417 * Figure out a reasonable way to port this?
5420 mtx_lock(&ctl_softc->ctl_lock);
5422 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5423 && (lun->flags & CTL_LUN_OFFLINE)) {
5425 * If the LUN is offline, and the on/offline bit isn't set,
5426 * reject the start or stop. Otherwise, let it through.
5428 mtx_unlock(&ctl_softc->ctl_lock);
5429 ctl_set_lun_not_ready(ctsio);
5430 ctl_done((union ctl_io *)ctsio);
5432 mtx_unlock(&ctl_softc->ctl_lock);
5433 #endif /* NEEDTOPORT */
5435 * This could be a start or a stop when we're online,
5436 * or a stop/offline or start/online. A start or stop when
5437 * we're offline is covered in the case above.
5440 * In the non-immediate case, we send the request to
5441 * the backend and return status to the user when
5444 * In the immediate case, we allocate a new ctl_io
5445 * to hold a copy of the request, and send that to
5446 * the backend. We then set good status on the
5447 * user's request and return it immediately.
5449 if (cdb->byte2 & SSS_IMMED) {
5450 union ctl_io *new_io;
5452 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5453 if (new_io == NULL) {
5454 ctl_set_busy(ctsio);
5455 ctl_done((union ctl_io *)ctsio);
5457 ctl_copy_io((union ctl_io *)ctsio,
5459 retval = lun->backend->config_write(new_io);
5460 ctl_set_success(ctsio);
5461 ctl_done((union ctl_io *)ctsio);
5464 retval = lun->backend->config_write(
5465 (union ctl_io *)ctsio);
5474 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5475 * we don't really do anything with the LBA and length fields if the user
5476 * passes them in. Instead we'll just flush out the cache for the entire
5480 ctl_sync_cache(struct ctl_scsiio *ctsio)
5482 struct ctl_lun *lun;
5483 struct ctl_softc *ctl_softc;
5484 uint64_t starting_lba;
5485 uint32_t block_count;
5489 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5491 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5492 ctl_softc = control_softc;
5497 switch (ctsio->cdb[0]) {
5498 case SYNCHRONIZE_CACHE: {
5499 struct scsi_sync_cache *cdb;
5500 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5502 if (cdb->byte2 & SSC_RELADR)
5505 if (cdb->byte2 & SSC_IMMED)
5508 starting_lba = scsi_4btoul(cdb->begin_lba);
5509 block_count = scsi_2btoul(cdb->lb_count);
5512 case SYNCHRONIZE_CACHE_16: {
5513 struct scsi_sync_cache_16 *cdb;
5514 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5516 if (cdb->byte2 & SSC_RELADR)
5519 if (cdb->byte2 & SSC_IMMED)
5522 starting_lba = scsi_8btou64(cdb->begin_lba);
5523 block_count = scsi_4btoul(cdb->lb_count);
5527 ctl_set_invalid_opcode(ctsio);
5528 ctl_done((union ctl_io *)ctsio);
5530 break; /* NOTREACHED */
5535 * We don't support the immediate bit. Since it's in the
5536 * same place for the 10 and 16 byte SYNCHRONIZE CACHE
5537 * commands, we can just return the same error in either
5540 ctl_set_invalid_field(ctsio,
5546 ctl_done((union ctl_io *)ctsio);
5552 * We don't support the reladr bit either. It can only be
5553 * used with linked commands, and we don't support linked
5554 * commands. Since the bit is in the same place for the
5555 * 10 and 16 byte SYNCHRONIZE CACHE * commands, we can
5556 * just return the same error in either case.
5558 ctl_set_invalid_field(ctsio,
5564 ctl_done((union ctl_io *)ctsio);
5569 * We check the LBA and length, but don't do anything with them.
5570 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5571 * get flushed. This check will just help satisfy anyone who wants
5572 * to see an error for an out of range LBA.
5574 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5575 ctl_set_lba_out_of_range(ctsio);
5576 ctl_done((union ctl_io *)ctsio);
5581 * If this LUN has no backend, we can't flush the cache anyway.
5583 if (lun->backend == NULL) {
5584 ctl_set_invalid_opcode(ctsio);
5585 ctl_done((union ctl_io *)ctsio);
5590 * Check to see whether we're configured to send the SYNCHRONIZE
5591 * CACHE command directly to the back end.
5593 mtx_lock(&ctl_softc->ctl_lock);
5594 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5595 && (++(lun->sync_count) >= lun->sync_interval)) {
5596 lun->sync_count = 0;
5597 mtx_unlock(&ctl_softc->ctl_lock);
5598 retval = lun->backend->config_write((union ctl_io *)ctsio);
5600 mtx_unlock(&ctl_softc->ctl_lock);
5601 ctl_set_success(ctsio);
5602 ctl_done((union ctl_io *)ctsio);
5611 ctl_format(struct ctl_scsiio *ctsio)
5613 struct scsi_format *cdb;
5614 struct ctl_lun *lun;
5615 struct ctl_softc *ctl_softc;
5616 int length, defect_list_len;
5618 CTL_DEBUG_PRINT(("ctl_format\n"));
5620 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5621 ctl_softc = control_softc;
5623 cdb = (struct scsi_format *)ctsio->cdb;
5626 if (cdb->byte2 & SF_FMTDATA) {
5627 if (cdb->byte2 & SF_LONGLIST)
5628 length = sizeof(struct scsi_format_header_long);
5630 length = sizeof(struct scsi_format_header_short);
5633 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5635 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5636 ctsio->kern_data_len = length;
5637 ctsio->kern_total_len = length;
5638 ctsio->kern_data_resid = 0;
5639 ctsio->kern_rel_offset = 0;
5640 ctsio->kern_sg_entries = 0;
5641 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5642 ctsio->be_move_done = ctl_config_move_done;
5643 ctl_datamove((union ctl_io *)ctsio);
5645 return (CTL_RETVAL_COMPLETE);
5648 defect_list_len = 0;
5650 if (cdb->byte2 & SF_FMTDATA) {
5651 if (cdb->byte2 & SF_LONGLIST) {
5652 struct scsi_format_header_long *header;
5654 header = (struct scsi_format_header_long *)
5655 ctsio->kern_data_ptr;
5657 defect_list_len = scsi_4btoul(header->defect_list_len);
5658 if (defect_list_len != 0) {
5659 ctl_set_invalid_field(ctsio,
5668 struct scsi_format_header_short *header;
5670 header = (struct scsi_format_header_short *)
5671 ctsio->kern_data_ptr;
5673 defect_list_len = scsi_2btoul(header->defect_list_len);
5674 if (defect_list_len != 0) {
5675 ctl_set_invalid_field(ctsio,
5687 * The format command will clear out the "Medium format corrupted"
5688 * status if set by the configuration code. That status is really
5689 * just a way to notify the host that we have lost the media, and
5690 * get them to issue a command that will basically make them think
5691 * they're blowing away the media.
5693 mtx_lock(&ctl_softc->ctl_lock);
5694 lun->flags &= ~CTL_LUN_INOPERABLE;
5695 mtx_unlock(&ctl_softc->ctl_lock);
5697 ctsio->scsi_status = SCSI_STATUS_OK;
5698 ctsio->io_hdr.status = CTL_SUCCESS;
5701 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5702 free(ctsio->kern_data_ptr, M_CTL);
5703 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5706 ctl_done((union ctl_io *)ctsio);
5707 return (CTL_RETVAL_COMPLETE);
5711 ctl_write_buffer(struct ctl_scsiio *ctsio)
5713 struct scsi_write_buffer *cdb;
5714 struct copan_page_header *header;
5715 struct ctl_lun *lun;
5716 struct ctl_softc *ctl_softc;
5717 int buffer_offset, len;
5722 retval = CTL_RETVAL_COMPLETE;
5724 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5726 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5727 ctl_softc = control_softc;
5728 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5730 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5731 ctl_set_invalid_field(ctsio,
5737 ctl_done((union ctl_io *)ctsio);
5738 return (CTL_RETVAL_COMPLETE);
5740 if (cdb->buffer_id != 0) {
5741 ctl_set_invalid_field(ctsio,
5747 ctl_done((union ctl_io *)ctsio);
5748 return (CTL_RETVAL_COMPLETE);
5751 len = scsi_3btoul(cdb->length);
5752 buffer_offset = scsi_3btoul(cdb->offset);
5754 if (len > sizeof(lun->write_buffer)) {
5755 ctl_set_invalid_field(ctsio,
5761 ctl_done((union ctl_io *)ctsio);
5762 return (CTL_RETVAL_COMPLETE);
5765 if (buffer_offset != 0) {
5766 ctl_set_invalid_field(ctsio,
5772 ctl_done((union ctl_io *)ctsio);
5773 return (CTL_RETVAL_COMPLETE);
5777 * If we've got a kernel request that hasn't been malloced yet,
5778 * malloc it and tell the caller the data buffer is here.
5780 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5781 ctsio->kern_data_ptr = lun->write_buffer;
5782 ctsio->kern_data_len = len;
5783 ctsio->kern_total_len = len;
5784 ctsio->kern_data_resid = 0;
5785 ctsio->kern_rel_offset = 0;
5786 ctsio->kern_sg_entries = 0;
5787 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5788 ctsio->be_move_done = ctl_config_move_done;
5789 ctl_datamove((union ctl_io *)ctsio);
5791 return (CTL_RETVAL_COMPLETE);
5794 ctl_done((union ctl_io *)ctsio);
5796 return (CTL_RETVAL_COMPLETE);
5800 * Note that this function currently doesn't actually do anything inside
5801 * CTL to enforce things if the DQue bit is turned on.
5803 * Also note that this function can't be used in the default case, because
5804 * the DQue bit isn't set in the changeable mask for the control mode page
5805 * anyway. This is just here as an example for how to implement a page
5806 * handler, and a placeholder in case we want to allow the user to turn
5807 * tagged queueing on and off.
5809 * The D_SENSE bit handling is functional, however, and will turn
5810 * descriptor sense on and off for a given LUN.
5813 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5814 struct ctl_page_index *page_index, uint8_t *page_ptr)
5816 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5817 struct ctl_lun *lun;
5818 struct ctl_softc *softc;
5822 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5823 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5826 user_cp = (struct scsi_control_page *)page_ptr;
5827 current_cp = (struct scsi_control_page *)
5828 (page_index->page_data + (page_index->page_len *
5830 saved_cp = (struct scsi_control_page *)
5831 (page_index->page_data + (page_index->page_len *
5834 softc = control_softc;
5836 mtx_lock(&softc->ctl_lock);
5837 if (((current_cp->rlec & SCP_DSENSE) == 0)
5838 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5840 * Descriptor sense is currently turned off and the user
5841 * wants to turn it on.
5843 current_cp->rlec |= SCP_DSENSE;
5844 saved_cp->rlec |= SCP_DSENSE;
5845 lun->flags |= CTL_LUN_SENSE_DESC;
5847 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5848 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5850 * Descriptor sense is currently turned on, and the user
5851 * wants to turn it off.
5853 current_cp->rlec &= ~SCP_DSENSE;
5854 saved_cp->rlec &= ~SCP_DSENSE;
5855 lun->flags &= ~CTL_LUN_SENSE_DESC;
5858 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
5859 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5861 csevent_log(CSC_CTL | CSC_SHELF_SW |
5863 csevent_LogType_Trace,
5864 csevent_Severity_Information,
5865 csevent_AlertLevel_Green,
5866 csevent_FRU_Firmware,
5867 csevent_FRU_Unknown,
5868 "Received untagged to untagged transition");
5869 #endif /* NEEDTOPORT */
5872 csevent_log(CSC_CTL | CSC_SHELF_SW |
5874 csevent_LogType_ConfigChange,
5875 csevent_Severity_Information,
5876 csevent_AlertLevel_Green,
5877 csevent_FRU_Firmware,
5878 csevent_FRU_Unknown,
5879 "Received untagged to tagged "
5880 "queueing transition");
5881 #endif /* NEEDTOPORT */
5883 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5884 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
5888 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
5890 csevent_log(CSC_CTL | CSC_SHELF_SW |
5892 csevent_LogType_ConfigChange,
5893 csevent_Severity_Warning,
5894 csevent_AlertLevel_Yellow,
5895 csevent_FRU_Firmware,
5896 csevent_FRU_Unknown,
5897 "Received tagged queueing to untagged "
5899 #endif /* NEEDTOPORT */
5901 current_cp->queue_flags |= SCP_QUEUE_DQUE;
5902 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
5906 csevent_log(CSC_CTL | CSC_SHELF_SW |
5908 csevent_LogType_Trace,
5909 csevent_Severity_Information,
5910 csevent_AlertLevel_Green,
5911 csevent_FRU_Firmware,
5912 csevent_FRU_Unknown,
5913 "Received tagged queueing to tagged "
5914 "queueing transition");
5915 #endif /* NEEDTOPORT */
5921 * Let other initiators know that the mode
5922 * parameters for this LUN have changed.
5924 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
5928 lun->pending_sense[i].ua_pending |=
5932 mtx_unlock(&softc->ctl_lock);
5938 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
5939 struct ctl_page_index *page_index, uint8_t *page_ptr)
5945 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
5946 struct ctl_page_index *page_index, int pc)
5948 struct copan_power_subpage *page;
5950 page = (struct copan_power_subpage *)page_index->page_data +
5951 (page_index->page_len * pc);
5954 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5956 * We don't update the changable bits for this page.
5959 case SMS_PAGE_CTRL_CURRENT >> 6:
5960 case SMS_PAGE_CTRL_DEFAULT >> 6:
5961 case SMS_PAGE_CTRL_SAVED >> 6:
5963 ctl_update_power_subpage(page);
5968 EPRINT(0, "Invalid PC %d!!", pc);
5977 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
5978 struct ctl_page_index *page_index, uint8_t *page_ptr)
5980 struct copan_aps_subpage *user_sp;
5981 struct copan_aps_subpage *current_sp;
5982 union ctl_modepage_info *modepage_info;
5983 struct ctl_softc *softc;
5984 struct ctl_lun *lun;
5987 retval = CTL_RETVAL_COMPLETE;
5988 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5989 (page_index->page_len * CTL_PAGE_CURRENT));
5990 softc = control_softc;
5991 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5993 user_sp = (struct copan_aps_subpage *)page_ptr;
5995 modepage_info = (union ctl_modepage_info *)
5996 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5998 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
5999 modepage_info->header.subpage = page_index->subpage;
6000 modepage_info->aps.lock_active = user_sp->lock_active;
6002 mtx_lock(&softc->ctl_lock);
6005 * If there is a request to lock the LUN and another LUN is locked
6006 * this is an error. If the requested LUN is already locked ignore
6007 * the request. If no LUN is locked attempt to lock it.
6008 * if there is a request to unlock the LUN and the LUN is currently
6009 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6010 * if another LUN is locked or no LUN is locked.
6012 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6013 if (softc->aps_locked_lun == lun->lun) {
6015 * This LUN is already locked, so we're done.
6017 retval = CTL_RETVAL_COMPLETE;
6018 } else if (softc->aps_locked_lun == 0) {
6020 * No one has the lock, pass the request to the
6023 retval = lun->backend->config_write(
6024 (union ctl_io *)ctsio);
6027 * Someone else has the lock, throw out the request.
6029 ctl_set_already_locked(ctsio);
6030 free(ctsio->kern_data_ptr, M_CTL);
6031 ctl_done((union ctl_io *)ctsio);
6034 * Set the return value so that ctl_do_mode_select()
6035 * won't try to complete the command. We already
6036 * completed it here.
6038 retval = CTL_RETVAL_ERROR;
6040 } else if (softc->aps_locked_lun == lun->lun) {
6042 * This LUN is locked, so pass the unlock request to the
6045 retval = lun->backend->config_write((union ctl_io *)ctsio);
6047 mtx_unlock(&softc->ctl_lock);
6053 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6054 struct ctl_page_index *page_index,
6060 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6065 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6066 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6067 printf("page data:");
6069 printf(" %.2x",page_ptr[i]);
6075 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6076 struct ctl_page_index *page_index,
6079 struct copan_debugconf_subpage *page;
6081 page = (struct copan_debugconf_subpage *)page_index->page_data +
6082 (page_index->page_len * pc);
6085 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6086 case SMS_PAGE_CTRL_DEFAULT >> 6:
6087 case SMS_PAGE_CTRL_SAVED >> 6:
6089 * We don't update the changable or default bits for this page.
6092 case SMS_PAGE_CTRL_CURRENT >> 6:
6093 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6094 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6098 EPRINT(0, "Invalid PC %d!!", pc);
6099 #endif /* NEEDTOPORT */
6107 ctl_do_mode_select(union ctl_io *io)
6109 struct scsi_mode_page_header *page_header;
6110 struct ctl_page_index *page_index;
6111 struct ctl_scsiio *ctsio;
6112 int control_dev, page_len;
6113 int page_len_offset, page_len_size;
6114 union ctl_modepage_info *modepage_info;
6115 struct ctl_lun *lun;
6116 int *len_left, *len_used;
6119 ctsio = &io->scsiio;
6122 retval = CTL_RETVAL_COMPLETE;
6124 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6126 if (lun->be_lun->lun_type != T_DIRECT)
6131 modepage_info = (union ctl_modepage_info *)
6132 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6133 len_left = &modepage_info->header.len_left;
6134 len_used = &modepage_info->header.len_used;
6138 page_header = (struct scsi_mode_page_header *)
6139 (ctsio->kern_data_ptr + *len_used);
6141 if (*len_left == 0) {
6142 free(ctsio->kern_data_ptr, M_CTL);
6143 ctl_set_success(ctsio);
6144 ctl_done((union ctl_io *)ctsio);
6145 return (CTL_RETVAL_COMPLETE);
6146 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6148 free(ctsio->kern_data_ptr, M_CTL);
6149 ctl_set_param_len_error(ctsio);
6150 ctl_done((union ctl_io *)ctsio);
6151 return (CTL_RETVAL_COMPLETE);
6153 } else if ((page_header->page_code & SMPH_SPF)
6154 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6156 free(ctsio->kern_data_ptr, M_CTL);
6157 ctl_set_param_len_error(ctsio);
6158 ctl_done((union ctl_io *)ctsio);
6159 return (CTL_RETVAL_COMPLETE);
6164 * XXX KDM should we do something with the block descriptor?
6166 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6168 if ((control_dev != 0)
6169 && (lun->mode_pages.index[i].page_flags &
6170 CTL_PAGE_FLAG_DISK_ONLY))
6173 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6174 (page_header->page_code & SMPH_PC_MASK))
6178 * If neither page has a subpage code, then we've got a
6181 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6182 && ((page_header->page_code & SMPH_SPF) == 0)) {
6183 page_index = &lun->mode_pages.index[i];
6184 page_len = page_header->page_length;
6189 * If both pages have subpages, then the subpage numbers
6192 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6193 && (page_header->page_code & SMPH_SPF)) {
6194 struct scsi_mode_page_header_sp *sph;
6196 sph = (struct scsi_mode_page_header_sp *)page_header;
6198 if (lun->mode_pages.index[i].subpage ==
6200 page_index = &lun->mode_pages.index[i];
6201 page_len = scsi_2btoul(sph->page_length);
6208 * If we couldn't find the page, or if we don't have a mode select
6209 * handler for it, send back an error to the user.
6211 if ((page_index == NULL)
6212 || (page_index->select_handler == NULL)) {
6213 ctl_set_invalid_field(ctsio,
6216 /*field*/ *len_used,
6219 free(ctsio->kern_data_ptr, M_CTL);
6220 ctl_done((union ctl_io *)ctsio);
6221 return (CTL_RETVAL_COMPLETE);
6224 if (page_index->page_code & SMPH_SPF) {
6225 page_len_offset = 2;
6229 page_len_offset = 1;
6233 * If the length the initiator gives us isn't the one we specify in
6234 * the mode page header, or if they didn't specify enough data in
6235 * the CDB to avoid truncating this page, kick out the request.
6237 if ((page_len != (page_index->page_len - page_len_offset -
6239 || (*len_left < page_index->page_len)) {
6242 ctl_set_invalid_field(ctsio,
6245 /*field*/ *len_used + page_len_offset,
6248 free(ctsio->kern_data_ptr, M_CTL);
6249 ctl_done((union ctl_io *)ctsio);
6250 return (CTL_RETVAL_COMPLETE);
6254 * Run through the mode page, checking to make sure that the bits
6255 * the user changed are actually legal for him to change.
6257 for (i = 0; i < page_index->page_len; i++) {
6258 uint8_t *user_byte, *change_mask, *current_byte;
6262 user_byte = (uint8_t *)page_header + i;
6263 change_mask = page_index->page_data +
6264 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6265 current_byte = page_index->page_data +
6266 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6269 * Check to see whether the user set any bits in this byte
6270 * that he is not allowed to set.
6272 if ((*user_byte & ~(*change_mask)) ==
6273 (*current_byte & ~(*change_mask)))
6277 * Go through bit by bit to determine which one is illegal.
6280 for (j = 7; j >= 0; j--) {
6281 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6282 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6287 ctl_set_invalid_field(ctsio,
6290 /*field*/ *len_used + i,
6293 free(ctsio->kern_data_ptr, M_CTL);
6294 ctl_done((union ctl_io *)ctsio);
6295 return (CTL_RETVAL_COMPLETE);
6299 * Decrement these before we call the page handler, since we may
6300 * end up getting called back one way or another before the handler
6301 * returns to this context.
6303 *len_left -= page_index->page_len;
6304 *len_used += page_index->page_len;
6306 retval = page_index->select_handler(ctsio, page_index,
6307 (uint8_t *)page_header);
6310 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6311 * wait until this queued command completes to finish processing
6312 * the mode page. If it returns anything other than
6313 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6314 * already set the sense information, freed the data pointer, and
6315 * completed the io for us.
6317 if (retval != CTL_RETVAL_COMPLETE)
6318 goto bailout_no_done;
6321 * If the initiator sent us more than one page, parse the next one.
6326 ctl_set_success(ctsio);
6327 free(ctsio->kern_data_ptr, M_CTL);
6328 ctl_done((union ctl_io *)ctsio);
6332 return (CTL_RETVAL_COMPLETE);
6337 ctl_mode_select(struct ctl_scsiio *ctsio)
6339 int param_len, pf, sp;
6340 int header_size, bd_len;
6341 int len_left, len_used;
6342 struct ctl_page_index *page_index;
6343 struct ctl_lun *lun;
6344 int control_dev, page_len;
6345 union ctl_modepage_info *modepage_info;
6357 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6359 if (lun->be_lun->lun_type != T_DIRECT)
6364 switch (ctsio->cdb[0]) {
6365 case MODE_SELECT_6: {
6366 struct scsi_mode_select_6 *cdb;
6368 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6370 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6371 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6373 param_len = cdb->length;
6374 header_size = sizeof(struct scsi_mode_header_6);
6377 case MODE_SELECT_10: {
6378 struct scsi_mode_select_10 *cdb;
6380 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6382 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6383 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6385 param_len = scsi_2btoul(cdb->length);
6386 header_size = sizeof(struct scsi_mode_header_10);
6390 ctl_set_invalid_opcode(ctsio);
6391 ctl_done((union ctl_io *)ctsio);
6392 return (CTL_RETVAL_COMPLETE);
6393 break; /* NOTREACHED */
6398 * "A parameter list length of zero indicates that the Data-Out Buffer
6399 * shall be empty. This condition shall not be considered as an error."
6401 if (param_len == 0) {
6402 ctl_set_success(ctsio);
6403 ctl_done((union ctl_io *)ctsio);
6404 return (CTL_RETVAL_COMPLETE);
6408 * Since we'll hit this the first time through, prior to
6409 * allocation, we don't need to free a data buffer here.
6411 if (param_len < header_size) {
6412 ctl_set_param_len_error(ctsio);
6413 ctl_done((union ctl_io *)ctsio);
6414 return (CTL_RETVAL_COMPLETE);
6418 * Allocate the data buffer and grab the user's data. In theory,
6419 * we shouldn't have to sanity check the parameter list length here
6420 * because the maximum size is 64K. We should be able to malloc
6421 * that much without too many problems.
6423 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6424 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6425 ctsio->kern_data_len = param_len;
6426 ctsio->kern_total_len = param_len;
6427 ctsio->kern_data_resid = 0;
6428 ctsio->kern_rel_offset = 0;
6429 ctsio->kern_sg_entries = 0;
6430 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6431 ctsio->be_move_done = ctl_config_move_done;
6432 ctl_datamove((union ctl_io *)ctsio);
6434 return (CTL_RETVAL_COMPLETE);
6437 switch (ctsio->cdb[0]) {
6438 case MODE_SELECT_6: {
6439 struct scsi_mode_header_6 *mh6;
6441 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6442 bd_len = mh6->blk_desc_len;
6445 case MODE_SELECT_10: {
6446 struct scsi_mode_header_10 *mh10;
6448 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6449 bd_len = scsi_2btoul(mh10->blk_desc_len);
6453 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6457 if (param_len < (header_size + bd_len)) {
6458 free(ctsio->kern_data_ptr, M_CTL);
6459 ctl_set_param_len_error(ctsio);
6460 ctl_done((union ctl_io *)ctsio);
6461 return (CTL_RETVAL_COMPLETE);
6465 * Set the IO_CONT flag, so that if this I/O gets passed to
6466 * ctl_config_write_done(), it'll get passed back to
6467 * ctl_do_mode_select() for further processing, or completion if
6470 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6471 ctsio->io_cont = ctl_do_mode_select;
6473 modepage_info = (union ctl_modepage_info *)
6474 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6476 memset(modepage_info, 0, sizeof(*modepage_info));
6478 len_left = param_len - header_size - bd_len;
6479 len_used = header_size + bd_len;
6481 modepage_info->header.len_left = len_left;
6482 modepage_info->header.len_used = len_used;
6484 return (ctl_do_mode_select((union ctl_io *)ctsio));
6488 ctl_mode_sense(struct ctl_scsiio *ctsio)
6490 struct ctl_lun *lun;
6491 int pc, page_code, dbd, llba, subpage;
6492 int alloc_len, page_len, header_len, total_len;
6493 struct scsi_mode_block_descr *block_desc;
6494 struct ctl_page_index *page_index;
6502 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6504 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6506 if (lun->be_lun->lun_type != T_DIRECT)
6511 switch (ctsio->cdb[0]) {
6512 case MODE_SENSE_6: {
6513 struct scsi_mode_sense_6 *cdb;
6515 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6517 header_len = sizeof(struct scsi_mode_hdr_6);
6518 if (cdb->byte2 & SMS_DBD)
6521 header_len += sizeof(struct scsi_mode_block_descr);
6523 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6524 page_code = cdb->page & SMS_PAGE_CODE;
6525 subpage = cdb->subpage;
6526 alloc_len = cdb->length;
6529 case MODE_SENSE_10: {
6530 struct scsi_mode_sense_10 *cdb;
6532 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6534 header_len = sizeof(struct scsi_mode_hdr_10);
6536 if (cdb->byte2 & SMS_DBD)
6539 header_len += sizeof(struct scsi_mode_block_descr);
6540 if (cdb->byte2 & SMS10_LLBAA)
6542 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6543 page_code = cdb->page & SMS_PAGE_CODE;
6544 subpage = cdb->subpage;
6545 alloc_len = scsi_2btoul(cdb->length);
6549 ctl_set_invalid_opcode(ctsio);
6550 ctl_done((union ctl_io *)ctsio);
6551 return (CTL_RETVAL_COMPLETE);
6552 break; /* NOTREACHED */
6556 * We have to make a first pass through to calculate the size of
6557 * the pages that match the user's query. Then we allocate enough
6558 * memory to hold it, and actually copy the data into the buffer.
6560 switch (page_code) {
6561 case SMS_ALL_PAGES_PAGE: {
6567 * At the moment, values other than 0 and 0xff here are
6568 * reserved according to SPC-3.
6570 if ((subpage != SMS_SUBPAGE_PAGE_0)
6571 && (subpage != SMS_SUBPAGE_ALL)) {
6572 ctl_set_invalid_field(ctsio,
6578 ctl_done((union ctl_io *)ctsio);
6579 return (CTL_RETVAL_COMPLETE);
6582 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6583 if ((control_dev != 0)
6584 && (lun->mode_pages.index[i].page_flags &
6585 CTL_PAGE_FLAG_DISK_ONLY))
6589 * We don't use this subpage if the user didn't
6590 * request all subpages.
6592 if ((lun->mode_pages.index[i].subpage != 0)
6593 && (subpage == SMS_SUBPAGE_PAGE_0))
6597 printf("found page %#x len %d\n",
6598 lun->mode_pages.index[i].page_code &
6600 lun->mode_pages.index[i].page_len);
6602 page_len += lun->mode_pages.index[i].page_len;
6611 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6612 /* Look for the right page code */
6613 if ((lun->mode_pages.index[i].page_code &
6614 SMPH_PC_MASK) != page_code)
6617 /* Look for the right subpage or the subpage wildcard*/
6618 if ((lun->mode_pages.index[i].subpage != subpage)
6619 && (subpage != SMS_SUBPAGE_ALL))
6622 /* Make sure the page is supported for this dev type */
6623 if ((control_dev != 0)
6624 && (lun->mode_pages.index[i].page_flags &
6625 CTL_PAGE_FLAG_DISK_ONLY))
6629 printf("found page %#x len %d\n",
6630 lun->mode_pages.index[i].page_code &
6632 lun->mode_pages.index[i].page_len);
6635 page_len += lun->mode_pages.index[i].page_len;
6638 if (page_len == 0) {
6639 ctl_set_invalid_field(ctsio,
6645 ctl_done((union ctl_io *)ctsio);
6646 return (CTL_RETVAL_COMPLETE);
6652 total_len = header_len + page_len;
6654 printf("header_len = %d, page_len = %d, total_len = %d\n",
6655 header_len, page_len, total_len);
6658 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6659 ctsio->kern_sg_entries = 0;
6660 ctsio->kern_data_resid = 0;
6661 ctsio->kern_rel_offset = 0;
6662 if (total_len < alloc_len) {
6663 ctsio->residual = alloc_len - total_len;
6664 ctsio->kern_data_len = total_len;
6665 ctsio->kern_total_len = total_len;
6667 ctsio->residual = 0;
6668 ctsio->kern_data_len = alloc_len;
6669 ctsio->kern_total_len = alloc_len;
6672 switch (ctsio->cdb[0]) {
6673 case MODE_SENSE_6: {
6674 struct scsi_mode_hdr_6 *header;
6676 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6678 header->datalen = ctl_min(total_len - 1, 254);
6681 header->block_descr_len = 0;
6683 header->block_descr_len =
6684 sizeof(struct scsi_mode_block_descr);
6685 block_desc = (struct scsi_mode_block_descr *)&header[1];
6688 case MODE_SENSE_10: {
6689 struct scsi_mode_hdr_10 *header;
6692 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6694 datalen = ctl_min(total_len - 2, 65533);
6695 scsi_ulto2b(datalen, header->datalen);
6697 scsi_ulto2b(0, header->block_descr_len);
6699 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6700 header->block_descr_len);
6701 block_desc = (struct scsi_mode_block_descr *)&header[1];
6705 panic("invalid CDB type %#x", ctsio->cdb[0]);
6706 break; /* NOTREACHED */
6710 * If we've got a disk, use its blocksize in the block
6711 * descriptor. Otherwise, just set it to 0.
6714 if (control_dev != 0)
6715 scsi_ulto3b(lun->be_lun->blocksize,
6716 block_desc->block_len);
6718 scsi_ulto3b(0, block_desc->block_len);
6721 switch (page_code) {
6722 case SMS_ALL_PAGES_PAGE: {
6725 data_used = header_len;
6726 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6727 struct ctl_page_index *page_index;
6729 page_index = &lun->mode_pages.index[i];
6731 if ((control_dev != 0)
6732 && (page_index->page_flags &
6733 CTL_PAGE_FLAG_DISK_ONLY))
6737 * We don't use this subpage if the user didn't
6738 * request all subpages. We already checked (above)
6739 * to make sure the user only specified a subpage
6740 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6742 if ((page_index->subpage != 0)
6743 && (subpage == SMS_SUBPAGE_PAGE_0))
6747 * Call the handler, if it exists, to update the
6748 * page to the latest values.
6750 if (page_index->sense_handler != NULL)
6751 page_index->sense_handler(ctsio, page_index,pc);
6753 memcpy(ctsio->kern_data_ptr + data_used,
6754 page_index->page_data +
6755 (page_index->page_len * pc),
6756 page_index->page_len);
6757 data_used += page_index->page_len;
6764 data_used = header_len;
6766 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6767 struct ctl_page_index *page_index;
6769 page_index = &lun->mode_pages.index[i];
6771 /* Look for the right page code */
6772 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6775 /* Look for the right subpage or the subpage wildcard*/
6776 if ((page_index->subpage != subpage)
6777 && (subpage != SMS_SUBPAGE_ALL))
6780 /* Make sure the page is supported for this dev type */
6781 if ((control_dev != 0)
6782 && (page_index->page_flags &
6783 CTL_PAGE_FLAG_DISK_ONLY))
6787 * Call the handler, if it exists, to update the
6788 * page to the latest values.
6790 if (page_index->sense_handler != NULL)
6791 page_index->sense_handler(ctsio, page_index,pc);
6793 memcpy(ctsio->kern_data_ptr + data_used,
6794 page_index->page_data +
6795 (page_index->page_len * pc),
6796 page_index->page_len);
6797 data_used += page_index->page_len;
6803 ctsio->scsi_status = SCSI_STATUS_OK;
6805 ctsio->be_move_done = ctl_config_move_done;
6806 ctl_datamove((union ctl_io *)ctsio);
6808 return (CTL_RETVAL_COMPLETE);
6812 ctl_read_capacity(struct ctl_scsiio *ctsio)
6814 struct scsi_read_capacity *cdb;
6815 struct scsi_read_capacity_data *data;
6816 struct ctl_lun *lun;
6819 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6821 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6823 lba = scsi_4btoul(cdb->addr);
6824 if (((cdb->pmi & SRC_PMI) == 0)
6826 ctl_set_invalid_field(/*ctsio*/ ctsio,
6832 ctl_done((union ctl_io *)ctsio);
6833 return (CTL_RETVAL_COMPLETE);
6836 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6838 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6839 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6840 ctsio->residual = 0;
6841 ctsio->kern_data_len = sizeof(*data);
6842 ctsio->kern_total_len = sizeof(*data);
6843 ctsio->kern_data_resid = 0;
6844 ctsio->kern_rel_offset = 0;
6845 ctsio->kern_sg_entries = 0;
6848 * If the maximum LBA is greater than 0xfffffffe, the user must
6849 * issue a SERVICE ACTION IN (16) command, with the read capacity
6850 * serivce action set.
6852 if (lun->be_lun->maxlba > 0xfffffffe)
6853 scsi_ulto4b(0xffffffff, data->addr);
6855 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6858 * XXX KDM this may not be 512 bytes...
6860 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6862 ctsio->scsi_status = SCSI_STATUS_OK;
6864 ctsio->be_move_done = ctl_config_move_done;
6865 ctl_datamove((union ctl_io *)ctsio);
6867 return (CTL_RETVAL_COMPLETE);
6871 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6873 struct scsi_read_capacity_16 *cdb;
6874 struct scsi_read_capacity_data_long *data;
6875 struct ctl_lun *lun;
6879 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6881 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6883 alloc_len = scsi_4btoul(cdb->alloc_len);
6884 lba = scsi_8btou64(cdb->addr);
6886 if ((cdb->reladr & SRC16_PMI)
6888 ctl_set_invalid_field(/*ctsio*/ ctsio,
6894 ctl_done((union ctl_io *)ctsio);
6895 return (CTL_RETVAL_COMPLETE);
6898 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6900 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6901 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6903 if (sizeof(*data) < alloc_len) {
6904 ctsio->residual = alloc_len - sizeof(*data);
6905 ctsio->kern_data_len = sizeof(*data);
6906 ctsio->kern_total_len = sizeof(*data);
6908 ctsio->residual = 0;
6909 ctsio->kern_data_len = alloc_len;
6910 ctsio->kern_total_len = alloc_len;
6912 ctsio->kern_data_resid = 0;
6913 ctsio->kern_rel_offset = 0;
6914 ctsio->kern_sg_entries = 0;
6916 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6917 /* XXX KDM this may not be 512 bytes... */
6918 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6920 ctsio->scsi_status = SCSI_STATUS_OK;
6922 ctsio->be_move_done = ctl_config_move_done;
6923 ctl_datamove((union ctl_io *)ctsio);
6925 return (CTL_RETVAL_COMPLETE);
6929 ctl_service_action_in(struct ctl_scsiio *ctsio)
6931 struct scsi_service_action_in *cdb;
6934 CTL_DEBUG_PRINT(("ctl_service_action_in\n"));
6936 cdb = (struct scsi_service_action_in *)ctsio->cdb;
6938 retval = CTL_RETVAL_COMPLETE;
6940 switch (cdb->service_action) {
6941 case SRC16_SERVICE_ACTION:
6942 retval = ctl_read_capacity_16(ctsio);
6945 ctl_set_invalid_field(/*ctsio*/ ctsio,
6951 ctl_done((union ctl_io *)ctsio);
6959 ctl_maintenance_in(struct ctl_scsiio *ctsio)
6961 struct scsi_maintenance_in *cdb;
6963 int alloc_len, total_len = 0;
6964 int num_target_port_groups, single;
6965 struct ctl_lun *lun;
6966 struct ctl_softc *softc;
6967 struct scsi_target_group_data *rtg_ptr;
6968 struct scsi_target_port_group_descriptor *tpg_desc_ptr1, *tpg_desc_ptr2;
6969 struct scsi_target_port_descriptor *tp_desc_ptr1_1, *tp_desc_ptr1_2,
6970 *tp_desc_ptr2_1, *tp_desc_ptr2_2;
6972 CTL_DEBUG_PRINT(("ctl_maintenance_in\n"));
6974 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
6975 softc = control_softc;
6976 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6978 retval = CTL_RETVAL_COMPLETE;
6980 if ((cdb->byte2 & SERVICE_ACTION_MASK) != SA_RPRT_TRGT_GRP) {
6981 ctl_set_invalid_field(/*ctsio*/ ctsio,
6987 ctl_done((union ctl_io *)ctsio);
6991 mtx_lock(&softc->ctl_lock);
6992 single = ctl_is_single;
6993 mtx_unlock(&softc->ctl_lock);
6996 num_target_port_groups = NUM_TARGET_PORT_GROUPS - 1;
6998 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7000 total_len = sizeof(struct scsi_target_group_data) +
7001 sizeof(struct scsi_target_port_group_descriptor) *
7002 num_target_port_groups +
7003 sizeof(struct scsi_target_port_descriptor) *
7004 NUM_PORTS_PER_GRP * num_target_port_groups;
7006 alloc_len = scsi_4btoul(cdb->length);
7008 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7010 ctsio->kern_sg_entries = 0;
7012 if (total_len < alloc_len) {
7013 ctsio->residual = alloc_len - total_len;
7014 ctsio->kern_data_len = total_len;
7015 ctsio->kern_total_len = total_len;
7017 ctsio->residual = 0;
7018 ctsio->kern_data_len = alloc_len;
7019 ctsio->kern_total_len = alloc_len;
7021 ctsio->kern_data_resid = 0;
7022 ctsio->kern_rel_offset = 0;
7024 rtg_ptr = (struct scsi_target_group_data *)ctsio->kern_data_ptr;
7026 tpg_desc_ptr1 = &rtg_ptr->groups[0];
7027 tp_desc_ptr1_1 = &tpg_desc_ptr1->descriptors[0];
7028 tp_desc_ptr1_2 = (struct scsi_target_port_descriptor *)
7029 &tp_desc_ptr1_1->desc_list[0];
7032 tpg_desc_ptr2 = (struct scsi_target_port_group_descriptor *)
7033 &tp_desc_ptr1_2->desc_list[0];
7034 tp_desc_ptr2_1 = &tpg_desc_ptr2->descriptors[0];
7035 tp_desc_ptr2_2 = (struct scsi_target_port_descriptor *)
7036 &tp_desc_ptr2_1->desc_list[0];
7038 tpg_desc_ptr2 = NULL;
7039 tp_desc_ptr2_1 = NULL;
7040 tp_desc_ptr2_2 = NULL;
7043 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7045 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7046 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7047 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7048 tpg_desc_ptr2->pref_state =
7049 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7051 tpg_desc_ptr1->pref_state =
7052 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7053 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7056 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7057 tpg_desc_ptr1->pref_state =
7058 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7059 tpg_desc_ptr2->pref_state = TPG_PRIMARY;
7061 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7062 tpg_desc_ptr2->pref_state =
7063 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7067 tpg_desc_ptr1->pref_state = TPG_PRIMARY;
7069 tpg_desc_ptr1->support = 0;
7070 tpg_desc_ptr1->target_port_group[1] = 1;
7071 tpg_desc_ptr1->status = TPG_IMPLICIT;
7072 tpg_desc_ptr1->target_port_count= NUM_PORTS_PER_GRP;
7075 tpg_desc_ptr2->support = 0;
7076 tpg_desc_ptr2->target_port_group[1] = 2;
7077 tpg_desc_ptr2->status = TPG_IMPLICIT;
7078 tpg_desc_ptr2->target_port_count = NUM_PORTS_PER_GRP;
7080 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7081 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7083 tp_desc_ptr2_1->relative_target_port_identifier[1] = 9;
7084 tp_desc_ptr2_2->relative_target_port_identifier[1] = 10;
7086 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS) {
7087 tp_desc_ptr1_1->relative_target_port_identifier[1] = 1;
7088 tp_desc_ptr1_2->relative_target_port_identifier[1] = 2;
7090 tp_desc_ptr1_1->relative_target_port_identifier[1] = 9;
7091 tp_desc_ptr1_2->relative_target_port_identifier[1] = 10;
7095 ctsio->be_move_done = ctl_config_move_done;
7097 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7098 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7099 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7100 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7101 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7103 ctl_datamove((union ctl_io *)ctsio);
7108 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7110 struct scsi_per_res_in *cdb;
7111 int alloc_len, total_len = 0;
7112 /* struct scsi_per_res_in_rsrv in_data; */
7113 struct ctl_lun *lun;
7114 struct ctl_softc *softc;
7116 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7118 softc = control_softc;
7120 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7122 alloc_len = scsi_2btoul(cdb->length);
7124 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7127 mtx_lock(&softc->ctl_lock);
7128 switch (cdb->action) {
7129 case SPRI_RK: /* read keys */
7130 total_len = sizeof(struct scsi_per_res_in_keys) +
7132 sizeof(struct scsi_per_res_key);
7134 case SPRI_RR: /* read reservation */
7135 if (lun->flags & CTL_LUN_PR_RESERVED)
7136 total_len = sizeof(struct scsi_per_res_in_rsrv);
7138 total_len = sizeof(struct scsi_per_res_in_header);
7140 case SPRI_RC: /* report capabilities */
7141 total_len = sizeof(struct scsi_per_res_cap);
7143 case SPRI_RS: /* read full status */
7145 mtx_unlock(&softc->ctl_lock);
7146 ctl_set_invalid_field(ctsio,
7152 ctl_done((union ctl_io *)ctsio);
7153 return (CTL_RETVAL_COMPLETE);
7154 break; /* NOTREACHED */
7156 mtx_unlock(&softc->ctl_lock);
7158 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7160 if (total_len < alloc_len) {
7161 ctsio->residual = alloc_len - total_len;
7162 ctsio->kern_data_len = total_len;
7163 ctsio->kern_total_len = total_len;
7165 ctsio->residual = 0;
7166 ctsio->kern_data_len = alloc_len;
7167 ctsio->kern_total_len = alloc_len;
7170 ctsio->kern_data_resid = 0;
7171 ctsio->kern_rel_offset = 0;
7172 ctsio->kern_sg_entries = 0;
7174 mtx_lock(&softc->ctl_lock);
7175 switch (cdb->action) {
7176 case SPRI_RK: { // read keys
7177 struct scsi_per_res_in_keys *res_keys;
7180 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7183 * We had to drop the lock to allocate our buffer, which
7184 * leaves time for someone to come in with another
7185 * persistent reservation. (That is unlikely, though,
7186 * since this should be the only persistent reservation
7187 * command active right now.)
7189 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7190 (lun->pr_key_count *
7191 sizeof(struct scsi_per_res_key)))){
7192 mtx_unlock(&softc->ctl_lock);
7193 free(ctsio->kern_data_ptr, M_CTL);
7194 printf("%s: reservation length changed, retrying\n",
7199 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7201 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7202 lun->pr_key_count, res_keys->header.length);
7204 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7205 if (!lun->per_res[i].registered)
7209 * We used lun->pr_key_count to calculate the
7210 * size to allocate. If it turns out the number of
7211 * initiators with the registered flag set is
7212 * larger than that (i.e. they haven't been kept in
7213 * sync), we've got a problem.
7215 if (key_count >= lun->pr_key_count) {
7217 csevent_log(CSC_CTL | CSC_SHELF_SW |
7219 csevent_LogType_Fault,
7220 csevent_AlertLevel_Yellow,
7221 csevent_FRU_ShelfController,
7222 csevent_FRU_Firmware,
7223 csevent_FRU_Unknown,
7224 "registered keys %d >= key "
7225 "count %d", key_count,
7231 memcpy(res_keys->keys[key_count].key,
7232 lun->per_res[i].res_key.key,
7233 ctl_min(sizeof(res_keys->keys[key_count].key),
7234 sizeof(lun->per_res[i].res_key)));
7239 case SPRI_RR: { // read reservation
7240 struct scsi_per_res_in_rsrv *res;
7241 int tmp_len, header_only;
7243 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7245 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7247 if (lun->flags & CTL_LUN_PR_RESERVED)
7249 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7250 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7251 res->header.length);
7254 tmp_len = sizeof(struct scsi_per_res_in_header);
7255 scsi_ulto4b(0, res->header.length);
7260 * We had to drop the lock to allocate our buffer, which
7261 * leaves time for someone to come in with another
7262 * persistent reservation. (That is unlikely, though,
7263 * since this should be the only persistent reservation
7264 * command active right now.)
7266 if (tmp_len != total_len) {
7267 mtx_unlock(&softc->ctl_lock);
7268 free(ctsio->kern_data_ptr, M_CTL);
7269 printf("%s: reservation status changed, retrying\n",
7275 * No reservation held, so we're done.
7277 if (header_only != 0)
7281 * If the registration is an All Registrants type, the key
7282 * is 0, since it doesn't really matter.
7284 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7285 memcpy(res->data.reservation,
7286 &lun->per_res[lun->pr_res_idx].res_key,
7287 sizeof(struct scsi_per_res_key));
7289 res->data.scopetype = lun->res_type;
7292 case SPRI_RC: //report capabilities
7294 struct scsi_per_res_cap *res_cap;
7297 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7298 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7299 res_cap->flags2 |= SPRI_TMV;
7300 type_mask = SPRI_TM_WR_EX_AR |
7306 scsi_ulto2b(type_mask, res_cap->type_mask);
7309 case SPRI_RS: //read full status
7312 * This is a bug, because we just checked for this above,
7313 * and should have returned an error.
7315 panic("Invalid PR type %x", cdb->action);
7316 break; /* NOTREACHED */
7318 mtx_unlock(&softc->ctl_lock);
7320 ctsio->be_move_done = ctl_config_move_done;
7322 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7323 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7324 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7325 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7326 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7328 ctl_datamove((union ctl_io *)ctsio);
7330 return (CTL_RETVAL_COMPLETE);
7334 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7338 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7339 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7340 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7341 struct scsi_per_res_out_parms* param)
7343 union ctl_ha_msg persis_io;
7349 if (sa_res_key == 0) {
7350 mtx_lock(&softc->ctl_lock);
7351 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7352 /* validate scope and type */
7353 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7355 mtx_unlock(&softc->ctl_lock);
7356 ctl_set_invalid_field(/*ctsio*/ ctsio,
7362 ctl_done((union ctl_io *)ctsio);
7366 if (type>8 || type==2 || type==4 || type==0) {
7367 mtx_unlock(&softc->ctl_lock);
7368 ctl_set_invalid_field(/*ctsio*/ ctsio,
7374 ctl_done((union ctl_io *)ctsio);
7378 /* temporarily unregister this nexus */
7379 lun->per_res[residx].registered = 0;
7382 * Unregister everybody else and build UA for
7385 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7386 if (lun->per_res[i].registered == 0)
7390 && i <CTL_MAX_INITIATORS)
7391 lun->pending_sense[i].ua_pending |=
7393 else if (persis_offset
7394 && i >= persis_offset)
7395 lun->pending_sense[i-persis_offset
7398 lun->per_res[i].registered = 0;
7399 memset(&lun->per_res[i].res_key, 0,
7400 sizeof(struct scsi_per_res_key));
7402 lun->per_res[residx].registered = 1;
7403 lun->pr_key_count = 1;
7404 lun->res_type = type;
7405 if (lun->res_type != SPR_TYPE_WR_EX_AR
7406 && lun->res_type != SPR_TYPE_EX_AC_AR)
7407 lun->pr_res_idx = residx;
7409 mtx_unlock(&softc->ctl_lock);
7410 /* send msg to other side */
7411 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7412 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7413 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7414 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7415 persis_io.pr.pr_info.res_type = type;
7416 memcpy(persis_io.pr.pr_info.sa_res_key,
7417 param->serv_act_res_key,
7418 sizeof(param->serv_act_res_key));
7419 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7420 &persis_io, sizeof(persis_io), 0)) >
7421 CTL_HA_STATUS_SUCCESS) {
7422 printf("CTL:Persis Out error returned "
7423 "from ctl_ha_msg_send %d\n",
7427 /* not all registrants */
7428 mtx_unlock(&softc->ctl_lock);
7429 free(ctsio->kern_data_ptr, M_CTL);
7430 ctl_set_invalid_field(ctsio,
7436 ctl_done((union ctl_io *)ctsio);
7439 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7440 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7443 mtx_lock(&softc->ctl_lock);
7444 if (res_key == sa_res_key) {
7447 * The spec implies this is not good but doesn't
7448 * say what to do. There are two choices either
7449 * generate a res conflict or check condition
7450 * with illegal field in parameter data. Since
7451 * that is what is done when the sa_res_key is
7452 * zero I'll take that approach since this has
7453 * to do with the sa_res_key.
7455 mtx_unlock(&softc->ctl_lock);
7456 free(ctsio->kern_data_ptr, M_CTL);
7457 ctl_set_invalid_field(ctsio,
7463 ctl_done((union ctl_io *)ctsio);
7467 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7468 if (lun->per_res[i].registered
7469 && memcmp(param->serv_act_res_key,
7470 lun->per_res[i].res_key.key,
7471 sizeof(struct scsi_per_res_key)) != 0)
7475 lun->per_res[i].registered = 0;
7476 memset(&lun->per_res[i].res_key, 0,
7477 sizeof(struct scsi_per_res_key));
7478 lun->pr_key_count--;
7481 && i < CTL_MAX_INITIATORS)
7482 lun->pending_sense[i].ua_pending |=
7484 else if (persis_offset
7485 && i >= persis_offset)
7486 lun->pending_sense[i-persis_offset].ua_pending|=
7489 mtx_unlock(&softc->ctl_lock);
7491 free(ctsio->kern_data_ptr, M_CTL);
7492 ctl_set_reservation_conflict(ctsio);
7493 ctl_done((union ctl_io *)ctsio);
7494 return (CTL_RETVAL_COMPLETE);
7496 /* send msg to other side */
7497 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7498 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7499 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7500 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7501 persis_io.pr.pr_info.res_type = type;
7502 memcpy(persis_io.pr.pr_info.sa_res_key,
7503 param->serv_act_res_key,
7504 sizeof(param->serv_act_res_key));
7505 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7506 &persis_io, sizeof(persis_io), 0)) >
7507 CTL_HA_STATUS_SUCCESS) {
7508 printf("CTL:Persis Out error returned from "
7509 "ctl_ha_msg_send %d\n", isc_retval);
7512 /* Reserved but not all registrants */
7513 /* sa_res_key is res holder */
7514 if (memcmp(param->serv_act_res_key,
7515 lun->per_res[lun->pr_res_idx].res_key.key,
7516 sizeof(struct scsi_per_res_key)) == 0) {
7517 /* validate scope and type */
7518 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7520 ctl_set_invalid_field(/*ctsio*/ ctsio,
7526 ctl_done((union ctl_io *)ctsio);
7530 if (type>8 || type==2 || type==4 || type==0) {
7531 ctl_set_invalid_field(/*ctsio*/ ctsio,
7537 ctl_done((union ctl_io *)ctsio);
7543 * if sa_res_key != res_key remove all
7544 * registrants w/sa_res_key and generate UA
7545 * for these registrants(Registrations
7546 * Preempted) if it wasn't an exclusive
7547 * reservation generate UA(Reservations
7548 * Preempted) for all other registered nexuses
7549 * if the type has changed. Establish the new
7550 * reservation and holder. If res_key and
7551 * sa_res_key are the same do the above
7552 * except don't unregister the res holder.
7556 * Temporarily unregister so it won't get
7557 * removed or UA generated
7559 lun->per_res[residx].registered = 0;
7560 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7561 if (lun->per_res[i].registered == 0)
7564 if (memcmp(param->serv_act_res_key,
7565 lun->per_res[i].res_key.key,
7566 sizeof(struct scsi_per_res_key)) == 0) {
7567 lun->per_res[i].registered = 0;
7568 memset(&lun->per_res[i].res_key,
7570 sizeof(struct scsi_per_res_key));
7571 lun->pr_key_count--;
7574 && i < CTL_MAX_INITIATORS)
7575 lun->pending_sense[i
7578 else if (persis_offset
7579 && i >= persis_offset)
7581 i-persis_offset].ua_pending |=
7583 } else if (type != lun->res_type
7584 && (lun->res_type == SPR_TYPE_WR_EX_RO
7585 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7587 && i < CTL_MAX_INITIATORS)
7588 lun->pending_sense[i
7591 else if (persis_offset
7592 && i >= persis_offset)
7599 lun->per_res[residx].registered = 1;
7600 lun->res_type = type;
7601 if (lun->res_type != SPR_TYPE_WR_EX_AR
7602 && lun->res_type != SPR_TYPE_EX_AC_AR)
7603 lun->pr_res_idx = residx;
7606 CTL_PR_ALL_REGISTRANTS;
7608 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7609 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7610 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7611 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7612 persis_io.pr.pr_info.res_type = type;
7613 memcpy(persis_io.pr.pr_info.sa_res_key,
7614 param->serv_act_res_key,
7615 sizeof(param->serv_act_res_key));
7616 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7617 &persis_io, sizeof(persis_io), 0)) >
7618 CTL_HA_STATUS_SUCCESS) {
7619 printf("CTL:Persis Out error returned "
7620 "from ctl_ha_msg_send %d\n",
7625 * sa_res_key is not the res holder just
7626 * remove registrants
7629 mtx_lock(&softc->ctl_lock);
7631 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7632 if (memcmp(param->serv_act_res_key,
7633 lun->per_res[i].res_key.key,
7634 sizeof(struct scsi_per_res_key)) != 0)
7638 lun->per_res[i].registered = 0;
7639 memset(&lun->per_res[i].res_key, 0,
7640 sizeof(struct scsi_per_res_key));
7641 lun->pr_key_count--;
7644 && i < CTL_MAX_INITIATORS)
7645 lun->pending_sense[i].ua_pending |=
7647 else if (persis_offset
7648 && i >= persis_offset)
7650 i-persis_offset].ua_pending |=
7655 mtx_unlock(&softc->ctl_lock);
7656 free(ctsio->kern_data_ptr, M_CTL);
7657 ctl_set_reservation_conflict(ctsio);
7658 ctl_done((union ctl_io *)ctsio);
7661 mtx_unlock(&softc->ctl_lock);
7662 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7663 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7664 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7665 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7666 persis_io.pr.pr_info.res_type = type;
7667 memcpy(persis_io.pr.pr_info.sa_res_key,
7668 param->serv_act_res_key,
7669 sizeof(param->serv_act_res_key));
7670 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
7671 &persis_io, sizeof(persis_io), 0)) >
7672 CTL_HA_STATUS_SUCCESS) {
7673 printf("CTL:Persis Out error returned "
7674 "from ctl_ha_msg_send %d\n",
7680 lun->PRGeneration++;
7686 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7690 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7691 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7692 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
7693 msg->pr.pr_info.sa_res_key,
7694 sizeof(struct scsi_per_res_key)) != 0) {
7695 uint64_t sa_res_key;
7696 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7698 if (sa_res_key == 0) {
7699 /* temporarily unregister this nexus */
7700 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7703 * Unregister everybody else and build UA for
7706 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7707 if (lun->per_res[i].registered == 0)
7711 && i < CTL_MAX_INITIATORS)
7712 lun->pending_sense[i].ua_pending |=
7714 else if (persis_offset && i >= persis_offset)
7715 lun->pending_sense[i -
7716 persis_offset].ua_pending |=
7718 lun->per_res[i].registered = 0;
7719 memset(&lun->per_res[i].res_key, 0,
7720 sizeof(struct scsi_per_res_key));
7723 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7724 lun->pr_key_count = 1;
7725 lun->res_type = msg->pr.pr_info.res_type;
7726 if (lun->res_type != SPR_TYPE_WR_EX_AR
7727 && lun->res_type != SPR_TYPE_EX_AC_AR)
7728 lun->pr_res_idx = msg->pr.pr_info.residx;
7730 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7731 if (memcmp(msg->pr.pr_info.sa_res_key,
7732 lun->per_res[i].res_key.key,
7733 sizeof(struct scsi_per_res_key)) != 0)
7736 lun->per_res[i].registered = 0;
7737 memset(&lun->per_res[i].res_key, 0,
7738 sizeof(struct scsi_per_res_key));
7739 lun->pr_key_count--;
7742 && i < persis_offset)
7743 lun->pending_sense[i].ua_pending |=
7745 else if (persis_offset
7746 && i >= persis_offset)
7747 lun->pending_sense[i -
7748 persis_offset].ua_pending |=
7754 * Temporarily unregister so it won't get removed
7757 lun->per_res[msg->pr.pr_info.residx].registered = 0;
7758 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7759 if (lun->per_res[i].registered == 0)
7762 if (memcmp(msg->pr.pr_info.sa_res_key,
7763 lun->per_res[i].res_key.key,
7764 sizeof(struct scsi_per_res_key)) == 0) {
7765 lun->per_res[i].registered = 0;
7766 memset(&lun->per_res[i].res_key, 0,
7767 sizeof(struct scsi_per_res_key));
7768 lun->pr_key_count--;
7770 && i < CTL_MAX_INITIATORS)
7771 lun->pending_sense[i].ua_pending |=
7773 else if (persis_offset
7774 && i >= persis_offset)
7775 lun->pending_sense[i -
7776 persis_offset].ua_pending |=
7778 } else if (msg->pr.pr_info.res_type != lun->res_type
7779 && (lun->res_type == SPR_TYPE_WR_EX_RO
7780 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
7782 && i < persis_offset)
7783 lun->pending_sense[i
7786 else if (persis_offset
7787 && i >= persis_offset)
7788 lun->pending_sense[i -
7789 persis_offset].ua_pending |=
7793 lun->per_res[msg->pr.pr_info.residx].registered = 1;
7794 lun->res_type = msg->pr.pr_info.res_type;
7795 if (lun->res_type != SPR_TYPE_WR_EX_AR
7796 && lun->res_type != SPR_TYPE_EX_AC_AR)
7797 lun->pr_res_idx = msg->pr.pr_info.residx;
7799 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7801 lun->PRGeneration++;
7807 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
7811 u_int32_t param_len;
7812 struct scsi_per_res_out *cdb;
7813 struct ctl_lun *lun;
7814 struct scsi_per_res_out_parms* param;
7815 struct ctl_softc *softc;
7817 uint64_t res_key, sa_res_key;
7819 union ctl_ha_msg persis_io;
7822 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
7824 retval = CTL_RETVAL_COMPLETE;
7826 softc = control_softc;
7828 cdb = (struct scsi_per_res_out *)ctsio->cdb;
7829 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7832 * We only support whole-LUN scope. The scope & type are ignored for
7833 * register, register and ignore existing key and clear.
7834 * We sometimes ignore scope and type on preempts too!!
7835 * Verify reservation type here as well.
7837 type = cdb->scope_type & SPR_TYPE_MASK;
7838 if ((cdb->action == SPRO_RESERVE)
7839 || (cdb->action == SPRO_RELEASE)) {
7840 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
7841 ctl_set_invalid_field(/*ctsio*/ ctsio,
7847 ctl_done((union ctl_io *)ctsio);
7848 return (CTL_RETVAL_COMPLETE);
7851 if (type>8 || type==2 || type==4 || type==0) {
7852 ctl_set_invalid_field(/*ctsio*/ ctsio,
7858 ctl_done((union ctl_io *)ctsio);
7859 return (CTL_RETVAL_COMPLETE);
7863 switch (cdb->action & SPRO_ACTION_MASK) {
7874 ctl_set_invalid_field(/*ctsio*/ ctsio,
7880 ctl_done((union ctl_io *)ctsio);
7881 return (CTL_RETVAL_COMPLETE);
7882 break; /* NOTREACHED */
7885 param_len = scsi_4btoul(cdb->length);
7887 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
7888 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
7889 ctsio->kern_data_len = param_len;
7890 ctsio->kern_total_len = param_len;
7891 ctsio->kern_data_resid = 0;
7892 ctsio->kern_rel_offset = 0;
7893 ctsio->kern_sg_entries = 0;
7894 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7895 ctsio->be_move_done = ctl_config_move_done;
7896 ctl_datamove((union ctl_io *)ctsio);
7898 return (CTL_RETVAL_COMPLETE);
7901 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
7903 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
7904 res_key = scsi_8btou64(param->res_key.key);
7905 sa_res_key = scsi_8btou64(param->serv_act_res_key);
7908 * Validate the reservation key here except for SPRO_REG_IGNO
7909 * This must be done for all other service actions
7911 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
7912 mtx_lock(&softc->ctl_lock);
7913 if (lun->per_res[residx].registered) {
7914 if (memcmp(param->res_key.key,
7915 lun->per_res[residx].res_key.key,
7916 ctl_min(sizeof(param->res_key),
7917 sizeof(lun->per_res[residx].res_key))) != 0) {
7919 * The current key passed in doesn't match
7920 * the one the initiator previously
7923 mtx_unlock(&softc->ctl_lock);
7924 free(ctsio->kern_data_ptr, M_CTL);
7925 ctl_set_reservation_conflict(ctsio);
7926 ctl_done((union ctl_io *)ctsio);
7927 return (CTL_RETVAL_COMPLETE);
7929 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
7931 * We are not registered
7933 mtx_unlock(&softc->ctl_lock);
7934 free(ctsio->kern_data_ptr, M_CTL);
7935 ctl_set_reservation_conflict(ctsio);
7936 ctl_done((union ctl_io *)ctsio);
7937 return (CTL_RETVAL_COMPLETE);
7938 } else if (res_key != 0) {
7940 * We are not registered and trying to register but
7941 * the register key isn't zero.
7943 mtx_unlock(&softc->ctl_lock);
7944 free(ctsio->kern_data_ptr, M_CTL);
7945 ctl_set_reservation_conflict(ctsio);
7946 ctl_done((union ctl_io *)ctsio);
7947 return (CTL_RETVAL_COMPLETE);
7949 mtx_unlock(&softc->ctl_lock);
7952 switch (cdb->action & SPRO_ACTION_MASK) {
7954 case SPRO_REG_IGNO: {
7957 printf("Registration received\n");
7961 * We don't support any of these options, as we report in
7962 * the read capabilities request (see
7963 * ctl_persistent_reserve_in(), above).
7965 if ((param->flags & SPR_SPEC_I_PT)
7966 || (param->flags & SPR_ALL_TG_PT)
7967 || (param->flags & SPR_APTPL)) {
7970 if (param->flags & SPR_APTPL)
7972 else if (param->flags & SPR_ALL_TG_PT)
7974 else /* SPR_SPEC_I_PT */
7977 free(ctsio->kern_data_ptr, M_CTL);
7978 ctl_set_invalid_field(ctsio,
7984 ctl_done((union ctl_io *)ctsio);
7985 return (CTL_RETVAL_COMPLETE);
7988 mtx_lock(&softc->ctl_lock);
7991 * The initiator wants to clear the
7994 if (sa_res_key == 0) {
7996 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
7997 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
7998 && !lun->per_res[residx].registered)) {
7999 mtx_unlock(&softc->ctl_lock);
8003 lun->per_res[residx].registered = 0;
8004 memset(&lun->per_res[residx].res_key,
8005 0, sizeof(lun->per_res[residx].res_key));
8006 lun->pr_key_count--;
8008 if (residx == lun->pr_res_idx) {
8009 lun->flags &= ~CTL_LUN_PR_RESERVED;
8010 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8012 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8013 || lun->res_type == SPR_TYPE_EX_AC_RO)
8014 && lun->pr_key_count) {
8016 * If the reservation is a registrants
8017 * only type we need to generate a UA
8018 * for other registered inits. The
8019 * sense code should be RESERVATIONS
8023 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8025 i+persis_offset].registered
8028 lun->pending_sense[i
8034 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8035 if (lun->pr_key_count==0) {
8036 lun->flags &= ~CTL_LUN_PR_RESERVED;
8038 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8041 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8042 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8043 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8044 persis_io.pr.pr_info.residx = residx;
8045 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8046 &persis_io, sizeof(persis_io), 0 )) >
8047 CTL_HA_STATUS_SUCCESS) {
8048 printf("CTL:Persis Out error returned from "
8049 "ctl_ha_msg_send %d\n", isc_retval);
8051 mtx_unlock(&softc->ctl_lock);
8052 } else /* sa_res_key != 0 */ {
8055 * If we aren't registered currently then increment
8056 * the key count and set the registered flag.
8058 if (!lun->per_res[residx].registered) {
8059 lun->pr_key_count++;
8060 lun->per_res[residx].registered = 1;
8063 memcpy(&lun->per_res[residx].res_key,
8064 param->serv_act_res_key,
8065 ctl_min(sizeof(param->serv_act_res_key),
8066 sizeof(lun->per_res[residx].res_key)));
8068 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8069 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8070 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8071 persis_io.pr.pr_info.residx = residx;
8072 memcpy(persis_io.pr.pr_info.sa_res_key,
8073 param->serv_act_res_key,
8074 sizeof(param->serv_act_res_key));
8075 mtx_unlock(&softc->ctl_lock);
8076 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8077 &persis_io, sizeof(persis_io), 0)) >
8078 CTL_HA_STATUS_SUCCESS) {
8079 printf("CTL:Persis Out error returned from "
8080 "ctl_ha_msg_send %d\n", isc_retval);
8083 lun->PRGeneration++;
8089 printf("Reserve executed type %d\n", type);
8091 mtx_lock(&softc->ctl_lock);
8092 if (lun->flags & CTL_LUN_PR_RESERVED) {
8094 * if this isn't the reservation holder and it's
8095 * not a "all registrants" type or if the type is
8096 * different then we have a conflict
8098 if ((lun->pr_res_idx != residx
8099 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8100 || lun->res_type != type) {
8101 mtx_unlock(&softc->ctl_lock);
8102 free(ctsio->kern_data_ptr, M_CTL);
8103 ctl_set_reservation_conflict(ctsio);
8104 ctl_done((union ctl_io *)ctsio);
8105 return (CTL_RETVAL_COMPLETE);
8107 } else /* create a reservation */ {
8109 * If it's not an "all registrants" type record
8110 * reservation holder
8112 if (type != SPR_TYPE_WR_EX_AR
8113 && type != SPR_TYPE_EX_AC_AR)
8114 lun->pr_res_idx = residx; /* Res holder */
8116 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8118 lun->flags |= CTL_LUN_PR_RESERVED;
8119 lun->res_type = type;
8121 mtx_unlock(&softc->ctl_lock);
8123 /* send msg to other side */
8124 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8125 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8126 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8127 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8128 persis_io.pr.pr_info.res_type = type;
8129 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8130 &persis_io, sizeof(persis_io), 0)) >
8131 CTL_HA_STATUS_SUCCESS) {
8132 printf("CTL:Persis Out error returned from "
8133 "ctl_ha_msg_send %d\n", isc_retval);
8139 mtx_lock(&softc->ctl_lock);
8140 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8141 /* No reservation exists return good status */
8142 mtx_unlock(&softc->ctl_lock);
8146 * Is this nexus a reservation holder?
8148 if (lun->pr_res_idx != residx
8149 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8151 * not a res holder return good status but
8154 mtx_unlock(&softc->ctl_lock);
8158 if (lun->res_type != type) {
8159 mtx_unlock(&softc->ctl_lock);
8160 free(ctsio->kern_data_ptr, M_CTL);
8161 ctl_set_illegal_pr_release(ctsio);
8162 ctl_done((union ctl_io *)ctsio);
8163 return (CTL_RETVAL_COMPLETE);
8166 /* okay to release */
8167 lun->flags &= ~CTL_LUN_PR_RESERVED;
8168 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8172 * if this isn't an exclusive access
8173 * res generate UA for all other
8176 if (type != SPR_TYPE_EX_AC
8177 && type != SPR_TYPE_WR_EX) {
8179 * temporarily unregister so we don't generate UA
8181 lun->per_res[residx].registered = 0;
8183 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8184 if (lun->per_res[i+persis_offset].registered
8187 lun->pending_sense[i].ua_pending |=
8191 lun->per_res[residx].registered = 1;
8193 mtx_unlock(&softc->ctl_lock);
8194 /* Send msg to other side */
8195 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8196 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8197 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8198 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8199 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8200 printf("CTL:Persis Out error returned from "
8201 "ctl_ha_msg_send %d\n", isc_retval);
8206 /* send msg to other side */
8208 mtx_lock(&softc->ctl_lock);
8209 lun->flags &= ~CTL_LUN_PR_RESERVED;
8211 lun->pr_key_count = 0;
8212 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8215 memset(&lun->per_res[residx].res_key,
8216 0, sizeof(lun->per_res[residx].res_key));
8217 lun->per_res[residx].registered = 0;
8219 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8220 if (lun->per_res[i].registered) {
8221 if (!persis_offset && i < CTL_MAX_INITIATORS)
8222 lun->pending_sense[i].ua_pending |=
8224 else if (persis_offset && i >= persis_offset)
8225 lun->pending_sense[i-persis_offset
8226 ].ua_pending |= CTL_UA_RES_PREEMPT;
8228 memset(&lun->per_res[i].res_key,
8229 0, sizeof(struct scsi_per_res_key));
8230 lun->per_res[i].registered = 0;
8232 lun->PRGeneration++;
8233 mtx_unlock(&softc->ctl_lock);
8234 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8235 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8236 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8237 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8238 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8239 printf("CTL:Persis Out error returned from "
8240 "ctl_ha_msg_send %d\n", isc_retval);
8244 case SPRO_PREEMPT: {
8247 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8248 residx, ctsio, cdb, param);
8250 return (CTL_RETVAL_COMPLETE);
8256 free(ctsio->kern_data_ptr, M_CTL);
8257 ctl_set_invalid_field(/*ctsio*/ ctsio,
8263 ctl_done((union ctl_io *)ctsio);
8264 return (CTL_RETVAL_COMPLETE);
8265 break; /* NOTREACHED */
8269 free(ctsio->kern_data_ptr, M_CTL);
8270 ctl_set_success(ctsio);
8271 ctl_done((union ctl_io *)ctsio);
8277 * This routine is for handling a message from the other SC pertaining to
8278 * persistent reserve out. All the error checking will have been done
8279 * so only perorming the action need be done here to keep the two
8283 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8285 struct ctl_lun *lun;
8286 struct ctl_softc *softc;
8290 softc = control_softc;
8292 mtx_lock(&softc->ctl_lock);
8294 targ_lun = msg->hdr.nexus.targ_lun;
8295 if (msg->hdr.nexus.lun_map_fn != NULL)
8296 targ_lun = msg->hdr.nexus.lun_map_fn(msg->hdr.nexus.lun_map_arg, targ_lun);
8297 lun = softc->ctl_luns[targ_lun];
8298 switch(msg->pr.pr_info.action) {
8299 case CTL_PR_REG_KEY:
8300 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8301 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8302 lun->pr_key_count++;
8304 lun->PRGeneration++;
8305 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8306 msg->pr.pr_info.sa_res_key,
8307 sizeof(struct scsi_per_res_key));
8310 case CTL_PR_UNREG_KEY:
8311 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8312 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8313 0, sizeof(struct scsi_per_res_key));
8314 lun->pr_key_count--;
8316 /* XXX Need to see if the reservation has been released */
8317 /* if so do we need to generate UA? */
8318 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8319 lun->flags &= ~CTL_LUN_PR_RESERVED;
8320 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8322 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8323 || lun->res_type == SPR_TYPE_EX_AC_RO)
8324 && lun->pr_key_count) {
8326 * If the reservation is a registrants
8327 * only type we need to generate a UA
8328 * for other registered inits. The
8329 * sense code should be RESERVATIONS
8333 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8335 persis_offset].registered == 0)
8338 lun->pending_sense[i
8344 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8345 if (lun->pr_key_count==0) {
8346 lun->flags &= ~CTL_LUN_PR_RESERVED;
8348 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8351 lun->PRGeneration++;
8354 case CTL_PR_RESERVE:
8355 lun->flags |= CTL_LUN_PR_RESERVED;
8356 lun->res_type = msg->pr.pr_info.res_type;
8357 lun->pr_res_idx = msg->pr.pr_info.residx;
8361 case CTL_PR_RELEASE:
8363 * if this isn't an exclusive access res generate UA for all
8364 * other registrants.
8366 if (lun->res_type != SPR_TYPE_EX_AC
8367 && lun->res_type != SPR_TYPE_WR_EX) {
8368 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8369 if (lun->per_res[i+persis_offset].registered)
8370 lun->pending_sense[i].ua_pending |=
8374 lun->flags &= ~CTL_LUN_PR_RESERVED;
8375 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8379 case CTL_PR_PREEMPT:
8380 ctl_pro_preempt_other(lun, msg);
8383 lun->flags &= ~CTL_LUN_PR_RESERVED;
8385 lun->pr_key_count = 0;
8386 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8388 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8389 if (lun->per_res[i].registered == 0)
8392 && i < CTL_MAX_INITIATORS)
8393 lun->pending_sense[i].ua_pending |=
8395 else if (persis_offset
8396 && i >= persis_offset)
8397 lun->pending_sense[i-persis_offset].ua_pending|=
8399 memset(&lun->per_res[i].res_key, 0,
8400 sizeof(struct scsi_per_res_key));
8401 lun->per_res[i].registered = 0;
8403 lun->PRGeneration++;
8407 mtx_unlock(&softc->ctl_lock);
8411 ctl_read_write(struct ctl_scsiio *ctsio)
8413 struct ctl_lun *lun;
8414 struct ctl_lba_len lbalen;
8416 uint32_t num_blocks;
8417 int reladdr, fua, dpo, ebp;
8421 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8423 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8430 retval = CTL_RETVAL_COMPLETE;
8432 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8433 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8434 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8438 * XXX KDM need a lock here.
8440 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8441 if ((lun->res_type == SPR_TYPE_EX_AC
8442 && residx != lun->pr_res_idx)
8443 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8444 || lun->res_type == SPR_TYPE_EX_AC_AR)
8445 && !lun->per_res[residx].registered)) {
8446 ctl_set_reservation_conflict(ctsio);
8447 ctl_done((union ctl_io *)ctsio);
8448 return (CTL_RETVAL_COMPLETE);
8452 switch (ctsio->cdb[0]) {
8455 struct scsi_rw_6 *cdb;
8457 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8459 lba = scsi_3btoul(cdb->addr);
8460 /* only 5 bits are valid in the most significant address byte */
8462 num_blocks = cdb->length;
8464 * This is correct according to SBC-2.
8466 if (num_blocks == 0)
8472 struct scsi_rw_10 *cdb;
8474 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8476 if (cdb->byte2 & SRW10_RELADDR)
8478 if (cdb->byte2 & SRW10_FUA)
8480 if (cdb->byte2 & SRW10_DPO)
8483 if ((cdb->opcode == WRITE_10)
8484 && (cdb->byte2 & SRW10_EBP))
8487 lba = scsi_4btoul(cdb->addr);
8488 num_blocks = scsi_2btoul(cdb->length);
8491 case WRITE_VERIFY_10: {
8492 struct scsi_write_verify_10 *cdb;
8494 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8497 * XXX KDM we should do actual write verify support at some
8498 * point. This is obviously fake, we're just translating
8499 * things to a write. So we don't even bother checking the
8500 * BYTCHK field, since we don't do any verification. If
8501 * the user asks for it, we'll just pretend we did it.
8503 if (cdb->byte2 & SWV_DPO)
8506 lba = scsi_4btoul(cdb->addr);
8507 num_blocks = scsi_2btoul(cdb->length);
8512 struct scsi_rw_12 *cdb;
8514 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8516 if (cdb->byte2 & SRW12_RELADDR)
8518 if (cdb->byte2 & SRW12_FUA)
8520 if (cdb->byte2 & SRW12_DPO)
8522 lba = scsi_4btoul(cdb->addr);
8523 num_blocks = scsi_4btoul(cdb->length);
8526 case WRITE_VERIFY_12: {
8527 struct scsi_write_verify_12 *cdb;
8529 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8531 if (cdb->byte2 & SWV_DPO)
8534 lba = scsi_4btoul(cdb->addr);
8535 num_blocks = scsi_4btoul(cdb->length);
8541 struct scsi_rw_16 *cdb;
8543 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8545 if (cdb->byte2 & SRW12_RELADDR)
8547 if (cdb->byte2 & SRW12_FUA)
8549 if (cdb->byte2 & SRW12_DPO)
8552 lba = scsi_8btou64(cdb->addr);
8553 num_blocks = scsi_4btoul(cdb->length);
8556 case WRITE_VERIFY_16: {
8557 struct scsi_write_verify_16 *cdb;
8559 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8561 if (cdb->byte2 & SWV_DPO)
8564 lba = scsi_8btou64(cdb->addr);
8565 num_blocks = scsi_4btoul(cdb->length);
8570 * We got a command we don't support. This shouldn't
8571 * happen, commands should be filtered out above us.
8573 ctl_set_invalid_opcode(ctsio);
8574 ctl_done((union ctl_io *)ctsio);
8576 return (CTL_RETVAL_COMPLETE);
8577 break; /* NOTREACHED */
8581 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
8582 * interesting for us, but if RAIDCore is in write-back mode,
8583 * getting it to do write-through for a particular transaction may
8587 * We don't support relative addressing. That also requires
8588 * supporting linked commands, which we don't do.
8591 ctl_set_invalid_field(ctsio,
8597 ctl_done((union ctl_io *)ctsio);
8598 return (CTL_RETVAL_COMPLETE);
8602 * The first check is to make sure we're in bounds, the second
8603 * check is to catch wrap-around problems. If the lba + num blocks
8604 * is less than the lba, then we've wrapped around and the block
8605 * range is invalid anyway.
8607 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8608 || ((lba + num_blocks) < lba)) {
8609 ctl_set_lba_out_of_range(ctsio);
8610 ctl_done((union ctl_io *)ctsio);
8611 return (CTL_RETVAL_COMPLETE);
8615 * According to SBC-3, a transfer length of 0 is not an error.
8616 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8617 * translates to 256 blocks for those commands.
8619 if (num_blocks == 0) {
8620 ctl_set_success(ctsio);
8621 ctl_done((union ctl_io *)ctsio);
8622 return (CTL_RETVAL_COMPLETE);
8626 lbalen.len = num_blocks;
8627 memcpy(ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes, &lbalen,
8630 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8632 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8638 ctl_report_luns(struct ctl_scsiio *ctsio)
8640 struct scsi_report_luns *cdb;
8641 struct scsi_report_luns_data *lun_data;
8642 struct ctl_lun *lun, *request_lun;
8643 int num_luns, retval;
8644 uint32_t alloc_len, lun_datalen;
8645 int num_filled, well_known;
8646 uint32_t initidx, targ_lun_id, lun_id;
8648 retval = CTL_RETVAL_COMPLETE;
8651 cdb = (struct scsi_report_luns *)ctsio->cdb;
8653 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8655 mtx_lock(&control_softc->ctl_lock);
8656 num_luns = control_softc->num_luns;
8657 mtx_unlock(&control_softc->ctl_lock);
8659 switch (cdb->select_report) {
8660 case RPL_REPORT_DEFAULT:
8661 case RPL_REPORT_ALL:
8663 case RPL_REPORT_WELLKNOWN:
8668 ctl_set_invalid_field(ctsio,
8674 ctl_done((union ctl_io *)ctsio);
8676 break; /* NOTREACHED */
8679 alloc_len = scsi_4btoul(cdb->length);
8681 * The initiator has to allocate at least 16 bytes for this request,
8682 * so he can at least get the header and the first LUN. Otherwise
8683 * we reject the request (per SPC-3 rev 14, section 6.21).
8685 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
8686 sizeof(struct scsi_report_luns_lundata))) {
8687 ctl_set_invalid_field(ctsio,
8693 ctl_done((union ctl_io *)ctsio);
8697 request_lun = (struct ctl_lun *)
8698 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8700 lun_datalen = sizeof(*lun_data) +
8701 (num_luns * sizeof(struct scsi_report_luns_lundata));
8703 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
8704 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
8705 ctsio->kern_sg_entries = 0;
8707 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8709 mtx_lock(&control_softc->ctl_lock);
8710 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
8711 lun_id = targ_lun_id;
8712 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
8713 lun_id = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, lun_id);
8714 if (lun_id >= CTL_MAX_LUNS)
8716 lun = control_softc->ctl_luns[lun_id];
8720 if (targ_lun_id <= 0xff) {
8722 * Peripheral addressing method, bus number 0.
8724 lun_data->luns[num_filled].lundata[0] =
8725 RPL_LUNDATA_ATYP_PERIPH;
8726 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
8728 } else if (targ_lun_id <= 0x3fff) {
8730 * Flat addressing method.
8732 lun_data->luns[num_filled].lundata[0] =
8733 RPL_LUNDATA_ATYP_FLAT |
8734 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
8735 #ifdef OLDCTLHEADERS
8736 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
8737 (targ_lun_id & SRLD_BUS_LUN_MASK);
8739 lun_data->luns[num_filled].lundata[1] =
8740 #ifdef OLDCTLHEADERS
8741 targ_lun_id >> SRLD_BUS_LUN_BITS;
8743 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
8746 printf("ctl_report_luns: bogus LUN number %jd, "
8747 "skipping\n", (intmax_t)targ_lun_id);
8750 * According to SPC-3, rev 14 section 6.21:
8752 * "The execution of a REPORT LUNS command to any valid and
8753 * installed logical unit shall clear the REPORTED LUNS DATA
8754 * HAS CHANGED unit attention condition for all logical
8755 * units of that target with respect to the requesting
8756 * initiator. A valid and installed logical unit is one
8757 * having a PERIPHERAL QUALIFIER of 000b in the standard
8758 * INQUIRY data (see 6.4.2)."
8760 * If request_lun is NULL, the LUN this report luns command
8761 * was issued to is either disabled or doesn't exist. In that
8762 * case, we shouldn't clear any pending lun change unit
8765 if (request_lun != NULL)
8766 lun->pending_sense[initidx].ua_pending &=
8769 mtx_unlock(&control_softc->ctl_lock);
8772 * It's quite possible that we've returned fewer LUNs than we allocated
8773 * space for. Trim it.
8775 lun_datalen = sizeof(*lun_data) +
8776 (num_filled * sizeof(struct scsi_report_luns_lundata));
8778 if (lun_datalen < alloc_len) {
8779 ctsio->residual = alloc_len - lun_datalen;
8780 ctsio->kern_data_len = lun_datalen;
8781 ctsio->kern_total_len = lun_datalen;
8783 ctsio->residual = 0;
8784 ctsio->kern_data_len = alloc_len;
8785 ctsio->kern_total_len = alloc_len;
8787 ctsio->kern_data_resid = 0;
8788 ctsio->kern_rel_offset = 0;
8789 ctsio->kern_sg_entries = 0;
8792 * We set this to the actual data length, regardless of how much
8793 * space we actually have to return results. If the user looks at
8794 * this value, he'll know whether or not he allocated enough space
8795 * and reissue the command if necessary. We don't support well
8796 * known logical units, so if the user asks for that, return none.
8798 scsi_ulto4b(lun_datalen - 8, lun_data->length);
8801 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
8804 ctsio->scsi_status = SCSI_STATUS_OK;
8806 ctsio->be_move_done = ctl_config_move_done;
8807 ctl_datamove((union ctl_io *)ctsio);
8813 ctl_request_sense(struct ctl_scsiio *ctsio)
8815 struct scsi_request_sense *cdb;
8816 struct scsi_sense_data *sense_ptr;
8817 struct ctl_lun *lun;
8820 scsi_sense_data_type sense_format;
8822 cdb = (struct scsi_request_sense *)ctsio->cdb;
8824 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8826 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
8829 * Determine which sense format the user wants.
8831 if (cdb->byte2 & SRS_DESC)
8832 sense_format = SSD_TYPE_DESC;
8834 sense_format = SSD_TYPE_FIXED;
8836 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
8837 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
8838 ctsio->kern_sg_entries = 0;
8841 * struct scsi_sense_data, which is currently set to 256 bytes, is
8842 * larger than the largest allowed value for the length field in the
8843 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
8845 ctsio->residual = 0;
8846 ctsio->kern_data_len = cdb->length;
8847 ctsio->kern_total_len = cdb->length;
8849 ctsio->kern_data_resid = 0;
8850 ctsio->kern_rel_offset = 0;
8851 ctsio->kern_sg_entries = 0;
8854 * If we don't have a LUN, we don't have any pending sense.
8860 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8862 * Check for pending sense, and then for pending unit attentions.
8863 * Pending sense gets returned first, then pending unit attentions.
8865 mtx_lock(&lun->ctl_softc->ctl_lock);
8866 if (ctl_is_set(lun->have_ca, initidx)) {
8867 scsi_sense_data_type stored_format;
8870 * Check to see which sense format was used for the stored
8873 stored_format = scsi_sense_type(
8874 &lun->pending_sense[initidx].sense);
8877 * If the user requested a different sense format than the
8878 * one we stored, then we need to convert it to the other
8879 * format. If we're going from descriptor to fixed format
8880 * sense data, we may lose things in translation, depending
8881 * on what options were used.
8883 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
8884 * for some reason we'll just copy it out as-is.
8886 if ((stored_format == SSD_TYPE_FIXED)
8887 && (sense_format == SSD_TYPE_DESC))
8888 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
8889 &lun->pending_sense[initidx].sense,
8890 (struct scsi_sense_data_desc *)sense_ptr);
8891 else if ((stored_format == SSD_TYPE_DESC)
8892 && (sense_format == SSD_TYPE_FIXED))
8893 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
8894 &lun->pending_sense[initidx].sense,
8895 (struct scsi_sense_data_fixed *)sense_ptr);
8897 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
8898 ctl_min(sizeof(*sense_ptr),
8899 sizeof(lun->pending_sense[initidx].sense)));
8901 ctl_clear_mask(lun->have_ca, initidx);
8903 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
8904 ctl_ua_type ua_type;
8906 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
8907 sense_ptr, sense_format);
8908 if (ua_type != CTL_UA_NONE) {
8910 /* We're reporting this UA, so clear it */
8911 lun->pending_sense[initidx].ua_pending &= ~ua_type;
8914 mtx_unlock(&lun->ctl_softc->ctl_lock);
8917 * We already have a pending error, return it.
8919 if (have_error != 0) {
8921 * We report the SCSI status as OK, since the status of the
8922 * request sense command itself is OK.
8924 ctsio->scsi_status = SCSI_STATUS_OK;
8927 * We report 0 for the sense length, because we aren't doing
8928 * autosense in this case. We're reporting sense as
8931 ctsio->sense_len = 0;
8933 ctsio->be_move_done = ctl_config_move_done;
8934 ctl_datamove((union ctl_io *)ctsio);
8936 return (CTL_RETVAL_COMPLETE);
8942 * No sense information to report, so we report that everything is
8945 ctl_set_sense_data(sense_ptr,
8948 /*current_error*/ 1,
8949 /*sense_key*/ SSD_KEY_NO_SENSE,
8954 ctsio->scsi_status = SCSI_STATUS_OK;
8957 * We report 0 for the sense length, because we aren't doing
8958 * autosense in this case. We're reporting sense as parameter data.
8960 ctsio->sense_len = 0;
8961 ctsio->be_move_done = ctl_config_move_done;
8962 ctl_datamove((union ctl_io *)ctsio);
8964 return (CTL_RETVAL_COMPLETE);
8968 ctl_tur(struct ctl_scsiio *ctsio)
8970 struct ctl_lun *lun;
8972 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8974 CTL_DEBUG_PRINT(("ctl_tur\n"));
8979 ctsio->scsi_status = SCSI_STATUS_OK;
8980 ctsio->io_hdr.status = CTL_SUCCESS;
8982 ctl_done((union ctl_io *)ctsio);
8984 return (CTL_RETVAL_COMPLETE);
8989 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
8996 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
8998 struct scsi_vpd_supported_pages *pages;
9000 struct ctl_lun *lun;
9002 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9004 sup_page_size = sizeof(struct scsi_vpd_supported_pages) +
9005 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9006 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9007 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9008 ctsio->kern_sg_entries = 0;
9010 if (sup_page_size < alloc_len) {
9011 ctsio->residual = alloc_len - sup_page_size;
9012 ctsio->kern_data_len = sup_page_size;
9013 ctsio->kern_total_len = sup_page_size;
9015 ctsio->residual = 0;
9016 ctsio->kern_data_len = alloc_len;
9017 ctsio->kern_total_len = alloc_len;
9019 ctsio->kern_data_resid = 0;
9020 ctsio->kern_rel_offset = 0;
9021 ctsio->kern_sg_entries = 0;
9024 * The control device is always connected. The disk device, on the
9025 * other hand, may not be online all the time. Need to change this
9026 * to figure out whether the disk device is actually online or not.
9029 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9030 lun->be_lun->lun_type;
9032 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9034 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9035 /* Supported VPD pages */
9036 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9038 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9039 /* Device Identification */
9040 pages->page_list[2] = SVPD_DEVICE_ID;
9042 ctsio->scsi_status = SCSI_STATUS_OK;
9044 ctsio->be_move_done = ctl_config_move_done;
9045 ctl_datamove((union ctl_io *)ctsio);
9047 return (CTL_RETVAL_COMPLETE);
9051 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9053 struct scsi_vpd_unit_serial_number *sn_ptr;
9054 struct ctl_lun *lun;
9055 #ifndef CTL_USE_BACKEND_SN
9059 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9061 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9062 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9063 ctsio->kern_sg_entries = 0;
9065 if (sizeof(*sn_ptr) < alloc_len) {
9066 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9067 ctsio->kern_data_len = sizeof(*sn_ptr);
9068 ctsio->kern_total_len = sizeof(*sn_ptr);
9070 ctsio->residual = 0;
9071 ctsio->kern_data_len = alloc_len;
9072 ctsio->kern_total_len = alloc_len;
9074 ctsio->kern_data_resid = 0;
9075 ctsio->kern_rel_offset = 0;
9076 ctsio->kern_sg_entries = 0;
9079 * The control device is always connected. The disk device, on the
9080 * other hand, may not be online all the time. Need to change this
9081 * to figure out whether the disk device is actually online or not.
9084 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9085 lun->be_lun->lun_type;
9087 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9089 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9090 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9091 #ifdef CTL_USE_BACKEND_SN
9093 * If we don't have a LUN, we just leave the serial number as
9096 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9098 strncpy((char *)sn_ptr->serial_num,
9099 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9103 * Note that we're using a non-unique serial number here,
9105 snprintf(tmpstr, sizeof(tmpstr), "MYSERIALNUMIS000");
9106 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9107 strncpy(sn_ptr->serial_num, tmpstr, ctl_min(CTL_SN_LEN,
9108 ctl_min(sizeof(tmpstr), sizeof(*sn_ptr) - 4)));
9110 ctsio->scsi_status = SCSI_STATUS_OK;
9112 ctsio->be_move_done = ctl_config_move_done;
9113 ctl_datamove((union ctl_io *)ctsio);
9115 return (CTL_RETVAL_COMPLETE);
9120 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9122 struct scsi_vpd_device_id *devid_ptr;
9123 struct scsi_vpd_id_descriptor *desc, *desc1;
9124 struct scsi_vpd_id_descriptor *desc2, *desc3; /* for types 4h and 5h */
9125 struct scsi_vpd_id_t10 *t10id;
9126 struct ctl_softc *ctl_softc;
9127 struct ctl_lun *lun;
9128 struct ctl_frontend *fe;
9129 #ifndef CTL_USE_BACKEND_SN
9131 #endif /* CTL_USE_BACKEND_SN */
9134 ctl_softc = control_softc;
9136 mtx_lock(&ctl_softc->ctl_lock);
9137 fe = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9138 mtx_unlock(&ctl_softc->ctl_lock);
9140 if (fe->devid != NULL)
9141 return ((fe->devid)(ctsio, alloc_len));
9143 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9145 devid_len = sizeof(struct scsi_vpd_device_id) +
9146 sizeof(struct scsi_vpd_id_descriptor) +
9147 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN +
9148 sizeof(struct scsi_vpd_id_descriptor) + CTL_WWPN_LEN +
9149 sizeof(struct scsi_vpd_id_descriptor) +
9150 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9151 sizeof(struct scsi_vpd_id_descriptor) +
9152 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9154 ctsio->kern_data_ptr = malloc(devid_len, M_CTL, M_WAITOK | M_ZERO);
9155 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9156 ctsio->kern_sg_entries = 0;
9158 if (devid_len < alloc_len) {
9159 ctsio->residual = alloc_len - devid_len;
9160 ctsio->kern_data_len = devid_len;
9161 ctsio->kern_total_len = devid_len;
9163 ctsio->residual = 0;
9164 ctsio->kern_data_len = alloc_len;
9165 ctsio->kern_total_len = alloc_len;
9167 ctsio->kern_data_resid = 0;
9168 ctsio->kern_rel_offset = 0;
9169 ctsio->kern_sg_entries = 0;
9171 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9172 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
9173 desc1 = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9174 sizeof(struct scsi_vpd_id_t10) + CTL_DEVID_LEN);
9175 desc2 = (struct scsi_vpd_id_descriptor *)(&desc1->identifier[0] +
9177 desc3 = (struct scsi_vpd_id_descriptor *)(&desc2->identifier[0] +
9178 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9181 * The control device is always connected. The disk device, on the
9182 * other hand, may not be online all the time.
9185 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9186 lun->be_lun->lun_type;
9188 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9190 devid_ptr->page_code = SVPD_DEVICE_ID;
9192 scsi_ulto2b(devid_len - 4, devid_ptr->length);
9194 mtx_lock(&ctl_softc->ctl_lock);
9197 * For Fibre channel,
9199 if (fe->port_type == CTL_PORT_FC)
9201 desc->proto_codeset = (SCSI_PROTO_FC << 4) |
9202 SVPD_ID_CODESET_ASCII;
9203 desc1->proto_codeset = (SCSI_PROTO_FC << 4) |
9204 SVPD_ID_CODESET_BINARY;
9208 desc->proto_codeset = (SCSI_PROTO_SPI << 4) |
9209 SVPD_ID_CODESET_ASCII;
9210 desc1->proto_codeset = (SCSI_PROTO_SPI << 4) |
9211 SVPD_ID_CODESET_BINARY;
9213 desc2->proto_codeset = desc3->proto_codeset = desc1->proto_codeset;
9214 mtx_unlock(&ctl_softc->ctl_lock);
9217 * We're using a LUN association here. i.e., this device ID is a
9218 * per-LUN identifier.
9220 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
9221 desc->length = sizeof(*t10id) + CTL_DEVID_LEN;
9222 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
9225 * desc1 is for the WWPN which is a port asscociation.
9227 desc1->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT | SVPD_ID_TYPE_NAA;
9228 desc1->length = CTL_WWPN_LEN;
9229 /* XXX Call Reggie's get_WWNN func here then add port # to the end */
9230 /* For testing just create the WWPN */
9232 ddb_GetWWNN((char *)desc1->identifier);
9234 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9235 /* This is so Copancontrol will return something sane */
9236 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9237 ctsio->io_hdr.nexus.targ_port!=8)
9238 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port-1;
9240 desc1->identifier[7] += ctsio->io_hdr.nexus.targ_port;
9243 be64enc(desc1->identifier, fe->wwpn);
9246 * desc2 is for the Relative Target Port(type 4h) identifier
9248 desc2->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9249 | SVPD_ID_TYPE_RELTARG;
9252 /* NOTE: if the port is 0 or 8 we don't want to subtract 1 */
9253 /* This is so Copancontrol will return something sane */
9254 if (ctsio->io_hdr.nexus.targ_port!=0 &&
9255 ctsio->io_hdr.nexus.targ_port!=8)
9256 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port - 1;
9258 desc2->identifier[3] = ctsio->io_hdr.nexus.targ_port;
9262 * desc3 is for the Target Port Group(type 5h) identifier
9264 desc3->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT
9265 | SVPD_ID_TYPE_TPORTGRP;
9267 if (ctsio->io_hdr.nexus.targ_port < CTL_MAX_PORTS || ctl_is_single)
9268 desc3->identifier[3] = 1;
9270 desc3->identifier[3] = 2;
9272 #ifdef CTL_USE_BACKEND_SN
9274 * If we've actually got a backend, copy the device id from the
9275 * per-LUN data. Otherwise, set it to all spaces.
9279 * Copy the backend's LUN ID.
9281 strncpy((char *)t10id->vendor_spec_id,
9282 (char *)lun->be_lun->device_id, CTL_DEVID_LEN);
9285 * No backend, set this to spaces.
9287 memset(t10id->vendor_spec_id, 0x20, CTL_DEVID_LEN);
9290 snprintf(tmpstr, sizeof(tmpstr), "MYDEVICEIDIS%4d",
9291 (lun != NULL) ? (int)lun->lun : 0);
9292 strncpy(t10id->vendor_spec_id, tmpstr, ctl_min(CTL_DEVID_LEN,
9296 ctsio->scsi_status = SCSI_STATUS_OK;
9298 ctsio->be_move_done = ctl_config_move_done;
9299 ctl_datamove((union ctl_io *)ctsio);
9301 return (CTL_RETVAL_COMPLETE);
9305 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9307 struct scsi_inquiry *cdb;
9308 int alloc_len, retval;
9310 cdb = (struct scsi_inquiry *)ctsio->cdb;
9312 retval = CTL_RETVAL_COMPLETE;
9314 alloc_len = scsi_2btoul(cdb->length);
9316 switch (cdb->page_code) {
9317 case SVPD_SUPPORTED_PAGES:
9318 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9320 case SVPD_UNIT_SERIAL_NUMBER:
9321 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9323 case SVPD_DEVICE_ID:
9324 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9327 ctl_set_invalid_field(ctsio,
9333 ctl_done((union ctl_io *)ctsio);
9334 retval = CTL_RETVAL_COMPLETE;
9342 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9344 struct scsi_inquiry_data *inq_ptr;
9345 struct scsi_inquiry *cdb;
9346 struct ctl_softc *ctl_softc;
9347 struct ctl_lun *lun;
9351 ctl_softc = control_softc;
9354 * Figure out whether we're talking to a Fibre Channel port or not.
9355 * We treat the ioctl front end, and any SCSI adapters, as packetized
9358 mtx_lock(&ctl_softc->ctl_lock);
9359 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
9364 mtx_unlock(&ctl_softc->ctl_lock);
9366 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9367 cdb = (struct scsi_inquiry *)ctsio->cdb;
9368 alloc_len = scsi_2btoul(cdb->length);
9371 * We malloc the full inquiry data size here and fill it
9372 * in. If the user only asks for less, we'll give him
9375 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
9376 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9377 ctsio->kern_sg_entries = 0;
9378 ctsio->kern_data_resid = 0;
9379 ctsio->kern_rel_offset = 0;
9381 if (sizeof(*inq_ptr) < alloc_len) {
9382 ctsio->residual = alloc_len - sizeof(*inq_ptr);
9383 ctsio->kern_data_len = sizeof(*inq_ptr);
9384 ctsio->kern_total_len = sizeof(*inq_ptr);
9386 ctsio->residual = 0;
9387 ctsio->kern_data_len = alloc_len;
9388 ctsio->kern_total_len = alloc_len;
9392 * If we have a LUN configured, report it as connected. Otherwise,
9393 * report that it is offline or no device is supported, depending
9394 * on the value of inquiry_pq_no_lun.
9396 * According to the spec (SPC-4 r34), the peripheral qualifier
9397 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
9399 * "A peripheral device having the specified peripheral device type
9400 * is not connected to this logical unit. However, the device
9401 * server is capable of supporting the specified peripheral device
9402 * type on this logical unit."
9404 * According to the same spec, the peripheral qualifier
9405 * SID_QUAL_BAD_LU (011b) is used in this scenario:
9407 * "The device server is not capable of supporting a peripheral
9408 * device on this logical unit. For this peripheral qualifier the
9409 * peripheral device type shall be set to 1Fh. All other peripheral
9410 * device type values are reserved for this peripheral qualifier."
9412 * Given the text, it would seem that we probably want to report that
9413 * the LUN is offline here. There is no LUN connected, but we can
9414 * support a LUN at the given LUN number.
9416 * In the real world, though, it sounds like things are a little
9419 * - Linux, when presented with a LUN with the offline peripheral
9420 * qualifier, will create an sg driver instance for it. So when
9421 * you attach it to CTL, you wind up with a ton of sg driver
9422 * instances. (One for every LUN that Linux bothered to probe.)
9423 * Linux does this despite the fact that it issues a REPORT LUNs
9424 * to LUN 0 to get the inventory of supported LUNs.
9426 * - There is other anecdotal evidence (from Emulex folks) about
9427 * arrays that use the offline peripheral qualifier for LUNs that
9428 * are on the "passive" path in an active/passive array.
9430 * So the solution is provide a hopefully reasonable default
9431 * (return bad/no LUN) and allow the user to change the behavior
9432 * with a tunable/sysctl variable.
9435 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9436 lun->be_lun->lun_type;
9437 else if (ctl_softc->inquiry_pq_no_lun == 0)
9438 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9440 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9442 /* RMB in byte 2 is 0 */
9443 inq_ptr->version = SCSI_REV_SPC3;
9446 * According to SAM-3, even if a device only supports a single
9447 * level of LUN addressing, it should still set the HISUP bit:
9449 * 4.9.1 Logical unit numbers overview
9451 * All logical unit number formats described in this standard are
9452 * hierarchical in structure even when only a single level in that
9453 * hierarchy is used. The HISUP bit shall be set to one in the
9454 * standard INQUIRY data (see SPC-2) when any logical unit number
9455 * format described in this standard is used. Non-hierarchical
9456 * formats are outside the scope of this standard.
9458 * Therefore we set the HiSup bit here.
9460 * The reponse format is 2, per SPC-3.
9462 inq_ptr->response_format = SID_HiSup | 2;
9464 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
9465 CTL_DEBUG_PRINT(("additional_length = %d\n",
9466 inq_ptr->additional_length));
9468 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
9469 /* 16 bit addressing */
9471 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
9472 /* XXX set the SID_MultiP bit here if we're actually going to
9473 respond on multiple ports */
9474 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
9476 /* 16 bit data bus, synchronous transfers */
9477 /* XXX these flags don't apply for FC */
9479 inq_ptr->flags = SID_WBus16 | SID_Sync;
9481 * XXX KDM do we want to support tagged queueing on the control
9485 || (lun->be_lun->lun_type != T_PROCESSOR))
9486 inq_ptr->flags |= SID_CmdQue;
9488 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
9489 * We have 8 bytes for the vendor name, and 16 bytes for the device
9490 * name and 4 bytes for the revision.
9492 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
9494 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9496 switch (lun->be_lun->lun_type) {
9498 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
9501 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
9504 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
9510 * XXX make this a macro somewhere so it automatically gets
9511 * incremented when we make changes.
9513 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
9516 * For parallel SCSI, we support double transition and single
9517 * transition clocking. We also support QAS (Quick Arbitration
9518 * and Selection) and Information Unit transfers on both the
9519 * control and array devices.
9522 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
9526 scsi_ulto2b(0x0060, inq_ptr->version1);
9527 /* SPC-3 (no version claimed) XXX should we claim a version? */
9528 scsi_ulto2b(0x0300, inq_ptr->version2);
9530 /* FCP-2 ANSI INCITS.350:2003 */
9531 scsi_ulto2b(0x0917, inq_ptr->version3);
9533 /* SPI-4 ANSI INCITS.362:200x */
9534 scsi_ulto2b(0x0B56, inq_ptr->version3);
9538 /* SBC-2 (no version claimed) XXX should we claim a version? */
9539 scsi_ulto2b(0x0320, inq_ptr->version4);
9541 switch (lun->be_lun->lun_type) {
9544 * SBC-2 (no version claimed) XXX should we claim a
9547 scsi_ulto2b(0x0320, inq_ptr->version4);
9555 ctsio->scsi_status = SCSI_STATUS_OK;
9556 if (ctsio->kern_data_len > 0) {
9557 ctsio->be_move_done = ctl_config_move_done;
9558 ctl_datamove((union ctl_io *)ctsio);
9560 ctsio->io_hdr.status = CTL_SUCCESS;
9561 ctl_done((union ctl_io *)ctsio);
9564 return (CTL_RETVAL_COMPLETE);
9568 ctl_inquiry(struct ctl_scsiio *ctsio)
9570 struct scsi_inquiry *cdb;
9573 cdb = (struct scsi_inquiry *)ctsio->cdb;
9577 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
9580 * Right now, we don't support the CmdDt inquiry information.
9581 * This would be nice to support in the future. When we do
9582 * support it, we should change this test so that it checks to make
9583 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
9586 if (((cdb->byte2 & SI_EVPD)
9587 && (cdb->byte2 & SI_CMDDT)))
9589 if (cdb->byte2 & SI_CMDDT) {
9591 * Point to the SI_CMDDT bit. We might change this
9592 * when we support SI_CMDDT, but since both bits would be
9593 * "wrong", this should probably just stay as-is then.
9595 ctl_set_invalid_field(ctsio,
9601 ctl_done((union ctl_io *)ctsio);
9602 return (CTL_RETVAL_COMPLETE);
9604 if (cdb->byte2 & SI_EVPD)
9605 retval = ctl_inquiry_evpd(ctsio);
9607 else if (cdb->byte2 & SI_CMDDT)
9608 retval = ctl_inquiry_cmddt(ctsio);
9611 retval = ctl_inquiry_std(ctsio);
9617 * For known CDB types, parse the LBA and length.
9620 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
9622 if (io->io_hdr.io_type != CTL_IO_SCSI)
9625 switch (io->scsiio.cdb[0]) {
9628 struct scsi_rw_6 *cdb;
9630 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
9632 *lba = scsi_3btoul(cdb->addr);
9633 /* only 5 bits are valid in the most significant address byte */
9640 struct scsi_rw_10 *cdb;
9642 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
9644 *lba = scsi_4btoul(cdb->addr);
9645 *len = scsi_2btoul(cdb->length);
9648 case WRITE_VERIFY_10: {
9649 struct scsi_write_verify_10 *cdb;
9651 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
9653 *lba = scsi_4btoul(cdb->addr);
9654 *len = scsi_2btoul(cdb->length);
9659 struct scsi_rw_12 *cdb;
9661 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
9663 *lba = scsi_4btoul(cdb->addr);
9664 *len = scsi_4btoul(cdb->length);
9667 case WRITE_VERIFY_12: {
9668 struct scsi_write_verify_12 *cdb;
9670 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
9672 *lba = scsi_4btoul(cdb->addr);
9673 *len = scsi_4btoul(cdb->length);
9678 struct scsi_rw_16 *cdb;
9680 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
9682 *lba = scsi_8btou64(cdb->addr);
9683 *len = scsi_4btoul(cdb->length);
9686 case WRITE_VERIFY_16: {
9687 struct scsi_write_verify_16 *cdb;
9689 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
9692 *lba = scsi_8btou64(cdb->addr);
9693 *len = scsi_4btoul(cdb->length);
9698 break; /* NOTREACHED */
9705 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
9707 uint64_t endlba1, endlba2;
9709 endlba1 = lba1 + len1 - 1;
9710 endlba2 = lba2 + len2 - 1;
9712 if ((endlba1 < lba2)
9713 || (endlba2 < lba1))
9714 return (CTL_ACTION_PASS);
9716 return (CTL_ACTION_BLOCK);
9720 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
9722 uint64_t lba1, lba2;
9723 uint32_t len1, len2;
9726 retval = ctl_get_lba_len(io1, &lba1, &len1);
9728 return (CTL_ACTION_ERROR);
9730 retval = ctl_get_lba_len(io2, &lba2, &len2);
9732 return (CTL_ACTION_ERROR);
9734 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
9738 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
9740 struct ctl_cmd_entry *pending_entry, *ooa_entry;
9741 ctl_serialize_action *serialize_row;
9744 * The initiator attempted multiple untagged commands at the same
9745 * time. Can't do that.
9747 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9748 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9749 && ((pending_io->io_hdr.nexus.targ_port ==
9750 ooa_io->io_hdr.nexus.targ_port)
9751 && (pending_io->io_hdr.nexus.initid.id ==
9752 ooa_io->io_hdr.nexus.initid.id))
9753 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
9754 return (CTL_ACTION_OVERLAP);
9757 * The initiator attempted to send multiple tagged commands with
9758 * the same ID. (It's fine if different initiators have the same
9761 * Even if all of those conditions are true, we don't kill the I/O
9762 * if the command ahead of us has been aborted. We won't end up
9763 * sending it to the FETD, and it's perfectly legal to resend a
9764 * command with the same tag number as long as the previous
9765 * instance of this tag number has been aborted somehow.
9767 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
9768 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
9769 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
9770 && ((pending_io->io_hdr.nexus.targ_port ==
9771 ooa_io->io_hdr.nexus.targ_port)
9772 && (pending_io->io_hdr.nexus.initid.id ==
9773 ooa_io->io_hdr.nexus.initid.id))
9774 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
9775 return (CTL_ACTION_OVERLAP_TAG);
9778 * If we get a head of queue tag, SAM-3 says that we should
9779 * immediately execute it.
9781 * What happens if this command would normally block for some other
9782 * reason? e.g. a request sense with a head of queue tag
9783 * immediately after a write. Normally that would block, but this
9784 * will result in its getting executed immediately...
9786 * We currently return "pass" instead of "skip", so we'll end up
9787 * going through the rest of the queue to check for overlapped tags.
9789 * XXX KDM check for other types of blockage first??
9791 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
9792 return (CTL_ACTION_PASS);
9795 * Ordered tags have to block until all items ahead of them
9796 * have completed. If we get called with an ordered tag, we always
9797 * block, if something else is ahead of us in the queue.
9799 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
9800 return (CTL_ACTION_BLOCK);
9803 * Simple tags get blocked until all head of queue and ordered tags
9804 * ahead of them have completed. I'm lumping untagged commands in
9805 * with simple tags here. XXX KDM is that the right thing to do?
9807 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
9808 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
9809 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
9810 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
9811 return (CTL_ACTION_BLOCK);
9813 pending_entry = &ctl_cmd_table[pending_io->scsiio.cdb[0]];
9814 ooa_entry = &ctl_cmd_table[ooa_io->scsiio.cdb[0]];
9816 serialize_row = ctl_serialize_table[ooa_entry->seridx];
9818 switch (serialize_row[pending_entry->seridx]) {
9820 return (CTL_ACTION_BLOCK);
9821 break; /* NOTREACHED */
9822 case CTL_SER_EXTENT:
9823 return (ctl_extent_check(pending_io, ooa_io));
9824 break; /* NOTREACHED */
9826 return (CTL_ACTION_PASS);
9827 break; /* NOTREACHED */
9829 return (CTL_ACTION_SKIP);
9832 panic("invalid serialization value %d",
9833 serialize_row[pending_entry->seridx]);
9834 break; /* NOTREACHED */
9837 return (CTL_ACTION_ERROR);
9841 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
9843 * - pending_io is generally either incoming, or on the blocked queue
9844 * - starting I/O is the I/O we want to start the check with.
9847 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
9848 union ctl_io *starting_io)
9850 union ctl_io *ooa_io;
9853 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
9856 * Run back along the OOA queue, starting with the current
9857 * blocked I/O and going through every I/O before it on the
9858 * queue. If starting_io is NULL, we'll just end up returning
9861 for (ooa_io = starting_io; ooa_io != NULL;
9862 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
9866 * This routine just checks to see whether
9867 * cur_blocked is blocked by ooa_io, which is ahead
9868 * of it in the queue. It doesn't queue/dequeue
9871 action = ctl_check_for_blockage(pending_io, ooa_io);
9873 case CTL_ACTION_BLOCK:
9874 case CTL_ACTION_OVERLAP:
9875 case CTL_ACTION_OVERLAP_TAG:
9876 case CTL_ACTION_SKIP:
9877 case CTL_ACTION_ERROR:
9879 break; /* NOTREACHED */
9880 case CTL_ACTION_PASS:
9883 panic("invalid action %d", action);
9884 break; /* NOTREACHED */
9888 return (CTL_ACTION_PASS);
9893 * - An I/O has just completed, and has been removed from the per-LUN OOA
9894 * queue, so some items on the blocked queue may now be unblocked.
9897 ctl_check_blocked(struct ctl_lun *lun)
9899 union ctl_io *cur_blocked, *next_blocked;
9901 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
9904 * Run forward from the head of the blocked queue, checking each
9905 * entry against the I/Os prior to it on the OOA queue to see if
9906 * there is still any blockage.
9908 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
9909 * with our removing a variable on it while it is traversing the
9912 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
9913 cur_blocked != NULL; cur_blocked = next_blocked) {
9914 union ctl_io *prev_ooa;
9917 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
9920 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
9921 ctl_ooaq, ooa_links);
9924 * If cur_blocked happens to be the first item in the OOA
9925 * queue now, prev_ooa will be NULL, and the action
9926 * returned will just be CTL_ACTION_PASS.
9928 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
9931 case CTL_ACTION_BLOCK:
9932 /* Nothing to do here, still blocked */
9934 case CTL_ACTION_OVERLAP:
9935 case CTL_ACTION_OVERLAP_TAG:
9937 * This shouldn't happen! In theory we've already
9938 * checked this command for overlap...
9941 case CTL_ACTION_PASS:
9942 case CTL_ACTION_SKIP: {
9943 struct ctl_softc *softc;
9944 struct ctl_cmd_entry *entry;
9950 * The skip case shouldn't happen, this transaction
9951 * should have never made it onto the blocked queue.
9954 * This I/O is no longer blocked, we can remove it
9955 * from the blocked queue. Since this is a TAILQ
9956 * (doubly linked list), we can do O(1) removals
9957 * from any place on the list.
9959 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
9961 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
9963 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
9965 * Need to send IO back to original side to
9968 union ctl_ha_msg msg_info;
9970 msg_info.hdr.original_sc =
9971 cur_blocked->io_hdr.original_sc;
9972 msg_info.hdr.serializing_sc = cur_blocked;
9973 msg_info.hdr.msg_type = CTL_MSG_R2R;
9974 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
9975 &msg_info, sizeof(msg_info), 0)) >
9976 CTL_HA_STATUS_SUCCESS) {
9977 printf("CTL:Check Blocked error from "
9978 "ctl_ha_msg_send %d\n",
9983 opcode = cur_blocked->scsiio.cdb[0];
9984 entry = &ctl_cmd_table[opcode];
9985 softc = control_softc;
9987 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
9990 * Check this I/O for LUN state changes that may
9991 * have happened while this command was blocked.
9992 * The LUN state may have been changed by a command
9993 * ahead of us in the queue, so we need to re-check
9994 * for any states that can be caused by SCSI
9997 if (ctl_scsiio_lun_check(softc, lun, entry,
9998 &cur_blocked->scsiio) == 0) {
9999 cur_blocked->io_hdr.flags |=
10000 CTL_FLAG_IS_WAS_ON_RTR;
10001 STAILQ_INSERT_TAIL(&lun->ctl_softc->rtr_queue,
10002 &cur_blocked->io_hdr, links);
10004 * In the non CTL_DONE_THREAD case, we need
10005 * to wake up the work thread here. When
10006 * we're processing completed requests from
10007 * the work thread context, we'll pop back
10008 * around and end up pulling things off the
10009 * RtR queue. When we aren't processing
10010 * things from the work thread context,
10011 * though, we won't ever check the RtR queue.
10012 * So we need to wake up the thread to clear
10013 * things off the queue. Otherwise this
10014 * transaction will just sit on the RtR queue
10015 * until a new I/O comes in. (Which may or
10016 * may not happen...)
10018 #ifndef CTL_DONE_THREAD
10019 ctl_wakeup_thread();
10022 ctl_done_lock(cur_blocked, /*have_lock*/ 1);
10027 * This probably shouldn't happen -- we shouldn't
10028 * get CTL_ACTION_ERROR, or anything else.
10034 return (CTL_RETVAL_COMPLETE);
10038 * This routine (with one exception) checks LUN flags that can be set by
10039 * commands ahead of us in the OOA queue. These flags have to be checked
10040 * when a command initially comes in, and when we pull a command off the
10041 * blocked queue and are preparing to execute it. The reason we have to
10042 * check these flags for commands on the blocked queue is that the LUN
10043 * state may have been changed by a command ahead of us while we're on the
10046 * Ordering is somewhat important with these checks, so please pay
10047 * careful attention to the placement of any new checks.
10050 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
10051 struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
10058 * If this shelf is a secondary shelf controller, we have to reject
10059 * any media access commands.
10062 /* No longer needed for HA */
10063 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
10064 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
10065 ctl_set_lun_standby(ctsio);
10072 * Check for a reservation conflict. If this command isn't allowed
10073 * even on reserved LUNs, and if this initiator isn't the one who
10074 * reserved us, reject the command with a reservation conflict.
10076 if ((lun->flags & CTL_LUN_RESERVED)
10077 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
10078 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
10079 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
10080 || (ctsio->io_hdr.nexus.targ_target.id !=
10081 lun->rsv_nexus.targ_target.id)) {
10082 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10083 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10089 if ( (lun->flags & CTL_LUN_PR_RESERVED)
10090 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
10093 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
10095 * if we aren't registered or it's a res holder type
10096 * reservation and this isn't the res holder then set a
10098 * NOTE: Commands which might be allowed on write exclusive
10099 * type reservations are checked in the particular command
10100 * for a conflict. Read and SSU are the only ones.
10102 if (!lun->per_res[residx].registered
10103 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
10104 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
10105 ctsio->io_hdr.status = CTL_SCSI_ERROR;
10112 if ((lun->flags & CTL_LUN_OFFLINE)
10113 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
10114 ctl_set_lun_not_ready(ctsio);
10120 * If the LUN is stopped, see if this particular command is allowed
10121 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
10123 if ((lun->flags & CTL_LUN_STOPPED)
10124 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
10125 /* "Logical unit not ready, initializing cmd. required" */
10126 ctl_set_lun_stopped(ctsio);
10131 if ((lun->flags & CTL_LUN_INOPERABLE)
10132 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
10133 /* "Medium format corrupted" */
10134 ctl_set_medium_format_corrupted(ctsio);
10145 ctl_failover_io(union ctl_io *io, int have_lock)
10147 ctl_set_busy(&io->scsiio);
10148 ctl_done_lock(io, have_lock);
10154 struct ctl_lun *lun;
10155 struct ctl_softc *ctl_softc;
10156 union ctl_io *next_io, *pending_io;
10161 ctl_softc = control_softc;
10163 mtx_lock(&ctl_softc->ctl_lock);
10165 * Remove any cmds from the other SC from the rtr queue. These
10166 * will obviously only be for LUNs for which we're the primary.
10167 * We can't send status or get/send data for these commands.
10168 * Since they haven't been executed yet, we can just remove them.
10169 * We'll either abort them or delete them below, depending on
10170 * which HA mode we're in.
10172 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
10173 io != NULL; io = next_io) {
10174 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10175 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10176 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
10177 ctl_io_hdr, links);
10180 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
10181 lun = ctl_softc->ctl_luns[lun_idx];
10186 * Processor LUNs are primary on both sides.
10187 * XXX will this always be true?
10189 if (lun->be_lun->lun_type == T_PROCESSOR)
10192 if ((lun->flags & CTL_LUN_PRIMARY_SC)
10193 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10194 printf("FAILOVER: primary lun %d\n", lun_idx);
10196 * Remove all commands from the other SC. First from the
10197 * blocked queue then from the ooa queue. Once we have
10198 * removed them. Call ctl_check_blocked to see if there
10199 * is anything that can run.
10201 for (io = (union ctl_io *)TAILQ_FIRST(
10202 &lun->blocked_queue); io != NULL; io = next_io) {
10204 next_io = (union ctl_io *)TAILQ_NEXT(
10205 &io->io_hdr, blocked_links);
10207 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10208 TAILQ_REMOVE(&lun->blocked_queue,
10209 &io->io_hdr,blocked_links);
10210 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10211 TAILQ_REMOVE(&lun->ooa_queue,
10212 &io->io_hdr, ooa_links);
10214 ctl_free_io_internal(io, 1);
10218 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10219 io != NULL; io = next_io) {
10221 next_io = (union ctl_io *)TAILQ_NEXT(
10222 &io->io_hdr, ooa_links);
10224 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
10226 TAILQ_REMOVE(&lun->ooa_queue,
10230 ctl_free_io_internal(io, 1);
10233 ctl_check_blocked(lun);
10234 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
10235 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10237 printf("FAILOVER: primary lun %d\n", lun_idx);
10239 * Abort all commands from the other SC. We can't
10240 * send status back for them now. These should get
10241 * cleaned up when they are completed or come out
10242 * for a datamove operation.
10244 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
10245 io != NULL; io = next_io) {
10246 next_io = (union ctl_io *)TAILQ_NEXT(
10247 &io->io_hdr, ooa_links);
10249 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
10250 io->io_hdr.flags |= CTL_FLAG_ABORT;
10252 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10253 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
10255 printf("FAILOVER: secondary lun %d\n", lun_idx);
10257 lun->flags |= CTL_LUN_PRIMARY_SC;
10260 * We send all I/O that was sent to this controller
10261 * and redirected to the other side back with
10262 * busy status, and have the initiator retry it.
10263 * Figuring out how much data has been transferred,
10264 * etc. and picking up where we left off would be
10267 * XXX KDM need to remove I/O from the blocked
10270 for (pending_io = (union ctl_io *)TAILQ_FIRST(
10271 &lun->ooa_queue); pending_io != NULL;
10272 pending_io = next_io) {
10274 next_io = (union ctl_io *)TAILQ_NEXT(
10275 &pending_io->io_hdr, ooa_links);
10277 pending_io->io_hdr.flags &=
10278 ~CTL_FLAG_SENT_2OTHER_SC;
10280 if (pending_io->io_hdr.flags &
10281 CTL_FLAG_IO_ACTIVE) {
10282 pending_io->io_hdr.flags |=
10285 ctl_set_busy(&pending_io->scsiio);
10286 ctl_done_lock(pending_io,
10292 * Build Unit Attention
10294 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10295 lun->pending_sense[i].ua_pending |=
10296 CTL_UA_ASYM_ACC_CHANGE;
10298 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
10299 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
10300 printf("FAILOVER: secondary lun %d\n", lun_idx);
10302 * if the first io on the OOA is not on the RtR queue
10305 lun->flags |= CTL_LUN_PRIMARY_SC;
10307 pending_io = (union ctl_io *)TAILQ_FIRST(
10309 if (pending_io==NULL) {
10310 printf("Nothing on OOA queue\n");
10314 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
10315 if ((pending_io->io_hdr.flags &
10316 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
10317 pending_io->io_hdr.flags |=
10318 CTL_FLAG_IS_WAS_ON_RTR;
10319 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
10320 &pending_io->io_hdr, links);
10325 printf("Tag 0x%04x is running\n",
10326 pending_io->scsiio.tag_num);
10330 next_io = (union ctl_io *)TAILQ_NEXT(
10331 &pending_io->io_hdr, ooa_links);
10332 for (pending_io=next_io; pending_io != NULL;
10333 pending_io = next_io) {
10334 pending_io->io_hdr.flags &=
10335 ~CTL_FLAG_SENT_2OTHER_SC;
10336 next_io = (union ctl_io *)TAILQ_NEXT(
10337 &pending_io->io_hdr, ooa_links);
10338 if (pending_io->io_hdr.flags &
10339 CTL_FLAG_IS_WAS_ON_RTR) {
10341 printf("Tag 0x%04x is running\n",
10342 pending_io->scsiio.tag_num);
10347 switch (ctl_check_ooa(lun, pending_io,
10348 (union ctl_io *)TAILQ_PREV(
10349 &pending_io->io_hdr, ctl_ooaq,
10352 case CTL_ACTION_BLOCK:
10353 TAILQ_INSERT_TAIL(&lun->blocked_queue,
10354 &pending_io->io_hdr,
10356 pending_io->io_hdr.flags |=
10359 case CTL_ACTION_PASS:
10360 case CTL_ACTION_SKIP:
10361 pending_io->io_hdr.flags |=
10362 CTL_FLAG_IS_WAS_ON_RTR;
10363 STAILQ_INSERT_TAIL(
10364 &ctl_softc->rtr_queue,
10365 &pending_io->io_hdr, links);
10367 case CTL_ACTION_OVERLAP:
10368 ctl_set_overlapped_cmd(
10369 (struct ctl_scsiio *)pending_io);
10370 ctl_done_lock(pending_io,
10373 case CTL_ACTION_OVERLAP_TAG:
10374 ctl_set_overlapped_tag(
10375 (struct ctl_scsiio *)pending_io,
10376 pending_io->scsiio.tag_num & 0xff);
10377 ctl_done_lock(pending_io,
10380 case CTL_ACTION_ERROR:
10382 ctl_set_internal_failure(
10383 (struct ctl_scsiio *)pending_io,
10386 ctl_done_lock(pending_io,
10393 * Build Unit Attention
10395 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10396 lun->pending_sense[i].ua_pending |=
10397 CTL_UA_ASYM_ACC_CHANGE;
10400 panic("Unhandled HA mode failover, LUN flags = %#x, "
10401 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
10405 mtx_unlock(&ctl_softc->ctl_lock);
10409 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
10411 struct ctl_lun *lun;
10412 struct ctl_cmd_entry *entry;
10414 uint32_t initidx, targ_lun;
10421 opcode = ctsio->cdb[0];
10423 mtx_lock(&ctl_softc->ctl_lock);
10425 targ_lun = ctsio->io_hdr.nexus.targ_lun;
10426 if (ctsio->io_hdr.nexus.lun_map_fn != NULL)
10427 targ_lun = ctsio->io_hdr.nexus.lun_map_fn(ctsio->io_hdr.nexus.lun_map_arg, targ_lun);
10428 if ((targ_lun < CTL_MAX_LUNS)
10429 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
10430 lun = ctl_softc->ctl_luns[targ_lun];
10432 * If the LUN is invalid, pretend that it doesn't exist.
10433 * It will go away as soon as all pending I/O has been
10436 if (lun->flags & CTL_LUN_DISABLED) {
10439 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
10440 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
10442 if (lun->be_lun->lun_type == T_PROCESSOR) {
10443 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
10447 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
10448 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
10451 entry = &ctl_cmd_table[opcode];
10453 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
10454 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
10457 * Check to see whether we can send this command to LUNs that don't
10458 * exist. This should pretty much only be the case for inquiry
10459 * and request sense. Further checks, below, really require having
10460 * a LUN, so we can't really check the command anymore. Just put
10461 * it on the rtr queue.
10464 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10467 ctl_set_unsupported_lun(ctsio);
10468 mtx_unlock(&ctl_softc->ctl_lock);
10469 ctl_done((union ctl_io *)ctsio);
10470 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
10474 * Every I/O goes into the OOA queue for a particular LUN, and
10475 * stays there until completion.
10477 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
10480 * Make sure we support this particular command on this LUN.
10481 * e.g., we don't support writes to the control LUN.
10483 switch (lun->be_lun->lun_type) {
10485 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
10486 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10488 ctl_set_invalid_opcode(ctsio);
10489 mtx_unlock(&ctl_softc->ctl_lock);
10490 ctl_done((union ctl_io *)ctsio);
10495 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0)
10496 && ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS)
10498 ctl_set_invalid_opcode(ctsio);
10499 mtx_unlock(&ctl_softc->ctl_lock);
10500 ctl_done((union ctl_io *)ctsio);
10505 printf("Unsupported CTL LUN type %d\n",
10506 lun->be_lun->lun_type);
10507 panic("Unsupported CTL LUN type %d\n",
10508 lun->be_lun->lun_type);
10509 break; /* NOTREACHED */
10513 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
10516 * If we've got a request sense, it'll clear the contingent
10517 * allegiance condition. Otherwise, if we have a CA condition for
10518 * this initiator, clear it, because it sent down a command other
10519 * than request sense.
10521 if ((opcode != REQUEST_SENSE)
10522 && (ctl_is_set(lun->have_ca, initidx)))
10523 ctl_clear_mask(lun->have_ca, initidx);
10526 * If the command has this flag set, it handles its own unit
10527 * attention reporting, we shouldn't do anything. Otherwise we
10528 * check for any pending unit attentions, and send them back to the
10529 * initiator. We only do this when a command initially comes in,
10530 * not when we pull it off the blocked queue.
10532 * According to SAM-3, section 5.3.2, the order that things get
10533 * presented back to the host is basically unit attentions caused
10534 * by some sort of reset event, busy status, reservation conflicts
10535 * or task set full, and finally any other status.
10537 * One issue here is that some of the unit attentions we report
10538 * don't fall into the "reset" category (e.g. "reported luns data
10539 * has changed"). So reporting it here, before the reservation
10540 * check, may be technically wrong. I guess the only thing to do
10541 * would be to check for and report the reset events here, and then
10542 * check for the other unit attention types after we check for a
10543 * reservation conflict.
10545 * XXX KDM need to fix this
10547 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
10548 ctl_ua_type ua_type;
10550 ua_type = lun->pending_sense[initidx].ua_pending;
10551 if (ua_type != CTL_UA_NONE) {
10552 scsi_sense_data_type sense_format;
10555 sense_format = (lun->flags &
10556 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
10559 sense_format = SSD_TYPE_FIXED;
10561 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
10563 if (ua_type != CTL_UA_NONE) {
10564 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
10565 ctsio->io_hdr.status = CTL_SCSI_ERROR |
10567 ctsio->sense_len = SSD_FULL_SIZE;
10568 lun->pending_sense[initidx].ua_pending &=
10570 mtx_unlock(&ctl_softc->ctl_lock);
10571 ctl_done((union ctl_io *)ctsio);
10578 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
10579 mtx_unlock(&ctl_softc->ctl_lock);
10580 ctl_done((union ctl_io *)ctsio);
10585 * XXX CHD this is where we want to send IO to other side if
10586 * this LUN is secondary on this SC. We will need to make a copy
10587 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
10588 * the copy we send as FROM_OTHER.
10589 * We also need to stuff the address of the original IO so we can
10590 * find it easily. Something similar will need be done on the other
10591 * side so when we are done we can find the copy.
10593 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
10594 union ctl_ha_msg msg_info;
10597 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
10599 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
10600 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
10602 printf("1. ctsio %p\n", ctsio);
10604 msg_info.hdr.serializing_sc = NULL;
10605 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
10606 msg_info.scsi.tag_num = ctsio->tag_num;
10607 msg_info.scsi.tag_type = ctsio->tag_type;
10608 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
10610 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10612 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10613 (void *)&msg_info, sizeof(msg_info), 0)) >
10614 CTL_HA_STATUS_SUCCESS) {
10615 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
10617 printf("CTL:opcode is %x\n",opcode);
10620 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
10625 * XXX KDM this I/O is off the incoming queue, but hasn't
10626 * been inserted on any other queue. We may need to come
10627 * up with a holding queue while we wait for serialization
10628 * so that we have an idea of what we're waiting for from
10631 goto bailout_unlock;
10634 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
10635 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
10636 ctl_ooaq, ooa_links))) {
10637 case CTL_ACTION_BLOCK:
10638 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
10639 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
10641 goto bailout_unlock;
10642 break; /* NOTREACHED */
10643 case CTL_ACTION_PASS:
10644 case CTL_ACTION_SKIP:
10646 break; /* NOTREACHED */
10647 case CTL_ACTION_OVERLAP:
10648 ctl_set_overlapped_cmd(ctsio);
10649 mtx_unlock(&ctl_softc->ctl_lock);
10650 ctl_done((union ctl_io *)ctsio);
10652 break; /* NOTREACHED */
10653 case CTL_ACTION_OVERLAP_TAG:
10654 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
10655 mtx_unlock(&ctl_softc->ctl_lock);
10656 ctl_done((union ctl_io *)ctsio);
10658 break; /* NOTREACHED */
10659 case CTL_ACTION_ERROR:
10661 ctl_set_internal_failure(ctsio,
10663 /*retry_count*/ 0);
10664 mtx_unlock(&ctl_softc->ctl_lock);
10665 ctl_done((union ctl_io *)ctsio);
10667 break; /* NOTREACHED */
10670 goto bailout_unlock;
10673 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
10674 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue, &ctsio->io_hdr, links);
10677 mtx_unlock(&ctl_softc->ctl_lock);
10684 ctl_scsiio(struct ctl_scsiio *ctsio)
10687 struct ctl_cmd_entry *entry;
10689 retval = CTL_RETVAL_COMPLETE;
10691 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
10693 entry = &ctl_cmd_table[ctsio->cdb[0]];
10696 * If this I/O has been aborted, just send it straight to
10697 * ctl_done() without executing it.
10699 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
10700 ctl_done((union ctl_io *)ctsio);
10705 * All the checks should have been handled by ctl_scsiio_precheck().
10706 * We should be clear now to just execute the I/O.
10708 retval = entry->execute(ctsio);
10715 * Since we only implement one target right now, a bus reset simply resets
10716 * our single target.
10719 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
10721 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
10725 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
10726 ctl_ua_type ua_type)
10728 struct ctl_lun *lun;
10731 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
10732 union ctl_ha_msg msg_info;
10734 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
10735 msg_info.hdr.nexus = io->io_hdr.nexus;
10736 if (ua_type==CTL_UA_TARG_RESET)
10737 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
10739 msg_info.task.task_action = CTL_TASK_BUS_RESET;
10740 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
10741 msg_info.hdr.original_sc = NULL;
10742 msg_info.hdr.serializing_sc = NULL;
10743 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10744 (void *)&msg_info, sizeof(msg_info), 0)) {
10749 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
10750 retval += ctl_lun_reset(lun, io, ua_type);
10756 * The LUN should always be set. The I/O is optional, and is used to
10757 * distinguish between I/Os sent by this initiator, and by other
10758 * initiators. We set unit attention for initiators other than this one.
10759 * SAM-3 is vague on this point. It does say that a unit attention should
10760 * be established for other initiators when a LUN is reset (see section
10761 * 5.7.3), but it doesn't specifically say that the unit attention should
10762 * be established for this particular initiator when a LUN is reset. Here
10763 * is the relevant text, from SAM-3 rev 8:
10765 * 5.7.2 When a SCSI initiator port aborts its own tasks
10767 * When a SCSI initiator port causes its own task(s) to be aborted, no
10768 * notification that the task(s) have been aborted shall be returned to
10769 * the SCSI initiator port other than the completion response for the
10770 * command or task management function action that caused the task(s) to
10771 * be aborted and notification(s) associated with related effects of the
10772 * action (e.g., a reset unit attention condition).
10774 * XXX KDM for now, we're setting unit attention for all initiators.
10777 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
10781 uint32_t initindex;
10786 * Run through the OOA queue and abort each I/O.
10789 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
10791 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
10792 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
10793 xio->io_hdr.flags |= CTL_FLAG_ABORT;
10797 * This version sets unit attention for every
10800 initindex = ctl_get_initindex(&io->io_hdr.nexus);
10801 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10802 if (initindex == i)
10804 lun->pending_sense[i].ua_pending |= ua_type;
10809 * A reset (any kind, really) clears reservations established with
10810 * RESERVE/RELEASE. It does not clear reservations established
10811 * with PERSISTENT RESERVE OUT, but we don't support that at the
10812 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
10813 * reservations made with the RESERVE/RELEASE commands, because
10814 * those commands are obsolete in SPC-3.
10816 lun->flags &= ~CTL_LUN_RESERVED;
10818 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
10819 ctl_clear_mask(lun->have_ca, i);
10820 lun->pending_sense[i].ua_pending |= ua_type;
10827 ctl_abort_task(union ctl_io *io)
10830 struct ctl_lun *lun;
10831 struct ctl_softc *ctl_softc;
10834 char printbuf[128];
10839 ctl_softc = control_softc;
10845 targ_lun = io->io_hdr.nexus.targ_lun;
10846 if (io->io_hdr.nexus.lun_map_fn != NULL)
10847 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
10848 if ((targ_lun < CTL_MAX_LUNS)
10849 && (ctl_softc->ctl_luns[targ_lun] != NULL))
10850 lun = ctl_softc->ctl_luns[targ_lun];
10855 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
10856 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
10860 * Run through the OOA queue and attempt to find the given I/O.
10861 * The target port, initiator ID, tag type and tag number have to
10862 * match the values that we got from the initiator. If we have an
10863 * untagged command to abort, simply abort the first untagged command
10864 * we come to. We only allow one untagged command at a time of course.
10867 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
10869 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
10870 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
10872 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
10874 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
10875 lun->lun, xio->scsiio.tag_num,
10876 xio->scsiio.tag_type,
10877 (xio->io_hdr.blocked_links.tqe_prev
10878 == NULL) ? "" : " BLOCKED",
10879 (xio->io_hdr.flags &
10880 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
10881 (xio->io_hdr.flags &
10882 CTL_FLAG_ABORT) ? " ABORT" : "",
10883 (xio->io_hdr.flags &
10884 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
10885 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
10887 printf("%s\n", sbuf_data(&sb));
10890 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
10891 && (xio->io_hdr.nexus.initid.id ==
10892 io->io_hdr.nexus.initid.id)) {
10894 * If the abort says that the task is untagged, the
10895 * task in the queue must be untagged. Otherwise,
10896 * we just check to see whether the tag numbers
10897 * match. This is because the QLogic firmware
10898 * doesn't pass back the tag type in an abort
10902 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
10903 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
10904 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
10907 * XXX KDM we've got problems with FC, because it
10908 * doesn't send down a tag type with aborts. So we
10909 * can only really go by the tag number...
10910 * This may cause problems with parallel SCSI.
10911 * Need to figure that out!!
10913 if (xio->scsiio.tag_num == io->taskio.tag_num) {
10914 xio->io_hdr.flags |= CTL_FLAG_ABORT;
10916 if ((io->io_hdr.flags &
10917 CTL_FLAG_FROM_OTHER_SC) == 0 &&
10918 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
10919 union ctl_ha_msg msg_info;
10921 io->io_hdr.flags |=
10922 CTL_FLAG_SENT_2OTHER_SC;
10923 msg_info.hdr.nexus = io->io_hdr.nexus;
10924 msg_info.task.task_action =
10925 CTL_TASK_ABORT_TASK;
10926 msg_info.task.tag_num =
10927 io->taskio.tag_num;
10928 msg_info.task.tag_type =
10929 io->taskio.tag_type;
10930 msg_info.hdr.msg_type =
10931 CTL_MSG_MANAGE_TASKS;
10932 msg_info.hdr.original_sc = NULL;
10933 msg_info.hdr.serializing_sc = NULL;
10935 printf("Sent Abort to other side\n");
10937 if (CTL_HA_STATUS_SUCCESS !=
10938 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10940 sizeof(msg_info), 0)) {
10944 printf("ctl_abort_task: found I/O to abort\n");
10955 * This isn't really an error. It's entirely possible for
10956 * the abort and command completion to cross on the wire.
10957 * This is more of an informative/diagnostic error.
10960 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
10961 "%d:%d:%d:%d tag %d type %d\n",
10962 io->io_hdr.nexus.initid.id,
10963 io->io_hdr.nexus.targ_port,
10964 io->io_hdr.nexus.targ_target.id,
10965 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
10966 io->taskio.tag_type);
10974 * This routine cannot block! It must be callable from an interrupt
10975 * handler as well as from the work thread.
10978 ctl_run_task_queue(struct ctl_softc *ctl_softc)
10980 union ctl_io *io, *next_io;
10982 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
10984 CTL_DEBUG_PRINT(("ctl_run_task_queue\n"));
10986 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->task_queue);
10987 io != NULL; io = next_io) {
10989 const char *task_desc;
10991 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
10995 switch (io->io_hdr.io_type) {
10996 case CTL_IO_TASK: {
10997 task_desc = ctl_scsi_task_string(&io->taskio);
10998 if (task_desc != NULL) {
11000 csevent_log(CSC_CTL | CSC_SHELF_SW |
11002 csevent_LogType_Trace,
11003 csevent_Severity_Information,
11004 csevent_AlertLevel_Green,
11005 csevent_FRU_Firmware,
11006 csevent_FRU_Unknown,
11007 "CTL: received task: %s",task_desc);
11011 csevent_log(CSC_CTL | CSC_SHELF_SW |
11013 csevent_LogType_Trace,
11014 csevent_Severity_Information,
11015 csevent_AlertLevel_Green,
11016 csevent_FRU_Firmware,
11017 csevent_FRU_Unknown,
11018 "CTL: received unknown task "
11020 io->taskio.task_action,
11021 io->taskio.task_action);
11024 switch (io->taskio.task_action) {
11025 case CTL_TASK_ABORT_TASK:
11026 retval = ctl_abort_task(io);
11028 case CTL_TASK_ABORT_TASK_SET:
11030 case CTL_TASK_CLEAR_ACA:
11032 case CTL_TASK_CLEAR_TASK_SET:
11034 case CTL_TASK_LUN_RESET: {
11035 struct ctl_lun *lun;
11039 targ_lun = io->io_hdr.nexus.targ_lun;
11040 if (io->io_hdr.nexus.lun_map_fn != NULL)
11041 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11043 if ((targ_lun < CTL_MAX_LUNS)
11044 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11045 lun = ctl_softc->ctl_luns[targ_lun];
11051 if (!(io->io_hdr.flags &
11052 CTL_FLAG_FROM_OTHER_SC)) {
11053 union ctl_ha_msg msg_info;
11055 io->io_hdr.flags |=
11056 CTL_FLAG_SENT_2OTHER_SC;
11057 msg_info.hdr.msg_type =
11058 CTL_MSG_MANAGE_TASKS;
11059 msg_info.hdr.nexus = io->io_hdr.nexus;
11060 msg_info.task.task_action =
11061 CTL_TASK_LUN_RESET;
11062 msg_info.hdr.original_sc = NULL;
11063 msg_info.hdr.serializing_sc = NULL;
11064 if (CTL_HA_STATUS_SUCCESS !=
11065 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11067 sizeof(msg_info), 0)) {
11071 retval = ctl_lun_reset(lun, io,
11075 case CTL_TASK_TARGET_RESET:
11076 retval = ctl_target_reset(ctl_softc, io,
11077 CTL_UA_TARG_RESET);
11079 case CTL_TASK_BUS_RESET:
11080 retval = ctl_bus_reset(ctl_softc, io);
11082 case CTL_TASK_PORT_LOGIN:
11084 case CTL_TASK_PORT_LOGOUT:
11087 printf("ctl_run_task_queue: got unknown task "
11088 "management event %d\n",
11089 io->taskio.task_action);
11093 io->io_hdr.status = CTL_SUCCESS;
11095 io->io_hdr.status = CTL_ERROR;
11097 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11098 ctl_io_hdr, links);
11100 * This will queue this I/O to the done queue, but the
11101 * work thread won't be able to process it until we
11102 * return and the lock is released.
11104 ctl_done_lock(io, /*have_lock*/ 1);
11109 printf("%s: invalid I/O type %d msg %d cdb %x"
11110 " iptl: %ju:%d:%ju:%d tag 0x%04x\n",
11111 __func__, io->io_hdr.io_type,
11112 io->io_hdr.msg_type, io->scsiio.cdb[0],
11113 (uintmax_t)io->io_hdr.nexus.initid.id,
11114 io->io_hdr.nexus.targ_port,
11115 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11116 io->io_hdr.nexus.targ_lun /* XXX */,
11117 (io->io_hdr.io_type == CTL_IO_TASK) ?
11118 io->taskio.tag_num : io->scsiio.tag_num);
11119 STAILQ_REMOVE(&ctl_softc->task_queue, &io->io_hdr,
11120 ctl_io_hdr, links);
11121 ctl_free_io_internal(io, 1);
11127 ctl_softc->flags &= ~CTL_FLAG_TASK_PENDING;
11131 * For HA operation. Handle commands that come in from the other
11135 ctl_handle_isc(union ctl_io *io)
11138 struct ctl_lun *lun;
11139 struct ctl_softc *ctl_softc;
11142 ctl_softc = control_softc;
11144 targ_lun = io->io_hdr.nexus.targ_lun;
11145 if (io->io_hdr.nexus.lun_map_fn != NULL)
11146 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
11147 lun = ctl_softc->ctl_luns[targ_lun];
11149 switch (io->io_hdr.msg_type) {
11150 case CTL_MSG_SERIALIZE:
11151 free_io = ctl_serialize_other_sc_cmd(&io->scsiio,
11154 case CTL_MSG_R2R: {
11156 struct ctl_cmd_entry *entry;
11159 * This is only used in SER_ONLY mode.
11162 opcode = io->scsiio.cdb[0];
11163 entry = &ctl_cmd_table[opcode];
11164 mtx_lock(&ctl_softc->ctl_lock);
11165 if (ctl_scsiio_lun_check(ctl_softc, lun,
11166 entry, (struct ctl_scsiio *)io) != 0) {
11167 ctl_done_lock(io, /*have_lock*/ 1);
11168 mtx_unlock(&ctl_softc->ctl_lock);
11171 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11172 STAILQ_INSERT_TAIL(&ctl_softc->rtr_queue,
11173 &io->io_hdr, links);
11174 mtx_unlock(&ctl_softc->ctl_lock);
11177 case CTL_MSG_FINISH_IO:
11178 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
11180 ctl_done_lock(io, /*have_lock*/ 0);
11183 mtx_lock(&ctl_softc->ctl_lock);
11184 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
11186 STAILQ_REMOVE(&ctl_softc->task_queue,
11187 &io->io_hdr, ctl_io_hdr, links);
11188 ctl_check_blocked(lun);
11189 mtx_unlock(&ctl_softc->ctl_lock);
11192 case CTL_MSG_PERS_ACTION:
11193 ctl_hndl_per_res_out_on_other_sc(
11194 (union ctl_ha_msg *)&io->presio.pr_msg);
11197 case CTL_MSG_BAD_JUJU:
11199 ctl_done_lock(io, /*have_lock*/ 0);
11201 case CTL_MSG_DATAMOVE:
11202 /* Only used in XFER mode */
11204 ctl_datamove_remote(io);
11206 case CTL_MSG_DATAMOVE_DONE:
11207 /* Only used in XFER mode */
11209 io->scsiio.be_move_done(io);
11213 printf("%s: Invalid message type %d\n",
11214 __func__, io->io_hdr.msg_type);
11218 ctl_free_io_internal(io, 0);
11224 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
11225 * there is no match.
11227 static ctl_lun_error_pattern
11228 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
11230 struct ctl_cmd_entry *entry;
11231 ctl_lun_error_pattern filtered_pattern, pattern;
11234 pattern = desc->error_pattern;
11237 * XXX KDM we need more data passed into this function to match a
11238 * custom pattern, and we actually need to implement custom pattern
11241 if (pattern & CTL_LUN_PAT_CMD)
11242 return (CTL_LUN_PAT_CMD);
11244 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
11245 return (CTL_LUN_PAT_ANY);
11247 opcode = ctsio->cdb[0];
11248 entry = &ctl_cmd_table[opcode];
11250 filtered_pattern = entry->pattern & pattern;
11253 * If the user requested specific flags in the pattern (e.g.
11254 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
11257 * If the user did not specify any flags, it doesn't matter whether
11258 * or not the command supports the flags.
11260 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
11261 (pattern & ~CTL_LUN_PAT_MASK))
11262 return (CTL_LUN_PAT_NONE);
11265 * If the user asked for a range check, see if the requested LBA
11266 * range overlaps with this command's LBA range.
11268 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
11274 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
11276 return (CTL_LUN_PAT_NONE);
11278 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
11279 desc->lba_range.len);
11281 * A "pass" means that the LBA ranges don't overlap, so
11282 * this doesn't match the user's range criteria.
11284 if (action == CTL_ACTION_PASS)
11285 return (CTL_LUN_PAT_NONE);
11288 return (filtered_pattern);
11292 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
11294 struct ctl_error_desc *desc, *desc2;
11296 mtx_assert(&control_softc->ctl_lock, MA_OWNED);
11298 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
11299 ctl_lun_error_pattern pattern;
11301 * Check to see whether this particular command matches
11302 * the pattern in the descriptor.
11304 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
11305 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
11308 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
11309 case CTL_LUN_INJ_ABORTED:
11310 ctl_set_aborted(&io->scsiio);
11312 case CTL_LUN_INJ_MEDIUM_ERR:
11313 ctl_set_medium_error(&io->scsiio);
11315 case CTL_LUN_INJ_UA:
11316 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
11318 ctl_set_ua(&io->scsiio, 0x29, 0x00);
11320 case CTL_LUN_INJ_CUSTOM:
11322 * We're assuming the user knows what he is doing.
11323 * Just copy the sense information without doing
11326 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
11327 ctl_min(sizeof(desc->custom_sense),
11328 sizeof(io->scsiio.sense_data)));
11329 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
11330 io->scsiio.sense_len = SSD_FULL_SIZE;
11331 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11333 case CTL_LUN_INJ_NONE:
11336 * If this is an error injection type we don't know
11337 * about, clear the continuous flag (if it is set)
11338 * so it will get deleted below.
11340 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
11344 * By default, each error injection action is a one-shot
11346 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
11349 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
11355 #ifdef CTL_IO_DELAY
11357 ctl_datamove_timer_wakeup(void *arg)
11361 io = (union ctl_io *)arg;
11365 #endif /* CTL_IO_DELAY */
11368 ctl_datamove(union ctl_io *io)
11370 void (*fe_datamove)(union ctl_io *io);
11372 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
11374 CTL_DEBUG_PRINT(("ctl_datamove\n"));
11377 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
11382 ctl_scsi_path_string(io, path_str, sizeof(path_str));
11383 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11385 sbuf_cat(&sb, path_str);
11386 switch (io->io_hdr.io_type) {
11388 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
11389 sbuf_printf(&sb, "\n");
11390 sbuf_cat(&sb, path_str);
11391 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11392 io->scsiio.tag_num, io->scsiio.tag_type);
11395 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
11396 "Tag Type: %d\n", io->taskio.task_action,
11397 io->taskio.tag_num, io->taskio.tag_type);
11400 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11401 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
11404 sbuf_cat(&sb, path_str);
11405 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
11406 (intmax_t)time_uptime - io->io_hdr.start_time);
11408 printf("%s", sbuf_data(&sb));
11410 #endif /* CTL_TIME_IO */
11412 mtx_lock(&control_softc->ctl_lock);
11413 #ifdef CTL_IO_DELAY
11414 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
11415 struct ctl_lun *lun;
11417 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11419 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
11421 struct ctl_lun *lun;
11423 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
11425 && (lun->delay_info.datamove_delay > 0)) {
11426 struct callout *callout;
11428 callout = (struct callout *)&io->io_hdr.timer_bytes;
11429 callout_init(callout, /*mpsafe*/ 1);
11430 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
11431 callout_reset(callout,
11432 lun->delay_info.datamove_delay * hz,
11433 ctl_datamove_timer_wakeup, io);
11434 if (lun->delay_info.datamove_type ==
11435 CTL_DELAY_TYPE_ONESHOT)
11436 lun->delay_info.datamove_delay = 0;
11437 mtx_unlock(&control_softc->ctl_lock);
11443 * If we have any pending task management commands, process them
11444 * first. This is necessary to eliminate a race condition with the
11447 * - FETD submits a task management command, like an abort.
11448 * - Back end calls fe_datamove() to move the data for the aborted
11449 * command. The FETD can't really accept it, but if it did, it
11450 * would end up transmitting data for a command that the initiator
11451 * told us to abort.
11453 * We close the race by processing all pending task management
11454 * commands here (we can't block!), and then check this I/O to see
11455 * if it has been aborted. If so, return it to the back end with
11456 * bad status, so the back end can say return an error to the back end
11457 * and then when the back end returns an error, we can return the
11458 * aborted command to the FETD, so it can clean up its resources.
11460 if (control_softc->flags & CTL_FLAG_TASK_PENDING)
11461 ctl_run_task_queue(control_softc);
11464 * This command has been aborted. Set the port status, so we fail
11467 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
11468 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
11469 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
11470 io->io_hdr.nexus.targ_port,
11471 (uintmax_t)io->io_hdr.nexus.targ_target.id,
11472 io->io_hdr.nexus.targ_lun);
11473 io->io_hdr.status = CTL_CMD_ABORTED;
11474 io->io_hdr.port_status = 31337;
11475 mtx_unlock(&control_softc->ctl_lock);
11477 * Note that the backend, in this case, will get the
11478 * callback in its context. In other cases it may get
11479 * called in the frontend's interrupt thread context.
11481 io->scsiio.be_move_done(io);
11486 * If we're in XFER mode and this I/O is from the other shelf
11487 * controller, we need to send the DMA to the other side to
11488 * actually transfer the data to/from the host. In serialize only
11489 * mode the transfer happens below CTL and ctl_datamove() is only
11490 * called on the machine that originally received the I/O.
11492 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
11493 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11494 union ctl_ha_msg msg;
11495 uint32_t sg_entries_sent;
11499 memset(&msg, 0, sizeof(msg));
11500 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
11501 msg.hdr.original_sc = io->io_hdr.original_sc;
11502 msg.hdr.serializing_sc = io;
11503 msg.hdr.nexus = io->io_hdr.nexus;
11504 msg.dt.flags = io->io_hdr.flags;
11506 * We convert everything into a S/G list here. We can't
11507 * pass by reference, only by value between controllers.
11508 * So we can't pass a pointer to the S/G list, only as many
11509 * S/G entries as we can fit in here. If it's possible for
11510 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
11511 * then we need to break this up into multiple transfers.
11513 if (io->scsiio.kern_sg_entries == 0) {
11514 msg.dt.kern_sg_entries = 1;
11516 * If this is in cached memory, flush the cache
11517 * before we send the DMA request to the other
11518 * controller. We want to do this in either the
11519 * read or the write case. The read case is
11520 * straightforward. In the write case, we want to
11521 * make sure nothing is in the local cache that
11522 * could overwrite the DMAed data.
11524 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11526 * XXX KDM use bus_dmamap_sync() here.
11531 * Convert to a physical address if this is a
11534 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
11535 msg.dt.sg_list[0].addr =
11536 io->scsiio.kern_data_ptr;
11539 * XXX KDM use busdma here!
11542 msg.dt.sg_list[0].addr = (void *)
11543 vtophys(io->scsiio.kern_data_ptr);
11547 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
11550 struct ctl_sg_entry *sgl;
11553 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
11554 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
11555 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
11557 * XXX KDM use bus_dmamap_sync() here.
11562 msg.dt.kern_data_len = io->scsiio.kern_data_len;
11563 msg.dt.kern_total_len = io->scsiio.kern_total_len;
11564 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
11565 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
11566 msg.dt.sg_sequence = 0;
11569 * Loop until we've sent all of the S/G entries. On the
11570 * other end, we'll recompose these S/G entries into one
11571 * contiguous list before passing it to the
11573 for (sg_entries_sent = 0; sg_entries_sent <
11574 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
11575 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
11576 sizeof(msg.dt.sg_list[0])),
11577 msg.dt.kern_sg_entries - sg_entries_sent);
11579 if (do_sg_copy != 0) {
11580 struct ctl_sg_entry *sgl;
11583 sgl = (struct ctl_sg_entry *)
11584 io->scsiio.kern_data_ptr;
11586 * If this is in cached memory, flush the cache
11587 * before we send the DMA request to the other
11588 * controller. We want to do this in either
11589 * the * read or the write case. The read
11590 * case is straightforward. In the write
11591 * case, we want to make sure nothing is
11592 * in the local cache that could overwrite
11596 for (i = sg_entries_sent, j = 0;
11597 i < msg.dt.cur_sg_entries; i++, j++) {
11598 if ((io->io_hdr.flags &
11599 CTL_FLAG_NO_DATASYNC) == 0) {
11601 * XXX KDM use bus_dmamap_sync()
11604 if ((io->io_hdr.flags &
11605 CTL_FLAG_BUS_ADDR) == 0) {
11607 * XXX KDM use busdma.
11610 msg.dt.sg_list[j].addr =(void *)
11611 vtophys(sgl[i].addr);
11614 msg.dt.sg_list[j].addr =
11617 msg.dt.sg_list[j].len = sgl[i].len;
11621 sg_entries_sent += msg.dt.cur_sg_entries;
11622 if (sg_entries_sent >= msg.dt.kern_sg_entries)
11623 msg.dt.sg_last = 1;
11625 msg.dt.sg_last = 0;
11628 * XXX KDM drop and reacquire the lock here?
11630 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
11631 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
11633 * XXX do something here.
11637 msg.dt.sent_sg_entries = sg_entries_sent;
11639 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11640 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
11641 ctl_failover_io(io, /*have_lock*/ 1);
11646 * Lookup the fe_datamove() function for this particular
11650 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11651 mtx_unlock(&control_softc->ctl_lock);
11658 ctl_send_datamove_done(union ctl_io *io, int have_lock)
11660 union ctl_ha_msg msg;
11663 memset(&msg, 0, sizeof(msg));
11665 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
11666 msg.hdr.original_sc = io;
11667 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
11668 msg.hdr.nexus = io->io_hdr.nexus;
11669 msg.hdr.status = io->io_hdr.status;
11670 msg.scsi.tag_num = io->scsiio.tag_num;
11671 msg.scsi.tag_type = io->scsiio.tag_type;
11672 msg.scsi.scsi_status = io->scsiio.scsi_status;
11673 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
11674 sizeof(io->scsiio.sense_data));
11675 msg.scsi.sense_len = io->scsiio.sense_len;
11676 msg.scsi.sense_residual = io->scsiio.sense_residual;
11677 msg.scsi.fetd_status = io->io_hdr.port_status;
11678 msg.scsi.residual = io->scsiio.residual;
11679 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11681 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
11682 ctl_failover_io(io, /*have_lock*/ have_lock);
11686 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
11687 if (isc_status > CTL_HA_STATUS_SUCCESS) {
11688 /* XXX do something if this fails */
11694 * The DMA to the remote side is done, now we need to tell the other side
11695 * we're done so it can continue with its data movement.
11698 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
11704 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
11705 printf("%s: ISC DMA write failed with error %d", __func__,
11707 ctl_set_internal_failure(&io->scsiio,
11709 /*retry_count*/ rq->ret);
11712 ctl_dt_req_free(rq);
11715 * In this case, we had to malloc the memory locally. Free it.
11717 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
11719 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11720 free(io->io_hdr.local_sglist[i].addr, M_CTL);
11723 * The data is in local and remote memory, so now we need to send
11724 * status (good or back) back to the other side.
11726 ctl_send_datamove_done(io, /*have_lock*/ 0);
11730 * We've moved the data from the host/controller into local memory. Now we
11731 * need to push it over to the remote controller's memory.
11734 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
11740 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
11741 ctl_datamove_remote_write_cb);
11747 ctl_datamove_remote_write(union ctl_io *io)
11750 void (*fe_datamove)(union ctl_io *io);
11753 * - Get the data from the host/HBA into local memory.
11754 * - DMA memory from the local controller to the remote controller.
11755 * - Send status back to the remote controller.
11758 retval = ctl_datamove_remote_sgl_setup(io);
11762 /* Switch the pointer over so the FETD knows what to do */
11763 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
11766 * Use a custom move done callback, since we need to send completion
11767 * back to the other controller, not to the backend on this side.
11769 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
11771 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11780 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
11789 * In this case, we had to malloc the memory locally. Free it.
11791 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
11793 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11794 free(io->io_hdr.local_sglist[i].addr, M_CTL);
11798 scsi_path_string(io, path_str, sizeof(path_str));
11799 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
11800 sbuf_cat(&sb, path_str);
11801 scsi_command_string(&io->scsiio, NULL, &sb);
11802 sbuf_printf(&sb, "\n");
11803 sbuf_cat(&sb, path_str);
11804 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
11805 io->scsiio.tag_num, io->scsiio.tag_type);
11806 sbuf_cat(&sb, path_str);
11807 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
11808 io->io_hdr.flags, io->io_hdr.status);
11810 printk("%s", sbuf_data(&sb));
11815 * The read is done, now we need to send status (good or bad) back
11816 * to the other side.
11818 ctl_send_datamove_done(io, /*have_lock*/ 0);
11824 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
11827 void (*fe_datamove)(union ctl_io *io);
11831 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
11832 printf("%s: ISC DMA read failed with error %d", __func__,
11834 ctl_set_internal_failure(&io->scsiio,
11836 /*retry_count*/ rq->ret);
11839 ctl_dt_req_free(rq);
11841 /* Switch the pointer over so the FETD knows what to do */
11842 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
11845 * Use a custom move done callback, since we need to send completion
11846 * back to the other controller, not to the backend on this side.
11848 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
11850 /* XXX KDM add checks like the ones in ctl_datamove? */
11852 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
11858 ctl_datamove_remote_sgl_setup(union ctl_io *io)
11860 struct ctl_sg_entry *local_sglist, *remote_sglist;
11861 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
11862 struct ctl_softc *softc;
11867 softc = control_softc;
11869 local_sglist = io->io_hdr.local_sglist;
11870 local_dma_sglist = io->io_hdr.local_dma_sglist;
11871 remote_sglist = io->io_hdr.remote_sglist;
11872 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
11874 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
11875 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
11876 local_sglist[i].len = remote_sglist[i].len;
11879 * XXX Detect the situation where the RS-level I/O
11880 * redirector on the other side has already read the
11881 * data off of the AOR RS on this side, and
11882 * transferred it to remote (mirror) memory on the
11883 * other side. Since we already have the data in
11884 * memory here, we just need to use it.
11886 * XXX KDM this can probably be removed once we
11887 * get the cache device code in and take the
11888 * current AOR implementation out.
11891 if ((remote_sglist[i].addr >=
11892 (void *)vtophys(softc->mirr->addr))
11893 && (remote_sglist[i].addr <
11894 ((void *)vtophys(softc->mirr->addr) +
11895 CacheMirrorOffset))) {
11896 local_sglist[i].addr = remote_sglist[i].addr -
11898 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
11900 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
11902 local_sglist[i].addr = remote_sglist[i].addr +
11907 printf("%s: local %p, remote %p, len %d\n",
11908 __func__, local_sglist[i].addr,
11909 remote_sglist[i].addr, local_sglist[i].len);
11913 uint32_t len_to_go;
11916 * In this case, we don't have automatically allocated
11917 * memory for this I/O on this controller. This typically
11918 * happens with internal CTL I/O -- e.g. inquiry, mode
11919 * sense, etc. Anything coming from RAIDCore will have
11920 * a mirror area available.
11922 len_to_go = io->scsiio.kern_data_len;
11925 * Clear the no datasync flag, we have to use malloced
11928 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
11931 * The difficult thing here is that the size of the various
11932 * S/G segments may be different than the size from the
11933 * remote controller. That'll make it harder when DMAing
11934 * the data back to the other side.
11936 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
11937 sizeof(io->io_hdr.remote_sglist[0])) &&
11938 (len_to_go > 0); i++) {
11939 local_sglist[i].len = ctl_min(len_to_go, 131072);
11940 CTL_SIZE_8B(local_dma_sglist[i].len,
11941 local_sglist[i].len);
11942 local_sglist[i].addr =
11943 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
11945 local_dma_sglist[i].addr = local_sglist[i].addr;
11947 if (local_sglist[i].addr == NULL) {
11950 printf("malloc failed for %zd bytes!",
11951 local_dma_sglist[i].len);
11952 for (j = 0; j < i; j++) {
11953 free(local_sglist[j].addr, M_CTL);
11955 ctl_set_internal_failure(&io->scsiio,
11957 /*retry_count*/ 4857);
11959 goto bailout_error;
11962 /* XXX KDM do we need a sync here? */
11964 len_to_go -= local_sglist[i].len;
11967 * Reset the number of S/G entries accordingly. The
11968 * original number of S/G entries is available in
11971 io->scsiio.kern_sg_entries = i;
11974 printf("%s: kern_sg_entries = %d\n", __func__,
11975 io->scsiio.kern_sg_entries);
11976 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
11977 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
11978 local_sglist[i].addr, local_sglist[i].len,
11979 local_dma_sglist[i].len);
11988 ctl_send_datamove_done(io, /*have_lock*/ 0);
11994 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
11995 ctl_ha_dt_cb callback)
11997 struct ctl_ha_dt_req *rq;
11998 struct ctl_sg_entry *remote_sglist, *local_sglist;
11999 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
12000 uint32_t local_used, remote_used, total_used;
12006 rq = ctl_dt_req_alloc();
12009 * If we failed to allocate the request, and if the DMA didn't fail
12010 * anyway, set busy status. This is just a resource allocation
12014 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
12015 ctl_set_busy(&io->scsiio);
12017 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
12020 ctl_dt_req_free(rq);
12023 * The data move failed. We need to return status back
12024 * to the other controller. No point in trying to DMA
12025 * data to the remote controller.
12028 ctl_send_datamove_done(io, /*have_lock*/ 0);
12035 local_sglist = io->io_hdr.local_sglist;
12036 local_dma_sglist = io->io_hdr.local_dma_sglist;
12037 remote_sglist = io->io_hdr.remote_sglist;
12038 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12043 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
12044 rq->ret = CTL_HA_STATUS_SUCCESS;
12051 * Pull/push the data over the wire from/to the other controller.
12052 * This takes into account the possibility that the local and
12053 * remote sglists may not be identical in terms of the size of
12054 * the elements and the number of elements.
12056 * One fundamental assumption here is that the length allocated for
12057 * both the local and remote sglists is identical. Otherwise, we've
12058 * essentially got a coding error of some sort.
12060 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12062 uint32_t cur_len, dma_length;
12065 rq->id = CTL_HA_DATA_CTL;
12066 rq->command = command;
12070 * Both pointers should be aligned. But it is possible
12071 * that the allocation length is not. They should both
12072 * also have enough slack left over at the end, though,
12073 * to round up to the next 8 byte boundary.
12075 cur_len = ctl_min(local_sglist[i].len - local_used,
12076 remote_sglist[j].len - remote_used);
12079 * In this case, we have a size issue and need to decrease
12080 * the size, except in the case where we actually have less
12081 * than 8 bytes left. In that case, we need to increase
12082 * the DMA length to get the last bit.
12084 if ((cur_len & 0x7) != 0) {
12085 if (cur_len > 0x7) {
12086 cur_len = cur_len - (cur_len & 0x7);
12087 dma_length = cur_len;
12089 CTL_SIZE_8B(dma_length, cur_len);
12093 dma_length = cur_len;
12096 * If we had to allocate memory for this I/O, instead of using
12097 * the non-cached mirror memory, we'll need to flush the cache
12098 * before trying to DMA to the other controller.
12100 * We could end up doing this multiple times for the same
12101 * segment if we have a larger local segment than remote
12102 * segment. That shouldn't be an issue.
12104 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12106 * XXX KDM use bus_dmamap_sync() here.
12110 rq->size = dma_length;
12112 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12113 tmp_ptr += local_used;
12115 /* Use physical addresses when talking to ISC hardware */
12116 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12117 /* XXX KDM use busdma */
12119 rq->local = vtophys(tmp_ptr);
12122 rq->local = tmp_ptr;
12124 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12125 tmp_ptr += remote_used;
12126 rq->remote = tmp_ptr;
12128 rq->callback = NULL;
12130 local_used += cur_len;
12131 if (local_used >= local_sglist[i].len) {
12136 remote_used += cur_len;
12137 if (remote_used >= remote_sglist[j].len) {
12141 total_used += cur_len;
12143 if (total_used >= io->scsiio.kern_data_len)
12144 rq->callback = callback;
12146 if ((rq->size & 0x7) != 0) {
12147 printf("%s: warning: size %d is not on 8b boundary\n",
12148 __func__, rq->size);
12150 if (((uintptr_t)rq->local & 0x7) != 0) {
12151 printf("%s: warning: local %p not on 8b boundary\n",
12152 __func__, rq->local);
12154 if (((uintptr_t)rq->remote & 0x7) != 0) {
12155 printf("%s: warning: remote %p not on 8b boundary\n",
12156 __func__, rq->local);
12159 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
12160 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12161 rq->local, rq->remote, rq->size);
12164 isc_ret = ctl_dt_single(rq);
12165 if (isc_ret == CTL_HA_STATUS_WAIT)
12168 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
12169 rq->ret = CTL_HA_STATUS_SUCCESS;
12183 ctl_datamove_remote_read(union ctl_io *io)
12189 * This will send an error to the other controller in the case of a
12192 retval = ctl_datamove_remote_sgl_setup(io);
12196 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12197 ctl_datamove_remote_read_cb);
12199 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
12201 * Make sure we free memory if there was an error.. The
12202 * ctl_datamove_remote_xfer() function will send the
12203 * datamove done message, or call the callback with an
12204 * error if there is a problem.
12206 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12207 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12214 * Process a datamove request from the other controller. This is used for
12215 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12216 * first. Once that is complete, the data gets DMAed into the remote
12217 * controller's memory. For reads, we DMA from the remote controller's
12218 * memory into our memory first, and then move it out to the FETD.
12221 ctl_datamove_remote(union ctl_io *io)
12223 struct ctl_softc *softc;
12225 softc = control_softc;
12227 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
12230 * Note that we look for an aborted I/O here, but don't do some of
12231 * the other checks that ctl_datamove() normally does. We don't
12232 * need to run the task queue, because this I/O is on the ISC
12233 * queue, which is executed by the work thread after the task queue.
12234 * We don't need to run the datamove delay code, since that should
12235 * have been done if need be on the other controller.
12237 mtx_lock(&softc->ctl_lock);
12239 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12241 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
12242 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
12243 io->io_hdr.nexus.targ_port,
12244 io->io_hdr.nexus.targ_target.id,
12245 io->io_hdr.nexus.targ_lun);
12246 io->io_hdr.status = CTL_CMD_ABORTED;
12247 io->io_hdr.port_status = 31338;
12249 mtx_unlock(&softc->ctl_lock);
12251 ctl_send_datamove_done(io, /*have_lock*/ 0);
12256 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
12257 mtx_unlock(&softc->ctl_lock);
12258 ctl_datamove_remote_write(io);
12259 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
12260 mtx_unlock(&softc->ctl_lock);
12261 ctl_datamove_remote_read(io);
12263 union ctl_ha_msg msg;
12264 struct scsi_sense_data *sense;
12268 memset(&msg, 0, sizeof(msg));
12270 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
12271 msg.hdr.status = CTL_SCSI_ERROR;
12272 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
12274 retry_count = 4243;
12276 sense = &msg.scsi.sense_data;
12277 sks[0] = SSD_SCS_VALID;
12278 sks[1] = (retry_count >> 8) & 0xff;
12279 sks[2] = retry_count & 0xff;
12281 /* "Internal target failure" */
12282 scsi_set_sense_data(sense,
12283 /*sense_format*/ SSD_TYPE_NONE,
12284 /*current_error*/ 1,
12285 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
12288 /*type*/ SSD_ELEM_SKS,
12289 /*size*/ sizeof(sks),
12293 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12294 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12295 ctl_failover_io(io, /*have_lock*/ 1);
12296 mtx_unlock(&softc->ctl_lock);
12300 mtx_unlock(&softc->ctl_lock);
12302 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
12303 CTL_HA_STATUS_SUCCESS) {
12304 /* XXX KDM what to do if this fails? */
12312 ctl_process_done(union ctl_io *io, int have_lock)
12314 struct ctl_lun *lun;
12315 struct ctl_softc *ctl_softc;
12316 void (*fe_done)(union ctl_io *io);
12317 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
12319 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12322 control_softc->ctl_ports[targ_port]->fe_done;
12325 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12330 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12331 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12333 sbuf_cat(&sb, path_str);
12334 switch (io->io_hdr.io_type) {
12336 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12337 sbuf_printf(&sb, "\n");
12338 sbuf_cat(&sb, path_str);
12339 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12340 io->scsiio.tag_num, io->scsiio.tag_type);
12343 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12344 "Tag Type: %d\n", io->taskio.task_action,
12345 io->taskio.tag_num, io->taskio.tag_type);
12348 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12349 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12352 sbuf_cat(&sb, path_str);
12353 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12354 (intmax_t)time_uptime - io->io_hdr.start_time);
12356 printf("%s", sbuf_data(&sb));
12358 #endif /* CTL_TIME_IO */
12360 switch (io->io_hdr.io_type) {
12364 ctl_io_error_print(io, NULL);
12365 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
12366 ctl_free_io_internal(io, /*have_lock*/ 0);
12369 return (CTL_RETVAL_COMPLETE);
12372 printf("ctl_process_done: invalid io type %d\n",
12373 io->io_hdr.io_type);
12374 panic("ctl_process_done: invalid io type %d\n",
12375 io->io_hdr.io_type);
12376 break; /* NOTREACHED */
12379 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12381 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12382 io->io_hdr.nexus.targ_lun));
12386 ctl_softc = lun->ctl_softc;
12389 * Remove this from the OOA queue.
12391 if (have_lock == 0)
12392 mtx_lock(&ctl_softc->ctl_lock);
12395 * Check to see if we have any errors to inject here. We only
12396 * inject errors for commands that don't already have errors set.
12398 if ((STAILQ_FIRST(&lun->error_list) != NULL)
12399 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
12400 ctl_inject_error(lun, io);
12403 * XXX KDM how do we treat commands that aren't completed
12406 * XXX KDM should we also track I/O latency?
12408 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
12409 uint32_t blocksize;
12411 struct bintime cur_bt;
12414 if ((lun->be_lun != NULL)
12415 && (lun->be_lun->blocksize != 0))
12416 blocksize = lun->be_lun->blocksize;
12420 switch (io->io_hdr.io_type) {
12421 case CTL_IO_SCSI: {
12423 struct ctl_lba_len lbalen;
12426 switch (io->scsiio.cdb[0]) {
12437 case WRITE_VERIFY_10:
12438 case WRITE_VERIFY_12:
12439 case WRITE_VERIFY_16:
12440 memcpy(&lbalen, io->io_hdr.ctl_private[
12441 CTL_PRIV_LBA_LEN].bytes, sizeof(lbalen));
12444 lun->stats.ports[targ_port].bytes[CTL_STATS_READ] +=
12445 lbalen.len * blocksize;
12446 lun->stats.ports[targ_port].operations[CTL_STATS_READ]++;
12450 &lun->stats.ports[targ_port].dma_time[CTL_STATS_READ],
12451 &io->io_hdr.dma_bt);
12452 lun->stats.ports[targ_port].num_dmas[CTL_STATS_READ] +=
12453 io->io_hdr.num_dmas;
12454 getbintime(&cur_bt);
12455 bintime_sub(&cur_bt,
12456 &io->io_hdr.start_bt);
12459 &lun->stats.ports[targ_port].time[CTL_STATS_READ],
12463 cs_prof_gettime(&cur_ticks);
12464 lun->stats.time[CTL_STATS_READ] +=
12466 io->io_hdr.start_ticks;
12469 lun->stats.time[CTL_STATS_READ] +=
12470 jiffies - io->io_hdr.start_time;
12472 #endif /* CTL_TIME_IO */
12474 lun->stats.ports[targ_port].bytes[CTL_STATS_WRITE] +=
12475 lbalen.len * blocksize;
12476 lun->stats.ports[targ_port].operations[
12477 CTL_STATS_WRITE]++;
12481 &lun->stats.ports[targ_port].dma_time[CTL_STATS_WRITE],
12482 &io->io_hdr.dma_bt);
12483 lun->stats.ports[targ_port].num_dmas[CTL_STATS_WRITE] +=
12484 io->io_hdr.num_dmas;
12485 getbintime(&cur_bt);
12486 bintime_sub(&cur_bt,
12487 &io->io_hdr.start_bt);
12490 &lun->stats.ports[targ_port].time[CTL_STATS_WRITE],
12493 cs_prof_gettime(&cur_ticks);
12494 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12496 io->io_hdr.start_ticks;
12497 lun->stats.ports[targ_port].time[CTL_STATS_WRITE] +=
12498 jiffies - io->io_hdr.start_time;
12500 #endif /* CTL_TIME_IO */
12504 lun->stats.ports[targ_port].operations[CTL_STATS_NO_IO]++;
12508 &lun->stats.ports[targ_port].dma_time[CTL_STATS_NO_IO],
12509 &io->io_hdr.dma_bt);
12510 lun->stats.ports[targ_port].num_dmas[CTL_STATS_NO_IO] +=
12511 io->io_hdr.num_dmas;
12512 getbintime(&cur_bt);
12513 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
12515 bintime_add(&lun->stats.ports[targ_port].time[CTL_STATS_NO_IO],
12519 cs_prof_gettime(&cur_ticks);
12520 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12522 io->io_hdr.start_ticks;
12523 lun->stats.ports[targ_port].time[CTL_STATS_NO_IO] +=
12524 jiffies - io->io_hdr.start_time;
12526 #endif /* CTL_TIME_IO */
12536 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12539 * Run through the blocked queue on this LUN and see if anything
12540 * has become unblocked, now that this transaction is done.
12542 ctl_check_blocked(lun);
12545 * If the LUN has been invalidated, free it if there is nothing
12546 * left on its OOA queue.
12548 if ((lun->flags & CTL_LUN_INVALID)
12549 && (TAILQ_FIRST(&lun->ooa_queue) == NULL))
12553 * If this command has been aborted, make sure we set the status
12554 * properly. The FETD is responsible for freeing the I/O and doing
12555 * whatever it needs to do to clean up its state.
12557 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12558 io->io_hdr.status = CTL_CMD_ABORTED;
12561 * We print out status for every task management command. For SCSI
12562 * commands, we filter out any unit attention errors; they happen
12563 * on every boot, and would clutter up the log. Note: task
12564 * management commands aren't printed here, they are printed above,
12565 * since they should never even make it down here.
12567 switch (io->io_hdr.io_type) {
12568 case CTL_IO_SCSI: {
12569 int error_code, sense_key, asc, ascq;
12573 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
12574 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
12576 * Since this is just for printing, no need to
12577 * show errors here.
12579 scsi_extract_sense_len(&io->scsiio.sense_data,
12580 io->scsiio.sense_len,
12585 /*show_errors*/ 0);
12588 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
12589 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
12590 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
12591 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
12593 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
12594 ctl_softc->skipped_prints++;
12595 if (have_lock == 0)
12596 mtx_unlock(&ctl_softc->ctl_lock);
12598 uint32_t skipped_prints;
12600 skipped_prints = ctl_softc->skipped_prints;
12602 ctl_softc->skipped_prints = 0;
12603 ctl_softc->last_print_jiffies = time_uptime;
12605 if (have_lock == 0)
12606 mtx_unlock(&ctl_softc->ctl_lock);
12607 if (skipped_prints > 0) {
12609 csevent_log(CSC_CTL | CSC_SHELF_SW |
12611 csevent_LogType_Trace,
12612 csevent_Severity_Information,
12613 csevent_AlertLevel_Green,
12614 csevent_FRU_Firmware,
12615 csevent_FRU_Unknown,
12616 "High CTL error volume, %d prints "
12617 "skipped", skipped_prints);
12620 ctl_io_error_print(io, NULL);
12623 if (have_lock == 0)
12624 mtx_unlock(&ctl_softc->ctl_lock);
12629 if (have_lock == 0)
12630 mtx_unlock(&ctl_softc->ctl_lock);
12631 ctl_io_error_print(io, NULL);
12634 if (have_lock == 0)
12635 mtx_unlock(&ctl_softc->ctl_lock);
12640 * Tell the FETD or the other shelf controller we're done with this
12641 * command. Note that only SCSI commands get to this point. Task
12642 * management commands are completed above.
12644 * We only send status to the other controller if we're in XFER
12645 * mode. In SER_ONLY mode, the I/O is done on the controller that
12646 * received the I/O (from CTL's perspective), and so the status is
12649 * XXX KDM if we hold the lock here, we could cause a deadlock
12650 * if the frontend comes back in in this context to queue
12653 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
12654 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12655 union ctl_ha_msg msg;
12657 memset(&msg, 0, sizeof(msg));
12658 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
12659 msg.hdr.original_sc = io->io_hdr.original_sc;
12660 msg.hdr.nexus = io->io_hdr.nexus;
12661 msg.hdr.status = io->io_hdr.status;
12662 msg.scsi.scsi_status = io->scsiio.scsi_status;
12663 msg.scsi.tag_num = io->scsiio.tag_num;
12664 msg.scsi.tag_type = io->scsiio.tag_type;
12665 msg.scsi.sense_len = io->scsiio.sense_len;
12666 msg.scsi.sense_residual = io->scsiio.sense_residual;
12667 msg.scsi.residual = io->scsiio.residual;
12668 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12669 sizeof(io->scsiio.sense_data));
12671 * We copy this whether or not this is an I/O-related
12672 * command. Otherwise, we'd have to go and check to see
12673 * whether it's a read/write command, and it really isn't
12676 memcpy(&msg.scsi.lbalen,
12677 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
12678 sizeof(msg.scsi.lbalen));
12680 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12681 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12682 /* XXX do something here */
12685 ctl_free_io_internal(io, /*have_lock*/ 0);
12691 return (CTL_RETVAL_COMPLETE);
12695 * Front end should call this if it doesn't do autosense. When the request
12696 * sense comes back in from the initiator, we'll dequeue this and send it.
12699 ctl_queue_sense(union ctl_io *io)
12701 struct ctl_lun *lun;
12702 struct ctl_softc *ctl_softc;
12703 uint32_t initidx, targ_lun;
12705 ctl_softc = control_softc;
12707 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
12710 * LUN lookup will likely move to the ctl_work_thread() once we
12711 * have our new queueing infrastructure (that doesn't put things on
12712 * a per-LUN queue initially). That is so that we can handle
12713 * things like an INQUIRY to a LUN that we don't have enabled. We
12714 * can't deal with that right now.
12716 mtx_lock(&ctl_softc->ctl_lock);
12719 * If we don't have a LUN for this, just toss the sense
12722 targ_lun = io->io_hdr.nexus.targ_lun;
12723 if (io->io_hdr.nexus.lun_map_fn != NULL)
12724 targ_lun = io->io_hdr.nexus.lun_map_fn(io->io_hdr.nexus.lun_map_arg, targ_lun);
12725 if ((targ_lun < CTL_MAX_LUNS)
12726 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12727 lun = ctl_softc->ctl_luns[targ_lun];
12731 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12734 * Already have CA set for this LUN...toss the sense information.
12736 if (ctl_is_set(lun->have_ca, initidx))
12739 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
12740 ctl_min(sizeof(lun->pending_sense[initidx].sense),
12741 sizeof(io->scsiio.sense_data)));
12742 ctl_set_mask(lun->have_ca, initidx);
12745 mtx_unlock(&ctl_softc->ctl_lock);
12749 return (CTL_RETVAL_COMPLETE);
12753 * Primary command inlet from frontend ports. All SCSI and task I/O
12754 * requests must go through this function.
12757 ctl_queue(union ctl_io *io)
12759 struct ctl_softc *ctl_softc;
12761 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
12763 ctl_softc = control_softc;
12766 io->io_hdr.start_time = time_uptime;
12767 getbintime(&io->io_hdr.start_bt);
12768 #endif /* CTL_TIME_IO */
12770 mtx_lock(&ctl_softc->ctl_lock);
12772 switch (io->io_hdr.io_type) {
12774 STAILQ_INSERT_TAIL(&ctl_softc->incoming_queue, &io->io_hdr,
12778 STAILQ_INSERT_TAIL(&ctl_softc->task_queue, &io->io_hdr, links);
12780 * Set the task pending flag. This is necessary to close a
12781 * race condition with the FETD:
12783 * - FETD submits a task management command, like an abort.
12784 * - Back end calls fe_datamove() to move the data for the
12785 * aborted command. The FETD can't really accept it, but
12786 * if it did, it would end up transmitting data for a
12787 * command that the initiator told us to abort.
12789 * We close the race condition by setting the flag here,
12790 * and checking it in ctl_datamove(), before calling the
12791 * FETD's fe_datamove routine. If we've got a task
12792 * pending, we run the task queue and then check to see
12793 * whether our particular I/O has been aborted.
12795 ctl_softc->flags |= CTL_FLAG_TASK_PENDING;
12798 mtx_unlock(&ctl_softc->ctl_lock);
12799 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
12801 break; /* NOTREACHED */
12803 mtx_unlock(&ctl_softc->ctl_lock);
12805 ctl_wakeup_thread();
12807 return (CTL_RETVAL_COMPLETE);
12810 #ifdef CTL_IO_DELAY
12812 ctl_done_timer_wakeup(void *arg)
12816 io = (union ctl_io *)arg;
12817 ctl_done_lock(io, /*have_lock*/ 0);
12819 #endif /* CTL_IO_DELAY */
12822 ctl_done_lock(union ctl_io *io, int have_lock)
12824 struct ctl_softc *ctl_softc;
12825 #ifndef CTL_DONE_THREAD
12827 #endif /* !CTL_DONE_THREAD */
12829 ctl_softc = control_softc;
12831 if (have_lock == 0)
12832 mtx_lock(&ctl_softc->ctl_lock);
12835 * Enable this to catch duplicate completion issues.
12838 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
12839 printf("%s: type %d msg %d cdb %x iptl: "
12840 "%d:%d:%d:%d tag 0x%04x "
12841 "flag %#x status %x\n",
12843 io->io_hdr.io_type,
12844 io->io_hdr.msg_type,
12846 io->io_hdr.nexus.initid.id,
12847 io->io_hdr.nexus.targ_port,
12848 io->io_hdr.nexus.targ_target.id,
12849 io->io_hdr.nexus.targ_lun,
12850 (io->io_hdr.io_type ==
12852 io->taskio.tag_num :
12853 io->scsiio.tag_num,
12855 io->io_hdr.status);
12857 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
12861 * This is an internal copy of an I/O, and should not go through
12862 * the normal done processing logic.
12864 if (io->io_hdr.flags & CTL_FLAG_INT_COPY) {
12865 if (have_lock == 0)
12866 mtx_unlock(&ctl_softc->ctl_lock);
12871 * We need to send a msg to the serializing shelf to finish the IO
12872 * as well. We don't send a finish message to the other shelf if
12873 * this is a task management command. Task management commands
12874 * aren't serialized in the OOA queue, but rather just executed on
12875 * both shelf controllers for commands that originated on that
12878 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
12879 && (io->io_hdr.io_type != CTL_IO_TASK)) {
12880 union ctl_ha_msg msg_io;
12882 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
12883 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
12884 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
12885 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
12887 /* continue on to finish IO */
12889 #ifdef CTL_IO_DELAY
12890 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12891 struct ctl_lun *lun;
12893 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12895 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12897 struct ctl_lun *lun;
12899 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12902 && (lun->delay_info.done_delay > 0)) {
12903 struct callout *callout;
12905 callout = (struct callout *)&io->io_hdr.timer_bytes;
12906 callout_init(callout, /*mpsafe*/ 1);
12907 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12908 callout_reset(callout,
12909 lun->delay_info.done_delay * hz,
12910 ctl_done_timer_wakeup, io);
12911 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
12912 lun->delay_info.done_delay = 0;
12913 if (have_lock == 0)
12914 mtx_unlock(&ctl_softc->ctl_lock);
12918 #endif /* CTL_IO_DELAY */
12920 STAILQ_INSERT_TAIL(&ctl_softc->done_queue, &io->io_hdr, links);
12922 #ifdef CTL_DONE_THREAD
12923 if (have_lock == 0)
12924 mtx_unlock(&ctl_softc->ctl_lock);
12926 ctl_wakeup_thread();
12927 #else /* CTL_DONE_THREAD */
12928 for (xio = (union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue);
12930 xio =(union ctl_io *)STAILQ_FIRST(&ctl_softc->done_queue)) {
12932 STAILQ_REMOVE_HEAD(&ctl_softc->done_queue, links);
12934 ctl_process_done(xio, /*have_lock*/ 1);
12936 if (have_lock == 0)
12937 mtx_unlock(&ctl_softc->ctl_lock);
12938 #endif /* CTL_DONE_THREAD */
12942 ctl_done(union ctl_io *io)
12944 ctl_done_lock(io, /*have_lock*/ 0);
12948 ctl_isc(struct ctl_scsiio *ctsio)
12950 struct ctl_lun *lun;
12953 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12955 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
12957 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
12959 retval = lun->backend->data_submit((union ctl_io *)ctsio);
12966 ctl_work_thread(void *arg)
12968 struct ctl_softc *softc;
12970 struct ctl_be_lun *be_lun;
12973 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
12975 softc = (struct ctl_softc *)arg;
12979 mtx_lock(&softc->ctl_lock);
12984 * We handle the queues in this order:
12985 * - task management
12987 * - done queue (to free up resources, unblock other commands)
12991 * If those queues are empty, we break out of the loop and
12994 io = (union ctl_io *)STAILQ_FIRST(&softc->task_queue);
12996 ctl_run_task_queue(softc);
12999 io = (union ctl_io *)STAILQ_FIRST(&softc->isc_queue);
13001 STAILQ_REMOVE_HEAD(&softc->isc_queue, links);
13002 ctl_handle_isc(io);
13005 io = (union ctl_io *)STAILQ_FIRST(&softc->done_queue);
13007 STAILQ_REMOVE_HEAD(&softc->done_queue, links);
13008 /* clear any blocked commands, call fe_done */
13009 mtx_unlock(&softc->ctl_lock);
13012 * Call this without a lock for now. This will
13013 * depend on whether there is any way the FETD can
13014 * sleep or deadlock if called with the CTL lock
13017 retval = ctl_process_done(io, /*have_lock*/ 0);
13018 mtx_lock(&softc->ctl_lock);
13021 if (!ctl_pause_rtr) {
13022 io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
13024 STAILQ_REMOVE_HEAD(&softc->rtr_queue, links);
13025 mtx_unlock(&softc->ctl_lock);
13029 io = (union ctl_io *)STAILQ_FIRST(&softc->incoming_queue);
13031 STAILQ_REMOVE_HEAD(&softc->incoming_queue, links);
13032 mtx_unlock(&softc->ctl_lock);
13033 ctl_scsiio_precheck(softc, &io->scsiio);
13034 mtx_lock(&softc->ctl_lock);
13038 * We might want to move this to a separate thread, so that
13039 * configuration requests (in this case LUN creations)
13040 * won't impact the I/O path.
13042 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13043 if (be_lun != NULL) {
13044 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13045 mtx_unlock(&softc->ctl_lock);
13046 ctl_create_lun(be_lun);
13047 mtx_lock(&softc->ctl_lock);
13051 /* XXX KDM use the PDROP flag?? */
13052 /* Sleep until we have something to do. */
13053 mtx_sleep(softc, &softc->ctl_lock, PRIBIO, "ctl_work", 0);
13055 /* Back to the top of the loop to see what woke us up. */
13059 retval = ctl_scsiio(&io->scsiio);
13061 case CTL_RETVAL_COMPLETE:
13065 * Probably need to make sure this doesn't happen.
13069 mtx_lock(&softc->ctl_lock);
13074 ctl_wakeup_thread()
13076 struct ctl_softc *softc;
13078 softc = control_softc;
13083 /* Initialization and failover */
13086 ctl_init_isc_msg(void)
13088 printf("CTL: Still calling this thing\n");
13093 * Initializes component into configuration defined by bootMode
13095 * returns hasc_Status:
13097 * ERROR - fatal error
13099 static ctl_ha_comp_status
13100 ctl_isc_init(struct ctl_ha_component *c)
13102 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13109 * Starts component in state requested. If component starts successfully,
13110 * it must set its own state to the requestrd state
13111 * When requested state is HASC_STATE_HA, the component may refine it
13112 * by adding _SLAVE or _MASTER flags.
13113 * Currently allowed state transitions are:
13114 * UNKNOWN->HA - initial startup
13115 * UNKNOWN->SINGLE - initial startup when no parter detected
13116 * HA->SINGLE - failover
13117 * returns ctl_ha_comp_status:
13118 * OK - component successfully started in requested state
13119 * FAILED - could not start the requested state, failover may
13121 * ERROR - fatal error detected, no future startup possible
13123 static ctl_ha_comp_status
13124 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
13126 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
13128 printf("%s: go\n", __func__);
13130 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
13131 if (c->state == CTL_HA_STATE_UNKNOWN ) {
13133 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
13134 != CTL_HA_STATUS_SUCCESS) {
13135 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
13136 ret = CTL_HA_COMP_STATUS_ERROR;
13138 } else if (CTL_HA_STATE_IS_HA(c->state)
13139 && CTL_HA_STATE_IS_SINGLE(state)){
13140 // HA->SINGLE transition
13144 printf("ctl_isc_start:Invalid state transition %X->%X\n",
13146 ret = CTL_HA_COMP_STATUS_ERROR;
13148 if (CTL_HA_STATE_IS_SINGLE(state))
13157 * Quiesce component
13158 * The component must clear any error conditions (set status to OK) and
13159 * prepare itself to another Start call
13160 * returns ctl_ha_comp_status:
13164 static ctl_ha_comp_status
13165 ctl_isc_quiesce(struct ctl_ha_component *c)
13167 int ret = CTL_HA_COMP_STATUS_OK;
13174 struct ctl_ha_component ctl_ha_component_ctlisc =
13177 .state = CTL_HA_STATE_UNKNOWN,
13178 .init = ctl_isc_init,
13179 .start = ctl_isc_start,
13180 .quiesce = ctl_isc_quiesce