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>
64 #include <sys/endian.h>
65 #include <sys/sysctl.h>
68 #include <cam/scsi/scsi_all.h>
69 #include <cam/scsi/scsi_da.h>
70 #include <cam/ctl/ctl_io.h>
71 #include <cam/ctl/ctl.h>
72 #include <cam/ctl/ctl_frontend.h>
73 #include <cam/ctl/ctl_frontend_internal.h>
74 #include <cam/ctl/ctl_util.h>
75 #include <cam/ctl/ctl_backend.h>
76 #include <cam/ctl/ctl_ioctl.h>
77 #include <cam/ctl/ctl_ha.h>
78 #include <cam/ctl/ctl_private.h>
79 #include <cam/ctl/ctl_debug.h>
80 #include <cam/ctl/ctl_scsi_all.h>
81 #include <cam/ctl/ctl_error.h>
83 struct ctl_softc *control_softc = NULL;
86 * Size and alignment macros needed for Copan-specific HA hardware. These
87 * can go away when the HA code is re-written, and uses busdma for any
90 #define CTL_ALIGN_8B(target, source, type) \
91 if (((uint32_t)source & 0x7) != 0) \
92 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
94 target = (type)source;
96 #define CTL_SIZE_8B(target, size) \
97 if ((size & 0x7) != 0) \
98 target = size + (0x8 - (size & 0x7)); \
102 #define CTL_ALIGN_8B_MARGIN 16
105 * Template mode pages.
109 * Note that these are default values only. The actual values will be
110 * filled in when the user does a mode sense.
112 static struct copan_power_subpage power_page_default = {
113 /*page_code*/ PWR_PAGE_CODE | SMPH_SPF,
114 /*subpage*/ PWR_SUBPAGE_CODE,
115 /*page_length*/ {(sizeof(struct copan_power_subpage) - 4) & 0xff00,
116 (sizeof(struct copan_power_subpage) - 4) & 0x00ff},
117 /*page_version*/ PWR_VERSION,
119 /* max_active_luns*/ PWR_DFLT_MAX_LUNS,
120 /*reserved*/ {0, 0, 0, 0, 0, 0, 0, 0, 0,
121 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
125 static struct copan_power_subpage power_page_changeable = {
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},
132 /* max_active_luns*/ 0,
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_aps_subpage aps_page_default = {
139 APS_PAGE_CODE | SMPH_SPF, //page_code
140 APS_SUBPAGE_CODE, //subpage
141 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
142 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
143 APS_VERSION, //page_version
145 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
146 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
147 0, 0, 0, 0, 0} //reserved
150 static struct copan_aps_subpage aps_page_changeable = {
151 APS_PAGE_CODE | SMPH_SPF, //page_code
152 APS_SUBPAGE_CODE, //subpage
153 {(sizeof(struct copan_aps_subpage) - 4) & 0xff00,
154 (sizeof(struct copan_aps_subpage) - 4) & 0x00ff}, //page_length
157 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
158 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0} //reserved
162 static struct copan_debugconf_subpage debugconf_page_default = {
163 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
164 DBGCNF_SUBPAGE_CODE, /* subpage */
165 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
166 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
167 DBGCNF_VERSION, /* page_version */
168 {CTL_TIME_IO_DEFAULT_SECS>>8,
169 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
172 static struct copan_debugconf_subpage debugconf_page_changeable = {
173 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
174 DBGCNF_SUBPAGE_CODE, /* subpage */
175 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
176 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
177 0, /* page_version */
178 {0xff,0xff}, /* ctl_time_io_secs */
181 static struct scsi_format_page format_page_default = {
182 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
183 /*page_length*/sizeof(struct scsi_format_page) - 2,
184 /*tracks_per_zone*/ {0, 0},
185 /*alt_sectors_per_zone*/ {0, 0},
186 /*alt_tracks_per_zone*/ {0, 0},
187 /*alt_tracks_per_lun*/ {0, 0},
188 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
189 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
190 /*bytes_per_sector*/ {0, 0},
191 /*interleave*/ {0, 0},
192 /*track_skew*/ {0, 0},
193 /*cylinder_skew*/ {0, 0},
195 /*reserved*/ {0, 0, 0}
198 static struct scsi_format_page format_page_changeable = {
199 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
200 /*page_length*/sizeof(struct scsi_format_page) - 2,
201 /*tracks_per_zone*/ {0, 0},
202 /*alt_sectors_per_zone*/ {0, 0},
203 /*alt_tracks_per_zone*/ {0, 0},
204 /*alt_tracks_per_lun*/ {0, 0},
205 /*sectors_per_track*/ {0, 0},
206 /*bytes_per_sector*/ {0, 0},
207 /*interleave*/ {0, 0},
208 /*track_skew*/ {0, 0},
209 /*cylinder_skew*/ {0, 0},
211 /*reserved*/ {0, 0, 0}
214 static struct scsi_rigid_disk_page rigid_disk_page_default = {
215 /*page_code*/SMS_RIGID_DISK_PAGE,
216 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
217 /*cylinders*/ {0, 0, 0},
218 /*heads*/ CTL_DEFAULT_HEADS,
219 /*start_write_precomp*/ {0, 0, 0},
220 /*start_reduced_current*/ {0, 0, 0},
221 /*step_rate*/ {0, 0},
222 /*landing_zone_cylinder*/ {0, 0, 0},
223 /*rpl*/ SRDP_RPL_DISABLED,
224 /*rotational_offset*/ 0,
226 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
227 CTL_DEFAULT_ROTATION_RATE & 0xff},
231 static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
232 /*page_code*/SMS_RIGID_DISK_PAGE,
233 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
234 /*cylinders*/ {0, 0, 0},
236 /*start_write_precomp*/ {0, 0, 0},
237 /*start_reduced_current*/ {0, 0, 0},
238 /*step_rate*/ {0, 0},
239 /*landing_zone_cylinder*/ {0, 0, 0},
241 /*rotational_offset*/ 0,
243 /*rotation_rate*/ {0, 0},
247 static struct scsi_caching_page caching_page_default = {
248 /*page_code*/SMS_CACHING_PAGE,
249 /*page_length*/sizeof(struct scsi_caching_page) - 2,
250 /*flags1*/ SCP_DISC | SCP_WCE,
252 /*disable_pf_transfer_len*/ {0xff, 0xff},
253 /*min_prefetch*/ {0, 0},
254 /*max_prefetch*/ {0xff, 0xff},
255 /*max_pf_ceiling*/ {0xff, 0xff},
257 /*cache_segments*/ 0,
258 /*cache_seg_size*/ {0, 0},
260 /*non_cache_seg_size*/ {0, 0, 0}
263 static struct scsi_caching_page caching_page_changeable = {
264 /*page_code*/SMS_CACHING_PAGE,
265 /*page_length*/sizeof(struct scsi_caching_page) - 2,
268 /*disable_pf_transfer_len*/ {0, 0},
269 /*min_prefetch*/ {0, 0},
270 /*max_prefetch*/ {0, 0},
271 /*max_pf_ceiling*/ {0, 0},
273 /*cache_segments*/ 0,
274 /*cache_seg_size*/ {0, 0},
276 /*non_cache_seg_size*/ {0, 0, 0}
279 static struct scsi_control_page control_page_default = {
280 /*page_code*/SMS_CONTROL_MODE_PAGE,
281 /*page_length*/sizeof(struct scsi_control_page) - 2,
286 /*aen_holdoff_period*/{0, 0}
289 static struct scsi_control_page control_page_changeable = {
290 /*page_code*/SMS_CONTROL_MODE_PAGE,
291 /*page_length*/sizeof(struct scsi_control_page) - 2,
296 /*aen_holdoff_period*/{0, 0}
301 * XXX KDM move these into the softc.
303 static int rcv_sync_msg;
304 static int persis_offset;
305 static uint8_t ctl_pause_rtr;
306 static int ctl_is_single = 1;
307 static int index_to_aps_page;
309 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
310 static int worker_threads = -1;
311 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
312 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
313 &worker_threads, 1, "Number of worker threads");
314 static int verbose = 0;
315 TUNABLE_INT("kern.cam.ctl.verbose", &verbose);
316 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
317 &verbose, 0, "Show SCSI errors returned to initiator");
320 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
321 * SCSI Ports (0x88), Block limits (0xB0) and
322 * Logical Block Provisioning (0xB2)
324 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 6
326 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
328 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
329 static int ctl_init(void);
330 void ctl_shutdown(void);
331 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
332 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
333 static void ctl_ioctl_online(void *arg);
334 static void ctl_ioctl_offline(void *arg);
335 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
336 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
337 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
338 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
339 static int ctl_ioctl_submit_wait(union ctl_io *io);
340 static void ctl_ioctl_datamove(union ctl_io *io);
341 static void ctl_ioctl_done(union ctl_io *io);
342 static void ctl_ioctl_hard_startstop_callback(void *arg,
343 struct cfi_metatask *metatask);
344 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
345 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
346 struct ctl_ooa *ooa_hdr,
347 struct ctl_ooa_entry *kern_entries);
348 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
350 uint32_t ctl_get_resindex(struct ctl_nexus *nexus);
351 uint32_t ctl_port_idx(int port_num);
352 static uint32_t ctl_map_lun(int port_num, uint32_t lun);
353 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun);
355 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
356 uint32_t targ_target, uint32_t targ_lun,
358 static void ctl_kfree_io(union ctl_io *io);
360 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
361 struct ctl_be_lun *be_lun, struct ctl_id target_id);
362 static int ctl_free_lun(struct ctl_lun *lun);
363 static void ctl_create_lun(struct ctl_be_lun *be_lun);
365 static void ctl_failover_change_pages(struct ctl_softc *softc,
366 struct ctl_scsiio *ctsio, int master);
369 static int ctl_do_mode_select(union ctl_io *io);
370 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
371 uint64_t res_key, uint64_t sa_res_key,
372 uint8_t type, uint32_t residx,
373 struct ctl_scsiio *ctsio,
374 struct scsi_per_res_out *cdb,
375 struct scsi_per_res_out_parms* param);
376 static void ctl_pro_preempt_other(struct ctl_lun *lun,
377 union ctl_ha_msg *msg);
378 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
379 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
380 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
381 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
382 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
384 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
386 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
387 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
388 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
389 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
390 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
391 static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
392 union ctl_io *ooa_io);
393 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
394 union ctl_io *starting_io);
395 static int ctl_check_blocked(struct ctl_lun *lun);
396 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
398 const struct ctl_cmd_entry *entry,
399 struct ctl_scsiio *ctsio);
400 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
401 static void ctl_failover(void);
402 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
403 struct ctl_scsiio *ctsio);
404 static int ctl_scsiio(struct ctl_scsiio *ctsio);
406 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
407 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
408 ctl_ua_type ua_type);
409 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
410 ctl_ua_type ua_type);
411 static int ctl_abort_task(union ctl_io *io);
412 static int ctl_abort_task_set(union ctl_io *io);
413 static int ctl_i_t_nexus_reset(union ctl_io *io);
414 static void ctl_run_task(union ctl_io *io);
416 static void ctl_datamove_timer_wakeup(void *arg);
417 static void ctl_done_timer_wakeup(void *arg);
418 #endif /* CTL_IO_DELAY */
420 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
421 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
422 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
423 static void ctl_datamove_remote_write(union ctl_io *io);
424 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
425 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
426 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
427 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
428 ctl_ha_dt_cb callback);
429 static void ctl_datamove_remote_read(union ctl_io *io);
430 static void ctl_datamove_remote(union ctl_io *io);
431 static int ctl_process_done(union ctl_io *io);
432 static void ctl_lun_thread(void *arg);
433 static void ctl_work_thread(void *arg);
434 static void ctl_enqueue_incoming(union ctl_io *io);
435 static void ctl_enqueue_rtr(union ctl_io *io);
436 static void ctl_enqueue_done(union ctl_io *io);
437 static void ctl_enqueue_isc(union ctl_io *io);
438 static const struct ctl_cmd_entry *
439 ctl_get_cmd_entry(struct ctl_scsiio *ctsio);
440 static const struct ctl_cmd_entry *
441 ctl_validate_command(struct ctl_scsiio *ctsio);
442 static int ctl_cmd_applicable(uint8_t lun_type,
443 const struct ctl_cmd_entry *entry);
446 * Load the serialization table. This isn't very pretty, but is probably
447 * the easiest way to do it.
449 #include "ctl_ser_table.c"
452 * We only need to define open, close and ioctl routines for this driver.
454 static struct cdevsw ctl_cdevsw = {
455 .d_version = D_VERSION,
458 .d_close = ctl_close,
459 .d_ioctl = ctl_ioctl,
464 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
465 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
467 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
469 static moduledata_t ctl_moduledata = {
471 ctl_module_event_handler,
475 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
476 MODULE_VERSION(ctl, 1);
478 static struct ctl_frontend ioctl_frontend =
484 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
485 union ctl_ha_msg *msg_info)
487 struct ctl_scsiio *ctsio;
489 if (msg_info->hdr.original_sc == NULL) {
490 printf("%s: original_sc == NULL!\n", __func__);
491 /* XXX KDM now what? */
495 ctsio = &msg_info->hdr.original_sc->scsiio;
496 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
497 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
498 ctsio->io_hdr.status = msg_info->hdr.status;
499 ctsio->scsi_status = msg_info->scsi.scsi_status;
500 ctsio->sense_len = msg_info->scsi.sense_len;
501 ctsio->sense_residual = msg_info->scsi.sense_residual;
502 ctsio->residual = msg_info->scsi.residual;
503 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
504 sizeof(ctsio->sense_data));
505 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
506 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
507 ctl_enqueue_isc((union ctl_io *)ctsio);
511 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
512 union ctl_ha_msg *msg_info)
514 struct ctl_scsiio *ctsio;
516 if (msg_info->hdr.serializing_sc == NULL) {
517 printf("%s: serializing_sc == NULL!\n", __func__);
518 /* XXX KDM now what? */
522 ctsio = &msg_info->hdr.serializing_sc->scsiio;
525 * Attempt to catch the situation where an I/O has
526 * been freed, and we're using it again.
528 if (ctsio->io_hdr.io_type == 0xff) {
529 union ctl_io *tmp_io;
530 tmp_io = (union ctl_io *)ctsio;
531 printf("%s: %p use after free!\n", __func__,
533 printf("%s: type %d msg %d cdb %x iptl: "
534 "%d:%d:%d:%d tag 0x%04x "
535 "flag %#x status %x\n",
537 tmp_io->io_hdr.io_type,
538 tmp_io->io_hdr.msg_type,
539 tmp_io->scsiio.cdb[0],
540 tmp_io->io_hdr.nexus.initid.id,
541 tmp_io->io_hdr.nexus.targ_port,
542 tmp_io->io_hdr.nexus.targ_target.id,
543 tmp_io->io_hdr.nexus.targ_lun,
544 (tmp_io->io_hdr.io_type ==
546 tmp_io->taskio.tag_num :
547 tmp_io->scsiio.tag_num,
548 tmp_io->io_hdr.flags,
549 tmp_io->io_hdr.status);
552 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
553 ctl_enqueue_isc((union ctl_io *)ctsio);
557 * ISC (Inter Shelf Communication) event handler. Events from the HA
558 * subsystem come in here.
561 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
563 struct ctl_softc *ctl_softc;
565 struct ctl_prio *presio;
566 ctl_ha_status isc_status;
568 ctl_softc = control_softc;
573 printf("CTL: Isc Msg event %d\n", event);
575 if (event == CTL_HA_EVT_MSG_RECV) {
576 union ctl_ha_msg msg_info;
578 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
579 sizeof(msg_info), /*wait*/ 0);
581 printf("CTL: msg_type %d\n", msg_info.msg_type);
583 if (isc_status != 0) {
584 printf("Error receiving message, status = %d\n",
589 switch (msg_info.hdr.msg_type) {
590 case CTL_MSG_SERIALIZE:
592 printf("Serialize\n");
594 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
596 printf("ctl_isc_event_handler: can't allocate "
599 /* Need to set busy and send msg back */
600 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
601 msg_info.hdr.status = CTL_SCSI_ERROR;
602 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
603 msg_info.scsi.sense_len = 0;
604 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
605 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
610 // populate ctsio from msg_info
611 io->io_hdr.io_type = CTL_IO_SCSI;
612 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
613 io->io_hdr.original_sc = msg_info.hdr.original_sc;
615 printf("pOrig %x\n", (int)msg_info.original_sc);
617 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
620 * If we're in serialization-only mode, we don't
621 * want to go through full done processing. Thus
624 * XXX KDM add another flag that is more specific.
626 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
627 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
628 io->io_hdr.nexus = msg_info.hdr.nexus;
630 printf("targ %d, port %d, iid %d, lun %d\n",
631 io->io_hdr.nexus.targ_target.id,
632 io->io_hdr.nexus.targ_port,
633 io->io_hdr.nexus.initid.id,
634 io->io_hdr.nexus.targ_lun);
636 io->scsiio.tag_num = msg_info.scsi.tag_num;
637 io->scsiio.tag_type = msg_info.scsi.tag_type;
638 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
640 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
641 const struct ctl_cmd_entry *entry;
643 entry = ctl_get_cmd_entry(&io->scsiio);
644 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
646 entry->flags & CTL_FLAG_DATA_MASK;
651 /* Performed on the Originating SC, XFER mode only */
652 case CTL_MSG_DATAMOVE: {
653 struct ctl_sg_entry *sgl;
656 io = msg_info.hdr.original_sc;
658 printf("%s: original_sc == NULL!\n", __func__);
659 /* XXX KDM do something here */
662 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
663 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
665 * Keep track of this, we need to send it back over
666 * when the datamove is complete.
668 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
670 if (msg_info.dt.sg_sequence == 0) {
672 * XXX KDM we use the preallocated S/G list
673 * here, but we'll need to change this to
674 * dynamic allocation if we need larger S/G
677 if (msg_info.dt.kern_sg_entries >
678 sizeof(io->io_hdr.remote_sglist) /
679 sizeof(io->io_hdr.remote_sglist[0])) {
680 printf("%s: number of S/G entries "
681 "needed %u > allocated num %zd\n",
683 msg_info.dt.kern_sg_entries,
684 sizeof(io->io_hdr.remote_sglist)/
685 sizeof(io->io_hdr.remote_sglist[0]));
688 * XXX KDM send a message back to
689 * the other side to shut down the
690 * DMA. The error will come back
691 * through via the normal channel.
695 sgl = io->io_hdr.remote_sglist;
697 sizeof(io->io_hdr.remote_sglist));
699 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
701 io->scsiio.kern_sg_entries =
702 msg_info.dt.kern_sg_entries;
703 io->scsiio.rem_sg_entries =
704 msg_info.dt.kern_sg_entries;
705 io->scsiio.kern_data_len =
706 msg_info.dt.kern_data_len;
707 io->scsiio.kern_total_len =
708 msg_info.dt.kern_total_len;
709 io->scsiio.kern_data_resid =
710 msg_info.dt.kern_data_resid;
711 io->scsiio.kern_rel_offset =
712 msg_info.dt.kern_rel_offset;
714 * Clear out per-DMA flags.
716 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
718 * Add per-DMA flags that are set for this
719 * particular DMA request.
721 io->io_hdr.flags |= msg_info.dt.flags &
724 sgl = (struct ctl_sg_entry *)
725 io->scsiio.kern_data_ptr;
727 for (i = msg_info.dt.sent_sg_entries, j = 0;
728 i < (msg_info.dt.sent_sg_entries +
729 msg_info.dt.cur_sg_entries); i++, j++) {
730 sgl[i].addr = msg_info.dt.sg_list[j].addr;
731 sgl[i].len = msg_info.dt.sg_list[j].len;
734 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
736 msg_info.dt.sg_list[j].addr,
737 msg_info.dt.sg_list[j].len,
738 sgl[i].addr, sgl[i].len, j, i);
742 memcpy(&sgl[msg_info.dt.sent_sg_entries],
744 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
748 * If this is the last piece of the I/O, we've got
749 * the full S/G list. Queue processing in the thread.
750 * Otherwise wait for the next piece.
752 if (msg_info.dt.sg_last != 0)
756 /* Performed on the Serializing (primary) SC, XFER mode only */
757 case CTL_MSG_DATAMOVE_DONE: {
758 if (msg_info.hdr.serializing_sc == NULL) {
759 printf("%s: serializing_sc == NULL!\n",
761 /* XXX KDM now what? */
765 * We grab the sense information here in case
766 * there was a failure, so we can return status
767 * back to the initiator.
769 io = msg_info.hdr.serializing_sc;
770 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
771 io->io_hdr.status = msg_info.hdr.status;
772 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
773 io->scsiio.sense_len = msg_info.scsi.sense_len;
774 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
775 io->io_hdr.port_status = msg_info.scsi.fetd_status;
776 io->scsiio.residual = msg_info.scsi.residual;
777 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
778 sizeof(io->scsiio.sense_data));
783 /* Preformed on Originating SC, SER_ONLY mode */
785 io = msg_info.hdr.original_sc;
787 printf("%s: Major Bummer\n", __func__);
791 printf("pOrig %x\n",(int) ctsio);
794 io->io_hdr.msg_type = CTL_MSG_R2R;
795 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
800 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
802 * Performed on the Originating (i.e. secondary) SC in XFER
805 case CTL_MSG_FINISH_IO:
806 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
807 ctl_isc_handler_finish_xfer(ctl_softc,
810 ctl_isc_handler_finish_ser_only(ctl_softc,
814 /* Preformed on Originating SC */
815 case CTL_MSG_BAD_JUJU:
816 io = msg_info.hdr.original_sc;
818 printf("%s: Bad JUJU!, original_sc is NULL!\n",
822 ctl_copy_sense_data(&msg_info, io);
824 * IO should have already been cleaned up on other
825 * SC so clear this flag so we won't send a message
826 * back to finish the IO there.
828 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
829 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
831 /* io = msg_info.hdr.serializing_sc; */
832 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
836 /* Handle resets sent from the other side */
837 case CTL_MSG_MANAGE_TASKS: {
838 struct ctl_taskio *taskio;
839 taskio = (struct ctl_taskio *)ctl_alloc_io(
840 (void *)ctl_softc->othersc_pool);
841 if (taskio == NULL) {
842 printf("ctl_isc_event_handler: can't allocate "
845 /* should I just call the proper reset func
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 ctl_run_task((union ctl_io *)taskio);
866 /* Persistent Reserve action which needs attention */
867 case CTL_MSG_PERS_ACTION:
868 presio = (struct ctl_prio *)ctl_alloc_io(
869 (void *)ctl_softc->othersc_pool);
870 if (presio == NULL) {
871 printf("ctl_isc_event_handler: can't allocate "
874 /* Need to set busy and send msg back */
877 ctl_zero_io((union ctl_io *)presio);
878 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
879 presio->pr_msg = msg_info.pr;
880 ctl_enqueue_isc((union ctl_io *)presio);
882 case CTL_MSG_SYNC_FE:
885 case CTL_MSG_APS_LOCK: {
886 // It's quicker to execute this then to
889 struct ctl_page_index *page_index;
890 struct copan_aps_subpage *current_sp;
893 targ_lun = msg_info.hdr.nexus.targ_mapped_lun;
894 lun = ctl_softc->ctl_luns[targ_lun];
895 mtx_lock(&lun->lun_lock);
896 page_index = &lun->mode_pages.index[index_to_aps_page];
897 current_sp = (struct copan_aps_subpage *)
898 (page_index->page_data +
899 (page_index->page_len * CTL_PAGE_CURRENT));
901 current_sp->lock_active = msg_info.aps.lock_flag;
902 mtx_unlock(&lun->lun_lock);
906 printf("How did I get here?\n");
908 } else if (event == CTL_HA_EVT_MSG_SENT) {
909 if (param != CTL_HA_STATUS_SUCCESS) {
910 printf("Bad status from ctl_ha_msg_send status %d\n",
914 } else if (event == CTL_HA_EVT_DISCONNECT) {
915 printf("CTL: Got a disconnect from Isc\n");
918 printf("ctl_isc_event_handler: Unknown event %d\n", event);
927 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
929 struct scsi_sense_data *sense;
931 sense = &dest->scsiio.sense_data;
932 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
933 dest->scsiio.scsi_status = src->scsi.scsi_status;
934 dest->scsiio.sense_len = src->scsi.sense_len;
935 dest->io_hdr.status = src->hdr.status;
941 struct ctl_softc *softc;
942 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
943 struct ctl_port *port;
945 int i, error, retval;
952 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
954 softc = control_softc;
956 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
959 softc->dev->si_drv1 = softc;
962 * By default, return a "bad LUN" peripheral qualifier for unknown
963 * LUNs. The user can override this default using the tunable or
964 * sysctl. See the comment in ctl_inquiry_std() for more details.
966 softc->inquiry_pq_no_lun = 1;
967 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
968 &softc->inquiry_pq_no_lun);
969 sysctl_ctx_init(&softc->sysctl_ctx);
970 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
971 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
972 CTLFLAG_RD, 0, "CAM Target Layer");
974 if (softc->sysctl_tree == NULL) {
975 printf("%s: unable to allocate sysctl tree\n", __func__);
976 destroy_dev(softc->dev);
977 free(control_softc, M_DEVBUF);
978 control_softc = NULL;
982 SYSCTL_ADD_INT(&softc->sysctl_ctx,
983 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
984 "inquiry_pq_no_lun", CTLFLAG_RW,
985 &softc->inquiry_pq_no_lun, 0,
986 "Report no lun possible for invalid LUNs");
988 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
989 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
990 softc->open_count = 0;
993 * Default to actually sending a SYNCHRONIZE CACHE command down to
996 softc->flags = CTL_FLAG_REAL_SYNC;
999 * In Copan's HA scheme, the "master" and "slave" roles are
1000 * figured out through the slot the controller is in. Although it
1001 * is an active/active system, someone has to be in charge.
1004 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1008 softc->flags |= CTL_FLAG_MASTER_SHELF;
1011 persis_offset = CTL_MAX_INITIATORS;
1014 * XXX KDM need to figure out where we want to get our target ID
1015 * and WWID. Is it different on each port?
1017 softc->target.id = 0;
1018 softc->target.wwid[0] = 0x12345678;
1019 softc->target.wwid[1] = 0x87654321;
1020 STAILQ_INIT(&softc->lun_list);
1021 STAILQ_INIT(&softc->pending_lun_queue);
1022 STAILQ_INIT(&softc->fe_list);
1023 STAILQ_INIT(&softc->port_list);
1024 STAILQ_INIT(&softc->be_list);
1025 STAILQ_INIT(&softc->io_pools);
1027 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1028 &internal_pool)!= 0){
1029 printf("ctl: can't allocate %d entry internal pool, "
1030 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1034 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1035 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1036 printf("ctl: can't allocate %d entry emergency pool, "
1037 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1038 ctl_pool_free(internal_pool);
1042 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1045 printf("ctl: can't allocate %d entry other SC pool, "
1046 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1047 ctl_pool_free(internal_pool);
1048 ctl_pool_free(emergency_pool);
1052 softc->internal_pool = internal_pool;
1053 softc->emergency_pool = emergency_pool;
1054 softc->othersc_pool = other_pool;
1056 if (worker_threads <= 0)
1057 worker_threads = max(1, mp_ncpus / 4);
1058 if (worker_threads > CTL_MAX_THREADS)
1059 worker_threads = CTL_MAX_THREADS;
1061 for (i = 0; i < worker_threads; i++) {
1062 struct ctl_thread *thr = &softc->threads[i];
1064 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1065 thr->ctl_softc = softc;
1066 STAILQ_INIT(&thr->incoming_queue);
1067 STAILQ_INIT(&thr->rtr_queue);
1068 STAILQ_INIT(&thr->done_queue);
1069 STAILQ_INIT(&thr->isc_queue);
1071 error = kproc_kthread_add(ctl_work_thread, thr,
1072 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1074 printf("error creating CTL work thread!\n");
1075 ctl_pool_free(internal_pool);
1076 ctl_pool_free(emergency_pool);
1077 ctl_pool_free(other_pool);
1081 error = kproc_kthread_add(ctl_lun_thread, softc,
1082 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1084 printf("error creating CTL lun thread!\n");
1085 ctl_pool_free(internal_pool);
1086 ctl_pool_free(emergency_pool);
1087 ctl_pool_free(other_pool);
1091 printf("ctl: CAM Target Layer loaded\n");
1094 * Initialize the ioctl front end.
1096 ctl_frontend_register(&ioctl_frontend);
1097 port = &softc->ioctl_info.port;
1098 port->frontend = &ioctl_frontend;
1099 sprintf(softc->ioctl_info.port_name, "ioctl");
1100 port->port_type = CTL_PORT_IOCTL;
1101 port->num_requested_ctl_io = 100;
1102 port->port_name = softc->ioctl_info.port_name;
1103 port->port_online = ctl_ioctl_online;
1104 port->port_offline = ctl_ioctl_offline;
1105 port->onoff_arg = &softc->ioctl_info;
1106 port->lun_enable = ctl_ioctl_lun_enable;
1107 port->lun_disable = ctl_ioctl_lun_disable;
1108 port->targ_lun_arg = &softc->ioctl_info;
1109 port->fe_datamove = ctl_ioctl_datamove;
1110 port->fe_done = ctl_ioctl_done;
1111 port->max_targets = 15;
1112 port->max_target_id = 15;
1114 if (ctl_port_register(&softc->ioctl_info.port,
1115 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1116 printf("ctl: ioctl front end registration failed, will "
1117 "continue anyway\n");
1121 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1122 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1123 sizeof(struct callout), CTL_TIMER_BYTES);
1126 #endif /* CTL_IO_DELAY */
1134 struct ctl_softc *softc;
1135 struct ctl_lun *lun, *next_lun;
1136 struct ctl_io_pool *pool;
1138 softc = (struct ctl_softc *)control_softc;
1140 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1141 printf("ctl: ioctl front end deregistration failed\n");
1143 mtx_lock(&softc->ctl_lock);
1148 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1149 next_lun = STAILQ_NEXT(lun, links);
1153 mtx_unlock(&softc->ctl_lock);
1155 ctl_frontend_deregister(&ioctl_frontend);
1158 * This will rip the rug out from under any FETDs or anyone else
1159 * that has a pool allocated. Since we increment our module
1160 * refcount any time someone outside the main CTL module allocates
1161 * a pool, we shouldn't have any problems here. The user won't be
1162 * able to unload the CTL module until client modules have
1163 * successfully unloaded.
1165 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1166 ctl_pool_free(pool);
1169 ctl_shutdown_thread(softc->work_thread);
1170 mtx_destroy(&softc->queue_lock);
1173 mtx_destroy(&softc->pool_lock);
1174 mtx_destroy(&softc->ctl_lock);
1176 destroy_dev(softc->dev);
1178 sysctl_ctx_free(&softc->sysctl_ctx);
1180 free(control_softc, M_DEVBUF);
1181 control_softc = NULL;
1184 printf("ctl: CAM Target Layer unloaded\n");
1188 ctl_module_event_handler(module_t mod, int what, void *arg)
1193 return (ctl_init());
1197 return (EOPNOTSUPP);
1202 * XXX KDM should we do some access checks here? Bump a reference count to
1203 * prevent a CTL module from being unloaded while someone has it open?
1206 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1212 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1218 ctl_port_enable(ctl_port_type port_type)
1220 struct ctl_softc *softc;
1221 struct ctl_port *port;
1223 if (ctl_is_single == 0) {
1224 union ctl_ha_msg msg_info;
1228 printf("%s: HA mode, synchronizing frontend enable\n",
1231 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1232 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1233 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1234 printf("Sync msg send error retval %d\n", isc_retval);
1236 if (!rcv_sync_msg) {
1237 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1238 sizeof(msg_info), 1);
1241 printf("CTL:Frontend Enable\n");
1243 printf("%s: single mode, skipping frontend synchronization\n",
1248 softc = control_softc;
1250 STAILQ_FOREACH(port, &softc->port_list, links) {
1251 if (port_type & port->port_type)
1254 printf("port %d\n", port->targ_port);
1256 ctl_port_online(port);
1264 ctl_port_disable(ctl_port_type port_type)
1266 struct ctl_softc *softc;
1267 struct ctl_port *port;
1269 softc = control_softc;
1271 STAILQ_FOREACH(port, &softc->port_list, links) {
1272 if (port_type & port->port_type)
1273 ctl_port_offline(port);
1280 * Returns 0 for success, 1 for failure.
1281 * Currently the only failure mode is if there aren't enough entries
1282 * allocated. So, in case of a failure, look at num_entries_dropped,
1283 * reallocate and try again.
1286 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1287 int *num_entries_filled, int *num_entries_dropped,
1288 ctl_port_type port_type, int no_virtual)
1290 struct ctl_softc *softc;
1291 struct ctl_port *port;
1292 int entries_dropped, entries_filled;
1296 softc = control_softc;
1300 entries_dropped = 0;
1303 mtx_lock(&softc->ctl_lock);
1304 STAILQ_FOREACH(port, &softc->port_list, links) {
1305 struct ctl_port_entry *entry;
1307 if ((port->port_type & port_type) == 0)
1310 if ((no_virtual != 0)
1311 && (port->virtual_port != 0))
1314 if (entries_filled >= num_entries_alloced) {
1318 entry = &entries[i];
1320 entry->port_type = port->port_type;
1321 strlcpy(entry->port_name, port->port_name,
1322 sizeof(entry->port_name));
1323 entry->physical_port = port->physical_port;
1324 entry->virtual_port = port->virtual_port;
1325 entry->wwnn = port->wwnn;
1326 entry->wwpn = port->wwpn;
1332 mtx_unlock(&softc->ctl_lock);
1334 if (entries_dropped > 0)
1337 *num_entries_dropped = entries_dropped;
1338 *num_entries_filled = entries_filled;
1344 ctl_ioctl_online(void *arg)
1346 struct ctl_ioctl_info *ioctl_info;
1348 ioctl_info = (struct ctl_ioctl_info *)arg;
1350 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1354 ctl_ioctl_offline(void *arg)
1356 struct ctl_ioctl_info *ioctl_info;
1358 ioctl_info = (struct ctl_ioctl_info *)arg;
1360 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1364 * Remove an initiator by port number and initiator ID.
1365 * Returns 0 for success, -1 for failure.
1368 ctl_remove_initiator(struct ctl_port *port, int iid)
1370 struct ctl_softc *softc = control_softc;
1372 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1374 if (iid > CTL_MAX_INIT_PER_PORT) {
1375 printf("%s: initiator ID %u > maximun %u!\n",
1376 __func__, iid, CTL_MAX_INIT_PER_PORT);
1380 mtx_lock(&softc->ctl_lock);
1381 port->wwpn_iid[iid].in_use--;
1382 port->wwpn_iid[iid].last_use = time_uptime;
1383 mtx_unlock(&softc->ctl_lock);
1389 * Add an initiator to the initiator map.
1390 * Returns iid for success, < 0 for failure.
1393 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1395 struct ctl_softc *softc = control_softc;
1399 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1401 if (iid >= CTL_MAX_INIT_PER_PORT) {
1402 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1403 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1408 mtx_lock(&softc->ctl_lock);
1410 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1411 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1412 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1416 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1417 strcmp(name, port->wwpn_iid[i].name) == 0) {
1425 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1426 if (port->wwpn_iid[i].in_use == 0 &&
1427 port->wwpn_iid[i].wwpn == 0 &&
1428 port->wwpn_iid[i].name == NULL) {
1437 best_time = INT32_MAX;
1438 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1439 if (port->wwpn_iid[i].in_use == 0) {
1440 if (port->wwpn_iid[i].last_use < best_time) {
1442 best_time = port->wwpn_iid[i].last_use;
1450 mtx_unlock(&softc->ctl_lock);
1455 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1457 * This is not an error yet.
1459 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1461 printf("%s: port %d iid %u WWPN %#jx arrived"
1462 " again\n", __func__, port->targ_port,
1463 iid, (uintmax_t)wwpn);
1467 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1468 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1470 printf("%s: port %d iid %u name '%s' arrived"
1471 " again\n", __func__, port->targ_port,
1478 * This is an error, but what do we do about it? The
1479 * driver is telling us we have a new WWPN for this
1480 * initiator ID, so we pretty much need to use it.
1482 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1483 " but WWPN %#jx '%s' is still at that address\n",
1484 __func__, port->targ_port, iid, wwpn, name,
1485 (uintmax_t)port->wwpn_iid[iid].wwpn,
1486 port->wwpn_iid[iid].name);
1489 * XXX KDM clear have_ca and ua_pending on each LUN for
1494 free(port->wwpn_iid[iid].name, M_CTL);
1495 port->wwpn_iid[iid].name = name;
1496 port->wwpn_iid[iid].wwpn = wwpn;
1497 port->wwpn_iid[iid].in_use++;
1498 mtx_unlock(&softc->ctl_lock);
1504 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1508 switch (port->port_type) {
1511 struct scsi_transportid_fcp *id =
1512 (struct scsi_transportid_fcp *)buf;
1513 if (port->wwpn_iid[iid].wwpn == 0)
1515 memset(id, 0, sizeof(*id));
1516 id->format_protocol = SCSI_PROTO_FC;
1517 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1518 return (sizeof(*id));
1520 case CTL_PORT_ISCSI:
1522 struct scsi_transportid_iscsi_port *id =
1523 (struct scsi_transportid_iscsi_port *)buf;
1524 if (port->wwpn_iid[iid].name == NULL)
1527 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1529 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1530 len = roundup2(min(len, 252), 4);
1531 scsi_ulto2b(len, id->additional_length);
1532 return (sizeof(*id) + len);
1536 struct scsi_transportid_sas *id =
1537 (struct scsi_transportid_sas *)buf;
1538 if (port->wwpn_iid[iid].wwpn == 0)
1540 memset(id, 0, sizeof(*id));
1541 id->format_protocol = SCSI_PROTO_SAS;
1542 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1543 return (sizeof(*id));
1547 struct scsi_transportid_spi *id =
1548 (struct scsi_transportid_spi *)buf;
1549 memset(id, 0, sizeof(*id));
1550 id->format_protocol = SCSI_PROTO_SPI;
1551 scsi_ulto2b(iid, id->scsi_addr);
1552 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1553 return (sizeof(*id));
1559 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1565 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1571 * Data movement routine for the CTL ioctl frontend port.
1574 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1576 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1577 struct ctl_sg_entry ext_entry, kern_entry;
1578 int ext_sglen, ext_sg_entries, kern_sg_entries;
1579 int ext_sg_start, ext_offset;
1580 int len_to_copy, len_copied;
1581 int kern_watermark, ext_watermark;
1582 int ext_sglist_malloced;
1585 ext_sglist_malloced = 0;
1589 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1592 * If this flag is set, fake the data transfer.
1594 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1595 ctsio->ext_data_filled = ctsio->ext_data_len;
1600 * To simplify things here, if we have a single buffer, stick it in
1601 * a S/G entry and just make it a single entry S/G list.
1603 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1606 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1608 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1610 ext_sglist_malloced = 1;
1611 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1613 ctl_set_internal_failure(ctsio,
1618 ext_sg_entries = ctsio->ext_sg_entries;
1620 for (i = 0; i < ext_sg_entries; i++) {
1621 if ((len_seen + ext_sglist[i].len) >=
1622 ctsio->ext_data_filled) {
1624 ext_offset = ctsio->ext_data_filled - len_seen;
1627 len_seen += ext_sglist[i].len;
1630 ext_sglist = &ext_entry;
1631 ext_sglist->addr = ctsio->ext_data_ptr;
1632 ext_sglist->len = ctsio->ext_data_len;
1635 ext_offset = ctsio->ext_data_filled;
1638 if (ctsio->kern_sg_entries > 0) {
1639 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1640 kern_sg_entries = ctsio->kern_sg_entries;
1642 kern_sglist = &kern_entry;
1643 kern_sglist->addr = ctsio->kern_data_ptr;
1644 kern_sglist->len = ctsio->kern_data_len;
1645 kern_sg_entries = 1;
1650 ext_watermark = ext_offset;
1652 for (i = ext_sg_start, j = 0;
1653 i < ext_sg_entries && j < kern_sg_entries;) {
1654 uint8_t *ext_ptr, *kern_ptr;
1656 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1657 kern_sglist[j].len - kern_watermark);
1659 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1660 ext_ptr = ext_ptr + ext_watermark;
1661 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1665 panic("need to implement bus address support");
1667 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1670 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1671 kern_ptr = kern_ptr + kern_watermark;
1673 kern_watermark += len_to_copy;
1674 ext_watermark += len_to_copy;
1676 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1678 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1679 "bytes to user\n", len_to_copy));
1680 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1681 "to %p\n", kern_ptr, ext_ptr));
1682 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1683 ctl_set_internal_failure(ctsio,
1689 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1690 "bytes from user\n", len_to_copy));
1691 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1692 "to %p\n", ext_ptr, kern_ptr));
1693 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1694 ctl_set_internal_failure(ctsio,
1701 len_copied += len_to_copy;
1703 if (ext_sglist[i].len == ext_watermark) {
1708 if (kern_sglist[j].len == kern_watermark) {
1714 ctsio->ext_data_filled += len_copied;
1716 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1717 "kern_sg_entries: %d\n", ext_sg_entries,
1719 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1720 "kern_data_len = %d\n", ctsio->ext_data_len,
1721 ctsio->kern_data_len));
1724 /* XXX KDM set residual?? */
1727 if (ext_sglist_malloced != 0)
1728 free(ext_sglist, M_CTL);
1730 return (CTL_RETVAL_COMPLETE);
1734 * Serialize a command that went down the "wrong" side, and so was sent to
1735 * this controller for execution. The logic is a little different than the
1736 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1737 * sent back to the other side, but in the success case, we execute the
1738 * command on this side (XFER mode) or tell the other side to execute it
1742 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1744 struct ctl_softc *ctl_softc;
1745 union ctl_ha_msg msg_info;
1746 struct ctl_lun *lun;
1750 ctl_softc = control_softc;
1752 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1753 lun = ctl_softc->ctl_luns[targ_lun];
1757 * Why isn't LUN defined? The other side wouldn't
1758 * send a cmd if the LUN is undefined.
1760 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1762 /* "Logical unit not supported" */
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;
1778 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1779 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1785 mtx_lock(&lun->lun_lock);
1786 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1788 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1789 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1791 case CTL_ACTION_BLOCK:
1792 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1793 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1796 case CTL_ACTION_PASS:
1797 case CTL_ACTION_SKIP:
1798 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1799 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1800 ctl_enqueue_rtr((union ctl_io *)ctsio);
1803 /* send msg back to other side */
1804 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1805 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1806 msg_info.hdr.msg_type = CTL_MSG_R2R;
1808 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1810 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1811 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1815 case CTL_ACTION_OVERLAP:
1816 /* OVERLAPPED COMMANDS ATTEMPTED */
1817 ctl_set_sense_data(&msg_info.scsi.sense_data,
1819 /*sense_format*/SSD_TYPE_NONE,
1820 /*current_error*/ 1,
1821 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1826 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1827 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1828 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1829 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1830 msg_info.hdr.serializing_sc = NULL;
1831 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1833 printf("BAD JUJU:Major Bummer Overlap\n");
1835 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1837 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1838 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1841 case CTL_ACTION_OVERLAP_TAG:
1842 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1843 ctl_set_sense_data(&msg_info.scsi.sense_data,
1845 /*sense_format*/SSD_TYPE_NONE,
1846 /*current_error*/ 1,
1847 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1849 /*ascq*/ ctsio->tag_num & 0xff,
1852 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1853 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1854 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1855 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1856 msg_info.hdr.serializing_sc = NULL;
1857 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1859 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1861 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1863 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1864 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1867 case CTL_ACTION_ERROR:
1869 /* "Internal target failure" */
1870 ctl_set_sense_data(&msg_info.scsi.sense_data,
1872 /*sense_format*/SSD_TYPE_NONE,
1873 /*current_error*/ 1,
1874 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1879 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1880 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1881 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1882 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1883 msg_info.hdr.serializing_sc = NULL;
1884 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1886 printf("BAD JUJU:Major Bummer HW Error\n");
1888 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1890 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1891 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1895 mtx_unlock(&lun->lun_lock);
1900 ctl_ioctl_submit_wait(union ctl_io *io)
1902 struct ctl_fe_ioctl_params params;
1903 ctl_fe_ioctl_state last_state;
1908 bzero(¶ms, sizeof(params));
1910 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1911 cv_init(¶ms.sem, "ctlioccv");
1912 params.state = CTL_IOCTL_INPROG;
1913 last_state = params.state;
1915 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1917 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1919 /* This shouldn't happen */
1920 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1926 mtx_lock(¶ms.ioctl_mtx);
1928 * Check the state here, and don't sleep if the state has
1929 * already changed (i.e. wakeup has already occured, but we
1930 * weren't waiting yet).
1932 if (params.state == last_state) {
1933 /* XXX KDM cv_wait_sig instead? */
1934 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1936 last_state = params.state;
1938 switch (params.state) {
1939 case CTL_IOCTL_INPROG:
1940 /* Why did we wake up? */
1941 /* XXX KDM error here? */
1942 mtx_unlock(¶ms.ioctl_mtx);
1944 case CTL_IOCTL_DATAMOVE:
1945 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1948 * change last_state back to INPROG to avoid
1949 * deadlock on subsequent data moves.
1951 params.state = last_state = CTL_IOCTL_INPROG;
1953 mtx_unlock(¶ms.ioctl_mtx);
1954 ctl_ioctl_do_datamove(&io->scsiio);
1956 * Note that in some cases, most notably writes,
1957 * this will queue the I/O and call us back later.
1958 * In other cases, generally reads, this routine
1959 * will immediately call back and wake us up,
1960 * probably using our own context.
1962 io->scsiio.be_move_done(io);
1964 case CTL_IOCTL_DONE:
1965 mtx_unlock(¶ms.ioctl_mtx);
1966 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1970 mtx_unlock(¶ms.ioctl_mtx);
1971 /* XXX KDM error here? */
1974 } while (done == 0);
1976 mtx_destroy(¶ms.ioctl_mtx);
1977 cv_destroy(¶ms.sem);
1979 return (CTL_RETVAL_COMPLETE);
1983 ctl_ioctl_datamove(union ctl_io *io)
1985 struct ctl_fe_ioctl_params *params;
1987 params = (struct ctl_fe_ioctl_params *)
1988 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1990 mtx_lock(¶ms->ioctl_mtx);
1991 params->state = CTL_IOCTL_DATAMOVE;
1992 cv_broadcast(¶ms->sem);
1993 mtx_unlock(¶ms->ioctl_mtx);
1997 ctl_ioctl_done(union ctl_io *io)
1999 struct ctl_fe_ioctl_params *params;
2001 params = (struct ctl_fe_ioctl_params *)
2002 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2004 mtx_lock(¶ms->ioctl_mtx);
2005 params->state = CTL_IOCTL_DONE;
2006 cv_broadcast(¶ms->sem);
2007 mtx_unlock(¶ms->ioctl_mtx);
2011 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2013 struct ctl_fe_ioctl_startstop_info *sd_info;
2015 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2017 sd_info->hs_info.status = metatask->status;
2018 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2019 sd_info->hs_info.luns_complete =
2020 metatask->taskinfo.startstop.luns_complete;
2021 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2023 cv_broadcast(&sd_info->sem);
2027 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2029 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2031 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2033 mtx_lock(fe_bbr_info->lock);
2034 fe_bbr_info->bbr_info->status = metatask->status;
2035 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2036 fe_bbr_info->wakeup_done = 1;
2037 mtx_unlock(fe_bbr_info->lock);
2039 cv_broadcast(&fe_bbr_info->sem);
2043 * Returns 0 for success, errno for failure.
2046 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2047 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2054 mtx_lock(&lun->lun_lock);
2055 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2056 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2058 struct ctl_ooa_entry *entry;
2061 * If we've got more than we can fit, just count the
2062 * remaining entries.
2064 if (*cur_fill_num >= ooa_hdr->alloc_num)
2067 entry = &kern_entries[*cur_fill_num];
2069 entry->tag_num = io->scsiio.tag_num;
2070 entry->lun_num = lun->lun;
2072 entry->start_bt = io->io_hdr.start_bt;
2074 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2075 entry->cdb_len = io->scsiio.cdb_len;
2076 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2077 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2079 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2080 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2082 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2083 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2085 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2086 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2088 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2089 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2091 mtx_unlock(&lun->lun_lock);
2097 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2098 size_t error_str_len)
2102 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2104 if (copyin(user_addr, kptr, len) != 0) {
2105 snprintf(error_str, error_str_len, "Error copying %d bytes "
2106 "from user address %p to kernel address %p", len,
2116 ctl_free_args(int num_args, struct ctl_be_arg *args)
2123 for (i = 0; i < num_args; i++) {
2124 free(args[i].kname, M_CTL);
2125 free(args[i].kvalue, M_CTL);
2131 static struct ctl_be_arg *
2132 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2133 char *error_str, size_t error_str_len)
2135 struct ctl_be_arg *args;
2138 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2139 error_str, error_str_len);
2144 for (i = 0; i < num_args; i++) {
2145 args[i].kname = NULL;
2146 args[i].kvalue = NULL;
2149 for (i = 0; i < num_args; i++) {
2152 args[i].kname = ctl_copyin_alloc(args[i].name,
2153 args[i].namelen, error_str, error_str_len);
2154 if (args[i].kname == NULL)
2157 if (args[i].kname[args[i].namelen - 1] != '\0') {
2158 snprintf(error_str, error_str_len, "Argument %d "
2159 "name is not NUL-terminated", i);
2163 if (args[i].flags & CTL_BEARG_RD) {
2164 tmpptr = ctl_copyin_alloc(args[i].value,
2165 args[i].vallen, error_str, error_str_len);
2168 if ((args[i].flags & CTL_BEARG_ASCII)
2169 && (tmpptr[args[i].vallen - 1] != '\0')) {
2170 snprintf(error_str, error_str_len, "Argument "
2171 "%d value is not NUL-terminated", i);
2174 args[i].kvalue = tmpptr;
2176 args[i].kvalue = malloc(args[i].vallen,
2177 M_CTL, M_WAITOK | M_ZERO);
2184 ctl_free_args(num_args, args);
2190 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2194 for (i = 0; i < num_args; i++) {
2195 if (args[i].flags & CTL_BEARG_WR)
2196 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2201 * Escape characters that are illegal or not recommended in XML.
2204 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2210 for (; *str; str++) {
2213 retval = sbuf_printf(sb, "&");
2216 retval = sbuf_printf(sb, ">");
2219 retval = sbuf_printf(sb, "<");
2222 retval = sbuf_putc(sb, *str);
2235 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2238 struct ctl_softc *softc;
2241 softc = control_softc;
2251 * If we haven't been "enabled", don't allow any SCSI I/O
2254 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2259 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2261 printf("ctl_ioctl: can't allocate ctl_io!\n");
2267 * Need to save the pool reference so it doesn't get
2268 * spammed by the user's ctl_io.
2270 pool_tmp = io->io_hdr.pool;
2272 memcpy(io, (void *)addr, sizeof(*io));
2274 io->io_hdr.pool = pool_tmp;
2276 * No status yet, so make sure the status is set properly.
2278 io->io_hdr.status = CTL_STATUS_NONE;
2281 * The user sets the initiator ID, target and LUN IDs.
2283 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2284 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2285 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2286 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2287 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2289 retval = ctl_ioctl_submit_wait(io);
2296 memcpy((void *)addr, io, sizeof(*io));
2298 /* return this to our pool */
2303 case CTL_ENABLE_PORT:
2304 case CTL_DISABLE_PORT:
2305 case CTL_SET_PORT_WWNS: {
2306 struct ctl_port *port;
2307 struct ctl_port_entry *entry;
2309 entry = (struct ctl_port_entry *)addr;
2311 mtx_lock(&softc->ctl_lock);
2312 STAILQ_FOREACH(port, &softc->port_list, links) {
2318 if ((entry->port_type == CTL_PORT_NONE)
2319 && (entry->targ_port == port->targ_port)) {
2321 * If the user only wants to enable or
2322 * disable or set WWNs on a specific port,
2323 * do the operation and we're done.
2327 } else if (entry->port_type & port->port_type) {
2329 * Compare the user's type mask with the
2330 * particular frontend type to see if we
2337 * Make sure the user isn't trying to set
2338 * WWNs on multiple ports at the same time.
2340 if (cmd == CTL_SET_PORT_WWNS) {
2341 printf("%s: Can't set WWNs on "
2342 "multiple ports\n", __func__);
2349 * XXX KDM we have to drop the lock here,
2350 * because the online/offline operations
2351 * can potentially block. We need to
2352 * reference count the frontends so they
2355 mtx_unlock(&softc->ctl_lock);
2357 if (cmd == CTL_ENABLE_PORT) {
2358 struct ctl_lun *lun;
2360 STAILQ_FOREACH(lun, &softc->lun_list,
2362 port->lun_enable(port->targ_lun_arg,
2367 ctl_port_online(port);
2368 } else if (cmd == CTL_DISABLE_PORT) {
2369 struct ctl_lun *lun;
2371 ctl_port_offline(port);
2373 STAILQ_FOREACH(lun, &softc->lun_list,
2382 mtx_lock(&softc->ctl_lock);
2384 if (cmd == CTL_SET_PORT_WWNS)
2385 ctl_port_set_wwns(port,
2386 (entry->flags & CTL_PORT_WWNN_VALID) ?
2388 (entry->flags & CTL_PORT_WWPN_VALID) ?
2389 1 : 0, entry->wwpn);
2394 mtx_unlock(&softc->ctl_lock);
2397 case CTL_GET_PORT_LIST: {
2398 struct ctl_port *port;
2399 struct ctl_port_list *list;
2402 list = (struct ctl_port_list *)addr;
2404 if (list->alloc_len != (list->alloc_num *
2405 sizeof(struct ctl_port_entry))) {
2406 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2407 "alloc_num %u * sizeof(struct ctl_port_entry) "
2408 "%zu\n", __func__, list->alloc_len,
2409 list->alloc_num, sizeof(struct ctl_port_entry));
2415 list->dropped_num = 0;
2417 mtx_lock(&softc->ctl_lock);
2418 STAILQ_FOREACH(port, &softc->port_list, links) {
2419 struct ctl_port_entry entry, *list_entry;
2421 if (list->fill_num >= list->alloc_num) {
2422 list->dropped_num++;
2426 entry.port_type = port->port_type;
2427 strlcpy(entry.port_name, port->port_name,
2428 sizeof(entry.port_name));
2429 entry.targ_port = port->targ_port;
2430 entry.physical_port = port->physical_port;
2431 entry.virtual_port = port->virtual_port;
2432 entry.wwnn = port->wwnn;
2433 entry.wwpn = port->wwpn;
2434 if (port->status & CTL_PORT_STATUS_ONLINE)
2439 list_entry = &list->entries[i];
2441 retval = copyout(&entry, list_entry, sizeof(entry));
2443 printf("%s: CTL_GET_PORT_LIST: copyout "
2444 "returned %d\n", __func__, retval);
2449 list->fill_len += sizeof(entry);
2451 mtx_unlock(&softc->ctl_lock);
2454 * If this is non-zero, we had a copyout fault, so there's
2455 * probably no point in attempting to set the status inside
2461 if (list->dropped_num > 0)
2462 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2464 list->status = CTL_PORT_LIST_OK;
2467 case CTL_DUMP_OOA: {
2468 struct ctl_lun *lun;
2473 mtx_lock(&softc->ctl_lock);
2474 printf("Dumping OOA queues:\n");
2475 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2476 mtx_lock(&lun->lun_lock);
2477 for (io = (union ctl_io *)TAILQ_FIRST(
2478 &lun->ooa_queue); io != NULL;
2479 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2481 sbuf_new(&sb, printbuf, sizeof(printbuf),
2483 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2487 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2489 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2491 CTL_FLAG_ABORT) ? " ABORT" : "",
2493 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2494 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2496 printf("%s\n", sbuf_data(&sb));
2498 mtx_unlock(&lun->lun_lock);
2500 printf("OOA queues dump done\n");
2501 mtx_unlock(&softc->ctl_lock);
2505 struct ctl_lun *lun;
2506 struct ctl_ooa *ooa_hdr;
2507 struct ctl_ooa_entry *entries;
2508 uint32_t cur_fill_num;
2510 ooa_hdr = (struct ctl_ooa *)addr;
2512 if ((ooa_hdr->alloc_len == 0)
2513 || (ooa_hdr->alloc_num == 0)) {
2514 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2515 "must be non-zero\n", __func__,
2516 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2521 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2522 sizeof(struct ctl_ooa_entry))) {
2523 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2524 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2525 __func__, ooa_hdr->alloc_len,
2526 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2531 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2532 if (entries == NULL) {
2533 printf("%s: could not allocate %d bytes for OOA "
2534 "dump\n", __func__, ooa_hdr->alloc_len);
2539 mtx_lock(&softc->ctl_lock);
2540 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2541 && ((ooa_hdr->lun_num > CTL_MAX_LUNS)
2542 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2543 mtx_unlock(&softc->ctl_lock);
2544 free(entries, M_CTL);
2545 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2546 __func__, (uintmax_t)ooa_hdr->lun_num);
2553 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2554 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2555 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2561 mtx_unlock(&softc->ctl_lock);
2562 free(entries, M_CTL);
2566 lun = softc->ctl_luns[ooa_hdr->lun_num];
2568 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2571 mtx_unlock(&softc->ctl_lock);
2573 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2574 ooa_hdr->fill_len = ooa_hdr->fill_num *
2575 sizeof(struct ctl_ooa_entry);
2576 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2578 printf("%s: error copying out %d bytes for OOA dump\n",
2579 __func__, ooa_hdr->fill_len);
2582 getbintime(&ooa_hdr->cur_bt);
2584 if (cur_fill_num > ooa_hdr->alloc_num) {
2585 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2586 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2588 ooa_hdr->dropped_num = 0;
2589 ooa_hdr->status = CTL_OOA_OK;
2592 free(entries, M_CTL);
2595 case CTL_CHECK_OOA: {
2597 struct ctl_lun *lun;
2598 struct ctl_ooa_info *ooa_info;
2601 ooa_info = (struct ctl_ooa_info *)addr;
2603 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2604 ooa_info->status = CTL_OOA_INVALID_LUN;
2607 mtx_lock(&softc->ctl_lock);
2608 lun = softc->ctl_luns[ooa_info->lun_id];
2610 mtx_unlock(&softc->ctl_lock);
2611 ooa_info->status = CTL_OOA_INVALID_LUN;
2614 mtx_lock(&lun->lun_lock);
2615 mtx_unlock(&softc->ctl_lock);
2616 ooa_info->num_entries = 0;
2617 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2618 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2619 &io->io_hdr, ooa_links)) {
2620 ooa_info->num_entries++;
2622 mtx_unlock(&lun->lun_lock);
2624 ooa_info->status = CTL_OOA_SUCCESS;
2628 case CTL_HARD_START:
2629 case CTL_HARD_STOP: {
2630 struct ctl_fe_ioctl_startstop_info ss_info;
2631 struct cfi_metatask *metatask;
2634 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2636 cv_init(&ss_info.sem, "hard start/stop cv" );
2638 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2639 if (metatask == NULL) {
2641 mtx_destroy(&hs_mtx);
2645 if (cmd == CTL_HARD_START)
2646 metatask->tasktype = CFI_TASK_STARTUP;
2648 metatask->tasktype = CFI_TASK_SHUTDOWN;
2650 metatask->callback = ctl_ioctl_hard_startstop_callback;
2651 metatask->callback_arg = &ss_info;
2653 cfi_action(metatask);
2655 /* Wait for the callback */
2657 cv_wait_sig(&ss_info.sem, &hs_mtx);
2658 mtx_unlock(&hs_mtx);
2661 * All information has been copied from the metatask by the
2662 * time cv_broadcast() is called, so we free the metatask here.
2664 cfi_free_metatask(metatask);
2666 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2668 mtx_destroy(&hs_mtx);
2672 struct ctl_bbrread_info *bbr_info;
2673 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2675 struct cfi_metatask *metatask;
2677 bbr_info = (struct ctl_bbrread_info *)addr;
2679 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2681 bzero(&bbr_mtx, sizeof(bbr_mtx));
2682 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2684 fe_bbr_info.bbr_info = bbr_info;
2685 fe_bbr_info.lock = &bbr_mtx;
2687 cv_init(&fe_bbr_info.sem, "BBR read cv");
2688 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2690 if (metatask == NULL) {
2691 mtx_destroy(&bbr_mtx);
2692 cv_destroy(&fe_bbr_info.sem);
2696 metatask->tasktype = CFI_TASK_BBRREAD;
2697 metatask->callback = ctl_ioctl_bbrread_callback;
2698 metatask->callback_arg = &fe_bbr_info;
2699 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2700 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2701 metatask->taskinfo.bbrread.len = bbr_info->len;
2703 cfi_action(metatask);
2706 while (fe_bbr_info.wakeup_done == 0)
2707 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2708 mtx_unlock(&bbr_mtx);
2710 bbr_info->status = metatask->status;
2711 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2712 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2713 memcpy(&bbr_info->sense_data,
2714 &metatask->taskinfo.bbrread.sense_data,
2715 ctl_min(sizeof(bbr_info->sense_data),
2716 sizeof(metatask->taskinfo.bbrread.sense_data)));
2718 cfi_free_metatask(metatask);
2720 mtx_destroy(&bbr_mtx);
2721 cv_destroy(&fe_bbr_info.sem);
2725 case CTL_DELAY_IO: {
2726 struct ctl_io_delay_info *delay_info;
2728 struct ctl_lun *lun;
2729 #endif /* CTL_IO_DELAY */
2731 delay_info = (struct ctl_io_delay_info *)addr;
2734 mtx_lock(&softc->ctl_lock);
2736 if ((delay_info->lun_id > CTL_MAX_LUNS)
2737 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2738 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2740 lun = softc->ctl_luns[delay_info->lun_id];
2741 mtx_lock(&lun->lun_lock);
2743 delay_info->status = CTL_DELAY_STATUS_OK;
2745 switch (delay_info->delay_type) {
2746 case CTL_DELAY_TYPE_CONT:
2748 case CTL_DELAY_TYPE_ONESHOT:
2751 delay_info->status =
2752 CTL_DELAY_STATUS_INVALID_TYPE;
2756 switch (delay_info->delay_loc) {
2757 case CTL_DELAY_LOC_DATAMOVE:
2758 lun->delay_info.datamove_type =
2759 delay_info->delay_type;
2760 lun->delay_info.datamove_delay =
2761 delay_info->delay_secs;
2763 case CTL_DELAY_LOC_DONE:
2764 lun->delay_info.done_type =
2765 delay_info->delay_type;
2766 lun->delay_info.done_delay =
2767 delay_info->delay_secs;
2770 delay_info->status =
2771 CTL_DELAY_STATUS_INVALID_LOC;
2774 mtx_unlock(&lun->lun_lock);
2777 mtx_unlock(&softc->ctl_lock);
2779 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2780 #endif /* CTL_IO_DELAY */
2783 case CTL_REALSYNC_SET: {
2786 syncstate = (int *)addr;
2788 mtx_lock(&softc->ctl_lock);
2789 switch (*syncstate) {
2791 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2794 softc->flags |= CTL_FLAG_REAL_SYNC;
2800 mtx_unlock(&softc->ctl_lock);
2803 case CTL_REALSYNC_GET: {
2806 syncstate = (int*)addr;
2808 mtx_lock(&softc->ctl_lock);
2809 if (softc->flags & CTL_FLAG_REAL_SYNC)
2813 mtx_unlock(&softc->ctl_lock);
2819 struct ctl_sync_info *sync_info;
2820 struct ctl_lun *lun;
2822 sync_info = (struct ctl_sync_info *)addr;
2824 mtx_lock(&softc->ctl_lock);
2825 lun = softc->ctl_luns[sync_info->lun_id];
2827 mtx_unlock(&softc->ctl_lock);
2828 sync_info->status = CTL_GS_SYNC_NO_LUN;
2831 * Get or set the sync interval. We're not bounds checking
2832 * in the set case, hopefully the user won't do something
2835 mtx_lock(&lun->lun_lock);
2836 mtx_unlock(&softc->ctl_lock);
2837 if (cmd == CTL_GETSYNC)
2838 sync_info->sync_interval = lun->sync_interval;
2840 lun->sync_interval = sync_info->sync_interval;
2841 mtx_unlock(&lun->lun_lock);
2843 sync_info->status = CTL_GS_SYNC_OK;
2847 case CTL_GETSTATS: {
2848 struct ctl_stats *stats;
2849 struct ctl_lun *lun;
2852 stats = (struct ctl_stats *)addr;
2854 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2856 stats->status = CTL_SS_NEED_MORE_SPACE;
2857 stats->num_luns = softc->num_luns;
2861 * XXX KDM no locking here. If the LUN list changes,
2862 * things can blow up.
2864 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2865 i++, lun = STAILQ_NEXT(lun, links)) {
2866 retval = copyout(&lun->stats, &stats->lun_stats[i],
2867 sizeof(lun->stats));
2871 stats->num_luns = softc->num_luns;
2872 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2874 stats->status = CTL_SS_OK;
2876 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2878 stats->flags = CTL_STATS_FLAG_NONE;
2880 getnanouptime(&stats->timestamp);
2883 case CTL_ERROR_INJECT: {
2884 struct ctl_error_desc *err_desc, *new_err_desc;
2885 struct ctl_lun *lun;
2887 err_desc = (struct ctl_error_desc *)addr;
2889 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2891 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2893 mtx_lock(&softc->ctl_lock);
2894 lun = softc->ctl_luns[err_desc->lun_id];
2896 mtx_unlock(&softc->ctl_lock);
2897 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2898 __func__, (uintmax_t)err_desc->lun_id);
2902 mtx_lock(&lun->lun_lock);
2903 mtx_unlock(&softc->ctl_lock);
2906 * We could do some checking here to verify the validity
2907 * of the request, but given the complexity of error
2908 * injection requests, the checking logic would be fairly
2911 * For now, if the request is invalid, it just won't get
2912 * executed and might get deleted.
2914 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2917 * XXX KDM check to make sure the serial number is unique,
2918 * in case we somehow manage to wrap. That shouldn't
2919 * happen for a very long time, but it's the right thing to
2922 new_err_desc->serial = lun->error_serial;
2923 err_desc->serial = lun->error_serial;
2924 lun->error_serial++;
2926 mtx_unlock(&lun->lun_lock);
2929 case CTL_ERROR_INJECT_DELETE: {
2930 struct ctl_error_desc *delete_desc, *desc, *desc2;
2931 struct ctl_lun *lun;
2934 delete_desc = (struct ctl_error_desc *)addr;
2937 mtx_lock(&softc->ctl_lock);
2938 lun = softc->ctl_luns[delete_desc->lun_id];
2940 mtx_unlock(&softc->ctl_lock);
2941 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2942 __func__, (uintmax_t)delete_desc->lun_id);
2946 mtx_lock(&lun->lun_lock);
2947 mtx_unlock(&softc->ctl_lock);
2948 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2949 if (desc->serial != delete_desc->serial)
2952 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2957 mtx_unlock(&lun->lun_lock);
2958 if (delete_done == 0) {
2959 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2960 "error serial %ju on LUN %u\n", __func__,
2961 delete_desc->serial, delete_desc->lun_id);
2967 case CTL_DUMP_STRUCTS: {
2969 struct ctl_port *port;
2970 struct ctl_frontend *fe;
2972 mtx_lock(&softc->ctl_lock);
2973 printf("CTL Persistent Reservation information start:\n");
2974 for (i = 0; i < CTL_MAX_LUNS; i++) {
2975 struct ctl_lun *lun;
2977 lun = softc->ctl_luns[i];
2980 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2983 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2984 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2985 idx = j * CTL_MAX_INIT_PER_PORT + k;
2986 if (lun->per_res[idx].registered == 0)
2988 printf(" LUN %d port %d iid %d key "
2990 (uintmax_t)scsi_8btou64(
2991 lun->per_res[idx].res_key.key));
2995 printf("CTL Persistent Reservation information end\n");
2996 printf("CTL Ports:\n");
2997 STAILQ_FOREACH(port, &softc->port_list, links) {
2998 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2999 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3000 port->frontend->name, port->port_type,
3001 port->physical_port, port->virtual_port,
3002 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3003 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3004 if (port->wwpn_iid[j].in_use == 0 &&
3005 port->wwpn_iid[j].wwpn == 0 &&
3006 port->wwpn_iid[j].name == NULL)
3009 printf(" iid %u use %d WWPN %#jx '%s'\n",
3010 j, port->wwpn_iid[j].in_use,
3011 (uintmax_t)port->wwpn_iid[j].wwpn,
3012 port->wwpn_iid[j].name);
3015 printf("CTL Port information end\n");
3016 mtx_unlock(&softc->ctl_lock);
3018 * XXX KDM calling this without a lock. We'd likely want
3019 * to drop the lock before calling the frontend's dump
3022 printf("CTL Frontends:\n");
3023 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3024 printf(" Frontend '%s'\n", fe->name);
3025 if (fe->fe_dump != NULL)
3028 printf("CTL Frontend information end\n");
3032 struct ctl_lun_req *lun_req;
3033 struct ctl_backend_driver *backend;
3035 lun_req = (struct ctl_lun_req *)addr;
3037 backend = ctl_backend_find(lun_req->backend);
3038 if (backend == NULL) {
3039 lun_req->status = CTL_LUN_ERROR;
3040 snprintf(lun_req->error_str,
3041 sizeof(lun_req->error_str),
3042 "Backend \"%s\" not found.",
3046 if (lun_req->num_be_args > 0) {
3047 lun_req->kern_be_args = ctl_copyin_args(
3048 lun_req->num_be_args,
3051 sizeof(lun_req->error_str));
3052 if (lun_req->kern_be_args == NULL) {
3053 lun_req->status = CTL_LUN_ERROR;
3058 retval = backend->ioctl(dev, cmd, addr, flag, td);
3060 if (lun_req->num_be_args > 0) {
3061 ctl_copyout_args(lun_req->num_be_args,
3062 lun_req->kern_be_args);
3063 ctl_free_args(lun_req->num_be_args,
3064 lun_req->kern_be_args);
3068 case CTL_LUN_LIST: {
3070 struct ctl_lun *lun;
3071 struct ctl_lun_list *list;
3072 struct ctl_option *opt;
3074 list = (struct ctl_lun_list *)addr;
3077 * Allocate a fixed length sbuf here, based on the length
3078 * of the user's buffer. We could allocate an auto-extending
3079 * buffer, and then tell the user how much larger our
3080 * amount of data is than his buffer, but that presents
3083 * 1. The sbuf(9) routines use a blocking malloc, and so
3084 * we can't hold a lock while calling them with an
3085 * auto-extending buffer.
3087 * 2. There is not currently a LUN reference counting
3088 * mechanism, outside of outstanding transactions on
3089 * the LUN's OOA queue. So a LUN could go away on us
3090 * while we're getting the LUN number, backend-specific
3091 * information, etc. Thus, given the way things
3092 * currently work, we need to hold the CTL lock while
3093 * grabbing LUN information.
3095 * So, from the user's standpoint, the best thing to do is
3096 * allocate what he thinks is a reasonable buffer length,
3097 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3098 * double the buffer length and try again. (And repeat
3099 * that until he succeeds.)
3101 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3103 list->status = CTL_LUN_LIST_ERROR;
3104 snprintf(list->error_str, sizeof(list->error_str),
3105 "Unable to allocate %d bytes for LUN list",
3110 sbuf_printf(sb, "<ctllunlist>\n");
3112 mtx_lock(&softc->ctl_lock);
3113 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3114 mtx_lock(&lun->lun_lock);
3115 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3116 (uintmax_t)lun->lun);
3119 * Bail out as soon as we see that we've overfilled
3125 retval = sbuf_printf(sb, "\t<backend_type>%s"
3126 "</backend_type>\n",
3127 (lun->backend == NULL) ? "none" :
3128 lun->backend->name);
3133 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3134 lun->be_lun->lun_type);
3139 if (lun->backend == NULL) {
3140 retval = sbuf_printf(sb, "</lun>\n");
3146 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3147 (lun->be_lun->maxlba > 0) ?
3148 lun->be_lun->maxlba + 1 : 0);
3153 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3154 lun->be_lun->blocksize);
3159 retval = sbuf_printf(sb, "\t<serial_number>");
3164 retval = ctl_sbuf_printf_esc(sb,
3165 lun->be_lun->serial_num);
3170 retval = sbuf_printf(sb, "</serial_number>\n");
3175 retval = sbuf_printf(sb, "\t<device_id>");
3180 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3185 retval = sbuf_printf(sb, "</device_id>\n");
3190 if (lun->backend->lun_info != NULL) {
3191 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3195 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3196 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3197 opt->name, opt->value, opt->name);
3202 retval = sbuf_printf(sb, "</lun>\n");
3206 mtx_unlock(&lun->lun_lock);
3209 mtx_unlock(&lun->lun_lock);
3210 mtx_unlock(&softc->ctl_lock);
3213 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3216 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3217 snprintf(list->error_str, sizeof(list->error_str),
3218 "Out of space, %d bytes is too small",
3225 retval = copyout(sbuf_data(sb), list->lun_xml,
3228 list->fill_len = sbuf_len(sb) + 1;
3229 list->status = CTL_LUN_LIST_OK;
3234 struct ctl_iscsi *ci;
3235 struct ctl_frontend *fe;
3237 ci = (struct ctl_iscsi *)addr;
3239 fe = ctl_frontend_find("iscsi");
3241 ci->status = CTL_ISCSI_ERROR;
3242 snprintf(ci->error_str, sizeof(ci->error_str),
3243 "Frontend \"iscsi\" not found.");
3247 retval = fe->ioctl(dev, cmd, addr, flag, td);
3250 case CTL_PORT_REQ: {
3251 struct ctl_req *req;
3252 struct ctl_frontend *fe;
3254 req = (struct ctl_req *)addr;
3256 fe = ctl_frontend_find(req->driver);
3258 req->status = CTL_LUN_ERROR;
3259 snprintf(req->error_str, sizeof(req->error_str),
3260 "Frontend \"%s\" not found.", req->driver);
3263 if (req->num_args > 0) {
3264 req->kern_args = ctl_copyin_args(req->num_args,
3265 req->args, req->error_str, sizeof(req->error_str));
3266 if (req->kern_args == NULL) {
3267 req->status = CTL_LUN_ERROR;
3272 retval = fe->ioctl(dev, cmd, addr, flag, td);
3274 if (req->num_args > 0) {
3275 ctl_copyout_args(req->num_args, req->kern_args);
3276 ctl_free_args(req->num_args, req->kern_args);
3280 case CTL_PORT_LIST: {
3282 struct ctl_port *port;
3283 struct ctl_lun_list *list;
3284 struct ctl_option *opt;
3286 list = (struct ctl_lun_list *)addr;
3288 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3290 list->status = CTL_LUN_LIST_ERROR;
3291 snprintf(list->error_str, sizeof(list->error_str),
3292 "Unable to allocate %d bytes for LUN list",
3297 sbuf_printf(sb, "<ctlportlist>\n");
3299 mtx_lock(&softc->ctl_lock);
3300 STAILQ_FOREACH(port, &softc->port_list, links) {
3301 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3302 (uintmax_t)port->targ_port);
3305 * Bail out as soon as we see that we've overfilled
3311 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3312 "</frontend_type>\n", port->frontend->name);
3316 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3321 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3322 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3326 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3331 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3332 port->physical_port);
3336 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3337 port->virtual_port);
3341 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n",
3342 (uintmax_t)port->wwnn);
3346 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n",
3347 (uintmax_t)port->wwpn);
3351 if (port->port_info != NULL) {
3352 retval = port->port_info(port->onoff_arg, sb);
3356 STAILQ_FOREACH(opt, &port->options, links) {
3357 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3358 opt->name, opt->value, opt->name);
3363 retval = sbuf_printf(sb, "</targ_port>\n");
3367 mtx_unlock(&softc->ctl_lock);
3370 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3373 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3374 snprintf(list->error_str, sizeof(list->error_str),
3375 "Out of space, %d bytes is too small",
3382 retval = copyout(sbuf_data(sb), list->lun_xml,
3385 list->fill_len = sbuf_len(sb) + 1;
3386 list->status = CTL_LUN_LIST_OK;
3391 /* XXX KDM should we fix this? */
3393 struct ctl_backend_driver *backend;
3400 * We encode the backend type as the ioctl type for backend
3401 * ioctls. So parse it out here, and then search for a
3402 * backend of this type.
3404 type = _IOC_TYPE(cmd);
3406 STAILQ_FOREACH(backend, &softc->be_list, links) {
3407 if (backend->type == type) {
3413 printf("ctl: unknown ioctl command %#lx or backend "
3418 retval = backend->ioctl(dev, cmd, addr, flag, td);
3428 ctl_get_initindex(struct ctl_nexus *nexus)
3430 if (nexus->targ_port < CTL_MAX_PORTS)
3431 return (nexus->initid.id +
3432 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3434 return (nexus->initid.id +
3435 ((nexus->targ_port - CTL_MAX_PORTS) *
3436 CTL_MAX_INIT_PER_PORT));
3440 ctl_get_resindex(struct ctl_nexus *nexus)
3442 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3446 ctl_port_idx(int port_num)
3448 if (port_num < CTL_MAX_PORTS)
3451 return(port_num - CTL_MAX_PORTS);
3455 ctl_map_lun(int port_num, uint32_t lun_id)
3457 struct ctl_port *port;
3459 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3461 return (UINT32_MAX);
3462 if (port->lun_map == NULL)
3464 return (port->lun_map(port->targ_lun_arg, lun_id));
3468 ctl_map_lun_back(int port_num, uint32_t lun_id)
3470 struct ctl_port *port;
3473 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3474 if (port->lun_map == NULL)
3476 for (i = 0; i < CTL_MAX_LUNS; i++) {
3477 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3480 return (UINT32_MAX);
3484 * Note: This only works for bitmask sizes that are at least 32 bits, and
3485 * that are a power of 2.
3488 ctl_ffz(uint32_t *mask, uint32_t size)
3490 uint32_t num_chunks, num_pieces;
3493 num_chunks = (size >> 5);
3494 if (num_chunks == 0)
3496 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3498 for (i = 0; i < num_chunks; i++) {
3499 for (j = 0; j < num_pieces; j++) {
3500 if ((mask[i] & (1 << j)) == 0)
3501 return ((i << 5) + j);
3509 ctl_set_mask(uint32_t *mask, uint32_t bit)
3511 uint32_t chunk, piece;
3514 piece = bit % (sizeof(uint32_t) * 8);
3516 if ((mask[chunk] & (1 << piece)) != 0)
3519 mask[chunk] |= (1 << piece);
3525 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3527 uint32_t chunk, piece;
3530 piece = bit % (sizeof(uint32_t) * 8);
3532 if ((mask[chunk] & (1 << piece)) == 0)
3535 mask[chunk] &= ~(1 << piece);
3541 ctl_is_set(uint32_t *mask, uint32_t bit)
3543 uint32_t chunk, piece;
3546 piece = bit % (sizeof(uint32_t) * 8);
3548 if ((mask[chunk] & (1 << piece)) == 0)
3556 * The bus, target and lun are optional, they can be filled in later.
3557 * can_wait is used to determine whether we can wait on the malloc or not.
3560 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3561 uint32_t targ_lun, int can_wait)
3566 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3568 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3571 io->io_hdr.io_type = io_type;
3572 io->io_hdr.targ_port = targ_port;
3574 * XXX KDM this needs to change/go away. We need to move
3575 * to a preallocated pool of ctl_scsiio structures.
3577 io->io_hdr.nexus.targ_target.id = targ_target;
3578 io->io_hdr.nexus.targ_lun = targ_lun;
3585 ctl_kfree_io(union ctl_io *io)
3592 * ctl_softc, pool_type, total_ctl_io are passed in.
3593 * npool is passed out.
3596 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3597 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3600 union ctl_io *cur_io, *next_io;
3601 struct ctl_io_pool *pool;
3606 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3613 pool->type = pool_type;
3614 pool->ctl_softc = ctl_softc;
3616 mtx_lock(&ctl_softc->pool_lock);
3617 pool->id = ctl_softc->cur_pool_id++;
3618 mtx_unlock(&ctl_softc->pool_lock);
3620 pool->flags = CTL_POOL_FLAG_NONE;
3621 pool->refcount = 1; /* Reference for validity. */
3622 STAILQ_INIT(&pool->free_queue);
3625 * XXX KDM other options here:
3626 * - allocate a page at a time
3627 * - allocate one big chunk of memory.
3628 * Page allocation might work well, but would take a little more
3631 for (i = 0; i < total_ctl_io; i++) {
3632 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO,
3634 if (cur_io == NULL) {
3638 cur_io->io_hdr.pool = pool;
3639 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3640 pool->total_ctl_io++;
3641 pool->free_ctl_io++;
3645 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3646 cur_io != NULL; cur_io = next_io) {
3647 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3649 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3651 free(cur_io, M_CTLIO);
3657 mtx_lock(&ctl_softc->pool_lock);
3658 ctl_softc->num_pools++;
3659 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3661 * Increment our usage count if this is an external consumer, so we
3662 * can't get unloaded until the external consumer (most likely a
3663 * FETD) unloads and frees his pool.
3665 * XXX KDM will this increment the caller's module use count, or
3669 if ((pool_type != CTL_POOL_EMERGENCY)
3670 && (pool_type != CTL_POOL_INTERNAL)
3671 && (pool_type != CTL_POOL_4OTHERSC))
3675 mtx_unlock(&ctl_softc->pool_lock);
3685 ctl_pool_acquire(struct ctl_io_pool *pool)
3688 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3690 if (pool->flags & CTL_POOL_FLAG_INVALID)
3699 ctl_pool_release(struct ctl_io_pool *pool)
3701 struct ctl_softc *ctl_softc = pool->ctl_softc;
3704 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3706 if (--pool->refcount != 0)
3709 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3710 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3715 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3716 ctl_softc->num_pools--;
3719 * XXX KDM will this decrement the caller's usage count or mine?
3722 if ((pool->type != CTL_POOL_EMERGENCY)
3723 && (pool->type != CTL_POOL_INTERNAL)
3724 && (pool->type != CTL_POOL_4OTHERSC))
3732 ctl_pool_free(struct ctl_io_pool *pool)
3734 struct ctl_softc *ctl_softc;
3739 ctl_softc = pool->ctl_softc;
3740 mtx_lock(&ctl_softc->pool_lock);
3741 pool->flags |= CTL_POOL_FLAG_INVALID;
3742 ctl_pool_release(pool);
3743 mtx_unlock(&ctl_softc->pool_lock);
3747 * This routine does not block (except for spinlocks of course).
3748 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3752 ctl_alloc_io(void *pool_ref)
3755 struct ctl_softc *ctl_softc;
3756 struct ctl_io_pool *pool, *npool;
3757 struct ctl_io_pool *emergency_pool;
3759 pool = (struct ctl_io_pool *)pool_ref;
3762 printf("%s: pool is NULL\n", __func__);
3766 emergency_pool = NULL;
3768 ctl_softc = pool->ctl_softc;
3770 mtx_lock(&ctl_softc->pool_lock);
3772 * First, try to get the io structure from the user's pool.
3774 if (ctl_pool_acquire(pool) == 0) {
3775 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3777 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3778 pool->total_allocated++;
3779 pool->free_ctl_io--;
3780 mtx_unlock(&ctl_softc->pool_lock);
3783 ctl_pool_release(pool);
3786 * If he doesn't have any io structures left, search for an
3787 * emergency pool and grab one from there.
3789 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3790 if (npool->type != CTL_POOL_EMERGENCY)
3793 if (ctl_pool_acquire(npool) != 0)
3796 emergency_pool = npool;
3798 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3800 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3801 npool->total_allocated++;
3802 npool->free_ctl_io--;
3803 mtx_unlock(&ctl_softc->pool_lock);
3806 ctl_pool_release(npool);
3809 /* Drop the spinlock before we malloc */
3810 mtx_unlock(&ctl_softc->pool_lock);
3813 * The emergency pool (if it exists) didn't have one, so try an
3814 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3816 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT);
3819 * If the emergency pool exists but is empty, add this
3820 * ctl_io to its list when it gets freed.
3822 if (emergency_pool != NULL) {
3823 mtx_lock(&ctl_softc->pool_lock);
3824 if (ctl_pool_acquire(emergency_pool) == 0) {
3825 io->io_hdr.pool = emergency_pool;
3826 emergency_pool->total_ctl_io++;
3828 * Need to bump this, otherwise
3829 * total_allocated and total_freed won't
3830 * match when we no longer have anything
3833 emergency_pool->total_allocated++;
3835 mtx_unlock(&ctl_softc->pool_lock);
3837 io->io_hdr.pool = NULL;
3844 ctl_free_io(union ctl_io *io)
3850 * If this ctl_io has a pool, return it to that pool.
3852 if (io->io_hdr.pool != NULL) {
3853 struct ctl_io_pool *pool;
3855 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3856 mtx_lock(&pool->ctl_softc->pool_lock);
3857 io->io_hdr.io_type = 0xff;
3858 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3859 pool->total_freed++;
3860 pool->free_ctl_io++;
3861 ctl_pool_release(pool);
3862 mtx_unlock(&pool->ctl_softc->pool_lock);
3865 * Otherwise, just free it. We probably malloced it and
3866 * the emergency pool wasn't available.
3874 ctl_zero_io(union ctl_io *io)
3882 * May need to preserve linked list pointers at some point too.
3884 pool_ref = io->io_hdr.pool;
3886 memset(io, 0, sizeof(*io));
3888 io->io_hdr.pool = pool_ref;
3892 * This routine is currently used for internal copies of ctl_ios that need
3893 * to persist for some reason after we've already returned status to the
3894 * FETD. (Thus the flag set.)
3897 * Note that this makes a blind copy of all fields in the ctl_io, except
3898 * for the pool reference. This includes any memory that has been
3899 * allocated! That memory will no longer be valid after done has been
3900 * called, so this would be VERY DANGEROUS for command that actually does
3901 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3902 * start and stop commands, which don't transfer any data, so this is not a
3903 * problem. If it is used for anything else, the caller would also need to
3904 * allocate data buffer space and this routine would need to be modified to
3905 * copy the data buffer(s) as well.
3908 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3917 * May need to preserve linked list pointers at some point too.
3919 pool_ref = dest->io_hdr.pool;
3921 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3923 dest->io_hdr.pool = pool_ref;
3925 * We need to know that this is an internal copy, and doesn't need
3926 * to get passed back to the FETD that allocated it.
3928 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3933 ctl_update_power_subpage(struct copan_power_subpage *page)
3935 int num_luns, num_partitions, config_type;
3936 struct ctl_softc *softc;
3937 cs_BOOL_t aor_present, shelf_50pct_power;
3938 cs_raidset_personality_t rs_type;
3939 int max_active_luns;
3941 softc = control_softc;
3943 /* subtract out the processor LUN */
3944 num_luns = softc->num_luns - 1;
3946 * Default to 7 LUNs active, which was the only number we allowed
3949 max_active_luns = 7;
3951 num_partitions = config_GetRsPartitionInfo();
3952 config_type = config_GetConfigType();
3953 shelf_50pct_power = config_GetShelfPowerMode();
3954 aor_present = config_IsAorRsPresent();
3956 rs_type = ddb_GetRsRaidType(1);
3957 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3958 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3959 EPRINT(0, "Unsupported RS type %d!", rs_type);
3963 page->total_luns = num_luns;
3965 switch (config_type) {
3968 * In a 40 drive configuration, it doesn't matter what DC
3969 * cards we have, whether we have AOR enabled or not,
3970 * partitioning or not, or what type of RAIDset we have.
3971 * In that scenario, we can power up every LUN we present
3974 max_active_luns = num_luns;
3978 if (shelf_50pct_power == CS_FALSE) {
3980 if (aor_present == CS_TRUE) {
3982 CS_RAIDSET_PERSONALITY_RAID5) {
3983 max_active_luns = 7;
3984 } else if (rs_type ==
3985 CS_RAIDSET_PERSONALITY_RAID1){
3986 max_active_luns = 14;
3988 /* XXX KDM now what?? */
3992 CS_RAIDSET_PERSONALITY_RAID5) {
3993 max_active_luns = 8;
3994 } else if (rs_type ==
3995 CS_RAIDSET_PERSONALITY_RAID1){
3996 max_active_luns = 16;
3998 /* XXX KDM now what?? */
4004 * With 50% power in a 64 drive configuration, we
4005 * can power all LUNs we present.
4007 max_active_luns = num_luns;
4011 if (shelf_50pct_power == CS_FALSE) {
4013 if (aor_present == CS_TRUE) {
4015 CS_RAIDSET_PERSONALITY_RAID5) {
4016 max_active_luns = 7;
4017 } else if (rs_type ==
4018 CS_RAIDSET_PERSONALITY_RAID1){
4019 max_active_luns = 14;
4021 /* XXX KDM now what?? */
4025 CS_RAIDSET_PERSONALITY_RAID5) {
4026 max_active_luns = 8;
4027 } else if (rs_type ==
4028 CS_RAIDSET_PERSONALITY_RAID1){
4029 max_active_luns = 16;
4031 /* XXX KDM now what?? */
4036 if (aor_present == CS_TRUE) {
4038 CS_RAIDSET_PERSONALITY_RAID5) {
4039 max_active_luns = 14;
4040 } else if (rs_type ==
4041 CS_RAIDSET_PERSONALITY_RAID1){
4043 * We're assuming here that disk
4044 * caching is enabled, and so we're
4045 * able to power up half of each
4046 * LUN, and cache all writes.
4048 max_active_luns = num_luns;
4050 /* XXX KDM now what?? */
4054 CS_RAIDSET_PERSONALITY_RAID5) {
4055 max_active_luns = 15;
4056 } else if (rs_type ==
4057 CS_RAIDSET_PERSONALITY_RAID1){
4058 max_active_luns = 30;
4060 /* XXX KDM now what?? */
4067 * In this case, we have an unknown configuration, so we
4068 * just use the default from above.
4073 page->max_active_luns = max_active_luns;
4075 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
4076 page->total_luns, page->max_active_luns);
4079 #endif /* NEEDTOPORT */
4082 * This routine could be used in the future to load default and/or saved
4083 * mode page parameters for a particuar lun.
4086 ctl_init_page_index(struct ctl_lun *lun)
4089 struct ctl_page_index *page_index;
4090 struct ctl_softc *softc;
4092 memcpy(&lun->mode_pages.index, page_index_template,
4093 sizeof(page_index_template));
4095 softc = lun->ctl_softc;
4097 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4099 page_index = &lun->mode_pages.index[i];
4101 * If this is a disk-only mode page, there's no point in
4102 * setting it up. For some pages, we have to have some
4103 * basic information about the disk in order to calculate the
4106 if ((lun->be_lun->lun_type != T_DIRECT)
4107 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4110 switch (page_index->page_code & SMPH_PC_MASK) {
4111 case SMS_FORMAT_DEVICE_PAGE: {
4112 struct scsi_format_page *format_page;
4114 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4115 panic("subpage is incorrect!");
4118 * Sectors per track are set above. Bytes per
4119 * sector need to be set here on a per-LUN basis.
4121 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4122 &format_page_default,
4123 sizeof(format_page_default));
4124 memcpy(&lun->mode_pages.format_page[
4125 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4126 sizeof(format_page_changeable));
4127 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4128 &format_page_default,
4129 sizeof(format_page_default));
4130 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4131 &format_page_default,
4132 sizeof(format_page_default));
4134 format_page = &lun->mode_pages.format_page[
4136 scsi_ulto2b(lun->be_lun->blocksize,
4137 format_page->bytes_per_sector);
4139 format_page = &lun->mode_pages.format_page[
4141 scsi_ulto2b(lun->be_lun->blocksize,
4142 format_page->bytes_per_sector);
4144 format_page = &lun->mode_pages.format_page[
4146 scsi_ulto2b(lun->be_lun->blocksize,
4147 format_page->bytes_per_sector);
4149 page_index->page_data =
4150 (uint8_t *)lun->mode_pages.format_page;
4153 case SMS_RIGID_DISK_PAGE: {
4154 struct scsi_rigid_disk_page *rigid_disk_page;
4155 uint32_t sectors_per_cylinder;
4159 #endif /* !__XSCALE__ */
4161 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4162 panic("invalid subpage value %d",
4163 page_index->subpage);
4166 * Rotation rate and sectors per track are set
4167 * above. We calculate the cylinders here based on
4168 * capacity. Due to the number of heads and
4169 * sectors per track we're using, smaller arrays
4170 * may turn out to have 0 cylinders. Linux and
4171 * FreeBSD don't pay attention to these mode pages
4172 * to figure out capacity, but Solaris does. It
4173 * seems to deal with 0 cylinders just fine, and
4174 * works out a fake geometry based on the capacity.
4176 memcpy(&lun->mode_pages.rigid_disk_page[
4177 CTL_PAGE_CURRENT], &rigid_disk_page_default,
4178 sizeof(rigid_disk_page_default));
4179 memcpy(&lun->mode_pages.rigid_disk_page[
4180 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4181 sizeof(rigid_disk_page_changeable));
4182 memcpy(&lun->mode_pages.rigid_disk_page[
4183 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4184 sizeof(rigid_disk_page_default));
4185 memcpy(&lun->mode_pages.rigid_disk_page[
4186 CTL_PAGE_SAVED], &rigid_disk_page_default,
4187 sizeof(rigid_disk_page_default));
4189 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4193 * The divide method here will be more accurate,
4194 * probably, but results in floating point being
4195 * used in the kernel on i386 (__udivdi3()). On the
4196 * XScale, though, __udivdi3() is implemented in
4199 * The shift method for cylinder calculation is
4200 * accurate if sectors_per_cylinder is a power of
4201 * 2. Otherwise it might be slightly off -- you
4202 * might have a bit of a truncation problem.
4205 cylinders = (lun->be_lun->maxlba + 1) /
4206 sectors_per_cylinder;
4208 for (shift = 31; shift > 0; shift--) {
4209 if (sectors_per_cylinder & (1 << shift))
4212 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4216 * We've basically got 3 bytes, or 24 bits for the
4217 * cylinder size in the mode page. If we're over,
4218 * just round down to 2^24.
4220 if (cylinders > 0xffffff)
4221 cylinders = 0xffffff;
4223 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4225 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4227 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4229 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4231 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4233 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4235 page_index->page_data =
4236 (uint8_t *)lun->mode_pages.rigid_disk_page;
4239 case SMS_CACHING_PAGE: {
4241 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4242 panic("invalid subpage value %d",
4243 page_index->subpage);
4245 * Defaults should be okay here, no calculations
4248 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4249 &caching_page_default,
4250 sizeof(caching_page_default));
4251 memcpy(&lun->mode_pages.caching_page[
4252 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4253 sizeof(caching_page_changeable));
4254 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4255 &caching_page_default,
4256 sizeof(caching_page_default));
4257 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4258 &caching_page_default,
4259 sizeof(caching_page_default));
4260 page_index->page_data =
4261 (uint8_t *)lun->mode_pages.caching_page;
4264 case SMS_CONTROL_MODE_PAGE: {
4266 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4267 panic("invalid subpage value %d",
4268 page_index->subpage);
4271 * Defaults should be okay here, no calculations
4274 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4275 &control_page_default,
4276 sizeof(control_page_default));
4277 memcpy(&lun->mode_pages.control_page[
4278 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4279 sizeof(control_page_changeable));
4280 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4281 &control_page_default,
4282 sizeof(control_page_default));
4283 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4284 &control_page_default,
4285 sizeof(control_page_default));
4286 page_index->page_data =
4287 (uint8_t *)lun->mode_pages.control_page;
4291 case SMS_VENDOR_SPECIFIC_PAGE:{
4292 switch (page_index->subpage) {
4293 case PWR_SUBPAGE_CODE: {
4294 struct copan_power_subpage *current_page,
4297 memcpy(&lun->mode_pages.power_subpage[
4299 &power_page_default,
4300 sizeof(power_page_default));
4301 memcpy(&lun->mode_pages.power_subpage[
4302 CTL_PAGE_CHANGEABLE],
4303 &power_page_changeable,
4304 sizeof(power_page_changeable));
4305 memcpy(&lun->mode_pages.power_subpage[
4307 &power_page_default,
4308 sizeof(power_page_default));
4309 memcpy(&lun->mode_pages.power_subpage[
4311 &power_page_default,
4312 sizeof(power_page_default));
4313 page_index->page_data =
4314 (uint8_t *)lun->mode_pages.power_subpage;
4316 current_page = (struct copan_power_subpage *)
4317 (page_index->page_data +
4318 (page_index->page_len *
4320 saved_page = (struct copan_power_subpage *)
4321 (page_index->page_data +
4322 (page_index->page_len *
4326 case APS_SUBPAGE_CODE: {
4327 struct copan_aps_subpage *current_page,
4330 // This gets set multiple times but
4331 // it should always be the same. It's
4332 // only done during init so who cares.
4333 index_to_aps_page = i;
4335 memcpy(&lun->mode_pages.aps_subpage[
4338 sizeof(aps_page_default));
4339 memcpy(&lun->mode_pages.aps_subpage[
4340 CTL_PAGE_CHANGEABLE],
4341 &aps_page_changeable,
4342 sizeof(aps_page_changeable));
4343 memcpy(&lun->mode_pages.aps_subpage[
4346 sizeof(aps_page_default));
4347 memcpy(&lun->mode_pages.aps_subpage[
4350 sizeof(aps_page_default));
4351 page_index->page_data =
4352 (uint8_t *)lun->mode_pages.aps_subpage;
4354 current_page = (struct copan_aps_subpage *)
4355 (page_index->page_data +
4356 (page_index->page_len *
4358 saved_page = (struct copan_aps_subpage *)
4359 (page_index->page_data +
4360 (page_index->page_len *
4364 case DBGCNF_SUBPAGE_CODE: {
4365 struct copan_debugconf_subpage *current_page,
4368 memcpy(&lun->mode_pages.debugconf_subpage[
4370 &debugconf_page_default,
4371 sizeof(debugconf_page_default));
4372 memcpy(&lun->mode_pages.debugconf_subpage[
4373 CTL_PAGE_CHANGEABLE],
4374 &debugconf_page_changeable,
4375 sizeof(debugconf_page_changeable));
4376 memcpy(&lun->mode_pages.debugconf_subpage[
4378 &debugconf_page_default,
4379 sizeof(debugconf_page_default));
4380 memcpy(&lun->mode_pages.debugconf_subpage[
4382 &debugconf_page_default,
4383 sizeof(debugconf_page_default));
4384 page_index->page_data =
4385 (uint8_t *)lun->mode_pages.debugconf_subpage;
4387 current_page = (struct copan_debugconf_subpage *)
4388 (page_index->page_data +
4389 (page_index->page_len *
4391 saved_page = (struct copan_debugconf_subpage *)
4392 (page_index->page_data +
4393 (page_index->page_len *
4398 panic("invalid subpage value %d",
4399 page_index->subpage);
4405 panic("invalid page value %d",
4406 page_index->page_code & SMPH_PC_MASK);
4411 return (CTL_RETVAL_COMPLETE);
4418 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4419 * wants us to allocate the LUN and he can block.
4420 * - ctl_softc is always set
4421 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4423 * Returns 0 for success, non-zero (errno) for failure.
4426 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4427 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4429 struct ctl_lun *nlun, *lun;
4430 struct ctl_port *port;
4431 struct scsi_vpd_id_descriptor *desc;
4432 struct scsi_vpd_id_t10 *t10id;
4433 const char *scsiname, *vendor;
4434 int lun_number, i, lun_malloced;
4435 int devidlen, idlen1, idlen2 = 0, len;
4441 * We currently only support Direct Access or Processor LUN types.
4443 switch (be_lun->lun_type) {
4451 be_lun->lun_config_status(be_lun->be_lun,
4452 CTL_LUN_CONFIG_FAILURE);
4455 if (ctl_lun == NULL) {
4456 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4463 memset(lun, 0, sizeof(*lun));
4465 lun->flags = CTL_LUN_MALLOCED;
4467 /* Generate LUN ID. */
4468 devidlen = max(CTL_DEVID_MIN_LEN,
4469 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4470 idlen1 = sizeof(*t10id) + devidlen;
4471 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4472 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4473 if (scsiname != NULL) {
4474 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4475 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4477 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4478 M_CTL, M_WAITOK | M_ZERO);
4479 lun->lun_devid->len = len;
4480 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4481 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4482 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4483 desc->length = idlen1;
4484 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4485 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4486 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4487 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4489 strncpy(t10id->vendor, vendor,
4490 min(sizeof(t10id->vendor), strlen(vendor)));
4492 strncpy((char *)t10id->vendor_spec_id,
4493 (char *)be_lun->device_id, devidlen);
4494 if (scsiname != NULL) {
4495 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4497 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4498 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4499 SVPD_ID_TYPE_SCSI_NAME;
4500 desc->length = idlen2;
4501 strlcpy(desc->identifier, scsiname, idlen2);
4504 mtx_lock(&ctl_softc->ctl_lock);
4506 * See if the caller requested a particular LUN number. If so, see
4507 * if it is available. Otherwise, allocate the first available LUN.
4509 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4510 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4511 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4512 mtx_unlock(&ctl_softc->ctl_lock);
4513 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4514 printf("ctl: requested LUN ID %d is higher "
4515 "than CTL_MAX_LUNS - 1 (%d)\n",
4516 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4519 * XXX KDM return an error, or just assign
4520 * another LUN ID in this case??
4522 printf("ctl: requested LUN ID %d is already "
4523 "in use\n", be_lun->req_lun_id);
4525 if (lun->flags & CTL_LUN_MALLOCED)
4527 be_lun->lun_config_status(be_lun->be_lun,
4528 CTL_LUN_CONFIG_FAILURE);
4531 lun_number = be_lun->req_lun_id;
4533 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4534 if (lun_number == -1) {
4535 mtx_unlock(&ctl_softc->ctl_lock);
4536 printf("ctl: can't allocate LUN on target %ju, out of "
4537 "LUNs\n", (uintmax_t)target_id.id);
4538 if (lun->flags & CTL_LUN_MALLOCED)
4540 be_lun->lun_config_status(be_lun->be_lun,
4541 CTL_LUN_CONFIG_FAILURE);
4545 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4547 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4548 lun->target = target_id;
4549 lun->lun = lun_number;
4550 lun->be_lun = be_lun;
4552 * The processor LUN is always enabled. Disk LUNs come on line
4553 * disabled, and must be enabled by the backend.
4555 lun->flags |= CTL_LUN_DISABLED;
4556 lun->backend = be_lun->be;
4557 be_lun->ctl_lun = lun;
4558 be_lun->lun_id = lun_number;
4559 atomic_add_int(&be_lun->be->num_luns, 1);
4560 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4561 lun->flags |= CTL_LUN_STOPPED;
4563 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4564 lun->flags |= CTL_LUN_INOPERABLE;
4566 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4567 lun->flags |= CTL_LUN_PRIMARY_SC;
4569 lun->ctl_softc = ctl_softc;
4570 TAILQ_INIT(&lun->ooa_queue);
4571 TAILQ_INIT(&lun->blocked_queue);
4572 STAILQ_INIT(&lun->error_list);
4575 * Initialize the mode page index.
4577 ctl_init_page_index(lun);
4580 * Set the poweron UA for all initiators on this LUN only.
4582 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4583 lun->pending_sense[i].ua_pending = CTL_UA_POWERON;
4586 * Now, before we insert this lun on the lun list, set the lun
4587 * inventory changed UA for all other luns.
4589 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4590 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4591 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4595 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4597 ctl_softc->ctl_luns[lun_number] = lun;
4599 ctl_softc->num_luns++;
4601 /* Setup statistics gathering */
4602 lun->stats.device_type = be_lun->lun_type;
4603 lun->stats.lun_number = lun_number;
4604 if (lun->stats.device_type == T_DIRECT)
4605 lun->stats.blocksize = be_lun->blocksize;
4607 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4608 for (i = 0;i < CTL_MAX_PORTS;i++)
4609 lun->stats.ports[i].targ_port = i;
4611 mtx_unlock(&ctl_softc->ctl_lock);
4613 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4616 * Run through each registered FETD and bring it online if it isn't
4617 * already. Enable the target ID if it hasn't been enabled, and
4618 * enable this particular LUN.
4620 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4623 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4625 printf("ctl_alloc_lun: FETD %s port %d returned error "
4626 "%d for lun_enable on target %ju lun %d\n",
4627 port->port_name, port->targ_port, retval,
4628 (uintmax_t)target_id.id, lun_number);
4630 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4638 * - LUN has already been marked invalid and any pending I/O has been taken
4642 ctl_free_lun(struct ctl_lun *lun)
4644 struct ctl_softc *softc;
4646 struct ctl_port *port;
4648 struct ctl_lun *nlun;
4651 softc = lun->ctl_softc;
4653 mtx_assert(&softc->ctl_lock, MA_OWNED);
4655 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4657 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4659 softc->ctl_luns[lun->lun] = NULL;
4661 if (!TAILQ_EMPTY(&lun->ooa_queue))
4662 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4667 * XXX KDM this scheme only works for a single target/multiple LUN
4668 * setup. It needs to be revamped for a multiple target scheme.
4670 * XXX KDM this results in port->lun_disable() getting called twice,
4671 * once when ctl_disable_lun() is called, and a second time here.
4672 * We really need to re-think the LUN disable semantics. There
4673 * should probably be several steps/levels to LUN removal:
4678 * Right now we only have a disable method when communicating to
4679 * the front end ports, at least for individual LUNs.
4682 STAILQ_FOREACH(port, &softc->port_list, links) {
4685 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4688 printf("ctl_free_lun: FETD %s port %d returned error "
4689 "%d for lun_disable on target %ju lun %jd\n",
4690 port->port_name, port->targ_port, retval,
4691 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4694 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4695 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4697 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4699 printf("ctl_free_lun: FETD %s port %d "
4700 "returned error %d for targ_disable on "
4701 "target %ju\n", port->port_name,
4702 port->targ_port, retval,
4703 (uintmax_t)lun->target.id);
4705 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4707 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4711 port->port_offline(port->onoff_arg);
4712 port->status &= ~CTL_PORT_STATUS_ONLINE;
4719 * Tell the backend to free resources, if this LUN has a backend.
4721 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4722 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4724 mtx_destroy(&lun->lun_lock);
4725 free(lun->lun_devid, M_CTL);
4726 if (lun->flags & CTL_LUN_MALLOCED)
4729 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4730 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4731 nlun->pending_sense[i].ua_pending |= CTL_UA_LUN_CHANGE;
4739 ctl_create_lun(struct ctl_be_lun *be_lun)
4741 struct ctl_softc *ctl_softc;
4743 ctl_softc = control_softc;
4746 * ctl_alloc_lun() should handle all potential failure cases.
4748 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4752 ctl_add_lun(struct ctl_be_lun *be_lun)
4754 struct ctl_softc *ctl_softc = control_softc;
4756 mtx_lock(&ctl_softc->ctl_lock);
4757 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4758 mtx_unlock(&ctl_softc->ctl_lock);
4759 wakeup(&ctl_softc->pending_lun_queue);
4765 ctl_enable_lun(struct ctl_be_lun *be_lun)
4767 struct ctl_softc *ctl_softc;
4768 struct ctl_port *port, *nport;
4769 struct ctl_lun *lun;
4772 ctl_softc = control_softc;
4774 lun = (struct ctl_lun *)be_lun->ctl_lun;
4776 mtx_lock(&ctl_softc->ctl_lock);
4777 mtx_lock(&lun->lun_lock);
4778 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4780 * eh? Why did we get called if the LUN is already
4783 mtx_unlock(&lun->lun_lock);
4784 mtx_unlock(&ctl_softc->ctl_lock);
4787 lun->flags &= ~CTL_LUN_DISABLED;
4788 mtx_unlock(&lun->lun_lock);
4790 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4791 nport = STAILQ_NEXT(port, links);
4794 * Drop the lock while we call the FETD's enable routine.
4795 * This can lead to a callback into CTL (at least in the
4796 * case of the internal initiator frontend.
4798 mtx_unlock(&ctl_softc->ctl_lock);
4799 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4800 mtx_lock(&ctl_softc->ctl_lock);
4802 printf("%s: FETD %s port %d returned error "
4803 "%d for lun_enable on target %ju lun %jd\n",
4804 __func__, port->port_name, port->targ_port, retval,
4805 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4809 /* NOTE: TODO: why does lun enable affect port status? */
4810 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4815 mtx_unlock(&ctl_softc->ctl_lock);
4821 ctl_disable_lun(struct ctl_be_lun *be_lun)
4823 struct ctl_softc *ctl_softc;
4824 struct ctl_port *port;
4825 struct ctl_lun *lun;
4828 ctl_softc = control_softc;
4830 lun = (struct ctl_lun *)be_lun->ctl_lun;
4832 mtx_lock(&ctl_softc->ctl_lock);
4833 mtx_lock(&lun->lun_lock);
4834 if (lun->flags & CTL_LUN_DISABLED) {
4835 mtx_unlock(&lun->lun_lock);
4836 mtx_unlock(&ctl_softc->ctl_lock);
4839 lun->flags |= CTL_LUN_DISABLED;
4840 mtx_unlock(&lun->lun_lock);
4842 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4843 mtx_unlock(&ctl_softc->ctl_lock);
4845 * Drop the lock before we call the frontend's disable
4846 * routine, to avoid lock order reversals.
4848 * XXX KDM what happens if the frontend list changes while
4849 * we're traversing it? It's unlikely, but should be handled.
4851 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4853 mtx_lock(&ctl_softc->ctl_lock);
4855 printf("ctl_alloc_lun: FETD %s port %d returned error "
4856 "%d for lun_disable on target %ju lun %jd\n",
4857 port->port_name, port->targ_port, retval,
4858 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4862 mtx_unlock(&ctl_softc->ctl_lock);
4868 ctl_start_lun(struct ctl_be_lun *be_lun)
4870 struct ctl_softc *ctl_softc;
4871 struct ctl_lun *lun;
4873 ctl_softc = control_softc;
4875 lun = (struct ctl_lun *)be_lun->ctl_lun;
4877 mtx_lock(&lun->lun_lock);
4878 lun->flags &= ~CTL_LUN_STOPPED;
4879 mtx_unlock(&lun->lun_lock);
4885 ctl_stop_lun(struct ctl_be_lun *be_lun)
4887 struct ctl_softc *ctl_softc;
4888 struct ctl_lun *lun;
4890 ctl_softc = control_softc;
4892 lun = (struct ctl_lun *)be_lun->ctl_lun;
4894 mtx_lock(&lun->lun_lock);
4895 lun->flags |= CTL_LUN_STOPPED;
4896 mtx_unlock(&lun->lun_lock);
4902 ctl_lun_offline(struct ctl_be_lun *be_lun)
4904 struct ctl_softc *ctl_softc;
4905 struct ctl_lun *lun;
4907 ctl_softc = control_softc;
4909 lun = (struct ctl_lun *)be_lun->ctl_lun;
4911 mtx_lock(&lun->lun_lock);
4912 lun->flags |= CTL_LUN_OFFLINE;
4913 mtx_unlock(&lun->lun_lock);
4919 ctl_lun_online(struct ctl_be_lun *be_lun)
4921 struct ctl_softc *ctl_softc;
4922 struct ctl_lun *lun;
4924 ctl_softc = control_softc;
4926 lun = (struct ctl_lun *)be_lun->ctl_lun;
4928 mtx_lock(&lun->lun_lock);
4929 lun->flags &= ~CTL_LUN_OFFLINE;
4930 mtx_unlock(&lun->lun_lock);
4936 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4938 struct ctl_softc *ctl_softc;
4939 struct ctl_lun *lun;
4941 ctl_softc = control_softc;
4943 lun = (struct ctl_lun *)be_lun->ctl_lun;
4945 mtx_lock(&lun->lun_lock);
4948 * The LUN needs to be disabled before it can be marked invalid.
4950 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4951 mtx_unlock(&lun->lun_lock);
4955 * Mark the LUN invalid.
4957 lun->flags |= CTL_LUN_INVALID;
4960 * If there is nothing in the OOA queue, go ahead and free the LUN.
4961 * If we have something in the OOA queue, we'll free it when the
4962 * last I/O completes.
4964 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4965 mtx_unlock(&lun->lun_lock);
4966 mtx_lock(&ctl_softc->ctl_lock);
4968 mtx_unlock(&ctl_softc->ctl_lock);
4970 mtx_unlock(&lun->lun_lock);
4976 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
4978 struct ctl_softc *ctl_softc;
4979 struct ctl_lun *lun;
4981 ctl_softc = control_softc;
4982 lun = (struct ctl_lun *)be_lun->ctl_lun;
4984 mtx_lock(&lun->lun_lock);
4985 lun->flags |= CTL_LUN_INOPERABLE;
4986 mtx_unlock(&lun->lun_lock);
4992 ctl_lun_operable(struct ctl_be_lun *be_lun)
4994 struct ctl_softc *ctl_softc;
4995 struct ctl_lun *lun;
4997 ctl_softc = control_softc;
4998 lun = (struct ctl_lun *)be_lun->ctl_lun;
5000 mtx_lock(&lun->lun_lock);
5001 lun->flags &= ~CTL_LUN_INOPERABLE;
5002 mtx_unlock(&lun->lun_lock);
5008 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
5011 struct ctl_softc *softc;
5012 struct ctl_lun *lun;
5013 struct copan_aps_subpage *current_sp;
5014 struct ctl_page_index *page_index;
5017 softc = control_softc;
5019 mtx_lock(&softc->ctl_lock);
5021 lun = (struct ctl_lun *)be_lun->ctl_lun;
5022 mtx_lock(&lun->lun_lock);
5025 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5026 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
5030 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
5032 page_index = &lun->mode_pages.index[i];
5035 if (page_index == NULL) {
5036 mtx_unlock(&lun->lun_lock);
5037 mtx_unlock(&softc->ctl_lock);
5038 printf("%s: APS subpage not found for lun %ju!\n", __func__,
5039 (uintmax_t)lun->lun);
5043 if ((softc->aps_locked_lun != 0)
5044 && (softc->aps_locked_lun != lun->lun)) {
5045 printf("%s: attempt to lock LUN %llu when %llu is already "
5047 mtx_unlock(&lun->lun_lock);
5048 mtx_unlock(&softc->ctl_lock);
5053 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5054 (page_index->page_len * CTL_PAGE_CURRENT));
5057 current_sp->lock_active = APS_LOCK_ACTIVE;
5058 softc->aps_locked_lun = lun->lun;
5060 current_sp->lock_active = 0;
5061 softc->aps_locked_lun = 0;
5066 * If we're in HA mode, try to send the lock message to the other
5069 if (ctl_is_single == 0) {
5071 union ctl_ha_msg lock_msg;
5073 lock_msg.hdr.nexus = *nexus;
5074 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
5076 lock_msg.aps.lock_flag = 1;
5078 lock_msg.aps.lock_flag = 0;
5079 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
5080 sizeof(lock_msg), 0);
5081 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
5082 printf("%s: APS (lock=%d) error returned from "
5083 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
5084 mtx_unlock(&lun->lun_lock);
5085 mtx_unlock(&softc->ctl_lock);
5090 mtx_unlock(&lun->lun_lock);
5091 mtx_unlock(&softc->ctl_lock);
5097 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5099 struct ctl_lun *lun;
5100 struct ctl_softc *softc;
5103 softc = control_softc;
5105 lun = (struct ctl_lun *)be_lun->ctl_lun;
5107 mtx_lock(&lun->lun_lock);
5109 for (i = 0; i < CTL_MAX_INITIATORS; i++)
5110 lun->pending_sense[i].ua_pending |= CTL_UA_CAPACITY_CHANGED;
5112 mtx_unlock(&lun->lun_lock);
5116 * Backend "memory move is complete" callback for requests that never
5117 * make it down to say RAIDCore's configuration code.
5120 ctl_config_move_done(union ctl_io *io)
5124 retval = CTL_RETVAL_COMPLETE;
5127 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5129 * XXX KDM this shouldn't happen, but what if it does?
5131 if (io->io_hdr.io_type != CTL_IO_SCSI)
5132 panic("I/O type isn't CTL_IO_SCSI!");
5134 if ((io->io_hdr.port_status == 0)
5135 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5136 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
5137 io->io_hdr.status = CTL_SUCCESS;
5138 else if ((io->io_hdr.port_status != 0)
5139 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5140 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
5142 * For hardware error sense keys, the sense key
5143 * specific value is defined to be a retry count,
5144 * but we use it to pass back an internal FETD
5145 * error code. XXX KDM Hopefully the FETD is only
5146 * using 16 bits for an error code, since that's
5147 * all the space we have in the sks field.
5149 ctl_set_internal_failure(&io->scsiio,
5152 io->io_hdr.port_status);
5153 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5154 free(io->scsiio.kern_data_ptr, M_CTL);
5159 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
5160 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
5161 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5163 * XXX KDM just assuming a single pointer here, and not a
5164 * S/G list. If we start using S/G lists for config data,
5165 * we'll need to know how to clean them up here as well.
5167 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5168 free(io->scsiio.kern_data_ptr, M_CTL);
5169 /* Hopefully the user has already set the status... */
5173 * XXX KDM now we need to continue data movement. Some
5175 * - call ctl_scsiio() again? We don't do this for data
5176 * writes, because for those at least we know ahead of
5177 * time where the write will go and how long it is. For
5178 * config writes, though, that information is largely
5179 * contained within the write itself, thus we need to
5180 * parse out the data again.
5182 * - Call some other function once the data is in?
5186 * XXX KDM call ctl_scsiio() again for now, and check flag
5187 * bits to see whether we're allocated or not.
5189 retval = ctl_scsiio(&io->scsiio);
5196 * This gets called by a backend driver when it is done with a
5197 * data_submit method.
5200 ctl_data_submit_done(union ctl_io *io)
5203 * If the IO_CONT flag is set, we need to call the supplied
5204 * function to continue processing the I/O, instead of completing
5207 * If there is an error, though, we don't want to keep processing.
5208 * Instead, just send status back to the initiator.
5210 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5211 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5212 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5213 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5214 io->scsiio.io_cont(io);
5221 * This gets called by a backend driver when it is done with a
5222 * configuration write.
5225 ctl_config_write_done(union ctl_io *io)
5228 * If the IO_CONT flag is set, we need to call the supplied
5229 * function to continue processing the I/O, instead of completing
5232 * If there is an error, though, we don't want to keep processing.
5233 * Instead, just send status back to the initiator.
5235 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5236 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5237 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5238 io->scsiio.io_cont(io);
5242 * Since a configuration write can be done for commands that actually
5243 * have data allocated, like write buffer, and commands that have
5244 * no data, like start/stop unit, we need to check here.
5246 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
5247 free(io->scsiio.kern_data_ptr, M_CTL);
5252 * SCSI release command.
5255 ctl_scsi_release(struct ctl_scsiio *ctsio)
5257 int length, longid, thirdparty_id, resv_id;
5258 struct ctl_softc *ctl_softc;
5259 struct ctl_lun *lun;
5264 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5266 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5267 ctl_softc = control_softc;
5269 switch (ctsio->cdb[0]) {
5271 struct scsi_release_10 *cdb;
5273 cdb = (struct scsi_release_10 *)ctsio->cdb;
5275 if (cdb->byte2 & SR10_LONGID)
5278 thirdparty_id = cdb->thirdparty_id;
5280 resv_id = cdb->resv_id;
5281 length = scsi_2btoul(cdb->length);
5288 * XXX KDM right now, we only support LUN reservation. We don't
5289 * support 3rd party reservations, or extent reservations, which
5290 * might actually need the parameter list. If we've gotten this
5291 * far, we've got a LUN reservation. Anything else got kicked out
5292 * above. So, according to SPC, ignore the length.
5296 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5298 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5299 ctsio->kern_data_len = length;
5300 ctsio->kern_total_len = length;
5301 ctsio->kern_data_resid = 0;
5302 ctsio->kern_rel_offset = 0;
5303 ctsio->kern_sg_entries = 0;
5304 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5305 ctsio->be_move_done = ctl_config_move_done;
5306 ctl_datamove((union ctl_io *)ctsio);
5308 return (CTL_RETVAL_COMPLETE);
5312 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5314 mtx_lock(&lun->lun_lock);
5317 * According to SPC, it is not an error for an intiator to attempt
5318 * to release a reservation on a LUN that isn't reserved, or that
5319 * is reserved by another initiator. The reservation can only be
5320 * released, though, by the initiator who made it or by one of
5321 * several reset type events.
5323 if (lun->flags & CTL_LUN_RESERVED) {
5324 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5325 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5326 && (ctsio->io_hdr.nexus.targ_target.id ==
5327 lun->rsv_nexus.targ_target.id)) {
5328 lun->flags &= ~CTL_LUN_RESERVED;
5332 mtx_unlock(&lun->lun_lock);
5334 ctsio->scsi_status = SCSI_STATUS_OK;
5335 ctsio->io_hdr.status = CTL_SUCCESS;
5337 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5338 free(ctsio->kern_data_ptr, M_CTL);
5339 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5342 ctl_done((union ctl_io *)ctsio);
5343 return (CTL_RETVAL_COMPLETE);
5347 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5349 int extent, thirdparty, longid;
5350 int resv_id, length;
5351 uint64_t thirdparty_id;
5352 struct ctl_softc *ctl_softc;
5353 struct ctl_lun *lun;
5362 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5364 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5365 ctl_softc = control_softc;
5367 switch (ctsio->cdb[0]) {
5369 struct scsi_reserve_10 *cdb;
5371 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5373 if (cdb->byte2 & SR10_LONGID)
5376 thirdparty_id = cdb->thirdparty_id;
5378 resv_id = cdb->resv_id;
5379 length = scsi_2btoul(cdb->length);
5385 * XXX KDM right now, we only support LUN reservation. We don't
5386 * support 3rd party reservations, or extent reservations, which
5387 * might actually need the parameter list. If we've gotten this
5388 * far, we've got a LUN reservation. Anything else got kicked out
5389 * above. So, according to SPC, ignore the length.
5393 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5395 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5396 ctsio->kern_data_len = length;
5397 ctsio->kern_total_len = length;
5398 ctsio->kern_data_resid = 0;
5399 ctsio->kern_rel_offset = 0;
5400 ctsio->kern_sg_entries = 0;
5401 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5402 ctsio->be_move_done = ctl_config_move_done;
5403 ctl_datamove((union ctl_io *)ctsio);
5405 return (CTL_RETVAL_COMPLETE);
5409 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5411 mtx_lock(&lun->lun_lock);
5412 if (lun->flags & CTL_LUN_RESERVED) {
5413 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5414 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5415 || (ctsio->io_hdr.nexus.targ_target.id !=
5416 lun->rsv_nexus.targ_target.id)) {
5417 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5418 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5423 lun->flags |= CTL_LUN_RESERVED;
5424 lun->rsv_nexus = ctsio->io_hdr.nexus;
5426 ctsio->scsi_status = SCSI_STATUS_OK;
5427 ctsio->io_hdr.status = CTL_SUCCESS;
5430 mtx_unlock(&lun->lun_lock);
5432 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5433 free(ctsio->kern_data_ptr, M_CTL);
5434 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5437 ctl_done((union ctl_io *)ctsio);
5438 return (CTL_RETVAL_COMPLETE);
5442 ctl_start_stop(struct ctl_scsiio *ctsio)
5444 struct scsi_start_stop_unit *cdb;
5445 struct ctl_lun *lun;
5446 struct ctl_softc *ctl_softc;
5449 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5451 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5452 ctl_softc = control_softc;
5455 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5459 * We don't support the immediate bit on a stop unit. In order to
5460 * do that, we would need to code up a way to know that a stop is
5461 * pending, and hold off any new commands until it completes, one
5462 * way or another. Then we could accept or reject those commands
5463 * depending on its status. We would almost need to do the reverse
5464 * of what we do below for an immediate start -- return the copy of
5465 * the ctl_io to the FETD with status to send to the host (and to
5466 * free the copy!) and then free the original I/O once the stop
5467 * actually completes. That way, the OOA queue mechanism can work
5468 * to block commands that shouldn't proceed. Another alternative
5469 * would be to put the copy in the queue in place of the original,
5470 * and return the original back to the caller. That could be
5473 if ((cdb->byte2 & SSS_IMMED)
5474 && ((cdb->how & SSS_START) == 0)) {
5475 ctl_set_invalid_field(ctsio,
5481 ctl_done((union ctl_io *)ctsio);
5482 return (CTL_RETVAL_COMPLETE);
5485 if ((lun->flags & CTL_LUN_PR_RESERVED)
5486 && ((cdb->how & SSS_START)==0)) {
5489 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5490 if (!lun->per_res[residx].registered
5491 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5493 ctl_set_reservation_conflict(ctsio);
5494 ctl_done((union ctl_io *)ctsio);
5495 return (CTL_RETVAL_COMPLETE);
5500 * If there is no backend on this device, we can't start or stop
5501 * it. In theory we shouldn't get any start/stop commands in the
5502 * first place at this level if the LUN doesn't have a backend.
5503 * That should get stopped by the command decode code.
5505 if (lun->backend == NULL) {
5506 ctl_set_invalid_opcode(ctsio);
5507 ctl_done((union ctl_io *)ctsio);
5508 return (CTL_RETVAL_COMPLETE);
5512 * XXX KDM Copan-specific offline behavior.
5513 * Figure out a reasonable way to port this?
5516 mtx_lock(&lun->lun_lock);
5518 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5519 && (lun->flags & CTL_LUN_OFFLINE)) {
5521 * If the LUN is offline, and the on/offline bit isn't set,
5522 * reject the start or stop. Otherwise, let it through.
5524 mtx_unlock(&lun->lun_lock);
5525 ctl_set_lun_not_ready(ctsio);
5526 ctl_done((union ctl_io *)ctsio);
5528 mtx_unlock(&lun->lun_lock);
5529 #endif /* NEEDTOPORT */
5531 * This could be a start or a stop when we're online,
5532 * or a stop/offline or start/online. A start or stop when
5533 * we're offline is covered in the case above.
5536 * In the non-immediate case, we send the request to
5537 * the backend and return status to the user when
5540 * In the immediate case, we allocate a new ctl_io
5541 * to hold a copy of the request, and send that to
5542 * the backend. We then set good status on the
5543 * user's request and return it immediately.
5545 if (cdb->byte2 & SSS_IMMED) {
5546 union ctl_io *new_io;
5548 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5549 if (new_io == NULL) {
5550 ctl_set_busy(ctsio);
5551 ctl_done((union ctl_io *)ctsio);
5553 ctl_copy_io((union ctl_io *)ctsio,
5555 retval = lun->backend->config_write(new_io);
5556 ctl_set_success(ctsio);
5557 ctl_done((union ctl_io *)ctsio);
5560 retval = lun->backend->config_write(
5561 (union ctl_io *)ctsio);
5570 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5571 * we don't really do anything with the LBA and length fields if the user
5572 * passes them in. Instead we'll just flush out the cache for the entire
5576 ctl_sync_cache(struct ctl_scsiio *ctsio)
5578 struct ctl_lun *lun;
5579 struct ctl_softc *ctl_softc;
5580 uint64_t starting_lba;
5581 uint32_t block_count;
5584 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5586 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5587 ctl_softc = control_softc;
5590 switch (ctsio->cdb[0]) {
5591 case SYNCHRONIZE_CACHE: {
5592 struct scsi_sync_cache *cdb;
5593 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5595 starting_lba = scsi_4btoul(cdb->begin_lba);
5596 block_count = scsi_2btoul(cdb->lb_count);
5599 case SYNCHRONIZE_CACHE_16: {
5600 struct scsi_sync_cache_16 *cdb;
5601 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5603 starting_lba = scsi_8btou64(cdb->begin_lba);
5604 block_count = scsi_4btoul(cdb->lb_count);
5608 ctl_set_invalid_opcode(ctsio);
5609 ctl_done((union ctl_io *)ctsio);
5611 break; /* NOTREACHED */
5615 * We check the LBA and length, but don't do anything with them.
5616 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5617 * get flushed. This check will just help satisfy anyone who wants
5618 * to see an error for an out of range LBA.
5620 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5621 ctl_set_lba_out_of_range(ctsio);
5622 ctl_done((union ctl_io *)ctsio);
5627 * If this LUN has no backend, we can't flush the cache anyway.
5629 if (lun->backend == NULL) {
5630 ctl_set_invalid_opcode(ctsio);
5631 ctl_done((union ctl_io *)ctsio);
5636 * Check to see whether we're configured to send the SYNCHRONIZE
5637 * CACHE command directly to the back end.
5639 mtx_lock(&lun->lun_lock);
5640 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5641 && (++(lun->sync_count) >= lun->sync_interval)) {
5642 lun->sync_count = 0;
5643 mtx_unlock(&lun->lun_lock);
5644 retval = lun->backend->config_write((union ctl_io *)ctsio);
5646 mtx_unlock(&lun->lun_lock);
5647 ctl_set_success(ctsio);
5648 ctl_done((union ctl_io *)ctsio);
5657 ctl_format(struct ctl_scsiio *ctsio)
5659 struct scsi_format *cdb;
5660 struct ctl_lun *lun;
5661 struct ctl_softc *ctl_softc;
5662 int length, defect_list_len;
5664 CTL_DEBUG_PRINT(("ctl_format\n"));
5666 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5667 ctl_softc = control_softc;
5669 cdb = (struct scsi_format *)ctsio->cdb;
5672 if (cdb->byte2 & SF_FMTDATA) {
5673 if (cdb->byte2 & SF_LONGLIST)
5674 length = sizeof(struct scsi_format_header_long);
5676 length = sizeof(struct scsi_format_header_short);
5679 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5681 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5682 ctsio->kern_data_len = length;
5683 ctsio->kern_total_len = length;
5684 ctsio->kern_data_resid = 0;
5685 ctsio->kern_rel_offset = 0;
5686 ctsio->kern_sg_entries = 0;
5687 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5688 ctsio->be_move_done = ctl_config_move_done;
5689 ctl_datamove((union ctl_io *)ctsio);
5691 return (CTL_RETVAL_COMPLETE);
5694 defect_list_len = 0;
5696 if (cdb->byte2 & SF_FMTDATA) {
5697 if (cdb->byte2 & SF_LONGLIST) {
5698 struct scsi_format_header_long *header;
5700 header = (struct scsi_format_header_long *)
5701 ctsio->kern_data_ptr;
5703 defect_list_len = scsi_4btoul(header->defect_list_len);
5704 if (defect_list_len != 0) {
5705 ctl_set_invalid_field(ctsio,
5714 struct scsi_format_header_short *header;
5716 header = (struct scsi_format_header_short *)
5717 ctsio->kern_data_ptr;
5719 defect_list_len = scsi_2btoul(header->defect_list_len);
5720 if (defect_list_len != 0) {
5721 ctl_set_invalid_field(ctsio,
5733 * The format command will clear out the "Medium format corrupted"
5734 * status if set by the configuration code. That status is really
5735 * just a way to notify the host that we have lost the media, and
5736 * get them to issue a command that will basically make them think
5737 * they're blowing away the media.
5739 mtx_lock(&lun->lun_lock);
5740 lun->flags &= ~CTL_LUN_INOPERABLE;
5741 mtx_unlock(&lun->lun_lock);
5743 ctsio->scsi_status = SCSI_STATUS_OK;
5744 ctsio->io_hdr.status = CTL_SUCCESS;
5747 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5748 free(ctsio->kern_data_ptr, M_CTL);
5749 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5752 ctl_done((union ctl_io *)ctsio);
5753 return (CTL_RETVAL_COMPLETE);
5757 ctl_read_buffer(struct ctl_scsiio *ctsio)
5759 struct scsi_read_buffer *cdb;
5760 struct ctl_lun *lun;
5761 int buffer_offset, len;
5762 static uint8_t descr[4];
5763 static uint8_t echo_descr[4] = { 0 };
5765 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5767 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5768 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5770 if (lun->flags & CTL_LUN_PR_RESERVED) {
5774 * XXX KDM need a lock here.
5776 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5777 if ((lun->res_type == SPR_TYPE_EX_AC
5778 && residx != lun->pr_res_idx)
5779 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5780 || lun->res_type == SPR_TYPE_EX_AC_AR)
5781 && !lun->per_res[residx].registered)) {
5782 ctl_set_reservation_conflict(ctsio);
5783 ctl_done((union ctl_io *)ctsio);
5784 return (CTL_RETVAL_COMPLETE);
5788 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5789 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5790 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5791 ctl_set_invalid_field(ctsio,
5797 ctl_done((union ctl_io *)ctsio);
5798 return (CTL_RETVAL_COMPLETE);
5801 len = scsi_3btoul(cdb->length);
5802 buffer_offset = scsi_3btoul(cdb->offset);
5804 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5805 ctl_set_invalid_field(ctsio,
5811 ctl_done((union ctl_io *)ctsio);
5812 return (CTL_RETVAL_COMPLETE);
5815 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5817 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5818 ctsio->kern_data_ptr = descr;
5819 len = min(len, sizeof(descr));
5820 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5821 ctsio->kern_data_ptr = echo_descr;
5822 len = min(len, sizeof(echo_descr));
5824 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5825 ctsio->kern_data_len = len;
5826 ctsio->kern_total_len = len;
5827 ctsio->kern_data_resid = 0;
5828 ctsio->kern_rel_offset = 0;
5829 ctsio->kern_sg_entries = 0;
5830 ctsio->be_move_done = ctl_config_move_done;
5831 ctl_datamove((union ctl_io *)ctsio);
5833 return (CTL_RETVAL_COMPLETE);
5837 ctl_write_buffer(struct ctl_scsiio *ctsio)
5839 struct scsi_write_buffer *cdb;
5840 struct ctl_lun *lun;
5841 int buffer_offset, len;
5843 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5845 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5846 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5848 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5849 ctl_set_invalid_field(ctsio,
5855 ctl_done((union ctl_io *)ctsio);
5856 return (CTL_RETVAL_COMPLETE);
5859 len = scsi_3btoul(cdb->length);
5860 buffer_offset = scsi_3btoul(cdb->offset);
5862 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5863 ctl_set_invalid_field(ctsio,
5869 ctl_done((union ctl_io *)ctsio);
5870 return (CTL_RETVAL_COMPLETE);
5874 * If we've got a kernel request that hasn't been malloced yet,
5875 * malloc it and tell the caller the data buffer is here.
5877 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5878 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5879 ctsio->kern_data_len = len;
5880 ctsio->kern_total_len = len;
5881 ctsio->kern_data_resid = 0;
5882 ctsio->kern_rel_offset = 0;
5883 ctsio->kern_sg_entries = 0;
5884 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5885 ctsio->be_move_done = ctl_config_move_done;
5886 ctl_datamove((union ctl_io *)ctsio);
5888 return (CTL_RETVAL_COMPLETE);
5891 ctl_done((union ctl_io *)ctsio);
5893 return (CTL_RETVAL_COMPLETE);
5897 ctl_write_same(struct ctl_scsiio *ctsio)
5899 struct ctl_lun *lun;
5900 struct ctl_lba_len_flags *lbalen;
5902 uint32_t num_blocks;
5906 retval = CTL_RETVAL_COMPLETE;
5908 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5910 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5912 switch (ctsio->cdb[0]) {
5913 case WRITE_SAME_10: {
5914 struct scsi_write_same_10 *cdb;
5916 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5918 lba = scsi_4btoul(cdb->addr);
5919 num_blocks = scsi_2btoul(cdb->length);
5923 case WRITE_SAME_16: {
5924 struct scsi_write_same_16 *cdb;
5926 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5928 lba = scsi_8btou64(cdb->addr);
5929 num_blocks = scsi_4btoul(cdb->length);
5935 * We got a command we don't support. This shouldn't
5936 * happen, commands should be filtered out above us.
5938 ctl_set_invalid_opcode(ctsio);
5939 ctl_done((union ctl_io *)ctsio);
5941 return (CTL_RETVAL_COMPLETE);
5942 break; /* NOTREACHED */
5946 * The first check is to make sure we're in bounds, the second
5947 * check is to catch wrap-around problems. If the lba + num blocks
5948 * is less than the lba, then we've wrapped around and the block
5949 * range is invalid anyway.
5951 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5952 || ((lba + num_blocks) < lba)) {
5953 ctl_set_lba_out_of_range(ctsio);
5954 ctl_done((union ctl_io *)ctsio);
5955 return (CTL_RETVAL_COMPLETE);
5958 /* Zero number of blocks means "to the last logical block" */
5959 if (num_blocks == 0) {
5960 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5961 ctl_set_invalid_field(ctsio,
5967 ctl_done((union ctl_io *)ctsio);
5968 return (CTL_RETVAL_COMPLETE);
5970 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5973 len = lun->be_lun->blocksize;
5976 * If we've got a kernel request that hasn't been malloced yet,
5977 * malloc it and tell the caller the data buffer is here.
5979 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5980 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5981 ctsio->kern_data_len = len;
5982 ctsio->kern_total_len = len;
5983 ctsio->kern_data_resid = 0;
5984 ctsio->kern_rel_offset = 0;
5985 ctsio->kern_sg_entries = 0;
5986 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5987 ctsio->be_move_done = ctl_config_move_done;
5988 ctl_datamove((union ctl_io *)ctsio);
5990 return (CTL_RETVAL_COMPLETE);
5993 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5995 lbalen->len = num_blocks;
5996 lbalen->flags = byte2;
5997 retval = lun->backend->config_write((union ctl_io *)ctsio);
6003 ctl_unmap(struct ctl_scsiio *ctsio)
6005 struct ctl_lun *lun;
6006 struct scsi_unmap *cdb;
6007 struct ctl_ptr_len_flags *ptrlen;
6008 struct scsi_unmap_header *hdr;
6009 struct scsi_unmap_desc *buf, *end;
6011 uint32_t num_blocks;
6015 retval = CTL_RETVAL_COMPLETE;
6017 CTL_DEBUG_PRINT(("ctl_unmap\n"));
6019 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6020 cdb = (struct scsi_unmap *)ctsio->cdb;
6022 len = scsi_2btoul(cdb->length);
6026 * If we've got a kernel request that hasn't been malloced yet,
6027 * malloc it and tell the caller the data buffer is here.
6029 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6030 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6031 ctsio->kern_data_len = len;
6032 ctsio->kern_total_len = len;
6033 ctsio->kern_data_resid = 0;
6034 ctsio->kern_rel_offset = 0;
6035 ctsio->kern_sg_entries = 0;
6036 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6037 ctsio->be_move_done = ctl_config_move_done;
6038 ctl_datamove((union ctl_io *)ctsio);
6040 return (CTL_RETVAL_COMPLETE);
6043 len = ctsio->kern_total_len - ctsio->kern_data_resid;
6044 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
6045 if (len < sizeof (*hdr) ||
6046 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
6047 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
6048 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
6049 ctl_set_invalid_field(ctsio,
6055 ctl_done((union ctl_io *)ctsio);
6056 return (CTL_RETVAL_COMPLETE);
6058 len = scsi_2btoul(hdr->desc_length);
6059 buf = (struct scsi_unmap_desc *)(hdr + 1);
6060 end = buf + len / sizeof(*buf);
6062 ptrlen = (struct ctl_ptr_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6063 ptrlen->ptr = (void *)buf;
6065 ptrlen->flags = byte2;
6067 for (; buf < end; buf++) {
6068 lba = scsi_8btou64(buf->lba);
6069 num_blocks = scsi_4btoul(buf->length);
6070 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6071 || ((lba + num_blocks) < lba)) {
6072 ctl_set_lba_out_of_range(ctsio);
6073 ctl_done((union ctl_io *)ctsio);
6074 return (CTL_RETVAL_COMPLETE);
6078 retval = lun->backend->config_write((union ctl_io *)ctsio);
6084 * Note that this function currently doesn't actually do anything inside
6085 * CTL to enforce things if the DQue bit is turned on.
6087 * Also note that this function can't be used in the default case, because
6088 * the DQue bit isn't set in the changeable mask for the control mode page
6089 * anyway. This is just here as an example for how to implement a page
6090 * handler, and a placeholder in case we want to allow the user to turn
6091 * tagged queueing on and off.
6093 * The D_SENSE bit handling is functional, however, and will turn
6094 * descriptor sense on and off for a given LUN.
6097 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6098 struct ctl_page_index *page_index, uint8_t *page_ptr)
6100 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6101 struct ctl_lun *lun;
6102 struct ctl_softc *softc;
6106 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6107 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6110 user_cp = (struct scsi_control_page *)page_ptr;
6111 current_cp = (struct scsi_control_page *)
6112 (page_index->page_data + (page_index->page_len *
6114 saved_cp = (struct scsi_control_page *)
6115 (page_index->page_data + (page_index->page_len *
6118 softc = control_softc;
6120 mtx_lock(&lun->lun_lock);
6121 if (((current_cp->rlec & SCP_DSENSE) == 0)
6122 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6124 * Descriptor sense is currently turned off and the user
6125 * wants to turn it on.
6127 current_cp->rlec |= SCP_DSENSE;
6128 saved_cp->rlec |= SCP_DSENSE;
6129 lun->flags |= CTL_LUN_SENSE_DESC;
6131 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6132 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6134 * Descriptor sense is currently turned on, and the user
6135 * wants to turn it off.
6137 current_cp->rlec &= ~SCP_DSENSE;
6138 saved_cp->rlec &= ~SCP_DSENSE;
6139 lun->flags &= ~CTL_LUN_SENSE_DESC;
6142 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
6143 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6145 csevent_log(CSC_CTL | CSC_SHELF_SW |
6147 csevent_LogType_Trace,
6148 csevent_Severity_Information,
6149 csevent_AlertLevel_Green,
6150 csevent_FRU_Firmware,
6151 csevent_FRU_Unknown,
6152 "Received untagged to untagged transition");
6153 #endif /* NEEDTOPORT */
6156 csevent_log(CSC_CTL | CSC_SHELF_SW |
6158 csevent_LogType_ConfigChange,
6159 csevent_Severity_Information,
6160 csevent_AlertLevel_Green,
6161 csevent_FRU_Firmware,
6162 csevent_FRU_Unknown,
6163 "Received untagged to tagged "
6164 "queueing transition");
6165 #endif /* NEEDTOPORT */
6167 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6168 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6172 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6174 csevent_log(CSC_CTL | CSC_SHELF_SW |
6176 csevent_LogType_ConfigChange,
6177 csevent_Severity_Warning,
6178 csevent_AlertLevel_Yellow,
6179 csevent_FRU_Firmware,
6180 csevent_FRU_Unknown,
6181 "Received tagged queueing to untagged "
6183 #endif /* NEEDTOPORT */
6185 current_cp->queue_flags |= SCP_QUEUE_DQUE;
6186 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
6190 csevent_log(CSC_CTL | CSC_SHELF_SW |
6192 csevent_LogType_Trace,
6193 csevent_Severity_Information,
6194 csevent_AlertLevel_Green,
6195 csevent_FRU_Firmware,
6196 csevent_FRU_Unknown,
6197 "Received tagged queueing to tagged "
6198 "queueing transition");
6199 #endif /* NEEDTOPORT */
6205 * Let other initiators know that the mode
6206 * parameters for this LUN have changed.
6208 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6212 lun->pending_sense[i].ua_pending |=
6216 mtx_unlock(&lun->lun_lock);
6222 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
6223 struct ctl_page_index *page_index, uint8_t *page_ptr)
6229 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
6230 struct ctl_page_index *page_index, int pc)
6232 struct copan_power_subpage *page;
6234 page = (struct copan_power_subpage *)page_index->page_data +
6235 (page_index->page_len * pc);
6238 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6240 * We don't update the changable bits for this page.
6243 case SMS_PAGE_CTRL_CURRENT >> 6:
6244 case SMS_PAGE_CTRL_DEFAULT >> 6:
6245 case SMS_PAGE_CTRL_SAVED >> 6:
6247 ctl_update_power_subpage(page);
6252 EPRINT(0, "Invalid PC %d!!", pc);
6261 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
6262 struct ctl_page_index *page_index, uint8_t *page_ptr)
6264 struct copan_aps_subpage *user_sp;
6265 struct copan_aps_subpage *current_sp;
6266 union ctl_modepage_info *modepage_info;
6267 struct ctl_softc *softc;
6268 struct ctl_lun *lun;
6271 retval = CTL_RETVAL_COMPLETE;
6272 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
6273 (page_index->page_len * CTL_PAGE_CURRENT));
6274 softc = control_softc;
6275 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6277 user_sp = (struct copan_aps_subpage *)page_ptr;
6279 modepage_info = (union ctl_modepage_info *)
6280 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6282 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
6283 modepage_info->header.subpage = page_index->subpage;
6284 modepage_info->aps.lock_active = user_sp->lock_active;
6286 mtx_lock(&softc->ctl_lock);
6289 * If there is a request to lock the LUN and another LUN is locked
6290 * this is an error. If the requested LUN is already locked ignore
6291 * the request. If no LUN is locked attempt to lock it.
6292 * if there is a request to unlock the LUN and the LUN is currently
6293 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6294 * if another LUN is locked or no LUN is locked.
6296 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6297 if (softc->aps_locked_lun == lun->lun) {
6299 * This LUN is already locked, so we're done.
6301 retval = CTL_RETVAL_COMPLETE;
6302 } else if (softc->aps_locked_lun == 0) {
6304 * No one has the lock, pass the request to the
6307 retval = lun->backend->config_write(
6308 (union ctl_io *)ctsio);
6311 * Someone else has the lock, throw out the request.
6313 ctl_set_already_locked(ctsio);
6314 free(ctsio->kern_data_ptr, M_CTL);
6315 ctl_done((union ctl_io *)ctsio);
6318 * Set the return value so that ctl_do_mode_select()
6319 * won't try to complete the command. We already
6320 * completed it here.
6322 retval = CTL_RETVAL_ERROR;
6324 } else if (softc->aps_locked_lun == lun->lun) {
6326 * This LUN is locked, so pass the unlock request to the
6329 retval = lun->backend->config_write((union ctl_io *)ctsio);
6331 mtx_unlock(&softc->ctl_lock);
6337 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6338 struct ctl_page_index *page_index,
6344 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6349 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6350 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6351 printf("page data:");
6353 printf(" %.2x",page_ptr[i]);
6359 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6360 struct ctl_page_index *page_index,
6363 struct copan_debugconf_subpage *page;
6365 page = (struct copan_debugconf_subpage *)page_index->page_data +
6366 (page_index->page_len * pc);
6369 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6370 case SMS_PAGE_CTRL_DEFAULT >> 6:
6371 case SMS_PAGE_CTRL_SAVED >> 6:
6373 * We don't update the changable or default bits for this page.
6376 case SMS_PAGE_CTRL_CURRENT >> 6:
6377 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6378 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6382 EPRINT(0, "Invalid PC %d!!", pc);
6383 #endif /* NEEDTOPORT */
6391 ctl_do_mode_select(union ctl_io *io)
6393 struct scsi_mode_page_header *page_header;
6394 struct ctl_page_index *page_index;
6395 struct ctl_scsiio *ctsio;
6396 int control_dev, page_len;
6397 int page_len_offset, page_len_size;
6398 union ctl_modepage_info *modepage_info;
6399 struct ctl_lun *lun;
6400 int *len_left, *len_used;
6403 ctsio = &io->scsiio;
6406 retval = CTL_RETVAL_COMPLETE;
6408 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6410 if (lun->be_lun->lun_type != T_DIRECT)
6415 modepage_info = (union ctl_modepage_info *)
6416 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6417 len_left = &modepage_info->header.len_left;
6418 len_used = &modepage_info->header.len_used;
6422 page_header = (struct scsi_mode_page_header *)
6423 (ctsio->kern_data_ptr + *len_used);
6425 if (*len_left == 0) {
6426 free(ctsio->kern_data_ptr, M_CTL);
6427 ctl_set_success(ctsio);
6428 ctl_done((union ctl_io *)ctsio);
6429 return (CTL_RETVAL_COMPLETE);
6430 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6432 free(ctsio->kern_data_ptr, M_CTL);
6433 ctl_set_param_len_error(ctsio);
6434 ctl_done((union ctl_io *)ctsio);
6435 return (CTL_RETVAL_COMPLETE);
6437 } else if ((page_header->page_code & SMPH_SPF)
6438 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6440 free(ctsio->kern_data_ptr, M_CTL);
6441 ctl_set_param_len_error(ctsio);
6442 ctl_done((union ctl_io *)ctsio);
6443 return (CTL_RETVAL_COMPLETE);
6448 * XXX KDM should we do something with the block descriptor?
6450 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6452 if ((control_dev != 0)
6453 && (lun->mode_pages.index[i].page_flags &
6454 CTL_PAGE_FLAG_DISK_ONLY))
6457 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6458 (page_header->page_code & SMPH_PC_MASK))
6462 * If neither page has a subpage code, then we've got a
6465 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6466 && ((page_header->page_code & SMPH_SPF) == 0)) {
6467 page_index = &lun->mode_pages.index[i];
6468 page_len = page_header->page_length;
6473 * If both pages have subpages, then the subpage numbers
6476 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6477 && (page_header->page_code & SMPH_SPF)) {
6478 struct scsi_mode_page_header_sp *sph;
6480 sph = (struct scsi_mode_page_header_sp *)page_header;
6482 if (lun->mode_pages.index[i].subpage ==
6484 page_index = &lun->mode_pages.index[i];
6485 page_len = scsi_2btoul(sph->page_length);
6492 * If we couldn't find the page, or if we don't have a mode select
6493 * handler for it, send back an error to the user.
6495 if ((page_index == NULL)
6496 || (page_index->select_handler == NULL)) {
6497 ctl_set_invalid_field(ctsio,
6500 /*field*/ *len_used,
6503 free(ctsio->kern_data_ptr, M_CTL);
6504 ctl_done((union ctl_io *)ctsio);
6505 return (CTL_RETVAL_COMPLETE);
6508 if (page_index->page_code & SMPH_SPF) {
6509 page_len_offset = 2;
6513 page_len_offset = 1;
6517 * If the length the initiator gives us isn't the one we specify in
6518 * the mode page header, or if they didn't specify enough data in
6519 * the CDB to avoid truncating this page, kick out the request.
6521 if ((page_len != (page_index->page_len - page_len_offset -
6523 || (*len_left < page_index->page_len)) {
6526 ctl_set_invalid_field(ctsio,
6529 /*field*/ *len_used + page_len_offset,
6532 free(ctsio->kern_data_ptr, M_CTL);
6533 ctl_done((union ctl_io *)ctsio);
6534 return (CTL_RETVAL_COMPLETE);
6538 * Run through the mode page, checking to make sure that the bits
6539 * the user changed are actually legal for him to change.
6541 for (i = 0; i < page_index->page_len; i++) {
6542 uint8_t *user_byte, *change_mask, *current_byte;
6546 user_byte = (uint8_t *)page_header + i;
6547 change_mask = page_index->page_data +
6548 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6549 current_byte = page_index->page_data +
6550 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6553 * Check to see whether the user set any bits in this byte
6554 * that he is not allowed to set.
6556 if ((*user_byte & ~(*change_mask)) ==
6557 (*current_byte & ~(*change_mask)))
6561 * Go through bit by bit to determine which one is illegal.
6564 for (j = 7; j >= 0; j--) {
6565 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6566 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6571 ctl_set_invalid_field(ctsio,
6574 /*field*/ *len_used + i,
6577 free(ctsio->kern_data_ptr, M_CTL);
6578 ctl_done((union ctl_io *)ctsio);
6579 return (CTL_RETVAL_COMPLETE);
6583 * Decrement these before we call the page handler, since we may
6584 * end up getting called back one way or another before the handler
6585 * returns to this context.
6587 *len_left -= page_index->page_len;
6588 *len_used += page_index->page_len;
6590 retval = page_index->select_handler(ctsio, page_index,
6591 (uint8_t *)page_header);
6594 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6595 * wait until this queued command completes to finish processing
6596 * the mode page. If it returns anything other than
6597 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6598 * already set the sense information, freed the data pointer, and
6599 * completed the io for us.
6601 if (retval != CTL_RETVAL_COMPLETE)
6602 goto bailout_no_done;
6605 * If the initiator sent us more than one page, parse the next one.
6610 ctl_set_success(ctsio);
6611 free(ctsio->kern_data_ptr, M_CTL);
6612 ctl_done((union ctl_io *)ctsio);
6616 return (CTL_RETVAL_COMPLETE);
6621 ctl_mode_select(struct ctl_scsiio *ctsio)
6623 int param_len, pf, sp;
6624 int header_size, bd_len;
6625 int len_left, len_used;
6626 struct ctl_page_index *page_index;
6627 struct ctl_lun *lun;
6628 int control_dev, page_len;
6629 union ctl_modepage_info *modepage_info;
6641 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6643 if (lun->be_lun->lun_type != T_DIRECT)
6648 switch (ctsio->cdb[0]) {
6649 case MODE_SELECT_6: {
6650 struct scsi_mode_select_6 *cdb;
6652 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6654 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6655 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6657 param_len = cdb->length;
6658 header_size = sizeof(struct scsi_mode_header_6);
6661 case MODE_SELECT_10: {
6662 struct scsi_mode_select_10 *cdb;
6664 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6666 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6667 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6669 param_len = scsi_2btoul(cdb->length);
6670 header_size = sizeof(struct scsi_mode_header_10);
6674 ctl_set_invalid_opcode(ctsio);
6675 ctl_done((union ctl_io *)ctsio);
6676 return (CTL_RETVAL_COMPLETE);
6677 break; /* NOTREACHED */
6682 * "A parameter list length of zero indicates that the Data-Out Buffer
6683 * shall be empty. This condition shall not be considered as an error."
6685 if (param_len == 0) {
6686 ctl_set_success(ctsio);
6687 ctl_done((union ctl_io *)ctsio);
6688 return (CTL_RETVAL_COMPLETE);
6692 * Since we'll hit this the first time through, prior to
6693 * allocation, we don't need to free a data buffer here.
6695 if (param_len < header_size) {
6696 ctl_set_param_len_error(ctsio);
6697 ctl_done((union ctl_io *)ctsio);
6698 return (CTL_RETVAL_COMPLETE);
6702 * Allocate the data buffer and grab the user's data. In theory,
6703 * we shouldn't have to sanity check the parameter list length here
6704 * because the maximum size is 64K. We should be able to malloc
6705 * that much without too many problems.
6707 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6708 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6709 ctsio->kern_data_len = param_len;
6710 ctsio->kern_total_len = param_len;
6711 ctsio->kern_data_resid = 0;
6712 ctsio->kern_rel_offset = 0;
6713 ctsio->kern_sg_entries = 0;
6714 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6715 ctsio->be_move_done = ctl_config_move_done;
6716 ctl_datamove((union ctl_io *)ctsio);
6718 return (CTL_RETVAL_COMPLETE);
6721 switch (ctsio->cdb[0]) {
6722 case MODE_SELECT_6: {
6723 struct scsi_mode_header_6 *mh6;
6725 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6726 bd_len = mh6->blk_desc_len;
6729 case MODE_SELECT_10: {
6730 struct scsi_mode_header_10 *mh10;
6732 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6733 bd_len = scsi_2btoul(mh10->blk_desc_len);
6737 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6741 if (param_len < (header_size + bd_len)) {
6742 free(ctsio->kern_data_ptr, M_CTL);
6743 ctl_set_param_len_error(ctsio);
6744 ctl_done((union ctl_io *)ctsio);
6745 return (CTL_RETVAL_COMPLETE);
6749 * Set the IO_CONT flag, so that if this I/O gets passed to
6750 * ctl_config_write_done(), it'll get passed back to
6751 * ctl_do_mode_select() for further processing, or completion if
6754 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6755 ctsio->io_cont = ctl_do_mode_select;
6757 modepage_info = (union ctl_modepage_info *)
6758 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6760 memset(modepage_info, 0, sizeof(*modepage_info));
6762 len_left = param_len - header_size - bd_len;
6763 len_used = header_size + bd_len;
6765 modepage_info->header.len_left = len_left;
6766 modepage_info->header.len_used = len_used;
6768 return (ctl_do_mode_select((union ctl_io *)ctsio));
6772 ctl_mode_sense(struct ctl_scsiio *ctsio)
6774 struct ctl_lun *lun;
6775 int pc, page_code, dbd, llba, subpage;
6776 int alloc_len, page_len, header_len, total_len;
6777 struct scsi_mode_block_descr *block_desc;
6778 struct ctl_page_index *page_index;
6786 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6788 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6790 if (lun->be_lun->lun_type != T_DIRECT)
6795 if (lun->flags & CTL_LUN_PR_RESERVED) {
6799 * XXX KDM need a lock here.
6801 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6802 if ((lun->res_type == SPR_TYPE_EX_AC
6803 && residx != lun->pr_res_idx)
6804 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6805 || lun->res_type == SPR_TYPE_EX_AC_AR)
6806 && !lun->per_res[residx].registered)) {
6807 ctl_set_reservation_conflict(ctsio);
6808 ctl_done((union ctl_io *)ctsio);
6809 return (CTL_RETVAL_COMPLETE);
6813 switch (ctsio->cdb[0]) {
6814 case MODE_SENSE_6: {
6815 struct scsi_mode_sense_6 *cdb;
6817 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6819 header_len = sizeof(struct scsi_mode_hdr_6);
6820 if (cdb->byte2 & SMS_DBD)
6823 header_len += sizeof(struct scsi_mode_block_descr);
6825 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6826 page_code = cdb->page & SMS_PAGE_CODE;
6827 subpage = cdb->subpage;
6828 alloc_len = cdb->length;
6831 case MODE_SENSE_10: {
6832 struct scsi_mode_sense_10 *cdb;
6834 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6836 header_len = sizeof(struct scsi_mode_hdr_10);
6838 if (cdb->byte2 & SMS_DBD)
6841 header_len += sizeof(struct scsi_mode_block_descr);
6842 if (cdb->byte2 & SMS10_LLBAA)
6844 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6845 page_code = cdb->page & SMS_PAGE_CODE;
6846 subpage = cdb->subpage;
6847 alloc_len = scsi_2btoul(cdb->length);
6851 ctl_set_invalid_opcode(ctsio);
6852 ctl_done((union ctl_io *)ctsio);
6853 return (CTL_RETVAL_COMPLETE);
6854 break; /* NOTREACHED */
6858 * We have to make a first pass through to calculate the size of
6859 * the pages that match the user's query. Then we allocate enough
6860 * memory to hold it, and actually copy the data into the buffer.
6862 switch (page_code) {
6863 case SMS_ALL_PAGES_PAGE: {
6869 * At the moment, values other than 0 and 0xff here are
6870 * reserved according to SPC-3.
6872 if ((subpage != SMS_SUBPAGE_PAGE_0)
6873 && (subpage != SMS_SUBPAGE_ALL)) {
6874 ctl_set_invalid_field(ctsio,
6880 ctl_done((union ctl_io *)ctsio);
6881 return (CTL_RETVAL_COMPLETE);
6884 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6885 if ((control_dev != 0)
6886 && (lun->mode_pages.index[i].page_flags &
6887 CTL_PAGE_FLAG_DISK_ONLY))
6891 * We don't use this subpage if the user didn't
6892 * request all subpages.
6894 if ((lun->mode_pages.index[i].subpage != 0)
6895 && (subpage == SMS_SUBPAGE_PAGE_0))
6899 printf("found page %#x len %d\n",
6900 lun->mode_pages.index[i].page_code &
6902 lun->mode_pages.index[i].page_len);
6904 page_len += lun->mode_pages.index[i].page_len;
6913 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6914 /* Look for the right page code */
6915 if ((lun->mode_pages.index[i].page_code &
6916 SMPH_PC_MASK) != page_code)
6919 /* Look for the right subpage or the subpage wildcard*/
6920 if ((lun->mode_pages.index[i].subpage != subpage)
6921 && (subpage != SMS_SUBPAGE_ALL))
6924 /* Make sure the page is supported for this dev type */
6925 if ((control_dev != 0)
6926 && (lun->mode_pages.index[i].page_flags &
6927 CTL_PAGE_FLAG_DISK_ONLY))
6931 printf("found page %#x len %d\n",
6932 lun->mode_pages.index[i].page_code &
6934 lun->mode_pages.index[i].page_len);
6937 page_len += lun->mode_pages.index[i].page_len;
6940 if (page_len == 0) {
6941 ctl_set_invalid_field(ctsio,
6947 ctl_done((union ctl_io *)ctsio);
6948 return (CTL_RETVAL_COMPLETE);
6954 total_len = header_len + page_len;
6956 printf("header_len = %d, page_len = %d, total_len = %d\n",
6957 header_len, page_len, total_len);
6960 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6961 ctsio->kern_sg_entries = 0;
6962 ctsio->kern_data_resid = 0;
6963 ctsio->kern_rel_offset = 0;
6964 if (total_len < alloc_len) {
6965 ctsio->residual = alloc_len - total_len;
6966 ctsio->kern_data_len = total_len;
6967 ctsio->kern_total_len = total_len;
6969 ctsio->residual = 0;
6970 ctsio->kern_data_len = alloc_len;
6971 ctsio->kern_total_len = alloc_len;
6974 switch (ctsio->cdb[0]) {
6975 case MODE_SENSE_6: {
6976 struct scsi_mode_hdr_6 *header;
6978 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6980 header->datalen = ctl_min(total_len - 1, 254);
6983 header->block_descr_len = 0;
6985 header->block_descr_len =
6986 sizeof(struct scsi_mode_block_descr);
6987 block_desc = (struct scsi_mode_block_descr *)&header[1];
6990 case MODE_SENSE_10: {
6991 struct scsi_mode_hdr_10 *header;
6994 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6996 datalen = ctl_min(total_len - 2, 65533);
6997 scsi_ulto2b(datalen, header->datalen);
6999 scsi_ulto2b(0, header->block_descr_len);
7001 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
7002 header->block_descr_len);
7003 block_desc = (struct scsi_mode_block_descr *)&header[1];
7007 panic("invalid CDB type %#x", ctsio->cdb[0]);
7008 break; /* NOTREACHED */
7012 * If we've got a disk, use its blocksize in the block
7013 * descriptor. Otherwise, just set it to 0.
7016 if (control_dev != 0)
7017 scsi_ulto3b(lun->be_lun->blocksize,
7018 block_desc->block_len);
7020 scsi_ulto3b(0, block_desc->block_len);
7023 switch (page_code) {
7024 case SMS_ALL_PAGES_PAGE: {
7027 data_used = header_len;
7028 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7029 struct ctl_page_index *page_index;
7031 page_index = &lun->mode_pages.index[i];
7033 if ((control_dev != 0)
7034 && (page_index->page_flags &
7035 CTL_PAGE_FLAG_DISK_ONLY))
7039 * We don't use this subpage if the user didn't
7040 * request all subpages. We already checked (above)
7041 * to make sure the user only specified a subpage
7042 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
7044 if ((page_index->subpage != 0)
7045 && (subpage == SMS_SUBPAGE_PAGE_0))
7049 * Call the handler, if it exists, to update the
7050 * page to the latest values.
7052 if (page_index->sense_handler != NULL)
7053 page_index->sense_handler(ctsio, page_index,pc);
7055 memcpy(ctsio->kern_data_ptr + data_used,
7056 page_index->page_data +
7057 (page_index->page_len * pc),
7058 page_index->page_len);
7059 data_used += page_index->page_len;
7066 data_used = header_len;
7068 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7069 struct ctl_page_index *page_index;
7071 page_index = &lun->mode_pages.index[i];
7073 /* Look for the right page code */
7074 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
7077 /* Look for the right subpage or the subpage wildcard*/
7078 if ((page_index->subpage != subpage)
7079 && (subpage != SMS_SUBPAGE_ALL))
7082 /* Make sure the page is supported for this dev type */
7083 if ((control_dev != 0)
7084 && (page_index->page_flags &
7085 CTL_PAGE_FLAG_DISK_ONLY))
7089 * Call the handler, if it exists, to update the
7090 * page to the latest values.
7092 if (page_index->sense_handler != NULL)
7093 page_index->sense_handler(ctsio, page_index,pc);
7095 memcpy(ctsio->kern_data_ptr + data_used,
7096 page_index->page_data +
7097 (page_index->page_len * pc),
7098 page_index->page_len);
7099 data_used += page_index->page_len;
7105 ctsio->scsi_status = SCSI_STATUS_OK;
7107 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7108 ctsio->be_move_done = ctl_config_move_done;
7109 ctl_datamove((union ctl_io *)ctsio);
7111 return (CTL_RETVAL_COMPLETE);
7115 ctl_read_capacity(struct ctl_scsiio *ctsio)
7117 struct scsi_read_capacity *cdb;
7118 struct scsi_read_capacity_data *data;
7119 struct ctl_lun *lun;
7122 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7124 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7126 lba = scsi_4btoul(cdb->addr);
7127 if (((cdb->pmi & SRC_PMI) == 0)
7129 ctl_set_invalid_field(/*ctsio*/ ctsio,
7135 ctl_done((union ctl_io *)ctsio);
7136 return (CTL_RETVAL_COMPLETE);
7139 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7141 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7142 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7143 ctsio->residual = 0;
7144 ctsio->kern_data_len = sizeof(*data);
7145 ctsio->kern_total_len = sizeof(*data);
7146 ctsio->kern_data_resid = 0;
7147 ctsio->kern_rel_offset = 0;
7148 ctsio->kern_sg_entries = 0;
7151 * If the maximum LBA is greater than 0xfffffffe, the user must
7152 * issue a SERVICE ACTION IN (16) command, with the read capacity
7153 * serivce action set.
7155 if (lun->be_lun->maxlba > 0xfffffffe)
7156 scsi_ulto4b(0xffffffff, data->addr);
7158 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7161 * XXX KDM this may not be 512 bytes...
7163 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7165 ctsio->scsi_status = SCSI_STATUS_OK;
7167 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7168 ctsio->be_move_done = ctl_config_move_done;
7169 ctl_datamove((union ctl_io *)ctsio);
7171 return (CTL_RETVAL_COMPLETE);
7175 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7177 struct scsi_read_capacity_16 *cdb;
7178 struct scsi_read_capacity_data_long *data;
7179 struct ctl_lun *lun;
7183 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7185 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7187 alloc_len = scsi_4btoul(cdb->alloc_len);
7188 lba = scsi_8btou64(cdb->addr);
7190 if ((cdb->reladr & SRC16_PMI)
7192 ctl_set_invalid_field(/*ctsio*/ ctsio,
7198 ctl_done((union ctl_io *)ctsio);
7199 return (CTL_RETVAL_COMPLETE);
7202 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7204 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7205 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7207 if (sizeof(*data) < alloc_len) {
7208 ctsio->residual = alloc_len - sizeof(*data);
7209 ctsio->kern_data_len = sizeof(*data);
7210 ctsio->kern_total_len = sizeof(*data);
7212 ctsio->residual = 0;
7213 ctsio->kern_data_len = alloc_len;
7214 ctsio->kern_total_len = alloc_len;
7216 ctsio->kern_data_resid = 0;
7217 ctsio->kern_rel_offset = 0;
7218 ctsio->kern_sg_entries = 0;
7220 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7221 /* XXX KDM this may not be 512 bytes... */
7222 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7223 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7224 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7225 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7226 data->lalba_lbp[0] |= SRC16_LBPME;
7228 ctsio->scsi_status = SCSI_STATUS_OK;
7230 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7231 ctsio->be_move_done = ctl_config_move_done;
7232 ctl_datamove((union ctl_io *)ctsio);
7234 return (CTL_RETVAL_COMPLETE);
7238 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7240 struct scsi_maintenance_in *cdb;
7242 int alloc_len, ext, total_len = 0, g, p, pc, pg;
7243 int num_target_port_groups, num_target_ports, single;
7244 struct ctl_lun *lun;
7245 struct ctl_softc *softc;
7246 struct ctl_port *port;
7247 struct scsi_target_group_data *rtg_ptr;
7248 struct scsi_target_group_data_extended *rtg_ext_ptr;
7249 struct scsi_target_port_group_descriptor *tpg_desc;
7251 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7253 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7254 softc = control_softc;
7255 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7257 retval = CTL_RETVAL_COMPLETE;
7259 switch (cdb->byte2 & STG_PDF_MASK) {
7260 case STG_PDF_LENGTH:
7263 case STG_PDF_EXTENDED:
7267 ctl_set_invalid_field(/*ctsio*/ ctsio,
7273 ctl_done((union ctl_io *)ctsio);
7277 single = ctl_is_single;
7279 num_target_port_groups = 1;
7281 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7282 num_target_ports = 0;
7283 mtx_lock(&softc->ctl_lock);
7284 STAILQ_FOREACH(port, &softc->port_list, links) {
7285 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7287 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7291 mtx_unlock(&softc->ctl_lock);
7294 total_len = sizeof(struct scsi_target_group_data_extended);
7296 total_len = sizeof(struct scsi_target_group_data);
7297 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7298 num_target_port_groups +
7299 sizeof(struct scsi_target_port_descriptor) *
7300 num_target_ports * num_target_port_groups;
7302 alloc_len = scsi_4btoul(cdb->length);
7304 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7306 ctsio->kern_sg_entries = 0;
7308 if (total_len < alloc_len) {
7309 ctsio->residual = alloc_len - total_len;
7310 ctsio->kern_data_len = total_len;
7311 ctsio->kern_total_len = total_len;
7313 ctsio->residual = 0;
7314 ctsio->kern_data_len = alloc_len;
7315 ctsio->kern_total_len = alloc_len;
7317 ctsio->kern_data_resid = 0;
7318 ctsio->kern_rel_offset = 0;
7321 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7322 ctsio->kern_data_ptr;
7323 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7324 rtg_ext_ptr->format_type = 0x10;
7325 rtg_ext_ptr->implicit_transition_time = 0;
7326 tpg_desc = &rtg_ext_ptr->groups[0];
7328 rtg_ptr = (struct scsi_target_group_data *)
7329 ctsio->kern_data_ptr;
7330 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7331 tpg_desc = &rtg_ptr->groups[0];
7334 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS;
7335 mtx_lock(&softc->ctl_lock);
7336 for (g = 0; g < num_target_port_groups; g++) {
7338 tpg_desc->pref_state = TPG_PRIMARY |
7339 TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7341 tpg_desc->pref_state =
7342 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7343 tpg_desc->support = TPG_AO_SUP;
7345 tpg_desc->support |= TPG_AN_SUP;
7346 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7347 tpg_desc->status = TPG_IMPLICIT;
7349 STAILQ_FOREACH(port, &softc->port_list, links) {
7350 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7352 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7355 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7356 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7357 relative_target_port_identifier);
7360 tpg_desc->target_port_count = pc;
7361 tpg_desc = (struct scsi_target_port_group_descriptor *)
7362 &tpg_desc->descriptors[pc];
7364 mtx_unlock(&softc->ctl_lock);
7366 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7367 ctsio->be_move_done = ctl_config_move_done;
7369 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7370 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7371 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7372 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7373 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7375 ctl_datamove((union ctl_io *)ctsio);
7380 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7382 struct ctl_lun *lun;
7383 struct scsi_report_supported_opcodes *cdb;
7384 const struct ctl_cmd_entry *entry, *sentry;
7385 struct scsi_report_supported_opcodes_all *all;
7386 struct scsi_report_supported_opcodes_descr *descr;
7387 struct scsi_report_supported_opcodes_one *one;
7389 int alloc_len, total_len;
7390 int opcode, service_action, i, j, num;
7392 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7394 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7395 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7397 retval = CTL_RETVAL_COMPLETE;
7399 opcode = cdb->requested_opcode;
7400 service_action = scsi_2btoul(cdb->requested_service_action);
7401 switch (cdb->options & RSO_OPTIONS_MASK) {
7402 case RSO_OPTIONS_ALL:
7404 for (i = 0; i < 256; i++) {
7405 entry = &ctl_cmd_table[i];
7406 if (entry->flags & CTL_CMD_FLAG_SA5) {
7407 for (j = 0; j < 32; j++) {
7408 sentry = &((const struct ctl_cmd_entry *)
7410 if (ctl_cmd_applicable(
7411 lun->be_lun->lun_type, sentry))
7415 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7420 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7421 num * sizeof(struct scsi_report_supported_opcodes_descr);
7423 case RSO_OPTIONS_OC:
7424 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7425 ctl_set_invalid_field(/*ctsio*/ ctsio,
7431 ctl_done((union ctl_io *)ctsio);
7432 return (CTL_RETVAL_COMPLETE);
7434 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7436 case RSO_OPTIONS_OC_SA:
7437 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7438 service_action >= 32) {
7439 ctl_set_invalid_field(/*ctsio*/ ctsio,
7445 ctl_done((union ctl_io *)ctsio);
7446 return (CTL_RETVAL_COMPLETE);
7448 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7451 ctl_set_invalid_field(/*ctsio*/ ctsio,
7457 ctl_done((union ctl_io *)ctsio);
7458 return (CTL_RETVAL_COMPLETE);
7461 alloc_len = scsi_4btoul(cdb->length);
7463 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7465 ctsio->kern_sg_entries = 0;
7467 if (total_len < alloc_len) {
7468 ctsio->residual = alloc_len - total_len;
7469 ctsio->kern_data_len = total_len;
7470 ctsio->kern_total_len = total_len;
7472 ctsio->residual = 0;
7473 ctsio->kern_data_len = alloc_len;
7474 ctsio->kern_total_len = alloc_len;
7476 ctsio->kern_data_resid = 0;
7477 ctsio->kern_rel_offset = 0;
7479 switch (cdb->options & RSO_OPTIONS_MASK) {
7480 case RSO_OPTIONS_ALL:
7481 all = (struct scsi_report_supported_opcodes_all *)
7482 ctsio->kern_data_ptr;
7484 for (i = 0; i < 256; i++) {
7485 entry = &ctl_cmd_table[i];
7486 if (entry->flags & CTL_CMD_FLAG_SA5) {
7487 for (j = 0; j < 32; j++) {
7488 sentry = &((const struct ctl_cmd_entry *)
7490 if (!ctl_cmd_applicable(
7491 lun->be_lun->lun_type, sentry))
7493 descr = &all->descr[num++];
7495 scsi_ulto2b(j, descr->service_action);
7496 descr->flags = RSO_SERVACTV;
7497 scsi_ulto2b(sentry->length,
7501 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7504 descr = &all->descr[num++];
7506 scsi_ulto2b(0, descr->service_action);
7508 scsi_ulto2b(entry->length, descr->cdb_length);
7512 num * sizeof(struct scsi_report_supported_opcodes_descr),
7515 case RSO_OPTIONS_OC:
7516 one = (struct scsi_report_supported_opcodes_one *)
7517 ctsio->kern_data_ptr;
7518 entry = &ctl_cmd_table[opcode];
7520 case RSO_OPTIONS_OC_SA:
7521 one = (struct scsi_report_supported_opcodes_one *)
7522 ctsio->kern_data_ptr;
7523 entry = &ctl_cmd_table[opcode];
7524 entry = &((const struct ctl_cmd_entry *)
7525 entry->execute)[service_action];
7527 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7529 scsi_ulto2b(entry->length, one->cdb_length);
7530 one->cdb_usage[0] = opcode;
7531 memcpy(&one->cdb_usage[1], entry->usage,
7538 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7539 ctsio->be_move_done = ctl_config_move_done;
7541 ctl_datamove((union ctl_io *)ctsio);
7546 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7548 struct ctl_lun *lun;
7549 struct scsi_report_supported_tmf *cdb;
7550 struct scsi_report_supported_tmf_data *data;
7552 int alloc_len, total_len;
7554 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7556 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7557 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7559 retval = CTL_RETVAL_COMPLETE;
7561 total_len = sizeof(struct scsi_report_supported_tmf_data);
7562 alloc_len = scsi_4btoul(cdb->length);
7564 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7566 ctsio->kern_sg_entries = 0;
7568 if (total_len < alloc_len) {
7569 ctsio->residual = alloc_len - total_len;
7570 ctsio->kern_data_len = total_len;
7571 ctsio->kern_total_len = total_len;
7573 ctsio->residual = 0;
7574 ctsio->kern_data_len = alloc_len;
7575 ctsio->kern_total_len = alloc_len;
7577 ctsio->kern_data_resid = 0;
7578 ctsio->kern_rel_offset = 0;
7580 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7581 data->byte1 |= RST_ATS | RST_ATSS | RST_LURS | RST_TRS;
7582 data->byte2 |= RST_ITNRS;
7584 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7585 ctsio->be_move_done = ctl_config_move_done;
7587 ctl_datamove((union ctl_io *)ctsio);
7592 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7594 struct ctl_lun *lun;
7595 struct scsi_report_timestamp *cdb;
7596 struct scsi_report_timestamp_data *data;
7600 int alloc_len, total_len;
7602 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7604 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7605 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7607 retval = CTL_RETVAL_COMPLETE;
7609 total_len = sizeof(struct scsi_report_timestamp_data);
7610 alloc_len = scsi_4btoul(cdb->length);
7612 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7614 ctsio->kern_sg_entries = 0;
7616 if (total_len < alloc_len) {
7617 ctsio->residual = alloc_len - total_len;
7618 ctsio->kern_data_len = total_len;
7619 ctsio->kern_total_len = total_len;
7621 ctsio->residual = 0;
7622 ctsio->kern_data_len = alloc_len;
7623 ctsio->kern_total_len = alloc_len;
7625 ctsio->kern_data_resid = 0;
7626 ctsio->kern_rel_offset = 0;
7628 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7629 scsi_ulto2b(sizeof(*data) - 2, data->length);
7630 data->origin = RTS_ORIG_OUTSIDE;
7632 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7633 scsi_ulto4b(timestamp >> 16, data->timestamp);
7634 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7636 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7637 ctsio->be_move_done = ctl_config_move_done;
7639 ctl_datamove((union ctl_io *)ctsio);
7644 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7646 struct scsi_per_res_in *cdb;
7647 int alloc_len, total_len = 0;
7648 /* struct scsi_per_res_in_rsrv in_data; */
7649 struct ctl_lun *lun;
7650 struct ctl_softc *softc;
7652 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7654 softc = control_softc;
7656 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7658 alloc_len = scsi_2btoul(cdb->length);
7660 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7663 mtx_lock(&lun->lun_lock);
7664 switch (cdb->action) {
7665 case SPRI_RK: /* read keys */
7666 total_len = sizeof(struct scsi_per_res_in_keys) +
7668 sizeof(struct scsi_per_res_key);
7670 case SPRI_RR: /* read reservation */
7671 if (lun->flags & CTL_LUN_PR_RESERVED)
7672 total_len = sizeof(struct scsi_per_res_in_rsrv);
7674 total_len = sizeof(struct scsi_per_res_in_header);
7676 case SPRI_RC: /* report capabilities */
7677 total_len = sizeof(struct scsi_per_res_cap);
7679 case SPRI_RS: /* read full status */
7680 total_len = sizeof(struct scsi_per_res_in_header) +
7681 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7685 panic("Invalid PR type %x", cdb->action);
7687 mtx_unlock(&lun->lun_lock);
7689 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7691 if (total_len < alloc_len) {
7692 ctsio->residual = alloc_len - total_len;
7693 ctsio->kern_data_len = total_len;
7694 ctsio->kern_total_len = total_len;
7696 ctsio->residual = 0;
7697 ctsio->kern_data_len = alloc_len;
7698 ctsio->kern_total_len = alloc_len;
7701 ctsio->kern_data_resid = 0;
7702 ctsio->kern_rel_offset = 0;
7703 ctsio->kern_sg_entries = 0;
7705 mtx_lock(&lun->lun_lock);
7706 switch (cdb->action) {
7707 case SPRI_RK: { // read keys
7708 struct scsi_per_res_in_keys *res_keys;
7711 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7714 * We had to drop the lock to allocate our buffer, which
7715 * leaves time for someone to come in with another
7716 * persistent reservation. (That is unlikely, though,
7717 * since this should be the only persistent reservation
7718 * command active right now.)
7720 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7721 (lun->pr_key_count *
7722 sizeof(struct scsi_per_res_key)))){
7723 mtx_unlock(&lun->lun_lock);
7724 free(ctsio->kern_data_ptr, M_CTL);
7725 printf("%s: reservation length changed, retrying\n",
7730 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7732 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7733 lun->pr_key_count, res_keys->header.length);
7735 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7736 if (!lun->per_res[i].registered)
7740 * We used lun->pr_key_count to calculate the
7741 * size to allocate. If it turns out the number of
7742 * initiators with the registered flag set is
7743 * larger than that (i.e. they haven't been kept in
7744 * sync), we've got a problem.
7746 if (key_count >= lun->pr_key_count) {
7748 csevent_log(CSC_CTL | CSC_SHELF_SW |
7750 csevent_LogType_Fault,
7751 csevent_AlertLevel_Yellow,
7752 csevent_FRU_ShelfController,
7753 csevent_FRU_Firmware,
7754 csevent_FRU_Unknown,
7755 "registered keys %d >= key "
7756 "count %d", key_count,
7762 memcpy(res_keys->keys[key_count].key,
7763 lun->per_res[i].res_key.key,
7764 ctl_min(sizeof(res_keys->keys[key_count].key),
7765 sizeof(lun->per_res[i].res_key)));
7770 case SPRI_RR: { // read reservation
7771 struct scsi_per_res_in_rsrv *res;
7772 int tmp_len, header_only;
7774 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7776 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7778 if (lun->flags & CTL_LUN_PR_RESERVED)
7780 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7781 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7782 res->header.length);
7785 tmp_len = sizeof(struct scsi_per_res_in_header);
7786 scsi_ulto4b(0, res->header.length);
7791 * We had to drop the lock to allocate our buffer, which
7792 * leaves time for someone to come in with another
7793 * persistent reservation. (That is unlikely, though,
7794 * since this should be the only persistent reservation
7795 * command active right now.)
7797 if (tmp_len != total_len) {
7798 mtx_unlock(&lun->lun_lock);
7799 free(ctsio->kern_data_ptr, M_CTL);
7800 printf("%s: reservation status changed, retrying\n",
7806 * No reservation held, so we're done.
7808 if (header_only != 0)
7812 * If the registration is an All Registrants type, the key
7813 * is 0, since it doesn't really matter.
7815 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7816 memcpy(res->data.reservation,
7817 &lun->per_res[lun->pr_res_idx].res_key,
7818 sizeof(struct scsi_per_res_key));
7820 res->data.scopetype = lun->res_type;
7823 case SPRI_RC: //report capabilities
7825 struct scsi_per_res_cap *res_cap;
7828 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7829 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7830 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3;
7831 type_mask = SPRI_TM_WR_EX_AR |
7837 scsi_ulto2b(type_mask, res_cap->type_mask);
7840 case SPRI_RS: { // read full status
7841 struct scsi_per_res_in_full *res_status;
7842 struct scsi_per_res_in_full_desc *res_desc;
7843 struct ctl_port *port;
7846 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7849 * We had to drop the lock to allocate our buffer, which
7850 * leaves time for someone to come in with another
7851 * persistent reservation. (That is unlikely, though,
7852 * since this should be the only persistent reservation
7853 * command active right now.)
7855 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7856 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7857 lun->pr_key_count)){
7858 mtx_unlock(&lun->lun_lock);
7859 free(ctsio->kern_data_ptr, M_CTL);
7860 printf("%s: reservation length changed, retrying\n",
7865 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7867 res_desc = &res_status->desc[0];
7868 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7869 if (!lun->per_res[i].registered)
7872 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key,
7873 sizeof(res_desc->res_key));
7874 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7875 (lun->pr_res_idx == i ||
7876 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7877 res_desc->flags = SPRI_FULL_R_HOLDER;
7878 res_desc->scopetype = lun->res_type;
7880 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7881 res_desc->rel_trgt_port_id);
7883 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7885 len = ctl_create_iid(port,
7886 i % CTL_MAX_INIT_PER_PORT,
7887 res_desc->transport_id);
7888 scsi_ulto4b(len, res_desc->additional_length);
7889 res_desc = (struct scsi_per_res_in_full_desc *)
7890 &res_desc->transport_id[len];
7892 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7893 res_status->header.length);
7898 * This is a bug, because we just checked for this above,
7899 * and should have returned an error.
7901 panic("Invalid PR type %x", cdb->action);
7902 break; /* NOTREACHED */
7904 mtx_unlock(&lun->lun_lock);
7906 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7907 ctsio->be_move_done = ctl_config_move_done;
7909 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7910 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7911 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7912 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7913 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7915 ctl_datamove((union ctl_io *)ctsio);
7917 return (CTL_RETVAL_COMPLETE);
7921 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7925 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7926 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7927 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7928 struct scsi_per_res_out_parms* param)
7930 union ctl_ha_msg persis_io;
7936 mtx_lock(&lun->lun_lock);
7937 if (sa_res_key == 0) {
7938 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7939 /* validate scope and type */
7940 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7942 mtx_unlock(&lun->lun_lock);
7943 ctl_set_invalid_field(/*ctsio*/ ctsio,
7949 ctl_done((union ctl_io *)ctsio);
7953 if (type>8 || type==2 || type==4 || type==0) {
7954 mtx_unlock(&lun->lun_lock);
7955 ctl_set_invalid_field(/*ctsio*/ ctsio,
7961 ctl_done((union ctl_io *)ctsio);
7965 /* temporarily unregister this nexus */
7966 lun->per_res[residx].registered = 0;
7969 * Unregister everybody else and build UA for
7972 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
7973 if (lun->per_res[i].registered == 0)
7977 && i <CTL_MAX_INITIATORS)
7978 lun->pending_sense[i].ua_pending |=
7980 else if (persis_offset
7981 && i >= persis_offset)
7982 lun->pending_sense[i-persis_offset
7985 lun->per_res[i].registered = 0;
7986 memset(&lun->per_res[i].res_key, 0,
7987 sizeof(struct scsi_per_res_key));
7989 lun->per_res[residx].registered = 1;
7990 lun->pr_key_count = 1;
7991 lun->res_type = type;
7992 if (lun->res_type != SPR_TYPE_WR_EX_AR
7993 && lun->res_type != SPR_TYPE_EX_AC_AR)
7994 lun->pr_res_idx = residx;
7996 /* send msg to other side */
7997 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7998 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7999 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8000 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8001 persis_io.pr.pr_info.res_type = type;
8002 memcpy(persis_io.pr.pr_info.sa_res_key,
8003 param->serv_act_res_key,
8004 sizeof(param->serv_act_res_key));
8005 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8006 &persis_io, sizeof(persis_io), 0)) >
8007 CTL_HA_STATUS_SUCCESS) {
8008 printf("CTL:Persis Out error returned "
8009 "from ctl_ha_msg_send %d\n",
8013 /* not all registrants */
8014 mtx_unlock(&lun->lun_lock);
8015 free(ctsio->kern_data_ptr, M_CTL);
8016 ctl_set_invalid_field(ctsio,
8022 ctl_done((union ctl_io *)ctsio);
8025 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8026 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8029 if (res_key == sa_res_key) {
8032 * The spec implies this is not good but doesn't
8033 * say what to do. There are two choices either
8034 * generate a res conflict or check condition
8035 * with illegal field in parameter data. Since
8036 * that is what is done when the sa_res_key is
8037 * zero I'll take that approach since this has
8038 * to do with the sa_res_key.
8040 mtx_unlock(&lun->lun_lock);
8041 free(ctsio->kern_data_ptr, M_CTL);
8042 ctl_set_invalid_field(ctsio,
8048 ctl_done((union ctl_io *)ctsio);
8052 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8053 if (lun->per_res[i].registered
8054 && memcmp(param->serv_act_res_key,
8055 lun->per_res[i].res_key.key,
8056 sizeof(struct scsi_per_res_key)) != 0)
8060 lun->per_res[i].registered = 0;
8061 memset(&lun->per_res[i].res_key, 0,
8062 sizeof(struct scsi_per_res_key));
8063 lun->pr_key_count--;
8066 && i < CTL_MAX_INITIATORS)
8067 lun->pending_sense[i].ua_pending |=
8069 else if (persis_offset
8070 && i >= persis_offset)
8071 lun->pending_sense[i-persis_offset].ua_pending|=
8075 mtx_unlock(&lun->lun_lock);
8076 free(ctsio->kern_data_ptr, M_CTL);
8077 ctl_set_reservation_conflict(ctsio);
8078 ctl_done((union ctl_io *)ctsio);
8079 return (CTL_RETVAL_COMPLETE);
8081 /* send msg to other side */
8082 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8083 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8084 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8085 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8086 persis_io.pr.pr_info.res_type = type;
8087 memcpy(persis_io.pr.pr_info.sa_res_key,
8088 param->serv_act_res_key,
8089 sizeof(param->serv_act_res_key));
8090 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8091 &persis_io, sizeof(persis_io), 0)) >
8092 CTL_HA_STATUS_SUCCESS) {
8093 printf("CTL:Persis Out error returned from "
8094 "ctl_ha_msg_send %d\n", isc_retval);
8097 /* Reserved but not all registrants */
8098 /* sa_res_key is res holder */
8099 if (memcmp(param->serv_act_res_key,
8100 lun->per_res[lun->pr_res_idx].res_key.key,
8101 sizeof(struct scsi_per_res_key)) == 0) {
8102 /* validate scope and type */
8103 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8105 mtx_unlock(&lun->lun_lock);
8106 ctl_set_invalid_field(/*ctsio*/ ctsio,
8112 ctl_done((union ctl_io *)ctsio);
8116 if (type>8 || type==2 || type==4 || type==0) {
8117 mtx_unlock(&lun->lun_lock);
8118 ctl_set_invalid_field(/*ctsio*/ ctsio,
8124 ctl_done((union ctl_io *)ctsio);
8130 * if sa_res_key != res_key remove all
8131 * registrants w/sa_res_key and generate UA
8132 * for these registrants(Registrations
8133 * Preempted) if it wasn't an exclusive
8134 * reservation generate UA(Reservations
8135 * Preempted) for all other registered nexuses
8136 * if the type has changed. Establish the new
8137 * reservation and holder. If res_key and
8138 * sa_res_key are the same do the above
8139 * except don't unregister the res holder.
8143 * Temporarily unregister so it won't get
8144 * removed or UA generated
8146 lun->per_res[residx].registered = 0;
8147 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8148 if (lun->per_res[i].registered == 0)
8151 if (memcmp(param->serv_act_res_key,
8152 lun->per_res[i].res_key.key,
8153 sizeof(struct scsi_per_res_key)) == 0) {
8154 lun->per_res[i].registered = 0;
8155 memset(&lun->per_res[i].res_key,
8157 sizeof(struct scsi_per_res_key));
8158 lun->pr_key_count--;
8161 && i < CTL_MAX_INITIATORS)
8162 lun->pending_sense[i
8165 else if (persis_offset
8166 && i >= persis_offset)
8168 i-persis_offset].ua_pending |=
8170 } else if (type != lun->res_type
8171 && (lun->res_type == SPR_TYPE_WR_EX_RO
8172 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8174 && i < CTL_MAX_INITIATORS)
8175 lun->pending_sense[i
8178 else if (persis_offset
8179 && i >= persis_offset)
8186 lun->per_res[residx].registered = 1;
8187 lun->res_type = type;
8188 if (lun->res_type != SPR_TYPE_WR_EX_AR
8189 && lun->res_type != SPR_TYPE_EX_AC_AR)
8190 lun->pr_res_idx = residx;
8193 CTL_PR_ALL_REGISTRANTS;
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_PREEMPT;
8198 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8199 persis_io.pr.pr_info.res_type = type;
8200 memcpy(persis_io.pr.pr_info.sa_res_key,
8201 param->serv_act_res_key,
8202 sizeof(param->serv_act_res_key));
8203 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8204 &persis_io, sizeof(persis_io), 0)) >
8205 CTL_HA_STATUS_SUCCESS) {
8206 printf("CTL:Persis Out error returned "
8207 "from ctl_ha_msg_send %d\n",
8212 * sa_res_key is not the res holder just
8213 * remove registrants
8217 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8218 if (memcmp(param->serv_act_res_key,
8219 lun->per_res[i].res_key.key,
8220 sizeof(struct scsi_per_res_key)) != 0)
8224 lun->per_res[i].registered = 0;
8225 memset(&lun->per_res[i].res_key, 0,
8226 sizeof(struct scsi_per_res_key));
8227 lun->pr_key_count--;
8230 && i < CTL_MAX_INITIATORS)
8231 lun->pending_sense[i].ua_pending |=
8233 else if (persis_offset
8234 && i >= persis_offset)
8236 i-persis_offset].ua_pending |=
8241 mtx_unlock(&lun->lun_lock);
8242 free(ctsio->kern_data_ptr, M_CTL);
8243 ctl_set_reservation_conflict(ctsio);
8244 ctl_done((union ctl_io *)ctsio);
8247 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8248 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8249 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8250 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8251 persis_io.pr.pr_info.res_type = type;
8252 memcpy(persis_io.pr.pr_info.sa_res_key,
8253 param->serv_act_res_key,
8254 sizeof(param->serv_act_res_key));
8255 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8256 &persis_io, sizeof(persis_io), 0)) >
8257 CTL_HA_STATUS_SUCCESS) {
8258 printf("CTL:Persis Out error returned "
8259 "from ctl_ha_msg_send %d\n",
8265 lun->PRGeneration++;
8266 mtx_unlock(&lun->lun_lock);
8272 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8276 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8277 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8278 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
8279 msg->pr.pr_info.sa_res_key,
8280 sizeof(struct scsi_per_res_key)) != 0) {
8281 uint64_t sa_res_key;
8282 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8284 if (sa_res_key == 0) {
8285 /* temporarily unregister this nexus */
8286 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8289 * Unregister everybody else and build UA for
8292 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8293 if (lun->per_res[i].registered == 0)
8297 && i < CTL_MAX_INITIATORS)
8298 lun->pending_sense[i].ua_pending |=
8300 else if (persis_offset && i >= persis_offset)
8301 lun->pending_sense[i -
8302 persis_offset].ua_pending |=
8304 lun->per_res[i].registered = 0;
8305 memset(&lun->per_res[i].res_key, 0,
8306 sizeof(struct scsi_per_res_key));
8309 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8310 lun->pr_key_count = 1;
8311 lun->res_type = msg->pr.pr_info.res_type;
8312 if (lun->res_type != SPR_TYPE_WR_EX_AR
8313 && lun->res_type != SPR_TYPE_EX_AC_AR)
8314 lun->pr_res_idx = msg->pr.pr_info.residx;
8316 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8317 if (memcmp(msg->pr.pr_info.sa_res_key,
8318 lun->per_res[i].res_key.key,
8319 sizeof(struct scsi_per_res_key)) != 0)
8322 lun->per_res[i].registered = 0;
8323 memset(&lun->per_res[i].res_key, 0,
8324 sizeof(struct scsi_per_res_key));
8325 lun->pr_key_count--;
8328 && i < persis_offset)
8329 lun->pending_sense[i].ua_pending |=
8331 else if (persis_offset
8332 && i >= persis_offset)
8333 lun->pending_sense[i -
8334 persis_offset].ua_pending |=
8340 * Temporarily unregister so it won't get removed
8343 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8344 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8345 if (lun->per_res[i].registered == 0)
8348 if (memcmp(msg->pr.pr_info.sa_res_key,
8349 lun->per_res[i].res_key.key,
8350 sizeof(struct scsi_per_res_key)) == 0) {
8351 lun->per_res[i].registered = 0;
8352 memset(&lun->per_res[i].res_key, 0,
8353 sizeof(struct scsi_per_res_key));
8354 lun->pr_key_count--;
8356 && i < CTL_MAX_INITIATORS)
8357 lun->pending_sense[i].ua_pending |=
8359 else if (persis_offset
8360 && i >= persis_offset)
8361 lun->pending_sense[i -
8362 persis_offset].ua_pending |=
8364 } else if (msg->pr.pr_info.res_type != lun->res_type
8365 && (lun->res_type == SPR_TYPE_WR_EX_RO
8366 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8368 && i < persis_offset)
8369 lun->pending_sense[i
8372 else if (persis_offset
8373 && i >= persis_offset)
8374 lun->pending_sense[i -
8375 persis_offset].ua_pending |=
8379 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8380 lun->res_type = msg->pr.pr_info.res_type;
8381 if (lun->res_type != SPR_TYPE_WR_EX_AR
8382 && lun->res_type != SPR_TYPE_EX_AC_AR)
8383 lun->pr_res_idx = msg->pr.pr_info.residx;
8385 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8387 lun->PRGeneration++;
8393 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8397 u_int32_t param_len;
8398 struct scsi_per_res_out *cdb;
8399 struct ctl_lun *lun;
8400 struct scsi_per_res_out_parms* param;
8401 struct ctl_softc *softc;
8403 uint64_t res_key, sa_res_key;
8405 union ctl_ha_msg persis_io;
8408 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8410 retval = CTL_RETVAL_COMPLETE;
8412 softc = control_softc;
8414 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8415 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8418 * We only support whole-LUN scope. The scope & type are ignored for
8419 * register, register and ignore existing key and clear.
8420 * We sometimes ignore scope and type on preempts too!!
8421 * Verify reservation type here as well.
8423 type = cdb->scope_type & SPR_TYPE_MASK;
8424 if ((cdb->action == SPRO_RESERVE)
8425 || (cdb->action == SPRO_RELEASE)) {
8426 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8427 ctl_set_invalid_field(/*ctsio*/ ctsio,
8433 ctl_done((union ctl_io *)ctsio);
8434 return (CTL_RETVAL_COMPLETE);
8437 if (type>8 || type==2 || type==4 || type==0) {
8438 ctl_set_invalid_field(/*ctsio*/ ctsio,
8444 ctl_done((union ctl_io *)ctsio);
8445 return (CTL_RETVAL_COMPLETE);
8449 param_len = scsi_4btoul(cdb->length);
8451 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8452 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8453 ctsio->kern_data_len = param_len;
8454 ctsio->kern_total_len = param_len;
8455 ctsio->kern_data_resid = 0;
8456 ctsio->kern_rel_offset = 0;
8457 ctsio->kern_sg_entries = 0;
8458 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8459 ctsio->be_move_done = ctl_config_move_done;
8460 ctl_datamove((union ctl_io *)ctsio);
8462 return (CTL_RETVAL_COMPLETE);
8465 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8467 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8468 res_key = scsi_8btou64(param->res_key.key);
8469 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8472 * Validate the reservation key here except for SPRO_REG_IGNO
8473 * This must be done for all other service actions
8475 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8476 mtx_lock(&lun->lun_lock);
8477 if (lun->per_res[residx].registered) {
8478 if (memcmp(param->res_key.key,
8479 lun->per_res[residx].res_key.key,
8480 ctl_min(sizeof(param->res_key),
8481 sizeof(lun->per_res[residx].res_key))) != 0) {
8483 * The current key passed in doesn't match
8484 * the one the initiator previously
8487 mtx_unlock(&lun->lun_lock);
8488 free(ctsio->kern_data_ptr, M_CTL);
8489 ctl_set_reservation_conflict(ctsio);
8490 ctl_done((union ctl_io *)ctsio);
8491 return (CTL_RETVAL_COMPLETE);
8493 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8495 * We are not registered
8497 mtx_unlock(&lun->lun_lock);
8498 free(ctsio->kern_data_ptr, M_CTL);
8499 ctl_set_reservation_conflict(ctsio);
8500 ctl_done((union ctl_io *)ctsio);
8501 return (CTL_RETVAL_COMPLETE);
8502 } else if (res_key != 0) {
8504 * We are not registered and trying to register but
8505 * the register key isn't zero.
8507 mtx_unlock(&lun->lun_lock);
8508 free(ctsio->kern_data_ptr, M_CTL);
8509 ctl_set_reservation_conflict(ctsio);
8510 ctl_done((union ctl_io *)ctsio);
8511 return (CTL_RETVAL_COMPLETE);
8513 mtx_unlock(&lun->lun_lock);
8516 switch (cdb->action & SPRO_ACTION_MASK) {
8518 case SPRO_REG_IGNO: {
8521 printf("Registration received\n");
8525 * We don't support any of these options, as we report in
8526 * the read capabilities request (see
8527 * ctl_persistent_reserve_in(), above).
8529 if ((param->flags & SPR_SPEC_I_PT)
8530 || (param->flags & SPR_ALL_TG_PT)
8531 || (param->flags & SPR_APTPL)) {
8534 if (param->flags & SPR_APTPL)
8536 else if (param->flags & SPR_ALL_TG_PT)
8538 else /* SPR_SPEC_I_PT */
8541 free(ctsio->kern_data_ptr, M_CTL);
8542 ctl_set_invalid_field(ctsio,
8548 ctl_done((union ctl_io *)ctsio);
8549 return (CTL_RETVAL_COMPLETE);
8552 mtx_lock(&lun->lun_lock);
8555 * The initiator wants to clear the
8558 if (sa_res_key == 0) {
8560 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8561 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8562 && !lun->per_res[residx].registered)) {
8563 mtx_unlock(&lun->lun_lock);
8567 lun->per_res[residx].registered = 0;
8568 memset(&lun->per_res[residx].res_key,
8569 0, sizeof(lun->per_res[residx].res_key));
8570 lun->pr_key_count--;
8572 if (residx == lun->pr_res_idx) {
8573 lun->flags &= ~CTL_LUN_PR_RESERVED;
8574 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8576 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8577 || lun->res_type == SPR_TYPE_EX_AC_RO)
8578 && lun->pr_key_count) {
8580 * If the reservation is a registrants
8581 * only type we need to generate a UA
8582 * for other registered inits. The
8583 * sense code should be RESERVATIONS
8587 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8589 i+persis_offset].registered
8592 lun->pending_sense[i
8598 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8599 if (lun->pr_key_count==0) {
8600 lun->flags &= ~CTL_LUN_PR_RESERVED;
8602 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8605 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8606 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8607 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8608 persis_io.pr.pr_info.residx = residx;
8609 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8610 &persis_io, sizeof(persis_io), 0 )) >
8611 CTL_HA_STATUS_SUCCESS) {
8612 printf("CTL:Persis Out error returned from "
8613 "ctl_ha_msg_send %d\n", isc_retval);
8615 } else /* sa_res_key != 0 */ {
8618 * If we aren't registered currently then increment
8619 * the key count and set the registered flag.
8621 if (!lun->per_res[residx].registered) {
8622 lun->pr_key_count++;
8623 lun->per_res[residx].registered = 1;
8626 memcpy(&lun->per_res[residx].res_key,
8627 param->serv_act_res_key,
8628 ctl_min(sizeof(param->serv_act_res_key),
8629 sizeof(lun->per_res[residx].res_key)));
8631 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8632 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8633 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8634 persis_io.pr.pr_info.residx = residx;
8635 memcpy(persis_io.pr.pr_info.sa_res_key,
8636 param->serv_act_res_key,
8637 sizeof(param->serv_act_res_key));
8638 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8639 &persis_io, sizeof(persis_io), 0)) >
8640 CTL_HA_STATUS_SUCCESS) {
8641 printf("CTL:Persis Out error returned from "
8642 "ctl_ha_msg_send %d\n", isc_retval);
8645 lun->PRGeneration++;
8646 mtx_unlock(&lun->lun_lock);
8652 printf("Reserve executed type %d\n", type);
8654 mtx_lock(&lun->lun_lock);
8655 if (lun->flags & CTL_LUN_PR_RESERVED) {
8657 * if this isn't the reservation holder and it's
8658 * not a "all registrants" type or if the type is
8659 * different then we have a conflict
8661 if ((lun->pr_res_idx != residx
8662 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8663 || lun->res_type != type) {
8664 mtx_unlock(&lun->lun_lock);
8665 free(ctsio->kern_data_ptr, M_CTL);
8666 ctl_set_reservation_conflict(ctsio);
8667 ctl_done((union ctl_io *)ctsio);
8668 return (CTL_RETVAL_COMPLETE);
8670 mtx_unlock(&lun->lun_lock);
8671 } else /* create a reservation */ {
8673 * If it's not an "all registrants" type record
8674 * reservation holder
8676 if (type != SPR_TYPE_WR_EX_AR
8677 && type != SPR_TYPE_EX_AC_AR)
8678 lun->pr_res_idx = residx; /* Res holder */
8680 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8682 lun->flags |= CTL_LUN_PR_RESERVED;
8683 lun->res_type = type;
8685 mtx_unlock(&lun->lun_lock);
8687 /* send msg to other side */
8688 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8689 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8690 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8691 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8692 persis_io.pr.pr_info.res_type = type;
8693 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8694 &persis_io, sizeof(persis_io), 0)) >
8695 CTL_HA_STATUS_SUCCESS) {
8696 printf("CTL:Persis Out error returned from "
8697 "ctl_ha_msg_send %d\n", isc_retval);
8703 mtx_lock(&lun->lun_lock);
8704 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8705 /* No reservation exists return good status */
8706 mtx_unlock(&lun->lun_lock);
8710 * Is this nexus a reservation holder?
8712 if (lun->pr_res_idx != residx
8713 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8715 * not a res holder return good status but
8718 mtx_unlock(&lun->lun_lock);
8722 if (lun->res_type != type) {
8723 mtx_unlock(&lun->lun_lock);
8724 free(ctsio->kern_data_ptr, M_CTL);
8725 ctl_set_illegal_pr_release(ctsio);
8726 ctl_done((union ctl_io *)ctsio);
8727 return (CTL_RETVAL_COMPLETE);
8730 /* okay to release */
8731 lun->flags &= ~CTL_LUN_PR_RESERVED;
8732 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8736 * if this isn't an exclusive access
8737 * res generate UA for all other
8740 if (type != SPR_TYPE_EX_AC
8741 && type != SPR_TYPE_WR_EX) {
8743 * temporarily unregister so we don't generate UA
8745 lun->per_res[residx].registered = 0;
8747 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8748 if (lun->per_res[i+persis_offset].registered
8751 lun->pending_sense[i].ua_pending |=
8755 lun->per_res[residx].registered = 1;
8757 mtx_unlock(&lun->lun_lock);
8758 /* Send msg to other side */
8759 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8760 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8761 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8762 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8763 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8764 printf("CTL:Persis Out error returned from "
8765 "ctl_ha_msg_send %d\n", isc_retval);
8770 /* send msg to other side */
8772 mtx_lock(&lun->lun_lock);
8773 lun->flags &= ~CTL_LUN_PR_RESERVED;
8775 lun->pr_key_count = 0;
8776 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8779 memset(&lun->per_res[residx].res_key,
8780 0, sizeof(lun->per_res[residx].res_key));
8781 lun->per_res[residx].registered = 0;
8783 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8784 if (lun->per_res[i].registered) {
8785 if (!persis_offset && i < CTL_MAX_INITIATORS)
8786 lun->pending_sense[i].ua_pending |=
8788 else if (persis_offset && i >= persis_offset)
8789 lun->pending_sense[i-persis_offset
8790 ].ua_pending |= CTL_UA_RES_PREEMPT;
8792 memset(&lun->per_res[i].res_key,
8793 0, sizeof(struct scsi_per_res_key));
8794 lun->per_res[i].registered = 0;
8796 lun->PRGeneration++;
8797 mtx_unlock(&lun->lun_lock);
8798 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8799 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8800 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8801 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8802 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8803 printf("CTL:Persis Out error returned from "
8804 "ctl_ha_msg_send %d\n", isc_retval);
8808 case SPRO_PREEMPT: {
8811 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8812 residx, ctsio, cdb, param);
8814 return (CTL_RETVAL_COMPLETE);
8818 panic("Invalid PR type %x", cdb->action);
8822 free(ctsio->kern_data_ptr, M_CTL);
8823 ctl_set_success(ctsio);
8824 ctl_done((union ctl_io *)ctsio);
8830 * This routine is for handling a message from the other SC pertaining to
8831 * persistent reserve out. All the error checking will have been done
8832 * so only perorming the action need be done here to keep the two
8836 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8838 struct ctl_lun *lun;
8839 struct ctl_softc *softc;
8843 softc = control_softc;
8845 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8846 lun = softc->ctl_luns[targ_lun];
8847 mtx_lock(&lun->lun_lock);
8848 switch(msg->pr.pr_info.action) {
8849 case CTL_PR_REG_KEY:
8850 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8851 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8852 lun->pr_key_count++;
8854 lun->PRGeneration++;
8855 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8856 msg->pr.pr_info.sa_res_key,
8857 sizeof(struct scsi_per_res_key));
8860 case CTL_PR_UNREG_KEY:
8861 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8862 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8863 0, sizeof(struct scsi_per_res_key));
8864 lun->pr_key_count--;
8866 /* XXX Need to see if the reservation has been released */
8867 /* if so do we need to generate UA? */
8868 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8869 lun->flags &= ~CTL_LUN_PR_RESERVED;
8870 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8872 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8873 || lun->res_type == SPR_TYPE_EX_AC_RO)
8874 && lun->pr_key_count) {
8876 * If the reservation is a registrants
8877 * only type we need to generate a UA
8878 * for other registered inits. The
8879 * sense code should be RESERVATIONS
8883 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8885 persis_offset].registered == 0)
8888 lun->pending_sense[i
8894 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8895 if (lun->pr_key_count==0) {
8896 lun->flags &= ~CTL_LUN_PR_RESERVED;
8898 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8901 lun->PRGeneration++;
8904 case CTL_PR_RESERVE:
8905 lun->flags |= CTL_LUN_PR_RESERVED;
8906 lun->res_type = msg->pr.pr_info.res_type;
8907 lun->pr_res_idx = msg->pr.pr_info.residx;
8911 case CTL_PR_RELEASE:
8913 * if this isn't an exclusive access res generate UA for all
8914 * other registrants.
8916 if (lun->res_type != SPR_TYPE_EX_AC
8917 && lun->res_type != SPR_TYPE_WR_EX) {
8918 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8919 if (lun->per_res[i+persis_offset].registered)
8920 lun->pending_sense[i].ua_pending |=
8924 lun->flags &= ~CTL_LUN_PR_RESERVED;
8925 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8929 case CTL_PR_PREEMPT:
8930 ctl_pro_preempt_other(lun, msg);
8933 lun->flags &= ~CTL_LUN_PR_RESERVED;
8935 lun->pr_key_count = 0;
8936 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8938 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8939 if (lun->per_res[i].registered == 0)
8942 && i < CTL_MAX_INITIATORS)
8943 lun->pending_sense[i].ua_pending |=
8945 else if (persis_offset
8946 && i >= persis_offset)
8947 lun->pending_sense[i-persis_offset].ua_pending|=
8949 memset(&lun->per_res[i].res_key, 0,
8950 sizeof(struct scsi_per_res_key));
8951 lun->per_res[i].registered = 0;
8953 lun->PRGeneration++;
8957 mtx_unlock(&lun->lun_lock);
8961 ctl_read_write(struct ctl_scsiio *ctsio)
8963 struct ctl_lun *lun;
8964 struct ctl_lba_len_flags *lbalen;
8966 uint32_t num_blocks;
8971 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8973 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8978 retval = CTL_RETVAL_COMPLETE;
8980 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8981 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8982 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
8986 * XXX KDM need a lock here.
8988 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8989 if ((lun->res_type == SPR_TYPE_EX_AC
8990 && residx != lun->pr_res_idx)
8991 || ((lun->res_type == SPR_TYPE_EX_AC_RO
8992 || lun->res_type == SPR_TYPE_EX_AC_AR)
8993 && !lun->per_res[residx].registered)) {
8994 ctl_set_reservation_conflict(ctsio);
8995 ctl_done((union ctl_io *)ctsio);
8996 return (CTL_RETVAL_COMPLETE);
9000 switch (ctsio->cdb[0]) {
9003 struct scsi_rw_6 *cdb;
9005 cdb = (struct scsi_rw_6 *)ctsio->cdb;
9007 lba = scsi_3btoul(cdb->addr);
9008 /* only 5 bits are valid in the most significant address byte */
9010 num_blocks = cdb->length;
9012 * This is correct according to SBC-2.
9014 if (num_blocks == 0)
9020 struct scsi_rw_10 *cdb;
9022 cdb = (struct scsi_rw_10 *)ctsio->cdb;
9024 if (cdb->byte2 & SRW10_FUA)
9026 if (cdb->byte2 & SRW10_DPO)
9029 lba = scsi_4btoul(cdb->addr);
9030 num_blocks = scsi_2btoul(cdb->length);
9033 case WRITE_VERIFY_10: {
9034 struct scsi_write_verify_10 *cdb;
9036 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
9039 * XXX KDM we should do actual write verify support at some
9040 * point. This is obviously fake, we're just translating
9041 * things to a write. So we don't even bother checking the
9042 * BYTCHK field, since we don't do any verification. If
9043 * the user asks for it, we'll just pretend we did it.
9045 if (cdb->byte2 & SWV_DPO)
9048 lba = scsi_4btoul(cdb->addr);
9049 num_blocks = scsi_2btoul(cdb->length);
9054 struct scsi_rw_12 *cdb;
9056 cdb = (struct scsi_rw_12 *)ctsio->cdb;
9058 if (cdb->byte2 & SRW12_FUA)
9060 if (cdb->byte2 & SRW12_DPO)
9062 lba = scsi_4btoul(cdb->addr);
9063 num_blocks = scsi_4btoul(cdb->length);
9066 case WRITE_VERIFY_12: {
9067 struct scsi_write_verify_12 *cdb;
9069 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
9071 if (cdb->byte2 & SWV_DPO)
9074 lba = scsi_4btoul(cdb->addr);
9075 num_blocks = scsi_4btoul(cdb->length);
9081 struct scsi_rw_16 *cdb;
9083 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9085 if (cdb->byte2 & SRW12_FUA)
9087 if (cdb->byte2 & SRW12_DPO)
9090 lba = scsi_8btou64(cdb->addr);
9091 num_blocks = scsi_4btoul(cdb->length);
9094 case WRITE_VERIFY_16: {
9095 struct scsi_write_verify_16 *cdb;
9097 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
9099 if (cdb->byte2 & SWV_DPO)
9102 lba = scsi_8btou64(cdb->addr);
9103 num_blocks = scsi_4btoul(cdb->length);
9108 * We got a command we don't support. This shouldn't
9109 * happen, commands should be filtered out above us.
9111 ctl_set_invalid_opcode(ctsio);
9112 ctl_done((union ctl_io *)ctsio);
9114 return (CTL_RETVAL_COMPLETE);
9115 break; /* NOTREACHED */
9119 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
9120 * interesting for us, but if RAIDCore is in write-back mode,
9121 * getting it to do write-through for a particular transaction may
9126 * The first check is to make sure we're in bounds, the second
9127 * check is to catch wrap-around problems. If the lba + num blocks
9128 * is less than the lba, then we've wrapped around and the block
9129 * range is invalid anyway.
9131 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9132 || ((lba + num_blocks) < lba)) {
9133 ctl_set_lba_out_of_range(ctsio);
9134 ctl_done((union ctl_io *)ctsio);
9135 return (CTL_RETVAL_COMPLETE);
9139 * According to SBC-3, a transfer length of 0 is not an error.
9140 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9141 * translates to 256 blocks for those commands.
9143 if (num_blocks == 0) {
9144 ctl_set_success(ctsio);
9145 ctl_done((union ctl_io *)ctsio);
9146 return (CTL_RETVAL_COMPLETE);
9149 lbalen = (struct ctl_lba_len_flags *)
9150 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9152 lbalen->len = num_blocks;
9153 lbalen->flags = isread ? CTL_LLF_READ : CTL_LLF_WRITE;
9155 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9156 ctsio->kern_rel_offset = 0;
9158 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9160 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9166 ctl_cnw_cont(union ctl_io *io)
9168 struct ctl_scsiio *ctsio;
9169 struct ctl_lun *lun;
9170 struct ctl_lba_len_flags *lbalen;
9173 ctsio = &io->scsiio;
9174 ctsio->io_hdr.status = CTL_STATUS_NONE;
9175 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9176 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9177 lbalen = (struct ctl_lba_len_flags *)
9178 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9179 lbalen->flags = CTL_LLF_WRITE;
9181 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9182 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9187 ctl_cnw(struct ctl_scsiio *ctsio)
9189 struct ctl_lun *lun;
9190 struct ctl_lba_len_flags *lbalen;
9192 uint32_t num_blocks;
9196 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9198 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9203 retval = CTL_RETVAL_COMPLETE;
9205 switch (ctsio->cdb[0]) {
9206 case COMPARE_AND_WRITE: {
9207 struct scsi_compare_and_write *cdb;
9209 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9211 if (cdb->byte2 & SRW10_FUA)
9213 if (cdb->byte2 & SRW10_DPO)
9215 lba = scsi_8btou64(cdb->addr);
9216 num_blocks = cdb->length;
9221 * We got a command we don't support. This shouldn't
9222 * happen, commands should be filtered out above us.
9224 ctl_set_invalid_opcode(ctsio);
9225 ctl_done((union ctl_io *)ctsio);
9227 return (CTL_RETVAL_COMPLETE);
9228 break; /* NOTREACHED */
9232 * XXX KDM what do we do with the DPO and FUA bits? FUA might be
9233 * interesting for us, but if RAIDCore is in write-back mode,
9234 * getting it to do write-through for a particular transaction may
9239 * The first check is to make sure we're in bounds, the second
9240 * check is to catch wrap-around problems. If the lba + num blocks
9241 * is less than the lba, then we've wrapped around and the block
9242 * range is invalid anyway.
9244 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9245 || ((lba + num_blocks) < lba)) {
9246 ctl_set_lba_out_of_range(ctsio);
9247 ctl_done((union ctl_io *)ctsio);
9248 return (CTL_RETVAL_COMPLETE);
9252 * According to SBC-3, a transfer length of 0 is not an error.
9254 if (num_blocks == 0) {
9255 ctl_set_success(ctsio);
9256 ctl_done((union ctl_io *)ctsio);
9257 return (CTL_RETVAL_COMPLETE);
9260 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9261 ctsio->kern_rel_offset = 0;
9264 * Set the IO_CONT flag, so that if this I/O gets passed to
9265 * ctl_data_submit_done(), it'll get passed back to
9266 * ctl_ctl_cnw_cont() for further processing.
9268 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9269 ctsio->io_cont = ctl_cnw_cont;
9271 lbalen = (struct ctl_lba_len_flags *)
9272 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9274 lbalen->len = num_blocks;
9275 lbalen->flags = CTL_LLF_COMPARE;
9277 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9278 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9283 ctl_verify(struct ctl_scsiio *ctsio)
9285 struct ctl_lun *lun;
9286 struct ctl_lba_len_flags *lbalen;
9288 uint32_t num_blocks;
9292 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9294 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9298 retval = CTL_RETVAL_COMPLETE;
9300 switch (ctsio->cdb[0]) {
9302 struct scsi_verify_10 *cdb;
9304 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9305 if (cdb->byte2 & SVFY_BYTCHK)
9307 if (cdb->byte2 & SVFY_DPO)
9309 lba = scsi_4btoul(cdb->addr);
9310 num_blocks = scsi_2btoul(cdb->length);
9314 struct scsi_verify_12 *cdb;
9316 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9317 if (cdb->byte2 & SVFY_BYTCHK)
9319 if (cdb->byte2 & SVFY_DPO)
9321 lba = scsi_4btoul(cdb->addr);
9322 num_blocks = scsi_4btoul(cdb->length);
9326 struct scsi_rw_16 *cdb;
9328 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9329 if (cdb->byte2 & SVFY_BYTCHK)
9331 if (cdb->byte2 & SVFY_DPO)
9333 lba = scsi_8btou64(cdb->addr);
9334 num_blocks = scsi_4btoul(cdb->length);
9339 * We got a command we don't support. This shouldn't
9340 * happen, commands should be filtered out above us.
9342 ctl_set_invalid_opcode(ctsio);
9343 ctl_done((union ctl_io *)ctsio);
9344 return (CTL_RETVAL_COMPLETE);
9348 * The first check is to make sure we're in bounds, the second
9349 * check is to catch wrap-around problems. If the lba + num blocks
9350 * is less than the lba, then we've wrapped around and the block
9351 * range is invalid anyway.
9353 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9354 || ((lba + num_blocks) < lba)) {
9355 ctl_set_lba_out_of_range(ctsio);
9356 ctl_done((union ctl_io *)ctsio);
9357 return (CTL_RETVAL_COMPLETE);
9361 * According to SBC-3, a transfer length of 0 is not an error.
9363 if (num_blocks == 0) {
9364 ctl_set_success(ctsio);
9365 ctl_done((union ctl_io *)ctsio);
9366 return (CTL_RETVAL_COMPLETE);
9369 lbalen = (struct ctl_lba_len_flags *)
9370 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9372 lbalen->len = num_blocks;
9374 lbalen->flags = CTL_LLF_COMPARE;
9375 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9377 lbalen->flags = CTL_LLF_VERIFY;
9378 ctsio->kern_total_len = 0;
9380 ctsio->kern_rel_offset = 0;
9382 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9383 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9388 ctl_report_luns(struct ctl_scsiio *ctsio)
9390 struct scsi_report_luns *cdb;
9391 struct scsi_report_luns_data *lun_data;
9392 struct ctl_lun *lun, *request_lun;
9393 int num_luns, retval;
9394 uint32_t alloc_len, lun_datalen;
9395 int num_filled, well_known;
9396 uint32_t initidx, targ_lun_id, lun_id;
9398 retval = CTL_RETVAL_COMPLETE;
9401 cdb = (struct scsi_report_luns *)ctsio->cdb;
9403 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9405 mtx_lock(&control_softc->ctl_lock);
9406 num_luns = control_softc->num_luns;
9407 mtx_unlock(&control_softc->ctl_lock);
9409 switch (cdb->select_report) {
9410 case RPL_REPORT_DEFAULT:
9411 case RPL_REPORT_ALL:
9413 case RPL_REPORT_WELLKNOWN:
9418 ctl_set_invalid_field(ctsio,
9424 ctl_done((union ctl_io *)ctsio);
9426 break; /* NOTREACHED */
9429 alloc_len = scsi_4btoul(cdb->length);
9431 * The initiator has to allocate at least 16 bytes for this request,
9432 * so he can at least get the header and the first LUN. Otherwise
9433 * we reject the request (per SPC-3 rev 14, section 6.21).
9435 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9436 sizeof(struct scsi_report_luns_lundata))) {
9437 ctl_set_invalid_field(ctsio,
9443 ctl_done((union ctl_io *)ctsio);
9447 request_lun = (struct ctl_lun *)
9448 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9450 lun_datalen = sizeof(*lun_data) +
9451 (num_luns * sizeof(struct scsi_report_luns_lundata));
9453 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9454 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9455 ctsio->kern_sg_entries = 0;
9457 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9459 mtx_lock(&control_softc->ctl_lock);
9460 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9461 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9462 if (lun_id >= CTL_MAX_LUNS)
9464 lun = control_softc->ctl_luns[lun_id];
9468 if (targ_lun_id <= 0xff) {
9470 * Peripheral addressing method, bus number 0.
9472 lun_data->luns[num_filled].lundata[0] =
9473 RPL_LUNDATA_ATYP_PERIPH;
9474 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9476 } else if (targ_lun_id <= 0x3fff) {
9478 * Flat addressing method.
9480 lun_data->luns[num_filled].lundata[0] =
9481 RPL_LUNDATA_ATYP_FLAT |
9482 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9483 #ifdef OLDCTLHEADERS
9484 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9485 (targ_lun_id & SRLD_BUS_LUN_MASK);
9487 lun_data->luns[num_filled].lundata[1] =
9488 #ifdef OLDCTLHEADERS
9489 targ_lun_id >> SRLD_BUS_LUN_BITS;
9491 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9494 printf("ctl_report_luns: bogus LUN number %jd, "
9495 "skipping\n", (intmax_t)targ_lun_id);
9498 * According to SPC-3, rev 14 section 6.21:
9500 * "The execution of a REPORT LUNS command to any valid and
9501 * installed logical unit shall clear the REPORTED LUNS DATA
9502 * HAS CHANGED unit attention condition for all logical
9503 * units of that target with respect to the requesting
9504 * initiator. A valid and installed logical unit is one
9505 * having a PERIPHERAL QUALIFIER of 000b in the standard
9506 * INQUIRY data (see 6.4.2)."
9508 * If request_lun is NULL, the LUN this report luns command
9509 * was issued to is either disabled or doesn't exist. In that
9510 * case, we shouldn't clear any pending lun change unit
9513 if (request_lun != NULL) {
9514 mtx_lock(&lun->lun_lock);
9515 lun->pending_sense[initidx].ua_pending &=
9517 mtx_unlock(&lun->lun_lock);
9520 mtx_unlock(&control_softc->ctl_lock);
9523 * It's quite possible that we've returned fewer LUNs than we allocated
9524 * space for. Trim it.
9526 lun_datalen = sizeof(*lun_data) +
9527 (num_filled * sizeof(struct scsi_report_luns_lundata));
9529 if (lun_datalen < alloc_len) {
9530 ctsio->residual = alloc_len - lun_datalen;
9531 ctsio->kern_data_len = lun_datalen;
9532 ctsio->kern_total_len = lun_datalen;
9534 ctsio->residual = 0;
9535 ctsio->kern_data_len = alloc_len;
9536 ctsio->kern_total_len = alloc_len;
9538 ctsio->kern_data_resid = 0;
9539 ctsio->kern_rel_offset = 0;
9540 ctsio->kern_sg_entries = 0;
9543 * We set this to the actual data length, regardless of how much
9544 * space we actually have to return results. If the user looks at
9545 * this value, he'll know whether or not he allocated enough space
9546 * and reissue the command if necessary. We don't support well
9547 * known logical units, so if the user asks for that, return none.
9549 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9552 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9555 ctsio->scsi_status = SCSI_STATUS_OK;
9557 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9558 ctsio->be_move_done = ctl_config_move_done;
9559 ctl_datamove((union ctl_io *)ctsio);
9565 ctl_request_sense(struct ctl_scsiio *ctsio)
9567 struct scsi_request_sense *cdb;
9568 struct scsi_sense_data *sense_ptr;
9569 struct ctl_lun *lun;
9572 scsi_sense_data_type sense_format;
9574 cdb = (struct scsi_request_sense *)ctsio->cdb;
9576 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9578 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9581 * Determine which sense format the user wants.
9583 if (cdb->byte2 & SRS_DESC)
9584 sense_format = SSD_TYPE_DESC;
9586 sense_format = SSD_TYPE_FIXED;
9588 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9589 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9590 ctsio->kern_sg_entries = 0;
9593 * struct scsi_sense_data, which is currently set to 256 bytes, is
9594 * larger than the largest allowed value for the length field in the
9595 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9597 ctsio->residual = 0;
9598 ctsio->kern_data_len = cdb->length;
9599 ctsio->kern_total_len = cdb->length;
9601 ctsio->kern_data_resid = 0;
9602 ctsio->kern_rel_offset = 0;
9603 ctsio->kern_sg_entries = 0;
9606 * If we don't have a LUN, we don't have any pending sense.
9612 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9614 * Check for pending sense, and then for pending unit attentions.
9615 * Pending sense gets returned first, then pending unit attentions.
9617 mtx_lock(&lun->lun_lock);
9618 if (ctl_is_set(lun->have_ca, initidx)) {
9619 scsi_sense_data_type stored_format;
9622 * Check to see which sense format was used for the stored
9625 stored_format = scsi_sense_type(
9626 &lun->pending_sense[initidx].sense);
9629 * If the user requested a different sense format than the
9630 * one we stored, then we need to convert it to the other
9631 * format. If we're going from descriptor to fixed format
9632 * sense data, we may lose things in translation, depending
9633 * on what options were used.
9635 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9636 * for some reason we'll just copy it out as-is.
9638 if ((stored_format == SSD_TYPE_FIXED)
9639 && (sense_format == SSD_TYPE_DESC))
9640 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9641 &lun->pending_sense[initidx].sense,
9642 (struct scsi_sense_data_desc *)sense_ptr);
9643 else if ((stored_format == SSD_TYPE_DESC)
9644 && (sense_format == SSD_TYPE_FIXED))
9645 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9646 &lun->pending_sense[initidx].sense,
9647 (struct scsi_sense_data_fixed *)sense_ptr);
9649 memcpy(sense_ptr, &lun->pending_sense[initidx].sense,
9650 ctl_min(sizeof(*sense_ptr),
9651 sizeof(lun->pending_sense[initidx].sense)));
9653 ctl_clear_mask(lun->have_ca, initidx);
9655 } else if (lun->pending_sense[initidx].ua_pending != CTL_UA_NONE) {
9656 ctl_ua_type ua_type;
9658 ua_type = ctl_build_ua(lun->pending_sense[initidx].ua_pending,
9659 sense_ptr, sense_format);
9660 if (ua_type != CTL_UA_NONE) {
9662 /* We're reporting this UA, so clear it */
9663 lun->pending_sense[initidx].ua_pending &= ~ua_type;
9666 mtx_unlock(&lun->lun_lock);
9669 * We already have a pending error, return it.
9671 if (have_error != 0) {
9673 * We report the SCSI status as OK, since the status of the
9674 * request sense command itself is OK.
9676 ctsio->scsi_status = SCSI_STATUS_OK;
9679 * We report 0 for the sense length, because we aren't doing
9680 * autosense in this case. We're reporting sense as
9683 ctsio->sense_len = 0;
9684 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9685 ctsio->be_move_done = ctl_config_move_done;
9686 ctl_datamove((union ctl_io *)ctsio);
9688 return (CTL_RETVAL_COMPLETE);
9694 * No sense information to report, so we report that everything is
9697 ctl_set_sense_data(sense_ptr,
9700 /*current_error*/ 1,
9701 /*sense_key*/ SSD_KEY_NO_SENSE,
9706 ctsio->scsi_status = SCSI_STATUS_OK;
9709 * We report 0 for the sense length, because we aren't doing
9710 * autosense in this case. We're reporting sense as parameter data.
9712 ctsio->sense_len = 0;
9713 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9714 ctsio->be_move_done = ctl_config_move_done;
9715 ctl_datamove((union ctl_io *)ctsio);
9717 return (CTL_RETVAL_COMPLETE);
9721 ctl_tur(struct ctl_scsiio *ctsio)
9723 struct ctl_lun *lun;
9725 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9727 CTL_DEBUG_PRINT(("ctl_tur\n"));
9732 ctsio->scsi_status = SCSI_STATUS_OK;
9733 ctsio->io_hdr.status = CTL_SUCCESS;
9735 ctl_done((union ctl_io *)ctsio);
9737 return (CTL_RETVAL_COMPLETE);
9742 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9749 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9751 struct scsi_vpd_supported_pages *pages;
9753 struct ctl_lun *lun;
9755 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9757 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9758 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9759 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9760 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9761 ctsio->kern_sg_entries = 0;
9763 if (sup_page_size < alloc_len) {
9764 ctsio->residual = alloc_len - sup_page_size;
9765 ctsio->kern_data_len = sup_page_size;
9766 ctsio->kern_total_len = sup_page_size;
9768 ctsio->residual = 0;
9769 ctsio->kern_data_len = alloc_len;
9770 ctsio->kern_total_len = alloc_len;
9772 ctsio->kern_data_resid = 0;
9773 ctsio->kern_rel_offset = 0;
9774 ctsio->kern_sg_entries = 0;
9777 * The control device is always connected. The disk device, on the
9778 * other hand, may not be online all the time. Need to change this
9779 * to figure out whether the disk device is actually online or not.
9782 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9783 lun->be_lun->lun_type;
9785 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9787 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9788 /* Supported VPD pages */
9789 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9791 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9792 /* Device Identification */
9793 pages->page_list[2] = SVPD_DEVICE_ID;
9795 pages->page_list[3] = SVPD_SCSI_PORTS;
9797 pages->page_list[4] = SVPD_BLOCK_LIMITS;
9798 /* Logical Block Provisioning */
9799 pages->page_list[5] = SVPD_LBP;
9801 ctsio->scsi_status = SCSI_STATUS_OK;
9803 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9804 ctsio->be_move_done = ctl_config_move_done;
9805 ctl_datamove((union ctl_io *)ctsio);
9807 return (CTL_RETVAL_COMPLETE);
9811 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9813 struct scsi_vpd_unit_serial_number *sn_ptr;
9814 struct ctl_lun *lun;
9816 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9818 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9819 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9820 ctsio->kern_sg_entries = 0;
9822 if (sizeof(*sn_ptr) < alloc_len) {
9823 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9824 ctsio->kern_data_len = sizeof(*sn_ptr);
9825 ctsio->kern_total_len = sizeof(*sn_ptr);
9827 ctsio->residual = 0;
9828 ctsio->kern_data_len = alloc_len;
9829 ctsio->kern_total_len = alloc_len;
9831 ctsio->kern_data_resid = 0;
9832 ctsio->kern_rel_offset = 0;
9833 ctsio->kern_sg_entries = 0;
9836 * The control device is always connected. The disk device, on the
9837 * other hand, may not be online all the time. Need to change this
9838 * to figure out whether the disk device is actually online or not.
9841 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9842 lun->be_lun->lun_type;
9844 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9846 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9847 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9849 * If we don't have a LUN, we just leave the serial number as
9852 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9854 strncpy((char *)sn_ptr->serial_num,
9855 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9857 ctsio->scsi_status = SCSI_STATUS_OK;
9859 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9860 ctsio->be_move_done = ctl_config_move_done;
9861 ctl_datamove((union ctl_io *)ctsio);
9863 return (CTL_RETVAL_COMPLETE);
9868 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9870 struct scsi_vpd_device_id *devid_ptr;
9871 struct scsi_vpd_id_descriptor *desc;
9872 struct ctl_softc *ctl_softc;
9873 struct ctl_lun *lun;
9874 struct ctl_port *port;
9878 ctl_softc = control_softc;
9880 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9881 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9883 data_len = sizeof(struct scsi_vpd_device_id) +
9884 sizeof(struct scsi_vpd_id_descriptor) +
9885 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9886 sizeof(struct scsi_vpd_id_descriptor) +
9887 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9888 if (lun && lun->lun_devid)
9889 data_len += lun->lun_devid->len;
9890 if (port->port_devid)
9891 data_len += port->port_devid->len;
9892 if (port->target_devid)
9893 data_len += port->target_devid->len;
9895 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9896 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9897 ctsio->kern_sg_entries = 0;
9899 if (data_len < alloc_len) {
9900 ctsio->residual = alloc_len - data_len;
9901 ctsio->kern_data_len = data_len;
9902 ctsio->kern_total_len = data_len;
9904 ctsio->residual = 0;
9905 ctsio->kern_data_len = alloc_len;
9906 ctsio->kern_total_len = alloc_len;
9908 ctsio->kern_data_resid = 0;
9909 ctsio->kern_rel_offset = 0;
9910 ctsio->kern_sg_entries = 0;
9913 * The control device is always connected. The disk device, on the
9914 * other hand, may not be online all the time.
9917 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9918 lun->be_lun->lun_type;
9920 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9921 devid_ptr->page_code = SVPD_DEVICE_ID;
9922 scsi_ulto2b(data_len - 4, devid_ptr->length);
9924 if (port->port_type == CTL_PORT_FC)
9925 proto = SCSI_PROTO_FC << 4;
9926 else if (port->port_type == CTL_PORT_ISCSI)
9927 proto = SCSI_PROTO_ISCSI << 4;
9929 proto = SCSI_PROTO_SPI << 4;
9930 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9933 * We're using a LUN association here. i.e., this device ID is a
9934 * per-LUN identifier.
9936 if (lun && lun->lun_devid) {
9937 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9938 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9939 lun->lun_devid->len);
9943 * This is for the WWPN which is a port association.
9945 if (port->port_devid) {
9946 memcpy(desc, port->port_devid->data, port->port_devid->len);
9947 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9948 port->port_devid->len);
9952 * This is for the Relative Target Port(type 4h) identifier
9954 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9955 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9956 SVPD_ID_TYPE_RELTARG;
9958 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9959 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9960 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9963 * This is for the Target Port Group(type 5h) identifier
9965 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9966 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9967 SVPD_ID_TYPE_TPORTGRP;
9969 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
9970 &desc->identifier[2]);
9971 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9972 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9975 * This is for the Target identifier
9977 if (port->target_devid) {
9978 memcpy(desc, port->target_devid->data, port->target_devid->len);
9981 ctsio->scsi_status = SCSI_STATUS_OK;
9982 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9983 ctsio->be_move_done = ctl_config_move_done;
9984 ctl_datamove((union ctl_io *)ctsio);
9986 return (CTL_RETVAL_COMPLETE);
9990 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9992 struct ctl_softc *softc = control_softc;
9993 struct scsi_vpd_scsi_ports *sp;
9994 struct scsi_vpd_port_designation *pd;
9995 struct scsi_vpd_port_designation_cont *pdc;
9996 struct ctl_lun *lun;
9997 struct ctl_port *port;
9998 int data_len, num_target_ports, id_len, g, pg, p;
9999 int num_target_port_groups, single;
10001 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10003 single = ctl_is_single;
10005 num_target_port_groups = 1;
10007 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
10008 num_target_ports = 0;
10010 mtx_lock(&softc->ctl_lock);
10011 STAILQ_FOREACH(port, &softc->port_list, links) {
10012 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10014 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
10017 num_target_ports++;
10018 if (port->port_devid)
10019 id_len += port->port_devid->len;
10021 mtx_unlock(&softc->ctl_lock);
10023 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
10024 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
10025 sizeof(struct scsi_vpd_port_designation_cont)) + id_len;
10026 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10027 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
10028 ctsio->kern_sg_entries = 0;
10030 if (data_len < alloc_len) {
10031 ctsio->residual = alloc_len - data_len;
10032 ctsio->kern_data_len = data_len;
10033 ctsio->kern_total_len = data_len;
10035 ctsio->residual = 0;
10036 ctsio->kern_data_len = alloc_len;
10037 ctsio->kern_total_len = alloc_len;
10039 ctsio->kern_data_resid = 0;
10040 ctsio->kern_rel_offset = 0;
10041 ctsio->kern_sg_entries = 0;
10044 * The control device is always connected. The disk device, on the
10045 * other hand, may not be online all the time. Need to change this
10046 * to figure out whether the disk device is actually online or not.
10049 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10050 lun->be_lun->lun_type;
10052 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10054 sp->page_code = SVPD_SCSI_PORTS;
10055 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10057 pd = &sp->design[0];
10059 mtx_lock(&softc->ctl_lock);
10060 if (softc->flags & CTL_FLAG_MASTER_SHELF)
10064 for (g = 0; g < num_target_port_groups; g++) {
10065 STAILQ_FOREACH(port, &softc->port_list, links) {
10066 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10068 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
10071 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10072 scsi_ulto2b(p, pd->relative_port_id);
10073 scsi_ulto2b(0, pd->initiator_transportid_length);
10074 pdc = (struct scsi_vpd_port_designation_cont *)
10075 &pd->initiator_transportid[0];
10076 if (port->port_devid && g == pg) {
10077 id_len = port->port_devid->len;
10078 scsi_ulto2b(port->port_devid->len,
10079 pdc->target_port_descriptors_length);
10080 memcpy(pdc->target_port_descriptors,
10081 port->port_devid->data, port->port_devid->len);
10084 scsi_ulto2b(0, pdc->target_port_descriptors_length);
10086 pd = (struct scsi_vpd_port_designation *)
10087 ((uint8_t *)pdc->target_port_descriptors + id_len);
10090 mtx_unlock(&softc->ctl_lock);
10092 ctsio->scsi_status = SCSI_STATUS_OK;
10093 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10094 ctsio->be_move_done = ctl_config_move_done;
10095 ctl_datamove((union ctl_io *)ctsio);
10097 return (CTL_RETVAL_COMPLETE);
10101 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10103 struct scsi_vpd_block_limits *bl_ptr;
10104 struct ctl_lun *lun;
10107 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10108 bs = lun->be_lun->blocksize;
10110 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10111 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10112 ctsio->kern_sg_entries = 0;
10114 if (sizeof(*bl_ptr) < alloc_len) {
10115 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10116 ctsio->kern_data_len = sizeof(*bl_ptr);
10117 ctsio->kern_total_len = sizeof(*bl_ptr);
10119 ctsio->residual = 0;
10120 ctsio->kern_data_len = alloc_len;
10121 ctsio->kern_total_len = alloc_len;
10123 ctsio->kern_data_resid = 0;
10124 ctsio->kern_rel_offset = 0;
10125 ctsio->kern_sg_entries = 0;
10128 * The control device is always connected. The disk device, on the
10129 * other hand, may not be online all the time. Need to change this
10130 * to figure out whether the disk device is actually online or not.
10133 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10134 lun->be_lun->lun_type;
10136 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10138 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10139 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
10140 bl_ptr->max_cmp_write_len = 0xff;
10141 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10142 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10143 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10144 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10145 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10147 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10149 ctsio->scsi_status = SCSI_STATUS_OK;
10150 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10151 ctsio->be_move_done = ctl_config_move_done;
10152 ctl_datamove((union ctl_io *)ctsio);
10154 return (CTL_RETVAL_COMPLETE);
10158 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10160 struct scsi_vpd_logical_block_prov *lbp_ptr;
10161 struct ctl_lun *lun;
10164 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10165 bs = lun->be_lun->blocksize;
10167 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10168 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10169 ctsio->kern_sg_entries = 0;
10171 if (sizeof(*lbp_ptr) < alloc_len) {
10172 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10173 ctsio->kern_data_len = sizeof(*lbp_ptr);
10174 ctsio->kern_total_len = sizeof(*lbp_ptr);
10176 ctsio->residual = 0;
10177 ctsio->kern_data_len = alloc_len;
10178 ctsio->kern_total_len = alloc_len;
10180 ctsio->kern_data_resid = 0;
10181 ctsio->kern_rel_offset = 0;
10182 ctsio->kern_sg_entries = 0;
10185 * The control device is always connected. The disk device, on the
10186 * other hand, may not be online all the time. Need to change this
10187 * to figure out whether the disk device is actually online or not.
10190 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10191 lun->be_lun->lun_type;
10193 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10195 lbp_ptr->page_code = SVPD_LBP;
10196 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
10197 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 | SVPD_LBP_WS10;
10199 ctsio->scsi_status = SCSI_STATUS_OK;
10200 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10201 ctsio->be_move_done = ctl_config_move_done;
10202 ctl_datamove((union ctl_io *)ctsio);
10204 return (CTL_RETVAL_COMPLETE);
10208 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10210 struct scsi_inquiry *cdb;
10211 struct ctl_lun *lun;
10212 int alloc_len, retval;
10214 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10215 cdb = (struct scsi_inquiry *)ctsio->cdb;
10217 retval = CTL_RETVAL_COMPLETE;
10219 alloc_len = scsi_2btoul(cdb->length);
10221 switch (cdb->page_code) {
10222 case SVPD_SUPPORTED_PAGES:
10223 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10225 case SVPD_UNIT_SERIAL_NUMBER:
10226 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10228 case SVPD_DEVICE_ID:
10229 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10231 case SVPD_SCSI_PORTS:
10232 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10234 case SVPD_BLOCK_LIMITS:
10235 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10238 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10241 ctl_set_invalid_field(ctsio,
10247 ctl_done((union ctl_io *)ctsio);
10248 retval = CTL_RETVAL_COMPLETE;
10256 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10258 struct scsi_inquiry_data *inq_ptr;
10259 struct scsi_inquiry *cdb;
10260 struct ctl_softc *ctl_softc;
10261 struct ctl_lun *lun;
10263 uint32_t alloc_len;
10266 ctl_softc = control_softc;
10269 * Figure out whether we're talking to a Fibre Channel port or not.
10270 * We treat the ioctl front end, and any SCSI adapters, as packetized
10273 if (ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type !=
10279 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10280 cdb = (struct scsi_inquiry *)ctsio->cdb;
10281 alloc_len = scsi_2btoul(cdb->length);
10284 * We malloc the full inquiry data size here and fill it
10285 * in. If the user only asks for less, we'll give him
10288 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
10289 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10290 ctsio->kern_sg_entries = 0;
10291 ctsio->kern_data_resid = 0;
10292 ctsio->kern_rel_offset = 0;
10294 if (sizeof(*inq_ptr) < alloc_len) {
10295 ctsio->residual = alloc_len - sizeof(*inq_ptr);
10296 ctsio->kern_data_len = sizeof(*inq_ptr);
10297 ctsio->kern_total_len = sizeof(*inq_ptr);
10299 ctsio->residual = 0;
10300 ctsio->kern_data_len = alloc_len;
10301 ctsio->kern_total_len = alloc_len;
10305 * If we have a LUN configured, report it as connected. Otherwise,
10306 * report that it is offline or no device is supported, depending
10307 * on the value of inquiry_pq_no_lun.
10309 * According to the spec (SPC-4 r34), the peripheral qualifier
10310 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10312 * "A peripheral device having the specified peripheral device type
10313 * is not connected to this logical unit. However, the device
10314 * server is capable of supporting the specified peripheral device
10315 * type on this logical unit."
10317 * According to the same spec, the peripheral qualifier
10318 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10320 * "The device server is not capable of supporting a peripheral
10321 * device on this logical unit. For this peripheral qualifier the
10322 * peripheral device type shall be set to 1Fh. All other peripheral
10323 * device type values are reserved for this peripheral qualifier."
10325 * Given the text, it would seem that we probably want to report that
10326 * the LUN is offline here. There is no LUN connected, but we can
10327 * support a LUN at the given LUN number.
10329 * In the real world, though, it sounds like things are a little
10332 * - Linux, when presented with a LUN with the offline peripheral
10333 * qualifier, will create an sg driver instance for it. So when
10334 * you attach it to CTL, you wind up with a ton of sg driver
10335 * instances. (One for every LUN that Linux bothered to probe.)
10336 * Linux does this despite the fact that it issues a REPORT LUNs
10337 * to LUN 0 to get the inventory of supported LUNs.
10339 * - There is other anecdotal evidence (from Emulex folks) about
10340 * arrays that use the offline peripheral qualifier for LUNs that
10341 * are on the "passive" path in an active/passive array.
10343 * So the solution is provide a hopefully reasonable default
10344 * (return bad/no LUN) and allow the user to change the behavior
10345 * with a tunable/sysctl variable.
10348 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10349 lun->be_lun->lun_type;
10350 else if (ctl_softc->inquiry_pq_no_lun == 0)
10351 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10353 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10355 /* RMB in byte 2 is 0 */
10356 inq_ptr->version = SCSI_REV_SPC3;
10359 * According to SAM-3, even if a device only supports a single
10360 * level of LUN addressing, it should still set the HISUP bit:
10362 * 4.9.1 Logical unit numbers overview
10364 * All logical unit number formats described in this standard are
10365 * hierarchical in structure even when only a single level in that
10366 * hierarchy is used. The HISUP bit shall be set to one in the
10367 * standard INQUIRY data (see SPC-2) when any logical unit number
10368 * format described in this standard is used. Non-hierarchical
10369 * formats are outside the scope of this standard.
10371 * Therefore we set the HiSup bit here.
10373 * The reponse format is 2, per SPC-3.
10375 inq_ptr->response_format = SID_HiSup | 2;
10377 inq_ptr->additional_length = sizeof(*inq_ptr) - 4;
10378 CTL_DEBUG_PRINT(("additional_length = %d\n",
10379 inq_ptr->additional_length));
10381 inq_ptr->spc3_flags = SPC3_SID_TPGS_IMPLICIT;
10382 /* 16 bit addressing */
10384 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10385 /* XXX set the SID_MultiP bit here if we're actually going to
10386 respond on multiple ports */
10387 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10389 /* 16 bit data bus, synchronous transfers */
10390 /* XXX these flags don't apply for FC */
10392 inq_ptr->flags = SID_WBus16 | SID_Sync;
10394 * XXX KDM do we want to support tagged queueing on the control
10398 || (lun->be_lun->lun_type != T_PROCESSOR))
10399 inq_ptr->flags |= SID_CmdQue;
10401 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10402 * We have 8 bytes for the vendor name, and 16 bytes for the device
10403 * name and 4 bytes for the revision.
10405 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10406 "vendor")) == NULL) {
10407 strcpy(inq_ptr->vendor, CTL_VENDOR);
10409 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10410 strncpy(inq_ptr->vendor, val,
10411 min(sizeof(inq_ptr->vendor), strlen(val)));
10414 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10415 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10416 switch (lun->be_lun->lun_type) {
10418 strcpy(inq_ptr->product, CTL_DIRECT_PRODUCT);
10421 strcpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT);
10424 strcpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT);
10428 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10429 strncpy(inq_ptr->product, val,
10430 min(sizeof(inq_ptr->product), strlen(val)));
10434 * XXX make this a macro somewhere so it automatically gets
10435 * incremented when we make changes.
10437 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10438 "revision")) == NULL) {
10439 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10441 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10442 strncpy(inq_ptr->revision, val,
10443 min(sizeof(inq_ptr->revision), strlen(val)));
10447 * For parallel SCSI, we support double transition and single
10448 * transition clocking. We also support QAS (Quick Arbitration
10449 * and Selection) and Information Unit transfers on both the
10450 * control and array devices.
10453 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10457 scsi_ulto2b(0x0060, inq_ptr->version1);
10458 /* SPC-3 (no version claimed) XXX should we claim a version? */
10459 scsi_ulto2b(0x0300, inq_ptr->version2);
10461 /* FCP-2 ANSI INCITS.350:2003 */
10462 scsi_ulto2b(0x0917, inq_ptr->version3);
10464 /* SPI-4 ANSI INCITS.362:200x */
10465 scsi_ulto2b(0x0B56, inq_ptr->version3);
10469 /* SBC-2 (no version claimed) XXX should we claim a version? */
10470 scsi_ulto2b(0x0320, inq_ptr->version4);
10472 switch (lun->be_lun->lun_type) {
10475 * SBC-2 (no version claimed) XXX should we claim a
10478 scsi_ulto2b(0x0320, inq_ptr->version4);
10486 ctsio->scsi_status = SCSI_STATUS_OK;
10487 if (ctsio->kern_data_len > 0) {
10488 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10489 ctsio->be_move_done = ctl_config_move_done;
10490 ctl_datamove((union ctl_io *)ctsio);
10492 ctsio->io_hdr.status = CTL_SUCCESS;
10493 ctl_done((union ctl_io *)ctsio);
10496 return (CTL_RETVAL_COMPLETE);
10500 ctl_inquiry(struct ctl_scsiio *ctsio)
10502 struct scsi_inquiry *cdb;
10505 cdb = (struct scsi_inquiry *)ctsio->cdb;
10509 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10512 * Right now, we don't support the CmdDt inquiry information.
10513 * This would be nice to support in the future. When we do
10514 * support it, we should change this test so that it checks to make
10515 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
10518 if (((cdb->byte2 & SI_EVPD)
10519 && (cdb->byte2 & SI_CMDDT)))
10521 if (cdb->byte2 & SI_CMDDT) {
10523 * Point to the SI_CMDDT bit. We might change this
10524 * when we support SI_CMDDT, but since both bits would be
10525 * "wrong", this should probably just stay as-is then.
10527 ctl_set_invalid_field(ctsio,
10533 ctl_done((union ctl_io *)ctsio);
10534 return (CTL_RETVAL_COMPLETE);
10536 if (cdb->byte2 & SI_EVPD)
10537 retval = ctl_inquiry_evpd(ctsio);
10539 else if (cdb->byte2 & SI_CMDDT)
10540 retval = ctl_inquiry_cmddt(ctsio);
10543 retval = ctl_inquiry_std(ctsio);
10549 * For known CDB types, parse the LBA and length.
10552 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
10554 if (io->io_hdr.io_type != CTL_IO_SCSI)
10557 switch (io->scsiio.cdb[0]) {
10558 case COMPARE_AND_WRITE: {
10559 struct scsi_compare_and_write *cdb;
10561 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10563 *lba = scsi_8btou64(cdb->addr);
10564 *len = cdb->length;
10569 struct scsi_rw_6 *cdb;
10571 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10573 *lba = scsi_3btoul(cdb->addr);
10574 /* only 5 bits are valid in the most significant address byte */
10576 *len = cdb->length;
10581 struct scsi_rw_10 *cdb;
10583 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10585 *lba = scsi_4btoul(cdb->addr);
10586 *len = scsi_2btoul(cdb->length);
10589 case WRITE_VERIFY_10: {
10590 struct scsi_write_verify_10 *cdb;
10592 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10594 *lba = scsi_4btoul(cdb->addr);
10595 *len = scsi_2btoul(cdb->length);
10600 struct scsi_rw_12 *cdb;
10602 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10604 *lba = scsi_4btoul(cdb->addr);
10605 *len = scsi_4btoul(cdb->length);
10608 case WRITE_VERIFY_12: {
10609 struct scsi_write_verify_12 *cdb;
10611 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10613 *lba = scsi_4btoul(cdb->addr);
10614 *len = scsi_4btoul(cdb->length);
10619 struct scsi_rw_16 *cdb;
10621 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10623 *lba = scsi_8btou64(cdb->addr);
10624 *len = scsi_4btoul(cdb->length);
10627 case WRITE_VERIFY_16: {
10628 struct scsi_write_verify_16 *cdb;
10630 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10633 *lba = scsi_8btou64(cdb->addr);
10634 *len = scsi_4btoul(cdb->length);
10637 case WRITE_SAME_10: {
10638 struct scsi_write_same_10 *cdb;
10640 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10642 *lba = scsi_4btoul(cdb->addr);
10643 *len = scsi_2btoul(cdb->length);
10646 case WRITE_SAME_16: {
10647 struct scsi_write_same_16 *cdb;
10649 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10651 *lba = scsi_8btou64(cdb->addr);
10652 *len = scsi_4btoul(cdb->length);
10656 struct scsi_verify_10 *cdb;
10658 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10660 *lba = scsi_4btoul(cdb->addr);
10661 *len = scsi_2btoul(cdb->length);
10665 struct scsi_verify_12 *cdb;
10667 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10669 *lba = scsi_4btoul(cdb->addr);
10670 *len = scsi_4btoul(cdb->length);
10674 struct scsi_verify_16 *cdb;
10676 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10678 *lba = scsi_8btou64(cdb->addr);
10679 *len = scsi_4btoul(cdb->length);
10684 break; /* NOTREACHED */
10691 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10693 uint64_t endlba1, endlba2;
10695 endlba1 = lba1 + len1 - 1;
10696 endlba2 = lba2 + len2 - 1;
10698 if ((endlba1 < lba2)
10699 || (endlba2 < lba1))
10700 return (CTL_ACTION_PASS);
10702 return (CTL_ACTION_BLOCK);
10706 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10708 uint64_t lba1, lba2;
10709 uint32_t len1, len2;
10712 retval = ctl_get_lba_len(io1, &lba1, &len1);
10714 return (CTL_ACTION_ERROR);
10716 retval = ctl_get_lba_len(io2, &lba2, &len2);
10718 return (CTL_ACTION_ERROR);
10720 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10724 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10726 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10727 ctl_serialize_action *serialize_row;
10730 * The initiator attempted multiple untagged commands at the same
10731 * time. Can't do that.
10733 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10734 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10735 && ((pending_io->io_hdr.nexus.targ_port ==
10736 ooa_io->io_hdr.nexus.targ_port)
10737 && (pending_io->io_hdr.nexus.initid.id ==
10738 ooa_io->io_hdr.nexus.initid.id))
10739 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10740 return (CTL_ACTION_OVERLAP);
10743 * The initiator attempted to send multiple tagged commands with
10744 * the same ID. (It's fine if different initiators have the same
10747 * Even if all of those conditions are true, we don't kill the I/O
10748 * if the command ahead of us has been aborted. We won't end up
10749 * sending it to the FETD, and it's perfectly legal to resend a
10750 * command with the same tag number as long as the previous
10751 * instance of this tag number has been aborted somehow.
10753 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10754 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10755 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10756 && ((pending_io->io_hdr.nexus.targ_port ==
10757 ooa_io->io_hdr.nexus.targ_port)
10758 && (pending_io->io_hdr.nexus.initid.id ==
10759 ooa_io->io_hdr.nexus.initid.id))
10760 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10761 return (CTL_ACTION_OVERLAP_TAG);
10764 * If we get a head of queue tag, SAM-3 says that we should
10765 * immediately execute it.
10767 * What happens if this command would normally block for some other
10768 * reason? e.g. a request sense with a head of queue tag
10769 * immediately after a write. Normally that would block, but this
10770 * will result in its getting executed immediately...
10772 * We currently return "pass" instead of "skip", so we'll end up
10773 * going through the rest of the queue to check for overlapped tags.
10775 * XXX KDM check for other types of blockage first??
10777 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10778 return (CTL_ACTION_PASS);
10781 * Ordered tags have to block until all items ahead of them
10782 * have completed. If we get called with an ordered tag, we always
10783 * block, if something else is ahead of us in the queue.
10785 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10786 return (CTL_ACTION_BLOCK);
10789 * Simple tags get blocked until all head of queue and ordered tags
10790 * ahead of them have completed. I'm lumping untagged commands in
10791 * with simple tags here. XXX KDM is that the right thing to do?
10793 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10794 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10795 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10796 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10797 return (CTL_ACTION_BLOCK);
10799 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio);
10800 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio);
10802 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10804 switch (serialize_row[pending_entry->seridx]) {
10805 case CTL_SER_BLOCK:
10806 return (CTL_ACTION_BLOCK);
10807 break; /* NOTREACHED */
10808 case CTL_SER_EXTENT:
10809 return (ctl_extent_check(pending_io, ooa_io));
10810 break; /* NOTREACHED */
10812 return (CTL_ACTION_PASS);
10813 break; /* NOTREACHED */
10815 return (CTL_ACTION_SKIP);
10818 panic("invalid serialization value %d",
10819 serialize_row[pending_entry->seridx]);
10820 break; /* NOTREACHED */
10823 return (CTL_ACTION_ERROR);
10827 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10829 * - pending_io is generally either incoming, or on the blocked queue
10830 * - starting I/O is the I/O we want to start the check with.
10833 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10834 union ctl_io *starting_io)
10836 union ctl_io *ooa_io;
10839 mtx_assert(&lun->lun_lock, MA_OWNED);
10842 * Run back along the OOA queue, starting with the current
10843 * blocked I/O and going through every I/O before it on the
10844 * queue. If starting_io is NULL, we'll just end up returning
10847 for (ooa_io = starting_io; ooa_io != NULL;
10848 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10852 * This routine just checks to see whether
10853 * cur_blocked is blocked by ooa_io, which is ahead
10854 * of it in the queue. It doesn't queue/dequeue
10857 action = ctl_check_for_blockage(pending_io, ooa_io);
10859 case CTL_ACTION_BLOCK:
10860 case CTL_ACTION_OVERLAP:
10861 case CTL_ACTION_OVERLAP_TAG:
10862 case CTL_ACTION_SKIP:
10863 case CTL_ACTION_ERROR:
10865 break; /* NOTREACHED */
10866 case CTL_ACTION_PASS:
10869 panic("invalid action %d", action);
10870 break; /* NOTREACHED */
10874 return (CTL_ACTION_PASS);
10879 * - An I/O has just completed, and has been removed from the per-LUN OOA
10880 * queue, so some items on the blocked queue may now be unblocked.
10883 ctl_check_blocked(struct ctl_lun *lun)
10885 union ctl_io *cur_blocked, *next_blocked;
10887 mtx_assert(&lun->lun_lock, MA_OWNED);
10890 * Run forward from the head of the blocked queue, checking each
10891 * entry against the I/Os prior to it on the OOA queue to see if
10892 * there is still any blockage.
10894 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10895 * with our removing a variable on it while it is traversing the
10898 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10899 cur_blocked != NULL; cur_blocked = next_blocked) {
10900 union ctl_io *prev_ooa;
10903 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10906 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10907 ctl_ooaq, ooa_links);
10910 * If cur_blocked happens to be the first item in the OOA
10911 * queue now, prev_ooa will be NULL, and the action
10912 * returned will just be CTL_ACTION_PASS.
10914 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
10917 case CTL_ACTION_BLOCK:
10918 /* Nothing to do here, still blocked */
10920 case CTL_ACTION_OVERLAP:
10921 case CTL_ACTION_OVERLAP_TAG:
10923 * This shouldn't happen! In theory we've already
10924 * checked this command for overlap...
10927 case CTL_ACTION_PASS:
10928 case CTL_ACTION_SKIP: {
10929 struct ctl_softc *softc;
10930 const struct ctl_cmd_entry *entry;
10935 * The skip case shouldn't happen, this transaction
10936 * should have never made it onto the blocked queue.
10939 * This I/O is no longer blocked, we can remove it
10940 * from the blocked queue. Since this is a TAILQ
10941 * (doubly linked list), we can do O(1) removals
10942 * from any place on the list.
10944 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
10946 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
10948 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
10950 * Need to send IO back to original side to
10953 union ctl_ha_msg msg_info;
10955 msg_info.hdr.original_sc =
10956 cur_blocked->io_hdr.original_sc;
10957 msg_info.hdr.serializing_sc = cur_blocked;
10958 msg_info.hdr.msg_type = CTL_MSG_R2R;
10959 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
10960 &msg_info, sizeof(msg_info), 0)) >
10961 CTL_HA_STATUS_SUCCESS) {
10962 printf("CTL:Check Blocked error from "
10963 "ctl_ha_msg_send %d\n",
10968 entry = ctl_get_cmd_entry(&cur_blocked->scsiio);
10969 softc = control_softc;
10971 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
10974 * Check this I/O for LUN state changes that may
10975 * have happened while this command was blocked.
10976 * The LUN state may have been changed by a command
10977 * ahead of us in the queue, so we need to re-check
10978 * for any states that can be caused by SCSI
10981 if (ctl_scsiio_lun_check(softc, lun, entry,
10982 &cur_blocked->scsiio) == 0) {
10983 cur_blocked->io_hdr.flags |=
10984 CTL_FLAG_IS_WAS_ON_RTR;
10985 ctl_enqueue_rtr(cur_blocked);
10987 ctl_done(cur_blocked);
10992 * This probably shouldn't happen -- we shouldn't
10993 * get CTL_ACTION_ERROR, or anything else.
10999 return (CTL_RETVAL_COMPLETE);
11003 * This routine (with one exception) checks LUN flags that can be set by
11004 * commands ahead of us in the OOA queue. These flags have to be checked
11005 * when a command initially comes in, and when we pull a command off the
11006 * blocked queue and are preparing to execute it. The reason we have to
11007 * check these flags for commands on the blocked queue is that the LUN
11008 * state may have been changed by a command ahead of us while we're on the
11011 * Ordering is somewhat important with these checks, so please pay
11012 * careful attention to the placement of any new checks.
11015 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
11016 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11022 mtx_assert(&lun->lun_lock, MA_OWNED);
11025 * If this shelf is a secondary shelf controller, we have to reject
11026 * any media access commands.
11029 /* No longer needed for HA */
11030 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
11031 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
11032 ctl_set_lun_standby(ctsio);
11039 * Check for a reservation conflict. If this command isn't allowed
11040 * even on reserved LUNs, and if this initiator isn't the one who
11041 * reserved us, reject the command with a reservation conflict.
11043 if ((lun->flags & CTL_LUN_RESERVED)
11044 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11045 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
11046 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
11047 || (ctsio->io_hdr.nexus.targ_target.id !=
11048 lun->rsv_nexus.targ_target.id)) {
11049 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11050 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11056 if ( (lun->flags & CTL_LUN_PR_RESERVED)
11057 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
11060 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11062 * if we aren't registered or it's a res holder type
11063 * reservation and this isn't the res holder then set a
11065 * NOTE: Commands which might be allowed on write exclusive
11066 * type reservations are checked in the particular command
11067 * for a conflict. Read and SSU are the only ones.
11069 if (!lun->per_res[residx].registered
11070 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11071 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11072 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11079 if ((lun->flags & CTL_LUN_OFFLINE)
11080 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11081 ctl_set_lun_not_ready(ctsio);
11087 * If the LUN is stopped, see if this particular command is allowed
11088 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11090 if ((lun->flags & CTL_LUN_STOPPED)
11091 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11092 /* "Logical unit not ready, initializing cmd. required" */
11093 ctl_set_lun_stopped(ctsio);
11098 if ((lun->flags & CTL_LUN_INOPERABLE)
11099 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11100 /* "Medium format corrupted" */
11101 ctl_set_medium_format_corrupted(ctsio);
11112 ctl_failover_io(union ctl_io *io, int have_lock)
11114 ctl_set_busy(&io->scsiio);
11121 struct ctl_lun *lun;
11122 struct ctl_softc *ctl_softc;
11123 union ctl_io *next_io, *pending_io;
11128 ctl_softc = control_softc;
11130 mtx_lock(&ctl_softc->ctl_lock);
11132 * Remove any cmds from the other SC from the rtr queue. These
11133 * will obviously only be for LUNs for which we're the primary.
11134 * We can't send status or get/send data for these commands.
11135 * Since they haven't been executed yet, we can just remove them.
11136 * We'll either abort them or delete them below, depending on
11137 * which HA mode we're in.
11140 mtx_lock(&ctl_softc->queue_lock);
11141 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11142 io != NULL; io = next_io) {
11143 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11144 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11145 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11146 ctl_io_hdr, links);
11148 mtx_unlock(&ctl_softc->queue_lock);
11151 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11152 lun = ctl_softc->ctl_luns[lun_idx];
11157 * Processor LUNs are primary on both sides.
11158 * XXX will this always be true?
11160 if (lun->be_lun->lun_type == T_PROCESSOR)
11163 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11164 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11165 printf("FAILOVER: primary lun %d\n", lun_idx);
11167 * Remove all commands from the other SC. First from the
11168 * blocked queue then from the ooa queue. Once we have
11169 * removed them. Call ctl_check_blocked to see if there
11170 * is anything that can run.
11172 for (io = (union ctl_io *)TAILQ_FIRST(
11173 &lun->blocked_queue); io != NULL; io = next_io) {
11175 next_io = (union ctl_io *)TAILQ_NEXT(
11176 &io->io_hdr, blocked_links);
11178 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11179 TAILQ_REMOVE(&lun->blocked_queue,
11180 &io->io_hdr,blocked_links);
11181 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11182 TAILQ_REMOVE(&lun->ooa_queue,
11183 &io->io_hdr, ooa_links);
11189 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11190 io != NULL; io = next_io) {
11192 next_io = (union ctl_io *)TAILQ_NEXT(
11193 &io->io_hdr, ooa_links);
11195 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11197 TAILQ_REMOVE(&lun->ooa_queue,
11204 ctl_check_blocked(lun);
11205 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11206 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11208 printf("FAILOVER: primary lun %d\n", lun_idx);
11210 * Abort all commands from the other SC. We can't
11211 * send status back for them now. These should get
11212 * cleaned up when they are completed or come out
11213 * for a datamove operation.
11215 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11216 io != NULL; io = next_io) {
11217 next_io = (union ctl_io *)TAILQ_NEXT(
11218 &io->io_hdr, ooa_links);
11220 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11221 io->io_hdr.flags |= CTL_FLAG_ABORT;
11223 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11224 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11226 printf("FAILOVER: secondary lun %d\n", lun_idx);
11228 lun->flags |= CTL_LUN_PRIMARY_SC;
11231 * We send all I/O that was sent to this controller
11232 * and redirected to the other side back with
11233 * busy status, and have the initiator retry it.
11234 * Figuring out how much data has been transferred,
11235 * etc. and picking up where we left off would be
11238 * XXX KDM need to remove I/O from the blocked
11241 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11242 &lun->ooa_queue); pending_io != NULL;
11243 pending_io = next_io) {
11245 next_io = (union ctl_io *)TAILQ_NEXT(
11246 &pending_io->io_hdr, ooa_links);
11248 pending_io->io_hdr.flags &=
11249 ~CTL_FLAG_SENT_2OTHER_SC;
11251 if (pending_io->io_hdr.flags &
11252 CTL_FLAG_IO_ACTIVE) {
11253 pending_io->io_hdr.flags |=
11256 ctl_set_busy(&pending_io->scsiio);
11257 ctl_done(pending_io);
11262 * Build Unit Attention
11264 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11265 lun->pending_sense[i].ua_pending |=
11266 CTL_UA_ASYM_ACC_CHANGE;
11268 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11269 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11270 printf("FAILOVER: secondary lun %d\n", lun_idx);
11272 * if the first io on the OOA is not on the RtR queue
11275 lun->flags |= CTL_LUN_PRIMARY_SC;
11277 pending_io = (union ctl_io *)TAILQ_FIRST(
11279 if (pending_io==NULL) {
11280 printf("Nothing on OOA queue\n");
11284 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11285 if ((pending_io->io_hdr.flags &
11286 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11287 pending_io->io_hdr.flags |=
11288 CTL_FLAG_IS_WAS_ON_RTR;
11289 ctl_enqueue_rtr(pending_io);
11294 printf("Tag 0x%04x is running\n",
11295 pending_io->scsiio.tag_num);
11299 next_io = (union ctl_io *)TAILQ_NEXT(
11300 &pending_io->io_hdr, ooa_links);
11301 for (pending_io=next_io; pending_io != NULL;
11302 pending_io = next_io) {
11303 pending_io->io_hdr.flags &=
11304 ~CTL_FLAG_SENT_2OTHER_SC;
11305 next_io = (union ctl_io *)TAILQ_NEXT(
11306 &pending_io->io_hdr, ooa_links);
11307 if (pending_io->io_hdr.flags &
11308 CTL_FLAG_IS_WAS_ON_RTR) {
11310 printf("Tag 0x%04x is running\n",
11311 pending_io->scsiio.tag_num);
11316 switch (ctl_check_ooa(lun, pending_io,
11317 (union ctl_io *)TAILQ_PREV(
11318 &pending_io->io_hdr, ctl_ooaq,
11321 case CTL_ACTION_BLOCK:
11322 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11323 &pending_io->io_hdr,
11325 pending_io->io_hdr.flags |=
11328 case CTL_ACTION_PASS:
11329 case CTL_ACTION_SKIP:
11330 pending_io->io_hdr.flags |=
11331 CTL_FLAG_IS_WAS_ON_RTR;
11332 ctl_enqueue_rtr(pending_io);
11334 case CTL_ACTION_OVERLAP:
11335 ctl_set_overlapped_cmd(
11336 (struct ctl_scsiio *)pending_io);
11337 ctl_done(pending_io);
11339 case CTL_ACTION_OVERLAP_TAG:
11340 ctl_set_overlapped_tag(
11341 (struct ctl_scsiio *)pending_io,
11342 pending_io->scsiio.tag_num & 0xff);
11343 ctl_done(pending_io);
11345 case CTL_ACTION_ERROR:
11347 ctl_set_internal_failure(
11348 (struct ctl_scsiio *)pending_io,
11351 ctl_done(pending_io);
11357 * Build Unit Attention
11359 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11360 lun->pending_sense[i].ua_pending |=
11361 CTL_UA_ASYM_ACC_CHANGE;
11364 panic("Unhandled HA mode failover, LUN flags = %#x, "
11365 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11369 mtx_unlock(&ctl_softc->ctl_lock);
11373 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11375 struct ctl_lun *lun;
11376 const struct ctl_cmd_entry *entry;
11377 uint32_t initidx, targ_lun;
11384 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11385 if ((targ_lun < CTL_MAX_LUNS)
11386 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
11387 lun = ctl_softc->ctl_luns[targ_lun];
11389 * If the LUN is invalid, pretend that it doesn't exist.
11390 * It will go away as soon as all pending I/O has been
11393 if (lun->flags & CTL_LUN_DISABLED) {
11396 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11397 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11399 if (lun->be_lun->lun_type == T_PROCESSOR) {
11400 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11404 * Every I/O goes into the OOA queue for a
11405 * particular LUN, and stays there until completion.
11407 mtx_lock(&lun->lun_lock);
11408 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11412 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11413 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11416 /* Get command entry and return error if it is unsuppotyed. */
11417 entry = ctl_validate_command(ctsio);
11418 if (entry == NULL) {
11420 mtx_unlock(&lun->lun_lock);
11424 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11425 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11428 * Check to see whether we can send this command to LUNs that don't
11429 * exist. This should pretty much only be the case for inquiry
11430 * and request sense. Further checks, below, really require having
11431 * a LUN, so we can't really check the command anymore. Just put
11432 * it on the rtr queue.
11435 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11436 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11437 ctl_enqueue_rtr((union ctl_io *)ctsio);
11441 ctl_set_unsupported_lun(ctsio);
11442 ctl_done((union ctl_io *)ctsio);
11443 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11447 * Make sure we support this particular command on this LUN.
11448 * e.g., we don't support writes to the control LUN.
11450 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11451 mtx_unlock(&lun->lun_lock);
11452 ctl_set_invalid_opcode(ctsio);
11453 ctl_done((union ctl_io *)ctsio);
11458 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11461 * If we've got a request sense, it'll clear the contingent
11462 * allegiance condition. Otherwise, if we have a CA condition for
11463 * this initiator, clear it, because it sent down a command other
11464 * than request sense.
11466 if ((ctsio->cdb[0] != REQUEST_SENSE)
11467 && (ctl_is_set(lun->have_ca, initidx)))
11468 ctl_clear_mask(lun->have_ca, initidx);
11471 * If the command has this flag set, it handles its own unit
11472 * attention reporting, we shouldn't do anything. Otherwise we
11473 * check for any pending unit attentions, and send them back to the
11474 * initiator. We only do this when a command initially comes in,
11475 * not when we pull it off the blocked queue.
11477 * According to SAM-3, section 5.3.2, the order that things get
11478 * presented back to the host is basically unit attentions caused
11479 * by some sort of reset event, busy status, reservation conflicts
11480 * or task set full, and finally any other status.
11482 * One issue here is that some of the unit attentions we report
11483 * don't fall into the "reset" category (e.g. "reported luns data
11484 * has changed"). So reporting it here, before the reservation
11485 * check, may be technically wrong. I guess the only thing to do
11486 * would be to check for and report the reset events here, and then
11487 * check for the other unit attention types after we check for a
11488 * reservation conflict.
11490 * XXX KDM need to fix this
11492 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11493 ctl_ua_type ua_type;
11495 ua_type = lun->pending_sense[initidx].ua_pending;
11496 if (ua_type != CTL_UA_NONE) {
11497 scsi_sense_data_type sense_format;
11500 sense_format = (lun->flags &
11501 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11504 sense_format = SSD_TYPE_FIXED;
11506 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
11508 if (ua_type != CTL_UA_NONE) {
11509 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11510 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11512 ctsio->sense_len = SSD_FULL_SIZE;
11513 lun->pending_sense[initidx].ua_pending &=
11515 mtx_unlock(&lun->lun_lock);
11516 ctl_done((union ctl_io *)ctsio);
11523 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11524 mtx_unlock(&lun->lun_lock);
11525 ctl_done((union ctl_io *)ctsio);
11530 * XXX CHD this is where we want to send IO to other side if
11531 * this LUN is secondary on this SC. We will need to make a copy
11532 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11533 * the copy we send as FROM_OTHER.
11534 * We also need to stuff the address of the original IO so we can
11535 * find it easily. Something similar will need be done on the other
11536 * side so when we are done we can find the copy.
11538 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11539 union ctl_ha_msg msg_info;
11542 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11544 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11545 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11547 printf("1. ctsio %p\n", ctsio);
11549 msg_info.hdr.serializing_sc = NULL;
11550 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11551 msg_info.scsi.tag_num = ctsio->tag_num;
11552 msg_info.scsi.tag_type = ctsio->tag_type;
11553 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11555 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11557 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11558 (void *)&msg_info, sizeof(msg_info), 0)) >
11559 CTL_HA_STATUS_SUCCESS) {
11560 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11562 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11565 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11570 * XXX KDM this I/O is off the incoming queue, but hasn't
11571 * been inserted on any other queue. We may need to come
11572 * up with a holding queue while we wait for serialization
11573 * so that we have an idea of what we're waiting for from
11576 mtx_unlock(&lun->lun_lock);
11580 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11581 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11582 ctl_ooaq, ooa_links))) {
11583 case CTL_ACTION_BLOCK:
11584 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11585 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11587 mtx_unlock(&lun->lun_lock);
11589 case CTL_ACTION_PASS:
11590 case CTL_ACTION_SKIP:
11591 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11592 mtx_unlock(&lun->lun_lock);
11593 ctl_enqueue_rtr((union ctl_io *)ctsio);
11595 case CTL_ACTION_OVERLAP:
11596 mtx_unlock(&lun->lun_lock);
11597 ctl_set_overlapped_cmd(ctsio);
11598 ctl_done((union ctl_io *)ctsio);
11600 case CTL_ACTION_OVERLAP_TAG:
11601 mtx_unlock(&lun->lun_lock);
11602 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11603 ctl_done((union ctl_io *)ctsio);
11605 case CTL_ACTION_ERROR:
11607 mtx_unlock(&lun->lun_lock);
11608 ctl_set_internal_failure(ctsio,
11610 /*retry_count*/ 0);
11611 ctl_done((union ctl_io *)ctsio);
11617 const struct ctl_cmd_entry *
11618 ctl_get_cmd_entry(struct ctl_scsiio *ctsio)
11620 const struct ctl_cmd_entry *entry;
11621 int service_action;
11623 entry = &ctl_cmd_table[ctsio->cdb[0]];
11624 if (entry->flags & CTL_CMD_FLAG_SA5) {
11625 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11626 entry = &((const struct ctl_cmd_entry *)
11627 entry->execute)[service_action];
11632 const struct ctl_cmd_entry *
11633 ctl_validate_command(struct ctl_scsiio *ctsio)
11635 const struct ctl_cmd_entry *entry;
11639 entry = ctl_get_cmd_entry(ctsio);
11640 if (entry->execute == NULL) {
11641 ctl_set_invalid_opcode(ctsio);
11642 ctl_done((union ctl_io *)ctsio);
11645 KASSERT(entry->length > 0,
11646 ("Not defined length for command 0x%02x/0x%02x",
11647 ctsio->cdb[0], ctsio->cdb[1]));
11648 for (i = 1; i < entry->length; i++) {
11649 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11652 ctl_set_invalid_field(ctsio,
11657 /*bit*/ fls(diff) - 1);
11658 ctl_done((union ctl_io *)ctsio);
11665 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11668 switch (lun_type) {
11670 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11671 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11675 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11676 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11686 ctl_scsiio(struct ctl_scsiio *ctsio)
11689 const struct ctl_cmd_entry *entry;
11691 retval = CTL_RETVAL_COMPLETE;
11693 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11695 entry = ctl_get_cmd_entry(ctsio);
11698 * If this I/O has been aborted, just send it straight to
11699 * ctl_done() without executing it.
11701 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11702 ctl_done((union ctl_io *)ctsio);
11707 * All the checks should have been handled by ctl_scsiio_precheck().
11708 * We should be clear now to just execute the I/O.
11710 retval = entry->execute(ctsio);
11717 * Since we only implement one target right now, a bus reset simply resets
11718 * our single target.
11721 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11723 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11727 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11728 ctl_ua_type ua_type)
11730 struct ctl_lun *lun;
11733 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11734 union ctl_ha_msg msg_info;
11736 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11737 msg_info.hdr.nexus = io->io_hdr.nexus;
11738 if (ua_type==CTL_UA_TARG_RESET)
11739 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11741 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11742 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11743 msg_info.hdr.original_sc = NULL;
11744 msg_info.hdr.serializing_sc = NULL;
11745 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11746 (void *)&msg_info, sizeof(msg_info), 0)) {
11751 mtx_lock(&ctl_softc->ctl_lock);
11752 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11753 retval += ctl_lun_reset(lun, io, ua_type);
11754 mtx_unlock(&ctl_softc->ctl_lock);
11760 * The LUN should always be set. The I/O is optional, and is used to
11761 * distinguish between I/Os sent by this initiator, and by other
11762 * initiators. We set unit attention for initiators other than this one.
11763 * SAM-3 is vague on this point. It does say that a unit attention should
11764 * be established for other initiators when a LUN is reset (see section
11765 * 5.7.3), but it doesn't specifically say that the unit attention should
11766 * be established for this particular initiator when a LUN is reset. Here
11767 * is the relevant text, from SAM-3 rev 8:
11769 * 5.7.2 When a SCSI initiator port aborts its own tasks
11771 * When a SCSI initiator port causes its own task(s) to be aborted, no
11772 * notification that the task(s) have been aborted shall be returned to
11773 * the SCSI initiator port other than the completion response for the
11774 * command or task management function action that caused the task(s) to
11775 * be aborted and notification(s) associated with related effects of the
11776 * action (e.g., a reset unit attention condition).
11778 * XXX KDM for now, we're setting unit attention for all initiators.
11781 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11785 uint32_t initindex;
11789 mtx_lock(&lun->lun_lock);
11791 * Run through the OOA queue and abort each I/O.
11794 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11796 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11797 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11798 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11802 * This version sets unit attention for every
11805 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11806 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11807 if (initindex == i)
11809 lun->pending_sense[i].ua_pending |= ua_type;
11814 * A reset (any kind, really) clears reservations established with
11815 * RESERVE/RELEASE. It does not clear reservations established
11816 * with PERSISTENT RESERVE OUT, but we don't support that at the
11817 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11818 * reservations made with the RESERVE/RELEASE commands, because
11819 * those commands are obsolete in SPC-3.
11821 lun->flags &= ~CTL_LUN_RESERVED;
11823 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11824 ctl_clear_mask(lun->have_ca, i);
11825 lun->pending_sense[i].ua_pending |= ua_type;
11827 mtx_unlock(&lun->lun_lock);
11833 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11839 mtx_assert(&lun->lun_lock, MA_OWNED);
11842 * Run through the OOA queue and attempt to find the given I/O.
11843 * The target port, initiator ID, tag type and tag number have to
11844 * match the values that we got from the initiator. If we have an
11845 * untagged command to abort, simply abort the first untagged command
11846 * we come to. We only allow one untagged command at a time of course.
11848 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11849 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11851 if ((targ_port == xio->io_hdr.nexus.targ_port) &&
11852 (init_id == xio->io_hdr.nexus.initid.id)) {
11853 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11855 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11856 union ctl_ha_msg msg_info;
11858 msg_info.hdr.nexus = xio->io_hdr.nexus;
11859 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11860 msg_info.task.tag_num = xio->scsiio.tag_num;
11861 msg_info.task.tag_type = xio->scsiio.tag_type;
11862 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11863 msg_info.hdr.original_sc = NULL;
11864 msg_info.hdr.serializing_sc = NULL;
11865 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11866 (void *)&msg_info, sizeof(msg_info), 0);
11874 ctl_abort_task_set(union ctl_io *io)
11876 struct ctl_softc *softc = control_softc;
11877 struct ctl_lun *lun;
11883 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11884 mtx_lock(&softc->ctl_lock);
11885 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
11886 lun = softc->ctl_luns[targ_lun];
11888 mtx_unlock(&softc->ctl_lock);
11892 mtx_lock(&lun->lun_lock);
11893 mtx_unlock(&softc->ctl_lock);
11894 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11895 io->io_hdr.nexus.initid.id,
11896 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11897 mtx_unlock(&lun->lun_lock);
11902 ctl_i_t_nexus_reset(union ctl_io *io)
11904 struct ctl_softc *softc = control_softc;
11905 struct ctl_lun *lun;
11906 uint32_t initindex;
11908 initindex = ctl_get_initindex(&io->io_hdr.nexus);
11909 mtx_lock(&softc->ctl_lock);
11910 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11911 mtx_lock(&lun->lun_lock);
11912 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11913 io->io_hdr.nexus.initid.id,
11914 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11915 ctl_clear_mask(lun->have_ca, initindex);
11916 lun->pending_sense[initindex].ua_pending |= CTL_UA_I_T_NEXUS_LOSS;
11917 mtx_unlock(&lun->lun_lock);
11919 mtx_unlock(&softc->ctl_lock);
11924 ctl_abort_task(union ctl_io *io)
11927 struct ctl_lun *lun;
11928 struct ctl_softc *ctl_softc;
11931 char printbuf[128];
11936 ctl_softc = control_softc;
11942 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11943 mtx_lock(&ctl_softc->ctl_lock);
11944 if ((targ_lun < CTL_MAX_LUNS)
11945 && (ctl_softc->ctl_luns[targ_lun] != NULL))
11946 lun = ctl_softc->ctl_luns[targ_lun];
11948 mtx_unlock(&ctl_softc->ctl_lock);
11953 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11954 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11957 mtx_lock(&lun->lun_lock);
11958 mtx_unlock(&ctl_softc->ctl_lock);
11960 * Run through the OOA queue and attempt to find the given I/O.
11961 * The target port, initiator ID, tag type and tag number have to
11962 * match the values that we got from the initiator. If we have an
11963 * untagged command to abort, simply abort the first untagged command
11964 * we come to. We only allow one untagged command at a time of course.
11967 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11969 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11970 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11972 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11974 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11975 lun->lun, xio->scsiio.tag_num,
11976 xio->scsiio.tag_type,
11977 (xio->io_hdr.blocked_links.tqe_prev
11978 == NULL) ? "" : " BLOCKED",
11979 (xio->io_hdr.flags &
11980 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11981 (xio->io_hdr.flags &
11982 CTL_FLAG_ABORT) ? " ABORT" : "",
11983 (xio->io_hdr.flags &
11984 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11985 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11987 printf("%s\n", sbuf_data(&sb));
11990 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
11991 && (xio->io_hdr.nexus.initid.id ==
11992 io->io_hdr.nexus.initid.id)) {
11994 * If the abort says that the task is untagged, the
11995 * task in the queue must be untagged. Otherwise,
11996 * we just check to see whether the tag numbers
11997 * match. This is because the QLogic firmware
11998 * doesn't pass back the tag type in an abort
12002 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12003 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12004 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12007 * XXX KDM we've got problems with FC, because it
12008 * doesn't send down a tag type with aborts. So we
12009 * can only really go by the tag number...
12010 * This may cause problems with parallel SCSI.
12011 * Need to figure that out!!
12013 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12014 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12016 if ((io->io_hdr.flags &
12017 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12018 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12019 union ctl_ha_msg msg_info;
12021 io->io_hdr.flags |=
12022 CTL_FLAG_SENT_2OTHER_SC;
12023 msg_info.hdr.nexus = io->io_hdr.nexus;
12024 msg_info.task.task_action =
12025 CTL_TASK_ABORT_TASK;
12026 msg_info.task.tag_num =
12027 io->taskio.tag_num;
12028 msg_info.task.tag_type =
12029 io->taskio.tag_type;
12030 msg_info.hdr.msg_type =
12031 CTL_MSG_MANAGE_TASKS;
12032 msg_info.hdr.original_sc = NULL;
12033 msg_info.hdr.serializing_sc = NULL;
12035 printf("Sent Abort to other side\n");
12037 if (CTL_HA_STATUS_SUCCESS !=
12038 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12040 sizeof(msg_info), 0)) {
12044 printf("ctl_abort_task: found I/O to abort\n");
12050 mtx_unlock(&lun->lun_lock);
12056 * This isn't really an error. It's entirely possible for
12057 * the abort and command completion to cross on the wire.
12058 * This is more of an informative/diagnostic error.
12061 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12062 "%d:%d:%d:%d tag %d type %d\n",
12063 io->io_hdr.nexus.initid.id,
12064 io->io_hdr.nexus.targ_port,
12065 io->io_hdr.nexus.targ_target.id,
12066 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12067 io->taskio.tag_type);
12075 ctl_run_task(union ctl_io *io)
12077 struct ctl_softc *ctl_softc;
12079 const char *task_desc;
12081 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12083 ctl_softc = control_softc;
12086 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12087 ("ctl_run_task: Unextected io_type %d\n",
12088 io->io_hdr.io_type));
12090 task_desc = ctl_scsi_task_string(&io->taskio);
12091 if (task_desc != NULL) {
12093 csevent_log(CSC_CTL | CSC_SHELF_SW |
12095 csevent_LogType_Trace,
12096 csevent_Severity_Information,
12097 csevent_AlertLevel_Green,
12098 csevent_FRU_Firmware,
12099 csevent_FRU_Unknown,
12100 "CTL: received task: %s",task_desc);
12104 csevent_log(CSC_CTL | CSC_SHELF_SW |
12106 csevent_LogType_Trace,
12107 csevent_Severity_Information,
12108 csevent_AlertLevel_Green,
12109 csevent_FRU_Firmware,
12110 csevent_FRU_Unknown,
12111 "CTL: received unknown task "
12113 io->taskio.task_action,
12114 io->taskio.task_action);
12117 switch (io->taskio.task_action) {
12118 case CTL_TASK_ABORT_TASK:
12119 retval = ctl_abort_task(io);
12121 case CTL_TASK_ABORT_TASK_SET:
12122 retval = ctl_abort_task_set(io);
12124 case CTL_TASK_CLEAR_ACA:
12126 case CTL_TASK_CLEAR_TASK_SET:
12128 case CTL_TASK_I_T_NEXUS_RESET:
12129 retval = ctl_i_t_nexus_reset(io);
12131 case CTL_TASK_LUN_RESET: {
12132 struct ctl_lun *lun;
12136 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12137 mtx_lock(&ctl_softc->ctl_lock);
12138 if ((targ_lun < CTL_MAX_LUNS)
12139 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12140 lun = ctl_softc->ctl_luns[targ_lun];
12142 mtx_unlock(&ctl_softc->ctl_lock);
12147 if (!(io->io_hdr.flags &
12148 CTL_FLAG_FROM_OTHER_SC)) {
12149 union ctl_ha_msg msg_info;
12151 io->io_hdr.flags |=
12152 CTL_FLAG_SENT_2OTHER_SC;
12153 msg_info.hdr.msg_type =
12154 CTL_MSG_MANAGE_TASKS;
12155 msg_info.hdr.nexus = io->io_hdr.nexus;
12156 msg_info.task.task_action =
12157 CTL_TASK_LUN_RESET;
12158 msg_info.hdr.original_sc = NULL;
12159 msg_info.hdr.serializing_sc = NULL;
12160 if (CTL_HA_STATUS_SUCCESS !=
12161 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12163 sizeof(msg_info), 0)) {
12167 retval = ctl_lun_reset(lun, io,
12169 mtx_unlock(&ctl_softc->ctl_lock);
12172 case CTL_TASK_TARGET_RESET:
12173 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12175 case CTL_TASK_BUS_RESET:
12176 retval = ctl_bus_reset(ctl_softc, io);
12178 case CTL_TASK_PORT_LOGIN:
12180 case CTL_TASK_PORT_LOGOUT:
12183 printf("ctl_run_task: got unknown task management event %d\n",
12184 io->taskio.task_action);
12188 io->io_hdr.status = CTL_SUCCESS;
12190 io->io_hdr.status = CTL_ERROR;
12193 * This will queue this I/O to the done queue, but the
12194 * work thread won't be able to process it until we
12195 * return and the lock is released.
12201 * For HA operation. Handle commands that come in from the other
12205 ctl_handle_isc(union ctl_io *io)
12208 struct ctl_lun *lun;
12209 struct ctl_softc *ctl_softc;
12212 ctl_softc = control_softc;
12214 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12215 lun = ctl_softc->ctl_luns[targ_lun];
12217 switch (io->io_hdr.msg_type) {
12218 case CTL_MSG_SERIALIZE:
12219 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12221 case CTL_MSG_R2R: {
12222 const struct ctl_cmd_entry *entry;
12225 * This is only used in SER_ONLY mode.
12228 entry = ctl_get_cmd_entry(&io->scsiio);
12229 mtx_lock(&lun->lun_lock);
12230 if (ctl_scsiio_lun_check(ctl_softc, lun,
12231 entry, (struct ctl_scsiio *)io) != 0) {
12232 mtx_unlock(&lun->lun_lock);
12236 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12237 mtx_unlock(&lun->lun_lock);
12238 ctl_enqueue_rtr(io);
12241 case CTL_MSG_FINISH_IO:
12242 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12247 mtx_lock(&lun->lun_lock);
12248 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12250 ctl_check_blocked(lun);
12251 mtx_unlock(&lun->lun_lock);
12254 case CTL_MSG_PERS_ACTION:
12255 ctl_hndl_per_res_out_on_other_sc(
12256 (union ctl_ha_msg *)&io->presio.pr_msg);
12259 case CTL_MSG_BAD_JUJU:
12263 case CTL_MSG_DATAMOVE:
12264 /* Only used in XFER mode */
12266 ctl_datamove_remote(io);
12268 case CTL_MSG_DATAMOVE_DONE:
12269 /* Only used in XFER mode */
12271 io->scsiio.be_move_done(io);
12275 printf("%s: Invalid message type %d\n",
12276 __func__, io->io_hdr.msg_type);
12286 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12287 * there is no match.
12289 static ctl_lun_error_pattern
12290 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12292 const struct ctl_cmd_entry *entry;
12293 ctl_lun_error_pattern filtered_pattern, pattern;
12295 pattern = desc->error_pattern;
12298 * XXX KDM we need more data passed into this function to match a
12299 * custom pattern, and we actually need to implement custom pattern
12302 if (pattern & CTL_LUN_PAT_CMD)
12303 return (CTL_LUN_PAT_CMD);
12305 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12306 return (CTL_LUN_PAT_ANY);
12308 entry = ctl_get_cmd_entry(ctsio);
12310 filtered_pattern = entry->pattern & pattern;
12313 * If the user requested specific flags in the pattern (e.g.
12314 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12317 * If the user did not specify any flags, it doesn't matter whether
12318 * or not the command supports the flags.
12320 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12321 (pattern & ~CTL_LUN_PAT_MASK))
12322 return (CTL_LUN_PAT_NONE);
12325 * If the user asked for a range check, see if the requested LBA
12326 * range overlaps with this command's LBA range.
12328 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12334 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12336 return (CTL_LUN_PAT_NONE);
12338 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12339 desc->lba_range.len);
12341 * A "pass" means that the LBA ranges don't overlap, so
12342 * this doesn't match the user's range criteria.
12344 if (action == CTL_ACTION_PASS)
12345 return (CTL_LUN_PAT_NONE);
12348 return (filtered_pattern);
12352 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12354 struct ctl_error_desc *desc, *desc2;
12356 mtx_assert(&lun->lun_lock, MA_OWNED);
12358 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12359 ctl_lun_error_pattern pattern;
12361 * Check to see whether this particular command matches
12362 * the pattern in the descriptor.
12364 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12365 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12368 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12369 case CTL_LUN_INJ_ABORTED:
12370 ctl_set_aborted(&io->scsiio);
12372 case CTL_LUN_INJ_MEDIUM_ERR:
12373 ctl_set_medium_error(&io->scsiio);
12375 case CTL_LUN_INJ_UA:
12376 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12378 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12380 case CTL_LUN_INJ_CUSTOM:
12382 * We're assuming the user knows what he is doing.
12383 * Just copy the sense information without doing
12386 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12387 ctl_min(sizeof(desc->custom_sense),
12388 sizeof(io->scsiio.sense_data)));
12389 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12390 io->scsiio.sense_len = SSD_FULL_SIZE;
12391 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12393 case CTL_LUN_INJ_NONE:
12396 * If this is an error injection type we don't know
12397 * about, clear the continuous flag (if it is set)
12398 * so it will get deleted below.
12400 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12404 * By default, each error injection action is a one-shot
12406 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12409 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12415 #ifdef CTL_IO_DELAY
12417 ctl_datamove_timer_wakeup(void *arg)
12421 io = (union ctl_io *)arg;
12425 #endif /* CTL_IO_DELAY */
12428 ctl_datamove(union ctl_io *io)
12430 void (*fe_datamove)(union ctl_io *io);
12432 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12434 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12437 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12442 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12443 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12445 sbuf_cat(&sb, path_str);
12446 switch (io->io_hdr.io_type) {
12448 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12449 sbuf_printf(&sb, "\n");
12450 sbuf_cat(&sb, path_str);
12451 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12452 io->scsiio.tag_num, io->scsiio.tag_type);
12455 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12456 "Tag Type: %d\n", io->taskio.task_action,
12457 io->taskio.tag_num, io->taskio.tag_type);
12460 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12461 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12464 sbuf_cat(&sb, path_str);
12465 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12466 (intmax_t)time_uptime - io->io_hdr.start_time);
12468 printf("%s", sbuf_data(&sb));
12470 #endif /* CTL_TIME_IO */
12472 #ifdef CTL_IO_DELAY
12473 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12474 struct ctl_lun *lun;
12476 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12478 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12480 struct ctl_lun *lun;
12482 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12484 && (lun->delay_info.datamove_delay > 0)) {
12485 struct callout *callout;
12487 callout = (struct callout *)&io->io_hdr.timer_bytes;
12488 callout_init(callout, /*mpsafe*/ 1);
12489 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12490 callout_reset(callout,
12491 lun->delay_info.datamove_delay * hz,
12492 ctl_datamove_timer_wakeup, io);
12493 if (lun->delay_info.datamove_type ==
12494 CTL_DELAY_TYPE_ONESHOT)
12495 lun->delay_info.datamove_delay = 0;
12502 * This command has been aborted. Set the port status, so we fail
12505 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12506 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12507 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12508 io->io_hdr.nexus.targ_port,
12509 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12510 io->io_hdr.nexus.targ_lun);
12511 io->io_hdr.status = CTL_CMD_ABORTED;
12512 io->io_hdr.port_status = 31337;
12514 * Note that the backend, in this case, will get the
12515 * callback in its context. In other cases it may get
12516 * called in the frontend's interrupt thread context.
12518 io->scsiio.be_move_done(io);
12523 * If we're in XFER mode and this I/O is from the other shelf
12524 * controller, we need to send the DMA to the other side to
12525 * actually transfer the data to/from the host. In serialize only
12526 * mode the transfer happens below CTL and ctl_datamove() is only
12527 * called on the machine that originally received the I/O.
12529 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12530 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12531 union ctl_ha_msg msg;
12532 uint32_t sg_entries_sent;
12536 memset(&msg, 0, sizeof(msg));
12537 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12538 msg.hdr.original_sc = io->io_hdr.original_sc;
12539 msg.hdr.serializing_sc = io;
12540 msg.hdr.nexus = io->io_hdr.nexus;
12541 msg.dt.flags = io->io_hdr.flags;
12543 * We convert everything into a S/G list here. We can't
12544 * pass by reference, only by value between controllers.
12545 * So we can't pass a pointer to the S/G list, only as many
12546 * S/G entries as we can fit in here. If it's possible for
12547 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12548 * then we need to break this up into multiple transfers.
12550 if (io->scsiio.kern_sg_entries == 0) {
12551 msg.dt.kern_sg_entries = 1;
12553 * If this is in cached memory, flush the cache
12554 * before we send the DMA request to the other
12555 * controller. We want to do this in either the
12556 * read or the write case. The read case is
12557 * straightforward. In the write case, we want to
12558 * make sure nothing is in the local cache that
12559 * could overwrite the DMAed data.
12561 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12563 * XXX KDM use bus_dmamap_sync() here.
12568 * Convert to a physical address if this is a
12571 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12572 msg.dt.sg_list[0].addr =
12573 io->scsiio.kern_data_ptr;
12576 * XXX KDM use busdma here!
12579 msg.dt.sg_list[0].addr = (void *)
12580 vtophys(io->scsiio.kern_data_ptr);
12584 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12587 struct ctl_sg_entry *sgl;
12590 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12591 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12592 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12594 * XXX KDM use bus_dmamap_sync() here.
12599 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12600 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12601 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12602 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12603 msg.dt.sg_sequence = 0;
12606 * Loop until we've sent all of the S/G entries. On the
12607 * other end, we'll recompose these S/G entries into one
12608 * contiguous list before passing it to the
12610 for (sg_entries_sent = 0; sg_entries_sent <
12611 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12612 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12613 sizeof(msg.dt.sg_list[0])),
12614 msg.dt.kern_sg_entries - sg_entries_sent);
12616 if (do_sg_copy != 0) {
12617 struct ctl_sg_entry *sgl;
12620 sgl = (struct ctl_sg_entry *)
12621 io->scsiio.kern_data_ptr;
12623 * If this is in cached memory, flush the cache
12624 * before we send the DMA request to the other
12625 * controller. We want to do this in either
12626 * the * read or the write case. The read
12627 * case is straightforward. In the write
12628 * case, we want to make sure nothing is
12629 * in the local cache that could overwrite
12633 for (i = sg_entries_sent, j = 0;
12634 i < msg.dt.cur_sg_entries; i++, j++) {
12635 if ((io->io_hdr.flags &
12636 CTL_FLAG_NO_DATASYNC) == 0) {
12638 * XXX KDM use bus_dmamap_sync()
12641 if ((io->io_hdr.flags &
12642 CTL_FLAG_BUS_ADDR) == 0) {
12644 * XXX KDM use busdma.
12647 msg.dt.sg_list[j].addr =(void *)
12648 vtophys(sgl[i].addr);
12651 msg.dt.sg_list[j].addr =
12654 msg.dt.sg_list[j].len = sgl[i].len;
12658 sg_entries_sent += msg.dt.cur_sg_entries;
12659 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12660 msg.dt.sg_last = 1;
12662 msg.dt.sg_last = 0;
12665 * XXX KDM drop and reacquire the lock here?
12667 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12668 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12670 * XXX do something here.
12674 msg.dt.sent_sg_entries = sg_entries_sent;
12676 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12677 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12678 ctl_failover_io(io, /*have_lock*/ 0);
12683 * Lookup the fe_datamove() function for this particular
12687 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12694 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12696 union ctl_ha_msg msg;
12699 memset(&msg, 0, sizeof(msg));
12701 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12702 msg.hdr.original_sc = io;
12703 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12704 msg.hdr.nexus = io->io_hdr.nexus;
12705 msg.hdr.status = io->io_hdr.status;
12706 msg.scsi.tag_num = io->scsiio.tag_num;
12707 msg.scsi.tag_type = io->scsiio.tag_type;
12708 msg.scsi.scsi_status = io->scsiio.scsi_status;
12709 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12710 sizeof(io->scsiio.sense_data));
12711 msg.scsi.sense_len = io->scsiio.sense_len;
12712 msg.scsi.sense_residual = io->scsiio.sense_residual;
12713 msg.scsi.fetd_status = io->io_hdr.port_status;
12714 msg.scsi.residual = io->scsiio.residual;
12715 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12717 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12718 ctl_failover_io(io, /*have_lock*/ have_lock);
12722 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12723 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12724 /* XXX do something if this fails */
12730 * The DMA to the remote side is done, now we need to tell the other side
12731 * we're done so it can continue with its data movement.
12734 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12740 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12741 printf("%s: ISC DMA write failed with error %d", __func__,
12743 ctl_set_internal_failure(&io->scsiio,
12745 /*retry_count*/ rq->ret);
12748 ctl_dt_req_free(rq);
12751 * In this case, we had to malloc the memory locally. Free it.
12753 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12755 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12756 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12759 * The data is in local and remote memory, so now we need to send
12760 * status (good or back) back to the other side.
12762 ctl_send_datamove_done(io, /*have_lock*/ 0);
12766 * We've moved the data from the host/controller into local memory. Now we
12767 * need to push it over to the remote controller's memory.
12770 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12776 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12777 ctl_datamove_remote_write_cb);
12783 ctl_datamove_remote_write(union ctl_io *io)
12786 void (*fe_datamove)(union ctl_io *io);
12789 * - Get the data from the host/HBA into local memory.
12790 * - DMA memory from the local controller to the remote controller.
12791 * - Send status back to the remote controller.
12794 retval = ctl_datamove_remote_sgl_setup(io);
12798 /* Switch the pointer over so the FETD knows what to do */
12799 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12802 * Use a custom move done callback, since we need to send completion
12803 * back to the other controller, not to the backend on this side.
12805 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12807 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12816 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12825 * In this case, we had to malloc the memory locally. Free it.
12827 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12829 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12830 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12834 scsi_path_string(io, path_str, sizeof(path_str));
12835 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12836 sbuf_cat(&sb, path_str);
12837 scsi_command_string(&io->scsiio, NULL, &sb);
12838 sbuf_printf(&sb, "\n");
12839 sbuf_cat(&sb, path_str);
12840 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12841 io->scsiio.tag_num, io->scsiio.tag_type);
12842 sbuf_cat(&sb, path_str);
12843 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12844 io->io_hdr.flags, io->io_hdr.status);
12846 printk("%s", sbuf_data(&sb));
12851 * The read is done, now we need to send status (good or bad) back
12852 * to the other side.
12854 ctl_send_datamove_done(io, /*have_lock*/ 0);
12860 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12863 void (*fe_datamove)(union ctl_io *io);
12867 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12868 printf("%s: ISC DMA read failed with error %d", __func__,
12870 ctl_set_internal_failure(&io->scsiio,
12872 /*retry_count*/ rq->ret);
12875 ctl_dt_req_free(rq);
12877 /* Switch the pointer over so the FETD knows what to do */
12878 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12881 * Use a custom move done callback, since we need to send completion
12882 * back to the other controller, not to the backend on this side.
12884 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12886 /* XXX KDM add checks like the ones in ctl_datamove? */
12888 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12894 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12896 struct ctl_sg_entry *local_sglist, *remote_sglist;
12897 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
12898 struct ctl_softc *softc;
12903 softc = control_softc;
12905 local_sglist = io->io_hdr.local_sglist;
12906 local_dma_sglist = io->io_hdr.local_dma_sglist;
12907 remote_sglist = io->io_hdr.remote_sglist;
12908 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
12910 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
12911 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
12912 local_sglist[i].len = remote_sglist[i].len;
12915 * XXX Detect the situation where the RS-level I/O
12916 * redirector on the other side has already read the
12917 * data off of the AOR RS on this side, and
12918 * transferred it to remote (mirror) memory on the
12919 * other side. Since we already have the data in
12920 * memory here, we just need to use it.
12922 * XXX KDM this can probably be removed once we
12923 * get the cache device code in and take the
12924 * current AOR implementation out.
12927 if ((remote_sglist[i].addr >=
12928 (void *)vtophys(softc->mirr->addr))
12929 && (remote_sglist[i].addr <
12930 ((void *)vtophys(softc->mirr->addr) +
12931 CacheMirrorOffset))) {
12932 local_sglist[i].addr = remote_sglist[i].addr -
12934 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12936 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
12938 local_sglist[i].addr = remote_sglist[i].addr +
12943 printf("%s: local %p, remote %p, len %d\n",
12944 __func__, local_sglist[i].addr,
12945 remote_sglist[i].addr, local_sglist[i].len);
12949 uint32_t len_to_go;
12952 * In this case, we don't have automatically allocated
12953 * memory for this I/O on this controller. This typically
12954 * happens with internal CTL I/O -- e.g. inquiry, mode
12955 * sense, etc. Anything coming from RAIDCore will have
12956 * a mirror area available.
12958 len_to_go = io->scsiio.kern_data_len;
12961 * Clear the no datasync flag, we have to use malloced
12964 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
12967 * The difficult thing here is that the size of the various
12968 * S/G segments may be different than the size from the
12969 * remote controller. That'll make it harder when DMAing
12970 * the data back to the other side.
12972 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
12973 sizeof(io->io_hdr.remote_sglist[0])) &&
12974 (len_to_go > 0); i++) {
12975 local_sglist[i].len = ctl_min(len_to_go, 131072);
12976 CTL_SIZE_8B(local_dma_sglist[i].len,
12977 local_sglist[i].len);
12978 local_sglist[i].addr =
12979 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
12981 local_dma_sglist[i].addr = local_sglist[i].addr;
12983 if (local_sglist[i].addr == NULL) {
12986 printf("malloc failed for %zd bytes!",
12987 local_dma_sglist[i].len);
12988 for (j = 0; j < i; j++) {
12989 free(local_sglist[j].addr, M_CTL);
12991 ctl_set_internal_failure(&io->scsiio,
12993 /*retry_count*/ 4857);
12995 goto bailout_error;
12998 /* XXX KDM do we need a sync here? */
13000 len_to_go -= local_sglist[i].len;
13003 * Reset the number of S/G entries accordingly. The
13004 * original number of S/G entries is available in
13007 io->scsiio.kern_sg_entries = i;
13010 printf("%s: kern_sg_entries = %d\n", __func__,
13011 io->scsiio.kern_sg_entries);
13012 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13013 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13014 local_sglist[i].addr, local_sglist[i].len,
13015 local_dma_sglist[i].len);
13024 ctl_send_datamove_done(io, /*have_lock*/ 0);
13030 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13031 ctl_ha_dt_cb callback)
13033 struct ctl_ha_dt_req *rq;
13034 struct ctl_sg_entry *remote_sglist, *local_sglist;
13035 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13036 uint32_t local_used, remote_used, total_used;
13042 rq = ctl_dt_req_alloc();
13045 * If we failed to allocate the request, and if the DMA didn't fail
13046 * anyway, set busy status. This is just a resource allocation
13050 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13051 ctl_set_busy(&io->scsiio);
13053 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13056 ctl_dt_req_free(rq);
13059 * The data move failed. We need to return status back
13060 * to the other controller. No point in trying to DMA
13061 * data to the remote controller.
13064 ctl_send_datamove_done(io, /*have_lock*/ 0);
13071 local_sglist = io->io_hdr.local_sglist;
13072 local_dma_sglist = io->io_hdr.local_dma_sglist;
13073 remote_sglist = io->io_hdr.remote_sglist;
13074 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13079 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13080 rq->ret = CTL_HA_STATUS_SUCCESS;
13087 * Pull/push the data over the wire from/to the other controller.
13088 * This takes into account the possibility that the local and
13089 * remote sglists may not be identical in terms of the size of
13090 * the elements and the number of elements.
13092 * One fundamental assumption here is that the length allocated for
13093 * both the local and remote sglists is identical. Otherwise, we've
13094 * essentially got a coding error of some sort.
13096 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13098 uint32_t cur_len, dma_length;
13101 rq->id = CTL_HA_DATA_CTL;
13102 rq->command = command;
13106 * Both pointers should be aligned. But it is possible
13107 * that the allocation length is not. They should both
13108 * also have enough slack left over at the end, though,
13109 * to round up to the next 8 byte boundary.
13111 cur_len = ctl_min(local_sglist[i].len - local_used,
13112 remote_sglist[j].len - remote_used);
13115 * In this case, we have a size issue and need to decrease
13116 * the size, except in the case where we actually have less
13117 * than 8 bytes left. In that case, we need to increase
13118 * the DMA length to get the last bit.
13120 if ((cur_len & 0x7) != 0) {
13121 if (cur_len > 0x7) {
13122 cur_len = cur_len - (cur_len & 0x7);
13123 dma_length = cur_len;
13125 CTL_SIZE_8B(dma_length, cur_len);
13129 dma_length = cur_len;
13132 * If we had to allocate memory for this I/O, instead of using
13133 * the non-cached mirror memory, we'll need to flush the cache
13134 * before trying to DMA to the other controller.
13136 * We could end up doing this multiple times for the same
13137 * segment if we have a larger local segment than remote
13138 * segment. That shouldn't be an issue.
13140 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13142 * XXX KDM use bus_dmamap_sync() here.
13146 rq->size = dma_length;
13148 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13149 tmp_ptr += local_used;
13151 /* Use physical addresses when talking to ISC hardware */
13152 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13153 /* XXX KDM use busdma */
13155 rq->local = vtophys(tmp_ptr);
13158 rq->local = tmp_ptr;
13160 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13161 tmp_ptr += remote_used;
13162 rq->remote = tmp_ptr;
13164 rq->callback = NULL;
13166 local_used += cur_len;
13167 if (local_used >= local_sglist[i].len) {
13172 remote_used += cur_len;
13173 if (remote_used >= remote_sglist[j].len) {
13177 total_used += cur_len;
13179 if (total_used >= io->scsiio.kern_data_len)
13180 rq->callback = callback;
13182 if ((rq->size & 0x7) != 0) {
13183 printf("%s: warning: size %d is not on 8b boundary\n",
13184 __func__, rq->size);
13186 if (((uintptr_t)rq->local & 0x7) != 0) {
13187 printf("%s: warning: local %p not on 8b boundary\n",
13188 __func__, rq->local);
13190 if (((uintptr_t)rq->remote & 0x7) != 0) {
13191 printf("%s: warning: remote %p not on 8b boundary\n",
13192 __func__, rq->local);
13195 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13196 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13197 rq->local, rq->remote, rq->size);
13200 isc_ret = ctl_dt_single(rq);
13201 if (isc_ret == CTL_HA_STATUS_WAIT)
13204 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13205 rq->ret = CTL_HA_STATUS_SUCCESS;
13219 ctl_datamove_remote_read(union ctl_io *io)
13225 * This will send an error to the other controller in the case of a
13228 retval = ctl_datamove_remote_sgl_setup(io);
13232 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13233 ctl_datamove_remote_read_cb);
13235 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13237 * Make sure we free memory if there was an error.. The
13238 * ctl_datamove_remote_xfer() function will send the
13239 * datamove done message, or call the callback with an
13240 * error if there is a problem.
13242 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13243 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13250 * Process a datamove request from the other controller. This is used for
13251 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13252 * first. Once that is complete, the data gets DMAed into the remote
13253 * controller's memory. For reads, we DMA from the remote controller's
13254 * memory into our memory first, and then move it out to the FETD.
13257 ctl_datamove_remote(union ctl_io *io)
13259 struct ctl_softc *softc;
13261 softc = control_softc;
13263 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13266 * Note that we look for an aborted I/O here, but don't do some of
13267 * the other checks that ctl_datamove() normally does. We don't
13268 * need to run the task queue, because this I/O is on the ISC
13269 * queue, which is executed by the work thread after the task queue.
13270 * We don't need to run the datamove delay code, since that should
13271 * have been done if need be on the other controller.
13273 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13275 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13276 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13277 io->io_hdr.nexus.targ_port,
13278 io->io_hdr.nexus.targ_target.id,
13279 io->io_hdr.nexus.targ_lun);
13280 io->io_hdr.status = CTL_CMD_ABORTED;
13281 io->io_hdr.port_status = 31338;
13283 ctl_send_datamove_done(io, /*have_lock*/ 0);
13288 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13289 ctl_datamove_remote_write(io);
13290 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13291 ctl_datamove_remote_read(io);
13293 union ctl_ha_msg msg;
13294 struct scsi_sense_data *sense;
13298 memset(&msg, 0, sizeof(msg));
13300 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13301 msg.hdr.status = CTL_SCSI_ERROR;
13302 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13304 retry_count = 4243;
13306 sense = &msg.scsi.sense_data;
13307 sks[0] = SSD_SCS_VALID;
13308 sks[1] = (retry_count >> 8) & 0xff;
13309 sks[2] = retry_count & 0xff;
13311 /* "Internal target failure" */
13312 scsi_set_sense_data(sense,
13313 /*sense_format*/ SSD_TYPE_NONE,
13314 /*current_error*/ 1,
13315 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13318 /*type*/ SSD_ELEM_SKS,
13319 /*size*/ sizeof(sks),
13323 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13324 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13325 ctl_failover_io(io, /*have_lock*/ 1);
13329 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13330 CTL_HA_STATUS_SUCCESS) {
13331 /* XXX KDM what to do if this fails? */
13339 ctl_process_done(union ctl_io *io)
13341 struct ctl_lun *lun;
13342 struct ctl_softc *ctl_softc;
13343 void (*fe_done)(union ctl_io *io);
13344 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13346 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13349 control_softc->ctl_ports[targ_port]->fe_done;
13352 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13357 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13358 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13360 sbuf_cat(&sb, path_str);
13361 switch (io->io_hdr.io_type) {
13363 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13364 sbuf_printf(&sb, "\n");
13365 sbuf_cat(&sb, path_str);
13366 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13367 io->scsiio.tag_num, io->scsiio.tag_type);
13370 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13371 "Tag Type: %d\n", io->taskio.task_action,
13372 io->taskio.tag_num, io->taskio.tag_type);
13375 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13376 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13379 sbuf_cat(&sb, path_str);
13380 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13381 (intmax_t)time_uptime - io->io_hdr.start_time);
13383 printf("%s", sbuf_data(&sb));
13385 #endif /* CTL_TIME_IO */
13387 switch (io->io_hdr.io_type) {
13391 if (bootverbose || verbose > 0)
13392 ctl_io_error_print(io, NULL);
13393 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13397 return (CTL_RETVAL_COMPLETE);
13400 printf("ctl_process_done: invalid io type %d\n",
13401 io->io_hdr.io_type);
13402 panic("ctl_process_done: invalid io type %d\n",
13403 io->io_hdr.io_type);
13404 break; /* NOTREACHED */
13407 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13409 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13410 io->io_hdr.nexus.targ_mapped_lun));
13414 ctl_softc = lun->ctl_softc;
13416 mtx_lock(&lun->lun_lock);
13419 * Check to see if we have any errors to inject here. We only
13420 * inject errors for commands that don't already have errors set.
13422 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13423 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13424 ctl_inject_error(lun, io);
13427 * XXX KDM how do we treat commands that aren't completed
13430 * XXX KDM should we also track I/O latency?
13432 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13433 io->io_hdr.io_type == CTL_IO_SCSI) {
13435 struct bintime cur_bt;
13439 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13441 type = CTL_STATS_READ;
13442 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13444 type = CTL_STATS_WRITE;
13446 type = CTL_STATS_NO_IO;
13448 lun->stats.ports[targ_port].bytes[type] +=
13449 io->scsiio.kern_total_len;
13450 lun->stats.ports[targ_port].operations[type]++;
13452 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13453 &io->io_hdr.dma_bt);
13454 lun->stats.ports[targ_port].num_dmas[type] +=
13455 io->io_hdr.num_dmas;
13456 getbintime(&cur_bt);
13457 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13458 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13463 * Remove this from the OOA queue.
13465 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13468 * Run through the blocked queue on this LUN and see if anything
13469 * has become unblocked, now that this transaction is done.
13471 ctl_check_blocked(lun);
13474 * If the LUN has been invalidated, free it if there is nothing
13475 * left on its OOA queue.
13477 if ((lun->flags & CTL_LUN_INVALID)
13478 && TAILQ_EMPTY(&lun->ooa_queue)) {
13479 mtx_unlock(&lun->lun_lock);
13480 mtx_lock(&ctl_softc->ctl_lock);
13482 mtx_unlock(&ctl_softc->ctl_lock);
13484 mtx_unlock(&lun->lun_lock);
13487 * If this command has been aborted, make sure we set the status
13488 * properly. The FETD is responsible for freeing the I/O and doing
13489 * whatever it needs to do to clean up its state.
13491 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13492 io->io_hdr.status = CTL_CMD_ABORTED;
13495 * We print out status for every task management command. For SCSI
13496 * commands, we filter out any unit attention errors; they happen
13497 * on every boot, and would clutter up the log. Note: task
13498 * management commands aren't printed here, they are printed above,
13499 * since they should never even make it down here.
13501 switch (io->io_hdr.io_type) {
13502 case CTL_IO_SCSI: {
13503 int error_code, sense_key, asc, ascq;
13507 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
13508 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13510 * Since this is just for printing, no need to
13511 * show errors here.
13513 scsi_extract_sense_len(&io->scsiio.sense_data,
13514 io->scsiio.sense_len,
13519 /*show_errors*/ 0);
13522 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
13523 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
13524 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
13525 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
13527 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
13528 ctl_softc->skipped_prints++;
13530 uint32_t skipped_prints;
13532 skipped_prints = ctl_softc->skipped_prints;
13534 ctl_softc->skipped_prints = 0;
13535 ctl_softc->last_print_jiffies = time_uptime;
13537 if (skipped_prints > 0) {
13539 csevent_log(CSC_CTL | CSC_SHELF_SW |
13541 csevent_LogType_Trace,
13542 csevent_Severity_Information,
13543 csevent_AlertLevel_Green,
13544 csevent_FRU_Firmware,
13545 csevent_FRU_Unknown,
13546 "High CTL error volume, %d prints "
13547 "skipped", skipped_prints);
13550 if (bootverbose || verbose > 0)
13551 ctl_io_error_print(io, NULL);
13557 if (bootverbose || verbose > 0)
13558 ctl_io_error_print(io, NULL);
13565 * Tell the FETD or the other shelf controller we're done with this
13566 * command. Note that only SCSI commands get to this point. Task
13567 * management commands are completed above.
13569 * We only send status to the other controller if we're in XFER
13570 * mode. In SER_ONLY mode, the I/O is done on the controller that
13571 * received the I/O (from CTL's perspective), and so the status is
13574 * XXX KDM if we hold the lock here, we could cause a deadlock
13575 * if the frontend comes back in in this context to queue
13578 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13579 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13580 union ctl_ha_msg msg;
13582 memset(&msg, 0, sizeof(msg));
13583 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13584 msg.hdr.original_sc = io->io_hdr.original_sc;
13585 msg.hdr.nexus = io->io_hdr.nexus;
13586 msg.hdr.status = io->io_hdr.status;
13587 msg.scsi.scsi_status = io->scsiio.scsi_status;
13588 msg.scsi.tag_num = io->scsiio.tag_num;
13589 msg.scsi.tag_type = io->scsiio.tag_type;
13590 msg.scsi.sense_len = io->scsiio.sense_len;
13591 msg.scsi.sense_residual = io->scsiio.sense_residual;
13592 msg.scsi.residual = io->scsiio.residual;
13593 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13594 sizeof(io->scsiio.sense_data));
13596 * We copy this whether or not this is an I/O-related
13597 * command. Otherwise, we'd have to go and check to see
13598 * whether it's a read/write command, and it really isn't
13601 memcpy(&msg.scsi.lbalen,
13602 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13603 sizeof(msg.scsi.lbalen));
13605 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13606 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13607 /* XXX do something here */
13616 return (CTL_RETVAL_COMPLETE);
13620 * Front end should call this if it doesn't do autosense. When the request
13621 * sense comes back in from the initiator, we'll dequeue this and send it.
13624 ctl_queue_sense(union ctl_io *io)
13626 struct ctl_lun *lun;
13627 struct ctl_softc *ctl_softc;
13628 uint32_t initidx, targ_lun;
13630 ctl_softc = control_softc;
13632 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13635 * LUN lookup will likely move to the ctl_work_thread() once we
13636 * have our new queueing infrastructure (that doesn't put things on
13637 * a per-LUN queue initially). That is so that we can handle
13638 * things like an INQUIRY to a LUN that we don't have enabled. We
13639 * can't deal with that right now.
13641 mtx_lock(&ctl_softc->ctl_lock);
13644 * If we don't have a LUN for this, just toss the sense
13647 targ_lun = io->io_hdr.nexus.targ_lun;
13648 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13649 if ((targ_lun < CTL_MAX_LUNS)
13650 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13651 lun = ctl_softc->ctl_luns[targ_lun];
13655 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13657 mtx_lock(&lun->lun_lock);
13659 * Already have CA set for this LUN...toss the sense information.
13661 if (ctl_is_set(lun->have_ca, initidx)) {
13662 mtx_unlock(&lun->lun_lock);
13666 memcpy(&lun->pending_sense[initidx].sense, &io->scsiio.sense_data,
13667 ctl_min(sizeof(lun->pending_sense[initidx].sense),
13668 sizeof(io->scsiio.sense_data)));
13669 ctl_set_mask(lun->have_ca, initidx);
13670 mtx_unlock(&lun->lun_lock);
13673 mtx_unlock(&ctl_softc->ctl_lock);
13677 return (CTL_RETVAL_COMPLETE);
13681 * Primary command inlet from frontend ports. All SCSI and task I/O
13682 * requests must go through this function.
13685 ctl_queue(union ctl_io *io)
13687 struct ctl_softc *ctl_softc;
13689 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13691 ctl_softc = control_softc;
13694 io->io_hdr.start_time = time_uptime;
13695 getbintime(&io->io_hdr.start_bt);
13696 #endif /* CTL_TIME_IO */
13698 /* Map FE-specific LUN ID into global one. */
13699 io->io_hdr.nexus.targ_mapped_lun =
13700 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
13702 switch (io->io_hdr.io_type) {
13705 ctl_enqueue_incoming(io);
13708 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13712 return (CTL_RETVAL_COMPLETE);
13715 #ifdef CTL_IO_DELAY
13717 ctl_done_timer_wakeup(void *arg)
13721 io = (union ctl_io *)arg;
13724 #endif /* CTL_IO_DELAY */
13727 ctl_done(union ctl_io *io)
13729 struct ctl_softc *ctl_softc;
13731 ctl_softc = control_softc;
13734 * Enable this to catch duplicate completion issues.
13737 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13738 printf("%s: type %d msg %d cdb %x iptl: "
13739 "%d:%d:%d:%d tag 0x%04x "
13740 "flag %#x status %x\n",
13742 io->io_hdr.io_type,
13743 io->io_hdr.msg_type,
13745 io->io_hdr.nexus.initid.id,
13746 io->io_hdr.nexus.targ_port,
13747 io->io_hdr.nexus.targ_target.id,
13748 io->io_hdr.nexus.targ_lun,
13749 (io->io_hdr.io_type ==
13751 io->taskio.tag_num :
13752 io->scsiio.tag_num,
13754 io->io_hdr.status);
13756 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13760 * This is an internal copy of an I/O, and should not go through
13761 * the normal done processing logic.
13763 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13767 * We need to send a msg to the serializing shelf to finish the IO
13768 * as well. We don't send a finish message to the other shelf if
13769 * this is a task management command. Task management commands
13770 * aren't serialized in the OOA queue, but rather just executed on
13771 * both shelf controllers for commands that originated on that
13774 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13775 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13776 union ctl_ha_msg msg_io;
13778 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13779 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13780 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13781 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13783 /* continue on to finish IO */
13785 #ifdef CTL_IO_DELAY
13786 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13787 struct ctl_lun *lun;
13789 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13791 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13793 struct ctl_lun *lun;
13795 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13798 && (lun->delay_info.done_delay > 0)) {
13799 struct callout *callout;
13801 callout = (struct callout *)&io->io_hdr.timer_bytes;
13802 callout_init(callout, /*mpsafe*/ 1);
13803 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13804 callout_reset(callout,
13805 lun->delay_info.done_delay * hz,
13806 ctl_done_timer_wakeup, io);
13807 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13808 lun->delay_info.done_delay = 0;
13812 #endif /* CTL_IO_DELAY */
13814 ctl_enqueue_done(io);
13818 ctl_isc(struct ctl_scsiio *ctsio)
13820 struct ctl_lun *lun;
13823 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13825 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
13827 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
13829 retval = lun->backend->data_submit((union ctl_io *)ctsio);
13836 ctl_work_thread(void *arg)
13838 struct ctl_thread *thr = (struct ctl_thread *)arg;
13839 struct ctl_softc *softc = thr->ctl_softc;
13843 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13849 * We handle the queues in this order:
13851 * - done queue (to free up resources, unblock other commands)
13855 * If those queues are empty, we break out of the loop and
13858 mtx_lock(&thr->queue_lock);
13859 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13861 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13862 mtx_unlock(&thr->queue_lock);
13863 ctl_handle_isc(io);
13866 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13868 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13869 /* clear any blocked commands, call fe_done */
13870 mtx_unlock(&thr->queue_lock);
13871 retval = ctl_process_done(io);
13874 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13876 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13877 mtx_unlock(&thr->queue_lock);
13878 if (io->io_hdr.io_type == CTL_IO_TASK)
13881 ctl_scsiio_precheck(softc, &io->scsiio);
13884 if (!ctl_pause_rtr) {
13885 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13887 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13888 mtx_unlock(&thr->queue_lock);
13889 retval = ctl_scsiio(&io->scsiio);
13890 if (retval != CTL_RETVAL_COMPLETE)
13891 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13896 /* Sleep until we have something to do. */
13897 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13902 ctl_lun_thread(void *arg)
13904 struct ctl_softc *softc = (struct ctl_softc *)arg;
13905 struct ctl_be_lun *be_lun;
13908 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13912 mtx_lock(&softc->ctl_lock);
13913 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13914 if (be_lun != NULL) {
13915 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13916 mtx_unlock(&softc->ctl_lock);
13917 ctl_create_lun(be_lun);
13921 /* Sleep until we have something to do. */
13922 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13923 PDROP | PRIBIO, "-", 0);
13928 ctl_enqueue_incoming(union ctl_io *io)
13930 struct ctl_softc *softc = control_softc;
13931 struct ctl_thread *thr;
13934 idx = (io->io_hdr.nexus.targ_port * 127 +
13935 io->io_hdr.nexus.initid.id) % worker_threads;
13936 thr = &softc->threads[idx];
13937 mtx_lock(&thr->queue_lock);
13938 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13939 mtx_unlock(&thr->queue_lock);
13944 ctl_enqueue_rtr(union ctl_io *io)
13946 struct ctl_softc *softc = control_softc;
13947 struct ctl_thread *thr;
13949 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13950 mtx_lock(&thr->queue_lock);
13951 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13952 mtx_unlock(&thr->queue_lock);
13957 ctl_enqueue_done(union ctl_io *io)
13959 struct ctl_softc *softc = control_softc;
13960 struct ctl_thread *thr;
13962 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13963 mtx_lock(&thr->queue_lock);
13964 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13965 mtx_unlock(&thr->queue_lock);
13970 ctl_enqueue_isc(union ctl_io *io)
13972 struct ctl_softc *softc = control_softc;
13973 struct ctl_thread *thr;
13975 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13976 mtx_lock(&thr->queue_lock);
13977 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13978 mtx_unlock(&thr->queue_lock);
13982 /* Initialization and failover */
13985 ctl_init_isc_msg(void)
13987 printf("CTL: Still calling this thing\n");
13992 * Initializes component into configuration defined by bootMode
13994 * returns hasc_Status:
13996 * ERROR - fatal error
13998 static ctl_ha_comp_status
13999 ctl_isc_init(struct ctl_ha_component *c)
14001 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14008 * Starts component in state requested. If component starts successfully,
14009 * it must set its own state to the requestrd state
14010 * When requested state is HASC_STATE_HA, the component may refine it
14011 * by adding _SLAVE or _MASTER flags.
14012 * Currently allowed state transitions are:
14013 * UNKNOWN->HA - initial startup
14014 * UNKNOWN->SINGLE - initial startup when no parter detected
14015 * HA->SINGLE - failover
14016 * returns ctl_ha_comp_status:
14017 * OK - component successfully started in requested state
14018 * FAILED - could not start the requested state, failover may
14020 * ERROR - fatal error detected, no future startup possible
14022 static ctl_ha_comp_status
14023 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14025 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14027 printf("%s: go\n", __func__);
14029 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14030 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14032 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14033 != CTL_HA_STATUS_SUCCESS) {
14034 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14035 ret = CTL_HA_COMP_STATUS_ERROR;
14037 } else if (CTL_HA_STATE_IS_HA(c->state)
14038 && CTL_HA_STATE_IS_SINGLE(state)){
14039 // HA->SINGLE transition
14043 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14045 ret = CTL_HA_COMP_STATUS_ERROR;
14047 if (CTL_HA_STATE_IS_SINGLE(state))
14056 * Quiesce component
14057 * The component must clear any error conditions (set status to OK) and
14058 * prepare itself to another Start call
14059 * returns ctl_ha_comp_status:
14063 static ctl_ha_comp_status
14064 ctl_isc_quiesce(struct ctl_ha_component *c)
14066 int ret = CTL_HA_COMP_STATUS_OK;
14073 struct ctl_ha_component ctl_ha_component_ctlisc =
14076 .state = CTL_HA_STATE_UNKNOWN,
14077 .init = ctl_isc_init,
14078 .start = ctl_isc_start,
14079 .quiesce = ctl_isc_quiesce