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 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2905 __func__, (uintmax_t)err_desc->lun_id);
2909 mtx_lock(&lun->lun_lock);
2910 mtx_unlock(&softc->ctl_lock);
2913 * We could do some checking here to verify the validity
2914 * of the request, but given the complexity of error
2915 * injection requests, the checking logic would be fairly
2918 * For now, if the request is invalid, it just won't get
2919 * executed and might get deleted.
2921 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2924 * XXX KDM check to make sure the serial number is unique,
2925 * in case we somehow manage to wrap. That shouldn't
2926 * happen for a very long time, but it's the right thing to
2929 new_err_desc->serial = lun->error_serial;
2930 err_desc->serial = lun->error_serial;
2931 lun->error_serial++;
2933 mtx_unlock(&lun->lun_lock);
2936 case CTL_ERROR_INJECT_DELETE: {
2937 struct ctl_error_desc *delete_desc, *desc, *desc2;
2938 struct ctl_lun *lun;
2941 delete_desc = (struct ctl_error_desc *)addr;
2944 mtx_lock(&softc->ctl_lock);
2945 lun = softc->ctl_luns[delete_desc->lun_id];
2947 mtx_unlock(&softc->ctl_lock);
2948 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2949 __func__, (uintmax_t)delete_desc->lun_id);
2953 mtx_lock(&lun->lun_lock);
2954 mtx_unlock(&softc->ctl_lock);
2955 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2956 if (desc->serial != delete_desc->serial)
2959 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2964 mtx_unlock(&lun->lun_lock);
2965 if (delete_done == 0) {
2966 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2967 "error serial %ju on LUN %u\n", __func__,
2968 delete_desc->serial, delete_desc->lun_id);
2974 case CTL_DUMP_STRUCTS: {
2976 struct ctl_port *port;
2977 struct ctl_frontend *fe;
2979 mtx_lock(&softc->ctl_lock);
2980 printf("CTL Persistent Reservation information start:\n");
2981 for (i = 0; i < CTL_MAX_LUNS; i++) {
2982 struct ctl_lun *lun;
2984 lun = softc->ctl_luns[i];
2987 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2990 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
2991 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2992 idx = j * CTL_MAX_INIT_PER_PORT + k;
2993 if (lun->per_res[idx].registered == 0)
2995 printf(" LUN %d port %d iid %d key "
2997 (uintmax_t)scsi_8btou64(
2998 lun->per_res[idx].res_key.key));
3002 printf("CTL Persistent Reservation information end\n");
3003 printf("CTL Ports:\n");
3004 STAILQ_FOREACH(port, &softc->port_list, links) {
3005 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3006 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3007 port->frontend->name, port->port_type,
3008 port->physical_port, port->virtual_port,
3009 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3010 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3011 if (port->wwpn_iid[j].in_use == 0 &&
3012 port->wwpn_iid[j].wwpn == 0 &&
3013 port->wwpn_iid[j].name == NULL)
3016 printf(" iid %u use %d WWPN %#jx '%s'\n",
3017 j, port->wwpn_iid[j].in_use,
3018 (uintmax_t)port->wwpn_iid[j].wwpn,
3019 port->wwpn_iid[j].name);
3022 printf("CTL Port information end\n");
3023 mtx_unlock(&softc->ctl_lock);
3025 * XXX KDM calling this without a lock. We'd likely want
3026 * to drop the lock before calling the frontend's dump
3029 printf("CTL Frontends:\n");
3030 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3031 printf(" Frontend '%s'\n", fe->name);
3032 if (fe->fe_dump != NULL)
3035 printf("CTL Frontend information end\n");
3039 struct ctl_lun_req *lun_req;
3040 struct ctl_backend_driver *backend;
3042 lun_req = (struct ctl_lun_req *)addr;
3044 backend = ctl_backend_find(lun_req->backend);
3045 if (backend == NULL) {
3046 lun_req->status = CTL_LUN_ERROR;
3047 snprintf(lun_req->error_str,
3048 sizeof(lun_req->error_str),
3049 "Backend \"%s\" not found.",
3053 if (lun_req->num_be_args > 0) {
3054 lun_req->kern_be_args = ctl_copyin_args(
3055 lun_req->num_be_args,
3058 sizeof(lun_req->error_str));
3059 if (lun_req->kern_be_args == NULL) {
3060 lun_req->status = CTL_LUN_ERROR;
3065 retval = backend->ioctl(dev, cmd, addr, flag, td);
3067 if (lun_req->num_be_args > 0) {
3068 ctl_copyout_args(lun_req->num_be_args,
3069 lun_req->kern_be_args);
3070 ctl_free_args(lun_req->num_be_args,
3071 lun_req->kern_be_args);
3075 case CTL_LUN_LIST: {
3077 struct ctl_lun *lun;
3078 struct ctl_lun_list *list;
3079 struct ctl_option *opt;
3081 list = (struct ctl_lun_list *)addr;
3084 * Allocate a fixed length sbuf here, based on the length
3085 * of the user's buffer. We could allocate an auto-extending
3086 * buffer, and then tell the user how much larger our
3087 * amount of data is than his buffer, but that presents
3090 * 1. The sbuf(9) routines use a blocking malloc, and so
3091 * we can't hold a lock while calling them with an
3092 * auto-extending buffer.
3094 * 2. There is not currently a LUN reference counting
3095 * mechanism, outside of outstanding transactions on
3096 * the LUN's OOA queue. So a LUN could go away on us
3097 * while we're getting the LUN number, backend-specific
3098 * information, etc. Thus, given the way things
3099 * currently work, we need to hold the CTL lock while
3100 * grabbing LUN information.
3102 * So, from the user's standpoint, the best thing to do is
3103 * allocate what he thinks is a reasonable buffer length,
3104 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3105 * double the buffer length and try again. (And repeat
3106 * that until he succeeds.)
3108 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3110 list->status = CTL_LUN_LIST_ERROR;
3111 snprintf(list->error_str, sizeof(list->error_str),
3112 "Unable to allocate %d bytes for LUN list",
3117 sbuf_printf(sb, "<ctllunlist>\n");
3119 mtx_lock(&softc->ctl_lock);
3120 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3121 mtx_lock(&lun->lun_lock);
3122 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3123 (uintmax_t)lun->lun);
3126 * Bail out as soon as we see that we've overfilled
3132 retval = sbuf_printf(sb, "\t<backend_type>%s"
3133 "</backend_type>\n",
3134 (lun->backend == NULL) ? "none" :
3135 lun->backend->name);
3140 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3141 lun->be_lun->lun_type);
3146 if (lun->backend == NULL) {
3147 retval = sbuf_printf(sb, "</lun>\n");
3153 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3154 (lun->be_lun->maxlba > 0) ?
3155 lun->be_lun->maxlba + 1 : 0);
3160 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3161 lun->be_lun->blocksize);
3166 retval = sbuf_printf(sb, "\t<serial_number>");
3171 retval = ctl_sbuf_printf_esc(sb,
3172 lun->be_lun->serial_num);
3177 retval = sbuf_printf(sb, "</serial_number>\n");
3182 retval = sbuf_printf(sb, "\t<device_id>");
3187 retval = ctl_sbuf_printf_esc(sb,lun->be_lun->device_id);
3192 retval = sbuf_printf(sb, "</device_id>\n");
3197 if (lun->backend->lun_info != NULL) {
3198 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3202 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3203 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3204 opt->name, opt->value, opt->name);
3209 retval = sbuf_printf(sb, "</lun>\n");
3213 mtx_unlock(&lun->lun_lock);
3216 mtx_unlock(&lun->lun_lock);
3217 mtx_unlock(&softc->ctl_lock);
3220 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3223 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3224 snprintf(list->error_str, sizeof(list->error_str),
3225 "Out of space, %d bytes is too small",
3232 retval = copyout(sbuf_data(sb), list->lun_xml,
3235 list->fill_len = sbuf_len(sb) + 1;
3236 list->status = CTL_LUN_LIST_OK;
3241 struct ctl_iscsi *ci;
3242 struct ctl_frontend *fe;
3244 ci = (struct ctl_iscsi *)addr;
3246 fe = ctl_frontend_find("iscsi");
3248 ci->status = CTL_ISCSI_ERROR;
3249 snprintf(ci->error_str, sizeof(ci->error_str),
3250 "Frontend \"iscsi\" not found.");
3254 retval = fe->ioctl(dev, cmd, addr, flag, td);
3257 case CTL_PORT_REQ: {
3258 struct ctl_req *req;
3259 struct ctl_frontend *fe;
3261 req = (struct ctl_req *)addr;
3263 fe = ctl_frontend_find(req->driver);
3265 req->status = CTL_LUN_ERROR;
3266 snprintf(req->error_str, sizeof(req->error_str),
3267 "Frontend \"%s\" not found.", req->driver);
3270 if (req->num_args > 0) {
3271 req->kern_args = ctl_copyin_args(req->num_args,
3272 req->args, req->error_str, sizeof(req->error_str));
3273 if (req->kern_args == NULL) {
3274 req->status = CTL_LUN_ERROR;
3279 retval = fe->ioctl(dev, cmd, addr, flag, td);
3281 if (req->num_args > 0) {
3282 ctl_copyout_args(req->num_args, req->kern_args);
3283 ctl_free_args(req->num_args, req->kern_args);
3287 case CTL_PORT_LIST: {
3289 struct ctl_port *port;
3290 struct ctl_lun_list *list;
3291 struct ctl_option *opt;
3293 list = (struct ctl_lun_list *)addr;
3295 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3297 list->status = CTL_LUN_LIST_ERROR;
3298 snprintf(list->error_str, sizeof(list->error_str),
3299 "Unable to allocate %d bytes for LUN list",
3304 sbuf_printf(sb, "<ctlportlist>\n");
3306 mtx_lock(&softc->ctl_lock);
3307 STAILQ_FOREACH(port, &softc->port_list, links) {
3308 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3309 (uintmax_t)port->targ_port);
3312 * Bail out as soon as we see that we've overfilled
3318 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3319 "</frontend_type>\n", port->frontend->name);
3323 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3328 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3329 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3333 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3338 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3339 port->physical_port);
3343 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3344 port->virtual_port);
3348 retval = sbuf_printf(sb, "\t<wwnn>%#jx</wwnn>\n",
3349 (uintmax_t)port->wwnn);
3353 retval = sbuf_printf(sb, "\t<wwpn>%#jx</wwpn>\n",
3354 (uintmax_t)port->wwpn);
3358 if (port->port_info != NULL) {
3359 retval = port->port_info(port->onoff_arg, sb);
3363 STAILQ_FOREACH(opt, &port->options, links) {
3364 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3365 opt->name, opt->value, opt->name);
3370 retval = sbuf_printf(sb, "</targ_port>\n");
3374 mtx_unlock(&softc->ctl_lock);
3377 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3380 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3381 snprintf(list->error_str, sizeof(list->error_str),
3382 "Out of space, %d bytes is too small",
3389 retval = copyout(sbuf_data(sb), list->lun_xml,
3392 list->fill_len = sbuf_len(sb) + 1;
3393 list->status = CTL_LUN_LIST_OK;
3398 /* XXX KDM should we fix this? */
3400 struct ctl_backend_driver *backend;
3407 * We encode the backend type as the ioctl type for backend
3408 * ioctls. So parse it out here, and then search for a
3409 * backend of this type.
3411 type = _IOC_TYPE(cmd);
3413 STAILQ_FOREACH(backend, &softc->be_list, links) {
3414 if (backend->type == type) {
3420 printf("ctl: unknown ioctl command %#lx or backend "
3425 retval = backend->ioctl(dev, cmd, addr, flag, td);
3435 ctl_get_initindex(struct ctl_nexus *nexus)
3437 if (nexus->targ_port < CTL_MAX_PORTS)
3438 return (nexus->initid.id +
3439 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3441 return (nexus->initid.id +
3442 ((nexus->targ_port - CTL_MAX_PORTS) *
3443 CTL_MAX_INIT_PER_PORT));
3447 ctl_get_resindex(struct ctl_nexus *nexus)
3449 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3453 ctl_port_idx(int port_num)
3455 if (port_num < CTL_MAX_PORTS)
3458 return(port_num - CTL_MAX_PORTS);
3462 ctl_map_lun(int port_num, uint32_t lun_id)
3464 struct ctl_port *port;
3466 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3468 return (UINT32_MAX);
3469 if (port->lun_map == NULL)
3471 return (port->lun_map(port->targ_lun_arg, lun_id));
3475 ctl_map_lun_back(int port_num, uint32_t lun_id)
3477 struct ctl_port *port;
3480 port = control_softc->ctl_ports[ctl_port_idx(port_num)];
3481 if (port->lun_map == NULL)
3483 for (i = 0; i < CTL_MAX_LUNS; i++) {
3484 if (port->lun_map(port->targ_lun_arg, i) == lun_id)
3487 return (UINT32_MAX);
3491 * Note: This only works for bitmask sizes that are at least 32 bits, and
3492 * that are a power of 2.
3495 ctl_ffz(uint32_t *mask, uint32_t size)
3497 uint32_t num_chunks, num_pieces;
3500 num_chunks = (size >> 5);
3501 if (num_chunks == 0)
3503 num_pieces = ctl_min((sizeof(uint32_t) * 8), size);
3505 for (i = 0; i < num_chunks; i++) {
3506 for (j = 0; j < num_pieces; j++) {
3507 if ((mask[i] & (1 << j)) == 0)
3508 return ((i << 5) + j);
3516 ctl_set_mask(uint32_t *mask, uint32_t bit)
3518 uint32_t chunk, piece;
3521 piece = bit % (sizeof(uint32_t) * 8);
3523 if ((mask[chunk] & (1 << piece)) != 0)
3526 mask[chunk] |= (1 << piece);
3532 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3534 uint32_t chunk, piece;
3537 piece = bit % (sizeof(uint32_t) * 8);
3539 if ((mask[chunk] & (1 << piece)) == 0)
3542 mask[chunk] &= ~(1 << piece);
3548 ctl_is_set(uint32_t *mask, uint32_t bit)
3550 uint32_t chunk, piece;
3553 piece = bit % (sizeof(uint32_t) * 8);
3555 if ((mask[chunk] & (1 << piece)) == 0)
3563 * The bus, target and lun are optional, they can be filled in later.
3564 * can_wait is used to determine whether we can wait on the malloc or not.
3567 ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port, uint32_t targ_target,
3568 uint32_t targ_lun, int can_wait)
3573 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_WAITOK);
3575 io = (union ctl_io *)malloc(sizeof(*io), M_CTL, M_NOWAIT);
3578 io->io_hdr.io_type = io_type;
3579 io->io_hdr.targ_port = targ_port;
3581 * XXX KDM this needs to change/go away. We need to move
3582 * to a preallocated pool of ctl_scsiio structures.
3584 io->io_hdr.nexus.targ_target.id = targ_target;
3585 io->io_hdr.nexus.targ_lun = targ_lun;
3592 ctl_kfree_io(union ctl_io *io)
3599 * ctl_softc, pool_type, total_ctl_io are passed in.
3600 * npool is passed out.
3603 ctl_pool_create(struct ctl_softc *ctl_softc, ctl_pool_type pool_type,
3604 uint32_t total_ctl_io, struct ctl_io_pool **npool)
3607 union ctl_io *cur_io, *next_io;
3608 struct ctl_io_pool *pool;
3613 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3620 pool->type = pool_type;
3621 pool->ctl_softc = ctl_softc;
3623 mtx_lock(&ctl_softc->pool_lock);
3624 pool->id = ctl_softc->cur_pool_id++;
3625 mtx_unlock(&ctl_softc->pool_lock);
3627 pool->flags = CTL_POOL_FLAG_NONE;
3628 pool->refcount = 1; /* Reference for validity. */
3629 STAILQ_INIT(&pool->free_queue);
3632 * XXX KDM other options here:
3633 * - allocate a page at a time
3634 * - allocate one big chunk of memory.
3635 * Page allocation might work well, but would take a little more
3638 for (i = 0; i < total_ctl_io; i++) {
3639 cur_io = (union ctl_io *)malloc(sizeof(*cur_io), M_CTLIO,
3641 if (cur_io == NULL) {
3645 cur_io->io_hdr.pool = pool;
3646 STAILQ_INSERT_TAIL(&pool->free_queue, &cur_io->io_hdr, links);
3647 pool->total_ctl_io++;
3648 pool->free_ctl_io++;
3652 for (cur_io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3653 cur_io != NULL; cur_io = next_io) {
3654 next_io = (union ctl_io *)STAILQ_NEXT(&cur_io->io_hdr,
3656 STAILQ_REMOVE(&pool->free_queue, &cur_io->io_hdr,
3658 free(cur_io, M_CTLIO);
3664 mtx_lock(&ctl_softc->pool_lock);
3665 ctl_softc->num_pools++;
3666 STAILQ_INSERT_TAIL(&ctl_softc->io_pools, pool, links);
3668 * Increment our usage count if this is an external consumer, so we
3669 * can't get unloaded until the external consumer (most likely a
3670 * FETD) unloads and frees his pool.
3672 * XXX KDM will this increment the caller's module use count, or
3676 if ((pool_type != CTL_POOL_EMERGENCY)
3677 && (pool_type != CTL_POOL_INTERNAL)
3678 && (pool_type != CTL_POOL_4OTHERSC))
3682 mtx_unlock(&ctl_softc->pool_lock);
3692 ctl_pool_acquire(struct ctl_io_pool *pool)
3695 mtx_assert(&pool->ctl_softc->pool_lock, MA_OWNED);
3697 if (pool->flags & CTL_POOL_FLAG_INVALID)
3706 ctl_pool_release(struct ctl_io_pool *pool)
3708 struct ctl_softc *ctl_softc = pool->ctl_softc;
3711 mtx_assert(&ctl_softc->pool_lock, MA_OWNED);
3713 if (--pool->refcount != 0)
3716 while ((io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue)) != NULL) {
3717 STAILQ_REMOVE(&pool->free_queue, &io->io_hdr, ctl_io_hdr,
3722 STAILQ_REMOVE(&ctl_softc->io_pools, pool, ctl_io_pool, links);
3723 ctl_softc->num_pools--;
3726 * XXX KDM will this decrement the caller's usage count or mine?
3729 if ((pool->type != CTL_POOL_EMERGENCY)
3730 && (pool->type != CTL_POOL_INTERNAL)
3731 && (pool->type != CTL_POOL_4OTHERSC))
3739 ctl_pool_free(struct ctl_io_pool *pool)
3741 struct ctl_softc *ctl_softc;
3746 ctl_softc = pool->ctl_softc;
3747 mtx_lock(&ctl_softc->pool_lock);
3748 pool->flags |= CTL_POOL_FLAG_INVALID;
3749 ctl_pool_release(pool);
3750 mtx_unlock(&ctl_softc->pool_lock);
3754 * This routine does not block (except for spinlocks of course).
3755 * It tries to allocate a ctl_io union from the caller's pool as quickly as
3759 ctl_alloc_io(void *pool_ref)
3762 struct ctl_softc *ctl_softc;
3763 struct ctl_io_pool *pool, *npool;
3764 struct ctl_io_pool *emergency_pool;
3766 pool = (struct ctl_io_pool *)pool_ref;
3769 printf("%s: pool is NULL\n", __func__);
3773 emergency_pool = NULL;
3775 ctl_softc = pool->ctl_softc;
3777 mtx_lock(&ctl_softc->pool_lock);
3779 * First, try to get the io structure from the user's pool.
3781 if (ctl_pool_acquire(pool) == 0) {
3782 io = (union ctl_io *)STAILQ_FIRST(&pool->free_queue);
3784 STAILQ_REMOVE_HEAD(&pool->free_queue, links);
3785 pool->total_allocated++;
3786 pool->free_ctl_io--;
3787 mtx_unlock(&ctl_softc->pool_lock);
3790 ctl_pool_release(pool);
3793 * If he doesn't have any io structures left, search for an
3794 * emergency pool and grab one from there.
3796 STAILQ_FOREACH(npool, &ctl_softc->io_pools, links) {
3797 if (npool->type != CTL_POOL_EMERGENCY)
3800 if (ctl_pool_acquire(npool) != 0)
3803 emergency_pool = npool;
3805 io = (union ctl_io *)STAILQ_FIRST(&npool->free_queue);
3807 STAILQ_REMOVE_HEAD(&npool->free_queue, links);
3808 npool->total_allocated++;
3809 npool->free_ctl_io--;
3810 mtx_unlock(&ctl_softc->pool_lock);
3813 ctl_pool_release(npool);
3816 /* Drop the spinlock before we malloc */
3817 mtx_unlock(&ctl_softc->pool_lock);
3820 * The emergency pool (if it exists) didn't have one, so try an
3821 * atomic (i.e. nonblocking) malloc and see if we get lucky.
3823 io = (union ctl_io *)malloc(sizeof(*io), M_CTLIO, M_NOWAIT);
3826 * If the emergency pool exists but is empty, add this
3827 * ctl_io to its list when it gets freed.
3829 if (emergency_pool != NULL) {
3830 mtx_lock(&ctl_softc->pool_lock);
3831 if (ctl_pool_acquire(emergency_pool) == 0) {
3832 io->io_hdr.pool = emergency_pool;
3833 emergency_pool->total_ctl_io++;
3835 * Need to bump this, otherwise
3836 * total_allocated and total_freed won't
3837 * match when we no longer have anything
3840 emergency_pool->total_allocated++;
3842 mtx_unlock(&ctl_softc->pool_lock);
3844 io->io_hdr.pool = NULL;
3851 ctl_free_io(union ctl_io *io)
3857 * If this ctl_io has a pool, return it to that pool.
3859 if (io->io_hdr.pool != NULL) {
3860 struct ctl_io_pool *pool;
3862 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3863 mtx_lock(&pool->ctl_softc->pool_lock);
3864 io->io_hdr.io_type = 0xff;
3865 STAILQ_INSERT_TAIL(&pool->free_queue, &io->io_hdr, links);
3866 pool->total_freed++;
3867 pool->free_ctl_io++;
3868 ctl_pool_release(pool);
3869 mtx_unlock(&pool->ctl_softc->pool_lock);
3872 * Otherwise, just free it. We probably malloced it and
3873 * the emergency pool wasn't available.
3881 ctl_zero_io(union ctl_io *io)
3889 * May need to preserve linked list pointers at some point too.
3891 pool_ref = io->io_hdr.pool;
3893 memset(io, 0, sizeof(*io));
3895 io->io_hdr.pool = pool_ref;
3899 * This routine is currently used for internal copies of ctl_ios that need
3900 * to persist for some reason after we've already returned status to the
3901 * FETD. (Thus the flag set.)
3904 * Note that this makes a blind copy of all fields in the ctl_io, except
3905 * for the pool reference. This includes any memory that has been
3906 * allocated! That memory will no longer be valid after done has been
3907 * called, so this would be VERY DANGEROUS for command that actually does
3908 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3909 * start and stop commands, which don't transfer any data, so this is not a
3910 * problem. If it is used for anything else, the caller would also need to
3911 * allocate data buffer space and this routine would need to be modified to
3912 * copy the data buffer(s) as well.
3915 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
3924 * May need to preserve linked list pointers at some point too.
3926 pool_ref = dest->io_hdr.pool;
3928 memcpy(dest, src, ctl_min(sizeof(*src), sizeof(*dest)));
3930 dest->io_hdr.pool = pool_ref;
3932 * We need to know that this is an internal copy, and doesn't need
3933 * to get passed back to the FETD that allocated it.
3935 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
3940 ctl_update_power_subpage(struct copan_power_subpage *page)
3942 int num_luns, num_partitions, config_type;
3943 struct ctl_softc *softc;
3944 cs_BOOL_t aor_present, shelf_50pct_power;
3945 cs_raidset_personality_t rs_type;
3946 int max_active_luns;
3948 softc = control_softc;
3950 /* subtract out the processor LUN */
3951 num_luns = softc->num_luns - 1;
3953 * Default to 7 LUNs active, which was the only number we allowed
3956 max_active_luns = 7;
3958 num_partitions = config_GetRsPartitionInfo();
3959 config_type = config_GetConfigType();
3960 shelf_50pct_power = config_GetShelfPowerMode();
3961 aor_present = config_IsAorRsPresent();
3963 rs_type = ddb_GetRsRaidType(1);
3964 if ((rs_type != CS_RAIDSET_PERSONALITY_RAID5)
3965 && (rs_type != CS_RAIDSET_PERSONALITY_RAID1)) {
3966 EPRINT(0, "Unsupported RS type %d!", rs_type);
3970 page->total_luns = num_luns;
3972 switch (config_type) {
3975 * In a 40 drive configuration, it doesn't matter what DC
3976 * cards we have, whether we have AOR enabled or not,
3977 * partitioning or not, or what type of RAIDset we have.
3978 * In that scenario, we can power up every LUN we present
3981 max_active_luns = num_luns;
3985 if (shelf_50pct_power == CS_FALSE) {
3987 if (aor_present == CS_TRUE) {
3989 CS_RAIDSET_PERSONALITY_RAID5) {
3990 max_active_luns = 7;
3991 } else if (rs_type ==
3992 CS_RAIDSET_PERSONALITY_RAID1){
3993 max_active_luns = 14;
3995 /* XXX KDM now what?? */
3999 CS_RAIDSET_PERSONALITY_RAID5) {
4000 max_active_luns = 8;
4001 } else if (rs_type ==
4002 CS_RAIDSET_PERSONALITY_RAID1){
4003 max_active_luns = 16;
4005 /* XXX KDM now what?? */
4011 * With 50% power in a 64 drive configuration, we
4012 * can power all LUNs we present.
4014 max_active_luns = num_luns;
4018 if (shelf_50pct_power == CS_FALSE) {
4020 if (aor_present == CS_TRUE) {
4022 CS_RAIDSET_PERSONALITY_RAID5) {
4023 max_active_luns = 7;
4024 } else if (rs_type ==
4025 CS_RAIDSET_PERSONALITY_RAID1){
4026 max_active_luns = 14;
4028 /* XXX KDM now what?? */
4032 CS_RAIDSET_PERSONALITY_RAID5) {
4033 max_active_luns = 8;
4034 } else if (rs_type ==
4035 CS_RAIDSET_PERSONALITY_RAID1){
4036 max_active_luns = 16;
4038 /* XXX KDM now what?? */
4043 if (aor_present == CS_TRUE) {
4045 CS_RAIDSET_PERSONALITY_RAID5) {
4046 max_active_luns = 14;
4047 } else if (rs_type ==
4048 CS_RAIDSET_PERSONALITY_RAID1){
4050 * We're assuming here that disk
4051 * caching is enabled, and so we're
4052 * able to power up half of each
4053 * LUN, and cache all writes.
4055 max_active_luns = num_luns;
4057 /* XXX KDM now what?? */
4061 CS_RAIDSET_PERSONALITY_RAID5) {
4062 max_active_luns = 15;
4063 } else if (rs_type ==
4064 CS_RAIDSET_PERSONALITY_RAID1){
4065 max_active_luns = 30;
4067 /* XXX KDM now what?? */
4074 * In this case, we have an unknown configuration, so we
4075 * just use the default from above.
4080 page->max_active_luns = max_active_luns;
4082 printk("%s: total_luns = %d, max_active_luns = %d\n", __func__,
4083 page->total_luns, page->max_active_luns);
4086 #endif /* NEEDTOPORT */
4089 * This routine could be used in the future to load default and/or saved
4090 * mode page parameters for a particuar lun.
4093 ctl_init_page_index(struct ctl_lun *lun)
4096 struct ctl_page_index *page_index;
4097 struct ctl_softc *softc;
4099 memcpy(&lun->mode_pages.index, page_index_template,
4100 sizeof(page_index_template));
4102 softc = lun->ctl_softc;
4104 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4106 page_index = &lun->mode_pages.index[i];
4108 * If this is a disk-only mode page, there's no point in
4109 * setting it up. For some pages, we have to have some
4110 * basic information about the disk in order to calculate the
4113 if ((lun->be_lun->lun_type != T_DIRECT)
4114 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4117 switch (page_index->page_code & SMPH_PC_MASK) {
4118 case SMS_FORMAT_DEVICE_PAGE: {
4119 struct scsi_format_page *format_page;
4121 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4122 panic("subpage is incorrect!");
4125 * Sectors per track are set above. Bytes per
4126 * sector need to be set here on a per-LUN basis.
4128 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4129 &format_page_default,
4130 sizeof(format_page_default));
4131 memcpy(&lun->mode_pages.format_page[
4132 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4133 sizeof(format_page_changeable));
4134 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4135 &format_page_default,
4136 sizeof(format_page_default));
4137 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4138 &format_page_default,
4139 sizeof(format_page_default));
4141 format_page = &lun->mode_pages.format_page[
4143 scsi_ulto2b(lun->be_lun->blocksize,
4144 format_page->bytes_per_sector);
4146 format_page = &lun->mode_pages.format_page[
4148 scsi_ulto2b(lun->be_lun->blocksize,
4149 format_page->bytes_per_sector);
4151 format_page = &lun->mode_pages.format_page[
4153 scsi_ulto2b(lun->be_lun->blocksize,
4154 format_page->bytes_per_sector);
4156 page_index->page_data =
4157 (uint8_t *)lun->mode_pages.format_page;
4160 case SMS_RIGID_DISK_PAGE: {
4161 struct scsi_rigid_disk_page *rigid_disk_page;
4162 uint32_t sectors_per_cylinder;
4166 #endif /* !__XSCALE__ */
4168 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4169 panic("invalid subpage value %d",
4170 page_index->subpage);
4173 * Rotation rate and sectors per track are set
4174 * above. We calculate the cylinders here based on
4175 * capacity. Due to the number of heads and
4176 * sectors per track we're using, smaller arrays
4177 * may turn out to have 0 cylinders. Linux and
4178 * FreeBSD don't pay attention to these mode pages
4179 * to figure out capacity, but Solaris does. It
4180 * seems to deal with 0 cylinders just fine, and
4181 * works out a fake geometry based on the capacity.
4183 memcpy(&lun->mode_pages.rigid_disk_page[
4184 CTL_PAGE_CURRENT], &rigid_disk_page_default,
4185 sizeof(rigid_disk_page_default));
4186 memcpy(&lun->mode_pages.rigid_disk_page[
4187 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4188 sizeof(rigid_disk_page_changeable));
4189 memcpy(&lun->mode_pages.rigid_disk_page[
4190 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4191 sizeof(rigid_disk_page_default));
4192 memcpy(&lun->mode_pages.rigid_disk_page[
4193 CTL_PAGE_SAVED], &rigid_disk_page_default,
4194 sizeof(rigid_disk_page_default));
4196 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4200 * The divide method here will be more accurate,
4201 * probably, but results in floating point being
4202 * used in the kernel on i386 (__udivdi3()). On the
4203 * XScale, though, __udivdi3() is implemented in
4206 * The shift method for cylinder calculation is
4207 * accurate if sectors_per_cylinder is a power of
4208 * 2. Otherwise it might be slightly off -- you
4209 * might have a bit of a truncation problem.
4212 cylinders = (lun->be_lun->maxlba + 1) /
4213 sectors_per_cylinder;
4215 for (shift = 31; shift > 0; shift--) {
4216 if (sectors_per_cylinder & (1 << shift))
4219 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4223 * We've basically got 3 bytes, or 24 bits for the
4224 * cylinder size in the mode page. If we're over,
4225 * just round down to 2^24.
4227 if (cylinders > 0xffffff)
4228 cylinders = 0xffffff;
4230 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4232 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4234 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4236 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4238 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4240 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4242 page_index->page_data =
4243 (uint8_t *)lun->mode_pages.rigid_disk_page;
4246 case SMS_CACHING_PAGE: {
4248 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4249 panic("invalid subpage value %d",
4250 page_index->subpage);
4252 * Defaults should be okay here, no calculations
4255 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4256 &caching_page_default,
4257 sizeof(caching_page_default));
4258 memcpy(&lun->mode_pages.caching_page[
4259 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4260 sizeof(caching_page_changeable));
4261 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4262 &caching_page_default,
4263 sizeof(caching_page_default));
4264 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4265 &caching_page_default,
4266 sizeof(caching_page_default));
4267 page_index->page_data =
4268 (uint8_t *)lun->mode_pages.caching_page;
4271 case SMS_CONTROL_MODE_PAGE: {
4273 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4274 panic("invalid subpage value %d",
4275 page_index->subpage);
4278 * Defaults should be okay here, no calculations
4281 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4282 &control_page_default,
4283 sizeof(control_page_default));
4284 memcpy(&lun->mode_pages.control_page[
4285 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4286 sizeof(control_page_changeable));
4287 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4288 &control_page_default,
4289 sizeof(control_page_default));
4290 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4291 &control_page_default,
4292 sizeof(control_page_default));
4293 page_index->page_data =
4294 (uint8_t *)lun->mode_pages.control_page;
4298 case SMS_VENDOR_SPECIFIC_PAGE:{
4299 switch (page_index->subpage) {
4300 case PWR_SUBPAGE_CODE: {
4301 struct copan_power_subpage *current_page,
4304 memcpy(&lun->mode_pages.power_subpage[
4306 &power_page_default,
4307 sizeof(power_page_default));
4308 memcpy(&lun->mode_pages.power_subpage[
4309 CTL_PAGE_CHANGEABLE],
4310 &power_page_changeable,
4311 sizeof(power_page_changeable));
4312 memcpy(&lun->mode_pages.power_subpage[
4314 &power_page_default,
4315 sizeof(power_page_default));
4316 memcpy(&lun->mode_pages.power_subpage[
4318 &power_page_default,
4319 sizeof(power_page_default));
4320 page_index->page_data =
4321 (uint8_t *)lun->mode_pages.power_subpage;
4323 current_page = (struct copan_power_subpage *)
4324 (page_index->page_data +
4325 (page_index->page_len *
4327 saved_page = (struct copan_power_subpage *)
4328 (page_index->page_data +
4329 (page_index->page_len *
4333 case APS_SUBPAGE_CODE: {
4334 struct copan_aps_subpage *current_page,
4337 // This gets set multiple times but
4338 // it should always be the same. It's
4339 // only done during init so who cares.
4340 index_to_aps_page = i;
4342 memcpy(&lun->mode_pages.aps_subpage[
4345 sizeof(aps_page_default));
4346 memcpy(&lun->mode_pages.aps_subpage[
4347 CTL_PAGE_CHANGEABLE],
4348 &aps_page_changeable,
4349 sizeof(aps_page_changeable));
4350 memcpy(&lun->mode_pages.aps_subpage[
4353 sizeof(aps_page_default));
4354 memcpy(&lun->mode_pages.aps_subpage[
4357 sizeof(aps_page_default));
4358 page_index->page_data =
4359 (uint8_t *)lun->mode_pages.aps_subpage;
4361 current_page = (struct copan_aps_subpage *)
4362 (page_index->page_data +
4363 (page_index->page_len *
4365 saved_page = (struct copan_aps_subpage *)
4366 (page_index->page_data +
4367 (page_index->page_len *
4371 case DBGCNF_SUBPAGE_CODE: {
4372 struct copan_debugconf_subpage *current_page,
4375 memcpy(&lun->mode_pages.debugconf_subpage[
4377 &debugconf_page_default,
4378 sizeof(debugconf_page_default));
4379 memcpy(&lun->mode_pages.debugconf_subpage[
4380 CTL_PAGE_CHANGEABLE],
4381 &debugconf_page_changeable,
4382 sizeof(debugconf_page_changeable));
4383 memcpy(&lun->mode_pages.debugconf_subpage[
4385 &debugconf_page_default,
4386 sizeof(debugconf_page_default));
4387 memcpy(&lun->mode_pages.debugconf_subpage[
4389 &debugconf_page_default,
4390 sizeof(debugconf_page_default));
4391 page_index->page_data =
4392 (uint8_t *)lun->mode_pages.debugconf_subpage;
4394 current_page = (struct copan_debugconf_subpage *)
4395 (page_index->page_data +
4396 (page_index->page_len *
4398 saved_page = (struct copan_debugconf_subpage *)
4399 (page_index->page_data +
4400 (page_index->page_len *
4405 panic("invalid subpage value %d",
4406 page_index->subpage);
4412 panic("invalid page value %d",
4413 page_index->page_code & SMPH_PC_MASK);
4418 return (CTL_RETVAL_COMPLETE);
4425 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4426 * wants us to allocate the LUN and he can block.
4427 * - ctl_softc is always set
4428 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4430 * Returns 0 for success, non-zero (errno) for failure.
4433 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4434 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4436 struct ctl_lun *nlun, *lun;
4437 struct ctl_port *port;
4438 struct scsi_vpd_id_descriptor *desc;
4439 struct scsi_vpd_id_t10 *t10id;
4440 const char *eui, *naa, *scsiname, *vendor;
4441 int lun_number, i, lun_malloced;
4442 int devidlen, idlen1, idlen2 = 0, len;
4448 * We currently only support Direct Access or Processor LUN types.
4450 switch (be_lun->lun_type) {
4458 be_lun->lun_config_status(be_lun->be_lun,
4459 CTL_LUN_CONFIG_FAILURE);
4462 if (ctl_lun == NULL) {
4463 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4470 memset(lun, 0, sizeof(*lun));
4472 lun->flags = CTL_LUN_MALLOCED;
4474 /* Generate LUN ID. */
4475 devidlen = max(CTL_DEVID_MIN_LEN,
4476 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4477 idlen1 = sizeof(*t10id) + devidlen;
4478 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4479 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4480 if (scsiname != NULL) {
4481 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4482 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4484 eui = ctl_get_opt(&be_lun->options, "eui");
4486 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
4488 naa = ctl_get_opt(&be_lun->options, "naa");
4490 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
4492 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4493 M_CTL, M_WAITOK | M_ZERO);
4494 lun->lun_devid->len = len;
4495 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4496 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4497 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4498 desc->length = idlen1;
4499 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4500 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4501 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4502 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4504 strncpy(t10id->vendor, vendor,
4505 min(sizeof(t10id->vendor), strlen(vendor)));
4507 strncpy((char *)t10id->vendor_spec_id,
4508 (char *)be_lun->device_id, devidlen);
4509 if (scsiname != NULL) {
4510 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4512 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4513 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4514 SVPD_ID_TYPE_SCSI_NAME;
4515 desc->length = idlen2;
4516 strlcpy(desc->identifier, scsiname, idlen2);
4519 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4521 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4522 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4525 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier);
4528 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4530 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4531 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4534 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier);
4537 mtx_lock(&ctl_softc->ctl_lock);
4539 * See if the caller requested a particular LUN number. If so, see
4540 * if it is available. Otherwise, allocate the first available LUN.
4542 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4543 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4544 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4545 mtx_unlock(&ctl_softc->ctl_lock);
4546 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4547 printf("ctl: requested LUN ID %d is higher "
4548 "than CTL_MAX_LUNS - 1 (%d)\n",
4549 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4552 * XXX KDM return an error, or just assign
4553 * another LUN ID in this case??
4555 printf("ctl: requested LUN ID %d is already "
4556 "in use\n", be_lun->req_lun_id);
4558 if (lun->flags & CTL_LUN_MALLOCED)
4560 be_lun->lun_config_status(be_lun->be_lun,
4561 CTL_LUN_CONFIG_FAILURE);
4564 lun_number = be_lun->req_lun_id;
4566 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4567 if (lun_number == -1) {
4568 mtx_unlock(&ctl_softc->ctl_lock);
4569 printf("ctl: can't allocate LUN on target %ju, out of "
4570 "LUNs\n", (uintmax_t)target_id.id);
4571 if (lun->flags & CTL_LUN_MALLOCED)
4573 be_lun->lun_config_status(be_lun->be_lun,
4574 CTL_LUN_CONFIG_FAILURE);
4578 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4580 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4581 lun->target = target_id;
4582 lun->lun = lun_number;
4583 lun->be_lun = be_lun;
4585 * The processor LUN is always enabled. Disk LUNs come on line
4586 * disabled, and must be enabled by the backend.
4588 lun->flags |= CTL_LUN_DISABLED;
4589 lun->backend = be_lun->be;
4590 be_lun->ctl_lun = lun;
4591 be_lun->lun_id = lun_number;
4592 atomic_add_int(&be_lun->be->num_luns, 1);
4593 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4594 lun->flags |= CTL_LUN_STOPPED;
4596 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4597 lun->flags |= CTL_LUN_INOPERABLE;
4599 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4600 lun->flags |= CTL_LUN_PRIMARY_SC;
4602 lun->ctl_softc = ctl_softc;
4603 TAILQ_INIT(&lun->ooa_queue);
4604 TAILQ_INIT(&lun->blocked_queue);
4605 STAILQ_INIT(&lun->error_list);
4606 ctl_tpc_lun_init(lun);
4609 * Initialize the mode page index.
4611 ctl_init_page_index(lun);
4614 * Set the poweron UA for all initiators on this LUN only.
4616 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4617 lun->pending_ua[i] = CTL_UA_POWERON;
4620 * Now, before we insert this lun on the lun list, set the lun
4621 * inventory changed UA for all other luns.
4623 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4624 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4625 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4629 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4631 ctl_softc->ctl_luns[lun_number] = lun;
4633 ctl_softc->num_luns++;
4635 /* Setup statistics gathering */
4636 lun->stats.device_type = be_lun->lun_type;
4637 lun->stats.lun_number = lun_number;
4638 if (lun->stats.device_type == T_DIRECT)
4639 lun->stats.blocksize = be_lun->blocksize;
4641 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4642 for (i = 0;i < CTL_MAX_PORTS;i++)
4643 lun->stats.ports[i].targ_port = i;
4645 mtx_unlock(&ctl_softc->ctl_lock);
4647 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4650 * Run through each registered FETD and bring it online if it isn't
4651 * already. Enable the target ID if it hasn't been enabled, and
4652 * enable this particular LUN.
4654 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4657 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4659 printf("ctl_alloc_lun: FETD %s port %d returned error "
4660 "%d for lun_enable on target %ju lun %d\n",
4661 port->port_name, port->targ_port, retval,
4662 (uintmax_t)target_id.id, lun_number);
4664 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4672 * - LUN has already been marked invalid and any pending I/O has been taken
4676 ctl_free_lun(struct ctl_lun *lun)
4678 struct ctl_softc *softc;
4680 struct ctl_port *port;
4682 struct ctl_lun *nlun;
4685 softc = lun->ctl_softc;
4687 mtx_assert(&softc->ctl_lock, MA_OWNED);
4689 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4691 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4693 softc->ctl_luns[lun->lun] = NULL;
4695 if (!TAILQ_EMPTY(&lun->ooa_queue))
4696 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4701 * XXX KDM this scheme only works for a single target/multiple LUN
4702 * setup. It needs to be revamped for a multiple target scheme.
4704 * XXX KDM this results in port->lun_disable() getting called twice,
4705 * once when ctl_disable_lun() is called, and a second time here.
4706 * We really need to re-think the LUN disable semantics. There
4707 * should probably be several steps/levels to LUN removal:
4712 * Right now we only have a disable method when communicating to
4713 * the front end ports, at least for individual LUNs.
4716 STAILQ_FOREACH(port, &softc->port_list, links) {
4719 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4722 printf("ctl_free_lun: FETD %s port %d returned error "
4723 "%d for lun_disable on target %ju lun %jd\n",
4724 port->port_name, port->targ_port, retval,
4725 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4728 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4729 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4731 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4733 printf("ctl_free_lun: FETD %s port %d "
4734 "returned error %d for targ_disable on "
4735 "target %ju\n", port->port_name,
4736 port->targ_port, retval,
4737 (uintmax_t)lun->target.id);
4739 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4741 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4745 port->port_offline(port->onoff_arg);
4746 port->status &= ~CTL_PORT_STATUS_ONLINE;
4753 * Tell the backend to free resources, if this LUN has a backend.
4755 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4756 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4758 ctl_tpc_lun_shutdown(lun);
4759 mtx_destroy(&lun->lun_lock);
4760 free(lun->lun_devid, M_CTL);
4761 if (lun->flags & CTL_LUN_MALLOCED)
4764 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4765 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4766 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4774 ctl_create_lun(struct ctl_be_lun *be_lun)
4776 struct ctl_softc *ctl_softc;
4778 ctl_softc = control_softc;
4781 * ctl_alloc_lun() should handle all potential failure cases.
4783 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4787 ctl_add_lun(struct ctl_be_lun *be_lun)
4789 struct ctl_softc *ctl_softc = control_softc;
4791 mtx_lock(&ctl_softc->ctl_lock);
4792 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4793 mtx_unlock(&ctl_softc->ctl_lock);
4794 wakeup(&ctl_softc->pending_lun_queue);
4800 ctl_enable_lun(struct ctl_be_lun *be_lun)
4802 struct ctl_softc *ctl_softc;
4803 struct ctl_port *port, *nport;
4804 struct ctl_lun *lun;
4807 ctl_softc = control_softc;
4809 lun = (struct ctl_lun *)be_lun->ctl_lun;
4811 mtx_lock(&ctl_softc->ctl_lock);
4812 mtx_lock(&lun->lun_lock);
4813 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4815 * eh? Why did we get called if the LUN is already
4818 mtx_unlock(&lun->lun_lock);
4819 mtx_unlock(&ctl_softc->ctl_lock);
4822 lun->flags &= ~CTL_LUN_DISABLED;
4823 mtx_unlock(&lun->lun_lock);
4825 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4826 nport = STAILQ_NEXT(port, links);
4829 * Drop the lock while we call the FETD's enable routine.
4830 * This can lead to a callback into CTL (at least in the
4831 * case of the internal initiator frontend.
4833 mtx_unlock(&ctl_softc->ctl_lock);
4834 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4835 mtx_lock(&ctl_softc->ctl_lock);
4837 printf("%s: FETD %s port %d returned error "
4838 "%d for lun_enable on target %ju lun %jd\n",
4839 __func__, port->port_name, port->targ_port, retval,
4840 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4844 /* NOTE: TODO: why does lun enable affect port status? */
4845 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4850 mtx_unlock(&ctl_softc->ctl_lock);
4856 ctl_disable_lun(struct ctl_be_lun *be_lun)
4858 struct ctl_softc *ctl_softc;
4859 struct ctl_port *port;
4860 struct ctl_lun *lun;
4863 ctl_softc = control_softc;
4865 lun = (struct ctl_lun *)be_lun->ctl_lun;
4867 mtx_lock(&ctl_softc->ctl_lock);
4868 mtx_lock(&lun->lun_lock);
4869 if (lun->flags & CTL_LUN_DISABLED) {
4870 mtx_unlock(&lun->lun_lock);
4871 mtx_unlock(&ctl_softc->ctl_lock);
4874 lun->flags |= CTL_LUN_DISABLED;
4875 mtx_unlock(&lun->lun_lock);
4877 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4878 mtx_unlock(&ctl_softc->ctl_lock);
4880 * Drop the lock before we call the frontend's disable
4881 * routine, to avoid lock order reversals.
4883 * XXX KDM what happens if the frontend list changes while
4884 * we're traversing it? It's unlikely, but should be handled.
4886 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4888 mtx_lock(&ctl_softc->ctl_lock);
4890 printf("ctl_alloc_lun: FETD %s port %d returned error "
4891 "%d for lun_disable on target %ju lun %jd\n",
4892 port->port_name, port->targ_port, retval,
4893 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4897 mtx_unlock(&ctl_softc->ctl_lock);
4903 ctl_start_lun(struct ctl_be_lun *be_lun)
4905 struct ctl_softc *ctl_softc;
4906 struct ctl_lun *lun;
4908 ctl_softc = control_softc;
4910 lun = (struct ctl_lun *)be_lun->ctl_lun;
4912 mtx_lock(&lun->lun_lock);
4913 lun->flags &= ~CTL_LUN_STOPPED;
4914 mtx_unlock(&lun->lun_lock);
4920 ctl_stop_lun(struct ctl_be_lun *be_lun)
4922 struct ctl_softc *ctl_softc;
4923 struct ctl_lun *lun;
4925 ctl_softc = control_softc;
4927 lun = (struct ctl_lun *)be_lun->ctl_lun;
4929 mtx_lock(&lun->lun_lock);
4930 lun->flags |= CTL_LUN_STOPPED;
4931 mtx_unlock(&lun->lun_lock);
4937 ctl_lun_offline(struct ctl_be_lun *be_lun)
4939 struct ctl_softc *ctl_softc;
4940 struct ctl_lun *lun;
4942 ctl_softc = control_softc;
4944 lun = (struct ctl_lun *)be_lun->ctl_lun;
4946 mtx_lock(&lun->lun_lock);
4947 lun->flags |= CTL_LUN_OFFLINE;
4948 mtx_unlock(&lun->lun_lock);
4954 ctl_lun_online(struct ctl_be_lun *be_lun)
4956 struct ctl_softc *ctl_softc;
4957 struct ctl_lun *lun;
4959 ctl_softc = control_softc;
4961 lun = (struct ctl_lun *)be_lun->ctl_lun;
4963 mtx_lock(&lun->lun_lock);
4964 lun->flags &= ~CTL_LUN_OFFLINE;
4965 mtx_unlock(&lun->lun_lock);
4971 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4973 struct ctl_softc *ctl_softc;
4974 struct ctl_lun *lun;
4976 ctl_softc = control_softc;
4978 lun = (struct ctl_lun *)be_lun->ctl_lun;
4980 mtx_lock(&lun->lun_lock);
4983 * The LUN needs to be disabled before it can be marked invalid.
4985 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4986 mtx_unlock(&lun->lun_lock);
4990 * Mark the LUN invalid.
4992 lun->flags |= CTL_LUN_INVALID;
4995 * If there is nothing in the OOA queue, go ahead and free the LUN.
4996 * If we have something in the OOA queue, we'll free it when the
4997 * last I/O completes.
4999 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5000 mtx_unlock(&lun->lun_lock);
5001 mtx_lock(&ctl_softc->ctl_lock);
5003 mtx_unlock(&ctl_softc->ctl_lock);
5005 mtx_unlock(&lun->lun_lock);
5011 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
5013 struct ctl_softc *ctl_softc;
5014 struct ctl_lun *lun;
5016 ctl_softc = control_softc;
5017 lun = (struct ctl_lun *)be_lun->ctl_lun;
5019 mtx_lock(&lun->lun_lock);
5020 lun->flags |= CTL_LUN_INOPERABLE;
5021 mtx_unlock(&lun->lun_lock);
5027 ctl_lun_operable(struct ctl_be_lun *be_lun)
5029 struct ctl_softc *ctl_softc;
5030 struct ctl_lun *lun;
5032 ctl_softc = control_softc;
5033 lun = (struct ctl_lun *)be_lun->ctl_lun;
5035 mtx_lock(&lun->lun_lock);
5036 lun->flags &= ~CTL_LUN_INOPERABLE;
5037 mtx_unlock(&lun->lun_lock);
5043 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
5046 struct ctl_softc *softc;
5047 struct ctl_lun *lun;
5048 struct copan_aps_subpage *current_sp;
5049 struct ctl_page_index *page_index;
5052 softc = control_softc;
5054 mtx_lock(&softc->ctl_lock);
5056 lun = (struct ctl_lun *)be_lun->ctl_lun;
5057 mtx_lock(&lun->lun_lock);
5060 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5061 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
5065 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
5067 page_index = &lun->mode_pages.index[i];
5070 if (page_index == NULL) {
5071 mtx_unlock(&lun->lun_lock);
5072 mtx_unlock(&softc->ctl_lock);
5073 printf("%s: APS subpage not found for lun %ju!\n", __func__,
5074 (uintmax_t)lun->lun);
5078 if ((softc->aps_locked_lun != 0)
5079 && (softc->aps_locked_lun != lun->lun)) {
5080 printf("%s: attempt to lock LUN %llu when %llu is already "
5082 mtx_unlock(&lun->lun_lock);
5083 mtx_unlock(&softc->ctl_lock);
5088 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5089 (page_index->page_len * CTL_PAGE_CURRENT));
5092 current_sp->lock_active = APS_LOCK_ACTIVE;
5093 softc->aps_locked_lun = lun->lun;
5095 current_sp->lock_active = 0;
5096 softc->aps_locked_lun = 0;
5101 * If we're in HA mode, try to send the lock message to the other
5104 if (ctl_is_single == 0) {
5106 union ctl_ha_msg lock_msg;
5108 lock_msg.hdr.nexus = *nexus;
5109 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
5111 lock_msg.aps.lock_flag = 1;
5113 lock_msg.aps.lock_flag = 0;
5114 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
5115 sizeof(lock_msg), 0);
5116 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
5117 printf("%s: APS (lock=%d) error returned from "
5118 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
5119 mtx_unlock(&lun->lun_lock);
5120 mtx_unlock(&softc->ctl_lock);
5125 mtx_unlock(&lun->lun_lock);
5126 mtx_unlock(&softc->ctl_lock);
5132 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5134 struct ctl_lun *lun;
5135 struct ctl_softc *softc;
5138 softc = control_softc;
5140 lun = (struct ctl_lun *)be_lun->ctl_lun;
5142 mtx_lock(&lun->lun_lock);
5144 for (i = 0; i < CTL_MAX_INITIATORS; i++)
5145 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED;
5147 mtx_unlock(&lun->lun_lock);
5151 * Backend "memory move is complete" callback for requests that never
5152 * make it down to say RAIDCore's configuration code.
5155 ctl_config_move_done(union ctl_io *io)
5159 retval = CTL_RETVAL_COMPLETE;
5162 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5164 * XXX KDM this shouldn't happen, but what if it does?
5166 if (io->io_hdr.io_type != CTL_IO_SCSI)
5167 panic("I/O type isn't CTL_IO_SCSI!");
5169 if ((io->io_hdr.port_status == 0)
5170 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5171 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
5172 io->io_hdr.status = CTL_SUCCESS;
5173 else if ((io->io_hdr.port_status != 0)
5174 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5175 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
5177 * For hardware error sense keys, the sense key
5178 * specific value is defined to be a retry count,
5179 * but we use it to pass back an internal FETD
5180 * error code. XXX KDM Hopefully the FETD is only
5181 * using 16 bits for an error code, since that's
5182 * all the space we have in the sks field.
5184 ctl_set_internal_failure(&io->scsiio,
5187 io->io_hdr.port_status);
5188 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5189 free(io->scsiio.kern_data_ptr, M_CTL);
5194 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
5195 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
5196 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5198 * XXX KDM just assuming a single pointer here, and not a
5199 * S/G list. If we start using S/G lists for config data,
5200 * we'll need to know how to clean them up here as well.
5202 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5203 free(io->scsiio.kern_data_ptr, M_CTL);
5204 /* Hopefully the user has already set the status... */
5208 * XXX KDM now we need to continue data movement. Some
5210 * - call ctl_scsiio() again? We don't do this for data
5211 * writes, because for those at least we know ahead of
5212 * time where the write will go and how long it is. For
5213 * config writes, though, that information is largely
5214 * contained within the write itself, thus we need to
5215 * parse out the data again.
5217 * - Call some other function once the data is in?
5221 * XXX KDM call ctl_scsiio() again for now, and check flag
5222 * bits to see whether we're allocated or not.
5224 retval = ctl_scsiio(&io->scsiio);
5231 * This gets called by a backend driver when it is done with a
5232 * data_submit method.
5235 ctl_data_submit_done(union ctl_io *io)
5238 * If the IO_CONT flag is set, we need to call the supplied
5239 * function to continue processing the I/O, instead of completing
5242 * If there is an error, though, we don't want to keep processing.
5243 * Instead, just send status back to the initiator.
5245 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5246 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5247 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5248 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5249 io->scsiio.io_cont(io);
5256 * This gets called by a backend driver when it is done with a
5257 * configuration write.
5260 ctl_config_write_done(union ctl_io *io)
5263 * If the IO_CONT flag is set, we need to call the supplied
5264 * function to continue processing the I/O, instead of completing
5267 * If there is an error, though, we don't want to keep processing.
5268 * Instead, just send status back to the initiator.
5270 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5271 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5272 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5273 io->scsiio.io_cont(io);
5277 * Since a configuration write can be done for commands that actually
5278 * have data allocated, like write buffer, and commands that have
5279 * no data, like start/stop unit, we need to check here.
5281 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
5282 free(io->scsiio.kern_data_ptr, M_CTL);
5287 * SCSI release command.
5290 ctl_scsi_release(struct ctl_scsiio *ctsio)
5292 int length, longid, thirdparty_id, resv_id;
5293 struct ctl_softc *ctl_softc;
5294 struct ctl_lun *lun;
5299 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5301 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5302 ctl_softc = control_softc;
5304 switch (ctsio->cdb[0]) {
5306 struct scsi_release_10 *cdb;
5308 cdb = (struct scsi_release_10 *)ctsio->cdb;
5310 if (cdb->byte2 & SR10_LONGID)
5313 thirdparty_id = cdb->thirdparty_id;
5315 resv_id = cdb->resv_id;
5316 length = scsi_2btoul(cdb->length);
5323 * XXX KDM right now, we only support LUN reservation. We don't
5324 * support 3rd party reservations, or extent reservations, which
5325 * might actually need the parameter list. If we've gotten this
5326 * far, we've got a LUN reservation. Anything else got kicked out
5327 * above. So, according to SPC, ignore the length.
5331 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5333 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5334 ctsio->kern_data_len = length;
5335 ctsio->kern_total_len = length;
5336 ctsio->kern_data_resid = 0;
5337 ctsio->kern_rel_offset = 0;
5338 ctsio->kern_sg_entries = 0;
5339 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5340 ctsio->be_move_done = ctl_config_move_done;
5341 ctl_datamove((union ctl_io *)ctsio);
5343 return (CTL_RETVAL_COMPLETE);
5347 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5349 mtx_lock(&lun->lun_lock);
5352 * According to SPC, it is not an error for an intiator to attempt
5353 * to release a reservation on a LUN that isn't reserved, or that
5354 * is reserved by another initiator. The reservation can only be
5355 * released, though, by the initiator who made it or by one of
5356 * several reset type events.
5358 if (lun->flags & CTL_LUN_RESERVED) {
5359 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5360 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5361 && (ctsio->io_hdr.nexus.targ_target.id ==
5362 lun->rsv_nexus.targ_target.id)) {
5363 lun->flags &= ~CTL_LUN_RESERVED;
5367 mtx_unlock(&lun->lun_lock);
5369 ctsio->scsi_status = SCSI_STATUS_OK;
5370 ctsio->io_hdr.status = CTL_SUCCESS;
5372 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5373 free(ctsio->kern_data_ptr, M_CTL);
5374 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5377 ctl_done((union ctl_io *)ctsio);
5378 return (CTL_RETVAL_COMPLETE);
5382 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5384 int extent, thirdparty, longid;
5385 int resv_id, length;
5386 uint64_t thirdparty_id;
5387 struct ctl_softc *ctl_softc;
5388 struct ctl_lun *lun;
5397 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5399 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5400 ctl_softc = control_softc;
5402 switch (ctsio->cdb[0]) {
5404 struct scsi_reserve_10 *cdb;
5406 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5408 if (cdb->byte2 & SR10_LONGID)
5411 thirdparty_id = cdb->thirdparty_id;
5413 resv_id = cdb->resv_id;
5414 length = scsi_2btoul(cdb->length);
5420 * XXX KDM right now, we only support LUN reservation. We don't
5421 * support 3rd party reservations, or extent reservations, which
5422 * might actually need the parameter list. If we've gotten this
5423 * far, we've got a LUN reservation. Anything else got kicked out
5424 * above. So, according to SPC, ignore the length.
5428 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5430 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5431 ctsio->kern_data_len = length;
5432 ctsio->kern_total_len = length;
5433 ctsio->kern_data_resid = 0;
5434 ctsio->kern_rel_offset = 0;
5435 ctsio->kern_sg_entries = 0;
5436 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5437 ctsio->be_move_done = ctl_config_move_done;
5438 ctl_datamove((union ctl_io *)ctsio);
5440 return (CTL_RETVAL_COMPLETE);
5444 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5446 mtx_lock(&lun->lun_lock);
5447 if (lun->flags & CTL_LUN_RESERVED) {
5448 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5449 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5450 || (ctsio->io_hdr.nexus.targ_target.id !=
5451 lun->rsv_nexus.targ_target.id)) {
5452 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5453 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5458 lun->flags |= CTL_LUN_RESERVED;
5459 lun->rsv_nexus = ctsio->io_hdr.nexus;
5461 ctsio->scsi_status = SCSI_STATUS_OK;
5462 ctsio->io_hdr.status = CTL_SUCCESS;
5465 mtx_unlock(&lun->lun_lock);
5467 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5468 free(ctsio->kern_data_ptr, M_CTL);
5469 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5472 ctl_done((union ctl_io *)ctsio);
5473 return (CTL_RETVAL_COMPLETE);
5477 ctl_start_stop(struct ctl_scsiio *ctsio)
5479 struct scsi_start_stop_unit *cdb;
5480 struct ctl_lun *lun;
5481 struct ctl_softc *ctl_softc;
5484 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5486 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5487 ctl_softc = control_softc;
5490 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5494 * We don't support the immediate bit on a stop unit. In order to
5495 * do that, we would need to code up a way to know that a stop is
5496 * pending, and hold off any new commands until it completes, one
5497 * way or another. Then we could accept or reject those commands
5498 * depending on its status. We would almost need to do the reverse
5499 * of what we do below for an immediate start -- return the copy of
5500 * the ctl_io to the FETD with status to send to the host (and to
5501 * free the copy!) and then free the original I/O once the stop
5502 * actually completes. That way, the OOA queue mechanism can work
5503 * to block commands that shouldn't proceed. Another alternative
5504 * would be to put the copy in the queue in place of the original,
5505 * and return the original back to the caller. That could be
5508 if ((cdb->byte2 & SSS_IMMED)
5509 && ((cdb->how & SSS_START) == 0)) {
5510 ctl_set_invalid_field(ctsio,
5516 ctl_done((union ctl_io *)ctsio);
5517 return (CTL_RETVAL_COMPLETE);
5520 if ((lun->flags & CTL_LUN_PR_RESERVED)
5521 && ((cdb->how & SSS_START)==0)) {
5524 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5525 if (!lun->per_res[residx].registered
5526 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5528 ctl_set_reservation_conflict(ctsio);
5529 ctl_done((union ctl_io *)ctsio);
5530 return (CTL_RETVAL_COMPLETE);
5535 * If there is no backend on this device, we can't start or stop
5536 * it. In theory we shouldn't get any start/stop commands in the
5537 * first place at this level if the LUN doesn't have a backend.
5538 * That should get stopped by the command decode code.
5540 if (lun->backend == NULL) {
5541 ctl_set_invalid_opcode(ctsio);
5542 ctl_done((union ctl_io *)ctsio);
5543 return (CTL_RETVAL_COMPLETE);
5547 * XXX KDM Copan-specific offline behavior.
5548 * Figure out a reasonable way to port this?
5551 mtx_lock(&lun->lun_lock);
5553 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5554 && (lun->flags & CTL_LUN_OFFLINE)) {
5556 * If the LUN is offline, and the on/offline bit isn't set,
5557 * reject the start or stop. Otherwise, let it through.
5559 mtx_unlock(&lun->lun_lock);
5560 ctl_set_lun_not_ready(ctsio);
5561 ctl_done((union ctl_io *)ctsio);
5563 mtx_unlock(&lun->lun_lock);
5564 #endif /* NEEDTOPORT */
5566 * This could be a start or a stop when we're online,
5567 * or a stop/offline or start/online. A start or stop when
5568 * we're offline is covered in the case above.
5571 * In the non-immediate case, we send the request to
5572 * the backend and return status to the user when
5575 * In the immediate case, we allocate a new ctl_io
5576 * to hold a copy of the request, and send that to
5577 * the backend. We then set good status on the
5578 * user's request and return it immediately.
5580 if (cdb->byte2 & SSS_IMMED) {
5581 union ctl_io *new_io;
5583 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5584 if (new_io == NULL) {
5585 ctl_set_busy(ctsio);
5586 ctl_done((union ctl_io *)ctsio);
5588 ctl_copy_io((union ctl_io *)ctsio,
5590 retval = lun->backend->config_write(new_io);
5591 ctl_set_success(ctsio);
5592 ctl_done((union ctl_io *)ctsio);
5595 retval = lun->backend->config_write(
5596 (union ctl_io *)ctsio);
5605 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5606 * we don't really do anything with the LBA and length fields if the user
5607 * passes them in. Instead we'll just flush out the cache for the entire
5611 ctl_sync_cache(struct ctl_scsiio *ctsio)
5613 struct ctl_lun *lun;
5614 struct ctl_softc *ctl_softc;
5615 uint64_t starting_lba;
5616 uint32_t block_count;
5619 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5621 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5622 ctl_softc = control_softc;
5625 switch (ctsio->cdb[0]) {
5626 case SYNCHRONIZE_CACHE: {
5627 struct scsi_sync_cache *cdb;
5628 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5630 starting_lba = scsi_4btoul(cdb->begin_lba);
5631 block_count = scsi_2btoul(cdb->lb_count);
5634 case SYNCHRONIZE_CACHE_16: {
5635 struct scsi_sync_cache_16 *cdb;
5636 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5638 starting_lba = scsi_8btou64(cdb->begin_lba);
5639 block_count = scsi_4btoul(cdb->lb_count);
5643 ctl_set_invalid_opcode(ctsio);
5644 ctl_done((union ctl_io *)ctsio);
5646 break; /* NOTREACHED */
5650 * We check the LBA and length, but don't do anything with them.
5651 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5652 * get flushed. This check will just help satisfy anyone who wants
5653 * to see an error for an out of range LBA.
5655 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5656 ctl_set_lba_out_of_range(ctsio);
5657 ctl_done((union ctl_io *)ctsio);
5662 * If this LUN has no backend, we can't flush the cache anyway.
5664 if (lun->backend == NULL) {
5665 ctl_set_invalid_opcode(ctsio);
5666 ctl_done((union ctl_io *)ctsio);
5671 * Check to see whether we're configured to send the SYNCHRONIZE
5672 * CACHE command directly to the back end.
5674 mtx_lock(&lun->lun_lock);
5675 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5676 && (++(lun->sync_count) >= lun->sync_interval)) {
5677 lun->sync_count = 0;
5678 mtx_unlock(&lun->lun_lock);
5679 retval = lun->backend->config_write((union ctl_io *)ctsio);
5681 mtx_unlock(&lun->lun_lock);
5682 ctl_set_success(ctsio);
5683 ctl_done((union ctl_io *)ctsio);
5692 ctl_format(struct ctl_scsiio *ctsio)
5694 struct scsi_format *cdb;
5695 struct ctl_lun *lun;
5696 struct ctl_softc *ctl_softc;
5697 int length, defect_list_len;
5699 CTL_DEBUG_PRINT(("ctl_format\n"));
5701 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5702 ctl_softc = control_softc;
5704 cdb = (struct scsi_format *)ctsio->cdb;
5707 if (cdb->byte2 & SF_FMTDATA) {
5708 if (cdb->byte2 & SF_LONGLIST)
5709 length = sizeof(struct scsi_format_header_long);
5711 length = sizeof(struct scsi_format_header_short);
5714 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5716 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5717 ctsio->kern_data_len = length;
5718 ctsio->kern_total_len = length;
5719 ctsio->kern_data_resid = 0;
5720 ctsio->kern_rel_offset = 0;
5721 ctsio->kern_sg_entries = 0;
5722 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5723 ctsio->be_move_done = ctl_config_move_done;
5724 ctl_datamove((union ctl_io *)ctsio);
5726 return (CTL_RETVAL_COMPLETE);
5729 defect_list_len = 0;
5731 if (cdb->byte2 & SF_FMTDATA) {
5732 if (cdb->byte2 & SF_LONGLIST) {
5733 struct scsi_format_header_long *header;
5735 header = (struct scsi_format_header_long *)
5736 ctsio->kern_data_ptr;
5738 defect_list_len = scsi_4btoul(header->defect_list_len);
5739 if (defect_list_len != 0) {
5740 ctl_set_invalid_field(ctsio,
5749 struct scsi_format_header_short *header;
5751 header = (struct scsi_format_header_short *)
5752 ctsio->kern_data_ptr;
5754 defect_list_len = scsi_2btoul(header->defect_list_len);
5755 if (defect_list_len != 0) {
5756 ctl_set_invalid_field(ctsio,
5768 * The format command will clear out the "Medium format corrupted"
5769 * status if set by the configuration code. That status is really
5770 * just a way to notify the host that we have lost the media, and
5771 * get them to issue a command that will basically make them think
5772 * they're blowing away the media.
5774 mtx_lock(&lun->lun_lock);
5775 lun->flags &= ~CTL_LUN_INOPERABLE;
5776 mtx_unlock(&lun->lun_lock);
5778 ctsio->scsi_status = SCSI_STATUS_OK;
5779 ctsio->io_hdr.status = CTL_SUCCESS;
5782 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5783 free(ctsio->kern_data_ptr, M_CTL);
5784 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5787 ctl_done((union ctl_io *)ctsio);
5788 return (CTL_RETVAL_COMPLETE);
5792 ctl_read_buffer(struct ctl_scsiio *ctsio)
5794 struct scsi_read_buffer *cdb;
5795 struct ctl_lun *lun;
5796 int buffer_offset, len;
5797 static uint8_t descr[4];
5798 static uint8_t echo_descr[4] = { 0 };
5800 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5802 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5803 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5805 if (lun->flags & CTL_LUN_PR_RESERVED) {
5809 * XXX KDM need a lock here.
5811 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5812 if ((lun->res_type == SPR_TYPE_EX_AC
5813 && residx != lun->pr_res_idx)
5814 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5815 || lun->res_type == SPR_TYPE_EX_AC_AR)
5816 && !lun->per_res[residx].registered)) {
5817 ctl_set_reservation_conflict(ctsio);
5818 ctl_done((union ctl_io *)ctsio);
5819 return (CTL_RETVAL_COMPLETE);
5823 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5824 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5825 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5826 ctl_set_invalid_field(ctsio,
5832 ctl_done((union ctl_io *)ctsio);
5833 return (CTL_RETVAL_COMPLETE);
5836 len = scsi_3btoul(cdb->length);
5837 buffer_offset = scsi_3btoul(cdb->offset);
5839 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5840 ctl_set_invalid_field(ctsio,
5846 ctl_done((union ctl_io *)ctsio);
5847 return (CTL_RETVAL_COMPLETE);
5850 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5852 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5853 ctsio->kern_data_ptr = descr;
5854 len = min(len, sizeof(descr));
5855 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5856 ctsio->kern_data_ptr = echo_descr;
5857 len = min(len, sizeof(echo_descr));
5859 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5860 ctsio->kern_data_len = len;
5861 ctsio->kern_total_len = len;
5862 ctsio->kern_data_resid = 0;
5863 ctsio->kern_rel_offset = 0;
5864 ctsio->kern_sg_entries = 0;
5865 ctsio->be_move_done = ctl_config_move_done;
5866 ctl_datamove((union ctl_io *)ctsio);
5868 return (CTL_RETVAL_COMPLETE);
5872 ctl_write_buffer(struct ctl_scsiio *ctsio)
5874 struct scsi_write_buffer *cdb;
5875 struct ctl_lun *lun;
5876 int buffer_offset, len;
5878 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5880 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5881 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5883 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5884 ctl_set_invalid_field(ctsio,
5890 ctl_done((union ctl_io *)ctsio);
5891 return (CTL_RETVAL_COMPLETE);
5894 len = scsi_3btoul(cdb->length);
5895 buffer_offset = scsi_3btoul(cdb->offset);
5897 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5898 ctl_set_invalid_field(ctsio,
5904 ctl_done((union ctl_io *)ctsio);
5905 return (CTL_RETVAL_COMPLETE);
5909 * If we've got a kernel request that hasn't been malloced yet,
5910 * malloc it and tell the caller the data buffer is here.
5912 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5913 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5914 ctsio->kern_data_len = len;
5915 ctsio->kern_total_len = len;
5916 ctsio->kern_data_resid = 0;
5917 ctsio->kern_rel_offset = 0;
5918 ctsio->kern_sg_entries = 0;
5919 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5920 ctsio->be_move_done = ctl_config_move_done;
5921 ctl_datamove((union ctl_io *)ctsio);
5923 return (CTL_RETVAL_COMPLETE);
5926 ctl_done((union ctl_io *)ctsio);
5928 return (CTL_RETVAL_COMPLETE);
5932 ctl_write_same(struct ctl_scsiio *ctsio)
5934 struct ctl_lun *lun;
5935 struct ctl_lba_len_flags *lbalen;
5937 uint32_t num_blocks;
5941 retval = CTL_RETVAL_COMPLETE;
5943 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5945 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5947 switch (ctsio->cdb[0]) {
5948 case WRITE_SAME_10: {
5949 struct scsi_write_same_10 *cdb;
5951 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5953 lba = scsi_4btoul(cdb->addr);
5954 num_blocks = scsi_2btoul(cdb->length);
5958 case WRITE_SAME_16: {
5959 struct scsi_write_same_16 *cdb;
5961 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5963 lba = scsi_8btou64(cdb->addr);
5964 num_blocks = scsi_4btoul(cdb->length);
5970 * We got a command we don't support. This shouldn't
5971 * happen, commands should be filtered out above us.
5973 ctl_set_invalid_opcode(ctsio);
5974 ctl_done((union ctl_io *)ctsio);
5976 return (CTL_RETVAL_COMPLETE);
5977 break; /* NOTREACHED */
5981 * The first check is to make sure we're in bounds, the second
5982 * check is to catch wrap-around problems. If the lba + num blocks
5983 * is less than the lba, then we've wrapped around and the block
5984 * range is invalid anyway.
5986 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5987 || ((lba + num_blocks) < lba)) {
5988 ctl_set_lba_out_of_range(ctsio);
5989 ctl_done((union ctl_io *)ctsio);
5990 return (CTL_RETVAL_COMPLETE);
5993 /* Zero number of blocks means "to the last logical block" */
5994 if (num_blocks == 0) {
5995 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5996 ctl_set_invalid_field(ctsio,
6002 ctl_done((union ctl_io *)ctsio);
6003 return (CTL_RETVAL_COMPLETE);
6005 num_blocks = (lun->be_lun->maxlba + 1) - lba;
6008 len = lun->be_lun->blocksize;
6011 * If we've got a kernel request that hasn't been malloced yet,
6012 * malloc it and tell the caller the data buffer is here.
6014 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6015 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6016 ctsio->kern_data_len = len;
6017 ctsio->kern_total_len = len;
6018 ctsio->kern_data_resid = 0;
6019 ctsio->kern_rel_offset = 0;
6020 ctsio->kern_sg_entries = 0;
6021 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6022 ctsio->be_move_done = ctl_config_move_done;
6023 ctl_datamove((union ctl_io *)ctsio);
6025 return (CTL_RETVAL_COMPLETE);
6028 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6030 lbalen->len = num_blocks;
6031 lbalen->flags = byte2;
6032 retval = lun->backend->config_write((union ctl_io *)ctsio);
6038 ctl_unmap(struct ctl_scsiio *ctsio)
6040 struct ctl_lun *lun;
6041 struct scsi_unmap *cdb;
6042 struct ctl_ptr_len_flags *ptrlen;
6043 struct scsi_unmap_header *hdr;
6044 struct scsi_unmap_desc *buf, *end, *endnz, *range;
6046 uint32_t num_blocks;
6050 retval = CTL_RETVAL_COMPLETE;
6052 CTL_DEBUG_PRINT(("ctl_unmap\n"));
6054 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6055 cdb = (struct scsi_unmap *)ctsio->cdb;
6057 len = scsi_2btoul(cdb->length);
6061 * If we've got a kernel request that hasn't been malloced yet,
6062 * malloc it and tell the caller the data buffer is here.
6064 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6065 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6066 ctsio->kern_data_len = len;
6067 ctsio->kern_total_len = len;
6068 ctsio->kern_data_resid = 0;
6069 ctsio->kern_rel_offset = 0;
6070 ctsio->kern_sg_entries = 0;
6071 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6072 ctsio->be_move_done = ctl_config_move_done;
6073 ctl_datamove((union ctl_io *)ctsio);
6075 return (CTL_RETVAL_COMPLETE);
6078 len = ctsio->kern_total_len - ctsio->kern_data_resid;
6079 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
6080 if (len < sizeof (*hdr) ||
6081 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
6082 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
6083 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
6084 ctl_set_invalid_field(ctsio,
6090 ctl_done((union ctl_io *)ctsio);
6091 return (CTL_RETVAL_COMPLETE);
6093 len = scsi_2btoul(hdr->desc_length);
6094 buf = (struct scsi_unmap_desc *)(hdr + 1);
6095 end = buf + len / sizeof(*buf);
6098 for (range = buf; range < end; range++) {
6099 lba = scsi_8btou64(range->lba);
6100 num_blocks = scsi_4btoul(range->length);
6101 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6102 || ((lba + num_blocks) < lba)) {
6103 ctl_set_lba_out_of_range(ctsio);
6104 ctl_done((union ctl_io *)ctsio);
6105 return (CTL_RETVAL_COMPLETE);
6107 if (num_blocks != 0)
6112 * Block backend can not handle zero last range.
6113 * Filter it out and return if there is nothing left.
6115 len = (uint8_t *)endnz - (uint8_t *)buf;
6117 ctl_set_success(ctsio);
6118 ctl_done((union ctl_io *)ctsio);
6119 return (CTL_RETVAL_COMPLETE);
6122 ptrlen = (struct ctl_ptr_len_flags *)
6123 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6124 ptrlen->ptr = (void *)buf;
6126 ptrlen->flags = byte2;
6128 retval = lun->backend->config_write((union ctl_io *)ctsio);
6133 * Note that this function currently doesn't actually do anything inside
6134 * CTL to enforce things if the DQue bit is turned on.
6136 * Also note that this function can't be used in the default case, because
6137 * the DQue bit isn't set in the changeable mask for the control mode page
6138 * anyway. This is just here as an example for how to implement a page
6139 * handler, and a placeholder in case we want to allow the user to turn
6140 * tagged queueing on and off.
6142 * The D_SENSE bit handling is functional, however, and will turn
6143 * descriptor sense on and off for a given LUN.
6146 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6147 struct ctl_page_index *page_index, uint8_t *page_ptr)
6149 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6150 struct ctl_lun *lun;
6151 struct ctl_softc *softc;
6155 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6156 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6159 user_cp = (struct scsi_control_page *)page_ptr;
6160 current_cp = (struct scsi_control_page *)
6161 (page_index->page_data + (page_index->page_len *
6163 saved_cp = (struct scsi_control_page *)
6164 (page_index->page_data + (page_index->page_len *
6167 softc = control_softc;
6169 mtx_lock(&lun->lun_lock);
6170 if (((current_cp->rlec & SCP_DSENSE) == 0)
6171 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6173 * Descriptor sense is currently turned off and the user
6174 * wants to turn it on.
6176 current_cp->rlec |= SCP_DSENSE;
6177 saved_cp->rlec |= SCP_DSENSE;
6178 lun->flags |= CTL_LUN_SENSE_DESC;
6180 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6181 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6183 * Descriptor sense is currently turned on, and the user
6184 * wants to turn it off.
6186 current_cp->rlec &= ~SCP_DSENSE;
6187 saved_cp->rlec &= ~SCP_DSENSE;
6188 lun->flags &= ~CTL_LUN_SENSE_DESC;
6191 if (current_cp->queue_flags & SCP_QUEUE_DQUE) {
6192 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6194 csevent_log(CSC_CTL | CSC_SHELF_SW |
6196 csevent_LogType_Trace,
6197 csevent_Severity_Information,
6198 csevent_AlertLevel_Green,
6199 csevent_FRU_Firmware,
6200 csevent_FRU_Unknown,
6201 "Received untagged to untagged transition");
6202 #endif /* NEEDTOPORT */
6205 csevent_log(CSC_CTL | CSC_SHELF_SW |
6207 csevent_LogType_ConfigChange,
6208 csevent_Severity_Information,
6209 csevent_AlertLevel_Green,
6210 csevent_FRU_Firmware,
6211 csevent_FRU_Unknown,
6212 "Received untagged to tagged "
6213 "queueing transition");
6214 #endif /* NEEDTOPORT */
6216 current_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6217 saved_cp->queue_flags &= ~SCP_QUEUE_DQUE;
6221 if (user_cp->queue_flags & SCP_QUEUE_DQUE) {
6223 csevent_log(CSC_CTL | CSC_SHELF_SW |
6225 csevent_LogType_ConfigChange,
6226 csevent_Severity_Warning,
6227 csevent_AlertLevel_Yellow,
6228 csevent_FRU_Firmware,
6229 csevent_FRU_Unknown,
6230 "Received tagged queueing to untagged "
6232 #endif /* NEEDTOPORT */
6234 current_cp->queue_flags |= SCP_QUEUE_DQUE;
6235 saved_cp->queue_flags |= SCP_QUEUE_DQUE;
6239 csevent_log(CSC_CTL | CSC_SHELF_SW |
6241 csevent_LogType_Trace,
6242 csevent_Severity_Information,
6243 csevent_AlertLevel_Green,
6244 csevent_FRU_Firmware,
6245 csevent_FRU_Unknown,
6246 "Received tagged queueing to tagged "
6247 "queueing transition");
6248 #endif /* NEEDTOPORT */
6254 * Let other initiators know that the mode
6255 * parameters for this LUN have changed.
6257 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6261 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6264 mtx_unlock(&lun->lun_lock);
6270 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6271 struct ctl_page_index *page_index, uint8_t *page_ptr)
6273 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6274 struct ctl_lun *lun;
6278 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6279 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6282 user_cp = (struct scsi_caching_page *)page_ptr;
6283 current_cp = (struct scsi_caching_page *)
6284 (page_index->page_data + (page_index->page_len *
6286 saved_cp = (struct scsi_caching_page *)
6287 (page_index->page_data + (page_index->page_len *
6290 mtx_lock(&lun->lun_lock);
6291 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6292 (user_cp->flags1 & (SCP_WCE | SCP_RCD)))
6294 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6295 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6296 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6297 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6301 * Let other initiators know that the mode
6302 * parameters for this LUN have changed.
6304 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6308 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6311 mtx_unlock(&lun->lun_lock);
6317 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
6318 struct ctl_page_index *page_index, uint8_t *page_ptr)
6324 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
6325 struct ctl_page_index *page_index, int pc)
6327 struct copan_power_subpage *page;
6329 page = (struct copan_power_subpage *)page_index->page_data +
6330 (page_index->page_len * pc);
6333 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6335 * We don't update the changable bits for this page.
6338 case SMS_PAGE_CTRL_CURRENT >> 6:
6339 case SMS_PAGE_CTRL_DEFAULT >> 6:
6340 case SMS_PAGE_CTRL_SAVED >> 6:
6342 ctl_update_power_subpage(page);
6347 EPRINT(0, "Invalid PC %d!!", pc);
6356 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
6357 struct ctl_page_index *page_index, uint8_t *page_ptr)
6359 struct copan_aps_subpage *user_sp;
6360 struct copan_aps_subpage *current_sp;
6361 union ctl_modepage_info *modepage_info;
6362 struct ctl_softc *softc;
6363 struct ctl_lun *lun;
6366 retval = CTL_RETVAL_COMPLETE;
6367 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
6368 (page_index->page_len * CTL_PAGE_CURRENT));
6369 softc = control_softc;
6370 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6372 user_sp = (struct copan_aps_subpage *)page_ptr;
6374 modepage_info = (union ctl_modepage_info *)
6375 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6377 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
6378 modepage_info->header.subpage = page_index->subpage;
6379 modepage_info->aps.lock_active = user_sp->lock_active;
6381 mtx_lock(&softc->ctl_lock);
6384 * If there is a request to lock the LUN and another LUN is locked
6385 * this is an error. If the requested LUN is already locked ignore
6386 * the request. If no LUN is locked attempt to lock it.
6387 * if there is a request to unlock the LUN and the LUN is currently
6388 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6389 * if another LUN is locked or no LUN is locked.
6391 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6392 if (softc->aps_locked_lun == lun->lun) {
6394 * This LUN is already locked, so we're done.
6396 retval = CTL_RETVAL_COMPLETE;
6397 } else if (softc->aps_locked_lun == 0) {
6399 * No one has the lock, pass the request to the
6402 retval = lun->backend->config_write(
6403 (union ctl_io *)ctsio);
6406 * Someone else has the lock, throw out the request.
6408 ctl_set_already_locked(ctsio);
6409 free(ctsio->kern_data_ptr, M_CTL);
6410 ctl_done((union ctl_io *)ctsio);
6413 * Set the return value so that ctl_do_mode_select()
6414 * won't try to complete the command. We already
6415 * completed it here.
6417 retval = CTL_RETVAL_ERROR;
6419 } else if (softc->aps_locked_lun == lun->lun) {
6421 * This LUN is locked, so pass the unlock request to the
6424 retval = lun->backend->config_write((union ctl_io *)ctsio);
6426 mtx_unlock(&softc->ctl_lock);
6432 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6433 struct ctl_page_index *page_index,
6439 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6444 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6445 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6446 printf("page data:");
6448 printf(" %.2x",page_ptr[i]);
6454 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6455 struct ctl_page_index *page_index,
6458 struct copan_debugconf_subpage *page;
6460 page = (struct copan_debugconf_subpage *)page_index->page_data +
6461 (page_index->page_len * pc);
6464 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6465 case SMS_PAGE_CTRL_DEFAULT >> 6:
6466 case SMS_PAGE_CTRL_SAVED >> 6:
6468 * We don't update the changable or default bits for this page.
6471 case SMS_PAGE_CTRL_CURRENT >> 6:
6472 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6473 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6477 EPRINT(0, "Invalid PC %d!!", pc);
6478 #endif /* NEEDTOPORT */
6486 ctl_do_mode_select(union ctl_io *io)
6488 struct scsi_mode_page_header *page_header;
6489 struct ctl_page_index *page_index;
6490 struct ctl_scsiio *ctsio;
6491 int control_dev, page_len;
6492 int page_len_offset, page_len_size;
6493 union ctl_modepage_info *modepage_info;
6494 struct ctl_lun *lun;
6495 int *len_left, *len_used;
6498 ctsio = &io->scsiio;
6501 retval = CTL_RETVAL_COMPLETE;
6503 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6505 if (lun->be_lun->lun_type != T_DIRECT)
6510 modepage_info = (union ctl_modepage_info *)
6511 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6512 len_left = &modepage_info->header.len_left;
6513 len_used = &modepage_info->header.len_used;
6517 page_header = (struct scsi_mode_page_header *)
6518 (ctsio->kern_data_ptr + *len_used);
6520 if (*len_left == 0) {
6521 free(ctsio->kern_data_ptr, M_CTL);
6522 ctl_set_success(ctsio);
6523 ctl_done((union ctl_io *)ctsio);
6524 return (CTL_RETVAL_COMPLETE);
6525 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6527 free(ctsio->kern_data_ptr, M_CTL);
6528 ctl_set_param_len_error(ctsio);
6529 ctl_done((union ctl_io *)ctsio);
6530 return (CTL_RETVAL_COMPLETE);
6532 } else if ((page_header->page_code & SMPH_SPF)
6533 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6535 free(ctsio->kern_data_ptr, M_CTL);
6536 ctl_set_param_len_error(ctsio);
6537 ctl_done((union ctl_io *)ctsio);
6538 return (CTL_RETVAL_COMPLETE);
6543 * XXX KDM should we do something with the block descriptor?
6545 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6547 if ((control_dev != 0)
6548 && (lun->mode_pages.index[i].page_flags &
6549 CTL_PAGE_FLAG_DISK_ONLY))
6552 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6553 (page_header->page_code & SMPH_PC_MASK))
6557 * If neither page has a subpage code, then we've got a
6560 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6561 && ((page_header->page_code & SMPH_SPF) == 0)) {
6562 page_index = &lun->mode_pages.index[i];
6563 page_len = page_header->page_length;
6568 * If both pages have subpages, then the subpage numbers
6571 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6572 && (page_header->page_code & SMPH_SPF)) {
6573 struct scsi_mode_page_header_sp *sph;
6575 sph = (struct scsi_mode_page_header_sp *)page_header;
6577 if (lun->mode_pages.index[i].subpage ==
6579 page_index = &lun->mode_pages.index[i];
6580 page_len = scsi_2btoul(sph->page_length);
6587 * If we couldn't find the page, or if we don't have a mode select
6588 * handler for it, send back an error to the user.
6590 if ((page_index == NULL)
6591 || (page_index->select_handler == NULL)) {
6592 ctl_set_invalid_field(ctsio,
6595 /*field*/ *len_used,
6598 free(ctsio->kern_data_ptr, M_CTL);
6599 ctl_done((union ctl_io *)ctsio);
6600 return (CTL_RETVAL_COMPLETE);
6603 if (page_index->page_code & SMPH_SPF) {
6604 page_len_offset = 2;
6608 page_len_offset = 1;
6612 * If the length the initiator gives us isn't the one we specify in
6613 * the mode page header, or if they didn't specify enough data in
6614 * the CDB to avoid truncating this page, kick out the request.
6616 if ((page_len != (page_index->page_len - page_len_offset -
6618 || (*len_left < page_index->page_len)) {
6621 ctl_set_invalid_field(ctsio,
6624 /*field*/ *len_used + page_len_offset,
6627 free(ctsio->kern_data_ptr, M_CTL);
6628 ctl_done((union ctl_io *)ctsio);
6629 return (CTL_RETVAL_COMPLETE);
6633 * Run through the mode page, checking to make sure that the bits
6634 * the user changed are actually legal for him to change.
6636 for (i = 0; i < page_index->page_len; i++) {
6637 uint8_t *user_byte, *change_mask, *current_byte;
6641 user_byte = (uint8_t *)page_header + i;
6642 change_mask = page_index->page_data +
6643 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6644 current_byte = page_index->page_data +
6645 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6648 * Check to see whether the user set any bits in this byte
6649 * that he is not allowed to set.
6651 if ((*user_byte & ~(*change_mask)) ==
6652 (*current_byte & ~(*change_mask)))
6656 * Go through bit by bit to determine which one is illegal.
6659 for (j = 7; j >= 0; j--) {
6660 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6661 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6666 ctl_set_invalid_field(ctsio,
6669 /*field*/ *len_used + i,
6672 free(ctsio->kern_data_ptr, M_CTL);
6673 ctl_done((union ctl_io *)ctsio);
6674 return (CTL_RETVAL_COMPLETE);
6678 * Decrement these before we call the page handler, since we may
6679 * end up getting called back one way or another before the handler
6680 * returns to this context.
6682 *len_left -= page_index->page_len;
6683 *len_used += page_index->page_len;
6685 retval = page_index->select_handler(ctsio, page_index,
6686 (uint8_t *)page_header);
6689 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6690 * wait until this queued command completes to finish processing
6691 * the mode page. If it returns anything other than
6692 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6693 * already set the sense information, freed the data pointer, and
6694 * completed the io for us.
6696 if (retval != CTL_RETVAL_COMPLETE)
6697 goto bailout_no_done;
6700 * If the initiator sent us more than one page, parse the next one.
6705 ctl_set_success(ctsio);
6706 free(ctsio->kern_data_ptr, M_CTL);
6707 ctl_done((union ctl_io *)ctsio);
6711 return (CTL_RETVAL_COMPLETE);
6716 ctl_mode_select(struct ctl_scsiio *ctsio)
6718 int param_len, pf, sp;
6719 int header_size, bd_len;
6720 int len_left, len_used;
6721 struct ctl_page_index *page_index;
6722 struct ctl_lun *lun;
6723 int control_dev, page_len;
6724 union ctl_modepage_info *modepage_info;
6736 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6738 if (lun->be_lun->lun_type != T_DIRECT)
6743 switch (ctsio->cdb[0]) {
6744 case MODE_SELECT_6: {
6745 struct scsi_mode_select_6 *cdb;
6747 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6749 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6750 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6752 param_len = cdb->length;
6753 header_size = sizeof(struct scsi_mode_header_6);
6756 case MODE_SELECT_10: {
6757 struct scsi_mode_select_10 *cdb;
6759 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6761 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6762 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6764 param_len = scsi_2btoul(cdb->length);
6765 header_size = sizeof(struct scsi_mode_header_10);
6769 ctl_set_invalid_opcode(ctsio);
6770 ctl_done((union ctl_io *)ctsio);
6771 return (CTL_RETVAL_COMPLETE);
6772 break; /* NOTREACHED */
6777 * "A parameter list length of zero indicates that the Data-Out Buffer
6778 * shall be empty. This condition shall not be considered as an error."
6780 if (param_len == 0) {
6781 ctl_set_success(ctsio);
6782 ctl_done((union ctl_io *)ctsio);
6783 return (CTL_RETVAL_COMPLETE);
6787 * Since we'll hit this the first time through, prior to
6788 * allocation, we don't need to free a data buffer here.
6790 if (param_len < header_size) {
6791 ctl_set_param_len_error(ctsio);
6792 ctl_done((union ctl_io *)ctsio);
6793 return (CTL_RETVAL_COMPLETE);
6797 * Allocate the data buffer and grab the user's data. In theory,
6798 * we shouldn't have to sanity check the parameter list length here
6799 * because the maximum size is 64K. We should be able to malloc
6800 * that much without too many problems.
6802 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6803 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6804 ctsio->kern_data_len = param_len;
6805 ctsio->kern_total_len = param_len;
6806 ctsio->kern_data_resid = 0;
6807 ctsio->kern_rel_offset = 0;
6808 ctsio->kern_sg_entries = 0;
6809 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6810 ctsio->be_move_done = ctl_config_move_done;
6811 ctl_datamove((union ctl_io *)ctsio);
6813 return (CTL_RETVAL_COMPLETE);
6816 switch (ctsio->cdb[0]) {
6817 case MODE_SELECT_6: {
6818 struct scsi_mode_header_6 *mh6;
6820 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6821 bd_len = mh6->blk_desc_len;
6824 case MODE_SELECT_10: {
6825 struct scsi_mode_header_10 *mh10;
6827 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6828 bd_len = scsi_2btoul(mh10->blk_desc_len);
6832 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6836 if (param_len < (header_size + bd_len)) {
6837 free(ctsio->kern_data_ptr, M_CTL);
6838 ctl_set_param_len_error(ctsio);
6839 ctl_done((union ctl_io *)ctsio);
6840 return (CTL_RETVAL_COMPLETE);
6844 * Set the IO_CONT flag, so that if this I/O gets passed to
6845 * ctl_config_write_done(), it'll get passed back to
6846 * ctl_do_mode_select() for further processing, or completion if
6849 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6850 ctsio->io_cont = ctl_do_mode_select;
6852 modepage_info = (union ctl_modepage_info *)
6853 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6855 memset(modepage_info, 0, sizeof(*modepage_info));
6857 len_left = param_len - header_size - bd_len;
6858 len_used = header_size + bd_len;
6860 modepage_info->header.len_left = len_left;
6861 modepage_info->header.len_used = len_used;
6863 return (ctl_do_mode_select((union ctl_io *)ctsio));
6867 ctl_mode_sense(struct ctl_scsiio *ctsio)
6869 struct ctl_lun *lun;
6870 int pc, page_code, dbd, llba, subpage;
6871 int alloc_len, page_len, header_len, total_len;
6872 struct scsi_mode_block_descr *block_desc;
6873 struct ctl_page_index *page_index;
6881 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6883 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6885 if (lun->be_lun->lun_type != T_DIRECT)
6890 if (lun->flags & CTL_LUN_PR_RESERVED) {
6894 * XXX KDM need a lock here.
6896 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6897 if ((lun->res_type == SPR_TYPE_EX_AC
6898 && residx != lun->pr_res_idx)
6899 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6900 || lun->res_type == SPR_TYPE_EX_AC_AR)
6901 && !lun->per_res[residx].registered)) {
6902 ctl_set_reservation_conflict(ctsio);
6903 ctl_done((union ctl_io *)ctsio);
6904 return (CTL_RETVAL_COMPLETE);
6908 switch (ctsio->cdb[0]) {
6909 case MODE_SENSE_6: {
6910 struct scsi_mode_sense_6 *cdb;
6912 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6914 header_len = sizeof(struct scsi_mode_hdr_6);
6915 if (cdb->byte2 & SMS_DBD)
6918 header_len += sizeof(struct scsi_mode_block_descr);
6920 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6921 page_code = cdb->page & SMS_PAGE_CODE;
6922 subpage = cdb->subpage;
6923 alloc_len = cdb->length;
6926 case MODE_SENSE_10: {
6927 struct scsi_mode_sense_10 *cdb;
6929 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6931 header_len = sizeof(struct scsi_mode_hdr_10);
6933 if (cdb->byte2 & SMS_DBD)
6936 header_len += sizeof(struct scsi_mode_block_descr);
6937 if (cdb->byte2 & SMS10_LLBAA)
6939 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6940 page_code = cdb->page & SMS_PAGE_CODE;
6941 subpage = cdb->subpage;
6942 alloc_len = scsi_2btoul(cdb->length);
6946 ctl_set_invalid_opcode(ctsio);
6947 ctl_done((union ctl_io *)ctsio);
6948 return (CTL_RETVAL_COMPLETE);
6949 break; /* NOTREACHED */
6953 * We have to make a first pass through to calculate the size of
6954 * the pages that match the user's query. Then we allocate enough
6955 * memory to hold it, and actually copy the data into the buffer.
6957 switch (page_code) {
6958 case SMS_ALL_PAGES_PAGE: {
6964 * At the moment, values other than 0 and 0xff here are
6965 * reserved according to SPC-3.
6967 if ((subpage != SMS_SUBPAGE_PAGE_0)
6968 && (subpage != SMS_SUBPAGE_ALL)) {
6969 ctl_set_invalid_field(ctsio,
6975 ctl_done((union ctl_io *)ctsio);
6976 return (CTL_RETVAL_COMPLETE);
6979 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6980 if ((control_dev != 0)
6981 && (lun->mode_pages.index[i].page_flags &
6982 CTL_PAGE_FLAG_DISK_ONLY))
6986 * We don't use this subpage if the user didn't
6987 * request all subpages.
6989 if ((lun->mode_pages.index[i].subpage != 0)
6990 && (subpage == SMS_SUBPAGE_PAGE_0))
6994 printf("found page %#x len %d\n",
6995 lun->mode_pages.index[i].page_code &
6997 lun->mode_pages.index[i].page_len);
6999 page_len += lun->mode_pages.index[i].page_len;
7008 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7009 /* Look for the right page code */
7010 if ((lun->mode_pages.index[i].page_code &
7011 SMPH_PC_MASK) != page_code)
7014 /* Look for the right subpage or the subpage wildcard*/
7015 if ((lun->mode_pages.index[i].subpage != subpage)
7016 && (subpage != SMS_SUBPAGE_ALL))
7019 /* Make sure the page is supported for this dev type */
7020 if ((control_dev != 0)
7021 && (lun->mode_pages.index[i].page_flags &
7022 CTL_PAGE_FLAG_DISK_ONLY))
7026 printf("found page %#x len %d\n",
7027 lun->mode_pages.index[i].page_code &
7029 lun->mode_pages.index[i].page_len);
7032 page_len += lun->mode_pages.index[i].page_len;
7035 if (page_len == 0) {
7036 ctl_set_invalid_field(ctsio,
7042 ctl_done((union ctl_io *)ctsio);
7043 return (CTL_RETVAL_COMPLETE);
7049 total_len = header_len + page_len;
7051 printf("header_len = %d, page_len = %d, total_len = %d\n",
7052 header_len, page_len, total_len);
7055 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7056 ctsio->kern_sg_entries = 0;
7057 ctsio->kern_data_resid = 0;
7058 ctsio->kern_rel_offset = 0;
7059 if (total_len < alloc_len) {
7060 ctsio->residual = alloc_len - total_len;
7061 ctsio->kern_data_len = total_len;
7062 ctsio->kern_total_len = total_len;
7064 ctsio->residual = 0;
7065 ctsio->kern_data_len = alloc_len;
7066 ctsio->kern_total_len = alloc_len;
7069 switch (ctsio->cdb[0]) {
7070 case MODE_SENSE_6: {
7071 struct scsi_mode_hdr_6 *header;
7073 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
7075 header->datalen = ctl_min(total_len - 1, 254);
7076 if (control_dev == 0)
7077 header->dev_specific = 0x10; /* DPOFUA */
7079 header->block_descr_len = 0;
7081 header->block_descr_len =
7082 sizeof(struct scsi_mode_block_descr);
7083 block_desc = (struct scsi_mode_block_descr *)&header[1];
7086 case MODE_SENSE_10: {
7087 struct scsi_mode_hdr_10 *header;
7090 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
7092 datalen = ctl_min(total_len - 2, 65533);
7093 scsi_ulto2b(datalen, header->datalen);
7094 if (control_dev == 0)
7095 header->dev_specific = 0x10; /* DPOFUA */
7097 scsi_ulto2b(0, header->block_descr_len);
7099 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
7100 header->block_descr_len);
7101 block_desc = (struct scsi_mode_block_descr *)&header[1];
7105 panic("invalid CDB type %#x", ctsio->cdb[0]);
7106 break; /* NOTREACHED */
7110 * If we've got a disk, use its blocksize in the block
7111 * descriptor. Otherwise, just set it to 0.
7114 if (control_dev != 0)
7115 scsi_ulto3b(lun->be_lun->blocksize,
7116 block_desc->block_len);
7118 scsi_ulto3b(0, block_desc->block_len);
7121 switch (page_code) {
7122 case SMS_ALL_PAGES_PAGE: {
7125 data_used = header_len;
7126 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7127 struct ctl_page_index *page_index;
7129 page_index = &lun->mode_pages.index[i];
7131 if ((control_dev != 0)
7132 && (page_index->page_flags &
7133 CTL_PAGE_FLAG_DISK_ONLY))
7137 * We don't use this subpage if the user didn't
7138 * request all subpages. We already checked (above)
7139 * to make sure the user only specified a subpage
7140 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
7142 if ((page_index->subpage != 0)
7143 && (subpage == SMS_SUBPAGE_PAGE_0))
7147 * Call the handler, if it exists, to update the
7148 * page to the latest values.
7150 if (page_index->sense_handler != NULL)
7151 page_index->sense_handler(ctsio, page_index,pc);
7153 memcpy(ctsio->kern_data_ptr + data_used,
7154 page_index->page_data +
7155 (page_index->page_len * pc),
7156 page_index->page_len);
7157 data_used += page_index->page_len;
7164 data_used = header_len;
7166 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7167 struct ctl_page_index *page_index;
7169 page_index = &lun->mode_pages.index[i];
7171 /* Look for the right page code */
7172 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
7175 /* Look for the right subpage or the subpage wildcard*/
7176 if ((page_index->subpage != subpage)
7177 && (subpage != SMS_SUBPAGE_ALL))
7180 /* Make sure the page is supported for this dev type */
7181 if ((control_dev != 0)
7182 && (page_index->page_flags &
7183 CTL_PAGE_FLAG_DISK_ONLY))
7187 * Call the handler, if it exists, to update the
7188 * page to the latest values.
7190 if (page_index->sense_handler != NULL)
7191 page_index->sense_handler(ctsio, page_index,pc);
7193 memcpy(ctsio->kern_data_ptr + data_used,
7194 page_index->page_data +
7195 (page_index->page_len * pc),
7196 page_index->page_len);
7197 data_used += page_index->page_len;
7203 ctsio->scsi_status = SCSI_STATUS_OK;
7205 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7206 ctsio->be_move_done = ctl_config_move_done;
7207 ctl_datamove((union ctl_io *)ctsio);
7209 return (CTL_RETVAL_COMPLETE);
7213 ctl_read_capacity(struct ctl_scsiio *ctsio)
7215 struct scsi_read_capacity *cdb;
7216 struct scsi_read_capacity_data *data;
7217 struct ctl_lun *lun;
7220 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7222 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7224 lba = scsi_4btoul(cdb->addr);
7225 if (((cdb->pmi & SRC_PMI) == 0)
7227 ctl_set_invalid_field(/*ctsio*/ ctsio,
7233 ctl_done((union ctl_io *)ctsio);
7234 return (CTL_RETVAL_COMPLETE);
7237 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7239 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7240 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7241 ctsio->residual = 0;
7242 ctsio->kern_data_len = sizeof(*data);
7243 ctsio->kern_total_len = sizeof(*data);
7244 ctsio->kern_data_resid = 0;
7245 ctsio->kern_rel_offset = 0;
7246 ctsio->kern_sg_entries = 0;
7249 * If the maximum LBA is greater than 0xfffffffe, the user must
7250 * issue a SERVICE ACTION IN (16) command, with the read capacity
7251 * serivce action set.
7253 if (lun->be_lun->maxlba > 0xfffffffe)
7254 scsi_ulto4b(0xffffffff, data->addr);
7256 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7259 * XXX KDM this may not be 512 bytes...
7261 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7263 ctsio->scsi_status = SCSI_STATUS_OK;
7265 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7266 ctsio->be_move_done = ctl_config_move_done;
7267 ctl_datamove((union ctl_io *)ctsio);
7269 return (CTL_RETVAL_COMPLETE);
7273 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7275 struct scsi_read_capacity_16 *cdb;
7276 struct scsi_read_capacity_data_long *data;
7277 struct ctl_lun *lun;
7281 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7283 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7285 alloc_len = scsi_4btoul(cdb->alloc_len);
7286 lba = scsi_8btou64(cdb->addr);
7288 if ((cdb->reladr & SRC16_PMI)
7290 ctl_set_invalid_field(/*ctsio*/ ctsio,
7296 ctl_done((union ctl_io *)ctsio);
7297 return (CTL_RETVAL_COMPLETE);
7300 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7302 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7303 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7305 if (sizeof(*data) < alloc_len) {
7306 ctsio->residual = alloc_len - sizeof(*data);
7307 ctsio->kern_data_len = sizeof(*data);
7308 ctsio->kern_total_len = sizeof(*data);
7310 ctsio->residual = 0;
7311 ctsio->kern_data_len = alloc_len;
7312 ctsio->kern_total_len = alloc_len;
7314 ctsio->kern_data_resid = 0;
7315 ctsio->kern_rel_offset = 0;
7316 ctsio->kern_sg_entries = 0;
7318 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7319 /* XXX KDM this may not be 512 bytes... */
7320 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7321 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7322 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7323 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7324 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7326 ctsio->scsi_status = SCSI_STATUS_OK;
7328 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7329 ctsio->be_move_done = ctl_config_move_done;
7330 ctl_datamove((union ctl_io *)ctsio);
7332 return (CTL_RETVAL_COMPLETE);
7336 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7338 struct scsi_maintenance_in *cdb;
7340 int alloc_len, ext, total_len = 0, g, p, pc, pg;
7341 int num_target_port_groups, num_target_ports, single;
7342 struct ctl_lun *lun;
7343 struct ctl_softc *softc;
7344 struct ctl_port *port;
7345 struct scsi_target_group_data *rtg_ptr;
7346 struct scsi_target_group_data_extended *rtg_ext_ptr;
7347 struct scsi_target_port_group_descriptor *tpg_desc;
7349 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7351 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7352 softc = control_softc;
7353 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7355 retval = CTL_RETVAL_COMPLETE;
7357 switch (cdb->byte2 & STG_PDF_MASK) {
7358 case STG_PDF_LENGTH:
7361 case STG_PDF_EXTENDED:
7365 ctl_set_invalid_field(/*ctsio*/ ctsio,
7371 ctl_done((union ctl_io *)ctsio);
7375 single = ctl_is_single;
7377 num_target_port_groups = 1;
7379 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7380 num_target_ports = 0;
7381 mtx_lock(&softc->ctl_lock);
7382 STAILQ_FOREACH(port, &softc->port_list, links) {
7383 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7385 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7389 mtx_unlock(&softc->ctl_lock);
7392 total_len = sizeof(struct scsi_target_group_data_extended);
7394 total_len = sizeof(struct scsi_target_group_data);
7395 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7396 num_target_port_groups +
7397 sizeof(struct scsi_target_port_descriptor) *
7398 num_target_ports * num_target_port_groups;
7400 alloc_len = scsi_4btoul(cdb->length);
7402 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7404 ctsio->kern_sg_entries = 0;
7406 if (total_len < alloc_len) {
7407 ctsio->residual = alloc_len - total_len;
7408 ctsio->kern_data_len = total_len;
7409 ctsio->kern_total_len = total_len;
7411 ctsio->residual = 0;
7412 ctsio->kern_data_len = alloc_len;
7413 ctsio->kern_total_len = alloc_len;
7415 ctsio->kern_data_resid = 0;
7416 ctsio->kern_rel_offset = 0;
7419 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7420 ctsio->kern_data_ptr;
7421 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7422 rtg_ext_ptr->format_type = 0x10;
7423 rtg_ext_ptr->implicit_transition_time = 0;
7424 tpg_desc = &rtg_ext_ptr->groups[0];
7426 rtg_ptr = (struct scsi_target_group_data *)
7427 ctsio->kern_data_ptr;
7428 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7429 tpg_desc = &rtg_ptr->groups[0];
7432 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS;
7433 mtx_lock(&softc->ctl_lock);
7434 for (g = 0; g < num_target_port_groups; g++) {
7436 tpg_desc->pref_state = TPG_PRIMARY |
7437 TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7439 tpg_desc->pref_state =
7440 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7441 tpg_desc->support = TPG_AO_SUP;
7443 tpg_desc->support |= TPG_AN_SUP;
7444 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7445 tpg_desc->status = TPG_IMPLICIT;
7447 STAILQ_FOREACH(port, &softc->port_list, links) {
7448 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7450 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7453 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7454 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7455 relative_target_port_identifier);
7458 tpg_desc->target_port_count = pc;
7459 tpg_desc = (struct scsi_target_port_group_descriptor *)
7460 &tpg_desc->descriptors[pc];
7462 mtx_unlock(&softc->ctl_lock);
7464 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7465 ctsio->be_move_done = ctl_config_move_done;
7467 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7468 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7469 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7470 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7471 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7473 ctl_datamove((union ctl_io *)ctsio);
7478 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7480 struct ctl_lun *lun;
7481 struct scsi_report_supported_opcodes *cdb;
7482 const struct ctl_cmd_entry *entry, *sentry;
7483 struct scsi_report_supported_opcodes_all *all;
7484 struct scsi_report_supported_opcodes_descr *descr;
7485 struct scsi_report_supported_opcodes_one *one;
7487 int alloc_len, total_len;
7488 int opcode, service_action, i, j, num;
7490 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7492 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7493 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7495 retval = CTL_RETVAL_COMPLETE;
7497 opcode = cdb->requested_opcode;
7498 service_action = scsi_2btoul(cdb->requested_service_action);
7499 switch (cdb->options & RSO_OPTIONS_MASK) {
7500 case RSO_OPTIONS_ALL:
7502 for (i = 0; i < 256; i++) {
7503 entry = &ctl_cmd_table[i];
7504 if (entry->flags & CTL_CMD_FLAG_SA5) {
7505 for (j = 0; j < 32; j++) {
7506 sentry = &((const struct ctl_cmd_entry *)
7508 if (ctl_cmd_applicable(
7509 lun->be_lun->lun_type, sentry))
7513 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7518 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7519 num * sizeof(struct scsi_report_supported_opcodes_descr);
7521 case RSO_OPTIONS_OC:
7522 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7523 ctl_set_invalid_field(/*ctsio*/ ctsio,
7529 ctl_done((union ctl_io *)ctsio);
7530 return (CTL_RETVAL_COMPLETE);
7532 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7534 case RSO_OPTIONS_OC_SA:
7535 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7536 service_action >= 32) {
7537 ctl_set_invalid_field(/*ctsio*/ ctsio,
7543 ctl_done((union ctl_io *)ctsio);
7544 return (CTL_RETVAL_COMPLETE);
7546 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7549 ctl_set_invalid_field(/*ctsio*/ ctsio,
7555 ctl_done((union ctl_io *)ctsio);
7556 return (CTL_RETVAL_COMPLETE);
7559 alloc_len = scsi_4btoul(cdb->length);
7561 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7563 ctsio->kern_sg_entries = 0;
7565 if (total_len < alloc_len) {
7566 ctsio->residual = alloc_len - total_len;
7567 ctsio->kern_data_len = total_len;
7568 ctsio->kern_total_len = total_len;
7570 ctsio->residual = 0;
7571 ctsio->kern_data_len = alloc_len;
7572 ctsio->kern_total_len = alloc_len;
7574 ctsio->kern_data_resid = 0;
7575 ctsio->kern_rel_offset = 0;
7577 switch (cdb->options & RSO_OPTIONS_MASK) {
7578 case RSO_OPTIONS_ALL:
7579 all = (struct scsi_report_supported_opcodes_all *)
7580 ctsio->kern_data_ptr;
7582 for (i = 0; i < 256; i++) {
7583 entry = &ctl_cmd_table[i];
7584 if (entry->flags & CTL_CMD_FLAG_SA5) {
7585 for (j = 0; j < 32; j++) {
7586 sentry = &((const struct ctl_cmd_entry *)
7588 if (!ctl_cmd_applicable(
7589 lun->be_lun->lun_type, sentry))
7591 descr = &all->descr[num++];
7593 scsi_ulto2b(j, descr->service_action);
7594 descr->flags = RSO_SERVACTV;
7595 scsi_ulto2b(sentry->length,
7599 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7602 descr = &all->descr[num++];
7604 scsi_ulto2b(0, descr->service_action);
7606 scsi_ulto2b(entry->length, descr->cdb_length);
7610 num * sizeof(struct scsi_report_supported_opcodes_descr),
7613 case RSO_OPTIONS_OC:
7614 one = (struct scsi_report_supported_opcodes_one *)
7615 ctsio->kern_data_ptr;
7616 entry = &ctl_cmd_table[opcode];
7618 case RSO_OPTIONS_OC_SA:
7619 one = (struct scsi_report_supported_opcodes_one *)
7620 ctsio->kern_data_ptr;
7621 entry = &ctl_cmd_table[opcode];
7622 entry = &((const struct ctl_cmd_entry *)
7623 entry->execute)[service_action];
7625 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7627 scsi_ulto2b(entry->length, one->cdb_length);
7628 one->cdb_usage[0] = opcode;
7629 memcpy(&one->cdb_usage[1], entry->usage,
7636 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7637 ctsio->be_move_done = ctl_config_move_done;
7639 ctl_datamove((union ctl_io *)ctsio);
7644 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7646 struct ctl_lun *lun;
7647 struct scsi_report_supported_tmf *cdb;
7648 struct scsi_report_supported_tmf_data *data;
7650 int alloc_len, total_len;
7652 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7654 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7655 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7657 retval = CTL_RETVAL_COMPLETE;
7659 total_len = sizeof(struct scsi_report_supported_tmf_data);
7660 alloc_len = scsi_4btoul(cdb->length);
7662 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7664 ctsio->kern_sg_entries = 0;
7666 if (total_len < alloc_len) {
7667 ctsio->residual = alloc_len - total_len;
7668 ctsio->kern_data_len = total_len;
7669 ctsio->kern_total_len = total_len;
7671 ctsio->residual = 0;
7672 ctsio->kern_data_len = alloc_len;
7673 ctsio->kern_total_len = alloc_len;
7675 ctsio->kern_data_resid = 0;
7676 ctsio->kern_rel_offset = 0;
7678 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7679 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7680 data->byte2 |= RST_ITNRS;
7682 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7683 ctsio->be_move_done = ctl_config_move_done;
7685 ctl_datamove((union ctl_io *)ctsio);
7690 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7692 struct ctl_lun *lun;
7693 struct scsi_report_timestamp *cdb;
7694 struct scsi_report_timestamp_data *data;
7698 int alloc_len, total_len;
7700 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7702 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7703 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7705 retval = CTL_RETVAL_COMPLETE;
7707 total_len = sizeof(struct scsi_report_timestamp_data);
7708 alloc_len = scsi_4btoul(cdb->length);
7710 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7712 ctsio->kern_sg_entries = 0;
7714 if (total_len < alloc_len) {
7715 ctsio->residual = alloc_len - total_len;
7716 ctsio->kern_data_len = total_len;
7717 ctsio->kern_total_len = total_len;
7719 ctsio->residual = 0;
7720 ctsio->kern_data_len = alloc_len;
7721 ctsio->kern_total_len = alloc_len;
7723 ctsio->kern_data_resid = 0;
7724 ctsio->kern_rel_offset = 0;
7726 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7727 scsi_ulto2b(sizeof(*data) - 2, data->length);
7728 data->origin = RTS_ORIG_OUTSIDE;
7730 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7731 scsi_ulto4b(timestamp >> 16, data->timestamp);
7732 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7734 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7735 ctsio->be_move_done = ctl_config_move_done;
7737 ctl_datamove((union ctl_io *)ctsio);
7742 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7744 struct scsi_per_res_in *cdb;
7745 int alloc_len, total_len = 0;
7746 /* struct scsi_per_res_in_rsrv in_data; */
7747 struct ctl_lun *lun;
7748 struct ctl_softc *softc;
7750 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7752 softc = control_softc;
7754 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7756 alloc_len = scsi_2btoul(cdb->length);
7758 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7761 mtx_lock(&lun->lun_lock);
7762 switch (cdb->action) {
7763 case SPRI_RK: /* read keys */
7764 total_len = sizeof(struct scsi_per_res_in_keys) +
7766 sizeof(struct scsi_per_res_key);
7768 case SPRI_RR: /* read reservation */
7769 if (lun->flags & CTL_LUN_PR_RESERVED)
7770 total_len = sizeof(struct scsi_per_res_in_rsrv);
7772 total_len = sizeof(struct scsi_per_res_in_header);
7774 case SPRI_RC: /* report capabilities */
7775 total_len = sizeof(struct scsi_per_res_cap);
7777 case SPRI_RS: /* read full status */
7778 total_len = sizeof(struct scsi_per_res_in_header) +
7779 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7783 panic("Invalid PR type %x", cdb->action);
7785 mtx_unlock(&lun->lun_lock);
7787 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7789 if (total_len < alloc_len) {
7790 ctsio->residual = alloc_len - total_len;
7791 ctsio->kern_data_len = total_len;
7792 ctsio->kern_total_len = total_len;
7794 ctsio->residual = 0;
7795 ctsio->kern_data_len = alloc_len;
7796 ctsio->kern_total_len = alloc_len;
7799 ctsio->kern_data_resid = 0;
7800 ctsio->kern_rel_offset = 0;
7801 ctsio->kern_sg_entries = 0;
7803 mtx_lock(&lun->lun_lock);
7804 switch (cdb->action) {
7805 case SPRI_RK: { // read keys
7806 struct scsi_per_res_in_keys *res_keys;
7809 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7812 * We had to drop the lock to allocate our buffer, which
7813 * leaves time for someone to come in with another
7814 * persistent reservation. (That is unlikely, though,
7815 * since this should be the only persistent reservation
7816 * command active right now.)
7818 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7819 (lun->pr_key_count *
7820 sizeof(struct scsi_per_res_key)))){
7821 mtx_unlock(&lun->lun_lock);
7822 free(ctsio->kern_data_ptr, M_CTL);
7823 printf("%s: reservation length changed, retrying\n",
7828 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7830 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7831 lun->pr_key_count, res_keys->header.length);
7833 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7834 if (!lun->per_res[i].registered)
7838 * We used lun->pr_key_count to calculate the
7839 * size to allocate. If it turns out the number of
7840 * initiators with the registered flag set is
7841 * larger than that (i.e. they haven't been kept in
7842 * sync), we've got a problem.
7844 if (key_count >= lun->pr_key_count) {
7846 csevent_log(CSC_CTL | CSC_SHELF_SW |
7848 csevent_LogType_Fault,
7849 csevent_AlertLevel_Yellow,
7850 csevent_FRU_ShelfController,
7851 csevent_FRU_Firmware,
7852 csevent_FRU_Unknown,
7853 "registered keys %d >= key "
7854 "count %d", key_count,
7860 memcpy(res_keys->keys[key_count].key,
7861 lun->per_res[i].res_key.key,
7862 ctl_min(sizeof(res_keys->keys[key_count].key),
7863 sizeof(lun->per_res[i].res_key)));
7868 case SPRI_RR: { // read reservation
7869 struct scsi_per_res_in_rsrv *res;
7870 int tmp_len, header_only;
7872 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7874 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7876 if (lun->flags & CTL_LUN_PR_RESERVED)
7878 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7879 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7880 res->header.length);
7883 tmp_len = sizeof(struct scsi_per_res_in_header);
7884 scsi_ulto4b(0, res->header.length);
7889 * We had to drop the lock to allocate our buffer, which
7890 * leaves time for someone to come in with another
7891 * persistent reservation. (That is unlikely, though,
7892 * since this should be the only persistent reservation
7893 * command active right now.)
7895 if (tmp_len != total_len) {
7896 mtx_unlock(&lun->lun_lock);
7897 free(ctsio->kern_data_ptr, M_CTL);
7898 printf("%s: reservation status changed, retrying\n",
7904 * No reservation held, so we're done.
7906 if (header_only != 0)
7910 * If the registration is an All Registrants type, the key
7911 * is 0, since it doesn't really matter.
7913 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7914 memcpy(res->data.reservation,
7915 &lun->per_res[lun->pr_res_idx].res_key,
7916 sizeof(struct scsi_per_res_key));
7918 res->data.scopetype = lun->res_type;
7921 case SPRI_RC: //report capabilities
7923 struct scsi_per_res_cap *res_cap;
7926 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7927 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7928 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_3;
7929 type_mask = SPRI_TM_WR_EX_AR |
7935 scsi_ulto2b(type_mask, res_cap->type_mask);
7938 case SPRI_RS: { // read full status
7939 struct scsi_per_res_in_full *res_status;
7940 struct scsi_per_res_in_full_desc *res_desc;
7941 struct ctl_port *port;
7944 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7947 * We had to drop the lock to allocate our buffer, which
7948 * leaves time for someone to come in with another
7949 * persistent reservation. (That is unlikely, though,
7950 * since this should be the only persistent reservation
7951 * command active right now.)
7953 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7954 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7955 lun->pr_key_count)){
7956 mtx_unlock(&lun->lun_lock);
7957 free(ctsio->kern_data_ptr, M_CTL);
7958 printf("%s: reservation length changed, retrying\n",
7963 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7965 res_desc = &res_status->desc[0];
7966 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7967 if (!lun->per_res[i].registered)
7970 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key,
7971 sizeof(res_desc->res_key));
7972 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7973 (lun->pr_res_idx == i ||
7974 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7975 res_desc->flags = SPRI_FULL_R_HOLDER;
7976 res_desc->scopetype = lun->res_type;
7978 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7979 res_desc->rel_trgt_port_id);
7981 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7983 len = ctl_create_iid(port,
7984 i % CTL_MAX_INIT_PER_PORT,
7985 res_desc->transport_id);
7986 scsi_ulto4b(len, res_desc->additional_length);
7987 res_desc = (struct scsi_per_res_in_full_desc *)
7988 &res_desc->transport_id[len];
7990 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7991 res_status->header.length);
7996 * This is a bug, because we just checked for this above,
7997 * and should have returned an error.
7999 panic("Invalid PR type %x", cdb->action);
8000 break; /* NOTREACHED */
8002 mtx_unlock(&lun->lun_lock);
8004 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8005 ctsio->be_move_done = ctl_config_move_done;
8007 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
8008 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
8009 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
8010 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
8011 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
8013 ctl_datamove((union ctl_io *)ctsio);
8015 return (CTL_RETVAL_COMPLETE);
8019 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
8023 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
8024 uint64_t sa_res_key, uint8_t type, uint32_t residx,
8025 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
8026 struct scsi_per_res_out_parms* param)
8028 union ctl_ha_msg persis_io;
8034 mtx_lock(&lun->lun_lock);
8035 if (sa_res_key == 0) {
8036 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8037 /* validate scope and type */
8038 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8040 mtx_unlock(&lun->lun_lock);
8041 ctl_set_invalid_field(/*ctsio*/ ctsio,
8047 ctl_done((union ctl_io *)ctsio);
8051 if (type>8 || type==2 || type==4 || type==0) {
8052 mtx_unlock(&lun->lun_lock);
8053 ctl_set_invalid_field(/*ctsio*/ ctsio,
8059 ctl_done((union ctl_io *)ctsio);
8063 /* temporarily unregister this nexus */
8064 lun->per_res[residx].registered = 0;
8067 * Unregister everybody else and build UA for
8070 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8071 if (lun->per_res[i].registered == 0)
8075 && i <CTL_MAX_INITIATORS)
8076 lun->pending_ua[i] |=
8078 else if (persis_offset
8079 && i >= persis_offset)
8080 lun->pending_ua[i-persis_offset] |=
8082 lun->per_res[i].registered = 0;
8083 memset(&lun->per_res[i].res_key, 0,
8084 sizeof(struct scsi_per_res_key));
8086 lun->per_res[residx].registered = 1;
8087 lun->pr_key_count = 1;
8088 lun->res_type = type;
8089 if (lun->res_type != SPR_TYPE_WR_EX_AR
8090 && lun->res_type != SPR_TYPE_EX_AC_AR)
8091 lun->pr_res_idx = residx;
8093 /* send msg to other side */
8094 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8095 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8096 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8097 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8098 persis_io.pr.pr_info.res_type = type;
8099 memcpy(persis_io.pr.pr_info.sa_res_key,
8100 param->serv_act_res_key,
8101 sizeof(param->serv_act_res_key));
8102 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8103 &persis_io, sizeof(persis_io), 0)) >
8104 CTL_HA_STATUS_SUCCESS) {
8105 printf("CTL:Persis Out error returned "
8106 "from ctl_ha_msg_send %d\n",
8110 /* not all registrants */
8111 mtx_unlock(&lun->lun_lock);
8112 free(ctsio->kern_data_ptr, M_CTL);
8113 ctl_set_invalid_field(ctsio,
8119 ctl_done((union ctl_io *)ctsio);
8122 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8123 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8126 if (res_key == sa_res_key) {
8129 * The spec implies this is not good but doesn't
8130 * say what to do. There are two choices either
8131 * generate a res conflict or check condition
8132 * with illegal field in parameter data. Since
8133 * that is what is done when the sa_res_key is
8134 * zero I'll take that approach since this has
8135 * to do with the sa_res_key.
8137 mtx_unlock(&lun->lun_lock);
8138 free(ctsio->kern_data_ptr, M_CTL);
8139 ctl_set_invalid_field(ctsio,
8145 ctl_done((union ctl_io *)ctsio);
8149 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8150 if (lun->per_res[i].registered
8151 && memcmp(param->serv_act_res_key,
8152 lun->per_res[i].res_key.key,
8153 sizeof(struct scsi_per_res_key)) != 0)
8157 lun->per_res[i].registered = 0;
8158 memset(&lun->per_res[i].res_key, 0,
8159 sizeof(struct scsi_per_res_key));
8160 lun->pr_key_count--;
8162 if (!persis_offset && i < CTL_MAX_INITIATORS)
8163 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT;
8164 else if (persis_offset && i >= persis_offset)
8165 lun->pending_ua[i-persis_offset] |=
8169 mtx_unlock(&lun->lun_lock);
8170 free(ctsio->kern_data_ptr, M_CTL);
8171 ctl_set_reservation_conflict(ctsio);
8172 ctl_done((union ctl_io *)ctsio);
8173 return (CTL_RETVAL_COMPLETE);
8175 /* send msg to other side */
8176 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8177 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8178 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8179 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8180 persis_io.pr.pr_info.res_type = type;
8181 memcpy(persis_io.pr.pr_info.sa_res_key,
8182 param->serv_act_res_key,
8183 sizeof(param->serv_act_res_key));
8184 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8185 &persis_io, sizeof(persis_io), 0)) >
8186 CTL_HA_STATUS_SUCCESS) {
8187 printf("CTL:Persis Out error returned from "
8188 "ctl_ha_msg_send %d\n", isc_retval);
8191 /* Reserved but not all registrants */
8192 /* sa_res_key is res holder */
8193 if (memcmp(param->serv_act_res_key,
8194 lun->per_res[lun->pr_res_idx].res_key.key,
8195 sizeof(struct scsi_per_res_key)) == 0) {
8196 /* validate scope and type */
8197 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8199 mtx_unlock(&lun->lun_lock);
8200 ctl_set_invalid_field(/*ctsio*/ ctsio,
8206 ctl_done((union ctl_io *)ctsio);
8210 if (type>8 || type==2 || type==4 || type==0) {
8211 mtx_unlock(&lun->lun_lock);
8212 ctl_set_invalid_field(/*ctsio*/ ctsio,
8218 ctl_done((union ctl_io *)ctsio);
8224 * if sa_res_key != res_key remove all
8225 * registrants w/sa_res_key and generate UA
8226 * for these registrants(Registrations
8227 * Preempted) if it wasn't an exclusive
8228 * reservation generate UA(Reservations
8229 * Preempted) for all other registered nexuses
8230 * if the type has changed. Establish the new
8231 * reservation and holder. If res_key and
8232 * sa_res_key are the same do the above
8233 * except don't unregister the res holder.
8237 * Temporarily unregister so it won't get
8238 * removed or UA generated
8240 lun->per_res[residx].registered = 0;
8241 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8242 if (lun->per_res[i].registered == 0)
8245 if (memcmp(param->serv_act_res_key,
8246 lun->per_res[i].res_key.key,
8247 sizeof(struct scsi_per_res_key)) == 0) {
8248 lun->per_res[i].registered = 0;
8249 memset(&lun->per_res[i].res_key,
8251 sizeof(struct scsi_per_res_key));
8252 lun->pr_key_count--;
8255 && i < CTL_MAX_INITIATORS)
8256 lun->pending_ua[i] |=
8258 else if (persis_offset
8259 && i >= persis_offset)
8260 lun->pending_ua[i-persis_offset] |=
8262 } else if (type != lun->res_type
8263 && (lun->res_type == SPR_TYPE_WR_EX_RO
8264 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8266 && i < CTL_MAX_INITIATORS)
8267 lun->pending_ua[i] |=
8269 else if (persis_offset
8270 && i >= persis_offset)
8276 lun->per_res[residx].registered = 1;
8277 lun->res_type = type;
8278 if (lun->res_type != SPR_TYPE_WR_EX_AR
8279 && lun->res_type != SPR_TYPE_EX_AC_AR)
8280 lun->pr_res_idx = residx;
8282 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8284 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8285 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8286 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8287 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8288 persis_io.pr.pr_info.res_type = type;
8289 memcpy(persis_io.pr.pr_info.sa_res_key,
8290 param->serv_act_res_key,
8291 sizeof(param->serv_act_res_key));
8292 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8293 &persis_io, sizeof(persis_io), 0)) >
8294 CTL_HA_STATUS_SUCCESS) {
8295 printf("CTL:Persis Out error returned "
8296 "from ctl_ha_msg_send %d\n",
8301 * sa_res_key is not the res holder just
8302 * remove registrants
8306 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8307 if (memcmp(param->serv_act_res_key,
8308 lun->per_res[i].res_key.key,
8309 sizeof(struct scsi_per_res_key)) != 0)
8313 lun->per_res[i].registered = 0;
8314 memset(&lun->per_res[i].res_key, 0,
8315 sizeof(struct scsi_per_res_key));
8316 lun->pr_key_count--;
8319 && i < CTL_MAX_INITIATORS)
8320 lun->pending_ua[i] |=
8322 else if (persis_offset
8323 && i >= persis_offset)
8324 lun->pending_ua[i-persis_offset] |=
8329 mtx_unlock(&lun->lun_lock);
8330 free(ctsio->kern_data_ptr, M_CTL);
8331 ctl_set_reservation_conflict(ctsio);
8332 ctl_done((union ctl_io *)ctsio);
8335 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8336 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8337 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8338 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8339 persis_io.pr.pr_info.res_type = type;
8340 memcpy(persis_io.pr.pr_info.sa_res_key,
8341 param->serv_act_res_key,
8342 sizeof(param->serv_act_res_key));
8343 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8344 &persis_io, sizeof(persis_io), 0)) >
8345 CTL_HA_STATUS_SUCCESS) {
8346 printf("CTL:Persis Out error returned "
8347 "from ctl_ha_msg_send %d\n",
8353 lun->PRGeneration++;
8354 mtx_unlock(&lun->lun_lock);
8360 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8364 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8365 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8366 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
8367 msg->pr.pr_info.sa_res_key,
8368 sizeof(struct scsi_per_res_key)) != 0) {
8369 uint64_t sa_res_key;
8370 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8372 if (sa_res_key == 0) {
8373 /* temporarily unregister this nexus */
8374 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8377 * Unregister everybody else and build UA for
8380 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8381 if (lun->per_res[i].registered == 0)
8385 && i < CTL_MAX_INITIATORS)
8386 lun->pending_ua[i] |=
8388 else if (persis_offset && i >= persis_offset)
8389 lun->pending_ua[i - persis_offset] |=
8391 lun->per_res[i].registered = 0;
8392 memset(&lun->per_res[i].res_key, 0,
8393 sizeof(struct scsi_per_res_key));
8396 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8397 lun->pr_key_count = 1;
8398 lun->res_type = msg->pr.pr_info.res_type;
8399 if (lun->res_type != SPR_TYPE_WR_EX_AR
8400 && lun->res_type != SPR_TYPE_EX_AC_AR)
8401 lun->pr_res_idx = msg->pr.pr_info.residx;
8403 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8404 if (memcmp(msg->pr.pr_info.sa_res_key,
8405 lun->per_res[i].res_key.key,
8406 sizeof(struct scsi_per_res_key)) != 0)
8409 lun->per_res[i].registered = 0;
8410 memset(&lun->per_res[i].res_key, 0,
8411 sizeof(struct scsi_per_res_key));
8412 lun->pr_key_count--;
8415 && i < persis_offset)
8416 lun->pending_ua[i] |=
8418 else if (persis_offset
8419 && i >= persis_offset)
8420 lun->pending_ua[i - persis_offset] |=
8426 * Temporarily unregister so it won't get removed
8429 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8430 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8431 if (lun->per_res[i].registered == 0)
8434 if (memcmp(msg->pr.pr_info.sa_res_key,
8435 lun->per_res[i].res_key.key,
8436 sizeof(struct scsi_per_res_key)) == 0) {
8437 lun->per_res[i].registered = 0;
8438 memset(&lun->per_res[i].res_key, 0,
8439 sizeof(struct scsi_per_res_key));
8440 lun->pr_key_count--;
8442 && i < CTL_MAX_INITIATORS)
8443 lun->pending_ua[i] |=
8445 else if (persis_offset
8446 && i >= persis_offset)
8447 lun->pending_ua[i - persis_offset] |=
8449 } else if (msg->pr.pr_info.res_type != lun->res_type
8450 && (lun->res_type == SPR_TYPE_WR_EX_RO
8451 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8453 && i < persis_offset)
8454 lun->pending_ua[i] |=
8456 else if (persis_offset
8457 && i >= persis_offset)
8458 lun->pending_ua[i - persis_offset] |=
8462 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8463 lun->res_type = msg->pr.pr_info.res_type;
8464 if (lun->res_type != SPR_TYPE_WR_EX_AR
8465 && lun->res_type != SPR_TYPE_EX_AC_AR)
8466 lun->pr_res_idx = msg->pr.pr_info.residx;
8468 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8470 lun->PRGeneration++;
8476 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8480 u_int32_t param_len;
8481 struct scsi_per_res_out *cdb;
8482 struct ctl_lun *lun;
8483 struct scsi_per_res_out_parms* param;
8484 struct ctl_softc *softc;
8486 uint64_t res_key, sa_res_key;
8488 union ctl_ha_msg persis_io;
8491 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8493 retval = CTL_RETVAL_COMPLETE;
8495 softc = control_softc;
8497 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8498 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8501 * We only support whole-LUN scope. The scope & type are ignored for
8502 * register, register and ignore existing key and clear.
8503 * We sometimes ignore scope and type on preempts too!!
8504 * Verify reservation type here as well.
8506 type = cdb->scope_type & SPR_TYPE_MASK;
8507 if ((cdb->action == SPRO_RESERVE)
8508 || (cdb->action == SPRO_RELEASE)) {
8509 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8510 ctl_set_invalid_field(/*ctsio*/ ctsio,
8516 ctl_done((union ctl_io *)ctsio);
8517 return (CTL_RETVAL_COMPLETE);
8520 if (type>8 || type==2 || type==4 || type==0) {
8521 ctl_set_invalid_field(/*ctsio*/ ctsio,
8527 ctl_done((union ctl_io *)ctsio);
8528 return (CTL_RETVAL_COMPLETE);
8532 param_len = scsi_4btoul(cdb->length);
8534 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8535 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8536 ctsio->kern_data_len = param_len;
8537 ctsio->kern_total_len = param_len;
8538 ctsio->kern_data_resid = 0;
8539 ctsio->kern_rel_offset = 0;
8540 ctsio->kern_sg_entries = 0;
8541 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8542 ctsio->be_move_done = ctl_config_move_done;
8543 ctl_datamove((union ctl_io *)ctsio);
8545 return (CTL_RETVAL_COMPLETE);
8548 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8550 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8551 res_key = scsi_8btou64(param->res_key.key);
8552 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8555 * Validate the reservation key here except for SPRO_REG_IGNO
8556 * This must be done for all other service actions
8558 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8559 mtx_lock(&lun->lun_lock);
8560 if (lun->per_res[residx].registered) {
8561 if (memcmp(param->res_key.key,
8562 lun->per_res[residx].res_key.key,
8563 ctl_min(sizeof(param->res_key),
8564 sizeof(lun->per_res[residx].res_key))) != 0) {
8566 * The current key passed in doesn't match
8567 * the one the initiator previously
8570 mtx_unlock(&lun->lun_lock);
8571 free(ctsio->kern_data_ptr, M_CTL);
8572 ctl_set_reservation_conflict(ctsio);
8573 ctl_done((union ctl_io *)ctsio);
8574 return (CTL_RETVAL_COMPLETE);
8576 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8578 * We are not registered
8580 mtx_unlock(&lun->lun_lock);
8581 free(ctsio->kern_data_ptr, M_CTL);
8582 ctl_set_reservation_conflict(ctsio);
8583 ctl_done((union ctl_io *)ctsio);
8584 return (CTL_RETVAL_COMPLETE);
8585 } else if (res_key != 0) {
8587 * We are not registered and trying to register but
8588 * the register key isn't zero.
8590 mtx_unlock(&lun->lun_lock);
8591 free(ctsio->kern_data_ptr, M_CTL);
8592 ctl_set_reservation_conflict(ctsio);
8593 ctl_done((union ctl_io *)ctsio);
8594 return (CTL_RETVAL_COMPLETE);
8596 mtx_unlock(&lun->lun_lock);
8599 switch (cdb->action & SPRO_ACTION_MASK) {
8601 case SPRO_REG_IGNO: {
8604 printf("Registration received\n");
8608 * We don't support any of these options, as we report in
8609 * the read capabilities request (see
8610 * ctl_persistent_reserve_in(), above).
8612 if ((param->flags & SPR_SPEC_I_PT)
8613 || (param->flags & SPR_ALL_TG_PT)
8614 || (param->flags & SPR_APTPL)) {
8617 if (param->flags & SPR_APTPL)
8619 else if (param->flags & SPR_ALL_TG_PT)
8621 else /* SPR_SPEC_I_PT */
8624 free(ctsio->kern_data_ptr, M_CTL);
8625 ctl_set_invalid_field(ctsio,
8631 ctl_done((union ctl_io *)ctsio);
8632 return (CTL_RETVAL_COMPLETE);
8635 mtx_lock(&lun->lun_lock);
8638 * The initiator wants to clear the
8641 if (sa_res_key == 0) {
8643 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8644 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8645 && !lun->per_res[residx].registered)) {
8646 mtx_unlock(&lun->lun_lock);
8650 lun->per_res[residx].registered = 0;
8651 memset(&lun->per_res[residx].res_key,
8652 0, sizeof(lun->per_res[residx].res_key));
8653 lun->pr_key_count--;
8655 if (residx == lun->pr_res_idx) {
8656 lun->flags &= ~CTL_LUN_PR_RESERVED;
8657 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8659 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8660 || lun->res_type == SPR_TYPE_EX_AC_RO)
8661 && lun->pr_key_count) {
8663 * If the reservation is a registrants
8664 * only type we need to generate a UA
8665 * for other registered inits. The
8666 * sense code should be RESERVATIONS
8670 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8672 i+persis_offset].registered
8675 lun->pending_ua[i] |=
8680 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8681 if (lun->pr_key_count==0) {
8682 lun->flags &= ~CTL_LUN_PR_RESERVED;
8684 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8687 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8688 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8689 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8690 persis_io.pr.pr_info.residx = residx;
8691 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8692 &persis_io, sizeof(persis_io), 0 )) >
8693 CTL_HA_STATUS_SUCCESS) {
8694 printf("CTL:Persis Out error returned from "
8695 "ctl_ha_msg_send %d\n", isc_retval);
8697 } else /* sa_res_key != 0 */ {
8700 * If we aren't registered currently then increment
8701 * the key count and set the registered flag.
8703 if (!lun->per_res[residx].registered) {
8704 lun->pr_key_count++;
8705 lun->per_res[residx].registered = 1;
8708 memcpy(&lun->per_res[residx].res_key,
8709 param->serv_act_res_key,
8710 ctl_min(sizeof(param->serv_act_res_key),
8711 sizeof(lun->per_res[residx].res_key)));
8713 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8714 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8715 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8716 persis_io.pr.pr_info.residx = residx;
8717 memcpy(persis_io.pr.pr_info.sa_res_key,
8718 param->serv_act_res_key,
8719 sizeof(param->serv_act_res_key));
8720 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8721 &persis_io, sizeof(persis_io), 0)) >
8722 CTL_HA_STATUS_SUCCESS) {
8723 printf("CTL:Persis Out error returned from "
8724 "ctl_ha_msg_send %d\n", isc_retval);
8727 lun->PRGeneration++;
8728 mtx_unlock(&lun->lun_lock);
8734 printf("Reserve executed type %d\n", type);
8736 mtx_lock(&lun->lun_lock);
8737 if (lun->flags & CTL_LUN_PR_RESERVED) {
8739 * if this isn't the reservation holder and it's
8740 * not a "all registrants" type or if the type is
8741 * different then we have a conflict
8743 if ((lun->pr_res_idx != residx
8744 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8745 || lun->res_type != type) {
8746 mtx_unlock(&lun->lun_lock);
8747 free(ctsio->kern_data_ptr, M_CTL);
8748 ctl_set_reservation_conflict(ctsio);
8749 ctl_done((union ctl_io *)ctsio);
8750 return (CTL_RETVAL_COMPLETE);
8752 mtx_unlock(&lun->lun_lock);
8753 } else /* create a reservation */ {
8755 * If it's not an "all registrants" type record
8756 * reservation holder
8758 if (type != SPR_TYPE_WR_EX_AR
8759 && type != SPR_TYPE_EX_AC_AR)
8760 lun->pr_res_idx = residx; /* Res holder */
8762 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8764 lun->flags |= CTL_LUN_PR_RESERVED;
8765 lun->res_type = type;
8767 mtx_unlock(&lun->lun_lock);
8769 /* send msg to other side */
8770 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8771 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8772 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8773 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8774 persis_io.pr.pr_info.res_type = type;
8775 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8776 &persis_io, sizeof(persis_io), 0)) >
8777 CTL_HA_STATUS_SUCCESS) {
8778 printf("CTL:Persis Out error returned from "
8779 "ctl_ha_msg_send %d\n", isc_retval);
8785 mtx_lock(&lun->lun_lock);
8786 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8787 /* No reservation exists return good status */
8788 mtx_unlock(&lun->lun_lock);
8792 * Is this nexus a reservation holder?
8794 if (lun->pr_res_idx != residx
8795 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8797 * not a res holder return good status but
8800 mtx_unlock(&lun->lun_lock);
8804 if (lun->res_type != type) {
8805 mtx_unlock(&lun->lun_lock);
8806 free(ctsio->kern_data_ptr, M_CTL);
8807 ctl_set_illegal_pr_release(ctsio);
8808 ctl_done((union ctl_io *)ctsio);
8809 return (CTL_RETVAL_COMPLETE);
8812 /* okay to release */
8813 lun->flags &= ~CTL_LUN_PR_RESERVED;
8814 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8818 * if this isn't an exclusive access
8819 * res generate UA for all other
8822 if (type != SPR_TYPE_EX_AC
8823 && type != SPR_TYPE_WR_EX) {
8825 * temporarily unregister so we don't generate UA
8827 lun->per_res[residx].registered = 0;
8829 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8830 if (lun->per_res[i+persis_offset].registered
8833 lun->pending_ua[i] |=
8837 lun->per_res[residx].registered = 1;
8839 mtx_unlock(&lun->lun_lock);
8840 /* Send msg to other side */
8841 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8842 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8843 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8844 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8845 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8846 printf("CTL:Persis Out error returned from "
8847 "ctl_ha_msg_send %d\n", isc_retval);
8852 /* send msg to other side */
8854 mtx_lock(&lun->lun_lock);
8855 lun->flags &= ~CTL_LUN_PR_RESERVED;
8857 lun->pr_key_count = 0;
8858 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8861 memset(&lun->per_res[residx].res_key,
8862 0, sizeof(lun->per_res[residx].res_key));
8863 lun->per_res[residx].registered = 0;
8865 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8866 if (lun->per_res[i].registered) {
8867 if (!persis_offset && i < CTL_MAX_INITIATORS)
8868 lun->pending_ua[i] |=
8870 else if (persis_offset && i >= persis_offset)
8871 lun->pending_ua[i-persis_offset] |=
8874 memset(&lun->per_res[i].res_key,
8875 0, sizeof(struct scsi_per_res_key));
8876 lun->per_res[i].registered = 0;
8878 lun->PRGeneration++;
8879 mtx_unlock(&lun->lun_lock);
8880 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8881 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8882 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8883 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8884 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8885 printf("CTL:Persis Out error returned from "
8886 "ctl_ha_msg_send %d\n", isc_retval);
8890 case SPRO_PREEMPT: {
8893 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8894 residx, ctsio, cdb, param);
8896 return (CTL_RETVAL_COMPLETE);
8900 panic("Invalid PR type %x", cdb->action);
8904 free(ctsio->kern_data_ptr, M_CTL);
8905 ctl_set_success(ctsio);
8906 ctl_done((union ctl_io *)ctsio);
8912 * This routine is for handling a message from the other SC pertaining to
8913 * persistent reserve out. All the error checking will have been done
8914 * so only perorming the action need be done here to keep the two
8918 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8920 struct ctl_lun *lun;
8921 struct ctl_softc *softc;
8925 softc = control_softc;
8927 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8928 lun = softc->ctl_luns[targ_lun];
8929 mtx_lock(&lun->lun_lock);
8930 switch(msg->pr.pr_info.action) {
8931 case CTL_PR_REG_KEY:
8932 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8933 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8934 lun->pr_key_count++;
8936 lun->PRGeneration++;
8937 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8938 msg->pr.pr_info.sa_res_key,
8939 sizeof(struct scsi_per_res_key));
8942 case CTL_PR_UNREG_KEY:
8943 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8944 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8945 0, sizeof(struct scsi_per_res_key));
8946 lun->pr_key_count--;
8948 /* XXX Need to see if the reservation has been released */
8949 /* if so do we need to generate UA? */
8950 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8951 lun->flags &= ~CTL_LUN_PR_RESERVED;
8952 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8954 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8955 || lun->res_type == SPR_TYPE_EX_AC_RO)
8956 && lun->pr_key_count) {
8958 * If the reservation is a registrants
8959 * only type we need to generate a UA
8960 * for other registered inits. The
8961 * sense code should be RESERVATIONS
8965 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8967 persis_offset].registered == 0)
8970 lun->pending_ua[i] |=
8975 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8976 if (lun->pr_key_count==0) {
8977 lun->flags &= ~CTL_LUN_PR_RESERVED;
8979 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8982 lun->PRGeneration++;
8985 case CTL_PR_RESERVE:
8986 lun->flags |= CTL_LUN_PR_RESERVED;
8987 lun->res_type = msg->pr.pr_info.res_type;
8988 lun->pr_res_idx = msg->pr.pr_info.residx;
8992 case CTL_PR_RELEASE:
8994 * if this isn't an exclusive access res generate UA for all
8995 * other registrants.
8997 if (lun->res_type != SPR_TYPE_EX_AC
8998 && lun->res_type != SPR_TYPE_WR_EX) {
8999 for (i = 0; i < CTL_MAX_INITIATORS; i++)
9000 if (lun->per_res[i+persis_offset].registered)
9001 lun->pending_ua[i] |=
9005 lun->flags &= ~CTL_LUN_PR_RESERVED;
9006 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
9010 case CTL_PR_PREEMPT:
9011 ctl_pro_preempt_other(lun, msg);
9014 lun->flags &= ~CTL_LUN_PR_RESERVED;
9016 lun->pr_key_count = 0;
9017 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
9019 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
9020 if (lun->per_res[i].registered == 0)
9023 && i < CTL_MAX_INITIATORS)
9024 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT;
9025 else if (persis_offset
9026 && i >= persis_offset)
9027 lun->pending_ua[i-persis_offset] |=
9029 memset(&lun->per_res[i].res_key, 0,
9030 sizeof(struct scsi_per_res_key));
9031 lun->per_res[i].registered = 0;
9033 lun->PRGeneration++;
9037 mtx_unlock(&lun->lun_lock);
9041 ctl_read_write(struct ctl_scsiio *ctsio)
9043 struct ctl_lun *lun;
9044 struct ctl_lba_len_flags *lbalen;
9046 uint32_t num_blocks;
9050 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9052 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
9055 retval = CTL_RETVAL_COMPLETE;
9057 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
9058 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
9059 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
9063 * XXX KDM need a lock here.
9065 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
9066 if ((lun->res_type == SPR_TYPE_EX_AC
9067 && residx != lun->pr_res_idx)
9068 || ((lun->res_type == SPR_TYPE_EX_AC_RO
9069 || lun->res_type == SPR_TYPE_EX_AC_AR)
9070 && !lun->per_res[residx].registered)) {
9071 ctl_set_reservation_conflict(ctsio);
9072 ctl_done((union ctl_io *)ctsio);
9073 return (CTL_RETVAL_COMPLETE);
9077 switch (ctsio->cdb[0]) {
9080 struct scsi_rw_6 *cdb;
9082 cdb = (struct scsi_rw_6 *)ctsio->cdb;
9084 lba = scsi_3btoul(cdb->addr);
9085 /* only 5 bits are valid in the most significant address byte */
9087 num_blocks = cdb->length;
9089 * This is correct according to SBC-2.
9091 if (num_blocks == 0)
9097 struct scsi_rw_10 *cdb;
9099 cdb = (struct scsi_rw_10 *)ctsio->cdb;
9100 if (cdb->byte2 & SRW10_FUA)
9101 flags |= CTL_LLF_FUA;
9102 if (cdb->byte2 & SRW10_DPO)
9103 flags |= CTL_LLF_DPO;
9104 lba = scsi_4btoul(cdb->addr);
9105 num_blocks = scsi_2btoul(cdb->length);
9108 case WRITE_VERIFY_10: {
9109 struct scsi_write_verify_10 *cdb;
9111 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
9112 flags |= CTL_LLF_FUA;
9113 if (cdb->byte2 & SWV_DPO)
9114 flags |= CTL_LLF_DPO;
9115 lba = scsi_4btoul(cdb->addr);
9116 num_blocks = scsi_2btoul(cdb->length);
9121 struct scsi_rw_12 *cdb;
9123 cdb = (struct scsi_rw_12 *)ctsio->cdb;
9124 if (cdb->byte2 & SRW12_FUA)
9125 flags |= CTL_LLF_FUA;
9126 if (cdb->byte2 & SRW12_DPO)
9127 flags |= CTL_LLF_DPO;
9128 lba = scsi_4btoul(cdb->addr);
9129 num_blocks = scsi_4btoul(cdb->length);
9132 case WRITE_VERIFY_12: {
9133 struct scsi_write_verify_12 *cdb;
9135 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
9136 flags |= CTL_LLF_FUA;
9137 if (cdb->byte2 & SWV_DPO)
9138 flags |= CTL_LLF_DPO;
9139 lba = scsi_4btoul(cdb->addr);
9140 num_blocks = scsi_4btoul(cdb->length);
9145 struct scsi_rw_16 *cdb;
9147 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9148 if (cdb->byte2 & SRW12_FUA)
9149 flags |= CTL_LLF_FUA;
9150 if (cdb->byte2 & SRW12_DPO)
9151 flags |= CTL_LLF_DPO;
9152 lba = scsi_8btou64(cdb->addr);
9153 num_blocks = scsi_4btoul(cdb->length);
9156 case WRITE_VERIFY_16: {
9157 struct scsi_write_verify_16 *cdb;
9159 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
9160 flags |= CTL_LLF_FUA;
9161 if (cdb->byte2 & SWV_DPO)
9162 flags |= CTL_LLF_DPO;
9163 lba = scsi_8btou64(cdb->addr);
9164 num_blocks = scsi_4btoul(cdb->length);
9169 * We got a command we don't support. This shouldn't
9170 * happen, commands should be filtered out above us.
9172 ctl_set_invalid_opcode(ctsio);
9173 ctl_done((union ctl_io *)ctsio);
9175 return (CTL_RETVAL_COMPLETE);
9176 break; /* NOTREACHED */
9180 * The first check is to make sure we're in bounds, the second
9181 * check is to catch wrap-around problems. If the lba + num blocks
9182 * is less than the lba, then we've wrapped around and the block
9183 * range is invalid anyway.
9185 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9186 || ((lba + num_blocks) < lba)) {
9187 ctl_set_lba_out_of_range(ctsio);
9188 ctl_done((union ctl_io *)ctsio);
9189 return (CTL_RETVAL_COMPLETE);
9193 * According to SBC-3, a transfer length of 0 is not an error.
9194 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9195 * translates to 256 blocks for those commands.
9197 if (num_blocks == 0) {
9198 ctl_set_success(ctsio);
9199 ctl_done((union ctl_io *)ctsio);
9200 return (CTL_RETVAL_COMPLETE);
9203 /* Set FUA and/or DPO if caches are disabled. */
9205 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9207 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
9209 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9211 flags |= CTL_LLF_FUA;
9214 lbalen = (struct ctl_lba_len_flags *)
9215 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9217 lbalen->len = num_blocks;
9218 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
9220 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9221 ctsio->kern_rel_offset = 0;
9223 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9225 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9231 ctl_cnw_cont(union ctl_io *io)
9233 struct ctl_scsiio *ctsio;
9234 struct ctl_lun *lun;
9235 struct ctl_lba_len_flags *lbalen;
9238 ctsio = &io->scsiio;
9239 ctsio->io_hdr.status = CTL_STATUS_NONE;
9240 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9241 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9242 lbalen = (struct ctl_lba_len_flags *)
9243 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9244 lbalen->flags &= ~CTL_LLF_COMPARE;
9245 lbalen->flags |= CTL_LLF_WRITE;
9247 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9248 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9253 ctl_cnw(struct ctl_scsiio *ctsio)
9255 struct ctl_lun *lun;
9256 struct ctl_lba_len_flags *lbalen;
9258 uint32_t num_blocks;
9261 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9263 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9266 retval = CTL_RETVAL_COMPLETE;
9268 switch (ctsio->cdb[0]) {
9269 case COMPARE_AND_WRITE: {
9270 struct scsi_compare_and_write *cdb;
9272 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9273 if (cdb->byte2 & SRW10_FUA)
9274 flags |= CTL_LLF_FUA;
9275 if (cdb->byte2 & SRW10_DPO)
9276 flags |= CTL_LLF_DPO;
9277 lba = scsi_8btou64(cdb->addr);
9278 num_blocks = cdb->length;
9283 * We got a command we don't support. This shouldn't
9284 * happen, commands should be filtered out above us.
9286 ctl_set_invalid_opcode(ctsio);
9287 ctl_done((union ctl_io *)ctsio);
9289 return (CTL_RETVAL_COMPLETE);
9290 break; /* NOTREACHED */
9294 * The first check is to make sure we're in bounds, the second
9295 * check is to catch wrap-around problems. If the lba + num blocks
9296 * is less than the lba, then we've wrapped around and the block
9297 * range is invalid anyway.
9299 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9300 || ((lba + num_blocks) < lba)) {
9301 ctl_set_lba_out_of_range(ctsio);
9302 ctl_done((union ctl_io *)ctsio);
9303 return (CTL_RETVAL_COMPLETE);
9307 * According to SBC-3, a transfer length of 0 is not an error.
9309 if (num_blocks == 0) {
9310 ctl_set_success(ctsio);
9311 ctl_done((union ctl_io *)ctsio);
9312 return (CTL_RETVAL_COMPLETE);
9315 /* Set FUA if write cache is disabled. */
9316 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9318 flags |= CTL_LLF_FUA;
9320 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9321 ctsio->kern_rel_offset = 0;
9324 * Set the IO_CONT flag, so that if this I/O gets passed to
9325 * ctl_data_submit_done(), it'll get passed back to
9326 * ctl_ctl_cnw_cont() for further processing.
9328 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9329 ctsio->io_cont = ctl_cnw_cont;
9331 lbalen = (struct ctl_lba_len_flags *)
9332 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9334 lbalen->len = num_blocks;
9335 lbalen->flags = CTL_LLF_COMPARE | flags;
9337 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9338 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9343 ctl_verify(struct ctl_scsiio *ctsio)
9345 struct ctl_lun *lun;
9346 struct ctl_lba_len_flags *lbalen;
9348 uint32_t num_blocks;
9352 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9354 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9357 flags = CTL_LLF_FUA;
9358 retval = CTL_RETVAL_COMPLETE;
9360 switch (ctsio->cdb[0]) {
9362 struct scsi_verify_10 *cdb;
9364 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9365 if (cdb->byte2 & SVFY_BYTCHK)
9367 if (cdb->byte2 & SVFY_DPO)
9368 flags |= CTL_LLF_DPO;
9369 lba = scsi_4btoul(cdb->addr);
9370 num_blocks = scsi_2btoul(cdb->length);
9374 struct scsi_verify_12 *cdb;
9376 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9377 if (cdb->byte2 & SVFY_BYTCHK)
9379 if (cdb->byte2 & SVFY_DPO)
9380 flags |= CTL_LLF_DPO;
9381 lba = scsi_4btoul(cdb->addr);
9382 num_blocks = scsi_4btoul(cdb->length);
9386 struct scsi_rw_16 *cdb;
9388 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9389 if (cdb->byte2 & SVFY_BYTCHK)
9391 if (cdb->byte2 & SVFY_DPO)
9392 flags |= CTL_LLF_DPO;
9393 lba = scsi_8btou64(cdb->addr);
9394 num_blocks = scsi_4btoul(cdb->length);
9399 * We got a command we don't support. This shouldn't
9400 * happen, commands should be filtered out above us.
9402 ctl_set_invalid_opcode(ctsio);
9403 ctl_done((union ctl_io *)ctsio);
9404 return (CTL_RETVAL_COMPLETE);
9408 * The first check is to make sure we're in bounds, the second
9409 * check is to catch wrap-around problems. If the lba + num blocks
9410 * is less than the lba, then we've wrapped around and the block
9411 * range is invalid anyway.
9413 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9414 || ((lba + num_blocks) < lba)) {
9415 ctl_set_lba_out_of_range(ctsio);
9416 ctl_done((union ctl_io *)ctsio);
9417 return (CTL_RETVAL_COMPLETE);
9421 * According to SBC-3, a transfer length of 0 is not an error.
9423 if (num_blocks == 0) {
9424 ctl_set_success(ctsio);
9425 ctl_done((union ctl_io *)ctsio);
9426 return (CTL_RETVAL_COMPLETE);
9429 lbalen = (struct ctl_lba_len_flags *)
9430 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9432 lbalen->len = num_blocks;
9434 lbalen->flags = CTL_LLF_COMPARE | flags;
9435 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9437 lbalen->flags = CTL_LLF_VERIFY | flags;
9438 ctsio->kern_total_len = 0;
9440 ctsio->kern_rel_offset = 0;
9442 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9443 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9448 ctl_report_luns(struct ctl_scsiio *ctsio)
9450 struct scsi_report_luns *cdb;
9451 struct scsi_report_luns_data *lun_data;
9452 struct ctl_lun *lun, *request_lun;
9453 int num_luns, retval;
9454 uint32_t alloc_len, lun_datalen;
9455 int num_filled, well_known;
9456 uint32_t initidx, targ_lun_id, lun_id;
9458 retval = CTL_RETVAL_COMPLETE;
9461 cdb = (struct scsi_report_luns *)ctsio->cdb;
9463 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9465 mtx_lock(&control_softc->ctl_lock);
9466 num_luns = control_softc->num_luns;
9467 mtx_unlock(&control_softc->ctl_lock);
9469 switch (cdb->select_report) {
9470 case RPL_REPORT_DEFAULT:
9471 case RPL_REPORT_ALL:
9473 case RPL_REPORT_WELLKNOWN:
9478 ctl_set_invalid_field(ctsio,
9484 ctl_done((union ctl_io *)ctsio);
9486 break; /* NOTREACHED */
9489 alloc_len = scsi_4btoul(cdb->length);
9491 * The initiator has to allocate at least 16 bytes for this request,
9492 * so he can at least get the header and the first LUN. Otherwise
9493 * we reject the request (per SPC-3 rev 14, section 6.21).
9495 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9496 sizeof(struct scsi_report_luns_lundata))) {
9497 ctl_set_invalid_field(ctsio,
9503 ctl_done((union ctl_io *)ctsio);
9507 request_lun = (struct ctl_lun *)
9508 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9510 lun_datalen = sizeof(*lun_data) +
9511 (num_luns * sizeof(struct scsi_report_luns_lundata));
9513 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9514 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9515 ctsio->kern_sg_entries = 0;
9517 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9519 mtx_lock(&control_softc->ctl_lock);
9520 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9521 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9522 if (lun_id >= CTL_MAX_LUNS)
9524 lun = control_softc->ctl_luns[lun_id];
9528 if (targ_lun_id <= 0xff) {
9530 * Peripheral addressing method, bus number 0.
9532 lun_data->luns[num_filled].lundata[0] =
9533 RPL_LUNDATA_ATYP_PERIPH;
9534 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9536 } else if (targ_lun_id <= 0x3fff) {
9538 * Flat addressing method.
9540 lun_data->luns[num_filled].lundata[0] =
9541 RPL_LUNDATA_ATYP_FLAT |
9542 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9543 #ifdef OLDCTLHEADERS
9544 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9545 (targ_lun_id & SRLD_BUS_LUN_MASK);
9547 lun_data->luns[num_filled].lundata[1] =
9548 #ifdef OLDCTLHEADERS
9549 targ_lun_id >> SRLD_BUS_LUN_BITS;
9551 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9554 printf("ctl_report_luns: bogus LUN number %jd, "
9555 "skipping\n", (intmax_t)targ_lun_id);
9558 * According to SPC-3, rev 14 section 6.21:
9560 * "The execution of a REPORT LUNS command to any valid and
9561 * installed logical unit shall clear the REPORTED LUNS DATA
9562 * HAS CHANGED unit attention condition for all logical
9563 * units of that target with respect to the requesting
9564 * initiator. A valid and installed logical unit is one
9565 * having a PERIPHERAL QUALIFIER of 000b in the standard
9566 * INQUIRY data (see 6.4.2)."
9568 * If request_lun is NULL, the LUN this report luns command
9569 * was issued to is either disabled or doesn't exist. In that
9570 * case, we shouldn't clear any pending lun change unit
9573 if (request_lun != NULL) {
9574 mtx_lock(&lun->lun_lock);
9575 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE;
9576 mtx_unlock(&lun->lun_lock);
9579 mtx_unlock(&control_softc->ctl_lock);
9582 * It's quite possible that we've returned fewer LUNs than we allocated
9583 * space for. Trim it.
9585 lun_datalen = sizeof(*lun_data) +
9586 (num_filled * sizeof(struct scsi_report_luns_lundata));
9588 if (lun_datalen < alloc_len) {
9589 ctsio->residual = alloc_len - lun_datalen;
9590 ctsio->kern_data_len = lun_datalen;
9591 ctsio->kern_total_len = lun_datalen;
9593 ctsio->residual = 0;
9594 ctsio->kern_data_len = alloc_len;
9595 ctsio->kern_total_len = alloc_len;
9597 ctsio->kern_data_resid = 0;
9598 ctsio->kern_rel_offset = 0;
9599 ctsio->kern_sg_entries = 0;
9602 * We set this to the actual data length, regardless of how much
9603 * space we actually have to return results. If the user looks at
9604 * this value, he'll know whether or not he allocated enough space
9605 * and reissue the command if necessary. We don't support well
9606 * known logical units, so if the user asks for that, return none.
9608 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9611 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9614 ctsio->scsi_status = SCSI_STATUS_OK;
9616 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9617 ctsio->be_move_done = ctl_config_move_done;
9618 ctl_datamove((union ctl_io *)ctsio);
9624 ctl_request_sense(struct ctl_scsiio *ctsio)
9626 struct scsi_request_sense *cdb;
9627 struct scsi_sense_data *sense_ptr;
9628 struct ctl_lun *lun;
9631 scsi_sense_data_type sense_format;
9633 cdb = (struct scsi_request_sense *)ctsio->cdb;
9635 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9637 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9640 * Determine which sense format the user wants.
9642 if (cdb->byte2 & SRS_DESC)
9643 sense_format = SSD_TYPE_DESC;
9645 sense_format = SSD_TYPE_FIXED;
9647 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9648 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9649 ctsio->kern_sg_entries = 0;
9652 * struct scsi_sense_data, which is currently set to 256 bytes, is
9653 * larger than the largest allowed value for the length field in the
9654 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9656 ctsio->residual = 0;
9657 ctsio->kern_data_len = cdb->length;
9658 ctsio->kern_total_len = cdb->length;
9660 ctsio->kern_data_resid = 0;
9661 ctsio->kern_rel_offset = 0;
9662 ctsio->kern_sg_entries = 0;
9665 * If we don't have a LUN, we don't have any pending sense.
9671 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9673 * Check for pending sense, and then for pending unit attentions.
9674 * Pending sense gets returned first, then pending unit attentions.
9676 mtx_lock(&lun->lun_lock);
9678 if (ctl_is_set(lun->have_ca, initidx)) {
9679 scsi_sense_data_type stored_format;
9682 * Check to see which sense format was used for the stored
9685 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9688 * If the user requested a different sense format than the
9689 * one we stored, then we need to convert it to the other
9690 * format. If we're going from descriptor to fixed format
9691 * sense data, we may lose things in translation, depending
9692 * on what options were used.
9694 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9695 * for some reason we'll just copy it out as-is.
9697 if ((stored_format == SSD_TYPE_FIXED)
9698 && (sense_format == SSD_TYPE_DESC))
9699 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9700 &lun->pending_sense[initidx],
9701 (struct scsi_sense_data_desc *)sense_ptr);
9702 else if ((stored_format == SSD_TYPE_DESC)
9703 && (sense_format == SSD_TYPE_FIXED))
9704 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9705 &lun->pending_sense[initidx],
9706 (struct scsi_sense_data_fixed *)sense_ptr);
9708 memcpy(sense_ptr, &lun->pending_sense[initidx],
9709 ctl_min(sizeof(*sense_ptr),
9710 sizeof(lun->pending_sense[initidx])));
9712 ctl_clear_mask(lun->have_ca, initidx);
9716 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
9717 ctl_ua_type ua_type;
9719 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
9720 sense_ptr, sense_format);
9721 if (ua_type != CTL_UA_NONE)
9724 mtx_unlock(&lun->lun_lock);
9727 * We already have a pending error, return it.
9729 if (have_error != 0) {
9731 * We report the SCSI status as OK, since the status of the
9732 * request sense command itself is OK.
9734 ctsio->scsi_status = SCSI_STATUS_OK;
9737 * We report 0 for the sense length, because we aren't doing
9738 * autosense in this case. We're reporting sense as
9741 ctsio->sense_len = 0;
9742 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9743 ctsio->be_move_done = ctl_config_move_done;
9744 ctl_datamove((union ctl_io *)ctsio);
9746 return (CTL_RETVAL_COMPLETE);
9752 * No sense information to report, so we report that everything is
9755 ctl_set_sense_data(sense_ptr,
9758 /*current_error*/ 1,
9759 /*sense_key*/ SSD_KEY_NO_SENSE,
9764 ctsio->scsi_status = SCSI_STATUS_OK;
9767 * We report 0 for the sense length, because we aren't doing
9768 * autosense in this case. We're reporting sense as parameter data.
9770 ctsio->sense_len = 0;
9771 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9772 ctsio->be_move_done = ctl_config_move_done;
9773 ctl_datamove((union ctl_io *)ctsio);
9775 return (CTL_RETVAL_COMPLETE);
9779 ctl_tur(struct ctl_scsiio *ctsio)
9781 struct ctl_lun *lun;
9783 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9785 CTL_DEBUG_PRINT(("ctl_tur\n"));
9790 ctsio->scsi_status = SCSI_STATUS_OK;
9791 ctsio->io_hdr.status = CTL_SUCCESS;
9793 ctl_done((union ctl_io *)ctsio);
9795 return (CTL_RETVAL_COMPLETE);
9800 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9807 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9809 struct scsi_vpd_supported_pages *pages;
9811 struct ctl_lun *lun;
9813 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9815 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9816 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9817 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9818 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9819 ctsio->kern_sg_entries = 0;
9821 if (sup_page_size < alloc_len) {
9822 ctsio->residual = alloc_len - sup_page_size;
9823 ctsio->kern_data_len = sup_page_size;
9824 ctsio->kern_total_len = sup_page_size;
9826 ctsio->residual = 0;
9827 ctsio->kern_data_len = alloc_len;
9828 ctsio->kern_total_len = alloc_len;
9830 ctsio->kern_data_resid = 0;
9831 ctsio->kern_rel_offset = 0;
9832 ctsio->kern_sg_entries = 0;
9835 * The control device is always connected. The disk device, on the
9836 * other hand, may not be online all the time. Need to change this
9837 * to figure out whether the disk device is actually online or not.
9840 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9841 lun->be_lun->lun_type;
9843 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9845 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9846 /* Supported VPD pages */
9847 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9849 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9850 /* Device Identification */
9851 pages->page_list[2] = SVPD_DEVICE_ID;
9852 /* Mode Page Policy */
9853 pages->page_list[3] = SVPD_MODE_PAGE_POLICY;
9855 pages->page_list[4] = SVPD_SCSI_PORTS;
9856 /* Third-party Copy */
9857 pages->page_list[5] = SVPD_SCSI_TPC;
9859 pages->page_list[6] = SVPD_BLOCK_LIMITS;
9860 /* Block Device Characteristics */
9861 pages->page_list[7] = SVPD_BDC;
9862 /* Logical Block Provisioning */
9863 pages->page_list[8] = SVPD_LBP;
9865 ctsio->scsi_status = SCSI_STATUS_OK;
9867 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9868 ctsio->be_move_done = ctl_config_move_done;
9869 ctl_datamove((union ctl_io *)ctsio);
9871 return (CTL_RETVAL_COMPLETE);
9875 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9877 struct scsi_vpd_unit_serial_number *sn_ptr;
9878 struct ctl_lun *lun;
9880 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9882 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9883 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9884 ctsio->kern_sg_entries = 0;
9886 if (sizeof(*sn_ptr) < alloc_len) {
9887 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9888 ctsio->kern_data_len = sizeof(*sn_ptr);
9889 ctsio->kern_total_len = sizeof(*sn_ptr);
9891 ctsio->residual = 0;
9892 ctsio->kern_data_len = alloc_len;
9893 ctsio->kern_total_len = alloc_len;
9895 ctsio->kern_data_resid = 0;
9896 ctsio->kern_rel_offset = 0;
9897 ctsio->kern_sg_entries = 0;
9900 * The control device is always connected. The disk device, on the
9901 * other hand, may not be online all the time. Need to change this
9902 * to figure out whether the disk device is actually online or not.
9905 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9906 lun->be_lun->lun_type;
9908 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9910 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9911 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9913 * If we don't have a LUN, we just leave the serial number as
9916 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9918 strncpy((char *)sn_ptr->serial_num,
9919 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9921 ctsio->scsi_status = SCSI_STATUS_OK;
9923 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9924 ctsio->be_move_done = ctl_config_move_done;
9925 ctl_datamove((union ctl_io *)ctsio);
9927 return (CTL_RETVAL_COMPLETE);
9932 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9934 struct scsi_vpd_mode_page_policy *mpp_ptr;
9935 struct ctl_lun *lun;
9938 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9940 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9941 sizeof(struct scsi_vpd_mode_page_policy_descr);
9943 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9944 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9945 ctsio->kern_sg_entries = 0;
9947 if (data_len < alloc_len) {
9948 ctsio->residual = alloc_len - data_len;
9949 ctsio->kern_data_len = data_len;
9950 ctsio->kern_total_len = data_len;
9952 ctsio->residual = 0;
9953 ctsio->kern_data_len = alloc_len;
9954 ctsio->kern_total_len = alloc_len;
9956 ctsio->kern_data_resid = 0;
9957 ctsio->kern_rel_offset = 0;
9958 ctsio->kern_sg_entries = 0;
9961 * The control device is always connected. The disk device, on the
9962 * other hand, may not be online all the time.
9965 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9966 lun->be_lun->lun_type;
9968 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9969 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9970 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9971 mpp_ptr->descr[0].page_code = 0x3f;
9972 mpp_ptr->descr[0].subpage_code = 0xff;
9973 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9975 ctsio->scsi_status = SCSI_STATUS_OK;
9976 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9977 ctsio->be_move_done = ctl_config_move_done;
9978 ctl_datamove((union ctl_io *)ctsio);
9980 return (CTL_RETVAL_COMPLETE);
9984 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9986 struct scsi_vpd_device_id *devid_ptr;
9987 struct scsi_vpd_id_descriptor *desc;
9988 struct ctl_softc *ctl_softc;
9989 struct ctl_lun *lun;
9990 struct ctl_port *port;
9994 ctl_softc = control_softc;
9996 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
9997 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9999 data_len = sizeof(struct scsi_vpd_device_id) +
10000 sizeof(struct scsi_vpd_id_descriptor) +
10001 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
10002 sizeof(struct scsi_vpd_id_descriptor) +
10003 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
10004 if (lun && lun->lun_devid)
10005 data_len += lun->lun_devid->len;
10006 if (port->port_devid)
10007 data_len += port->port_devid->len;
10008 if (port->target_devid)
10009 data_len += port->target_devid->len;
10011 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10012 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
10013 ctsio->kern_sg_entries = 0;
10015 if (data_len < alloc_len) {
10016 ctsio->residual = alloc_len - data_len;
10017 ctsio->kern_data_len = data_len;
10018 ctsio->kern_total_len = data_len;
10020 ctsio->residual = 0;
10021 ctsio->kern_data_len = alloc_len;
10022 ctsio->kern_total_len = alloc_len;
10024 ctsio->kern_data_resid = 0;
10025 ctsio->kern_rel_offset = 0;
10026 ctsio->kern_sg_entries = 0;
10029 * The control device is always connected. The disk device, on the
10030 * other hand, may not be online all the time.
10033 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10034 lun->be_lun->lun_type;
10036 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10037 devid_ptr->page_code = SVPD_DEVICE_ID;
10038 scsi_ulto2b(data_len - 4, devid_ptr->length);
10040 if (port->port_type == CTL_PORT_FC)
10041 proto = SCSI_PROTO_FC << 4;
10042 else if (port->port_type == CTL_PORT_ISCSI)
10043 proto = SCSI_PROTO_ISCSI << 4;
10045 proto = SCSI_PROTO_SPI << 4;
10046 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
10049 * We're using a LUN association here. i.e., this device ID is a
10050 * per-LUN identifier.
10052 if (lun && lun->lun_devid) {
10053 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
10054 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10055 lun->lun_devid->len);
10059 * This is for the WWPN which is a port association.
10061 if (port->port_devid) {
10062 memcpy(desc, port->port_devid->data, port->port_devid->len);
10063 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10064 port->port_devid->len);
10068 * This is for the Relative Target Port(type 4h) identifier
10070 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10071 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10072 SVPD_ID_TYPE_RELTARG;
10074 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
10075 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10076 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
10079 * This is for the Target Port Group(type 5h) identifier
10081 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10082 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10083 SVPD_ID_TYPE_TPORTGRP;
10085 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
10086 &desc->identifier[2]);
10087 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10088 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
10091 * This is for the Target identifier
10093 if (port->target_devid) {
10094 memcpy(desc, port->target_devid->data, port->target_devid->len);
10097 ctsio->scsi_status = SCSI_STATUS_OK;
10098 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10099 ctsio->be_move_done = ctl_config_move_done;
10100 ctl_datamove((union ctl_io *)ctsio);
10102 return (CTL_RETVAL_COMPLETE);
10106 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
10108 struct ctl_softc *softc = control_softc;
10109 struct scsi_vpd_scsi_ports *sp;
10110 struct scsi_vpd_port_designation *pd;
10111 struct scsi_vpd_port_designation_cont *pdc;
10112 struct ctl_lun *lun;
10113 struct ctl_port *port;
10114 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
10115 int num_target_port_groups, single;
10117 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10119 single = ctl_is_single;
10121 num_target_port_groups = 1;
10123 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
10124 num_target_ports = 0;
10127 mtx_lock(&softc->ctl_lock);
10128 STAILQ_FOREACH(port, &softc->port_list, links) {
10129 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10132 ctl_map_lun_back(port->targ_port, lun->lun) >=
10135 num_target_ports++;
10136 if (port->init_devid)
10137 iid_len += port->init_devid->len;
10138 if (port->port_devid)
10139 id_len += port->port_devid->len;
10141 mtx_unlock(&softc->ctl_lock);
10143 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
10144 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
10145 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
10146 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10147 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
10148 ctsio->kern_sg_entries = 0;
10150 if (data_len < alloc_len) {
10151 ctsio->residual = alloc_len - data_len;
10152 ctsio->kern_data_len = data_len;
10153 ctsio->kern_total_len = data_len;
10155 ctsio->residual = 0;
10156 ctsio->kern_data_len = alloc_len;
10157 ctsio->kern_total_len = alloc_len;
10159 ctsio->kern_data_resid = 0;
10160 ctsio->kern_rel_offset = 0;
10161 ctsio->kern_sg_entries = 0;
10164 * The control device is always connected. The disk device, on the
10165 * other hand, may not be online all the time. Need to change this
10166 * to figure out whether the disk device is actually online or not.
10169 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10170 lun->be_lun->lun_type;
10172 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10174 sp->page_code = SVPD_SCSI_PORTS;
10175 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10177 pd = &sp->design[0];
10179 mtx_lock(&softc->ctl_lock);
10180 if (softc->flags & CTL_FLAG_MASTER_SHELF)
10184 for (g = 0; g < num_target_port_groups; g++) {
10185 STAILQ_FOREACH(port, &softc->port_list, links) {
10186 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10189 ctl_map_lun_back(port->targ_port, lun->lun) >=
10192 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10193 scsi_ulto2b(p, pd->relative_port_id);
10194 if (port->init_devid && g == pg) {
10195 iid_len = port->init_devid->len;
10196 memcpy(pd->initiator_transportid,
10197 port->init_devid->data, port->init_devid->len);
10200 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10201 pdc = (struct scsi_vpd_port_designation_cont *)
10202 (&pd->initiator_transportid[iid_len]);
10203 if (port->port_devid && g == pg) {
10204 id_len = port->port_devid->len;
10205 memcpy(pdc->target_port_descriptors,
10206 port->port_devid->data, port->port_devid->len);
10209 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10210 pd = (struct scsi_vpd_port_designation *)
10211 ((uint8_t *)pdc->target_port_descriptors + id_len);
10214 mtx_unlock(&softc->ctl_lock);
10216 ctsio->scsi_status = SCSI_STATUS_OK;
10217 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10218 ctsio->be_move_done = ctl_config_move_done;
10219 ctl_datamove((union ctl_io *)ctsio);
10221 return (CTL_RETVAL_COMPLETE);
10225 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10227 struct scsi_vpd_block_limits *bl_ptr;
10228 struct ctl_lun *lun;
10231 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10233 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10234 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10235 ctsio->kern_sg_entries = 0;
10237 if (sizeof(*bl_ptr) < alloc_len) {
10238 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10239 ctsio->kern_data_len = sizeof(*bl_ptr);
10240 ctsio->kern_total_len = sizeof(*bl_ptr);
10242 ctsio->residual = 0;
10243 ctsio->kern_data_len = alloc_len;
10244 ctsio->kern_total_len = alloc_len;
10246 ctsio->kern_data_resid = 0;
10247 ctsio->kern_rel_offset = 0;
10248 ctsio->kern_sg_entries = 0;
10251 * The control device is always connected. The disk device, on the
10252 * other hand, may not be online all the time. Need to change this
10253 * to figure out whether the disk device is actually online or not.
10256 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10257 lun->be_lun->lun_type;
10259 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10261 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10262 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
10263 bl_ptr->max_cmp_write_len = 0xff;
10264 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10266 bs = lun->be_lun->blocksize;
10267 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10268 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10269 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10270 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10271 if (lun->be_lun->pblockexp != 0) {
10272 scsi_ulto4b((1 << lun->be_lun->pblockexp),
10273 bl_ptr->opt_unmap_grain);
10274 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff,
10275 bl_ptr->unmap_grain_align);
10279 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10281 ctsio->scsi_status = SCSI_STATUS_OK;
10282 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10283 ctsio->be_move_done = ctl_config_move_done;
10284 ctl_datamove((union ctl_io *)ctsio);
10286 return (CTL_RETVAL_COMPLETE);
10290 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10292 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10293 struct ctl_lun *lun;
10295 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10297 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10298 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10299 ctsio->kern_sg_entries = 0;
10301 if (sizeof(*bdc_ptr) < alloc_len) {
10302 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10303 ctsio->kern_data_len = sizeof(*bdc_ptr);
10304 ctsio->kern_total_len = sizeof(*bdc_ptr);
10306 ctsio->residual = 0;
10307 ctsio->kern_data_len = alloc_len;
10308 ctsio->kern_total_len = alloc_len;
10310 ctsio->kern_data_resid = 0;
10311 ctsio->kern_rel_offset = 0;
10312 ctsio->kern_sg_entries = 0;
10315 * The control device is always connected. The disk device, on the
10316 * other hand, may not be online all the time. Need to change this
10317 * to figure out whether the disk device is actually online or not.
10320 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10321 lun->be_lun->lun_type;
10323 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10324 bdc_ptr->page_code = SVPD_BDC;
10325 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10326 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate);
10327 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10329 ctsio->scsi_status = SCSI_STATUS_OK;
10330 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10331 ctsio->be_move_done = ctl_config_move_done;
10332 ctl_datamove((union ctl_io *)ctsio);
10334 return (CTL_RETVAL_COMPLETE);
10338 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10340 struct scsi_vpd_logical_block_prov *lbp_ptr;
10341 struct ctl_lun *lun;
10343 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10345 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10346 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10347 ctsio->kern_sg_entries = 0;
10349 if (sizeof(*lbp_ptr) < alloc_len) {
10350 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10351 ctsio->kern_data_len = sizeof(*lbp_ptr);
10352 ctsio->kern_total_len = sizeof(*lbp_ptr);
10354 ctsio->residual = 0;
10355 ctsio->kern_data_len = alloc_len;
10356 ctsio->kern_total_len = alloc_len;
10358 ctsio->kern_data_resid = 0;
10359 ctsio->kern_rel_offset = 0;
10360 ctsio->kern_sg_entries = 0;
10363 * The control device is always connected. The disk device, on the
10364 * other hand, may not be online all the time. Need to change this
10365 * to figure out whether the disk device is actually online or not.
10368 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10369 lun->be_lun->lun_type;
10371 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10373 lbp_ptr->page_code = SVPD_LBP;
10374 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10375 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10376 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10377 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10378 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10381 ctsio->scsi_status = SCSI_STATUS_OK;
10382 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10383 ctsio->be_move_done = ctl_config_move_done;
10384 ctl_datamove((union ctl_io *)ctsio);
10386 return (CTL_RETVAL_COMPLETE);
10390 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10392 struct scsi_inquiry *cdb;
10393 struct ctl_lun *lun;
10394 int alloc_len, retval;
10396 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10397 cdb = (struct scsi_inquiry *)ctsio->cdb;
10399 retval = CTL_RETVAL_COMPLETE;
10401 alloc_len = scsi_2btoul(cdb->length);
10403 switch (cdb->page_code) {
10404 case SVPD_SUPPORTED_PAGES:
10405 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10407 case SVPD_UNIT_SERIAL_NUMBER:
10408 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10410 case SVPD_DEVICE_ID:
10411 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10413 case SVPD_MODE_PAGE_POLICY:
10414 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10416 case SVPD_SCSI_PORTS:
10417 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10419 case SVPD_SCSI_TPC:
10420 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10422 case SVPD_BLOCK_LIMITS:
10423 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10426 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10429 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10432 ctl_set_invalid_field(ctsio,
10438 ctl_done((union ctl_io *)ctsio);
10439 retval = CTL_RETVAL_COMPLETE;
10447 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10449 struct scsi_inquiry_data *inq_ptr;
10450 struct scsi_inquiry *cdb;
10451 struct ctl_softc *ctl_softc;
10452 struct ctl_lun *lun;
10454 uint32_t alloc_len;
10455 ctl_port_type port_type;
10457 ctl_softc = control_softc;
10460 * Figure out whether we're talking to a Fibre Channel port or not.
10461 * We treat the ioctl front end, and any SCSI adapters, as packetized
10464 port_type = ctl_softc->ctl_ports[
10465 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10466 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10467 port_type = CTL_PORT_SCSI;
10469 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10470 cdb = (struct scsi_inquiry *)ctsio->cdb;
10471 alloc_len = scsi_2btoul(cdb->length);
10474 * We malloc the full inquiry data size here and fill it
10475 * in. If the user only asks for less, we'll give him
10478 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
10479 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10480 ctsio->kern_sg_entries = 0;
10481 ctsio->kern_data_resid = 0;
10482 ctsio->kern_rel_offset = 0;
10484 if (sizeof(*inq_ptr) < alloc_len) {
10485 ctsio->residual = alloc_len - sizeof(*inq_ptr);
10486 ctsio->kern_data_len = sizeof(*inq_ptr);
10487 ctsio->kern_total_len = sizeof(*inq_ptr);
10489 ctsio->residual = 0;
10490 ctsio->kern_data_len = alloc_len;
10491 ctsio->kern_total_len = alloc_len;
10495 * If we have a LUN configured, report it as connected. Otherwise,
10496 * report that it is offline or no device is supported, depending
10497 * on the value of inquiry_pq_no_lun.
10499 * According to the spec (SPC-4 r34), the peripheral qualifier
10500 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10502 * "A peripheral device having the specified peripheral device type
10503 * is not connected to this logical unit. However, the device
10504 * server is capable of supporting the specified peripheral device
10505 * type on this logical unit."
10507 * According to the same spec, the peripheral qualifier
10508 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10510 * "The device server is not capable of supporting a peripheral
10511 * device on this logical unit. For this peripheral qualifier the
10512 * peripheral device type shall be set to 1Fh. All other peripheral
10513 * device type values are reserved for this peripheral qualifier."
10515 * Given the text, it would seem that we probably want to report that
10516 * the LUN is offline here. There is no LUN connected, but we can
10517 * support a LUN at the given LUN number.
10519 * In the real world, though, it sounds like things are a little
10522 * - Linux, when presented with a LUN with the offline peripheral
10523 * qualifier, will create an sg driver instance for it. So when
10524 * you attach it to CTL, you wind up with a ton of sg driver
10525 * instances. (One for every LUN that Linux bothered to probe.)
10526 * Linux does this despite the fact that it issues a REPORT LUNs
10527 * to LUN 0 to get the inventory of supported LUNs.
10529 * - There is other anecdotal evidence (from Emulex folks) about
10530 * arrays that use the offline peripheral qualifier for LUNs that
10531 * are on the "passive" path in an active/passive array.
10533 * So the solution is provide a hopefully reasonable default
10534 * (return bad/no LUN) and allow the user to change the behavior
10535 * with a tunable/sysctl variable.
10538 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10539 lun->be_lun->lun_type;
10540 else if (ctl_softc->inquiry_pq_no_lun == 0)
10541 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10543 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10545 /* RMB in byte 2 is 0 */
10546 inq_ptr->version = SCSI_REV_SPC4;
10549 * According to SAM-3, even if a device only supports a single
10550 * level of LUN addressing, it should still set the HISUP bit:
10552 * 4.9.1 Logical unit numbers overview
10554 * All logical unit number formats described in this standard are
10555 * hierarchical in structure even when only a single level in that
10556 * hierarchy is used. The HISUP bit shall be set to one in the
10557 * standard INQUIRY data (see SPC-2) when any logical unit number
10558 * format described in this standard is used. Non-hierarchical
10559 * formats are outside the scope of this standard.
10561 * Therefore we set the HiSup bit here.
10563 * The reponse format is 2, per SPC-3.
10565 inq_ptr->response_format = SID_HiSup | 2;
10567 inq_ptr->additional_length =
10568 offsetof(struct scsi_inquiry_data, vendor_specific1) -
10569 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10570 CTL_DEBUG_PRINT(("additional_length = %d\n",
10571 inq_ptr->additional_length));
10573 inq_ptr->spc3_flags = SPC3_SID_3PC;
10574 if (!ctl_is_single)
10575 inq_ptr->spc3_flags |= SPC3_SID_TPGS_IMPLICIT;
10576 /* 16 bit addressing */
10577 if (port_type == CTL_PORT_SCSI)
10578 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10579 /* XXX set the SID_MultiP bit here if we're actually going to
10580 respond on multiple ports */
10581 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10583 /* 16 bit data bus, synchronous transfers */
10584 if (port_type == CTL_PORT_SCSI)
10585 inq_ptr->flags = SID_WBus16 | SID_Sync;
10587 * XXX KDM do we want to support tagged queueing on the control
10591 || (lun->be_lun->lun_type != T_PROCESSOR))
10592 inq_ptr->flags |= SID_CmdQue;
10594 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10595 * We have 8 bytes for the vendor name, and 16 bytes for the device
10596 * name and 4 bytes for the revision.
10598 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10599 "vendor")) == NULL) {
10600 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10602 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10603 strncpy(inq_ptr->vendor, val,
10604 min(sizeof(inq_ptr->vendor), strlen(val)));
10607 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10608 sizeof(inq_ptr->product));
10609 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10610 switch (lun->be_lun->lun_type) {
10612 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10613 sizeof(inq_ptr->product));
10616 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10617 sizeof(inq_ptr->product));
10620 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10621 sizeof(inq_ptr->product));
10625 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10626 strncpy(inq_ptr->product, val,
10627 min(sizeof(inq_ptr->product), strlen(val)));
10631 * XXX make this a macro somewhere so it automatically gets
10632 * incremented when we make changes.
10634 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10635 "revision")) == NULL) {
10636 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10638 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10639 strncpy(inq_ptr->revision, val,
10640 min(sizeof(inq_ptr->revision), strlen(val)));
10644 * For parallel SCSI, we support double transition and single
10645 * transition clocking. We also support QAS (Quick Arbitration
10646 * and Selection) and Information Unit transfers on both the
10647 * control and array devices.
10649 if (port_type == CTL_PORT_SCSI)
10650 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10653 /* SAM-5 (no version claimed) */
10654 scsi_ulto2b(0x00A0, inq_ptr->version1);
10655 /* SPC-4 (no version claimed) */
10656 scsi_ulto2b(0x0460, inq_ptr->version2);
10657 if (port_type == CTL_PORT_FC) {
10658 /* FCP-2 ANSI INCITS.350:2003 */
10659 scsi_ulto2b(0x0917, inq_ptr->version3);
10660 } else if (port_type == CTL_PORT_SCSI) {
10661 /* SPI-4 ANSI INCITS.362:200x */
10662 scsi_ulto2b(0x0B56, inq_ptr->version3);
10663 } else if (port_type == CTL_PORT_ISCSI) {
10664 /* iSCSI (no version claimed) */
10665 scsi_ulto2b(0x0960, inq_ptr->version3);
10666 } else if (port_type == CTL_PORT_SAS) {
10667 /* SAS (no version claimed) */
10668 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10672 /* SBC-3 (no version claimed) */
10673 scsi_ulto2b(0x04C0, inq_ptr->version4);
10675 switch (lun->be_lun->lun_type) {
10677 /* SBC-3 (no version claimed) */
10678 scsi_ulto2b(0x04C0, inq_ptr->version4);
10686 ctsio->scsi_status = SCSI_STATUS_OK;
10687 if (ctsio->kern_data_len > 0) {
10688 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10689 ctsio->be_move_done = ctl_config_move_done;
10690 ctl_datamove((union ctl_io *)ctsio);
10692 ctsio->io_hdr.status = CTL_SUCCESS;
10693 ctl_done((union ctl_io *)ctsio);
10696 return (CTL_RETVAL_COMPLETE);
10700 ctl_inquiry(struct ctl_scsiio *ctsio)
10702 struct scsi_inquiry *cdb;
10705 cdb = (struct scsi_inquiry *)ctsio->cdb;
10709 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10712 * Right now, we don't support the CmdDt inquiry information.
10713 * This would be nice to support in the future. When we do
10714 * support it, we should change this test so that it checks to make
10715 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
10718 if (((cdb->byte2 & SI_EVPD)
10719 && (cdb->byte2 & SI_CMDDT)))
10721 if (cdb->byte2 & SI_CMDDT) {
10723 * Point to the SI_CMDDT bit. We might change this
10724 * when we support SI_CMDDT, but since both bits would be
10725 * "wrong", this should probably just stay as-is then.
10727 ctl_set_invalid_field(ctsio,
10733 ctl_done((union ctl_io *)ctsio);
10734 return (CTL_RETVAL_COMPLETE);
10736 if (cdb->byte2 & SI_EVPD)
10737 retval = ctl_inquiry_evpd(ctsio);
10739 else if (cdb->byte2 & SI_CMDDT)
10740 retval = ctl_inquiry_cmddt(ctsio);
10743 retval = ctl_inquiry_std(ctsio);
10749 * For known CDB types, parse the LBA and length.
10752 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint32_t *len)
10754 if (io->io_hdr.io_type != CTL_IO_SCSI)
10757 switch (io->scsiio.cdb[0]) {
10758 case COMPARE_AND_WRITE: {
10759 struct scsi_compare_and_write *cdb;
10761 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10763 *lba = scsi_8btou64(cdb->addr);
10764 *len = cdb->length;
10769 struct scsi_rw_6 *cdb;
10771 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10773 *lba = scsi_3btoul(cdb->addr);
10774 /* only 5 bits are valid in the most significant address byte */
10776 *len = cdb->length;
10781 struct scsi_rw_10 *cdb;
10783 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10785 *lba = scsi_4btoul(cdb->addr);
10786 *len = scsi_2btoul(cdb->length);
10789 case WRITE_VERIFY_10: {
10790 struct scsi_write_verify_10 *cdb;
10792 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10794 *lba = scsi_4btoul(cdb->addr);
10795 *len = scsi_2btoul(cdb->length);
10800 struct scsi_rw_12 *cdb;
10802 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10804 *lba = scsi_4btoul(cdb->addr);
10805 *len = scsi_4btoul(cdb->length);
10808 case WRITE_VERIFY_12: {
10809 struct scsi_write_verify_12 *cdb;
10811 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10813 *lba = scsi_4btoul(cdb->addr);
10814 *len = scsi_4btoul(cdb->length);
10819 struct scsi_rw_16 *cdb;
10821 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10823 *lba = scsi_8btou64(cdb->addr);
10824 *len = scsi_4btoul(cdb->length);
10827 case WRITE_VERIFY_16: {
10828 struct scsi_write_verify_16 *cdb;
10830 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10833 *lba = scsi_8btou64(cdb->addr);
10834 *len = scsi_4btoul(cdb->length);
10837 case WRITE_SAME_10: {
10838 struct scsi_write_same_10 *cdb;
10840 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10842 *lba = scsi_4btoul(cdb->addr);
10843 *len = scsi_2btoul(cdb->length);
10846 case WRITE_SAME_16: {
10847 struct scsi_write_same_16 *cdb;
10849 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10851 *lba = scsi_8btou64(cdb->addr);
10852 *len = scsi_4btoul(cdb->length);
10856 struct scsi_verify_10 *cdb;
10858 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10860 *lba = scsi_4btoul(cdb->addr);
10861 *len = scsi_2btoul(cdb->length);
10865 struct scsi_verify_12 *cdb;
10867 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10869 *lba = scsi_4btoul(cdb->addr);
10870 *len = scsi_4btoul(cdb->length);
10874 struct scsi_verify_16 *cdb;
10876 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10878 *lba = scsi_8btou64(cdb->addr);
10879 *len = scsi_4btoul(cdb->length);
10884 break; /* NOTREACHED */
10891 ctl_extent_check_lba(uint64_t lba1, uint32_t len1, uint64_t lba2, uint32_t len2)
10893 uint64_t endlba1, endlba2;
10895 endlba1 = lba1 + len1 - 1;
10896 endlba2 = lba2 + len2 - 1;
10898 if ((endlba1 < lba2)
10899 || (endlba2 < lba1))
10900 return (CTL_ACTION_PASS);
10902 return (CTL_ACTION_BLOCK);
10906 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10908 uint64_t lba1, lba2;
10909 uint32_t len1, len2;
10912 retval = ctl_get_lba_len(io1, &lba1, &len1);
10914 return (CTL_ACTION_ERROR);
10916 retval = ctl_get_lba_len(io2, &lba2, &len2);
10918 return (CTL_ACTION_ERROR);
10920 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10924 ctl_check_for_blockage(union ctl_io *pending_io, union ctl_io *ooa_io)
10926 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10927 ctl_serialize_action *serialize_row;
10930 * The initiator attempted multiple untagged commands at the same
10931 * time. Can't do that.
10933 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10934 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10935 && ((pending_io->io_hdr.nexus.targ_port ==
10936 ooa_io->io_hdr.nexus.targ_port)
10937 && (pending_io->io_hdr.nexus.initid.id ==
10938 ooa_io->io_hdr.nexus.initid.id))
10939 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10940 return (CTL_ACTION_OVERLAP);
10943 * The initiator attempted to send multiple tagged commands with
10944 * the same ID. (It's fine if different initiators have the same
10947 * Even if all of those conditions are true, we don't kill the I/O
10948 * if the command ahead of us has been aborted. We won't end up
10949 * sending it to the FETD, and it's perfectly legal to resend a
10950 * command with the same tag number as long as the previous
10951 * instance of this tag number has been aborted somehow.
10953 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10954 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10955 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10956 && ((pending_io->io_hdr.nexus.targ_port ==
10957 ooa_io->io_hdr.nexus.targ_port)
10958 && (pending_io->io_hdr.nexus.initid.id ==
10959 ooa_io->io_hdr.nexus.initid.id))
10960 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
10961 return (CTL_ACTION_OVERLAP_TAG);
10964 * If we get a head of queue tag, SAM-3 says that we should
10965 * immediately execute it.
10967 * What happens if this command would normally block for some other
10968 * reason? e.g. a request sense with a head of queue tag
10969 * immediately after a write. Normally that would block, but this
10970 * will result in its getting executed immediately...
10972 * We currently return "pass" instead of "skip", so we'll end up
10973 * going through the rest of the queue to check for overlapped tags.
10975 * XXX KDM check for other types of blockage first??
10977 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10978 return (CTL_ACTION_PASS);
10981 * Ordered tags have to block until all items ahead of them
10982 * have completed. If we get called with an ordered tag, we always
10983 * block, if something else is ahead of us in the queue.
10985 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10986 return (CTL_ACTION_BLOCK);
10989 * Simple tags get blocked until all head of queue and ordered tags
10990 * ahead of them have completed. I'm lumping untagged commands in
10991 * with simple tags here. XXX KDM is that the right thing to do?
10993 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10994 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10995 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10996 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10997 return (CTL_ACTION_BLOCK);
10999 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio);
11000 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio);
11002 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11004 switch (serialize_row[pending_entry->seridx]) {
11005 case CTL_SER_BLOCK:
11006 return (CTL_ACTION_BLOCK);
11007 break; /* NOTREACHED */
11008 case CTL_SER_EXTENT:
11009 return (ctl_extent_check(pending_io, ooa_io));
11010 break; /* NOTREACHED */
11012 return (CTL_ACTION_PASS);
11013 break; /* NOTREACHED */
11015 return (CTL_ACTION_SKIP);
11018 panic("invalid serialization value %d",
11019 serialize_row[pending_entry->seridx]);
11020 break; /* NOTREACHED */
11023 return (CTL_ACTION_ERROR);
11027 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11029 * - pending_io is generally either incoming, or on the blocked queue
11030 * - starting I/O is the I/O we want to start the check with.
11033 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11034 union ctl_io *starting_io)
11036 union ctl_io *ooa_io;
11039 mtx_assert(&lun->lun_lock, MA_OWNED);
11042 * Run back along the OOA queue, starting with the current
11043 * blocked I/O and going through every I/O before it on the
11044 * queue. If starting_io is NULL, we'll just end up returning
11047 for (ooa_io = starting_io; ooa_io != NULL;
11048 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11052 * This routine just checks to see whether
11053 * cur_blocked is blocked by ooa_io, which is ahead
11054 * of it in the queue. It doesn't queue/dequeue
11057 action = ctl_check_for_blockage(pending_io, ooa_io);
11059 case CTL_ACTION_BLOCK:
11060 case CTL_ACTION_OVERLAP:
11061 case CTL_ACTION_OVERLAP_TAG:
11062 case CTL_ACTION_SKIP:
11063 case CTL_ACTION_ERROR:
11065 break; /* NOTREACHED */
11066 case CTL_ACTION_PASS:
11069 panic("invalid action %d", action);
11070 break; /* NOTREACHED */
11074 return (CTL_ACTION_PASS);
11079 * - An I/O has just completed, and has been removed from the per-LUN OOA
11080 * queue, so some items on the blocked queue may now be unblocked.
11083 ctl_check_blocked(struct ctl_lun *lun)
11085 union ctl_io *cur_blocked, *next_blocked;
11087 mtx_assert(&lun->lun_lock, MA_OWNED);
11090 * Run forward from the head of the blocked queue, checking each
11091 * entry against the I/Os prior to it on the OOA queue to see if
11092 * there is still any blockage.
11094 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11095 * with our removing a variable on it while it is traversing the
11098 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11099 cur_blocked != NULL; cur_blocked = next_blocked) {
11100 union ctl_io *prev_ooa;
11103 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11106 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11107 ctl_ooaq, ooa_links);
11110 * If cur_blocked happens to be the first item in the OOA
11111 * queue now, prev_ooa will be NULL, and the action
11112 * returned will just be CTL_ACTION_PASS.
11114 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11117 case CTL_ACTION_BLOCK:
11118 /* Nothing to do here, still blocked */
11120 case CTL_ACTION_OVERLAP:
11121 case CTL_ACTION_OVERLAP_TAG:
11123 * This shouldn't happen! In theory we've already
11124 * checked this command for overlap...
11127 case CTL_ACTION_PASS:
11128 case CTL_ACTION_SKIP: {
11129 struct ctl_softc *softc;
11130 const struct ctl_cmd_entry *entry;
11135 * The skip case shouldn't happen, this transaction
11136 * should have never made it onto the blocked queue.
11139 * This I/O is no longer blocked, we can remove it
11140 * from the blocked queue. Since this is a TAILQ
11141 * (doubly linked list), we can do O(1) removals
11142 * from any place on the list.
11144 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11146 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11148 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
11150 * Need to send IO back to original side to
11153 union ctl_ha_msg msg_info;
11155 msg_info.hdr.original_sc =
11156 cur_blocked->io_hdr.original_sc;
11157 msg_info.hdr.serializing_sc = cur_blocked;
11158 msg_info.hdr.msg_type = CTL_MSG_R2R;
11159 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11160 &msg_info, sizeof(msg_info), 0)) >
11161 CTL_HA_STATUS_SUCCESS) {
11162 printf("CTL:Check Blocked error from "
11163 "ctl_ha_msg_send %d\n",
11168 entry = ctl_get_cmd_entry(&cur_blocked->scsiio);
11169 softc = control_softc;
11171 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
11174 * Check this I/O for LUN state changes that may
11175 * have happened while this command was blocked.
11176 * The LUN state may have been changed by a command
11177 * ahead of us in the queue, so we need to re-check
11178 * for any states that can be caused by SCSI
11181 if (ctl_scsiio_lun_check(softc, lun, entry,
11182 &cur_blocked->scsiio) == 0) {
11183 cur_blocked->io_hdr.flags |=
11184 CTL_FLAG_IS_WAS_ON_RTR;
11185 ctl_enqueue_rtr(cur_blocked);
11187 ctl_done(cur_blocked);
11192 * This probably shouldn't happen -- we shouldn't
11193 * get CTL_ACTION_ERROR, or anything else.
11199 return (CTL_RETVAL_COMPLETE);
11203 * This routine (with one exception) checks LUN flags that can be set by
11204 * commands ahead of us in the OOA queue. These flags have to be checked
11205 * when a command initially comes in, and when we pull a command off the
11206 * blocked queue and are preparing to execute it. The reason we have to
11207 * check these flags for commands on the blocked queue is that the LUN
11208 * state may have been changed by a command ahead of us while we're on the
11211 * Ordering is somewhat important with these checks, so please pay
11212 * careful attention to the placement of any new checks.
11215 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
11216 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11222 mtx_assert(&lun->lun_lock, MA_OWNED);
11225 * If this shelf is a secondary shelf controller, we have to reject
11226 * any media access commands.
11229 /* No longer needed for HA */
11230 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
11231 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
11232 ctl_set_lun_standby(ctsio);
11239 * Check for a reservation conflict. If this command isn't allowed
11240 * even on reserved LUNs, and if this initiator isn't the one who
11241 * reserved us, reject the command with a reservation conflict.
11243 if ((lun->flags & CTL_LUN_RESERVED)
11244 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11245 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
11246 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
11247 || (ctsio->io_hdr.nexus.targ_target.id !=
11248 lun->rsv_nexus.targ_target.id)) {
11249 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11250 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11256 if ( (lun->flags & CTL_LUN_PR_RESERVED)
11257 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
11260 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11262 * if we aren't registered or it's a res holder type
11263 * reservation and this isn't the res holder then set a
11265 * NOTE: Commands which might be allowed on write exclusive
11266 * type reservations are checked in the particular command
11267 * for a conflict. Read and SSU are the only ones.
11269 if (!lun->per_res[residx].registered
11270 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11271 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11272 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11279 if ((lun->flags & CTL_LUN_OFFLINE)
11280 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11281 ctl_set_lun_not_ready(ctsio);
11287 * If the LUN is stopped, see if this particular command is allowed
11288 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11290 if ((lun->flags & CTL_LUN_STOPPED)
11291 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11292 /* "Logical unit not ready, initializing cmd. required" */
11293 ctl_set_lun_stopped(ctsio);
11298 if ((lun->flags & CTL_LUN_INOPERABLE)
11299 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11300 /* "Medium format corrupted" */
11301 ctl_set_medium_format_corrupted(ctsio);
11312 ctl_failover_io(union ctl_io *io, int have_lock)
11314 ctl_set_busy(&io->scsiio);
11321 struct ctl_lun *lun;
11322 struct ctl_softc *ctl_softc;
11323 union ctl_io *next_io, *pending_io;
11328 ctl_softc = control_softc;
11330 mtx_lock(&ctl_softc->ctl_lock);
11332 * Remove any cmds from the other SC from the rtr queue. These
11333 * will obviously only be for LUNs for which we're the primary.
11334 * We can't send status or get/send data for these commands.
11335 * Since they haven't been executed yet, we can just remove them.
11336 * We'll either abort them or delete them below, depending on
11337 * which HA mode we're in.
11340 mtx_lock(&ctl_softc->queue_lock);
11341 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11342 io != NULL; io = next_io) {
11343 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11344 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11345 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11346 ctl_io_hdr, links);
11348 mtx_unlock(&ctl_softc->queue_lock);
11351 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11352 lun = ctl_softc->ctl_luns[lun_idx];
11357 * Processor LUNs are primary on both sides.
11358 * XXX will this always be true?
11360 if (lun->be_lun->lun_type == T_PROCESSOR)
11363 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11364 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11365 printf("FAILOVER: primary lun %d\n", lun_idx);
11367 * Remove all commands from the other SC. First from the
11368 * blocked queue then from the ooa queue. Once we have
11369 * removed them. Call ctl_check_blocked to see if there
11370 * is anything that can run.
11372 for (io = (union ctl_io *)TAILQ_FIRST(
11373 &lun->blocked_queue); io != NULL; io = next_io) {
11375 next_io = (union ctl_io *)TAILQ_NEXT(
11376 &io->io_hdr, blocked_links);
11378 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11379 TAILQ_REMOVE(&lun->blocked_queue,
11380 &io->io_hdr,blocked_links);
11381 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11382 TAILQ_REMOVE(&lun->ooa_queue,
11383 &io->io_hdr, ooa_links);
11389 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11390 io != NULL; io = next_io) {
11392 next_io = (union ctl_io *)TAILQ_NEXT(
11393 &io->io_hdr, ooa_links);
11395 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11397 TAILQ_REMOVE(&lun->ooa_queue,
11404 ctl_check_blocked(lun);
11405 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11406 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11408 printf("FAILOVER: primary lun %d\n", lun_idx);
11410 * Abort all commands from the other SC. We can't
11411 * send status back for them now. These should get
11412 * cleaned up when they are completed or come out
11413 * for a datamove operation.
11415 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11416 io != NULL; io = next_io) {
11417 next_io = (union ctl_io *)TAILQ_NEXT(
11418 &io->io_hdr, ooa_links);
11420 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11421 io->io_hdr.flags |= CTL_FLAG_ABORT;
11423 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11424 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11426 printf("FAILOVER: secondary lun %d\n", lun_idx);
11428 lun->flags |= CTL_LUN_PRIMARY_SC;
11431 * We send all I/O that was sent to this controller
11432 * and redirected to the other side back with
11433 * busy status, and have the initiator retry it.
11434 * Figuring out how much data has been transferred,
11435 * etc. and picking up where we left off would be
11438 * XXX KDM need to remove I/O from the blocked
11441 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11442 &lun->ooa_queue); pending_io != NULL;
11443 pending_io = next_io) {
11445 next_io = (union ctl_io *)TAILQ_NEXT(
11446 &pending_io->io_hdr, ooa_links);
11448 pending_io->io_hdr.flags &=
11449 ~CTL_FLAG_SENT_2OTHER_SC;
11451 if (pending_io->io_hdr.flags &
11452 CTL_FLAG_IO_ACTIVE) {
11453 pending_io->io_hdr.flags |=
11456 ctl_set_busy(&pending_io->scsiio);
11457 ctl_done(pending_io);
11462 * Build Unit Attention
11464 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11465 lun->pending_ua[i] |=
11466 CTL_UA_ASYM_ACC_CHANGE;
11468 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11469 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11470 printf("FAILOVER: secondary lun %d\n", lun_idx);
11472 * if the first io on the OOA is not on the RtR queue
11475 lun->flags |= CTL_LUN_PRIMARY_SC;
11477 pending_io = (union ctl_io *)TAILQ_FIRST(
11479 if (pending_io==NULL) {
11480 printf("Nothing on OOA queue\n");
11484 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11485 if ((pending_io->io_hdr.flags &
11486 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11487 pending_io->io_hdr.flags |=
11488 CTL_FLAG_IS_WAS_ON_RTR;
11489 ctl_enqueue_rtr(pending_io);
11494 printf("Tag 0x%04x is running\n",
11495 pending_io->scsiio.tag_num);
11499 next_io = (union ctl_io *)TAILQ_NEXT(
11500 &pending_io->io_hdr, ooa_links);
11501 for (pending_io=next_io; pending_io != NULL;
11502 pending_io = next_io) {
11503 pending_io->io_hdr.flags &=
11504 ~CTL_FLAG_SENT_2OTHER_SC;
11505 next_io = (union ctl_io *)TAILQ_NEXT(
11506 &pending_io->io_hdr, ooa_links);
11507 if (pending_io->io_hdr.flags &
11508 CTL_FLAG_IS_WAS_ON_RTR) {
11510 printf("Tag 0x%04x is running\n",
11511 pending_io->scsiio.tag_num);
11516 switch (ctl_check_ooa(lun, pending_io,
11517 (union ctl_io *)TAILQ_PREV(
11518 &pending_io->io_hdr, ctl_ooaq,
11521 case CTL_ACTION_BLOCK:
11522 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11523 &pending_io->io_hdr,
11525 pending_io->io_hdr.flags |=
11528 case CTL_ACTION_PASS:
11529 case CTL_ACTION_SKIP:
11530 pending_io->io_hdr.flags |=
11531 CTL_FLAG_IS_WAS_ON_RTR;
11532 ctl_enqueue_rtr(pending_io);
11534 case CTL_ACTION_OVERLAP:
11535 ctl_set_overlapped_cmd(
11536 (struct ctl_scsiio *)pending_io);
11537 ctl_done(pending_io);
11539 case CTL_ACTION_OVERLAP_TAG:
11540 ctl_set_overlapped_tag(
11541 (struct ctl_scsiio *)pending_io,
11542 pending_io->scsiio.tag_num & 0xff);
11543 ctl_done(pending_io);
11545 case CTL_ACTION_ERROR:
11547 ctl_set_internal_failure(
11548 (struct ctl_scsiio *)pending_io,
11551 ctl_done(pending_io);
11557 * Build Unit Attention
11559 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11560 lun->pending_ua[i] |=
11561 CTL_UA_ASYM_ACC_CHANGE;
11564 panic("Unhandled HA mode failover, LUN flags = %#x, "
11565 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11569 mtx_unlock(&ctl_softc->ctl_lock);
11573 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11575 struct ctl_lun *lun;
11576 const struct ctl_cmd_entry *entry;
11577 uint32_t initidx, targ_lun;
11584 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11585 if ((targ_lun < CTL_MAX_LUNS)
11586 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
11587 lun = ctl_softc->ctl_luns[targ_lun];
11589 * If the LUN is invalid, pretend that it doesn't exist.
11590 * It will go away as soon as all pending I/O has been
11593 if (lun->flags & CTL_LUN_DISABLED) {
11596 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11597 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11599 if (lun->be_lun->lun_type == T_PROCESSOR) {
11600 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11604 * Every I/O goes into the OOA queue for a
11605 * particular LUN, and stays there until completion.
11607 mtx_lock(&lun->lun_lock);
11608 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11612 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11613 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11616 /* Get command entry and return error if it is unsuppotyed. */
11617 entry = ctl_validate_command(ctsio);
11618 if (entry == NULL) {
11620 mtx_unlock(&lun->lun_lock);
11624 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11625 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11628 * Check to see whether we can send this command to LUNs that don't
11629 * exist. This should pretty much only be the case for inquiry
11630 * and request sense. Further checks, below, really require having
11631 * a LUN, so we can't really check the command anymore. Just put
11632 * it on the rtr queue.
11635 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11636 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11637 ctl_enqueue_rtr((union ctl_io *)ctsio);
11641 ctl_set_unsupported_lun(ctsio);
11642 ctl_done((union ctl_io *)ctsio);
11643 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11647 * Make sure we support this particular command on this LUN.
11648 * e.g., we don't support writes to the control LUN.
11650 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11651 mtx_unlock(&lun->lun_lock);
11652 ctl_set_invalid_opcode(ctsio);
11653 ctl_done((union ctl_io *)ctsio);
11658 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11662 * If we've got a request sense, it'll clear the contingent
11663 * allegiance condition. Otherwise, if we have a CA condition for
11664 * this initiator, clear it, because it sent down a command other
11665 * than request sense.
11667 if ((ctsio->cdb[0] != REQUEST_SENSE)
11668 && (ctl_is_set(lun->have_ca, initidx)))
11669 ctl_clear_mask(lun->have_ca, initidx);
11673 * If the command has this flag set, it handles its own unit
11674 * attention reporting, we shouldn't do anything. Otherwise we
11675 * check for any pending unit attentions, and send them back to the
11676 * initiator. We only do this when a command initially comes in,
11677 * not when we pull it off the blocked queue.
11679 * According to SAM-3, section 5.3.2, the order that things get
11680 * presented back to the host is basically unit attentions caused
11681 * by some sort of reset event, busy status, reservation conflicts
11682 * or task set full, and finally any other status.
11684 * One issue here is that some of the unit attentions we report
11685 * don't fall into the "reset" category (e.g. "reported luns data
11686 * has changed"). So reporting it here, before the reservation
11687 * check, may be technically wrong. I guess the only thing to do
11688 * would be to check for and report the reset events here, and then
11689 * check for the other unit attention types after we check for a
11690 * reservation conflict.
11692 * XXX KDM need to fix this
11694 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11695 ctl_ua_type ua_type;
11697 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
11698 scsi_sense_data_type sense_format;
11701 sense_format = (lun->flags &
11702 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11705 sense_format = SSD_TYPE_FIXED;
11707 ua_type = ctl_build_ua(&lun->pending_ua[initidx],
11708 &ctsio->sense_data, sense_format);
11709 if (ua_type != CTL_UA_NONE) {
11710 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11711 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11713 ctsio->sense_len = SSD_FULL_SIZE;
11714 mtx_unlock(&lun->lun_lock);
11715 ctl_done((union ctl_io *)ctsio);
11722 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11723 mtx_unlock(&lun->lun_lock);
11724 ctl_done((union ctl_io *)ctsio);
11729 * XXX CHD this is where we want to send IO to other side if
11730 * this LUN is secondary on this SC. We will need to make a copy
11731 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11732 * the copy we send as FROM_OTHER.
11733 * We also need to stuff the address of the original IO so we can
11734 * find it easily. Something similar will need be done on the other
11735 * side so when we are done we can find the copy.
11737 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11738 union ctl_ha_msg msg_info;
11741 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11743 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11744 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11746 printf("1. ctsio %p\n", ctsio);
11748 msg_info.hdr.serializing_sc = NULL;
11749 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11750 msg_info.scsi.tag_num = ctsio->tag_num;
11751 msg_info.scsi.tag_type = ctsio->tag_type;
11752 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11754 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11756 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11757 (void *)&msg_info, sizeof(msg_info), 0)) >
11758 CTL_HA_STATUS_SUCCESS) {
11759 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11761 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11764 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11769 * XXX KDM this I/O is off the incoming queue, but hasn't
11770 * been inserted on any other queue. We may need to come
11771 * up with a holding queue while we wait for serialization
11772 * so that we have an idea of what we're waiting for from
11775 mtx_unlock(&lun->lun_lock);
11779 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11780 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11781 ctl_ooaq, ooa_links))) {
11782 case CTL_ACTION_BLOCK:
11783 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11784 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11786 mtx_unlock(&lun->lun_lock);
11788 case CTL_ACTION_PASS:
11789 case CTL_ACTION_SKIP:
11790 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11791 mtx_unlock(&lun->lun_lock);
11792 ctl_enqueue_rtr((union ctl_io *)ctsio);
11794 case CTL_ACTION_OVERLAP:
11795 mtx_unlock(&lun->lun_lock);
11796 ctl_set_overlapped_cmd(ctsio);
11797 ctl_done((union ctl_io *)ctsio);
11799 case CTL_ACTION_OVERLAP_TAG:
11800 mtx_unlock(&lun->lun_lock);
11801 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11802 ctl_done((union ctl_io *)ctsio);
11804 case CTL_ACTION_ERROR:
11806 mtx_unlock(&lun->lun_lock);
11807 ctl_set_internal_failure(ctsio,
11809 /*retry_count*/ 0);
11810 ctl_done((union ctl_io *)ctsio);
11816 const struct ctl_cmd_entry *
11817 ctl_get_cmd_entry(struct ctl_scsiio *ctsio)
11819 const struct ctl_cmd_entry *entry;
11820 int service_action;
11822 entry = &ctl_cmd_table[ctsio->cdb[0]];
11823 if (entry->flags & CTL_CMD_FLAG_SA5) {
11824 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11825 entry = &((const struct ctl_cmd_entry *)
11826 entry->execute)[service_action];
11831 const struct ctl_cmd_entry *
11832 ctl_validate_command(struct ctl_scsiio *ctsio)
11834 const struct ctl_cmd_entry *entry;
11838 entry = ctl_get_cmd_entry(ctsio);
11839 if (entry->execute == NULL) {
11840 ctl_set_invalid_opcode(ctsio);
11841 ctl_done((union ctl_io *)ctsio);
11844 KASSERT(entry->length > 0,
11845 ("Not defined length for command 0x%02x/0x%02x",
11846 ctsio->cdb[0], ctsio->cdb[1]));
11847 for (i = 1; i < entry->length; i++) {
11848 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11851 ctl_set_invalid_field(ctsio,
11856 /*bit*/ fls(diff) - 1);
11857 ctl_done((union ctl_io *)ctsio);
11864 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11867 switch (lun_type) {
11869 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11870 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11874 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11875 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11885 ctl_scsiio(struct ctl_scsiio *ctsio)
11888 const struct ctl_cmd_entry *entry;
11890 retval = CTL_RETVAL_COMPLETE;
11892 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11894 entry = ctl_get_cmd_entry(ctsio);
11897 * If this I/O has been aborted, just send it straight to
11898 * ctl_done() without executing it.
11900 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11901 ctl_done((union ctl_io *)ctsio);
11906 * All the checks should have been handled by ctl_scsiio_precheck().
11907 * We should be clear now to just execute the I/O.
11909 retval = entry->execute(ctsio);
11916 * Since we only implement one target right now, a bus reset simply resets
11917 * our single target.
11920 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11922 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11926 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11927 ctl_ua_type ua_type)
11929 struct ctl_lun *lun;
11932 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11933 union ctl_ha_msg msg_info;
11935 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11936 msg_info.hdr.nexus = io->io_hdr.nexus;
11937 if (ua_type==CTL_UA_TARG_RESET)
11938 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11940 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11941 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11942 msg_info.hdr.original_sc = NULL;
11943 msg_info.hdr.serializing_sc = NULL;
11944 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11945 (void *)&msg_info, sizeof(msg_info), 0)) {
11950 mtx_lock(&ctl_softc->ctl_lock);
11951 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
11952 retval += ctl_lun_reset(lun, io, ua_type);
11953 mtx_unlock(&ctl_softc->ctl_lock);
11959 * The LUN should always be set. The I/O is optional, and is used to
11960 * distinguish between I/Os sent by this initiator, and by other
11961 * initiators. We set unit attention for initiators other than this one.
11962 * SAM-3 is vague on this point. It does say that a unit attention should
11963 * be established for other initiators when a LUN is reset (see section
11964 * 5.7.3), but it doesn't specifically say that the unit attention should
11965 * be established for this particular initiator when a LUN is reset. Here
11966 * is the relevant text, from SAM-3 rev 8:
11968 * 5.7.2 When a SCSI initiator port aborts its own tasks
11970 * When a SCSI initiator port causes its own task(s) to be aborted, no
11971 * notification that the task(s) have been aborted shall be returned to
11972 * the SCSI initiator port other than the completion response for the
11973 * command or task management function action that caused the task(s) to
11974 * be aborted and notification(s) associated with related effects of the
11975 * action (e.g., a reset unit attention condition).
11977 * XXX KDM for now, we're setting unit attention for all initiators.
11980 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11984 uint32_t initindex;
11988 mtx_lock(&lun->lun_lock);
11990 * Run through the OOA queue and abort each I/O.
11993 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
11995 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11996 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11997 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
12001 * This version sets unit attention for every
12004 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12005 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
12006 if (initindex == i)
12008 lun->pending_ua[i] |= ua_type;
12013 * A reset (any kind, really) clears reservations established with
12014 * RESERVE/RELEASE. It does not clear reservations established
12015 * with PERSISTENT RESERVE OUT, but we don't support that at the
12016 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
12017 * reservations made with the RESERVE/RELEASE commands, because
12018 * those commands are obsolete in SPC-3.
12020 lun->flags &= ~CTL_LUN_RESERVED;
12022 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
12024 ctl_clear_mask(lun->have_ca, i);
12026 lun->pending_ua[i] |= ua_type;
12028 mtx_unlock(&lun->lun_lock);
12034 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12040 mtx_assert(&lun->lun_lock, MA_OWNED);
12043 * Run through the OOA queue and attempt to find the given I/O.
12044 * The target port, initiator ID, tag type and tag number have to
12045 * match the values that we got from the initiator. If we have an
12046 * untagged command to abort, simply abort the first untagged command
12047 * we come to. We only allow one untagged command at a time of course.
12049 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12050 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12052 if ((targ_port == UINT32_MAX ||
12053 targ_port == xio->io_hdr.nexus.targ_port) &&
12054 (init_id == UINT32_MAX ||
12055 init_id == xio->io_hdr.nexus.initid.id)) {
12056 if (targ_port != xio->io_hdr.nexus.targ_port ||
12057 init_id != xio->io_hdr.nexus.initid.id)
12058 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12059 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12061 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12062 union ctl_ha_msg msg_info;
12064 msg_info.hdr.nexus = xio->io_hdr.nexus;
12065 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12066 msg_info.task.tag_num = xio->scsiio.tag_num;
12067 msg_info.task.tag_type = xio->scsiio.tag_type;
12068 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12069 msg_info.hdr.original_sc = NULL;
12070 msg_info.hdr.serializing_sc = NULL;
12071 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12072 (void *)&msg_info, sizeof(msg_info), 0);
12080 ctl_abort_task_set(union ctl_io *io)
12082 struct ctl_softc *softc = control_softc;
12083 struct ctl_lun *lun;
12089 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12090 mtx_lock(&softc->ctl_lock);
12091 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
12092 lun = softc->ctl_luns[targ_lun];
12094 mtx_unlock(&softc->ctl_lock);
12098 mtx_lock(&lun->lun_lock);
12099 mtx_unlock(&softc->ctl_lock);
12100 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12101 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12102 io->io_hdr.nexus.initid.id,
12103 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12104 } else { /* CTL_TASK_CLEAR_TASK_SET */
12105 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12106 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12108 mtx_unlock(&lun->lun_lock);
12113 ctl_i_t_nexus_reset(union ctl_io *io)
12115 struct ctl_softc *softc = control_softc;
12116 struct ctl_lun *lun;
12117 uint32_t initindex;
12119 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12120 mtx_lock(&softc->ctl_lock);
12121 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12122 mtx_lock(&lun->lun_lock);
12123 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12124 io->io_hdr.nexus.initid.id,
12125 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12127 ctl_clear_mask(lun->have_ca, initindex);
12129 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS;
12130 mtx_unlock(&lun->lun_lock);
12132 mtx_unlock(&softc->ctl_lock);
12137 ctl_abort_task(union ctl_io *io)
12140 struct ctl_lun *lun;
12141 struct ctl_softc *ctl_softc;
12144 char printbuf[128];
12149 ctl_softc = control_softc;
12155 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12156 mtx_lock(&ctl_softc->ctl_lock);
12157 if ((targ_lun < CTL_MAX_LUNS)
12158 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12159 lun = ctl_softc->ctl_luns[targ_lun];
12161 mtx_unlock(&ctl_softc->ctl_lock);
12166 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12167 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12170 mtx_lock(&lun->lun_lock);
12171 mtx_unlock(&ctl_softc->ctl_lock);
12173 * Run through the OOA queue and attempt to find the given I/O.
12174 * The target port, initiator ID, tag type and tag number have to
12175 * match the values that we got from the initiator. If we have an
12176 * untagged command to abort, simply abort the first untagged command
12177 * we come to. We only allow one untagged command at a time of course.
12180 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12182 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12183 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12185 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12187 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12188 lun->lun, xio->scsiio.tag_num,
12189 xio->scsiio.tag_type,
12190 (xio->io_hdr.blocked_links.tqe_prev
12191 == NULL) ? "" : " BLOCKED",
12192 (xio->io_hdr.flags &
12193 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12194 (xio->io_hdr.flags &
12195 CTL_FLAG_ABORT) ? " ABORT" : "",
12196 (xio->io_hdr.flags &
12197 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12198 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12200 printf("%s\n", sbuf_data(&sb));
12203 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12204 && (xio->io_hdr.nexus.initid.id ==
12205 io->io_hdr.nexus.initid.id)) {
12207 * If the abort says that the task is untagged, the
12208 * task in the queue must be untagged. Otherwise,
12209 * we just check to see whether the tag numbers
12210 * match. This is because the QLogic firmware
12211 * doesn't pass back the tag type in an abort
12215 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12216 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12217 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12220 * XXX KDM we've got problems with FC, because it
12221 * doesn't send down a tag type with aborts. So we
12222 * can only really go by the tag number...
12223 * This may cause problems with parallel SCSI.
12224 * Need to figure that out!!
12226 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12227 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12229 if ((io->io_hdr.flags &
12230 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12231 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12232 union ctl_ha_msg msg_info;
12234 io->io_hdr.flags |=
12235 CTL_FLAG_SENT_2OTHER_SC;
12236 msg_info.hdr.nexus = io->io_hdr.nexus;
12237 msg_info.task.task_action =
12238 CTL_TASK_ABORT_TASK;
12239 msg_info.task.tag_num =
12240 io->taskio.tag_num;
12241 msg_info.task.tag_type =
12242 io->taskio.tag_type;
12243 msg_info.hdr.msg_type =
12244 CTL_MSG_MANAGE_TASKS;
12245 msg_info.hdr.original_sc = NULL;
12246 msg_info.hdr.serializing_sc = NULL;
12248 printf("Sent Abort to other side\n");
12250 if (CTL_HA_STATUS_SUCCESS !=
12251 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12253 sizeof(msg_info), 0)) {
12257 printf("ctl_abort_task: found I/O to abort\n");
12263 mtx_unlock(&lun->lun_lock);
12267 * This isn't really an error. It's entirely possible for
12268 * the abort and command completion to cross on the wire.
12269 * This is more of an informative/diagnostic error.
12272 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12273 "%d:%d:%d:%d tag %d type %d\n",
12274 io->io_hdr.nexus.initid.id,
12275 io->io_hdr.nexus.targ_port,
12276 io->io_hdr.nexus.targ_target.id,
12277 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12278 io->taskio.tag_type);
12285 ctl_run_task(union ctl_io *io)
12287 struct ctl_softc *ctl_softc = control_softc;
12289 const char *task_desc;
12291 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12293 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12294 ("ctl_run_task: Unextected io_type %d\n",
12295 io->io_hdr.io_type));
12297 task_desc = ctl_scsi_task_string(&io->taskio);
12298 if (task_desc != NULL) {
12300 csevent_log(CSC_CTL | CSC_SHELF_SW |
12302 csevent_LogType_Trace,
12303 csevent_Severity_Information,
12304 csevent_AlertLevel_Green,
12305 csevent_FRU_Firmware,
12306 csevent_FRU_Unknown,
12307 "CTL: received task: %s",task_desc);
12311 csevent_log(CSC_CTL | CSC_SHELF_SW |
12313 csevent_LogType_Trace,
12314 csevent_Severity_Information,
12315 csevent_AlertLevel_Green,
12316 csevent_FRU_Firmware,
12317 csevent_FRU_Unknown,
12318 "CTL: received unknown task "
12320 io->taskio.task_action,
12321 io->taskio.task_action);
12324 switch (io->taskio.task_action) {
12325 case CTL_TASK_ABORT_TASK:
12326 retval = ctl_abort_task(io);
12328 case CTL_TASK_ABORT_TASK_SET:
12329 case CTL_TASK_CLEAR_TASK_SET:
12330 retval = ctl_abort_task_set(io);
12332 case CTL_TASK_CLEAR_ACA:
12334 case CTL_TASK_I_T_NEXUS_RESET:
12335 retval = ctl_i_t_nexus_reset(io);
12337 case CTL_TASK_LUN_RESET: {
12338 struct ctl_lun *lun;
12341 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12342 mtx_lock(&ctl_softc->ctl_lock);
12343 if ((targ_lun < CTL_MAX_LUNS)
12344 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12345 lun = ctl_softc->ctl_luns[targ_lun];
12347 mtx_unlock(&ctl_softc->ctl_lock);
12352 if (!(io->io_hdr.flags &
12353 CTL_FLAG_FROM_OTHER_SC)) {
12354 union ctl_ha_msg msg_info;
12356 io->io_hdr.flags |=
12357 CTL_FLAG_SENT_2OTHER_SC;
12358 msg_info.hdr.msg_type =
12359 CTL_MSG_MANAGE_TASKS;
12360 msg_info.hdr.nexus = io->io_hdr.nexus;
12361 msg_info.task.task_action =
12362 CTL_TASK_LUN_RESET;
12363 msg_info.hdr.original_sc = NULL;
12364 msg_info.hdr.serializing_sc = NULL;
12365 if (CTL_HA_STATUS_SUCCESS !=
12366 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12368 sizeof(msg_info), 0)) {
12372 retval = ctl_lun_reset(lun, io,
12374 mtx_unlock(&ctl_softc->ctl_lock);
12377 case CTL_TASK_TARGET_RESET:
12378 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12380 case CTL_TASK_BUS_RESET:
12381 retval = ctl_bus_reset(ctl_softc, io);
12383 case CTL_TASK_PORT_LOGIN:
12385 case CTL_TASK_PORT_LOGOUT:
12388 printf("ctl_run_task: got unknown task management event %d\n",
12389 io->taskio.task_action);
12393 io->io_hdr.status = CTL_SUCCESS;
12395 io->io_hdr.status = CTL_ERROR;
12400 * For HA operation. Handle commands that come in from the other
12404 ctl_handle_isc(union ctl_io *io)
12407 struct ctl_lun *lun;
12408 struct ctl_softc *ctl_softc;
12411 ctl_softc = control_softc;
12413 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12414 lun = ctl_softc->ctl_luns[targ_lun];
12416 switch (io->io_hdr.msg_type) {
12417 case CTL_MSG_SERIALIZE:
12418 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12420 case CTL_MSG_R2R: {
12421 const struct ctl_cmd_entry *entry;
12424 * This is only used in SER_ONLY mode.
12427 entry = ctl_get_cmd_entry(&io->scsiio);
12428 mtx_lock(&lun->lun_lock);
12429 if (ctl_scsiio_lun_check(ctl_softc, lun,
12430 entry, (struct ctl_scsiio *)io) != 0) {
12431 mtx_unlock(&lun->lun_lock);
12435 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12436 mtx_unlock(&lun->lun_lock);
12437 ctl_enqueue_rtr(io);
12440 case CTL_MSG_FINISH_IO:
12441 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12446 mtx_lock(&lun->lun_lock);
12447 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12449 ctl_check_blocked(lun);
12450 mtx_unlock(&lun->lun_lock);
12453 case CTL_MSG_PERS_ACTION:
12454 ctl_hndl_per_res_out_on_other_sc(
12455 (union ctl_ha_msg *)&io->presio.pr_msg);
12458 case CTL_MSG_BAD_JUJU:
12462 case CTL_MSG_DATAMOVE:
12463 /* Only used in XFER mode */
12465 ctl_datamove_remote(io);
12467 case CTL_MSG_DATAMOVE_DONE:
12468 /* Only used in XFER mode */
12470 io->scsiio.be_move_done(io);
12474 printf("%s: Invalid message type %d\n",
12475 __func__, io->io_hdr.msg_type);
12485 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12486 * there is no match.
12488 static ctl_lun_error_pattern
12489 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12491 const struct ctl_cmd_entry *entry;
12492 ctl_lun_error_pattern filtered_pattern, pattern;
12494 pattern = desc->error_pattern;
12497 * XXX KDM we need more data passed into this function to match a
12498 * custom pattern, and we actually need to implement custom pattern
12501 if (pattern & CTL_LUN_PAT_CMD)
12502 return (CTL_LUN_PAT_CMD);
12504 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12505 return (CTL_LUN_PAT_ANY);
12507 entry = ctl_get_cmd_entry(ctsio);
12509 filtered_pattern = entry->pattern & pattern;
12512 * If the user requested specific flags in the pattern (e.g.
12513 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12516 * If the user did not specify any flags, it doesn't matter whether
12517 * or not the command supports the flags.
12519 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12520 (pattern & ~CTL_LUN_PAT_MASK))
12521 return (CTL_LUN_PAT_NONE);
12524 * If the user asked for a range check, see if the requested LBA
12525 * range overlaps with this command's LBA range.
12527 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12533 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12535 return (CTL_LUN_PAT_NONE);
12537 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12538 desc->lba_range.len);
12540 * A "pass" means that the LBA ranges don't overlap, so
12541 * this doesn't match the user's range criteria.
12543 if (action == CTL_ACTION_PASS)
12544 return (CTL_LUN_PAT_NONE);
12547 return (filtered_pattern);
12551 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12553 struct ctl_error_desc *desc, *desc2;
12555 mtx_assert(&lun->lun_lock, MA_OWNED);
12557 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12558 ctl_lun_error_pattern pattern;
12560 * Check to see whether this particular command matches
12561 * the pattern in the descriptor.
12563 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12564 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12567 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12568 case CTL_LUN_INJ_ABORTED:
12569 ctl_set_aborted(&io->scsiio);
12571 case CTL_LUN_INJ_MEDIUM_ERR:
12572 ctl_set_medium_error(&io->scsiio);
12574 case CTL_LUN_INJ_UA:
12575 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12577 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12579 case CTL_LUN_INJ_CUSTOM:
12581 * We're assuming the user knows what he is doing.
12582 * Just copy the sense information without doing
12585 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12586 ctl_min(sizeof(desc->custom_sense),
12587 sizeof(io->scsiio.sense_data)));
12588 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12589 io->scsiio.sense_len = SSD_FULL_SIZE;
12590 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12592 case CTL_LUN_INJ_NONE:
12595 * If this is an error injection type we don't know
12596 * about, clear the continuous flag (if it is set)
12597 * so it will get deleted below.
12599 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12603 * By default, each error injection action is a one-shot
12605 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12608 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12614 #ifdef CTL_IO_DELAY
12616 ctl_datamove_timer_wakeup(void *arg)
12620 io = (union ctl_io *)arg;
12624 #endif /* CTL_IO_DELAY */
12627 ctl_datamove(union ctl_io *io)
12629 void (*fe_datamove)(union ctl_io *io);
12631 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12633 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12636 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12641 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12642 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12644 sbuf_cat(&sb, path_str);
12645 switch (io->io_hdr.io_type) {
12647 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12648 sbuf_printf(&sb, "\n");
12649 sbuf_cat(&sb, path_str);
12650 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12651 io->scsiio.tag_num, io->scsiio.tag_type);
12654 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12655 "Tag Type: %d\n", io->taskio.task_action,
12656 io->taskio.tag_num, io->taskio.tag_type);
12659 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12660 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12663 sbuf_cat(&sb, path_str);
12664 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12665 (intmax_t)time_uptime - io->io_hdr.start_time);
12667 printf("%s", sbuf_data(&sb));
12669 #endif /* CTL_TIME_IO */
12671 #ifdef CTL_IO_DELAY
12672 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12673 struct ctl_lun *lun;
12675 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12677 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12679 struct ctl_lun *lun;
12681 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12683 && (lun->delay_info.datamove_delay > 0)) {
12684 struct callout *callout;
12686 callout = (struct callout *)&io->io_hdr.timer_bytes;
12687 callout_init(callout, /*mpsafe*/ 1);
12688 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12689 callout_reset(callout,
12690 lun->delay_info.datamove_delay * hz,
12691 ctl_datamove_timer_wakeup, io);
12692 if (lun->delay_info.datamove_type ==
12693 CTL_DELAY_TYPE_ONESHOT)
12694 lun->delay_info.datamove_delay = 0;
12701 * This command has been aborted. Set the port status, so we fail
12704 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12705 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12706 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12707 io->io_hdr.nexus.targ_port,
12708 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12709 io->io_hdr.nexus.targ_lun);
12710 io->io_hdr.port_status = 31337;
12712 * Note that the backend, in this case, will get the
12713 * callback in its context. In other cases it may get
12714 * called in the frontend's interrupt thread context.
12716 io->scsiio.be_move_done(io);
12721 * If we're in XFER mode and this I/O is from the other shelf
12722 * controller, we need to send the DMA to the other side to
12723 * actually transfer the data to/from the host. In serialize only
12724 * mode the transfer happens below CTL and ctl_datamove() is only
12725 * called on the machine that originally received the I/O.
12727 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12728 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12729 union ctl_ha_msg msg;
12730 uint32_t sg_entries_sent;
12734 memset(&msg, 0, sizeof(msg));
12735 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12736 msg.hdr.original_sc = io->io_hdr.original_sc;
12737 msg.hdr.serializing_sc = io;
12738 msg.hdr.nexus = io->io_hdr.nexus;
12739 msg.dt.flags = io->io_hdr.flags;
12741 * We convert everything into a S/G list here. We can't
12742 * pass by reference, only by value between controllers.
12743 * So we can't pass a pointer to the S/G list, only as many
12744 * S/G entries as we can fit in here. If it's possible for
12745 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12746 * then we need to break this up into multiple transfers.
12748 if (io->scsiio.kern_sg_entries == 0) {
12749 msg.dt.kern_sg_entries = 1;
12751 * If this is in cached memory, flush the cache
12752 * before we send the DMA request to the other
12753 * controller. We want to do this in either the
12754 * read or the write case. The read case is
12755 * straightforward. In the write case, we want to
12756 * make sure nothing is in the local cache that
12757 * could overwrite the DMAed data.
12759 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12761 * XXX KDM use bus_dmamap_sync() here.
12766 * Convert to a physical address if this is a
12769 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12770 msg.dt.sg_list[0].addr =
12771 io->scsiio.kern_data_ptr;
12774 * XXX KDM use busdma here!
12777 msg.dt.sg_list[0].addr = (void *)
12778 vtophys(io->scsiio.kern_data_ptr);
12782 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12785 struct ctl_sg_entry *sgl;
12788 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12789 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12790 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12792 * XXX KDM use bus_dmamap_sync() here.
12797 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12798 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12799 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12800 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12801 msg.dt.sg_sequence = 0;
12804 * Loop until we've sent all of the S/G entries. On the
12805 * other end, we'll recompose these S/G entries into one
12806 * contiguous list before passing it to the
12808 for (sg_entries_sent = 0; sg_entries_sent <
12809 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12810 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12811 sizeof(msg.dt.sg_list[0])),
12812 msg.dt.kern_sg_entries - sg_entries_sent);
12814 if (do_sg_copy != 0) {
12815 struct ctl_sg_entry *sgl;
12818 sgl = (struct ctl_sg_entry *)
12819 io->scsiio.kern_data_ptr;
12821 * If this is in cached memory, flush the cache
12822 * before we send the DMA request to the other
12823 * controller. We want to do this in either
12824 * the * read or the write case. The read
12825 * case is straightforward. In the write
12826 * case, we want to make sure nothing is
12827 * in the local cache that could overwrite
12831 for (i = sg_entries_sent, j = 0;
12832 i < msg.dt.cur_sg_entries; i++, j++) {
12833 if ((io->io_hdr.flags &
12834 CTL_FLAG_NO_DATASYNC) == 0) {
12836 * XXX KDM use bus_dmamap_sync()
12839 if ((io->io_hdr.flags &
12840 CTL_FLAG_BUS_ADDR) == 0) {
12842 * XXX KDM use busdma.
12845 msg.dt.sg_list[j].addr =(void *)
12846 vtophys(sgl[i].addr);
12849 msg.dt.sg_list[j].addr =
12852 msg.dt.sg_list[j].len = sgl[i].len;
12856 sg_entries_sent += msg.dt.cur_sg_entries;
12857 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12858 msg.dt.sg_last = 1;
12860 msg.dt.sg_last = 0;
12863 * XXX KDM drop and reacquire the lock here?
12865 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12866 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12868 * XXX do something here.
12872 msg.dt.sent_sg_entries = sg_entries_sent;
12874 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12875 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12876 ctl_failover_io(io, /*have_lock*/ 0);
12881 * Lookup the fe_datamove() function for this particular
12885 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12892 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12894 union ctl_ha_msg msg;
12897 memset(&msg, 0, sizeof(msg));
12899 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12900 msg.hdr.original_sc = io;
12901 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12902 msg.hdr.nexus = io->io_hdr.nexus;
12903 msg.hdr.status = io->io_hdr.status;
12904 msg.scsi.tag_num = io->scsiio.tag_num;
12905 msg.scsi.tag_type = io->scsiio.tag_type;
12906 msg.scsi.scsi_status = io->scsiio.scsi_status;
12907 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12908 sizeof(io->scsiio.sense_data));
12909 msg.scsi.sense_len = io->scsiio.sense_len;
12910 msg.scsi.sense_residual = io->scsiio.sense_residual;
12911 msg.scsi.fetd_status = io->io_hdr.port_status;
12912 msg.scsi.residual = io->scsiio.residual;
12913 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12915 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12916 ctl_failover_io(io, /*have_lock*/ have_lock);
12920 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12921 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12922 /* XXX do something if this fails */
12928 * The DMA to the remote side is done, now we need to tell the other side
12929 * we're done so it can continue with its data movement.
12932 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12938 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12939 printf("%s: ISC DMA write failed with error %d", __func__,
12941 ctl_set_internal_failure(&io->scsiio,
12943 /*retry_count*/ rq->ret);
12946 ctl_dt_req_free(rq);
12949 * In this case, we had to malloc the memory locally. Free it.
12951 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
12953 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12954 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12957 * The data is in local and remote memory, so now we need to send
12958 * status (good or back) back to the other side.
12960 ctl_send_datamove_done(io, /*have_lock*/ 0);
12964 * We've moved the data from the host/controller into local memory. Now we
12965 * need to push it over to the remote controller's memory.
12968 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12974 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12975 ctl_datamove_remote_write_cb);
12981 ctl_datamove_remote_write(union ctl_io *io)
12984 void (*fe_datamove)(union ctl_io *io);
12987 * - Get the data from the host/HBA into local memory.
12988 * - DMA memory from the local controller to the remote controller.
12989 * - Send status back to the remote controller.
12992 retval = ctl_datamove_remote_sgl_setup(io);
12996 /* Switch the pointer over so the FETD knows what to do */
12997 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13000 * Use a custom move done callback, since we need to send completion
13001 * back to the other controller, not to the backend on this side.
13003 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
13005 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13014 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
13023 * In this case, we had to malloc the memory locally. Free it.
13025 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
13027 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13028 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13032 scsi_path_string(io, path_str, sizeof(path_str));
13033 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13034 sbuf_cat(&sb, path_str);
13035 scsi_command_string(&io->scsiio, NULL, &sb);
13036 sbuf_printf(&sb, "\n");
13037 sbuf_cat(&sb, path_str);
13038 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13039 io->scsiio.tag_num, io->scsiio.tag_type);
13040 sbuf_cat(&sb, path_str);
13041 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
13042 io->io_hdr.flags, io->io_hdr.status);
13044 printk("%s", sbuf_data(&sb));
13049 * The read is done, now we need to send status (good or bad) back
13050 * to the other side.
13052 ctl_send_datamove_done(io, /*have_lock*/ 0);
13058 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
13061 void (*fe_datamove)(union ctl_io *io);
13065 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13066 printf("%s: ISC DMA read failed with error %d", __func__,
13068 ctl_set_internal_failure(&io->scsiio,
13070 /*retry_count*/ rq->ret);
13073 ctl_dt_req_free(rq);
13075 /* Switch the pointer over so the FETD knows what to do */
13076 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13079 * Use a custom move done callback, since we need to send completion
13080 * back to the other controller, not to the backend on this side.
13082 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
13084 /* XXX KDM add checks like the ones in ctl_datamove? */
13086 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13092 ctl_datamove_remote_sgl_setup(union ctl_io *io)
13094 struct ctl_sg_entry *local_sglist, *remote_sglist;
13095 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
13096 struct ctl_softc *softc;
13101 softc = control_softc;
13103 local_sglist = io->io_hdr.local_sglist;
13104 local_dma_sglist = io->io_hdr.local_dma_sglist;
13105 remote_sglist = io->io_hdr.remote_sglist;
13106 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13108 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
13109 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
13110 local_sglist[i].len = remote_sglist[i].len;
13113 * XXX Detect the situation where the RS-level I/O
13114 * redirector on the other side has already read the
13115 * data off of the AOR RS on this side, and
13116 * transferred it to remote (mirror) memory on the
13117 * other side. Since we already have the data in
13118 * memory here, we just need to use it.
13120 * XXX KDM this can probably be removed once we
13121 * get the cache device code in and take the
13122 * current AOR implementation out.
13125 if ((remote_sglist[i].addr >=
13126 (void *)vtophys(softc->mirr->addr))
13127 && (remote_sglist[i].addr <
13128 ((void *)vtophys(softc->mirr->addr) +
13129 CacheMirrorOffset))) {
13130 local_sglist[i].addr = remote_sglist[i].addr -
13132 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13134 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13136 local_sglist[i].addr = remote_sglist[i].addr +
13141 printf("%s: local %p, remote %p, len %d\n",
13142 __func__, local_sglist[i].addr,
13143 remote_sglist[i].addr, local_sglist[i].len);
13147 uint32_t len_to_go;
13150 * In this case, we don't have automatically allocated
13151 * memory for this I/O on this controller. This typically
13152 * happens with internal CTL I/O -- e.g. inquiry, mode
13153 * sense, etc. Anything coming from RAIDCore will have
13154 * a mirror area available.
13156 len_to_go = io->scsiio.kern_data_len;
13159 * Clear the no datasync flag, we have to use malloced
13162 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13165 * The difficult thing here is that the size of the various
13166 * S/G segments may be different than the size from the
13167 * remote controller. That'll make it harder when DMAing
13168 * the data back to the other side.
13170 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13171 sizeof(io->io_hdr.remote_sglist[0])) &&
13172 (len_to_go > 0); i++) {
13173 local_sglist[i].len = ctl_min(len_to_go, 131072);
13174 CTL_SIZE_8B(local_dma_sglist[i].len,
13175 local_sglist[i].len);
13176 local_sglist[i].addr =
13177 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13179 local_dma_sglist[i].addr = local_sglist[i].addr;
13181 if (local_sglist[i].addr == NULL) {
13184 printf("malloc failed for %zd bytes!",
13185 local_dma_sglist[i].len);
13186 for (j = 0; j < i; j++) {
13187 free(local_sglist[j].addr, M_CTL);
13189 ctl_set_internal_failure(&io->scsiio,
13191 /*retry_count*/ 4857);
13193 goto bailout_error;
13196 /* XXX KDM do we need a sync here? */
13198 len_to_go -= local_sglist[i].len;
13201 * Reset the number of S/G entries accordingly. The
13202 * original number of S/G entries is available in
13205 io->scsiio.kern_sg_entries = i;
13208 printf("%s: kern_sg_entries = %d\n", __func__,
13209 io->scsiio.kern_sg_entries);
13210 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13211 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13212 local_sglist[i].addr, local_sglist[i].len,
13213 local_dma_sglist[i].len);
13222 ctl_send_datamove_done(io, /*have_lock*/ 0);
13228 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13229 ctl_ha_dt_cb callback)
13231 struct ctl_ha_dt_req *rq;
13232 struct ctl_sg_entry *remote_sglist, *local_sglist;
13233 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13234 uint32_t local_used, remote_used, total_used;
13240 rq = ctl_dt_req_alloc();
13243 * If we failed to allocate the request, and if the DMA didn't fail
13244 * anyway, set busy status. This is just a resource allocation
13248 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13249 ctl_set_busy(&io->scsiio);
13251 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13254 ctl_dt_req_free(rq);
13257 * The data move failed. We need to return status back
13258 * to the other controller. No point in trying to DMA
13259 * data to the remote controller.
13262 ctl_send_datamove_done(io, /*have_lock*/ 0);
13269 local_sglist = io->io_hdr.local_sglist;
13270 local_dma_sglist = io->io_hdr.local_dma_sglist;
13271 remote_sglist = io->io_hdr.remote_sglist;
13272 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13277 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13278 rq->ret = CTL_HA_STATUS_SUCCESS;
13285 * Pull/push the data over the wire from/to the other controller.
13286 * This takes into account the possibility that the local and
13287 * remote sglists may not be identical in terms of the size of
13288 * the elements and the number of elements.
13290 * One fundamental assumption here is that the length allocated for
13291 * both the local and remote sglists is identical. Otherwise, we've
13292 * essentially got a coding error of some sort.
13294 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13296 uint32_t cur_len, dma_length;
13299 rq->id = CTL_HA_DATA_CTL;
13300 rq->command = command;
13304 * Both pointers should be aligned. But it is possible
13305 * that the allocation length is not. They should both
13306 * also have enough slack left over at the end, though,
13307 * to round up to the next 8 byte boundary.
13309 cur_len = ctl_min(local_sglist[i].len - local_used,
13310 remote_sglist[j].len - remote_used);
13313 * In this case, we have a size issue and need to decrease
13314 * the size, except in the case where we actually have less
13315 * than 8 bytes left. In that case, we need to increase
13316 * the DMA length to get the last bit.
13318 if ((cur_len & 0x7) != 0) {
13319 if (cur_len > 0x7) {
13320 cur_len = cur_len - (cur_len & 0x7);
13321 dma_length = cur_len;
13323 CTL_SIZE_8B(dma_length, cur_len);
13327 dma_length = cur_len;
13330 * If we had to allocate memory for this I/O, instead of using
13331 * the non-cached mirror memory, we'll need to flush the cache
13332 * before trying to DMA to the other controller.
13334 * We could end up doing this multiple times for the same
13335 * segment if we have a larger local segment than remote
13336 * segment. That shouldn't be an issue.
13338 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13340 * XXX KDM use bus_dmamap_sync() here.
13344 rq->size = dma_length;
13346 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13347 tmp_ptr += local_used;
13349 /* Use physical addresses when talking to ISC hardware */
13350 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13351 /* XXX KDM use busdma */
13353 rq->local = vtophys(tmp_ptr);
13356 rq->local = tmp_ptr;
13358 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13359 tmp_ptr += remote_used;
13360 rq->remote = tmp_ptr;
13362 rq->callback = NULL;
13364 local_used += cur_len;
13365 if (local_used >= local_sglist[i].len) {
13370 remote_used += cur_len;
13371 if (remote_used >= remote_sglist[j].len) {
13375 total_used += cur_len;
13377 if (total_used >= io->scsiio.kern_data_len)
13378 rq->callback = callback;
13380 if ((rq->size & 0x7) != 0) {
13381 printf("%s: warning: size %d is not on 8b boundary\n",
13382 __func__, rq->size);
13384 if (((uintptr_t)rq->local & 0x7) != 0) {
13385 printf("%s: warning: local %p not on 8b boundary\n",
13386 __func__, rq->local);
13388 if (((uintptr_t)rq->remote & 0x7) != 0) {
13389 printf("%s: warning: remote %p not on 8b boundary\n",
13390 __func__, rq->local);
13393 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13394 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13395 rq->local, rq->remote, rq->size);
13398 isc_ret = ctl_dt_single(rq);
13399 if (isc_ret == CTL_HA_STATUS_WAIT)
13402 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13403 rq->ret = CTL_HA_STATUS_SUCCESS;
13417 ctl_datamove_remote_read(union ctl_io *io)
13423 * This will send an error to the other controller in the case of a
13426 retval = ctl_datamove_remote_sgl_setup(io);
13430 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13431 ctl_datamove_remote_read_cb);
13433 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13435 * Make sure we free memory if there was an error.. The
13436 * ctl_datamove_remote_xfer() function will send the
13437 * datamove done message, or call the callback with an
13438 * error if there is a problem.
13440 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13441 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13448 * Process a datamove request from the other controller. This is used for
13449 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13450 * first. Once that is complete, the data gets DMAed into the remote
13451 * controller's memory. For reads, we DMA from the remote controller's
13452 * memory into our memory first, and then move it out to the FETD.
13455 ctl_datamove_remote(union ctl_io *io)
13457 struct ctl_softc *softc;
13459 softc = control_softc;
13461 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13464 * Note that we look for an aborted I/O here, but don't do some of
13465 * the other checks that ctl_datamove() normally does.
13466 * We don't need to run the datamove delay code, since that should
13467 * have been done if need be on the other controller.
13469 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13470 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13471 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13472 io->io_hdr.nexus.targ_port,
13473 io->io_hdr.nexus.targ_target.id,
13474 io->io_hdr.nexus.targ_lun);
13475 io->io_hdr.port_status = 31338;
13476 ctl_send_datamove_done(io, /*have_lock*/ 0);
13480 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13481 ctl_datamove_remote_write(io);
13482 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13483 ctl_datamove_remote_read(io);
13485 union ctl_ha_msg msg;
13486 struct scsi_sense_data *sense;
13490 memset(&msg, 0, sizeof(msg));
13492 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13493 msg.hdr.status = CTL_SCSI_ERROR;
13494 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13496 retry_count = 4243;
13498 sense = &msg.scsi.sense_data;
13499 sks[0] = SSD_SCS_VALID;
13500 sks[1] = (retry_count >> 8) & 0xff;
13501 sks[2] = retry_count & 0xff;
13503 /* "Internal target failure" */
13504 scsi_set_sense_data(sense,
13505 /*sense_format*/ SSD_TYPE_NONE,
13506 /*current_error*/ 1,
13507 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13510 /*type*/ SSD_ELEM_SKS,
13511 /*size*/ sizeof(sks),
13515 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13516 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13517 ctl_failover_io(io, /*have_lock*/ 1);
13521 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13522 CTL_HA_STATUS_SUCCESS) {
13523 /* XXX KDM what to do if this fails? */
13531 ctl_process_done(union ctl_io *io)
13533 struct ctl_lun *lun;
13534 struct ctl_softc *ctl_softc;
13535 void (*fe_done)(union ctl_io *io);
13536 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13538 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13541 control_softc->ctl_ports[targ_port]->fe_done;
13544 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13549 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13550 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13552 sbuf_cat(&sb, path_str);
13553 switch (io->io_hdr.io_type) {
13555 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13556 sbuf_printf(&sb, "\n");
13557 sbuf_cat(&sb, path_str);
13558 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13559 io->scsiio.tag_num, io->scsiio.tag_type);
13562 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13563 "Tag Type: %d\n", io->taskio.task_action,
13564 io->taskio.tag_num, io->taskio.tag_type);
13567 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13568 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13571 sbuf_cat(&sb, path_str);
13572 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13573 (intmax_t)time_uptime - io->io_hdr.start_time);
13575 printf("%s", sbuf_data(&sb));
13577 #endif /* CTL_TIME_IO */
13579 switch (io->io_hdr.io_type) {
13583 if (bootverbose || verbose > 0)
13584 ctl_io_error_print(io, NULL);
13585 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13589 return (CTL_RETVAL_COMPLETE);
13592 printf("ctl_process_done: invalid io type %d\n",
13593 io->io_hdr.io_type);
13594 panic("ctl_process_done: invalid io type %d\n",
13595 io->io_hdr.io_type);
13596 break; /* NOTREACHED */
13599 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13601 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13602 io->io_hdr.nexus.targ_mapped_lun));
13606 ctl_softc = lun->ctl_softc;
13608 mtx_lock(&lun->lun_lock);
13611 * Check to see if we have any errors to inject here. We only
13612 * inject errors for commands that don't already have errors set.
13614 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13615 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13616 ctl_inject_error(lun, io);
13619 * XXX KDM how do we treat commands that aren't completed
13622 * XXX KDM should we also track I/O latency?
13624 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13625 io->io_hdr.io_type == CTL_IO_SCSI) {
13627 struct bintime cur_bt;
13631 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13633 type = CTL_STATS_READ;
13634 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13636 type = CTL_STATS_WRITE;
13638 type = CTL_STATS_NO_IO;
13640 lun->stats.ports[targ_port].bytes[type] +=
13641 io->scsiio.kern_total_len;
13642 lun->stats.ports[targ_port].operations[type]++;
13644 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13645 &io->io_hdr.dma_bt);
13646 lun->stats.ports[targ_port].num_dmas[type] +=
13647 io->io_hdr.num_dmas;
13648 getbintime(&cur_bt);
13649 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13650 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13655 * Remove this from the OOA queue.
13657 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13660 * Run through the blocked queue on this LUN and see if anything
13661 * has become unblocked, now that this transaction is done.
13663 ctl_check_blocked(lun);
13666 * If the LUN has been invalidated, free it if there is nothing
13667 * left on its OOA queue.
13669 if ((lun->flags & CTL_LUN_INVALID)
13670 && TAILQ_EMPTY(&lun->ooa_queue)) {
13671 mtx_unlock(&lun->lun_lock);
13672 mtx_lock(&ctl_softc->ctl_lock);
13674 mtx_unlock(&ctl_softc->ctl_lock);
13676 mtx_unlock(&lun->lun_lock);
13679 * If this command has been aborted, make sure we set the status
13680 * properly. The FETD is responsible for freeing the I/O and doing
13681 * whatever it needs to do to clean up its state.
13683 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13684 ctl_set_task_aborted(&io->scsiio);
13687 * We print out status for every task management command. For SCSI
13688 * commands, we filter out any unit attention errors; they happen
13689 * on every boot, and would clutter up the log. Note: task
13690 * management commands aren't printed here, they are printed above,
13691 * since they should never even make it down here.
13693 switch (io->io_hdr.io_type) {
13694 case CTL_IO_SCSI: {
13695 int error_code, sense_key, asc, ascq;
13699 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
13700 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13702 * Since this is just for printing, no need to
13703 * show errors here.
13705 scsi_extract_sense_len(&io->scsiio.sense_data,
13706 io->scsiio.sense_len,
13711 /*show_errors*/ 0);
13714 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
13715 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
13716 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
13717 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
13719 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
13720 ctl_softc->skipped_prints++;
13722 uint32_t skipped_prints;
13724 skipped_prints = ctl_softc->skipped_prints;
13726 ctl_softc->skipped_prints = 0;
13727 ctl_softc->last_print_jiffies = time_uptime;
13729 if (skipped_prints > 0) {
13731 csevent_log(CSC_CTL | CSC_SHELF_SW |
13733 csevent_LogType_Trace,
13734 csevent_Severity_Information,
13735 csevent_AlertLevel_Green,
13736 csevent_FRU_Firmware,
13737 csevent_FRU_Unknown,
13738 "High CTL error volume, %d prints "
13739 "skipped", skipped_prints);
13742 if (bootverbose || verbose > 0)
13743 ctl_io_error_print(io, NULL);
13749 if (bootverbose || verbose > 0)
13750 ctl_io_error_print(io, NULL);
13757 * Tell the FETD or the other shelf controller we're done with this
13758 * command. Note that only SCSI commands get to this point. Task
13759 * management commands are completed above.
13761 * We only send status to the other controller if we're in XFER
13762 * mode. In SER_ONLY mode, the I/O is done on the controller that
13763 * received the I/O (from CTL's perspective), and so the status is
13766 * XXX KDM if we hold the lock here, we could cause a deadlock
13767 * if the frontend comes back in in this context to queue
13770 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13771 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13772 union ctl_ha_msg msg;
13774 memset(&msg, 0, sizeof(msg));
13775 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13776 msg.hdr.original_sc = io->io_hdr.original_sc;
13777 msg.hdr.nexus = io->io_hdr.nexus;
13778 msg.hdr.status = io->io_hdr.status;
13779 msg.scsi.scsi_status = io->scsiio.scsi_status;
13780 msg.scsi.tag_num = io->scsiio.tag_num;
13781 msg.scsi.tag_type = io->scsiio.tag_type;
13782 msg.scsi.sense_len = io->scsiio.sense_len;
13783 msg.scsi.sense_residual = io->scsiio.sense_residual;
13784 msg.scsi.residual = io->scsiio.residual;
13785 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13786 sizeof(io->scsiio.sense_data));
13788 * We copy this whether or not this is an I/O-related
13789 * command. Otherwise, we'd have to go and check to see
13790 * whether it's a read/write command, and it really isn't
13793 memcpy(&msg.scsi.lbalen,
13794 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13795 sizeof(msg.scsi.lbalen));
13797 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13798 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13799 /* XXX do something here */
13808 return (CTL_RETVAL_COMPLETE);
13813 * Front end should call this if it doesn't do autosense. When the request
13814 * sense comes back in from the initiator, we'll dequeue this and send it.
13817 ctl_queue_sense(union ctl_io *io)
13819 struct ctl_lun *lun;
13820 struct ctl_softc *ctl_softc;
13821 uint32_t initidx, targ_lun;
13823 ctl_softc = control_softc;
13825 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13828 * LUN lookup will likely move to the ctl_work_thread() once we
13829 * have our new queueing infrastructure (that doesn't put things on
13830 * a per-LUN queue initially). That is so that we can handle
13831 * things like an INQUIRY to a LUN that we don't have enabled. We
13832 * can't deal with that right now.
13834 mtx_lock(&ctl_softc->ctl_lock);
13837 * If we don't have a LUN for this, just toss the sense
13840 targ_lun = io->io_hdr.nexus.targ_lun;
13841 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13842 if ((targ_lun < CTL_MAX_LUNS)
13843 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13844 lun = ctl_softc->ctl_luns[targ_lun];
13848 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13850 mtx_lock(&lun->lun_lock);
13852 * Already have CA set for this LUN...toss the sense information.
13854 if (ctl_is_set(lun->have_ca, initidx)) {
13855 mtx_unlock(&lun->lun_lock);
13859 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13860 ctl_min(sizeof(lun->pending_sense[initidx]),
13861 sizeof(io->scsiio.sense_data)));
13862 ctl_set_mask(lun->have_ca, initidx);
13863 mtx_unlock(&lun->lun_lock);
13866 mtx_unlock(&ctl_softc->ctl_lock);
13870 return (CTL_RETVAL_COMPLETE);
13875 * Primary command inlet from frontend ports. All SCSI and task I/O
13876 * requests must go through this function.
13879 ctl_queue(union ctl_io *io)
13881 struct ctl_softc *ctl_softc;
13883 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13885 ctl_softc = control_softc;
13888 io->io_hdr.start_time = time_uptime;
13889 getbintime(&io->io_hdr.start_bt);
13890 #endif /* CTL_TIME_IO */
13892 /* Map FE-specific LUN ID into global one. */
13893 io->io_hdr.nexus.targ_mapped_lun =
13894 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
13896 switch (io->io_hdr.io_type) {
13899 ctl_enqueue_incoming(io);
13902 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13906 return (CTL_RETVAL_COMPLETE);
13909 #ifdef CTL_IO_DELAY
13911 ctl_done_timer_wakeup(void *arg)
13915 io = (union ctl_io *)arg;
13918 #endif /* CTL_IO_DELAY */
13921 ctl_done(union ctl_io *io)
13923 struct ctl_softc *ctl_softc;
13925 ctl_softc = control_softc;
13928 * Enable this to catch duplicate completion issues.
13931 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13932 printf("%s: type %d msg %d cdb %x iptl: "
13933 "%d:%d:%d:%d tag 0x%04x "
13934 "flag %#x status %x\n",
13936 io->io_hdr.io_type,
13937 io->io_hdr.msg_type,
13939 io->io_hdr.nexus.initid.id,
13940 io->io_hdr.nexus.targ_port,
13941 io->io_hdr.nexus.targ_target.id,
13942 io->io_hdr.nexus.targ_lun,
13943 (io->io_hdr.io_type ==
13945 io->taskio.tag_num :
13946 io->scsiio.tag_num,
13948 io->io_hdr.status);
13950 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13954 * This is an internal copy of an I/O, and should not go through
13955 * the normal done processing logic.
13957 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13961 * We need to send a msg to the serializing shelf to finish the IO
13962 * as well. We don't send a finish message to the other shelf if
13963 * this is a task management command. Task management commands
13964 * aren't serialized in the OOA queue, but rather just executed on
13965 * both shelf controllers for commands that originated on that
13968 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
13969 && (io->io_hdr.io_type != CTL_IO_TASK)) {
13970 union ctl_ha_msg msg_io;
13972 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
13973 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
13974 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
13975 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
13977 /* continue on to finish IO */
13979 #ifdef CTL_IO_DELAY
13980 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13981 struct ctl_lun *lun;
13983 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13985 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13987 struct ctl_lun *lun;
13989 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13992 && (lun->delay_info.done_delay > 0)) {
13993 struct callout *callout;
13995 callout = (struct callout *)&io->io_hdr.timer_bytes;
13996 callout_init(callout, /*mpsafe*/ 1);
13997 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13998 callout_reset(callout,
13999 lun->delay_info.done_delay * hz,
14000 ctl_done_timer_wakeup, io);
14001 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
14002 lun->delay_info.done_delay = 0;
14006 #endif /* CTL_IO_DELAY */
14008 ctl_enqueue_done(io);
14012 ctl_isc(struct ctl_scsiio *ctsio)
14014 struct ctl_lun *lun;
14017 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14019 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
14021 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
14023 retval = lun->backend->data_submit((union ctl_io *)ctsio);
14030 ctl_work_thread(void *arg)
14032 struct ctl_thread *thr = (struct ctl_thread *)arg;
14033 struct ctl_softc *softc = thr->ctl_softc;
14037 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
14043 * We handle the queues in this order:
14045 * - done queue (to free up resources, unblock other commands)
14049 * If those queues are empty, we break out of the loop and
14052 mtx_lock(&thr->queue_lock);
14053 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
14055 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
14056 mtx_unlock(&thr->queue_lock);
14057 ctl_handle_isc(io);
14060 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
14062 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
14063 /* clear any blocked commands, call fe_done */
14064 mtx_unlock(&thr->queue_lock);
14065 retval = ctl_process_done(io);
14068 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
14070 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
14071 mtx_unlock(&thr->queue_lock);
14072 if (io->io_hdr.io_type == CTL_IO_TASK)
14075 ctl_scsiio_precheck(softc, &io->scsiio);
14078 if (!ctl_pause_rtr) {
14079 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
14081 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
14082 mtx_unlock(&thr->queue_lock);
14083 retval = ctl_scsiio(&io->scsiio);
14084 if (retval != CTL_RETVAL_COMPLETE)
14085 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
14090 /* Sleep until we have something to do. */
14091 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
14096 ctl_lun_thread(void *arg)
14098 struct ctl_softc *softc = (struct ctl_softc *)arg;
14099 struct ctl_be_lun *be_lun;
14102 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
14106 mtx_lock(&softc->ctl_lock);
14107 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
14108 if (be_lun != NULL) {
14109 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
14110 mtx_unlock(&softc->ctl_lock);
14111 ctl_create_lun(be_lun);
14115 /* Sleep until we have something to do. */
14116 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
14117 PDROP | PRIBIO, "-", 0);
14122 ctl_enqueue_incoming(union ctl_io *io)
14124 struct ctl_softc *softc = control_softc;
14125 struct ctl_thread *thr;
14128 idx = (io->io_hdr.nexus.targ_port * 127 +
14129 io->io_hdr.nexus.initid.id) % worker_threads;
14130 thr = &softc->threads[idx];
14131 mtx_lock(&thr->queue_lock);
14132 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14133 mtx_unlock(&thr->queue_lock);
14138 ctl_enqueue_rtr(union ctl_io *io)
14140 struct ctl_softc *softc = control_softc;
14141 struct ctl_thread *thr;
14143 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14144 mtx_lock(&thr->queue_lock);
14145 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14146 mtx_unlock(&thr->queue_lock);
14151 ctl_enqueue_done(union ctl_io *io)
14153 struct ctl_softc *softc = control_softc;
14154 struct ctl_thread *thr;
14156 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14157 mtx_lock(&thr->queue_lock);
14158 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14159 mtx_unlock(&thr->queue_lock);
14164 ctl_enqueue_isc(union ctl_io *io)
14166 struct ctl_softc *softc = control_softc;
14167 struct ctl_thread *thr;
14169 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14170 mtx_lock(&thr->queue_lock);
14171 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14172 mtx_unlock(&thr->queue_lock);
14176 /* Initialization and failover */
14179 ctl_init_isc_msg(void)
14181 printf("CTL: Still calling this thing\n");
14186 * Initializes component into configuration defined by bootMode
14188 * returns hasc_Status:
14190 * ERROR - fatal error
14192 static ctl_ha_comp_status
14193 ctl_isc_init(struct ctl_ha_component *c)
14195 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14202 * Starts component in state requested. If component starts successfully,
14203 * it must set its own state to the requestrd state
14204 * When requested state is HASC_STATE_HA, the component may refine it
14205 * by adding _SLAVE or _MASTER flags.
14206 * Currently allowed state transitions are:
14207 * UNKNOWN->HA - initial startup
14208 * UNKNOWN->SINGLE - initial startup when no parter detected
14209 * HA->SINGLE - failover
14210 * returns ctl_ha_comp_status:
14211 * OK - component successfully started in requested state
14212 * FAILED - could not start the requested state, failover may
14214 * ERROR - fatal error detected, no future startup possible
14216 static ctl_ha_comp_status
14217 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14219 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14221 printf("%s: go\n", __func__);
14223 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14224 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14226 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14227 != CTL_HA_STATUS_SUCCESS) {
14228 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14229 ret = CTL_HA_COMP_STATUS_ERROR;
14231 } else if (CTL_HA_STATE_IS_HA(c->state)
14232 && CTL_HA_STATE_IS_SINGLE(state)){
14233 // HA->SINGLE transition
14237 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14239 ret = CTL_HA_COMP_STATUS_ERROR;
14241 if (CTL_HA_STATE_IS_SINGLE(state))
14250 * Quiesce component
14251 * The component must clear any error conditions (set status to OK) and
14252 * prepare itself to another Start call
14253 * returns ctl_ha_comp_status:
14257 static ctl_ha_comp_status
14258 ctl_isc_quiesce(struct ctl_ha_component *c)
14260 int ret = CTL_HA_COMP_STATUS_OK;
14267 struct ctl_ha_component ctl_ha_component_ctlisc =
14270 .state = CTL_HA_STATE_UNKNOWN,
14271 .init = ctl_isc_init,
14272 .start = ctl_isc_start,
14273 .quiesce = ctl_isc_quiesce