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,
266 /*flags1*/ SCP_WCE | SCP_RCD,
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},
287 /*busy_timeout_period*/{0, 0},
288 /*extended_selftest_completion_time*/{0, 0}
291 static struct scsi_control_page control_page_changeable = {
292 /*page_code*/SMS_CONTROL_MODE_PAGE,
293 /*page_length*/sizeof(struct scsi_control_page) - 2,
298 /*aen_holdoff_period*/{0, 0},
299 /*busy_timeout_period*/{0, 0},
300 /*extended_selftest_completion_time*/{0, 0}
305 * XXX KDM move these into the softc.
307 static int rcv_sync_msg;
308 static int persis_offset;
309 static uint8_t ctl_pause_rtr;
310 static int ctl_is_single = 1;
311 static int index_to_aps_page;
313 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
314 static int worker_threads = -1;
315 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
316 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
317 &worker_threads, 1, "Number of worker threads");
318 static int verbose = 0;
319 TUNABLE_INT("kern.cam.ctl.verbose", &verbose);
320 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
321 &verbose, 0, "Show SCSI errors returned to initiator");
324 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
325 * Mode Page Policy (0x87),
326 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
327 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
329 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 9
331 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
333 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
334 static int ctl_init(void);
335 void ctl_shutdown(void);
336 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
337 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
338 static void ctl_ioctl_online(void *arg);
339 static void ctl_ioctl_offline(void *arg);
340 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
341 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
342 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
343 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
344 static int ctl_ioctl_submit_wait(union ctl_io *io);
345 static void ctl_ioctl_datamove(union ctl_io *io);
346 static void ctl_ioctl_done(union ctl_io *io);
347 static void ctl_ioctl_hard_startstop_callback(void *arg,
348 struct cfi_metatask *metatask);
349 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
350 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
351 struct ctl_ooa *ooa_hdr,
352 struct ctl_ooa_entry *kern_entries);
353 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
355 static uint32_t ctl_map_lun(int port_num, uint32_t lun);
356 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun);
358 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
359 uint32_t targ_target, uint32_t targ_lun,
361 static void ctl_kfree_io(union ctl_io *io);
363 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
364 struct ctl_be_lun *be_lun, struct ctl_id target_id);
365 static int ctl_free_lun(struct ctl_lun *lun);
366 static void ctl_create_lun(struct ctl_be_lun *be_lun);
368 static void ctl_failover_change_pages(struct ctl_softc *softc,
369 struct ctl_scsiio *ctsio, int master);
372 static int ctl_do_mode_select(union ctl_io *io);
373 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
374 uint64_t res_key, uint64_t sa_res_key,
375 uint8_t type, uint32_t residx,
376 struct ctl_scsiio *ctsio,
377 struct scsi_per_res_out *cdb,
378 struct scsi_per_res_out_parms* param);
379 static void ctl_pro_preempt_other(struct ctl_lun *lun,
380 union ctl_ha_msg *msg);
381 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
382 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
383 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
384 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
385 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
386 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
388 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
390 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
391 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
392 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
393 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
394 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len);
395 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
396 static ctl_action ctl_check_for_blockage(union ctl_io *pending_io,
397 union ctl_io *ooa_io);
398 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
399 union ctl_io *starting_io);
400 static int ctl_check_blocked(struct ctl_lun *lun);
401 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
403 const struct ctl_cmd_entry *entry,
404 struct ctl_scsiio *ctsio);
405 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
406 static void ctl_failover(void);
407 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
408 struct ctl_scsiio *ctsio);
409 static int ctl_scsiio(struct ctl_scsiio *ctsio);
411 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
412 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
413 ctl_ua_type ua_type);
414 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
415 ctl_ua_type ua_type);
416 static int ctl_abort_task(union ctl_io *io);
417 static int ctl_abort_task_set(union ctl_io *io);
418 static int ctl_i_t_nexus_reset(union ctl_io *io);
419 static void ctl_run_task(union ctl_io *io);
421 static void ctl_datamove_timer_wakeup(void *arg);
422 static void ctl_done_timer_wakeup(void *arg);
423 #endif /* CTL_IO_DELAY */
425 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
426 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
427 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
428 static void ctl_datamove_remote_write(union ctl_io *io);
429 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
430 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
431 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
432 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
433 ctl_ha_dt_cb callback);
434 static void ctl_datamove_remote_read(union ctl_io *io);
435 static void ctl_datamove_remote(union ctl_io *io);
436 static int ctl_process_done(union ctl_io *io);
437 static void ctl_lun_thread(void *arg);
438 static void ctl_work_thread(void *arg);
439 static void ctl_enqueue_incoming(union ctl_io *io);
440 static void ctl_enqueue_rtr(union ctl_io *io);
441 static void ctl_enqueue_done(union ctl_io *io);
442 static void ctl_enqueue_isc(union ctl_io *io);
443 static const struct ctl_cmd_entry *
444 ctl_get_cmd_entry(struct ctl_scsiio *ctsio);
445 static const struct ctl_cmd_entry *
446 ctl_validate_command(struct ctl_scsiio *ctsio);
447 static int ctl_cmd_applicable(uint8_t lun_type,
448 const struct ctl_cmd_entry *entry);
451 * Load the serialization table. This isn't very pretty, but is probably
452 * the easiest way to do it.
454 #include "ctl_ser_table.c"
457 * We only need to define open, close and ioctl routines for this driver.
459 static struct cdevsw ctl_cdevsw = {
460 .d_version = D_VERSION,
463 .d_close = ctl_close,
464 .d_ioctl = ctl_ioctl,
469 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
470 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
472 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
474 static moduledata_t ctl_moduledata = {
476 ctl_module_event_handler,
480 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
481 MODULE_VERSION(ctl, 1);
483 static struct ctl_frontend ioctl_frontend =
489 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
490 union ctl_ha_msg *msg_info)
492 struct ctl_scsiio *ctsio;
494 if (msg_info->hdr.original_sc == NULL) {
495 printf("%s: original_sc == NULL!\n", __func__);
496 /* XXX KDM now what? */
500 ctsio = &msg_info->hdr.original_sc->scsiio;
501 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
502 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
503 ctsio->io_hdr.status = msg_info->hdr.status;
504 ctsio->scsi_status = msg_info->scsi.scsi_status;
505 ctsio->sense_len = msg_info->scsi.sense_len;
506 ctsio->sense_residual = msg_info->scsi.sense_residual;
507 ctsio->residual = msg_info->scsi.residual;
508 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
509 sizeof(ctsio->sense_data));
510 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
511 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
512 ctl_enqueue_isc((union ctl_io *)ctsio);
516 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
517 union ctl_ha_msg *msg_info)
519 struct ctl_scsiio *ctsio;
521 if (msg_info->hdr.serializing_sc == NULL) {
522 printf("%s: serializing_sc == NULL!\n", __func__);
523 /* XXX KDM now what? */
527 ctsio = &msg_info->hdr.serializing_sc->scsiio;
530 * Attempt to catch the situation where an I/O has
531 * been freed, and we're using it again.
533 if (ctsio->io_hdr.io_type == 0xff) {
534 union ctl_io *tmp_io;
535 tmp_io = (union ctl_io *)ctsio;
536 printf("%s: %p use after free!\n", __func__,
538 printf("%s: type %d msg %d cdb %x iptl: "
539 "%d:%d:%d:%d tag 0x%04x "
540 "flag %#x status %x\n",
542 tmp_io->io_hdr.io_type,
543 tmp_io->io_hdr.msg_type,
544 tmp_io->scsiio.cdb[0],
545 tmp_io->io_hdr.nexus.initid.id,
546 tmp_io->io_hdr.nexus.targ_port,
547 tmp_io->io_hdr.nexus.targ_target.id,
548 tmp_io->io_hdr.nexus.targ_lun,
549 (tmp_io->io_hdr.io_type ==
551 tmp_io->taskio.tag_num :
552 tmp_io->scsiio.tag_num,
553 tmp_io->io_hdr.flags,
554 tmp_io->io_hdr.status);
557 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
558 ctl_enqueue_isc((union ctl_io *)ctsio);
562 * ISC (Inter Shelf Communication) event handler. Events from the HA
563 * subsystem come in here.
566 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
568 struct ctl_softc *ctl_softc;
570 struct ctl_prio *presio;
571 ctl_ha_status isc_status;
573 ctl_softc = control_softc;
578 printf("CTL: Isc Msg event %d\n", event);
580 if (event == CTL_HA_EVT_MSG_RECV) {
581 union ctl_ha_msg msg_info;
583 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
584 sizeof(msg_info), /*wait*/ 0);
586 printf("CTL: msg_type %d\n", msg_info.msg_type);
588 if (isc_status != 0) {
589 printf("Error receiving message, status = %d\n",
594 switch (msg_info.hdr.msg_type) {
595 case CTL_MSG_SERIALIZE:
597 printf("Serialize\n");
599 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
601 printf("ctl_isc_event_handler: can't allocate "
604 /* Need to set busy and send msg back */
605 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
606 msg_info.hdr.status = CTL_SCSI_ERROR;
607 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
608 msg_info.scsi.sense_len = 0;
609 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
610 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
615 // populate ctsio from msg_info
616 io->io_hdr.io_type = CTL_IO_SCSI;
617 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
618 io->io_hdr.original_sc = msg_info.hdr.original_sc;
620 printf("pOrig %x\n", (int)msg_info.original_sc);
622 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
625 * If we're in serialization-only mode, we don't
626 * want to go through full done processing. Thus
629 * XXX KDM add another flag that is more specific.
631 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
632 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
633 io->io_hdr.nexus = msg_info.hdr.nexus;
635 printf("targ %d, port %d, iid %d, lun %d\n",
636 io->io_hdr.nexus.targ_target.id,
637 io->io_hdr.nexus.targ_port,
638 io->io_hdr.nexus.initid.id,
639 io->io_hdr.nexus.targ_lun);
641 io->scsiio.tag_num = msg_info.scsi.tag_num;
642 io->scsiio.tag_type = msg_info.scsi.tag_type;
643 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
645 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
646 const struct ctl_cmd_entry *entry;
648 entry = ctl_get_cmd_entry(&io->scsiio);
649 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
651 entry->flags & CTL_FLAG_DATA_MASK;
656 /* Performed on the Originating SC, XFER mode only */
657 case CTL_MSG_DATAMOVE: {
658 struct ctl_sg_entry *sgl;
661 io = msg_info.hdr.original_sc;
663 printf("%s: original_sc == NULL!\n", __func__);
664 /* XXX KDM do something here */
667 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
668 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
670 * Keep track of this, we need to send it back over
671 * when the datamove is complete.
673 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
675 if (msg_info.dt.sg_sequence == 0) {
677 * XXX KDM we use the preallocated S/G list
678 * here, but we'll need to change this to
679 * dynamic allocation if we need larger S/G
682 if (msg_info.dt.kern_sg_entries >
683 sizeof(io->io_hdr.remote_sglist) /
684 sizeof(io->io_hdr.remote_sglist[0])) {
685 printf("%s: number of S/G entries "
686 "needed %u > allocated num %zd\n",
688 msg_info.dt.kern_sg_entries,
689 sizeof(io->io_hdr.remote_sglist)/
690 sizeof(io->io_hdr.remote_sglist[0]));
693 * XXX KDM send a message back to
694 * the other side to shut down the
695 * DMA. The error will come back
696 * through via the normal channel.
700 sgl = io->io_hdr.remote_sglist;
702 sizeof(io->io_hdr.remote_sglist));
704 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
706 io->scsiio.kern_sg_entries =
707 msg_info.dt.kern_sg_entries;
708 io->scsiio.rem_sg_entries =
709 msg_info.dt.kern_sg_entries;
710 io->scsiio.kern_data_len =
711 msg_info.dt.kern_data_len;
712 io->scsiio.kern_total_len =
713 msg_info.dt.kern_total_len;
714 io->scsiio.kern_data_resid =
715 msg_info.dt.kern_data_resid;
716 io->scsiio.kern_rel_offset =
717 msg_info.dt.kern_rel_offset;
719 * Clear out per-DMA flags.
721 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
723 * Add per-DMA flags that are set for this
724 * particular DMA request.
726 io->io_hdr.flags |= msg_info.dt.flags &
729 sgl = (struct ctl_sg_entry *)
730 io->scsiio.kern_data_ptr;
732 for (i = msg_info.dt.sent_sg_entries, j = 0;
733 i < (msg_info.dt.sent_sg_entries +
734 msg_info.dt.cur_sg_entries); i++, j++) {
735 sgl[i].addr = msg_info.dt.sg_list[j].addr;
736 sgl[i].len = msg_info.dt.sg_list[j].len;
739 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
741 msg_info.dt.sg_list[j].addr,
742 msg_info.dt.sg_list[j].len,
743 sgl[i].addr, sgl[i].len, j, i);
747 memcpy(&sgl[msg_info.dt.sent_sg_entries],
749 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
753 * If this is the last piece of the I/O, we've got
754 * the full S/G list. Queue processing in the thread.
755 * Otherwise wait for the next piece.
757 if (msg_info.dt.sg_last != 0)
761 /* Performed on the Serializing (primary) SC, XFER mode only */
762 case CTL_MSG_DATAMOVE_DONE: {
763 if (msg_info.hdr.serializing_sc == NULL) {
764 printf("%s: serializing_sc == NULL!\n",
766 /* XXX KDM now what? */
770 * We grab the sense information here in case
771 * there was a failure, so we can return status
772 * back to the initiator.
774 io = msg_info.hdr.serializing_sc;
775 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
776 io->io_hdr.status = msg_info.hdr.status;
777 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
778 io->scsiio.sense_len = msg_info.scsi.sense_len;
779 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
780 io->io_hdr.port_status = msg_info.scsi.fetd_status;
781 io->scsiio.residual = msg_info.scsi.residual;
782 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
783 sizeof(io->scsiio.sense_data));
788 /* Preformed on Originating SC, SER_ONLY mode */
790 io = msg_info.hdr.original_sc;
792 printf("%s: Major Bummer\n", __func__);
796 printf("pOrig %x\n",(int) ctsio);
799 io->io_hdr.msg_type = CTL_MSG_R2R;
800 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
805 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
807 * Performed on the Originating (i.e. secondary) SC in XFER
810 case CTL_MSG_FINISH_IO:
811 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
812 ctl_isc_handler_finish_xfer(ctl_softc,
815 ctl_isc_handler_finish_ser_only(ctl_softc,
819 /* Preformed on Originating SC */
820 case CTL_MSG_BAD_JUJU:
821 io = msg_info.hdr.original_sc;
823 printf("%s: Bad JUJU!, original_sc is NULL!\n",
827 ctl_copy_sense_data(&msg_info, io);
829 * IO should have already been cleaned up on other
830 * SC so clear this flag so we won't send a message
831 * back to finish the IO there.
833 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
834 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
836 /* io = msg_info.hdr.serializing_sc; */
837 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
841 /* Handle resets sent from the other side */
842 case CTL_MSG_MANAGE_TASKS: {
843 struct ctl_taskio *taskio;
844 taskio = (struct ctl_taskio *)ctl_alloc_io(
845 (void *)ctl_softc->othersc_pool);
846 if (taskio == NULL) {
847 printf("ctl_isc_event_handler: can't allocate "
850 /* should I just call the proper reset func
854 ctl_zero_io((union ctl_io *)taskio);
855 taskio->io_hdr.io_type = CTL_IO_TASK;
856 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
857 taskio->io_hdr.nexus = msg_info.hdr.nexus;
858 taskio->task_action = msg_info.task.task_action;
859 taskio->tag_num = msg_info.task.tag_num;
860 taskio->tag_type = msg_info.task.tag_type;
862 taskio->io_hdr.start_time = time_uptime;
863 getbintime(&taskio->io_hdr.start_bt);
865 cs_prof_gettime(&taskio->io_hdr.start_ticks);
867 #endif /* CTL_TIME_IO */
868 ctl_run_task((union ctl_io *)taskio);
871 /* Persistent Reserve action which needs attention */
872 case CTL_MSG_PERS_ACTION:
873 presio = (struct ctl_prio *)ctl_alloc_io(
874 (void *)ctl_softc->othersc_pool);
875 if (presio == NULL) {
876 printf("ctl_isc_event_handler: can't allocate "
879 /* Need to set busy and send msg back */
882 ctl_zero_io((union ctl_io *)presio);
883 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
884 presio->pr_msg = msg_info.pr;
885 ctl_enqueue_isc((union ctl_io *)presio);
887 case CTL_MSG_SYNC_FE:
890 case CTL_MSG_APS_LOCK: {
891 // It's quicker to execute this then to
894 struct ctl_page_index *page_index;
895 struct copan_aps_subpage *current_sp;
898 targ_lun = msg_info.hdr.nexus.targ_mapped_lun;
899 lun = ctl_softc->ctl_luns[targ_lun];
900 mtx_lock(&lun->lun_lock);
901 page_index = &lun->mode_pages.index[index_to_aps_page];
902 current_sp = (struct copan_aps_subpage *)
903 (page_index->page_data +
904 (page_index->page_len * CTL_PAGE_CURRENT));
906 current_sp->lock_active = msg_info.aps.lock_flag;
907 mtx_unlock(&lun->lun_lock);
911 printf("How did I get here?\n");
913 } else if (event == CTL_HA_EVT_MSG_SENT) {
914 if (param != CTL_HA_STATUS_SUCCESS) {
915 printf("Bad status from ctl_ha_msg_send status %d\n",
919 } else if (event == CTL_HA_EVT_DISCONNECT) {
920 printf("CTL: Got a disconnect from Isc\n");
923 printf("ctl_isc_event_handler: Unknown event %d\n", event);
932 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
934 struct scsi_sense_data *sense;
936 sense = &dest->scsiio.sense_data;
937 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
938 dest->scsiio.scsi_status = src->scsi.scsi_status;
939 dest->scsiio.sense_len = src->scsi.sense_len;
940 dest->io_hdr.status = src->hdr.status;
946 struct ctl_softc *softc;
947 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
948 struct ctl_port *port;
950 int i, error, retval;
957 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
959 softc = control_softc;
961 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
964 softc->dev->si_drv1 = softc;
967 * By default, return a "bad LUN" peripheral qualifier for unknown
968 * LUNs. The user can override this default using the tunable or
969 * sysctl. See the comment in ctl_inquiry_std() for more details.
971 softc->inquiry_pq_no_lun = 1;
972 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
973 &softc->inquiry_pq_no_lun);
974 sysctl_ctx_init(&softc->sysctl_ctx);
975 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
976 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
977 CTLFLAG_RD, 0, "CAM Target Layer");
979 if (softc->sysctl_tree == NULL) {
980 printf("%s: unable to allocate sysctl tree\n", __func__);
981 destroy_dev(softc->dev);
982 free(control_softc, M_DEVBUF);
983 control_softc = NULL;
987 SYSCTL_ADD_INT(&softc->sysctl_ctx,
988 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
989 "inquiry_pq_no_lun", CTLFLAG_RW,
990 &softc->inquiry_pq_no_lun, 0,
991 "Report no lun possible for invalid LUNs");
993 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
994 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
995 softc->open_count = 0;
998 * Default to actually sending a SYNCHRONIZE CACHE command down to
1001 softc->flags = CTL_FLAG_REAL_SYNC;
1004 * In Copan's HA scheme, the "master" and "slave" roles are
1005 * figured out through the slot the controller is in. Although it
1006 * is an active/active system, someone has to be in charge.
1009 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1013 softc->flags |= CTL_FLAG_MASTER_SHELF;
1016 persis_offset = CTL_MAX_INITIATORS;
1019 * XXX KDM need to figure out where we want to get our target ID
1020 * and WWID. Is it different on each port?
1022 softc->target.id = 0;
1023 softc->target.wwid[0] = 0x12345678;
1024 softc->target.wwid[1] = 0x87654321;
1025 STAILQ_INIT(&softc->lun_list);
1026 STAILQ_INIT(&softc->pending_lun_queue);
1027 STAILQ_INIT(&softc->fe_list);
1028 STAILQ_INIT(&softc->port_list);
1029 STAILQ_INIT(&softc->be_list);
1030 STAILQ_INIT(&softc->io_pools);
1031 ctl_tpc_init(softc);
1033 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1034 &internal_pool)!= 0){
1035 printf("ctl: can't allocate %d entry internal pool, "
1036 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1040 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1041 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1042 printf("ctl: can't allocate %d entry emergency pool, "
1043 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1044 ctl_pool_free(internal_pool);
1048 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1051 printf("ctl: can't allocate %d entry other SC pool, "
1052 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1053 ctl_pool_free(internal_pool);
1054 ctl_pool_free(emergency_pool);
1058 softc->internal_pool = internal_pool;
1059 softc->emergency_pool = emergency_pool;
1060 softc->othersc_pool = other_pool;
1062 if (worker_threads <= 0)
1063 worker_threads = max(1, mp_ncpus / 4);
1064 if (worker_threads > CTL_MAX_THREADS)
1065 worker_threads = CTL_MAX_THREADS;
1067 for (i = 0; i < worker_threads; i++) {
1068 struct ctl_thread *thr = &softc->threads[i];
1070 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1071 thr->ctl_softc = softc;
1072 STAILQ_INIT(&thr->incoming_queue);
1073 STAILQ_INIT(&thr->rtr_queue);
1074 STAILQ_INIT(&thr->done_queue);
1075 STAILQ_INIT(&thr->isc_queue);
1077 error = kproc_kthread_add(ctl_work_thread, thr,
1078 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1080 printf("error creating CTL work thread!\n");
1081 ctl_pool_free(internal_pool);
1082 ctl_pool_free(emergency_pool);
1083 ctl_pool_free(other_pool);
1087 error = kproc_kthread_add(ctl_lun_thread, softc,
1088 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1090 printf("error creating CTL lun thread!\n");
1091 ctl_pool_free(internal_pool);
1092 ctl_pool_free(emergency_pool);
1093 ctl_pool_free(other_pool);
1097 printf("ctl: CAM Target Layer loaded\n");
1100 * Initialize the ioctl front end.
1102 ctl_frontend_register(&ioctl_frontend);
1103 port = &softc->ioctl_info.port;
1104 port->frontend = &ioctl_frontend;
1105 sprintf(softc->ioctl_info.port_name, "ioctl");
1106 port->port_type = CTL_PORT_IOCTL;
1107 port->num_requested_ctl_io = 100;
1108 port->port_name = softc->ioctl_info.port_name;
1109 port->port_online = ctl_ioctl_online;
1110 port->port_offline = ctl_ioctl_offline;
1111 port->onoff_arg = &softc->ioctl_info;
1112 port->lun_enable = ctl_ioctl_lun_enable;
1113 port->lun_disable = ctl_ioctl_lun_disable;
1114 port->targ_lun_arg = &softc->ioctl_info;
1115 port->fe_datamove = ctl_ioctl_datamove;
1116 port->fe_done = ctl_ioctl_done;
1117 port->max_targets = 15;
1118 port->max_target_id = 15;
1120 if (ctl_port_register(&softc->ioctl_info.port,
1121 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1122 printf("ctl: ioctl front end registration failed, will "
1123 "continue anyway\n");
1127 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1128 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1129 sizeof(struct callout), CTL_TIMER_BYTES);
1132 #endif /* CTL_IO_DELAY */
1140 struct ctl_softc *softc;
1141 struct ctl_lun *lun, *next_lun;
1142 struct ctl_io_pool *pool;
1144 softc = (struct ctl_softc *)control_softc;
1146 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1147 printf("ctl: ioctl front end deregistration failed\n");
1149 mtx_lock(&softc->ctl_lock);
1154 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1155 next_lun = STAILQ_NEXT(lun, links);
1159 mtx_unlock(&softc->ctl_lock);
1161 ctl_frontend_deregister(&ioctl_frontend);
1164 * This will rip the rug out from under any FETDs or anyone else
1165 * that has a pool allocated. Since we increment our module
1166 * refcount any time someone outside the main CTL module allocates
1167 * a pool, we shouldn't have any problems here. The user won't be
1168 * able to unload the CTL module until client modules have
1169 * successfully unloaded.
1171 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1172 ctl_pool_free(pool);
1175 ctl_shutdown_thread(softc->work_thread);
1176 mtx_destroy(&softc->queue_lock);
1179 ctl_tpc_shutdown(softc);
1180 mtx_destroy(&softc->pool_lock);
1181 mtx_destroy(&softc->ctl_lock);
1183 destroy_dev(softc->dev);
1185 sysctl_ctx_free(&softc->sysctl_ctx);
1187 free(control_softc, M_DEVBUF);
1188 control_softc = NULL;
1191 printf("ctl: CAM Target Layer unloaded\n");
1195 ctl_module_event_handler(module_t mod, int what, void *arg)
1200 return (ctl_init());
1204 return (EOPNOTSUPP);
1209 * XXX KDM should we do some access checks here? Bump a reference count to
1210 * prevent a CTL module from being unloaded while someone has it open?
1213 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1219 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1225 ctl_port_enable(ctl_port_type port_type)
1227 struct ctl_softc *softc;
1228 struct ctl_port *port;
1230 if (ctl_is_single == 0) {
1231 union ctl_ha_msg msg_info;
1235 printf("%s: HA mode, synchronizing frontend enable\n",
1238 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1239 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1240 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1241 printf("Sync msg send error retval %d\n", isc_retval);
1243 if (!rcv_sync_msg) {
1244 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1245 sizeof(msg_info), 1);
1248 printf("CTL:Frontend Enable\n");
1250 printf("%s: single mode, skipping frontend synchronization\n",
1255 softc = control_softc;
1257 STAILQ_FOREACH(port, &softc->port_list, links) {
1258 if (port_type & port->port_type)
1261 printf("port %d\n", port->targ_port);
1263 ctl_port_online(port);
1271 ctl_port_disable(ctl_port_type port_type)
1273 struct ctl_softc *softc;
1274 struct ctl_port *port;
1276 softc = control_softc;
1278 STAILQ_FOREACH(port, &softc->port_list, links) {
1279 if (port_type & port->port_type)
1280 ctl_port_offline(port);
1287 * Returns 0 for success, 1 for failure.
1288 * Currently the only failure mode is if there aren't enough entries
1289 * allocated. So, in case of a failure, look at num_entries_dropped,
1290 * reallocate and try again.
1293 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1294 int *num_entries_filled, int *num_entries_dropped,
1295 ctl_port_type port_type, int no_virtual)
1297 struct ctl_softc *softc;
1298 struct ctl_port *port;
1299 int entries_dropped, entries_filled;
1303 softc = control_softc;
1307 entries_dropped = 0;
1310 mtx_lock(&softc->ctl_lock);
1311 STAILQ_FOREACH(port, &softc->port_list, links) {
1312 struct ctl_port_entry *entry;
1314 if ((port->port_type & port_type) == 0)
1317 if ((no_virtual != 0)
1318 && (port->virtual_port != 0))
1321 if (entries_filled >= num_entries_alloced) {
1325 entry = &entries[i];
1327 entry->port_type = port->port_type;
1328 strlcpy(entry->port_name, port->port_name,
1329 sizeof(entry->port_name));
1330 entry->physical_port = port->physical_port;
1331 entry->virtual_port = port->virtual_port;
1332 entry->wwnn = port->wwnn;
1333 entry->wwpn = port->wwpn;
1339 mtx_unlock(&softc->ctl_lock);
1341 if (entries_dropped > 0)
1344 *num_entries_dropped = entries_dropped;
1345 *num_entries_filled = entries_filled;
1351 ctl_ioctl_online(void *arg)
1353 struct ctl_ioctl_info *ioctl_info;
1355 ioctl_info = (struct ctl_ioctl_info *)arg;
1357 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1361 ctl_ioctl_offline(void *arg)
1363 struct ctl_ioctl_info *ioctl_info;
1365 ioctl_info = (struct ctl_ioctl_info *)arg;
1367 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1371 * Remove an initiator by port number and initiator ID.
1372 * Returns 0 for success, -1 for failure.
1375 ctl_remove_initiator(struct ctl_port *port, int iid)
1377 struct ctl_softc *softc = control_softc;
1379 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1381 if (iid > CTL_MAX_INIT_PER_PORT) {
1382 printf("%s: initiator ID %u > maximun %u!\n",
1383 __func__, iid, CTL_MAX_INIT_PER_PORT);
1387 mtx_lock(&softc->ctl_lock);
1388 port->wwpn_iid[iid].in_use--;
1389 port->wwpn_iid[iid].last_use = time_uptime;
1390 mtx_unlock(&softc->ctl_lock);
1396 * Add an initiator to the initiator map.
1397 * Returns iid for success, < 0 for failure.
1400 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1402 struct ctl_softc *softc = control_softc;
1406 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1408 if (iid >= CTL_MAX_INIT_PER_PORT) {
1409 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1410 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1415 mtx_lock(&softc->ctl_lock);
1417 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1418 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1419 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1423 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1424 strcmp(name, port->wwpn_iid[i].name) == 0) {
1432 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1433 if (port->wwpn_iid[i].in_use == 0 &&
1434 port->wwpn_iid[i].wwpn == 0 &&
1435 port->wwpn_iid[i].name == NULL) {
1444 best_time = INT32_MAX;
1445 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1446 if (port->wwpn_iid[i].in_use == 0) {
1447 if (port->wwpn_iid[i].last_use < best_time) {
1449 best_time = port->wwpn_iid[i].last_use;
1457 mtx_unlock(&softc->ctl_lock);
1462 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1464 * This is not an error yet.
1466 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1468 printf("%s: port %d iid %u WWPN %#jx arrived"
1469 " again\n", __func__, port->targ_port,
1470 iid, (uintmax_t)wwpn);
1474 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1475 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1477 printf("%s: port %d iid %u name '%s' arrived"
1478 " again\n", __func__, port->targ_port,
1485 * This is an error, but what do we do about it? The
1486 * driver is telling us we have a new WWPN for this
1487 * initiator ID, so we pretty much need to use it.
1489 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1490 " but WWPN %#jx '%s' is still at that address\n",
1491 __func__, port->targ_port, iid, wwpn, name,
1492 (uintmax_t)port->wwpn_iid[iid].wwpn,
1493 port->wwpn_iid[iid].name);
1496 * XXX KDM clear have_ca and ua_pending on each LUN for
1501 free(port->wwpn_iid[iid].name, M_CTL);
1502 port->wwpn_iid[iid].name = name;
1503 port->wwpn_iid[iid].wwpn = wwpn;
1504 port->wwpn_iid[iid].in_use++;
1505 mtx_unlock(&softc->ctl_lock);
1511 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1515 switch (port->port_type) {
1518 struct scsi_transportid_fcp *id =
1519 (struct scsi_transportid_fcp *)buf;
1520 if (port->wwpn_iid[iid].wwpn == 0)
1522 memset(id, 0, sizeof(*id));
1523 id->format_protocol = SCSI_PROTO_FC;
1524 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1525 return (sizeof(*id));
1527 case CTL_PORT_ISCSI:
1529 struct scsi_transportid_iscsi_port *id =
1530 (struct scsi_transportid_iscsi_port *)buf;
1531 if (port->wwpn_iid[iid].name == NULL)
1534 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1536 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1537 len = roundup2(min(len, 252), 4);
1538 scsi_ulto2b(len, id->additional_length);
1539 return (sizeof(*id) + len);
1543 struct scsi_transportid_sas *id =
1544 (struct scsi_transportid_sas *)buf;
1545 if (port->wwpn_iid[iid].wwpn == 0)
1547 memset(id, 0, sizeof(*id));
1548 id->format_protocol = SCSI_PROTO_SAS;
1549 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1550 return (sizeof(*id));
1554 struct scsi_transportid_spi *id =
1555 (struct scsi_transportid_spi *)buf;
1556 memset(id, 0, sizeof(*id));
1557 id->format_protocol = SCSI_PROTO_SPI;
1558 scsi_ulto2b(iid, id->scsi_addr);
1559 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1560 return (sizeof(*id));
1566 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1572 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1578 * Data movement routine for the CTL ioctl frontend port.
1581 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1583 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1584 struct ctl_sg_entry ext_entry, kern_entry;
1585 int ext_sglen, ext_sg_entries, kern_sg_entries;
1586 int ext_sg_start, ext_offset;
1587 int len_to_copy, len_copied;
1588 int kern_watermark, ext_watermark;
1589 int ext_sglist_malloced;
1592 ext_sglist_malloced = 0;
1596 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1599 * If this flag is set, fake the data transfer.
1601 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1602 ctsio->ext_data_filled = ctsio->ext_data_len;
1607 * To simplify things here, if we have a single buffer, stick it in
1608 * a S/G entry and just make it a single entry S/G list.
1610 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1613 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1615 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1617 ext_sglist_malloced = 1;
1618 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1620 ctl_set_internal_failure(ctsio,
1625 ext_sg_entries = ctsio->ext_sg_entries;
1627 for (i = 0; i < ext_sg_entries; i++) {
1628 if ((len_seen + ext_sglist[i].len) >=
1629 ctsio->ext_data_filled) {
1631 ext_offset = ctsio->ext_data_filled - len_seen;
1634 len_seen += ext_sglist[i].len;
1637 ext_sglist = &ext_entry;
1638 ext_sglist->addr = ctsio->ext_data_ptr;
1639 ext_sglist->len = ctsio->ext_data_len;
1642 ext_offset = ctsio->ext_data_filled;
1645 if (ctsio->kern_sg_entries > 0) {
1646 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1647 kern_sg_entries = ctsio->kern_sg_entries;
1649 kern_sglist = &kern_entry;
1650 kern_sglist->addr = ctsio->kern_data_ptr;
1651 kern_sglist->len = ctsio->kern_data_len;
1652 kern_sg_entries = 1;
1657 ext_watermark = ext_offset;
1659 for (i = ext_sg_start, j = 0;
1660 i < ext_sg_entries && j < kern_sg_entries;) {
1661 uint8_t *ext_ptr, *kern_ptr;
1663 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1664 kern_sglist[j].len - kern_watermark);
1666 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1667 ext_ptr = ext_ptr + ext_watermark;
1668 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1672 panic("need to implement bus address support");
1674 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1677 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1678 kern_ptr = kern_ptr + kern_watermark;
1680 kern_watermark += len_to_copy;
1681 ext_watermark += len_to_copy;
1683 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1685 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1686 "bytes to user\n", len_to_copy));
1687 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1688 "to %p\n", kern_ptr, ext_ptr));
1689 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1690 ctl_set_internal_failure(ctsio,
1696 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1697 "bytes from user\n", len_to_copy));
1698 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1699 "to %p\n", ext_ptr, kern_ptr));
1700 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1701 ctl_set_internal_failure(ctsio,
1708 len_copied += len_to_copy;
1710 if (ext_sglist[i].len == ext_watermark) {
1715 if (kern_sglist[j].len == kern_watermark) {
1721 ctsio->ext_data_filled += len_copied;
1723 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1724 "kern_sg_entries: %d\n", ext_sg_entries,
1726 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1727 "kern_data_len = %d\n", ctsio->ext_data_len,
1728 ctsio->kern_data_len));
1731 /* XXX KDM set residual?? */
1734 if (ext_sglist_malloced != 0)
1735 free(ext_sglist, M_CTL);
1737 return (CTL_RETVAL_COMPLETE);
1741 * Serialize a command that went down the "wrong" side, and so was sent to
1742 * this controller for execution. The logic is a little different than the
1743 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1744 * sent back to the other side, but in the success case, we execute the
1745 * command on this side (XFER mode) or tell the other side to execute it
1749 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1751 struct ctl_softc *ctl_softc;
1752 union ctl_ha_msg msg_info;
1753 struct ctl_lun *lun;
1757 ctl_softc = control_softc;
1759 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1760 lun = ctl_softc->ctl_luns[targ_lun];
1764 * Why isn't LUN defined? The other side wouldn't
1765 * send a cmd if the LUN is undefined.
1767 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1769 /* "Logical unit not supported" */
1770 ctl_set_sense_data(&msg_info.scsi.sense_data,
1772 /*sense_format*/SSD_TYPE_NONE,
1773 /*current_error*/ 1,
1774 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1779 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1780 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1781 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1782 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1783 msg_info.hdr.serializing_sc = NULL;
1784 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1785 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1786 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1792 mtx_lock(&lun->lun_lock);
1793 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1795 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1796 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1798 case CTL_ACTION_BLOCK:
1799 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1800 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1803 case CTL_ACTION_PASS:
1804 case CTL_ACTION_SKIP:
1805 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1806 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1807 ctl_enqueue_rtr((union ctl_io *)ctsio);
1810 /* send msg back to other side */
1811 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1812 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1813 msg_info.hdr.msg_type = CTL_MSG_R2R;
1815 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1817 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1818 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1822 case CTL_ACTION_OVERLAP:
1823 /* OVERLAPPED COMMANDS ATTEMPTED */
1824 ctl_set_sense_data(&msg_info.scsi.sense_data,
1826 /*sense_format*/SSD_TYPE_NONE,
1827 /*current_error*/ 1,
1828 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1833 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1834 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1835 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1836 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1837 msg_info.hdr.serializing_sc = NULL;
1838 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1840 printf("BAD JUJU:Major Bummer Overlap\n");
1842 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1844 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1845 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1848 case CTL_ACTION_OVERLAP_TAG:
1849 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1850 ctl_set_sense_data(&msg_info.scsi.sense_data,
1852 /*sense_format*/SSD_TYPE_NONE,
1853 /*current_error*/ 1,
1854 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1856 /*ascq*/ ctsio->tag_num & 0xff,
1859 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1860 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1861 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1862 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1863 msg_info.hdr.serializing_sc = NULL;
1864 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1866 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1868 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1870 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1871 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1874 case CTL_ACTION_ERROR:
1876 /* "Internal target failure" */
1877 ctl_set_sense_data(&msg_info.scsi.sense_data,
1879 /*sense_format*/SSD_TYPE_NONE,
1880 /*current_error*/ 1,
1881 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1886 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1887 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1888 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1889 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1890 msg_info.hdr.serializing_sc = NULL;
1891 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1893 printf("BAD JUJU:Major Bummer HW Error\n");
1895 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1897 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1898 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1902 mtx_unlock(&lun->lun_lock);
1907 ctl_ioctl_submit_wait(union ctl_io *io)
1909 struct ctl_fe_ioctl_params params;
1910 ctl_fe_ioctl_state last_state;
1915 bzero(¶ms, sizeof(params));
1917 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1918 cv_init(¶ms.sem, "ctlioccv");
1919 params.state = CTL_IOCTL_INPROG;
1920 last_state = params.state;
1922 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1924 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1926 /* This shouldn't happen */
1927 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1933 mtx_lock(¶ms.ioctl_mtx);
1935 * Check the state here, and don't sleep if the state has
1936 * already changed (i.e. wakeup has already occured, but we
1937 * weren't waiting yet).
1939 if (params.state == last_state) {
1940 /* XXX KDM cv_wait_sig instead? */
1941 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1943 last_state = params.state;
1945 switch (params.state) {
1946 case CTL_IOCTL_INPROG:
1947 /* Why did we wake up? */
1948 /* XXX KDM error here? */
1949 mtx_unlock(¶ms.ioctl_mtx);
1951 case CTL_IOCTL_DATAMOVE:
1952 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1955 * change last_state back to INPROG to avoid
1956 * deadlock on subsequent data moves.
1958 params.state = last_state = CTL_IOCTL_INPROG;
1960 mtx_unlock(¶ms.ioctl_mtx);
1961 ctl_ioctl_do_datamove(&io->scsiio);
1963 * Note that in some cases, most notably writes,
1964 * this will queue the I/O and call us back later.
1965 * In other cases, generally reads, this routine
1966 * will immediately call back and wake us up,
1967 * probably using our own context.
1969 io->scsiio.be_move_done(io);
1971 case CTL_IOCTL_DONE:
1972 mtx_unlock(¶ms.ioctl_mtx);
1973 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1977 mtx_unlock(¶ms.ioctl_mtx);
1978 /* XXX KDM error here? */
1981 } while (done == 0);
1983 mtx_destroy(¶ms.ioctl_mtx);
1984 cv_destroy(¶ms.sem);
1986 return (CTL_RETVAL_COMPLETE);
1990 ctl_ioctl_datamove(union ctl_io *io)
1992 struct ctl_fe_ioctl_params *params;
1994 params = (struct ctl_fe_ioctl_params *)
1995 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1997 mtx_lock(¶ms->ioctl_mtx);
1998 params->state = CTL_IOCTL_DATAMOVE;
1999 cv_broadcast(¶ms->sem);
2000 mtx_unlock(¶ms->ioctl_mtx);
2004 ctl_ioctl_done(union ctl_io *io)
2006 struct ctl_fe_ioctl_params *params;
2008 params = (struct ctl_fe_ioctl_params *)
2009 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2011 mtx_lock(¶ms->ioctl_mtx);
2012 params->state = CTL_IOCTL_DONE;
2013 cv_broadcast(¶ms->sem);
2014 mtx_unlock(¶ms->ioctl_mtx);
2018 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2020 struct ctl_fe_ioctl_startstop_info *sd_info;
2022 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2024 sd_info->hs_info.status = metatask->status;
2025 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2026 sd_info->hs_info.luns_complete =
2027 metatask->taskinfo.startstop.luns_complete;
2028 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2030 cv_broadcast(&sd_info->sem);
2034 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2036 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2038 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2040 mtx_lock(fe_bbr_info->lock);
2041 fe_bbr_info->bbr_info->status = metatask->status;
2042 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2043 fe_bbr_info->wakeup_done = 1;
2044 mtx_unlock(fe_bbr_info->lock);
2046 cv_broadcast(&fe_bbr_info->sem);
2050 * Returns 0 for success, errno for failure.
2053 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2054 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2061 mtx_lock(&lun->lun_lock);
2062 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2063 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2065 struct ctl_ooa_entry *entry;
2068 * If we've got more than we can fit, just count the
2069 * remaining entries.
2071 if (*cur_fill_num >= ooa_hdr->alloc_num)
2074 entry = &kern_entries[*cur_fill_num];
2076 entry->tag_num = io->scsiio.tag_num;
2077 entry->lun_num = lun->lun;
2079 entry->start_bt = io->io_hdr.start_bt;
2081 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2082 entry->cdb_len = io->scsiio.cdb_len;
2083 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2084 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2086 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2087 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2089 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2090 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2092 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2093 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2095 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2096 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2098 mtx_unlock(&lun->lun_lock);
2104 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2105 size_t error_str_len)
2109 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2111 if (copyin(user_addr, kptr, len) != 0) {
2112 snprintf(error_str, error_str_len, "Error copying %d bytes "
2113 "from user address %p to kernel address %p", len,
2123 ctl_free_args(int num_args, struct ctl_be_arg *args)
2130 for (i = 0; i < num_args; i++) {
2131 free(args[i].kname, M_CTL);
2132 free(args[i].kvalue, M_CTL);
2138 static struct ctl_be_arg *
2139 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2140 char *error_str, size_t error_str_len)
2142 struct ctl_be_arg *args;
2145 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2146 error_str, error_str_len);
2151 for (i = 0; i < num_args; i++) {
2152 args[i].kname = NULL;
2153 args[i].kvalue = NULL;
2156 for (i = 0; i < num_args; i++) {
2159 args[i].kname = ctl_copyin_alloc(args[i].name,
2160 args[i].namelen, error_str, error_str_len);
2161 if (args[i].kname == NULL)
2164 if (args[i].kname[args[i].namelen - 1] != '\0') {
2165 snprintf(error_str, error_str_len, "Argument %d "
2166 "name is not NUL-terminated", i);
2170 if (args[i].flags & CTL_BEARG_RD) {
2171 tmpptr = ctl_copyin_alloc(args[i].value,
2172 args[i].vallen, error_str, error_str_len);
2175 if ((args[i].flags & CTL_BEARG_ASCII)
2176 && (tmpptr[args[i].vallen - 1] != '\0')) {
2177 snprintf(error_str, error_str_len, "Argument "
2178 "%d value is not NUL-terminated", i);
2181 args[i].kvalue = tmpptr;
2183 args[i].kvalue = malloc(args[i].vallen,
2184 M_CTL, M_WAITOK | M_ZERO);
2191 ctl_free_args(num_args, args);
2197 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2201 for (i = 0; i < num_args; i++) {
2202 if (args[i].flags & CTL_BEARG_WR)
2203 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2208 * Escape characters that are illegal or not recommended in XML.
2211 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2217 for (; *str; str++) {
2220 retval = sbuf_printf(sb, "&");
2223 retval = sbuf_printf(sb, ">");
2226 retval = sbuf_printf(sb, "<");
2229 retval = sbuf_putc(sb, *str);
2242 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2245 struct ctl_softc *softc;
2248 softc = control_softc;
2258 * If we haven't been "enabled", don't allow any SCSI I/O
2261 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2266 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2268 printf("ctl_ioctl: can't allocate ctl_io!\n");
2274 * Need to save the pool reference so it doesn't get
2275 * spammed by the user's ctl_io.
2277 pool_tmp = io->io_hdr.pool;
2279 memcpy(io, (void *)addr, sizeof(*io));
2281 io->io_hdr.pool = pool_tmp;
2283 * No status yet, so make sure the status is set properly.
2285 io->io_hdr.status = CTL_STATUS_NONE;
2288 * The user sets the initiator ID, target and LUN IDs.
2290 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2291 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2292 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2293 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2294 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2296 retval = ctl_ioctl_submit_wait(io);
2303 memcpy((void *)addr, io, sizeof(*io));
2305 /* return this to our pool */
2310 case CTL_ENABLE_PORT:
2311 case CTL_DISABLE_PORT:
2312 case CTL_SET_PORT_WWNS: {
2313 struct ctl_port *port;
2314 struct ctl_port_entry *entry;
2316 entry = (struct ctl_port_entry *)addr;
2318 mtx_lock(&softc->ctl_lock);
2319 STAILQ_FOREACH(port, &softc->port_list, links) {
2325 if ((entry->port_type == CTL_PORT_NONE)
2326 && (entry->targ_port == port->targ_port)) {
2328 * If the user only wants to enable or
2329 * disable or set WWNs on a specific port,
2330 * do the operation and we're done.
2334 } else if (entry->port_type & port->port_type) {
2336 * Compare the user's type mask with the
2337 * particular frontend type to see if we
2344 * Make sure the user isn't trying to set
2345 * WWNs on multiple ports at the same time.
2347 if (cmd == CTL_SET_PORT_WWNS) {
2348 printf("%s: Can't set WWNs on "
2349 "multiple ports\n", __func__);
2356 * XXX KDM we have to drop the lock here,
2357 * because the online/offline operations
2358 * can potentially block. We need to
2359 * reference count the frontends so they
2362 mtx_unlock(&softc->ctl_lock);
2364 if (cmd == CTL_ENABLE_PORT) {
2365 struct ctl_lun *lun;
2367 STAILQ_FOREACH(lun, &softc->lun_list,
2369 port->lun_enable(port->targ_lun_arg,
2374 ctl_port_online(port);
2375 } else if (cmd == CTL_DISABLE_PORT) {
2376 struct ctl_lun *lun;
2378 ctl_port_offline(port);
2380 STAILQ_FOREACH(lun, &softc->lun_list,
2389 mtx_lock(&softc->ctl_lock);
2391 if (cmd == CTL_SET_PORT_WWNS)
2392 ctl_port_set_wwns(port,
2393 (entry->flags & CTL_PORT_WWNN_VALID) ?
2395 (entry->flags & CTL_PORT_WWPN_VALID) ?
2396 1 : 0, entry->wwpn);
2401 mtx_unlock(&softc->ctl_lock);
2404 case CTL_GET_PORT_LIST: {
2405 struct ctl_port *port;
2406 struct ctl_port_list *list;
2409 list = (struct ctl_port_list *)addr;
2411 if (list->alloc_len != (list->alloc_num *
2412 sizeof(struct ctl_port_entry))) {
2413 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2414 "alloc_num %u * sizeof(struct ctl_port_entry) "
2415 "%zu\n", __func__, list->alloc_len,
2416 list->alloc_num, sizeof(struct ctl_port_entry));
2422 list->dropped_num = 0;
2424 mtx_lock(&softc->ctl_lock);
2425 STAILQ_FOREACH(port, &softc->port_list, links) {
2426 struct ctl_port_entry entry, *list_entry;
2428 if (list->fill_num >= list->alloc_num) {
2429 list->dropped_num++;
2433 entry.port_type = port->port_type;
2434 strlcpy(entry.port_name, port->port_name,
2435 sizeof(entry.port_name));
2436 entry.targ_port = port->targ_port;
2437 entry.physical_port = port->physical_port;
2438 entry.virtual_port = port->virtual_port;
2439 entry.wwnn = port->wwnn;
2440 entry.wwpn = port->wwpn;
2441 if (port->status & CTL_PORT_STATUS_ONLINE)
2446 list_entry = &list->entries[i];
2448 retval = copyout(&entry, list_entry, sizeof(entry));
2450 printf("%s: CTL_GET_PORT_LIST: copyout "
2451 "returned %d\n", __func__, retval);
2456 list->fill_len += sizeof(entry);
2458 mtx_unlock(&softc->ctl_lock);
2461 * If this is non-zero, we had a copyout fault, so there's
2462 * probably no point in attempting to set the status inside
2468 if (list->dropped_num > 0)
2469 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2471 list->status = CTL_PORT_LIST_OK;
2474 case CTL_DUMP_OOA: {
2475 struct ctl_lun *lun;
2480 mtx_lock(&softc->ctl_lock);
2481 printf("Dumping OOA queues:\n");
2482 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2483 mtx_lock(&lun->lun_lock);
2484 for (io = (union ctl_io *)TAILQ_FIRST(
2485 &lun->ooa_queue); io != NULL;
2486 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2488 sbuf_new(&sb, printbuf, sizeof(printbuf),
2490 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2494 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2496 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2498 CTL_FLAG_ABORT) ? " ABORT" : "",
2500 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2501 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2503 printf("%s\n", sbuf_data(&sb));
2505 mtx_unlock(&lun->lun_lock);
2507 printf("OOA queues dump done\n");
2508 mtx_unlock(&softc->ctl_lock);
2512 struct ctl_lun *lun;
2513 struct ctl_ooa *ooa_hdr;
2514 struct ctl_ooa_entry *entries;
2515 uint32_t cur_fill_num;
2517 ooa_hdr = (struct ctl_ooa *)addr;
2519 if ((ooa_hdr->alloc_len == 0)
2520 || (ooa_hdr->alloc_num == 0)) {
2521 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2522 "must be non-zero\n", __func__,
2523 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2528 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2529 sizeof(struct ctl_ooa_entry))) {
2530 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2531 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2532 __func__, ooa_hdr->alloc_len,
2533 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2538 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2539 if (entries == NULL) {
2540 printf("%s: could not allocate %d bytes for OOA "
2541 "dump\n", __func__, ooa_hdr->alloc_len);
2546 mtx_lock(&softc->ctl_lock);
2547 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2548 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2549 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2550 mtx_unlock(&softc->ctl_lock);
2551 free(entries, M_CTL);
2552 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2553 __func__, (uintmax_t)ooa_hdr->lun_num);
2560 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2561 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2562 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2568 mtx_unlock(&softc->ctl_lock);
2569 free(entries, M_CTL);
2573 lun = softc->ctl_luns[ooa_hdr->lun_num];
2575 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2578 mtx_unlock(&softc->ctl_lock);
2580 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2581 ooa_hdr->fill_len = ooa_hdr->fill_num *
2582 sizeof(struct ctl_ooa_entry);
2583 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2585 printf("%s: error copying out %d bytes for OOA dump\n",
2586 __func__, ooa_hdr->fill_len);
2589 getbintime(&ooa_hdr->cur_bt);
2591 if (cur_fill_num > ooa_hdr->alloc_num) {
2592 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2593 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2595 ooa_hdr->dropped_num = 0;
2596 ooa_hdr->status = CTL_OOA_OK;
2599 free(entries, M_CTL);
2602 case CTL_CHECK_OOA: {
2604 struct ctl_lun *lun;
2605 struct ctl_ooa_info *ooa_info;
2608 ooa_info = (struct ctl_ooa_info *)addr;
2610 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2611 ooa_info->status = CTL_OOA_INVALID_LUN;
2614 mtx_lock(&softc->ctl_lock);
2615 lun = softc->ctl_luns[ooa_info->lun_id];
2617 mtx_unlock(&softc->ctl_lock);
2618 ooa_info->status = CTL_OOA_INVALID_LUN;
2621 mtx_lock(&lun->lun_lock);
2622 mtx_unlock(&softc->ctl_lock);
2623 ooa_info->num_entries = 0;
2624 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2625 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2626 &io->io_hdr, ooa_links)) {
2627 ooa_info->num_entries++;
2629 mtx_unlock(&lun->lun_lock);
2631 ooa_info->status = CTL_OOA_SUCCESS;
2635 case CTL_HARD_START:
2636 case CTL_HARD_STOP: {
2637 struct ctl_fe_ioctl_startstop_info ss_info;
2638 struct cfi_metatask *metatask;
2641 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2643 cv_init(&ss_info.sem, "hard start/stop cv" );
2645 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2646 if (metatask == NULL) {
2648 mtx_destroy(&hs_mtx);
2652 if (cmd == CTL_HARD_START)
2653 metatask->tasktype = CFI_TASK_STARTUP;
2655 metatask->tasktype = CFI_TASK_SHUTDOWN;
2657 metatask->callback = ctl_ioctl_hard_startstop_callback;
2658 metatask->callback_arg = &ss_info;
2660 cfi_action(metatask);
2662 /* Wait for the callback */
2664 cv_wait_sig(&ss_info.sem, &hs_mtx);
2665 mtx_unlock(&hs_mtx);
2668 * All information has been copied from the metatask by the
2669 * time cv_broadcast() is called, so we free the metatask here.
2671 cfi_free_metatask(metatask);
2673 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2675 mtx_destroy(&hs_mtx);
2679 struct ctl_bbrread_info *bbr_info;
2680 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2682 struct cfi_metatask *metatask;
2684 bbr_info = (struct ctl_bbrread_info *)addr;
2686 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2688 bzero(&bbr_mtx, sizeof(bbr_mtx));
2689 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2691 fe_bbr_info.bbr_info = bbr_info;
2692 fe_bbr_info.lock = &bbr_mtx;
2694 cv_init(&fe_bbr_info.sem, "BBR read cv");
2695 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2697 if (metatask == NULL) {
2698 mtx_destroy(&bbr_mtx);
2699 cv_destroy(&fe_bbr_info.sem);
2703 metatask->tasktype = CFI_TASK_BBRREAD;
2704 metatask->callback = ctl_ioctl_bbrread_callback;
2705 metatask->callback_arg = &fe_bbr_info;
2706 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2707 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2708 metatask->taskinfo.bbrread.len = bbr_info->len;
2710 cfi_action(metatask);
2713 while (fe_bbr_info.wakeup_done == 0)
2714 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2715 mtx_unlock(&bbr_mtx);
2717 bbr_info->status = metatask->status;
2718 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2719 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2720 memcpy(&bbr_info->sense_data,
2721 &metatask->taskinfo.bbrread.sense_data,
2722 ctl_min(sizeof(bbr_info->sense_data),
2723 sizeof(metatask->taskinfo.bbrread.sense_data)));
2725 cfi_free_metatask(metatask);
2727 mtx_destroy(&bbr_mtx);
2728 cv_destroy(&fe_bbr_info.sem);
2732 case CTL_DELAY_IO: {
2733 struct ctl_io_delay_info *delay_info;
2735 struct ctl_lun *lun;
2736 #endif /* CTL_IO_DELAY */
2738 delay_info = (struct ctl_io_delay_info *)addr;
2741 mtx_lock(&softc->ctl_lock);
2743 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2744 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2745 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2747 lun = softc->ctl_luns[delay_info->lun_id];
2748 mtx_lock(&lun->lun_lock);
2750 delay_info->status = CTL_DELAY_STATUS_OK;
2752 switch (delay_info->delay_type) {
2753 case CTL_DELAY_TYPE_CONT:
2755 case CTL_DELAY_TYPE_ONESHOT:
2758 delay_info->status =
2759 CTL_DELAY_STATUS_INVALID_TYPE;
2763 switch (delay_info->delay_loc) {
2764 case CTL_DELAY_LOC_DATAMOVE:
2765 lun->delay_info.datamove_type =
2766 delay_info->delay_type;
2767 lun->delay_info.datamove_delay =
2768 delay_info->delay_secs;
2770 case CTL_DELAY_LOC_DONE:
2771 lun->delay_info.done_type =
2772 delay_info->delay_type;
2773 lun->delay_info.done_delay =
2774 delay_info->delay_secs;
2777 delay_info->status =
2778 CTL_DELAY_STATUS_INVALID_LOC;
2781 mtx_unlock(&lun->lun_lock);
2784 mtx_unlock(&softc->ctl_lock);
2786 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2787 #endif /* CTL_IO_DELAY */
2790 case CTL_REALSYNC_SET: {
2793 syncstate = (int *)addr;
2795 mtx_lock(&softc->ctl_lock);
2796 switch (*syncstate) {
2798 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2801 softc->flags |= CTL_FLAG_REAL_SYNC;
2807 mtx_unlock(&softc->ctl_lock);
2810 case CTL_REALSYNC_GET: {
2813 syncstate = (int*)addr;
2815 mtx_lock(&softc->ctl_lock);
2816 if (softc->flags & CTL_FLAG_REAL_SYNC)
2820 mtx_unlock(&softc->ctl_lock);
2826 struct ctl_sync_info *sync_info;
2827 struct ctl_lun *lun;
2829 sync_info = (struct ctl_sync_info *)addr;
2831 mtx_lock(&softc->ctl_lock);
2832 lun = softc->ctl_luns[sync_info->lun_id];
2834 mtx_unlock(&softc->ctl_lock);
2835 sync_info->status = CTL_GS_SYNC_NO_LUN;
2838 * Get or set the sync interval. We're not bounds checking
2839 * in the set case, hopefully the user won't do something
2842 mtx_lock(&lun->lun_lock);
2843 mtx_unlock(&softc->ctl_lock);
2844 if (cmd == CTL_GETSYNC)
2845 sync_info->sync_interval = lun->sync_interval;
2847 lun->sync_interval = sync_info->sync_interval;
2848 mtx_unlock(&lun->lun_lock);
2850 sync_info->status = CTL_GS_SYNC_OK;
2854 case CTL_GETSTATS: {
2855 struct ctl_stats *stats;
2856 struct ctl_lun *lun;
2859 stats = (struct ctl_stats *)addr;
2861 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2863 stats->status = CTL_SS_NEED_MORE_SPACE;
2864 stats->num_luns = softc->num_luns;
2868 * XXX KDM no locking here. If the LUN list changes,
2869 * things can blow up.
2871 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2872 i++, lun = STAILQ_NEXT(lun, links)) {
2873 retval = copyout(&lun->stats, &stats->lun_stats[i],
2874 sizeof(lun->stats));
2878 stats->num_luns = softc->num_luns;
2879 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2881 stats->status = CTL_SS_OK;
2883 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2885 stats->flags = CTL_STATS_FLAG_NONE;
2887 getnanouptime(&stats->timestamp);
2890 case CTL_ERROR_INJECT: {
2891 struct ctl_error_desc *err_desc, *new_err_desc;
2892 struct ctl_lun *lun;
2894 err_desc = (struct ctl_error_desc *)addr;
2896 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2898 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2900 mtx_lock(&softc->ctl_lock);
2901 lun = softc->ctl_luns[err_desc->lun_id];
2903 mtx_unlock(&softc->ctl_lock);
2904 free(new_err_desc, M_CTL);
2905 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2906 __func__, (uintmax_t)err_desc->lun_id);
2910 mtx_lock(&lun->lun_lock);
2911 mtx_unlock(&softc->ctl_lock);
2914 * We could do some checking here to verify the validity
2915 * of the request, but given the complexity of error
2916 * injection requests, the checking logic would be fairly
2919 * For now, if the request is invalid, it just won't get
2920 * executed and might get deleted.
2922 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2925 * XXX KDM check to make sure the serial number is unique,
2926 * in case we somehow manage to wrap. That shouldn't
2927 * happen for a very long time, but it's the right thing to
2930 new_err_desc->serial = lun->error_serial;
2931 err_desc->serial = lun->error_serial;
2932 lun->error_serial++;
2934 mtx_unlock(&lun->lun_lock);
2937 case CTL_ERROR_INJECT_DELETE: {
2938 struct ctl_error_desc *delete_desc, *desc, *desc2;
2939 struct ctl_lun *lun;
2942 delete_desc = (struct ctl_error_desc *)addr;
2945 mtx_lock(&softc->ctl_lock);
2946 lun = softc->ctl_luns[delete_desc->lun_id];
2948 mtx_unlock(&softc->ctl_lock);
2949 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2950 __func__, (uintmax_t)delete_desc->lun_id);
2954 mtx_lock(&lun->lun_lock);
2955 mtx_unlock(&softc->ctl_lock);
2956 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2957 if (desc->serial != delete_desc->serial)
2960 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2965 mtx_unlock(&lun->lun_lock);
2966 if (delete_done == 0) {
2967 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2968 "error serial %ju on LUN %u\n", __func__,
2969 delete_desc->serial, delete_desc->lun_id);
2975 case CTL_DUMP_STRUCTS: {
2977 struct ctl_port *port;
2978 struct ctl_frontend *fe;
2980 mtx_lock(&softc->ctl_lock);
2981 printf("CTL Persistent Reservation information start:\n");
2982 for (i = 0; i < CTL_MAX_LUNS; i++) {
2983 struct ctl_lun *lun;
2985 lun = softc->ctl_luns[i];
2988 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2991 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2992 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2993 idx = j * CTL_MAX_INIT_PER_PORT + k;
2994 if (lun->per_res[idx].registered == 0)
2996 printf(" LUN %d port %d iid %d key "
2998 (uintmax_t)scsi_8btou64(
2999 lun->per_res[idx].res_key.key));
3003 printf("CTL Persistent Reservation information end\n");
3004 printf("CTL Ports:\n");
3005 STAILQ_FOREACH(port, &softc->port_list, links) {
3006 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3007 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3008 port->frontend->name, port->port_type,
3009 port->physical_port, port->virtual_port,
3010 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3011 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3012 if (port->wwpn_iid[j].in_use == 0 &&
3013 port->wwpn_iid[j].wwpn == 0 &&
3014 port->wwpn_iid[j].name == NULL)
3017 printf(" iid %u use %d WWPN %#jx '%s'\n",
3018 j, port->wwpn_iid[j].in_use,
3019 (uintmax_t)port->wwpn_iid[j].wwpn,
3020 port->wwpn_iid[j].name);
3023 printf("CTL Port information end\n");
3024 mtx_unlock(&softc->ctl_lock);
3026 * XXX KDM calling this without a lock. We'd likely want
3027 * to drop the lock before calling the frontend's dump
3030 printf("CTL Frontends:\n");
3031 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3032 printf(" Frontend '%s'\n", fe->name);
3033 if (fe->fe_dump != NULL)
3036 printf("CTL Frontend information end\n");
3040 struct ctl_lun_req *lun_req;
3041 struct ctl_backend_driver *backend;
3043 lun_req = (struct ctl_lun_req *)addr;
3045 backend = ctl_backend_find(lun_req->backend);
3046 if (backend == NULL) {
3047 lun_req->status = CTL_LUN_ERROR;
3048 snprintf(lun_req->error_str,
3049 sizeof(lun_req->error_str),
3050 "Backend \"%s\" not found.",
3054 if (lun_req->num_be_args > 0) {
3055 lun_req->kern_be_args = ctl_copyin_args(
3056 lun_req->num_be_args,
3059 sizeof(lun_req->error_str));
3060 if (lun_req->kern_be_args == NULL) {
3061 lun_req->status = CTL_LUN_ERROR;
3066 retval = backend->ioctl(dev, cmd, addr, flag, td);
3068 if (lun_req->num_be_args > 0) {
3069 ctl_copyout_args(lun_req->num_be_args,
3070 lun_req->kern_be_args);
3071 ctl_free_args(lun_req->num_be_args,
3072 lun_req->kern_be_args);
3076 case CTL_LUN_LIST: {
3078 struct ctl_lun *lun;
3079 struct ctl_lun_list *list;
3080 struct ctl_option *opt;
3082 list = (struct ctl_lun_list *)addr;
3085 * Allocate a fixed length sbuf here, based on the length
3086 * of the user's buffer. We could allocate an auto-extending
3087 * buffer, and then tell the user how much larger our
3088 * amount of data is than his buffer, but that presents
3091 * 1. The sbuf(9) routines use a blocking malloc, and so
3092 * we can't hold a lock while calling them with an
3093 * auto-extending buffer.
3095 * 2. There is not currently a LUN reference counting
3096 * mechanism, outside of outstanding transactions on
3097 * the LUN's OOA queue. So a LUN could go away on us
3098 * while we're getting the LUN number, backend-specific
3099 * information, etc. Thus, given the way things
3100 * currently work, we need to hold the CTL lock while
3101 * grabbing LUN information.
3103 * So, from the user's standpoint, the best thing to do is
3104 * allocate what he thinks is a reasonable buffer length,
3105 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3106 * double the buffer length and try again. (And repeat
3107 * that until he succeeds.)
3109 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3111 list->status = CTL_LUN_LIST_ERROR;
3112 snprintf(list->error_str, sizeof(list->error_str),
3113 "Unable to allocate %d bytes for LUN list",
3118 sbuf_printf(sb, "<ctllunlist>\n");
3120 mtx_lock(&softc->ctl_lock);
3121 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3122 mtx_lock(&lun->lun_lock);
3123 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3124 (uintmax_t)lun->lun);
3127 * Bail out as soon as we see that we've overfilled
3133 retval = sbuf_printf(sb, "\t<backend_type>%s"
3134 "</backend_type>\n",
3135 (lun->backend == NULL) ? "none" :
3136 lun->backend->name);
3141 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3142 lun->be_lun->lun_type);
3147 if (lun->backend == NULL) {
3148 retval = sbuf_printf(sb, "</lun>\n");
3154 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3155 (lun->be_lun->maxlba > 0) ?
3156 lun->be_lun->maxlba + 1 : 0);
3161 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3162 lun->be_lun->blocksize);
3167 retval = sbuf_printf(sb, "\t<serial_number>");
3172 retval = ctl_sbuf_printf_esc(sb,
3173 lun->be_lun->serial_num);
3178 retval = sbuf_printf(sb, "</serial_number>\n");
3183 retval = sbuf_printf(sb, "\t<device_id>");
3188 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3193 retval = sbuf_printf(sb, "</device_id>\n");
3198 if (lun->backend->lun_info != NULL) {
3199 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3203 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3204 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3205 opt->name, opt->value, opt->name);
3210 retval = sbuf_printf(sb, "</lun>\n");
3214 mtx_unlock(&lun->lun_lock);
3217 mtx_unlock(&lun->lun_lock);
3218 mtx_unlock(&softc->ctl_lock);
3221 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3224 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3225 snprintf(list->error_str, sizeof(list->error_str),
3226 "Out of space, %d bytes is too small",
3233 retval = copyout(sbuf_data(sb), list->lun_xml,
3236 list->fill_len = sbuf_len(sb) + 1;
3237 list->status = CTL_LUN_LIST_OK;
3242 struct ctl_iscsi *ci;
3243 struct ctl_frontend *fe;
3245 ci = (struct ctl_iscsi *)addr;
3247 fe = ctl_frontend_find("iscsi");
3249 ci->status = CTL_ISCSI_ERROR;
3250 snprintf(ci->error_str, sizeof(ci->error_str),
3251 "Frontend \"iscsi\" not found.");
3255 retval = fe->ioctl(dev, cmd, addr, flag, td);
3258 case CTL_PORT_REQ: {
3259 struct ctl_req *req;
3260 struct ctl_frontend *fe;
3262 req = (struct ctl_req *)addr;
3264 fe = ctl_frontend_find(req->driver);
3266 req->status = CTL_LUN_ERROR;
3267 snprintf(req->error_str, sizeof(req->error_str),
3268 "Frontend \"%s\" not found.", req->driver);
3271 if (req->num_args > 0) {
3272 req->kern_args = ctl_copyin_args(req->num_args,
3273 req->args, req->error_str, sizeof(req->error_str));
3274 if (req->kern_args == NULL) {
3275 req->status = CTL_LUN_ERROR;
3280 retval = fe->ioctl(dev, cmd, addr, flag, td);
3282 if (req->num_args > 0) {
3283 ctl_copyout_args(req->num_args, req->kern_args);
3284 ctl_free_args(req->num_args, req->kern_args);
3288 case CTL_PORT_LIST: {
3290 struct ctl_port *port;
3291 struct ctl_lun_list *list;
3292 struct ctl_option *opt;
3294 list = (struct ctl_lun_list *)addr;
3296 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3298 list->status = CTL_LUN_LIST_ERROR;
3299 snprintf(list->error_str, sizeof(list->error_str),
3300 "Unable to allocate %d bytes for LUN list",
3305 sbuf_printf(sb, "<ctlportlist>\n");
3307 mtx_lock(&softc->ctl_lock);
3308 STAILQ_FOREACH(port, &softc->port_list, links) {
3309 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3310 (uintmax_t)port->targ_port);
3313 * Bail out as soon as we see that we've overfilled
3319 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3320 "</frontend_type>\n", port->frontend->name);
3324 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3329 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3330 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3334 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3339 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3340 port->physical_port);
3344 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3345 port->virtual_port);
3349 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n",
3350 (uintmax_t)port->wwnn);
3354 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n",
3355 (uintmax_t)port->wwpn);
3359 if (port->port_info != NULL) {
3360 retval = port->port_info(port->onoff_arg, sb);
3364 STAILQ_FOREACH(opt, &port->options, links) {
3365 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3366 opt->name, opt->value, opt->name);
3371 retval = sbuf_printf(sb, "</targ_port>\n");
3375 mtx_unlock(&softc->ctl_lock);
3378 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3381 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3382 snprintf(list->error_str, sizeof(list->error_str),
3383 "Out of space, %d bytes is too small",
3390 retval = copyout(sbuf_data(sb), list->lun_xml,
3393 list->fill_len = sbuf_len(sb) + 1;
3394 list->status = CTL_LUN_LIST_OK;
3399 /* XXX KDM should we fix this? */
3401 struct ctl_backend_driver *backend;
3408 * We encode the backend type as the ioctl type for backend
3409 * ioctls. So parse it out here, and then search for a
3410 * backend of this type.
3412 type = _IOC_TYPE(cmd);
3414 STAILQ_FOREACH(backend, &softc->be_list, links) {
3415 if (backend->type == type) {
3421 printf("ctl: unknown ioctl command %#lx or backend "
3426 retval = backend->ioctl(dev, cmd, addr, flag, td);
3436 ctl_get_initindex(struct ctl_nexus *nexus)
3438 if (nexus->targ_port < CTL_MAX_PORTS)
3439 return (nexus->initid.id +
3440 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3442 return (nexus->initid.id +
3443 ((nexus->targ_port - CTL_MAX_PORTS) *
3444 CTL_MAX_INIT_PER_PORT));
3448 ctl_get_resindex(struct ctl_nexus *nexus)
3450 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3454 ctl_port_idx(int port_num)
3456 if (port_num < CTL_MAX_PORTS)
3459 return(port_num - CTL_MAX_PORTS);
3463 ctl_map_lun(int port_num, uint32_t lun_id)
3465 struct ctl_port *port;
3467 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3469 return (UINT32_MAX);
3470 if (port->lun_map == NULL)
3472 return (port->lun_map(port->targ_lun_arg, lun_id));
3476 ctl_map_lun_back(int port_num, uint32_t lun_id)
3478 struct ctl_port *port;
3481 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3482 if (port->lun_map == NULL)
3484 for (i = 0; i < CTL_MAX_LUNS; i++) {
3485 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3488 return (UINT32_MAX);
3492 * Note: This only works for bitmask sizes that are at least 32 bits, and
3493 * that are a power of 2.
3496 ctl_ffz(uint32_t *mask, uint32_t size)
3498 uint32_t num_chunks, num_pieces;
3501 num_chunks = (size >> 5);
3502 if (num_chunks == 0)
3504 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3506 for (i = 0; i < num_chunks; i++) {
3507 for (j = 0; j < num_pieces; j++) {
3508 if ((mask[i] & (1 << j)) == 0)
3509 return ((i << 5) + j);
3517 ctl_set_mask(uint32_t *mask, uint32_t bit)
3519 uint32_t chunk, piece;
3522 piece = bit % (sizeof(uint32_t) * 8);
3524 if ((mask[chunk] & (1 << piece)) != 0)
3527 mask[chunk] |= (1 << piece);
3533 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3535 uint32_t chunk, piece;
3538 piece = bit % (sizeof(uint32_t) * 8);
3540 if ((mask[chunk] & (1 << piece)) == 0)
3543 mask[chunk] &= ~(1 << piece);
3549 ctl_is_set(uint32_t *mask, uint32_t bit)
3551 uint32_t chunk, piece;
3554 piece = bit % (sizeof(uint32_t) * 8);
3556 if ((mask[chunk] & (1 << piece)) == 0)
3564 * The bus, target and lun are optional, they can be filled in later.
3565 * can_wait is used to determine whether we can wait on the malloc or not.
3568 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3569 uint32_t targ_lun, int can_wait)
3574 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3576 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3579 io->io_hdr.io_type = io_type;
3580 io->io_hdr.targ_port = targ_port;
3582 * XXX KDM this needs to change/go away. We need to move
3583 * to a preallocated pool of ctl_scsiio structures.
3585 io->io_hdr.nexus.targ_target.id = targ_target;
3586 io->io_hdr.nexus.targ_lun = targ_lun;
3593 ctl_kfree_io(union ctl_io *io)
3600 * ctl_softc, pool_type, total_ctl_io are passed in.
3601 * npool is passed out.
3604 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3605 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3608 union ctl_io *cur_io, *next_io;
3609 struct ctl_io_pool *pool;
3614 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3621 pool->type = pool_type;
3622 pool->ctl_softc = ctl_softc;
3624 mtx_lock(&ctl_softc->pool_lock);
3625 pool->id = ctl_softc->cur_pool_id++;
3626 mtx_unlock(&ctl_softc->pool_lock);
3628 pool->flags = CTL_POOL_FLAG_NONE;
3629 pool->refcount = 1; /* Reference for validity. */
3630 STAILQ_INIT(&pool->free_queue);
3633 * XXX KDM other options here:
3634 * - allocate a page at a time
3635 * - allocate one big chunk of memory.
3636 * Page allocation might work well, but would take a little more
3639 for (i = 0; i < total_ctl_io; i++) {
3640 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO,
3642 if (cur_io == NULL) {
3646 cur_io->io_hdr.pool = pool;
3647 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3648 pool->total_ctl_io++;
3649 pool->free_ctl_io++;
3653 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3654 cur_io != NULL; cur_io = next_io) {
3655 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3657 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3659 free(cur_io, M_CTLIO);
3665 mtx_lock(&ctl_softc->pool_lock);
3666 ctl_softc->num_pools++;
3667 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3669 * Increment our usage count if this is an external consumer, so we
3670 * can't get unloaded until the external consumer (most likely a
3671 * FETD) unloads and frees his pool.
3673 * XXX KDM will this increment the caller's module use count, or
3677 if ((pool_type != CTL_POOL_EMERGENCY)
3678 && (pool_type != CTL_POOL_INTERNAL)
3679 && (pool_type != CTL_POOL_4OTHERSC))
3683 mtx_unlock(&ctl_softc->pool_lock);
3693 ctl_pool_acquire(struct ctl_io_pool *pool)
3696 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3698 if (pool->flags & CTL_POOL_FLAG_INVALID)
3707 ctl_pool_release(struct ctl_io_pool *pool)
3709 struct ctl_softc *ctl_softc = pool->ctl_softc;
3712 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3714 if (--pool->refcount != 0)
3717 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3718 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3723 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3724 ctl_softc->num_pools--;
3727 * XXX KDM will this decrement the caller's usage count or mine?
3730 if ((pool->type != CTL_POOL_EMERGENCY)
3731 && (pool->type != CTL_POOL_INTERNAL)
3732 && (pool->type != CTL_POOL_4OTHERSC))
3740 ctl_pool_free(struct ctl_io_pool *pool)
3742 struct ctl_softc *ctl_softc;
3747 ctl_softc = pool->ctl_softc;
3748 mtx_lock(&ctl_softc->pool_lock);
3749 pool->flags |= CTL_POOL_FLAG_INVALID;
3750 ctl_pool_release(pool);
3751 mtx_unlock(&ctl_softc->pool_lock);
3755 * This routine does not block (except for spinlocks of course).
3756 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3760 ctl_alloc_io(void *pool_ref)
3763 struct ctl_softc *ctl_softc;
3764 struct ctl_io_pool *pool, *npool;
3765 struct ctl_io_pool *emergency_pool;
3767 pool = (struct ctl_io_pool *)pool_ref;
3770 printf("%s: pool is NULL\n", __func__);
3774 emergency_pool = NULL;
3776 ctl_softc = pool->ctl_softc;
3778 mtx_lock(&ctl_softc->pool_lock);
3780 * First, try to get the io structure from the user's pool.
3782 if (ctl_pool_acquire(pool) == 0) {
3783 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3785 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3786 pool->total_allocated++;
3787 pool->free_ctl_io--;
3788 mtx_unlock(&ctl_softc->pool_lock);
3791 ctl_pool_release(pool);
3794 * If he doesn't have any io structures left, search for an
3795 * emergency pool and grab one from there.
3797 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3798 if (npool->type != CTL_POOL_EMERGENCY)
3801 if (ctl_pool_acquire(npool) != 0)
3804 emergency_pool = npool;
3806 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3808 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3809 npool->total_allocated++;
3810 npool->free_ctl_io--;
3811 mtx_unlock(&ctl_softc->pool_lock);
3814 ctl_pool_release(npool);
3817 /* Drop the spinlock before we malloc */
3818 mtx_unlock(&ctl_softc->pool_lock);
3821 * The emergency pool (if it exists) didn't have one, so try an
3822 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3824 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT);
3827 * If the emergency pool exists but is empty, add this
3828 * ctl_io to its list when it gets freed.
3830 if (emergency_pool != NULL) {
3831 mtx_lock(&ctl_softc->pool_lock);
3832 if (ctl_pool_acquire(emergency_pool) == 0) {
3833 io->io_hdr.pool = emergency_pool;
3834 emergency_pool->total_ctl_io++;
3836 * Need to bump this, otherwise
3837 * total_allocated and total_freed won't
3838 * match when we no longer have anything
3841 emergency_pool->total_allocated++;
3843 mtx_unlock(&ctl_softc->pool_lock);
3845 io->io_hdr.pool = NULL;
3852 ctl_free_io(union ctl_io *io)
3858 * If this ctl_io has a pool, return it to that pool.
3860 if (io->io_hdr.pool != NULL) {
3861 struct ctl_io_pool *pool;
3863 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3864 mtx_lock(&pool->ctl_softc->pool_lock);
3865 io->io_hdr.io_type = 0xff;
3866 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3867 pool->total_freed++;
3868 pool->free_ctl_io++;
3869 ctl_pool_release(pool);
3870 mtx_unlock(&pool->ctl_softc->pool_lock);
3873 * Otherwise, just free it. We probably malloced it and
3874 * the emergency pool wasn't available.
3882 ctl_zero_io(union ctl_io *io)
3890 * May need to preserve linked list pointers at some point too.
3892 pool_ref = io->io_hdr.pool;
3894 memset(io, 0, sizeof(*io));
3896 io->io_hdr.pool = pool_ref;
3900 * This routine is currently used for internal copies of ctl_ios that need
3901 * to persist for some reason after we've already returned status to the
3902 * FETD. (Thus the flag set.)
3905 * Note that this makes a blind copy of all fields in the ctl_io, except
3906 * for the pool reference. This includes any memory that has been
3907 * allocated! That memory will no longer be valid after done has been
3908 * called, so this would be VERY DANGEROUS for command that actually does
3909 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3910 * start and stop commands, which don't transfer any data, so this is not a
3911 * problem. If it is used for anything else, the caller would also need to
3912 * allocate data buffer space and this routine would need to be modified to
3913 * copy the data buffer(s) as well.
3916 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3925 * May need to preserve linked list pointers at some point too.
3927 pool_ref = dest->io_hdr.pool;
3929 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3931 dest->io_hdr.pool = pool_ref;
3933 * We need to know that this is an internal copy, and doesn't need
3934 * to get passed back to the FETD that allocated it.
3936 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3941 ctl_update_power_subpage(struct copan_power_subpage *page)
3943 int num_luns, num_partitions, config_type;
3944 struct ctl_softc *softc;
3945 cs_BOOL_t aor_present, shelf_50pct_power;
3946 cs_raidset_personality_t rs_type;
3947 int max_active_luns;
3949 softc = control_softc;
3951 /* subtract out the processor LUN */
3952 num_luns = softc->num_luns - 1;
3954 * Default to 7 LUNs active, which was the only number we allowed
3957 max_active_luns = 7;
3959 num_partitions = config_GetRsPartitionInfo();
3960 config_type = config_GetConfigType();
3961 shelf_50pct_power = config_GetShelfPowerMode();
3962 aor_present = config_IsAorRsPresent();
3964 rs_type = ddb_GetRsRaidType(1);
3965 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3966 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3967 EPRINT(0, "Unsupported RS type %d!", rs_type);
3971 page->total_luns = num_luns;
3973 switch (config_type) {
3976 * In a 40 drive configuration, it doesn't matter what DC
3977 * cards we have, whether we have AOR enabled or not,
3978 * partitioning or not, or what type of RAIDset we have.
3979 * In that scenario, we can power up every LUN we present
3982 max_active_luns = num_luns;
3986 if (shelf_50pct_power == CS_FALSE) {
3988 if (aor_present == CS_TRUE) {
3990 CS_RAIDSET_PERSONALITY_RAID5) {
3991 max_active_luns = 7;
3992 } else if (rs_type ==
3993 CS_RAIDSET_PERSONALITY_RAID1){
3994 max_active_luns = 14;
3996 /* XXX KDM now what?? */
4000 CS_RAIDSET_PERSONALITY_RAID5) {
4001 max_active_luns = 8;
4002 } else if (rs_type ==
4003 CS_RAIDSET_PERSONALITY_RAID1){
4004 max_active_luns = 16;
4006 /* XXX KDM now what?? */
4012 * With 50% power in a 64 drive configuration, we
4013 * can power all LUNs we present.
4015 max_active_luns = num_luns;
4019 if (shelf_50pct_power == CS_FALSE) {
4021 if (aor_present == CS_TRUE) {
4023 CS_RAIDSET_PERSONALITY_RAID5) {
4024 max_active_luns = 7;
4025 } else if (rs_type ==
4026 CS_RAIDSET_PERSONALITY_RAID1){
4027 max_active_luns = 14;
4029 /* XXX KDM now what?? */
4033 CS_RAIDSET_PERSONALITY_RAID5) {
4034 max_active_luns = 8;
4035 } else if (rs_type ==
4036 CS_RAIDSET_PERSONALITY_RAID1){
4037 max_active_luns = 16;
4039 /* XXX KDM now what?? */
4044 if (aor_present == CS_TRUE) {
4046 CS_RAIDSET_PERSONALITY_RAID5) {
4047 max_active_luns = 14;
4048 } else if (rs_type ==
4049 CS_RAIDSET_PERSONALITY_RAID1){
4051 * We're assuming here that disk
4052 * caching is enabled, and so we're
4053 * able to power up half of each
4054 * LUN, and cache all writes.
4056 max_active_luns = num_luns;
4058 /* XXX KDM now what?? */
4062 CS_RAIDSET_PERSONALITY_RAID5) {
4063 max_active_luns = 15;
4064 } else if (rs_type ==
4065 CS_RAIDSET_PERSONALITY_RAID1){
4066 max_active_luns = 30;
4068 /* XXX KDM now what?? */
4075 * In this case, we have an unknown configuration, so we
4076 * just use the default from above.
4081 page->max_active_luns = max_active_luns;
4083 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
4084 page->total_luns, page->max_active_luns);
4087 #endif /* NEEDTOPORT */
4090 * This routine could be used in the future to load default and/or saved
4091 * mode page parameters for a particuar lun.
4094 ctl_init_page_index(struct ctl_lun *lun)
4097 struct ctl_page_index *page_index;
4098 struct ctl_softc *softc;
4100 memcpy(&lun->mode_pages.index, page_index_template,
4101 sizeof(page_index_template));
4103 softc = lun->ctl_softc;
4105 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4107 page_index = &lun->mode_pages.index[i];
4109 * If this is a disk-only mode page, there's no point in
4110 * setting it up. For some pages, we have to have some
4111 * basic information about the disk in order to calculate the
4114 if ((lun->be_lun->lun_type != T_DIRECT)
4115 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4118 switch (page_index->page_code & SMPH_PC_MASK) {
4119 case SMS_FORMAT_DEVICE_PAGE: {
4120 struct scsi_format_page *format_page;
4122 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4123 panic("subpage is incorrect!");
4126 * Sectors per track are set above. Bytes per
4127 * sector need to be set here on a per-LUN basis.
4129 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4130 &format_page_default,
4131 sizeof(format_page_default));
4132 memcpy(&lun->mode_pages.format_page[
4133 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4134 sizeof(format_page_changeable));
4135 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4136 &format_page_default,
4137 sizeof(format_page_default));
4138 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4139 &format_page_default,
4140 sizeof(format_page_default));
4142 format_page = &lun->mode_pages.format_page[
4144 scsi_ulto2b(lun->be_lun->blocksize,
4145 format_page->bytes_per_sector);
4147 format_page = &lun->mode_pages.format_page[
4149 scsi_ulto2b(lun->be_lun->blocksize,
4150 format_page->bytes_per_sector);
4152 format_page = &lun->mode_pages.format_page[
4154 scsi_ulto2b(lun->be_lun->blocksize,
4155 format_page->bytes_per_sector);
4157 page_index->page_data =
4158 (uint8_t *)lun->mode_pages.format_page;
4161 case SMS_RIGID_DISK_PAGE: {
4162 struct scsi_rigid_disk_page *rigid_disk_page;
4163 uint32_t sectors_per_cylinder;
4167 #endif /* !__XSCALE__ */
4169 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4170 panic("invalid subpage value %d",
4171 page_index->subpage);
4174 * Rotation rate and sectors per track are set
4175 * above. We calculate the cylinders here based on
4176 * capacity. Due to the number of heads and
4177 * sectors per track we're using, smaller arrays
4178 * may turn out to have 0 cylinders. Linux and
4179 * FreeBSD don't pay attention to these mode pages
4180 * to figure out capacity, but Solaris does. It
4181 * seems to deal with 0 cylinders just fine, and
4182 * works out a fake geometry based on the capacity.
4184 memcpy(&lun->mode_pages.rigid_disk_page[
4185 CTL_PAGE_CURRENT], &rigid_disk_page_default,
4186 sizeof(rigid_disk_page_default));
4187 memcpy(&lun->mode_pages.rigid_disk_page[
4188 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4189 sizeof(rigid_disk_page_changeable));
4190 memcpy(&lun->mode_pages.rigid_disk_page[
4191 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4192 sizeof(rigid_disk_page_default));
4193 memcpy(&lun->mode_pages.rigid_disk_page[
4194 CTL_PAGE_SAVED], &rigid_disk_page_default,
4195 sizeof(rigid_disk_page_default));
4197 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4201 * The divide method here will be more accurate,
4202 * probably, but results in floating point being
4203 * used in the kernel on i386 (__udivdi3()). On the
4204 * XScale, though, __udivdi3() is implemented in
4207 * The shift method for cylinder calculation is
4208 * accurate if sectors_per_cylinder is a power of
4209 * 2. Otherwise it might be slightly off -- you
4210 * might have a bit of a truncation problem.
4213 cylinders = (lun->be_lun->maxlba + 1) /
4214 sectors_per_cylinder;
4216 for (shift = 31; shift > 0; shift--) {
4217 if (sectors_per_cylinder & (1 << shift))
4220 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4224 * We've basically got 3 bytes, or 24 bits for the
4225 * cylinder size in the mode page. If we're over,
4226 * just round down to 2^24.
4228 if (cylinders > 0xffffff)
4229 cylinders = 0xffffff;
4231 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4233 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4235 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4237 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4239 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4241 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4243 page_index->page_data =
4244 (uint8_t *)lun->mode_pages.rigid_disk_page;
4247 case SMS_CACHING_PAGE: {
4249 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4250 panic("invalid subpage value %d",
4251 page_index->subpage);
4253 * Defaults should be okay here, no calculations
4256 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4257 &caching_page_default,
4258 sizeof(caching_page_default));
4259 memcpy(&lun->mode_pages.caching_page[
4260 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4261 sizeof(caching_page_changeable));
4262 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4263 &caching_page_default,
4264 sizeof(caching_page_default));
4265 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4266 &caching_page_default,
4267 sizeof(caching_page_default));
4268 page_index->page_data =
4269 (uint8_t *)lun->mode_pages.caching_page;
4272 case SMS_CONTROL_MODE_PAGE: {
4274 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4275 panic("invalid subpage value %d",
4276 page_index->subpage);
4279 * Defaults should be okay here, no calculations
4282 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4283 &control_page_default,
4284 sizeof(control_page_default));
4285 memcpy(&lun->mode_pages.control_page[
4286 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4287 sizeof(control_page_changeable));
4288 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4289 &control_page_default,
4290 sizeof(control_page_default));
4291 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4292 &control_page_default,
4293 sizeof(control_page_default));
4294 page_index->page_data =
4295 (uint8_t *)lun->mode_pages.control_page;
4299 case SMS_VENDOR_SPECIFIC_PAGE:{
4300 switch (page_index->subpage) {
4301 case PWR_SUBPAGE_CODE: {
4302 struct copan_power_subpage *current_page,
4305 memcpy(&lun->mode_pages.power_subpage[
4307 &power_page_default,
4308 sizeof(power_page_default));
4309 memcpy(&lun->mode_pages.power_subpage[
4310 CTL_PAGE_CHANGEABLE],
4311 &power_page_changeable,
4312 sizeof(power_page_changeable));
4313 memcpy(&lun->mode_pages.power_subpage[
4315 &power_page_default,
4316 sizeof(power_page_default));
4317 memcpy(&lun->mode_pages.power_subpage[
4319 &power_page_default,
4320 sizeof(power_page_default));
4321 page_index->page_data =
4322 (uint8_t *)lun->mode_pages.power_subpage;
4324 current_page = (struct copan_power_subpage *)
4325 (page_index->page_data +
4326 (page_index->page_len *
4328 saved_page = (struct copan_power_subpage *)
4329 (page_index->page_data +
4330 (page_index->page_len *
4334 case APS_SUBPAGE_CODE: {
4335 struct copan_aps_subpage *current_page,
4338 // This gets set multiple times but
4339 // it should always be the same. It's
4340 // only done during init so who cares.
4341 index_to_aps_page = i;
4343 memcpy(&lun->mode_pages.aps_subpage[
4346 sizeof(aps_page_default));
4347 memcpy(&lun->mode_pages.aps_subpage[
4348 CTL_PAGE_CHANGEABLE],
4349 &aps_page_changeable,
4350 sizeof(aps_page_changeable));
4351 memcpy(&lun->mode_pages.aps_subpage[
4354 sizeof(aps_page_default));
4355 memcpy(&lun->mode_pages.aps_subpage[
4358 sizeof(aps_page_default));
4359 page_index->page_data =
4360 (uint8_t *)lun->mode_pages.aps_subpage;
4362 current_page = (struct copan_aps_subpage *)
4363 (page_index->page_data +
4364 (page_index->page_len *
4366 saved_page = (struct copan_aps_subpage *)
4367 (page_index->page_data +
4368 (page_index->page_len *
4372 case DBGCNF_SUBPAGE_CODE: {
4373 struct copan_debugconf_subpage *current_page,
4376 memcpy(&lun->mode_pages.debugconf_subpage[
4378 &debugconf_page_default,
4379 sizeof(debugconf_page_default));
4380 memcpy(&lun->mode_pages.debugconf_subpage[
4381 CTL_PAGE_CHANGEABLE],
4382 &debugconf_page_changeable,
4383 sizeof(debugconf_page_changeable));
4384 memcpy(&lun->mode_pages.debugconf_subpage[
4386 &debugconf_page_default,
4387 sizeof(debugconf_page_default));
4388 memcpy(&lun->mode_pages.debugconf_subpage[
4390 &debugconf_page_default,
4391 sizeof(debugconf_page_default));
4392 page_index->page_data =
4393 (uint8_t *)lun->mode_pages.debugconf_subpage;
4395 current_page = (struct copan_debugconf_subpage *)
4396 (page_index->page_data +
4397 (page_index->page_len *
4399 saved_page = (struct copan_debugconf_subpage *)
4400 (page_index->page_data +
4401 (page_index->page_len *
4406 panic("invalid subpage value %d",
4407 page_index->subpage);
4413 panic("invalid page value %d",
4414 page_index->page_code & SMPH_PC_MASK);
4419 return (CTL_RETVAL_COMPLETE);
4426 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4427 * wants us to allocate the LUN and he can block.
4428 * - ctl_softc is always set
4429 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4431 * Returns 0 for success, non-zero (errno) for failure.
4434 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4435 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4437 struct ctl_lun *nlun, *lun;
4438 struct ctl_port *port;
4439 struct scsi_vpd_id_descriptor *desc;
4440 struct scsi_vpd_id_t10 *t10id;
4441 const char *eui, *naa, *scsiname, *vendor;
4442 int lun_number, i, lun_malloced;
4443 int devidlen, idlen1, idlen2 = 0, len;
4449 * We currently only support Direct Access or Processor LUN types.
4451 switch (be_lun->lun_type) {
4459 be_lun->lun_config_status(be_lun->be_lun,
4460 CTL_LUN_CONFIG_FAILURE);
4463 if (ctl_lun == NULL) {
4464 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4471 memset(lun, 0, sizeof(*lun));
4473 lun->flags = CTL_LUN_MALLOCED;
4475 /* Generate LUN ID. */
4476 devidlen = max(CTL_DEVID_MIN_LEN,
4477 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4478 idlen1 = sizeof(*t10id) + devidlen;
4479 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4480 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4481 if (scsiname != NULL) {
4482 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4483 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4485 eui = ctl_get_opt(&be_lun->options, "eui");
4487 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
4489 naa = ctl_get_opt(&be_lun->options, "naa");
4491 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
4493 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4494 M_CTL, M_WAITOK | M_ZERO);
4495 lun->lun_devid->len = len;
4496 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4497 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4498 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4499 desc->length = idlen1;
4500 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4501 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4502 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4503 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4505 strncpy(t10id->vendor, vendor,
4506 min(sizeof(t10id->vendor), strlen(vendor)));
4508 strncpy((char *)t10id->vendor_spec_id,
4509 (char *)be_lun->device_id, devidlen);
4510 if (scsiname != NULL) {
4511 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4513 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4514 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4515 SVPD_ID_TYPE_SCSI_NAME;
4516 desc->length = idlen2;
4517 strlcpy(desc->identifier, scsiname, idlen2);
4520 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4522 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4523 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4526 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier);
4529 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4531 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4532 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4535 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier);
4538 mtx_lock(&ctl_softc->ctl_lock);
4540 * See if the caller requested a particular LUN number. If so, see
4541 * if it is available. Otherwise, allocate the first available LUN.
4543 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4544 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4545 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4546 mtx_unlock(&ctl_softc->ctl_lock);
4547 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4548 printf("ctl: requested LUN ID %d is higher "
4549 "than CTL_MAX_LUNS - 1 (%d)\n",
4550 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4553 * XXX KDM return an error, or just assign
4554 * another LUN ID in this case??
4556 printf("ctl: requested LUN ID %d is already "
4557 "in use\n", be_lun->req_lun_id);
4559 if (lun->flags & CTL_LUN_MALLOCED)
4561 be_lun->lun_config_status(be_lun->be_lun,
4562 CTL_LUN_CONFIG_FAILURE);
4565 lun_number = be_lun->req_lun_id;
4567 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4568 if (lun_number == -1) {
4569 mtx_unlock(&ctl_softc->ctl_lock);
4570 printf("ctl: can't allocate LUN on target %ju, out of "
4571 "LUNs\n", (uintmax_t)target_id.id);
4572 if (lun->flags & CTL_LUN_MALLOCED)
4574 be_lun->lun_config_status(be_lun->be_lun,
4575 CTL_LUN_CONFIG_FAILURE);
4579 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4581 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4582 lun->target = target_id;
4583 lun->lun = lun_number;
4584 lun->be_lun = be_lun;
4586 * The processor LUN is always enabled. Disk LUNs come on line
4587 * disabled, and must be enabled by the backend.
4589 lun->flags |= CTL_LUN_DISABLED;
4590 lun->backend = be_lun->be;
4591 be_lun->ctl_lun = lun;
4592 be_lun->lun_id = lun_number;
4593 atomic_add_int(&be_lun->be->num_luns, 1);
4594 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4595 lun->flags |= CTL_LUN_STOPPED;
4597 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4598 lun->flags |= CTL_LUN_INOPERABLE;
4600 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4601 lun->flags |= CTL_LUN_PRIMARY_SC;
4603 lun->ctl_softc = ctl_softc;
4604 TAILQ_INIT(&lun->ooa_queue);
4605 TAILQ_INIT(&lun->blocked_queue);
4606 STAILQ_INIT(&lun->error_list);
4607 ctl_tpc_lun_init(lun);
4610 * Initialize the mode page index.
4612 ctl_init_page_index(lun);
4615 * Set the poweron UA for all initiators on this LUN only.
4617 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4618 lun->pending_ua[i] = CTL_UA_POWERON;
4621 * Now, before we insert this lun on the lun list, set the lun
4622 * inventory changed UA for all other luns.
4624 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4625 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4626 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4630 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4632 ctl_softc->ctl_luns[lun_number] = lun;
4634 ctl_softc->num_luns++;
4636 /* Setup statistics gathering */
4637 lun->stats.device_type = be_lun->lun_type;
4638 lun->stats.lun_number = lun_number;
4639 if (lun->stats.device_type == T_DIRECT)
4640 lun->stats.blocksize = be_lun->blocksize;
4642 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4643 for (i = 0;i < CTL_MAX_PORTS;i++)
4644 lun->stats.ports[i].targ_port = i;
4646 mtx_unlock(&ctl_softc->ctl_lock);
4648 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4651 * Run through each registered FETD and bring it online if it isn't
4652 * already. Enable the target ID if it hasn't been enabled, and
4653 * enable this particular LUN.
4655 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4658 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4660 printf("ctl_alloc_lun: FETD %s port %d returned error "
4661 "%d for lun_enable on target %ju lun %d\n",
4662 port->port_name, port->targ_port, retval,
4663 (uintmax_t)target_id.id, lun_number);
4665 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4673 * - LUN has already been marked invalid and any pending I/O has been taken
4677 ctl_free_lun(struct ctl_lun *lun)
4679 struct ctl_softc *softc;
4681 struct ctl_port *port;
4683 struct ctl_lun *nlun;
4686 softc = lun->ctl_softc;
4688 mtx_assert(&softc->ctl_lock, MA_OWNED);
4690 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4692 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4694 softc->ctl_luns[lun->lun] = NULL;
4696 if (!TAILQ_EMPTY(&lun->ooa_queue))
4697 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4702 * XXX KDM this scheme only works for a single target/multiple LUN
4703 * setup. It needs to be revamped for a multiple target scheme.
4705 * XXX KDM this results in port->lun_disable() getting called twice,
4706 * once when ctl_disable_lun() is called, and a second time here.
4707 * We really need to re-think the LUN disable semantics. There
4708 * should probably be several steps/levels to LUN removal:
4713 * Right now we only have a disable method when communicating to
4714 * the front end ports, at least for individual LUNs.
4717 STAILQ_FOREACH(port, &softc->port_list, links) {
4720 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4723 printf("ctl_free_lun: FETD %s port %d returned error "
4724 "%d for lun_disable on target %ju lun %jd\n",
4725 port->port_name, port->targ_port, retval,
4726 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4729 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4730 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4732 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4734 printf("ctl_free_lun: FETD %s port %d "
4735 "returned error %d for targ_disable on "
4736 "target %ju\n", port->port_name,
4737 port->targ_port, retval,
4738 (uintmax_t)lun->target.id);
4740 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4742 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4746 port->port_offline(port->onoff_arg);
4747 port->status &= ~CTL_PORT_STATUS_ONLINE;
4754 * Tell the backend to free resources, if this LUN has a backend.
4756 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4757 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4759 ctl_tpc_lun_shutdown(lun);
4760 mtx_destroy(&lun->lun_lock);
4761 free(lun->lun_devid, M_CTL);
4762 if (lun->flags & CTL_LUN_MALLOCED)
4765 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4766 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4767 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4775 ctl_create_lun(struct ctl_be_lun *be_lun)
4777 struct ctl_softc *ctl_softc;
4779 ctl_softc = control_softc;
4782 * ctl_alloc_lun() should handle all potential failure cases.
4784 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4788 ctl_add_lun(struct ctl_be_lun *be_lun)
4790 struct ctl_softc *ctl_softc = control_softc;
4792 mtx_lock(&ctl_softc->ctl_lock);
4793 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4794 mtx_unlock(&ctl_softc->ctl_lock);
4795 wakeup(&ctl_softc->pending_lun_queue);
4801 ctl_enable_lun(struct ctl_be_lun *be_lun)
4803 struct ctl_softc *ctl_softc;
4804 struct ctl_port *port, *nport;
4805 struct ctl_lun *lun;
4808 ctl_softc = control_softc;
4810 lun = (struct ctl_lun *)be_lun->ctl_lun;
4812 mtx_lock(&ctl_softc->ctl_lock);
4813 mtx_lock(&lun->lun_lock);
4814 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4816 * eh? Why did we get called if the LUN is already
4819 mtx_unlock(&lun->lun_lock);
4820 mtx_unlock(&ctl_softc->ctl_lock);
4823 lun->flags &= ~CTL_LUN_DISABLED;
4824 mtx_unlock(&lun->lun_lock);
4826 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4827 nport = STAILQ_NEXT(port, links);
4830 * Drop the lock while we call the FETD's enable routine.
4831 * This can lead to a callback into CTL (at least in the
4832 * case of the internal initiator frontend.
4834 mtx_unlock(&ctl_softc->ctl_lock);
4835 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4836 mtx_lock(&ctl_softc->ctl_lock);
4838 printf("%s: FETD %s port %d returned error "
4839 "%d for lun_enable on target %ju lun %jd\n",
4840 __func__, port->port_name, port->targ_port, retval,
4841 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4845 /* NOTE: TODO: why does lun enable affect port status? */
4846 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4851 mtx_unlock(&ctl_softc->ctl_lock);
4857 ctl_disable_lun(struct ctl_be_lun *be_lun)
4859 struct ctl_softc *ctl_softc;
4860 struct ctl_port *port;
4861 struct ctl_lun *lun;
4864 ctl_softc = control_softc;
4866 lun = (struct ctl_lun *)be_lun->ctl_lun;
4868 mtx_lock(&ctl_softc->ctl_lock);
4869 mtx_lock(&lun->lun_lock);
4870 if (lun->flags & CTL_LUN_DISABLED) {
4871 mtx_unlock(&lun->lun_lock);
4872 mtx_unlock(&ctl_softc->ctl_lock);
4875 lun->flags |= CTL_LUN_DISABLED;
4876 mtx_unlock(&lun->lun_lock);
4878 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4879 mtx_unlock(&ctl_softc->ctl_lock);
4881 * Drop the lock before we call the frontend's disable
4882 * routine, to avoid lock order reversals.
4884 * XXX KDM what happens if the frontend list changes while
4885 * we're traversing it? It's unlikely, but should be handled.
4887 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4889 mtx_lock(&ctl_softc->ctl_lock);
4891 printf("ctl_alloc_lun: FETD %s port %d returned error "
4892 "%d for lun_disable on target %ju lun %jd\n",
4893 port->port_name, port->targ_port, retval,
4894 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4898 mtx_unlock(&ctl_softc->ctl_lock);
4904 ctl_start_lun(struct ctl_be_lun *be_lun)
4906 struct ctl_softc *ctl_softc;
4907 struct ctl_lun *lun;
4909 ctl_softc = control_softc;
4911 lun = (struct ctl_lun *)be_lun->ctl_lun;
4913 mtx_lock(&lun->lun_lock);
4914 lun->flags &= ~CTL_LUN_STOPPED;
4915 mtx_unlock(&lun->lun_lock);
4921 ctl_stop_lun(struct ctl_be_lun *be_lun)
4923 struct ctl_softc *ctl_softc;
4924 struct ctl_lun *lun;
4926 ctl_softc = control_softc;
4928 lun = (struct ctl_lun *)be_lun->ctl_lun;
4930 mtx_lock(&lun->lun_lock);
4931 lun->flags |= CTL_LUN_STOPPED;
4932 mtx_unlock(&lun->lun_lock);
4938 ctl_lun_offline(struct ctl_be_lun *be_lun)
4940 struct ctl_softc *ctl_softc;
4941 struct ctl_lun *lun;
4943 ctl_softc = control_softc;
4945 lun = (struct ctl_lun *)be_lun->ctl_lun;
4947 mtx_lock(&lun->lun_lock);
4948 lun->flags |= CTL_LUN_OFFLINE;
4949 mtx_unlock(&lun->lun_lock);
4955 ctl_lun_online(struct ctl_be_lun *be_lun)
4957 struct ctl_softc *ctl_softc;
4958 struct ctl_lun *lun;
4960 ctl_softc = control_softc;
4962 lun = (struct ctl_lun *)be_lun->ctl_lun;
4964 mtx_lock(&lun->lun_lock);
4965 lun->flags &= ~CTL_LUN_OFFLINE;
4966 mtx_unlock(&lun->lun_lock);
4972 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4974 struct ctl_softc *ctl_softc;
4975 struct ctl_lun *lun;
4977 ctl_softc = control_softc;
4979 lun = (struct ctl_lun *)be_lun->ctl_lun;
4981 mtx_lock(&lun->lun_lock);
4984 * The LUN needs to be disabled before it can be marked invalid.
4986 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4987 mtx_unlock(&lun->lun_lock);
4991 * Mark the LUN invalid.
4993 lun->flags |= CTL_LUN_INVALID;
4996 * If there is nothing in the OOA queue, go ahead and free the LUN.
4997 * If we have something in the OOA queue, we'll free it when the
4998 * last I/O completes.
5000 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5001 mtx_unlock(&lun->lun_lock);
5002 mtx_lock(&ctl_softc->ctl_lock);
5004 mtx_unlock(&ctl_softc->ctl_lock);
5006 mtx_unlock(&lun->lun_lock);
5012 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
5014 struct ctl_softc *ctl_softc;
5015 struct ctl_lun *lun;
5017 ctl_softc = control_softc;
5018 lun = (struct ctl_lun *)be_lun->ctl_lun;
5020 mtx_lock(&lun->lun_lock);
5021 lun->flags |= CTL_LUN_INOPERABLE;
5022 mtx_unlock(&lun->lun_lock);
5028 ctl_lun_operable(struct ctl_be_lun *be_lun)
5030 struct ctl_softc *ctl_softc;
5031 struct ctl_lun *lun;
5033 ctl_softc = control_softc;
5034 lun = (struct ctl_lun *)be_lun->ctl_lun;
5036 mtx_lock(&lun->lun_lock);
5037 lun->flags &= ~CTL_LUN_INOPERABLE;
5038 mtx_unlock(&lun->lun_lock);
5044 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
5047 struct ctl_softc *softc;
5048 struct ctl_lun *lun;
5049 struct copan_aps_subpage *current_sp;
5050 struct ctl_page_index *page_index;
5053 softc = control_softc;
5055 mtx_lock(&softc->ctl_lock);
5057 lun = (struct ctl_lun *)be_lun->ctl_lun;
5058 mtx_lock(&lun->lun_lock);
5061 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5062 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
5066 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
5068 page_index = &lun->mode_pages.index[i];
5071 if (page_index == NULL) {
5072 mtx_unlock(&lun->lun_lock);
5073 mtx_unlock(&softc->ctl_lock);
5074 printf("%s: APS subpage not found for lun %ju!\n", __func__,
5075 (uintmax_t)lun->lun);
5079 if ((softc->aps_locked_lun != 0)
5080 && (softc->aps_locked_lun != lun->lun)) {
5081 printf("%s: attempt to lock LUN %llu when %llu is already "
5083 mtx_unlock(&lun->lun_lock);
5084 mtx_unlock(&softc->ctl_lock);
5089 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5090 (page_index->page_len * CTL_PAGE_CURRENT));
5093 current_sp->lock_active = APS_LOCK_ACTIVE;
5094 softc->aps_locked_lun = lun->lun;
5096 current_sp->lock_active = 0;
5097 softc->aps_locked_lun = 0;
5102 * If we're in HA mode, try to send the lock message to the other
5105 if (ctl_is_single == 0) {
5107 union ctl_ha_msg lock_msg;
5109 lock_msg.hdr.nexus = *nexus;
5110 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
5112 lock_msg.aps.lock_flag = 1;
5114 lock_msg.aps.lock_flag = 0;
5115 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
5116 sizeof(lock_msg), 0);
5117 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
5118 printf("%s: APS (lock=%d) error returned from "
5119 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
5120 mtx_unlock(&lun->lun_lock);
5121 mtx_unlock(&softc->ctl_lock);
5126 mtx_unlock(&lun->lun_lock);
5127 mtx_unlock(&softc->ctl_lock);
5133 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5135 struct ctl_lun *lun;
5136 struct ctl_softc *softc;
5139 softc = control_softc;
5141 lun = (struct ctl_lun *)be_lun->ctl_lun;
5143 mtx_lock(&lun->lun_lock);
5145 for (i = 0; i < CTL_MAX_INITIATORS; i++)
5146 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED;
5148 mtx_unlock(&lun->lun_lock);
5152 * Backend "memory move is complete" callback for requests that never
5153 * make it down to say RAIDCore's configuration code.
5156 ctl_config_move_done(union ctl_io *io)
5160 retval = CTL_RETVAL_COMPLETE;
5163 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5165 * XXX KDM this shouldn't happen, but what if it does?
5167 if (io->io_hdr.io_type != CTL_IO_SCSI)
5168 panic("I/O type isn't CTL_IO_SCSI!");
5170 if ((io->io_hdr.port_status == 0)
5171 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5172 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
5173 io->io_hdr.status = CTL_SUCCESS;
5174 else if ((io->io_hdr.port_status != 0)
5175 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5176 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
5178 * For hardware error sense keys, the sense key
5179 * specific value is defined to be a retry count,
5180 * but we use it to pass back an internal FETD
5181 * error code. XXX KDM Hopefully the FETD is only
5182 * using 16 bits for an error code, since that's
5183 * all the space we have in the sks field.
5185 ctl_set_internal_failure(&io->scsiio,
5188 io->io_hdr.port_status);
5189 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5190 free(io->scsiio.kern_data_ptr, M_CTL);
5195 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
5196 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
5197 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5199 * XXX KDM just assuming a single pointer here, and not a
5200 * S/G list. If we start using S/G lists for config data,
5201 * we'll need to know how to clean them up here as well.
5203 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5204 free(io->scsiio.kern_data_ptr, M_CTL);
5205 /* Hopefully the user has already set the status... */
5209 * XXX KDM now we need to continue data movement. Some
5211 * - call ctl_scsiio() again? We don't do this for data
5212 * writes, because for those at least we know ahead of
5213 * time where the write will go and how long it is. For
5214 * config writes, though, that information is largely
5215 * contained within the write itself, thus we need to
5216 * parse out the data again.
5218 * - Call some other function once the data is in?
5222 * XXX KDM call ctl_scsiio() again for now, and check flag
5223 * bits to see whether we're allocated or not.
5225 retval = ctl_scsiio(&io->scsiio);
5232 * This gets called by a backend driver when it is done with a
5233 * data_submit method.
5236 ctl_data_submit_done(union ctl_io *io)
5239 * If the IO_CONT flag is set, we need to call the supplied
5240 * function to continue processing the I/O, instead of completing
5243 * If there is an error, though, we don't want to keep processing.
5244 * Instead, just send status back to the initiator.
5246 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5247 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5248 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5249 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5250 io->scsiio.io_cont(io);
5257 * This gets called by a backend driver when it is done with a
5258 * configuration write.
5261 ctl_config_write_done(union ctl_io *io)
5264 * If the IO_CONT flag is set, we need to call the supplied
5265 * function to continue processing the I/O, instead of completing
5268 * If there is an error, though, we don't want to keep processing.
5269 * Instead, just send status back to the initiator.
5271 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5272 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5273 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5274 io->scsiio.io_cont(io);
5278 * Since a configuration write can be done for commands that actually
5279 * have data allocated, like write buffer, and commands that have
5280 * no data, like start/stop unit, we need to check here.
5282 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
5283 free(io->scsiio.kern_data_ptr, M_CTL);
5288 * SCSI release command.
5291 ctl_scsi_release(struct ctl_scsiio *ctsio)
5293 int length, longid, thirdparty_id, resv_id;
5294 struct ctl_softc *ctl_softc;
5295 struct ctl_lun *lun;
5300 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5302 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5303 ctl_softc = control_softc;
5305 switch (ctsio->cdb[0]) {
5307 struct scsi_release_10 *cdb;
5309 cdb = (struct scsi_release_10 *)ctsio->cdb;
5311 if (cdb->byte2 & SR10_LONGID)
5314 thirdparty_id = cdb->thirdparty_id;
5316 resv_id = cdb->resv_id;
5317 length = scsi_2btoul(cdb->length);
5324 * XXX KDM right now, we only support LUN reservation. We don't
5325 * support 3rd party reservations, or extent reservations, which
5326 * might actually need the parameter list. If we've gotten this
5327 * far, we've got a LUN reservation. Anything else got kicked out
5328 * above. So, according to SPC, ignore the length.
5332 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5334 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5335 ctsio->kern_data_len = length;
5336 ctsio->kern_total_len = length;
5337 ctsio->kern_data_resid = 0;
5338 ctsio->kern_rel_offset = 0;
5339 ctsio->kern_sg_entries = 0;
5340 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5341 ctsio->be_move_done = ctl_config_move_done;
5342 ctl_datamove((union ctl_io *)ctsio);
5344 return (CTL_RETVAL_COMPLETE);
5348 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5350 mtx_lock(&lun->lun_lock);
5353 * According to SPC, it is not an error for an intiator to attempt
5354 * to release a reservation on a LUN that isn't reserved, or that
5355 * is reserved by another initiator. The reservation can only be
5356 * released, though, by the initiator who made it or by one of
5357 * several reset type events.
5359 if (lun->flags & CTL_LUN_RESERVED) {
5360 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5361 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5362 && (ctsio->io_hdr.nexus.targ_target.id ==
5363 lun->rsv_nexus.targ_target.id)) {
5364 lun->flags &= ~CTL_LUN_RESERVED;
5368 mtx_unlock(&lun->lun_lock);
5370 ctsio->scsi_status = SCSI_STATUS_OK;
5371 ctsio->io_hdr.status = CTL_SUCCESS;
5373 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5374 free(ctsio->kern_data_ptr, M_CTL);
5375 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5378 ctl_done((union ctl_io *)ctsio);
5379 return (CTL_RETVAL_COMPLETE);
5383 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5385 int extent, thirdparty, longid;
5386 int resv_id, length;
5387 uint64_t thirdparty_id;
5388 struct ctl_softc *ctl_softc;
5389 struct ctl_lun *lun;
5398 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5400 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5401 ctl_softc = control_softc;
5403 switch (ctsio->cdb[0]) {
5405 struct scsi_reserve_10 *cdb;
5407 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5409 if (cdb->byte2 & SR10_LONGID)
5412 thirdparty_id = cdb->thirdparty_id;
5414 resv_id = cdb->resv_id;
5415 length = scsi_2btoul(cdb->length);
5421 * XXX KDM right now, we only support LUN reservation. We don't
5422 * support 3rd party reservations, or extent reservations, which
5423 * might actually need the parameter list. If we've gotten this
5424 * far, we've got a LUN reservation. Anything else got kicked out
5425 * above. So, according to SPC, ignore the length.
5429 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5431 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5432 ctsio->kern_data_len = length;
5433 ctsio->kern_total_len = length;
5434 ctsio->kern_data_resid = 0;
5435 ctsio->kern_rel_offset = 0;
5436 ctsio->kern_sg_entries = 0;
5437 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5438 ctsio->be_move_done = ctl_config_move_done;
5439 ctl_datamove((union ctl_io *)ctsio);
5441 return (CTL_RETVAL_COMPLETE);
5445 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5447 mtx_lock(&lun->lun_lock);
5448 if (lun->flags & CTL_LUN_RESERVED) {
5449 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5450 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5451 || (ctsio->io_hdr.nexus.targ_target.id !=
5452 lun->rsv_nexus.targ_target.id)) {
5453 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5454 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5459 lun->flags |= CTL_LUN_RESERVED;
5460 lun->rsv_nexus = ctsio->io_hdr.nexus;
5462 ctsio->scsi_status = SCSI_STATUS_OK;
5463 ctsio->io_hdr.status = CTL_SUCCESS;
5466 mtx_unlock(&lun->lun_lock);
5468 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5469 free(ctsio->kern_data_ptr, M_CTL);
5470 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5473 ctl_done((union ctl_io *)ctsio);
5474 return (CTL_RETVAL_COMPLETE);
5478 ctl_start_stop(struct ctl_scsiio *ctsio)
5480 struct scsi_start_stop_unit *cdb;
5481 struct ctl_lun *lun;
5482 struct ctl_softc *ctl_softc;
5485 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5487 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5488 ctl_softc = control_softc;
5491 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5495 * We don't support the immediate bit on a stop unit. In order to
5496 * do that, we would need to code up a way to know that a stop is
5497 * pending, and hold off any new commands until it completes, one
5498 * way or another. Then we could accept or reject those commands
5499 * depending on its status. We would almost need to do the reverse
5500 * of what we do below for an immediate start -- return the copy of
5501 * the ctl_io to the FETD with status to send to the host (and to
5502 * free the copy!) and then free the original I/O once the stop
5503 * actually completes. That way, the OOA queue mechanism can work
5504 * to block commands that shouldn't proceed. Another alternative
5505 * would be to put the copy in the queue in place of the original,
5506 * and return the original back to the caller. That could be
5509 if ((cdb->byte2 & SSS_IMMED)
5510 && ((cdb->how & SSS_START) == 0)) {
5511 ctl_set_invalid_field(ctsio,
5517 ctl_done((union ctl_io *)ctsio);
5518 return (CTL_RETVAL_COMPLETE);
5521 if ((lun->flags & CTL_LUN_PR_RESERVED)
5522 && ((cdb->how & SSS_START)==0)) {
5525 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5526 if (!lun->per_res[residx].registered
5527 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5529 ctl_set_reservation_conflict(ctsio);
5530 ctl_done((union ctl_io *)ctsio);
5531 return (CTL_RETVAL_COMPLETE);
5536 * If there is no backend on this device, we can't start or stop
5537 * it. In theory we shouldn't get any start/stop commands in the
5538 * first place at this level if the LUN doesn't have a backend.
5539 * That should get stopped by the command decode code.
5541 if (lun->backend == NULL) {
5542 ctl_set_invalid_opcode(ctsio);
5543 ctl_done((union ctl_io *)ctsio);
5544 return (CTL_RETVAL_COMPLETE);
5548 * XXX KDM Copan-specific offline behavior.
5549 * Figure out a reasonable way to port this?
5552 mtx_lock(&lun->lun_lock);
5554 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5555 && (lun->flags & CTL_LUN_OFFLINE)) {
5557 * If the LUN is offline, and the on/offline bit isn't set,
5558 * reject the start or stop. Otherwise, let it through.
5560 mtx_unlock(&lun->lun_lock);
5561 ctl_set_lun_not_ready(ctsio);
5562 ctl_done((union ctl_io *)ctsio);
5564 mtx_unlock(&lun->lun_lock);
5565 #endif /* NEEDTOPORT */
5567 * This could be a start or a stop when we're online,
5568 * or a stop/offline or start/online. A start or stop when
5569 * we're offline is covered in the case above.
5572 * In the non-immediate case, we send the request to
5573 * the backend and return status to the user when
5576 * In the immediate case, we allocate a new ctl_io
5577 * to hold a copy of the request, and send that to
5578 * the backend. We then set good status on the
5579 * user's request and return it immediately.
5581 if (cdb->byte2 & SSS_IMMED) {
5582 union ctl_io *new_io;
5584 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5585 if (new_io == NULL) {
5586 ctl_set_busy(ctsio);
5587 ctl_done((union ctl_io *)ctsio);
5589 ctl_copy_io((union ctl_io *)ctsio,
5591 retval = lun->backend->config_write(new_io);
5592 ctl_set_success(ctsio);
5593 ctl_done((union ctl_io *)ctsio);
5596 retval = lun->backend->config_write(
5597 (union ctl_io *)ctsio);
5606 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5607 * we don't really do anything with the LBA and length fields if the user
5608 * passes them in. Instead we'll just flush out the cache for the entire
5612 ctl_sync_cache(struct ctl_scsiio *ctsio)
5614 struct ctl_lun *lun;
5615 struct ctl_softc *ctl_softc;
5616 uint64_t starting_lba;
5617 uint32_t block_count;
5620 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5622 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5623 ctl_softc = control_softc;
5626 switch (ctsio->cdb[0]) {
5627 case SYNCHRONIZE_CACHE: {
5628 struct scsi_sync_cache *cdb;
5629 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5631 starting_lba = scsi_4btoul(cdb->begin_lba);
5632 block_count = scsi_2btoul(cdb->lb_count);
5635 case SYNCHRONIZE_CACHE_16: {
5636 struct scsi_sync_cache_16 *cdb;
5637 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5639 starting_lba = scsi_8btou64(cdb->begin_lba);
5640 block_count = scsi_4btoul(cdb->lb_count);
5644 ctl_set_invalid_opcode(ctsio);
5645 ctl_done((union ctl_io *)ctsio);
5647 break; /* NOTREACHED */
5651 * We check the LBA and length, but don't do anything with them.
5652 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5653 * get flushed. This check will just help satisfy anyone who wants
5654 * to see an error for an out of range LBA.
5656 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5657 ctl_set_lba_out_of_range(ctsio);
5658 ctl_done((union ctl_io *)ctsio);
5663 * If this LUN has no backend, we can't flush the cache anyway.
5665 if (lun->backend == NULL) {
5666 ctl_set_invalid_opcode(ctsio);
5667 ctl_done((union ctl_io *)ctsio);
5672 * Check to see whether we're configured to send the SYNCHRONIZE
5673 * CACHE command directly to the back end.
5675 mtx_lock(&lun->lun_lock);
5676 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5677 && (++(lun->sync_count) >= lun->sync_interval)) {
5678 lun->sync_count = 0;
5679 mtx_unlock(&lun->lun_lock);
5680 retval = lun->backend->config_write((union ctl_io *)ctsio);
5682 mtx_unlock(&lun->lun_lock);
5683 ctl_set_success(ctsio);
5684 ctl_done((union ctl_io *)ctsio);
5693 ctl_format(struct ctl_scsiio *ctsio)
5695 struct scsi_format *cdb;
5696 struct ctl_lun *lun;
5697 struct ctl_softc *ctl_softc;
5698 int length, defect_list_len;
5700 CTL_DEBUG_PRINT(("ctl_format\n"));
5702 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5703 ctl_softc = control_softc;
5705 cdb = (struct scsi_format *)ctsio->cdb;
5708 if (cdb->byte2 & SF_FMTDATA) {
5709 if (cdb->byte2 & SF_LONGLIST)
5710 length = sizeof(struct scsi_format_header_long);
5712 length = sizeof(struct scsi_format_header_short);
5715 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5717 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5718 ctsio->kern_data_len = length;
5719 ctsio->kern_total_len = length;
5720 ctsio->kern_data_resid = 0;
5721 ctsio->kern_rel_offset = 0;
5722 ctsio->kern_sg_entries = 0;
5723 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5724 ctsio->be_move_done = ctl_config_move_done;
5725 ctl_datamove((union ctl_io *)ctsio);
5727 return (CTL_RETVAL_COMPLETE);
5730 defect_list_len = 0;
5732 if (cdb->byte2 & SF_FMTDATA) {
5733 if (cdb->byte2 & SF_LONGLIST) {
5734 struct scsi_format_header_long *header;
5736 header = (struct scsi_format_header_long *)
5737 ctsio->kern_data_ptr;
5739 defect_list_len = scsi_4btoul(header->defect_list_len);
5740 if (defect_list_len != 0) {
5741 ctl_set_invalid_field(ctsio,
5750 struct scsi_format_header_short *header;
5752 header = (struct scsi_format_header_short *)
5753 ctsio->kern_data_ptr;
5755 defect_list_len = scsi_2btoul(header->defect_list_len);
5756 if (defect_list_len != 0) {
5757 ctl_set_invalid_field(ctsio,
5769 * The format command will clear out the "Medium format corrupted"
5770 * status if set by the configuration code. That status is really
5771 * just a way to notify the host that we have lost the media, and
5772 * get them to issue a command that will basically make them think
5773 * they're blowing away the media.
5775 mtx_lock(&lun->lun_lock);
5776 lun->flags &= ~CTL_LUN_INOPERABLE;
5777 mtx_unlock(&lun->lun_lock);
5779 ctsio->scsi_status = SCSI_STATUS_OK;
5780 ctsio->io_hdr.status = CTL_SUCCESS;
5783 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5784 free(ctsio->kern_data_ptr, M_CTL);
5785 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5788 ctl_done((union ctl_io *)ctsio);
5789 return (CTL_RETVAL_COMPLETE);
5793 ctl_read_buffer(struct ctl_scsiio *ctsio)
5795 struct scsi_read_buffer *cdb;
5796 struct ctl_lun *lun;
5797 int buffer_offset, len;
5798 static uint8_t descr[4];
5799 static uint8_t echo_descr[4] = { 0 };
5801 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5803 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5804 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5806 if (lun->flags & CTL_LUN_PR_RESERVED) {
5810 * XXX KDM need a lock here.
5812 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5813 if ((lun->res_type == SPR_TYPE_EX_AC
5814 && residx != lun->pr_res_idx)
5815 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5816 || lun->res_type == SPR_TYPE_EX_AC_AR)
5817 && !lun->per_res[residx].registered)) {
5818 ctl_set_reservation_conflict(ctsio);
5819 ctl_done((union ctl_io *)ctsio);
5820 return (CTL_RETVAL_COMPLETE);
5824 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5825 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5826 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5827 ctl_set_invalid_field(ctsio,
5833 ctl_done((union ctl_io *)ctsio);
5834 return (CTL_RETVAL_COMPLETE);
5837 len = scsi_3btoul(cdb->length);
5838 buffer_offset = scsi_3btoul(cdb->offset);
5840 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5841 ctl_set_invalid_field(ctsio,
5847 ctl_done((union ctl_io *)ctsio);
5848 return (CTL_RETVAL_COMPLETE);
5851 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5853 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5854 ctsio->kern_data_ptr = descr;
5855 len = min(len, sizeof(descr));
5856 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5857 ctsio->kern_data_ptr = echo_descr;
5858 len = min(len, sizeof(echo_descr));
5860 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5861 ctsio->kern_data_len = len;
5862 ctsio->kern_total_len = len;
5863 ctsio->kern_data_resid = 0;
5864 ctsio->kern_rel_offset = 0;
5865 ctsio->kern_sg_entries = 0;
5866 ctsio->be_move_done = ctl_config_move_done;
5867 ctl_datamove((union ctl_io *)ctsio);
5869 return (CTL_RETVAL_COMPLETE);
5873 ctl_write_buffer(struct ctl_scsiio *ctsio)
5875 struct scsi_write_buffer *cdb;
5876 struct ctl_lun *lun;
5877 int buffer_offset, len;
5879 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5881 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5882 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5884 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5885 ctl_set_invalid_field(ctsio,
5891 ctl_done((union ctl_io *)ctsio);
5892 return (CTL_RETVAL_COMPLETE);
5895 len = scsi_3btoul(cdb->length);
5896 buffer_offset = scsi_3btoul(cdb->offset);
5898 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5899 ctl_set_invalid_field(ctsio,
5905 ctl_done((union ctl_io *)ctsio);
5906 return (CTL_RETVAL_COMPLETE);
5910 * If we've got a kernel request that hasn't been malloced yet,
5911 * malloc it and tell the caller the data buffer is here.
5913 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5914 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5915 ctsio->kern_data_len = len;
5916 ctsio->kern_total_len = len;
5917 ctsio->kern_data_resid = 0;
5918 ctsio->kern_rel_offset = 0;
5919 ctsio->kern_sg_entries = 0;
5920 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5921 ctsio->be_move_done = ctl_config_move_done;
5922 ctl_datamove((union ctl_io *)ctsio);
5924 return (CTL_RETVAL_COMPLETE);
5927 ctl_done((union ctl_io *)ctsio);
5929 return (CTL_RETVAL_COMPLETE);
5933 ctl_write_same(struct ctl_scsiio *ctsio)
5935 struct ctl_lun *lun;
5936 struct ctl_lba_len_flags *lbalen;
5938 uint32_t num_blocks;
5942 retval = CTL_RETVAL_COMPLETE;
5944 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5946 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5948 switch (ctsio->cdb[0]) {
5949 case WRITE_SAME_10: {
5950 struct scsi_write_same_10 *cdb;
5952 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5954 lba = scsi_4btoul(cdb->addr);
5955 num_blocks = scsi_2btoul(cdb->length);
5959 case WRITE_SAME_16: {
5960 struct scsi_write_same_16 *cdb;
5962 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5964 lba = scsi_8btou64(cdb->addr);
5965 num_blocks = scsi_4btoul(cdb->length);
5971 * We got a command we don't support. This shouldn't
5972 * happen, commands should be filtered out above us.
5974 ctl_set_invalid_opcode(ctsio);
5975 ctl_done((union ctl_io *)ctsio);
5977 return (CTL_RETVAL_COMPLETE);
5978 break; /* NOTREACHED */
5982 * The first check is to make sure we're in bounds, the second
5983 * check is to catch wrap-around problems. If the lba + num blocks
5984 * is less than the lba, then we've wrapped around and the block
5985 * range is invalid anyway.
5987 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5988 || ((lba + num_blocks) < lba)) {
5989 ctl_set_lba_out_of_range(ctsio);
5990 ctl_done((union ctl_io *)ctsio);
5991 return (CTL_RETVAL_COMPLETE);
5994 /* Zero number of blocks means "to the last logical block" */
5995 if (num_blocks == 0) {
5996 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5997 ctl_set_invalid_field(ctsio,
6003 ctl_done((union ctl_io *)ctsio);
6004 return (CTL_RETVAL_COMPLETE);
6006 num_blocks = (lun->be_lun->maxlba + 1) - lba;
6009 len = lun->be_lun->blocksize;
6012 * If we've got a kernel request that hasn't been malloced yet,
6013 * malloc it and tell the caller the data buffer is here.
6015 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6016 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6017 ctsio->kern_data_len = len;
6018 ctsio->kern_total_len = len;
6019 ctsio->kern_data_resid = 0;
6020 ctsio->kern_rel_offset = 0;
6021 ctsio->kern_sg_entries = 0;
6022 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6023 ctsio->be_move_done = ctl_config_move_done;
6024 ctl_datamove((union ctl_io *)ctsio);
6026 return (CTL_RETVAL_COMPLETE);
6029 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6031 lbalen->len = num_blocks;
6032 lbalen->flags = byte2;
6033 retval = lun->backend->config_write((union ctl_io *)ctsio);
6039 ctl_unmap(struct ctl_scsiio *ctsio)
6041 struct ctl_lun *lun;
6042 struct scsi_unmap *cdb;
6043 struct ctl_ptr_len_flags *ptrlen;
6044 struct scsi_unmap_header *hdr;
6045 struct scsi_unmap_desc *buf, *end, *endnz, *range;
6047 uint32_t num_blocks;
6051 retval = CTL_RETVAL_COMPLETE;
6053 CTL_DEBUG_PRINT(("ctl_unmap\n"));
6055 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6056 cdb = (struct scsi_unmap *)ctsio->cdb;
6058 len = scsi_2btoul(cdb->length);
6062 * If we've got a kernel request that hasn't been malloced yet,
6063 * malloc it and tell the caller the data buffer is here.
6065 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6066 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6067 ctsio->kern_data_len = len;
6068 ctsio->kern_total_len = len;
6069 ctsio->kern_data_resid = 0;
6070 ctsio->kern_rel_offset = 0;
6071 ctsio->kern_sg_entries = 0;
6072 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6073 ctsio->be_move_done = ctl_config_move_done;
6074 ctl_datamove((union ctl_io *)ctsio);
6076 return (CTL_RETVAL_COMPLETE);
6079 len = ctsio->kern_total_len - ctsio->kern_data_resid;
6080 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
6081 if (len < sizeof (*hdr) ||
6082 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
6083 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
6084 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
6085 ctl_set_invalid_field(ctsio,
6091 ctl_done((union ctl_io *)ctsio);
6092 return (CTL_RETVAL_COMPLETE);
6094 len = scsi_2btoul(hdr->desc_length);
6095 buf = (struct scsi_unmap_desc *)(hdr + 1);
6096 end = buf + len / sizeof(*buf);
6099 for (range = buf; range < end; range++) {
6100 lba = scsi_8btou64(range->lba);
6101 num_blocks = scsi_4btoul(range->length);
6102 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6103 || ((lba + num_blocks) < lba)) {
6104 ctl_set_lba_out_of_range(ctsio);
6105 ctl_done((union ctl_io *)ctsio);
6106 return (CTL_RETVAL_COMPLETE);
6108 if (num_blocks != 0)
6113 * Block backend can not handle zero last range.
6114 * Filter it out and return if there is nothing left.
6116 len = (uint8_t *)endnz - (uint8_t *)buf;
6118 ctl_set_success(ctsio);
6119 ctl_done((union ctl_io *)ctsio);
6120 return (CTL_RETVAL_COMPLETE);
6123 ptrlen = (struct ctl_ptr_len_flags *)
6124 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6125 ptrlen->ptr = (void *)buf;
6127 ptrlen->flags = byte2;
6129 retval = lun->backend->config_write((union ctl_io *)ctsio);
6134 * Note that this function currently doesn't actually do anything inside
6135 * CTL to enforce things if the DQue bit is turned on.
6137 * Also note that this function can't be used in the default case, because
6138 * the DQue bit isn't set in the changeable mask for the control mode page
6139 * anyway. This is just here as an example for how to implement a page
6140 * handler, and a placeholder in case we want to allow the user to turn
6141 * tagged queueing on and off.
6143 * The D_SENSE bit handling is functional, however, and will turn
6144 * descriptor sense on and off for a given LUN.
6147 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6148 struct ctl_page_index *page_index, uint8_t *page_ptr)
6150 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6151 struct ctl_lun *lun;
6152 struct ctl_softc *softc;
6156 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6157 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6160 user_cp = (struct scsi_control_page *)page_ptr;
6161 current_cp = (struct scsi_control_page *)
6162 (page_index->page_data + (page_index->page_len *
6164 saved_cp = (struct scsi_control_page *)
6165 (page_index->page_data + (page_index->page_len *
6168 softc = control_softc;
6170 mtx_lock(&lun->lun_lock);
6171 if (((current_cp->rlec & SCP_DSENSE) == 0)
6172 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6174 * Descriptor sense is currently turned off and the user
6175 * wants to turn it on.
6177 current_cp->rlec |= SCP_DSENSE;
6178 saved_cp->rlec |= SCP_DSENSE;
6179 lun->flags |= CTL_LUN_SENSE_DESC;
6181 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6182 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6184 * Descriptor sense is currently turned on, and the user
6185 * wants to turn it off.
6187 current_cp->rlec &= ~SCP_DSENSE;
6188 saved_cp->rlec &= ~SCP_DSENSE;
6189 lun->flags &= ~CTL_LUN_SENSE_DESC;
6192 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
6193 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6195 csevent_log(CSC_CTL | CSC_SHELF_SW |
6197 csevent_LogType_Trace,
6198 csevent_Severity_Information,
6199 csevent_AlertLevel_Green,
6200 csevent_FRU_Firmware,
6201 csevent_FRU_Unknown,
6202 "Received untagged to untagged transition");
6203 #endif /* NEEDTOPORT */
6206 csevent_log(CSC_CTL | CSC_SHELF_SW |
6208 csevent_LogType_ConfigChange,
6209 csevent_Severity_Information,
6210 csevent_AlertLevel_Green,
6211 csevent_FRU_Firmware,
6212 csevent_FRU_Unknown,
6213 "Received untagged to tagged "
6214 "queueing transition");
6215 #endif /* NEEDTOPORT */
6217 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6218 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6222 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6224 csevent_log(CSC_CTL | CSC_SHELF_SW |
6226 csevent_LogType_ConfigChange,
6227 csevent_Severity_Warning,
6228 csevent_AlertLevel_Yellow,
6229 csevent_FRU_Firmware,
6230 csevent_FRU_Unknown,
6231 "Received tagged queueing to untagged "
6233 #endif /* NEEDTOPORT */
6235 current_cp->queue_flags |= SCP_QUEUE_DQUE;
6236 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
6240 csevent_log(CSC_CTL | CSC_SHELF_SW |
6242 csevent_LogType_Trace,
6243 csevent_Severity_Information,
6244 csevent_AlertLevel_Green,
6245 csevent_FRU_Firmware,
6246 csevent_FRU_Unknown,
6247 "Received tagged queueing to tagged "
6248 "queueing transition");
6249 #endif /* NEEDTOPORT */
6255 * Let other initiators know that the mode
6256 * parameters for this LUN have changed.
6258 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6262 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6265 mtx_unlock(&lun->lun_lock);
6271 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6272 struct ctl_page_index *page_index, uint8_t *page_ptr)
6274 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6275 struct ctl_lun *lun;
6279 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6280 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6283 user_cp = (struct scsi_caching_page *)page_ptr;
6284 current_cp = (struct scsi_caching_page *)
6285 (page_index->page_data + (page_index->page_len *
6287 saved_cp = (struct scsi_caching_page *)
6288 (page_index->page_data + (page_index->page_len *
6291 mtx_lock(&lun->lun_lock);
6292 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6293 (user_cp->flags1 & (SCP_WCE | SCP_RCD)))
6295 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6296 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6297 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6298 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6302 * Let other initiators know that the mode
6303 * parameters for this LUN have changed.
6305 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6309 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6312 mtx_unlock(&lun->lun_lock);
6318 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
6319 struct ctl_page_index *page_index, uint8_t *page_ptr)
6325 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
6326 struct ctl_page_index *page_index, int pc)
6328 struct copan_power_subpage *page;
6330 page = (struct copan_power_subpage *)page_index->page_data +
6331 (page_index->page_len * pc);
6334 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6336 * We don't update the changable bits for this page.
6339 case SMS_PAGE_CTRL_CURRENT >> 6:
6340 case SMS_PAGE_CTRL_DEFAULT >> 6:
6341 case SMS_PAGE_CTRL_SAVED >> 6:
6343 ctl_update_power_subpage(page);
6348 EPRINT(0, "Invalid PC %d!!", pc);
6357 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
6358 struct ctl_page_index *page_index, uint8_t *page_ptr)
6360 struct copan_aps_subpage *user_sp;
6361 struct copan_aps_subpage *current_sp;
6362 union ctl_modepage_info *modepage_info;
6363 struct ctl_softc *softc;
6364 struct ctl_lun *lun;
6367 retval = CTL_RETVAL_COMPLETE;
6368 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
6369 (page_index->page_len * CTL_PAGE_CURRENT));
6370 softc = control_softc;
6371 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6373 user_sp = (struct copan_aps_subpage *)page_ptr;
6375 modepage_info = (union ctl_modepage_info *)
6376 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6378 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
6379 modepage_info->header.subpage = page_index->subpage;
6380 modepage_info->aps.lock_active = user_sp->lock_active;
6382 mtx_lock(&softc->ctl_lock);
6385 * If there is a request to lock the LUN and another LUN is locked
6386 * this is an error. If the requested LUN is already locked ignore
6387 * the request. If no LUN is locked attempt to lock it.
6388 * if there is a request to unlock the LUN and the LUN is currently
6389 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6390 * if another LUN is locked or no LUN is locked.
6392 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6393 if (softc->aps_locked_lun == lun->lun) {
6395 * This LUN is already locked, so we're done.
6397 retval = CTL_RETVAL_COMPLETE;
6398 } else if (softc->aps_locked_lun == 0) {
6400 * No one has the lock, pass the request to the
6403 retval = lun->backend->config_write(
6404 (union ctl_io *)ctsio);
6407 * Someone else has the lock, throw out the request.
6409 ctl_set_already_locked(ctsio);
6410 free(ctsio->kern_data_ptr, M_CTL);
6411 ctl_done((union ctl_io *)ctsio);
6414 * Set the return value so that ctl_do_mode_select()
6415 * won't try to complete the command. We already
6416 * completed it here.
6418 retval = CTL_RETVAL_ERROR;
6420 } else if (softc->aps_locked_lun == lun->lun) {
6422 * This LUN is locked, so pass the unlock request to the
6425 retval = lun->backend->config_write((union ctl_io *)ctsio);
6427 mtx_unlock(&softc->ctl_lock);
6433 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6434 struct ctl_page_index *page_index,
6440 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6445 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6446 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6447 printf("page data:");
6449 printf(" %.2x",page_ptr[i]);
6455 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6456 struct ctl_page_index *page_index,
6459 struct copan_debugconf_subpage *page;
6461 page = (struct copan_debugconf_subpage *)page_index->page_data +
6462 (page_index->page_len * pc);
6465 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6466 case SMS_PAGE_CTRL_DEFAULT >> 6:
6467 case SMS_PAGE_CTRL_SAVED >> 6:
6469 * We don't update the changable or default bits for this page.
6472 case SMS_PAGE_CTRL_CURRENT >> 6:
6473 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6474 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6478 EPRINT(0, "Invalid PC %d!!", pc);
6479 #endif /* NEEDTOPORT */
6487 ctl_do_mode_select(union ctl_io *io)
6489 struct scsi_mode_page_header *page_header;
6490 struct ctl_page_index *page_index;
6491 struct ctl_scsiio *ctsio;
6492 int control_dev, page_len;
6493 int page_len_offset, page_len_size;
6494 union ctl_modepage_info *modepage_info;
6495 struct ctl_lun *lun;
6496 int *len_left, *len_used;
6499 ctsio = &io->scsiio;
6502 retval = CTL_RETVAL_COMPLETE;
6504 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6506 if (lun->be_lun->lun_type != T_DIRECT)
6511 modepage_info = (union ctl_modepage_info *)
6512 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6513 len_left = &modepage_info->header.len_left;
6514 len_used = &modepage_info->header.len_used;
6518 page_header = (struct scsi_mode_page_header *)
6519 (ctsio->kern_data_ptr + *len_used);
6521 if (*len_left == 0) {
6522 free(ctsio->kern_data_ptr, M_CTL);
6523 ctl_set_success(ctsio);
6524 ctl_done((union ctl_io *)ctsio);
6525 return (CTL_RETVAL_COMPLETE);
6526 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6528 free(ctsio->kern_data_ptr, M_CTL);
6529 ctl_set_param_len_error(ctsio);
6530 ctl_done((union ctl_io *)ctsio);
6531 return (CTL_RETVAL_COMPLETE);
6533 } else if ((page_header->page_code & SMPH_SPF)
6534 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6536 free(ctsio->kern_data_ptr, M_CTL);
6537 ctl_set_param_len_error(ctsio);
6538 ctl_done((union ctl_io *)ctsio);
6539 return (CTL_RETVAL_COMPLETE);
6544 * XXX KDM should we do something with the block descriptor?
6546 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6548 if ((control_dev != 0)
6549 && (lun->mode_pages.index[i].page_flags &
6550 CTL_PAGE_FLAG_DISK_ONLY))
6553 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6554 (page_header->page_code & SMPH_PC_MASK))
6558 * If neither page has a subpage code, then we've got a
6561 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6562 && ((page_header->page_code & SMPH_SPF) == 0)) {
6563 page_index = &lun->mode_pages.index[i];
6564 page_len = page_header->page_length;
6569 * If both pages have subpages, then the subpage numbers
6572 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6573 && (page_header->page_code & SMPH_SPF)) {
6574 struct scsi_mode_page_header_sp *sph;
6576 sph = (struct scsi_mode_page_header_sp *)page_header;
6578 if (lun->mode_pages.index[i].subpage ==
6580 page_index = &lun->mode_pages.index[i];
6581 page_len = scsi_2btoul(sph->page_length);
6588 * If we couldn't find the page, or if we don't have a mode select
6589 * handler for it, send back an error to the user.
6591 if ((page_index == NULL)
6592 || (page_index->select_handler == NULL)) {
6593 ctl_set_invalid_field(ctsio,
6596 /*field*/ *len_used,
6599 free(ctsio->kern_data_ptr, M_CTL);
6600 ctl_done((union ctl_io *)ctsio);
6601 return (CTL_RETVAL_COMPLETE);
6604 if (page_index->page_code & SMPH_SPF) {
6605 page_len_offset = 2;
6609 page_len_offset = 1;
6613 * If the length the initiator gives us isn't the one we specify in
6614 * the mode page header, or if they didn't specify enough data in
6615 * the CDB to avoid truncating this page, kick out the request.
6617 if ((page_len != (page_index->page_len - page_len_offset -
6619 || (*len_left < page_index->page_len)) {
6622 ctl_set_invalid_field(ctsio,
6625 /*field*/ *len_used + page_len_offset,
6628 free(ctsio->kern_data_ptr, M_CTL);
6629 ctl_done((union ctl_io *)ctsio);
6630 return (CTL_RETVAL_COMPLETE);
6634 * Run through the mode page, checking to make sure that the bits
6635 * the user changed are actually legal for him to change.
6637 for (i = 0; i < page_index->page_len; i++) {
6638 uint8_t *user_byte, *change_mask, *current_byte;
6642 user_byte = (uint8_t *)page_header + i;
6643 change_mask = page_index->page_data +
6644 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6645 current_byte = page_index->page_data +
6646 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6649 * Check to see whether the user set any bits in this byte
6650 * that he is not allowed to set.
6652 if ((*user_byte & ~(*change_mask)) ==
6653 (*current_byte & ~(*change_mask)))
6657 * Go through bit by bit to determine which one is illegal.
6660 for (j = 7; j >= 0; j--) {
6661 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6662 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6667 ctl_set_invalid_field(ctsio,
6670 /*field*/ *len_used + i,
6673 free(ctsio->kern_data_ptr, M_CTL);
6674 ctl_done((union ctl_io *)ctsio);
6675 return (CTL_RETVAL_COMPLETE);
6679 * Decrement these before we call the page handler, since we may
6680 * end up getting called back one way or another before the handler
6681 * returns to this context.
6683 *len_left -= page_index->page_len;
6684 *len_used += page_index->page_len;
6686 retval = page_index->select_handler(ctsio, page_index,
6687 (uint8_t *)page_header);
6690 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6691 * wait until this queued command completes to finish processing
6692 * the mode page. If it returns anything other than
6693 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6694 * already set the sense information, freed the data pointer, and
6695 * completed the io for us.
6697 if (retval != CTL_RETVAL_COMPLETE)
6698 goto bailout_no_done;
6701 * If the initiator sent us more than one page, parse the next one.
6706 ctl_set_success(ctsio);
6707 free(ctsio->kern_data_ptr, M_CTL);
6708 ctl_done((union ctl_io *)ctsio);
6712 return (CTL_RETVAL_COMPLETE);
6717 ctl_mode_select(struct ctl_scsiio *ctsio)
6719 int param_len, pf, sp;
6720 int header_size, bd_len;
6721 int len_left, len_used;
6722 struct ctl_page_index *page_index;
6723 struct ctl_lun *lun;
6724 int control_dev, page_len;
6725 union ctl_modepage_info *modepage_info;
6737 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6739 if (lun->be_lun->lun_type != T_DIRECT)
6744 switch (ctsio->cdb[0]) {
6745 case MODE_SELECT_6: {
6746 struct scsi_mode_select_6 *cdb;
6748 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6750 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6751 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6753 param_len = cdb->length;
6754 header_size = sizeof(struct scsi_mode_header_6);
6757 case MODE_SELECT_10: {
6758 struct scsi_mode_select_10 *cdb;
6760 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6762 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6763 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6765 param_len = scsi_2btoul(cdb->length);
6766 header_size = sizeof(struct scsi_mode_header_10);
6770 ctl_set_invalid_opcode(ctsio);
6771 ctl_done((union ctl_io *)ctsio);
6772 return (CTL_RETVAL_COMPLETE);
6773 break; /* NOTREACHED */
6778 * "A parameter list length of zero indicates that the Data-Out Buffer
6779 * shall be empty. This condition shall not be considered as an error."
6781 if (param_len == 0) {
6782 ctl_set_success(ctsio);
6783 ctl_done((union ctl_io *)ctsio);
6784 return (CTL_RETVAL_COMPLETE);
6788 * Since we'll hit this the first time through, prior to
6789 * allocation, we don't need to free a data buffer here.
6791 if (param_len < header_size) {
6792 ctl_set_param_len_error(ctsio);
6793 ctl_done((union ctl_io *)ctsio);
6794 return (CTL_RETVAL_COMPLETE);
6798 * Allocate the data buffer and grab the user's data. In theory,
6799 * we shouldn't have to sanity check the parameter list length here
6800 * because the maximum size is 64K. We should be able to malloc
6801 * that much without too many problems.
6803 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6804 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6805 ctsio->kern_data_len = param_len;
6806 ctsio->kern_total_len = param_len;
6807 ctsio->kern_data_resid = 0;
6808 ctsio->kern_rel_offset = 0;
6809 ctsio->kern_sg_entries = 0;
6810 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6811 ctsio->be_move_done = ctl_config_move_done;
6812 ctl_datamove((union ctl_io *)ctsio);
6814 return (CTL_RETVAL_COMPLETE);
6817 switch (ctsio->cdb[0]) {
6818 case MODE_SELECT_6: {
6819 struct scsi_mode_header_6 *mh6;
6821 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6822 bd_len = mh6->blk_desc_len;
6825 case MODE_SELECT_10: {
6826 struct scsi_mode_header_10 *mh10;
6828 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6829 bd_len = scsi_2btoul(mh10->blk_desc_len);
6833 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6837 if (param_len < (header_size + bd_len)) {
6838 free(ctsio->kern_data_ptr, M_CTL);
6839 ctl_set_param_len_error(ctsio);
6840 ctl_done((union ctl_io *)ctsio);
6841 return (CTL_RETVAL_COMPLETE);
6845 * Set the IO_CONT flag, so that if this I/O gets passed to
6846 * ctl_config_write_done(), it'll get passed back to
6847 * ctl_do_mode_select() for further processing, or completion if
6850 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6851 ctsio->io_cont = ctl_do_mode_select;
6853 modepage_info = (union ctl_modepage_info *)
6854 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6856 memset(modepage_info, 0, sizeof(*modepage_info));
6858 len_left = param_len - header_size - bd_len;
6859 len_used = header_size + bd_len;
6861 modepage_info->header.len_left = len_left;
6862 modepage_info->header.len_used = len_used;
6864 return (ctl_do_mode_select((union ctl_io *)ctsio));
6868 ctl_mode_sense(struct ctl_scsiio *ctsio)
6870 struct ctl_lun *lun;
6871 int pc, page_code, dbd, llba, subpage;
6872 int alloc_len, page_len, header_len, total_len;
6873 struct scsi_mode_block_descr *block_desc;
6874 struct ctl_page_index *page_index;
6882 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6884 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6886 if (lun->be_lun->lun_type != T_DIRECT)
6891 if (lun->flags & CTL_LUN_PR_RESERVED) {
6895 * XXX KDM need a lock here.
6897 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6898 if ((lun->res_type == SPR_TYPE_EX_AC
6899 && residx != lun->pr_res_idx)
6900 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6901 || lun->res_type == SPR_TYPE_EX_AC_AR)
6902 && !lun->per_res[residx].registered)) {
6903 ctl_set_reservation_conflict(ctsio);
6904 ctl_done((union ctl_io *)ctsio);
6905 return (CTL_RETVAL_COMPLETE);
6909 switch (ctsio->cdb[0]) {
6910 case MODE_SENSE_6: {
6911 struct scsi_mode_sense_6 *cdb;
6913 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6915 header_len = sizeof(struct scsi_mode_hdr_6);
6916 if (cdb->byte2 & SMS_DBD)
6919 header_len += sizeof(struct scsi_mode_block_descr);
6921 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6922 page_code = cdb->page & SMS_PAGE_CODE;
6923 subpage = cdb->subpage;
6924 alloc_len = cdb->length;
6927 case MODE_SENSE_10: {
6928 struct scsi_mode_sense_10 *cdb;
6930 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6932 header_len = sizeof(struct scsi_mode_hdr_10);
6934 if (cdb->byte2 & SMS_DBD)
6937 header_len += sizeof(struct scsi_mode_block_descr);
6938 if (cdb->byte2 & SMS10_LLBAA)
6940 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6941 page_code = cdb->page & SMS_PAGE_CODE;
6942 subpage = cdb->subpage;
6943 alloc_len = scsi_2btoul(cdb->length);
6947 ctl_set_invalid_opcode(ctsio);
6948 ctl_done((union ctl_io *)ctsio);
6949 return (CTL_RETVAL_COMPLETE);
6950 break; /* NOTREACHED */
6954 * We have to make a first pass through to calculate the size of
6955 * the pages that match the user's query. Then we allocate enough
6956 * memory to hold it, and actually copy the data into the buffer.
6958 switch (page_code) {
6959 case SMS_ALL_PAGES_PAGE: {
6965 * At the moment, values other than 0 and 0xff here are
6966 * reserved according to SPC-3.
6968 if ((subpage != SMS_SUBPAGE_PAGE_0)
6969 && (subpage != SMS_SUBPAGE_ALL)) {
6970 ctl_set_invalid_field(ctsio,
6976 ctl_done((union ctl_io *)ctsio);
6977 return (CTL_RETVAL_COMPLETE);
6980 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6981 if ((control_dev != 0)
6982 && (lun->mode_pages.index[i].page_flags &
6983 CTL_PAGE_FLAG_DISK_ONLY))
6987 * We don't use this subpage if the user didn't
6988 * request all subpages.
6990 if ((lun->mode_pages.index[i].subpage != 0)
6991 && (subpage == SMS_SUBPAGE_PAGE_0))
6995 printf("found page %#x len %d\n",
6996 lun->mode_pages.index[i].page_code &
6998 lun->mode_pages.index[i].page_len);
7000 page_len += lun->mode_pages.index[i].page_len;
7009 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7010 /* Look for the right page code */
7011 if ((lun->mode_pages.index[i].page_code &
7012 SMPH_PC_MASK) != page_code)
7015 /* Look for the right subpage or the subpage wildcard*/
7016 if ((lun->mode_pages.index[i].subpage != subpage)
7017 && (subpage != SMS_SUBPAGE_ALL))
7020 /* Make sure the page is supported for this dev type */
7021 if ((control_dev != 0)
7022 && (lun->mode_pages.index[i].page_flags &
7023 CTL_PAGE_FLAG_DISK_ONLY))
7027 printf("found page %#x len %d\n",
7028 lun->mode_pages.index[i].page_code &
7030 lun->mode_pages.index[i].page_len);
7033 page_len += lun->mode_pages.index[i].page_len;
7036 if (page_len == 0) {
7037 ctl_set_invalid_field(ctsio,
7043 ctl_done((union ctl_io *)ctsio);
7044 return (CTL_RETVAL_COMPLETE);
7050 total_len = header_len + page_len;
7052 printf("header_len = %d, page_len = %d, total_len = %d\n",
7053 header_len, page_len, total_len);
7056 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7057 ctsio->kern_sg_entries = 0;
7058 ctsio->kern_data_resid = 0;
7059 ctsio->kern_rel_offset = 0;
7060 if (total_len < alloc_len) {
7061 ctsio->residual = alloc_len - total_len;
7062 ctsio->kern_data_len = total_len;
7063 ctsio->kern_total_len = total_len;
7065 ctsio->residual = 0;
7066 ctsio->kern_data_len = alloc_len;
7067 ctsio->kern_total_len = alloc_len;
7070 switch (ctsio->cdb[0]) {
7071 case MODE_SENSE_6: {
7072 struct scsi_mode_hdr_6 *header;
7074 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
7076 header->datalen = ctl_min(total_len - 1, 254);
7077 if (control_dev == 0)
7078 header->dev_specific = 0x10; /* DPOFUA */
7080 header->block_descr_len = 0;
7082 header->block_descr_len =
7083 sizeof(struct scsi_mode_block_descr);
7084 block_desc = (struct scsi_mode_block_descr *)&header[1];
7087 case MODE_SENSE_10: {
7088 struct scsi_mode_hdr_10 *header;
7091 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
7093 datalen = ctl_min(total_len - 2, 65533);
7094 scsi_ulto2b(datalen, header->datalen);
7095 if (control_dev == 0)
7096 header->dev_specific = 0x10; /* DPOFUA */
7098 scsi_ulto2b(0, header->block_descr_len);
7100 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
7101 header->block_descr_len);
7102 block_desc = (struct scsi_mode_block_descr *)&header[1];
7106 panic("invalid CDB type %#x", ctsio->cdb[0]);
7107 break; /* NOTREACHED */
7111 * If we've got a disk, use its blocksize in the block
7112 * descriptor. Otherwise, just set it to 0.
7115 if (control_dev != 0)
7116 scsi_ulto3b(lun->be_lun->blocksize,
7117 block_desc->block_len);
7119 scsi_ulto3b(0, block_desc->block_len);
7122 switch (page_code) {
7123 case SMS_ALL_PAGES_PAGE: {
7126 data_used = header_len;
7127 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7128 struct ctl_page_index *page_index;
7130 page_index = &lun->mode_pages.index[i];
7132 if ((control_dev != 0)
7133 && (page_index->page_flags &
7134 CTL_PAGE_FLAG_DISK_ONLY))
7138 * We don't use this subpage if the user didn't
7139 * request all subpages. We already checked (above)
7140 * to make sure the user only specified a subpage
7141 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
7143 if ((page_index->subpage != 0)
7144 && (subpage == SMS_SUBPAGE_PAGE_0))
7148 * Call the handler, if it exists, to update the
7149 * page to the latest values.
7151 if (page_index->sense_handler != NULL)
7152 page_index->sense_handler(ctsio, page_index,pc);
7154 memcpy(ctsio->kern_data_ptr + data_used,
7155 page_index->page_data +
7156 (page_index->page_len * pc),
7157 page_index->page_len);
7158 data_used += page_index->page_len;
7165 data_used = header_len;
7167 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7168 struct ctl_page_index *page_index;
7170 page_index = &lun->mode_pages.index[i];
7172 /* Look for the right page code */
7173 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
7176 /* Look for the right subpage or the subpage wildcard*/
7177 if ((page_index->subpage != subpage)
7178 && (subpage != SMS_SUBPAGE_ALL))
7181 /* Make sure the page is supported for this dev type */
7182 if ((control_dev != 0)
7183 && (page_index->page_flags &
7184 CTL_PAGE_FLAG_DISK_ONLY))
7188 * Call the handler, if it exists, to update the
7189 * page to the latest values.
7191 if (page_index->sense_handler != NULL)
7192 page_index->sense_handler(ctsio, page_index,pc);
7194 memcpy(ctsio->kern_data_ptr + data_used,
7195 page_index->page_data +
7196 (page_index->page_len * pc),
7197 page_index->page_len);
7198 data_used += page_index->page_len;
7204 ctsio->scsi_status = SCSI_STATUS_OK;
7206 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7207 ctsio->be_move_done = ctl_config_move_done;
7208 ctl_datamove((union ctl_io *)ctsio);
7210 return (CTL_RETVAL_COMPLETE);
7214 ctl_read_capacity(struct ctl_scsiio *ctsio)
7216 struct scsi_read_capacity *cdb;
7217 struct scsi_read_capacity_data *data;
7218 struct ctl_lun *lun;
7221 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7223 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7225 lba = scsi_4btoul(cdb->addr);
7226 if (((cdb->pmi & SRC_PMI) == 0)
7228 ctl_set_invalid_field(/*ctsio*/ ctsio,
7234 ctl_done((union ctl_io *)ctsio);
7235 return (CTL_RETVAL_COMPLETE);
7238 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7240 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7241 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7242 ctsio->residual = 0;
7243 ctsio->kern_data_len = sizeof(*data);
7244 ctsio->kern_total_len = sizeof(*data);
7245 ctsio->kern_data_resid = 0;
7246 ctsio->kern_rel_offset = 0;
7247 ctsio->kern_sg_entries = 0;
7250 * If the maximum LBA is greater than 0xfffffffe, the user must
7251 * issue a SERVICE ACTION IN (16) command, with the read capacity
7252 * serivce action set.
7254 if (lun->be_lun->maxlba > 0xfffffffe)
7255 scsi_ulto4b(0xffffffff, data->addr);
7257 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7260 * XXX KDM this may not be 512 bytes...
7262 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7264 ctsio->scsi_status = SCSI_STATUS_OK;
7266 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7267 ctsio->be_move_done = ctl_config_move_done;
7268 ctl_datamove((union ctl_io *)ctsio);
7270 return (CTL_RETVAL_COMPLETE);
7274 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7276 struct scsi_read_capacity_16 *cdb;
7277 struct scsi_read_capacity_data_long *data;
7278 struct ctl_lun *lun;
7282 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7284 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7286 alloc_len = scsi_4btoul(cdb->alloc_len);
7287 lba = scsi_8btou64(cdb->addr);
7289 if ((cdb->reladr & SRC16_PMI)
7291 ctl_set_invalid_field(/*ctsio*/ ctsio,
7297 ctl_done((union ctl_io *)ctsio);
7298 return (CTL_RETVAL_COMPLETE);
7301 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7303 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7304 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7306 if (sizeof(*data) < alloc_len) {
7307 ctsio->residual = alloc_len - sizeof(*data);
7308 ctsio->kern_data_len = sizeof(*data);
7309 ctsio->kern_total_len = sizeof(*data);
7311 ctsio->residual = 0;
7312 ctsio->kern_data_len = alloc_len;
7313 ctsio->kern_total_len = alloc_len;
7315 ctsio->kern_data_resid = 0;
7316 ctsio->kern_rel_offset = 0;
7317 ctsio->kern_sg_entries = 0;
7319 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7320 /* XXX KDM this may not be 512 bytes... */
7321 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7322 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7323 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7324 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7325 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7327 ctsio->scsi_status = SCSI_STATUS_OK;
7329 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7330 ctsio->be_move_done = ctl_config_move_done;
7331 ctl_datamove((union ctl_io *)ctsio);
7333 return (CTL_RETVAL_COMPLETE);
7337 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7339 struct scsi_maintenance_in *cdb;
7341 int alloc_len, ext, total_len = 0, g, p, pc, pg;
7342 int num_target_port_groups, num_target_ports, single;
7343 struct ctl_lun *lun;
7344 struct ctl_softc *softc;
7345 struct ctl_port *port;
7346 struct scsi_target_group_data *rtg_ptr;
7347 struct scsi_target_group_data_extended *rtg_ext_ptr;
7348 struct scsi_target_port_group_descriptor *tpg_desc;
7350 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7352 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7353 softc = control_softc;
7354 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7356 retval = CTL_RETVAL_COMPLETE;
7358 switch (cdb->byte2 & STG_PDF_MASK) {
7359 case STG_PDF_LENGTH:
7362 case STG_PDF_EXTENDED:
7366 ctl_set_invalid_field(/*ctsio*/ ctsio,
7372 ctl_done((union ctl_io *)ctsio);
7376 single = ctl_is_single;
7378 num_target_port_groups = 1;
7380 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7381 num_target_ports = 0;
7382 mtx_lock(&softc->ctl_lock);
7383 STAILQ_FOREACH(port, &softc->port_list, links) {
7384 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7386 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7390 mtx_unlock(&softc->ctl_lock);
7393 total_len = sizeof(struct scsi_target_group_data_extended);
7395 total_len = sizeof(struct scsi_target_group_data);
7396 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7397 num_target_port_groups +
7398 sizeof(struct scsi_target_port_descriptor) *
7399 num_target_ports * num_target_port_groups;
7401 alloc_len = scsi_4btoul(cdb->length);
7403 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7405 ctsio->kern_sg_entries = 0;
7407 if (total_len < alloc_len) {
7408 ctsio->residual = alloc_len - total_len;
7409 ctsio->kern_data_len = total_len;
7410 ctsio->kern_total_len = total_len;
7412 ctsio->residual = 0;
7413 ctsio->kern_data_len = alloc_len;
7414 ctsio->kern_total_len = alloc_len;
7416 ctsio->kern_data_resid = 0;
7417 ctsio->kern_rel_offset = 0;
7420 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7421 ctsio->kern_data_ptr;
7422 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7423 rtg_ext_ptr->format_type = 0x10;
7424 rtg_ext_ptr->implicit_transition_time = 0;
7425 tpg_desc = &rtg_ext_ptr->groups[0];
7427 rtg_ptr = (struct scsi_target_group_data *)
7428 ctsio->kern_data_ptr;
7429 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7430 tpg_desc = &rtg_ptr->groups[0];
7433 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS;
7434 mtx_lock(&softc->ctl_lock);
7435 for (g = 0; g < num_target_port_groups; g++) {
7437 tpg_desc->pref_state = TPG_PRIMARY |
7438 TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7440 tpg_desc->pref_state =
7441 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7442 tpg_desc->support = TPG_AO_SUP;
7444 tpg_desc->support |= TPG_AN_SUP;
7445 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7446 tpg_desc->status = TPG_IMPLICIT;
7448 STAILQ_FOREACH(port, &softc->port_list, links) {
7449 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7451 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7454 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7455 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7456 relative_target_port_identifier);
7459 tpg_desc->target_port_count = pc;
7460 tpg_desc = (struct scsi_target_port_group_descriptor *)
7461 &tpg_desc->descriptors[pc];
7463 mtx_unlock(&softc->ctl_lock);
7465 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7466 ctsio->be_move_done = ctl_config_move_done;
7468 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7469 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7470 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7471 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7472 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7474 ctl_datamove((union ctl_io *)ctsio);
7479 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7481 struct ctl_lun *lun;
7482 struct scsi_report_supported_opcodes *cdb;
7483 const struct ctl_cmd_entry *entry, *sentry;
7484 struct scsi_report_supported_opcodes_all *all;
7485 struct scsi_report_supported_opcodes_descr *descr;
7486 struct scsi_report_supported_opcodes_one *one;
7488 int alloc_len, total_len;
7489 int opcode, service_action, i, j, num;
7491 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7493 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7494 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7496 retval = CTL_RETVAL_COMPLETE;
7498 opcode = cdb->requested_opcode;
7499 service_action = scsi_2btoul(cdb->requested_service_action);
7500 switch (cdb->options & RSO_OPTIONS_MASK) {
7501 case RSO_OPTIONS_ALL:
7503 for (i = 0; i < 256; i++) {
7504 entry = &ctl_cmd_table[i];
7505 if (entry->flags & CTL_CMD_FLAG_SA5) {
7506 for (j = 0; j < 32; j++) {
7507 sentry = &((const struct ctl_cmd_entry *)
7509 if (ctl_cmd_applicable(
7510 lun->be_lun->lun_type, sentry))
7514 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7519 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7520 num * sizeof(struct scsi_report_supported_opcodes_descr);
7522 case RSO_OPTIONS_OC:
7523 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7524 ctl_set_invalid_field(/*ctsio*/ ctsio,
7530 ctl_done((union ctl_io *)ctsio);
7531 return (CTL_RETVAL_COMPLETE);
7533 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7535 case RSO_OPTIONS_OC_SA:
7536 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7537 service_action >= 32) {
7538 ctl_set_invalid_field(/*ctsio*/ ctsio,
7544 ctl_done((union ctl_io *)ctsio);
7545 return (CTL_RETVAL_COMPLETE);
7547 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7550 ctl_set_invalid_field(/*ctsio*/ ctsio,
7556 ctl_done((union ctl_io *)ctsio);
7557 return (CTL_RETVAL_COMPLETE);
7560 alloc_len = scsi_4btoul(cdb->length);
7562 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7564 ctsio->kern_sg_entries = 0;
7566 if (total_len < alloc_len) {
7567 ctsio->residual = alloc_len - total_len;
7568 ctsio->kern_data_len = total_len;
7569 ctsio->kern_total_len = total_len;
7571 ctsio->residual = 0;
7572 ctsio->kern_data_len = alloc_len;
7573 ctsio->kern_total_len = alloc_len;
7575 ctsio->kern_data_resid = 0;
7576 ctsio->kern_rel_offset = 0;
7578 switch (cdb->options & RSO_OPTIONS_MASK) {
7579 case RSO_OPTIONS_ALL:
7580 all = (struct scsi_report_supported_opcodes_all *)
7581 ctsio->kern_data_ptr;
7583 for (i = 0; i < 256; i++) {
7584 entry = &ctl_cmd_table[i];
7585 if (entry->flags & CTL_CMD_FLAG_SA5) {
7586 for (j = 0; j < 32; j++) {
7587 sentry = &((const struct ctl_cmd_entry *)
7589 if (!ctl_cmd_applicable(
7590 lun->be_lun->lun_type, sentry))
7592 descr = &all->descr[num++];
7594 scsi_ulto2b(j, descr->service_action);
7595 descr->flags = RSO_SERVACTV;
7596 scsi_ulto2b(sentry->length,
7600 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7603 descr = &all->descr[num++];
7605 scsi_ulto2b(0, descr->service_action);
7607 scsi_ulto2b(entry->length, descr->cdb_length);
7611 num * sizeof(struct scsi_report_supported_opcodes_descr),
7614 case RSO_OPTIONS_OC:
7615 one = (struct scsi_report_supported_opcodes_one *)
7616 ctsio->kern_data_ptr;
7617 entry = &ctl_cmd_table[opcode];
7619 case RSO_OPTIONS_OC_SA:
7620 one = (struct scsi_report_supported_opcodes_one *)
7621 ctsio->kern_data_ptr;
7622 entry = &ctl_cmd_table[opcode];
7623 entry = &((const struct ctl_cmd_entry *)
7624 entry->execute)[service_action];
7626 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7628 scsi_ulto2b(entry->length, one->cdb_length);
7629 one->cdb_usage[0] = opcode;
7630 memcpy(&one->cdb_usage[1], entry->usage,
7637 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7638 ctsio->be_move_done = ctl_config_move_done;
7640 ctl_datamove((union ctl_io *)ctsio);
7645 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7647 struct ctl_lun *lun;
7648 struct scsi_report_supported_tmf *cdb;
7649 struct scsi_report_supported_tmf_data *data;
7651 int alloc_len, total_len;
7653 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7655 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7656 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7658 retval = CTL_RETVAL_COMPLETE;
7660 total_len = sizeof(struct scsi_report_supported_tmf_data);
7661 alloc_len = scsi_4btoul(cdb->length);
7663 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7665 ctsio->kern_sg_entries = 0;
7667 if (total_len < alloc_len) {
7668 ctsio->residual = alloc_len - total_len;
7669 ctsio->kern_data_len = total_len;
7670 ctsio->kern_total_len = total_len;
7672 ctsio->residual = 0;
7673 ctsio->kern_data_len = alloc_len;
7674 ctsio->kern_total_len = alloc_len;
7676 ctsio->kern_data_resid = 0;
7677 ctsio->kern_rel_offset = 0;
7679 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7680 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7681 data->byte2 |= RST_ITNRS;
7683 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7684 ctsio->be_move_done = ctl_config_move_done;
7686 ctl_datamove((union ctl_io *)ctsio);
7691 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7693 struct ctl_lun *lun;
7694 struct scsi_report_timestamp *cdb;
7695 struct scsi_report_timestamp_data *data;
7699 int alloc_len, total_len;
7701 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7703 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7704 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7706 retval = CTL_RETVAL_COMPLETE;
7708 total_len = sizeof(struct scsi_report_timestamp_data);
7709 alloc_len = scsi_4btoul(cdb->length);
7711 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7713 ctsio->kern_sg_entries = 0;
7715 if (total_len < alloc_len) {
7716 ctsio->residual = alloc_len - total_len;
7717 ctsio->kern_data_len = total_len;
7718 ctsio->kern_total_len = total_len;
7720 ctsio->residual = 0;
7721 ctsio->kern_data_len = alloc_len;
7722 ctsio->kern_total_len = alloc_len;
7724 ctsio->kern_data_resid = 0;
7725 ctsio->kern_rel_offset = 0;
7727 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7728 scsi_ulto2b(sizeof(*data) - 2, data->length);
7729 data->origin = RTS_ORIG_OUTSIDE;
7731 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7732 scsi_ulto4b(timestamp >> 16, data->timestamp);
7733 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7735 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7736 ctsio->be_move_done = ctl_config_move_done;
7738 ctl_datamove((union ctl_io *)ctsio);
7743 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7745 struct scsi_per_res_in *cdb;
7746 int alloc_len, total_len = 0;
7747 /* struct scsi_per_res_in_rsrv in_data; */
7748 struct ctl_lun *lun;
7749 struct ctl_softc *softc;
7751 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7753 softc = control_softc;
7755 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7757 alloc_len = scsi_2btoul(cdb->length);
7759 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7762 mtx_lock(&lun->lun_lock);
7763 switch (cdb->action) {
7764 case SPRI_RK: /* read keys */
7765 total_len = sizeof(struct scsi_per_res_in_keys) +
7767 sizeof(struct scsi_per_res_key);
7769 case SPRI_RR: /* read reservation */
7770 if (lun->flags & CTL_LUN_PR_RESERVED)
7771 total_len = sizeof(struct scsi_per_res_in_rsrv);
7773 total_len = sizeof(struct scsi_per_res_in_header);
7775 case SPRI_RC: /* report capabilities */
7776 total_len = sizeof(struct scsi_per_res_cap);
7778 case SPRI_RS: /* read full status */
7779 total_len = sizeof(struct scsi_per_res_in_header) +
7780 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7784 panic("Invalid PR type %x", cdb->action);
7786 mtx_unlock(&lun->lun_lock);
7788 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7790 if (total_len < alloc_len) {
7791 ctsio->residual = alloc_len - total_len;
7792 ctsio->kern_data_len = total_len;
7793 ctsio->kern_total_len = total_len;
7795 ctsio->residual = 0;
7796 ctsio->kern_data_len = alloc_len;
7797 ctsio->kern_total_len = alloc_len;
7800 ctsio->kern_data_resid = 0;
7801 ctsio->kern_rel_offset = 0;
7802 ctsio->kern_sg_entries = 0;
7804 mtx_lock(&lun->lun_lock);
7805 switch (cdb->action) {
7806 case SPRI_RK: { // read keys
7807 struct scsi_per_res_in_keys *res_keys;
7810 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7813 * We had to drop the lock to allocate our buffer, which
7814 * leaves time for someone to come in with another
7815 * persistent reservation. (That is unlikely, though,
7816 * since this should be the only persistent reservation
7817 * command active right now.)
7819 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7820 (lun->pr_key_count *
7821 sizeof(struct scsi_per_res_key)))){
7822 mtx_unlock(&lun->lun_lock);
7823 free(ctsio->kern_data_ptr, M_CTL);
7824 printf("%s: reservation length changed, retrying\n",
7829 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7831 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7832 lun->pr_key_count, res_keys->header.length);
7834 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7835 if (!lun->per_res[i].registered)
7839 * We used lun->pr_key_count to calculate the
7840 * size to allocate. If it turns out the number of
7841 * initiators with the registered flag set is
7842 * larger than that (i.e. they haven't been kept in
7843 * sync), we've got a problem.
7845 if (key_count >= lun->pr_key_count) {
7847 csevent_log(CSC_CTL | CSC_SHELF_SW |
7849 csevent_LogType_Fault,
7850 csevent_AlertLevel_Yellow,
7851 csevent_FRU_ShelfController,
7852 csevent_FRU_Firmware,
7853 csevent_FRU_Unknown,
7854 "registered keys %d >= key "
7855 "count %d", key_count,
7861 memcpy(res_keys->keys[key_count].key,
7862 lun->per_res[i].res_key.key,
7863 ctl_min(sizeof(res_keys->keys[key_count].key),
7864 sizeof(lun->per_res[i].res_key)));
7869 case SPRI_RR: { // read reservation
7870 struct scsi_per_res_in_rsrv *res;
7871 int tmp_len, header_only;
7873 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7875 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7877 if (lun->flags & CTL_LUN_PR_RESERVED)
7879 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7880 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7881 res->header.length);
7884 tmp_len = sizeof(struct scsi_per_res_in_header);
7885 scsi_ulto4b(0, res->header.length);
7890 * We had to drop the lock to allocate our buffer, which
7891 * leaves time for someone to come in with another
7892 * persistent reservation. (That is unlikely, though,
7893 * since this should be the only persistent reservation
7894 * command active right now.)
7896 if (tmp_len != total_len) {
7897 mtx_unlock(&lun->lun_lock);
7898 free(ctsio->kern_data_ptr, M_CTL);
7899 printf("%s: reservation status changed, retrying\n",
7905 * No reservation held, so we're done.
7907 if (header_only != 0)
7911 * If the registration is an All Registrants type, the key
7912 * is 0, since it doesn't really matter.
7914 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7915 memcpy(res->data.reservation,
7916 &lun->per_res[lun->pr_res_idx].res_key,
7917 sizeof(struct scsi_per_res_key));
7919 res->data.scopetype = lun->res_type;
7922 case SPRI_RC: //report capabilities
7924 struct scsi_per_res_cap *res_cap;
7927 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7928 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7929 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3;
7930 type_mask = SPRI_TM_WR_EX_AR |
7936 scsi_ulto2b(type_mask, res_cap->type_mask);
7939 case SPRI_RS: { // read full status
7940 struct scsi_per_res_in_full *res_status;
7941 struct scsi_per_res_in_full_desc *res_desc;
7942 struct ctl_port *port;
7945 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7948 * We had to drop the lock to allocate our buffer, which
7949 * leaves time for someone to come in with another
7950 * persistent reservation. (That is unlikely, though,
7951 * since this should be the only persistent reservation
7952 * command active right now.)
7954 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7955 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7956 lun->pr_key_count)){
7957 mtx_unlock(&lun->lun_lock);
7958 free(ctsio->kern_data_ptr, M_CTL);
7959 printf("%s: reservation length changed, retrying\n",
7964 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7966 res_desc = &res_status->desc[0];
7967 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7968 if (!lun->per_res[i].registered)
7971 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key,
7972 sizeof(res_desc->res_key));
7973 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7974 (lun->pr_res_idx == i ||
7975 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7976 res_desc->flags = SPRI_FULL_R_HOLDER;
7977 res_desc->scopetype = lun->res_type;
7979 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7980 res_desc->rel_trgt_port_id);
7982 port = softc->ctl_ports[
7983 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
7985 len = ctl_create_iid(port,
7986 i % CTL_MAX_INIT_PER_PORT,
7987 res_desc->transport_id);
7988 scsi_ulto4b(len, res_desc->additional_length);
7989 res_desc = (struct scsi_per_res_in_full_desc *)
7990 &res_desc->transport_id[len];
7992 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7993 res_status->header.length);
7998 * This is a bug, because we just checked for this above,
7999 * and should have returned an error.
8001 panic("Invalid PR type %x", cdb->action);
8002 break; /* NOTREACHED */
8004 mtx_unlock(&lun->lun_lock);
8006 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8007 ctsio->be_move_done = ctl_config_move_done;
8009 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
8010 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
8011 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
8012 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
8013 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
8015 ctl_datamove((union ctl_io *)ctsio);
8017 return (CTL_RETVAL_COMPLETE);
8021 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
8025 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
8026 uint64_t sa_res_key, uint8_t type, uint32_t residx,
8027 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
8028 struct scsi_per_res_out_parms* param)
8030 union ctl_ha_msg persis_io;
8036 mtx_lock(&lun->lun_lock);
8037 if (sa_res_key == 0) {
8038 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8039 /* validate scope and type */
8040 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8042 mtx_unlock(&lun->lun_lock);
8043 ctl_set_invalid_field(/*ctsio*/ ctsio,
8049 ctl_done((union ctl_io *)ctsio);
8053 if (type>8 || type==2 || type==4 || type==0) {
8054 mtx_unlock(&lun->lun_lock);
8055 ctl_set_invalid_field(/*ctsio*/ ctsio,
8061 ctl_done((union ctl_io *)ctsio);
8065 /* temporarily unregister this nexus */
8066 lun->per_res[residx].registered = 0;
8069 * Unregister everybody else and build UA for
8072 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8073 if (lun->per_res[i].registered == 0)
8077 && i <CTL_MAX_INITIATORS)
8078 lun->pending_ua[i] |=
8080 else if (persis_offset
8081 && i >= persis_offset)
8082 lun->pending_ua[i-persis_offset] |=
8084 lun->per_res[i].registered = 0;
8085 memset(&lun->per_res[i].res_key, 0,
8086 sizeof(struct scsi_per_res_key));
8088 lun->per_res[residx].registered = 1;
8089 lun->pr_key_count = 1;
8090 lun->res_type = type;
8091 if (lun->res_type != SPR_TYPE_WR_EX_AR
8092 && lun->res_type != SPR_TYPE_EX_AC_AR)
8093 lun->pr_res_idx = residx;
8095 /* send msg to other side */
8096 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8097 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8098 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8099 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8100 persis_io.pr.pr_info.res_type = type;
8101 memcpy(persis_io.pr.pr_info.sa_res_key,
8102 param->serv_act_res_key,
8103 sizeof(param->serv_act_res_key));
8104 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8105 &persis_io, sizeof(persis_io), 0)) >
8106 CTL_HA_STATUS_SUCCESS) {
8107 printf("CTL:Persis Out error returned "
8108 "from ctl_ha_msg_send %d\n",
8112 /* not all registrants */
8113 mtx_unlock(&lun->lun_lock);
8114 free(ctsio->kern_data_ptr, M_CTL);
8115 ctl_set_invalid_field(ctsio,
8121 ctl_done((union ctl_io *)ctsio);
8124 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8125 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8128 if (res_key == sa_res_key) {
8131 * The spec implies this is not good but doesn't
8132 * say what to do. There are two choices either
8133 * generate a res conflict or check condition
8134 * with illegal field in parameter data. Since
8135 * that is what is done when the sa_res_key is
8136 * zero I'll take that approach since this has
8137 * to do with the sa_res_key.
8139 mtx_unlock(&lun->lun_lock);
8140 free(ctsio->kern_data_ptr, M_CTL);
8141 ctl_set_invalid_field(ctsio,
8147 ctl_done((union ctl_io *)ctsio);
8151 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8152 if (lun->per_res[i].registered
8153 && memcmp(param->serv_act_res_key,
8154 lun->per_res[i].res_key.key,
8155 sizeof(struct scsi_per_res_key)) != 0)
8159 lun->per_res[i].registered = 0;
8160 memset(&lun->per_res[i].res_key, 0,
8161 sizeof(struct scsi_per_res_key));
8162 lun->pr_key_count--;
8164 if (!persis_offset && i < CTL_MAX_INITIATORS)
8165 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT;
8166 else if (persis_offset && i >= persis_offset)
8167 lun->pending_ua[i-persis_offset] |=
8171 mtx_unlock(&lun->lun_lock);
8172 free(ctsio->kern_data_ptr, M_CTL);
8173 ctl_set_reservation_conflict(ctsio);
8174 ctl_done((union ctl_io *)ctsio);
8175 return (CTL_RETVAL_COMPLETE);
8177 /* send msg to other side */
8178 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8179 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8180 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8181 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8182 persis_io.pr.pr_info.res_type = type;
8183 memcpy(persis_io.pr.pr_info.sa_res_key,
8184 param->serv_act_res_key,
8185 sizeof(param->serv_act_res_key));
8186 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8187 &persis_io, sizeof(persis_io), 0)) >
8188 CTL_HA_STATUS_SUCCESS) {
8189 printf("CTL:Persis Out error returned from "
8190 "ctl_ha_msg_send %d\n", isc_retval);
8193 /* Reserved but not all registrants */
8194 /* sa_res_key is res holder */
8195 if (memcmp(param->serv_act_res_key,
8196 lun->per_res[lun->pr_res_idx].res_key.key,
8197 sizeof(struct scsi_per_res_key)) == 0) {
8198 /* validate scope and type */
8199 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8201 mtx_unlock(&lun->lun_lock);
8202 ctl_set_invalid_field(/*ctsio*/ ctsio,
8208 ctl_done((union ctl_io *)ctsio);
8212 if (type>8 || type==2 || type==4 || type==0) {
8213 mtx_unlock(&lun->lun_lock);
8214 ctl_set_invalid_field(/*ctsio*/ ctsio,
8220 ctl_done((union ctl_io *)ctsio);
8226 * if sa_res_key != res_key remove all
8227 * registrants w/sa_res_key and generate UA
8228 * for these registrants(Registrations
8229 * Preempted) if it wasn't an exclusive
8230 * reservation generate UA(Reservations
8231 * Preempted) for all other registered nexuses
8232 * if the type has changed. Establish the new
8233 * reservation and holder. If res_key and
8234 * sa_res_key are the same do the above
8235 * except don't unregister the res holder.
8239 * Temporarily unregister so it won't get
8240 * removed or UA generated
8242 lun->per_res[residx].registered = 0;
8243 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8244 if (lun->per_res[i].registered == 0)
8247 if (memcmp(param->serv_act_res_key,
8248 lun->per_res[i].res_key.key,
8249 sizeof(struct scsi_per_res_key)) == 0) {
8250 lun->per_res[i].registered = 0;
8251 memset(&lun->per_res[i].res_key,
8253 sizeof(struct scsi_per_res_key));
8254 lun->pr_key_count--;
8257 && i < CTL_MAX_INITIATORS)
8258 lun->pending_ua[i] |=
8260 else if (persis_offset
8261 && i >= persis_offset)
8262 lun->pending_ua[i-persis_offset] |=
8264 } else if (type != lun->res_type
8265 && (lun->res_type == SPR_TYPE_WR_EX_RO
8266 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8268 && i < CTL_MAX_INITIATORS)
8269 lun->pending_ua[i] |=
8271 else if (persis_offset
8272 && i >= persis_offset)
8278 lun->per_res[residx].registered = 1;
8279 lun->res_type = type;
8280 if (lun->res_type != SPR_TYPE_WR_EX_AR
8281 && lun->res_type != SPR_TYPE_EX_AC_AR)
8282 lun->pr_res_idx = residx;
8284 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8286 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8287 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8288 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8289 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8290 persis_io.pr.pr_info.res_type = type;
8291 memcpy(persis_io.pr.pr_info.sa_res_key,
8292 param->serv_act_res_key,
8293 sizeof(param->serv_act_res_key));
8294 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8295 &persis_io, sizeof(persis_io), 0)) >
8296 CTL_HA_STATUS_SUCCESS) {
8297 printf("CTL:Persis Out error returned "
8298 "from ctl_ha_msg_send %d\n",
8303 * sa_res_key is not the res holder just
8304 * remove registrants
8308 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8309 if (memcmp(param->serv_act_res_key,
8310 lun->per_res[i].res_key.key,
8311 sizeof(struct scsi_per_res_key)) != 0)
8315 lun->per_res[i].registered = 0;
8316 memset(&lun->per_res[i].res_key, 0,
8317 sizeof(struct scsi_per_res_key));
8318 lun->pr_key_count--;
8321 && i < CTL_MAX_INITIATORS)
8322 lun->pending_ua[i] |=
8324 else if (persis_offset
8325 && i >= persis_offset)
8326 lun->pending_ua[i-persis_offset] |=
8331 mtx_unlock(&lun->lun_lock);
8332 free(ctsio->kern_data_ptr, M_CTL);
8333 ctl_set_reservation_conflict(ctsio);
8334 ctl_done((union ctl_io *)ctsio);
8337 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8338 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8339 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8340 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8341 persis_io.pr.pr_info.res_type = type;
8342 memcpy(persis_io.pr.pr_info.sa_res_key,
8343 param->serv_act_res_key,
8344 sizeof(param->serv_act_res_key));
8345 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8346 &persis_io, sizeof(persis_io), 0)) >
8347 CTL_HA_STATUS_SUCCESS) {
8348 printf("CTL:Persis Out error returned "
8349 "from ctl_ha_msg_send %d\n",
8355 lun->PRGeneration++;
8356 mtx_unlock(&lun->lun_lock);
8362 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8366 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8367 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8368 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
8369 msg->pr.pr_info.sa_res_key,
8370 sizeof(struct scsi_per_res_key)) != 0) {
8371 uint64_t sa_res_key;
8372 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8374 if (sa_res_key == 0) {
8375 /* temporarily unregister this nexus */
8376 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8379 * Unregister everybody else and build UA for
8382 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8383 if (lun->per_res[i].registered == 0)
8387 && i < CTL_MAX_INITIATORS)
8388 lun->pending_ua[i] |=
8390 else if (persis_offset && i >= persis_offset)
8391 lun->pending_ua[i - persis_offset] |=
8393 lun->per_res[i].registered = 0;
8394 memset(&lun->per_res[i].res_key, 0,
8395 sizeof(struct scsi_per_res_key));
8398 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8399 lun->pr_key_count = 1;
8400 lun->res_type = msg->pr.pr_info.res_type;
8401 if (lun->res_type != SPR_TYPE_WR_EX_AR
8402 && lun->res_type != SPR_TYPE_EX_AC_AR)
8403 lun->pr_res_idx = msg->pr.pr_info.residx;
8405 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8406 if (memcmp(msg->pr.pr_info.sa_res_key,
8407 lun->per_res[i].res_key.key,
8408 sizeof(struct scsi_per_res_key)) != 0)
8411 lun->per_res[i].registered = 0;
8412 memset(&lun->per_res[i].res_key, 0,
8413 sizeof(struct scsi_per_res_key));
8414 lun->pr_key_count--;
8417 && i < persis_offset)
8418 lun->pending_ua[i] |=
8420 else if (persis_offset
8421 && i >= persis_offset)
8422 lun->pending_ua[i - persis_offset] |=
8428 * Temporarily unregister so it won't get removed
8431 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8432 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8433 if (lun->per_res[i].registered == 0)
8436 if (memcmp(msg->pr.pr_info.sa_res_key,
8437 lun->per_res[i].res_key.key,
8438 sizeof(struct scsi_per_res_key)) == 0) {
8439 lun->per_res[i].registered = 0;
8440 memset(&lun->per_res[i].res_key, 0,
8441 sizeof(struct scsi_per_res_key));
8442 lun->pr_key_count--;
8444 && i < CTL_MAX_INITIATORS)
8445 lun->pending_ua[i] |=
8447 else if (persis_offset
8448 && i >= persis_offset)
8449 lun->pending_ua[i - persis_offset] |=
8451 } else if (msg->pr.pr_info.res_type != lun->res_type
8452 && (lun->res_type == SPR_TYPE_WR_EX_RO
8453 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8455 && i < persis_offset)
8456 lun->pending_ua[i] |=
8458 else if (persis_offset
8459 && i >= persis_offset)
8460 lun->pending_ua[i - persis_offset] |=
8464 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8465 lun->res_type = msg->pr.pr_info.res_type;
8466 if (lun->res_type != SPR_TYPE_WR_EX_AR
8467 && lun->res_type != SPR_TYPE_EX_AC_AR)
8468 lun->pr_res_idx = msg->pr.pr_info.residx;
8470 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8472 lun->PRGeneration++;
8478 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8482 u_int32_t param_len;
8483 struct scsi_per_res_out *cdb;
8484 struct ctl_lun *lun;
8485 struct scsi_per_res_out_parms* param;
8486 struct ctl_softc *softc;
8488 uint64_t res_key, sa_res_key;
8490 union ctl_ha_msg persis_io;
8493 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8495 retval = CTL_RETVAL_COMPLETE;
8497 softc = control_softc;
8499 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8500 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8503 * We only support whole-LUN scope. The scope & type are ignored for
8504 * register, register and ignore existing key and clear.
8505 * We sometimes ignore scope and type on preempts too!!
8506 * Verify reservation type here as well.
8508 type = cdb->scope_type & SPR_TYPE_MASK;
8509 if ((cdb->action == SPRO_RESERVE)
8510 || (cdb->action == SPRO_RELEASE)) {
8511 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8512 ctl_set_invalid_field(/*ctsio*/ ctsio,
8518 ctl_done((union ctl_io *)ctsio);
8519 return (CTL_RETVAL_COMPLETE);
8522 if (type>8 || type==2 || type==4 || type==0) {
8523 ctl_set_invalid_field(/*ctsio*/ ctsio,
8529 ctl_done((union ctl_io *)ctsio);
8530 return (CTL_RETVAL_COMPLETE);
8534 param_len = scsi_4btoul(cdb->length);
8536 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8537 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8538 ctsio->kern_data_len = param_len;
8539 ctsio->kern_total_len = param_len;
8540 ctsio->kern_data_resid = 0;
8541 ctsio->kern_rel_offset = 0;
8542 ctsio->kern_sg_entries = 0;
8543 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8544 ctsio->be_move_done = ctl_config_move_done;
8545 ctl_datamove((union ctl_io *)ctsio);
8547 return (CTL_RETVAL_COMPLETE);
8550 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8552 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8553 res_key = scsi_8btou64(param->res_key.key);
8554 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8557 * Validate the reservation key here except for SPRO_REG_IGNO
8558 * This must be done for all other service actions
8560 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8561 mtx_lock(&lun->lun_lock);
8562 if (lun->per_res[residx].registered) {
8563 if (memcmp(param->res_key.key,
8564 lun->per_res[residx].res_key.key,
8565 ctl_min(sizeof(param->res_key),
8566 sizeof(lun->per_res[residx].res_key))) != 0) {
8568 * The current key passed in doesn't match
8569 * the one the initiator previously
8572 mtx_unlock(&lun->lun_lock);
8573 free(ctsio->kern_data_ptr, M_CTL);
8574 ctl_set_reservation_conflict(ctsio);
8575 ctl_done((union ctl_io *)ctsio);
8576 return (CTL_RETVAL_COMPLETE);
8578 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8580 * We are not registered
8582 mtx_unlock(&lun->lun_lock);
8583 free(ctsio->kern_data_ptr, M_CTL);
8584 ctl_set_reservation_conflict(ctsio);
8585 ctl_done((union ctl_io *)ctsio);
8586 return (CTL_RETVAL_COMPLETE);
8587 } else if (res_key != 0) {
8589 * We are not registered and trying to register but
8590 * the register key isn't zero.
8592 mtx_unlock(&lun->lun_lock);
8593 free(ctsio->kern_data_ptr, M_CTL);
8594 ctl_set_reservation_conflict(ctsio);
8595 ctl_done((union ctl_io *)ctsio);
8596 return (CTL_RETVAL_COMPLETE);
8598 mtx_unlock(&lun->lun_lock);
8601 switch (cdb->action & SPRO_ACTION_MASK) {
8603 case SPRO_REG_IGNO: {
8606 printf("Registration received\n");
8610 * We don't support any of these options, as we report in
8611 * the read capabilities request (see
8612 * ctl_persistent_reserve_in(), above).
8614 if ((param->flags & SPR_SPEC_I_PT)
8615 || (param->flags & SPR_ALL_TG_PT)
8616 || (param->flags & SPR_APTPL)) {
8619 if (param->flags & SPR_APTPL)
8621 else if (param->flags & SPR_ALL_TG_PT)
8623 else /* SPR_SPEC_I_PT */
8626 free(ctsio->kern_data_ptr, M_CTL);
8627 ctl_set_invalid_field(ctsio,
8633 ctl_done((union ctl_io *)ctsio);
8634 return (CTL_RETVAL_COMPLETE);
8637 mtx_lock(&lun->lun_lock);
8640 * The initiator wants to clear the
8643 if (sa_res_key == 0) {
8645 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8646 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8647 && !lun->per_res[residx].registered)) {
8648 mtx_unlock(&lun->lun_lock);
8652 lun->per_res[residx].registered = 0;
8653 memset(&lun->per_res[residx].res_key,
8654 0, sizeof(lun->per_res[residx].res_key));
8655 lun->pr_key_count--;
8657 if (residx == lun->pr_res_idx) {
8658 lun->flags &= ~CTL_LUN_PR_RESERVED;
8659 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8661 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8662 || lun->res_type == SPR_TYPE_EX_AC_RO)
8663 && lun->pr_key_count) {
8665 * If the reservation is a registrants
8666 * only type we need to generate a UA
8667 * for other registered inits. The
8668 * sense code should be RESERVATIONS
8672 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8674 i+persis_offset].registered
8677 lun->pending_ua[i] |=
8682 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8683 if (lun->pr_key_count==0) {
8684 lun->flags &= ~CTL_LUN_PR_RESERVED;
8686 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8689 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8690 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8691 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8692 persis_io.pr.pr_info.residx = residx;
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);
8699 } else /* sa_res_key != 0 */ {
8702 * If we aren't registered currently then increment
8703 * the key count and set the registered flag.
8705 if (!lun->per_res[residx].registered) {
8706 lun->pr_key_count++;
8707 lun->per_res[residx].registered = 1;
8710 memcpy(&lun->per_res[residx].res_key,
8711 param->serv_act_res_key,
8712 ctl_min(sizeof(param->serv_act_res_key),
8713 sizeof(lun->per_res[residx].res_key)));
8715 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8716 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8717 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8718 persis_io.pr.pr_info.residx = residx;
8719 memcpy(persis_io.pr.pr_info.sa_res_key,
8720 param->serv_act_res_key,
8721 sizeof(param->serv_act_res_key));
8722 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8723 &persis_io, sizeof(persis_io), 0)) >
8724 CTL_HA_STATUS_SUCCESS) {
8725 printf("CTL:Persis Out error returned from "
8726 "ctl_ha_msg_send %d\n", isc_retval);
8729 lun->PRGeneration++;
8730 mtx_unlock(&lun->lun_lock);
8736 printf("Reserve executed type %d\n", type);
8738 mtx_lock(&lun->lun_lock);
8739 if (lun->flags & CTL_LUN_PR_RESERVED) {
8741 * if this isn't the reservation holder and it's
8742 * not a "all registrants" type or if the type is
8743 * different then we have a conflict
8745 if ((lun->pr_res_idx != residx
8746 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8747 || lun->res_type != type) {
8748 mtx_unlock(&lun->lun_lock);
8749 free(ctsio->kern_data_ptr, M_CTL);
8750 ctl_set_reservation_conflict(ctsio);
8751 ctl_done((union ctl_io *)ctsio);
8752 return (CTL_RETVAL_COMPLETE);
8754 mtx_unlock(&lun->lun_lock);
8755 } else /* create a reservation */ {
8757 * If it's not an "all registrants" type record
8758 * reservation holder
8760 if (type != SPR_TYPE_WR_EX_AR
8761 && type != SPR_TYPE_EX_AC_AR)
8762 lun->pr_res_idx = residx; /* Res holder */
8764 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8766 lun->flags |= CTL_LUN_PR_RESERVED;
8767 lun->res_type = type;
8769 mtx_unlock(&lun->lun_lock);
8771 /* send msg to other side */
8772 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8773 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8774 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8775 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8776 persis_io.pr.pr_info.res_type = type;
8777 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8778 &persis_io, sizeof(persis_io), 0)) >
8779 CTL_HA_STATUS_SUCCESS) {
8780 printf("CTL:Persis Out error returned from "
8781 "ctl_ha_msg_send %d\n", isc_retval);
8787 mtx_lock(&lun->lun_lock);
8788 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8789 /* No reservation exists return good status */
8790 mtx_unlock(&lun->lun_lock);
8794 * Is this nexus a reservation holder?
8796 if (lun->pr_res_idx != residx
8797 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8799 * not a res holder return good status but
8802 mtx_unlock(&lun->lun_lock);
8806 if (lun->res_type != type) {
8807 mtx_unlock(&lun->lun_lock);
8808 free(ctsio->kern_data_ptr, M_CTL);
8809 ctl_set_illegal_pr_release(ctsio);
8810 ctl_done((union ctl_io *)ctsio);
8811 return (CTL_RETVAL_COMPLETE);
8814 /* okay to release */
8815 lun->flags &= ~CTL_LUN_PR_RESERVED;
8816 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8820 * if this isn't an exclusive access
8821 * res generate UA for all other
8824 if (type != SPR_TYPE_EX_AC
8825 && type != SPR_TYPE_WR_EX) {
8827 * temporarily unregister so we don't generate UA
8829 lun->per_res[residx].registered = 0;
8831 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8832 if (lun->per_res[i+persis_offset].registered
8835 lun->pending_ua[i] |=
8839 lun->per_res[residx].registered = 1;
8841 mtx_unlock(&lun->lun_lock);
8842 /* Send msg to other side */
8843 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8844 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8845 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8846 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8847 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8848 printf("CTL:Persis Out error returned from "
8849 "ctl_ha_msg_send %d\n", isc_retval);
8854 /* send msg to other side */
8856 mtx_lock(&lun->lun_lock);
8857 lun->flags &= ~CTL_LUN_PR_RESERVED;
8859 lun->pr_key_count = 0;
8860 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8863 memset(&lun->per_res[residx].res_key,
8864 0, sizeof(lun->per_res[residx].res_key));
8865 lun->per_res[residx].registered = 0;
8867 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8868 if (lun->per_res[i].registered) {
8869 if (!persis_offset && i < CTL_MAX_INITIATORS)
8870 lun->pending_ua[i] |=
8872 else if (persis_offset && i >= persis_offset)
8873 lun->pending_ua[i-persis_offset] |=
8876 memset(&lun->per_res[i].res_key,
8877 0, sizeof(struct scsi_per_res_key));
8878 lun->per_res[i].registered = 0;
8880 lun->PRGeneration++;
8881 mtx_unlock(&lun->lun_lock);
8882 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8883 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8884 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8885 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8886 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8887 printf("CTL:Persis Out error returned from "
8888 "ctl_ha_msg_send %d\n", isc_retval);
8892 case SPRO_PREEMPT: {
8895 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8896 residx, ctsio, cdb, param);
8898 return (CTL_RETVAL_COMPLETE);
8902 panic("Invalid PR type %x", cdb->action);
8906 free(ctsio->kern_data_ptr, M_CTL);
8907 ctl_set_success(ctsio);
8908 ctl_done((union ctl_io *)ctsio);
8914 * This routine is for handling a message from the other SC pertaining to
8915 * persistent reserve out. All the error checking will have been done
8916 * so only perorming the action need be done here to keep the two
8920 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8922 struct ctl_lun *lun;
8923 struct ctl_softc *softc;
8927 softc = control_softc;
8929 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8930 lun = softc->ctl_luns[targ_lun];
8931 mtx_lock(&lun->lun_lock);
8932 switch(msg->pr.pr_info.action) {
8933 case CTL_PR_REG_KEY:
8934 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8935 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8936 lun->pr_key_count++;
8938 lun->PRGeneration++;
8939 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8940 msg->pr.pr_info.sa_res_key,
8941 sizeof(struct scsi_per_res_key));
8944 case CTL_PR_UNREG_KEY:
8945 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8946 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8947 0, sizeof(struct scsi_per_res_key));
8948 lun->pr_key_count--;
8950 /* XXX Need to see if the reservation has been released */
8951 /* if so do we need to generate UA? */
8952 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8953 lun->flags &= ~CTL_LUN_PR_RESERVED;
8954 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8956 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8957 || lun->res_type == SPR_TYPE_EX_AC_RO)
8958 && lun->pr_key_count) {
8960 * If the reservation is a registrants
8961 * only type we need to generate a UA
8962 * for other registered inits. The
8963 * sense code should be RESERVATIONS
8967 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8969 persis_offset].registered == 0)
8972 lun->pending_ua[i] |=
8977 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8978 if (lun->pr_key_count==0) {
8979 lun->flags &= ~CTL_LUN_PR_RESERVED;
8981 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8984 lun->PRGeneration++;
8987 case CTL_PR_RESERVE:
8988 lun->flags |= CTL_LUN_PR_RESERVED;
8989 lun->res_type = msg->pr.pr_info.res_type;
8990 lun->pr_res_idx = msg->pr.pr_info.residx;
8994 case CTL_PR_RELEASE:
8996 * if this isn't an exclusive access res generate UA for all
8997 * other registrants.
8999 if (lun->res_type != SPR_TYPE_EX_AC
9000 && lun->res_type != SPR_TYPE_WR_EX) {
9001 for (i = 0; i < CTL_MAX_INITIATORS; i++)
9002 if (lun->per_res[i+persis_offset].registered)
9003 lun->pending_ua[i] |=
9007 lun->flags &= ~CTL_LUN_PR_RESERVED;
9008 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
9012 case CTL_PR_PREEMPT:
9013 ctl_pro_preempt_other(lun, msg);
9016 lun->flags &= ~CTL_LUN_PR_RESERVED;
9018 lun->pr_key_count = 0;
9019 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
9021 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
9022 if (lun->per_res[i].registered == 0)
9025 && i < CTL_MAX_INITIATORS)
9026 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT;
9027 else if (persis_offset
9028 && i >= persis_offset)
9029 lun->pending_ua[i-persis_offset] |=
9031 memset(&lun->per_res[i].res_key, 0,
9032 sizeof(struct scsi_per_res_key));
9033 lun->per_res[i].registered = 0;
9035 lun->PRGeneration++;
9039 mtx_unlock(&lun->lun_lock);
9043 ctl_read_write(struct ctl_scsiio *ctsio)
9045 struct ctl_lun *lun;
9046 struct ctl_lba_len_flags *lbalen;
9048 uint32_t num_blocks;
9052 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9054 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
9057 retval = CTL_RETVAL_COMPLETE;
9059 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
9060 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
9061 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
9065 * XXX KDM need a lock here.
9067 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
9068 if ((lun->res_type == SPR_TYPE_EX_AC
9069 && residx != lun->pr_res_idx)
9070 || ((lun->res_type == SPR_TYPE_EX_AC_RO
9071 || lun->res_type == SPR_TYPE_EX_AC_AR)
9072 && !lun->per_res[residx].registered)) {
9073 ctl_set_reservation_conflict(ctsio);
9074 ctl_done((union ctl_io *)ctsio);
9075 return (CTL_RETVAL_COMPLETE);
9079 switch (ctsio->cdb[0]) {
9082 struct scsi_rw_6 *cdb;
9084 cdb = (struct scsi_rw_6 *)ctsio->cdb;
9086 lba = scsi_3btoul(cdb->addr);
9087 /* only 5 bits are valid in the most significant address byte */
9089 num_blocks = cdb->length;
9091 * This is correct according to SBC-2.
9093 if (num_blocks == 0)
9099 struct scsi_rw_10 *cdb;
9101 cdb = (struct scsi_rw_10 *)ctsio->cdb;
9102 if (cdb->byte2 & SRW10_FUA)
9103 flags |= CTL_LLF_FUA;
9104 if (cdb->byte2 & SRW10_DPO)
9105 flags |= CTL_LLF_DPO;
9106 lba = scsi_4btoul(cdb->addr);
9107 num_blocks = scsi_2btoul(cdb->length);
9110 case WRITE_VERIFY_10: {
9111 struct scsi_write_verify_10 *cdb;
9113 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
9114 flags |= CTL_LLF_FUA;
9115 if (cdb->byte2 & SWV_DPO)
9116 flags |= CTL_LLF_DPO;
9117 lba = scsi_4btoul(cdb->addr);
9118 num_blocks = scsi_2btoul(cdb->length);
9123 struct scsi_rw_12 *cdb;
9125 cdb = (struct scsi_rw_12 *)ctsio->cdb;
9126 if (cdb->byte2 & SRW12_FUA)
9127 flags |= CTL_LLF_FUA;
9128 if (cdb->byte2 & SRW12_DPO)
9129 flags |= CTL_LLF_DPO;
9130 lba = scsi_4btoul(cdb->addr);
9131 num_blocks = scsi_4btoul(cdb->length);
9134 case WRITE_VERIFY_12: {
9135 struct scsi_write_verify_12 *cdb;
9137 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
9138 flags |= CTL_LLF_FUA;
9139 if (cdb->byte2 & SWV_DPO)
9140 flags |= CTL_LLF_DPO;
9141 lba = scsi_4btoul(cdb->addr);
9142 num_blocks = scsi_4btoul(cdb->length);
9147 struct scsi_rw_16 *cdb;
9149 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9150 if (cdb->byte2 & SRW12_FUA)
9151 flags |= CTL_LLF_FUA;
9152 if (cdb->byte2 & SRW12_DPO)
9153 flags |= CTL_LLF_DPO;
9154 lba = scsi_8btou64(cdb->addr);
9155 num_blocks = scsi_4btoul(cdb->length);
9158 case WRITE_VERIFY_16: {
9159 struct scsi_write_verify_16 *cdb;
9161 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
9162 flags |= CTL_LLF_FUA;
9163 if (cdb->byte2 & SWV_DPO)
9164 flags |= CTL_LLF_DPO;
9165 lba = scsi_8btou64(cdb->addr);
9166 num_blocks = scsi_4btoul(cdb->length);
9171 * We got a command we don't support. This shouldn't
9172 * happen, commands should be filtered out above us.
9174 ctl_set_invalid_opcode(ctsio);
9175 ctl_done((union ctl_io *)ctsio);
9177 return (CTL_RETVAL_COMPLETE);
9178 break; /* NOTREACHED */
9182 * The first check is to make sure we're in bounds, the second
9183 * check is to catch wrap-around problems. If the lba + num blocks
9184 * is less than the lba, then we've wrapped around and the block
9185 * range is invalid anyway.
9187 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9188 || ((lba + num_blocks) < lba)) {
9189 ctl_set_lba_out_of_range(ctsio);
9190 ctl_done((union ctl_io *)ctsio);
9191 return (CTL_RETVAL_COMPLETE);
9195 * According to SBC-3, a transfer length of 0 is not an error.
9196 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9197 * translates to 256 blocks for those commands.
9199 if (num_blocks == 0) {
9200 ctl_set_success(ctsio);
9201 ctl_done((union ctl_io *)ctsio);
9202 return (CTL_RETVAL_COMPLETE);
9205 /* Set FUA and/or DPO if caches are disabled. */
9207 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9209 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
9211 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9213 flags |= CTL_LLF_FUA;
9216 lbalen = (struct ctl_lba_len_flags *)
9217 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9219 lbalen->len = num_blocks;
9220 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
9222 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9223 ctsio->kern_rel_offset = 0;
9225 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9227 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9233 ctl_cnw_cont(union ctl_io *io)
9235 struct ctl_scsiio *ctsio;
9236 struct ctl_lun *lun;
9237 struct ctl_lba_len_flags *lbalen;
9240 ctsio = &io->scsiio;
9241 ctsio->io_hdr.status = CTL_STATUS_NONE;
9242 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9243 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9244 lbalen = (struct ctl_lba_len_flags *)
9245 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9246 lbalen->flags &= ~CTL_LLF_COMPARE;
9247 lbalen->flags |= CTL_LLF_WRITE;
9249 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9250 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9255 ctl_cnw(struct ctl_scsiio *ctsio)
9257 struct ctl_lun *lun;
9258 struct ctl_lba_len_flags *lbalen;
9260 uint32_t num_blocks;
9263 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9265 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9268 retval = CTL_RETVAL_COMPLETE;
9270 switch (ctsio->cdb[0]) {
9271 case COMPARE_AND_WRITE: {
9272 struct scsi_compare_and_write *cdb;
9274 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9275 if (cdb->byte2 & SRW10_FUA)
9276 flags |= CTL_LLF_FUA;
9277 if (cdb->byte2 & SRW10_DPO)
9278 flags |= CTL_LLF_DPO;
9279 lba = scsi_8btou64(cdb->addr);
9280 num_blocks = cdb->length;
9285 * We got a command we don't support. This shouldn't
9286 * happen, commands should be filtered out above us.
9288 ctl_set_invalid_opcode(ctsio);
9289 ctl_done((union ctl_io *)ctsio);
9291 return (CTL_RETVAL_COMPLETE);
9292 break; /* NOTREACHED */
9296 * The first check is to make sure we're in bounds, the second
9297 * check is to catch wrap-around problems. If the lba + num blocks
9298 * is less than the lba, then we've wrapped around and the block
9299 * range is invalid anyway.
9301 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9302 || ((lba + num_blocks) < lba)) {
9303 ctl_set_lba_out_of_range(ctsio);
9304 ctl_done((union ctl_io *)ctsio);
9305 return (CTL_RETVAL_COMPLETE);
9309 * According to SBC-3, a transfer length of 0 is not an error.
9311 if (num_blocks == 0) {
9312 ctl_set_success(ctsio);
9313 ctl_done((union ctl_io *)ctsio);
9314 return (CTL_RETVAL_COMPLETE);
9317 /* Set FUA if write cache is disabled. */
9318 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9320 flags |= CTL_LLF_FUA;
9322 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9323 ctsio->kern_rel_offset = 0;
9326 * Set the IO_CONT flag, so that if this I/O gets passed to
9327 * ctl_data_submit_done(), it'll get passed back to
9328 * ctl_ctl_cnw_cont() for further processing.
9330 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9331 ctsio->io_cont = ctl_cnw_cont;
9333 lbalen = (struct ctl_lba_len_flags *)
9334 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9336 lbalen->len = num_blocks;
9337 lbalen->flags = CTL_LLF_COMPARE | flags;
9339 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9340 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9345 ctl_verify(struct ctl_scsiio *ctsio)
9347 struct ctl_lun *lun;
9348 struct ctl_lba_len_flags *lbalen;
9350 uint32_t num_blocks;
9354 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9356 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9359 flags = CTL_LLF_FUA;
9360 retval = CTL_RETVAL_COMPLETE;
9362 switch (ctsio->cdb[0]) {
9364 struct scsi_verify_10 *cdb;
9366 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9367 if (cdb->byte2 & SVFY_BYTCHK)
9369 if (cdb->byte2 & SVFY_DPO)
9370 flags |= CTL_LLF_DPO;
9371 lba = scsi_4btoul(cdb->addr);
9372 num_blocks = scsi_2btoul(cdb->length);
9376 struct scsi_verify_12 *cdb;
9378 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9379 if (cdb->byte2 & SVFY_BYTCHK)
9381 if (cdb->byte2 & SVFY_DPO)
9382 flags |= CTL_LLF_DPO;
9383 lba = scsi_4btoul(cdb->addr);
9384 num_blocks = scsi_4btoul(cdb->length);
9388 struct scsi_rw_16 *cdb;
9390 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9391 if (cdb->byte2 & SVFY_BYTCHK)
9393 if (cdb->byte2 & SVFY_DPO)
9394 flags |= CTL_LLF_DPO;
9395 lba = scsi_8btou64(cdb->addr);
9396 num_blocks = scsi_4btoul(cdb->length);
9401 * We got a command we don't support. This shouldn't
9402 * happen, commands should be filtered out above us.
9404 ctl_set_invalid_opcode(ctsio);
9405 ctl_done((union ctl_io *)ctsio);
9406 return (CTL_RETVAL_COMPLETE);
9410 * The first check is to make sure we're in bounds, the second
9411 * check is to catch wrap-around problems. If the lba + num blocks
9412 * is less than the lba, then we've wrapped around and the block
9413 * range is invalid anyway.
9415 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9416 || ((lba + num_blocks) < lba)) {
9417 ctl_set_lba_out_of_range(ctsio);
9418 ctl_done((union ctl_io *)ctsio);
9419 return (CTL_RETVAL_COMPLETE);
9423 * According to SBC-3, a transfer length of 0 is not an error.
9425 if (num_blocks == 0) {
9426 ctl_set_success(ctsio);
9427 ctl_done((union ctl_io *)ctsio);
9428 return (CTL_RETVAL_COMPLETE);
9431 lbalen = (struct ctl_lba_len_flags *)
9432 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9434 lbalen->len = num_blocks;
9436 lbalen->flags = CTL_LLF_COMPARE | flags;
9437 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9439 lbalen->flags = CTL_LLF_VERIFY | flags;
9440 ctsio->kern_total_len = 0;
9442 ctsio->kern_rel_offset = 0;
9444 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9445 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9450 ctl_report_luns(struct ctl_scsiio *ctsio)
9452 struct scsi_report_luns *cdb;
9453 struct scsi_report_luns_data *lun_data;
9454 struct ctl_lun *lun, *request_lun;
9455 int num_luns, retval;
9456 uint32_t alloc_len, lun_datalen;
9457 int num_filled, well_known;
9458 uint32_t initidx, targ_lun_id, lun_id;
9460 retval = CTL_RETVAL_COMPLETE;
9463 cdb = (struct scsi_report_luns *)ctsio->cdb;
9465 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9467 mtx_lock(&control_softc->ctl_lock);
9468 num_luns = control_softc->num_luns;
9469 mtx_unlock(&control_softc->ctl_lock);
9471 switch (cdb->select_report) {
9472 case RPL_REPORT_DEFAULT:
9473 case RPL_REPORT_ALL:
9475 case RPL_REPORT_WELLKNOWN:
9480 ctl_set_invalid_field(ctsio,
9486 ctl_done((union ctl_io *)ctsio);
9488 break; /* NOTREACHED */
9491 alloc_len = scsi_4btoul(cdb->length);
9493 * The initiator has to allocate at least 16 bytes for this request,
9494 * so he can at least get the header and the first LUN. Otherwise
9495 * we reject the request (per SPC-3 rev 14, section 6.21).
9497 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9498 sizeof(struct scsi_report_luns_lundata))) {
9499 ctl_set_invalid_field(ctsio,
9505 ctl_done((union ctl_io *)ctsio);
9509 request_lun = (struct ctl_lun *)
9510 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9512 lun_datalen = sizeof(*lun_data) +
9513 (num_luns * sizeof(struct scsi_report_luns_lundata));
9515 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9516 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9517 ctsio->kern_sg_entries = 0;
9519 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9521 mtx_lock(&control_softc->ctl_lock);
9522 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9523 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9524 if (lun_id >= CTL_MAX_LUNS)
9526 lun = control_softc->ctl_luns[lun_id];
9530 if (targ_lun_id <= 0xff) {
9532 * Peripheral addressing method, bus number 0.
9534 lun_data->luns[num_filled].lundata[0] =
9535 RPL_LUNDATA_ATYP_PERIPH;
9536 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9538 } else if (targ_lun_id <= 0x3fff) {
9540 * Flat addressing method.
9542 lun_data->luns[num_filled].lundata[0] =
9543 RPL_LUNDATA_ATYP_FLAT |
9544 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9545 #ifdef OLDCTLHEADERS
9546 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9547 (targ_lun_id & SRLD_BUS_LUN_MASK);
9549 lun_data->luns[num_filled].lundata[1] =
9550 #ifdef OLDCTLHEADERS
9551 targ_lun_id >> SRLD_BUS_LUN_BITS;
9553 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9556 printf("ctl_report_luns: bogus LUN number %jd, "
9557 "skipping\n", (intmax_t)targ_lun_id);
9560 * According to SPC-3, rev 14 section 6.21:
9562 * "The execution of a REPORT LUNS command to any valid and
9563 * installed logical unit shall clear the REPORTED LUNS DATA
9564 * HAS CHANGED unit attention condition for all logical
9565 * units of that target with respect to the requesting
9566 * initiator. A valid and installed logical unit is one
9567 * having a PERIPHERAL QUALIFIER of 000b in the standard
9568 * INQUIRY data (see 6.4.2)."
9570 * If request_lun is NULL, the LUN this report luns command
9571 * was issued to is either disabled or doesn't exist. In that
9572 * case, we shouldn't clear any pending lun change unit
9575 if (request_lun != NULL) {
9576 mtx_lock(&lun->lun_lock);
9577 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE;
9578 mtx_unlock(&lun->lun_lock);
9581 mtx_unlock(&control_softc->ctl_lock);
9584 * It's quite possible that we've returned fewer LUNs than we allocated
9585 * space for. Trim it.
9587 lun_datalen = sizeof(*lun_data) +
9588 (num_filled * sizeof(struct scsi_report_luns_lundata));
9590 if (lun_datalen < alloc_len) {
9591 ctsio->residual = alloc_len - lun_datalen;
9592 ctsio->kern_data_len = lun_datalen;
9593 ctsio->kern_total_len = lun_datalen;
9595 ctsio->residual = 0;
9596 ctsio->kern_data_len = alloc_len;
9597 ctsio->kern_total_len = alloc_len;
9599 ctsio->kern_data_resid = 0;
9600 ctsio->kern_rel_offset = 0;
9601 ctsio->kern_sg_entries = 0;
9604 * We set this to the actual data length, regardless of how much
9605 * space we actually have to return results. If the user looks at
9606 * this value, he'll know whether or not he allocated enough space
9607 * and reissue the command if necessary. We don't support well
9608 * known logical units, so if the user asks for that, return none.
9610 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9613 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9616 ctsio->scsi_status = SCSI_STATUS_OK;
9618 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9619 ctsio->be_move_done = ctl_config_move_done;
9620 ctl_datamove((union ctl_io *)ctsio);
9626 ctl_request_sense(struct ctl_scsiio *ctsio)
9628 struct scsi_request_sense *cdb;
9629 struct scsi_sense_data *sense_ptr;
9630 struct ctl_lun *lun;
9633 scsi_sense_data_type sense_format;
9635 cdb = (struct scsi_request_sense *)ctsio->cdb;
9637 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9639 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9642 * Determine which sense format the user wants.
9644 if (cdb->byte2 & SRS_DESC)
9645 sense_format = SSD_TYPE_DESC;
9647 sense_format = SSD_TYPE_FIXED;
9649 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9650 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9651 ctsio->kern_sg_entries = 0;
9654 * struct scsi_sense_data, which is currently set to 256 bytes, is
9655 * larger than the largest allowed value for the length field in the
9656 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9658 ctsio->residual = 0;
9659 ctsio->kern_data_len = cdb->length;
9660 ctsio->kern_total_len = cdb->length;
9662 ctsio->kern_data_resid = 0;
9663 ctsio->kern_rel_offset = 0;
9664 ctsio->kern_sg_entries = 0;
9667 * If we don't have a LUN, we don't have any pending sense.
9673 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9675 * Check for pending sense, and then for pending unit attentions.
9676 * Pending sense gets returned first, then pending unit attentions.
9678 mtx_lock(&lun->lun_lock);
9680 if (ctl_is_set(lun->have_ca, initidx)) {
9681 scsi_sense_data_type stored_format;
9684 * Check to see which sense format was used for the stored
9687 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9690 * If the user requested a different sense format than the
9691 * one we stored, then we need to convert it to the other
9692 * format. If we're going from descriptor to fixed format
9693 * sense data, we may lose things in translation, depending
9694 * on what options were used.
9696 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9697 * for some reason we'll just copy it out as-is.
9699 if ((stored_format == SSD_TYPE_FIXED)
9700 && (sense_format == SSD_TYPE_DESC))
9701 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9702 &lun->pending_sense[initidx],
9703 (struct scsi_sense_data_desc *)sense_ptr);
9704 else if ((stored_format == SSD_TYPE_DESC)
9705 && (sense_format == SSD_TYPE_FIXED))
9706 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9707 &lun->pending_sense[initidx],
9708 (struct scsi_sense_data_fixed *)sense_ptr);
9710 memcpy(sense_ptr, &lun->pending_sense[initidx],
9711 ctl_min(sizeof(*sense_ptr),
9712 sizeof(lun->pending_sense[initidx])));
9714 ctl_clear_mask(lun->have_ca, initidx);
9718 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
9719 ctl_ua_type ua_type;
9721 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
9722 sense_ptr, sense_format);
9723 if (ua_type != CTL_UA_NONE)
9726 mtx_unlock(&lun->lun_lock);
9729 * We already have a pending error, return it.
9731 if (have_error != 0) {
9733 * We report the SCSI status as OK, since the status of the
9734 * request sense command itself is OK.
9736 ctsio->scsi_status = SCSI_STATUS_OK;
9739 * We report 0 for the sense length, because we aren't doing
9740 * autosense in this case. We're reporting sense as
9743 ctsio->sense_len = 0;
9744 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9745 ctsio->be_move_done = ctl_config_move_done;
9746 ctl_datamove((union ctl_io *)ctsio);
9748 return (CTL_RETVAL_COMPLETE);
9754 * No sense information to report, so we report that everything is
9757 ctl_set_sense_data(sense_ptr,
9760 /*current_error*/ 1,
9761 /*sense_key*/ SSD_KEY_NO_SENSE,
9766 ctsio->scsi_status = SCSI_STATUS_OK;
9769 * We report 0 for the sense length, because we aren't doing
9770 * autosense in this case. We're reporting sense as parameter data.
9772 ctsio->sense_len = 0;
9773 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9774 ctsio->be_move_done = ctl_config_move_done;
9775 ctl_datamove((union ctl_io *)ctsio);
9777 return (CTL_RETVAL_COMPLETE);
9781 ctl_tur(struct ctl_scsiio *ctsio)
9783 struct ctl_lun *lun;
9785 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9787 CTL_DEBUG_PRINT(("ctl_tur\n"));
9792 ctsio->scsi_status = SCSI_STATUS_OK;
9793 ctsio->io_hdr.status = CTL_SUCCESS;
9795 ctl_done((union ctl_io *)ctsio);
9797 return (CTL_RETVAL_COMPLETE);
9802 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9809 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9811 struct scsi_vpd_supported_pages *pages;
9813 struct ctl_lun *lun;
9815 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9817 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9818 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9819 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9820 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9821 ctsio->kern_sg_entries = 0;
9823 if (sup_page_size < alloc_len) {
9824 ctsio->residual = alloc_len - sup_page_size;
9825 ctsio->kern_data_len = sup_page_size;
9826 ctsio->kern_total_len = sup_page_size;
9828 ctsio->residual = 0;
9829 ctsio->kern_data_len = alloc_len;
9830 ctsio->kern_total_len = alloc_len;
9832 ctsio->kern_data_resid = 0;
9833 ctsio->kern_rel_offset = 0;
9834 ctsio->kern_sg_entries = 0;
9837 * The control device is always connected. The disk device, on the
9838 * other hand, may not be online all the time. Need to change this
9839 * to figure out whether the disk device is actually online or not.
9842 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9843 lun->be_lun->lun_type;
9845 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9847 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9848 /* Supported VPD pages */
9849 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9851 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9852 /* Device Identification */
9853 pages->page_list[2] = SVPD_DEVICE_ID;
9854 /* Mode Page Policy */
9855 pages->page_list[3] = SVPD_MODE_PAGE_POLICY;
9857 pages->page_list[4] = SVPD_SCSI_PORTS;
9858 /* Third-party Copy */
9859 pages->page_list[5] = SVPD_SCSI_TPC;
9861 pages->page_list[6] = SVPD_BLOCK_LIMITS;
9862 /* Block Device Characteristics */
9863 pages->page_list[7] = SVPD_BDC;
9864 /* Logical Block Provisioning */
9865 pages->page_list[8] = SVPD_LBP;
9867 ctsio->scsi_status = SCSI_STATUS_OK;
9869 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9870 ctsio->be_move_done = ctl_config_move_done;
9871 ctl_datamove((union ctl_io *)ctsio);
9873 return (CTL_RETVAL_COMPLETE);
9877 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9879 struct scsi_vpd_unit_serial_number *sn_ptr;
9880 struct ctl_lun *lun;
9882 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9884 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9885 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9886 ctsio->kern_sg_entries = 0;
9888 if (sizeof(*sn_ptr) < alloc_len) {
9889 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9890 ctsio->kern_data_len = sizeof(*sn_ptr);
9891 ctsio->kern_total_len = sizeof(*sn_ptr);
9893 ctsio->residual = 0;
9894 ctsio->kern_data_len = alloc_len;
9895 ctsio->kern_total_len = alloc_len;
9897 ctsio->kern_data_resid = 0;
9898 ctsio->kern_rel_offset = 0;
9899 ctsio->kern_sg_entries = 0;
9902 * The control device is always connected. The disk device, on the
9903 * other hand, may not be online all the time. Need to change this
9904 * to figure out whether the disk device is actually online or not.
9907 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9908 lun->be_lun->lun_type;
9910 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9912 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9913 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9915 * If we don't have a LUN, we just leave the serial number as
9918 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9920 strncpy((char *)sn_ptr->serial_num,
9921 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9923 ctsio->scsi_status = SCSI_STATUS_OK;
9925 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9926 ctsio->be_move_done = ctl_config_move_done;
9927 ctl_datamove((union ctl_io *)ctsio);
9929 return (CTL_RETVAL_COMPLETE);
9934 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9936 struct scsi_vpd_mode_page_policy *mpp_ptr;
9937 struct ctl_lun *lun;
9940 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9942 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9943 sizeof(struct scsi_vpd_mode_page_policy_descr);
9945 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9946 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9947 ctsio->kern_sg_entries = 0;
9949 if (data_len < alloc_len) {
9950 ctsio->residual = alloc_len - data_len;
9951 ctsio->kern_data_len = data_len;
9952 ctsio->kern_total_len = data_len;
9954 ctsio->residual = 0;
9955 ctsio->kern_data_len = alloc_len;
9956 ctsio->kern_total_len = alloc_len;
9958 ctsio->kern_data_resid = 0;
9959 ctsio->kern_rel_offset = 0;
9960 ctsio->kern_sg_entries = 0;
9963 * The control device is always connected. The disk device, on the
9964 * other hand, may not be online all the time.
9967 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9968 lun->be_lun->lun_type;
9970 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9971 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9972 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9973 mpp_ptr->descr[0].page_code = 0x3f;
9974 mpp_ptr->descr[0].subpage_code = 0xff;
9975 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9977 ctsio->scsi_status = SCSI_STATUS_OK;
9978 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9979 ctsio->be_move_done = ctl_config_move_done;
9980 ctl_datamove((union ctl_io *)ctsio);
9982 return (CTL_RETVAL_COMPLETE);
9986 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9988 struct scsi_vpd_device_id *devid_ptr;
9989 struct scsi_vpd_id_descriptor *desc;
9990 struct ctl_softc *ctl_softc;
9991 struct ctl_lun *lun;
9992 struct ctl_port *port;
9996 ctl_softc = control_softc;
9998 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9999 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10001 data_len = sizeof(struct scsi_vpd_device_id) +
10002 sizeof(struct scsi_vpd_id_descriptor) +
10003 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
10004 sizeof(struct scsi_vpd_id_descriptor) +
10005 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
10006 if (lun && lun->lun_devid)
10007 data_len += lun->lun_devid->len;
10008 if (port->port_devid)
10009 data_len += port->port_devid->len;
10010 if (port->target_devid)
10011 data_len += port->target_devid->len;
10013 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10014 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
10015 ctsio->kern_sg_entries = 0;
10017 if (data_len < alloc_len) {
10018 ctsio->residual = alloc_len - data_len;
10019 ctsio->kern_data_len = data_len;
10020 ctsio->kern_total_len = data_len;
10022 ctsio->residual = 0;
10023 ctsio->kern_data_len = alloc_len;
10024 ctsio->kern_total_len = alloc_len;
10026 ctsio->kern_data_resid = 0;
10027 ctsio->kern_rel_offset = 0;
10028 ctsio->kern_sg_entries = 0;
10031 * The control device is always connected. The disk device, on the
10032 * other hand, may not be online all the time.
10035 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10036 lun->be_lun->lun_type;
10038 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10039 devid_ptr->page_code = SVPD_DEVICE_ID;
10040 scsi_ulto2b(data_len - 4, devid_ptr->length);
10042 if (port->port_type == CTL_PORT_FC)
10043 proto = SCSI_PROTO_FC << 4;
10044 else if (port->port_type == CTL_PORT_ISCSI)
10045 proto = SCSI_PROTO_ISCSI << 4;
10047 proto = SCSI_PROTO_SPI << 4;
10048 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
10051 * We're using a LUN association here. i.e., this device ID is a
10052 * per-LUN identifier.
10054 if (lun && lun->lun_devid) {
10055 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
10056 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10057 lun->lun_devid->len);
10061 * This is for the WWPN which is a port association.
10063 if (port->port_devid) {
10064 memcpy(desc, port->port_devid->data, port->port_devid->len);
10065 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10066 port->port_devid->len);
10070 * This is for the Relative Target Port(type 4h) identifier
10072 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10073 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10074 SVPD_ID_TYPE_RELTARG;
10076 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
10077 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10078 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
10081 * This is for the Target Port Group(type 5h) identifier
10083 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10084 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10085 SVPD_ID_TYPE_TPORTGRP;
10087 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
10088 &desc->identifier[2]);
10089 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10090 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
10093 * This is for the Target identifier
10095 if (port->target_devid) {
10096 memcpy(desc, port->target_devid->data, port->target_devid->len);
10099 ctsio->scsi_status = SCSI_STATUS_OK;
10100 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10101 ctsio->be_move_done = ctl_config_move_done;
10102 ctl_datamove((union ctl_io *)ctsio);
10104 return (CTL_RETVAL_COMPLETE);
10108 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
10110 struct ctl_softc *softc = control_softc;
10111 struct scsi_vpd_scsi_ports *sp;
10112 struct scsi_vpd_port_designation *pd;
10113 struct scsi_vpd_port_designation_cont *pdc;
10114 struct ctl_lun *lun;
10115 struct ctl_port *port;
10116 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
10117 int num_target_port_groups, single;
10119 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10121 single = ctl_is_single;
10123 num_target_port_groups = 1;
10125 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
10126 num_target_ports = 0;
10129 mtx_lock(&softc->ctl_lock);
10130 STAILQ_FOREACH(port, &softc->port_list, links) {
10131 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10134 ctl_map_lun_back(port->targ_port, lun->lun) >=
10137 num_target_ports++;
10138 if (port->init_devid)
10139 iid_len += port->init_devid->len;
10140 if (port->port_devid)
10141 id_len += port->port_devid->len;
10143 mtx_unlock(&softc->ctl_lock);
10145 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
10146 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
10147 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
10148 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10149 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
10150 ctsio->kern_sg_entries = 0;
10152 if (data_len < alloc_len) {
10153 ctsio->residual = alloc_len - data_len;
10154 ctsio->kern_data_len = data_len;
10155 ctsio->kern_total_len = data_len;
10157 ctsio->residual = 0;
10158 ctsio->kern_data_len = alloc_len;
10159 ctsio->kern_total_len = alloc_len;
10161 ctsio->kern_data_resid = 0;
10162 ctsio->kern_rel_offset = 0;
10163 ctsio->kern_sg_entries = 0;
10166 * The control device is always connected. The disk device, on the
10167 * other hand, may not be online all the time. Need to change this
10168 * to figure out whether the disk device is actually online or not.
10171 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10172 lun->be_lun->lun_type;
10174 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10176 sp->page_code = SVPD_SCSI_PORTS;
10177 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10179 pd = &sp->design[0];
10181 mtx_lock(&softc->ctl_lock);
10182 if (softc->flags & CTL_FLAG_MASTER_SHELF)
10186 for (g = 0; g < num_target_port_groups; g++) {
10187 STAILQ_FOREACH(port, &softc->port_list, links) {
10188 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10191 ctl_map_lun_back(port->targ_port, lun->lun) >=
10194 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10195 scsi_ulto2b(p, pd->relative_port_id);
10196 if (port->init_devid && g == pg) {
10197 iid_len = port->init_devid->len;
10198 memcpy(pd->initiator_transportid,
10199 port->init_devid->data, port->init_devid->len);
10202 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10203 pdc = (struct scsi_vpd_port_designation_cont *)
10204 (&pd->initiator_transportid[iid_len]);
10205 if (port->port_devid && g == pg) {
10206 id_len = port->port_devid->len;
10207 memcpy(pdc->target_port_descriptors,
10208 port->port_devid->data, port->port_devid->len);
10211 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10212 pd = (struct scsi_vpd_port_designation *)
10213 ((uint8_t *)pdc->target_port_descriptors + id_len);
10216 mtx_unlock(&softc->ctl_lock);
10218 ctsio->scsi_status = SCSI_STATUS_OK;
10219 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10220 ctsio->be_move_done = ctl_config_move_done;
10221 ctl_datamove((union ctl_io *)ctsio);
10223 return (CTL_RETVAL_COMPLETE);
10227 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10229 struct scsi_vpd_block_limits *bl_ptr;
10230 struct ctl_lun *lun;
10233 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10235 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10236 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10237 ctsio->kern_sg_entries = 0;
10239 if (sizeof(*bl_ptr) < alloc_len) {
10240 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10241 ctsio->kern_data_len = sizeof(*bl_ptr);
10242 ctsio->kern_total_len = sizeof(*bl_ptr);
10244 ctsio->residual = 0;
10245 ctsio->kern_data_len = alloc_len;
10246 ctsio->kern_total_len = alloc_len;
10248 ctsio->kern_data_resid = 0;
10249 ctsio->kern_rel_offset = 0;
10250 ctsio->kern_sg_entries = 0;
10253 * The control device is always connected. The disk device, on the
10254 * other hand, may not be online all the time. Need to change this
10255 * to figure out whether the disk device is actually online or not.
10258 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10259 lun->be_lun->lun_type;
10261 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10263 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10264 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
10265 bl_ptr->max_cmp_write_len = 0xff;
10266 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10268 bs = lun->be_lun->blocksize;
10269 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10270 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10271 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10272 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10273 if (lun->be_lun->pblockexp != 0) {
10274 scsi_ulto4b((1 << lun->be_lun->pblockexp),
10275 bl_ptr->opt_unmap_grain);
10276 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff,
10277 bl_ptr->unmap_grain_align);
10281 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10283 ctsio->scsi_status = SCSI_STATUS_OK;
10284 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10285 ctsio->be_move_done = ctl_config_move_done;
10286 ctl_datamove((union ctl_io *)ctsio);
10288 return (CTL_RETVAL_COMPLETE);
10292 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10294 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10295 struct ctl_lun *lun;
10297 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10299 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10300 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10301 ctsio->kern_sg_entries = 0;
10303 if (sizeof(*bdc_ptr) < alloc_len) {
10304 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10305 ctsio->kern_data_len = sizeof(*bdc_ptr);
10306 ctsio->kern_total_len = sizeof(*bdc_ptr);
10308 ctsio->residual = 0;
10309 ctsio->kern_data_len = alloc_len;
10310 ctsio->kern_total_len = alloc_len;
10312 ctsio->kern_data_resid = 0;
10313 ctsio->kern_rel_offset = 0;
10314 ctsio->kern_sg_entries = 0;
10317 * The control device is always connected. The disk device, on the
10318 * other hand, may not be online all the time. Need to change this
10319 * to figure out whether the disk device is actually online or not.
10322 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10323 lun->be_lun->lun_type;
10325 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10326 bdc_ptr->page_code = SVPD_BDC;
10327 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10328 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate);
10329 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10331 ctsio->scsi_status = SCSI_STATUS_OK;
10332 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10333 ctsio->be_move_done = ctl_config_move_done;
10334 ctl_datamove((union ctl_io *)ctsio);
10336 return (CTL_RETVAL_COMPLETE);
10340 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10342 struct scsi_vpd_logical_block_prov *lbp_ptr;
10343 struct ctl_lun *lun;
10345 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10347 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10348 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10349 ctsio->kern_sg_entries = 0;
10351 if (sizeof(*lbp_ptr) < alloc_len) {
10352 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10353 ctsio->kern_data_len = sizeof(*lbp_ptr);
10354 ctsio->kern_total_len = sizeof(*lbp_ptr);
10356 ctsio->residual = 0;
10357 ctsio->kern_data_len = alloc_len;
10358 ctsio->kern_total_len = alloc_len;
10360 ctsio->kern_data_resid = 0;
10361 ctsio->kern_rel_offset = 0;
10362 ctsio->kern_sg_entries = 0;
10365 * The control device is always connected. The disk device, on the
10366 * other hand, may not be online all the time. Need to change this
10367 * to figure out whether the disk device is actually online or not.
10370 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10371 lun->be_lun->lun_type;
10373 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10375 lbp_ptr->page_code = SVPD_LBP;
10376 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10377 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10378 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10379 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10380 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10383 ctsio->scsi_status = SCSI_STATUS_OK;
10384 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10385 ctsio->be_move_done = ctl_config_move_done;
10386 ctl_datamove((union ctl_io *)ctsio);
10388 return (CTL_RETVAL_COMPLETE);
10392 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10394 struct scsi_inquiry *cdb;
10395 struct ctl_lun *lun;
10396 int alloc_len, retval;
10398 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10399 cdb = (struct scsi_inquiry *)ctsio->cdb;
10401 retval = CTL_RETVAL_COMPLETE;
10403 alloc_len = scsi_2btoul(cdb->length);
10405 switch (cdb->page_code) {
10406 case SVPD_SUPPORTED_PAGES:
10407 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10409 case SVPD_UNIT_SERIAL_NUMBER:
10410 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10412 case SVPD_DEVICE_ID:
10413 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10415 case SVPD_MODE_PAGE_POLICY:
10416 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10418 case SVPD_SCSI_PORTS:
10419 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10421 case SVPD_SCSI_TPC:
10422 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10424 case SVPD_BLOCK_LIMITS:
10425 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10428 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10431 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10434 ctl_set_invalid_field(ctsio,
10440 ctl_done((union ctl_io *)ctsio);
10441 retval = CTL_RETVAL_COMPLETE;
10449 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10451 struct scsi_inquiry_data *inq_ptr;
10452 struct scsi_inquiry *cdb;
10453 struct ctl_softc *ctl_softc;
10454 struct ctl_lun *lun;
10456 uint32_t alloc_len;
10457 ctl_port_type port_type;
10459 ctl_softc = control_softc;
10462 * Figure out whether we're talking to a Fibre Channel port or not.
10463 * We treat the ioctl front end, and any SCSI adapters, as packetized
10466 port_type = ctl_softc->ctl_ports[
10467 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10468 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10469 port_type = CTL_PORT_SCSI;
10471 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10472 cdb = (struct scsi_inquiry *)ctsio->cdb;
10473 alloc_len = scsi_2btoul(cdb->length);
10476 * We malloc the full inquiry data size here and fill it
10477 * in. If the user only asks for less, we'll give him
10480 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
10481 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10482 ctsio->kern_sg_entries = 0;
10483 ctsio->kern_data_resid = 0;
10484 ctsio->kern_rel_offset = 0;
10486 if (sizeof(*inq_ptr) < alloc_len) {
10487 ctsio->residual = alloc_len - sizeof(*inq_ptr);
10488 ctsio->kern_data_len = sizeof(*inq_ptr);
10489 ctsio->kern_total_len = sizeof(*inq_ptr);
10491 ctsio->residual = 0;
10492 ctsio->kern_data_len = alloc_len;
10493 ctsio->kern_total_len = alloc_len;
10497 * If we have a LUN configured, report it as connected. Otherwise,
10498 * report that it is offline or no device is supported, depending
10499 * on the value of inquiry_pq_no_lun.
10501 * According to the spec (SPC-4 r34), the peripheral qualifier
10502 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10504 * "A peripheral device having the specified peripheral device type
10505 * is not connected to this logical unit. However, the device
10506 * server is capable of supporting the specified peripheral device
10507 * type on this logical unit."
10509 * According to the same spec, the peripheral qualifier
10510 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10512 * "The device server is not capable of supporting a peripheral
10513 * device on this logical unit. For this peripheral qualifier the
10514 * peripheral device type shall be set to 1Fh. All other peripheral
10515 * device type values are reserved for this peripheral qualifier."
10517 * Given the text, it would seem that we probably want to report that
10518 * the LUN is offline here. There is no LUN connected, but we can
10519 * support a LUN at the given LUN number.
10521 * In the real world, though, it sounds like things are a little
10524 * - Linux, when presented with a LUN with the offline peripheral
10525 * qualifier, will create an sg driver instance for it. So when
10526 * you attach it to CTL, you wind up with a ton of sg driver
10527 * instances. (One for every LUN that Linux bothered to probe.)
10528 * Linux does this despite the fact that it issues a REPORT LUNs
10529 * to LUN 0 to get the inventory of supported LUNs.
10531 * - There is other anecdotal evidence (from Emulex folks) about
10532 * arrays that use the offline peripheral qualifier for LUNs that
10533 * are on the "passive" path in an active/passive array.
10535 * So the solution is provide a hopefully reasonable default
10536 * (return bad/no LUN) and allow the user to change the behavior
10537 * with a tunable/sysctl variable.
10540 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10541 lun->be_lun->lun_type;
10542 else if (ctl_softc->inquiry_pq_no_lun == 0)
10543 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10545 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10547 /* RMB in byte 2 is 0 */
10548 inq_ptr->version = SCSI_REV_SPC4;
10551 * According to SAM-3, even if a device only supports a single
10552 * level of LUN addressing, it should still set the HISUP bit:
10554 * 4.9.1 Logical unit numbers overview
10556 * All logical unit number formats described in this standard are
10557 * hierarchical in structure even when only a single level in that
10558 * hierarchy is used. The HISUP bit shall be set to one in the
10559 * standard INQUIRY data (see SPC-2) when any logical unit number
10560 * format described in this standard is used. Non-hierarchical
10561 * formats are outside the scope of this standard.
10563 * Therefore we set the HiSup bit here.
10565 * The reponse format is 2, per SPC-3.
10567 inq_ptr->response_format = SID_HiSup | 2;
10569 inq_ptr->additional_length =
10570 offsetof(struct scsi_inquiry_data, vendor_specific1) -
10571 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10572 CTL_DEBUG_PRINT(("additional_length = %d\n",
10573 inq_ptr->additional_length));
10575 inq_ptr->spc3_flags = SPC3_SID_3PC;
10576 if (!ctl_is_single)
10577 inq_ptr->spc3_flags |= SPC3_SID_TPGS_IMPLICIT;
10578 /* 16 bit addressing */
10579 if (port_type == CTL_PORT_SCSI)
10580 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10581 /* XXX set the SID_MultiP bit here if we're actually going to
10582 respond on multiple ports */
10583 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10585 /* 16 bit data bus, synchronous transfers */
10586 if (port_type == CTL_PORT_SCSI)
10587 inq_ptr->flags = SID_WBus16 | SID_Sync;
10589 * XXX KDM do we want to support tagged queueing on the control
10593 || (lun->be_lun->lun_type != T_PROCESSOR))
10594 inq_ptr->flags |= SID_CmdQue;
10596 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10597 * We have 8 bytes for the vendor name, and 16 bytes for the device
10598 * name and 4 bytes for the revision.
10600 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10601 "vendor")) == NULL) {
10602 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10604 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10605 strncpy(inq_ptr->vendor, val,
10606 min(sizeof(inq_ptr->vendor), strlen(val)));
10609 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10610 sizeof(inq_ptr->product));
10611 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10612 switch (lun->be_lun->lun_type) {
10614 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10615 sizeof(inq_ptr->product));
10618 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10619 sizeof(inq_ptr->product));
10622 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10623 sizeof(inq_ptr->product));
10627 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10628 strncpy(inq_ptr->product, val,
10629 min(sizeof(inq_ptr->product), strlen(val)));
10633 * XXX make this a macro somewhere so it automatically gets
10634 * incremented when we make changes.
10636 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10637 "revision")) == NULL) {
10638 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10640 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10641 strncpy(inq_ptr->revision, val,
10642 min(sizeof(inq_ptr->revision), strlen(val)));
10646 * For parallel SCSI, we support double transition and single
10647 * transition clocking. We also support QAS (Quick Arbitration
10648 * and Selection) and Information Unit transfers on both the
10649 * control and array devices.
10651 if (port_type == CTL_PORT_SCSI)
10652 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10655 /* SAM-5 (no version claimed) */
10656 scsi_ulto2b(0x00A0, inq_ptr->version1);
10657 /* SPC-4 (no version claimed) */
10658 scsi_ulto2b(0x0460, inq_ptr->version2);
10659 if (port_type == CTL_PORT_FC) {
10660 /* FCP-2 ANSI INCITS.350:2003 */
10661 scsi_ulto2b(0x0917, inq_ptr->version3);
10662 } else if (port_type == CTL_PORT_SCSI) {
10663 /* SPI-4 ANSI INCITS.362:200x */
10664 scsi_ulto2b(0x0B56, inq_ptr->version3);
10665 } else if (port_type == CTL_PORT_ISCSI) {
10666 /* iSCSI (no version claimed) */
10667 scsi_ulto2b(0x0960, inq_ptr->version3);
10668 } else if (port_type == CTL_PORT_SAS) {
10669 /* SAS (no version claimed) */
10670 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10674 /* SBC-3 (no version claimed) */
10675 scsi_ulto2b(0x04C0, inq_ptr->version4);
10677 switch (lun->be_lun->lun_type) {
10679 /* SBC-3 (no version claimed) */
10680 scsi_ulto2b(0x04C0, inq_ptr->version4);
10688 ctsio->scsi_status = SCSI_STATUS_OK;
10689 if (ctsio->kern_data_len > 0) {
10690 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10691 ctsio->be_move_done = ctl_config_move_done;
10692 ctl_datamove((union ctl_io *)ctsio);
10694 ctsio->io_hdr.status = CTL_SUCCESS;
10695 ctl_done((union ctl_io *)ctsio);
10698 return (CTL_RETVAL_COMPLETE);
10702 ctl_inquiry(struct ctl_scsiio *ctsio)
10704 struct scsi_inquiry *cdb;
10707 cdb = (struct scsi_inquiry *)ctsio->cdb;
10711 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10714 * Right now, we don't support the CmdDt inquiry information.
10715 * This would be nice to support in the future. When we do
10716 * support it, we should change this test so that it checks to make
10717 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
10720 if (((cdb->byte2 & SI_EVPD)
10721 && (cdb->byte2 & SI_CMDDT)))
10723 if (cdb->byte2 & SI_CMDDT) {
10725 * Point to the SI_CMDDT bit. We might change this
10726 * when we support SI_CMDDT, but since both bits would be
10727 * "wrong", this should probably just stay as-is then.
10729 ctl_set_invalid_field(ctsio,
10735 ctl_done((union ctl_io *)ctsio);
10736 return (CTL_RETVAL_COMPLETE);
10738 if (cdb->byte2 & SI_EVPD)
10739 retval = ctl_inquiry_evpd(ctsio);
10741 else if (cdb->byte2 & SI_CMDDT)
10742 retval = ctl_inquiry_cmddt(ctsio);
10745 retval = ctl_inquiry_std(ctsio);
10751 * For known CDB types, parse the LBA and length.
10754 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
10756 if (io->io_hdr.io_type != CTL_IO_SCSI)
10759 switch (io->scsiio.cdb[0]) {
10760 case COMPARE_AND_WRITE: {
10761 struct scsi_compare_and_write *cdb;
10763 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10765 *lba = scsi_8btou64(cdb->addr);
10766 *len = cdb->length;
10771 struct scsi_rw_6 *cdb;
10773 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10775 *lba = scsi_3btoul(cdb->addr);
10776 /* only 5 bits are valid in the most significant address byte */
10778 *len = cdb->length;
10783 struct scsi_rw_10 *cdb;
10785 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10787 *lba = scsi_4btoul(cdb->addr);
10788 *len = scsi_2btoul(cdb->length);
10791 case WRITE_VERIFY_10: {
10792 struct scsi_write_verify_10 *cdb;
10794 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10796 *lba = scsi_4btoul(cdb->addr);
10797 *len = scsi_2btoul(cdb->length);
10802 struct scsi_rw_12 *cdb;
10804 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10806 *lba = scsi_4btoul(cdb->addr);
10807 *len = scsi_4btoul(cdb->length);
10810 case WRITE_VERIFY_12: {
10811 struct scsi_write_verify_12 *cdb;
10813 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10815 *lba = scsi_4btoul(cdb->addr);
10816 *len = scsi_4btoul(cdb->length);
10821 struct scsi_rw_16 *cdb;
10823 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10825 *lba = scsi_8btou64(cdb->addr);
10826 *len = scsi_4btoul(cdb->length);
10829 case WRITE_VERIFY_16: {
10830 struct scsi_write_verify_16 *cdb;
10832 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10835 *lba = scsi_8btou64(cdb->addr);
10836 *len = scsi_4btoul(cdb->length);
10839 case WRITE_SAME_10: {
10840 struct scsi_write_same_10 *cdb;
10842 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10844 *lba = scsi_4btoul(cdb->addr);
10845 *len = scsi_2btoul(cdb->length);
10848 case WRITE_SAME_16: {
10849 struct scsi_write_same_16 *cdb;
10851 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10853 *lba = scsi_8btou64(cdb->addr);
10854 *len = scsi_4btoul(cdb->length);
10858 struct scsi_verify_10 *cdb;
10860 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10862 *lba = scsi_4btoul(cdb->addr);
10863 *len = scsi_2btoul(cdb->length);
10867 struct scsi_verify_12 *cdb;
10869 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10871 *lba = scsi_4btoul(cdb->addr);
10872 *len = scsi_4btoul(cdb->length);
10876 struct scsi_verify_16 *cdb;
10878 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10880 *lba = scsi_8btou64(cdb->addr);
10881 *len = scsi_4btoul(cdb->length);
10886 break; /* NOTREACHED */
10893 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10895 uint64_t endlba1, endlba2;
10897 endlba1 = lba1 + len1 - 1;
10898 endlba2 = lba2 + len2 - 1;
10900 if ((endlba1 < lba2)
10901 || (endlba2 < lba1))
10902 return (CTL_ACTION_PASS);
10904 return (CTL_ACTION_BLOCK);
10908 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10910 uint64_t lba1, lba2;
10911 uint32_t len1, len2;
10914 retval = ctl_get_lba_len(io1, &lba1, &len1);
10916 return (CTL_ACTION_ERROR);
10918 retval = ctl_get_lba_len(io2, &lba2, &len2);
10920 return (CTL_ACTION_ERROR);
10922 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10926 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10928 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10929 ctl_serialize_action *serialize_row;
10932 * The initiator attempted multiple untagged commands at the same
10933 * time. Can't do that.
10935 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10936 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10937 && ((pending_io->io_hdr.nexus.targ_port ==
10938 ooa_io->io_hdr.nexus.targ_port)
10939 && (pending_io->io_hdr.nexus.initid.id ==
10940 ooa_io->io_hdr.nexus.initid.id))
10941 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10942 return (CTL_ACTION_OVERLAP);
10945 * The initiator attempted to send multiple tagged commands with
10946 * the same ID. (It's fine if different initiators have the same
10949 * Even if all of those conditions are true, we don't kill the I/O
10950 * if the command ahead of us has been aborted. We won't end up
10951 * sending it to the FETD, and it's perfectly legal to resend a
10952 * command with the same tag number as long as the previous
10953 * instance of this tag number has been aborted somehow.
10955 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10956 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10957 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10958 && ((pending_io->io_hdr.nexus.targ_port ==
10959 ooa_io->io_hdr.nexus.targ_port)
10960 && (pending_io->io_hdr.nexus.initid.id ==
10961 ooa_io->io_hdr.nexus.initid.id))
10962 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10963 return (CTL_ACTION_OVERLAP_TAG);
10966 * If we get a head of queue tag, SAM-3 says that we should
10967 * immediately execute it.
10969 * What happens if this command would normally block for some other
10970 * reason? e.g. a request sense with a head of queue tag
10971 * immediately after a write. Normally that would block, but this
10972 * will result in its getting executed immediately...
10974 * We currently return "pass" instead of "skip", so we'll end up
10975 * going through the rest of the queue to check for overlapped tags.
10977 * XXX KDM check for other types of blockage first??
10979 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10980 return (CTL_ACTION_PASS);
10983 * Ordered tags have to block until all items ahead of them
10984 * have completed. If we get called with an ordered tag, we always
10985 * block, if something else is ahead of us in the queue.
10987 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10988 return (CTL_ACTION_BLOCK);
10991 * Simple tags get blocked until all head of queue and ordered tags
10992 * ahead of them have completed. I'm lumping untagged commands in
10993 * with simple tags here. XXX KDM is that the right thing to do?
10995 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10996 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10997 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10998 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10999 return (CTL_ACTION_BLOCK);
11001 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio);
11002 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio);
11004 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11006 switch (serialize_row[pending_entry->seridx]) {
11007 case CTL_SER_BLOCK:
11008 return (CTL_ACTION_BLOCK);
11009 break; /* NOTREACHED */
11010 case CTL_SER_EXTENT:
11011 return (ctl_extent_check(pending_io, ooa_io));
11012 break; /* NOTREACHED */
11014 return (CTL_ACTION_PASS);
11015 break; /* NOTREACHED */
11017 return (CTL_ACTION_SKIP);
11020 panic("invalid serialization value %d",
11021 serialize_row[pending_entry->seridx]);
11022 break; /* NOTREACHED */
11025 return (CTL_ACTION_ERROR);
11029 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11031 * - pending_io is generally either incoming, or on the blocked queue
11032 * - starting I/O is the I/O we want to start the check with.
11035 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11036 union ctl_io *starting_io)
11038 union ctl_io *ooa_io;
11041 mtx_assert(&lun->lun_lock, MA_OWNED);
11044 * Run back along the OOA queue, starting with the current
11045 * blocked I/O and going through every I/O before it on the
11046 * queue. If starting_io is NULL, we'll just end up returning
11049 for (ooa_io = starting_io; ooa_io != NULL;
11050 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11054 * This routine just checks to see whether
11055 * cur_blocked is blocked by ooa_io, which is ahead
11056 * of it in the queue. It doesn't queue/dequeue
11059 action = ctl_check_for_blockage(pending_io, ooa_io);
11061 case CTL_ACTION_BLOCK:
11062 case CTL_ACTION_OVERLAP:
11063 case CTL_ACTION_OVERLAP_TAG:
11064 case CTL_ACTION_SKIP:
11065 case CTL_ACTION_ERROR:
11067 break; /* NOTREACHED */
11068 case CTL_ACTION_PASS:
11071 panic("invalid action %d", action);
11072 break; /* NOTREACHED */
11076 return (CTL_ACTION_PASS);
11081 * - An I/O has just completed, and has been removed from the per-LUN OOA
11082 * queue, so some items on the blocked queue may now be unblocked.
11085 ctl_check_blocked(struct ctl_lun *lun)
11087 union ctl_io *cur_blocked, *next_blocked;
11089 mtx_assert(&lun->lun_lock, MA_OWNED);
11092 * Run forward from the head of the blocked queue, checking each
11093 * entry against the I/Os prior to it on the OOA queue to see if
11094 * there is still any blockage.
11096 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11097 * with our removing a variable on it while it is traversing the
11100 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11101 cur_blocked != NULL; cur_blocked = next_blocked) {
11102 union ctl_io *prev_ooa;
11105 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11108 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11109 ctl_ooaq, ooa_links);
11112 * If cur_blocked happens to be the first item in the OOA
11113 * queue now, prev_ooa will be NULL, and the action
11114 * returned will just be CTL_ACTION_PASS.
11116 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11119 case CTL_ACTION_BLOCK:
11120 /* Nothing to do here, still blocked */
11122 case CTL_ACTION_OVERLAP:
11123 case CTL_ACTION_OVERLAP_TAG:
11125 * This shouldn't happen! In theory we've already
11126 * checked this command for overlap...
11129 case CTL_ACTION_PASS:
11130 case CTL_ACTION_SKIP: {
11131 struct ctl_softc *softc;
11132 const struct ctl_cmd_entry *entry;
11137 * The skip case shouldn't happen, this transaction
11138 * should have never made it onto the blocked queue.
11141 * This I/O is no longer blocked, we can remove it
11142 * from the blocked queue. Since this is a TAILQ
11143 * (doubly linked list), we can do O(1) removals
11144 * from any place on the list.
11146 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11148 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11150 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
11152 * Need to send IO back to original side to
11155 union ctl_ha_msg msg_info;
11157 msg_info.hdr.original_sc =
11158 cur_blocked->io_hdr.original_sc;
11159 msg_info.hdr.serializing_sc = cur_blocked;
11160 msg_info.hdr.msg_type = CTL_MSG_R2R;
11161 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11162 &msg_info, sizeof(msg_info), 0)) >
11163 CTL_HA_STATUS_SUCCESS) {
11164 printf("CTL:Check Blocked error from "
11165 "ctl_ha_msg_send %d\n",
11170 entry = ctl_get_cmd_entry(&cur_blocked->scsiio);
11171 softc = control_softc;
11173 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
11176 * Check this I/O for LUN state changes that may
11177 * have happened while this command was blocked.
11178 * The LUN state may have been changed by a command
11179 * ahead of us in the queue, so we need to re-check
11180 * for any states that can be caused by SCSI
11183 if (ctl_scsiio_lun_check(softc, lun, entry,
11184 &cur_blocked->scsiio) == 0) {
11185 cur_blocked->io_hdr.flags |=
11186 CTL_FLAG_IS_WAS_ON_RTR;
11187 ctl_enqueue_rtr(cur_blocked);
11189 ctl_done(cur_blocked);
11194 * This probably shouldn't happen -- we shouldn't
11195 * get CTL_ACTION_ERROR, or anything else.
11201 return (CTL_RETVAL_COMPLETE);
11205 * This routine (with one exception) checks LUN flags that can be set by
11206 * commands ahead of us in the OOA queue. These flags have to be checked
11207 * when a command initially comes in, and when we pull a command off the
11208 * blocked queue and are preparing to execute it. The reason we have to
11209 * check these flags for commands on the blocked queue is that the LUN
11210 * state may have been changed by a command ahead of us while we're on the
11213 * Ordering is somewhat important with these checks, so please pay
11214 * careful attention to the placement of any new checks.
11217 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
11218 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11224 mtx_assert(&lun->lun_lock, MA_OWNED);
11227 * If this shelf is a secondary shelf controller, we have to reject
11228 * any media access commands.
11231 /* No longer needed for HA */
11232 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
11233 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
11234 ctl_set_lun_standby(ctsio);
11241 * Check for a reservation conflict. If this command isn't allowed
11242 * even on reserved LUNs, and if this initiator isn't the one who
11243 * reserved us, reject the command with a reservation conflict.
11245 if ((lun->flags & CTL_LUN_RESERVED)
11246 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11247 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
11248 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
11249 || (ctsio->io_hdr.nexus.targ_target.id !=
11250 lun->rsv_nexus.targ_target.id)) {
11251 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11252 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11258 if ( (lun->flags & CTL_LUN_PR_RESERVED)
11259 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
11262 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11264 * if we aren't registered or it's a res holder type
11265 * reservation and this isn't the res holder then set a
11267 * NOTE: Commands which might be allowed on write exclusive
11268 * type reservations are checked in the particular command
11269 * for a conflict. Read and SSU are the only ones.
11271 if (!lun->per_res[residx].registered
11272 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11273 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11274 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11281 if ((lun->flags & CTL_LUN_OFFLINE)
11282 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11283 ctl_set_lun_not_ready(ctsio);
11289 * If the LUN is stopped, see if this particular command is allowed
11290 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11292 if ((lun->flags & CTL_LUN_STOPPED)
11293 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11294 /* "Logical unit not ready, initializing cmd. required" */
11295 ctl_set_lun_stopped(ctsio);
11300 if ((lun->flags & CTL_LUN_INOPERABLE)
11301 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11302 /* "Medium format corrupted" */
11303 ctl_set_medium_format_corrupted(ctsio);
11314 ctl_failover_io(union ctl_io *io, int have_lock)
11316 ctl_set_busy(&io->scsiio);
11323 struct ctl_lun *lun;
11324 struct ctl_softc *ctl_softc;
11325 union ctl_io *next_io, *pending_io;
11330 ctl_softc = control_softc;
11332 mtx_lock(&ctl_softc->ctl_lock);
11334 * Remove any cmds from the other SC from the rtr queue. These
11335 * will obviously only be for LUNs for which we're the primary.
11336 * We can't send status or get/send data for these commands.
11337 * Since they haven't been executed yet, we can just remove them.
11338 * We'll either abort them or delete them below, depending on
11339 * which HA mode we're in.
11342 mtx_lock(&ctl_softc->queue_lock);
11343 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11344 io != NULL; io = next_io) {
11345 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11346 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11347 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11348 ctl_io_hdr, links);
11350 mtx_unlock(&ctl_softc->queue_lock);
11353 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11354 lun = ctl_softc->ctl_luns[lun_idx];
11359 * Processor LUNs are primary on both sides.
11360 * XXX will this always be true?
11362 if (lun->be_lun->lun_type == T_PROCESSOR)
11365 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11366 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11367 printf("FAILOVER: primary lun %d\n", lun_idx);
11369 * Remove all commands from the other SC. First from the
11370 * blocked queue then from the ooa queue. Once we have
11371 * removed them. Call ctl_check_blocked to see if there
11372 * is anything that can run.
11374 for (io = (union ctl_io *)TAILQ_FIRST(
11375 &lun->blocked_queue); io != NULL; io = next_io) {
11377 next_io = (union ctl_io *)TAILQ_NEXT(
11378 &io->io_hdr, blocked_links);
11380 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11381 TAILQ_REMOVE(&lun->blocked_queue,
11382 &io->io_hdr,blocked_links);
11383 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11384 TAILQ_REMOVE(&lun->ooa_queue,
11385 &io->io_hdr, ooa_links);
11391 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11392 io != NULL; io = next_io) {
11394 next_io = (union ctl_io *)TAILQ_NEXT(
11395 &io->io_hdr, ooa_links);
11397 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11399 TAILQ_REMOVE(&lun->ooa_queue,
11406 ctl_check_blocked(lun);
11407 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11408 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11410 printf("FAILOVER: primary lun %d\n", lun_idx);
11412 * Abort all commands from the other SC. We can't
11413 * send status back for them now. These should get
11414 * cleaned up when they are completed or come out
11415 * for a datamove operation.
11417 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11418 io != NULL; io = next_io) {
11419 next_io = (union ctl_io *)TAILQ_NEXT(
11420 &io->io_hdr, ooa_links);
11422 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11423 io->io_hdr.flags |= CTL_FLAG_ABORT;
11425 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11426 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11428 printf("FAILOVER: secondary lun %d\n", lun_idx);
11430 lun->flags |= CTL_LUN_PRIMARY_SC;
11433 * We send all I/O that was sent to this controller
11434 * and redirected to the other side back with
11435 * busy status, and have the initiator retry it.
11436 * Figuring out how much data has been transferred,
11437 * etc. and picking up where we left off would be
11440 * XXX KDM need to remove I/O from the blocked
11443 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11444 &lun->ooa_queue); pending_io != NULL;
11445 pending_io = next_io) {
11447 next_io = (union ctl_io *)TAILQ_NEXT(
11448 &pending_io->io_hdr, ooa_links);
11450 pending_io->io_hdr.flags &=
11451 ~CTL_FLAG_SENT_2OTHER_SC;
11453 if (pending_io->io_hdr.flags &
11454 CTL_FLAG_IO_ACTIVE) {
11455 pending_io->io_hdr.flags |=
11458 ctl_set_busy(&pending_io->scsiio);
11459 ctl_done(pending_io);
11464 * Build Unit Attention
11466 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11467 lun->pending_ua[i] |=
11468 CTL_UA_ASYM_ACC_CHANGE;
11470 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11471 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11472 printf("FAILOVER: secondary lun %d\n", lun_idx);
11474 * if the first io on the OOA is not on the RtR queue
11477 lun->flags |= CTL_LUN_PRIMARY_SC;
11479 pending_io = (union ctl_io *)TAILQ_FIRST(
11481 if (pending_io==NULL) {
11482 printf("Nothing on OOA queue\n");
11486 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11487 if ((pending_io->io_hdr.flags &
11488 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11489 pending_io->io_hdr.flags |=
11490 CTL_FLAG_IS_WAS_ON_RTR;
11491 ctl_enqueue_rtr(pending_io);
11496 printf("Tag 0x%04x is running\n",
11497 pending_io->scsiio.tag_num);
11501 next_io = (union ctl_io *)TAILQ_NEXT(
11502 &pending_io->io_hdr, ooa_links);
11503 for (pending_io=next_io; pending_io != NULL;
11504 pending_io = next_io) {
11505 pending_io->io_hdr.flags &=
11506 ~CTL_FLAG_SENT_2OTHER_SC;
11507 next_io = (union ctl_io *)TAILQ_NEXT(
11508 &pending_io->io_hdr, ooa_links);
11509 if (pending_io->io_hdr.flags &
11510 CTL_FLAG_IS_WAS_ON_RTR) {
11512 printf("Tag 0x%04x is running\n",
11513 pending_io->scsiio.tag_num);
11518 switch (ctl_check_ooa(lun, pending_io,
11519 (union ctl_io *)TAILQ_PREV(
11520 &pending_io->io_hdr, ctl_ooaq,
11523 case CTL_ACTION_BLOCK:
11524 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11525 &pending_io->io_hdr,
11527 pending_io->io_hdr.flags |=
11530 case CTL_ACTION_PASS:
11531 case CTL_ACTION_SKIP:
11532 pending_io->io_hdr.flags |=
11533 CTL_FLAG_IS_WAS_ON_RTR;
11534 ctl_enqueue_rtr(pending_io);
11536 case CTL_ACTION_OVERLAP:
11537 ctl_set_overlapped_cmd(
11538 (struct ctl_scsiio *)pending_io);
11539 ctl_done(pending_io);
11541 case CTL_ACTION_OVERLAP_TAG:
11542 ctl_set_overlapped_tag(
11543 (struct ctl_scsiio *)pending_io,
11544 pending_io->scsiio.tag_num & 0xff);
11545 ctl_done(pending_io);
11547 case CTL_ACTION_ERROR:
11549 ctl_set_internal_failure(
11550 (struct ctl_scsiio *)pending_io,
11553 ctl_done(pending_io);
11559 * Build Unit Attention
11561 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11562 lun->pending_ua[i] |=
11563 CTL_UA_ASYM_ACC_CHANGE;
11566 panic("Unhandled HA mode failover, LUN flags = %#x, "
11567 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11571 mtx_unlock(&ctl_softc->ctl_lock);
11575 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11577 struct ctl_lun *lun;
11578 const struct ctl_cmd_entry *entry;
11579 uint32_t initidx, targ_lun;
11586 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11587 if ((targ_lun < CTL_MAX_LUNS)
11588 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
11589 lun = ctl_softc->ctl_luns[targ_lun];
11591 * If the LUN is invalid, pretend that it doesn't exist.
11592 * It will go away as soon as all pending I/O has been
11595 if (lun->flags & CTL_LUN_DISABLED) {
11598 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11599 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11601 if (lun->be_lun->lun_type == T_PROCESSOR) {
11602 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11606 * Every I/O goes into the OOA queue for a
11607 * particular LUN, and stays there until completion.
11609 mtx_lock(&lun->lun_lock);
11610 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11614 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11615 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11618 /* Get command entry and return error if it is unsuppotyed. */
11619 entry = ctl_validate_command(ctsio);
11620 if (entry == NULL) {
11622 mtx_unlock(&lun->lun_lock);
11626 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11627 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11630 * Check to see whether we can send this command to LUNs that don't
11631 * exist. This should pretty much only be the case for inquiry
11632 * and request sense. Further checks, below, really require having
11633 * a LUN, so we can't really check the command anymore. Just put
11634 * it on the rtr queue.
11637 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11638 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11639 ctl_enqueue_rtr((union ctl_io *)ctsio);
11643 ctl_set_unsupported_lun(ctsio);
11644 ctl_done((union ctl_io *)ctsio);
11645 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11649 * Make sure we support this particular command on this LUN.
11650 * e.g., we don't support writes to the control LUN.
11652 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11653 mtx_unlock(&lun->lun_lock);
11654 ctl_set_invalid_opcode(ctsio);
11655 ctl_done((union ctl_io *)ctsio);
11660 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11664 * If we've got a request sense, it'll clear the contingent
11665 * allegiance condition. Otherwise, if we have a CA condition for
11666 * this initiator, clear it, because it sent down a command other
11667 * than request sense.
11669 if ((ctsio->cdb[0] != REQUEST_SENSE)
11670 && (ctl_is_set(lun->have_ca, initidx)))
11671 ctl_clear_mask(lun->have_ca, initidx);
11675 * If the command has this flag set, it handles its own unit
11676 * attention reporting, we shouldn't do anything. Otherwise we
11677 * check for any pending unit attentions, and send them back to the
11678 * initiator. We only do this when a command initially comes in,
11679 * not when we pull it off the blocked queue.
11681 * According to SAM-3, section 5.3.2, the order that things get
11682 * presented back to the host is basically unit attentions caused
11683 * by some sort of reset event, busy status, reservation conflicts
11684 * or task set full, and finally any other status.
11686 * One issue here is that some of the unit attentions we report
11687 * don't fall into the "reset" category (e.g. "reported luns data
11688 * has changed"). So reporting it here, before the reservation
11689 * check, may be technically wrong. I guess the only thing to do
11690 * would be to check for and report the reset events here, and then
11691 * check for the other unit attention types after we check for a
11692 * reservation conflict.
11694 * XXX KDM need to fix this
11696 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11697 ctl_ua_type ua_type;
11699 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
11700 scsi_sense_data_type sense_format;
11703 sense_format = (lun->flags &
11704 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11707 sense_format = SSD_TYPE_FIXED;
11709 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
11710 &ctsio->sense_data, sense_format);
11711 if (ua_type != CTL_UA_NONE) {
11712 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11713 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11715 ctsio->sense_len = SSD_FULL_SIZE;
11716 mtx_unlock(&lun->lun_lock);
11717 ctl_done((union ctl_io *)ctsio);
11724 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11725 mtx_unlock(&lun->lun_lock);
11726 ctl_done((union ctl_io *)ctsio);
11731 * XXX CHD this is where we want to send IO to other side if
11732 * this LUN is secondary on this SC. We will need to make a copy
11733 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11734 * the copy we send as FROM_OTHER.
11735 * We also need to stuff the address of the original IO so we can
11736 * find it easily. Something similar will need be done on the other
11737 * side so when we are done we can find the copy.
11739 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11740 union ctl_ha_msg msg_info;
11743 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11745 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11746 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11748 printf("1. ctsio %p\n", ctsio);
11750 msg_info.hdr.serializing_sc = NULL;
11751 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11752 msg_info.scsi.tag_num = ctsio->tag_num;
11753 msg_info.scsi.tag_type = ctsio->tag_type;
11754 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11756 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11758 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11759 (void *)&msg_info, sizeof(msg_info), 0)) >
11760 CTL_HA_STATUS_SUCCESS) {
11761 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11763 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11766 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11771 * XXX KDM this I/O is off the incoming queue, but hasn't
11772 * been inserted on any other queue. We may need to come
11773 * up with a holding queue while we wait for serialization
11774 * so that we have an idea of what we're waiting for from
11777 mtx_unlock(&lun->lun_lock);
11781 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11782 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11783 ctl_ooaq, ooa_links))) {
11784 case CTL_ACTION_BLOCK:
11785 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11786 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11788 mtx_unlock(&lun->lun_lock);
11790 case CTL_ACTION_PASS:
11791 case CTL_ACTION_SKIP:
11792 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11793 mtx_unlock(&lun->lun_lock);
11794 ctl_enqueue_rtr((union ctl_io *)ctsio);
11796 case CTL_ACTION_OVERLAP:
11797 mtx_unlock(&lun->lun_lock);
11798 ctl_set_overlapped_cmd(ctsio);
11799 ctl_done((union ctl_io *)ctsio);
11801 case CTL_ACTION_OVERLAP_TAG:
11802 mtx_unlock(&lun->lun_lock);
11803 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11804 ctl_done((union ctl_io *)ctsio);
11806 case CTL_ACTION_ERROR:
11808 mtx_unlock(&lun->lun_lock);
11809 ctl_set_internal_failure(ctsio,
11811 /*retry_count*/ 0);
11812 ctl_done((union ctl_io *)ctsio);
11818 const struct ctl_cmd_entry *
11819 ctl_get_cmd_entry(struct ctl_scsiio *ctsio)
11821 const struct ctl_cmd_entry *entry;
11822 int service_action;
11824 entry = &ctl_cmd_table[ctsio->cdb[0]];
11825 if (entry->flags & CTL_CMD_FLAG_SA5) {
11826 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11827 entry = &((const struct ctl_cmd_entry *)
11828 entry->execute)[service_action];
11833 const struct ctl_cmd_entry *
11834 ctl_validate_command(struct ctl_scsiio *ctsio)
11836 const struct ctl_cmd_entry *entry;
11840 entry = ctl_get_cmd_entry(ctsio);
11841 if (entry->execute == NULL) {
11842 ctl_set_invalid_opcode(ctsio);
11843 ctl_done((union ctl_io *)ctsio);
11846 KASSERT(entry->length > 0,
11847 ("Not defined length for command 0x%02x/0x%02x",
11848 ctsio->cdb[0], ctsio->cdb[1]));
11849 for (i = 1; i < entry->length; i++) {
11850 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11853 ctl_set_invalid_field(ctsio,
11858 /*bit*/ fls(diff) - 1);
11859 ctl_done((union ctl_io *)ctsio);
11866 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11869 switch (lun_type) {
11871 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11872 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11876 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11877 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11887 ctl_scsiio(struct ctl_scsiio *ctsio)
11890 const struct ctl_cmd_entry *entry;
11892 retval = CTL_RETVAL_COMPLETE;
11894 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11896 entry = ctl_get_cmd_entry(ctsio);
11899 * If this I/O has been aborted, just send it straight to
11900 * ctl_done() without executing it.
11902 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11903 ctl_done((union ctl_io *)ctsio);
11908 * All the checks should have been handled by ctl_scsiio_precheck().
11909 * We should be clear now to just execute the I/O.
11911 retval = entry->execute(ctsio);
11918 * Since we only implement one target right now, a bus reset simply resets
11919 * our single target.
11922 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11924 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11928 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11929 ctl_ua_type ua_type)
11931 struct ctl_lun *lun;
11934 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11935 union ctl_ha_msg msg_info;
11937 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11938 msg_info.hdr.nexus = io->io_hdr.nexus;
11939 if (ua_type==CTL_UA_TARG_RESET)
11940 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11942 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11943 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11944 msg_info.hdr.original_sc = NULL;
11945 msg_info.hdr.serializing_sc = NULL;
11946 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11947 (void *)&msg_info, sizeof(msg_info), 0)) {
11952 mtx_lock(&ctl_softc->ctl_lock);
11953 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11954 retval += ctl_lun_reset(lun, io, ua_type);
11955 mtx_unlock(&ctl_softc->ctl_lock);
11961 * The LUN should always be set. The I/O is optional, and is used to
11962 * distinguish between I/Os sent by this initiator, and by other
11963 * initiators. We set unit attention for initiators other than this one.
11964 * SAM-3 is vague on this point. It does say that a unit attention should
11965 * be established for other initiators when a LUN is reset (see section
11966 * 5.7.3), but it doesn't specifically say that the unit attention should
11967 * be established for this particular initiator when a LUN is reset. Here
11968 * is the relevant text, from SAM-3 rev 8:
11970 * 5.7.2 When a SCSI initiator port aborts its own tasks
11972 * When a SCSI initiator port causes its own task(s) to be aborted, no
11973 * notification that the task(s) have been aborted shall be returned to
11974 * the SCSI initiator port other than the completion response for the
11975 * command or task management function action that caused the task(s) to
11976 * be aborted and notification(s) associated with related effects of the
11977 * action (e.g., a reset unit attention condition).
11979 * XXX KDM for now, we're setting unit attention for all initiators.
11982 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11986 uint32_t initindex;
11990 mtx_lock(&lun->lun_lock);
11992 * Run through the OOA queue and abort each I/O.
11995 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11997 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11998 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11999 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
12003 * This version sets unit attention for every
12006 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12007 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
12008 if (initindex == i)
12010 lun->pending_ua[i] |= ua_type;
12015 * A reset (any kind, really) clears reservations established with
12016 * RESERVE/RELEASE. It does not clear reservations established
12017 * with PERSISTENT RESERVE OUT, but we don't support that at the
12018 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
12019 * reservations made with the RESERVE/RELEASE commands, because
12020 * those commands are obsolete in SPC-3.
12022 lun->flags &= ~CTL_LUN_RESERVED;
12024 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
12026 ctl_clear_mask(lun->have_ca, i);
12028 lun->pending_ua[i] |= ua_type;
12030 mtx_unlock(&lun->lun_lock);
12036 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12042 mtx_assert(&lun->lun_lock, MA_OWNED);
12045 * Run through the OOA queue and attempt to find the given I/O.
12046 * The target port, initiator ID, tag type and tag number have to
12047 * match the values that we got from the initiator. If we have an
12048 * untagged command to abort, simply abort the first untagged command
12049 * we come to. We only allow one untagged command at a time of course.
12051 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12052 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12054 if ((targ_port == UINT32_MAX ||
12055 targ_port == xio->io_hdr.nexus.targ_port) &&
12056 (init_id == UINT32_MAX ||
12057 init_id == xio->io_hdr.nexus.initid.id)) {
12058 if (targ_port != xio->io_hdr.nexus.targ_port ||
12059 init_id != xio->io_hdr.nexus.initid.id)
12060 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12061 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12063 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12064 union ctl_ha_msg msg_info;
12066 msg_info.hdr.nexus = xio->io_hdr.nexus;
12067 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12068 msg_info.task.tag_num = xio->scsiio.tag_num;
12069 msg_info.task.tag_type = xio->scsiio.tag_type;
12070 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12071 msg_info.hdr.original_sc = NULL;
12072 msg_info.hdr.serializing_sc = NULL;
12073 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12074 (void *)&msg_info, sizeof(msg_info), 0);
12082 ctl_abort_task_set(union ctl_io *io)
12084 struct ctl_softc *softc = control_softc;
12085 struct ctl_lun *lun;
12091 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12092 mtx_lock(&softc->ctl_lock);
12093 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
12094 lun = softc->ctl_luns[targ_lun];
12096 mtx_unlock(&softc->ctl_lock);
12100 mtx_lock(&lun->lun_lock);
12101 mtx_unlock(&softc->ctl_lock);
12102 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12103 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12104 io->io_hdr.nexus.initid.id,
12105 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12106 } else { /* CTL_TASK_CLEAR_TASK_SET */
12107 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12108 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12110 mtx_unlock(&lun->lun_lock);
12115 ctl_i_t_nexus_reset(union ctl_io *io)
12117 struct ctl_softc *softc = control_softc;
12118 struct ctl_lun *lun;
12119 uint32_t initindex;
12121 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12122 mtx_lock(&softc->ctl_lock);
12123 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12124 mtx_lock(&lun->lun_lock);
12125 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12126 io->io_hdr.nexus.initid.id,
12127 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12129 ctl_clear_mask(lun->have_ca, initindex);
12131 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS;
12132 mtx_unlock(&lun->lun_lock);
12134 mtx_unlock(&softc->ctl_lock);
12139 ctl_abort_task(union ctl_io *io)
12142 struct ctl_lun *lun;
12143 struct ctl_softc *ctl_softc;
12146 char printbuf[128];
12151 ctl_softc = control_softc;
12157 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12158 mtx_lock(&ctl_softc->ctl_lock);
12159 if ((targ_lun < CTL_MAX_LUNS)
12160 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12161 lun = ctl_softc->ctl_luns[targ_lun];
12163 mtx_unlock(&ctl_softc->ctl_lock);
12168 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12169 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12172 mtx_lock(&lun->lun_lock);
12173 mtx_unlock(&ctl_softc->ctl_lock);
12175 * Run through the OOA queue and attempt to find the given I/O.
12176 * The target port, initiator ID, tag type and tag number have to
12177 * match the values that we got from the initiator. If we have an
12178 * untagged command to abort, simply abort the first untagged command
12179 * we come to. We only allow one untagged command at a time of course.
12182 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12184 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12185 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12187 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12189 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12190 lun->lun, xio->scsiio.tag_num,
12191 xio->scsiio.tag_type,
12192 (xio->io_hdr.blocked_links.tqe_prev
12193 == NULL) ? "" : " BLOCKED",
12194 (xio->io_hdr.flags &
12195 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12196 (xio->io_hdr.flags &
12197 CTL_FLAG_ABORT) ? " ABORT" : "",
12198 (xio->io_hdr.flags &
12199 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12200 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12202 printf("%s\n", sbuf_data(&sb));
12205 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12206 && (xio->io_hdr.nexus.initid.id ==
12207 io->io_hdr.nexus.initid.id)) {
12209 * If the abort says that the task is untagged, the
12210 * task in the queue must be untagged. Otherwise,
12211 * we just check to see whether the tag numbers
12212 * match. This is because the QLogic firmware
12213 * doesn't pass back the tag type in an abort
12217 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12218 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12219 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12222 * XXX KDM we've got problems with FC, because it
12223 * doesn't send down a tag type with aborts. So we
12224 * can only really go by the tag number...
12225 * This may cause problems with parallel SCSI.
12226 * Need to figure that out!!
12228 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12229 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12231 if ((io->io_hdr.flags &
12232 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12233 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12234 union ctl_ha_msg msg_info;
12236 io->io_hdr.flags |=
12237 CTL_FLAG_SENT_2OTHER_SC;
12238 msg_info.hdr.nexus = io->io_hdr.nexus;
12239 msg_info.task.task_action =
12240 CTL_TASK_ABORT_TASK;
12241 msg_info.task.tag_num =
12242 io->taskio.tag_num;
12243 msg_info.task.tag_type =
12244 io->taskio.tag_type;
12245 msg_info.hdr.msg_type =
12246 CTL_MSG_MANAGE_TASKS;
12247 msg_info.hdr.original_sc = NULL;
12248 msg_info.hdr.serializing_sc = NULL;
12250 printf("Sent Abort to other side\n");
12252 if (CTL_HA_STATUS_SUCCESS !=
12253 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12255 sizeof(msg_info), 0)) {
12259 printf("ctl_abort_task: found I/O to abort\n");
12265 mtx_unlock(&lun->lun_lock);
12269 * This isn't really an error. It's entirely possible for
12270 * the abort and command completion to cross on the wire.
12271 * This is more of an informative/diagnostic error.
12274 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12275 "%d:%d:%d:%d tag %d type %d\n",
12276 io->io_hdr.nexus.initid.id,
12277 io->io_hdr.nexus.targ_port,
12278 io->io_hdr.nexus.targ_target.id,
12279 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12280 io->taskio.tag_type);
12287 ctl_run_task(union ctl_io *io)
12289 struct ctl_softc *ctl_softc = control_softc;
12291 const char *task_desc;
12293 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12295 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12296 ("ctl_run_task: Unextected io_type %d\n",
12297 io->io_hdr.io_type));
12299 task_desc = ctl_scsi_task_string(&io->taskio);
12300 if (task_desc != NULL) {
12302 csevent_log(CSC_CTL | CSC_SHELF_SW |
12304 csevent_LogType_Trace,
12305 csevent_Severity_Information,
12306 csevent_AlertLevel_Green,
12307 csevent_FRU_Firmware,
12308 csevent_FRU_Unknown,
12309 "CTL: received task: %s",task_desc);
12313 csevent_log(CSC_CTL | CSC_SHELF_SW |
12315 csevent_LogType_Trace,
12316 csevent_Severity_Information,
12317 csevent_AlertLevel_Green,
12318 csevent_FRU_Firmware,
12319 csevent_FRU_Unknown,
12320 "CTL: received unknown task "
12322 io->taskio.task_action,
12323 io->taskio.task_action);
12326 switch (io->taskio.task_action) {
12327 case CTL_TASK_ABORT_TASK:
12328 retval = ctl_abort_task(io);
12330 case CTL_TASK_ABORT_TASK_SET:
12331 case CTL_TASK_CLEAR_TASK_SET:
12332 retval = ctl_abort_task_set(io);
12334 case CTL_TASK_CLEAR_ACA:
12336 case CTL_TASK_I_T_NEXUS_RESET:
12337 retval = ctl_i_t_nexus_reset(io);
12339 case CTL_TASK_LUN_RESET: {
12340 struct ctl_lun *lun;
12343 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12344 mtx_lock(&ctl_softc->ctl_lock);
12345 if ((targ_lun < CTL_MAX_LUNS)
12346 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12347 lun = ctl_softc->ctl_luns[targ_lun];
12349 mtx_unlock(&ctl_softc->ctl_lock);
12354 if (!(io->io_hdr.flags &
12355 CTL_FLAG_FROM_OTHER_SC)) {
12356 union ctl_ha_msg msg_info;
12358 io->io_hdr.flags |=
12359 CTL_FLAG_SENT_2OTHER_SC;
12360 msg_info.hdr.msg_type =
12361 CTL_MSG_MANAGE_TASKS;
12362 msg_info.hdr.nexus = io->io_hdr.nexus;
12363 msg_info.task.task_action =
12364 CTL_TASK_LUN_RESET;
12365 msg_info.hdr.original_sc = NULL;
12366 msg_info.hdr.serializing_sc = NULL;
12367 if (CTL_HA_STATUS_SUCCESS !=
12368 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12370 sizeof(msg_info), 0)) {
12374 retval = ctl_lun_reset(lun, io,
12376 mtx_unlock(&ctl_softc->ctl_lock);
12379 case CTL_TASK_TARGET_RESET:
12380 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12382 case CTL_TASK_BUS_RESET:
12383 retval = ctl_bus_reset(ctl_softc, io);
12385 case CTL_TASK_PORT_LOGIN:
12387 case CTL_TASK_PORT_LOGOUT:
12390 printf("ctl_run_task: got unknown task management event %d\n",
12391 io->taskio.task_action);
12395 io->io_hdr.status = CTL_SUCCESS;
12397 io->io_hdr.status = CTL_ERROR;
12402 * For HA operation. Handle commands that come in from the other
12406 ctl_handle_isc(union ctl_io *io)
12409 struct ctl_lun *lun;
12410 struct ctl_softc *ctl_softc;
12413 ctl_softc = control_softc;
12415 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12416 lun = ctl_softc->ctl_luns[targ_lun];
12418 switch (io->io_hdr.msg_type) {
12419 case CTL_MSG_SERIALIZE:
12420 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12422 case CTL_MSG_R2R: {
12423 const struct ctl_cmd_entry *entry;
12426 * This is only used in SER_ONLY mode.
12429 entry = ctl_get_cmd_entry(&io->scsiio);
12430 mtx_lock(&lun->lun_lock);
12431 if (ctl_scsiio_lun_check(ctl_softc, lun,
12432 entry, (struct ctl_scsiio *)io) != 0) {
12433 mtx_unlock(&lun->lun_lock);
12437 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12438 mtx_unlock(&lun->lun_lock);
12439 ctl_enqueue_rtr(io);
12442 case CTL_MSG_FINISH_IO:
12443 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12448 mtx_lock(&lun->lun_lock);
12449 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12451 ctl_check_blocked(lun);
12452 mtx_unlock(&lun->lun_lock);
12455 case CTL_MSG_PERS_ACTION:
12456 ctl_hndl_per_res_out_on_other_sc(
12457 (union ctl_ha_msg *)&io->presio.pr_msg);
12460 case CTL_MSG_BAD_JUJU:
12464 case CTL_MSG_DATAMOVE:
12465 /* Only used in XFER mode */
12467 ctl_datamove_remote(io);
12469 case CTL_MSG_DATAMOVE_DONE:
12470 /* Only used in XFER mode */
12472 io->scsiio.be_move_done(io);
12476 printf("%s: Invalid message type %d\n",
12477 __func__, io->io_hdr.msg_type);
12487 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12488 * there is no match.
12490 static ctl_lun_error_pattern
12491 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12493 const struct ctl_cmd_entry *entry;
12494 ctl_lun_error_pattern filtered_pattern, pattern;
12496 pattern = desc->error_pattern;
12499 * XXX KDM we need more data passed into this function to match a
12500 * custom pattern, and we actually need to implement custom pattern
12503 if (pattern & CTL_LUN_PAT_CMD)
12504 return (CTL_LUN_PAT_CMD);
12506 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12507 return (CTL_LUN_PAT_ANY);
12509 entry = ctl_get_cmd_entry(ctsio);
12511 filtered_pattern = entry->pattern & pattern;
12514 * If the user requested specific flags in the pattern (e.g.
12515 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12518 * If the user did not specify any flags, it doesn't matter whether
12519 * or not the command supports the flags.
12521 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12522 (pattern & ~CTL_LUN_PAT_MASK))
12523 return (CTL_LUN_PAT_NONE);
12526 * If the user asked for a range check, see if the requested LBA
12527 * range overlaps with this command's LBA range.
12529 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12535 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12537 return (CTL_LUN_PAT_NONE);
12539 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12540 desc->lba_range.len);
12542 * A "pass" means that the LBA ranges don't overlap, so
12543 * this doesn't match the user's range criteria.
12545 if (action == CTL_ACTION_PASS)
12546 return (CTL_LUN_PAT_NONE);
12549 return (filtered_pattern);
12553 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12555 struct ctl_error_desc *desc, *desc2;
12557 mtx_assert(&lun->lun_lock, MA_OWNED);
12559 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12560 ctl_lun_error_pattern pattern;
12562 * Check to see whether this particular command matches
12563 * the pattern in the descriptor.
12565 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12566 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12569 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12570 case CTL_LUN_INJ_ABORTED:
12571 ctl_set_aborted(&io->scsiio);
12573 case CTL_LUN_INJ_MEDIUM_ERR:
12574 ctl_set_medium_error(&io->scsiio);
12576 case CTL_LUN_INJ_UA:
12577 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12579 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12581 case CTL_LUN_INJ_CUSTOM:
12583 * We're assuming the user knows what he is doing.
12584 * Just copy the sense information without doing
12587 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12588 ctl_min(sizeof(desc->custom_sense),
12589 sizeof(io->scsiio.sense_data)));
12590 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12591 io->scsiio.sense_len = SSD_FULL_SIZE;
12592 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12594 case CTL_LUN_INJ_NONE:
12597 * If this is an error injection type we don't know
12598 * about, clear the continuous flag (if it is set)
12599 * so it will get deleted below.
12601 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12605 * By default, each error injection action is a one-shot
12607 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12610 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12616 #ifdef CTL_IO_DELAY
12618 ctl_datamove_timer_wakeup(void *arg)
12622 io = (union ctl_io *)arg;
12626 #endif /* CTL_IO_DELAY */
12629 ctl_datamove(union ctl_io *io)
12631 void (*fe_datamove)(union ctl_io *io);
12633 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12635 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12638 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12643 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12644 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12646 sbuf_cat(&sb, path_str);
12647 switch (io->io_hdr.io_type) {
12649 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12650 sbuf_printf(&sb, "\n");
12651 sbuf_cat(&sb, path_str);
12652 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12653 io->scsiio.tag_num, io->scsiio.tag_type);
12656 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12657 "Tag Type: %d\n", io->taskio.task_action,
12658 io->taskio.tag_num, io->taskio.tag_type);
12661 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12662 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12665 sbuf_cat(&sb, path_str);
12666 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12667 (intmax_t)time_uptime - io->io_hdr.start_time);
12669 printf("%s", sbuf_data(&sb));
12671 #endif /* CTL_TIME_IO */
12673 #ifdef CTL_IO_DELAY
12674 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12675 struct ctl_lun *lun;
12677 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12679 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12681 struct ctl_lun *lun;
12683 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12685 && (lun->delay_info.datamove_delay > 0)) {
12686 struct callout *callout;
12688 callout = (struct callout *)&io->io_hdr.timer_bytes;
12689 callout_init(callout, /*mpsafe*/ 1);
12690 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12691 callout_reset(callout,
12692 lun->delay_info.datamove_delay * hz,
12693 ctl_datamove_timer_wakeup, io);
12694 if (lun->delay_info.datamove_type ==
12695 CTL_DELAY_TYPE_ONESHOT)
12696 lun->delay_info.datamove_delay = 0;
12703 * This command has been aborted. Set the port status, so we fail
12706 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12707 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12708 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12709 io->io_hdr.nexus.targ_port,
12710 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12711 io->io_hdr.nexus.targ_lun);
12712 io->io_hdr.port_status = 31337;
12714 * Note that the backend, in this case, will get the
12715 * callback in its context. In other cases it may get
12716 * called in the frontend's interrupt thread context.
12718 io->scsiio.be_move_done(io);
12723 * If we're in XFER mode and this I/O is from the other shelf
12724 * controller, we need to send the DMA to the other side to
12725 * actually transfer the data to/from the host. In serialize only
12726 * mode the transfer happens below CTL and ctl_datamove() is only
12727 * called on the machine that originally received the I/O.
12729 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12730 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12731 union ctl_ha_msg msg;
12732 uint32_t sg_entries_sent;
12736 memset(&msg, 0, sizeof(msg));
12737 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12738 msg.hdr.original_sc = io->io_hdr.original_sc;
12739 msg.hdr.serializing_sc = io;
12740 msg.hdr.nexus = io->io_hdr.nexus;
12741 msg.dt.flags = io->io_hdr.flags;
12743 * We convert everything into a S/G list here. We can't
12744 * pass by reference, only by value between controllers.
12745 * So we can't pass a pointer to the S/G list, only as many
12746 * S/G entries as we can fit in here. If it's possible for
12747 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12748 * then we need to break this up into multiple transfers.
12750 if (io->scsiio.kern_sg_entries == 0) {
12751 msg.dt.kern_sg_entries = 1;
12753 * If this is in cached memory, flush the cache
12754 * before we send the DMA request to the other
12755 * controller. We want to do this in either the
12756 * read or the write case. The read case is
12757 * straightforward. In the write case, we want to
12758 * make sure nothing is in the local cache that
12759 * could overwrite the DMAed data.
12761 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12763 * XXX KDM use bus_dmamap_sync() here.
12768 * Convert to a physical address if this is a
12771 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12772 msg.dt.sg_list[0].addr =
12773 io->scsiio.kern_data_ptr;
12776 * XXX KDM use busdma here!
12779 msg.dt.sg_list[0].addr = (void *)
12780 vtophys(io->scsiio.kern_data_ptr);
12784 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12787 struct ctl_sg_entry *sgl;
12790 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12791 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12792 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12794 * XXX KDM use bus_dmamap_sync() here.
12799 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12800 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12801 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12802 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12803 msg.dt.sg_sequence = 0;
12806 * Loop until we've sent all of the S/G entries. On the
12807 * other end, we'll recompose these S/G entries into one
12808 * contiguous list before passing it to the
12810 for (sg_entries_sent = 0; sg_entries_sent <
12811 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12812 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12813 sizeof(msg.dt.sg_list[0])),
12814 msg.dt.kern_sg_entries - sg_entries_sent);
12816 if (do_sg_copy != 0) {
12817 struct ctl_sg_entry *sgl;
12820 sgl = (struct ctl_sg_entry *)
12821 io->scsiio.kern_data_ptr;
12823 * If this is in cached memory, flush the cache
12824 * before we send the DMA request to the other
12825 * controller. We want to do this in either
12826 * the * read or the write case. The read
12827 * case is straightforward. In the write
12828 * case, we want to make sure nothing is
12829 * in the local cache that could overwrite
12833 for (i = sg_entries_sent, j = 0;
12834 i < msg.dt.cur_sg_entries; i++, j++) {
12835 if ((io->io_hdr.flags &
12836 CTL_FLAG_NO_DATASYNC) == 0) {
12838 * XXX KDM use bus_dmamap_sync()
12841 if ((io->io_hdr.flags &
12842 CTL_FLAG_BUS_ADDR) == 0) {
12844 * XXX KDM use busdma.
12847 msg.dt.sg_list[j].addr =(void *)
12848 vtophys(sgl[i].addr);
12851 msg.dt.sg_list[j].addr =
12854 msg.dt.sg_list[j].len = sgl[i].len;
12858 sg_entries_sent += msg.dt.cur_sg_entries;
12859 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12860 msg.dt.sg_last = 1;
12862 msg.dt.sg_last = 0;
12865 * XXX KDM drop and reacquire the lock here?
12867 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12868 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12870 * XXX do something here.
12874 msg.dt.sent_sg_entries = sg_entries_sent;
12876 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12877 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12878 ctl_failover_io(io, /*have_lock*/ 0);
12883 * Lookup the fe_datamove() function for this particular
12887 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12894 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12896 union ctl_ha_msg msg;
12899 memset(&msg, 0, sizeof(msg));
12901 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12902 msg.hdr.original_sc = io;
12903 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12904 msg.hdr.nexus = io->io_hdr.nexus;
12905 msg.hdr.status = io->io_hdr.status;
12906 msg.scsi.tag_num = io->scsiio.tag_num;
12907 msg.scsi.tag_type = io->scsiio.tag_type;
12908 msg.scsi.scsi_status = io->scsiio.scsi_status;
12909 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12910 sizeof(io->scsiio.sense_data));
12911 msg.scsi.sense_len = io->scsiio.sense_len;
12912 msg.scsi.sense_residual = io->scsiio.sense_residual;
12913 msg.scsi.fetd_status = io->io_hdr.port_status;
12914 msg.scsi.residual = io->scsiio.residual;
12915 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12917 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12918 ctl_failover_io(io, /*have_lock*/ have_lock);
12922 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12923 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12924 /* XXX do something if this fails */
12930 * The DMA to the remote side is done, now we need to tell the other side
12931 * we're done so it can continue with its data movement.
12934 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12940 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12941 printf("%s: ISC DMA write failed with error %d", __func__,
12943 ctl_set_internal_failure(&io->scsiio,
12945 /*retry_count*/ rq->ret);
12948 ctl_dt_req_free(rq);
12951 * In this case, we had to malloc the memory locally. Free it.
12953 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12955 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12956 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12959 * The data is in local and remote memory, so now we need to send
12960 * status (good or back) back to the other side.
12962 ctl_send_datamove_done(io, /*have_lock*/ 0);
12966 * We've moved the data from the host/controller into local memory. Now we
12967 * need to push it over to the remote controller's memory.
12970 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12976 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12977 ctl_datamove_remote_write_cb);
12983 ctl_datamove_remote_write(union ctl_io *io)
12986 void (*fe_datamove)(union ctl_io *io);
12989 * - Get the data from the host/HBA into local memory.
12990 * - DMA memory from the local controller to the remote controller.
12991 * - Send status back to the remote controller.
12994 retval = ctl_datamove_remote_sgl_setup(io);
12998 /* Switch the pointer over so the FETD knows what to do */
12999 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13002 * Use a custom move done callback, since we need to send completion
13003 * back to the other controller, not to the backend on this side.
13005 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
13007 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13016 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
13025 * In this case, we had to malloc the memory locally. Free it.
13027 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
13029 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13030 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13034 scsi_path_string(io, path_str, sizeof(path_str));
13035 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13036 sbuf_cat(&sb, path_str);
13037 scsi_command_string(&io->scsiio, NULL, &sb);
13038 sbuf_printf(&sb, "\n");
13039 sbuf_cat(&sb, path_str);
13040 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13041 io->scsiio.tag_num, io->scsiio.tag_type);
13042 sbuf_cat(&sb, path_str);
13043 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
13044 io->io_hdr.flags, io->io_hdr.status);
13046 printk("%s", sbuf_data(&sb));
13051 * The read is done, now we need to send status (good or bad) back
13052 * to the other side.
13054 ctl_send_datamove_done(io, /*have_lock*/ 0);
13060 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
13063 void (*fe_datamove)(union ctl_io *io);
13067 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13068 printf("%s: ISC DMA read failed with error %d", __func__,
13070 ctl_set_internal_failure(&io->scsiio,
13072 /*retry_count*/ rq->ret);
13075 ctl_dt_req_free(rq);
13077 /* Switch the pointer over so the FETD knows what to do */
13078 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13081 * Use a custom move done callback, since we need to send completion
13082 * back to the other controller, not to the backend on this side.
13084 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
13086 /* XXX KDM add checks like the ones in ctl_datamove? */
13088 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13094 ctl_datamove_remote_sgl_setup(union ctl_io *io)
13096 struct ctl_sg_entry *local_sglist, *remote_sglist;
13097 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
13098 struct ctl_softc *softc;
13103 softc = control_softc;
13105 local_sglist = io->io_hdr.local_sglist;
13106 local_dma_sglist = io->io_hdr.local_dma_sglist;
13107 remote_sglist = io->io_hdr.remote_sglist;
13108 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13110 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
13111 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
13112 local_sglist[i].len = remote_sglist[i].len;
13115 * XXX Detect the situation where the RS-level I/O
13116 * redirector on the other side has already read the
13117 * data off of the AOR RS on this side, and
13118 * transferred it to remote (mirror) memory on the
13119 * other side. Since we already have the data in
13120 * memory here, we just need to use it.
13122 * XXX KDM this can probably be removed once we
13123 * get the cache device code in and take the
13124 * current AOR implementation out.
13127 if ((remote_sglist[i].addr >=
13128 (void *)vtophys(softc->mirr->addr))
13129 && (remote_sglist[i].addr <
13130 ((void *)vtophys(softc->mirr->addr) +
13131 CacheMirrorOffset))) {
13132 local_sglist[i].addr = remote_sglist[i].addr -
13134 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13136 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13138 local_sglist[i].addr = remote_sglist[i].addr +
13143 printf("%s: local %p, remote %p, len %d\n",
13144 __func__, local_sglist[i].addr,
13145 remote_sglist[i].addr, local_sglist[i].len);
13149 uint32_t len_to_go;
13152 * In this case, we don't have automatically allocated
13153 * memory for this I/O on this controller. This typically
13154 * happens with internal CTL I/O -- e.g. inquiry, mode
13155 * sense, etc. Anything coming from RAIDCore will have
13156 * a mirror area available.
13158 len_to_go = io->scsiio.kern_data_len;
13161 * Clear the no datasync flag, we have to use malloced
13164 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13167 * The difficult thing here is that the size of the various
13168 * S/G segments may be different than the size from the
13169 * remote controller. That'll make it harder when DMAing
13170 * the data back to the other side.
13172 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13173 sizeof(io->io_hdr.remote_sglist[0])) &&
13174 (len_to_go > 0); i++) {
13175 local_sglist[i].len = ctl_min(len_to_go, 131072);
13176 CTL_SIZE_8B(local_dma_sglist[i].len,
13177 local_sglist[i].len);
13178 local_sglist[i].addr =
13179 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13181 local_dma_sglist[i].addr = local_sglist[i].addr;
13183 if (local_sglist[i].addr == NULL) {
13186 printf("malloc failed for %zd bytes!",
13187 local_dma_sglist[i].len);
13188 for (j = 0; j < i; j++) {
13189 free(local_sglist[j].addr, M_CTL);
13191 ctl_set_internal_failure(&io->scsiio,
13193 /*retry_count*/ 4857);
13195 goto bailout_error;
13198 /* XXX KDM do we need a sync here? */
13200 len_to_go -= local_sglist[i].len;
13203 * Reset the number of S/G entries accordingly. The
13204 * original number of S/G entries is available in
13207 io->scsiio.kern_sg_entries = i;
13210 printf("%s: kern_sg_entries = %d\n", __func__,
13211 io->scsiio.kern_sg_entries);
13212 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13213 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13214 local_sglist[i].addr, local_sglist[i].len,
13215 local_dma_sglist[i].len);
13224 ctl_send_datamove_done(io, /*have_lock*/ 0);
13230 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13231 ctl_ha_dt_cb callback)
13233 struct ctl_ha_dt_req *rq;
13234 struct ctl_sg_entry *remote_sglist, *local_sglist;
13235 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13236 uint32_t local_used, remote_used, total_used;
13242 rq = ctl_dt_req_alloc();
13245 * If we failed to allocate the request, and if the DMA didn't fail
13246 * anyway, set busy status. This is just a resource allocation
13250 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13251 ctl_set_busy(&io->scsiio);
13253 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13256 ctl_dt_req_free(rq);
13259 * The data move failed. We need to return status back
13260 * to the other controller. No point in trying to DMA
13261 * data to the remote controller.
13264 ctl_send_datamove_done(io, /*have_lock*/ 0);
13271 local_sglist = io->io_hdr.local_sglist;
13272 local_dma_sglist = io->io_hdr.local_dma_sglist;
13273 remote_sglist = io->io_hdr.remote_sglist;
13274 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13279 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13280 rq->ret = CTL_HA_STATUS_SUCCESS;
13287 * Pull/push the data over the wire from/to the other controller.
13288 * This takes into account the possibility that the local and
13289 * remote sglists may not be identical in terms of the size of
13290 * the elements and the number of elements.
13292 * One fundamental assumption here is that the length allocated for
13293 * both the local and remote sglists is identical. Otherwise, we've
13294 * essentially got a coding error of some sort.
13296 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13298 uint32_t cur_len, dma_length;
13301 rq->id = CTL_HA_DATA_CTL;
13302 rq->command = command;
13306 * Both pointers should be aligned. But it is possible
13307 * that the allocation length is not. They should both
13308 * also have enough slack left over at the end, though,
13309 * to round up to the next 8 byte boundary.
13311 cur_len = ctl_min(local_sglist[i].len - local_used,
13312 remote_sglist[j].len - remote_used);
13315 * In this case, we have a size issue and need to decrease
13316 * the size, except in the case where we actually have less
13317 * than 8 bytes left. In that case, we need to increase
13318 * the DMA length to get the last bit.
13320 if ((cur_len & 0x7) != 0) {
13321 if (cur_len > 0x7) {
13322 cur_len = cur_len - (cur_len & 0x7);
13323 dma_length = cur_len;
13325 CTL_SIZE_8B(dma_length, cur_len);
13329 dma_length = cur_len;
13332 * If we had to allocate memory for this I/O, instead of using
13333 * the non-cached mirror memory, we'll need to flush the cache
13334 * before trying to DMA to the other controller.
13336 * We could end up doing this multiple times for the same
13337 * segment if we have a larger local segment than remote
13338 * segment. That shouldn't be an issue.
13340 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13342 * XXX KDM use bus_dmamap_sync() here.
13346 rq->size = dma_length;
13348 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13349 tmp_ptr += local_used;
13351 /* Use physical addresses when talking to ISC hardware */
13352 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13353 /* XXX KDM use busdma */
13355 rq->local = vtophys(tmp_ptr);
13358 rq->local = tmp_ptr;
13360 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13361 tmp_ptr += remote_used;
13362 rq->remote = tmp_ptr;
13364 rq->callback = NULL;
13366 local_used += cur_len;
13367 if (local_used >= local_sglist[i].len) {
13372 remote_used += cur_len;
13373 if (remote_used >= remote_sglist[j].len) {
13377 total_used += cur_len;
13379 if (total_used >= io->scsiio.kern_data_len)
13380 rq->callback = callback;
13382 if ((rq->size & 0x7) != 0) {
13383 printf("%s: warning: size %d is not on 8b boundary\n",
13384 __func__, rq->size);
13386 if (((uintptr_t)rq->local & 0x7) != 0) {
13387 printf("%s: warning: local %p not on 8b boundary\n",
13388 __func__, rq->local);
13390 if (((uintptr_t)rq->remote & 0x7) != 0) {
13391 printf("%s: warning: remote %p not on 8b boundary\n",
13392 __func__, rq->local);
13395 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13396 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13397 rq->local, rq->remote, rq->size);
13400 isc_ret = ctl_dt_single(rq);
13401 if (isc_ret == CTL_HA_STATUS_WAIT)
13404 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13405 rq->ret = CTL_HA_STATUS_SUCCESS;
13419 ctl_datamove_remote_read(union ctl_io *io)
13425 * This will send an error to the other controller in the case of a
13428 retval = ctl_datamove_remote_sgl_setup(io);
13432 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13433 ctl_datamove_remote_read_cb);
13435 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13437 * Make sure we free memory if there was an error.. The
13438 * ctl_datamove_remote_xfer() function will send the
13439 * datamove done message, or call the callback with an
13440 * error if there is a problem.
13442 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13443 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13450 * Process a datamove request from the other controller. This is used for
13451 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13452 * first. Once that is complete, the data gets DMAed into the remote
13453 * controller's memory. For reads, we DMA from the remote controller's
13454 * memory into our memory first, and then move it out to the FETD.
13457 ctl_datamove_remote(union ctl_io *io)
13459 struct ctl_softc *softc;
13461 softc = control_softc;
13463 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13466 * Note that we look for an aborted I/O here, but don't do some of
13467 * the other checks that ctl_datamove() normally does.
13468 * We don't need to run the datamove delay code, since that should
13469 * have been done if need be on the other controller.
13471 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13472 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13473 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13474 io->io_hdr.nexus.targ_port,
13475 io->io_hdr.nexus.targ_target.id,
13476 io->io_hdr.nexus.targ_lun);
13477 io->io_hdr.port_status = 31338;
13478 ctl_send_datamove_done(io, /*have_lock*/ 0);
13482 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13483 ctl_datamove_remote_write(io);
13484 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13485 ctl_datamove_remote_read(io);
13487 union ctl_ha_msg msg;
13488 struct scsi_sense_data *sense;
13492 memset(&msg, 0, sizeof(msg));
13494 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13495 msg.hdr.status = CTL_SCSI_ERROR;
13496 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13498 retry_count = 4243;
13500 sense = &msg.scsi.sense_data;
13501 sks[0] = SSD_SCS_VALID;
13502 sks[1] = (retry_count >> 8) & 0xff;
13503 sks[2] = retry_count & 0xff;
13505 /* "Internal target failure" */
13506 scsi_set_sense_data(sense,
13507 /*sense_format*/ SSD_TYPE_NONE,
13508 /*current_error*/ 1,
13509 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13512 /*type*/ SSD_ELEM_SKS,
13513 /*size*/ sizeof(sks),
13517 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13518 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13519 ctl_failover_io(io, /*have_lock*/ 1);
13523 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13524 CTL_HA_STATUS_SUCCESS) {
13525 /* XXX KDM what to do if this fails? */
13533 ctl_process_done(union ctl_io *io)
13535 struct ctl_lun *lun;
13536 struct ctl_softc *ctl_softc;
13537 void (*fe_done)(union ctl_io *io);
13538 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13540 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13543 control_softc->ctl_ports[targ_port]->fe_done;
13546 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13551 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13552 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13554 sbuf_cat(&sb, path_str);
13555 switch (io->io_hdr.io_type) {
13557 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13558 sbuf_printf(&sb, "\n");
13559 sbuf_cat(&sb, path_str);
13560 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13561 io->scsiio.tag_num, io->scsiio.tag_type);
13564 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13565 "Tag Type: %d\n", io->taskio.task_action,
13566 io->taskio.tag_num, io->taskio.tag_type);
13569 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13570 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13573 sbuf_cat(&sb, path_str);
13574 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13575 (intmax_t)time_uptime - io->io_hdr.start_time);
13577 printf("%s", sbuf_data(&sb));
13579 #endif /* CTL_TIME_IO */
13581 switch (io->io_hdr.io_type) {
13585 if (bootverbose || verbose > 0)
13586 ctl_io_error_print(io, NULL);
13587 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13591 return (CTL_RETVAL_COMPLETE);
13594 printf("ctl_process_done: invalid io type %d\n",
13595 io->io_hdr.io_type);
13596 panic("ctl_process_done: invalid io type %d\n",
13597 io->io_hdr.io_type);
13598 break; /* NOTREACHED */
13601 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13603 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13604 io->io_hdr.nexus.targ_mapped_lun));
13608 ctl_softc = lun->ctl_softc;
13610 mtx_lock(&lun->lun_lock);
13613 * Check to see if we have any errors to inject here. We only
13614 * inject errors for commands that don't already have errors set.
13616 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13617 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13618 ctl_inject_error(lun, io);
13621 * XXX KDM how do we treat commands that aren't completed
13624 * XXX KDM should we also track I/O latency?
13626 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13627 io->io_hdr.io_type == CTL_IO_SCSI) {
13629 struct bintime cur_bt;
13633 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13635 type = CTL_STATS_READ;
13636 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13638 type = CTL_STATS_WRITE;
13640 type = CTL_STATS_NO_IO;
13642 lun->stats.ports[targ_port].bytes[type] +=
13643 io->scsiio.kern_total_len;
13644 lun->stats.ports[targ_port].operations[type]++;
13646 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13647 &io->io_hdr.dma_bt);
13648 lun->stats.ports[targ_port].num_dmas[type] +=
13649 io->io_hdr.num_dmas;
13650 getbintime(&cur_bt);
13651 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13652 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13657 * Remove this from the OOA queue.
13659 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13662 * Run through the blocked queue on this LUN and see if anything
13663 * has become unblocked, now that this transaction is done.
13665 ctl_check_blocked(lun);
13668 * If the LUN has been invalidated, free it if there is nothing
13669 * left on its OOA queue.
13671 if ((lun->flags & CTL_LUN_INVALID)
13672 && TAILQ_EMPTY(&lun->ooa_queue)) {
13673 mtx_unlock(&lun->lun_lock);
13674 mtx_lock(&ctl_softc->ctl_lock);
13676 mtx_unlock(&ctl_softc->ctl_lock);
13678 mtx_unlock(&lun->lun_lock);
13681 * If this command has been aborted, make sure we set the status
13682 * properly. The FETD is responsible for freeing the I/O and doing
13683 * whatever it needs to do to clean up its state.
13685 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13686 ctl_set_task_aborted(&io->scsiio);
13689 * We print out status for every task management command. For SCSI
13690 * commands, we filter out any unit attention errors; they happen
13691 * on every boot, and would clutter up the log. Note: task
13692 * management commands aren't printed here, they are printed above,
13693 * since they should never even make it down here.
13695 switch (io->io_hdr.io_type) {
13696 case CTL_IO_SCSI: {
13697 int error_code, sense_key, asc, ascq;
13701 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
13702 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13704 * Since this is just for printing, no need to
13705 * show errors here.
13707 scsi_extract_sense_len(&io->scsiio.sense_data,
13708 io->scsiio.sense_len,
13713 /*show_errors*/ 0);
13716 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
13717 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
13718 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
13719 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
13721 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
13722 ctl_softc->skipped_prints++;
13724 uint32_t skipped_prints;
13726 skipped_prints = ctl_softc->skipped_prints;
13728 ctl_softc->skipped_prints = 0;
13729 ctl_softc->last_print_jiffies = time_uptime;
13731 if (skipped_prints > 0) {
13733 csevent_log(CSC_CTL | CSC_SHELF_SW |
13735 csevent_LogType_Trace,
13736 csevent_Severity_Information,
13737 csevent_AlertLevel_Green,
13738 csevent_FRU_Firmware,
13739 csevent_FRU_Unknown,
13740 "High CTL error volume, %d prints "
13741 "skipped", skipped_prints);
13744 if (bootverbose || verbose > 0)
13745 ctl_io_error_print(io, NULL);
13751 if (bootverbose || verbose > 0)
13752 ctl_io_error_print(io, NULL);
13759 * Tell the FETD or the other shelf controller we're done with this
13760 * command. Note that only SCSI commands get to this point. Task
13761 * management commands are completed above.
13763 * We only send status to the other controller if we're in XFER
13764 * mode. In SER_ONLY mode, the I/O is done on the controller that
13765 * received the I/O (from CTL's perspective), and so the status is
13768 * XXX KDM if we hold the lock here, we could cause a deadlock
13769 * if the frontend comes back in in this context to queue
13772 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13773 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13774 union ctl_ha_msg msg;
13776 memset(&msg, 0, sizeof(msg));
13777 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13778 msg.hdr.original_sc = io->io_hdr.original_sc;
13779 msg.hdr.nexus = io->io_hdr.nexus;
13780 msg.hdr.status = io->io_hdr.status;
13781 msg.scsi.scsi_status = io->scsiio.scsi_status;
13782 msg.scsi.tag_num = io->scsiio.tag_num;
13783 msg.scsi.tag_type = io->scsiio.tag_type;
13784 msg.scsi.sense_len = io->scsiio.sense_len;
13785 msg.scsi.sense_residual = io->scsiio.sense_residual;
13786 msg.scsi.residual = io->scsiio.residual;
13787 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13788 sizeof(io->scsiio.sense_data));
13790 * We copy this whether or not this is an I/O-related
13791 * command. Otherwise, we'd have to go and check to see
13792 * whether it's a read/write command, and it really isn't
13795 memcpy(&msg.scsi.lbalen,
13796 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13797 sizeof(msg.scsi.lbalen));
13799 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13800 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13801 /* XXX do something here */
13810 return (CTL_RETVAL_COMPLETE);
13815 * Front end should call this if it doesn't do autosense. When the request
13816 * sense comes back in from the initiator, we'll dequeue this and send it.
13819 ctl_queue_sense(union ctl_io *io)
13821 struct ctl_lun *lun;
13822 struct ctl_softc *ctl_softc;
13823 uint32_t initidx, targ_lun;
13825 ctl_softc = control_softc;
13827 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13830 * LUN lookup will likely move to the ctl_work_thread() once we
13831 * have our new queueing infrastructure (that doesn't put things on
13832 * a per-LUN queue initially). That is so that we can handle
13833 * things like an INQUIRY to a LUN that we don't have enabled. We
13834 * can't deal with that right now.
13836 mtx_lock(&ctl_softc->ctl_lock);
13839 * If we don't have a LUN for this, just toss the sense
13842 targ_lun = io->io_hdr.nexus.targ_lun;
13843 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13844 if ((targ_lun < CTL_MAX_LUNS)
13845 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13846 lun = ctl_softc->ctl_luns[targ_lun];
13850 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13852 mtx_lock(&lun->lun_lock);
13854 * Already have CA set for this LUN...toss the sense information.
13856 if (ctl_is_set(lun->have_ca, initidx)) {
13857 mtx_unlock(&lun->lun_lock);
13861 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13862 ctl_min(sizeof(lun->pending_sense[initidx]),
13863 sizeof(io->scsiio.sense_data)));
13864 ctl_set_mask(lun->have_ca, initidx);
13865 mtx_unlock(&lun->lun_lock);
13868 mtx_unlock(&ctl_softc->ctl_lock);
13872 return (CTL_RETVAL_COMPLETE);
13877 * Primary command inlet from frontend ports. All SCSI and task I/O
13878 * requests must go through this function.
13881 ctl_queue(union ctl_io *io)
13883 struct ctl_softc *ctl_softc;
13885 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13887 ctl_softc = control_softc;
13890 io->io_hdr.start_time = time_uptime;
13891 getbintime(&io->io_hdr.start_bt);
13892 #endif /* CTL_TIME_IO */
13894 /* Map FE-specific LUN ID into global one. */
13895 io->io_hdr.nexus.targ_mapped_lun =
13896 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
13898 switch (io->io_hdr.io_type) {
13901 ctl_enqueue_incoming(io);
13904 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13908 return (CTL_RETVAL_COMPLETE);
13911 #ifdef CTL_IO_DELAY
13913 ctl_done_timer_wakeup(void *arg)
13917 io = (union ctl_io *)arg;
13920 #endif /* CTL_IO_DELAY */
13923 ctl_done(union ctl_io *io)
13925 struct ctl_softc *ctl_softc;
13927 ctl_softc = control_softc;
13930 * Enable this to catch duplicate completion issues.
13933 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13934 printf("%s: type %d msg %d cdb %x iptl: "
13935 "%d:%d:%d:%d tag 0x%04x "
13936 "flag %#x status %x\n",
13938 io->io_hdr.io_type,
13939 io->io_hdr.msg_type,
13941 io->io_hdr.nexus.initid.id,
13942 io->io_hdr.nexus.targ_port,
13943 io->io_hdr.nexus.targ_target.id,
13944 io->io_hdr.nexus.targ_lun,
13945 (io->io_hdr.io_type ==
13947 io->taskio.tag_num :
13948 io->scsiio.tag_num,
13950 io->io_hdr.status);
13952 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13956 * This is an internal copy of an I/O, and should not go through
13957 * the normal done processing logic.
13959 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13963 * We need to send a msg to the serializing shelf to finish the IO
13964 * as well. We don't send a finish message to the other shelf if
13965 * this is a task management command. Task management commands
13966 * aren't serialized in the OOA queue, but rather just executed on
13967 * both shelf controllers for commands that originated on that
13970 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13971 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13972 union ctl_ha_msg msg_io;
13974 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13975 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13976 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13977 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13979 /* continue on to finish IO */
13981 #ifdef CTL_IO_DELAY
13982 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13983 struct ctl_lun *lun;
13985 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13987 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13989 struct ctl_lun *lun;
13991 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13994 && (lun->delay_info.done_delay > 0)) {
13995 struct callout *callout;
13997 callout = (struct callout *)&io->io_hdr.timer_bytes;
13998 callout_init(callout, /*mpsafe*/ 1);
13999 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
14000 callout_reset(callout,
14001 lun->delay_info.done_delay * hz,
14002 ctl_done_timer_wakeup, io);
14003 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
14004 lun->delay_info.done_delay = 0;
14008 #endif /* CTL_IO_DELAY */
14010 ctl_enqueue_done(io);
14014 ctl_isc(struct ctl_scsiio *ctsio)
14016 struct ctl_lun *lun;
14019 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14021 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
14023 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
14025 retval = lun->backend->data_submit((union ctl_io *)ctsio);
14032 ctl_work_thread(void *arg)
14034 struct ctl_thread *thr = (struct ctl_thread *)arg;
14035 struct ctl_softc *softc = thr->ctl_softc;
14039 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
14045 * We handle the queues in this order:
14047 * - done queue (to free up resources, unblock other commands)
14051 * If those queues are empty, we break out of the loop and
14054 mtx_lock(&thr->queue_lock);
14055 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
14057 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
14058 mtx_unlock(&thr->queue_lock);
14059 ctl_handle_isc(io);
14062 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
14064 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
14065 /* clear any blocked commands, call fe_done */
14066 mtx_unlock(&thr->queue_lock);
14067 retval = ctl_process_done(io);
14070 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
14072 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
14073 mtx_unlock(&thr->queue_lock);
14074 if (io->io_hdr.io_type == CTL_IO_TASK)
14077 ctl_scsiio_precheck(softc, &io->scsiio);
14080 if (!ctl_pause_rtr) {
14081 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
14083 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
14084 mtx_unlock(&thr->queue_lock);
14085 retval = ctl_scsiio(&io->scsiio);
14086 if (retval != CTL_RETVAL_COMPLETE)
14087 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
14092 /* Sleep until we have something to do. */
14093 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
14098 ctl_lun_thread(void *arg)
14100 struct ctl_softc *softc = (struct ctl_softc *)arg;
14101 struct ctl_be_lun *be_lun;
14104 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
14108 mtx_lock(&softc->ctl_lock);
14109 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
14110 if (be_lun != NULL) {
14111 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
14112 mtx_unlock(&softc->ctl_lock);
14113 ctl_create_lun(be_lun);
14117 /* Sleep until we have something to do. */
14118 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
14119 PDROP | PRIBIO, "-", 0);
14124 ctl_enqueue_incoming(union ctl_io *io)
14126 struct ctl_softc *softc = control_softc;
14127 struct ctl_thread *thr;
14130 idx = (io->io_hdr.nexus.targ_port * 127 +
14131 io->io_hdr.nexus.initid.id) % worker_threads;
14132 thr = &softc->threads[idx];
14133 mtx_lock(&thr->queue_lock);
14134 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14135 mtx_unlock(&thr->queue_lock);
14140 ctl_enqueue_rtr(union ctl_io *io)
14142 struct ctl_softc *softc = control_softc;
14143 struct ctl_thread *thr;
14145 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14146 mtx_lock(&thr->queue_lock);
14147 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14148 mtx_unlock(&thr->queue_lock);
14153 ctl_enqueue_done(union ctl_io *io)
14155 struct ctl_softc *softc = control_softc;
14156 struct ctl_thread *thr;
14158 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14159 mtx_lock(&thr->queue_lock);
14160 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14161 mtx_unlock(&thr->queue_lock);
14166 ctl_enqueue_isc(union ctl_io *io)
14168 struct ctl_softc *softc = control_softc;
14169 struct ctl_thread *thr;
14171 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14172 mtx_lock(&thr->queue_lock);
14173 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14174 mtx_unlock(&thr->queue_lock);
14178 /* Initialization and failover */
14181 ctl_init_isc_msg(void)
14183 printf("CTL: Still calling this thing\n");
14188 * Initializes component into configuration defined by bootMode
14190 * returns hasc_Status:
14192 * ERROR - fatal error
14194 static ctl_ha_comp_status
14195 ctl_isc_init(struct ctl_ha_component *c)
14197 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14204 * Starts component in state requested. If component starts successfully,
14205 * it must set its own state to the requestrd state
14206 * When requested state is HASC_STATE_HA, the component may refine it
14207 * by adding _SLAVE or _MASTER flags.
14208 * Currently allowed state transitions are:
14209 * UNKNOWN->HA - initial startup
14210 * UNKNOWN->SINGLE - initial startup when no parter detected
14211 * HA->SINGLE - failover
14212 * returns ctl_ha_comp_status:
14213 * OK - component successfully started in requested state
14214 * FAILED - could not start the requested state, failover may
14216 * ERROR - fatal error detected, no future startup possible
14218 static ctl_ha_comp_status
14219 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14221 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14223 printf("%s: go\n", __func__);
14225 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14226 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14228 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14229 != CTL_HA_STATUS_SUCCESS) {
14230 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14231 ret = CTL_HA_COMP_STATUS_ERROR;
14233 } else if (CTL_HA_STATE_IS_HA(c->state)
14234 && CTL_HA_STATE_IS_SINGLE(state)){
14235 // HA->SINGLE transition
14239 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14241 ret = CTL_HA_COMP_STATUS_ERROR;
14243 if (CTL_HA_STATE_IS_SINGLE(state))
14252 * Quiesce component
14253 * The component must clear any error conditions (set status to OK) and
14254 * prepare itself to another Start call
14255 * returns ctl_ha_comp_status:
14259 static ctl_ha_comp_status
14260 ctl_isc_quiesce(struct ctl_ha_component *c)
14262 int ret = CTL_HA_COMP_STATUS_OK;
14269 struct ctl_ha_component ctl_ha_component_ctlisc =
14272 .state = CTL_HA_STATE_UNKNOWN,
14273 .init = ctl_isc_init,
14274 .start = ctl_isc_start,
14275 .quiesce = ctl_isc_quiesce