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,
283 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
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,
295 /*queue_flags*/SCP_QUEUE_ALG_MASK,
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 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
316 &worker_threads, 1, "Number of worker threads");
317 static int verbose = 0;
318 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, verbose, CTLFLAG_RWTUN,
319 &verbose, 0, "Show SCSI errors returned to initiator");
322 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
323 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
324 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
325 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
327 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
329 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
331 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
332 static int ctl_init(void);
333 void ctl_shutdown(void);
334 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
335 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
336 static void ctl_ioctl_online(void *arg);
337 static void ctl_ioctl_offline(void *arg);
338 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
339 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
340 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
341 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
342 static int ctl_ioctl_submit_wait(union ctl_io *io);
343 static void ctl_ioctl_datamove(union ctl_io *io);
344 static void ctl_ioctl_done(union ctl_io *io);
345 static void ctl_ioctl_hard_startstop_callback(void *arg,
346 struct cfi_metatask *metatask);
347 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
348 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
349 struct ctl_ooa *ooa_hdr,
350 struct ctl_ooa_entry *kern_entries);
351 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
353 static uint32_t ctl_map_lun(int port_num, uint32_t lun);
354 static uint32_t ctl_map_lun_back(int port_num, uint32_t lun);
356 static union ctl_io *ctl_malloc_io(ctl_io_type io_type, uint32_t targ_port,
357 uint32_t targ_target, uint32_t targ_lun,
359 static void ctl_kfree_io(union ctl_io *io);
361 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
362 struct ctl_be_lun *be_lun, struct ctl_id target_id);
363 static int ctl_free_lun(struct ctl_lun *lun);
364 static void ctl_create_lun(struct ctl_be_lun *be_lun);
366 static void ctl_failover_change_pages(struct ctl_softc *softc,
367 struct ctl_scsiio *ctsio, int master);
370 static int ctl_do_mode_select(union ctl_io *io);
371 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
372 uint64_t res_key, uint64_t sa_res_key,
373 uint8_t type, uint32_t residx,
374 struct ctl_scsiio *ctsio,
375 struct scsi_per_res_out *cdb,
376 struct scsi_per_res_out_parms* param);
377 static void ctl_pro_preempt_other(struct ctl_lun *lun,
378 union ctl_ha_msg *msg);
379 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
380 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
381 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
382 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
383 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
384 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
385 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
387 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
389 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
390 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
391 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
392 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
393 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
394 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2);
395 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
396 union ctl_io *pending_io, union ctl_io *ooa_io);
397 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
398 union ctl_io *starting_io);
399 static int ctl_check_blocked(struct ctl_lun *lun);
400 static int ctl_scsiio_lun_check(struct ctl_softc *ctl_softc,
402 const struct ctl_cmd_entry *entry,
403 struct ctl_scsiio *ctsio);
404 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
405 static void ctl_failover(void);
406 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
407 struct ctl_scsiio *ctsio);
408 static int ctl_scsiio(struct ctl_scsiio *ctsio);
410 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
411 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
412 ctl_ua_type ua_type);
413 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
414 ctl_ua_type ua_type);
415 static int ctl_abort_task(union ctl_io *io);
416 static int ctl_abort_task_set(union ctl_io *io);
417 static int ctl_i_t_nexus_reset(union ctl_io *io);
418 static void ctl_run_task(union ctl_io *io);
420 static void ctl_datamove_timer_wakeup(void *arg);
421 static void ctl_done_timer_wakeup(void *arg);
422 #endif /* CTL_IO_DELAY */
424 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
425 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
426 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
427 static void ctl_datamove_remote_write(union ctl_io *io);
428 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
429 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
430 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
431 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
432 ctl_ha_dt_cb callback);
433 static void ctl_datamove_remote_read(union ctl_io *io);
434 static void ctl_datamove_remote(union ctl_io *io);
435 static int ctl_process_done(union ctl_io *io);
436 static void ctl_lun_thread(void *arg);
437 static void ctl_work_thread(void *arg);
438 static void ctl_enqueue_incoming(union ctl_io *io);
439 static void ctl_enqueue_rtr(union ctl_io *io);
440 static void ctl_enqueue_done(union ctl_io *io);
441 static void ctl_enqueue_isc(union ctl_io *io);
442 static const struct ctl_cmd_entry *
443 ctl_get_cmd_entry(struct ctl_scsiio *ctsio);
444 static const struct ctl_cmd_entry *
445 ctl_validate_command(struct ctl_scsiio *ctsio);
446 static int ctl_cmd_applicable(uint8_t lun_type,
447 const struct ctl_cmd_entry *entry);
450 * Load the serialization table. This isn't very pretty, but is probably
451 * the easiest way to do it.
453 #include "ctl_ser_table.c"
456 * We only need to define open, close and ioctl routines for this driver.
458 static struct cdevsw ctl_cdevsw = {
459 .d_version = D_VERSION,
462 .d_close = ctl_close,
463 .d_ioctl = ctl_ioctl,
468 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
469 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
471 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
473 static moduledata_t ctl_moduledata = {
475 ctl_module_event_handler,
479 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
480 MODULE_VERSION(ctl, 1);
482 static struct ctl_frontend ioctl_frontend =
488 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
489 union ctl_ha_msg *msg_info)
491 struct ctl_scsiio *ctsio;
493 if (msg_info->hdr.original_sc == NULL) {
494 printf("%s: original_sc == NULL!\n", __func__);
495 /* XXX KDM now what? */
499 ctsio = &msg_info->hdr.original_sc->scsiio;
500 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
501 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
502 ctsio->io_hdr.status = msg_info->hdr.status;
503 ctsio->scsi_status = msg_info->scsi.scsi_status;
504 ctsio->sense_len = msg_info->scsi.sense_len;
505 ctsio->sense_residual = msg_info->scsi.sense_residual;
506 ctsio->residual = msg_info->scsi.residual;
507 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
508 sizeof(ctsio->sense_data));
509 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
510 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
511 ctl_enqueue_isc((union ctl_io *)ctsio);
515 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
516 union ctl_ha_msg *msg_info)
518 struct ctl_scsiio *ctsio;
520 if (msg_info->hdr.serializing_sc == NULL) {
521 printf("%s: serializing_sc == NULL!\n", __func__);
522 /* XXX KDM now what? */
526 ctsio = &msg_info->hdr.serializing_sc->scsiio;
529 * Attempt to catch the situation where an I/O has
530 * been freed, and we're using it again.
532 if (ctsio->io_hdr.io_type == 0xff) {
533 union ctl_io *tmp_io;
534 tmp_io = (union ctl_io *)ctsio;
535 printf("%s: %p use after free!\n", __func__,
537 printf("%s: type %d msg %d cdb %x iptl: "
538 "%d:%d:%d:%d tag 0x%04x "
539 "flag %#x status %x\n",
541 tmp_io->io_hdr.io_type,
542 tmp_io->io_hdr.msg_type,
543 tmp_io->scsiio.cdb[0],
544 tmp_io->io_hdr.nexus.initid.id,
545 tmp_io->io_hdr.nexus.targ_port,
546 tmp_io->io_hdr.nexus.targ_target.id,
547 tmp_io->io_hdr.nexus.targ_lun,
548 (tmp_io->io_hdr.io_type ==
550 tmp_io->taskio.tag_num :
551 tmp_io->scsiio.tag_num,
552 tmp_io->io_hdr.flags,
553 tmp_io->io_hdr.status);
556 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
557 ctl_enqueue_isc((union ctl_io *)ctsio);
561 * ISC (Inter Shelf Communication) event handler. Events from the HA
562 * subsystem come in here.
565 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
567 struct ctl_softc *ctl_softc;
569 struct ctl_prio *presio;
570 ctl_ha_status isc_status;
572 ctl_softc = control_softc;
577 printf("CTL: Isc Msg event %d\n", event);
579 if (event == CTL_HA_EVT_MSG_RECV) {
580 union ctl_ha_msg msg_info;
582 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
583 sizeof(msg_info), /*wait*/ 0);
585 printf("CTL: msg_type %d\n", msg_info.msg_type);
587 if (isc_status != 0) {
588 printf("Error receiving message, status = %d\n",
593 switch (msg_info.hdr.msg_type) {
594 case CTL_MSG_SERIALIZE:
596 printf("Serialize\n");
598 io = ctl_alloc_io((void *)ctl_softc->othersc_pool);
600 printf("ctl_isc_event_handler: can't allocate "
603 /* Need to set busy and send msg back */
604 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
605 msg_info.hdr.status = CTL_SCSI_ERROR;
606 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
607 msg_info.scsi.sense_len = 0;
608 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
609 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
614 // populate ctsio from msg_info
615 io->io_hdr.io_type = CTL_IO_SCSI;
616 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
617 io->io_hdr.original_sc = msg_info.hdr.original_sc;
619 printf("pOrig %x\n", (int)msg_info.original_sc);
621 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
624 * If we're in serialization-only mode, we don't
625 * want to go through full done processing. Thus
628 * XXX KDM add another flag that is more specific.
630 if (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)
631 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
632 io->io_hdr.nexus = msg_info.hdr.nexus;
634 printf("targ %d, port %d, iid %d, lun %d\n",
635 io->io_hdr.nexus.targ_target.id,
636 io->io_hdr.nexus.targ_port,
637 io->io_hdr.nexus.initid.id,
638 io->io_hdr.nexus.targ_lun);
640 io->scsiio.tag_num = msg_info.scsi.tag_num;
641 io->scsiio.tag_type = msg_info.scsi.tag_type;
642 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
644 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
645 const struct ctl_cmd_entry *entry;
647 entry = ctl_get_cmd_entry(&io->scsiio);
648 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
650 entry->flags & CTL_FLAG_DATA_MASK;
655 /* Performed on the Originating SC, XFER mode only */
656 case CTL_MSG_DATAMOVE: {
657 struct ctl_sg_entry *sgl;
660 io = msg_info.hdr.original_sc;
662 printf("%s: original_sc == NULL!\n", __func__);
663 /* XXX KDM do something here */
666 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
667 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
669 * Keep track of this, we need to send it back over
670 * when the datamove is complete.
672 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
674 if (msg_info.dt.sg_sequence == 0) {
676 * XXX KDM we use the preallocated S/G list
677 * here, but we'll need to change this to
678 * dynamic allocation if we need larger S/G
681 if (msg_info.dt.kern_sg_entries >
682 sizeof(io->io_hdr.remote_sglist) /
683 sizeof(io->io_hdr.remote_sglist[0])) {
684 printf("%s: number of S/G entries "
685 "needed %u > allocated num %zd\n",
687 msg_info.dt.kern_sg_entries,
688 sizeof(io->io_hdr.remote_sglist)/
689 sizeof(io->io_hdr.remote_sglist[0]));
692 * XXX KDM send a message back to
693 * the other side to shut down the
694 * DMA. The error will come back
695 * through via the normal channel.
699 sgl = io->io_hdr.remote_sglist;
701 sizeof(io->io_hdr.remote_sglist));
703 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
705 io->scsiio.kern_sg_entries =
706 msg_info.dt.kern_sg_entries;
707 io->scsiio.rem_sg_entries =
708 msg_info.dt.kern_sg_entries;
709 io->scsiio.kern_data_len =
710 msg_info.dt.kern_data_len;
711 io->scsiio.kern_total_len =
712 msg_info.dt.kern_total_len;
713 io->scsiio.kern_data_resid =
714 msg_info.dt.kern_data_resid;
715 io->scsiio.kern_rel_offset =
716 msg_info.dt.kern_rel_offset;
718 * Clear out per-DMA flags.
720 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
722 * Add per-DMA flags that are set for this
723 * particular DMA request.
725 io->io_hdr.flags |= msg_info.dt.flags &
728 sgl = (struct ctl_sg_entry *)
729 io->scsiio.kern_data_ptr;
731 for (i = msg_info.dt.sent_sg_entries, j = 0;
732 i < (msg_info.dt.sent_sg_entries +
733 msg_info.dt.cur_sg_entries); i++, j++) {
734 sgl[i].addr = msg_info.dt.sg_list[j].addr;
735 sgl[i].len = msg_info.dt.sg_list[j].len;
738 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
740 msg_info.dt.sg_list[j].addr,
741 msg_info.dt.sg_list[j].len,
742 sgl[i].addr, sgl[i].len, j, i);
746 memcpy(&sgl[msg_info.dt.sent_sg_entries],
748 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
752 * If this is the last piece of the I/O, we've got
753 * the full S/G list. Queue processing in the thread.
754 * Otherwise wait for the next piece.
756 if (msg_info.dt.sg_last != 0)
760 /* Performed on the Serializing (primary) SC, XFER mode only */
761 case CTL_MSG_DATAMOVE_DONE: {
762 if (msg_info.hdr.serializing_sc == NULL) {
763 printf("%s: serializing_sc == NULL!\n",
765 /* XXX KDM now what? */
769 * We grab the sense information here in case
770 * there was a failure, so we can return status
771 * back to the initiator.
773 io = msg_info.hdr.serializing_sc;
774 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
775 io->io_hdr.status = msg_info.hdr.status;
776 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
777 io->scsiio.sense_len = msg_info.scsi.sense_len;
778 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
779 io->io_hdr.port_status = msg_info.scsi.fetd_status;
780 io->scsiio.residual = msg_info.scsi.residual;
781 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
782 sizeof(io->scsiio.sense_data));
787 /* Preformed on Originating SC, SER_ONLY mode */
789 io = msg_info.hdr.original_sc;
791 printf("%s: Major Bummer\n", __func__);
795 printf("pOrig %x\n",(int) ctsio);
798 io->io_hdr.msg_type = CTL_MSG_R2R;
799 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
804 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
806 * Performed on the Originating (i.e. secondary) SC in XFER
809 case CTL_MSG_FINISH_IO:
810 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER)
811 ctl_isc_handler_finish_xfer(ctl_softc,
814 ctl_isc_handler_finish_ser_only(ctl_softc,
818 /* Preformed on Originating SC */
819 case CTL_MSG_BAD_JUJU:
820 io = msg_info.hdr.original_sc;
822 printf("%s: Bad JUJU!, original_sc is NULL!\n",
826 ctl_copy_sense_data(&msg_info, io);
828 * IO should have already been cleaned up on other
829 * SC so clear this flag so we won't send a message
830 * back to finish the IO there.
832 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
833 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
835 /* io = msg_info.hdr.serializing_sc; */
836 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
840 /* Handle resets sent from the other side */
841 case CTL_MSG_MANAGE_TASKS: {
842 struct ctl_taskio *taskio;
843 taskio = (struct ctl_taskio *)ctl_alloc_io(
844 (void *)ctl_softc->othersc_pool);
845 if (taskio == NULL) {
846 printf("ctl_isc_event_handler: can't allocate "
849 /* should I just call the proper reset func
853 ctl_zero_io((union ctl_io *)taskio);
854 taskio->io_hdr.io_type = CTL_IO_TASK;
855 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
856 taskio->io_hdr.nexus = msg_info.hdr.nexus;
857 taskio->task_action = msg_info.task.task_action;
858 taskio->tag_num = msg_info.task.tag_num;
859 taskio->tag_type = msg_info.task.tag_type;
861 taskio->io_hdr.start_time = time_uptime;
862 getbintime(&taskio->io_hdr.start_bt);
864 cs_prof_gettime(&taskio->io_hdr.start_ticks);
866 #endif /* CTL_TIME_IO */
867 ctl_run_task((union ctl_io *)taskio);
870 /* Persistent Reserve action which needs attention */
871 case CTL_MSG_PERS_ACTION:
872 presio = (struct ctl_prio *)ctl_alloc_io(
873 (void *)ctl_softc->othersc_pool);
874 if (presio == NULL) {
875 printf("ctl_isc_event_handler: can't allocate "
878 /* Need to set busy and send msg back */
881 ctl_zero_io((union ctl_io *)presio);
882 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
883 presio->pr_msg = msg_info.pr;
884 ctl_enqueue_isc((union ctl_io *)presio);
886 case CTL_MSG_SYNC_FE:
889 case CTL_MSG_APS_LOCK: {
890 // It's quicker to execute this then to
893 struct ctl_page_index *page_index;
894 struct copan_aps_subpage *current_sp;
897 targ_lun = msg_info.hdr.nexus.targ_mapped_lun;
898 lun = ctl_softc->ctl_luns[targ_lun];
899 mtx_lock(&lun->lun_lock);
900 page_index = &lun->mode_pages.index[index_to_aps_page];
901 current_sp = (struct copan_aps_subpage *)
902 (page_index->page_data +
903 (page_index->page_len * CTL_PAGE_CURRENT));
905 current_sp->lock_active = msg_info.aps.lock_flag;
906 mtx_unlock(&lun->lun_lock);
910 printf("How did I get here?\n");
912 } else if (event == CTL_HA_EVT_MSG_SENT) {
913 if (param != CTL_HA_STATUS_SUCCESS) {
914 printf("Bad status from ctl_ha_msg_send status %d\n",
918 } else if (event == CTL_HA_EVT_DISCONNECT) {
919 printf("CTL: Got a disconnect from Isc\n");
922 printf("ctl_isc_event_handler: Unknown event %d\n", event);
931 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
933 struct scsi_sense_data *sense;
935 sense = &dest->scsiio.sense_data;
936 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
937 dest->scsiio.scsi_status = src->scsi.scsi_status;
938 dest->scsiio.sense_len = src->scsi.sense_len;
939 dest->io_hdr.status = src->hdr.status;
945 struct ctl_softc *softc;
946 struct ctl_io_pool *internal_pool, *emergency_pool, *other_pool;
947 struct ctl_port *port;
949 int i, error, retval;
956 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
958 softc = control_softc;
960 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
963 softc->dev->si_drv1 = softc;
966 * By default, return a "bad LUN" peripheral qualifier for unknown
967 * LUNs. The user can override this default using the tunable or
968 * sysctl. See the comment in ctl_inquiry_std() for more details.
970 softc->inquiry_pq_no_lun = 1;
971 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
972 &softc->inquiry_pq_no_lun);
973 sysctl_ctx_init(&softc->sysctl_ctx);
974 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
975 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
976 CTLFLAG_RD, 0, "CAM Target Layer");
978 if (softc->sysctl_tree == NULL) {
979 printf("%s: unable to allocate sysctl tree\n", __func__);
980 destroy_dev(softc->dev);
981 free(control_softc, M_DEVBUF);
982 control_softc = NULL;
986 SYSCTL_ADD_INT(&softc->sysctl_ctx,
987 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
988 "inquiry_pq_no_lun", CTLFLAG_RW,
989 &softc->inquiry_pq_no_lun, 0,
990 "Report no lun possible for invalid LUNs");
992 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
993 mtx_init(&softc->pool_lock, "CTL pool mutex", NULL, MTX_DEF);
994 softc->open_count = 0;
997 * Default to actually sending a SYNCHRONIZE CACHE command down to
1000 softc->flags = CTL_FLAG_REAL_SYNC;
1003 * In Copan's HA scheme, the "master" and "slave" roles are
1004 * figured out through the slot the controller is in. Although it
1005 * is an active/active system, someone has to be in charge.
1008 scmicro_rw(SCMICRO_GET_SHELF_ID, &sc_id);
1012 softc->flags |= CTL_FLAG_MASTER_SHELF;
1015 persis_offset = CTL_MAX_INITIATORS;
1018 * XXX KDM need to figure out where we want to get our target ID
1019 * and WWID. Is it different on each port?
1021 softc->target.id = 0;
1022 softc->target.wwid[0] = 0x12345678;
1023 softc->target.wwid[1] = 0x87654321;
1024 STAILQ_INIT(&softc->lun_list);
1025 STAILQ_INIT(&softc->pending_lun_queue);
1026 STAILQ_INIT(&softc->fe_list);
1027 STAILQ_INIT(&softc->port_list);
1028 STAILQ_INIT(&softc->be_list);
1029 STAILQ_INIT(&softc->io_pools);
1030 ctl_tpc_init(softc);
1032 if (ctl_pool_create(softc, CTL_POOL_INTERNAL, CTL_POOL_ENTRIES_INTERNAL,
1033 &internal_pool)!= 0){
1034 printf("ctl: can't allocate %d entry internal pool, "
1035 "exiting\n", CTL_POOL_ENTRIES_INTERNAL);
1039 if (ctl_pool_create(softc, CTL_POOL_EMERGENCY,
1040 CTL_POOL_ENTRIES_EMERGENCY, &emergency_pool) != 0) {
1041 printf("ctl: can't allocate %d entry emergency pool, "
1042 "exiting\n", CTL_POOL_ENTRIES_EMERGENCY);
1043 ctl_pool_free(internal_pool);
1047 if (ctl_pool_create(softc, CTL_POOL_4OTHERSC, CTL_POOL_ENTRIES_OTHER_SC,
1050 printf("ctl: can't allocate %d entry other SC pool, "
1051 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1052 ctl_pool_free(internal_pool);
1053 ctl_pool_free(emergency_pool);
1057 softc->internal_pool = internal_pool;
1058 softc->emergency_pool = emergency_pool;
1059 softc->othersc_pool = other_pool;
1061 if (worker_threads <= 0)
1062 worker_threads = max(1, mp_ncpus / 4);
1063 if (worker_threads > CTL_MAX_THREADS)
1064 worker_threads = CTL_MAX_THREADS;
1066 for (i = 0; i < worker_threads; i++) {
1067 struct ctl_thread *thr = &softc->threads[i];
1069 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1070 thr->ctl_softc = softc;
1071 STAILQ_INIT(&thr->incoming_queue);
1072 STAILQ_INIT(&thr->rtr_queue);
1073 STAILQ_INIT(&thr->done_queue);
1074 STAILQ_INIT(&thr->isc_queue);
1076 error = kproc_kthread_add(ctl_work_thread, thr,
1077 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1079 printf("error creating CTL work thread!\n");
1080 ctl_pool_free(internal_pool);
1081 ctl_pool_free(emergency_pool);
1082 ctl_pool_free(other_pool);
1086 error = kproc_kthread_add(ctl_lun_thread, softc,
1087 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1089 printf("error creating CTL lun thread!\n");
1090 ctl_pool_free(internal_pool);
1091 ctl_pool_free(emergency_pool);
1092 ctl_pool_free(other_pool);
1096 printf("ctl: CAM Target Layer loaded\n");
1099 * Initialize the ioctl front end.
1101 ctl_frontend_register(&ioctl_frontend);
1102 port = &softc->ioctl_info.port;
1103 port->frontend = &ioctl_frontend;
1104 sprintf(softc->ioctl_info.port_name, "ioctl");
1105 port->port_type = CTL_PORT_IOCTL;
1106 port->num_requested_ctl_io = 100;
1107 port->port_name = softc->ioctl_info.port_name;
1108 port->port_online = ctl_ioctl_online;
1109 port->port_offline = ctl_ioctl_offline;
1110 port->onoff_arg = &softc->ioctl_info;
1111 port->lun_enable = ctl_ioctl_lun_enable;
1112 port->lun_disable = ctl_ioctl_lun_disable;
1113 port->targ_lun_arg = &softc->ioctl_info;
1114 port->fe_datamove = ctl_ioctl_datamove;
1115 port->fe_done = ctl_ioctl_done;
1116 port->max_targets = 15;
1117 port->max_target_id = 15;
1119 if (ctl_port_register(&softc->ioctl_info.port,
1120 (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0) {
1121 printf("ctl: ioctl front end registration failed, will "
1122 "continue anyway\n");
1126 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1127 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1128 sizeof(struct callout), CTL_TIMER_BYTES);
1131 #endif /* CTL_IO_DELAY */
1139 struct ctl_softc *softc;
1140 struct ctl_lun *lun, *next_lun;
1141 struct ctl_io_pool *pool;
1143 softc = (struct ctl_softc *)control_softc;
1145 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1146 printf("ctl: ioctl front end deregistration failed\n");
1148 mtx_lock(&softc->ctl_lock);
1153 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1154 next_lun = STAILQ_NEXT(lun, links);
1158 mtx_unlock(&softc->ctl_lock);
1160 ctl_frontend_deregister(&ioctl_frontend);
1163 * This will rip the rug out from under any FETDs or anyone else
1164 * that has a pool allocated. Since we increment our module
1165 * refcount any time someone outside the main CTL module allocates
1166 * a pool, we shouldn't have any problems here. The user won't be
1167 * able to unload the CTL module until client modules have
1168 * successfully unloaded.
1170 while ((pool = STAILQ_FIRST(&softc->io_pools)) != NULL)
1171 ctl_pool_free(pool);
1174 ctl_shutdown_thread(softc->work_thread);
1175 mtx_destroy(&softc->queue_lock);
1178 ctl_tpc_shutdown(softc);
1179 mtx_destroy(&softc->pool_lock);
1180 mtx_destroy(&softc->ctl_lock);
1182 destroy_dev(softc->dev);
1184 sysctl_ctx_free(&softc->sysctl_ctx);
1186 free(control_softc, M_DEVBUF);
1187 control_softc = NULL;
1190 printf("ctl: CAM Target Layer unloaded\n");
1194 ctl_module_event_handler(module_t mod, int what, void *arg)
1199 return (ctl_init());
1203 return (EOPNOTSUPP);
1208 * XXX KDM should we do some access checks here? Bump a reference count to
1209 * prevent a CTL module from being unloaded while someone has it open?
1212 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1218 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1224 ctl_port_enable(ctl_port_type port_type)
1226 struct ctl_softc *softc;
1227 struct ctl_port *port;
1229 if (ctl_is_single == 0) {
1230 union ctl_ha_msg msg_info;
1234 printf("%s: HA mode, synchronizing frontend enable\n",
1237 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1238 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1239 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1240 printf("Sync msg send error retval %d\n", isc_retval);
1242 if (!rcv_sync_msg) {
1243 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1244 sizeof(msg_info), 1);
1247 printf("CTL:Frontend Enable\n");
1249 printf("%s: single mode, skipping frontend synchronization\n",
1254 softc = control_softc;
1256 STAILQ_FOREACH(port, &softc->port_list, links) {
1257 if (port_type & port->port_type)
1260 printf("port %d\n", port->targ_port);
1262 ctl_port_online(port);
1270 ctl_port_disable(ctl_port_type port_type)
1272 struct ctl_softc *softc;
1273 struct ctl_port *port;
1275 softc = control_softc;
1277 STAILQ_FOREACH(port, &softc->port_list, links) {
1278 if (port_type & port->port_type)
1279 ctl_port_offline(port);
1286 * Returns 0 for success, 1 for failure.
1287 * Currently the only failure mode is if there aren't enough entries
1288 * allocated. So, in case of a failure, look at num_entries_dropped,
1289 * reallocate and try again.
1292 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1293 int *num_entries_filled, int *num_entries_dropped,
1294 ctl_port_type port_type, int no_virtual)
1296 struct ctl_softc *softc;
1297 struct ctl_port *port;
1298 int entries_dropped, entries_filled;
1302 softc = control_softc;
1306 entries_dropped = 0;
1309 mtx_lock(&softc->ctl_lock);
1310 STAILQ_FOREACH(port, &softc->port_list, links) {
1311 struct ctl_port_entry *entry;
1313 if ((port->port_type & port_type) == 0)
1316 if ((no_virtual != 0)
1317 && (port->virtual_port != 0))
1320 if (entries_filled >= num_entries_alloced) {
1324 entry = &entries[i];
1326 entry->port_type = port->port_type;
1327 strlcpy(entry->port_name, port->port_name,
1328 sizeof(entry->port_name));
1329 entry->physical_port = port->physical_port;
1330 entry->virtual_port = port->virtual_port;
1331 entry->wwnn = port->wwnn;
1332 entry->wwpn = port->wwpn;
1338 mtx_unlock(&softc->ctl_lock);
1340 if (entries_dropped > 0)
1343 *num_entries_dropped = entries_dropped;
1344 *num_entries_filled = entries_filled;
1350 ctl_ioctl_online(void *arg)
1352 struct ctl_ioctl_info *ioctl_info;
1354 ioctl_info = (struct ctl_ioctl_info *)arg;
1356 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1360 ctl_ioctl_offline(void *arg)
1362 struct ctl_ioctl_info *ioctl_info;
1364 ioctl_info = (struct ctl_ioctl_info *)arg;
1366 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1370 * Remove an initiator by port number and initiator ID.
1371 * Returns 0 for success, -1 for failure.
1374 ctl_remove_initiator(struct ctl_port *port, int iid)
1376 struct ctl_softc *softc = control_softc;
1378 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1380 if (iid > CTL_MAX_INIT_PER_PORT) {
1381 printf("%s: initiator ID %u > maximun %u!\n",
1382 __func__, iid, CTL_MAX_INIT_PER_PORT);
1386 mtx_lock(&softc->ctl_lock);
1387 port->wwpn_iid[iid].in_use--;
1388 port->wwpn_iid[iid].last_use = time_uptime;
1389 mtx_unlock(&softc->ctl_lock);
1395 * Add an initiator to the initiator map.
1396 * Returns iid for success, < 0 for failure.
1399 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1401 struct ctl_softc *softc = control_softc;
1405 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1407 if (iid >= CTL_MAX_INIT_PER_PORT) {
1408 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1409 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1414 mtx_lock(&softc->ctl_lock);
1416 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1417 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1418 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1422 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1423 strcmp(name, port->wwpn_iid[i].name) == 0) {
1431 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1432 if (port->wwpn_iid[i].in_use == 0 &&
1433 port->wwpn_iid[i].wwpn == 0 &&
1434 port->wwpn_iid[i].name == NULL) {
1443 best_time = INT32_MAX;
1444 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1445 if (port->wwpn_iid[i].in_use == 0) {
1446 if (port->wwpn_iid[i].last_use < best_time) {
1448 best_time = port->wwpn_iid[i].last_use;
1456 mtx_unlock(&softc->ctl_lock);
1461 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1463 * This is not an error yet.
1465 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1467 printf("%s: port %d iid %u WWPN %#jx arrived"
1468 " again\n", __func__, port->targ_port,
1469 iid, (uintmax_t)wwpn);
1473 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1474 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1476 printf("%s: port %d iid %u name '%s' arrived"
1477 " again\n", __func__, port->targ_port,
1484 * This is an error, but what do we do about it? The
1485 * driver is telling us we have a new WWPN for this
1486 * initiator ID, so we pretty much need to use it.
1488 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1489 " but WWPN %#jx '%s' is still at that address\n",
1490 __func__, port->targ_port, iid, wwpn, name,
1491 (uintmax_t)port->wwpn_iid[iid].wwpn,
1492 port->wwpn_iid[iid].name);
1495 * XXX KDM clear have_ca and ua_pending on each LUN for
1500 free(port->wwpn_iid[iid].name, M_CTL);
1501 port->wwpn_iid[iid].name = name;
1502 port->wwpn_iid[iid].wwpn = wwpn;
1503 port->wwpn_iid[iid].in_use++;
1504 mtx_unlock(&softc->ctl_lock);
1510 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1514 switch (port->port_type) {
1517 struct scsi_transportid_fcp *id =
1518 (struct scsi_transportid_fcp *)buf;
1519 if (port->wwpn_iid[iid].wwpn == 0)
1521 memset(id, 0, sizeof(*id));
1522 id->format_protocol = SCSI_PROTO_FC;
1523 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1524 return (sizeof(*id));
1526 case CTL_PORT_ISCSI:
1528 struct scsi_transportid_iscsi_port *id =
1529 (struct scsi_transportid_iscsi_port *)buf;
1530 if (port->wwpn_iid[iid].name == NULL)
1533 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1535 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1536 len = roundup2(min(len, 252), 4);
1537 scsi_ulto2b(len, id->additional_length);
1538 return (sizeof(*id) + len);
1542 struct scsi_transportid_sas *id =
1543 (struct scsi_transportid_sas *)buf;
1544 if (port->wwpn_iid[iid].wwpn == 0)
1546 memset(id, 0, sizeof(*id));
1547 id->format_protocol = SCSI_PROTO_SAS;
1548 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1549 return (sizeof(*id));
1553 struct scsi_transportid_spi *id =
1554 (struct scsi_transportid_spi *)buf;
1555 memset(id, 0, sizeof(*id));
1556 id->format_protocol = SCSI_PROTO_SPI;
1557 scsi_ulto2b(iid, id->scsi_addr);
1558 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1559 return (sizeof(*id));
1565 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1571 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1577 * Data movement routine for the CTL ioctl frontend port.
1580 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1582 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1583 struct ctl_sg_entry ext_entry, kern_entry;
1584 int ext_sglen, ext_sg_entries, kern_sg_entries;
1585 int ext_sg_start, ext_offset;
1586 int len_to_copy, len_copied;
1587 int kern_watermark, ext_watermark;
1588 int ext_sglist_malloced;
1591 ext_sglist_malloced = 0;
1595 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1598 * If this flag is set, fake the data transfer.
1600 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1601 ctsio->ext_data_filled = ctsio->ext_data_len;
1606 * To simplify things here, if we have a single buffer, stick it in
1607 * a S/G entry and just make it a single entry S/G list.
1609 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1612 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1614 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1616 ext_sglist_malloced = 1;
1617 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1619 ctl_set_internal_failure(ctsio,
1624 ext_sg_entries = ctsio->ext_sg_entries;
1626 for (i = 0; i < ext_sg_entries; i++) {
1627 if ((len_seen + ext_sglist[i].len) >=
1628 ctsio->ext_data_filled) {
1630 ext_offset = ctsio->ext_data_filled - len_seen;
1633 len_seen += ext_sglist[i].len;
1636 ext_sglist = &ext_entry;
1637 ext_sglist->addr = ctsio->ext_data_ptr;
1638 ext_sglist->len = ctsio->ext_data_len;
1641 ext_offset = ctsio->ext_data_filled;
1644 if (ctsio->kern_sg_entries > 0) {
1645 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1646 kern_sg_entries = ctsio->kern_sg_entries;
1648 kern_sglist = &kern_entry;
1649 kern_sglist->addr = ctsio->kern_data_ptr;
1650 kern_sglist->len = ctsio->kern_data_len;
1651 kern_sg_entries = 1;
1656 ext_watermark = ext_offset;
1658 for (i = ext_sg_start, j = 0;
1659 i < ext_sg_entries && j < kern_sg_entries;) {
1660 uint8_t *ext_ptr, *kern_ptr;
1662 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
1663 kern_sglist[j].len - kern_watermark);
1665 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1666 ext_ptr = ext_ptr + ext_watermark;
1667 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1671 panic("need to implement bus address support");
1673 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1676 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1677 kern_ptr = kern_ptr + kern_watermark;
1679 kern_watermark += len_to_copy;
1680 ext_watermark += len_to_copy;
1682 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1684 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1685 "bytes to user\n", len_to_copy));
1686 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1687 "to %p\n", kern_ptr, ext_ptr));
1688 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1689 ctl_set_internal_failure(ctsio,
1695 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1696 "bytes from user\n", len_to_copy));
1697 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1698 "to %p\n", ext_ptr, kern_ptr));
1699 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1700 ctl_set_internal_failure(ctsio,
1707 len_copied += len_to_copy;
1709 if (ext_sglist[i].len == ext_watermark) {
1714 if (kern_sglist[j].len == kern_watermark) {
1720 ctsio->ext_data_filled += len_copied;
1722 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1723 "kern_sg_entries: %d\n", ext_sg_entries,
1725 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1726 "kern_data_len = %d\n", ctsio->ext_data_len,
1727 ctsio->kern_data_len));
1730 /* XXX KDM set residual?? */
1733 if (ext_sglist_malloced != 0)
1734 free(ext_sglist, M_CTL);
1736 return (CTL_RETVAL_COMPLETE);
1740 * Serialize a command that went down the "wrong" side, and so was sent to
1741 * this controller for execution. The logic is a little different than the
1742 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1743 * sent back to the other side, but in the success case, we execute the
1744 * command on this side (XFER mode) or tell the other side to execute it
1748 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1750 struct ctl_softc *ctl_softc;
1751 union ctl_ha_msg msg_info;
1752 struct ctl_lun *lun;
1756 ctl_softc = control_softc;
1758 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1759 lun = ctl_softc->ctl_luns[targ_lun];
1763 * Why isn't LUN defined? The other side wouldn't
1764 * send a cmd if the LUN is undefined.
1766 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1768 /* "Logical unit not supported" */
1769 ctl_set_sense_data(&msg_info.scsi.sense_data,
1771 /*sense_format*/SSD_TYPE_NONE,
1772 /*current_error*/ 1,
1773 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1778 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1779 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1780 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1781 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1782 msg_info.hdr.serializing_sc = NULL;
1783 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1784 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1785 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1791 mtx_lock(&lun->lun_lock);
1792 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1794 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1795 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1797 case CTL_ACTION_BLOCK:
1798 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1799 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1802 case CTL_ACTION_PASS:
1803 case CTL_ACTION_SKIP:
1804 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
1805 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1806 ctl_enqueue_rtr((union ctl_io *)ctsio);
1809 /* send msg back to other side */
1810 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1811 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1812 msg_info.hdr.msg_type = CTL_MSG_R2R;
1814 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1816 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1817 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1821 case CTL_ACTION_OVERLAP:
1822 /* OVERLAPPED COMMANDS ATTEMPTED */
1823 ctl_set_sense_data(&msg_info.scsi.sense_data,
1825 /*sense_format*/SSD_TYPE_NONE,
1826 /*current_error*/ 1,
1827 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1832 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1833 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1834 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1835 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1836 msg_info.hdr.serializing_sc = NULL;
1837 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1839 printf("BAD JUJU:Major Bummer Overlap\n");
1841 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1843 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1844 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1847 case CTL_ACTION_OVERLAP_TAG:
1848 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1849 ctl_set_sense_data(&msg_info.scsi.sense_data,
1851 /*sense_format*/SSD_TYPE_NONE,
1852 /*current_error*/ 1,
1853 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1855 /*ascq*/ ctsio->tag_num & 0xff,
1858 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1859 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1860 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1861 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1862 msg_info.hdr.serializing_sc = NULL;
1863 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1865 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1867 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1869 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1870 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1873 case CTL_ACTION_ERROR:
1875 /* "Internal target failure" */
1876 ctl_set_sense_data(&msg_info.scsi.sense_data,
1878 /*sense_format*/SSD_TYPE_NONE,
1879 /*current_error*/ 1,
1880 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1885 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1886 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1887 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1888 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1889 msg_info.hdr.serializing_sc = NULL;
1890 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1892 printf("BAD JUJU:Major Bummer HW Error\n");
1894 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1896 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1897 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1901 mtx_unlock(&lun->lun_lock);
1906 ctl_ioctl_submit_wait(union ctl_io *io)
1908 struct ctl_fe_ioctl_params params;
1909 ctl_fe_ioctl_state last_state;
1914 bzero(¶ms, sizeof(params));
1916 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
1917 cv_init(¶ms.sem, "ctlioccv");
1918 params.state = CTL_IOCTL_INPROG;
1919 last_state = params.state;
1921 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
1923 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
1925 /* This shouldn't happen */
1926 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
1932 mtx_lock(¶ms.ioctl_mtx);
1934 * Check the state here, and don't sleep if the state has
1935 * already changed (i.e. wakeup has already occured, but we
1936 * weren't waiting yet).
1938 if (params.state == last_state) {
1939 /* XXX KDM cv_wait_sig instead? */
1940 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
1942 last_state = params.state;
1944 switch (params.state) {
1945 case CTL_IOCTL_INPROG:
1946 /* Why did we wake up? */
1947 /* XXX KDM error here? */
1948 mtx_unlock(¶ms.ioctl_mtx);
1950 case CTL_IOCTL_DATAMOVE:
1951 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
1954 * change last_state back to INPROG to avoid
1955 * deadlock on subsequent data moves.
1957 params.state = last_state = CTL_IOCTL_INPROG;
1959 mtx_unlock(¶ms.ioctl_mtx);
1960 ctl_ioctl_do_datamove(&io->scsiio);
1962 * Note that in some cases, most notably writes,
1963 * this will queue the I/O and call us back later.
1964 * In other cases, generally reads, this routine
1965 * will immediately call back and wake us up,
1966 * probably using our own context.
1968 io->scsiio.be_move_done(io);
1970 case CTL_IOCTL_DONE:
1971 mtx_unlock(¶ms.ioctl_mtx);
1972 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
1976 mtx_unlock(¶ms.ioctl_mtx);
1977 /* XXX KDM error here? */
1980 } while (done == 0);
1982 mtx_destroy(¶ms.ioctl_mtx);
1983 cv_destroy(¶ms.sem);
1985 return (CTL_RETVAL_COMPLETE);
1989 ctl_ioctl_datamove(union ctl_io *io)
1991 struct ctl_fe_ioctl_params *params;
1993 params = (struct ctl_fe_ioctl_params *)
1994 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1996 mtx_lock(¶ms->ioctl_mtx);
1997 params->state = CTL_IOCTL_DATAMOVE;
1998 cv_broadcast(¶ms->sem);
1999 mtx_unlock(¶ms->ioctl_mtx);
2003 ctl_ioctl_done(union ctl_io *io)
2005 struct ctl_fe_ioctl_params *params;
2007 params = (struct ctl_fe_ioctl_params *)
2008 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2010 mtx_lock(¶ms->ioctl_mtx);
2011 params->state = CTL_IOCTL_DONE;
2012 cv_broadcast(¶ms->sem);
2013 mtx_unlock(¶ms->ioctl_mtx);
2017 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2019 struct ctl_fe_ioctl_startstop_info *sd_info;
2021 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2023 sd_info->hs_info.status = metatask->status;
2024 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2025 sd_info->hs_info.luns_complete =
2026 metatask->taskinfo.startstop.luns_complete;
2027 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2029 cv_broadcast(&sd_info->sem);
2033 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2035 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2037 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2039 mtx_lock(fe_bbr_info->lock);
2040 fe_bbr_info->bbr_info->status = metatask->status;
2041 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2042 fe_bbr_info->wakeup_done = 1;
2043 mtx_unlock(fe_bbr_info->lock);
2045 cv_broadcast(&fe_bbr_info->sem);
2049 * Returns 0 for success, errno for failure.
2052 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2053 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2060 mtx_lock(&lun->lun_lock);
2061 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2062 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2064 struct ctl_ooa_entry *entry;
2067 * If we've got more than we can fit, just count the
2068 * remaining entries.
2070 if (*cur_fill_num >= ooa_hdr->alloc_num)
2073 entry = &kern_entries[*cur_fill_num];
2075 entry->tag_num = io->scsiio.tag_num;
2076 entry->lun_num = lun->lun;
2078 entry->start_bt = io->io_hdr.start_bt;
2080 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2081 entry->cdb_len = io->scsiio.cdb_len;
2082 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2083 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2085 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2086 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2088 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2089 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2091 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2092 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2094 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2095 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2097 mtx_unlock(&lun->lun_lock);
2103 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2104 size_t error_str_len)
2108 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2110 if (copyin(user_addr, kptr, len) != 0) {
2111 snprintf(error_str, error_str_len, "Error copying %d bytes "
2112 "from user address %p to kernel address %p", len,
2122 ctl_free_args(int num_args, struct ctl_be_arg *args)
2129 for (i = 0; i < num_args; i++) {
2130 free(args[i].kname, M_CTL);
2131 free(args[i].kvalue, M_CTL);
2137 static struct ctl_be_arg *
2138 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2139 char *error_str, size_t error_str_len)
2141 struct ctl_be_arg *args;
2144 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2145 error_str, error_str_len);
2150 for (i = 0; i < num_args; i++) {
2151 args[i].kname = NULL;
2152 args[i].kvalue = NULL;
2155 for (i = 0; i < num_args; i++) {
2158 args[i].kname = ctl_copyin_alloc(args[i].name,
2159 args[i].namelen, error_str, error_str_len);
2160 if (args[i].kname == NULL)
2163 if (args[i].kname[args[i].namelen - 1] != '\0') {
2164 snprintf(error_str, error_str_len, "Argument %d "
2165 "name is not NUL-terminated", i);
2169 if (args[i].flags & CTL_BEARG_RD) {
2170 tmpptr = ctl_copyin_alloc(args[i].value,
2171 args[i].vallen, error_str, error_str_len);
2174 if ((args[i].flags & CTL_BEARG_ASCII)
2175 && (tmpptr[args[i].vallen - 1] != '\0')) {
2176 snprintf(error_str, error_str_len, "Argument "
2177 "%d value is not NUL-terminated", i);
2180 args[i].kvalue = tmpptr;
2182 args[i].kvalue = malloc(args[i].vallen,
2183 M_CTL, M_WAITOK | M_ZERO);
2190 ctl_free_args(num_args, args);
2196 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2200 for (i = 0; i < num_args; i++) {
2201 if (args[i].flags & CTL_BEARG_WR)
2202 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2207 * Escape characters that are illegal or not recommended in XML.
2210 ctl_sbuf_printf_esc(struct sbuf *sb, char *str)
2216 for (; *str; str++) {
2219 retval = sbuf_printf(sb, "&");
2222 retval = sbuf_printf(sb, ">");
2225 retval = sbuf_printf(sb, "<");
2228 retval = sbuf_putc(sb, *str);
2241 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2244 struct ctl_softc *softc;
2247 softc = control_softc;
2257 * If we haven't been "enabled", don't allow any SCSI I/O
2260 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2265 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2267 printf("ctl_ioctl: can't allocate ctl_io!\n");
2273 * Need to save the pool reference so it doesn't get
2274 * spammed by the user's ctl_io.
2276 pool_tmp = io->io_hdr.pool;
2278 memcpy(io, (void *)addr, sizeof(*io));
2280 io->io_hdr.pool = pool_tmp;
2282 * No status yet, so make sure the status is set properly.
2284 io->io_hdr.status = CTL_STATUS_NONE;
2287 * The user sets the initiator ID, target and LUN IDs.
2289 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2290 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2291 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2292 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2293 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2295 retval = ctl_ioctl_submit_wait(io);
2302 memcpy((void *)addr, io, sizeof(*io));
2304 /* return this to our pool */
2309 case CTL_ENABLE_PORT:
2310 case CTL_DISABLE_PORT:
2311 case CTL_SET_PORT_WWNS: {
2312 struct ctl_port *port;
2313 struct ctl_port_entry *entry;
2315 entry = (struct ctl_port_entry *)addr;
2317 mtx_lock(&softc->ctl_lock);
2318 STAILQ_FOREACH(port, &softc->port_list, links) {
2324 if ((entry->port_type == CTL_PORT_NONE)
2325 && (entry->targ_port == port->targ_port)) {
2327 * If the user only wants to enable or
2328 * disable or set WWNs on a specific port,
2329 * do the operation and we're done.
2333 } else if (entry->port_type & port->port_type) {
2335 * Compare the user's type mask with the
2336 * particular frontend type to see if we
2343 * Make sure the user isn't trying to set
2344 * WWNs on multiple ports at the same time.
2346 if (cmd == CTL_SET_PORT_WWNS) {
2347 printf("%s: Can't set WWNs on "
2348 "multiple ports\n", __func__);
2355 * XXX KDM we have to drop the lock here,
2356 * because the online/offline operations
2357 * can potentially block. We need to
2358 * reference count the frontends so they
2361 mtx_unlock(&softc->ctl_lock);
2363 if (cmd == CTL_ENABLE_PORT) {
2364 struct ctl_lun *lun;
2366 STAILQ_FOREACH(lun, &softc->lun_list,
2368 port->lun_enable(port->targ_lun_arg,
2373 ctl_port_online(port);
2374 } else if (cmd == CTL_DISABLE_PORT) {
2375 struct ctl_lun *lun;
2377 ctl_port_offline(port);
2379 STAILQ_FOREACH(lun, &softc->lun_list,
2388 mtx_lock(&softc->ctl_lock);
2390 if (cmd == CTL_SET_PORT_WWNS)
2391 ctl_port_set_wwns(port,
2392 (entry->flags & CTL_PORT_WWNN_VALID) ?
2394 (entry->flags & CTL_PORT_WWPN_VALID) ?
2395 1 : 0, entry->wwpn);
2400 mtx_unlock(&softc->ctl_lock);
2403 case CTL_GET_PORT_LIST: {
2404 struct ctl_port *port;
2405 struct ctl_port_list *list;
2408 list = (struct ctl_port_list *)addr;
2410 if (list->alloc_len != (list->alloc_num *
2411 sizeof(struct ctl_port_entry))) {
2412 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2413 "alloc_num %u * sizeof(struct ctl_port_entry) "
2414 "%zu\n", __func__, list->alloc_len,
2415 list->alloc_num, sizeof(struct ctl_port_entry));
2421 list->dropped_num = 0;
2423 mtx_lock(&softc->ctl_lock);
2424 STAILQ_FOREACH(port, &softc->port_list, links) {
2425 struct ctl_port_entry entry, *list_entry;
2427 if (list->fill_num >= list->alloc_num) {
2428 list->dropped_num++;
2432 entry.port_type = port->port_type;
2433 strlcpy(entry.port_name, port->port_name,
2434 sizeof(entry.port_name));
2435 entry.targ_port = port->targ_port;
2436 entry.physical_port = port->physical_port;
2437 entry.virtual_port = port->virtual_port;
2438 entry.wwnn = port->wwnn;
2439 entry.wwpn = port->wwpn;
2440 if (port->status & CTL_PORT_STATUS_ONLINE)
2445 list_entry = &list->entries[i];
2447 retval = copyout(&entry, list_entry, sizeof(entry));
2449 printf("%s: CTL_GET_PORT_LIST: copyout "
2450 "returned %d\n", __func__, retval);
2455 list->fill_len += sizeof(entry);
2457 mtx_unlock(&softc->ctl_lock);
2460 * If this is non-zero, we had a copyout fault, so there's
2461 * probably no point in attempting to set the status inside
2467 if (list->dropped_num > 0)
2468 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2470 list->status = CTL_PORT_LIST_OK;
2473 case CTL_DUMP_OOA: {
2474 struct ctl_lun *lun;
2479 mtx_lock(&softc->ctl_lock);
2480 printf("Dumping OOA queues:\n");
2481 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2482 mtx_lock(&lun->lun_lock);
2483 for (io = (union ctl_io *)TAILQ_FIRST(
2484 &lun->ooa_queue); io != NULL;
2485 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2487 sbuf_new(&sb, printbuf, sizeof(printbuf),
2489 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2493 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2495 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2497 CTL_FLAG_ABORT) ? " ABORT" : "",
2499 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2500 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2502 printf("%s\n", sbuf_data(&sb));
2504 mtx_unlock(&lun->lun_lock);
2506 printf("OOA queues dump done\n");
2507 mtx_unlock(&softc->ctl_lock);
2511 struct ctl_lun *lun;
2512 struct ctl_ooa *ooa_hdr;
2513 struct ctl_ooa_entry *entries;
2514 uint32_t cur_fill_num;
2516 ooa_hdr = (struct ctl_ooa *)addr;
2518 if ((ooa_hdr->alloc_len == 0)
2519 || (ooa_hdr->alloc_num == 0)) {
2520 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2521 "must be non-zero\n", __func__,
2522 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2527 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2528 sizeof(struct ctl_ooa_entry))) {
2529 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2530 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2531 __func__, ooa_hdr->alloc_len,
2532 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2537 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2538 if (entries == NULL) {
2539 printf("%s: could not allocate %d bytes for OOA "
2540 "dump\n", __func__, ooa_hdr->alloc_len);
2545 mtx_lock(&softc->ctl_lock);
2546 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2547 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2548 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2549 mtx_unlock(&softc->ctl_lock);
2550 free(entries, M_CTL);
2551 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2552 __func__, (uintmax_t)ooa_hdr->lun_num);
2559 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2560 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2561 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2567 mtx_unlock(&softc->ctl_lock);
2568 free(entries, M_CTL);
2572 lun = softc->ctl_luns[ooa_hdr->lun_num];
2574 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2577 mtx_unlock(&softc->ctl_lock);
2579 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2580 ooa_hdr->fill_len = ooa_hdr->fill_num *
2581 sizeof(struct ctl_ooa_entry);
2582 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2584 printf("%s: error copying out %d bytes for OOA dump\n",
2585 __func__, ooa_hdr->fill_len);
2588 getbintime(&ooa_hdr->cur_bt);
2590 if (cur_fill_num > ooa_hdr->alloc_num) {
2591 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2592 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2594 ooa_hdr->dropped_num = 0;
2595 ooa_hdr->status = CTL_OOA_OK;
2598 free(entries, M_CTL);
2601 case CTL_CHECK_OOA: {
2603 struct ctl_lun *lun;
2604 struct ctl_ooa_info *ooa_info;
2607 ooa_info = (struct ctl_ooa_info *)addr;
2609 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2610 ooa_info->status = CTL_OOA_INVALID_LUN;
2613 mtx_lock(&softc->ctl_lock);
2614 lun = softc->ctl_luns[ooa_info->lun_id];
2616 mtx_unlock(&softc->ctl_lock);
2617 ooa_info->status = CTL_OOA_INVALID_LUN;
2620 mtx_lock(&lun->lun_lock);
2621 mtx_unlock(&softc->ctl_lock);
2622 ooa_info->num_entries = 0;
2623 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2624 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2625 &io->io_hdr, ooa_links)) {
2626 ooa_info->num_entries++;
2628 mtx_unlock(&lun->lun_lock);
2630 ooa_info->status = CTL_OOA_SUCCESS;
2634 case CTL_HARD_START:
2635 case CTL_HARD_STOP: {
2636 struct ctl_fe_ioctl_startstop_info ss_info;
2637 struct cfi_metatask *metatask;
2640 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2642 cv_init(&ss_info.sem, "hard start/stop cv" );
2644 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2645 if (metatask == NULL) {
2647 mtx_destroy(&hs_mtx);
2651 if (cmd == CTL_HARD_START)
2652 metatask->tasktype = CFI_TASK_STARTUP;
2654 metatask->tasktype = CFI_TASK_SHUTDOWN;
2656 metatask->callback = ctl_ioctl_hard_startstop_callback;
2657 metatask->callback_arg = &ss_info;
2659 cfi_action(metatask);
2661 /* Wait for the callback */
2663 cv_wait_sig(&ss_info.sem, &hs_mtx);
2664 mtx_unlock(&hs_mtx);
2667 * All information has been copied from the metatask by the
2668 * time cv_broadcast() is called, so we free the metatask here.
2670 cfi_free_metatask(metatask);
2672 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2674 mtx_destroy(&hs_mtx);
2678 struct ctl_bbrread_info *bbr_info;
2679 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2681 struct cfi_metatask *metatask;
2683 bbr_info = (struct ctl_bbrread_info *)addr;
2685 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2687 bzero(&bbr_mtx, sizeof(bbr_mtx));
2688 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2690 fe_bbr_info.bbr_info = bbr_info;
2691 fe_bbr_info.lock = &bbr_mtx;
2693 cv_init(&fe_bbr_info.sem, "BBR read cv");
2694 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2696 if (metatask == NULL) {
2697 mtx_destroy(&bbr_mtx);
2698 cv_destroy(&fe_bbr_info.sem);
2702 metatask->tasktype = CFI_TASK_BBRREAD;
2703 metatask->callback = ctl_ioctl_bbrread_callback;
2704 metatask->callback_arg = &fe_bbr_info;
2705 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2706 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2707 metatask->taskinfo.bbrread.len = bbr_info->len;
2709 cfi_action(metatask);
2712 while (fe_bbr_info.wakeup_done == 0)
2713 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2714 mtx_unlock(&bbr_mtx);
2716 bbr_info->status = metatask->status;
2717 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2718 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2719 memcpy(&bbr_info->sense_data,
2720 &metatask->taskinfo.bbrread.sense_data,
2721 ctl_min(sizeof(bbr_info->sense_data),
2722 sizeof(metatask->taskinfo.bbrread.sense_data)));
2724 cfi_free_metatask(metatask);
2726 mtx_destroy(&bbr_mtx);
2727 cv_destroy(&fe_bbr_info.sem);
2731 case CTL_DELAY_IO: {
2732 struct ctl_io_delay_info *delay_info;
2734 struct ctl_lun *lun;
2735 #endif /* CTL_IO_DELAY */
2737 delay_info = (struct ctl_io_delay_info *)addr;
2740 mtx_lock(&softc->ctl_lock);
2742 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2743 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2744 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2746 lun = softc->ctl_luns[delay_info->lun_id];
2747 mtx_lock(&lun->lun_lock);
2749 delay_info->status = CTL_DELAY_STATUS_OK;
2751 switch (delay_info->delay_type) {
2752 case CTL_DELAY_TYPE_CONT:
2754 case CTL_DELAY_TYPE_ONESHOT:
2757 delay_info->status =
2758 CTL_DELAY_STATUS_INVALID_TYPE;
2762 switch (delay_info->delay_loc) {
2763 case CTL_DELAY_LOC_DATAMOVE:
2764 lun->delay_info.datamove_type =
2765 delay_info->delay_type;
2766 lun->delay_info.datamove_delay =
2767 delay_info->delay_secs;
2769 case CTL_DELAY_LOC_DONE:
2770 lun->delay_info.done_type =
2771 delay_info->delay_type;
2772 lun->delay_info.done_delay =
2773 delay_info->delay_secs;
2776 delay_info->status =
2777 CTL_DELAY_STATUS_INVALID_LOC;
2780 mtx_unlock(&lun->lun_lock);
2783 mtx_unlock(&softc->ctl_lock);
2785 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2786 #endif /* CTL_IO_DELAY */
2789 case CTL_REALSYNC_SET: {
2792 syncstate = (int *)addr;
2794 mtx_lock(&softc->ctl_lock);
2795 switch (*syncstate) {
2797 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2800 softc->flags |= CTL_FLAG_REAL_SYNC;
2806 mtx_unlock(&softc->ctl_lock);
2809 case CTL_REALSYNC_GET: {
2812 syncstate = (int*)addr;
2814 mtx_lock(&softc->ctl_lock);
2815 if (softc->flags & CTL_FLAG_REAL_SYNC)
2819 mtx_unlock(&softc->ctl_lock);
2825 struct ctl_sync_info *sync_info;
2826 struct ctl_lun *lun;
2828 sync_info = (struct ctl_sync_info *)addr;
2830 mtx_lock(&softc->ctl_lock);
2831 lun = softc->ctl_luns[sync_info->lun_id];
2833 mtx_unlock(&softc->ctl_lock);
2834 sync_info->status = CTL_GS_SYNC_NO_LUN;
2837 * Get or set the sync interval. We're not bounds checking
2838 * in the set case, hopefully the user won't do something
2841 mtx_lock(&lun->lun_lock);
2842 mtx_unlock(&softc->ctl_lock);
2843 if (cmd == CTL_GETSYNC)
2844 sync_info->sync_interval = lun->sync_interval;
2846 lun->sync_interval = sync_info->sync_interval;
2847 mtx_unlock(&lun->lun_lock);
2849 sync_info->status = CTL_GS_SYNC_OK;
2853 case CTL_GETSTATS: {
2854 struct ctl_stats *stats;
2855 struct ctl_lun *lun;
2858 stats = (struct ctl_stats *)addr;
2860 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2862 stats->status = CTL_SS_NEED_MORE_SPACE;
2863 stats->num_luns = softc->num_luns;
2867 * XXX KDM no locking here. If the LUN list changes,
2868 * things can blow up.
2870 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2871 i++, lun = STAILQ_NEXT(lun, links)) {
2872 retval = copyout(&lun->stats, &stats->lun_stats[i],
2873 sizeof(lun->stats));
2877 stats->num_luns = softc->num_luns;
2878 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2880 stats->status = CTL_SS_OK;
2882 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2884 stats->flags = CTL_STATS_FLAG_NONE;
2886 getnanouptime(&stats->timestamp);
2889 case CTL_ERROR_INJECT: {
2890 struct ctl_error_desc *err_desc, *new_err_desc;
2891 struct ctl_lun *lun;
2893 err_desc = (struct ctl_error_desc *)addr;
2895 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2897 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2899 mtx_lock(&softc->ctl_lock);
2900 lun = softc->ctl_luns[err_desc->lun_id];
2902 mtx_unlock(&softc->ctl_lock);
2903 free(new_err_desc, M_CTL);
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;
4100 memcpy(&lun->mode_pages.index, page_index_template,
4101 sizeof(page_index_template));
4103 softc = lun->ctl_softc;
4105 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4107 page_index = &lun->mode_pages.index[i];
4109 * If this is a disk-only mode page, there's no point in
4110 * setting it up. For some pages, we have to have some
4111 * basic information about the disk in order to calculate the
4114 if ((lun->be_lun->lun_type != T_DIRECT)
4115 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4118 switch (page_index->page_code & SMPH_PC_MASK) {
4119 case SMS_FORMAT_DEVICE_PAGE: {
4120 struct scsi_format_page *format_page;
4122 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4123 panic("subpage is incorrect!");
4126 * Sectors per track are set above. Bytes per
4127 * sector need to be set here on a per-LUN basis.
4129 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4130 &format_page_default,
4131 sizeof(format_page_default));
4132 memcpy(&lun->mode_pages.format_page[
4133 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4134 sizeof(format_page_changeable));
4135 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4136 &format_page_default,
4137 sizeof(format_page_default));
4138 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4139 &format_page_default,
4140 sizeof(format_page_default));
4142 format_page = &lun->mode_pages.format_page[
4144 scsi_ulto2b(lun->be_lun->blocksize,
4145 format_page->bytes_per_sector);
4147 format_page = &lun->mode_pages.format_page[
4149 scsi_ulto2b(lun->be_lun->blocksize,
4150 format_page->bytes_per_sector);
4152 format_page = &lun->mode_pages.format_page[
4154 scsi_ulto2b(lun->be_lun->blocksize,
4155 format_page->bytes_per_sector);
4157 page_index->page_data =
4158 (uint8_t *)lun->mode_pages.format_page;
4161 case SMS_RIGID_DISK_PAGE: {
4162 struct scsi_rigid_disk_page *rigid_disk_page;
4163 uint32_t sectors_per_cylinder;
4167 #endif /* !__XSCALE__ */
4169 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4170 panic("invalid subpage value %d",
4171 page_index->subpage);
4174 * Rotation rate and sectors per track are set
4175 * above. We calculate the cylinders here based on
4176 * capacity. Due to the number of heads and
4177 * sectors per track we're using, smaller arrays
4178 * may turn out to have 0 cylinders. Linux and
4179 * FreeBSD don't pay attention to these mode pages
4180 * to figure out capacity, but Solaris does. It
4181 * seems to deal with 0 cylinders just fine, and
4182 * works out a fake geometry based on the capacity.
4184 memcpy(&lun->mode_pages.rigid_disk_page[
4185 CTL_PAGE_CURRENT], &rigid_disk_page_default,
4186 sizeof(rigid_disk_page_default));
4187 memcpy(&lun->mode_pages.rigid_disk_page[
4188 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4189 sizeof(rigid_disk_page_changeable));
4190 memcpy(&lun->mode_pages.rigid_disk_page[
4191 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4192 sizeof(rigid_disk_page_default));
4193 memcpy(&lun->mode_pages.rigid_disk_page[
4194 CTL_PAGE_SAVED], &rigid_disk_page_default,
4195 sizeof(rigid_disk_page_default));
4197 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4201 * The divide method here will be more accurate,
4202 * probably, but results in floating point being
4203 * used in the kernel on i386 (__udivdi3()). On the
4204 * XScale, though, __udivdi3() is implemented in
4207 * The shift method for cylinder calculation is
4208 * accurate if sectors_per_cylinder is a power of
4209 * 2. Otherwise it might be slightly off -- you
4210 * might have a bit of a truncation problem.
4213 cylinders = (lun->be_lun->maxlba + 1) /
4214 sectors_per_cylinder;
4216 for (shift = 31; shift > 0; shift--) {
4217 if (sectors_per_cylinder & (1 << shift))
4220 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4224 * We've basically got 3 bytes, or 24 bits for the
4225 * cylinder size in the mode page. If we're over,
4226 * just round down to 2^24.
4228 if (cylinders > 0xffffff)
4229 cylinders = 0xffffff;
4231 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4233 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4235 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4237 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4239 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4241 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4243 page_index->page_data =
4244 (uint8_t *)lun->mode_pages.rigid_disk_page;
4247 case SMS_CACHING_PAGE: {
4248 struct scsi_caching_page *caching_page;
4250 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4251 panic("invalid subpage value %d",
4252 page_index->subpage);
4253 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4254 &caching_page_default,
4255 sizeof(caching_page_default));
4256 memcpy(&lun->mode_pages.caching_page[
4257 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4258 sizeof(caching_page_changeable));
4259 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4260 &caching_page_default,
4261 sizeof(caching_page_default));
4262 caching_page = &lun->mode_pages.caching_page[
4264 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4265 if (value != NULL && strcmp(value, "off") == 0)
4266 caching_page->flags1 &= ~SCP_WCE;
4267 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4268 if (value != NULL && strcmp(value, "off") == 0)
4269 caching_page->flags1 |= SCP_RCD;
4270 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4271 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4272 sizeof(caching_page_default));
4273 page_index->page_data =
4274 (uint8_t *)lun->mode_pages.caching_page;
4277 case SMS_CONTROL_MODE_PAGE: {
4278 struct scsi_control_page *control_page;
4280 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4281 panic("invalid subpage value %d",
4282 page_index->subpage);
4284 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4285 &control_page_default,
4286 sizeof(control_page_default));
4287 memcpy(&lun->mode_pages.control_page[
4288 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4289 sizeof(control_page_changeable));
4290 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4291 &control_page_default,
4292 sizeof(control_page_default));
4293 control_page = &lun->mode_pages.control_page[
4295 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4296 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4297 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4298 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4300 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4301 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4302 sizeof(control_page_default));
4303 page_index->page_data =
4304 (uint8_t *)lun->mode_pages.control_page;
4308 case SMS_VENDOR_SPECIFIC_PAGE:{
4309 switch (page_index->subpage) {
4310 case PWR_SUBPAGE_CODE: {
4311 struct copan_power_subpage *current_page,
4314 memcpy(&lun->mode_pages.power_subpage[
4316 &power_page_default,
4317 sizeof(power_page_default));
4318 memcpy(&lun->mode_pages.power_subpage[
4319 CTL_PAGE_CHANGEABLE],
4320 &power_page_changeable,
4321 sizeof(power_page_changeable));
4322 memcpy(&lun->mode_pages.power_subpage[
4324 &power_page_default,
4325 sizeof(power_page_default));
4326 memcpy(&lun->mode_pages.power_subpage[
4328 &power_page_default,
4329 sizeof(power_page_default));
4330 page_index->page_data =
4331 (uint8_t *)lun->mode_pages.power_subpage;
4333 current_page = (struct copan_power_subpage *)
4334 (page_index->page_data +
4335 (page_index->page_len *
4337 saved_page = (struct copan_power_subpage *)
4338 (page_index->page_data +
4339 (page_index->page_len *
4343 case APS_SUBPAGE_CODE: {
4344 struct copan_aps_subpage *current_page,
4347 // This gets set multiple times but
4348 // it should always be the same. It's
4349 // only done during init so who cares.
4350 index_to_aps_page = i;
4352 memcpy(&lun->mode_pages.aps_subpage[
4355 sizeof(aps_page_default));
4356 memcpy(&lun->mode_pages.aps_subpage[
4357 CTL_PAGE_CHANGEABLE],
4358 &aps_page_changeable,
4359 sizeof(aps_page_changeable));
4360 memcpy(&lun->mode_pages.aps_subpage[
4363 sizeof(aps_page_default));
4364 memcpy(&lun->mode_pages.aps_subpage[
4367 sizeof(aps_page_default));
4368 page_index->page_data =
4369 (uint8_t *)lun->mode_pages.aps_subpage;
4371 current_page = (struct copan_aps_subpage *)
4372 (page_index->page_data +
4373 (page_index->page_len *
4375 saved_page = (struct copan_aps_subpage *)
4376 (page_index->page_data +
4377 (page_index->page_len *
4381 case DBGCNF_SUBPAGE_CODE: {
4382 struct copan_debugconf_subpage *current_page,
4385 memcpy(&lun->mode_pages.debugconf_subpage[
4387 &debugconf_page_default,
4388 sizeof(debugconf_page_default));
4389 memcpy(&lun->mode_pages.debugconf_subpage[
4390 CTL_PAGE_CHANGEABLE],
4391 &debugconf_page_changeable,
4392 sizeof(debugconf_page_changeable));
4393 memcpy(&lun->mode_pages.debugconf_subpage[
4395 &debugconf_page_default,
4396 sizeof(debugconf_page_default));
4397 memcpy(&lun->mode_pages.debugconf_subpage[
4399 &debugconf_page_default,
4400 sizeof(debugconf_page_default));
4401 page_index->page_data =
4402 (uint8_t *)lun->mode_pages.debugconf_subpage;
4404 current_page = (struct copan_debugconf_subpage *)
4405 (page_index->page_data +
4406 (page_index->page_len *
4408 saved_page = (struct copan_debugconf_subpage *)
4409 (page_index->page_data +
4410 (page_index->page_len *
4415 panic("invalid subpage value %d",
4416 page_index->subpage);
4422 panic("invalid page value %d",
4423 page_index->page_code & SMPH_PC_MASK);
4428 return (CTL_RETVAL_COMPLETE);
4435 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4436 * wants us to allocate the LUN and he can block.
4437 * - ctl_softc is always set
4438 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4440 * Returns 0 for success, non-zero (errno) for failure.
4443 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4444 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4446 struct ctl_lun *nlun, *lun;
4447 struct ctl_port *port;
4448 struct scsi_vpd_id_descriptor *desc;
4449 struct scsi_vpd_id_t10 *t10id;
4450 const char *eui, *naa, *scsiname, *vendor;
4451 int lun_number, i, lun_malloced;
4452 int devidlen, idlen1, idlen2 = 0, len;
4458 * We currently only support Direct Access or Processor LUN types.
4460 switch (be_lun->lun_type) {
4468 be_lun->lun_config_status(be_lun->be_lun,
4469 CTL_LUN_CONFIG_FAILURE);
4472 if (ctl_lun == NULL) {
4473 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4480 memset(lun, 0, sizeof(*lun));
4482 lun->flags = CTL_LUN_MALLOCED;
4484 /* Generate LUN ID. */
4485 devidlen = max(CTL_DEVID_MIN_LEN,
4486 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4487 idlen1 = sizeof(*t10id) + devidlen;
4488 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4489 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4490 if (scsiname != NULL) {
4491 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4492 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4494 eui = ctl_get_opt(&be_lun->options, "eui");
4496 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
4498 naa = ctl_get_opt(&be_lun->options, "naa");
4500 len += sizeof(struct scsi_vpd_id_descriptor) + 8;
4502 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4503 M_CTL, M_WAITOK | M_ZERO);
4504 lun->lun_devid->len = len;
4505 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4506 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4507 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4508 desc->length = idlen1;
4509 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4510 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4511 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4512 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4514 strncpy(t10id->vendor, vendor,
4515 min(sizeof(t10id->vendor), strlen(vendor)));
4517 strncpy((char *)t10id->vendor_spec_id,
4518 (char *)be_lun->device_id, devidlen);
4519 if (scsiname != NULL) {
4520 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4522 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4523 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4524 SVPD_ID_TYPE_SCSI_NAME;
4525 desc->length = idlen2;
4526 strlcpy(desc->identifier, scsiname, idlen2);
4529 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4531 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4532 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4535 scsi_u64to8b(strtouq(eui, NULL, 0), desc->identifier);
4538 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4540 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4541 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4544 scsi_u64to8b(strtouq(naa, NULL, 0), desc->identifier);
4547 mtx_lock(&ctl_softc->ctl_lock);
4549 * See if the caller requested a particular LUN number. If so, see
4550 * if it is available. Otherwise, allocate the first available LUN.
4552 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4553 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4554 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4555 mtx_unlock(&ctl_softc->ctl_lock);
4556 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4557 printf("ctl: requested LUN ID %d is higher "
4558 "than CTL_MAX_LUNS - 1 (%d)\n",
4559 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4562 * XXX KDM return an error, or just assign
4563 * another LUN ID in this case??
4565 printf("ctl: requested LUN ID %d is already "
4566 "in use\n", be_lun->req_lun_id);
4568 if (lun->flags & CTL_LUN_MALLOCED)
4570 be_lun->lun_config_status(be_lun->be_lun,
4571 CTL_LUN_CONFIG_FAILURE);
4574 lun_number = be_lun->req_lun_id;
4576 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4577 if (lun_number == -1) {
4578 mtx_unlock(&ctl_softc->ctl_lock);
4579 printf("ctl: can't allocate LUN on target %ju, out of "
4580 "LUNs\n", (uintmax_t)target_id.id);
4581 if (lun->flags & CTL_LUN_MALLOCED)
4583 be_lun->lun_config_status(be_lun->be_lun,
4584 CTL_LUN_CONFIG_FAILURE);
4588 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4590 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4591 lun->target = target_id;
4592 lun->lun = lun_number;
4593 lun->be_lun = be_lun;
4595 * The processor LUN is always enabled. Disk LUNs come on line
4596 * disabled, and must be enabled by the backend.
4598 lun->flags |= CTL_LUN_DISABLED;
4599 lun->backend = be_lun->be;
4600 be_lun->ctl_lun = lun;
4601 be_lun->lun_id = lun_number;
4602 atomic_add_int(&be_lun->be->num_luns, 1);
4603 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4604 lun->flags |= CTL_LUN_STOPPED;
4606 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4607 lun->flags |= CTL_LUN_INOPERABLE;
4609 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4610 lun->flags |= CTL_LUN_PRIMARY_SC;
4612 lun->ctl_softc = ctl_softc;
4613 TAILQ_INIT(&lun->ooa_queue);
4614 TAILQ_INIT(&lun->blocked_queue);
4615 STAILQ_INIT(&lun->error_list);
4616 ctl_tpc_lun_init(lun);
4619 * Initialize the mode page index.
4621 ctl_init_page_index(lun);
4624 * Set the poweron UA for all initiators on this LUN only.
4626 for (i = 0; i < CTL_MAX_INITIATORS; i++)
4627 lun->pending_ua[i] = CTL_UA_POWERON;
4630 * Now, before we insert this lun on the lun list, set the lun
4631 * inventory changed UA for all other luns.
4633 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4634 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4635 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4639 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4641 ctl_softc->ctl_luns[lun_number] = lun;
4643 ctl_softc->num_luns++;
4645 /* Setup statistics gathering */
4646 lun->stats.device_type = be_lun->lun_type;
4647 lun->stats.lun_number = lun_number;
4648 if (lun->stats.device_type == T_DIRECT)
4649 lun->stats.blocksize = be_lun->blocksize;
4651 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4652 for (i = 0;i < CTL_MAX_PORTS;i++)
4653 lun->stats.ports[i].targ_port = i;
4655 mtx_unlock(&ctl_softc->ctl_lock);
4657 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4660 * Run through each registered FETD and bring it online if it isn't
4661 * already. Enable the target ID if it hasn't been enabled, and
4662 * enable this particular LUN.
4664 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4667 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4669 printf("ctl_alloc_lun: FETD %s port %d returned error "
4670 "%d for lun_enable on target %ju lun %d\n",
4671 port->port_name, port->targ_port, retval,
4672 (uintmax_t)target_id.id, lun_number);
4674 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4682 * - LUN has already been marked invalid and any pending I/O has been taken
4686 ctl_free_lun(struct ctl_lun *lun)
4688 struct ctl_softc *softc;
4690 struct ctl_port *port;
4692 struct ctl_lun *nlun;
4695 softc = lun->ctl_softc;
4697 mtx_assert(&softc->ctl_lock, MA_OWNED);
4699 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4701 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4703 softc->ctl_luns[lun->lun] = NULL;
4705 if (!TAILQ_EMPTY(&lun->ooa_queue))
4706 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4711 * XXX KDM this scheme only works for a single target/multiple LUN
4712 * setup. It needs to be revamped for a multiple target scheme.
4714 * XXX KDM this results in port->lun_disable() getting called twice,
4715 * once when ctl_disable_lun() is called, and a second time here.
4716 * We really need to re-think the LUN disable semantics. There
4717 * should probably be several steps/levels to LUN removal:
4722 * Right now we only have a disable method when communicating to
4723 * the front end ports, at least for individual LUNs.
4726 STAILQ_FOREACH(port, &softc->port_list, links) {
4729 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4732 printf("ctl_free_lun: FETD %s port %d returned error "
4733 "%d for lun_disable on target %ju lun %jd\n",
4734 port->port_name, port->targ_port, retval,
4735 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4738 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4739 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4741 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4743 printf("ctl_free_lun: FETD %s port %d "
4744 "returned error %d for targ_disable on "
4745 "target %ju\n", port->port_name,
4746 port->targ_port, retval,
4747 (uintmax_t)lun->target.id);
4749 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4751 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4755 port->port_offline(port->onoff_arg);
4756 port->status &= ~CTL_PORT_STATUS_ONLINE;
4763 * Tell the backend to free resources, if this LUN has a backend.
4765 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4766 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4768 ctl_tpc_lun_shutdown(lun);
4769 mtx_destroy(&lun->lun_lock);
4770 free(lun->lun_devid, M_CTL);
4771 if (lun->flags & CTL_LUN_MALLOCED)
4774 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4775 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
4776 nlun->pending_ua[i] |= CTL_UA_LUN_CHANGE;
4784 ctl_create_lun(struct ctl_be_lun *be_lun)
4786 struct ctl_softc *ctl_softc;
4788 ctl_softc = control_softc;
4791 * ctl_alloc_lun() should handle all potential failure cases.
4793 ctl_alloc_lun(ctl_softc, NULL, be_lun, ctl_softc->target);
4797 ctl_add_lun(struct ctl_be_lun *be_lun)
4799 struct ctl_softc *ctl_softc = control_softc;
4801 mtx_lock(&ctl_softc->ctl_lock);
4802 STAILQ_INSERT_TAIL(&ctl_softc->pending_lun_queue, be_lun, links);
4803 mtx_unlock(&ctl_softc->ctl_lock);
4804 wakeup(&ctl_softc->pending_lun_queue);
4810 ctl_enable_lun(struct ctl_be_lun *be_lun)
4812 struct ctl_softc *ctl_softc;
4813 struct ctl_port *port, *nport;
4814 struct ctl_lun *lun;
4817 ctl_softc = control_softc;
4819 lun = (struct ctl_lun *)be_lun->ctl_lun;
4821 mtx_lock(&ctl_softc->ctl_lock);
4822 mtx_lock(&lun->lun_lock);
4823 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4825 * eh? Why did we get called if the LUN is already
4828 mtx_unlock(&lun->lun_lock);
4829 mtx_unlock(&ctl_softc->ctl_lock);
4832 lun->flags &= ~CTL_LUN_DISABLED;
4833 mtx_unlock(&lun->lun_lock);
4835 for (port = STAILQ_FIRST(&ctl_softc->port_list); port != NULL; port = nport) {
4836 nport = STAILQ_NEXT(port, links);
4839 * Drop the lock while we call the FETD's enable routine.
4840 * This can lead to a callback into CTL (at least in the
4841 * case of the internal initiator frontend.
4843 mtx_unlock(&ctl_softc->ctl_lock);
4844 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4845 mtx_lock(&ctl_softc->ctl_lock);
4847 printf("%s: FETD %s port %d returned error "
4848 "%d for lun_enable on target %ju lun %jd\n",
4849 __func__, port->port_name, port->targ_port, retval,
4850 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4854 /* NOTE: TODO: why does lun enable affect port status? */
4855 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4860 mtx_unlock(&ctl_softc->ctl_lock);
4866 ctl_disable_lun(struct ctl_be_lun *be_lun)
4868 struct ctl_softc *ctl_softc;
4869 struct ctl_port *port;
4870 struct ctl_lun *lun;
4873 ctl_softc = control_softc;
4875 lun = (struct ctl_lun *)be_lun->ctl_lun;
4877 mtx_lock(&ctl_softc->ctl_lock);
4878 mtx_lock(&lun->lun_lock);
4879 if (lun->flags & CTL_LUN_DISABLED) {
4880 mtx_unlock(&lun->lun_lock);
4881 mtx_unlock(&ctl_softc->ctl_lock);
4884 lun->flags |= CTL_LUN_DISABLED;
4885 mtx_unlock(&lun->lun_lock);
4887 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4888 mtx_unlock(&ctl_softc->ctl_lock);
4890 * Drop the lock before we call the frontend's disable
4891 * routine, to avoid lock order reversals.
4893 * XXX KDM what happens if the frontend list changes while
4894 * we're traversing it? It's unlikely, but should be handled.
4896 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4898 mtx_lock(&ctl_softc->ctl_lock);
4900 printf("ctl_alloc_lun: FETD %s port %d returned error "
4901 "%d for lun_disable on target %ju lun %jd\n",
4902 port->port_name, port->targ_port, retval,
4903 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4907 mtx_unlock(&ctl_softc->ctl_lock);
4913 ctl_start_lun(struct ctl_be_lun *be_lun)
4915 struct ctl_softc *ctl_softc;
4916 struct ctl_lun *lun;
4918 ctl_softc = control_softc;
4920 lun = (struct ctl_lun *)be_lun->ctl_lun;
4922 mtx_lock(&lun->lun_lock);
4923 lun->flags &= ~CTL_LUN_STOPPED;
4924 mtx_unlock(&lun->lun_lock);
4930 ctl_stop_lun(struct ctl_be_lun *be_lun)
4932 struct ctl_softc *ctl_softc;
4933 struct ctl_lun *lun;
4935 ctl_softc = control_softc;
4937 lun = (struct ctl_lun *)be_lun->ctl_lun;
4939 mtx_lock(&lun->lun_lock);
4940 lun->flags |= CTL_LUN_STOPPED;
4941 mtx_unlock(&lun->lun_lock);
4947 ctl_lun_offline(struct ctl_be_lun *be_lun)
4949 struct ctl_softc *ctl_softc;
4950 struct ctl_lun *lun;
4952 ctl_softc = control_softc;
4954 lun = (struct ctl_lun *)be_lun->ctl_lun;
4956 mtx_lock(&lun->lun_lock);
4957 lun->flags |= CTL_LUN_OFFLINE;
4958 mtx_unlock(&lun->lun_lock);
4964 ctl_lun_online(struct ctl_be_lun *be_lun)
4966 struct ctl_softc *ctl_softc;
4967 struct ctl_lun *lun;
4969 ctl_softc = control_softc;
4971 lun = (struct ctl_lun *)be_lun->ctl_lun;
4973 mtx_lock(&lun->lun_lock);
4974 lun->flags &= ~CTL_LUN_OFFLINE;
4975 mtx_unlock(&lun->lun_lock);
4981 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4983 struct ctl_softc *ctl_softc;
4984 struct ctl_lun *lun;
4986 ctl_softc = control_softc;
4988 lun = (struct ctl_lun *)be_lun->ctl_lun;
4990 mtx_lock(&lun->lun_lock);
4993 * The LUN needs to be disabled before it can be marked invalid.
4995 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4996 mtx_unlock(&lun->lun_lock);
5000 * Mark the LUN invalid.
5002 lun->flags |= CTL_LUN_INVALID;
5005 * If there is nothing in the OOA queue, go ahead and free the LUN.
5006 * If we have something in the OOA queue, we'll free it when the
5007 * last I/O completes.
5009 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5010 mtx_unlock(&lun->lun_lock);
5011 mtx_lock(&ctl_softc->ctl_lock);
5013 mtx_unlock(&ctl_softc->ctl_lock);
5015 mtx_unlock(&lun->lun_lock);
5021 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
5023 struct ctl_softc *ctl_softc;
5024 struct ctl_lun *lun;
5026 ctl_softc = control_softc;
5027 lun = (struct ctl_lun *)be_lun->ctl_lun;
5029 mtx_lock(&lun->lun_lock);
5030 lun->flags |= CTL_LUN_INOPERABLE;
5031 mtx_unlock(&lun->lun_lock);
5037 ctl_lun_operable(struct ctl_be_lun *be_lun)
5039 struct ctl_softc *ctl_softc;
5040 struct ctl_lun *lun;
5042 ctl_softc = control_softc;
5043 lun = (struct ctl_lun *)be_lun->ctl_lun;
5045 mtx_lock(&lun->lun_lock);
5046 lun->flags &= ~CTL_LUN_INOPERABLE;
5047 mtx_unlock(&lun->lun_lock);
5053 ctl_lun_power_lock(struct ctl_be_lun *be_lun, struct ctl_nexus *nexus,
5056 struct ctl_softc *softc;
5057 struct ctl_lun *lun;
5058 struct copan_aps_subpage *current_sp;
5059 struct ctl_page_index *page_index;
5062 softc = control_softc;
5064 mtx_lock(&softc->ctl_lock);
5066 lun = (struct ctl_lun *)be_lun->ctl_lun;
5067 mtx_lock(&lun->lun_lock);
5070 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5071 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
5075 if (lun->mode_pages.index[i].subpage != APS_SUBPAGE_CODE)
5077 page_index = &lun->mode_pages.index[i];
5080 if (page_index == NULL) {
5081 mtx_unlock(&lun->lun_lock);
5082 mtx_unlock(&softc->ctl_lock);
5083 printf("%s: APS subpage not found for lun %ju!\n", __func__,
5084 (uintmax_t)lun->lun);
5088 if ((softc->aps_locked_lun != 0)
5089 && (softc->aps_locked_lun != lun->lun)) {
5090 printf("%s: attempt to lock LUN %llu when %llu is already "
5092 mtx_unlock(&lun->lun_lock);
5093 mtx_unlock(&softc->ctl_lock);
5098 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
5099 (page_index->page_len * CTL_PAGE_CURRENT));
5102 current_sp->lock_active = APS_LOCK_ACTIVE;
5103 softc->aps_locked_lun = lun->lun;
5105 current_sp->lock_active = 0;
5106 softc->aps_locked_lun = 0;
5111 * If we're in HA mode, try to send the lock message to the other
5114 if (ctl_is_single == 0) {
5116 union ctl_ha_msg lock_msg;
5118 lock_msg.hdr.nexus = *nexus;
5119 lock_msg.hdr.msg_type = CTL_MSG_APS_LOCK;
5121 lock_msg.aps.lock_flag = 1;
5123 lock_msg.aps.lock_flag = 0;
5124 isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &lock_msg,
5125 sizeof(lock_msg), 0);
5126 if (isc_retval > CTL_HA_STATUS_SUCCESS) {
5127 printf("%s: APS (lock=%d) error returned from "
5128 "ctl_ha_msg_send: %d\n", __func__, lock, isc_retval);
5129 mtx_unlock(&lun->lun_lock);
5130 mtx_unlock(&softc->ctl_lock);
5135 mtx_unlock(&lun->lun_lock);
5136 mtx_unlock(&softc->ctl_lock);
5142 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5144 struct ctl_lun *lun;
5145 struct ctl_softc *softc;
5148 softc = control_softc;
5150 lun = (struct ctl_lun *)be_lun->ctl_lun;
5152 mtx_lock(&lun->lun_lock);
5154 for (i = 0; i < CTL_MAX_INITIATORS; i++)
5155 lun->pending_ua[i] |= CTL_UA_CAPACITY_CHANGED;
5157 mtx_unlock(&lun->lun_lock);
5161 * Backend "memory move is complete" callback for requests that never
5162 * make it down to say RAIDCore's configuration code.
5165 ctl_config_move_done(union ctl_io *io)
5169 retval = CTL_RETVAL_COMPLETE;
5172 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5174 * XXX KDM this shouldn't happen, but what if it does?
5176 if (io->io_hdr.io_type != CTL_IO_SCSI)
5177 panic("I/O type isn't CTL_IO_SCSI!");
5179 if ((io->io_hdr.port_status == 0)
5180 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5181 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE))
5182 io->io_hdr.status = CTL_SUCCESS;
5183 else if ((io->io_hdr.port_status != 0)
5184 && ((io->io_hdr.flags & CTL_FLAG_ABORT) == 0)
5185 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)){
5187 * For hardware error sense keys, the sense key
5188 * specific value is defined to be a retry count,
5189 * but we use it to pass back an internal FETD
5190 * error code. XXX KDM Hopefully the FETD is only
5191 * using 16 bits for an error code, since that's
5192 * all the space we have in the sks field.
5194 ctl_set_internal_failure(&io->scsiio,
5197 io->io_hdr.port_status);
5198 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5199 free(io->scsiio.kern_data_ptr, M_CTL);
5204 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
5205 || ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
5206 || ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5208 * XXX KDM just assuming a single pointer here, and not a
5209 * S/G list. If we start using S/G lists for config data,
5210 * we'll need to know how to clean them up here as well.
5212 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5213 free(io->scsiio.kern_data_ptr, M_CTL);
5214 /* Hopefully the user has already set the status... */
5218 * XXX KDM now we need to continue data movement. Some
5220 * - call ctl_scsiio() again? We don't do this for data
5221 * writes, because for those at least we know ahead of
5222 * time where the write will go and how long it is. For
5223 * config writes, though, that information is largely
5224 * contained within the write itself, thus we need to
5225 * parse out the data again.
5227 * - Call some other function once the data is in?
5231 * XXX KDM call ctl_scsiio() again for now, and check flag
5232 * bits to see whether we're allocated or not.
5234 retval = ctl_scsiio(&io->scsiio);
5241 * This gets called by a backend driver when it is done with a
5242 * data_submit method.
5245 ctl_data_submit_done(union ctl_io *io)
5248 * If the IO_CONT flag is set, we need to call the supplied
5249 * function to continue processing the I/O, instead of completing
5252 * If there is an error, though, we don't want to keep processing.
5253 * Instead, just send status back to the initiator.
5255 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5256 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5257 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5258 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5259 io->scsiio.io_cont(io);
5266 * This gets called by a backend driver when it is done with a
5267 * configuration write.
5270 ctl_config_write_done(union ctl_io *io)
5275 * If the IO_CONT flag is set, we need to call the supplied
5276 * function to continue processing the I/O, instead of completing
5279 * If there is an error, though, we don't want to keep processing.
5280 * Instead, just send status back to the initiator.
5282 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT)
5283 && (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE)
5284 || ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))) {
5285 io->scsiio.io_cont(io);
5289 * Since a configuration write can be done for commands that actually
5290 * have data allocated, like write buffer, and commands that have
5291 * no data, like start/stop unit, we need to check here.
5293 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5294 buf = io->scsiio.kern_data_ptr;
5303 * SCSI release command.
5306 ctl_scsi_release(struct ctl_scsiio *ctsio)
5308 int length, longid, thirdparty_id, resv_id;
5309 struct ctl_softc *ctl_softc;
5310 struct ctl_lun *lun;
5315 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5317 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5318 ctl_softc = control_softc;
5320 switch (ctsio->cdb[0]) {
5322 struct scsi_release_10 *cdb;
5324 cdb = (struct scsi_release_10 *)ctsio->cdb;
5326 if (cdb->byte2 & SR10_LONGID)
5329 thirdparty_id = cdb->thirdparty_id;
5331 resv_id = cdb->resv_id;
5332 length = scsi_2btoul(cdb->length);
5339 * XXX KDM right now, we only support LUN reservation. We don't
5340 * support 3rd party reservations, or extent reservations, which
5341 * might actually need the parameter list. If we've gotten this
5342 * far, we've got a LUN reservation. Anything else got kicked out
5343 * above. So, according to SPC, ignore the length.
5347 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5349 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5350 ctsio->kern_data_len = length;
5351 ctsio->kern_total_len = length;
5352 ctsio->kern_data_resid = 0;
5353 ctsio->kern_rel_offset = 0;
5354 ctsio->kern_sg_entries = 0;
5355 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5356 ctsio->be_move_done = ctl_config_move_done;
5357 ctl_datamove((union ctl_io *)ctsio);
5359 return (CTL_RETVAL_COMPLETE);
5363 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5365 mtx_lock(&lun->lun_lock);
5368 * According to SPC, it is not an error for an intiator to attempt
5369 * to release a reservation on a LUN that isn't reserved, or that
5370 * is reserved by another initiator. The reservation can only be
5371 * released, though, by the initiator who made it or by one of
5372 * several reset type events.
5374 if (lun->flags & CTL_LUN_RESERVED) {
5375 if ((ctsio->io_hdr.nexus.initid.id == lun->rsv_nexus.initid.id)
5376 && (ctsio->io_hdr.nexus.targ_port == lun->rsv_nexus.targ_port)
5377 && (ctsio->io_hdr.nexus.targ_target.id ==
5378 lun->rsv_nexus.targ_target.id)) {
5379 lun->flags &= ~CTL_LUN_RESERVED;
5383 mtx_unlock(&lun->lun_lock);
5385 ctsio->scsi_status = SCSI_STATUS_OK;
5386 ctsio->io_hdr.status = CTL_SUCCESS;
5388 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5389 free(ctsio->kern_data_ptr, M_CTL);
5390 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5393 ctl_done((union ctl_io *)ctsio);
5394 return (CTL_RETVAL_COMPLETE);
5398 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5400 int extent, thirdparty, longid;
5401 int resv_id, length;
5402 uint64_t thirdparty_id;
5403 struct ctl_softc *ctl_softc;
5404 struct ctl_lun *lun;
5413 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5415 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5416 ctl_softc = control_softc;
5418 switch (ctsio->cdb[0]) {
5420 struct scsi_reserve_10 *cdb;
5422 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5424 if (cdb->byte2 & SR10_LONGID)
5427 thirdparty_id = cdb->thirdparty_id;
5429 resv_id = cdb->resv_id;
5430 length = scsi_2btoul(cdb->length);
5436 * XXX KDM right now, we only support LUN reservation. We don't
5437 * support 3rd party reservations, or extent reservations, which
5438 * might actually need the parameter list. If we've gotten this
5439 * far, we've got a LUN reservation. Anything else got kicked out
5440 * above. So, according to SPC, ignore the length.
5444 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5446 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5447 ctsio->kern_data_len = length;
5448 ctsio->kern_total_len = length;
5449 ctsio->kern_data_resid = 0;
5450 ctsio->kern_rel_offset = 0;
5451 ctsio->kern_sg_entries = 0;
5452 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5453 ctsio->be_move_done = ctl_config_move_done;
5454 ctl_datamove((union ctl_io *)ctsio);
5456 return (CTL_RETVAL_COMPLETE);
5460 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5462 mtx_lock(&lun->lun_lock);
5463 if (lun->flags & CTL_LUN_RESERVED) {
5464 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
5465 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
5466 || (ctsio->io_hdr.nexus.targ_target.id !=
5467 lun->rsv_nexus.targ_target.id)) {
5468 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
5469 ctsio->io_hdr.status = CTL_SCSI_ERROR;
5474 lun->flags |= CTL_LUN_RESERVED;
5475 lun->rsv_nexus = ctsio->io_hdr.nexus;
5477 ctsio->scsi_status = SCSI_STATUS_OK;
5478 ctsio->io_hdr.status = CTL_SUCCESS;
5481 mtx_unlock(&lun->lun_lock);
5483 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5484 free(ctsio->kern_data_ptr, M_CTL);
5485 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5488 ctl_done((union ctl_io *)ctsio);
5489 return (CTL_RETVAL_COMPLETE);
5493 ctl_start_stop(struct ctl_scsiio *ctsio)
5495 struct scsi_start_stop_unit *cdb;
5496 struct ctl_lun *lun;
5497 struct ctl_softc *ctl_softc;
5500 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5502 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5503 ctl_softc = control_softc;
5506 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5510 * We don't support the immediate bit on a stop unit. In order to
5511 * do that, we would need to code up a way to know that a stop is
5512 * pending, and hold off any new commands until it completes, one
5513 * way or another. Then we could accept or reject those commands
5514 * depending on its status. We would almost need to do the reverse
5515 * of what we do below for an immediate start -- return the copy of
5516 * the ctl_io to the FETD with status to send to the host (and to
5517 * free the copy!) and then free the original I/O once the stop
5518 * actually completes. That way, the OOA queue mechanism can work
5519 * to block commands that shouldn't proceed. Another alternative
5520 * would be to put the copy in the queue in place of the original,
5521 * and return the original back to the caller. That could be
5524 if ((cdb->byte2 & SSS_IMMED)
5525 && ((cdb->how & SSS_START) == 0)) {
5526 ctl_set_invalid_field(ctsio,
5532 ctl_done((union ctl_io *)ctsio);
5533 return (CTL_RETVAL_COMPLETE);
5536 if ((lun->flags & CTL_LUN_PR_RESERVED)
5537 && ((cdb->how & SSS_START)==0)) {
5540 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5541 if (!lun->per_res[residx].registered
5542 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5544 ctl_set_reservation_conflict(ctsio);
5545 ctl_done((union ctl_io *)ctsio);
5546 return (CTL_RETVAL_COMPLETE);
5551 * If there is no backend on this device, we can't start or stop
5552 * it. In theory we shouldn't get any start/stop commands in the
5553 * first place at this level if the LUN doesn't have a backend.
5554 * That should get stopped by the command decode code.
5556 if (lun->backend == NULL) {
5557 ctl_set_invalid_opcode(ctsio);
5558 ctl_done((union ctl_io *)ctsio);
5559 return (CTL_RETVAL_COMPLETE);
5563 * XXX KDM Copan-specific offline behavior.
5564 * Figure out a reasonable way to port this?
5567 mtx_lock(&lun->lun_lock);
5569 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5570 && (lun->flags & CTL_LUN_OFFLINE)) {
5572 * If the LUN is offline, and the on/offline bit isn't set,
5573 * reject the start or stop. Otherwise, let it through.
5575 mtx_unlock(&lun->lun_lock);
5576 ctl_set_lun_not_ready(ctsio);
5577 ctl_done((union ctl_io *)ctsio);
5579 mtx_unlock(&lun->lun_lock);
5580 #endif /* NEEDTOPORT */
5582 * This could be a start or a stop when we're online,
5583 * or a stop/offline or start/online. A start or stop when
5584 * we're offline is covered in the case above.
5587 * In the non-immediate case, we send the request to
5588 * the backend and return status to the user when
5591 * In the immediate case, we allocate a new ctl_io
5592 * to hold a copy of the request, and send that to
5593 * the backend. We then set good status on the
5594 * user's request and return it immediately.
5596 if (cdb->byte2 & SSS_IMMED) {
5597 union ctl_io *new_io;
5599 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5600 if (new_io == NULL) {
5601 ctl_set_busy(ctsio);
5602 ctl_done((union ctl_io *)ctsio);
5604 ctl_copy_io((union ctl_io *)ctsio,
5606 retval = lun->backend->config_write(new_io);
5607 ctl_set_success(ctsio);
5608 ctl_done((union ctl_io *)ctsio);
5611 retval = lun->backend->config_write(
5612 (union ctl_io *)ctsio);
5621 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5622 * we don't really do anything with the LBA and length fields if the user
5623 * passes them in. Instead we'll just flush out the cache for the entire
5627 ctl_sync_cache(struct ctl_scsiio *ctsio)
5629 struct ctl_lun *lun;
5630 struct ctl_softc *ctl_softc;
5631 uint64_t starting_lba;
5632 uint32_t block_count;
5635 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5637 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5638 ctl_softc = control_softc;
5641 switch (ctsio->cdb[0]) {
5642 case SYNCHRONIZE_CACHE: {
5643 struct scsi_sync_cache *cdb;
5644 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5646 starting_lba = scsi_4btoul(cdb->begin_lba);
5647 block_count = scsi_2btoul(cdb->lb_count);
5650 case SYNCHRONIZE_CACHE_16: {
5651 struct scsi_sync_cache_16 *cdb;
5652 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5654 starting_lba = scsi_8btou64(cdb->begin_lba);
5655 block_count = scsi_4btoul(cdb->lb_count);
5659 ctl_set_invalid_opcode(ctsio);
5660 ctl_done((union ctl_io *)ctsio);
5662 break; /* NOTREACHED */
5666 * We check the LBA and length, but don't do anything with them.
5667 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5668 * get flushed. This check will just help satisfy anyone who wants
5669 * to see an error for an out of range LBA.
5671 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5672 ctl_set_lba_out_of_range(ctsio);
5673 ctl_done((union ctl_io *)ctsio);
5678 * If this LUN has no backend, we can't flush the cache anyway.
5680 if (lun->backend == NULL) {
5681 ctl_set_invalid_opcode(ctsio);
5682 ctl_done((union ctl_io *)ctsio);
5687 * Check to see whether we're configured to send the SYNCHRONIZE
5688 * CACHE command directly to the back end.
5690 mtx_lock(&lun->lun_lock);
5691 if ((ctl_softc->flags & CTL_FLAG_REAL_SYNC)
5692 && (++(lun->sync_count) >= lun->sync_interval)) {
5693 lun->sync_count = 0;
5694 mtx_unlock(&lun->lun_lock);
5695 retval = lun->backend->config_write((union ctl_io *)ctsio);
5697 mtx_unlock(&lun->lun_lock);
5698 ctl_set_success(ctsio);
5699 ctl_done((union ctl_io *)ctsio);
5708 ctl_format(struct ctl_scsiio *ctsio)
5710 struct scsi_format *cdb;
5711 struct ctl_lun *lun;
5712 struct ctl_softc *ctl_softc;
5713 int length, defect_list_len;
5715 CTL_DEBUG_PRINT(("ctl_format\n"));
5717 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5718 ctl_softc = control_softc;
5720 cdb = (struct scsi_format *)ctsio->cdb;
5723 if (cdb->byte2 & SF_FMTDATA) {
5724 if (cdb->byte2 & SF_LONGLIST)
5725 length = sizeof(struct scsi_format_header_long);
5727 length = sizeof(struct scsi_format_header_short);
5730 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5732 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5733 ctsio->kern_data_len = length;
5734 ctsio->kern_total_len = length;
5735 ctsio->kern_data_resid = 0;
5736 ctsio->kern_rel_offset = 0;
5737 ctsio->kern_sg_entries = 0;
5738 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5739 ctsio->be_move_done = ctl_config_move_done;
5740 ctl_datamove((union ctl_io *)ctsio);
5742 return (CTL_RETVAL_COMPLETE);
5745 defect_list_len = 0;
5747 if (cdb->byte2 & SF_FMTDATA) {
5748 if (cdb->byte2 & SF_LONGLIST) {
5749 struct scsi_format_header_long *header;
5751 header = (struct scsi_format_header_long *)
5752 ctsio->kern_data_ptr;
5754 defect_list_len = scsi_4btoul(header->defect_list_len);
5755 if (defect_list_len != 0) {
5756 ctl_set_invalid_field(ctsio,
5765 struct scsi_format_header_short *header;
5767 header = (struct scsi_format_header_short *)
5768 ctsio->kern_data_ptr;
5770 defect_list_len = scsi_2btoul(header->defect_list_len);
5771 if (defect_list_len != 0) {
5772 ctl_set_invalid_field(ctsio,
5784 * The format command will clear out the "Medium format corrupted"
5785 * status if set by the configuration code. That status is really
5786 * just a way to notify the host that we have lost the media, and
5787 * get them to issue a command that will basically make them think
5788 * they're blowing away the media.
5790 mtx_lock(&lun->lun_lock);
5791 lun->flags &= ~CTL_LUN_INOPERABLE;
5792 mtx_unlock(&lun->lun_lock);
5794 ctsio->scsi_status = SCSI_STATUS_OK;
5795 ctsio->io_hdr.status = CTL_SUCCESS;
5798 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5799 free(ctsio->kern_data_ptr, M_CTL);
5800 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5803 ctl_done((union ctl_io *)ctsio);
5804 return (CTL_RETVAL_COMPLETE);
5808 ctl_read_buffer(struct ctl_scsiio *ctsio)
5810 struct scsi_read_buffer *cdb;
5811 struct ctl_lun *lun;
5812 int buffer_offset, len;
5813 static uint8_t descr[4];
5814 static uint8_t echo_descr[4] = { 0 };
5816 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5818 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5819 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5821 if (lun->flags & CTL_LUN_PR_RESERVED) {
5825 * XXX KDM need a lock here.
5827 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5828 if ((lun->res_type == SPR_TYPE_EX_AC
5829 && residx != lun->pr_res_idx)
5830 || ((lun->res_type == SPR_TYPE_EX_AC_RO
5831 || lun->res_type == SPR_TYPE_EX_AC_AR)
5832 && !lun->per_res[residx].registered)) {
5833 ctl_set_reservation_conflict(ctsio);
5834 ctl_done((union ctl_io *)ctsio);
5835 return (CTL_RETVAL_COMPLETE);
5839 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5840 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5841 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5842 ctl_set_invalid_field(ctsio,
5848 ctl_done((union ctl_io *)ctsio);
5849 return (CTL_RETVAL_COMPLETE);
5852 len = scsi_3btoul(cdb->length);
5853 buffer_offset = scsi_3btoul(cdb->offset);
5855 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5856 ctl_set_invalid_field(ctsio,
5862 ctl_done((union ctl_io *)ctsio);
5863 return (CTL_RETVAL_COMPLETE);
5866 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5868 scsi_ulto3b(sizeof(lun->write_buffer), &descr[1]);
5869 ctsio->kern_data_ptr = descr;
5870 len = min(len, sizeof(descr));
5871 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5872 ctsio->kern_data_ptr = echo_descr;
5873 len = min(len, sizeof(echo_descr));
5875 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5876 ctsio->kern_data_len = len;
5877 ctsio->kern_total_len = len;
5878 ctsio->kern_data_resid = 0;
5879 ctsio->kern_rel_offset = 0;
5880 ctsio->kern_sg_entries = 0;
5881 ctsio->be_move_done = ctl_config_move_done;
5882 ctl_datamove((union ctl_io *)ctsio);
5884 return (CTL_RETVAL_COMPLETE);
5888 ctl_write_buffer(struct ctl_scsiio *ctsio)
5890 struct scsi_write_buffer *cdb;
5891 struct ctl_lun *lun;
5892 int buffer_offset, len;
5894 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5896 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5897 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5899 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5900 ctl_set_invalid_field(ctsio,
5906 ctl_done((union ctl_io *)ctsio);
5907 return (CTL_RETVAL_COMPLETE);
5910 len = scsi_3btoul(cdb->length);
5911 buffer_offset = scsi_3btoul(cdb->offset);
5913 if (buffer_offset + len > sizeof(lun->write_buffer)) {
5914 ctl_set_invalid_field(ctsio,
5920 ctl_done((union ctl_io *)ctsio);
5921 return (CTL_RETVAL_COMPLETE);
5925 * If we've got a kernel request that hasn't been malloced yet,
5926 * malloc it and tell the caller the data buffer is here.
5928 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5929 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5930 ctsio->kern_data_len = len;
5931 ctsio->kern_total_len = len;
5932 ctsio->kern_data_resid = 0;
5933 ctsio->kern_rel_offset = 0;
5934 ctsio->kern_sg_entries = 0;
5935 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5936 ctsio->be_move_done = ctl_config_move_done;
5937 ctl_datamove((union ctl_io *)ctsio);
5939 return (CTL_RETVAL_COMPLETE);
5942 ctl_done((union ctl_io *)ctsio);
5944 return (CTL_RETVAL_COMPLETE);
5948 ctl_write_same(struct ctl_scsiio *ctsio)
5950 struct ctl_lun *lun;
5951 struct ctl_lba_len_flags *lbalen;
5953 uint32_t num_blocks;
5957 retval = CTL_RETVAL_COMPLETE;
5959 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5961 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5963 switch (ctsio->cdb[0]) {
5964 case WRITE_SAME_10: {
5965 struct scsi_write_same_10 *cdb;
5967 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5969 lba = scsi_4btoul(cdb->addr);
5970 num_blocks = scsi_2btoul(cdb->length);
5974 case WRITE_SAME_16: {
5975 struct scsi_write_same_16 *cdb;
5977 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5979 lba = scsi_8btou64(cdb->addr);
5980 num_blocks = scsi_4btoul(cdb->length);
5986 * We got a command we don't support. This shouldn't
5987 * happen, commands should be filtered out above us.
5989 ctl_set_invalid_opcode(ctsio);
5990 ctl_done((union ctl_io *)ctsio);
5992 return (CTL_RETVAL_COMPLETE);
5993 break; /* NOTREACHED */
5996 /* NDOB flag can be used only together with UNMAP */
5997 if ((byte2 & (SWS_NDOB | SWS_UNMAP)) == SWS_NDOB) {
5998 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5999 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
6000 ctl_done((union ctl_io *)ctsio);
6001 return (CTL_RETVAL_COMPLETE);
6005 * The first check is to make sure we're in bounds, the second
6006 * check is to catch wrap-around problems. If the lba + num blocks
6007 * is less than the lba, then we've wrapped around and the block
6008 * range is invalid anyway.
6010 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6011 || ((lba + num_blocks) < lba)) {
6012 ctl_set_lba_out_of_range(ctsio);
6013 ctl_done((union ctl_io *)ctsio);
6014 return (CTL_RETVAL_COMPLETE);
6017 /* Zero number of blocks means "to the last logical block" */
6018 if (num_blocks == 0) {
6019 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
6020 ctl_set_invalid_field(ctsio,
6026 ctl_done((union ctl_io *)ctsio);
6027 return (CTL_RETVAL_COMPLETE);
6029 num_blocks = (lun->be_lun->maxlba + 1) - lba;
6032 len = lun->be_lun->blocksize;
6035 * If we've got a kernel request that hasn't been malloced yet,
6036 * malloc it and tell the caller the data buffer is here.
6038 if ((byte2 & SWS_NDOB) == 0 &&
6039 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6040 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6041 ctsio->kern_data_len = len;
6042 ctsio->kern_total_len = len;
6043 ctsio->kern_data_resid = 0;
6044 ctsio->kern_rel_offset = 0;
6045 ctsio->kern_sg_entries = 0;
6046 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6047 ctsio->be_move_done = ctl_config_move_done;
6048 ctl_datamove((union ctl_io *)ctsio);
6050 return (CTL_RETVAL_COMPLETE);
6053 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6055 lbalen->len = num_blocks;
6056 lbalen->flags = byte2;
6057 retval = lun->backend->config_write((union ctl_io *)ctsio);
6063 ctl_unmap(struct ctl_scsiio *ctsio)
6065 struct ctl_lun *lun;
6066 struct scsi_unmap *cdb;
6067 struct ctl_ptr_len_flags *ptrlen;
6068 struct scsi_unmap_header *hdr;
6069 struct scsi_unmap_desc *buf, *end, *range;
6071 uint32_t num_blocks;
6075 retval = CTL_RETVAL_COMPLETE;
6077 CTL_DEBUG_PRINT(("ctl_unmap\n"));
6079 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6080 cdb = (struct scsi_unmap *)ctsio->cdb;
6082 len = scsi_2btoul(cdb->length);
6086 * If we've got a kernel request that hasn't been malloced yet,
6087 * malloc it and tell the caller the data buffer is here.
6089 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6090 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6091 ctsio->kern_data_len = len;
6092 ctsio->kern_total_len = len;
6093 ctsio->kern_data_resid = 0;
6094 ctsio->kern_rel_offset = 0;
6095 ctsio->kern_sg_entries = 0;
6096 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6097 ctsio->be_move_done = ctl_config_move_done;
6098 ctl_datamove((union ctl_io *)ctsio);
6100 return (CTL_RETVAL_COMPLETE);
6103 len = ctsio->kern_total_len - ctsio->kern_data_resid;
6104 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
6105 if (len < sizeof (*hdr) ||
6106 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
6107 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
6108 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
6109 ctl_set_invalid_field(ctsio,
6115 ctl_done((union ctl_io *)ctsio);
6116 return (CTL_RETVAL_COMPLETE);
6118 len = scsi_2btoul(hdr->desc_length);
6119 buf = (struct scsi_unmap_desc *)(hdr + 1);
6120 end = buf + len / sizeof(*buf);
6122 for (range = buf; range < end; range++) {
6123 lba = scsi_8btou64(range->lba);
6124 num_blocks = scsi_4btoul(range->length);
6125 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6126 || ((lba + num_blocks) < lba)) {
6127 ctl_set_lba_out_of_range(ctsio);
6128 ctl_done((union ctl_io *)ctsio);
6129 return (CTL_RETVAL_COMPLETE);
6133 mtx_lock(&lun->lun_lock);
6134 ptrlen = (struct ctl_ptr_len_flags *)
6135 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6136 ptrlen->ptr = (void *)buf;
6138 ptrlen->flags = byte2;
6139 ctl_check_blocked(lun);
6140 mtx_unlock(&lun->lun_lock);
6142 retval = lun->backend->config_write((union ctl_io *)ctsio);
6147 * Note that this function currently doesn't actually do anything inside
6148 * CTL to enforce things if the DQue bit is turned on.
6150 * Also note that this function can't be used in the default case, because
6151 * the DQue bit isn't set in the changeable mask for the control mode page
6152 * anyway. This is just here as an example for how to implement a page
6153 * handler, and a placeholder in case we want to allow the user to turn
6154 * tagged queueing on and off.
6156 * The D_SENSE bit handling is functional, however, and will turn
6157 * descriptor sense on and off for a given LUN.
6160 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6161 struct ctl_page_index *page_index, uint8_t *page_ptr)
6163 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6164 struct ctl_lun *lun;
6165 struct ctl_softc *softc;
6169 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6170 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6173 user_cp = (struct scsi_control_page *)page_ptr;
6174 current_cp = (struct scsi_control_page *)
6175 (page_index->page_data + (page_index->page_len *
6177 saved_cp = (struct scsi_control_page *)
6178 (page_index->page_data + (page_index->page_len *
6181 softc = control_softc;
6183 mtx_lock(&lun->lun_lock);
6184 if (((current_cp->rlec & SCP_DSENSE) == 0)
6185 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6187 * Descriptor sense is currently turned off and the user
6188 * wants to turn it on.
6190 current_cp->rlec |= SCP_DSENSE;
6191 saved_cp->rlec |= SCP_DSENSE;
6192 lun->flags |= CTL_LUN_SENSE_DESC;
6194 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6195 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6197 * Descriptor sense is currently turned on, and the user
6198 * wants to turn it off.
6200 current_cp->rlec &= ~SCP_DSENSE;
6201 saved_cp->rlec &= ~SCP_DSENSE;
6202 lun->flags &= ~CTL_LUN_SENSE_DESC;
6205 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6206 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6207 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6208 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6209 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6210 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6216 * Let other initiators know that the mode
6217 * parameters for this LUN have changed.
6219 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6223 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6226 mtx_unlock(&lun->lun_lock);
6232 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6233 struct ctl_page_index *page_index, uint8_t *page_ptr)
6235 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6236 struct ctl_lun *lun;
6240 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6241 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6244 user_cp = (struct scsi_caching_page *)page_ptr;
6245 current_cp = (struct scsi_caching_page *)
6246 (page_index->page_data + (page_index->page_len *
6248 saved_cp = (struct scsi_caching_page *)
6249 (page_index->page_data + (page_index->page_len *
6252 mtx_lock(&lun->lun_lock);
6253 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6254 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6255 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6256 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6257 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6258 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6264 * Let other initiators know that the mode
6265 * parameters for this LUN have changed.
6267 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
6271 lun->pending_ua[i] |= CTL_UA_MODE_CHANGE;
6274 mtx_unlock(&lun->lun_lock);
6280 ctl_power_sp_handler(struct ctl_scsiio *ctsio,
6281 struct ctl_page_index *page_index, uint8_t *page_ptr)
6287 ctl_power_sp_sense_handler(struct ctl_scsiio *ctsio,
6288 struct ctl_page_index *page_index, int pc)
6290 struct copan_power_subpage *page;
6292 page = (struct copan_power_subpage *)page_index->page_data +
6293 (page_index->page_len * pc);
6296 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6298 * We don't update the changable bits for this page.
6301 case SMS_PAGE_CTRL_CURRENT >> 6:
6302 case SMS_PAGE_CTRL_DEFAULT >> 6:
6303 case SMS_PAGE_CTRL_SAVED >> 6:
6305 ctl_update_power_subpage(page);
6310 EPRINT(0, "Invalid PC %d!!", pc);
6319 ctl_aps_sp_handler(struct ctl_scsiio *ctsio,
6320 struct ctl_page_index *page_index, uint8_t *page_ptr)
6322 struct copan_aps_subpage *user_sp;
6323 struct copan_aps_subpage *current_sp;
6324 union ctl_modepage_info *modepage_info;
6325 struct ctl_softc *softc;
6326 struct ctl_lun *lun;
6329 retval = CTL_RETVAL_COMPLETE;
6330 current_sp = (struct copan_aps_subpage *)(page_index->page_data +
6331 (page_index->page_len * CTL_PAGE_CURRENT));
6332 softc = control_softc;
6333 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6335 user_sp = (struct copan_aps_subpage *)page_ptr;
6337 modepage_info = (union ctl_modepage_info *)
6338 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6340 modepage_info->header.page_code = page_index->page_code & SMPH_PC_MASK;
6341 modepage_info->header.subpage = page_index->subpage;
6342 modepage_info->aps.lock_active = user_sp->lock_active;
6344 mtx_lock(&softc->ctl_lock);
6347 * If there is a request to lock the LUN and another LUN is locked
6348 * this is an error. If the requested LUN is already locked ignore
6349 * the request. If no LUN is locked attempt to lock it.
6350 * if there is a request to unlock the LUN and the LUN is currently
6351 * locked attempt to unlock it. Otherwise ignore the request. i.e.
6352 * if another LUN is locked or no LUN is locked.
6354 if (user_sp->lock_active & APS_LOCK_ACTIVE) {
6355 if (softc->aps_locked_lun == lun->lun) {
6357 * This LUN is already locked, so we're done.
6359 retval = CTL_RETVAL_COMPLETE;
6360 } else if (softc->aps_locked_lun == 0) {
6362 * No one has the lock, pass the request to the
6365 retval = lun->backend->config_write(
6366 (union ctl_io *)ctsio);
6369 * Someone else has the lock, throw out the request.
6371 ctl_set_already_locked(ctsio);
6372 free(ctsio->kern_data_ptr, M_CTL);
6373 ctl_done((union ctl_io *)ctsio);
6376 * Set the return value so that ctl_do_mode_select()
6377 * won't try to complete the command. We already
6378 * completed it here.
6380 retval = CTL_RETVAL_ERROR;
6382 } else if (softc->aps_locked_lun == lun->lun) {
6384 * This LUN is locked, so pass the unlock request to the
6387 retval = lun->backend->config_write((union ctl_io *)ctsio);
6389 mtx_unlock(&softc->ctl_lock);
6395 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6396 struct ctl_page_index *page_index,
6402 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6407 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6408 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6409 printf("page data:");
6411 printf(" %.2x",page_ptr[i]);
6417 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6418 struct ctl_page_index *page_index,
6421 struct copan_debugconf_subpage *page;
6423 page = (struct copan_debugconf_subpage *)page_index->page_data +
6424 (page_index->page_len * pc);
6427 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6428 case SMS_PAGE_CTRL_DEFAULT >> 6:
6429 case SMS_PAGE_CTRL_SAVED >> 6:
6431 * We don't update the changable or default bits for this page.
6434 case SMS_PAGE_CTRL_CURRENT >> 6:
6435 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6436 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6440 EPRINT(0, "Invalid PC %d!!", pc);
6441 #endif /* NEEDTOPORT */
6449 ctl_do_mode_select(union ctl_io *io)
6451 struct scsi_mode_page_header *page_header;
6452 struct ctl_page_index *page_index;
6453 struct ctl_scsiio *ctsio;
6454 int control_dev, page_len;
6455 int page_len_offset, page_len_size;
6456 union ctl_modepage_info *modepage_info;
6457 struct ctl_lun *lun;
6458 int *len_left, *len_used;
6461 ctsio = &io->scsiio;
6464 retval = CTL_RETVAL_COMPLETE;
6466 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6468 if (lun->be_lun->lun_type != T_DIRECT)
6473 modepage_info = (union ctl_modepage_info *)
6474 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6475 len_left = &modepage_info->header.len_left;
6476 len_used = &modepage_info->header.len_used;
6480 page_header = (struct scsi_mode_page_header *)
6481 (ctsio->kern_data_ptr + *len_used);
6483 if (*len_left == 0) {
6484 free(ctsio->kern_data_ptr, M_CTL);
6485 ctl_set_success(ctsio);
6486 ctl_done((union ctl_io *)ctsio);
6487 return (CTL_RETVAL_COMPLETE);
6488 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6490 free(ctsio->kern_data_ptr, M_CTL);
6491 ctl_set_param_len_error(ctsio);
6492 ctl_done((union ctl_io *)ctsio);
6493 return (CTL_RETVAL_COMPLETE);
6495 } else if ((page_header->page_code & SMPH_SPF)
6496 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6498 free(ctsio->kern_data_ptr, M_CTL);
6499 ctl_set_param_len_error(ctsio);
6500 ctl_done((union ctl_io *)ctsio);
6501 return (CTL_RETVAL_COMPLETE);
6506 * XXX KDM should we do something with the block descriptor?
6508 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6510 if ((control_dev != 0)
6511 && (lun->mode_pages.index[i].page_flags &
6512 CTL_PAGE_FLAG_DISK_ONLY))
6515 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6516 (page_header->page_code & SMPH_PC_MASK))
6520 * If neither page has a subpage code, then we've got a
6523 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6524 && ((page_header->page_code & SMPH_SPF) == 0)) {
6525 page_index = &lun->mode_pages.index[i];
6526 page_len = page_header->page_length;
6531 * If both pages have subpages, then the subpage numbers
6534 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6535 && (page_header->page_code & SMPH_SPF)) {
6536 struct scsi_mode_page_header_sp *sph;
6538 sph = (struct scsi_mode_page_header_sp *)page_header;
6540 if (lun->mode_pages.index[i].subpage ==
6542 page_index = &lun->mode_pages.index[i];
6543 page_len = scsi_2btoul(sph->page_length);
6550 * If we couldn't find the page, or if we don't have a mode select
6551 * handler for it, send back an error to the user.
6553 if ((page_index == NULL)
6554 || (page_index->select_handler == NULL)) {
6555 ctl_set_invalid_field(ctsio,
6558 /*field*/ *len_used,
6561 free(ctsio->kern_data_ptr, M_CTL);
6562 ctl_done((union ctl_io *)ctsio);
6563 return (CTL_RETVAL_COMPLETE);
6566 if (page_index->page_code & SMPH_SPF) {
6567 page_len_offset = 2;
6571 page_len_offset = 1;
6575 * If the length the initiator gives us isn't the one we specify in
6576 * the mode page header, or if they didn't specify enough data in
6577 * the CDB to avoid truncating this page, kick out the request.
6579 if ((page_len != (page_index->page_len - page_len_offset -
6581 || (*len_left < page_index->page_len)) {
6584 ctl_set_invalid_field(ctsio,
6587 /*field*/ *len_used + page_len_offset,
6590 free(ctsio->kern_data_ptr, M_CTL);
6591 ctl_done((union ctl_io *)ctsio);
6592 return (CTL_RETVAL_COMPLETE);
6596 * Run through the mode page, checking to make sure that the bits
6597 * the user changed are actually legal for him to change.
6599 for (i = 0; i < page_index->page_len; i++) {
6600 uint8_t *user_byte, *change_mask, *current_byte;
6604 user_byte = (uint8_t *)page_header + i;
6605 change_mask = page_index->page_data +
6606 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6607 current_byte = page_index->page_data +
6608 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6611 * Check to see whether the user set any bits in this byte
6612 * that he is not allowed to set.
6614 if ((*user_byte & ~(*change_mask)) ==
6615 (*current_byte & ~(*change_mask)))
6619 * Go through bit by bit to determine which one is illegal.
6622 for (j = 7; j >= 0; j--) {
6623 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6624 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6629 ctl_set_invalid_field(ctsio,
6632 /*field*/ *len_used + i,
6635 free(ctsio->kern_data_ptr, M_CTL);
6636 ctl_done((union ctl_io *)ctsio);
6637 return (CTL_RETVAL_COMPLETE);
6641 * Decrement these before we call the page handler, since we may
6642 * end up getting called back one way or another before the handler
6643 * returns to this context.
6645 *len_left -= page_index->page_len;
6646 *len_used += page_index->page_len;
6648 retval = page_index->select_handler(ctsio, page_index,
6649 (uint8_t *)page_header);
6652 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6653 * wait until this queued command completes to finish processing
6654 * the mode page. If it returns anything other than
6655 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6656 * already set the sense information, freed the data pointer, and
6657 * completed the io for us.
6659 if (retval != CTL_RETVAL_COMPLETE)
6660 goto bailout_no_done;
6663 * If the initiator sent us more than one page, parse the next one.
6668 ctl_set_success(ctsio);
6669 free(ctsio->kern_data_ptr, M_CTL);
6670 ctl_done((union ctl_io *)ctsio);
6674 return (CTL_RETVAL_COMPLETE);
6679 ctl_mode_select(struct ctl_scsiio *ctsio)
6681 int param_len, pf, sp;
6682 int header_size, bd_len;
6683 int len_left, len_used;
6684 struct ctl_page_index *page_index;
6685 struct ctl_lun *lun;
6686 int control_dev, page_len;
6687 union ctl_modepage_info *modepage_info;
6699 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6701 if (lun->be_lun->lun_type != T_DIRECT)
6706 switch (ctsio->cdb[0]) {
6707 case MODE_SELECT_6: {
6708 struct scsi_mode_select_6 *cdb;
6710 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6712 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6713 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6715 param_len = cdb->length;
6716 header_size = sizeof(struct scsi_mode_header_6);
6719 case MODE_SELECT_10: {
6720 struct scsi_mode_select_10 *cdb;
6722 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6724 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6725 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6727 param_len = scsi_2btoul(cdb->length);
6728 header_size = sizeof(struct scsi_mode_header_10);
6732 ctl_set_invalid_opcode(ctsio);
6733 ctl_done((union ctl_io *)ctsio);
6734 return (CTL_RETVAL_COMPLETE);
6735 break; /* NOTREACHED */
6740 * "A parameter list length of zero indicates that the Data-Out Buffer
6741 * shall be empty. This condition shall not be considered as an error."
6743 if (param_len == 0) {
6744 ctl_set_success(ctsio);
6745 ctl_done((union ctl_io *)ctsio);
6746 return (CTL_RETVAL_COMPLETE);
6750 * Since we'll hit this the first time through, prior to
6751 * allocation, we don't need to free a data buffer here.
6753 if (param_len < header_size) {
6754 ctl_set_param_len_error(ctsio);
6755 ctl_done((union ctl_io *)ctsio);
6756 return (CTL_RETVAL_COMPLETE);
6760 * Allocate the data buffer and grab the user's data. In theory,
6761 * we shouldn't have to sanity check the parameter list length here
6762 * because the maximum size is 64K. We should be able to malloc
6763 * that much without too many problems.
6765 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6766 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6767 ctsio->kern_data_len = param_len;
6768 ctsio->kern_total_len = param_len;
6769 ctsio->kern_data_resid = 0;
6770 ctsio->kern_rel_offset = 0;
6771 ctsio->kern_sg_entries = 0;
6772 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6773 ctsio->be_move_done = ctl_config_move_done;
6774 ctl_datamove((union ctl_io *)ctsio);
6776 return (CTL_RETVAL_COMPLETE);
6779 switch (ctsio->cdb[0]) {
6780 case MODE_SELECT_6: {
6781 struct scsi_mode_header_6 *mh6;
6783 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6784 bd_len = mh6->blk_desc_len;
6787 case MODE_SELECT_10: {
6788 struct scsi_mode_header_10 *mh10;
6790 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6791 bd_len = scsi_2btoul(mh10->blk_desc_len);
6795 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6799 if (param_len < (header_size + bd_len)) {
6800 free(ctsio->kern_data_ptr, M_CTL);
6801 ctl_set_param_len_error(ctsio);
6802 ctl_done((union ctl_io *)ctsio);
6803 return (CTL_RETVAL_COMPLETE);
6807 * Set the IO_CONT flag, so that if this I/O gets passed to
6808 * ctl_config_write_done(), it'll get passed back to
6809 * ctl_do_mode_select() for further processing, or completion if
6812 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6813 ctsio->io_cont = ctl_do_mode_select;
6815 modepage_info = (union ctl_modepage_info *)
6816 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6818 memset(modepage_info, 0, sizeof(*modepage_info));
6820 len_left = param_len - header_size - bd_len;
6821 len_used = header_size + bd_len;
6823 modepage_info->header.len_left = len_left;
6824 modepage_info->header.len_used = len_used;
6826 return (ctl_do_mode_select((union ctl_io *)ctsio));
6830 ctl_mode_sense(struct ctl_scsiio *ctsio)
6832 struct ctl_lun *lun;
6833 int pc, page_code, dbd, llba, subpage;
6834 int alloc_len, page_len, header_len, total_len;
6835 struct scsi_mode_block_descr *block_desc;
6836 struct ctl_page_index *page_index;
6844 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6846 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6848 if (lun->be_lun->lun_type != T_DIRECT)
6853 if (lun->flags & CTL_LUN_PR_RESERVED) {
6857 * XXX KDM need a lock here.
6859 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
6860 if ((lun->res_type == SPR_TYPE_EX_AC
6861 && residx != lun->pr_res_idx)
6862 || ((lun->res_type == SPR_TYPE_EX_AC_RO
6863 || lun->res_type == SPR_TYPE_EX_AC_AR)
6864 && !lun->per_res[residx].registered)) {
6865 ctl_set_reservation_conflict(ctsio);
6866 ctl_done((union ctl_io *)ctsio);
6867 return (CTL_RETVAL_COMPLETE);
6871 switch (ctsio->cdb[0]) {
6872 case MODE_SENSE_6: {
6873 struct scsi_mode_sense_6 *cdb;
6875 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6877 header_len = sizeof(struct scsi_mode_hdr_6);
6878 if (cdb->byte2 & SMS_DBD)
6881 header_len += sizeof(struct scsi_mode_block_descr);
6883 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6884 page_code = cdb->page & SMS_PAGE_CODE;
6885 subpage = cdb->subpage;
6886 alloc_len = cdb->length;
6889 case MODE_SENSE_10: {
6890 struct scsi_mode_sense_10 *cdb;
6892 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6894 header_len = sizeof(struct scsi_mode_hdr_10);
6896 if (cdb->byte2 & SMS_DBD)
6899 header_len += sizeof(struct scsi_mode_block_descr);
6900 if (cdb->byte2 & SMS10_LLBAA)
6902 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6903 page_code = cdb->page & SMS_PAGE_CODE;
6904 subpage = cdb->subpage;
6905 alloc_len = scsi_2btoul(cdb->length);
6909 ctl_set_invalid_opcode(ctsio);
6910 ctl_done((union ctl_io *)ctsio);
6911 return (CTL_RETVAL_COMPLETE);
6912 break; /* NOTREACHED */
6916 * We have to make a first pass through to calculate the size of
6917 * the pages that match the user's query. Then we allocate enough
6918 * memory to hold it, and actually copy the data into the buffer.
6920 switch (page_code) {
6921 case SMS_ALL_PAGES_PAGE: {
6927 * At the moment, values other than 0 and 0xff here are
6928 * reserved according to SPC-3.
6930 if ((subpage != SMS_SUBPAGE_PAGE_0)
6931 && (subpage != SMS_SUBPAGE_ALL)) {
6932 ctl_set_invalid_field(ctsio,
6938 ctl_done((union ctl_io *)ctsio);
6939 return (CTL_RETVAL_COMPLETE);
6942 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6943 if ((control_dev != 0)
6944 && (lun->mode_pages.index[i].page_flags &
6945 CTL_PAGE_FLAG_DISK_ONLY))
6949 * We don't use this subpage if the user didn't
6950 * request all subpages.
6952 if ((lun->mode_pages.index[i].subpage != 0)
6953 && (subpage == SMS_SUBPAGE_PAGE_0))
6957 printf("found page %#x len %d\n",
6958 lun->mode_pages.index[i].page_code &
6960 lun->mode_pages.index[i].page_len);
6962 page_len += lun->mode_pages.index[i].page_len;
6971 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6972 /* Look for the right page code */
6973 if ((lun->mode_pages.index[i].page_code &
6974 SMPH_PC_MASK) != page_code)
6977 /* Look for the right subpage or the subpage wildcard*/
6978 if ((lun->mode_pages.index[i].subpage != subpage)
6979 && (subpage != SMS_SUBPAGE_ALL))
6982 /* Make sure the page is supported for this dev type */
6983 if ((control_dev != 0)
6984 && (lun->mode_pages.index[i].page_flags &
6985 CTL_PAGE_FLAG_DISK_ONLY))
6989 printf("found page %#x len %d\n",
6990 lun->mode_pages.index[i].page_code &
6992 lun->mode_pages.index[i].page_len);
6995 page_len += lun->mode_pages.index[i].page_len;
6998 if (page_len == 0) {
6999 ctl_set_invalid_field(ctsio,
7005 ctl_done((union ctl_io *)ctsio);
7006 return (CTL_RETVAL_COMPLETE);
7012 total_len = header_len + page_len;
7014 printf("header_len = %d, page_len = %d, total_len = %d\n",
7015 header_len, page_len, total_len);
7018 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7019 ctsio->kern_sg_entries = 0;
7020 ctsio->kern_data_resid = 0;
7021 ctsio->kern_rel_offset = 0;
7022 if (total_len < alloc_len) {
7023 ctsio->residual = alloc_len - total_len;
7024 ctsio->kern_data_len = total_len;
7025 ctsio->kern_total_len = total_len;
7027 ctsio->residual = 0;
7028 ctsio->kern_data_len = alloc_len;
7029 ctsio->kern_total_len = alloc_len;
7032 switch (ctsio->cdb[0]) {
7033 case MODE_SENSE_6: {
7034 struct scsi_mode_hdr_6 *header;
7036 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
7038 header->datalen = ctl_min(total_len - 1, 254);
7039 if (control_dev == 0)
7040 header->dev_specific = 0x10; /* DPOFUA */
7042 header->block_descr_len = 0;
7044 header->block_descr_len =
7045 sizeof(struct scsi_mode_block_descr);
7046 block_desc = (struct scsi_mode_block_descr *)&header[1];
7049 case MODE_SENSE_10: {
7050 struct scsi_mode_hdr_10 *header;
7053 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
7055 datalen = ctl_min(total_len - 2, 65533);
7056 scsi_ulto2b(datalen, header->datalen);
7057 if (control_dev == 0)
7058 header->dev_specific = 0x10; /* DPOFUA */
7060 scsi_ulto2b(0, header->block_descr_len);
7062 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
7063 header->block_descr_len);
7064 block_desc = (struct scsi_mode_block_descr *)&header[1];
7068 panic("invalid CDB type %#x", ctsio->cdb[0]);
7069 break; /* NOTREACHED */
7073 * If we've got a disk, use its blocksize in the block
7074 * descriptor. Otherwise, just set it to 0.
7077 if (control_dev != 0)
7078 scsi_ulto3b(lun->be_lun->blocksize,
7079 block_desc->block_len);
7081 scsi_ulto3b(0, block_desc->block_len);
7084 switch (page_code) {
7085 case SMS_ALL_PAGES_PAGE: {
7088 data_used = header_len;
7089 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7090 struct ctl_page_index *page_index;
7092 page_index = &lun->mode_pages.index[i];
7094 if ((control_dev != 0)
7095 && (page_index->page_flags &
7096 CTL_PAGE_FLAG_DISK_ONLY))
7100 * We don't use this subpage if the user didn't
7101 * request all subpages. We already checked (above)
7102 * to make sure the user only specified a subpage
7103 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
7105 if ((page_index->subpage != 0)
7106 && (subpage == SMS_SUBPAGE_PAGE_0))
7110 * Call the handler, if it exists, to update the
7111 * page to the latest values.
7113 if (page_index->sense_handler != NULL)
7114 page_index->sense_handler(ctsio, page_index,pc);
7116 memcpy(ctsio->kern_data_ptr + data_used,
7117 page_index->page_data +
7118 (page_index->page_len * pc),
7119 page_index->page_len);
7120 data_used += page_index->page_len;
7127 data_used = header_len;
7129 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
7130 struct ctl_page_index *page_index;
7132 page_index = &lun->mode_pages.index[i];
7134 /* Look for the right page code */
7135 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
7138 /* Look for the right subpage or the subpage wildcard*/
7139 if ((page_index->subpage != subpage)
7140 && (subpage != SMS_SUBPAGE_ALL))
7143 /* Make sure the page is supported for this dev type */
7144 if ((control_dev != 0)
7145 && (page_index->page_flags &
7146 CTL_PAGE_FLAG_DISK_ONLY))
7150 * Call the handler, if it exists, to update the
7151 * page to the latest values.
7153 if (page_index->sense_handler != NULL)
7154 page_index->sense_handler(ctsio, page_index,pc);
7156 memcpy(ctsio->kern_data_ptr + data_used,
7157 page_index->page_data +
7158 (page_index->page_len * pc),
7159 page_index->page_len);
7160 data_used += page_index->page_len;
7166 ctsio->scsi_status = SCSI_STATUS_OK;
7168 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7169 ctsio->be_move_done = ctl_config_move_done;
7170 ctl_datamove((union ctl_io *)ctsio);
7172 return (CTL_RETVAL_COMPLETE);
7176 ctl_read_capacity(struct ctl_scsiio *ctsio)
7178 struct scsi_read_capacity *cdb;
7179 struct scsi_read_capacity_data *data;
7180 struct ctl_lun *lun;
7183 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7185 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7187 lba = scsi_4btoul(cdb->addr);
7188 if (((cdb->pmi & SRC_PMI) == 0)
7190 ctl_set_invalid_field(/*ctsio*/ ctsio,
7196 ctl_done((union ctl_io *)ctsio);
7197 return (CTL_RETVAL_COMPLETE);
7200 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7202 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7203 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7204 ctsio->residual = 0;
7205 ctsio->kern_data_len = sizeof(*data);
7206 ctsio->kern_total_len = sizeof(*data);
7207 ctsio->kern_data_resid = 0;
7208 ctsio->kern_rel_offset = 0;
7209 ctsio->kern_sg_entries = 0;
7212 * If the maximum LBA is greater than 0xfffffffe, the user must
7213 * issue a SERVICE ACTION IN (16) command, with the read capacity
7214 * serivce action set.
7216 if (lun->be_lun->maxlba > 0xfffffffe)
7217 scsi_ulto4b(0xffffffff, data->addr);
7219 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7222 * XXX KDM this may not be 512 bytes...
7224 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7226 ctsio->scsi_status = SCSI_STATUS_OK;
7228 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7229 ctsio->be_move_done = ctl_config_move_done;
7230 ctl_datamove((union ctl_io *)ctsio);
7232 return (CTL_RETVAL_COMPLETE);
7236 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7238 struct scsi_read_capacity_16 *cdb;
7239 struct scsi_read_capacity_data_long *data;
7240 struct ctl_lun *lun;
7244 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7246 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7248 alloc_len = scsi_4btoul(cdb->alloc_len);
7249 lba = scsi_8btou64(cdb->addr);
7251 if ((cdb->reladr & SRC16_PMI)
7253 ctl_set_invalid_field(/*ctsio*/ ctsio,
7259 ctl_done((union ctl_io *)ctsio);
7260 return (CTL_RETVAL_COMPLETE);
7263 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7265 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7266 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7268 if (sizeof(*data) < alloc_len) {
7269 ctsio->residual = alloc_len - sizeof(*data);
7270 ctsio->kern_data_len = sizeof(*data);
7271 ctsio->kern_total_len = sizeof(*data);
7273 ctsio->residual = 0;
7274 ctsio->kern_data_len = alloc_len;
7275 ctsio->kern_total_len = alloc_len;
7277 ctsio->kern_data_resid = 0;
7278 ctsio->kern_rel_offset = 0;
7279 ctsio->kern_sg_entries = 0;
7281 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7282 /* XXX KDM this may not be 512 bytes... */
7283 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7284 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7285 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7286 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7287 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7289 ctsio->scsi_status = SCSI_STATUS_OK;
7291 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7292 ctsio->be_move_done = ctl_config_move_done;
7293 ctl_datamove((union ctl_io *)ctsio);
7295 return (CTL_RETVAL_COMPLETE);
7299 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7301 struct scsi_maintenance_in *cdb;
7303 int alloc_len, ext, total_len = 0, g, p, pc, pg;
7304 int num_target_port_groups, num_target_ports, single;
7305 struct ctl_lun *lun;
7306 struct ctl_softc *softc;
7307 struct ctl_port *port;
7308 struct scsi_target_group_data *rtg_ptr;
7309 struct scsi_target_group_data_extended *rtg_ext_ptr;
7310 struct scsi_target_port_group_descriptor *tpg_desc;
7312 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7314 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7315 softc = control_softc;
7316 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7318 retval = CTL_RETVAL_COMPLETE;
7320 switch (cdb->byte2 & STG_PDF_MASK) {
7321 case STG_PDF_LENGTH:
7324 case STG_PDF_EXTENDED:
7328 ctl_set_invalid_field(/*ctsio*/ ctsio,
7334 ctl_done((union ctl_io *)ctsio);
7338 single = ctl_is_single;
7340 num_target_port_groups = 1;
7342 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7343 num_target_ports = 0;
7344 mtx_lock(&softc->ctl_lock);
7345 STAILQ_FOREACH(port, &softc->port_list, links) {
7346 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7348 if (ctl_map_lun_back(port->targ_port, lun->lun) >= CTL_MAX_LUNS)
7352 mtx_unlock(&softc->ctl_lock);
7355 total_len = sizeof(struct scsi_target_group_data_extended);
7357 total_len = sizeof(struct scsi_target_group_data);
7358 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7359 num_target_port_groups +
7360 sizeof(struct scsi_target_port_descriptor) *
7361 num_target_ports * num_target_port_groups;
7363 alloc_len = scsi_4btoul(cdb->length);
7365 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7367 ctsio->kern_sg_entries = 0;
7369 if (total_len < alloc_len) {
7370 ctsio->residual = alloc_len - total_len;
7371 ctsio->kern_data_len = total_len;
7372 ctsio->kern_total_len = total_len;
7374 ctsio->residual = 0;
7375 ctsio->kern_data_len = alloc_len;
7376 ctsio->kern_total_len = alloc_len;
7378 ctsio->kern_data_resid = 0;
7379 ctsio->kern_rel_offset = 0;
7382 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7383 ctsio->kern_data_ptr;
7384 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7385 rtg_ext_ptr->format_type = 0x10;
7386 rtg_ext_ptr->implicit_transition_time = 0;
7387 tpg_desc = &rtg_ext_ptr->groups[0];
7389 rtg_ptr = (struct scsi_target_group_data *)
7390 ctsio->kern_data_ptr;
7391 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7392 tpg_desc = &rtg_ptr->groups[0];
7395 pg = ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS;
7396 mtx_lock(&softc->ctl_lock);
7397 for (g = 0; g < num_target_port_groups; g++) {
7399 tpg_desc->pref_state = TPG_PRIMARY |
7400 TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7402 tpg_desc->pref_state =
7403 TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7404 tpg_desc->support = TPG_AO_SUP;
7406 tpg_desc->support |= TPG_AN_SUP;
7407 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7408 tpg_desc->status = TPG_IMPLICIT;
7410 STAILQ_FOREACH(port, &softc->port_list, links) {
7411 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7413 if (ctl_map_lun_back(port->targ_port, lun->lun) >=
7416 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7417 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7418 relative_target_port_identifier);
7421 tpg_desc->target_port_count = pc;
7422 tpg_desc = (struct scsi_target_port_group_descriptor *)
7423 &tpg_desc->descriptors[pc];
7425 mtx_unlock(&softc->ctl_lock);
7427 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7428 ctsio->be_move_done = ctl_config_move_done;
7430 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7431 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7432 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7433 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7434 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7436 ctl_datamove((union ctl_io *)ctsio);
7441 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7443 struct ctl_lun *lun;
7444 struct scsi_report_supported_opcodes *cdb;
7445 const struct ctl_cmd_entry *entry, *sentry;
7446 struct scsi_report_supported_opcodes_all *all;
7447 struct scsi_report_supported_opcodes_descr *descr;
7448 struct scsi_report_supported_opcodes_one *one;
7450 int alloc_len, total_len;
7451 int opcode, service_action, i, j, num;
7453 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7455 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7456 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7458 retval = CTL_RETVAL_COMPLETE;
7460 opcode = cdb->requested_opcode;
7461 service_action = scsi_2btoul(cdb->requested_service_action);
7462 switch (cdb->options & RSO_OPTIONS_MASK) {
7463 case RSO_OPTIONS_ALL:
7465 for (i = 0; i < 256; i++) {
7466 entry = &ctl_cmd_table[i];
7467 if (entry->flags & CTL_CMD_FLAG_SA5) {
7468 for (j = 0; j < 32; j++) {
7469 sentry = &((const struct ctl_cmd_entry *)
7471 if (ctl_cmd_applicable(
7472 lun->be_lun->lun_type, sentry))
7476 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7481 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7482 num * sizeof(struct scsi_report_supported_opcodes_descr);
7484 case RSO_OPTIONS_OC:
7485 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7486 ctl_set_invalid_field(/*ctsio*/ ctsio,
7492 ctl_done((union ctl_io *)ctsio);
7493 return (CTL_RETVAL_COMPLETE);
7495 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7497 case RSO_OPTIONS_OC_SA:
7498 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7499 service_action >= 32) {
7500 ctl_set_invalid_field(/*ctsio*/ ctsio,
7506 ctl_done((union ctl_io *)ctsio);
7507 return (CTL_RETVAL_COMPLETE);
7509 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7512 ctl_set_invalid_field(/*ctsio*/ ctsio,
7518 ctl_done((union ctl_io *)ctsio);
7519 return (CTL_RETVAL_COMPLETE);
7522 alloc_len = scsi_4btoul(cdb->length);
7524 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7526 ctsio->kern_sg_entries = 0;
7528 if (total_len < alloc_len) {
7529 ctsio->residual = alloc_len - total_len;
7530 ctsio->kern_data_len = total_len;
7531 ctsio->kern_total_len = total_len;
7533 ctsio->residual = 0;
7534 ctsio->kern_data_len = alloc_len;
7535 ctsio->kern_total_len = alloc_len;
7537 ctsio->kern_data_resid = 0;
7538 ctsio->kern_rel_offset = 0;
7540 switch (cdb->options & RSO_OPTIONS_MASK) {
7541 case RSO_OPTIONS_ALL:
7542 all = (struct scsi_report_supported_opcodes_all *)
7543 ctsio->kern_data_ptr;
7545 for (i = 0; i < 256; i++) {
7546 entry = &ctl_cmd_table[i];
7547 if (entry->flags & CTL_CMD_FLAG_SA5) {
7548 for (j = 0; j < 32; j++) {
7549 sentry = &((const struct ctl_cmd_entry *)
7551 if (!ctl_cmd_applicable(
7552 lun->be_lun->lun_type, sentry))
7554 descr = &all->descr[num++];
7556 scsi_ulto2b(j, descr->service_action);
7557 descr->flags = RSO_SERVACTV;
7558 scsi_ulto2b(sentry->length,
7562 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7565 descr = &all->descr[num++];
7567 scsi_ulto2b(0, descr->service_action);
7569 scsi_ulto2b(entry->length, descr->cdb_length);
7573 num * sizeof(struct scsi_report_supported_opcodes_descr),
7576 case RSO_OPTIONS_OC:
7577 one = (struct scsi_report_supported_opcodes_one *)
7578 ctsio->kern_data_ptr;
7579 entry = &ctl_cmd_table[opcode];
7581 case RSO_OPTIONS_OC_SA:
7582 one = (struct scsi_report_supported_opcodes_one *)
7583 ctsio->kern_data_ptr;
7584 entry = &ctl_cmd_table[opcode];
7585 entry = &((const struct ctl_cmd_entry *)
7586 entry->execute)[service_action];
7588 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7590 scsi_ulto2b(entry->length, one->cdb_length);
7591 one->cdb_usage[0] = opcode;
7592 memcpy(&one->cdb_usage[1], entry->usage,
7599 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7600 ctsio->be_move_done = ctl_config_move_done;
7602 ctl_datamove((union ctl_io *)ctsio);
7607 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7609 struct ctl_lun *lun;
7610 struct scsi_report_supported_tmf *cdb;
7611 struct scsi_report_supported_tmf_data *data;
7613 int alloc_len, total_len;
7615 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7617 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7618 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7620 retval = CTL_RETVAL_COMPLETE;
7622 total_len = sizeof(struct scsi_report_supported_tmf_data);
7623 alloc_len = scsi_4btoul(cdb->length);
7625 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7627 ctsio->kern_sg_entries = 0;
7629 if (total_len < alloc_len) {
7630 ctsio->residual = alloc_len - total_len;
7631 ctsio->kern_data_len = total_len;
7632 ctsio->kern_total_len = total_len;
7634 ctsio->residual = 0;
7635 ctsio->kern_data_len = alloc_len;
7636 ctsio->kern_total_len = alloc_len;
7638 ctsio->kern_data_resid = 0;
7639 ctsio->kern_rel_offset = 0;
7641 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7642 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7643 data->byte2 |= RST_ITNRS;
7645 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7646 ctsio->be_move_done = ctl_config_move_done;
7648 ctl_datamove((union ctl_io *)ctsio);
7653 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7655 struct ctl_lun *lun;
7656 struct scsi_report_timestamp *cdb;
7657 struct scsi_report_timestamp_data *data;
7661 int alloc_len, total_len;
7663 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7665 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7666 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7668 retval = CTL_RETVAL_COMPLETE;
7670 total_len = sizeof(struct scsi_report_timestamp_data);
7671 alloc_len = scsi_4btoul(cdb->length);
7673 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7675 ctsio->kern_sg_entries = 0;
7677 if (total_len < alloc_len) {
7678 ctsio->residual = alloc_len - total_len;
7679 ctsio->kern_data_len = total_len;
7680 ctsio->kern_total_len = total_len;
7682 ctsio->residual = 0;
7683 ctsio->kern_data_len = alloc_len;
7684 ctsio->kern_total_len = alloc_len;
7686 ctsio->kern_data_resid = 0;
7687 ctsio->kern_rel_offset = 0;
7689 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7690 scsi_ulto2b(sizeof(*data) - 2, data->length);
7691 data->origin = RTS_ORIG_OUTSIDE;
7693 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7694 scsi_ulto4b(timestamp >> 16, data->timestamp);
7695 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7697 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7698 ctsio->be_move_done = ctl_config_move_done;
7700 ctl_datamove((union ctl_io *)ctsio);
7705 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7707 struct scsi_per_res_in *cdb;
7708 int alloc_len, total_len = 0;
7709 /* struct scsi_per_res_in_rsrv in_data; */
7710 struct ctl_lun *lun;
7711 struct ctl_softc *softc;
7713 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7715 softc = control_softc;
7717 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7719 alloc_len = scsi_2btoul(cdb->length);
7721 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7724 mtx_lock(&lun->lun_lock);
7725 switch (cdb->action) {
7726 case SPRI_RK: /* read keys */
7727 total_len = sizeof(struct scsi_per_res_in_keys) +
7729 sizeof(struct scsi_per_res_key);
7731 case SPRI_RR: /* read reservation */
7732 if (lun->flags & CTL_LUN_PR_RESERVED)
7733 total_len = sizeof(struct scsi_per_res_in_rsrv);
7735 total_len = sizeof(struct scsi_per_res_in_header);
7737 case SPRI_RC: /* report capabilities */
7738 total_len = sizeof(struct scsi_per_res_cap);
7740 case SPRI_RS: /* read full status */
7741 total_len = sizeof(struct scsi_per_res_in_header) +
7742 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7746 panic("Invalid PR type %x", cdb->action);
7748 mtx_unlock(&lun->lun_lock);
7750 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7752 if (total_len < alloc_len) {
7753 ctsio->residual = alloc_len - total_len;
7754 ctsio->kern_data_len = total_len;
7755 ctsio->kern_total_len = total_len;
7757 ctsio->residual = 0;
7758 ctsio->kern_data_len = alloc_len;
7759 ctsio->kern_total_len = alloc_len;
7762 ctsio->kern_data_resid = 0;
7763 ctsio->kern_rel_offset = 0;
7764 ctsio->kern_sg_entries = 0;
7766 mtx_lock(&lun->lun_lock);
7767 switch (cdb->action) {
7768 case SPRI_RK: { // read keys
7769 struct scsi_per_res_in_keys *res_keys;
7772 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7775 * We had to drop the lock to allocate our buffer, which
7776 * leaves time for someone to come in with another
7777 * persistent reservation. (That is unlikely, though,
7778 * since this should be the only persistent reservation
7779 * command active right now.)
7781 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7782 (lun->pr_key_count *
7783 sizeof(struct scsi_per_res_key)))){
7784 mtx_unlock(&lun->lun_lock);
7785 free(ctsio->kern_data_ptr, M_CTL);
7786 printf("%s: reservation length changed, retrying\n",
7791 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7793 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7794 lun->pr_key_count, res_keys->header.length);
7796 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7797 if (!lun->per_res[i].registered)
7801 * We used lun->pr_key_count to calculate the
7802 * size to allocate. If it turns out the number of
7803 * initiators with the registered flag set is
7804 * larger than that (i.e. they haven't been kept in
7805 * sync), we've got a problem.
7807 if (key_count >= lun->pr_key_count) {
7809 csevent_log(CSC_CTL | CSC_SHELF_SW |
7811 csevent_LogType_Fault,
7812 csevent_AlertLevel_Yellow,
7813 csevent_FRU_ShelfController,
7814 csevent_FRU_Firmware,
7815 csevent_FRU_Unknown,
7816 "registered keys %d >= key "
7817 "count %d", key_count,
7823 memcpy(res_keys->keys[key_count].key,
7824 lun->per_res[i].res_key.key,
7825 ctl_min(sizeof(res_keys->keys[key_count].key),
7826 sizeof(lun->per_res[i].res_key)));
7831 case SPRI_RR: { // read reservation
7832 struct scsi_per_res_in_rsrv *res;
7833 int tmp_len, header_only;
7835 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7837 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7839 if (lun->flags & CTL_LUN_PR_RESERVED)
7841 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7842 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7843 res->header.length);
7846 tmp_len = sizeof(struct scsi_per_res_in_header);
7847 scsi_ulto4b(0, res->header.length);
7852 * We had to drop the lock to allocate our buffer, which
7853 * leaves time for someone to come in with another
7854 * persistent reservation. (That is unlikely, though,
7855 * since this should be the only persistent reservation
7856 * command active right now.)
7858 if (tmp_len != total_len) {
7859 mtx_unlock(&lun->lun_lock);
7860 free(ctsio->kern_data_ptr, M_CTL);
7861 printf("%s: reservation status changed, retrying\n",
7867 * No reservation held, so we're done.
7869 if (header_only != 0)
7873 * If the registration is an All Registrants type, the key
7874 * is 0, since it doesn't really matter.
7876 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7877 memcpy(res->data.reservation,
7878 &lun->per_res[lun->pr_res_idx].res_key,
7879 sizeof(struct scsi_per_res_key));
7881 res->data.scopetype = lun->res_type;
7884 case SPRI_RC: //report capabilities
7886 struct scsi_per_res_cap *res_cap;
7889 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7890 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7891 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7892 type_mask = SPRI_TM_WR_EX_AR |
7898 scsi_ulto2b(type_mask, res_cap->type_mask);
7901 case SPRI_RS: { // read full status
7902 struct scsi_per_res_in_full *res_status;
7903 struct scsi_per_res_in_full_desc *res_desc;
7904 struct ctl_port *port;
7907 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7910 * We had to drop the lock to allocate our buffer, which
7911 * leaves time for someone to come in with another
7912 * persistent reservation. (That is unlikely, though,
7913 * since this should be the only persistent reservation
7914 * command active right now.)
7916 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7917 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7918 lun->pr_key_count)){
7919 mtx_unlock(&lun->lun_lock);
7920 free(ctsio->kern_data_ptr, M_CTL);
7921 printf("%s: reservation length changed, retrying\n",
7926 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7928 res_desc = &res_status->desc[0];
7929 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7930 if (!lun->per_res[i].registered)
7933 memcpy(&res_desc->res_key, &lun->per_res[i].res_key.key,
7934 sizeof(res_desc->res_key));
7935 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7936 (lun->pr_res_idx == i ||
7937 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7938 res_desc->flags = SPRI_FULL_R_HOLDER;
7939 res_desc->scopetype = lun->res_type;
7941 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7942 res_desc->rel_trgt_port_id);
7944 port = softc->ctl_ports[
7945 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
7947 len = ctl_create_iid(port,
7948 i % CTL_MAX_INIT_PER_PORT,
7949 res_desc->transport_id);
7950 scsi_ulto4b(len, res_desc->additional_length);
7951 res_desc = (struct scsi_per_res_in_full_desc *)
7952 &res_desc->transport_id[len];
7954 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7955 res_status->header.length);
7960 * This is a bug, because we just checked for this above,
7961 * and should have returned an error.
7963 panic("Invalid PR type %x", cdb->action);
7964 break; /* NOTREACHED */
7966 mtx_unlock(&lun->lun_lock);
7968 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7969 ctsio->be_move_done = ctl_config_move_done;
7971 CTL_DEBUG_PRINT(("buf = %x %x %x %x %x %x %x %x\n",
7972 ctsio->kern_data_ptr[0], ctsio->kern_data_ptr[1],
7973 ctsio->kern_data_ptr[2], ctsio->kern_data_ptr[3],
7974 ctsio->kern_data_ptr[4], ctsio->kern_data_ptr[5],
7975 ctsio->kern_data_ptr[6], ctsio->kern_data_ptr[7]));
7977 ctl_datamove((union ctl_io *)ctsio);
7979 return (CTL_RETVAL_COMPLETE);
7983 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7987 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7988 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7989 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7990 struct scsi_per_res_out_parms* param)
7992 union ctl_ha_msg persis_io;
7998 mtx_lock(&lun->lun_lock);
7999 if (sa_res_key == 0) {
8000 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8001 /* validate scope and type */
8002 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8004 mtx_unlock(&lun->lun_lock);
8005 ctl_set_invalid_field(/*ctsio*/ ctsio,
8011 ctl_done((union ctl_io *)ctsio);
8015 if (type>8 || type==2 || type==4 || type==0) {
8016 mtx_unlock(&lun->lun_lock);
8017 ctl_set_invalid_field(/*ctsio*/ ctsio,
8023 ctl_done((union ctl_io *)ctsio);
8027 /* temporarily unregister this nexus */
8028 lun->per_res[residx].registered = 0;
8031 * Unregister everybody else and build UA for
8034 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8035 if (lun->per_res[i].registered == 0)
8039 && i <CTL_MAX_INITIATORS)
8040 lun->pending_ua[i] |=
8042 else if (persis_offset
8043 && i >= persis_offset)
8044 lun->pending_ua[i-persis_offset] |=
8046 lun->per_res[i].registered = 0;
8047 memset(&lun->per_res[i].res_key, 0,
8048 sizeof(struct scsi_per_res_key));
8050 lun->per_res[residx].registered = 1;
8051 lun->pr_key_count = 1;
8052 lun->res_type = type;
8053 if (lun->res_type != SPR_TYPE_WR_EX_AR
8054 && lun->res_type != SPR_TYPE_EX_AC_AR)
8055 lun->pr_res_idx = residx;
8057 /* send msg to other side */
8058 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8059 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8060 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8061 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8062 persis_io.pr.pr_info.res_type = type;
8063 memcpy(persis_io.pr.pr_info.sa_res_key,
8064 param->serv_act_res_key,
8065 sizeof(param->serv_act_res_key));
8066 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8067 &persis_io, sizeof(persis_io), 0)) >
8068 CTL_HA_STATUS_SUCCESS) {
8069 printf("CTL:Persis Out error returned "
8070 "from ctl_ha_msg_send %d\n",
8074 /* not all registrants */
8075 mtx_unlock(&lun->lun_lock);
8076 free(ctsio->kern_data_ptr, M_CTL);
8077 ctl_set_invalid_field(ctsio,
8083 ctl_done((union ctl_io *)ctsio);
8086 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8087 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8090 if (res_key == sa_res_key) {
8093 * The spec implies this is not good but doesn't
8094 * say what to do. There are two choices either
8095 * generate a res conflict or check condition
8096 * with illegal field in parameter data. Since
8097 * that is what is done when the sa_res_key is
8098 * zero I'll take that approach since this has
8099 * to do with the sa_res_key.
8101 mtx_unlock(&lun->lun_lock);
8102 free(ctsio->kern_data_ptr, M_CTL);
8103 ctl_set_invalid_field(ctsio,
8109 ctl_done((union ctl_io *)ctsio);
8113 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8114 if (lun->per_res[i].registered
8115 && memcmp(param->serv_act_res_key,
8116 lun->per_res[i].res_key.key,
8117 sizeof(struct scsi_per_res_key)) != 0)
8121 lun->per_res[i].registered = 0;
8122 memset(&lun->per_res[i].res_key, 0,
8123 sizeof(struct scsi_per_res_key));
8124 lun->pr_key_count--;
8126 if (!persis_offset && i < CTL_MAX_INITIATORS)
8127 lun->pending_ua[i] |= CTL_UA_REG_PREEMPT;
8128 else if (persis_offset && i >= persis_offset)
8129 lun->pending_ua[i-persis_offset] |=
8133 mtx_unlock(&lun->lun_lock);
8134 free(ctsio->kern_data_ptr, M_CTL);
8135 ctl_set_reservation_conflict(ctsio);
8136 ctl_done((union ctl_io *)ctsio);
8137 return (CTL_RETVAL_COMPLETE);
8139 /* send msg to other side */
8140 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8141 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8142 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8143 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8144 persis_io.pr.pr_info.res_type = type;
8145 memcpy(persis_io.pr.pr_info.sa_res_key,
8146 param->serv_act_res_key,
8147 sizeof(param->serv_act_res_key));
8148 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8149 &persis_io, sizeof(persis_io), 0)) >
8150 CTL_HA_STATUS_SUCCESS) {
8151 printf("CTL:Persis Out error returned from "
8152 "ctl_ha_msg_send %d\n", isc_retval);
8155 /* Reserved but not all registrants */
8156 /* sa_res_key is res holder */
8157 if (memcmp(param->serv_act_res_key,
8158 lun->per_res[lun->pr_res_idx].res_key.key,
8159 sizeof(struct scsi_per_res_key)) == 0) {
8160 /* validate scope and type */
8161 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8163 mtx_unlock(&lun->lun_lock);
8164 ctl_set_invalid_field(/*ctsio*/ ctsio,
8170 ctl_done((union ctl_io *)ctsio);
8174 if (type>8 || type==2 || type==4 || type==0) {
8175 mtx_unlock(&lun->lun_lock);
8176 ctl_set_invalid_field(/*ctsio*/ ctsio,
8182 ctl_done((union ctl_io *)ctsio);
8188 * if sa_res_key != res_key remove all
8189 * registrants w/sa_res_key and generate UA
8190 * for these registrants(Registrations
8191 * Preempted) if it wasn't an exclusive
8192 * reservation generate UA(Reservations
8193 * Preempted) for all other registered nexuses
8194 * if the type has changed. Establish the new
8195 * reservation and holder. If res_key and
8196 * sa_res_key are the same do the above
8197 * except don't unregister the res holder.
8201 * Temporarily unregister so it won't get
8202 * removed or UA generated
8204 lun->per_res[residx].registered = 0;
8205 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8206 if (lun->per_res[i].registered == 0)
8209 if (memcmp(param->serv_act_res_key,
8210 lun->per_res[i].res_key.key,
8211 sizeof(struct scsi_per_res_key)) == 0) {
8212 lun->per_res[i].registered = 0;
8213 memset(&lun->per_res[i].res_key,
8215 sizeof(struct scsi_per_res_key));
8216 lun->pr_key_count--;
8219 && i < CTL_MAX_INITIATORS)
8220 lun->pending_ua[i] |=
8222 else if (persis_offset
8223 && i >= persis_offset)
8224 lun->pending_ua[i-persis_offset] |=
8226 } else if (type != lun->res_type
8227 && (lun->res_type == SPR_TYPE_WR_EX_RO
8228 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8230 && i < CTL_MAX_INITIATORS)
8231 lun->pending_ua[i] |=
8233 else if (persis_offset
8234 && i >= persis_offset)
8240 lun->per_res[residx].registered = 1;
8241 lun->res_type = type;
8242 if (lun->res_type != SPR_TYPE_WR_EX_AR
8243 && lun->res_type != SPR_TYPE_EX_AC_AR)
8244 lun->pr_res_idx = residx;
8246 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8248 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8249 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8250 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8251 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8252 persis_io.pr.pr_info.res_type = type;
8253 memcpy(persis_io.pr.pr_info.sa_res_key,
8254 param->serv_act_res_key,
8255 sizeof(param->serv_act_res_key));
8256 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8257 &persis_io, sizeof(persis_io), 0)) >
8258 CTL_HA_STATUS_SUCCESS) {
8259 printf("CTL:Persis Out error returned "
8260 "from ctl_ha_msg_send %d\n",
8265 * sa_res_key is not the res holder just
8266 * remove registrants
8270 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8271 if (memcmp(param->serv_act_res_key,
8272 lun->per_res[i].res_key.key,
8273 sizeof(struct scsi_per_res_key)) != 0)
8277 lun->per_res[i].registered = 0;
8278 memset(&lun->per_res[i].res_key, 0,
8279 sizeof(struct scsi_per_res_key));
8280 lun->pr_key_count--;
8283 && i < CTL_MAX_INITIATORS)
8284 lun->pending_ua[i] |=
8286 else if (persis_offset
8287 && i >= persis_offset)
8288 lun->pending_ua[i-persis_offset] |=
8293 mtx_unlock(&lun->lun_lock);
8294 free(ctsio->kern_data_ptr, M_CTL);
8295 ctl_set_reservation_conflict(ctsio);
8296 ctl_done((union ctl_io *)ctsio);
8299 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8300 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8301 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8302 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8303 persis_io.pr.pr_info.res_type = type;
8304 memcpy(persis_io.pr.pr_info.sa_res_key,
8305 param->serv_act_res_key,
8306 sizeof(param->serv_act_res_key));
8307 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8308 &persis_io, sizeof(persis_io), 0)) >
8309 CTL_HA_STATUS_SUCCESS) {
8310 printf("CTL:Persis Out error returned "
8311 "from ctl_ha_msg_send %d\n",
8317 lun->PRGeneration++;
8318 mtx_unlock(&lun->lun_lock);
8324 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8328 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8329 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8330 || memcmp(&lun->per_res[lun->pr_res_idx].res_key,
8331 msg->pr.pr_info.sa_res_key,
8332 sizeof(struct scsi_per_res_key)) != 0) {
8333 uint64_t sa_res_key;
8334 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8336 if (sa_res_key == 0) {
8337 /* temporarily unregister this nexus */
8338 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8341 * Unregister everybody else and build UA for
8344 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8345 if (lun->per_res[i].registered == 0)
8349 && i < CTL_MAX_INITIATORS)
8350 lun->pending_ua[i] |=
8352 else if (persis_offset && i >= persis_offset)
8353 lun->pending_ua[i - persis_offset] |=
8355 lun->per_res[i].registered = 0;
8356 memset(&lun->per_res[i].res_key, 0,
8357 sizeof(struct scsi_per_res_key));
8360 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8361 lun->pr_key_count = 1;
8362 lun->res_type = msg->pr.pr_info.res_type;
8363 if (lun->res_type != SPR_TYPE_WR_EX_AR
8364 && lun->res_type != SPR_TYPE_EX_AC_AR)
8365 lun->pr_res_idx = msg->pr.pr_info.residx;
8367 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8368 if (memcmp(msg->pr.pr_info.sa_res_key,
8369 lun->per_res[i].res_key.key,
8370 sizeof(struct scsi_per_res_key)) != 0)
8373 lun->per_res[i].registered = 0;
8374 memset(&lun->per_res[i].res_key, 0,
8375 sizeof(struct scsi_per_res_key));
8376 lun->pr_key_count--;
8379 && i < persis_offset)
8380 lun->pending_ua[i] |=
8382 else if (persis_offset
8383 && i >= persis_offset)
8384 lun->pending_ua[i - persis_offset] |=
8390 * Temporarily unregister so it won't get removed
8393 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8394 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8395 if (lun->per_res[i].registered == 0)
8398 if (memcmp(msg->pr.pr_info.sa_res_key,
8399 lun->per_res[i].res_key.key,
8400 sizeof(struct scsi_per_res_key)) == 0) {
8401 lun->per_res[i].registered = 0;
8402 memset(&lun->per_res[i].res_key, 0,
8403 sizeof(struct scsi_per_res_key));
8404 lun->pr_key_count--;
8406 && i < CTL_MAX_INITIATORS)
8407 lun->pending_ua[i] |=
8409 else if (persis_offset
8410 && i >= persis_offset)
8411 lun->pending_ua[i - persis_offset] |=
8413 } else if (msg->pr.pr_info.res_type != lun->res_type
8414 && (lun->res_type == SPR_TYPE_WR_EX_RO
8415 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8417 && i < persis_offset)
8418 lun->pending_ua[i] |=
8420 else if (persis_offset
8421 && i >= persis_offset)
8422 lun->pending_ua[i - persis_offset] |=
8426 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8427 lun->res_type = msg->pr.pr_info.res_type;
8428 if (lun->res_type != SPR_TYPE_WR_EX_AR
8429 && lun->res_type != SPR_TYPE_EX_AC_AR)
8430 lun->pr_res_idx = msg->pr.pr_info.residx;
8432 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8434 lun->PRGeneration++;
8440 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8444 u_int32_t param_len;
8445 struct scsi_per_res_out *cdb;
8446 struct ctl_lun *lun;
8447 struct scsi_per_res_out_parms* param;
8448 struct ctl_softc *softc;
8450 uint64_t res_key, sa_res_key;
8452 union ctl_ha_msg persis_io;
8455 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8457 retval = CTL_RETVAL_COMPLETE;
8459 softc = control_softc;
8461 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8462 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8465 * We only support whole-LUN scope. The scope & type are ignored for
8466 * register, register and ignore existing key and clear.
8467 * We sometimes ignore scope and type on preempts too!!
8468 * Verify reservation type here as well.
8470 type = cdb->scope_type & SPR_TYPE_MASK;
8471 if ((cdb->action == SPRO_RESERVE)
8472 || (cdb->action == SPRO_RELEASE)) {
8473 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8474 ctl_set_invalid_field(/*ctsio*/ ctsio,
8480 ctl_done((union ctl_io *)ctsio);
8481 return (CTL_RETVAL_COMPLETE);
8484 if (type>8 || type==2 || type==4 || type==0) {
8485 ctl_set_invalid_field(/*ctsio*/ ctsio,
8491 ctl_done((union ctl_io *)ctsio);
8492 return (CTL_RETVAL_COMPLETE);
8496 param_len = scsi_4btoul(cdb->length);
8498 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8499 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8500 ctsio->kern_data_len = param_len;
8501 ctsio->kern_total_len = param_len;
8502 ctsio->kern_data_resid = 0;
8503 ctsio->kern_rel_offset = 0;
8504 ctsio->kern_sg_entries = 0;
8505 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8506 ctsio->be_move_done = ctl_config_move_done;
8507 ctl_datamove((union ctl_io *)ctsio);
8509 return (CTL_RETVAL_COMPLETE);
8512 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8514 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8515 res_key = scsi_8btou64(param->res_key.key);
8516 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8519 * Validate the reservation key here except for SPRO_REG_IGNO
8520 * This must be done for all other service actions
8522 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8523 mtx_lock(&lun->lun_lock);
8524 if (lun->per_res[residx].registered) {
8525 if (memcmp(param->res_key.key,
8526 lun->per_res[residx].res_key.key,
8527 ctl_min(sizeof(param->res_key),
8528 sizeof(lun->per_res[residx].res_key))) != 0) {
8530 * The current key passed in doesn't match
8531 * the one the initiator previously
8534 mtx_unlock(&lun->lun_lock);
8535 free(ctsio->kern_data_ptr, M_CTL);
8536 ctl_set_reservation_conflict(ctsio);
8537 ctl_done((union ctl_io *)ctsio);
8538 return (CTL_RETVAL_COMPLETE);
8540 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8542 * We are not registered
8544 mtx_unlock(&lun->lun_lock);
8545 free(ctsio->kern_data_ptr, M_CTL);
8546 ctl_set_reservation_conflict(ctsio);
8547 ctl_done((union ctl_io *)ctsio);
8548 return (CTL_RETVAL_COMPLETE);
8549 } else if (res_key != 0) {
8551 * We are not registered and trying to register but
8552 * the register key isn't zero.
8554 mtx_unlock(&lun->lun_lock);
8555 free(ctsio->kern_data_ptr, M_CTL);
8556 ctl_set_reservation_conflict(ctsio);
8557 ctl_done((union ctl_io *)ctsio);
8558 return (CTL_RETVAL_COMPLETE);
8560 mtx_unlock(&lun->lun_lock);
8563 switch (cdb->action & SPRO_ACTION_MASK) {
8565 case SPRO_REG_IGNO: {
8568 printf("Registration received\n");
8572 * We don't support any of these options, as we report in
8573 * the read capabilities request (see
8574 * ctl_persistent_reserve_in(), above).
8576 if ((param->flags & SPR_SPEC_I_PT)
8577 || (param->flags & SPR_ALL_TG_PT)
8578 || (param->flags & SPR_APTPL)) {
8581 if (param->flags & SPR_APTPL)
8583 else if (param->flags & SPR_ALL_TG_PT)
8585 else /* SPR_SPEC_I_PT */
8588 free(ctsio->kern_data_ptr, M_CTL);
8589 ctl_set_invalid_field(ctsio,
8595 ctl_done((union ctl_io *)ctsio);
8596 return (CTL_RETVAL_COMPLETE);
8599 mtx_lock(&lun->lun_lock);
8602 * The initiator wants to clear the
8605 if (sa_res_key == 0) {
8607 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8608 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8609 && !lun->per_res[residx].registered)) {
8610 mtx_unlock(&lun->lun_lock);
8614 lun->per_res[residx].registered = 0;
8615 memset(&lun->per_res[residx].res_key,
8616 0, sizeof(lun->per_res[residx].res_key));
8617 lun->pr_key_count--;
8619 if (residx == lun->pr_res_idx) {
8620 lun->flags &= ~CTL_LUN_PR_RESERVED;
8621 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8623 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8624 || lun->res_type == SPR_TYPE_EX_AC_RO)
8625 && lun->pr_key_count) {
8627 * If the reservation is a registrants
8628 * only type we need to generate a UA
8629 * for other registered inits. The
8630 * sense code should be RESERVATIONS
8634 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8636 i+persis_offset].registered
8639 lun->pending_ua[i] |=
8644 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8645 if (lun->pr_key_count==0) {
8646 lun->flags &= ~CTL_LUN_PR_RESERVED;
8648 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8651 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8652 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8653 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8654 persis_io.pr.pr_info.residx = residx;
8655 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8656 &persis_io, sizeof(persis_io), 0 )) >
8657 CTL_HA_STATUS_SUCCESS) {
8658 printf("CTL:Persis Out error returned from "
8659 "ctl_ha_msg_send %d\n", isc_retval);
8661 } else /* sa_res_key != 0 */ {
8664 * If we aren't registered currently then increment
8665 * the key count and set the registered flag.
8667 if (!lun->per_res[residx].registered) {
8668 lun->pr_key_count++;
8669 lun->per_res[residx].registered = 1;
8672 memcpy(&lun->per_res[residx].res_key,
8673 param->serv_act_res_key,
8674 ctl_min(sizeof(param->serv_act_res_key),
8675 sizeof(lun->per_res[residx].res_key)));
8677 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8678 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8679 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8680 persis_io.pr.pr_info.residx = residx;
8681 memcpy(persis_io.pr.pr_info.sa_res_key,
8682 param->serv_act_res_key,
8683 sizeof(param->serv_act_res_key));
8684 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8685 &persis_io, sizeof(persis_io), 0)) >
8686 CTL_HA_STATUS_SUCCESS) {
8687 printf("CTL:Persis Out error returned from "
8688 "ctl_ha_msg_send %d\n", isc_retval);
8691 lun->PRGeneration++;
8692 mtx_unlock(&lun->lun_lock);
8698 printf("Reserve executed type %d\n", type);
8700 mtx_lock(&lun->lun_lock);
8701 if (lun->flags & CTL_LUN_PR_RESERVED) {
8703 * if this isn't the reservation holder and it's
8704 * not a "all registrants" type or if the type is
8705 * different then we have a conflict
8707 if ((lun->pr_res_idx != residx
8708 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8709 || lun->res_type != type) {
8710 mtx_unlock(&lun->lun_lock);
8711 free(ctsio->kern_data_ptr, M_CTL);
8712 ctl_set_reservation_conflict(ctsio);
8713 ctl_done((union ctl_io *)ctsio);
8714 return (CTL_RETVAL_COMPLETE);
8716 mtx_unlock(&lun->lun_lock);
8717 } else /* create a reservation */ {
8719 * If it's not an "all registrants" type record
8720 * reservation holder
8722 if (type != SPR_TYPE_WR_EX_AR
8723 && type != SPR_TYPE_EX_AC_AR)
8724 lun->pr_res_idx = residx; /* Res holder */
8726 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8728 lun->flags |= CTL_LUN_PR_RESERVED;
8729 lun->res_type = type;
8731 mtx_unlock(&lun->lun_lock);
8733 /* send msg to other side */
8734 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8735 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8736 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8737 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8738 persis_io.pr.pr_info.res_type = type;
8739 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8740 &persis_io, sizeof(persis_io), 0)) >
8741 CTL_HA_STATUS_SUCCESS) {
8742 printf("CTL:Persis Out error returned from "
8743 "ctl_ha_msg_send %d\n", isc_retval);
8749 mtx_lock(&lun->lun_lock);
8750 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8751 /* No reservation exists return good status */
8752 mtx_unlock(&lun->lun_lock);
8756 * Is this nexus a reservation holder?
8758 if (lun->pr_res_idx != residx
8759 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8761 * not a res holder return good status but
8764 mtx_unlock(&lun->lun_lock);
8768 if (lun->res_type != type) {
8769 mtx_unlock(&lun->lun_lock);
8770 free(ctsio->kern_data_ptr, M_CTL);
8771 ctl_set_illegal_pr_release(ctsio);
8772 ctl_done((union ctl_io *)ctsio);
8773 return (CTL_RETVAL_COMPLETE);
8776 /* okay to release */
8777 lun->flags &= ~CTL_LUN_PR_RESERVED;
8778 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8782 * if this isn't an exclusive access
8783 * res generate UA for all other
8786 if (type != SPR_TYPE_EX_AC
8787 && type != SPR_TYPE_WR_EX) {
8789 * temporarily unregister so we don't generate UA
8791 lun->per_res[residx].registered = 0;
8793 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8794 if (lun->per_res[i+persis_offset].registered
8797 lun->pending_ua[i] |=
8801 lun->per_res[residx].registered = 1;
8803 mtx_unlock(&lun->lun_lock);
8804 /* Send msg to other side */
8805 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8806 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8807 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8808 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8809 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8810 printf("CTL:Persis Out error returned from "
8811 "ctl_ha_msg_send %d\n", isc_retval);
8816 /* send msg to other side */
8818 mtx_lock(&lun->lun_lock);
8819 lun->flags &= ~CTL_LUN_PR_RESERVED;
8821 lun->pr_key_count = 0;
8822 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8825 memset(&lun->per_res[residx].res_key,
8826 0, sizeof(lun->per_res[residx].res_key));
8827 lun->per_res[residx].registered = 0;
8829 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8830 if (lun->per_res[i].registered) {
8831 if (!persis_offset && i < CTL_MAX_INITIATORS)
8832 lun->pending_ua[i] |=
8834 else if (persis_offset && i >= persis_offset)
8835 lun->pending_ua[i-persis_offset] |=
8838 memset(&lun->per_res[i].res_key,
8839 0, sizeof(struct scsi_per_res_key));
8840 lun->per_res[i].registered = 0;
8842 lun->PRGeneration++;
8843 mtx_unlock(&lun->lun_lock);
8844 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8845 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8846 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8847 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8848 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8849 printf("CTL:Persis Out error returned from "
8850 "ctl_ha_msg_send %d\n", isc_retval);
8854 case SPRO_PREEMPT: {
8857 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8858 residx, ctsio, cdb, param);
8860 return (CTL_RETVAL_COMPLETE);
8864 panic("Invalid PR type %x", cdb->action);
8868 free(ctsio->kern_data_ptr, M_CTL);
8869 ctl_set_success(ctsio);
8870 ctl_done((union ctl_io *)ctsio);
8876 * This routine is for handling a message from the other SC pertaining to
8877 * persistent reserve out. All the error checking will have been done
8878 * so only perorming the action need be done here to keep the two
8882 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8884 struct ctl_lun *lun;
8885 struct ctl_softc *softc;
8889 softc = control_softc;
8891 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8892 lun = softc->ctl_luns[targ_lun];
8893 mtx_lock(&lun->lun_lock);
8894 switch(msg->pr.pr_info.action) {
8895 case CTL_PR_REG_KEY:
8896 if (!lun->per_res[msg->pr.pr_info.residx].registered) {
8897 lun->per_res[msg->pr.pr_info.residx].registered = 1;
8898 lun->pr_key_count++;
8900 lun->PRGeneration++;
8901 memcpy(&lun->per_res[msg->pr.pr_info.residx].res_key,
8902 msg->pr.pr_info.sa_res_key,
8903 sizeof(struct scsi_per_res_key));
8906 case CTL_PR_UNREG_KEY:
8907 lun->per_res[msg->pr.pr_info.residx].registered = 0;
8908 memset(&lun->per_res[msg->pr.pr_info.residx].res_key,
8909 0, sizeof(struct scsi_per_res_key));
8910 lun->pr_key_count--;
8912 /* XXX Need to see if the reservation has been released */
8913 /* if so do we need to generate UA? */
8914 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8915 lun->flags &= ~CTL_LUN_PR_RESERVED;
8916 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8918 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8919 || lun->res_type == SPR_TYPE_EX_AC_RO)
8920 && lun->pr_key_count) {
8922 * If the reservation is a registrants
8923 * only type we need to generate a UA
8924 * for other registered inits. The
8925 * sense code should be RESERVATIONS
8929 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8931 persis_offset].registered == 0)
8934 lun->pending_ua[i] |=
8939 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8940 if (lun->pr_key_count==0) {
8941 lun->flags &= ~CTL_LUN_PR_RESERVED;
8943 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8946 lun->PRGeneration++;
8949 case CTL_PR_RESERVE:
8950 lun->flags |= CTL_LUN_PR_RESERVED;
8951 lun->res_type = msg->pr.pr_info.res_type;
8952 lun->pr_res_idx = msg->pr.pr_info.residx;
8956 case CTL_PR_RELEASE:
8958 * if this isn't an exclusive access res generate UA for all
8959 * other registrants.
8961 if (lun->res_type != SPR_TYPE_EX_AC
8962 && lun->res_type != SPR_TYPE_WR_EX) {
8963 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8964 if (lun->per_res[i+persis_offset].registered)
8965 lun->pending_ua[i] |=
8969 lun->flags &= ~CTL_LUN_PR_RESERVED;
8970 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8974 case CTL_PR_PREEMPT:
8975 ctl_pro_preempt_other(lun, msg);
8978 lun->flags &= ~CTL_LUN_PR_RESERVED;
8980 lun->pr_key_count = 0;
8981 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8983 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8984 if (lun->per_res[i].registered == 0)
8987 && i < CTL_MAX_INITIATORS)
8988 lun->pending_ua[i] |= CTL_UA_RES_PREEMPT;
8989 else if (persis_offset
8990 && i >= persis_offset)
8991 lun->pending_ua[i-persis_offset] |=
8993 memset(&lun->per_res[i].res_key, 0,
8994 sizeof(struct scsi_per_res_key));
8995 lun->per_res[i].registered = 0;
8997 lun->PRGeneration++;
9001 mtx_unlock(&lun->lun_lock);
9005 ctl_read_write(struct ctl_scsiio *ctsio)
9007 struct ctl_lun *lun;
9008 struct ctl_lba_len_flags *lbalen;
9010 uint32_t num_blocks;
9014 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9016 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
9019 retval = CTL_RETVAL_COMPLETE;
9021 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
9022 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
9023 if (lun->flags & CTL_LUN_PR_RESERVED && isread) {
9027 * XXX KDM need a lock here.
9029 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
9030 if ((lun->res_type == SPR_TYPE_EX_AC
9031 && residx != lun->pr_res_idx)
9032 || ((lun->res_type == SPR_TYPE_EX_AC_RO
9033 || lun->res_type == SPR_TYPE_EX_AC_AR)
9034 && !lun->per_res[residx].registered)) {
9035 ctl_set_reservation_conflict(ctsio);
9036 ctl_done((union ctl_io *)ctsio);
9037 return (CTL_RETVAL_COMPLETE);
9041 switch (ctsio->cdb[0]) {
9044 struct scsi_rw_6 *cdb;
9046 cdb = (struct scsi_rw_6 *)ctsio->cdb;
9048 lba = scsi_3btoul(cdb->addr);
9049 /* only 5 bits are valid in the most significant address byte */
9051 num_blocks = cdb->length;
9053 * This is correct according to SBC-2.
9055 if (num_blocks == 0)
9061 struct scsi_rw_10 *cdb;
9063 cdb = (struct scsi_rw_10 *)ctsio->cdb;
9064 if (cdb->byte2 & SRW10_FUA)
9065 flags |= CTL_LLF_FUA;
9066 if (cdb->byte2 & SRW10_DPO)
9067 flags |= CTL_LLF_DPO;
9068 lba = scsi_4btoul(cdb->addr);
9069 num_blocks = scsi_2btoul(cdb->length);
9072 case WRITE_VERIFY_10: {
9073 struct scsi_write_verify_10 *cdb;
9075 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
9076 flags |= CTL_LLF_FUA;
9077 if (cdb->byte2 & SWV_DPO)
9078 flags |= CTL_LLF_DPO;
9079 lba = scsi_4btoul(cdb->addr);
9080 num_blocks = scsi_2btoul(cdb->length);
9085 struct scsi_rw_12 *cdb;
9087 cdb = (struct scsi_rw_12 *)ctsio->cdb;
9088 if (cdb->byte2 & SRW12_FUA)
9089 flags |= CTL_LLF_FUA;
9090 if (cdb->byte2 & SRW12_DPO)
9091 flags |= CTL_LLF_DPO;
9092 lba = scsi_4btoul(cdb->addr);
9093 num_blocks = scsi_4btoul(cdb->length);
9096 case WRITE_VERIFY_12: {
9097 struct scsi_write_verify_12 *cdb;
9099 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
9100 flags |= CTL_LLF_FUA;
9101 if (cdb->byte2 & SWV_DPO)
9102 flags |= CTL_LLF_DPO;
9103 lba = scsi_4btoul(cdb->addr);
9104 num_blocks = scsi_4btoul(cdb->length);
9109 struct scsi_rw_16 *cdb;
9111 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9112 if (cdb->byte2 & SRW12_FUA)
9113 flags |= CTL_LLF_FUA;
9114 if (cdb->byte2 & SRW12_DPO)
9115 flags |= CTL_LLF_DPO;
9116 lba = scsi_8btou64(cdb->addr);
9117 num_blocks = scsi_4btoul(cdb->length);
9120 case WRITE_VERIFY_16: {
9121 struct scsi_write_verify_16 *cdb;
9123 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
9124 flags |= CTL_LLF_FUA;
9125 if (cdb->byte2 & SWV_DPO)
9126 flags |= CTL_LLF_DPO;
9127 lba = scsi_8btou64(cdb->addr);
9128 num_blocks = scsi_4btoul(cdb->length);
9133 * We got a command we don't support. This shouldn't
9134 * happen, commands should be filtered out above us.
9136 ctl_set_invalid_opcode(ctsio);
9137 ctl_done((union ctl_io *)ctsio);
9139 return (CTL_RETVAL_COMPLETE);
9140 break; /* NOTREACHED */
9144 * The first check is to make sure we're in bounds, the second
9145 * check is to catch wrap-around problems. If the lba + num blocks
9146 * is less than the lba, then we've wrapped around and the block
9147 * range is invalid anyway.
9149 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9150 || ((lba + num_blocks) < lba)) {
9151 ctl_set_lba_out_of_range(ctsio);
9152 ctl_done((union ctl_io *)ctsio);
9153 return (CTL_RETVAL_COMPLETE);
9157 * According to SBC-3, a transfer length of 0 is not an error.
9158 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9159 * translates to 256 blocks for those commands.
9161 if (num_blocks == 0) {
9162 ctl_set_success(ctsio);
9163 ctl_done((union ctl_io *)ctsio);
9164 return (CTL_RETVAL_COMPLETE);
9167 /* Set FUA and/or DPO if caches are disabled. */
9169 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9171 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
9173 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9175 flags |= CTL_LLF_FUA;
9178 lbalen = (struct ctl_lba_len_flags *)
9179 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9181 lbalen->len = num_blocks;
9182 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
9184 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9185 ctsio->kern_rel_offset = 0;
9187 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9189 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9195 ctl_cnw_cont(union ctl_io *io)
9197 struct ctl_scsiio *ctsio;
9198 struct ctl_lun *lun;
9199 struct ctl_lba_len_flags *lbalen;
9202 ctsio = &io->scsiio;
9203 ctsio->io_hdr.status = CTL_STATUS_NONE;
9204 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9205 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9206 lbalen = (struct ctl_lba_len_flags *)
9207 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9208 lbalen->flags &= ~CTL_LLF_COMPARE;
9209 lbalen->flags |= CTL_LLF_WRITE;
9211 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9212 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9217 ctl_cnw(struct ctl_scsiio *ctsio)
9219 struct ctl_lun *lun;
9220 struct ctl_lba_len_flags *lbalen;
9222 uint32_t num_blocks;
9225 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9227 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9230 retval = CTL_RETVAL_COMPLETE;
9232 switch (ctsio->cdb[0]) {
9233 case COMPARE_AND_WRITE: {
9234 struct scsi_compare_and_write *cdb;
9236 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9237 if (cdb->byte2 & SRW10_FUA)
9238 flags |= CTL_LLF_FUA;
9239 if (cdb->byte2 & SRW10_DPO)
9240 flags |= CTL_LLF_DPO;
9241 lba = scsi_8btou64(cdb->addr);
9242 num_blocks = cdb->length;
9247 * We got a command we don't support. This shouldn't
9248 * happen, commands should be filtered out above us.
9250 ctl_set_invalid_opcode(ctsio);
9251 ctl_done((union ctl_io *)ctsio);
9253 return (CTL_RETVAL_COMPLETE);
9254 break; /* NOTREACHED */
9258 * The first check is to make sure we're in bounds, the second
9259 * check is to catch wrap-around problems. If the lba + num blocks
9260 * is less than the lba, then we've wrapped around and the block
9261 * range is invalid anyway.
9263 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9264 || ((lba + num_blocks) < lba)) {
9265 ctl_set_lba_out_of_range(ctsio);
9266 ctl_done((union ctl_io *)ctsio);
9267 return (CTL_RETVAL_COMPLETE);
9271 * According to SBC-3, a transfer length of 0 is not an error.
9273 if (num_blocks == 0) {
9274 ctl_set_success(ctsio);
9275 ctl_done((union ctl_io *)ctsio);
9276 return (CTL_RETVAL_COMPLETE);
9279 /* Set FUA if write cache is disabled. */
9280 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9282 flags |= CTL_LLF_FUA;
9284 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9285 ctsio->kern_rel_offset = 0;
9288 * Set the IO_CONT flag, so that if this I/O gets passed to
9289 * ctl_data_submit_done(), it'll get passed back to
9290 * ctl_ctl_cnw_cont() for further processing.
9292 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9293 ctsio->io_cont = ctl_cnw_cont;
9295 lbalen = (struct ctl_lba_len_flags *)
9296 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9298 lbalen->len = num_blocks;
9299 lbalen->flags = CTL_LLF_COMPARE | flags;
9301 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9302 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9307 ctl_verify(struct ctl_scsiio *ctsio)
9309 struct ctl_lun *lun;
9310 struct ctl_lba_len_flags *lbalen;
9312 uint32_t num_blocks;
9316 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9318 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9321 flags = CTL_LLF_FUA;
9322 retval = CTL_RETVAL_COMPLETE;
9324 switch (ctsio->cdb[0]) {
9326 struct scsi_verify_10 *cdb;
9328 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9329 if (cdb->byte2 & SVFY_BYTCHK)
9331 if (cdb->byte2 & SVFY_DPO)
9332 flags |= CTL_LLF_DPO;
9333 lba = scsi_4btoul(cdb->addr);
9334 num_blocks = scsi_2btoul(cdb->length);
9338 struct scsi_verify_12 *cdb;
9340 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9341 if (cdb->byte2 & SVFY_BYTCHK)
9343 if (cdb->byte2 & SVFY_DPO)
9344 flags |= CTL_LLF_DPO;
9345 lba = scsi_4btoul(cdb->addr);
9346 num_blocks = scsi_4btoul(cdb->length);
9350 struct scsi_rw_16 *cdb;
9352 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9353 if (cdb->byte2 & SVFY_BYTCHK)
9355 if (cdb->byte2 & SVFY_DPO)
9356 flags |= CTL_LLF_DPO;
9357 lba = scsi_8btou64(cdb->addr);
9358 num_blocks = scsi_4btoul(cdb->length);
9363 * We got a command we don't support. This shouldn't
9364 * happen, commands should be filtered out above us.
9366 ctl_set_invalid_opcode(ctsio);
9367 ctl_done((union ctl_io *)ctsio);
9368 return (CTL_RETVAL_COMPLETE);
9372 * The first check is to make sure we're in bounds, the second
9373 * check is to catch wrap-around problems. If the lba + num blocks
9374 * is less than the lba, then we've wrapped around and the block
9375 * range is invalid anyway.
9377 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9378 || ((lba + num_blocks) < lba)) {
9379 ctl_set_lba_out_of_range(ctsio);
9380 ctl_done((union ctl_io *)ctsio);
9381 return (CTL_RETVAL_COMPLETE);
9385 * According to SBC-3, a transfer length of 0 is not an error.
9387 if (num_blocks == 0) {
9388 ctl_set_success(ctsio);
9389 ctl_done((union ctl_io *)ctsio);
9390 return (CTL_RETVAL_COMPLETE);
9393 lbalen = (struct ctl_lba_len_flags *)
9394 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9396 lbalen->len = num_blocks;
9398 lbalen->flags = CTL_LLF_COMPARE | flags;
9399 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9401 lbalen->flags = CTL_LLF_VERIFY | flags;
9402 ctsio->kern_total_len = 0;
9404 ctsio->kern_rel_offset = 0;
9406 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9407 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9412 ctl_report_luns(struct ctl_scsiio *ctsio)
9414 struct scsi_report_luns *cdb;
9415 struct scsi_report_luns_data *lun_data;
9416 struct ctl_lun *lun, *request_lun;
9417 int num_luns, retval;
9418 uint32_t alloc_len, lun_datalen;
9419 int num_filled, well_known;
9420 uint32_t initidx, targ_lun_id, lun_id;
9422 retval = CTL_RETVAL_COMPLETE;
9425 cdb = (struct scsi_report_luns *)ctsio->cdb;
9427 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9429 mtx_lock(&control_softc->ctl_lock);
9430 num_luns = control_softc->num_luns;
9431 mtx_unlock(&control_softc->ctl_lock);
9433 switch (cdb->select_report) {
9434 case RPL_REPORT_DEFAULT:
9435 case RPL_REPORT_ALL:
9437 case RPL_REPORT_WELLKNOWN:
9442 ctl_set_invalid_field(ctsio,
9448 ctl_done((union ctl_io *)ctsio);
9450 break; /* NOTREACHED */
9453 alloc_len = scsi_4btoul(cdb->length);
9455 * The initiator has to allocate at least 16 bytes for this request,
9456 * so he can at least get the header and the first LUN. Otherwise
9457 * we reject the request (per SPC-3 rev 14, section 6.21).
9459 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9460 sizeof(struct scsi_report_luns_lundata))) {
9461 ctl_set_invalid_field(ctsio,
9467 ctl_done((union ctl_io *)ctsio);
9471 request_lun = (struct ctl_lun *)
9472 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9474 lun_datalen = sizeof(*lun_data) +
9475 (num_luns * sizeof(struct scsi_report_luns_lundata));
9477 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9478 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9479 ctsio->kern_sg_entries = 0;
9481 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9483 mtx_lock(&control_softc->ctl_lock);
9484 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9485 lun_id = ctl_map_lun(ctsio->io_hdr.nexus.targ_port, targ_lun_id);
9486 if (lun_id >= CTL_MAX_LUNS)
9488 lun = control_softc->ctl_luns[lun_id];
9492 if (targ_lun_id <= 0xff) {
9494 * Peripheral addressing method, bus number 0.
9496 lun_data->luns[num_filled].lundata[0] =
9497 RPL_LUNDATA_ATYP_PERIPH;
9498 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9500 } else if (targ_lun_id <= 0x3fff) {
9502 * Flat addressing method.
9504 lun_data->luns[num_filled].lundata[0] =
9505 RPL_LUNDATA_ATYP_FLAT |
9506 (targ_lun_id & RPL_LUNDATA_FLAT_LUN_MASK);
9507 #ifdef OLDCTLHEADERS
9508 (SRLD_ADDR_FLAT << SRLD_ADDR_SHIFT) |
9509 (targ_lun_id & SRLD_BUS_LUN_MASK);
9511 lun_data->luns[num_filled].lundata[1] =
9512 #ifdef OLDCTLHEADERS
9513 targ_lun_id >> SRLD_BUS_LUN_BITS;
9515 targ_lun_id >> RPL_LUNDATA_FLAT_LUN_BITS;
9518 printf("ctl_report_luns: bogus LUN number %jd, "
9519 "skipping\n", (intmax_t)targ_lun_id);
9522 * According to SPC-3, rev 14 section 6.21:
9524 * "The execution of a REPORT LUNS command to any valid and
9525 * installed logical unit shall clear the REPORTED LUNS DATA
9526 * HAS CHANGED unit attention condition for all logical
9527 * units of that target with respect to the requesting
9528 * initiator. A valid and installed logical unit is one
9529 * having a PERIPHERAL QUALIFIER of 000b in the standard
9530 * INQUIRY data (see 6.4.2)."
9532 * If request_lun is NULL, the LUN this report luns command
9533 * was issued to is either disabled or doesn't exist. In that
9534 * case, we shouldn't clear any pending lun change unit
9537 if (request_lun != NULL) {
9538 mtx_lock(&lun->lun_lock);
9539 lun->pending_ua[initidx] &= ~CTL_UA_LUN_CHANGE;
9540 mtx_unlock(&lun->lun_lock);
9543 mtx_unlock(&control_softc->ctl_lock);
9546 * It's quite possible that we've returned fewer LUNs than we allocated
9547 * space for. Trim it.
9549 lun_datalen = sizeof(*lun_data) +
9550 (num_filled * sizeof(struct scsi_report_luns_lundata));
9552 if (lun_datalen < alloc_len) {
9553 ctsio->residual = alloc_len - lun_datalen;
9554 ctsio->kern_data_len = lun_datalen;
9555 ctsio->kern_total_len = lun_datalen;
9557 ctsio->residual = 0;
9558 ctsio->kern_data_len = alloc_len;
9559 ctsio->kern_total_len = alloc_len;
9561 ctsio->kern_data_resid = 0;
9562 ctsio->kern_rel_offset = 0;
9563 ctsio->kern_sg_entries = 0;
9566 * We set this to the actual data length, regardless of how much
9567 * space we actually have to return results. If the user looks at
9568 * this value, he'll know whether or not he allocated enough space
9569 * and reissue the command if necessary. We don't support well
9570 * known logical units, so if the user asks for that, return none.
9572 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9575 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9578 ctsio->scsi_status = SCSI_STATUS_OK;
9580 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9581 ctsio->be_move_done = ctl_config_move_done;
9582 ctl_datamove((union ctl_io *)ctsio);
9588 ctl_request_sense(struct ctl_scsiio *ctsio)
9590 struct scsi_request_sense *cdb;
9591 struct scsi_sense_data *sense_ptr;
9592 struct ctl_lun *lun;
9595 scsi_sense_data_type sense_format;
9597 cdb = (struct scsi_request_sense *)ctsio->cdb;
9599 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9601 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9604 * Determine which sense format the user wants.
9606 if (cdb->byte2 & SRS_DESC)
9607 sense_format = SSD_TYPE_DESC;
9609 sense_format = SSD_TYPE_FIXED;
9611 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9612 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9613 ctsio->kern_sg_entries = 0;
9616 * struct scsi_sense_data, which is currently set to 256 bytes, is
9617 * larger than the largest allowed value for the length field in the
9618 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9620 ctsio->residual = 0;
9621 ctsio->kern_data_len = cdb->length;
9622 ctsio->kern_total_len = cdb->length;
9624 ctsio->kern_data_resid = 0;
9625 ctsio->kern_rel_offset = 0;
9626 ctsio->kern_sg_entries = 0;
9629 * If we don't have a LUN, we don't have any pending sense.
9635 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9637 * Check for pending sense, and then for pending unit attentions.
9638 * Pending sense gets returned first, then pending unit attentions.
9640 mtx_lock(&lun->lun_lock);
9642 if (ctl_is_set(lun->have_ca, initidx)) {
9643 scsi_sense_data_type stored_format;
9646 * Check to see which sense format was used for the stored
9649 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9652 * If the user requested a different sense format than the
9653 * one we stored, then we need to convert it to the other
9654 * format. If we're going from descriptor to fixed format
9655 * sense data, we may lose things in translation, depending
9656 * on what options were used.
9658 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9659 * for some reason we'll just copy it out as-is.
9661 if ((stored_format == SSD_TYPE_FIXED)
9662 && (sense_format == SSD_TYPE_DESC))
9663 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9664 &lun->pending_sense[initidx],
9665 (struct scsi_sense_data_desc *)sense_ptr);
9666 else if ((stored_format == SSD_TYPE_DESC)
9667 && (sense_format == SSD_TYPE_FIXED))
9668 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9669 &lun->pending_sense[initidx],
9670 (struct scsi_sense_data_fixed *)sense_ptr);
9672 memcpy(sense_ptr, &lun->pending_sense[initidx],
9673 ctl_min(sizeof(*sense_ptr),
9674 sizeof(lun->pending_sense[initidx])));
9676 ctl_clear_mask(lun->have_ca, initidx);
9680 if (lun->pending_ua[initidx] != CTL_UA_NONE) {
9681 ctl_ua_type ua_type;
9683 ua_type = ctl_build_ua(lun->pending_ua[initidx],
9684 sense_ptr, sense_format);
9685 if (ua_type != CTL_UA_NONE) {
9687 /* We're reporting this UA, so clear it */
9688 lun->pending_ua[initidx] &= ~ua_type;
9691 mtx_unlock(&lun->lun_lock);
9694 * We already have a pending error, return it.
9696 if (have_error != 0) {
9698 * We report the SCSI status as OK, since the status of the
9699 * request sense command itself is OK.
9701 ctsio->scsi_status = SCSI_STATUS_OK;
9704 * We report 0 for the sense length, because we aren't doing
9705 * autosense in this case. We're reporting sense as
9708 ctsio->sense_len = 0;
9709 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9710 ctsio->be_move_done = ctl_config_move_done;
9711 ctl_datamove((union ctl_io *)ctsio);
9713 return (CTL_RETVAL_COMPLETE);
9719 * No sense information to report, so we report that everything is
9722 ctl_set_sense_data(sense_ptr,
9725 /*current_error*/ 1,
9726 /*sense_key*/ SSD_KEY_NO_SENSE,
9731 ctsio->scsi_status = SCSI_STATUS_OK;
9734 * We report 0 for the sense length, because we aren't doing
9735 * autosense in this case. We're reporting sense as parameter data.
9737 ctsio->sense_len = 0;
9738 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9739 ctsio->be_move_done = ctl_config_move_done;
9740 ctl_datamove((union ctl_io *)ctsio);
9742 return (CTL_RETVAL_COMPLETE);
9746 ctl_tur(struct ctl_scsiio *ctsio)
9748 struct ctl_lun *lun;
9750 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9752 CTL_DEBUG_PRINT(("ctl_tur\n"));
9757 ctsio->scsi_status = SCSI_STATUS_OK;
9758 ctsio->io_hdr.status = CTL_SUCCESS;
9760 ctl_done((union ctl_io *)ctsio);
9762 return (CTL_RETVAL_COMPLETE);
9767 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9774 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9776 struct scsi_vpd_supported_pages *pages;
9778 struct ctl_lun *lun;
9780 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9782 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9783 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9784 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9785 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9786 ctsio->kern_sg_entries = 0;
9788 if (sup_page_size < alloc_len) {
9789 ctsio->residual = alloc_len - sup_page_size;
9790 ctsio->kern_data_len = sup_page_size;
9791 ctsio->kern_total_len = sup_page_size;
9793 ctsio->residual = 0;
9794 ctsio->kern_data_len = alloc_len;
9795 ctsio->kern_total_len = alloc_len;
9797 ctsio->kern_data_resid = 0;
9798 ctsio->kern_rel_offset = 0;
9799 ctsio->kern_sg_entries = 0;
9802 * The control device is always connected. The disk device, on the
9803 * other hand, may not be online all the time. Need to change this
9804 * to figure out whether the disk device is actually online or not.
9807 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9808 lun->be_lun->lun_type;
9810 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9812 pages->length = SCSI_EVPD_NUM_SUPPORTED_PAGES;
9813 /* Supported VPD pages */
9814 pages->page_list[0] = SVPD_SUPPORTED_PAGES;
9816 pages->page_list[1] = SVPD_UNIT_SERIAL_NUMBER;
9817 /* Device Identification */
9818 pages->page_list[2] = SVPD_DEVICE_ID;
9819 /* Extended INQUIRY Data */
9820 pages->page_list[3] = SVPD_EXTENDED_INQUIRY_DATA;
9821 /* Mode Page Policy */
9822 pages->page_list[4] = SVPD_MODE_PAGE_POLICY;
9824 pages->page_list[5] = SVPD_SCSI_PORTS;
9825 /* Third-party Copy */
9826 pages->page_list[6] = SVPD_SCSI_TPC;
9828 pages->page_list[7] = SVPD_BLOCK_LIMITS;
9829 /* Block Device Characteristics */
9830 pages->page_list[8] = SVPD_BDC;
9831 /* Logical Block Provisioning */
9832 pages->page_list[9] = SVPD_LBP;
9834 ctsio->scsi_status = SCSI_STATUS_OK;
9836 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9837 ctsio->be_move_done = ctl_config_move_done;
9838 ctl_datamove((union ctl_io *)ctsio);
9840 return (CTL_RETVAL_COMPLETE);
9844 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9846 struct scsi_vpd_unit_serial_number *sn_ptr;
9847 struct ctl_lun *lun;
9849 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9851 ctsio->kern_data_ptr = malloc(sizeof(*sn_ptr), M_CTL, M_WAITOK | M_ZERO);
9852 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9853 ctsio->kern_sg_entries = 0;
9855 if (sizeof(*sn_ptr) < alloc_len) {
9856 ctsio->residual = alloc_len - sizeof(*sn_ptr);
9857 ctsio->kern_data_len = sizeof(*sn_ptr);
9858 ctsio->kern_total_len = sizeof(*sn_ptr);
9860 ctsio->residual = 0;
9861 ctsio->kern_data_len = alloc_len;
9862 ctsio->kern_total_len = alloc_len;
9864 ctsio->kern_data_resid = 0;
9865 ctsio->kern_rel_offset = 0;
9866 ctsio->kern_sg_entries = 0;
9869 * The control device is always connected. The disk device, on the
9870 * other hand, may not be online all the time. Need to change this
9871 * to figure out whether the disk device is actually online or not.
9874 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9875 lun->be_lun->lun_type;
9877 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9879 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9880 sn_ptr->length = ctl_min(sizeof(*sn_ptr) - 4, CTL_SN_LEN);
9882 * If we don't have a LUN, we just leave the serial number as
9885 memset(sn_ptr->serial_num, 0x20, sizeof(sn_ptr->serial_num));
9887 strncpy((char *)sn_ptr->serial_num,
9888 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9890 ctsio->scsi_status = SCSI_STATUS_OK;
9892 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9893 ctsio->be_move_done = ctl_config_move_done;
9894 ctl_datamove((union ctl_io *)ctsio);
9896 return (CTL_RETVAL_COMPLETE);
9901 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9903 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9904 struct ctl_lun *lun;
9907 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9909 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9910 sizeof(struct scsi_vpd_mode_page_policy_descr);
9912 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9913 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9914 ctsio->kern_sg_entries = 0;
9916 if (data_len < alloc_len) {
9917 ctsio->residual = alloc_len - data_len;
9918 ctsio->kern_data_len = data_len;
9919 ctsio->kern_total_len = data_len;
9921 ctsio->residual = 0;
9922 ctsio->kern_data_len = alloc_len;
9923 ctsio->kern_total_len = alloc_len;
9925 ctsio->kern_data_resid = 0;
9926 ctsio->kern_rel_offset = 0;
9927 ctsio->kern_sg_entries = 0;
9930 * The control device is always connected. The disk device, on the
9931 * other hand, may not be online all the time.
9934 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9935 lun->be_lun->lun_type;
9937 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9938 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9939 eid_ptr->page_length = data_len - 4;
9940 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9941 eid_ptr->flags3 = SVPD_EID_V_SUP;
9943 ctsio->scsi_status = SCSI_STATUS_OK;
9944 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9945 ctsio->be_move_done = ctl_config_move_done;
9946 ctl_datamove((union ctl_io *)ctsio);
9948 return (CTL_RETVAL_COMPLETE);
9952 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9954 struct scsi_vpd_mode_page_policy *mpp_ptr;
9955 struct ctl_lun *lun;
9958 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9960 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9961 sizeof(struct scsi_vpd_mode_page_policy_descr);
9963 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9964 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9965 ctsio->kern_sg_entries = 0;
9967 if (data_len < alloc_len) {
9968 ctsio->residual = alloc_len - data_len;
9969 ctsio->kern_data_len = data_len;
9970 ctsio->kern_total_len = data_len;
9972 ctsio->residual = 0;
9973 ctsio->kern_data_len = alloc_len;
9974 ctsio->kern_total_len = alloc_len;
9976 ctsio->kern_data_resid = 0;
9977 ctsio->kern_rel_offset = 0;
9978 ctsio->kern_sg_entries = 0;
9981 * The control device is always connected. The disk device, on the
9982 * other hand, may not be online all the time.
9985 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9986 lun->be_lun->lun_type;
9988 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9989 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9990 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9991 mpp_ptr->descr[0].page_code = 0x3f;
9992 mpp_ptr->descr[0].subpage_code = 0xff;
9993 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9995 ctsio->scsi_status = SCSI_STATUS_OK;
9996 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9997 ctsio->be_move_done = ctl_config_move_done;
9998 ctl_datamove((union ctl_io *)ctsio);
10000 return (CTL_RETVAL_COMPLETE);
10004 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
10006 struct scsi_vpd_device_id *devid_ptr;
10007 struct scsi_vpd_id_descriptor *desc;
10008 struct ctl_softc *ctl_softc;
10009 struct ctl_lun *lun;
10010 struct ctl_port *port;
10014 ctl_softc = control_softc;
10016 port = ctl_softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
10017 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10019 data_len = sizeof(struct scsi_vpd_device_id) +
10020 sizeof(struct scsi_vpd_id_descriptor) +
10021 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
10022 sizeof(struct scsi_vpd_id_descriptor) +
10023 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
10024 if (lun && lun->lun_devid)
10025 data_len += lun->lun_devid->len;
10026 if (port->port_devid)
10027 data_len += port->port_devid->len;
10028 if (port->target_devid)
10029 data_len += port->target_devid->len;
10031 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10032 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
10033 ctsio->kern_sg_entries = 0;
10035 if (data_len < alloc_len) {
10036 ctsio->residual = alloc_len - data_len;
10037 ctsio->kern_data_len = data_len;
10038 ctsio->kern_total_len = data_len;
10040 ctsio->residual = 0;
10041 ctsio->kern_data_len = alloc_len;
10042 ctsio->kern_total_len = alloc_len;
10044 ctsio->kern_data_resid = 0;
10045 ctsio->kern_rel_offset = 0;
10046 ctsio->kern_sg_entries = 0;
10049 * The control device is always connected. The disk device, on the
10050 * other hand, may not be online all the time.
10053 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10054 lun->be_lun->lun_type;
10056 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10057 devid_ptr->page_code = SVPD_DEVICE_ID;
10058 scsi_ulto2b(data_len - 4, devid_ptr->length);
10060 if (port->port_type == CTL_PORT_FC)
10061 proto = SCSI_PROTO_FC << 4;
10062 else if (port->port_type == CTL_PORT_ISCSI)
10063 proto = SCSI_PROTO_ISCSI << 4;
10065 proto = SCSI_PROTO_SPI << 4;
10066 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
10069 * We're using a LUN association here. i.e., this device ID is a
10070 * per-LUN identifier.
10072 if (lun && lun->lun_devid) {
10073 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
10074 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10075 lun->lun_devid->len);
10079 * This is for the WWPN which is a port association.
10081 if (port->port_devid) {
10082 memcpy(desc, port->port_devid->data, port->port_devid->len);
10083 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10084 port->port_devid->len);
10088 * This is for the Relative Target Port(type 4h) identifier
10090 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10091 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10092 SVPD_ID_TYPE_RELTARG;
10094 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
10095 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10096 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
10099 * This is for the Target Port Group(type 5h) identifier
10101 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10102 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10103 SVPD_ID_TYPE_TPORTGRP;
10105 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
10106 &desc->identifier[2]);
10107 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10108 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
10111 * This is for the Target identifier
10113 if (port->target_devid) {
10114 memcpy(desc, port->target_devid->data, port->target_devid->len);
10117 ctsio->scsi_status = SCSI_STATUS_OK;
10118 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10119 ctsio->be_move_done = ctl_config_move_done;
10120 ctl_datamove((union ctl_io *)ctsio);
10122 return (CTL_RETVAL_COMPLETE);
10126 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
10128 struct ctl_softc *softc = control_softc;
10129 struct scsi_vpd_scsi_ports *sp;
10130 struct scsi_vpd_port_designation *pd;
10131 struct scsi_vpd_port_designation_cont *pdc;
10132 struct ctl_lun *lun;
10133 struct ctl_port *port;
10134 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
10135 int num_target_port_groups, single;
10137 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10139 single = ctl_is_single;
10141 num_target_port_groups = 1;
10143 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
10144 num_target_ports = 0;
10147 mtx_lock(&softc->ctl_lock);
10148 STAILQ_FOREACH(port, &softc->port_list, links) {
10149 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10152 ctl_map_lun_back(port->targ_port, lun->lun) >=
10155 num_target_ports++;
10156 if (port->init_devid)
10157 iid_len += port->init_devid->len;
10158 if (port->port_devid)
10159 id_len += port->port_devid->len;
10161 mtx_unlock(&softc->ctl_lock);
10163 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
10164 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
10165 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
10166 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10167 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
10168 ctsio->kern_sg_entries = 0;
10170 if (data_len < alloc_len) {
10171 ctsio->residual = alloc_len - data_len;
10172 ctsio->kern_data_len = data_len;
10173 ctsio->kern_total_len = data_len;
10175 ctsio->residual = 0;
10176 ctsio->kern_data_len = alloc_len;
10177 ctsio->kern_total_len = alloc_len;
10179 ctsio->kern_data_resid = 0;
10180 ctsio->kern_rel_offset = 0;
10181 ctsio->kern_sg_entries = 0;
10184 * The control device is always connected. The disk device, on the
10185 * other hand, may not be online all the time. Need to change this
10186 * to figure out whether the disk device is actually online or not.
10189 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10190 lun->be_lun->lun_type;
10192 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10194 sp->page_code = SVPD_SCSI_PORTS;
10195 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10197 pd = &sp->design[0];
10199 mtx_lock(&softc->ctl_lock);
10200 if (softc->flags & CTL_FLAG_MASTER_SHELF)
10204 for (g = 0; g < num_target_port_groups; g++) {
10205 STAILQ_FOREACH(port, &softc->port_list, links) {
10206 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10209 ctl_map_lun_back(port->targ_port, lun->lun) >=
10212 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10213 scsi_ulto2b(p, pd->relative_port_id);
10214 if (port->init_devid && g == pg) {
10215 iid_len = port->init_devid->len;
10216 memcpy(pd->initiator_transportid,
10217 port->init_devid->data, port->init_devid->len);
10220 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10221 pdc = (struct scsi_vpd_port_designation_cont *)
10222 (&pd->initiator_transportid[iid_len]);
10223 if (port->port_devid && g == pg) {
10224 id_len = port->port_devid->len;
10225 memcpy(pdc->target_port_descriptors,
10226 port->port_devid->data, port->port_devid->len);
10229 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10230 pd = (struct scsi_vpd_port_designation *)
10231 ((uint8_t *)pdc->target_port_descriptors + id_len);
10234 mtx_unlock(&softc->ctl_lock);
10236 ctsio->scsi_status = SCSI_STATUS_OK;
10237 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10238 ctsio->be_move_done = ctl_config_move_done;
10239 ctl_datamove((union ctl_io *)ctsio);
10241 return (CTL_RETVAL_COMPLETE);
10245 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10247 struct scsi_vpd_block_limits *bl_ptr;
10248 struct ctl_lun *lun;
10251 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10253 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10254 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10255 ctsio->kern_sg_entries = 0;
10257 if (sizeof(*bl_ptr) < alloc_len) {
10258 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10259 ctsio->kern_data_len = sizeof(*bl_ptr);
10260 ctsio->kern_total_len = sizeof(*bl_ptr);
10262 ctsio->residual = 0;
10263 ctsio->kern_data_len = alloc_len;
10264 ctsio->kern_total_len = alloc_len;
10266 ctsio->kern_data_resid = 0;
10267 ctsio->kern_rel_offset = 0;
10268 ctsio->kern_sg_entries = 0;
10271 * The control device is always connected. The disk device, on the
10272 * other hand, may not be online all the time. Need to change this
10273 * to figure out whether the disk device is actually online or not.
10276 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10277 lun->be_lun->lun_type;
10279 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10281 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10282 scsi_ulto2b(sizeof(*bl_ptr), bl_ptr->page_length);
10283 bl_ptr->max_cmp_write_len = 0xff;
10284 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10286 bs = lun->be_lun->blocksize;
10287 scsi_ulto4b(MAXPHYS / bs, bl_ptr->opt_txfer_len);
10288 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10289 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10290 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10291 if (lun->be_lun->pblockexp != 0) {
10292 scsi_ulto4b((1 << lun->be_lun->pblockexp),
10293 bl_ptr->opt_unmap_grain);
10294 scsi_ulto4b(0x80000000 | lun->be_lun->pblockoff,
10295 bl_ptr->unmap_grain_align);
10299 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10301 ctsio->scsi_status = SCSI_STATUS_OK;
10302 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10303 ctsio->be_move_done = ctl_config_move_done;
10304 ctl_datamove((union ctl_io *)ctsio);
10306 return (CTL_RETVAL_COMPLETE);
10310 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10312 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10313 struct ctl_lun *lun;
10315 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10317 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10318 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10319 ctsio->kern_sg_entries = 0;
10321 if (sizeof(*bdc_ptr) < alloc_len) {
10322 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10323 ctsio->kern_data_len = sizeof(*bdc_ptr);
10324 ctsio->kern_total_len = sizeof(*bdc_ptr);
10326 ctsio->residual = 0;
10327 ctsio->kern_data_len = alloc_len;
10328 ctsio->kern_total_len = alloc_len;
10330 ctsio->kern_data_resid = 0;
10331 ctsio->kern_rel_offset = 0;
10332 ctsio->kern_sg_entries = 0;
10335 * The control device is always connected. The disk device, on the
10336 * other hand, may not be online all the time. Need to change this
10337 * to figure out whether the disk device is actually online or not.
10340 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10341 lun->be_lun->lun_type;
10343 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10344 bdc_ptr->page_code = SVPD_BDC;
10345 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10346 scsi_ulto2b(SVPD_NON_ROTATING, bdc_ptr->medium_rotation_rate);
10347 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10349 ctsio->scsi_status = SCSI_STATUS_OK;
10350 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10351 ctsio->be_move_done = ctl_config_move_done;
10352 ctl_datamove((union ctl_io *)ctsio);
10354 return (CTL_RETVAL_COMPLETE);
10358 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10360 struct scsi_vpd_logical_block_prov *lbp_ptr;
10361 struct ctl_lun *lun;
10363 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10365 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10366 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10367 ctsio->kern_sg_entries = 0;
10369 if (sizeof(*lbp_ptr) < alloc_len) {
10370 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10371 ctsio->kern_data_len = sizeof(*lbp_ptr);
10372 ctsio->kern_total_len = sizeof(*lbp_ptr);
10374 ctsio->residual = 0;
10375 ctsio->kern_data_len = alloc_len;
10376 ctsio->kern_total_len = alloc_len;
10378 ctsio->kern_data_resid = 0;
10379 ctsio->kern_rel_offset = 0;
10380 ctsio->kern_sg_entries = 0;
10383 * The control device is always connected. The disk device, on the
10384 * other hand, may not be online all the time. Need to change this
10385 * to figure out whether the disk device is actually online or not.
10388 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10389 lun->be_lun->lun_type;
10391 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10393 lbp_ptr->page_code = SVPD_LBP;
10394 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10395 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10396 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10397 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10398 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10401 ctsio->scsi_status = SCSI_STATUS_OK;
10402 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10403 ctsio->be_move_done = ctl_config_move_done;
10404 ctl_datamove((union ctl_io *)ctsio);
10406 return (CTL_RETVAL_COMPLETE);
10410 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10412 struct scsi_inquiry *cdb;
10413 struct ctl_lun *lun;
10414 int alloc_len, retval;
10416 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10417 cdb = (struct scsi_inquiry *)ctsio->cdb;
10419 retval = CTL_RETVAL_COMPLETE;
10421 alloc_len = scsi_2btoul(cdb->length);
10423 switch (cdb->page_code) {
10424 case SVPD_SUPPORTED_PAGES:
10425 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10427 case SVPD_UNIT_SERIAL_NUMBER:
10428 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10430 case SVPD_DEVICE_ID:
10431 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10433 case SVPD_EXTENDED_INQUIRY_DATA:
10434 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10436 case SVPD_MODE_PAGE_POLICY:
10437 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10439 case SVPD_SCSI_PORTS:
10440 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10442 case SVPD_SCSI_TPC:
10443 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10445 case SVPD_BLOCK_LIMITS:
10446 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10449 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10452 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10455 ctl_set_invalid_field(ctsio,
10461 ctl_done((union ctl_io *)ctsio);
10462 retval = CTL_RETVAL_COMPLETE;
10470 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10472 struct scsi_inquiry_data *inq_ptr;
10473 struct scsi_inquiry *cdb;
10474 struct ctl_softc *ctl_softc;
10475 struct ctl_lun *lun;
10477 uint32_t alloc_len;
10478 ctl_port_type port_type;
10480 ctl_softc = control_softc;
10483 * Figure out whether we're talking to a Fibre Channel port or not.
10484 * We treat the ioctl front end, and any SCSI adapters, as packetized
10487 port_type = ctl_softc->ctl_ports[
10488 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10489 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10490 port_type = CTL_PORT_SCSI;
10492 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10493 cdb = (struct scsi_inquiry *)ctsio->cdb;
10494 alloc_len = scsi_2btoul(cdb->length);
10497 * We malloc the full inquiry data size here and fill it
10498 * in. If the user only asks for less, we'll give him
10501 ctsio->kern_data_ptr = malloc(sizeof(*inq_ptr), M_CTL, M_WAITOK | M_ZERO);
10502 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10503 ctsio->kern_sg_entries = 0;
10504 ctsio->kern_data_resid = 0;
10505 ctsio->kern_rel_offset = 0;
10507 if (sizeof(*inq_ptr) < alloc_len) {
10508 ctsio->residual = alloc_len - sizeof(*inq_ptr);
10509 ctsio->kern_data_len = sizeof(*inq_ptr);
10510 ctsio->kern_total_len = sizeof(*inq_ptr);
10512 ctsio->residual = 0;
10513 ctsio->kern_data_len = alloc_len;
10514 ctsio->kern_total_len = alloc_len;
10518 * If we have a LUN configured, report it as connected. Otherwise,
10519 * report that it is offline or no device is supported, depending
10520 * on the value of inquiry_pq_no_lun.
10522 * According to the spec (SPC-4 r34), the peripheral qualifier
10523 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10525 * "A peripheral device having the specified peripheral device type
10526 * is not connected to this logical unit. However, the device
10527 * server is capable of supporting the specified peripheral device
10528 * type on this logical unit."
10530 * According to the same spec, the peripheral qualifier
10531 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10533 * "The device server is not capable of supporting a peripheral
10534 * device on this logical unit. For this peripheral qualifier the
10535 * peripheral device type shall be set to 1Fh. All other peripheral
10536 * device type values are reserved for this peripheral qualifier."
10538 * Given the text, it would seem that we probably want to report that
10539 * the LUN is offline here. There is no LUN connected, but we can
10540 * support a LUN at the given LUN number.
10542 * In the real world, though, it sounds like things are a little
10545 * - Linux, when presented with a LUN with the offline peripheral
10546 * qualifier, will create an sg driver instance for it. So when
10547 * you attach it to CTL, you wind up with a ton of sg driver
10548 * instances. (One for every LUN that Linux bothered to probe.)
10549 * Linux does this despite the fact that it issues a REPORT LUNs
10550 * to LUN 0 to get the inventory of supported LUNs.
10552 * - There is other anecdotal evidence (from Emulex folks) about
10553 * arrays that use the offline peripheral qualifier for LUNs that
10554 * are on the "passive" path in an active/passive array.
10556 * So the solution is provide a hopefully reasonable default
10557 * (return bad/no LUN) and allow the user to change the behavior
10558 * with a tunable/sysctl variable.
10561 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10562 lun->be_lun->lun_type;
10563 else if (ctl_softc->inquiry_pq_no_lun == 0)
10564 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10566 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10568 /* RMB in byte 2 is 0 */
10569 inq_ptr->version = SCSI_REV_SPC4;
10572 * According to SAM-3, even if a device only supports a single
10573 * level of LUN addressing, it should still set the HISUP bit:
10575 * 4.9.1 Logical unit numbers overview
10577 * All logical unit number formats described in this standard are
10578 * hierarchical in structure even when only a single level in that
10579 * hierarchy is used. The HISUP bit shall be set to one in the
10580 * standard INQUIRY data (see SPC-2) when any logical unit number
10581 * format described in this standard is used. Non-hierarchical
10582 * formats are outside the scope of this standard.
10584 * Therefore we set the HiSup bit here.
10586 * The reponse format is 2, per SPC-3.
10588 inq_ptr->response_format = SID_HiSup | 2;
10590 inq_ptr->additional_length =
10591 offsetof(struct scsi_inquiry_data, vendor_specific1) -
10592 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10593 CTL_DEBUG_PRINT(("additional_length = %d\n",
10594 inq_ptr->additional_length));
10596 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10597 /* 16 bit addressing */
10598 if (port_type == CTL_PORT_SCSI)
10599 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10600 /* XXX set the SID_MultiP bit here if we're actually going to
10601 respond on multiple ports */
10602 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10604 /* 16 bit data bus, synchronous transfers */
10605 if (port_type == CTL_PORT_SCSI)
10606 inq_ptr->flags = SID_WBus16 | SID_Sync;
10608 * XXX KDM do we want to support tagged queueing on the control
10612 || (lun->be_lun->lun_type != T_PROCESSOR))
10613 inq_ptr->flags |= SID_CmdQue;
10615 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10616 * We have 8 bytes for the vendor name, and 16 bytes for the device
10617 * name and 4 bytes for the revision.
10619 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10620 "vendor")) == NULL) {
10621 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10623 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10624 strncpy(inq_ptr->vendor, val,
10625 min(sizeof(inq_ptr->vendor), strlen(val)));
10628 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10629 sizeof(inq_ptr->product));
10630 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10631 switch (lun->be_lun->lun_type) {
10633 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10634 sizeof(inq_ptr->product));
10637 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10638 sizeof(inq_ptr->product));
10641 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10642 sizeof(inq_ptr->product));
10646 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10647 strncpy(inq_ptr->product, val,
10648 min(sizeof(inq_ptr->product), strlen(val)));
10652 * XXX make this a macro somewhere so it automatically gets
10653 * incremented when we make changes.
10655 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10656 "revision")) == NULL) {
10657 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10659 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10660 strncpy(inq_ptr->revision, val,
10661 min(sizeof(inq_ptr->revision), strlen(val)));
10665 * For parallel SCSI, we support double transition and single
10666 * transition clocking. We also support QAS (Quick Arbitration
10667 * and Selection) and Information Unit transfers on both the
10668 * control and array devices.
10670 if (port_type == CTL_PORT_SCSI)
10671 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10674 /* SAM-5 (no version claimed) */
10675 scsi_ulto2b(0x00A0, inq_ptr->version1);
10676 /* SPC-4 (no version claimed) */
10677 scsi_ulto2b(0x0460, inq_ptr->version2);
10678 if (port_type == CTL_PORT_FC) {
10679 /* FCP-2 ANSI INCITS.350:2003 */
10680 scsi_ulto2b(0x0917, inq_ptr->version3);
10681 } else if (port_type == CTL_PORT_SCSI) {
10682 /* SPI-4 ANSI INCITS.362:200x */
10683 scsi_ulto2b(0x0B56, inq_ptr->version3);
10684 } else if (port_type == CTL_PORT_ISCSI) {
10685 /* iSCSI (no version claimed) */
10686 scsi_ulto2b(0x0960, inq_ptr->version3);
10687 } else if (port_type == CTL_PORT_SAS) {
10688 /* SAS (no version claimed) */
10689 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10693 /* SBC-3 (no version claimed) */
10694 scsi_ulto2b(0x04C0, inq_ptr->version4);
10696 switch (lun->be_lun->lun_type) {
10698 /* SBC-3 (no version claimed) */
10699 scsi_ulto2b(0x04C0, inq_ptr->version4);
10707 ctsio->scsi_status = SCSI_STATUS_OK;
10708 if (ctsio->kern_data_len > 0) {
10709 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10710 ctsio->be_move_done = ctl_config_move_done;
10711 ctl_datamove((union ctl_io *)ctsio);
10713 ctsio->io_hdr.status = CTL_SUCCESS;
10714 ctl_done((union ctl_io *)ctsio);
10717 return (CTL_RETVAL_COMPLETE);
10721 ctl_inquiry(struct ctl_scsiio *ctsio)
10723 struct scsi_inquiry *cdb;
10726 cdb = (struct scsi_inquiry *)ctsio->cdb;
10730 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10733 * Right now, we don't support the CmdDt inquiry information.
10734 * This would be nice to support in the future. When we do
10735 * support it, we should change this test so that it checks to make
10736 * sure SI_EVPD and SI_CMDDT aren't both set at the same time.
10739 if (((cdb->byte2 & SI_EVPD)
10740 && (cdb->byte2 & SI_CMDDT)))
10742 if (cdb->byte2 & SI_CMDDT) {
10744 * Point to the SI_CMDDT bit. We might change this
10745 * when we support SI_CMDDT, but since both bits would be
10746 * "wrong", this should probably just stay as-is then.
10748 ctl_set_invalid_field(ctsio,
10754 ctl_done((union ctl_io *)ctsio);
10755 return (CTL_RETVAL_COMPLETE);
10757 if (cdb->byte2 & SI_EVPD)
10758 retval = ctl_inquiry_evpd(ctsio);
10760 else if (cdb->byte2 & SI_CMDDT)
10761 retval = ctl_inquiry_cmddt(ctsio);
10764 retval = ctl_inquiry_std(ctsio);
10770 * For known CDB types, parse the LBA and length.
10773 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10775 if (io->io_hdr.io_type != CTL_IO_SCSI)
10778 switch (io->scsiio.cdb[0]) {
10779 case COMPARE_AND_WRITE: {
10780 struct scsi_compare_and_write *cdb;
10782 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10784 *lba = scsi_8btou64(cdb->addr);
10785 *len = cdb->length;
10790 struct scsi_rw_6 *cdb;
10792 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10794 *lba = scsi_3btoul(cdb->addr);
10795 /* only 5 bits are valid in the most significant address byte */
10797 *len = cdb->length;
10802 struct scsi_rw_10 *cdb;
10804 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10806 *lba = scsi_4btoul(cdb->addr);
10807 *len = scsi_2btoul(cdb->length);
10810 case WRITE_VERIFY_10: {
10811 struct scsi_write_verify_10 *cdb;
10813 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10815 *lba = scsi_4btoul(cdb->addr);
10816 *len = scsi_2btoul(cdb->length);
10821 struct scsi_rw_12 *cdb;
10823 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10825 *lba = scsi_4btoul(cdb->addr);
10826 *len = scsi_4btoul(cdb->length);
10829 case WRITE_VERIFY_12: {
10830 struct scsi_write_verify_12 *cdb;
10832 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10834 *lba = scsi_4btoul(cdb->addr);
10835 *len = scsi_4btoul(cdb->length);
10840 struct scsi_rw_16 *cdb;
10842 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10844 *lba = scsi_8btou64(cdb->addr);
10845 *len = scsi_4btoul(cdb->length);
10848 case WRITE_VERIFY_16: {
10849 struct scsi_write_verify_16 *cdb;
10851 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10854 *lba = scsi_8btou64(cdb->addr);
10855 *len = scsi_4btoul(cdb->length);
10858 case WRITE_SAME_10: {
10859 struct scsi_write_same_10 *cdb;
10861 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10863 *lba = scsi_4btoul(cdb->addr);
10864 *len = scsi_2btoul(cdb->length);
10867 case WRITE_SAME_16: {
10868 struct scsi_write_same_16 *cdb;
10870 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10872 *lba = scsi_8btou64(cdb->addr);
10873 *len = scsi_4btoul(cdb->length);
10877 struct scsi_verify_10 *cdb;
10879 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10881 *lba = scsi_4btoul(cdb->addr);
10882 *len = scsi_2btoul(cdb->length);
10886 struct scsi_verify_12 *cdb;
10888 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10890 *lba = scsi_4btoul(cdb->addr);
10891 *len = scsi_4btoul(cdb->length);
10895 struct scsi_verify_16 *cdb;
10897 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10899 *lba = scsi_8btou64(cdb->addr);
10900 *len = scsi_4btoul(cdb->length);
10910 break; /* NOTREACHED */
10917 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2)
10919 uint64_t endlba1, endlba2;
10921 endlba1 = lba1 + len1 - 1;
10922 endlba2 = lba2 + len2 - 1;
10924 if ((endlba1 < lba2)
10925 || (endlba2 < lba1))
10926 return (CTL_ACTION_PASS);
10928 return (CTL_ACTION_BLOCK);
10932 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10934 struct ctl_ptr_len_flags *ptrlen;
10935 struct scsi_unmap_desc *buf, *end, *range;
10939 /* If not UNMAP -- go other way. */
10940 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10941 io->scsiio.cdb[0] != UNMAP)
10942 return (CTL_ACTION_ERROR);
10944 /* If UNMAP without data -- block and wait for data. */
10945 ptrlen = (struct ctl_ptr_len_flags *)
10946 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10947 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10948 ptrlen->ptr == NULL)
10949 return (CTL_ACTION_BLOCK);
10951 /* UNMAP with data -- check for collision. */
10952 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10953 end = buf + ptrlen->len / sizeof(*buf);
10954 for (range = buf; range < end; range++) {
10955 lba = scsi_8btou64(range->lba);
10956 len = scsi_4btoul(range->length);
10957 if ((lba < lba2 + len2) && (lba + len > lba2))
10958 return (CTL_ACTION_BLOCK);
10960 return (CTL_ACTION_PASS);
10964 ctl_extent_check(union ctl_io *io1, union ctl_io *io2)
10966 uint64_t lba1, lba2;
10967 uint64_t len1, len2;
10970 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10971 return (CTL_ACTION_ERROR);
10973 retval = ctl_extent_check_unmap(io2, lba1, len1);
10974 if (retval != CTL_ACTION_ERROR)
10977 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10978 return (CTL_ACTION_ERROR);
10980 return (ctl_extent_check_lba(lba1, len1, lba2, len2));
10984 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10985 union ctl_io *ooa_io)
10987 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10988 ctl_serialize_action *serialize_row;
10991 * The initiator attempted multiple untagged commands at the same
10992 * time. Can't do that.
10994 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10995 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10996 && ((pending_io->io_hdr.nexus.targ_port ==
10997 ooa_io->io_hdr.nexus.targ_port)
10998 && (pending_io->io_hdr.nexus.initid.id ==
10999 ooa_io->io_hdr.nexus.initid.id))
11000 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
11001 return (CTL_ACTION_OVERLAP);
11004 * The initiator attempted to send multiple tagged commands with
11005 * the same ID. (It's fine if different initiators have the same
11008 * Even if all of those conditions are true, we don't kill the I/O
11009 * if the command ahead of us has been aborted. We won't end up
11010 * sending it to the FETD, and it's perfectly legal to resend a
11011 * command with the same tag number as long as the previous
11012 * instance of this tag number has been aborted somehow.
11014 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11015 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11016 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11017 && ((pending_io->io_hdr.nexus.targ_port ==
11018 ooa_io->io_hdr.nexus.targ_port)
11019 && (pending_io->io_hdr.nexus.initid.id ==
11020 ooa_io->io_hdr.nexus.initid.id))
11021 && ((ooa_io->io_hdr.flags & CTL_FLAG_ABORT) == 0))
11022 return (CTL_ACTION_OVERLAP_TAG);
11025 * If we get a head of queue tag, SAM-3 says that we should
11026 * immediately execute it.
11028 * What happens if this command would normally block for some other
11029 * reason? e.g. a request sense with a head of queue tag
11030 * immediately after a write. Normally that would block, but this
11031 * will result in its getting executed immediately...
11033 * We currently return "pass" instead of "skip", so we'll end up
11034 * going through the rest of the queue to check for overlapped tags.
11036 * XXX KDM check for other types of blockage first??
11038 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11039 return (CTL_ACTION_PASS);
11042 * Ordered tags have to block until all items ahead of them
11043 * have completed. If we get called with an ordered tag, we always
11044 * block, if something else is ahead of us in the queue.
11046 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11047 return (CTL_ACTION_BLOCK);
11050 * Simple tags get blocked until all head of queue and ordered tags
11051 * ahead of them have completed. I'm lumping untagged commands in
11052 * with simple tags here. XXX KDM is that the right thing to do?
11054 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11055 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11056 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11057 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11058 return (CTL_ACTION_BLOCK);
11060 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio);
11061 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio);
11063 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11065 switch (serialize_row[pending_entry->seridx]) {
11066 case CTL_SER_BLOCK:
11067 return (CTL_ACTION_BLOCK);
11068 case CTL_SER_EXTENT:
11069 return (ctl_extent_check(pending_io, ooa_io));
11070 case CTL_SER_EXTENTOPT:
11071 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11072 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11073 return (ctl_extent_check(pending_io, ooa_io));
11076 return (CTL_ACTION_PASS);
11077 case CTL_SER_BLOCKOPT:
11078 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11079 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11080 return (CTL_ACTION_BLOCK);
11081 return (CTL_ACTION_PASS);
11083 return (CTL_ACTION_SKIP);
11085 panic("invalid serialization value %d",
11086 serialize_row[pending_entry->seridx]);
11089 return (CTL_ACTION_ERROR);
11093 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11095 * - pending_io is generally either incoming, or on the blocked queue
11096 * - starting I/O is the I/O we want to start the check with.
11099 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11100 union ctl_io *starting_io)
11102 union ctl_io *ooa_io;
11105 mtx_assert(&lun->lun_lock, MA_OWNED);
11108 * Run back along the OOA queue, starting with the current
11109 * blocked I/O and going through every I/O before it on the
11110 * queue. If starting_io is NULL, we'll just end up returning
11113 for (ooa_io = starting_io; ooa_io != NULL;
11114 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11118 * This routine just checks to see whether
11119 * cur_blocked is blocked by ooa_io, which is ahead
11120 * of it in the queue. It doesn't queue/dequeue
11123 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11125 case CTL_ACTION_BLOCK:
11126 case CTL_ACTION_OVERLAP:
11127 case CTL_ACTION_OVERLAP_TAG:
11128 case CTL_ACTION_SKIP:
11129 case CTL_ACTION_ERROR:
11131 break; /* NOTREACHED */
11132 case CTL_ACTION_PASS:
11135 panic("invalid action %d", action);
11136 break; /* NOTREACHED */
11140 return (CTL_ACTION_PASS);
11145 * - An I/O has just completed, and has been removed from the per-LUN OOA
11146 * queue, so some items on the blocked queue may now be unblocked.
11149 ctl_check_blocked(struct ctl_lun *lun)
11151 union ctl_io *cur_blocked, *next_blocked;
11153 mtx_assert(&lun->lun_lock, MA_OWNED);
11156 * Run forward from the head of the blocked queue, checking each
11157 * entry against the I/Os prior to it on the OOA queue to see if
11158 * there is still any blockage.
11160 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11161 * with our removing a variable on it while it is traversing the
11164 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11165 cur_blocked != NULL; cur_blocked = next_blocked) {
11166 union ctl_io *prev_ooa;
11169 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11172 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11173 ctl_ooaq, ooa_links);
11176 * If cur_blocked happens to be the first item in the OOA
11177 * queue now, prev_ooa will be NULL, and the action
11178 * returned will just be CTL_ACTION_PASS.
11180 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11183 case CTL_ACTION_BLOCK:
11184 /* Nothing to do here, still blocked */
11186 case CTL_ACTION_OVERLAP:
11187 case CTL_ACTION_OVERLAP_TAG:
11189 * This shouldn't happen! In theory we've already
11190 * checked this command for overlap...
11193 case CTL_ACTION_PASS:
11194 case CTL_ACTION_SKIP: {
11195 struct ctl_softc *softc;
11196 const struct ctl_cmd_entry *entry;
11201 * The skip case shouldn't happen, this transaction
11202 * should have never made it onto the blocked queue.
11205 * This I/O is no longer blocked, we can remove it
11206 * from the blocked queue. Since this is a TAILQ
11207 * (doubly linked list), we can do O(1) removals
11208 * from any place on the list.
11210 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11212 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11214 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
11216 * Need to send IO back to original side to
11219 union ctl_ha_msg msg_info;
11221 msg_info.hdr.original_sc =
11222 cur_blocked->io_hdr.original_sc;
11223 msg_info.hdr.serializing_sc = cur_blocked;
11224 msg_info.hdr.msg_type = CTL_MSG_R2R;
11225 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11226 &msg_info, sizeof(msg_info), 0)) >
11227 CTL_HA_STATUS_SUCCESS) {
11228 printf("CTL:Check Blocked error from "
11229 "ctl_ha_msg_send %d\n",
11234 entry = ctl_get_cmd_entry(&cur_blocked->scsiio);
11235 softc = control_softc;
11237 initidx = ctl_get_initindex(&cur_blocked->io_hdr.nexus);
11240 * Check this I/O for LUN state changes that may
11241 * have happened while this command was blocked.
11242 * The LUN state may have been changed by a command
11243 * ahead of us in the queue, so we need to re-check
11244 * for any states that can be caused by SCSI
11247 if (ctl_scsiio_lun_check(softc, lun, entry,
11248 &cur_blocked->scsiio) == 0) {
11249 cur_blocked->io_hdr.flags |=
11250 CTL_FLAG_IS_WAS_ON_RTR;
11251 ctl_enqueue_rtr(cur_blocked);
11253 ctl_done(cur_blocked);
11258 * This probably shouldn't happen -- we shouldn't
11259 * get CTL_ACTION_ERROR, or anything else.
11265 return (CTL_RETVAL_COMPLETE);
11269 * This routine (with one exception) checks LUN flags that can be set by
11270 * commands ahead of us in the OOA queue. These flags have to be checked
11271 * when a command initially comes in, and when we pull a command off the
11272 * blocked queue and are preparing to execute it. The reason we have to
11273 * check these flags for commands on the blocked queue is that the LUN
11274 * state may have been changed by a command ahead of us while we're on the
11277 * Ordering is somewhat important with these checks, so please pay
11278 * careful attention to the placement of any new checks.
11281 ctl_scsiio_lun_check(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
11282 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11288 mtx_assert(&lun->lun_lock, MA_OWNED);
11291 * If this shelf is a secondary shelf controller, we have to reject
11292 * any media access commands.
11295 /* No longer needed for HA */
11296 if (((ctl_softc->flags & CTL_FLAG_MASTER_SHELF) == 0)
11297 && ((entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0)) {
11298 ctl_set_lun_standby(ctsio);
11305 * Check for a reservation conflict. If this command isn't allowed
11306 * even on reserved LUNs, and if this initiator isn't the one who
11307 * reserved us, reject the command with a reservation conflict.
11309 if ((lun->flags & CTL_LUN_RESERVED)
11310 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11311 if ((ctsio->io_hdr.nexus.initid.id != lun->rsv_nexus.initid.id)
11312 || (ctsio->io_hdr.nexus.targ_port != lun->rsv_nexus.targ_port)
11313 || (ctsio->io_hdr.nexus.targ_target.id !=
11314 lun->rsv_nexus.targ_target.id)) {
11315 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11316 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11322 if ( (lun->flags & CTL_LUN_PR_RESERVED)
11323 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV) == 0)) {
11326 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11328 * if we aren't registered or it's a res holder type
11329 * reservation and this isn't the res holder then set a
11331 * NOTE: Commands which might be allowed on write exclusive
11332 * type reservations are checked in the particular command
11333 * for a conflict. Read and SSU are the only ones.
11335 if (!lun->per_res[residx].registered
11336 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11337 ctsio->scsi_status = SCSI_STATUS_RESERV_CONFLICT;
11338 ctsio->io_hdr.status = CTL_SCSI_ERROR;
11345 if ((lun->flags & CTL_LUN_OFFLINE)
11346 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11347 ctl_set_lun_not_ready(ctsio);
11353 * If the LUN is stopped, see if this particular command is allowed
11354 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11356 if ((lun->flags & CTL_LUN_STOPPED)
11357 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11358 /* "Logical unit not ready, initializing cmd. required" */
11359 ctl_set_lun_stopped(ctsio);
11364 if ((lun->flags & CTL_LUN_INOPERABLE)
11365 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11366 /* "Medium format corrupted" */
11367 ctl_set_medium_format_corrupted(ctsio);
11378 ctl_failover_io(union ctl_io *io, int have_lock)
11380 ctl_set_busy(&io->scsiio);
11387 struct ctl_lun *lun;
11388 struct ctl_softc *ctl_softc;
11389 union ctl_io *next_io, *pending_io;
11394 ctl_softc = control_softc;
11396 mtx_lock(&ctl_softc->ctl_lock);
11398 * Remove any cmds from the other SC from the rtr queue. These
11399 * will obviously only be for LUNs for which we're the primary.
11400 * We can't send status or get/send data for these commands.
11401 * Since they haven't been executed yet, we can just remove them.
11402 * We'll either abort them or delete them below, depending on
11403 * which HA mode we're in.
11406 mtx_lock(&ctl_softc->queue_lock);
11407 for (io = (union ctl_io *)STAILQ_FIRST(&ctl_softc->rtr_queue);
11408 io != NULL; io = next_io) {
11409 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11410 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11411 STAILQ_REMOVE(&ctl_softc->rtr_queue, &io->io_hdr,
11412 ctl_io_hdr, links);
11414 mtx_unlock(&ctl_softc->queue_lock);
11417 for (lun_idx=0; lun_idx < ctl_softc->num_luns; lun_idx++) {
11418 lun = ctl_softc->ctl_luns[lun_idx];
11423 * Processor LUNs are primary on both sides.
11424 * XXX will this always be true?
11426 if (lun->be_lun->lun_type == T_PROCESSOR)
11429 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11430 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11431 printf("FAILOVER: primary lun %d\n", lun_idx);
11433 * Remove all commands from the other SC. First from the
11434 * blocked queue then from the ooa queue. Once we have
11435 * removed them. Call ctl_check_blocked to see if there
11436 * is anything that can run.
11438 for (io = (union ctl_io *)TAILQ_FIRST(
11439 &lun->blocked_queue); io != NULL; io = next_io) {
11441 next_io = (union ctl_io *)TAILQ_NEXT(
11442 &io->io_hdr, blocked_links);
11444 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11445 TAILQ_REMOVE(&lun->blocked_queue,
11446 &io->io_hdr,blocked_links);
11447 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11448 TAILQ_REMOVE(&lun->ooa_queue,
11449 &io->io_hdr, ooa_links);
11455 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11456 io != NULL; io = next_io) {
11458 next_io = (union ctl_io *)TAILQ_NEXT(
11459 &io->io_hdr, ooa_links);
11461 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11463 TAILQ_REMOVE(&lun->ooa_queue,
11470 ctl_check_blocked(lun);
11471 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11472 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11474 printf("FAILOVER: primary lun %d\n", lun_idx);
11476 * Abort all commands from the other SC. We can't
11477 * send status back for them now. These should get
11478 * cleaned up when they are completed or come out
11479 * for a datamove operation.
11481 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11482 io != NULL; io = next_io) {
11483 next_io = (union ctl_io *)TAILQ_NEXT(
11484 &io->io_hdr, ooa_links);
11486 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11487 io->io_hdr.flags |= CTL_FLAG_ABORT;
11489 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11490 && (ctl_softc->ha_mode == CTL_HA_MODE_XFER)) {
11492 printf("FAILOVER: secondary lun %d\n", lun_idx);
11494 lun->flags |= CTL_LUN_PRIMARY_SC;
11497 * We send all I/O that was sent to this controller
11498 * and redirected to the other side back with
11499 * busy status, and have the initiator retry it.
11500 * Figuring out how much data has been transferred,
11501 * etc. and picking up where we left off would be
11504 * XXX KDM need to remove I/O from the blocked
11507 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11508 &lun->ooa_queue); pending_io != NULL;
11509 pending_io = next_io) {
11511 next_io = (union ctl_io *)TAILQ_NEXT(
11512 &pending_io->io_hdr, ooa_links);
11514 pending_io->io_hdr.flags &=
11515 ~CTL_FLAG_SENT_2OTHER_SC;
11517 if (pending_io->io_hdr.flags &
11518 CTL_FLAG_IO_ACTIVE) {
11519 pending_io->io_hdr.flags |=
11522 ctl_set_busy(&pending_io->scsiio);
11523 ctl_done(pending_io);
11528 * Build Unit Attention
11530 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11531 lun->pending_ua[i] |=
11532 CTL_UA_ASYM_ACC_CHANGE;
11534 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11535 && (ctl_softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11536 printf("FAILOVER: secondary lun %d\n", lun_idx);
11538 * if the first io on the OOA is not on the RtR queue
11541 lun->flags |= CTL_LUN_PRIMARY_SC;
11543 pending_io = (union ctl_io *)TAILQ_FIRST(
11545 if (pending_io==NULL) {
11546 printf("Nothing on OOA queue\n");
11550 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11551 if ((pending_io->io_hdr.flags &
11552 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11553 pending_io->io_hdr.flags |=
11554 CTL_FLAG_IS_WAS_ON_RTR;
11555 ctl_enqueue_rtr(pending_io);
11560 printf("Tag 0x%04x is running\n",
11561 pending_io->scsiio.tag_num);
11565 next_io = (union ctl_io *)TAILQ_NEXT(
11566 &pending_io->io_hdr, ooa_links);
11567 for (pending_io=next_io; pending_io != NULL;
11568 pending_io = next_io) {
11569 pending_io->io_hdr.flags &=
11570 ~CTL_FLAG_SENT_2OTHER_SC;
11571 next_io = (union ctl_io *)TAILQ_NEXT(
11572 &pending_io->io_hdr, ooa_links);
11573 if (pending_io->io_hdr.flags &
11574 CTL_FLAG_IS_WAS_ON_RTR) {
11576 printf("Tag 0x%04x is running\n",
11577 pending_io->scsiio.tag_num);
11582 switch (ctl_check_ooa(lun, pending_io,
11583 (union ctl_io *)TAILQ_PREV(
11584 &pending_io->io_hdr, ctl_ooaq,
11587 case CTL_ACTION_BLOCK:
11588 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11589 &pending_io->io_hdr,
11591 pending_io->io_hdr.flags |=
11594 case CTL_ACTION_PASS:
11595 case CTL_ACTION_SKIP:
11596 pending_io->io_hdr.flags |=
11597 CTL_FLAG_IS_WAS_ON_RTR;
11598 ctl_enqueue_rtr(pending_io);
11600 case CTL_ACTION_OVERLAP:
11601 ctl_set_overlapped_cmd(
11602 (struct ctl_scsiio *)pending_io);
11603 ctl_done(pending_io);
11605 case CTL_ACTION_OVERLAP_TAG:
11606 ctl_set_overlapped_tag(
11607 (struct ctl_scsiio *)pending_io,
11608 pending_io->scsiio.tag_num & 0xff);
11609 ctl_done(pending_io);
11611 case CTL_ACTION_ERROR:
11613 ctl_set_internal_failure(
11614 (struct ctl_scsiio *)pending_io,
11617 ctl_done(pending_io);
11623 * Build Unit Attention
11625 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
11626 lun->pending_ua[i] |=
11627 CTL_UA_ASYM_ACC_CHANGE;
11630 panic("Unhandled HA mode failover, LUN flags = %#x, "
11631 "ha_mode = #%x", lun->flags, ctl_softc->ha_mode);
11635 mtx_unlock(&ctl_softc->ctl_lock);
11639 ctl_scsiio_precheck(struct ctl_softc *ctl_softc, struct ctl_scsiio *ctsio)
11641 struct ctl_lun *lun;
11642 const struct ctl_cmd_entry *entry;
11643 uint32_t initidx, targ_lun;
11650 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11651 if ((targ_lun < CTL_MAX_LUNS)
11652 && (ctl_softc->ctl_luns[targ_lun] != NULL)) {
11653 lun = ctl_softc->ctl_luns[targ_lun];
11655 * If the LUN is invalid, pretend that it doesn't exist.
11656 * It will go away as soon as all pending I/O has been
11659 if (lun->flags & CTL_LUN_DISABLED) {
11662 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11663 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11665 if (lun->be_lun->lun_type == T_PROCESSOR) {
11666 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11670 * Every I/O goes into the OOA queue for a
11671 * particular LUN, and stays there until completion.
11673 mtx_lock(&lun->lun_lock);
11674 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11678 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11679 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11682 /* Get command entry and return error if it is unsuppotyed. */
11683 entry = ctl_validate_command(ctsio);
11684 if (entry == NULL) {
11686 mtx_unlock(&lun->lun_lock);
11690 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11691 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11694 * Check to see whether we can send this command to LUNs that don't
11695 * exist. This should pretty much only be the case for inquiry
11696 * and request sense. Further checks, below, really require having
11697 * a LUN, so we can't really check the command anymore. Just put
11698 * it on the rtr queue.
11701 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11702 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11703 ctl_enqueue_rtr((union ctl_io *)ctsio);
11707 ctl_set_unsupported_lun(ctsio);
11708 ctl_done((union ctl_io *)ctsio);
11709 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11713 * Make sure we support this particular command on this LUN.
11714 * e.g., we don't support writes to the control LUN.
11716 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11717 mtx_unlock(&lun->lun_lock);
11718 ctl_set_invalid_opcode(ctsio);
11719 ctl_done((union ctl_io *)ctsio);
11724 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11728 * If we've got a request sense, it'll clear the contingent
11729 * allegiance condition. Otherwise, if we have a CA condition for
11730 * this initiator, clear it, because it sent down a command other
11731 * than request sense.
11733 if ((ctsio->cdb[0] != REQUEST_SENSE)
11734 && (ctl_is_set(lun->have_ca, initidx)))
11735 ctl_clear_mask(lun->have_ca, initidx);
11739 * If the command has this flag set, it handles its own unit
11740 * attention reporting, we shouldn't do anything. Otherwise we
11741 * check for any pending unit attentions, and send them back to the
11742 * initiator. We only do this when a command initially comes in,
11743 * not when we pull it off the blocked queue.
11745 * According to SAM-3, section 5.3.2, the order that things get
11746 * presented back to the host is basically unit attentions caused
11747 * by some sort of reset event, busy status, reservation conflicts
11748 * or task set full, and finally any other status.
11750 * One issue here is that some of the unit attentions we report
11751 * don't fall into the "reset" category (e.g. "reported luns data
11752 * has changed"). So reporting it here, before the reservation
11753 * check, may be technically wrong. I guess the only thing to do
11754 * would be to check for and report the reset events here, and then
11755 * check for the other unit attention types after we check for a
11756 * reservation conflict.
11758 * XXX KDM need to fix this
11760 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11761 ctl_ua_type ua_type;
11763 ua_type = lun->pending_ua[initidx];
11764 if (ua_type != CTL_UA_NONE) {
11765 scsi_sense_data_type sense_format;
11768 sense_format = (lun->flags &
11769 CTL_LUN_SENSE_DESC) ? SSD_TYPE_DESC :
11772 sense_format = SSD_TYPE_FIXED;
11774 ua_type = ctl_build_ua(ua_type, &ctsio->sense_data,
11776 if (ua_type != CTL_UA_NONE) {
11777 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11778 ctsio->io_hdr.status = CTL_SCSI_ERROR |
11780 ctsio->sense_len = SSD_FULL_SIZE;
11781 lun->pending_ua[initidx] &= ~ua_type;
11782 mtx_unlock(&lun->lun_lock);
11783 ctl_done((union ctl_io *)ctsio);
11790 if (ctl_scsiio_lun_check(ctl_softc, lun, entry, ctsio) != 0) {
11791 mtx_unlock(&lun->lun_lock);
11792 ctl_done((union ctl_io *)ctsio);
11797 * XXX CHD this is where we want to send IO to other side if
11798 * this LUN is secondary on this SC. We will need to make a copy
11799 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11800 * the copy we send as FROM_OTHER.
11801 * We also need to stuff the address of the original IO so we can
11802 * find it easily. Something similar will need be done on the other
11803 * side so when we are done we can find the copy.
11805 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11806 union ctl_ha_msg msg_info;
11809 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11811 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11812 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11814 printf("1. ctsio %p\n", ctsio);
11816 msg_info.hdr.serializing_sc = NULL;
11817 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11818 msg_info.scsi.tag_num = ctsio->tag_num;
11819 msg_info.scsi.tag_type = ctsio->tag_type;
11820 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11822 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11824 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11825 (void *)&msg_info, sizeof(msg_info), 0)) >
11826 CTL_HA_STATUS_SUCCESS) {
11827 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11829 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11832 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11837 * XXX KDM this I/O is off the incoming queue, but hasn't
11838 * been inserted on any other queue. We may need to come
11839 * up with a holding queue while we wait for serialization
11840 * so that we have an idea of what we're waiting for from
11843 mtx_unlock(&lun->lun_lock);
11847 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11848 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11849 ctl_ooaq, ooa_links))) {
11850 case CTL_ACTION_BLOCK:
11851 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11852 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11854 mtx_unlock(&lun->lun_lock);
11856 case CTL_ACTION_PASS:
11857 case CTL_ACTION_SKIP:
11858 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11859 mtx_unlock(&lun->lun_lock);
11860 ctl_enqueue_rtr((union ctl_io *)ctsio);
11862 case CTL_ACTION_OVERLAP:
11863 mtx_unlock(&lun->lun_lock);
11864 ctl_set_overlapped_cmd(ctsio);
11865 ctl_done((union ctl_io *)ctsio);
11867 case CTL_ACTION_OVERLAP_TAG:
11868 mtx_unlock(&lun->lun_lock);
11869 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11870 ctl_done((union ctl_io *)ctsio);
11872 case CTL_ACTION_ERROR:
11874 mtx_unlock(&lun->lun_lock);
11875 ctl_set_internal_failure(ctsio,
11877 /*retry_count*/ 0);
11878 ctl_done((union ctl_io *)ctsio);
11884 const struct ctl_cmd_entry *
11885 ctl_get_cmd_entry(struct ctl_scsiio *ctsio)
11887 const struct ctl_cmd_entry *entry;
11888 int service_action;
11890 entry = &ctl_cmd_table[ctsio->cdb[0]];
11891 if (entry->flags & CTL_CMD_FLAG_SA5) {
11892 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11893 entry = &((const struct ctl_cmd_entry *)
11894 entry->execute)[service_action];
11899 const struct ctl_cmd_entry *
11900 ctl_validate_command(struct ctl_scsiio *ctsio)
11902 const struct ctl_cmd_entry *entry;
11906 entry = ctl_get_cmd_entry(ctsio);
11907 if (entry->execute == NULL) {
11908 ctl_set_invalid_opcode(ctsio);
11909 ctl_done((union ctl_io *)ctsio);
11912 KASSERT(entry->length > 0,
11913 ("Not defined length for command 0x%02x/0x%02x",
11914 ctsio->cdb[0], ctsio->cdb[1]));
11915 for (i = 1; i < entry->length; i++) {
11916 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11919 ctl_set_invalid_field(ctsio,
11924 /*bit*/ fls(diff) - 1);
11925 ctl_done((union ctl_io *)ctsio);
11932 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11935 switch (lun_type) {
11937 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
11938 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11942 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
11943 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
11953 ctl_scsiio(struct ctl_scsiio *ctsio)
11956 const struct ctl_cmd_entry *entry;
11958 retval = CTL_RETVAL_COMPLETE;
11960 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11962 entry = ctl_get_cmd_entry(ctsio);
11965 * If this I/O has been aborted, just send it straight to
11966 * ctl_done() without executing it.
11968 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11969 ctl_done((union ctl_io *)ctsio);
11974 * All the checks should have been handled by ctl_scsiio_precheck().
11975 * We should be clear now to just execute the I/O.
11977 retval = entry->execute(ctsio);
11984 * Since we only implement one target right now, a bus reset simply resets
11985 * our single target.
11988 ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io)
11990 return(ctl_target_reset(ctl_softc, io, CTL_UA_BUS_RESET));
11994 ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
11995 ctl_ua_type ua_type)
11997 struct ctl_lun *lun;
12000 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12001 union ctl_ha_msg msg_info;
12003 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
12004 msg_info.hdr.nexus = io->io_hdr.nexus;
12005 if (ua_type==CTL_UA_TARG_RESET)
12006 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
12008 msg_info.task.task_action = CTL_TASK_BUS_RESET;
12009 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12010 msg_info.hdr.original_sc = NULL;
12011 msg_info.hdr.serializing_sc = NULL;
12012 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12013 (void *)&msg_info, sizeof(msg_info), 0)) {
12018 mtx_lock(&ctl_softc->ctl_lock);
12019 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links)
12020 retval += ctl_lun_reset(lun, io, ua_type);
12021 mtx_unlock(&ctl_softc->ctl_lock);
12027 * The LUN should always be set. The I/O is optional, and is used to
12028 * distinguish between I/Os sent by this initiator, and by other
12029 * initiators. We set unit attention for initiators other than this one.
12030 * SAM-3 is vague on this point. It does say that a unit attention should
12031 * be established for other initiators when a LUN is reset (see section
12032 * 5.7.3), but it doesn't specifically say that the unit attention should
12033 * be established for this particular initiator when a LUN is reset. Here
12034 * is the relevant text, from SAM-3 rev 8:
12036 * 5.7.2 When a SCSI initiator port aborts its own tasks
12038 * When a SCSI initiator port causes its own task(s) to be aborted, no
12039 * notification that the task(s) have been aborted shall be returned to
12040 * the SCSI initiator port other than the completion response for the
12041 * command or task management function action that caused the task(s) to
12042 * be aborted and notification(s) associated with related effects of the
12043 * action (e.g., a reset unit attention condition).
12045 * XXX KDM for now, we're setting unit attention for all initiators.
12048 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
12052 uint32_t initindex;
12056 mtx_lock(&lun->lun_lock);
12058 * Run through the OOA queue and abort each I/O.
12061 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12063 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12064 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12065 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
12069 * This version sets unit attention for every
12072 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12073 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
12074 if (initindex == i)
12076 lun->pending_ua[i] |= ua_type;
12081 * A reset (any kind, really) clears reservations established with
12082 * RESERVE/RELEASE. It does not clear reservations established
12083 * with PERSISTENT RESERVE OUT, but we don't support that at the
12084 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
12085 * reservations made with the RESERVE/RELEASE commands, because
12086 * those commands are obsolete in SPC-3.
12088 lun->flags &= ~CTL_LUN_RESERVED;
12090 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
12092 ctl_clear_mask(lun->have_ca, i);
12094 lun->pending_ua[i] |= ua_type;
12096 mtx_unlock(&lun->lun_lock);
12102 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12107 mtx_assert(&lun->lun_lock, MA_OWNED);
12110 * Run through the OOA queue and attempt to find the given I/O.
12111 * The target port, initiator ID, tag type and tag number have to
12112 * match the values that we got from the initiator. If we have an
12113 * untagged command to abort, simply abort the first untagged command
12114 * we come to. We only allow one untagged command at a time of course.
12116 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12117 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12119 if ((targ_port == UINT32_MAX ||
12120 targ_port == xio->io_hdr.nexus.targ_port) &&
12121 (init_id == UINT32_MAX ||
12122 init_id == xio->io_hdr.nexus.initid.id)) {
12123 if (targ_port != xio->io_hdr.nexus.targ_port ||
12124 init_id != xio->io_hdr.nexus.initid.id)
12125 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12126 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12127 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12128 union ctl_ha_msg msg_info;
12130 msg_info.hdr.nexus = xio->io_hdr.nexus;
12131 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12132 msg_info.task.tag_num = xio->scsiio.tag_num;
12133 msg_info.task.tag_type = xio->scsiio.tag_type;
12134 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12135 msg_info.hdr.original_sc = NULL;
12136 msg_info.hdr.serializing_sc = NULL;
12137 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12138 (void *)&msg_info, sizeof(msg_info), 0);
12145 ctl_abort_task_set(union ctl_io *io)
12147 struct ctl_softc *softc = control_softc;
12148 struct ctl_lun *lun;
12154 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12155 mtx_lock(&softc->ctl_lock);
12156 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
12157 lun = softc->ctl_luns[targ_lun];
12159 mtx_unlock(&softc->ctl_lock);
12163 mtx_lock(&lun->lun_lock);
12164 mtx_unlock(&softc->ctl_lock);
12165 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12166 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12167 io->io_hdr.nexus.initid.id,
12168 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12169 } else { /* CTL_TASK_CLEAR_TASK_SET */
12170 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12171 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12173 mtx_unlock(&lun->lun_lock);
12178 ctl_i_t_nexus_reset(union ctl_io *io)
12180 struct ctl_softc *softc = control_softc;
12181 struct ctl_lun *lun;
12182 uint32_t initindex;
12184 initindex = ctl_get_initindex(&io->io_hdr.nexus);
12185 mtx_lock(&softc->ctl_lock);
12186 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12187 mtx_lock(&lun->lun_lock);
12188 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12189 io->io_hdr.nexus.initid.id,
12190 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12192 ctl_clear_mask(lun->have_ca, initindex);
12194 lun->pending_ua[initindex] |= CTL_UA_I_T_NEXUS_LOSS;
12195 mtx_unlock(&lun->lun_lock);
12197 mtx_unlock(&softc->ctl_lock);
12202 ctl_abort_task(union ctl_io *io)
12205 struct ctl_lun *lun;
12206 struct ctl_softc *ctl_softc;
12209 char printbuf[128];
12214 ctl_softc = control_softc;
12220 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12221 mtx_lock(&ctl_softc->ctl_lock);
12222 if ((targ_lun < CTL_MAX_LUNS)
12223 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12224 lun = ctl_softc->ctl_luns[targ_lun];
12226 mtx_unlock(&ctl_softc->ctl_lock);
12231 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12232 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12235 mtx_lock(&lun->lun_lock);
12236 mtx_unlock(&ctl_softc->ctl_lock);
12238 * Run through the OOA queue and attempt to find the given I/O.
12239 * The target port, initiator ID, tag type and tag number have to
12240 * match the values that we got from the initiator. If we have an
12241 * untagged command to abort, simply abort the first untagged command
12242 * we come to. We only allow one untagged command at a time of course.
12245 TAILQ_FOREACH((struct ctl_io_hdr *)xio, &lun->ooa_queue, ooa_links) {
12247 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12248 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12250 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12252 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12253 lun->lun, xio->scsiio.tag_num,
12254 xio->scsiio.tag_type,
12255 (xio->io_hdr.blocked_links.tqe_prev
12256 == NULL) ? "" : " BLOCKED",
12257 (xio->io_hdr.flags &
12258 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12259 (xio->io_hdr.flags &
12260 CTL_FLAG_ABORT) ? " ABORT" : "",
12261 (xio->io_hdr.flags &
12262 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12263 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12265 printf("%s\n", sbuf_data(&sb));
12268 if ((xio->io_hdr.nexus.targ_port == io->io_hdr.nexus.targ_port)
12269 && (xio->io_hdr.nexus.initid.id ==
12270 io->io_hdr.nexus.initid.id)) {
12272 * If the abort says that the task is untagged, the
12273 * task in the queue must be untagged. Otherwise,
12274 * we just check to see whether the tag numbers
12275 * match. This is because the QLogic firmware
12276 * doesn't pass back the tag type in an abort
12280 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12281 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12282 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12285 * XXX KDM we've got problems with FC, because it
12286 * doesn't send down a tag type with aborts. So we
12287 * can only really go by the tag number...
12288 * This may cause problems with parallel SCSI.
12289 * Need to figure that out!!
12291 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12292 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12294 if ((io->io_hdr.flags &
12295 CTL_FLAG_FROM_OTHER_SC) == 0 &&
12296 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12297 union ctl_ha_msg msg_info;
12299 io->io_hdr.flags |=
12300 CTL_FLAG_SENT_2OTHER_SC;
12301 msg_info.hdr.nexus = io->io_hdr.nexus;
12302 msg_info.task.task_action =
12303 CTL_TASK_ABORT_TASK;
12304 msg_info.task.tag_num =
12305 io->taskio.tag_num;
12306 msg_info.task.tag_type =
12307 io->taskio.tag_type;
12308 msg_info.hdr.msg_type =
12309 CTL_MSG_MANAGE_TASKS;
12310 msg_info.hdr.original_sc = NULL;
12311 msg_info.hdr.serializing_sc = NULL;
12313 printf("Sent Abort to other side\n");
12315 if (CTL_HA_STATUS_SUCCESS !=
12316 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12318 sizeof(msg_info), 0)) {
12322 printf("ctl_abort_task: found I/O to abort\n");
12328 mtx_unlock(&lun->lun_lock);
12332 * This isn't really an error. It's entirely possible for
12333 * the abort and command completion to cross on the wire.
12334 * This is more of an informative/diagnostic error.
12337 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12338 "%d:%d:%d:%d tag %d type %d\n",
12339 io->io_hdr.nexus.initid.id,
12340 io->io_hdr.nexus.targ_port,
12341 io->io_hdr.nexus.targ_target.id,
12342 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12343 io->taskio.tag_type);
12350 ctl_run_task(union ctl_io *io)
12352 struct ctl_softc *ctl_softc = control_softc;
12354 const char *task_desc;
12356 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12358 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12359 ("ctl_run_task: Unextected io_type %d\n",
12360 io->io_hdr.io_type));
12362 task_desc = ctl_scsi_task_string(&io->taskio);
12363 if (task_desc != NULL) {
12365 csevent_log(CSC_CTL | CSC_SHELF_SW |
12367 csevent_LogType_Trace,
12368 csevent_Severity_Information,
12369 csevent_AlertLevel_Green,
12370 csevent_FRU_Firmware,
12371 csevent_FRU_Unknown,
12372 "CTL: received task: %s",task_desc);
12376 csevent_log(CSC_CTL | CSC_SHELF_SW |
12378 csevent_LogType_Trace,
12379 csevent_Severity_Information,
12380 csevent_AlertLevel_Green,
12381 csevent_FRU_Firmware,
12382 csevent_FRU_Unknown,
12383 "CTL: received unknown task "
12385 io->taskio.task_action,
12386 io->taskio.task_action);
12389 switch (io->taskio.task_action) {
12390 case CTL_TASK_ABORT_TASK:
12391 retval = ctl_abort_task(io);
12393 case CTL_TASK_ABORT_TASK_SET:
12394 case CTL_TASK_CLEAR_TASK_SET:
12395 retval = ctl_abort_task_set(io);
12397 case CTL_TASK_CLEAR_ACA:
12399 case CTL_TASK_I_T_NEXUS_RESET:
12400 retval = ctl_i_t_nexus_reset(io);
12402 case CTL_TASK_LUN_RESET: {
12403 struct ctl_lun *lun;
12406 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12407 mtx_lock(&ctl_softc->ctl_lock);
12408 if ((targ_lun < CTL_MAX_LUNS)
12409 && (ctl_softc->ctl_luns[targ_lun] != NULL))
12410 lun = ctl_softc->ctl_luns[targ_lun];
12412 mtx_unlock(&ctl_softc->ctl_lock);
12417 if (!(io->io_hdr.flags &
12418 CTL_FLAG_FROM_OTHER_SC)) {
12419 union ctl_ha_msg msg_info;
12421 io->io_hdr.flags |=
12422 CTL_FLAG_SENT_2OTHER_SC;
12423 msg_info.hdr.msg_type =
12424 CTL_MSG_MANAGE_TASKS;
12425 msg_info.hdr.nexus = io->io_hdr.nexus;
12426 msg_info.task.task_action =
12427 CTL_TASK_LUN_RESET;
12428 msg_info.hdr.original_sc = NULL;
12429 msg_info.hdr.serializing_sc = NULL;
12430 if (CTL_HA_STATUS_SUCCESS !=
12431 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12433 sizeof(msg_info), 0)) {
12437 retval = ctl_lun_reset(lun, io,
12439 mtx_unlock(&ctl_softc->ctl_lock);
12442 case CTL_TASK_TARGET_RESET:
12443 retval = ctl_target_reset(ctl_softc, io, CTL_UA_TARG_RESET);
12445 case CTL_TASK_BUS_RESET:
12446 retval = ctl_bus_reset(ctl_softc, io);
12448 case CTL_TASK_PORT_LOGIN:
12450 case CTL_TASK_PORT_LOGOUT:
12453 printf("ctl_run_task: got unknown task management event %d\n",
12454 io->taskio.task_action);
12458 io->io_hdr.status = CTL_SUCCESS;
12460 io->io_hdr.status = CTL_ERROR;
12465 * For HA operation. Handle commands that come in from the other
12469 ctl_handle_isc(union ctl_io *io)
12472 struct ctl_lun *lun;
12473 struct ctl_softc *ctl_softc;
12476 ctl_softc = control_softc;
12478 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12479 lun = ctl_softc->ctl_luns[targ_lun];
12481 switch (io->io_hdr.msg_type) {
12482 case CTL_MSG_SERIALIZE:
12483 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12485 case CTL_MSG_R2R: {
12486 const struct ctl_cmd_entry *entry;
12489 * This is only used in SER_ONLY mode.
12492 entry = ctl_get_cmd_entry(&io->scsiio);
12493 mtx_lock(&lun->lun_lock);
12494 if (ctl_scsiio_lun_check(ctl_softc, lun,
12495 entry, (struct ctl_scsiio *)io) != 0) {
12496 mtx_unlock(&lun->lun_lock);
12500 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12501 mtx_unlock(&lun->lun_lock);
12502 ctl_enqueue_rtr(io);
12505 case CTL_MSG_FINISH_IO:
12506 if (ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
12511 mtx_lock(&lun->lun_lock);
12512 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12514 ctl_check_blocked(lun);
12515 mtx_unlock(&lun->lun_lock);
12518 case CTL_MSG_PERS_ACTION:
12519 ctl_hndl_per_res_out_on_other_sc(
12520 (union ctl_ha_msg *)&io->presio.pr_msg);
12523 case CTL_MSG_BAD_JUJU:
12527 case CTL_MSG_DATAMOVE:
12528 /* Only used in XFER mode */
12530 ctl_datamove_remote(io);
12532 case CTL_MSG_DATAMOVE_DONE:
12533 /* Only used in XFER mode */
12535 io->scsiio.be_move_done(io);
12539 printf("%s: Invalid message type %d\n",
12540 __func__, io->io_hdr.msg_type);
12550 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12551 * there is no match.
12553 static ctl_lun_error_pattern
12554 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12556 const struct ctl_cmd_entry *entry;
12557 ctl_lun_error_pattern filtered_pattern, pattern;
12559 pattern = desc->error_pattern;
12562 * XXX KDM we need more data passed into this function to match a
12563 * custom pattern, and we actually need to implement custom pattern
12566 if (pattern & CTL_LUN_PAT_CMD)
12567 return (CTL_LUN_PAT_CMD);
12569 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12570 return (CTL_LUN_PAT_ANY);
12572 entry = ctl_get_cmd_entry(ctsio);
12574 filtered_pattern = entry->pattern & pattern;
12577 * If the user requested specific flags in the pattern (e.g.
12578 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12581 * If the user did not specify any flags, it doesn't matter whether
12582 * or not the command supports the flags.
12584 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12585 (pattern & ~CTL_LUN_PAT_MASK))
12586 return (CTL_LUN_PAT_NONE);
12589 * If the user asked for a range check, see if the requested LBA
12590 * range overlaps with this command's LBA range.
12592 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12598 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12600 return (CTL_LUN_PAT_NONE);
12602 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12603 desc->lba_range.len);
12605 * A "pass" means that the LBA ranges don't overlap, so
12606 * this doesn't match the user's range criteria.
12608 if (action == CTL_ACTION_PASS)
12609 return (CTL_LUN_PAT_NONE);
12612 return (filtered_pattern);
12616 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12618 struct ctl_error_desc *desc, *desc2;
12620 mtx_assert(&lun->lun_lock, MA_OWNED);
12622 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12623 ctl_lun_error_pattern pattern;
12625 * Check to see whether this particular command matches
12626 * the pattern in the descriptor.
12628 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12629 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12632 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12633 case CTL_LUN_INJ_ABORTED:
12634 ctl_set_aborted(&io->scsiio);
12636 case CTL_LUN_INJ_MEDIUM_ERR:
12637 ctl_set_medium_error(&io->scsiio);
12639 case CTL_LUN_INJ_UA:
12640 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12642 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12644 case CTL_LUN_INJ_CUSTOM:
12646 * We're assuming the user knows what he is doing.
12647 * Just copy the sense information without doing
12650 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12651 ctl_min(sizeof(desc->custom_sense),
12652 sizeof(io->scsiio.sense_data)));
12653 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12654 io->scsiio.sense_len = SSD_FULL_SIZE;
12655 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12657 case CTL_LUN_INJ_NONE:
12660 * If this is an error injection type we don't know
12661 * about, clear the continuous flag (if it is set)
12662 * so it will get deleted below.
12664 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12668 * By default, each error injection action is a one-shot
12670 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12673 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12679 #ifdef CTL_IO_DELAY
12681 ctl_datamove_timer_wakeup(void *arg)
12685 io = (union ctl_io *)arg;
12689 #endif /* CTL_IO_DELAY */
12692 ctl_datamove(union ctl_io *io)
12694 void (*fe_datamove)(union ctl_io *io);
12696 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12698 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12701 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12706 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12707 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12709 sbuf_cat(&sb, path_str);
12710 switch (io->io_hdr.io_type) {
12712 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12713 sbuf_printf(&sb, "\n");
12714 sbuf_cat(&sb, path_str);
12715 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12716 io->scsiio.tag_num, io->scsiio.tag_type);
12719 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12720 "Tag Type: %d\n", io->taskio.task_action,
12721 io->taskio.tag_num, io->taskio.tag_type);
12724 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12725 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12728 sbuf_cat(&sb, path_str);
12729 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12730 (intmax_t)time_uptime - io->io_hdr.start_time);
12732 printf("%s", sbuf_data(&sb));
12734 #endif /* CTL_TIME_IO */
12736 #ifdef CTL_IO_DELAY
12737 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12738 struct ctl_lun *lun;
12740 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12742 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12744 struct ctl_lun *lun;
12746 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12748 && (lun->delay_info.datamove_delay > 0)) {
12749 struct callout *callout;
12751 callout = (struct callout *)&io->io_hdr.timer_bytes;
12752 callout_init(callout, /*mpsafe*/ 1);
12753 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12754 callout_reset(callout,
12755 lun->delay_info.datamove_delay * hz,
12756 ctl_datamove_timer_wakeup, io);
12757 if (lun->delay_info.datamove_type ==
12758 CTL_DELAY_TYPE_ONESHOT)
12759 lun->delay_info.datamove_delay = 0;
12766 * This command has been aborted. Set the port status, so we fail
12769 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12770 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12771 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12772 io->io_hdr.nexus.targ_port,
12773 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12774 io->io_hdr.nexus.targ_lun);
12775 io->io_hdr.port_status = 31337;
12777 * Note that the backend, in this case, will get the
12778 * callback in its context. In other cases it may get
12779 * called in the frontend's interrupt thread context.
12781 io->scsiio.be_move_done(io);
12786 * If we're in XFER mode and this I/O is from the other shelf
12787 * controller, we need to send the DMA to the other side to
12788 * actually transfer the data to/from the host. In serialize only
12789 * mode the transfer happens below CTL and ctl_datamove() is only
12790 * called on the machine that originally received the I/O.
12792 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12793 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12794 union ctl_ha_msg msg;
12795 uint32_t sg_entries_sent;
12799 memset(&msg, 0, sizeof(msg));
12800 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12801 msg.hdr.original_sc = io->io_hdr.original_sc;
12802 msg.hdr.serializing_sc = io;
12803 msg.hdr.nexus = io->io_hdr.nexus;
12804 msg.dt.flags = io->io_hdr.flags;
12806 * We convert everything into a S/G list here. We can't
12807 * pass by reference, only by value between controllers.
12808 * So we can't pass a pointer to the S/G list, only as many
12809 * S/G entries as we can fit in here. If it's possible for
12810 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12811 * then we need to break this up into multiple transfers.
12813 if (io->scsiio.kern_sg_entries == 0) {
12814 msg.dt.kern_sg_entries = 1;
12816 * If this is in cached memory, flush the cache
12817 * before we send the DMA request to the other
12818 * controller. We want to do this in either the
12819 * read or the write case. The read case is
12820 * straightforward. In the write case, we want to
12821 * make sure nothing is in the local cache that
12822 * could overwrite the DMAed data.
12824 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12826 * XXX KDM use bus_dmamap_sync() here.
12831 * Convert to a physical address if this is a
12834 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12835 msg.dt.sg_list[0].addr =
12836 io->scsiio.kern_data_ptr;
12839 * XXX KDM use busdma here!
12842 msg.dt.sg_list[0].addr = (void *)
12843 vtophys(io->scsiio.kern_data_ptr);
12847 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12850 struct ctl_sg_entry *sgl;
12853 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12854 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12855 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12857 * XXX KDM use bus_dmamap_sync() here.
12862 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12863 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12864 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12865 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12866 msg.dt.sg_sequence = 0;
12869 * Loop until we've sent all of the S/G entries. On the
12870 * other end, we'll recompose these S/G entries into one
12871 * contiguous list before passing it to the
12873 for (sg_entries_sent = 0; sg_entries_sent <
12874 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12875 msg.dt.cur_sg_entries = ctl_min((sizeof(msg.dt.sg_list)/
12876 sizeof(msg.dt.sg_list[0])),
12877 msg.dt.kern_sg_entries - sg_entries_sent);
12879 if (do_sg_copy != 0) {
12880 struct ctl_sg_entry *sgl;
12883 sgl = (struct ctl_sg_entry *)
12884 io->scsiio.kern_data_ptr;
12886 * If this is in cached memory, flush the cache
12887 * before we send the DMA request to the other
12888 * controller. We want to do this in either
12889 * the * read or the write case. The read
12890 * case is straightforward. In the write
12891 * case, we want to make sure nothing is
12892 * in the local cache that could overwrite
12896 for (i = sg_entries_sent, j = 0;
12897 i < msg.dt.cur_sg_entries; i++, j++) {
12898 if ((io->io_hdr.flags &
12899 CTL_FLAG_NO_DATASYNC) == 0) {
12901 * XXX KDM use bus_dmamap_sync()
12904 if ((io->io_hdr.flags &
12905 CTL_FLAG_BUS_ADDR) == 0) {
12907 * XXX KDM use busdma.
12910 msg.dt.sg_list[j].addr =(void *)
12911 vtophys(sgl[i].addr);
12914 msg.dt.sg_list[j].addr =
12917 msg.dt.sg_list[j].len = sgl[i].len;
12921 sg_entries_sent += msg.dt.cur_sg_entries;
12922 if (sg_entries_sent >= msg.dt.kern_sg_entries)
12923 msg.dt.sg_last = 1;
12925 msg.dt.sg_last = 0;
12928 * XXX KDM drop and reacquire the lock here?
12930 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12931 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
12933 * XXX do something here.
12937 msg.dt.sent_sg_entries = sg_entries_sent;
12939 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12940 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
12941 ctl_failover_io(io, /*have_lock*/ 0);
12946 * Lookup the fe_datamove() function for this particular
12950 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
12957 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12959 union ctl_ha_msg msg;
12962 memset(&msg, 0, sizeof(msg));
12964 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12965 msg.hdr.original_sc = io;
12966 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12967 msg.hdr.nexus = io->io_hdr.nexus;
12968 msg.hdr.status = io->io_hdr.status;
12969 msg.scsi.tag_num = io->scsiio.tag_num;
12970 msg.scsi.tag_type = io->scsiio.tag_type;
12971 msg.scsi.scsi_status = io->scsiio.scsi_status;
12972 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12973 sizeof(io->scsiio.sense_data));
12974 msg.scsi.sense_len = io->scsiio.sense_len;
12975 msg.scsi.sense_residual = io->scsiio.sense_residual;
12976 msg.scsi.fetd_status = io->io_hdr.port_status;
12977 msg.scsi.residual = io->scsiio.residual;
12978 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12980 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12981 ctl_failover_io(io, /*have_lock*/ have_lock);
12985 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
12986 if (isc_status > CTL_HA_STATUS_SUCCESS) {
12987 /* XXX do something if this fails */
12993 * The DMA to the remote side is done, now we need to tell the other side
12994 * we're done so it can continue with its data movement.
12997 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
13003 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13004 printf("%s: ISC DMA write failed with error %d", __func__,
13006 ctl_set_internal_failure(&io->scsiio,
13008 /*retry_count*/ rq->ret);
13011 ctl_dt_req_free(rq);
13014 * In this case, we had to malloc the memory locally. Free it.
13016 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
13018 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13019 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13022 * The data is in local and remote memory, so now we need to send
13023 * status (good or back) back to the other side.
13025 ctl_send_datamove_done(io, /*have_lock*/ 0);
13029 * We've moved the data from the host/controller into local memory. Now we
13030 * need to push it over to the remote controller's memory.
13033 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
13039 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
13040 ctl_datamove_remote_write_cb);
13046 ctl_datamove_remote_write(union ctl_io *io)
13049 void (*fe_datamove)(union ctl_io *io);
13052 * - Get the data from the host/HBA into local memory.
13053 * - DMA memory from the local controller to the remote controller.
13054 * - Send status back to the remote controller.
13057 retval = ctl_datamove_remote_sgl_setup(io);
13061 /* Switch the pointer over so the FETD knows what to do */
13062 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13065 * Use a custom move done callback, since we need to send completion
13066 * back to the other controller, not to the backend on this side.
13068 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
13070 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13079 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
13088 * In this case, we had to malloc the memory locally. Free it.
13090 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
13092 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13093 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13097 scsi_path_string(io, path_str, sizeof(path_str));
13098 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13099 sbuf_cat(&sb, path_str);
13100 scsi_command_string(&io->scsiio, NULL, &sb);
13101 sbuf_printf(&sb, "\n");
13102 sbuf_cat(&sb, path_str);
13103 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13104 io->scsiio.tag_num, io->scsiio.tag_type);
13105 sbuf_cat(&sb, path_str);
13106 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
13107 io->io_hdr.flags, io->io_hdr.status);
13109 printk("%s", sbuf_data(&sb));
13114 * The read is done, now we need to send status (good or bad) back
13115 * to the other side.
13117 ctl_send_datamove_done(io, /*have_lock*/ 0);
13123 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
13126 void (*fe_datamove)(union ctl_io *io);
13130 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13131 printf("%s: ISC DMA read failed with error %d", __func__,
13133 ctl_set_internal_failure(&io->scsiio,
13135 /*retry_count*/ rq->ret);
13138 ctl_dt_req_free(rq);
13140 /* Switch the pointer over so the FETD knows what to do */
13141 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13144 * Use a custom move done callback, since we need to send completion
13145 * back to the other controller, not to the backend on this side.
13147 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
13149 /* XXX KDM add checks like the ones in ctl_datamove? */
13151 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13157 ctl_datamove_remote_sgl_setup(union ctl_io *io)
13159 struct ctl_sg_entry *local_sglist, *remote_sglist;
13160 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
13161 struct ctl_softc *softc;
13166 softc = control_softc;
13168 local_sglist = io->io_hdr.local_sglist;
13169 local_dma_sglist = io->io_hdr.local_dma_sglist;
13170 remote_sglist = io->io_hdr.remote_sglist;
13171 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13173 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
13174 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
13175 local_sglist[i].len = remote_sglist[i].len;
13178 * XXX Detect the situation where the RS-level I/O
13179 * redirector on the other side has already read the
13180 * data off of the AOR RS on this side, and
13181 * transferred it to remote (mirror) memory on the
13182 * other side. Since we already have the data in
13183 * memory here, we just need to use it.
13185 * XXX KDM this can probably be removed once we
13186 * get the cache device code in and take the
13187 * current AOR implementation out.
13190 if ((remote_sglist[i].addr >=
13191 (void *)vtophys(softc->mirr->addr))
13192 && (remote_sglist[i].addr <
13193 ((void *)vtophys(softc->mirr->addr) +
13194 CacheMirrorOffset))) {
13195 local_sglist[i].addr = remote_sglist[i].addr -
13197 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13199 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13201 local_sglist[i].addr = remote_sglist[i].addr +
13206 printf("%s: local %p, remote %p, len %d\n",
13207 __func__, local_sglist[i].addr,
13208 remote_sglist[i].addr, local_sglist[i].len);
13212 uint32_t len_to_go;
13215 * In this case, we don't have automatically allocated
13216 * memory for this I/O on this controller. This typically
13217 * happens with internal CTL I/O -- e.g. inquiry, mode
13218 * sense, etc. Anything coming from RAIDCore will have
13219 * a mirror area available.
13221 len_to_go = io->scsiio.kern_data_len;
13224 * Clear the no datasync flag, we have to use malloced
13227 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13230 * The difficult thing here is that the size of the various
13231 * S/G segments may be different than the size from the
13232 * remote controller. That'll make it harder when DMAing
13233 * the data back to the other side.
13235 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13236 sizeof(io->io_hdr.remote_sglist[0])) &&
13237 (len_to_go > 0); i++) {
13238 local_sglist[i].len = ctl_min(len_to_go, 131072);
13239 CTL_SIZE_8B(local_dma_sglist[i].len,
13240 local_sglist[i].len);
13241 local_sglist[i].addr =
13242 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13244 local_dma_sglist[i].addr = local_sglist[i].addr;
13246 if (local_sglist[i].addr == NULL) {
13249 printf("malloc failed for %zd bytes!",
13250 local_dma_sglist[i].len);
13251 for (j = 0; j < i; j++) {
13252 free(local_sglist[j].addr, M_CTL);
13254 ctl_set_internal_failure(&io->scsiio,
13256 /*retry_count*/ 4857);
13258 goto bailout_error;
13261 /* XXX KDM do we need a sync here? */
13263 len_to_go -= local_sglist[i].len;
13266 * Reset the number of S/G entries accordingly. The
13267 * original number of S/G entries is available in
13270 io->scsiio.kern_sg_entries = i;
13273 printf("%s: kern_sg_entries = %d\n", __func__,
13274 io->scsiio.kern_sg_entries);
13275 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13276 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13277 local_sglist[i].addr, local_sglist[i].len,
13278 local_dma_sglist[i].len);
13287 ctl_send_datamove_done(io, /*have_lock*/ 0);
13293 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13294 ctl_ha_dt_cb callback)
13296 struct ctl_ha_dt_req *rq;
13297 struct ctl_sg_entry *remote_sglist, *local_sglist;
13298 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13299 uint32_t local_used, remote_used, total_used;
13305 rq = ctl_dt_req_alloc();
13308 * If we failed to allocate the request, and if the DMA didn't fail
13309 * anyway, set busy status. This is just a resource allocation
13313 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13314 ctl_set_busy(&io->scsiio);
13316 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13319 ctl_dt_req_free(rq);
13322 * The data move failed. We need to return status back
13323 * to the other controller. No point in trying to DMA
13324 * data to the remote controller.
13327 ctl_send_datamove_done(io, /*have_lock*/ 0);
13334 local_sglist = io->io_hdr.local_sglist;
13335 local_dma_sglist = io->io_hdr.local_dma_sglist;
13336 remote_sglist = io->io_hdr.remote_sglist;
13337 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13342 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13343 rq->ret = CTL_HA_STATUS_SUCCESS;
13350 * Pull/push the data over the wire from/to the other controller.
13351 * This takes into account the possibility that the local and
13352 * remote sglists may not be identical in terms of the size of
13353 * the elements and the number of elements.
13355 * One fundamental assumption here is that the length allocated for
13356 * both the local and remote sglists is identical. Otherwise, we've
13357 * essentially got a coding error of some sort.
13359 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13361 uint32_t cur_len, dma_length;
13364 rq->id = CTL_HA_DATA_CTL;
13365 rq->command = command;
13369 * Both pointers should be aligned. But it is possible
13370 * that the allocation length is not. They should both
13371 * also have enough slack left over at the end, though,
13372 * to round up to the next 8 byte boundary.
13374 cur_len = ctl_min(local_sglist[i].len - local_used,
13375 remote_sglist[j].len - remote_used);
13378 * In this case, we have a size issue and need to decrease
13379 * the size, except in the case where we actually have less
13380 * than 8 bytes left. In that case, we need to increase
13381 * the DMA length to get the last bit.
13383 if ((cur_len & 0x7) != 0) {
13384 if (cur_len > 0x7) {
13385 cur_len = cur_len - (cur_len & 0x7);
13386 dma_length = cur_len;
13388 CTL_SIZE_8B(dma_length, cur_len);
13392 dma_length = cur_len;
13395 * If we had to allocate memory for this I/O, instead of using
13396 * the non-cached mirror memory, we'll need to flush the cache
13397 * before trying to DMA to the other controller.
13399 * We could end up doing this multiple times for the same
13400 * segment if we have a larger local segment than remote
13401 * segment. That shouldn't be an issue.
13403 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13405 * XXX KDM use bus_dmamap_sync() here.
13409 rq->size = dma_length;
13411 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13412 tmp_ptr += local_used;
13414 /* Use physical addresses when talking to ISC hardware */
13415 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13416 /* XXX KDM use busdma */
13418 rq->local = vtophys(tmp_ptr);
13421 rq->local = tmp_ptr;
13423 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13424 tmp_ptr += remote_used;
13425 rq->remote = tmp_ptr;
13427 rq->callback = NULL;
13429 local_used += cur_len;
13430 if (local_used >= local_sglist[i].len) {
13435 remote_used += cur_len;
13436 if (remote_used >= remote_sglist[j].len) {
13440 total_used += cur_len;
13442 if (total_used >= io->scsiio.kern_data_len)
13443 rq->callback = callback;
13445 if ((rq->size & 0x7) != 0) {
13446 printf("%s: warning: size %d is not on 8b boundary\n",
13447 __func__, rq->size);
13449 if (((uintptr_t)rq->local & 0x7) != 0) {
13450 printf("%s: warning: local %p not on 8b boundary\n",
13451 __func__, rq->local);
13453 if (((uintptr_t)rq->remote & 0x7) != 0) {
13454 printf("%s: warning: remote %p not on 8b boundary\n",
13455 __func__, rq->local);
13458 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13459 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13460 rq->local, rq->remote, rq->size);
13463 isc_ret = ctl_dt_single(rq);
13464 if (isc_ret == CTL_HA_STATUS_WAIT)
13467 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13468 rq->ret = CTL_HA_STATUS_SUCCESS;
13482 ctl_datamove_remote_read(union ctl_io *io)
13488 * This will send an error to the other controller in the case of a
13491 retval = ctl_datamove_remote_sgl_setup(io);
13495 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13496 ctl_datamove_remote_read_cb);
13498 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13500 * Make sure we free memory if there was an error.. The
13501 * ctl_datamove_remote_xfer() function will send the
13502 * datamove done message, or call the callback with an
13503 * error if there is a problem.
13505 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13506 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13513 * Process a datamove request from the other controller. This is used for
13514 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13515 * first. Once that is complete, the data gets DMAed into the remote
13516 * controller's memory. For reads, we DMA from the remote controller's
13517 * memory into our memory first, and then move it out to the FETD.
13520 ctl_datamove_remote(union ctl_io *io)
13522 struct ctl_softc *softc;
13524 softc = control_softc;
13526 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13529 * Note that we look for an aborted I/O here, but don't do some of
13530 * the other checks that ctl_datamove() normally does.
13531 * We don't need to run the datamove delay code, since that should
13532 * have been done if need be on the other controller.
13534 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13535 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13536 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13537 io->io_hdr.nexus.targ_port,
13538 io->io_hdr.nexus.targ_target.id,
13539 io->io_hdr.nexus.targ_lun);
13540 io->io_hdr.port_status = 31338;
13541 ctl_send_datamove_done(io, /*have_lock*/ 0);
13545 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13546 ctl_datamove_remote_write(io);
13547 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13548 ctl_datamove_remote_read(io);
13550 union ctl_ha_msg msg;
13551 struct scsi_sense_data *sense;
13555 memset(&msg, 0, sizeof(msg));
13557 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13558 msg.hdr.status = CTL_SCSI_ERROR;
13559 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13561 retry_count = 4243;
13563 sense = &msg.scsi.sense_data;
13564 sks[0] = SSD_SCS_VALID;
13565 sks[1] = (retry_count >> 8) & 0xff;
13566 sks[2] = retry_count & 0xff;
13568 /* "Internal target failure" */
13569 scsi_set_sense_data(sense,
13570 /*sense_format*/ SSD_TYPE_NONE,
13571 /*current_error*/ 1,
13572 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13575 /*type*/ SSD_ELEM_SKS,
13576 /*size*/ sizeof(sks),
13580 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13581 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13582 ctl_failover_io(io, /*have_lock*/ 1);
13586 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13587 CTL_HA_STATUS_SUCCESS) {
13588 /* XXX KDM what to do if this fails? */
13596 ctl_process_done(union ctl_io *io)
13598 struct ctl_lun *lun;
13599 struct ctl_softc *ctl_softc;
13600 void (*fe_done)(union ctl_io *io);
13601 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13603 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13606 control_softc->ctl_ports[targ_port]->fe_done;
13609 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13614 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13615 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13617 sbuf_cat(&sb, path_str);
13618 switch (io->io_hdr.io_type) {
13620 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13621 sbuf_printf(&sb, "\n");
13622 sbuf_cat(&sb, path_str);
13623 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13624 io->scsiio.tag_num, io->scsiio.tag_type);
13627 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13628 "Tag Type: %d\n", io->taskio.task_action,
13629 io->taskio.tag_num, io->taskio.tag_type);
13632 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13633 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13636 sbuf_cat(&sb, path_str);
13637 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13638 (intmax_t)time_uptime - io->io_hdr.start_time);
13640 printf("%s", sbuf_data(&sb));
13642 #endif /* CTL_TIME_IO */
13644 switch (io->io_hdr.io_type) {
13648 if (bootverbose || verbose > 0)
13649 ctl_io_error_print(io, NULL);
13650 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13654 return (CTL_RETVAL_COMPLETE);
13657 printf("ctl_process_done: invalid io type %d\n",
13658 io->io_hdr.io_type);
13659 panic("ctl_process_done: invalid io type %d\n",
13660 io->io_hdr.io_type);
13661 break; /* NOTREACHED */
13664 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13666 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13667 io->io_hdr.nexus.targ_mapped_lun));
13671 ctl_softc = lun->ctl_softc;
13673 mtx_lock(&lun->lun_lock);
13676 * Check to see if we have any errors to inject here. We only
13677 * inject errors for commands that don't already have errors set.
13679 if ((STAILQ_FIRST(&lun->error_list) != NULL)
13680 && ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS))
13681 ctl_inject_error(lun, io);
13684 * XXX KDM how do we treat commands that aren't completed
13687 * XXX KDM should we also track I/O latency?
13689 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13690 io->io_hdr.io_type == CTL_IO_SCSI) {
13692 struct bintime cur_bt;
13696 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13698 type = CTL_STATS_READ;
13699 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13701 type = CTL_STATS_WRITE;
13703 type = CTL_STATS_NO_IO;
13705 lun->stats.ports[targ_port].bytes[type] +=
13706 io->scsiio.kern_total_len;
13707 lun->stats.ports[targ_port].operations[type]++;
13709 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13710 &io->io_hdr.dma_bt);
13711 lun->stats.ports[targ_port].num_dmas[type] +=
13712 io->io_hdr.num_dmas;
13713 getbintime(&cur_bt);
13714 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13715 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13720 * Remove this from the OOA queue.
13722 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13725 * Run through the blocked queue on this LUN and see if anything
13726 * has become unblocked, now that this transaction is done.
13728 ctl_check_blocked(lun);
13731 * If the LUN has been invalidated, free it if there is nothing
13732 * left on its OOA queue.
13734 if ((lun->flags & CTL_LUN_INVALID)
13735 && TAILQ_EMPTY(&lun->ooa_queue)) {
13736 mtx_unlock(&lun->lun_lock);
13737 mtx_lock(&ctl_softc->ctl_lock);
13739 mtx_unlock(&ctl_softc->ctl_lock);
13741 mtx_unlock(&lun->lun_lock);
13744 * If this command has been aborted, make sure we set the status
13745 * properly. The FETD is responsible for freeing the I/O and doing
13746 * whatever it needs to do to clean up its state.
13748 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13749 ctl_set_task_aborted(&io->scsiio);
13752 * We print out status for every task management command. For SCSI
13753 * commands, we filter out any unit attention errors; they happen
13754 * on every boot, and would clutter up the log. Note: task
13755 * management commands aren't printed here, they are printed above,
13756 * since they should never even make it down here.
13758 switch (io->io_hdr.io_type) {
13759 case CTL_IO_SCSI: {
13760 int error_code, sense_key, asc, ascq;
13764 if (((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR)
13765 && (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13767 * Since this is just for printing, no need to
13768 * show errors here.
13770 scsi_extract_sense_len(&io->scsiio.sense_data,
13771 io->scsiio.sense_len,
13776 /*show_errors*/ 0);
13779 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
13780 && (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR)
13781 || (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND)
13782 || (sense_key != SSD_KEY_UNIT_ATTENTION))) {
13784 if ((time_uptime - ctl_softc->last_print_jiffies) <= 0){
13785 ctl_softc->skipped_prints++;
13787 uint32_t skipped_prints;
13789 skipped_prints = ctl_softc->skipped_prints;
13791 ctl_softc->skipped_prints = 0;
13792 ctl_softc->last_print_jiffies = time_uptime;
13794 if (skipped_prints > 0) {
13796 csevent_log(CSC_CTL | CSC_SHELF_SW |
13798 csevent_LogType_Trace,
13799 csevent_Severity_Information,
13800 csevent_AlertLevel_Green,
13801 csevent_FRU_Firmware,
13802 csevent_FRU_Unknown,
13803 "High CTL error volume, %d prints "
13804 "skipped", skipped_prints);
13807 if (bootverbose || verbose > 0)
13808 ctl_io_error_print(io, NULL);
13814 if (bootverbose || verbose > 0)
13815 ctl_io_error_print(io, NULL);
13822 * Tell the FETD or the other shelf controller we're done with this
13823 * command. Note that only SCSI commands get to this point. Task
13824 * management commands are completed above.
13826 * We only send status to the other controller if we're in XFER
13827 * mode. In SER_ONLY mode, the I/O is done on the controller that
13828 * received the I/O (from CTL's perspective), and so the status is
13831 * XXX KDM if we hold the lock here, we could cause a deadlock
13832 * if the frontend comes back in in this context to queue
13835 if ((ctl_softc->ha_mode == CTL_HA_MODE_XFER)
13836 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13837 union ctl_ha_msg msg;
13839 memset(&msg, 0, sizeof(msg));
13840 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13841 msg.hdr.original_sc = io->io_hdr.original_sc;
13842 msg.hdr.nexus = io->io_hdr.nexus;
13843 msg.hdr.status = io->io_hdr.status;
13844 msg.scsi.scsi_status = io->scsiio.scsi_status;
13845 msg.scsi.tag_num = io->scsiio.tag_num;
13846 msg.scsi.tag_type = io->scsiio.tag_type;
13847 msg.scsi.sense_len = io->scsiio.sense_len;
13848 msg.scsi.sense_residual = io->scsiio.sense_residual;
13849 msg.scsi.residual = io->scsiio.residual;
13850 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13851 sizeof(io->scsiio.sense_data));
13853 * We copy this whether or not this is an I/O-related
13854 * command. Otherwise, we'd have to go and check to see
13855 * whether it's a read/write command, and it really isn't
13858 memcpy(&msg.scsi.lbalen,
13859 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13860 sizeof(msg.scsi.lbalen));
13862 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13863 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13864 /* XXX do something here */
13873 return (CTL_RETVAL_COMPLETE);
13878 * Front end should call this if it doesn't do autosense. When the request
13879 * sense comes back in from the initiator, we'll dequeue this and send it.
13882 ctl_queue_sense(union ctl_io *io)
13884 struct ctl_lun *lun;
13885 struct ctl_softc *ctl_softc;
13886 uint32_t initidx, targ_lun;
13888 ctl_softc = control_softc;
13890 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13893 * LUN lookup will likely move to the ctl_work_thread() once we
13894 * have our new queueing infrastructure (that doesn't put things on
13895 * a per-LUN queue initially). That is so that we can handle
13896 * things like an INQUIRY to a LUN that we don't have enabled. We
13897 * can't deal with that right now.
13899 mtx_lock(&ctl_softc->ctl_lock);
13902 * If we don't have a LUN for this, just toss the sense
13905 targ_lun = io->io_hdr.nexus.targ_lun;
13906 targ_lun = ctl_map_lun(io->io_hdr.nexus.targ_port, targ_lun);
13907 if ((targ_lun < CTL_MAX_LUNS)
13908 && (ctl_softc->ctl_luns[targ_lun] != NULL))
13909 lun = ctl_softc->ctl_luns[targ_lun];
13913 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13915 mtx_lock(&lun->lun_lock);
13917 * Already have CA set for this LUN...toss the sense information.
13919 if (ctl_is_set(lun->have_ca, initidx)) {
13920 mtx_unlock(&lun->lun_lock);
13924 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13925 ctl_min(sizeof(lun->pending_sense[initidx]),
13926 sizeof(io->scsiio.sense_data)));
13927 ctl_set_mask(lun->have_ca, initidx);
13928 mtx_unlock(&lun->lun_lock);
13931 mtx_unlock(&ctl_softc->ctl_lock);
13935 return (CTL_RETVAL_COMPLETE);
13940 * Primary command inlet from frontend ports. All SCSI and task I/O
13941 * requests must go through this function.
13944 ctl_queue(union ctl_io *io)
13946 struct ctl_softc *ctl_softc;
13948 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13950 ctl_softc = control_softc;
13953 io->io_hdr.start_time = time_uptime;
13954 getbintime(&io->io_hdr.start_bt);
13955 #endif /* CTL_TIME_IO */
13957 /* Map FE-specific LUN ID into global one. */
13958 io->io_hdr.nexus.targ_mapped_lun =
13959 ctl_map_lun(io->io_hdr.nexus.targ_port, io->io_hdr.nexus.targ_lun);
13961 switch (io->io_hdr.io_type) {
13964 ctl_enqueue_incoming(io);
13967 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13971 return (CTL_RETVAL_COMPLETE);
13974 #ifdef CTL_IO_DELAY
13976 ctl_done_timer_wakeup(void *arg)
13980 io = (union ctl_io *)arg;
13983 #endif /* CTL_IO_DELAY */
13986 ctl_done(union ctl_io *io)
13988 struct ctl_softc *ctl_softc;
13990 ctl_softc = control_softc;
13993 * Enable this to catch duplicate completion issues.
13996 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13997 printf("%s: type %d msg %d cdb %x iptl: "
13998 "%d:%d:%d:%d tag 0x%04x "
13999 "flag %#x status %x\n",
14001 io->io_hdr.io_type,
14002 io->io_hdr.msg_type,
14004 io->io_hdr.nexus.initid.id,
14005 io->io_hdr.nexus.targ_port,
14006 io->io_hdr.nexus.targ_target.id,
14007 io->io_hdr.nexus.targ_lun,
14008 (io->io_hdr.io_type ==
14010 io->taskio.tag_num :
14011 io->scsiio.tag_num,
14013 io->io_hdr.status);
14015 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
14019 * This is an internal copy of an I/O, and should not go through
14020 * the normal done processing logic.
14022 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
14026 * We need to send a msg to the serializing shelf to finish the IO
14027 * as well. We don't send a finish message to the other shelf if
14028 * this is a task management command. Task management commands
14029 * aren't serialized in the OOA queue, but rather just executed on
14030 * both shelf controllers for commands that originated on that
14033 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
14034 && (io->io_hdr.io_type != CTL_IO_TASK)) {
14035 union ctl_ha_msg msg_io;
14037 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
14038 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
14039 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
14040 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
14042 /* continue on to finish IO */
14044 #ifdef CTL_IO_DELAY
14045 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
14046 struct ctl_lun *lun;
14048 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14050 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
14052 struct ctl_lun *lun;
14054 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14057 && (lun->delay_info.done_delay > 0)) {
14058 struct callout *callout;
14060 callout = (struct callout *)&io->io_hdr.timer_bytes;
14061 callout_init(callout, /*mpsafe*/ 1);
14062 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
14063 callout_reset(callout,
14064 lun->delay_info.done_delay * hz,
14065 ctl_done_timer_wakeup, io);
14066 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
14067 lun->delay_info.done_delay = 0;
14071 #endif /* CTL_IO_DELAY */
14073 ctl_enqueue_done(io);
14077 ctl_isc(struct ctl_scsiio *ctsio)
14079 struct ctl_lun *lun;
14082 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14084 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
14086 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
14088 retval = lun->backend->data_submit((union ctl_io *)ctsio);
14095 ctl_work_thread(void *arg)
14097 struct ctl_thread *thr = (struct ctl_thread *)arg;
14098 struct ctl_softc *softc = thr->ctl_softc;
14102 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
14108 * We handle the queues in this order:
14110 * - done queue (to free up resources, unblock other commands)
14114 * If those queues are empty, we break out of the loop and
14117 mtx_lock(&thr->queue_lock);
14118 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
14120 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
14121 mtx_unlock(&thr->queue_lock);
14122 ctl_handle_isc(io);
14125 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
14127 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
14128 /* clear any blocked commands, call fe_done */
14129 mtx_unlock(&thr->queue_lock);
14130 retval = ctl_process_done(io);
14133 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
14135 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
14136 mtx_unlock(&thr->queue_lock);
14137 if (io->io_hdr.io_type == CTL_IO_TASK)
14140 ctl_scsiio_precheck(softc, &io->scsiio);
14143 if (!ctl_pause_rtr) {
14144 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
14146 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
14147 mtx_unlock(&thr->queue_lock);
14148 retval = ctl_scsiio(&io->scsiio);
14149 if (retval != CTL_RETVAL_COMPLETE)
14150 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
14155 /* Sleep until we have something to do. */
14156 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
14161 ctl_lun_thread(void *arg)
14163 struct ctl_softc *softc = (struct ctl_softc *)arg;
14164 struct ctl_be_lun *be_lun;
14167 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
14171 mtx_lock(&softc->ctl_lock);
14172 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
14173 if (be_lun != NULL) {
14174 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
14175 mtx_unlock(&softc->ctl_lock);
14176 ctl_create_lun(be_lun);
14180 /* Sleep until we have something to do. */
14181 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
14182 PDROP | PRIBIO, "-", 0);
14187 ctl_enqueue_incoming(union ctl_io *io)
14189 struct ctl_softc *softc = control_softc;
14190 struct ctl_thread *thr;
14193 idx = (io->io_hdr.nexus.targ_port * 127 +
14194 io->io_hdr.nexus.initid.id) % worker_threads;
14195 thr = &softc->threads[idx];
14196 mtx_lock(&thr->queue_lock);
14197 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14198 mtx_unlock(&thr->queue_lock);
14203 ctl_enqueue_rtr(union ctl_io *io)
14205 struct ctl_softc *softc = control_softc;
14206 struct ctl_thread *thr;
14208 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14209 mtx_lock(&thr->queue_lock);
14210 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14211 mtx_unlock(&thr->queue_lock);
14216 ctl_enqueue_done(union ctl_io *io)
14218 struct ctl_softc *softc = control_softc;
14219 struct ctl_thread *thr;
14221 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14222 mtx_lock(&thr->queue_lock);
14223 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14224 mtx_unlock(&thr->queue_lock);
14229 ctl_enqueue_isc(union ctl_io *io)
14231 struct ctl_softc *softc = control_softc;
14232 struct ctl_thread *thr;
14234 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14235 mtx_lock(&thr->queue_lock);
14236 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14237 mtx_unlock(&thr->queue_lock);
14241 /* Initialization and failover */
14244 ctl_init_isc_msg(void)
14246 printf("CTL: Still calling this thing\n");
14251 * Initializes component into configuration defined by bootMode
14253 * returns hasc_Status:
14255 * ERROR - fatal error
14257 static ctl_ha_comp_status
14258 ctl_isc_init(struct ctl_ha_component *c)
14260 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14267 * Starts component in state requested. If component starts successfully,
14268 * it must set its own state to the requestrd state
14269 * When requested state is HASC_STATE_HA, the component may refine it
14270 * by adding _SLAVE or _MASTER flags.
14271 * Currently allowed state transitions are:
14272 * UNKNOWN->HA - initial startup
14273 * UNKNOWN->SINGLE - initial startup when no parter detected
14274 * HA->SINGLE - failover
14275 * returns ctl_ha_comp_status:
14276 * OK - component successfully started in requested state
14277 * FAILED - could not start the requested state, failover may
14279 * ERROR - fatal error detected, no future startup possible
14281 static ctl_ha_comp_status
14282 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14284 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14286 printf("%s: go\n", __func__);
14288 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14289 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14291 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14292 != CTL_HA_STATUS_SUCCESS) {
14293 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14294 ret = CTL_HA_COMP_STATUS_ERROR;
14296 } else if (CTL_HA_STATE_IS_HA(c->state)
14297 && CTL_HA_STATE_IS_SINGLE(state)){
14298 // HA->SINGLE transition
14302 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14304 ret = CTL_HA_COMP_STATUS_ERROR;
14306 if (CTL_HA_STATE_IS_SINGLE(state))
14315 * Quiesce component
14316 * The component must clear any error conditions (set status to OK) and
14317 * prepare itself to another Start call
14318 * returns ctl_ha_comp_status:
14322 static ctl_ha_comp_status
14323 ctl_isc_quiesce(struct ctl_ha_component *c)
14325 int ret = CTL_HA_COMP_STATUS_OK;
14332 struct ctl_ha_component ctl_ha_component_ctlisc =
14335 .state = CTL_HA_STATE_UNKNOWN,
14336 .init = ctl_isc_init,
14337 .start = ctl_isc_start,
14338 .quiesce = ctl_isc_quiesce