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.
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/ctype.h>
50 #include <sys/kernel.h>
51 #include <sys/types.h>
52 #include <sys/kthread.h>
54 #include <sys/fcntl.h>
56 #include <sys/module.h>
57 #include <sys/mutex.h>
58 #include <sys/condvar.h>
59 #include <sys/malloc.h>
61 #include <sys/ioccom.h>
62 #include <sys/queue.h>
65 #include <sys/endian.h>
66 #include <sys/sysctl.h>
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_da.h>
72 #include <cam/ctl/ctl_io.h>
73 #include <cam/ctl/ctl.h>
74 #include <cam/ctl/ctl_frontend.h>
75 #include <cam/ctl/ctl_frontend_internal.h>
76 #include <cam/ctl/ctl_util.h>
77 #include <cam/ctl/ctl_backend.h>
78 #include <cam/ctl/ctl_ioctl.h>
79 #include <cam/ctl/ctl_ha.h>
80 #include <cam/ctl/ctl_private.h>
81 #include <cam/ctl/ctl_debug.h>
82 #include <cam/ctl/ctl_scsi_all.h>
83 #include <cam/ctl/ctl_error.h>
85 struct ctl_softc *control_softc = NULL;
88 * Size and alignment macros needed for Copan-specific HA hardware. These
89 * can go away when the HA code is re-written, and uses busdma for any
92 #define CTL_ALIGN_8B(target, source, type) \
93 if (((uint32_t)source & 0x7) != 0) \
94 target = (type)(source + (0x8 - ((uint32_t)source & 0x7)));\
96 target = (type)source;
98 #define CTL_SIZE_8B(target, size) \
99 if ((size & 0x7) != 0) \
100 target = size + (0x8 - (size & 0x7)); \
104 #define CTL_ALIGN_8B_MARGIN 16
107 * Template mode pages.
111 * Note that these are default values only. The actual values will be
112 * filled in when the user does a mode sense.
114 const static struct copan_debugconf_subpage debugconf_page_default = {
115 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
116 DBGCNF_SUBPAGE_CODE, /* subpage */
117 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
118 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
119 DBGCNF_VERSION, /* page_version */
120 {CTL_TIME_IO_DEFAULT_SECS>>8,
121 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
124 const static struct copan_debugconf_subpage debugconf_page_changeable = {
125 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
126 DBGCNF_SUBPAGE_CODE, /* subpage */
127 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
128 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
129 0, /* page_version */
130 {0xff,0xff}, /* ctl_time_io_secs */
133 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
134 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
135 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
136 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
137 /*read_retry_count*/0,
138 /*correction_span*/0,
139 /*head_offset_count*/0,
140 /*data_strobe_offset_cnt*/0,
141 /*byte8*/SMS_RWER_LBPERE,
142 /*write_retry_count*/0,
144 /*recovery_time_limit*/{0, 0},
147 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
148 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
149 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
151 /*read_retry_count*/0,
152 /*correction_span*/0,
153 /*head_offset_count*/0,
154 /*data_strobe_offset_cnt*/0,
156 /*write_retry_count*/0,
158 /*recovery_time_limit*/{0, 0},
161 const static struct scsi_format_page format_page_default = {
162 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
163 /*page_length*/sizeof(struct scsi_format_page) - 2,
164 /*tracks_per_zone*/ {0, 0},
165 /*alt_sectors_per_zone*/ {0, 0},
166 /*alt_tracks_per_zone*/ {0, 0},
167 /*alt_tracks_per_lun*/ {0, 0},
168 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
169 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
170 /*bytes_per_sector*/ {0, 0},
171 /*interleave*/ {0, 0},
172 /*track_skew*/ {0, 0},
173 /*cylinder_skew*/ {0, 0},
175 /*reserved*/ {0, 0, 0}
178 const static struct scsi_format_page format_page_changeable = {
179 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
180 /*page_length*/sizeof(struct scsi_format_page) - 2,
181 /*tracks_per_zone*/ {0, 0},
182 /*alt_sectors_per_zone*/ {0, 0},
183 /*alt_tracks_per_zone*/ {0, 0},
184 /*alt_tracks_per_lun*/ {0, 0},
185 /*sectors_per_track*/ {0, 0},
186 /*bytes_per_sector*/ {0, 0},
187 /*interleave*/ {0, 0},
188 /*track_skew*/ {0, 0},
189 /*cylinder_skew*/ {0, 0},
191 /*reserved*/ {0, 0, 0}
194 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
195 /*page_code*/SMS_RIGID_DISK_PAGE,
196 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
197 /*cylinders*/ {0, 0, 0},
198 /*heads*/ CTL_DEFAULT_HEADS,
199 /*start_write_precomp*/ {0, 0, 0},
200 /*start_reduced_current*/ {0, 0, 0},
201 /*step_rate*/ {0, 0},
202 /*landing_zone_cylinder*/ {0, 0, 0},
203 /*rpl*/ SRDP_RPL_DISABLED,
204 /*rotational_offset*/ 0,
206 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
207 CTL_DEFAULT_ROTATION_RATE & 0xff},
211 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
212 /*page_code*/SMS_RIGID_DISK_PAGE,
213 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
214 /*cylinders*/ {0, 0, 0},
216 /*start_write_precomp*/ {0, 0, 0},
217 /*start_reduced_current*/ {0, 0, 0},
218 /*step_rate*/ {0, 0},
219 /*landing_zone_cylinder*/ {0, 0, 0},
221 /*rotational_offset*/ 0,
223 /*rotation_rate*/ {0, 0},
227 const static struct scsi_caching_page caching_page_default = {
228 /*page_code*/SMS_CACHING_PAGE,
229 /*page_length*/sizeof(struct scsi_caching_page) - 2,
230 /*flags1*/ SCP_DISC | SCP_WCE,
232 /*disable_pf_transfer_len*/ {0xff, 0xff},
233 /*min_prefetch*/ {0, 0},
234 /*max_prefetch*/ {0xff, 0xff},
235 /*max_pf_ceiling*/ {0xff, 0xff},
237 /*cache_segments*/ 0,
238 /*cache_seg_size*/ {0, 0},
240 /*non_cache_seg_size*/ {0, 0, 0}
243 const static struct scsi_caching_page caching_page_changeable = {
244 /*page_code*/SMS_CACHING_PAGE,
245 /*page_length*/sizeof(struct scsi_caching_page) - 2,
246 /*flags1*/ SCP_WCE | SCP_RCD,
248 /*disable_pf_transfer_len*/ {0, 0},
249 /*min_prefetch*/ {0, 0},
250 /*max_prefetch*/ {0, 0},
251 /*max_pf_ceiling*/ {0, 0},
253 /*cache_segments*/ 0,
254 /*cache_seg_size*/ {0, 0},
256 /*non_cache_seg_size*/ {0, 0, 0}
259 const static struct scsi_control_page control_page_default = {
260 /*page_code*/SMS_CONTROL_MODE_PAGE,
261 /*page_length*/sizeof(struct scsi_control_page) - 2,
263 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
266 /*aen_holdoff_period*/{0, 0},
267 /*busy_timeout_period*/{0, 0},
268 /*extended_selftest_completion_time*/{0, 0}
271 const static struct scsi_control_page control_page_changeable = {
272 /*page_code*/SMS_CONTROL_MODE_PAGE,
273 /*page_length*/sizeof(struct scsi_control_page) - 2,
275 /*queue_flags*/SCP_QUEUE_ALG_MASK,
276 /*eca_and_aen*/SCP_SWP,
278 /*aen_holdoff_period*/{0, 0},
279 /*busy_timeout_period*/{0, 0},
280 /*extended_selftest_completion_time*/{0, 0}
283 const static struct scsi_info_exceptions_page ie_page_default = {
284 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
285 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
286 /*info_flags*/SIEP_FLAGS_DEXCPT,
288 /*interval_timer*/{0, 0, 0, 0},
289 /*report_count*/{0, 0, 0, 0}
292 const static struct scsi_info_exceptions_page ie_page_changeable = {
293 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
294 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
297 /*interval_timer*/{0, 0, 0, 0},
298 /*report_count*/{0, 0, 0, 0}
301 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
303 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
304 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
305 /*subpage_code*/0x02,
306 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
308 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
313 /*count*/{0, 0, 0, 0}},
317 /*count*/{0, 0, 0, 0}},
321 /*count*/{0, 0, 0, 0}},
325 /*count*/{0, 0, 0, 0}}
329 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
330 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
331 /*subpage_code*/0x02,
332 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
334 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
339 /*count*/{0, 0, 0, 0}},
343 /*count*/{0, 0, 0, 0}},
347 /*count*/{0, 0, 0, 0}},
351 /*count*/{0, 0, 0, 0}}
356 * XXX KDM move these into the softc.
358 static int rcv_sync_msg;
359 static uint8_t ctl_pause_rtr;
361 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
362 static int worker_threads = -1;
363 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
364 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
365 &worker_threads, 1, "Number of worker threads");
366 static int ctl_debug = CTL_DEBUG_NONE;
367 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
368 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
369 &ctl_debug, 0, "Enabled debug flags");
372 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
373 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
374 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
375 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
377 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
379 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
381 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
382 static int ctl_init(void);
383 void ctl_shutdown(void);
384 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
385 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
386 static void ctl_ioctl_online(void *arg);
387 static void ctl_ioctl_offline(void *arg);
388 static int ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id);
389 static int ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id);
390 static int ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio);
391 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
392 static int ctl_ioctl_submit_wait(union ctl_io *io);
393 static void ctl_ioctl_datamove(union ctl_io *io);
394 static void ctl_ioctl_done(union ctl_io *io);
395 static void ctl_ioctl_hard_startstop_callback(void *arg,
396 struct cfi_metatask *metatask);
397 static void ctl_ioctl_bbrread_callback(void *arg,struct cfi_metatask *metatask);
398 static int ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
399 struct ctl_ooa *ooa_hdr,
400 struct ctl_ooa_entry *kern_entries);
401 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
403 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
404 struct ctl_be_lun *be_lun, struct ctl_id target_id);
405 static int ctl_free_lun(struct ctl_lun *lun);
406 static void ctl_create_lun(struct ctl_be_lun *be_lun);
407 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
409 static void ctl_failover_change_pages(struct ctl_softc *softc,
410 struct ctl_scsiio *ctsio, int master);
413 static int ctl_do_mode_select(union ctl_io *io);
414 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
415 uint64_t res_key, uint64_t sa_res_key,
416 uint8_t type, uint32_t residx,
417 struct ctl_scsiio *ctsio,
418 struct scsi_per_res_out *cdb,
419 struct scsi_per_res_out_parms* param);
420 static void ctl_pro_preempt_other(struct ctl_lun *lun,
421 union ctl_ha_msg *msg);
422 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
423 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
424 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
425 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
426 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
427 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
428 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
430 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
432 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
433 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
434 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
435 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
436 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
437 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
439 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
440 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
441 union ctl_io *pending_io, union ctl_io *ooa_io);
442 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
443 union ctl_io *starting_io);
444 static int ctl_check_blocked(struct ctl_lun *lun);
445 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
446 const struct ctl_cmd_entry *entry,
447 struct ctl_scsiio *ctsio);
448 //static int ctl_check_rtr(union ctl_io *pending_io, struct ctl_softc *softc);
449 static void ctl_failover(void);
450 static void ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx,
451 ctl_ua_type ua_type);
452 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
453 struct ctl_scsiio *ctsio);
454 static int ctl_scsiio(struct ctl_scsiio *ctsio);
456 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
457 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
458 ctl_ua_type ua_type);
459 static int ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io,
460 ctl_ua_type ua_type);
461 static int ctl_abort_task(union ctl_io *io);
462 static int ctl_abort_task_set(union ctl_io *io);
463 static int ctl_i_t_nexus_reset(union ctl_io *io);
464 static void ctl_run_task(union ctl_io *io);
466 static void ctl_datamove_timer_wakeup(void *arg);
467 static void ctl_done_timer_wakeup(void *arg);
468 #endif /* CTL_IO_DELAY */
470 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
471 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
472 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
473 static void ctl_datamove_remote_write(union ctl_io *io);
474 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
475 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
476 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
477 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
478 ctl_ha_dt_cb callback);
479 static void ctl_datamove_remote_read(union ctl_io *io);
480 static void ctl_datamove_remote(union ctl_io *io);
481 static int ctl_process_done(union ctl_io *io);
482 static void ctl_lun_thread(void *arg);
483 static void ctl_thresh_thread(void *arg);
484 static void ctl_work_thread(void *arg);
485 static void ctl_enqueue_incoming(union ctl_io *io);
486 static void ctl_enqueue_rtr(union ctl_io *io);
487 static void ctl_enqueue_done(union ctl_io *io);
488 static void ctl_enqueue_isc(union ctl_io *io);
489 static const struct ctl_cmd_entry *
490 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
491 static const struct ctl_cmd_entry *
492 ctl_validate_command(struct ctl_scsiio *ctsio);
493 static int ctl_cmd_applicable(uint8_t lun_type,
494 const struct ctl_cmd_entry *entry);
497 * Load the serialization table. This isn't very pretty, but is probably
498 * the easiest way to do it.
500 #include "ctl_ser_table.c"
503 * We only need to define open, close and ioctl routines for this driver.
505 static struct cdevsw ctl_cdevsw = {
506 .d_version = D_VERSION,
509 .d_close = ctl_close,
510 .d_ioctl = ctl_ioctl,
515 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
516 MALLOC_DEFINE(M_CTLIO, "ctlio", "Memory used for CTL requests");
518 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
520 static moduledata_t ctl_moduledata = {
522 ctl_module_event_handler,
526 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
527 MODULE_VERSION(ctl, 1);
529 static struct ctl_frontend ioctl_frontend =
535 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
536 union ctl_ha_msg *msg_info)
538 struct ctl_scsiio *ctsio;
540 if (msg_info->hdr.original_sc == NULL) {
541 printf("%s: original_sc == NULL!\n", __func__);
542 /* XXX KDM now what? */
546 ctsio = &msg_info->hdr.original_sc->scsiio;
547 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
548 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
549 ctsio->io_hdr.status = msg_info->hdr.status;
550 ctsio->scsi_status = msg_info->scsi.scsi_status;
551 ctsio->sense_len = msg_info->scsi.sense_len;
552 ctsio->sense_residual = msg_info->scsi.sense_residual;
553 ctsio->residual = msg_info->scsi.residual;
554 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
555 sizeof(ctsio->sense_data));
556 memcpy(&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
557 &msg_info->scsi.lbalen, sizeof(msg_info->scsi.lbalen));
558 ctl_enqueue_isc((union ctl_io *)ctsio);
562 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
563 union ctl_ha_msg *msg_info)
565 struct ctl_scsiio *ctsio;
567 if (msg_info->hdr.serializing_sc == NULL) {
568 printf("%s: serializing_sc == NULL!\n", __func__);
569 /* XXX KDM now what? */
573 ctsio = &msg_info->hdr.serializing_sc->scsiio;
576 * Attempt to catch the situation where an I/O has
577 * been freed, and we're using it again.
579 if (ctsio->io_hdr.io_type == 0xff) {
580 union ctl_io *tmp_io;
581 tmp_io = (union ctl_io *)ctsio;
582 printf("%s: %p use after free!\n", __func__,
584 printf("%s: type %d msg %d cdb %x iptl: "
585 "%d:%d:%d:%d tag 0x%04x "
586 "flag %#x status %x\n",
588 tmp_io->io_hdr.io_type,
589 tmp_io->io_hdr.msg_type,
590 tmp_io->scsiio.cdb[0],
591 tmp_io->io_hdr.nexus.initid.id,
592 tmp_io->io_hdr.nexus.targ_port,
593 tmp_io->io_hdr.nexus.targ_target.id,
594 tmp_io->io_hdr.nexus.targ_lun,
595 (tmp_io->io_hdr.io_type ==
597 tmp_io->taskio.tag_num :
598 tmp_io->scsiio.tag_num,
599 tmp_io->io_hdr.flags,
600 tmp_io->io_hdr.status);
603 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
604 ctl_enqueue_isc((union ctl_io *)ctsio);
608 * ISC (Inter Shelf Communication) event handler. Events from the HA
609 * subsystem come in here.
612 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
614 struct ctl_softc *softc;
616 struct ctl_prio *presio;
617 ctl_ha_status isc_status;
619 softc = control_softc;
624 printf("CTL: Isc Msg event %d\n", event);
626 if (event == CTL_HA_EVT_MSG_RECV) {
627 union ctl_ha_msg msg_info;
629 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
630 sizeof(msg_info), /*wait*/ 0);
632 printf("CTL: msg_type %d\n", msg_info.msg_type);
634 if (isc_status != 0) {
635 printf("Error receiving message, status = %d\n",
640 switch (msg_info.hdr.msg_type) {
641 case CTL_MSG_SERIALIZE:
643 printf("Serialize\n");
645 io = ctl_alloc_io_nowait(softc->othersc_pool);
647 printf("ctl_isc_event_handler: can't allocate "
650 /* Need to set busy and send msg back */
651 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
652 msg_info.hdr.status = CTL_SCSI_ERROR;
653 msg_info.scsi.scsi_status = SCSI_STATUS_BUSY;
654 msg_info.scsi.sense_len = 0;
655 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
656 sizeof(msg_info), 0) > CTL_HA_STATUS_SUCCESS){
661 // populate ctsio from msg_info
662 io->io_hdr.io_type = CTL_IO_SCSI;
663 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
664 io->io_hdr.original_sc = msg_info.hdr.original_sc;
666 printf("pOrig %x\n", (int)msg_info.original_sc);
668 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
671 * If we're in serialization-only mode, we don't
672 * want to go through full done processing. Thus
675 * XXX KDM add another flag that is more specific.
677 if (softc->ha_mode == CTL_HA_MODE_SER_ONLY)
678 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
679 io->io_hdr.nexus = msg_info.hdr.nexus;
681 printf("targ %d, port %d, iid %d, lun %d\n",
682 io->io_hdr.nexus.targ_target.id,
683 io->io_hdr.nexus.targ_port,
684 io->io_hdr.nexus.initid.id,
685 io->io_hdr.nexus.targ_lun);
687 io->scsiio.tag_num = msg_info.scsi.tag_num;
688 io->scsiio.tag_type = msg_info.scsi.tag_type;
689 memcpy(io->scsiio.cdb, msg_info.scsi.cdb,
691 if (softc->ha_mode == CTL_HA_MODE_XFER) {
692 const struct ctl_cmd_entry *entry;
694 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
695 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
697 entry->flags & CTL_FLAG_DATA_MASK;
702 /* Performed on the Originating SC, XFER mode only */
703 case CTL_MSG_DATAMOVE: {
704 struct ctl_sg_entry *sgl;
707 io = msg_info.hdr.original_sc;
709 printf("%s: original_sc == NULL!\n", __func__);
710 /* XXX KDM do something here */
713 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
714 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
716 * Keep track of this, we need to send it back over
717 * when the datamove is complete.
719 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
721 if (msg_info.dt.sg_sequence == 0) {
723 * XXX KDM we use the preallocated S/G list
724 * here, but we'll need to change this to
725 * dynamic allocation if we need larger S/G
728 if (msg_info.dt.kern_sg_entries >
729 sizeof(io->io_hdr.remote_sglist) /
730 sizeof(io->io_hdr.remote_sglist[0])) {
731 printf("%s: number of S/G entries "
732 "needed %u > allocated num %zd\n",
734 msg_info.dt.kern_sg_entries,
735 sizeof(io->io_hdr.remote_sglist)/
736 sizeof(io->io_hdr.remote_sglist[0]));
739 * XXX KDM send a message back to
740 * the other side to shut down the
741 * DMA. The error will come back
742 * through via the normal channel.
746 sgl = io->io_hdr.remote_sglist;
748 sizeof(io->io_hdr.remote_sglist));
750 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
752 io->scsiio.kern_sg_entries =
753 msg_info.dt.kern_sg_entries;
754 io->scsiio.rem_sg_entries =
755 msg_info.dt.kern_sg_entries;
756 io->scsiio.kern_data_len =
757 msg_info.dt.kern_data_len;
758 io->scsiio.kern_total_len =
759 msg_info.dt.kern_total_len;
760 io->scsiio.kern_data_resid =
761 msg_info.dt.kern_data_resid;
762 io->scsiio.kern_rel_offset =
763 msg_info.dt.kern_rel_offset;
765 * Clear out per-DMA flags.
767 io->io_hdr.flags &= ~CTL_FLAG_RDMA_MASK;
769 * Add per-DMA flags that are set for this
770 * particular DMA request.
772 io->io_hdr.flags |= msg_info.dt.flags &
775 sgl = (struct ctl_sg_entry *)
776 io->scsiio.kern_data_ptr;
778 for (i = msg_info.dt.sent_sg_entries, j = 0;
779 i < (msg_info.dt.sent_sg_entries +
780 msg_info.dt.cur_sg_entries); i++, j++) {
781 sgl[i].addr = msg_info.dt.sg_list[j].addr;
782 sgl[i].len = msg_info.dt.sg_list[j].len;
785 printf("%s: L: %p,%d -> %p,%d j=%d, i=%d\n",
787 msg_info.dt.sg_list[j].addr,
788 msg_info.dt.sg_list[j].len,
789 sgl[i].addr, sgl[i].len, j, i);
793 memcpy(&sgl[msg_info.dt.sent_sg_entries],
795 sizeof(*sgl) * msg_info.dt.cur_sg_entries);
799 * If this is the last piece of the I/O, we've got
800 * the full S/G list. Queue processing in the thread.
801 * Otherwise wait for the next piece.
803 if (msg_info.dt.sg_last != 0)
807 /* Performed on the Serializing (primary) SC, XFER mode only */
808 case CTL_MSG_DATAMOVE_DONE: {
809 if (msg_info.hdr.serializing_sc == NULL) {
810 printf("%s: serializing_sc == NULL!\n",
812 /* XXX KDM now what? */
816 * We grab the sense information here in case
817 * there was a failure, so we can return status
818 * back to the initiator.
820 io = msg_info.hdr.serializing_sc;
821 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
822 io->io_hdr.status = msg_info.hdr.status;
823 io->scsiio.scsi_status = msg_info.scsi.scsi_status;
824 io->scsiio.sense_len = msg_info.scsi.sense_len;
825 io->scsiio.sense_residual =msg_info.scsi.sense_residual;
826 io->io_hdr.port_status = msg_info.scsi.fetd_status;
827 io->scsiio.residual = msg_info.scsi.residual;
828 memcpy(&io->scsiio.sense_data,&msg_info.scsi.sense_data,
829 sizeof(io->scsiio.sense_data));
834 /* Preformed on Originating SC, SER_ONLY mode */
836 io = msg_info.hdr.original_sc;
838 printf("%s: Major Bummer\n", __func__);
842 printf("pOrig %x\n",(int) ctsio);
845 io->io_hdr.msg_type = CTL_MSG_R2R;
846 io->io_hdr.serializing_sc = msg_info.hdr.serializing_sc;
851 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
853 * Performed on the Originating (i.e. secondary) SC in XFER
856 case CTL_MSG_FINISH_IO:
857 if (softc->ha_mode == CTL_HA_MODE_XFER)
858 ctl_isc_handler_finish_xfer(softc,
861 ctl_isc_handler_finish_ser_only(softc,
865 /* Preformed on Originating SC */
866 case CTL_MSG_BAD_JUJU:
867 io = msg_info.hdr.original_sc;
869 printf("%s: Bad JUJU!, original_sc is NULL!\n",
873 ctl_copy_sense_data(&msg_info, io);
875 * IO should have already been cleaned up on other
876 * SC so clear this flag so we won't send a message
877 * back to finish the IO there.
879 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
880 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
882 /* io = msg_info.hdr.serializing_sc; */
883 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
887 /* Handle resets sent from the other side */
888 case CTL_MSG_MANAGE_TASKS: {
889 struct ctl_taskio *taskio;
890 taskio = (struct ctl_taskio *)ctl_alloc_io_nowait(
891 softc->othersc_pool);
892 if (taskio == NULL) {
893 printf("ctl_isc_event_handler: can't allocate "
896 /* should I just call the proper reset func
900 ctl_zero_io((union ctl_io *)taskio);
901 taskio->io_hdr.io_type = CTL_IO_TASK;
902 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
903 taskio->io_hdr.nexus = msg_info.hdr.nexus;
904 taskio->task_action = msg_info.task.task_action;
905 taskio->tag_num = msg_info.task.tag_num;
906 taskio->tag_type = msg_info.task.tag_type;
908 taskio->io_hdr.start_time = time_uptime;
909 getbintime(&taskio->io_hdr.start_bt);
911 cs_prof_gettime(&taskio->io_hdr.start_ticks);
913 #endif /* CTL_TIME_IO */
914 ctl_run_task((union ctl_io *)taskio);
917 /* Persistent Reserve action which needs attention */
918 case CTL_MSG_PERS_ACTION:
919 presio = (struct ctl_prio *)ctl_alloc_io_nowait(
920 softc->othersc_pool);
921 if (presio == NULL) {
922 printf("ctl_isc_event_handler: can't allocate "
925 /* Need to set busy and send msg back */
928 ctl_zero_io((union ctl_io *)presio);
929 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
930 presio->pr_msg = msg_info.pr;
931 ctl_enqueue_isc((union ctl_io *)presio);
933 case CTL_MSG_SYNC_FE:
937 printf("How did I get here?\n");
939 } else if (event == CTL_HA_EVT_MSG_SENT) {
940 if (param != CTL_HA_STATUS_SUCCESS) {
941 printf("Bad status from ctl_ha_msg_send status %d\n",
945 } else if (event == CTL_HA_EVT_DISCONNECT) {
946 printf("CTL: Got a disconnect from Isc\n");
949 printf("ctl_isc_event_handler: Unknown event %d\n", event);
958 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
960 struct scsi_sense_data *sense;
962 sense = &dest->scsiio.sense_data;
963 bcopy(&src->scsi.sense_data, sense, sizeof(*sense));
964 dest->scsiio.scsi_status = src->scsi.scsi_status;
965 dest->scsiio.sense_len = src->scsi.sense_len;
966 dest->io_hdr.status = src->hdr.status;
970 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
974 mtx_assert(&lun->lun_lock, MA_OWNED);
975 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
978 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
982 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
986 mtx_assert(&lun->lun_lock, MA_OWNED);
987 for (i = 0; i < CTL_MAX_PORTS; i++) {
988 if (lun->pending_ua[i] == NULL)
990 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
991 if (i * CTL_MAX_INIT_PER_PORT + j == except)
993 lun->pending_ua[i][j] |= ua;
999 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1003 mtx_assert(&lun->lun_lock, MA_OWNED);
1004 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1007 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1011 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1015 mtx_assert(&lun->lun_lock, MA_OWNED);
1016 for (i = 0; i < CTL_MAX_PORTS; i++) {
1017 if (lun->pending_ua[i] == NULL)
1019 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1020 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1022 lun->pending_ua[i][j] &= ~ua;
1028 ctl_ha_state_sysctl(SYSCTL_HANDLER_ARGS)
1030 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1031 struct ctl_lun *lun;
1034 if (softc->flags & CTL_FLAG_ACTIVE_SHELF)
1039 error = sysctl_handle_int(oidp, &value, 0, req);
1040 if ((error != 0) || (req->newptr == NULL))
1043 mtx_lock(&softc->ctl_lock);
1045 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1047 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1048 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1049 mtx_lock(&lun->lun_lock);
1050 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1051 mtx_unlock(&lun->lun_lock);
1053 mtx_unlock(&softc->ctl_lock);
1060 struct ctl_softc *softc;
1062 struct ctl_port *port;
1063 int i, error, retval;
1070 control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1072 softc = control_softc;
1074 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1077 softc->dev->si_drv1 = softc;
1080 * By default, return a "bad LUN" peripheral qualifier for unknown
1081 * LUNs. The user can override this default using the tunable or
1082 * sysctl. See the comment in ctl_inquiry_std() for more details.
1084 softc->inquiry_pq_no_lun = 1;
1085 TUNABLE_INT_FETCH("kern.cam.ctl.inquiry_pq_no_lun",
1086 &softc->inquiry_pq_no_lun);
1087 sysctl_ctx_init(&softc->sysctl_ctx);
1088 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1089 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1090 CTLFLAG_RD, 0, "CAM Target Layer");
1092 if (softc->sysctl_tree == NULL) {
1093 printf("%s: unable to allocate sysctl tree\n", __func__);
1094 destroy_dev(softc->dev);
1095 free(control_softc, M_DEVBUF);
1096 control_softc = NULL;
1100 SYSCTL_ADD_INT(&softc->sysctl_ctx,
1101 SYSCTL_CHILDREN(softc->sysctl_tree), OID_AUTO,
1102 "inquiry_pq_no_lun", CTLFLAG_RW,
1103 &softc->inquiry_pq_no_lun, 0,
1104 "Report no lun possible for invalid LUNs");
1106 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1107 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1108 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1109 softc->open_count = 0;
1112 * Default to actually sending a SYNCHRONIZE CACHE command down to
1115 softc->flags = CTL_FLAG_REAL_SYNC;
1118 * In Copan's HA scheme, the "master" and "slave" roles are
1119 * figured out through the slot the controller is in. Although it
1120 * is an active/active system, someone has to be in charge.
1122 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1123 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1124 "HA head ID (0 - no HA)");
1125 if (softc->ha_id == 0) {
1126 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1127 softc->is_single = 1;
1128 softc->port_offset = 0;
1130 softc->port_offset = (softc->ha_id - 1) * CTL_MAX_PORTS;
1131 softc->persis_offset = softc->port_offset * CTL_MAX_INIT_PER_PORT;
1134 * XXX KDM need to figure out where we want to get our target ID
1135 * and WWID. Is it different on each port?
1137 softc->target.id = 0;
1138 softc->target.wwid[0] = 0x12345678;
1139 softc->target.wwid[1] = 0x87654321;
1140 STAILQ_INIT(&softc->lun_list);
1141 STAILQ_INIT(&softc->pending_lun_queue);
1142 STAILQ_INIT(&softc->fe_list);
1143 STAILQ_INIT(&softc->port_list);
1144 STAILQ_INIT(&softc->be_list);
1145 ctl_tpc_init(softc);
1147 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1150 printf("ctl: can't allocate %d entry other SC pool, "
1151 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1154 softc->othersc_pool = other_pool;
1156 if (worker_threads <= 0)
1157 worker_threads = max(1, mp_ncpus / 4);
1158 if (worker_threads > CTL_MAX_THREADS)
1159 worker_threads = CTL_MAX_THREADS;
1161 for (i = 0; i < worker_threads; i++) {
1162 struct ctl_thread *thr = &softc->threads[i];
1164 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1165 thr->ctl_softc = softc;
1166 STAILQ_INIT(&thr->incoming_queue);
1167 STAILQ_INIT(&thr->rtr_queue);
1168 STAILQ_INIT(&thr->done_queue);
1169 STAILQ_INIT(&thr->isc_queue);
1171 error = kproc_kthread_add(ctl_work_thread, thr,
1172 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1174 printf("error creating CTL work thread!\n");
1175 ctl_pool_free(other_pool);
1179 error = kproc_kthread_add(ctl_lun_thread, softc,
1180 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1182 printf("error creating CTL lun thread!\n");
1183 ctl_pool_free(other_pool);
1186 error = kproc_kthread_add(ctl_thresh_thread, softc,
1187 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1189 printf("error creating CTL threshold thread!\n");
1190 ctl_pool_free(other_pool);
1194 printf("ctl: CAM Target Layer loaded\n");
1197 * Initialize the ioctl front end.
1199 ctl_frontend_register(&ioctl_frontend);
1200 port = &softc->ioctl_info.port;
1201 port->frontend = &ioctl_frontend;
1202 sprintf(softc->ioctl_info.port_name, "ioctl");
1203 port->port_type = CTL_PORT_IOCTL;
1204 port->num_requested_ctl_io = 100;
1205 port->port_name = softc->ioctl_info.port_name;
1206 port->port_online = ctl_ioctl_online;
1207 port->port_offline = ctl_ioctl_offline;
1208 port->onoff_arg = &softc->ioctl_info;
1209 port->lun_enable = ctl_ioctl_lun_enable;
1210 port->lun_disable = ctl_ioctl_lun_disable;
1211 port->targ_lun_arg = &softc->ioctl_info;
1212 port->fe_datamove = ctl_ioctl_datamove;
1213 port->fe_done = ctl_ioctl_done;
1214 port->max_targets = 15;
1215 port->max_target_id = 15;
1217 if (ctl_port_register(&softc->ioctl_info.port) != 0) {
1218 printf("ctl: ioctl front end registration failed, will "
1219 "continue anyway\n");
1222 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1223 OID_AUTO, "ha_state", CTLTYPE_INT | CTLFLAG_RWTUN,
1224 softc, 0, ctl_ha_state_sysctl, "I", "HA state for this head");
1227 if (sizeof(struct callout) > CTL_TIMER_BYTES) {
1228 printf("sizeof(struct callout) %zd > CTL_TIMER_BYTES %zd\n",
1229 sizeof(struct callout), CTL_TIMER_BYTES);
1232 #endif /* CTL_IO_DELAY */
1240 struct ctl_softc *softc;
1241 struct ctl_lun *lun, *next_lun;
1243 softc = (struct ctl_softc *)control_softc;
1245 if (ctl_port_deregister(&softc->ioctl_info.port) != 0)
1246 printf("ctl: ioctl front end deregistration failed\n");
1248 mtx_lock(&softc->ctl_lock);
1253 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1254 next_lun = STAILQ_NEXT(lun, links);
1258 mtx_unlock(&softc->ctl_lock);
1260 ctl_frontend_deregister(&ioctl_frontend);
1263 ctl_shutdown_thread(softc->work_thread);
1264 mtx_destroy(&softc->queue_lock);
1267 ctl_tpc_shutdown(softc);
1268 uma_zdestroy(softc->io_zone);
1269 mtx_destroy(&softc->ctl_lock);
1271 destroy_dev(softc->dev);
1273 sysctl_ctx_free(&softc->sysctl_ctx);
1275 free(control_softc, M_DEVBUF);
1276 control_softc = NULL;
1279 printf("ctl: CAM Target Layer unloaded\n");
1283 ctl_module_event_handler(module_t mod, int what, void *arg)
1288 return (ctl_init());
1292 return (EOPNOTSUPP);
1297 * XXX KDM should we do some access checks here? Bump a reference count to
1298 * prevent a CTL module from being unloaded while someone has it open?
1301 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1307 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1313 ctl_port_enable(ctl_port_type port_type)
1315 struct ctl_softc *softc = control_softc;
1316 struct ctl_port *port;
1318 if (softc->is_single == 0) {
1319 union ctl_ha_msg msg_info;
1323 printf("%s: HA mode, synchronizing frontend enable\n",
1326 msg_info.hdr.msg_type = CTL_MSG_SYNC_FE;
1327 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1328 sizeof(msg_info), 1 )) > CTL_HA_STATUS_SUCCESS) {
1329 printf("Sync msg send error retval %d\n", isc_retval);
1331 if (!rcv_sync_msg) {
1332 isc_retval=ctl_ha_msg_recv(CTL_HA_CHAN_CTL, &msg_info,
1333 sizeof(msg_info), 1);
1336 printf("CTL:Frontend Enable\n");
1338 printf("%s: single mode, skipping frontend synchronization\n",
1343 STAILQ_FOREACH(port, &softc->port_list, links) {
1344 if (port_type & port->port_type)
1347 printf("port %d\n", port->targ_port);
1349 ctl_port_online(port);
1357 ctl_port_disable(ctl_port_type port_type)
1359 struct ctl_softc *softc;
1360 struct ctl_port *port;
1362 softc = control_softc;
1364 STAILQ_FOREACH(port, &softc->port_list, links) {
1365 if (port_type & port->port_type)
1366 ctl_port_offline(port);
1373 * Returns 0 for success, 1 for failure.
1374 * Currently the only failure mode is if there aren't enough entries
1375 * allocated. So, in case of a failure, look at num_entries_dropped,
1376 * reallocate and try again.
1379 ctl_port_list(struct ctl_port_entry *entries, int num_entries_alloced,
1380 int *num_entries_filled, int *num_entries_dropped,
1381 ctl_port_type port_type, int no_virtual)
1383 struct ctl_softc *softc;
1384 struct ctl_port *port;
1385 int entries_dropped, entries_filled;
1389 softc = control_softc;
1393 entries_dropped = 0;
1396 mtx_lock(&softc->ctl_lock);
1397 STAILQ_FOREACH(port, &softc->port_list, links) {
1398 struct ctl_port_entry *entry;
1400 if ((port->port_type & port_type) == 0)
1403 if ((no_virtual != 0)
1404 && (port->virtual_port != 0))
1407 if (entries_filled >= num_entries_alloced) {
1411 entry = &entries[i];
1413 entry->port_type = port->port_type;
1414 strlcpy(entry->port_name, port->port_name,
1415 sizeof(entry->port_name));
1416 entry->physical_port = port->physical_port;
1417 entry->virtual_port = port->virtual_port;
1418 entry->wwnn = port->wwnn;
1419 entry->wwpn = port->wwpn;
1425 mtx_unlock(&softc->ctl_lock);
1427 if (entries_dropped > 0)
1430 *num_entries_dropped = entries_dropped;
1431 *num_entries_filled = entries_filled;
1437 ctl_ioctl_online(void *arg)
1439 struct ctl_ioctl_info *ioctl_info;
1441 ioctl_info = (struct ctl_ioctl_info *)arg;
1443 ioctl_info->flags |= CTL_IOCTL_FLAG_ENABLED;
1447 ctl_ioctl_offline(void *arg)
1449 struct ctl_ioctl_info *ioctl_info;
1451 ioctl_info = (struct ctl_ioctl_info *)arg;
1453 ioctl_info->flags &= ~CTL_IOCTL_FLAG_ENABLED;
1457 * Remove an initiator by port number and initiator ID.
1458 * Returns 0 for success, -1 for failure.
1461 ctl_remove_initiator(struct ctl_port *port, int iid)
1463 struct ctl_softc *softc = control_softc;
1465 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1467 if (iid > CTL_MAX_INIT_PER_PORT) {
1468 printf("%s: initiator ID %u > maximun %u!\n",
1469 __func__, iid, CTL_MAX_INIT_PER_PORT);
1473 mtx_lock(&softc->ctl_lock);
1474 port->wwpn_iid[iid].in_use--;
1475 port->wwpn_iid[iid].last_use = time_uptime;
1476 mtx_unlock(&softc->ctl_lock);
1482 * Add an initiator to the initiator map.
1483 * Returns iid for success, < 0 for failure.
1486 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
1488 struct ctl_softc *softc = control_softc;
1492 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1494 if (iid >= CTL_MAX_INIT_PER_PORT) {
1495 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
1496 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
1501 mtx_lock(&softc->ctl_lock);
1503 if (iid < 0 && (wwpn != 0 || name != NULL)) {
1504 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1505 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
1509 if (name != NULL && port->wwpn_iid[i].name != NULL &&
1510 strcmp(name, port->wwpn_iid[i].name) == 0) {
1518 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1519 if (port->wwpn_iid[i].in_use == 0 &&
1520 port->wwpn_iid[i].wwpn == 0 &&
1521 port->wwpn_iid[i].name == NULL) {
1530 best_time = INT32_MAX;
1531 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1532 if (port->wwpn_iid[i].in_use == 0) {
1533 if (port->wwpn_iid[i].last_use < best_time) {
1535 best_time = port->wwpn_iid[i].last_use;
1543 mtx_unlock(&softc->ctl_lock);
1548 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
1550 * This is not an error yet.
1552 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
1554 printf("%s: port %d iid %u WWPN %#jx arrived"
1555 " again\n", __func__, port->targ_port,
1556 iid, (uintmax_t)wwpn);
1560 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
1561 strcmp(name, port->wwpn_iid[iid].name) == 0) {
1563 printf("%s: port %d iid %u name '%s' arrived"
1564 " again\n", __func__, port->targ_port,
1571 * This is an error, but what do we do about it? The
1572 * driver is telling us we have a new WWPN for this
1573 * initiator ID, so we pretty much need to use it.
1575 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
1576 " but WWPN %#jx '%s' is still at that address\n",
1577 __func__, port->targ_port, iid, wwpn, name,
1578 (uintmax_t)port->wwpn_iid[iid].wwpn,
1579 port->wwpn_iid[iid].name);
1582 * XXX KDM clear have_ca and ua_pending on each LUN for
1587 free(port->wwpn_iid[iid].name, M_CTL);
1588 port->wwpn_iid[iid].name = name;
1589 port->wwpn_iid[iid].wwpn = wwpn;
1590 port->wwpn_iid[iid].in_use++;
1591 mtx_unlock(&softc->ctl_lock);
1597 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
1601 switch (port->port_type) {
1604 struct scsi_transportid_fcp *id =
1605 (struct scsi_transportid_fcp *)buf;
1606 if (port->wwpn_iid[iid].wwpn == 0)
1608 memset(id, 0, sizeof(*id));
1609 id->format_protocol = SCSI_PROTO_FC;
1610 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
1611 return (sizeof(*id));
1613 case CTL_PORT_ISCSI:
1615 struct scsi_transportid_iscsi_port *id =
1616 (struct scsi_transportid_iscsi_port *)buf;
1617 if (port->wwpn_iid[iid].name == NULL)
1620 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
1622 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
1623 len = roundup2(min(len, 252), 4);
1624 scsi_ulto2b(len, id->additional_length);
1625 return (sizeof(*id) + len);
1629 struct scsi_transportid_sas *id =
1630 (struct scsi_transportid_sas *)buf;
1631 if (port->wwpn_iid[iid].wwpn == 0)
1633 memset(id, 0, sizeof(*id));
1634 id->format_protocol = SCSI_PROTO_SAS;
1635 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
1636 return (sizeof(*id));
1640 struct scsi_transportid_spi *id =
1641 (struct scsi_transportid_spi *)buf;
1642 memset(id, 0, sizeof(*id));
1643 id->format_protocol = SCSI_PROTO_SPI;
1644 scsi_ulto2b(iid, id->scsi_addr);
1645 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
1646 return (sizeof(*id));
1652 ctl_ioctl_lun_enable(void *arg, struct ctl_id targ_id, int lun_id)
1658 ctl_ioctl_lun_disable(void *arg, struct ctl_id targ_id, int lun_id)
1664 * Data movement routine for the CTL ioctl frontend port.
1667 ctl_ioctl_do_datamove(struct ctl_scsiio *ctsio)
1669 struct ctl_sg_entry *ext_sglist, *kern_sglist;
1670 struct ctl_sg_entry ext_entry, kern_entry;
1671 int ext_sglen, ext_sg_entries, kern_sg_entries;
1672 int ext_sg_start, ext_offset;
1673 int len_to_copy, len_copied;
1674 int kern_watermark, ext_watermark;
1675 int ext_sglist_malloced;
1678 ext_sglist_malloced = 0;
1682 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove\n"));
1685 * If this flag is set, fake the data transfer.
1687 if (ctsio->io_hdr.flags & CTL_FLAG_NO_DATAMOVE) {
1688 ctsio->ext_data_filled = ctsio->ext_data_len;
1693 * To simplify things here, if we have a single buffer, stick it in
1694 * a S/G entry and just make it a single entry S/G list.
1696 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
1699 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
1701 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL,
1703 ext_sglist_malloced = 1;
1704 if (copyin(ctsio->ext_data_ptr, ext_sglist,
1706 ctl_set_internal_failure(ctsio,
1711 ext_sg_entries = ctsio->ext_sg_entries;
1713 for (i = 0; i < ext_sg_entries; i++) {
1714 if ((len_seen + ext_sglist[i].len) >=
1715 ctsio->ext_data_filled) {
1717 ext_offset = ctsio->ext_data_filled - len_seen;
1720 len_seen += ext_sglist[i].len;
1723 ext_sglist = &ext_entry;
1724 ext_sglist->addr = ctsio->ext_data_ptr;
1725 ext_sglist->len = ctsio->ext_data_len;
1728 ext_offset = ctsio->ext_data_filled;
1731 if (ctsio->kern_sg_entries > 0) {
1732 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
1733 kern_sg_entries = ctsio->kern_sg_entries;
1735 kern_sglist = &kern_entry;
1736 kern_sglist->addr = ctsio->kern_data_ptr;
1737 kern_sglist->len = ctsio->kern_data_len;
1738 kern_sg_entries = 1;
1743 ext_watermark = ext_offset;
1745 for (i = ext_sg_start, j = 0;
1746 i < ext_sg_entries && j < kern_sg_entries;) {
1747 uint8_t *ext_ptr, *kern_ptr;
1749 len_to_copy = MIN(ext_sglist[i].len - ext_watermark,
1750 kern_sglist[j].len - kern_watermark);
1752 ext_ptr = (uint8_t *)ext_sglist[i].addr;
1753 ext_ptr = ext_ptr + ext_watermark;
1754 if (ctsio->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
1758 panic("need to implement bus address support");
1760 kern_ptr = bus_to_virt(kern_sglist[j].addr);
1763 kern_ptr = (uint8_t *)kern_sglist[j].addr;
1764 kern_ptr = kern_ptr + kern_watermark;
1766 kern_watermark += len_to_copy;
1767 ext_watermark += len_to_copy;
1769 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
1771 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1772 "bytes to user\n", len_to_copy));
1773 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1774 "to %p\n", kern_ptr, ext_ptr));
1775 if (copyout(kern_ptr, ext_ptr, len_to_copy) != 0) {
1776 ctl_set_internal_failure(ctsio,
1782 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: copying %d "
1783 "bytes from user\n", len_to_copy));
1784 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: from %p "
1785 "to %p\n", ext_ptr, kern_ptr));
1786 if (copyin(ext_ptr, kern_ptr, len_to_copy)!= 0){
1787 ctl_set_internal_failure(ctsio,
1794 len_copied += len_to_copy;
1796 if (ext_sglist[i].len == ext_watermark) {
1801 if (kern_sglist[j].len == kern_watermark) {
1807 ctsio->ext_data_filled += len_copied;
1809 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_sg_entries: %d, "
1810 "kern_sg_entries: %d\n", ext_sg_entries,
1812 CTL_DEBUG_PRINT(("ctl_ioctl_do_datamove: ext_data_len = %d, "
1813 "kern_data_len = %d\n", ctsio->ext_data_len,
1814 ctsio->kern_data_len));
1817 /* XXX KDM set residual?? */
1820 if (ext_sglist_malloced != 0)
1821 free(ext_sglist, M_CTL);
1823 return (CTL_RETVAL_COMPLETE);
1827 * Serialize a command that went down the "wrong" side, and so was sent to
1828 * this controller for execution. The logic is a little different than the
1829 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
1830 * sent back to the other side, but in the success case, we execute the
1831 * command on this side (XFER mode) or tell the other side to execute it
1835 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
1837 struct ctl_softc *softc;
1838 union ctl_ha_msg msg_info;
1839 struct ctl_lun *lun;
1843 softc = control_softc;
1845 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
1846 lun = softc->ctl_luns[targ_lun];
1850 * Why isn't LUN defined? The other side wouldn't
1851 * send a cmd if the LUN is undefined.
1853 printf("%s: Bad JUJU!, LUN is NULL!\n", __func__);
1855 /* "Logical unit not supported" */
1856 ctl_set_sense_data(&msg_info.scsi.sense_data,
1858 /*sense_format*/SSD_TYPE_NONE,
1859 /*current_error*/ 1,
1860 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1865 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1866 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1867 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1868 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1869 msg_info.hdr.serializing_sc = NULL;
1870 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1871 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1872 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1878 mtx_lock(&lun->lun_lock);
1879 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1881 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
1882 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
1884 case CTL_ACTION_BLOCK:
1885 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
1886 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
1889 case CTL_ACTION_PASS:
1890 case CTL_ACTION_SKIP:
1891 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1892 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
1893 ctl_enqueue_rtr((union ctl_io *)ctsio);
1896 /* send msg back to other side */
1897 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1898 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
1899 msg_info.hdr.msg_type = CTL_MSG_R2R;
1901 printf("2. pOrig %x\n", (int)msg_info.hdr.original_sc);
1903 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1904 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1908 case CTL_ACTION_OVERLAP:
1909 /* OVERLAPPED COMMANDS ATTEMPTED */
1910 ctl_set_sense_data(&msg_info.scsi.sense_data,
1912 /*sense_format*/SSD_TYPE_NONE,
1913 /*current_error*/ 1,
1914 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1919 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1920 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1921 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1922 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1923 msg_info.hdr.serializing_sc = NULL;
1924 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1926 printf("BAD JUJU:Major Bummer Overlap\n");
1928 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1930 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1931 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1934 case CTL_ACTION_OVERLAP_TAG:
1935 /* TAGGED OVERLAPPED COMMANDS (NN = QUEUE TAG) */
1936 ctl_set_sense_data(&msg_info.scsi.sense_data,
1938 /*sense_format*/SSD_TYPE_NONE,
1939 /*current_error*/ 1,
1940 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
1942 /*ascq*/ ctsio->tag_num & 0xff,
1945 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1946 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1947 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1948 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1949 msg_info.hdr.serializing_sc = NULL;
1950 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1952 printf("BAD JUJU:Major Bummer Overlap Tag\n");
1954 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1956 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1957 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1960 case CTL_ACTION_ERROR:
1962 /* "Internal target failure" */
1963 ctl_set_sense_data(&msg_info.scsi.sense_data,
1965 /*sense_format*/SSD_TYPE_NONE,
1966 /*current_error*/ 1,
1967 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
1972 msg_info.scsi.sense_len = SSD_FULL_SIZE;
1973 msg_info.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
1974 msg_info.hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
1975 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
1976 msg_info.hdr.serializing_sc = NULL;
1977 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
1979 printf("BAD JUJU:Major Bummer HW Error\n");
1981 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
1983 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
1984 sizeof(msg_info), 0 ) > CTL_HA_STATUS_SUCCESS) {
1988 mtx_unlock(&lun->lun_lock);
1993 ctl_ioctl_submit_wait(union ctl_io *io)
1995 struct ctl_fe_ioctl_params params;
1996 ctl_fe_ioctl_state last_state;
2001 bzero(¶ms, sizeof(params));
2003 mtx_init(¶ms.ioctl_mtx, "ctliocmtx", NULL, MTX_DEF);
2004 cv_init(¶ms.sem, "ctlioccv");
2005 params.state = CTL_IOCTL_INPROG;
2006 last_state = params.state;
2008 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = ¶ms;
2010 CTL_DEBUG_PRINT(("ctl_ioctl_submit_wait\n"));
2012 /* This shouldn't happen */
2013 if ((retval = ctl_queue(io)) != CTL_RETVAL_COMPLETE)
2019 mtx_lock(¶ms.ioctl_mtx);
2021 * Check the state here, and don't sleep if the state has
2022 * already changed (i.e. wakeup has already occured, but we
2023 * weren't waiting yet).
2025 if (params.state == last_state) {
2026 /* XXX KDM cv_wait_sig instead? */
2027 cv_wait(¶ms.sem, ¶ms.ioctl_mtx);
2029 last_state = params.state;
2031 switch (params.state) {
2032 case CTL_IOCTL_INPROG:
2033 /* Why did we wake up? */
2034 /* XXX KDM error here? */
2035 mtx_unlock(¶ms.ioctl_mtx);
2037 case CTL_IOCTL_DATAMOVE:
2038 CTL_DEBUG_PRINT(("got CTL_IOCTL_DATAMOVE\n"));
2041 * change last_state back to INPROG to avoid
2042 * deadlock on subsequent data moves.
2044 params.state = last_state = CTL_IOCTL_INPROG;
2046 mtx_unlock(¶ms.ioctl_mtx);
2047 ctl_ioctl_do_datamove(&io->scsiio);
2049 * Note that in some cases, most notably writes,
2050 * this will queue the I/O and call us back later.
2051 * In other cases, generally reads, this routine
2052 * will immediately call back and wake us up,
2053 * probably using our own context.
2055 io->scsiio.be_move_done(io);
2057 case CTL_IOCTL_DONE:
2058 mtx_unlock(¶ms.ioctl_mtx);
2059 CTL_DEBUG_PRINT(("got CTL_IOCTL_DONE\n"));
2063 mtx_unlock(¶ms.ioctl_mtx);
2064 /* XXX KDM error here? */
2067 } while (done == 0);
2069 mtx_destroy(¶ms.ioctl_mtx);
2070 cv_destroy(¶ms.sem);
2072 return (CTL_RETVAL_COMPLETE);
2076 ctl_ioctl_datamove(union ctl_io *io)
2078 struct ctl_fe_ioctl_params *params;
2080 params = (struct ctl_fe_ioctl_params *)
2081 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2083 mtx_lock(¶ms->ioctl_mtx);
2084 params->state = CTL_IOCTL_DATAMOVE;
2085 cv_broadcast(¶ms->sem);
2086 mtx_unlock(¶ms->ioctl_mtx);
2090 ctl_ioctl_done(union ctl_io *io)
2092 struct ctl_fe_ioctl_params *params;
2094 params = (struct ctl_fe_ioctl_params *)
2095 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
2097 mtx_lock(¶ms->ioctl_mtx);
2098 params->state = CTL_IOCTL_DONE;
2099 cv_broadcast(¶ms->sem);
2100 mtx_unlock(¶ms->ioctl_mtx);
2104 ctl_ioctl_hard_startstop_callback(void *arg, struct cfi_metatask *metatask)
2106 struct ctl_fe_ioctl_startstop_info *sd_info;
2108 sd_info = (struct ctl_fe_ioctl_startstop_info *)arg;
2110 sd_info->hs_info.status = metatask->status;
2111 sd_info->hs_info.total_luns = metatask->taskinfo.startstop.total_luns;
2112 sd_info->hs_info.luns_complete =
2113 metatask->taskinfo.startstop.luns_complete;
2114 sd_info->hs_info.luns_failed = metatask->taskinfo.startstop.luns_failed;
2116 cv_broadcast(&sd_info->sem);
2120 ctl_ioctl_bbrread_callback(void *arg, struct cfi_metatask *metatask)
2122 struct ctl_fe_ioctl_bbrread_info *fe_bbr_info;
2124 fe_bbr_info = (struct ctl_fe_ioctl_bbrread_info *)arg;
2126 mtx_lock(fe_bbr_info->lock);
2127 fe_bbr_info->bbr_info->status = metatask->status;
2128 fe_bbr_info->bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2129 fe_bbr_info->wakeup_done = 1;
2130 mtx_unlock(fe_bbr_info->lock);
2132 cv_broadcast(&fe_bbr_info->sem);
2136 * Returns 0 for success, errno for failure.
2139 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2140 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2147 mtx_lock(&lun->lun_lock);
2148 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2149 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2151 struct ctl_ooa_entry *entry;
2154 * If we've got more than we can fit, just count the
2155 * remaining entries.
2157 if (*cur_fill_num >= ooa_hdr->alloc_num)
2160 entry = &kern_entries[*cur_fill_num];
2162 entry->tag_num = io->scsiio.tag_num;
2163 entry->lun_num = lun->lun;
2165 entry->start_bt = io->io_hdr.start_bt;
2167 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2168 entry->cdb_len = io->scsiio.cdb_len;
2169 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2170 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2172 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2173 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2175 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2176 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2178 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2179 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2181 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2182 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2184 mtx_unlock(&lun->lun_lock);
2190 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2191 size_t error_str_len)
2195 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2197 if (copyin(user_addr, kptr, len) != 0) {
2198 snprintf(error_str, error_str_len, "Error copying %d bytes "
2199 "from user address %p to kernel address %p", len,
2209 ctl_free_args(int num_args, struct ctl_be_arg *args)
2216 for (i = 0; i < num_args; i++) {
2217 free(args[i].kname, M_CTL);
2218 free(args[i].kvalue, M_CTL);
2224 static struct ctl_be_arg *
2225 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2226 char *error_str, size_t error_str_len)
2228 struct ctl_be_arg *args;
2231 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2232 error_str, error_str_len);
2237 for (i = 0; i < num_args; i++) {
2238 args[i].kname = NULL;
2239 args[i].kvalue = NULL;
2242 for (i = 0; i < num_args; i++) {
2245 args[i].kname = ctl_copyin_alloc(args[i].name,
2246 args[i].namelen, error_str, error_str_len);
2247 if (args[i].kname == NULL)
2250 if (args[i].kname[args[i].namelen - 1] != '\0') {
2251 snprintf(error_str, error_str_len, "Argument %d "
2252 "name is not NUL-terminated", i);
2256 if (args[i].flags & CTL_BEARG_RD) {
2257 tmpptr = ctl_copyin_alloc(args[i].value,
2258 args[i].vallen, error_str, error_str_len);
2261 if ((args[i].flags & CTL_BEARG_ASCII)
2262 && (tmpptr[args[i].vallen - 1] != '\0')) {
2263 snprintf(error_str, error_str_len, "Argument "
2264 "%d value is not NUL-terminated", i);
2267 args[i].kvalue = tmpptr;
2269 args[i].kvalue = malloc(args[i].vallen,
2270 M_CTL, M_WAITOK | M_ZERO);
2277 ctl_free_args(num_args, args);
2283 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2287 for (i = 0; i < num_args; i++) {
2288 if (args[i].flags & CTL_BEARG_WR)
2289 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2294 * Escape characters that are illegal or not recommended in XML.
2297 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2299 char *end = str + size;
2304 for (; *str && str < end; str++) {
2307 retval = sbuf_printf(sb, "&");
2310 retval = sbuf_printf(sb, ">");
2313 retval = sbuf_printf(sb, "<");
2316 retval = sbuf_putc(sb, *str);
2329 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2331 struct scsi_vpd_id_descriptor *desc;
2334 if (id == NULL || id->len < 4)
2336 desc = (struct scsi_vpd_id_descriptor *)id->data;
2337 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2338 case SVPD_ID_TYPE_T10:
2339 sbuf_printf(sb, "t10.");
2341 case SVPD_ID_TYPE_EUI64:
2342 sbuf_printf(sb, "eui.");
2344 case SVPD_ID_TYPE_NAA:
2345 sbuf_printf(sb, "naa.");
2347 case SVPD_ID_TYPE_SCSI_NAME:
2350 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2351 case SVPD_ID_CODESET_BINARY:
2352 for (i = 0; i < desc->length; i++)
2353 sbuf_printf(sb, "%02x", desc->identifier[i]);
2355 case SVPD_ID_CODESET_ASCII:
2356 sbuf_printf(sb, "%.*s", (int)desc->length,
2357 (char *)desc->identifier);
2359 case SVPD_ID_CODESET_UTF8:
2360 sbuf_printf(sb, "%s", (char *)desc->identifier);
2366 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2369 struct ctl_softc *softc;
2372 softc = control_softc;
2382 * If we haven't been "enabled", don't allow any SCSI I/O
2385 if ((softc->ioctl_info.flags & CTL_IOCTL_FLAG_ENABLED) == 0) {
2390 io = ctl_alloc_io(softc->ioctl_info.port.ctl_pool_ref);
2393 * Need to save the pool reference so it doesn't get
2394 * spammed by the user's ctl_io.
2396 pool_tmp = io->io_hdr.pool;
2397 memcpy(io, (void *)addr, sizeof(*io));
2398 io->io_hdr.pool = pool_tmp;
2401 * No status yet, so make sure the status is set properly.
2403 io->io_hdr.status = CTL_STATUS_NONE;
2406 * The user sets the initiator ID, target and LUN IDs.
2408 io->io_hdr.nexus.targ_port = softc->ioctl_info.port.targ_port;
2409 io->io_hdr.flags |= CTL_FLAG_USER_REQ;
2410 if ((io->io_hdr.io_type == CTL_IO_SCSI)
2411 && (io->scsiio.tag_type != CTL_TAG_UNTAGGED))
2412 io->scsiio.tag_num = softc->ioctl_info.cur_tag_num++;
2414 retval = ctl_ioctl_submit_wait(io);
2421 memcpy((void *)addr, io, sizeof(*io));
2423 /* return this to our pool */
2428 case CTL_ENABLE_PORT:
2429 case CTL_DISABLE_PORT:
2430 case CTL_SET_PORT_WWNS: {
2431 struct ctl_port *port;
2432 struct ctl_port_entry *entry;
2434 entry = (struct ctl_port_entry *)addr;
2436 mtx_lock(&softc->ctl_lock);
2437 STAILQ_FOREACH(port, &softc->port_list, links) {
2443 if ((entry->port_type == CTL_PORT_NONE)
2444 && (entry->targ_port == port->targ_port)) {
2446 * If the user only wants to enable or
2447 * disable or set WWNs on a specific port,
2448 * do the operation and we're done.
2452 } else if (entry->port_type & port->port_type) {
2454 * Compare the user's type mask with the
2455 * particular frontend type to see if we
2462 * Make sure the user isn't trying to set
2463 * WWNs on multiple ports at the same time.
2465 if (cmd == CTL_SET_PORT_WWNS) {
2466 printf("%s: Can't set WWNs on "
2467 "multiple ports\n", __func__);
2474 * XXX KDM we have to drop the lock here,
2475 * because the online/offline operations
2476 * can potentially block. We need to
2477 * reference count the frontends so they
2480 mtx_unlock(&softc->ctl_lock);
2482 if (cmd == CTL_ENABLE_PORT) {
2483 struct ctl_lun *lun;
2485 STAILQ_FOREACH(lun, &softc->lun_list,
2487 port->lun_enable(port->targ_lun_arg,
2492 ctl_port_online(port);
2493 } else if (cmd == CTL_DISABLE_PORT) {
2494 struct ctl_lun *lun;
2496 ctl_port_offline(port);
2498 STAILQ_FOREACH(lun, &softc->lun_list,
2507 mtx_lock(&softc->ctl_lock);
2509 if (cmd == CTL_SET_PORT_WWNS)
2510 ctl_port_set_wwns(port,
2511 (entry->flags & CTL_PORT_WWNN_VALID) ?
2513 (entry->flags & CTL_PORT_WWPN_VALID) ?
2514 1 : 0, entry->wwpn);
2519 mtx_unlock(&softc->ctl_lock);
2522 case CTL_GET_PORT_LIST: {
2523 struct ctl_port *port;
2524 struct ctl_port_list *list;
2527 list = (struct ctl_port_list *)addr;
2529 if (list->alloc_len != (list->alloc_num *
2530 sizeof(struct ctl_port_entry))) {
2531 printf("%s: CTL_GET_PORT_LIST: alloc_len %u != "
2532 "alloc_num %u * sizeof(struct ctl_port_entry) "
2533 "%zu\n", __func__, list->alloc_len,
2534 list->alloc_num, sizeof(struct ctl_port_entry));
2540 list->dropped_num = 0;
2542 mtx_lock(&softc->ctl_lock);
2543 STAILQ_FOREACH(port, &softc->port_list, links) {
2544 struct ctl_port_entry entry, *list_entry;
2546 if (list->fill_num >= list->alloc_num) {
2547 list->dropped_num++;
2551 entry.port_type = port->port_type;
2552 strlcpy(entry.port_name, port->port_name,
2553 sizeof(entry.port_name));
2554 entry.targ_port = port->targ_port;
2555 entry.physical_port = port->physical_port;
2556 entry.virtual_port = port->virtual_port;
2557 entry.wwnn = port->wwnn;
2558 entry.wwpn = port->wwpn;
2559 if (port->status & CTL_PORT_STATUS_ONLINE)
2564 list_entry = &list->entries[i];
2566 retval = copyout(&entry, list_entry, sizeof(entry));
2568 printf("%s: CTL_GET_PORT_LIST: copyout "
2569 "returned %d\n", __func__, retval);
2574 list->fill_len += sizeof(entry);
2576 mtx_unlock(&softc->ctl_lock);
2579 * If this is non-zero, we had a copyout fault, so there's
2580 * probably no point in attempting to set the status inside
2586 if (list->dropped_num > 0)
2587 list->status = CTL_PORT_LIST_NEED_MORE_SPACE;
2589 list->status = CTL_PORT_LIST_OK;
2592 case CTL_DUMP_OOA: {
2593 struct ctl_lun *lun;
2598 mtx_lock(&softc->ctl_lock);
2599 printf("Dumping OOA queues:\n");
2600 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2601 mtx_lock(&lun->lun_lock);
2602 for (io = (union ctl_io *)TAILQ_FIRST(
2603 &lun->ooa_queue); io != NULL;
2604 io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2606 sbuf_new(&sb, printbuf, sizeof(printbuf),
2608 sbuf_printf(&sb, "LUN %jd tag 0x%04x%s%s%s%s: ",
2612 CTL_FLAG_BLOCKED) ? "" : " BLOCKED",
2614 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
2616 CTL_FLAG_ABORT) ? " ABORT" : "",
2618 CTL_FLAG_IS_WAS_ON_RTR) ? " RTR" : "");
2619 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
2621 printf("%s\n", sbuf_data(&sb));
2623 mtx_unlock(&lun->lun_lock);
2625 printf("OOA queues dump done\n");
2626 mtx_unlock(&softc->ctl_lock);
2630 struct ctl_lun *lun;
2631 struct ctl_ooa *ooa_hdr;
2632 struct ctl_ooa_entry *entries;
2633 uint32_t cur_fill_num;
2635 ooa_hdr = (struct ctl_ooa *)addr;
2637 if ((ooa_hdr->alloc_len == 0)
2638 || (ooa_hdr->alloc_num == 0)) {
2639 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2640 "must be non-zero\n", __func__,
2641 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2646 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2647 sizeof(struct ctl_ooa_entry))) {
2648 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2649 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2650 __func__, ooa_hdr->alloc_len,
2651 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2656 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2657 if (entries == NULL) {
2658 printf("%s: could not allocate %d bytes for OOA "
2659 "dump\n", __func__, ooa_hdr->alloc_len);
2664 mtx_lock(&softc->ctl_lock);
2665 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2666 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2667 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2668 mtx_unlock(&softc->ctl_lock);
2669 free(entries, M_CTL);
2670 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2671 __func__, (uintmax_t)ooa_hdr->lun_num);
2678 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2679 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2680 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2686 mtx_unlock(&softc->ctl_lock);
2687 free(entries, M_CTL);
2691 lun = softc->ctl_luns[ooa_hdr->lun_num];
2693 retval = ctl_ioctl_fill_ooa(lun, &cur_fill_num,ooa_hdr,
2696 mtx_unlock(&softc->ctl_lock);
2698 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2699 ooa_hdr->fill_len = ooa_hdr->fill_num *
2700 sizeof(struct ctl_ooa_entry);
2701 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2703 printf("%s: error copying out %d bytes for OOA dump\n",
2704 __func__, ooa_hdr->fill_len);
2707 getbintime(&ooa_hdr->cur_bt);
2709 if (cur_fill_num > ooa_hdr->alloc_num) {
2710 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2711 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2713 ooa_hdr->dropped_num = 0;
2714 ooa_hdr->status = CTL_OOA_OK;
2717 free(entries, M_CTL);
2720 case CTL_CHECK_OOA: {
2722 struct ctl_lun *lun;
2723 struct ctl_ooa_info *ooa_info;
2726 ooa_info = (struct ctl_ooa_info *)addr;
2728 if (ooa_info->lun_id >= CTL_MAX_LUNS) {
2729 ooa_info->status = CTL_OOA_INVALID_LUN;
2732 mtx_lock(&softc->ctl_lock);
2733 lun = softc->ctl_luns[ooa_info->lun_id];
2735 mtx_unlock(&softc->ctl_lock);
2736 ooa_info->status = CTL_OOA_INVALID_LUN;
2739 mtx_lock(&lun->lun_lock);
2740 mtx_unlock(&softc->ctl_lock);
2741 ooa_info->num_entries = 0;
2742 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
2743 io != NULL; io = (union ctl_io *)TAILQ_NEXT(
2744 &io->io_hdr, ooa_links)) {
2745 ooa_info->num_entries++;
2747 mtx_unlock(&lun->lun_lock);
2749 ooa_info->status = CTL_OOA_SUCCESS;
2753 case CTL_HARD_START:
2754 case CTL_HARD_STOP: {
2755 struct ctl_fe_ioctl_startstop_info ss_info;
2756 struct cfi_metatask *metatask;
2759 mtx_init(&hs_mtx, "HS Mutex", NULL, MTX_DEF);
2761 cv_init(&ss_info.sem, "hard start/stop cv" );
2763 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2764 if (metatask == NULL) {
2766 mtx_destroy(&hs_mtx);
2770 if (cmd == CTL_HARD_START)
2771 metatask->tasktype = CFI_TASK_STARTUP;
2773 metatask->tasktype = CFI_TASK_SHUTDOWN;
2775 metatask->callback = ctl_ioctl_hard_startstop_callback;
2776 metatask->callback_arg = &ss_info;
2778 cfi_action(metatask);
2780 /* Wait for the callback */
2782 cv_wait_sig(&ss_info.sem, &hs_mtx);
2783 mtx_unlock(&hs_mtx);
2786 * All information has been copied from the metatask by the
2787 * time cv_broadcast() is called, so we free the metatask here.
2789 cfi_free_metatask(metatask);
2791 memcpy((void *)addr, &ss_info.hs_info, sizeof(ss_info.hs_info));
2793 mtx_destroy(&hs_mtx);
2797 struct ctl_bbrread_info *bbr_info;
2798 struct ctl_fe_ioctl_bbrread_info fe_bbr_info;
2800 struct cfi_metatask *metatask;
2802 bbr_info = (struct ctl_bbrread_info *)addr;
2804 bzero(&fe_bbr_info, sizeof(fe_bbr_info));
2806 bzero(&bbr_mtx, sizeof(bbr_mtx));
2807 mtx_init(&bbr_mtx, "BBR Mutex", NULL, MTX_DEF);
2809 fe_bbr_info.bbr_info = bbr_info;
2810 fe_bbr_info.lock = &bbr_mtx;
2812 cv_init(&fe_bbr_info.sem, "BBR read cv");
2813 metatask = cfi_alloc_metatask(/*can_wait*/ 1);
2815 if (metatask == NULL) {
2816 mtx_destroy(&bbr_mtx);
2817 cv_destroy(&fe_bbr_info.sem);
2821 metatask->tasktype = CFI_TASK_BBRREAD;
2822 metatask->callback = ctl_ioctl_bbrread_callback;
2823 metatask->callback_arg = &fe_bbr_info;
2824 metatask->taskinfo.bbrread.lun_num = bbr_info->lun_num;
2825 metatask->taskinfo.bbrread.lba = bbr_info->lba;
2826 metatask->taskinfo.bbrread.len = bbr_info->len;
2828 cfi_action(metatask);
2831 while (fe_bbr_info.wakeup_done == 0)
2832 cv_wait_sig(&fe_bbr_info.sem, &bbr_mtx);
2833 mtx_unlock(&bbr_mtx);
2835 bbr_info->status = metatask->status;
2836 bbr_info->bbr_status = metatask->taskinfo.bbrread.status;
2837 bbr_info->scsi_status = metatask->taskinfo.bbrread.scsi_status;
2838 memcpy(&bbr_info->sense_data,
2839 &metatask->taskinfo.bbrread.sense_data,
2840 MIN(sizeof(bbr_info->sense_data),
2841 sizeof(metatask->taskinfo.bbrread.sense_data)));
2843 cfi_free_metatask(metatask);
2845 mtx_destroy(&bbr_mtx);
2846 cv_destroy(&fe_bbr_info.sem);
2850 case CTL_DELAY_IO: {
2851 struct ctl_io_delay_info *delay_info;
2853 struct ctl_lun *lun;
2854 #endif /* CTL_IO_DELAY */
2856 delay_info = (struct ctl_io_delay_info *)addr;
2859 mtx_lock(&softc->ctl_lock);
2861 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2862 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2863 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2865 lun = softc->ctl_luns[delay_info->lun_id];
2866 mtx_lock(&lun->lun_lock);
2868 delay_info->status = CTL_DELAY_STATUS_OK;
2870 switch (delay_info->delay_type) {
2871 case CTL_DELAY_TYPE_CONT:
2873 case CTL_DELAY_TYPE_ONESHOT:
2876 delay_info->status =
2877 CTL_DELAY_STATUS_INVALID_TYPE;
2881 switch (delay_info->delay_loc) {
2882 case CTL_DELAY_LOC_DATAMOVE:
2883 lun->delay_info.datamove_type =
2884 delay_info->delay_type;
2885 lun->delay_info.datamove_delay =
2886 delay_info->delay_secs;
2888 case CTL_DELAY_LOC_DONE:
2889 lun->delay_info.done_type =
2890 delay_info->delay_type;
2891 lun->delay_info.done_delay =
2892 delay_info->delay_secs;
2895 delay_info->status =
2896 CTL_DELAY_STATUS_INVALID_LOC;
2899 mtx_unlock(&lun->lun_lock);
2902 mtx_unlock(&softc->ctl_lock);
2904 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2905 #endif /* CTL_IO_DELAY */
2908 case CTL_REALSYNC_SET: {
2911 syncstate = (int *)addr;
2913 mtx_lock(&softc->ctl_lock);
2914 switch (*syncstate) {
2916 softc->flags &= ~CTL_FLAG_REAL_SYNC;
2919 softc->flags |= CTL_FLAG_REAL_SYNC;
2925 mtx_unlock(&softc->ctl_lock);
2928 case CTL_REALSYNC_GET: {
2931 syncstate = (int*)addr;
2933 mtx_lock(&softc->ctl_lock);
2934 if (softc->flags & CTL_FLAG_REAL_SYNC)
2938 mtx_unlock(&softc->ctl_lock);
2944 struct ctl_sync_info *sync_info;
2945 struct ctl_lun *lun;
2947 sync_info = (struct ctl_sync_info *)addr;
2949 mtx_lock(&softc->ctl_lock);
2950 lun = softc->ctl_luns[sync_info->lun_id];
2952 mtx_unlock(&softc->ctl_lock);
2953 sync_info->status = CTL_GS_SYNC_NO_LUN;
2956 * Get or set the sync interval. We're not bounds checking
2957 * in the set case, hopefully the user won't do something
2960 mtx_lock(&lun->lun_lock);
2961 mtx_unlock(&softc->ctl_lock);
2962 if (cmd == CTL_GETSYNC)
2963 sync_info->sync_interval = lun->sync_interval;
2965 lun->sync_interval = sync_info->sync_interval;
2966 mtx_unlock(&lun->lun_lock);
2968 sync_info->status = CTL_GS_SYNC_OK;
2972 case CTL_GETSTATS: {
2973 struct ctl_stats *stats;
2974 struct ctl_lun *lun;
2977 stats = (struct ctl_stats *)addr;
2979 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2981 stats->status = CTL_SS_NEED_MORE_SPACE;
2982 stats->num_luns = softc->num_luns;
2986 * XXX KDM no locking here. If the LUN list changes,
2987 * things can blow up.
2989 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2990 i++, lun = STAILQ_NEXT(lun, links)) {
2991 retval = copyout(&lun->stats, &stats->lun_stats[i],
2992 sizeof(lun->stats));
2996 stats->num_luns = softc->num_luns;
2997 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2999 stats->status = CTL_SS_OK;
3001 stats->flags = CTL_STATS_FLAG_TIME_VALID;
3003 stats->flags = CTL_STATS_FLAG_NONE;
3005 getnanouptime(&stats->timestamp);
3008 case CTL_ERROR_INJECT: {
3009 struct ctl_error_desc *err_desc, *new_err_desc;
3010 struct ctl_lun *lun;
3012 err_desc = (struct ctl_error_desc *)addr;
3014 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
3016 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
3018 mtx_lock(&softc->ctl_lock);
3019 lun = softc->ctl_luns[err_desc->lun_id];
3021 mtx_unlock(&softc->ctl_lock);
3022 free(new_err_desc, M_CTL);
3023 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
3024 __func__, (uintmax_t)err_desc->lun_id);
3028 mtx_lock(&lun->lun_lock);
3029 mtx_unlock(&softc->ctl_lock);
3032 * We could do some checking here to verify the validity
3033 * of the request, but given the complexity of error
3034 * injection requests, the checking logic would be fairly
3037 * For now, if the request is invalid, it just won't get
3038 * executed and might get deleted.
3040 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
3043 * XXX KDM check to make sure the serial number is unique,
3044 * in case we somehow manage to wrap. That shouldn't
3045 * happen for a very long time, but it's the right thing to
3048 new_err_desc->serial = lun->error_serial;
3049 err_desc->serial = lun->error_serial;
3050 lun->error_serial++;
3052 mtx_unlock(&lun->lun_lock);
3055 case CTL_ERROR_INJECT_DELETE: {
3056 struct ctl_error_desc *delete_desc, *desc, *desc2;
3057 struct ctl_lun *lun;
3060 delete_desc = (struct ctl_error_desc *)addr;
3063 mtx_lock(&softc->ctl_lock);
3064 lun = softc->ctl_luns[delete_desc->lun_id];
3066 mtx_unlock(&softc->ctl_lock);
3067 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
3068 __func__, (uintmax_t)delete_desc->lun_id);
3072 mtx_lock(&lun->lun_lock);
3073 mtx_unlock(&softc->ctl_lock);
3074 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
3075 if (desc->serial != delete_desc->serial)
3078 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
3083 mtx_unlock(&lun->lun_lock);
3084 if (delete_done == 0) {
3085 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
3086 "error serial %ju on LUN %u\n", __func__,
3087 delete_desc->serial, delete_desc->lun_id);
3093 case CTL_DUMP_STRUCTS: {
3095 struct ctl_port *port;
3096 struct ctl_frontend *fe;
3098 mtx_lock(&softc->ctl_lock);
3099 printf("CTL Persistent Reservation information start:\n");
3100 for (i = 0; i < CTL_MAX_LUNS; i++) {
3101 struct ctl_lun *lun;
3103 lun = softc->ctl_luns[i];
3106 || ((lun->flags & CTL_LUN_DISABLED) != 0))
3109 for (j = 0; j < (CTL_MAX_PORTS * 2); j++) {
3110 if (lun->pr_keys[j] == NULL)
3112 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
3113 if (lun->pr_keys[j][k] == 0)
3115 printf(" LUN %d port %d iid %d key "
3117 (uintmax_t)lun->pr_keys[j][k]);
3121 printf("CTL Persistent Reservation information end\n");
3122 printf("CTL Ports:\n");
3123 STAILQ_FOREACH(port, &softc->port_list, links) {
3124 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3125 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3126 port->frontend->name, port->port_type,
3127 port->physical_port, port->virtual_port,
3128 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3129 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3130 if (port->wwpn_iid[j].in_use == 0 &&
3131 port->wwpn_iid[j].wwpn == 0 &&
3132 port->wwpn_iid[j].name == NULL)
3135 printf(" iid %u use %d WWPN %#jx '%s'\n",
3136 j, port->wwpn_iid[j].in_use,
3137 (uintmax_t)port->wwpn_iid[j].wwpn,
3138 port->wwpn_iid[j].name);
3141 printf("CTL Port information end\n");
3142 mtx_unlock(&softc->ctl_lock);
3144 * XXX KDM calling this without a lock. We'd likely want
3145 * to drop the lock before calling the frontend's dump
3148 printf("CTL Frontends:\n");
3149 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3150 printf(" Frontend '%s'\n", fe->name);
3151 if (fe->fe_dump != NULL)
3154 printf("CTL Frontend information end\n");
3158 struct ctl_lun_req *lun_req;
3159 struct ctl_backend_driver *backend;
3161 lun_req = (struct ctl_lun_req *)addr;
3163 backend = ctl_backend_find(lun_req->backend);
3164 if (backend == NULL) {
3165 lun_req->status = CTL_LUN_ERROR;
3166 snprintf(lun_req->error_str,
3167 sizeof(lun_req->error_str),
3168 "Backend \"%s\" not found.",
3172 if (lun_req->num_be_args > 0) {
3173 lun_req->kern_be_args = ctl_copyin_args(
3174 lun_req->num_be_args,
3177 sizeof(lun_req->error_str));
3178 if (lun_req->kern_be_args == NULL) {
3179 lun_req->status = CTL_LUN_ERROR;
3184 retval = backend->ioctl(dev, cmd, addr, flag, td);
3186 if (lun_req->num_be_args > 0) {
3187 ctl_copyout_args(lun_req->num_be_args,
3188 lun_req->kern_be_args);
3189 ctl_free_args(lun_req->num_be_args,
3190 lun_req->kern_be_args);
3194 case CTL_LUN_LIST: {
3196 struct ctl_lun *lun;
3197 struct ctl_lun_list *list;
3198 struct ctl_option *opt;
3200 list = (struct ctl_lun_list *)addr;
3203 * Allocate a fixed length sbuf here, based on the length
3204 * of the user's buffer. We could allocate an auto-extending
3205 * buffer, and then tell the user how much larger our
3206 * amount of data is than his buffer, but that presents
3209 * 1. The sbuf(9) routines use a blocking malloc, and so
3210 * we can't hold a lock while calling them with an
3211 * auto-extending buffer.
3213 * 2. There is not currently a LUN reference counting
3214 * mechanism, outside of outstanding transactions on
3215 * the LUN's OOA queue. So a LUN could go away on us
3216 * while we're getting the LUN number, backend-specific
3217 * information, etc. Thus, given the way things
3218 * currently work, we need to hold the CTL lock while
3219 * grabbing LUN information.
3221 * So, from the user's standpoint, the best thing to do is
3222 * allocate what he thinks is a reasonable buffer length,
3223 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3224 * double the buffer length and try again. (And repeat
3225 * that until he succeeds.)
3227 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3229 list->status = CTL_LUN_LIST_ERROR;
3230 snprintf(list->error_str, sizeof(list->error_str),
3231 "Unable to allocate %d bytes for LUN list",
3236 sbuf_printf(sb, "<ctllunlist>\n");
3238 mtx_lock(&softc->ctl_lock);
3239 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3240 mtx_lock(&lun->lun_lock);
3241 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3242 (uintmax_t)lun->lun);
3245 * Bail out as soon as we see that we've overfilled
3251 retval = sbuf_printf(sb, "\t<backend_type>%s"
3252 "</backend_type>\n",
3253 (lun->backend == NULL) ? "none" :
3254 lun->backend->name);
3259 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3260 lun->be_lun->lun_type);
3265 if (lun->backend == NULL) {
3266 retval = sbuf_printf(sb, "</lun>\n");
3272 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3273 (lun->be_lun->maxlba > 0) ?
3274 lun->be_lun->maxlba + 1 : 0);
3279 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3280 lun->be_lun->blocksize);
3285 retval = sbuf_printf(sb, "\t<serial_number>");
3290 retval = ctl_sbuf_printf_esc(sb,
3291 lun->be_lun->serial_num,
3292 sizeof(lun->be_lun->serial_num));
3297 retval = sbuf_printf(sb, "</serial_number>\n");
3302 retval = sbuf_printf(sb, "\t<device_id>");
3307 retval = ctl_sbuf_printf_esc(sb,
3308 lun->be_lun->device_id,
3309 sizeof(lun->be_lun->device_id));
3314 retval = sbuf_printf(sb, "</device_id>\n");
3319 if (lun->backend->lun_info != NULL) {
3320 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3324 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3325 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3326 opt->name, opt->value, opt->name);
3331 retval = sbuf_printf(sb, "</lun>\n");
3335 mtx_unlock(&lun->lun_lock);
3338 mtx_unlock(&lun->lun_lock);
3339 mtx_unlock(&softc->ctl_lock);
3342 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3345 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3346 snprintf(list->error_str, sizeof(list->error_str),
3347 "Out of space, %d bytes is too small",
3354 retval = copyout(sbuf_data(sb), list->lun_xml,
3357 list->fill_len = sbuf_len(sb) + 1;
3358 list->status = CTL_LUN_LIST_OK;
3363 struct ctl_iscsi *ci;
3364 struct ctl_frontend *fe;
3366 ci = (struct ctl_iscsi *)addr;
3368 fe = ctl_frontend_find("iscsi");
3370 ci->status = CTL_ISCSI_ERROR;
3371 snprintf(ci->error_str, sizeof(ci->error_str),
3372 "Frontend \"iscsi\" not found.");
3376 retval = fe->ioctl(dev, cmd, addr, flag, td);
3379 case CTL_PORT_REQ: {
3380 struct ctl_req *req;
3381 struct ctl_frontend *fe;
3383 req = (struct ctl_req *)addr;
3385 fe = ctl_frontend_find(req->driver);
3387 req->status = CTL_LUN_ERROR;
3388 snprintf(req->error_str, sizeof(req->error_str),
3389 "Frontend \"%s\" not found.", req->driver);
3392 if (req->num_args > 0) {
3393 req->kern_args = ctl_copyin_args(req->num_args,
3394 req->args, req->error_str, sizeof(req->error_str));
3395 if (req->kern_args == NULL) {
3396 req->status = CTL_LUN_ERROR;
3401 retval = fe->ioctl(dev, cmd, addr, flag, td);
3403 if (req->num_args > 0) {
3404 ctl_copyout_args(req->num_args, req->kern_args);
3405 ctl_free_args(req->num_args, req->kern_args);
3409 case CTL_PORT_LIST: {
3411 struct ctl_port *port;
3412 struct ctl_lun_list *list;
3413 struct ctl_option *opt;
3417 list = (struct ctl_lun_list *)addr;
3419 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3421 list->status = CTL_LUN_LIST_ERROR;
3422 snprintf(list->error_str, sizeof(list->error_str),
3423 "Unable to allocate %d bytes for LUN list",
3428 sbuf_printf(sb, "<ctlportlist>\n");
3430 mtx_lock(&softc->ctl_lock);
3431 STAILQ_FOREACH(port, &softc->port_list, links) {
3432 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3433 (uintmax_t)port->targ_port);
3436 * Bail out as soon as we see that we've overfilled
3442 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3443 "</frontend_type>\n", port->frontend->name);
3447 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3452 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3453 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3457 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3462 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3463 port->physical_port);
3467 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3468 port->virtual_port);
3472 if (port->target_devid != NULL) {
3473 sbuf_printf(sb, "\t<target>");
3474 ctl_id_sbuf(port->target_devid, sb);
3475 sbuf_printf(sb, "</target>\n");
3478 if (port->port_devid != NULL) {
3479 sbuf_printf(sb, "\t<port>");
3480 ctl_id_sbuf(port->port_devid, sb);
3481 sbuf_printf(sb, "</port>\n");
3484 if (port->port_info != NULL) {
3485 retval = port->port_info(port->onoff_arg, sb);
3489 STAILQ_FOREACH(opt, &port->options, links) {
3490 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3491 opt->name, opt->value, opt->name);
3496 if (port->lun_map != NULL) {
3497 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3498 for (j = 0; j < CTL_MAX_LUNS; j++) {
3499 plun = ctl_lun_map_from_port(port, j);
3500 if (plun >= CTL_MAX_LUNS)
3503 "\t<lun id=\"%u\">%u</lun>\n",
3508 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3509 if (port->wwpn_iid[j].in_use == 0 ||
3510 (port->wwpn_iid[j].wwpn == 0 &&
3511 port->wwpn_iid[j].name == NULL))
3514 if (port->wwpn_iid[j].name != NULL)
3515 retval = sbuf_printf(sb,
3516 "\t<initiator id=\"%u\">%s</initiator>\n",
3517 j, port->wwpn_iid[j].name);
3519 retval = sbuf_printf(sb,
3520 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3521 j, port->wwpn_iid[j].wwpn);
3528 retval = sbuf_printf(sb, "</targ_port>\n");
3532 mtx_unlock(&softc->ctl_lock);
3535 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3538 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3539 snprintf(list->error_str, sizeof(list->error_str),
3540 "Out of space, %d bytes is too small",
3547 retval = copyout(sbuf_data(sb), list->lun_xml,
3550 list->fill_len = sbuf_len(sb) + 1;
3551 list->status = CTL_LUN_LIST_OK;
3556 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3557 struct ctl_port *port;
3559 mtx_lock(&softc->ctl_lock);
3560 if (lm->port >= CTL_MAX_PORTS ||
3561 (port = softc->ctl_ports[lm->port]) == NULL) {
3562 mtx_unlock(&softc->ctl_lock);
3565 if (lm->plun < CTL_MAX_LUNS) {
3566 if (lm->lun == UINT32_MAX)
3567 retval = ctl_lun_map_unset(port, lm->plun);
3568 else if (lm->lun < CTL_MAX_LUNS &&
3569 softc->ctl_luns[lm->lun] != NULL)
3570 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3572 mtx_unlock(&softc->ctl_lock);
3575 } else if (lm->plun == UINT32_MAX) {
3576 if (lm->lun == UINT32_MAX)
3577 retval = ctl_lun_map_deinit(port);
3579 retval = ctl_lun_map_init(port);
3581 mtx_unlock(&softc->ctl_lock);
3584 mtx_unlock(&softc->ctl_lock);
3588 /* XXX KDM should we fix this? */
3590 struct ctl_backend_driver *backend;
3597 * We encode the backend type as the ioctl type for backend
3598 * ioctls. So parse it out here, and then search for a
3599 * backend of this type.
3601 type = _IOC_TYPE(cmd);
3603 STAILQ_FOREACH(backend, &softc->be_list, links) {
3604 if (backend->type == type) {
3610 printf("ctl: unknown ioctl command %#lx or backend "
3615 retval = backend->ioctl(dev, cmd, addr, flag, td);
3625 ctl_get_initindex(struct ctl_nexus *nexus)
3627 if (nexus->targ_port < CTL_MAX_PORTS)
3628 return (nexus->initid.id +
3629 (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3631 return (nexus->initid.id +
3632 ((nexus->targ_port - CTL_MAX_PORTS) *
3633 CTL_MAX_INIT_PER_PORT));
3637 ctl_get_resindex(struct ctl_nexus *nexus)
3639 return (nexus->initid.id + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3643 ctl_port_idx(int port_num)
3645 if (port_num < CTL_MAX_PORTS)
3648 return(port_num - CTL_MAX_PORTS);
3652 ctl_lun_map_init(struct ctl_port *port)
3656 if (port->lun_map == NULL)
3657 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3659 if (port->lun_map == NULL)
3661 for (i = 0; i < CTL_MAX_LUNS; i++)
3662 port->lun_map[i] = UINT32_MAX;
3667 ctl_lun_map_deinit(struct ctl_port *port)
3670 if (port->lun_map == NULL)
3672 free(port->lun_map, M_CTL);
3673 port->lun_map = NULL;
3678 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3682 if (port->lun_map == NULL) {
3683 status = ctl_lun_map_init(port);
3687 port->lun_map[plun] = glun;
3692 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3695 if (port->lun_map == NULL)
3697 port->lun_map[plun] = UINT32_MAX;
3702 ctl_lun_map_unsetg(struct ctl_port *port, uint32_t glun)
3706 if (port->lun_map == NULL)
3708 for (i = 0; i < CTL_MAX_LUNS; i++) {
3709 if (port->lun_map[i] == glun)
3710 port->lun_map[i] = UINT32_MAX;
3716 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3720 return (UINT32_MAX);
3721 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3723 return (port->lun_map[lun_id]);
3727 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3732 return (UINT32_MAX);
3733 if (port->lun_map == NULL)
3735 for (i = 0; i < CTL_MAX_LUNS; i++) {
3736 if (port->lun_map[i] == lun_id)
3739 return (UINT32_MAX);
3742 static struct ctl_port *
3743 ctl_io_port(struct ctl_io_hdr *io_hdr)
3747 port_num = io_hdr->nexus.targ_port;
3748 return (control_softc->ctl_ports[ctl_port_idx(port_num)]);
3752 * Note: This only works for bitmask sizes that are at least 32 bits, and
3753 * that are a power of 2.
3756 ctl_ffz(uint32_t *mask, uint32_t size)
3758 uint32_t num_chunks, num_pieces;
3761 num_chunks = (size >> 5);
3762 if (num_chunks == 0)
3764 num_pieces = MIN((sizeof(uint32_t) * 8), size);
3766 for (i = 0; i < num_chunks; i++) {
3767 for (j = 0; j < num_pieces; j++) {
3768 if ((mask[i] & (1 << j)) == 0)
3769 return ((i << 5) + j);
3777 ctl_set_mask(uint32_t *mask, uint32_t bit)
3779 uint32_t chunk, piece;
3782 piece = bit % (sizeof(uint32_t) * 8);
3784 if ((mask[chunk] & (1 << piece)) != 0)
3787 mask[chunk] |= (1 << piece);
3793 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3795 uint32_t chunk, piece;
3798 piece = bit % (sizeof(uint32_t) * 8);
3800 if ((mask[chunk] & (1 << piece)) == 0)
3803 mask[chunk] &= ~(1 << piece);
3809 ctl_is_set(uint32_t *mask, uint32_t bit)
3811 uint32_t chunk, piece;
3814 piece = bit % (sizeof(uint32_t) * 8);
3816 if ((mask[chunk] & (1 << piece)) == 0)
3823 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3827 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3830 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3834 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3838 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3841 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3845 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3850 i = residx/CTL_MAX_INIT_PER_PORT;
3851 if (lun->pr_keys[i] != NULL)
3853 mtx_unlock(&lun->lun_lock);
3854 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3856 mtx_lock(&lun->lun_lock);
3857 if (lun->pr_keys[i] == NULL)
3858 lun->pr_keys[i] = p;
3864 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3868 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3869 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3870 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3874 * ctl_softc, pool_name, total_ctl_io are passed in.
3875 * npool is passed out.
3878 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3879 uint32_t total_ctl_io, void **npool)
3882 struct ctl_io_pool *pool;
3884 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3889 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3890 pool->ctl_softc = ctl_softc;
3891 pool->zone = uma_zsecond_create(pool->name, NULL,
3892 NULL, NULL, NULL, ctl_softc->io_zone);
3893 /* uma_prealloc(pool->zone, total_ctl_io); */
3897 *npool = ctl_softc->io_zone;
3903 ctl_pool_free(struct ctl_io_pool *pool)
3910 uma_zdestroy(pool->zone);
3916 ctl_alloc_io(void *pool_ref)
3920 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3922 io = uma_zalloc(pool->zone, M_WAITOK);
3924 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3927 io->io_hdr.pool = pool_ref;
3932 ctl_alloc_io_nowait(void *pool_ref)
3936 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3938 io = uma_zalloc(pool->zone, M_NOWAIT);
3940 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3943 io->io_hdr.pool = pool_ref;
3948 ctl_free_io(union ctl_io *io)
3951 struct ctl_io_pool *pool;
3958 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3959 uma_zfree(pool->zone, io);
3961 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3966 ctl_zero_io(union ctl_io *io)
3974 * May need to preserve linked list pointers at some point too.
3976 pool_ref = io->io_hdr.pool;
3977 memset(io, 0, sizeof(*io));
3978 io->io_hdr.pool = pool_ref;
3982 * This routine is currently used for internal copies of ctl_ios that need
3983 * to persist for some reason after we've already returned status to the
3984 * FETD. (Thus the flag set.)
3987 * Note that this makes a blind copy of all fields in the ctl_io, except
3988 * for the pool reference. This includes any memory that has been
3989 * allocated! That memory will no longer be valid after done has been
3990 * called, so this would be VERY DANGEROUS for command that actually does
3991 * any reads or writes. Right now (11/7/2005), this is only used for immediate
3992 * start and stop commands, which don't transfer any data, so this is not a
3993 * problem. If it is used for anything else, the caller would also need to
3994 * allocate data buffer space and this routine would need to be modified to
3995 * copy the data buffer(s) as well.
3998 ctl_copy_io(union ctl_io *src, union ctl_io *dest)
4007 * May need to preserve linked list pointers at some point too.
4009 pool_ref = dest->io_hdr.pool;
4011 memcpy(dest, src, MIN(sizeof(*src), sizeof(*dest)));
4013 dest->io_hdr.pool = pool_ref;
4015 * We need to know that this is an internal copy, and doesn't need
4016 * to get passed back to the FETD that allocated it.
4018 dest->io_hdr.flags |= CTL_FLAG_INT_COPY;
4022 ctl_expand_number(const char *buf, uint64_t *num)
4028 number = strtoq(buf, &endptr, 0);
4030 switch (tolower((unsigned char)*endptr)) {
4050 case '\0': /* No unit. */
4054 /* Unrecognized unit. */
4058 if ((number << shift) >> shift != number) {
4062 *num = number << shift;
4068 * This routine could be used in the future to load default and/or saved
4069 * mode page parameters for a particuar lun.
4072 ctl_init_page_index(struct ctl_lun *lun)
4075 struct ctl_page_index *page_index;
4079 memcpy(&lun->mode_pages.index, page_index_template,
4080 sizeof(page_index_template));
4082 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4084 page_index = &lun->mode_pages.index[i];
4086 * If this is a disk-only mode page, there's no point in
4087 * setting it up. For some pages, we have to have some
4088 * basic information about the disk in order to calculate the
4091 if ((lun->be_lun->lun_type != T_DIRECT)
4092 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4095 switch (page_index->page_code & SMPH_PC_MASK) {
4096 case SMS_RW_ERROR_RECOVERY_PAGE: {
4097 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4098 panic("subpage is incorrect!");
4099 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4100 &rw_er_page_default,
4101 sizeof(rw_er_page_default));
4102 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4103 &rw_er_page_changeable,
4104 sizeof(rw_er_page_changeable));
4105 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4106 &rw_er_page_default,
4107 sizeof(rw_er_page_default));
4108 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4109 &rw_er_page_default,
4110 sizeof(rw_er_page_default));
4111 page_index->page_data =
4112 (uint8_t *)lun->mode_pages.rw_er_page;
4115 case SMS_FORMAT_DEVICE_PAGE: {
4116 struct scsi_format_page *format_page;
4118 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4119 panic("subpage is incorrect!");
4122 * Sectors per track are set above. Bytes per
4123 * sector need to be set here on a per-LUN basis.
4125 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4126 &format_page_default,
4127 sizeof(format_page_default));
4128 memcpy(&lun->mode_pages.format_page[
4129 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4130 sizeof(format_page_changeable));
4131 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4132 &format_page_default,
4133 sizeof(format_page_default));
4134 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4135 &format_page_default,
4136 sizeof(format_page_default));
4138 format_page = &lun->mode_pages.format_page[
4140 scsi_ulto2b(lun->be_lun->blocksize,
4141 format_page->bytes_per_sector);
4143 format_page = &lun->mode_pages.format_page[
4145 scsi_ulto2b(lun->be_lun->blocksize,
4146 format_page->bytes_per_sector);
4148 format_page = &lun->mode_pages.format_page[
4150 scsi_ulto2b(lun->be_lun->blocksize,
4151 format_page->bytes_per_sector);
4153 page_index->page_data =
4154 (uint8_t *)lun->mode_pages.format_page;
4157 case SMS_RIGID_DISK_PAGE: {
4158 struct scsi_rigid_disk_page *rigid_disk_page;
4159 uint32_t sectors_per_cylinder;
4163 #endif /* !__XSCALE__ */
4165 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4166 panic("invalid subpage value %d",
4167 page_index->subpage);
4170 * Rotation rate and sectors per track are set
4171 * above. We calculate the cylinders here based on
4172 * capacity. Due to the number of heads and
4173 * sectors per track we're using, smaller arrays
4174 * may turn out to have 0 cylinders. Linux and
4175 * FreeBSD don't pay attention to these mode pages
4176 * to figure out capacity, but Solaris does. It
4177 * seems to deal with 0 cylinders just fine, and
4178 * works out a fake geometry based on the capacity.
4180 memcpy(&lun->mode_pages.rigid_disk_page[
4181 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4182 sizeof(rigid_disk_page_default));
4183 memcpy(&lun->mode_pages.rigid_disk_page[
4184 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4185 sizeof(rigid_disk_page_changeable));
4187 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4191 * The divide method here will be more accurate,
4192 * probably, but results in floating point being
4193 * used in the kernel on i386 (__udivdi3()). On the
4194 * XScale, though, __udivdi3() is implemented in
4197 * The shift method for cylinder calculation is
4198 * accurate if sectors_per_cylinder is a power of
4199 * 2. Otherwise it might be slightly off -- you
4200 * might have a bit of a truncation problem.
4203 cylinders = (lun->be_lun->maxlba + 1) /
4204 sectors_per_cylinder;
4206 for (shift = 31; shift > 0; shift--) {
4207 if (sectors_per_cylinder & (1 << shift))
4210 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4214 * We've basically got 3 bytes, or 24 bits for the
4215 * cylinder size in the mode page. If we're over,
4216 * just round down to 2^24.
4218 if (cylinders > 0xffffff)
4219 cylinders = 0xffffff;
4221 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4223 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4225 if ((value = ctl_get_opt(&lun->be_lun->options,
4227 scsi_ulto2b(strtol(value, NULL, 0),
4228 rigid_disk_page->rotation_rate);
4231 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4232 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4233 sizeof(rigid_disk_page_default));
4234 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4235 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4236 sizeof(rigid_disk_page_default));
4238 page_index->page_data =
4239 (uint8_t *)lun->mode_pages.rigid_disk_page;
4242 case SMS_CACHING_PAGE: {
4243 struct scsi_caching_page *caching_page;
4245 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4246 panic("invalid subpage value %d",
4247 page_index->subpage);
4248 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4249 &caching_page_default,
4250 sizeof(caching_page_default));
4251 memcpy(&lun->mode_pages.caching_page[
4252 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4253 sizeof(caching_page_changeable));
4254 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4255 &caching_page_default,
4256 sizeof(caching_page_default));
4257 caching_page = &lun->mode_pages.caching_page[
4259 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4260 if (value != NULL && strcmp(value, "off") == 0)
4261 caching_page->flags1 &= ~SCP_WCE;
4262 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4263 if (value != NULL && strcmp(value, "off") == 0)
4264 caching_page->flags1 |= SCP_RCD;
4265 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4266 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4267 sizeof(caching_page_default));
4268 page_index->page_data =
4269 (uint8_t *)lun->mode_pages.caching_page;
4272 case SMS_CONTROL_MODE_PAGE: {
4273 struct scsi_control_page *control_page;
4275 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
4276 panic("invalid subpage value %d",
4277 page_index->subpage);
4279 memcpy(&lun->mode_pages.control_page[CTL_PAGE_DEFAULT],
4280 &control_page_default,
4281 sizeof(control_page_default));
4282 memcpy(&lun->mode_pages.control_page[
4283 CTL_PAGE_CHANGEABLE], &control_page_changeable,
4284 sizeof(control_page_changeable));
4285 memcpy(&lun->mode_pages.control_page[CTL_PAGE_SAVED],
4286 &control_page_default,
4287 sizeof(control_page_default));
4288 control_page = &lun->mode_pages.control_page[
4290 value = ctl_get_opt(&lun->be_lun->options, "reordering");
4291 if (value != NULL && strcmp(value, "unrestricted") == 0) {
4292 control_page->queue_flags &= ~SCP_QUEUE_ALG_MASK;
4293 control_page->queue_flags |= SCP_QUEUE_ALG_UNRESTRICTED;
4295 memcpy(&lun->mode_pages.control_page[CTL_PAGE_CURRENT],
4296 &lun->mode_pages.control_page[CTL_PAGE_SAVED],
4297 sizeof(control_page_default));
4298 page_index->page_data =
4299 (uint8_t *)lun->mode_pages.control_page;
4303 case SMS_INFO_EXCEPTIONS_PAGE: {
4304 switch (page_index->subpage) {
4305 case SMS_SUBPAGE_PAGE_0:
4306 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4308 sizeof(ie_page_default));
4309 memcpy(&lun->mode_pages.ie_page[
4310 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4311 sizeof(ie_page_changeable));
4312 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4314 sizeof(ie_page_default));
4315 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4317 sizeof(ie_page_default));
4318 page_index->page_data =
4319 (uint8_t *)lun->mode_pages.ie_page;
4322 struct ctl_logical_block_provisioning_page *page;
4324 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4326 sizeof(lbp_page_default));
4327 memcpy(&lun->mode_pages.lbp_page[
4328 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4329 sizeof(lbp_page_changeable));
4330 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4332 sizeof(lbp_page_default));
4333 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4334 value = ctl_get_opt(&lun->be_lun->options,
4336 if (value != NULL &&
4337 ctl_expand_number(value, &ival) == 0) {
4338 page->descr[0].flags |= SLBPPD_ENABLED |
4340 if (lun->be_lun->blocksize)
4341 ival /= lun->be_lun->blocksize;
4344 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4345 page->descr[0].count);
4347 value = ctl_get_opt(&lun->be_lun->options,
4349 if (value != NULL &&
4350 ctl_expand_number(value, &ival) == 0) {
4351 page->descr[1].flags |= SLBPPD_ENABLED |
4353 if (lun->be_lun->blocksize)
4354 ival /= lun->be_lun->blocksize;
4357 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4358 page->descr[1].count);
4360 value = ctl_get_opt(&lun->be_lun->options,
4361 "pool-avail-threshold");
4362 if (value != NULL &&
4363 ctl_expand_number(value, &ival) == 0) {
4364 page->descr[2].flags |= SLBPPD_ENABLED |
4366 if (lun->be_lun->blocksize)
4367 ival /= lun->be_lun->blocksize;
4370 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4371 page->descr[2].count);
4373 value = ctl_get_opt(&lun->be_lun->options,
4374 "pool-used-threshold");
4375 if (value != NULL &&
4376 ctl_expand_number(value, &ival) == 0) {
4377 page->descr[3].flags |= SLBPPD_ENABLED |
4379 if (lun->be_lun->blocksize)
4380 ival /= lun->be_lun->blocksize;
4383 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4384 page->descr[3].count);
4386 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4387 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4388 sizeof(lbp_page_default));
4389 page_index->page_data =
4390 (uint8_t *)lun->mode_pages.lbp_page;
4394 case SMS_VENDOR_SPECIFIC_PAGE:{
4395 switch (page_index->subpage) {
4396 case DBGCNF_SUBPAGE_CODE: {
4397 struct copan_debugconf_subpage *current_page,
4400 memcpy(&lun->mode_pages.debugconf_subpage[
4402 &debugconf_page_default,
4403 sizeof(debugconf_page_default));
4404 memcpy(&lun->mode_pages.debugconf_subpage[
4405 CTL_PAGE_CHANGEABLE],
4406 &debugconf_page_changeable,
4407 sizeof(debugconf_page_changeable));
4408 memcpy(&lun->mode_pages.debugconf_subpage[
4410 &debugconf_page_default,
4411 sizeof(debugconf_page_default));
4412 memcpy(&lun->mode_pages.debugconf_subpage[
4414 &debugconf_page_default,
4415 sizeof(debugconf_page_default));
4416 page_index->page_data =
4417 (uint8_t *)lun->mode_pages.debugconf_subpage;
4419 current_page = (struct copan_debugconf_subpage *)
4420 (page_index->page_data +
4421 (page_index->page_len *
4423 saved_page = (struct copan_debugconf_subpage *)
4424 (page_index->page_data +
4425 (page_index->page_len *
4430 panic("invalid subpage value %d",
4431 page_index->subpage);
4437 panic("invalid page value %d",
4438 page_index->page_code & SMPH_PC_MASK);
4443 return (CTL_RETVAL_COMPLETE);
4447 ctl_init_log_page_index(struct ctl_lun *lun)
4449 struct ctl_page_index *page_index;
4452 memcpy(&lun->log_pages.index, log_page_index_template,
4453 sizeof(log_page_index_template));
4456 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4458 page_index = &lun->log_pages.index[i];
4460 * If this is a disk-only mode page, there's no point in
4461 * setting it up. For some pages, we have to have some
4462 * basic information about the disk in order to calculate the
4465 if ((lun->be_lun->lun_type != T_DIRECT)
4466 && (page_index->page_flags & CTL_PAGE_FLAG_DISK_ONLY))
4469 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4470 lun->backend->lun_attr == NULL)
4473 if (page_index->page_code != prev) {
4474 lun->log_pages.pages_page[j] = page_index->page_code;
4475 prev = page_index->page_code;
4478 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4479 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4482 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4483 lun->log_pages.index[0].page_len = j;
4484 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4485 lun->log_pages.index[1].page_len = k * 2;
4486 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4487 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4488 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4489 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4491 return (CTL_RETVAL_COMPLETE);
4495 hex2bin(const char *str, uint8_t *buf, int buf_size)
4500 memset(buf, 0, buf_size);
4501 while (isspace(str[0]))
4503 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4506 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4510 else if (isalpha(c))
4511 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4517 buf[i / 2] |= (c << 4);
4521 return ((i + 1) / 2);
4528 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4529 * wants us to allocate the LUN and he can block.
4530 * - ctl_softc is always set
4531 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4533 * Returns 0 for success, non-zero (errno) for failure.
4536 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4537 struct ctl_be_lun *const be_lun, struct ctl_id target_id)
4539 struct ctl_lun *nlun, *lun;
4540 struct ctl_port *port;
4541 struct scsi_vpd_id_descriptor *desc;
4542 struct scsi_vpd_id_t10 *t10id;
4543 const char *eui, *naa, *scsiname, *vendor, *value;
4544 int lun_number, i, lun_malloced;
4545 int devidlen, idlen1, idlen2 = 0, len;
4551 * We currently only support Direct Access or Processor LUN types.
4553 switch (be_lun->lun_type) {
4561 be_lun->lun_config_status(be_lun->be_lun,
4562 CTL_LUN_CONFIG_FAILURE);
4565 if (ctl_lun == NULL) {
4566 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4573 memset(lun, 0, sizeof(*lun));
4575 lun->flags = CTL_LUN_MALLOCED;
4577 /* Generate LUN ID. */
4578 devidlen = max(CTL_DEVID_MIN_LEN,
4579 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4580 idlen1 = sizeof(*t10id) + devidlen;
4581 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4582 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4583 if (scsiname != NULL) {
4584 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4585 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4587 eui = ctl_get_opt(&be_lun->options, "eui");
4589 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4591 naa = ctl_get_opt(&be_lun->options, "naa");
4593 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4595 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4596 M_CTL, M_WAITOK | M_ZERO);
4597 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4598 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4599 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4600 desc->length = idlen1;
4601 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4602 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4603 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4604 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4606 strncpy(t10id->vendor, vendor,
4607 min(sizeof(t10id->vendor), strlen(vendor)));
4609 strncpy((char *)t10id->vendor_spec_id,
4610 (char *)be_lun->device_id, devidlen);
4611 if (scsiname != NULL) {
4612 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4614 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4615 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4616 SVPD_ID_TYPE_SCSI_NAME;
4617 desc->length = idlen2;
4618 strlcpy(desc->identifier, scsiname, idlen2);
4621 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4623 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4624 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4626 desc->length = hex2bin(eui, desc->identifier, 16);
4627 desc->length = desc->length > 12 ? 16 :
4628 (desc->length > 8 ? 12 : 8);
4629 len -= 16 - desc->length;
4632 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4634 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4635 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4637 desc->length = hex2bin(naa, desc->identifier, 16);
4638 desc->length = desc->length > 8 ? 16 : 8;
4639 len -= 16 - desc->length;
4641 lun->lun_devid->len = len;
4643 mtx_lock(&ctl_softc->ctl_lock);
4645 * See if the caller requested a particular LUN number. If so, see
4646 * if it is available. Otherwise, allocate the first available LUN.
4648 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4649 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4650 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4651 mtx_unlock(&ctl_softc->ctl_lock);
4652 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4653 printf("ctl: requested LUN ID %d is higher "
4654 "than CTL_MAX_LUNS - 1 (%d)\n",
4655 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4658 * XXX KDM return an error, or just assign
4659 * another LUN ID in this case??
4661 printf("ctl: requested LUN ID %d is already "
4662 "in use\n", be_lun->req_lun_id);
4664 if (lun->flags & CTL_LUN_MALLOCED)
4666 be_lun->lun_config_status(be_lun->be_lun,
4667 CTL_LUN_CONFIG_FAILURE);
4670 lun_number = be_lun->req_lun_id;
4672 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, CTL_MAX_LUNS);
4673 if (lun_number == -1) {
4674 mtx_unlock(&ctl_softc->ctl_lock);
4675 printf("ctl: can't allocate LUN on target %ju, out of "
4676 "LUNs\n", (uintmax_t)target_id.id);
4677 if (lun->flags & CTL_LUN_MALLOCED)
4679 be_lun->lun_config_status(be_lun->be_lun,
4680 CTL_LUN_CONFIG_FAILURE);
4684 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4686 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4687 lun->target = target_id;
4688 lun->lun = lun_number;
4689 lun->be_lun = be_lun;
4691 * The processor LUN is always enabled. Disk LUNs come on line
4692 * disabled, and must be enabled by the backend.
4694 lun->flags |= CTL_LUN_DISABLED;
4695 lun->backend = be_lun->be;
4696 be_lun->ctl_lun = lun;
4697 be_lun->lun_id = lun_number;
4698 atomic_add_int(&be_lun->be->num_luns, 1);
4699 if (be_lun->flags & CTL_LUN_FLAG_OFFLINE)
4700 lun->flags |= CTL_LUN_OFFLINE;
4702 if (be_lun->flags & CTL_LUN_FLAG_POWERED_OFF)
4703 lun->flags |= CTL_LUN_STOPPED;
4705 if (be_lun->flags & CTL_LUN_FLAG_INOPERABLE)
4706 lun->flags |= CTL_LUN_INOPERABLE;
4708 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4709 lun->flags |= CTL_LUN_PRIMARY_SC;
4711 value = ctl_get_opt(&be_lun->options, "readonly");
4712 if (value != NULL && strcmp(value, "on") == 0)
4713 lun->flags |= CTL_LUN_READONLY;
4715 lun->serseq = CTL_LUN_SERSEQ_OFF;
4716 if (be_lun->flags & CTL_LUN_FLAG_SERSEQ_READ)
4717 lun->serseq = CTL_LUN_SERSEQ_READ;
4718 value = ctl_get_opt(&be_lun->options, "serseq");
4719 if (value != NULL && strcmp(value, "on") == 0)
4720 lun->serseq = CTL_LUN_SERSEQ_ON;
4721 else if (value != NULL && strcmp(value, "read") == 0)
4722 lun->serseq = CTL_LUN_SERSEQ_READ;
4723 else if (value != NULL && strcmp(value, "off") == 0)
4724 lun->serseq = CTL_LUN_SERSEQ_OFF;
4726 lun->ctl_softc = ctl_softc;
4728 lun->last_busy = getsbinuptime();
4730 TAILQ_INIT(&lun->ooa_queue);
4731 TAILQ_INIT(&lun->blocked_queue);
4732 STAILQ_INIT(&lun->error_list);
4733 ctl_tpc_lun_init(lun);
4736 * Initialize the mode and log page index.
4738 ctl_init_page_index(lun);
4739 ctl_init_log_page_index(lun);
4742 * Now, before we insert this lun on the lun list, set the lun
4743 * inventory changed UA for all other luns.
4745 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4746 mtx_lock(&nlun->lun_lock);
4747 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4748 mtx_unlock(&nlun->lun_lock);
4751 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4753 ctl_softc->ctl_luns[lun_number] = lun;
4755 ctl_softc->num_luns++;
4757 /* Setup statistics gathering */
4758 lun->stats.device_type = be_lun->lun_type;
4759 lun->stats.lun_number = lun_number;
4760 if (lun->stats.device_type == T_DIRECT)
4761 lun->stats.blocksize = be_lun->blocksize;
4763 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4764 for (i = 0;i < CTL_MAX_PORTS;i++)
4765 lun->stats.ports[i].targ_port = i;
4767 mtx_unlock(&ctl_softc->ctl_lock);
4769 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4772 * Run through each registered FETD and bring it online if it isn't
4773 * already. Enable the target ID if it hasn't been enabled, and
4774 * enable this particular LUN.
4776 STAILQ_FOREACH(port, &ctl_softc->port_list, links) {
4779 retval = port->lun_enable(port->targ_lun_arg, target_id,lun_number);
4781 printf("ctl_alloc_lun: FETD %s port %d returned error "
4782 "%d for lun_enable on target %ju lun %d\n",
4783 port->port_name, port->targ_port, retval,
4784 (uintmax_t)target_id.id, lun_number);
4786 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4794 * - LUN has already been marked invalid and any pending I/O has been taken
4798 ctl_free_lun(struct ctl_lun *lun)
4800 struct ctl_softc *softc;
4801 struct ctl_port *port;
4802 struct ctl_lun *nlun;
4805 softc = lun->ctl_softc;
4807 mtx_assert(&softc->ctl_lock, MA_OWNED);
4809 STAILQ_FOREACH(port, &softc->port_list, links)
4810 ctl_lun_map_unsetg(port, lun->lun);
4812 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4814 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4816 softc->ctl_luns[lun->lun] = NULL;
4818 if (!TAILQ_EMPTY(&lun->ooa_queue))
4819 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4824 * XXX KDM this scheme only works for a single target/multiple LUN
4825 * setup. It needs to be revamped for a multiple target scheme.
4827 * XXX KDM this results in port->lun_disable() getting called twice,
4828 * once when ctl_disable_lun() is called, and a second time here.
4829 * We really need to re-think the LUN disable semantics. There
4830 * should probably be several steps/levels to LUN removal:
4835 * Right now we only have a disable method when communicating to
4836 * the front end ports, at least for individual LUNs.
4839 STAILQ_FOREACH(port, &softc->port_list, links) {
4842 retval = port->lun_disable(port->targ_lun_arg, lun->target,
4845 printf("ctl_free_lun: FETD %s port %d returned error "
4846 "%d for lun_disable on target %ju lun %jd\n",
4847 port->port_name, port->targ_port, retval,
4848 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4851 if (STAILQ_FIRST(&softc->lun_list) == NULL) {
4852 port->status &= ~CTL_PORT_STATUS_LUN_ONLINE;
4854 retval = port->targ_disable(port->targ_lun_arg,lun->target);
4856 printf("ctl_free_lun: FETD %s port %d "
4857 "returned error %d for targ_disable on "
4858 "target %ju\n", port->port_name,
4859 port->targ_port, retval,
4860 (uintmax_t)lun->target.id);
4862 port->status &= ~CTL_PORT_STATUS_TARG_ONLINE;
4864 if ((port->status & CTL_PORT_STATUS_TARG_ONLINE) != 0)
4868 port->port_offline(port->onoff_arg);
4869 port->status &= ~CTL_PORT_STATUS_ONLINE;
4876 * Tell the backend to free resources, if this LUN has a backend.
4878 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4879 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4881 ctl_tpc_lun_shutdown(lun);
4882 mtx_destroy(&lun->lun_lock);
4883 free(lun->lun_devid, M_CTL);
4884 for (i = 0; i < CTL_MAX_PORTS; i++)
4885 free(lun->pending_ua[i], M_CTL);
4886 for (i = 0; i < 2 * CTL_MAX_PORTS; i++)
4887 free(lun->pr_keys[i], M_CTL);
4888 free(lun->write_buffer, M_CTL);
4889 if (lun->flags & CTL_LUN_MALLOCED)
4892 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4893 mtx_lock(&nlun->lun_lock);
4894 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4895 mtx_unlock(&nlun->lun_lock);
4902 ctl_create_lun(struct ctl_be_lun *be_lun)
4904 struct ctl_softc *softc;
4906 softc = control_softc;
4909 * ctl_alloc_lun() should handle all potential failure cases.
4911 ctl_alloc_lun(softc, NULL, be_lun, softc->target);
4915 ctl_add_lun(struct ctl_be_lun *be_lun)
4917 struct ctl_softc *softc = control_softc;
4919 mtx_lock(&softc->ctl_lock);
4920 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4921 mtx_unlock(&softc->ctl_lock);
4922 wakeup(&softc->pending_lun_queue);
4928 ctl_enable_lun(struct ctl_be_lun *be_lun)
4930 struct ctl_softc *softc;
4931 struct ctl_port *port, *nport;
4932 struct ctl_lun *lun;
4935 lun = (struct ctl_lun *)be_lun->ctl_lun;
4936 softc = lun->ctl_softc;
4938 mtx_lock(&softc->ctl_lock);
4939 mtx_lock(&lun->lun_lock);
4940 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4942 * eh? Why did we get called if the LUN is already
4945 mtx_unlock(&lun->lun_lock);
4946 mtx_unlock(&softc->ctl_lock);
4949 lun->flags &= ~CTL_LUN_DISABLED;
4950 mtx_unlock(&lun->lun_lock);
4952 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4953 nport = STAILQ_NEXT(port, links);
4956 * Drop the lock while we call the FETD's enable routine.
4957 * This can lead to a callback into CTL (at least in the
4958 * case of the internal initiator frontend.
4960 mtx_unlock(&softc->ctl_lock);
4961 retval = port->lun_enable(port->targ_lun_arg, lun->target,lun->lun);
4962 mtx_lock(&softc->ctl_lock);
4964 printf("%s: FETD %s port %d returned error "
4965 "%d for lun_enable on target %ju lun %jd\n",
4966 __func__, port->port_name, port->targ_port, retval,
4967 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
4971 /* NOTE: TODO: why does lun enable affect port status? */
4972 port->status |= CTL_PORT_STATUS_LUN_ONLINE;
4977 mtx_unlock(&softc->ctl_lock);
4983 ctl_disable_lun(struct ctl_be_lun *be_lun)
4985 struct ctl_softc *softc;
4986 struct ctl_port *port;
4987 struct ctl_lun *lun;
4990 lun = (struct ctl_lun *)be_lun->ctl_lun;
4991 softc = lun->ctl_softc;
4993 mtx_lock(&softc->ctl_lock);
4994 mtx_lock(&lun->lun_lock);
4995 if (lun->flags & CTL_LUN_DISABLED) {
4996 mtx_unlock(&lun->lun_lock);
4997 mtx_unlock(&softc->ctl_lock);
5000 lun->flags |= CTL_LUN_DISABLED;
5001 mtx_unlock(&lun->lun_lock);
5003 STAILQ_FOREACH(port, &softc->port_list, links) {
5004 mtx_unlock(&softc->ctl_lock);
5006 * Drop the lock before we call the frontend's disable
5007 * routine, to avoid lock order reversals.
5009 * XXX KDM what happens if the frontend list changes while
5010 * we're traversing it? It's unlikely, but should be handled.
5012 retval = port->lun_disable(port->targ_lun_arg, lun->target,
5014 mtx_lock(&softc->ctl_lock);
5016 printf("%s: FETD %s port %d returned error "
5017 "%d for lun_disable on target %ju lun %jd\n",
5018 __func__, port->port_name, port->targ_port, retval,
5019 (uintmax_t)lun->target.id, (intmax_t)lun->lun);
5023 mtx_unlock(&softc->ctl_lock);
5029 ctl_start_lun(struct ctl_be_lun *be_lun)
5031 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5033 mtx_lock(&lun->lun_lock);
5034 lun->flags &= ~CTL_LUN_STOPPED;
5035 mtx_unlock(&lun->lun_lock);
5040 ctl_stop_lun(struct ctl_be_lun *be_lun)
5042 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5044 mtx_lock(&lun->lun_lock);
5045 lun->flags |= CTL_LUN_STOPPED;
5046 mtx_unlock(&lun->lun_lock);
5051 ctl_lun_offline(struct ctl_be_lun *be_lun)
5053 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5055 mtx_lock(&lun->lun_lock);
5056 lun->flags |= CTL_LUN_OFFLINE;
5057 mtx_unlock(&lun->lun_lock);
5062 ctl_lun_online(struct ctl_be_lun *be_lun)
5064 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5066 mtx_lock(&lun->lun_lock);
5067 lun->flags &= ~CTL_LUN_OFFLINE;
5068 mtx_unlock(&lun->lun_lock);
5073 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5075 struct ctl_softc *softc;
5076 struct ctl_lun *lun;
5078 lun = (struct ctl_lun *)be_lun->ctl_lun;
5079 softc = lun->ctl_softc;
5081 mtx_lock(&lun->lun_lock);
5084 * The LUN needs to be disabled before it can be marked invalid.
5086 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5087 mtx_unlock(&lun->lun_lock);
5091 * Mark the LUN invalid.
5093 lun->flags |= CTL_LUN_INVALID;
5096 * If there is nothing in the OOA queue, go ahead and free the LUN.
5097 * If we have something in the OOA queue, we'll free it when the
5098 * last I/O completes.
5100 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5101 mtx_unlock(&lun->lun_lock);
5102 mtx_lock(&softc->ctl_lock);
5104 mtx_unlock(&softc->ctl_lock);
5106 mtx_unlock(&lun->lun_lock);
5112 ctl_lun_inoperable(struct ctl_be_lun *be_lun)
5114 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5116 mtx_lock(&lun->lun_lock);
5117 lun->flags |= CTL_LUN_INOPERABLE;
5118 mtx_unlock(&lun->lun_lock);
5123 ctl_lun_operable(struct ctl_be_lun *be_lun)
5125 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5127 mtx_lock(&lun->lun_lock);
5128 lun->flags &= ~CTL_LUN_INOPERABLE;
5129 mtx_unlock(&lun->lun_lock);
5134 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5136 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5138 mtx_lock(&lun->lun_lock);
5139 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGED);
5140 mtx_unlock(&lun->lun_lock);
5144 * Backend "memory move is complete" callback for requests that never
5145 * make it down to say RAIDCore's configuration code.
5148 ctl_config_move_done(union ctl_io *io)
5152 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5153 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5154 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5156 if ((io->io_hdr.port_status != 0) &&
5157 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5158 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5160 * For hardware error sense keys, the sense key
5161 * specific value is defined to be a retry count,
5162 * but we use it to pass back an internal FETD
5163 * error code. XXX KDM Hopefully the FETD is only
5164 * using 16 bits for an error code, since that's
5165 * all the space we have in the sks field.
5167 ctl_set_internal_failure(&io->scsiio,
5170 io->io_hdr.port_status);
5173 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5174 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5175 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5176 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5178 * XXX KDM just assuming a single pointer here, and not a
5179 * S/G list. If we start using S/G lists for config data,
5180 * we'll need to know how to clean them up here as well.
5182 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5183 free(io->scsiio.kern_data_ptr, M_CTL);
5185 retval = CTL_RETVAL_COMPLETE;
5188 * XXX KDM now we need to continue data movement. Some
5190 * - call ctl_scsiio() again? We don't do this for data
5191 * writes, because for those at least we know ahead of
5192 * time where the write will go and how long it is. For
5193 * config writes, though, that information is largely
5194 * contained within the write itself, thus we need to
5195 * parse out the data again.
5197 * - Call some other function once the data is in?
5199 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5203 * XXX KDM call ctl_scsiio() again for now, and check flag
5204 * bits to see whether we're allocated or not.
5206 retval = ctl_scsiio(&io->scsiio);
5212 * This gets called by a backend driver when it is done with a
5213 * data_submit method.
5216 ctl_data_submit_done(union ctl_io *io)
5219 * If the IO_CONT flag is set, we need to call the supplied
5220 * function to continue processing the I/O, instead of completing
5223 * If there is an error, though, we don't want to keep processing.
5224 * Instead, just send status back to the initiator.
5226 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5227 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5228 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5229 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5230 io->scsiio.io_cont(io);
5237 * This gets called by a backend driver when it is done with a
5238 * configuration write.
5241 ctl_config_write_done(union ctl_io *io)
5246 * If the IO_CONT flag is set, we need to call the supplied
5247 * function to continue processing the I/O, instead of completing
5250 * If there is an error, though, we don't want to keep processing.
5251 * Instead, just send status back to the initiator.
5253 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5254 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5255 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5256 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5257 io->scsiio.io_cont(io);
5261 * Since a configuration write can be done for commands that actually
5262 * have data allocated, like write buffer, and commands that have
5263 * no data, like start/stop unit, we need to check here.
5265 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5266 buf = io->scsiio.kern_data_ptr;
5275 ctl_config_read_done(union ctl_io *io)
5280 * If there is some error -- we are done, skip data transfer.
5282 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5283 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5284 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5285 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5286 buf = io->scsiio.kern_data_ptr;
5296 * If the IO_CONT flag is set, we need to call the supplied
5297 * function to continue processing the I/O, instead of completing
5300 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5301 io->scsiio.io_cont(io);
5309 * SCSI release command.
5312 ctl_scsi_release(struct ctl_scsiio *ctsio)
5314 int length, longid, thirdparty_id, resv_id;
5315 struct ctl_lun *lun;
5321 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5323 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5324 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5326 switch (ctsio->cdb[0]) {
5328 struct scsi_release_10 *cdb;
5330 cdb = (struct scsi_release_10 *)ctsio->cdb;
5332 if (cdb->byte2 & SR10_LONGID)
5335 thirdparty_id = cdb->thirdparty_id;
5337 resv_id = cdb->resv_id;
5338 length = scsi_2btoul(cdb->length);
5345 * XXX KDM right now, we only support LUN reservation. We don't
5346 * support 3rd party reservations, or extent reservations, which
5347 * might actually need the parameter list. If we've gotten this
5348 * far, we've got a LUN reservation. Anything else got kicked out
5349 * above. So, according to SPC, ignore the length.
5353 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5355 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5356 ctsio->kern_data_len = length;
5357 ctsio->kern_total_len = length;
5358 ctsio->kern_data_resid = 0;
5359 ctsio->kern_rel_offset = 0;
5360 ctsio->kern_sg_entries = 0;
5361 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5362 ctsio->be_move_done = ctl_config_move_done;
5363 ctl_datamove((union ctl_io *)ctsio);
5365 return (CTL_RETVAL_COMPLETE);
5369 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5371 mtx_lock(&lun->lun_lock);
5374 * According to SPC, it is not an error for an intiator to attempt
5375 * to release a reservation on a LUN that isn't reserved, or that
5376 * is reserved by another initiator. The reservation can only be
5377 * released, though, by the initiator who made it or by one of
5378 * several reset type events.
5380 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5381 lun->flags &= ~CTL_LUN_RESERVED;
5383 mtx_unlock(&lun->lun_lock);
5385 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5386 free(ctsio->kern_data_ptr, M_CTL);
5387 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5390 ctl_set_success(ctsio);
5391 ctl_done((union ctl_io *)ctsio);
5392 return (CTL_RETVAL_COMPLETE);
5396 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5398 int extent, thirdparty, longid;
5399 int resv_id, length;
5400 uint64_t thirdparty_id;
5401 struct ctl_lun *lun;
5411 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5413 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5414 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5416 switch (ctsio->cdb[0]) {
5418 struct scsi_reserve_10 *cdb;
5420 cdb = (struct scsi_reserve_10 *)ctsio->cdb;
5422 if (cdb->byte2 & SR10_LONGID)
5425 thirdparty_id = cdb->thirdparty_id;
5427 resv_id = cdb->resv_id;
5428 length = scsi_2btoul(cdb->length);
5434 * XXX KDM right now, we only support LUN reservation. We don't
5435 * support 3rd party reservations, or extent reservations, which
5436 * might actually need the parameter list. If we've gotten this
5437 * far, we've got a LUN reservation. Anything else got kicked out
5438 * above. So, according to SPC, ignore the length.
5442 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5444 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5445 ctsio->kern_data_len = length;
5446 ctsio->kern_total_len = length;
5447 ctsio->kern_data_resid = 0;
5448 ctsio->kern_rel_offset = 0;
5449 ctsio->kern_sg_entries = 0;
5450 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5451 ctsio->be_move_done = ctl_config_move_done;
5452 ctl_datamove((union ctl_io *)ctsio);
5454 return (CTL_RETVAL_COMPLETE);
5458 thirdparty_id = scsi_8btou64(ctsio->kern_data_ptr);
5460 mtx_lock(&lun->lun_lock);
5461 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5462 ctl_set_reservation_conflict(ctsio);
5466 lun->flags |= CTL_LUN_RESERVED;
5467 lun->res_idx = residx;
5469 ctl_set_success(ctsio);
5472 mtx_unlock(&lun->lun_lock);
5474 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5475 free(ctsio->kern_data_ptr, M_CTL);
5476 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5479 ctl_done((union ctl_io *)ctsio);
5480 return (CTL_RETVAL_COMPLETE);
5484 ctl_start_stop(struct ctl_scsiio *ctsio)
5486 struct scsi_start_stop_unit *cdb;
5487 struct ctl_lun *lun;
5490 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5492 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5495 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5499 * We don't support the immediate bit on a stop unit. In order to
5500 * do that, we would need to code up a way to know that a stop is
5501 * pending, and hold off any new commands until it completes, one
5502 * way or another. Then we could accept or reject those commands
5503 * depending on its status. We would almost need to do the reverse
5504 * of what we do below for an immediate start -- return the copy of
5505 * the ctl_io to the FETD with status to send to the host (and to
5506 * free the copy!) and then free the original I/O once the stop
5507 * actually completes. That way, the OOA queue mechanism can work
5508 * to block commands that shouldn't proceed. Another alternative
5509 * would be to put the copy in the queue in place of the original,
5510 * and return the original back to the caller. That could be
5513 if ((cdb->byte2 & SSS_IMMED)
5514 && ((cdb->how & SSS_START) == 0)) {
5515 ctl_set_invalid_field(ctsio,
5521 ctl_done((union ctl_io *)ctsio);
5522 return (CTL_RETVAL_COMPLETE);
5525 if ((lun->flags & CTL_LUN_PR_RESERVED)
5526 && ((cdb->how & SSS_START)==0)) {
5529 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
5530 if (ctl_get_prkey(lun, residx) == 0
5531 || (lun->pr_res_idx!=residx && lun->res_type < 4)) {
5533 ctl_set_reservation_conflict(ctsio);
5534 ctl_done((union ctl_io *)ctsio);
5535 return (CTL_RETVAL_COMPLETE);
5540 * If there is no backend on this device, we can't start or stop
5541 * it. In theory we shouldn't get any start/stop commands in the
5542 * first place at this level if the LUN doesn't have a backend.
5543 * That should get stopped by the command decode code.
5545 if (lun->backend == NULL) {
5546 ctl_set_invalid_opcode(ctsio);
5547 ctl_done((union ctl_io *)ctsio);
5548 return (CTL_RETVAL_COMPLETE);
5552 * XXX KDM Copan-specific offline behavior.
5553 * Figure out a reasonable way to port this?
5556 mtx_lock(&lun->lun_lock);
5558 if (((cdb->byte2 & SSS_ONOFFLINE) == 0)
5559 && (lun->flags & CTL_LUN_OFFLINE)) {
5561 * If the LUN is offline, and the on/offline bit isn't set,
5562 * reject the start or stop. Otherwise, let it through.
5564 mtx_unlock(&lun->lun_lock);
5565 ctl_set_lun_not_ready(ctsio);
5566 ctl_done((union ctl_io *)ctsio);
5568 mtx_unlock(&lun->lun_lock);
5569 #endif /* NEEDTOPORT */
5571 * This could be a start or a stop when we're online,
5572 * or a stop/offline or start/online. A start or stop when
5573 * we're offline is covered in the case above.
5576 * In the non-immediate case, we send the request to
5577 * the backend and return status to the user when
5580 * In the immediate case, we allocate a new ctl_io
5581 * to hold a copy of the request, and send that to
5582 * the backend. We then set good status on the
5583 * user's request and return it immediately.
5585 if (cdb->byte2 & SSS_IMMED) {
5586 union ctl_io *new_io;
5588 new_io = ctl_alloc_io(ctsio->io_hdr.pool);
5589 ctl_copy_io((union ctl_io *)ctsio, new_io);
5590 retval = lun->backend->config_write(new_io);
5591 ctl_set_success(ctsio);
5592 ctl_done((union ctl_io *)ctsio);
5594 retval = lun->backend->config_write(
5595 (union ctl_io *)ctsio);
5604 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5605 * we don't really do anything with the LBA and length fields if the user
5606 * passes them in. Instead we'll just flush out the cache for the entire
5610 ctl_sync_cache(struct ctl_scsiio *ctsio)
5612 struct ctl_lun *lun;
5613 struct ctl_softc *softc;
5614 uint64_t starting_lba;
5615 uint32_t block_count;
5618 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5620 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5621 softc = lun->ctl_softc;
5624 switch (ctsio->cdb[0]) {
5625 case SYNCHRONIZE_CACHE: {
5626 struct scsi_sync_cache *cdb;
5627 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5629 starting_lba = scsi_4btoul(cdb->begin_lba);
5630 block_count = scsi_2btoul(cdb->lb_count);
5633 case SYNCHRONIZE_CACHE_16: {
5634 struct scsi_sync_cache_16 *cdb;
5635 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5637 starting_lba = scsi_8btou64(cdb->begin_lba);
5638 block_count = scsi_4btoul(cdb->lb_count);
5642 ctl_set_invalid_opcode(ctsio);
5643 ctl_done((union ctl_io *)ctsio);
5645 break; /* NOTREACHED */
5649 * We check the LBA and length, but don't do anything with them.
5650 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5651 * get flushed. This check will just help satisfy anyone who wants
5652 * to see an error for an out of range LBA.
5654 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5655 ctl_set_lba_out_of_range(ctsio);
5656 ctl_done((union ctl_io *)ctsio);
5661 * If this LUN has no backend, we can't flush the cache anyway.
5663 if (lun->backend == NULL) {
5664 ctl_set_invalid_opcode(ctsio);
5665 ctl_done((union ctl_io *)ctsio);
5670 * Check to see whether we're configured to send the SYNCHRONIZE
5671 * CACHE command directly to the back end.
5673 mtx_lock(&lun->lun_lock);
5674 if ((softc->flags & CTL_FLAG_REAL_SYNC)
5675 && (++(lun->sync_count) >= lun->sync_interval)) {
5676 lun->sync_count = 0;
5677 mtx_unlock(&lun->lun_lock);
5678 retval = lun->backend->config_write((union ctl_io *)ctsio);
5680 mtx_unlock(&lun->lun_lock);
5681 ctl_set_success(ctsio);
5682 ctl_done((union ctl_io *)ctsio);
5691 ctl_format(struct ctl_scsiio *ctsio)
5693 struct scsi_format *cdb;
5694 struct ctl_lun *lun;
5695 int length, defect_list_len;
5697 CTL_DEBUG_PRINT(("ctl_format\n"));
5699 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5701 cdb = (struct scsi_format *)ctsio->cdb;
5704 if (cdb->byte2 & SF_FMTDATA) {
5705 if (cdb->byte2 & SF_LONGLIST)
5706 length = sizeof(struct scsi_format_header_long);
5708 length = sizeof(struct scsi_format_header_short);
5711 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5713 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5714 ctsio->kern_data_len = length;
5715 ctsio->kern_total_len = length;
5716 ctsio->kern_data_resid = 0;
5717 ctsio->kern_rel_offset = 0;
5718 ctsio->kern_sg_entries = 0;
5719 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5720 ctsio->be_move_done = ctl_config_move_done;
5721 ctl_datamove((union ctl_io *)ctsio);
5723 return (CTL_RETVAL_COMPLETE);
5726 defect_list_len = 0;
5728 if (cdb->byte2 & SF_FMTDATA) {
5729 if (cdb->byte2 & SF_LONGLIST) {
5730 struct scsi_format_header_long *header;
5732 header = (struct scsi_format_header_long *)
5733 ctsio->kern_data_ptr;
5735 defect_list_len = scsi_4btoul(header->defect_list_len);
5736 if (defect_list_len != 0) {
5737 ctl_set_invalid_field(ctsio,
5746 struct scsi_format_header_short *header;
5748 header = (struct scsi_format_header_short *)
5749 ctsio->kern_data_ptr;
5751 defect_list_len = scsi_2btoul(header->defect_list_len);
5752 if (defect_list_len != 0) {
5753 ctl_set_invalid_field(ctsio,
5765 * The format command will clear out the "Medium format corrupted"
5766 * status if set by the configuration code. That status is really
5767 * just a way to notify the host that we have lost the media, and
5768 * get them to issue a command that will basically make them think
5769 * they're blowing away the media.
5771 mtx_lock(&lun->lun_lock);
5772 lun->flags &= ~CTL_LUN_INOPERABLE;
5773 mtx_unlock(&lun->lun_lock);
5775 ctl_set_success(ctsio);
5778 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5779 free(ctsio->kern_data_ptr, M_CTL);
5780 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5783 ctl_done((union ctl_io *)ctsio);
5784 return (CTL_RETVAL_COMPLETE);
5788 ctl_read_buffer(struct ctl_scsiio *ctsio)
5790 struct scsi_read_buffer *cdb;
5791 struct ctl_lun *lun;
5792 int buffer_offset, len;
5793 static uint8_t descr[4];
5794 static uint8_t echo_descr[4] = { 0 };
5796 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5798 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5799 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5801 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA &&
5802 (cdb->byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5803 (cdb->byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5804 ctl_set_invalid_field(ctsio,
5810 ctl_done((union ctl_io *)ctsio);
5811 return (CTL_RETVAL_COMPLETE);
5814 len = scsi_3btoul(cdb->length);
5815 buffer_offset = scsi_3btoul(cdb->offset);
5817 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5818 ctl_set_invalid_field(ctsio,
5824 ctl_done((union ctl_io *)ctsio);
5825 return (CTL_RETVAL_COMPLETE);
5828 if ((cdb->byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5830 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5831 ctsio->kern_data_ptr = descr;
5832 len = min(len, sizeof(descr));
5833 } else if ((cdb->byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5834 ctsio->kern_data_ptr = echo_descr;
5835 len = min(len, sizeof(echo_descr));
5837 if (lun->write_buffer == NULL) {
5838 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5841 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5843 ctsio->kern_data_len = len;
5844 ctsio->kern_total_len = len;
5845 ctsio->kern_data_resid = 0;
5846 ctsio->kern_rel_offset = 0;
5847 ctsio->kern_sg_entries = 0;
5848 ctl_set_success(ctsio);
5849 ctsio->be_move_done = ctl_config_move_done;
5850 ctl_datamove((union ctl_io *)ctsio);
5851 return (CTL_RETVAL_COMPLETE);
5855 ctl_write_buffer(struct ctl_scsiio *ctsio)
5857 struct scsi_write_buffer *cdb;
5858 struct ctl_lun *lun;
5859 int buffer_offset, len;
5861 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5863 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5864 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5866 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5867 ctl_set_invalid_field(ctsio,
5873 ctl_done((union ctl_io *)ctsio);
5874 return (CTL_RETVAL_COMPLETE);
5877 len = scsi_3btoul(cdb->length);
5878 buffer_offset = scsi_3btoul(cdb->offset);
5880 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5881 ctl_set_invalid_field(ctsio,
5887 ctl_done((union ctl_io *)ctsio);
5888 return (CTL_RETVAL_COMPLETE);
5892 * If we've got a kernel request that hasn't been malloced yet,
5893 * malloc it and tell the caller the data buffer is here.
5895 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5896 if (lun->write_buffer == NULL) {
5897 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5900 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5901 ctsio->kern_data_len = len;
5902 ctsio->kern_total_len = len;
5903 ctsio->kern_data_resid = 0;
5904 ctsio->kern_rel_offset = 0;
5905 ctsio->kern_sg_entries = 0;
5906 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5907 ctsio->be_move_done = ctl_config_move_done;
5908 ctl_datamove((union ctl_io *)ctsio);
5910 return (CTL_RETVAL_COMPLETE);
5913 ctl_set_success(ctsio);
5914 ctl_done((union ctl_io *)ctsio);
5915 return (CTL_RETVAL_COMPLETE);
5919 ctl_write_same(struct ctl_scsiio *ctsio)
5921 struct ctl_lun *lun;
5922 struct ctl_lba_len_flags *lbalen;
5924 uint32_t num_blocks;
5928 retval = CTL_RETVAL_COMPLETE;
5930 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5932 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5934 switch (ctsio->cdb[0]) {
5935 case WRITE_SAME_10: {
5936 struct scsi_write_same_10 *cdb;
5938 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5940 lba = scsi_4btoul(cdb->addr);
5941 num_blocks = scsi_2btoul(cdb->length);
5945 case WRITE_SAME_16: {
5946 struct scsi_write_same_16 *cdb;
5948 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5950 lba = scsi_8btou64(cdb->addr);
5951 num_blocks = scsi_4btoul(cdb->length);
5957 * We got a command we don't support. This shouldn't
5958 * happen, commands should be filtered out above us.
5960 ctl_set_invalid_opcode(ctsio);
5961 ctl_done((union ctl_io *)ctsio);
5963 return (CTL_RETVAL_COMPLETE);
5964 break; /* NOTREACHED */
5967 /* NDOB and ANCHOR flags can be used only together with UNMAP */
5968 if ((byte2 & SWS_UNMAP) == 0 &&
5969 (byte2 & (SWS_NDOB | SWS_ANCHOR)) != 0) {
5970 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5971 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5972 ctl_done((union ctl_io *)ctsio);
5973 return (CTL_RETVAL_COMPLETE);
5977 * The first check is to make sure we're in bounds, the second
5978 * check is to catch wrap-around problems. If the lba + num blocks
5979 * is less than the lba, then we've wrapped around and the block
5980 * range is invalid anyway.
5982 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5983 || ((lba + num_blocks) < lba)) {
5984 ctl_set_lba_out_of_range(ctsio);
5985 ctl_done((union ctl_io *)ctsio);
5986 return (CTL_RETVAL_COMPLETE);
5989 /* Zero number of blocks means "to the last logical block" */
5990 if (num_blocks == 0) {
5991 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5992 ctl_set_invalid_field(ctsio,
5998 ctl_done((union ctl_io *)ctsio);
5999 return (CTL_RETVAL_COMPLETE);
6001 num_blocks = (lun->be_lun->maxlba + 1) - lba;
6004 len = lun->be_lun->blocksize;
6007 * If we've got a kernel request that hasn't been malloced yet,
6008 * malloc it and tell the caller the data buffer is here.
6010 if ((byte2 & SWS_NDOB) == 0 &&
6011 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6012 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6013 ctsio->kern_data_len = len;
6014 ctsio->kern_total_len = len;
6015 ctsio->kern_data_resid = 0;
6016 ctsio->kern_rel_offset = 0;
6017 ctsio->kern_sg_entries = 0;
6018 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6019 ctsio->be_move_done = ctl_config_move_done;
6020 ctl_datamove((union ctl_io *)ctsio);
6022 return (CTL_RETVAL_COMPLETE);
6025 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6027 lbalen->len = num_blocks;
6028 lbalen->flags = byte2;
6029 retval = lun->backend->config_write((union ctl_io *)ctsio);
6035 ctl_unmap(struct ctl_scsiio *ctsio)
6037 struct ctl_lun *lun;
6038 struct scsi_unmap *cdb;
6039 struct ctl_ptr_len_flags *ptrlen;
6040 struct scsi_unmap_header *hdr;
6041 struct scsi_unmap_desc *buf, *end, *endnz, *range;
6043 uint32_t num_blocks;
6047 retval = CTL_RETVAL_COMPLETE;
6049 CTL_DEBUG_PRINT(("ctl_unmap\n"));
6051 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6052 cdb = (struct scsi_unmap *)ctsio->cdb;
6054 len = scsi_2btoul(cdb->length);
6058 * If we've got a kernel request that hasn't been malloced yet,
6059 * malloc it and tell the caller the data buffer is here.
6061 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6062 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
6063 ctsio->kern_data_len = len;
6064 ctsio->kern_total_len = len;
6065 ctsio->kern_data_resid = 0;
6066 ctsio->kern_rel_offset = 0;
6067 ctsio->kern_sg_entries = 0;
6068 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6069 ctsio->be_move_done = ctl_config_move_done;
6070 ctl_datamove((union ctl_io *)ctsio);
6072 return (CTL_RETVAL_COMPLETE);
6075 len = ctsio->kern_total_len - ctsio->kern_data_resid;
6076 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
6077 if (len < sizeof (*hdr) ||
6078 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
6079 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
6080 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
6081 ctl_set_invalid_field(ctsio,
6089 len = scsi_2btoul(hdr->desc_length);
6090 buf = (struct scsi_unmap_desc *)(hdr + 1);
6091 end = buf + len / sizeof(*buf);
6094 for (range = buf; range < end; range++) {
6095 lba = scsi_8btou64(range->lba);
6096 num_blocks = scsi_4btoul(range->length);
6097 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
6098 || ((lba + num_blocks) < lba)) {
6099 ctl_set_lba_out_of_range(ctsio);
6100 ctl_done((union ctl_io *)ctsio);
6101 return (CTL_RETVAL_COMPLETE);
6103 if (num_blocks != 0)
6108 * Block backend can not handle zero last range.
6109 * Filter it out and return if there is nothing left.
6111 len = (uint8_t *)endnz - (uint8_t *)buf;
6113 ctl_set_success(ctsio);
6117 mtx_lock(&lun->lun_lock);
6118 ptrlen = (struct ctl_ptr_len_flags *)
6119 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6120 ptrlen->ptr = (void *)buf;
6122 ptrlen->flags = byte2;
6123 ctl_check_blocked(lun);
6124 mtx_unlock(&lun->lun_lock);
6126 retval = lun->backend->config_write((union ctl_io *)ctsio);
6130 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
6131 free(ctsio->kern_data_ptr, M_CTL);
6132 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
6134 ctl_done((union ctl_io *)ctsio);
6135 return (CTL_RETVAL_COMPLETE);
6139 * Note that this function currently doesn't actually do anything inside
6140 * CTL to enforce things if the DQue bit is turned on.
6142 * Also note that this function can't be used in the default case, because
6143 * the DQue bit isn't set in the changeable mask for the control mode page
6144 * anyway. This is just here as an example for how to implement a page
6145 * handler, and a placeholder in case we want to allow the user to turn
6146 * tagged queueing on and off.
6148 * The D_SENSE bit handling is functional, however, and will turn
6149 * descriptor sense on and off for a given LUN.
6152 ctl_control_page_handler(struct ctl_scsiio *ctsio,
6153 struct ctl_page_index *page_index, uint8_t *page_ptr)
6155 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
6156 struct ctl_lun *lun;
6160 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6161 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6164 user_cp = (struct scsi_control_page *)page_ptr;
6165 current_cp = (struct scsi_control_page *)
6166 (page_index->page_data + (page_index->page_len *
6168 saved_cp = (struct scsi_control_page *)
6169 (page_index->page_data + (page_index->page_len *
6172 mtx_lock(&lun->lun_lock);
6173 if (((current_cp->rlec & SCP_DSENSE) == 0)
6174 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
6176 * Descriptor sense is currently turned off and the user
6177 * wants to turn it on.
6179 current_cp->rlec |= SCP_DSENSE;
6180 saved_cp->rlec |= SCP_DSENSE;
6181 lun->flags |= CTL_LUN_SENSE_DESC;
6183 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
6184 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
6186 * Descriptor sense is currently turned on, and the user
6187 * wants to turn it off.
6189 current_cp->rlec &= ~SCP_DSENSE;
6190 saved_cp->rlec &= ~SCP_DSENSE;
6191 lun->flags &= ~CTL_LUN_SENSE_DESC;
6194 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
6195 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
6196 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6197 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6198 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
6199 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
6202 if ((current_cp->eca_and_aen & SCP_SWP) !=
6203 (user_cp->eca_and_aen & SCP_SWP)) {
6204 current_cp->eca_and_aen &= ~SCP_SWP;
6205 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6206 saved_cp->eca_and_aen &= ~SCP_SWP;
6207 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
6211 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6212 mtx_unlock(&lun->lun_lock);
6218 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
6219 struct ctl_page_index *page_index, uint8_t *page_ptr)
6221 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
6222 struct ctl_lun *lun;
6226 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6227 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6230 user_cp = (struct scsi_caching_page *)page_ptr;
6231 current_cp = (struct scsi_caching_page *)
6232 (page_index->page_data + (page_index->page_len *
6234 saved_cp = (struct scsi_caching_page *)
6235 (page_index->page_data + (page_index->page_len *
6238 mtx_lock(&lun->lun_lock);
6239 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
6240 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
6241 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6242 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6243 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
6244 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
6248 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6249 mtx_unlock(&lun->lun_lock);
6255 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
6256 struct ctl_page_index *page_index,
6262 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
6267 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
6268 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
6269 printf("page data:");
6271 printf(" %.2x",page_ptr[i]);
6277 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
6278 struct ctl_page_index *page_index,
6281 struct copan_debugconf_subpage *page;
6283 page = (struct copan_debugconf_subpage *)page_index->page_data +
6284 (page_index->page_len * pc);
6287 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
6288 case SMS_PAGE_CTRL_DEFAULT >> 6:
6289 case SMS_PAGE_CTRL_SAVED >> 6:
6291 * We don't update the changable or default bits for this page.
6294 case SMS_PAGE_CTRL_CURRENT >> 6:
6295 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
6296 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6300 EPRINT(0, "Invalid PC %d!!", pc);
6301 #endif /* NEEDTOPORT */
6309 ctl_do_mode_select(union ctl_io *io)
6311 struct scsi_mode_page_header *page_header;
6312 struct ctl_page_index *page_index;
6313 struct ctl_scsiio *ctsio;
6314 int control_dev, page_len;
6315 int page_len_offset, page_len_size;
6316 union ctl_modepage_info *modepage_info;
6317 struct ctl_lun *lun;
6318 int *len_left, *len_used;
6321 ctsio = &io->scsiio;
6324 retval = CTL_RETVAL_COMPLETE;
6326 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6328 if (lun->be_lun->lun_type != T_DIRECT)
6333 modepage_info = (union ctl_modepage_info *)
6334 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6335 len_left = &modepage_info->header.len_left;
6336 len_used = &modepage_info->header.len_used;
6340 page_header = (struct scsi_mode_page_header *)
6341 (ctsio->kern_data_ptr + *len_used);
6343 if (*len_left == 0) {
6344 free(ctsio->kern_data_ptr, M_CTL);
6345 ctl_set_success(ctsio);
6346 ctl_done((union ctl_io *)ctsio);
6347 return (CTL_RETVAL_COMPLETE);
6348 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6350 free(ctsio->kern_data_ptr, M_CTL);
6351 ctl_set_param_len_error(ctsio);
6352 ctl_done((union ctl_io *)ctsio);
6353 return (CTL_RETVAL_COMPLETE);
6355 } else if ((page_header->page_code & SMPH_SPF)
6356 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6358 free(ctsio->kern_data_ptr, M_CTL);
6359 ctl_set_param_len_error(ctsio);
6360 ctl_done((union ctl_io *)ctsio);
6361 return (CTL_RETVAL_COMPLETE);
6366 * XXX KDM should we do something with the block descriptor?
6368 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6370 if ((control_dev != 0)
6371 && (lun->mode_pages.index[i].page_flags &
6372 CTL_PAGE_FLAG_DISK_ONLY))
6375 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) !=
6376 (page_header->page_code & SMPH_PC_MASK))
6380 * If neither page has a subpage code, then we've got a
6383 if (((lun->mode_pages.index[i].page_code & SMPH_SPF) == 0)
6384 && ((page_header->page_code & SMPH_SPF) == 0)) {
6385 page_index = &lun->mode_pages.index[i];
6386 page_len = page_header->page_length;
6391 * If both pages have subpages, then the subpage numbers
6394 if ((lun->mode_pages.index[i].page_code & SMPH_SPF)
6395 && (page_header->page_code & SMPH_SPF)) {
6396 struct scsi_mode_page_header_sp *sph;
6398 sph = (struct scsi_mode_page_header_sp *)page_header;
6400 if (lun->mode_pages.index[i].subpage ==
6402 page_index = &lun->mode_pages.index[i];
6403 page_len = scsi_2btoul(sph->page_length);
6410 * If we couldn't find the page, or if we don't have a mode select
6411 * handler for it, send back an error to the user.
6413 if ((page_index == NULL)
6414 || (page_index->select_handler == NULL)) {
6415 ctl_set_invalid_field(ctsio,
6418 /*field*/ *len_used,
6421 free(ctsio->kern_data_ptr, M_CTL);
6422 ctl_done((union ctl_io *)ctsio);
6423 return (CTL_RETVAL_COMPLETE);
6426 if (page_index->page_code & SMPH_SPF) {
6427 page_len_offset = 2;
6431 page_len_offset = 1;
6435 * If the length the initiator gives us isn't the one we specify in
6436 * the mode page header, or if they didn't specify enough data in
6437 * the CDB to avoid truncating this page, kick out the request.
6439 if ((page_len != (page_index->page_len - page_len_offset -
6441 || (*len_left < page_index->page_len)) {
6444 ctl_set_invalid_field(ctsio,
6447 /*field*/ *len_used + page_len_offset,
6450 free(ctsio->kern_data_ptr, M_CTL);
6451 ctl_done((union ctl_io *)ctsio);
6452 return (CTL_RETVAL_COMPLETE);
6456 * Run through the mode page, checking to make sure that the bits
6457 * the user changed are actually legal for him to change.
6459 for (i = 0; i < page_index->page_len; i++) {
6460 uint8_t *user_byte, *change_mask, *current_byte;
6464 user_byte = (uint8_t *)page_header + i;
6465 change_mask = page_index->page_data +
6466 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6467 current_byte = page_index->page_data +
6468 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6471 * Check to see whether the user set any bits in this byte
6472 * that he is not allowed to set.
6474 if ((*user_byte & ~(*change_mask)) ==
6475 (*current_byte & ~(*change_mask)))
6479 * Go through bit by bit to determine which one is illegal.
6482 for (j = 7; j >= 0; j--) {
6483 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6484 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6489 ctl_set_invalid_field(ctsio,
6492 /*field*/ *len_used + i,
6495 free(ctsio->kern_data_ptr, M_CTL);
6496 ctl_done((union ctl_io *)ctsio);
6497 return (CTL_RETVAL_COMPLETE);
6501 * Decrement these before we call the page handler, since we may
6502 * end up getting called back one way or another before the handler
6503 * returns to this context.
6505 *len_left -= page_index->page_len;
6506 *len_used += page_index->page_len;
6508 retval = page_index->select_handler(ctsio, page_index,
6509 (uint8_t *)page_header);
6512 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6513 * wait until this queued command completes to finish processing
6514 * the mode page. If it returns anything other than
6515 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6516 * already set the sense information, freed the data pointer, and
6517 * completed the io for us.
6519 if (retval != CTL_RETVAL_COMPLETE)
6520 goto bailout_no_done;
6523 * If the initiator sent us more than one page, parse the next one.
6528 ctl_set_success(ctsio);
6529 free(ctsio->kern_data_ptr, M_CTL);
6530 ctl_done((union ctl_io *)ctsio);
6534 return (CTL_RETVAL_COMPLETE);
6539 ctl_mode_select(struct ctl_scsiio *ctsio)
6541 int param_len, pf, sp;
6542 int header_size, bd_len;
6543 int len_left, len_used;
6544 struct ctl_page_index *page_index;
6545 struct ctl_lun *lun;
6546 int control_dev, page_len;
6547 union ctl_modepage_info *modepage_info;
6559 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6561 if (lun->be_lun->lun_type != T_DIRECT)
6566 switch (ctsio->cdb[0]) {
6567 case MODE_SELECT_6: {
6568 struct scsi_mode_select_6 *cdb;
6570 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6572 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6573 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6575 param_len = cdb->length;
6576 header_size = sizeof(struct scsi_mode_header_6);
6579 case MODE_SELECT_10: {
6580 struct scsi_mode_select_10 *cdb;
6582 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6584 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6585 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6587 param_len = scsi_2btoul(cdb->length);
6588 header_size = sizeof(struct scsi_mode_header_10);
6592 ctl_set_invalid_opcode(ctsio);
6593 ctl_done((union ctl_io *)ctsio);
6594 return (CTL_RETVAL_COMPLETE);
6595 break; /* NOTREACHED */
6600 * "A parameter list length of zero indicates that the Data-Out Buffer
6601 * shall be empty. This condition shall not be considered as an error."
6603 if (param_len == 0) {
6604 ctl_set_success(ctsio);
6605 ctl_done((union ctl_io *)ctsio);
6606 return (CTL_RETVAL_COMPLETE);
6610 * Since we'll hit this the first time through, prior to
6611 * allocation, we don't need to free a data buffer here.
6613 if (param_len < header_size) {
6614 ctl_set_param_len_error(ctsio);
6615 ctl_done((union ctl_io *)ctsio);
6616 return (CTL_RETVAL_COMPLETE);
6620 * Allocate the data buffer and grab the user's data. In theory,
6621 * we shouldn't have to sanity check the parameter list length here
6622 * because the maximum size is 64K. We should be able to malloc
6623 * that much without too many problems.
6625 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6626 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6627 ctsio->kern_data_len = param_len;
6628 ctsio->kern_total_len = param_len;
6629 ctsio->kern_data_resid = 0;
6630 ctsio->kern_rel_offset = 0;
6631 ctsio->kern_sg_entries = 0;
6632 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6633 ctsio->be_move_done = ctl_config_move_done;
6634 ctl_datamove((union ctl_io *)ctsio);
6636 return (CTL_RETVAL_COMPLETE);
6639 switch (ctsio->cdb[0]) {
6640 case MODE_SELECT_6: {
6641 struct scsi_mode_header_6 *mh6;
6643 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6644 bd_len = mh6->blk_desc_len;
6647 case MODE_SELECT_10: {
6648 struct scsi_mode_header_10 *mh10;
6650 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6651 bd_len = scsi_2btoul(mh10->blk_desc_len);
6655 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6659 if (param_len < (header_size + bd_len)) {
6660 free(ctsio->kern_data_ptr, M_CTL);
6661 ctl_set_param_len_error(ctsio);
6662 ctl_done((union ctl_io *)ctsio);
6663 return (CTL_RETVAL_COMPLETE);
6667 * Set the IO_CONT flag, so that if this I/O gets passed to
6668 * ctl_config_write_done(), it'll get passed back to
6669 * ctl_do_mode_select() for further processing, or completion if
6672 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6673 ctsio->io_cont = ctl_do_mode_select;
6675 modepage_info = (union ctl_modepage_info *)
6676 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6678 memset(modepage_info, 0, sizeof(*modepage_info));
6680 len_left = param_len - header_size - bd_len;
6681 len_used = header_size + bd_len;
6683 modepage_info->header.len_left = len_left;
6684 modepage_info->header.len_used = len_used;
6686 return (ctl_do_mode_select((union ctl_io *)ctsio));
6690 ctl_mode_sense(struct ctl_scsiio *ctsio)
6692 struct ctl_lun *lun;
6693 int pc, page_code, dbd, llba, subpage;
6694 int alloc_len, page_len, header_len, total_len;
6695 struct scsi_mode_block_descr *block_desc;
6696 struct ctl_page_index *page_index;
6704 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6706 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6708 if (lun->be_lun->lun_type != T_DIRECT)
6713 switch (ctsio->cdb[0]) {
6714 case MODE_SENSE_6: {
6715 struct scsi_mode_sense_6 *cdb;
6717 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6719 header_len = sizeof(struct scsi_mode_hdr_6);
6720 if (cdb->byte2 & SMS_DBD)
6723 header_len += sizeof(struct scsi_mode_block_descr);
6725 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6726 page_code = cdb->page & SMS_PAGE_CODE;
6727 subpage = cdb->subpage;
6728 alloc_len = cdb->length;
6731 case MODE_SENSE_10: {
6732 struct scsi_mode_sense_10 *cdb;
6734 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6736 header_len = sizeof(struct scsi_mode_hdr_10);
6738 if (cdb->byte2 & SMS_DBD)
6741 header_len += sizeof(struct scsi_mode_block_descr);
6742 if (cdb->byte2 & SMS10_LLBAA)
6744 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6745 page_code = cdb->page & SMS_PAGE_CODE;
6746 subpage = cdb->subpage;
6747 alloc_len = scsi_2btoul(cdb->length);
6751 ctl_set_invalid_opcode(ctsio);
6752 ctl_done((union ctl_io *)ctsio);
6753 return (CTL_RETVAL_COMPLETE);
6754 break; /* NOTREACHED */
6758 * We have to make a first pass through to calculate the size of
6759 * the pages that match the user's query. Then we allocate enough
6760 * memory to hold it, and actually copy the data into the buffer.
6762 switch (page_code) {
6763 case SMS_ALL_PAGES_PAGE: {
6769 * At the moment, values other than 0 and 0xff here are
6770 * reserved according to SPC-3.
6772 if ((subpage != SMS_SUBPAGE_PAGE_0)
6773 && (subpage != SMS_SUBPAGE_ALL)) {
6774 ctl_set_invalid_field(ctsio,
6780 ctl_done((union ctl_io *)ctsio);
6781 return (CTL_RETVAL_COMPLETE);
6784 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6785 if ((control_dev != 0)
6786 && (lun->mode_pages.index[i].page_flags &
6787 CTL_PAGE_FLAG_DISK_ONLY))
6791 * We don't use this subpage if the user didn't
6792 * request all subpages.
6794 if ((lun->mode_pages.index[i].subpage != 0)
6795 && (subpage == SMS_SUBPAGE_PAGE_0))
6799 printf("found page %#x len %d\n",
6800 lun->mode_pages.index[i].page_code &
6802 lun->mode_pages.index[i].page_len);
6804 page_len += lun->mode_pages.index[i].page_len;
6813 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6814 /* Look for the right page code */
6815 if ((lun->mode_pages.index[i].page_code &
6816 SMPH_PC_MASK) != page_code)
6819 /* Look for the right subpage or the subpage wildcard*/
6820 if ((lun->mode_pages.index[i].subpage != subpage)
6821 && (subpage != SMS_SUBPAGE_ALL))
6824 /* Make sure the page is supported for this dev type */
6825 if ((control_dev != 0)
6826 && (lun->mode_pages.index[i].page_flags &
6827 CTL_PAGE_FLAG_DISK_ONLY))
6831 printf("found page %#x len %d\n",
6832 lun->mode_pages.index[i].page_code &
6834 lun->mode_pages.index[i].page_len);
6837 page_len += lun->mode_pages.index[i].page_len;
6840 if (page_len == 0) {
6841 ctl_set_invalid_field(ctsio,
6847 ctl_done((union ctl_io *)ctsio);
6848 return (CTL_RETVAL_COMPLETE);
6854 total_len = header_len + page_len;
6856 printf("header_len = %d, page_len = %d, total_len = %d\n",
6857 header_len, page_len, total_len);
6860 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6861 ctsio->kern_sg_entries = 0;
6862 ctsio->kern_data_resid = 0;
6863 ctsio->kern_rel_offset = 0;
6864 if (total_len < alloc_len) {
6865 ctsio->residual = alloc_len - total_len;
6866 ctsio->kern_data_len = total_len;
6867 ctsio->kern_total_len = total_len;
6869 ctsio->residual = 0;
6870 ctsio->kern_data_len = alloc_len;
6871 ctsio->kern_total_len = alloc_len;
6874 switch (ctsio->cdb[0]) {
6875 case MODE_SENSE_6: {
6876 struct scsi_mode_hdr_6 *header;
6878 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6880 header->datalen = MIN(total_len - 1, 254);
6881 if (control_dev == 0) {
6882 header->dev_specific = 0x10; /* DPOFUA */
6883 if ((lun->flags & CTL_LUN_READONLY) ||
6884 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6885 .eca_and_aen & SCP_SWP) != 0)
6886 header->dev_specific |= 0x80; /* WP */
6889 header->block_descr_len = 0;
6891 header->block_descr_len =
6892 sizeof(struct scsi_mode_block_descr);
6893 block_desc = (struct scsi_mode_block_descr *)&header[1];
6896 case MODE_SENSE_10: {
6897 struct scsi_mode_hdr_10 *header;
6900 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6902 datalen = MIN(total_len - 2, 65533);
6903 scsi_ulto2b(datalen, header->datalen);
6904 if (control_dev == 0) {
6905 header->dev_specific = 0x10; /* DPOFUA */
6906 if ((lun->flags & CTL_LUN_READONLY) ||
6907 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6908 .eca_and_aen & SCP_SWP) != 0)
6909 header->dev_specific |= 0x80; /* WP */
6912 scsi_ulto2b(0, header->block_descr_len);
6914 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6915 header->block_descr_len);
6916 block_desc = (struct scsi_mode_block_descr *)&header[1];
6920 panic("invalid CDB type %#x", ctsio->cdb[0]);
6921 break; /* NOTREACHED */
6925 * If we've got a disk, use its blocksize in the block
6926 * descriptor. Otherwise, just set it to 0.
6929 if (control_dev == 0)
6930 scsi_ulto3b(lun->be_lun->blocksize,
6931 block_desc->block_len);
6933 scsi_ulto3b(0, block_desc->block_len);
6936 switch (page_code) {
6937 case SMS_ALL_PAGES_PAGE: {
6940 data_used = header_len;
6941 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6942 struct ctl_page_index *page_index;
6944 page_index = &lun->mode_pages.index[i];
6946 if ((control_dev != 0)
6947 && (page_index->page_flags &
6948 CTL_PAGE_FLAG_DISK_ONLY))
6952 * We don't use this subpage if the user didn't
6953 * request all subpages. We already checked (above)
6954 * to make sure the user only specified a subpage
6955 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6957 if ((page_index->subpage != 0)
6958 && (subpage == SMS_SUBPAGE_PAGE_0))
6962 * Call the handler, if it exists, to update the
6963 * page to the latest values.
6965 if (page_index->sense_handler != NULL)
6966 page_index->sense_handler(ctsio, page_index,pc);
6968 memcpy(ctsio->kern_data_ptr + data_used,
6969 page_index->page_data +
6970 (page_index->page_len * pc),
6971 page_index->page_len);
6972 data_used += page_index->page_len;
6979 data_used = header_len;
6981 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6982 struct ctl_page_index *page_index;
6984 page_index = &lun->mode_pages.index[i];
6986 /* Look for the right page code */
6987 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6990 /* Look for the right subpage or the subpage wildcard*/
6991 if ((page_index->subpage != subpage)
6992 && (subpage != SMS_SUBPAGE_ALL))
6995 /* Make sure the page is supported for this dev type */
6996 if ((control_dev != 0)
6997 && (page_index->page_flags &
6998 CTL_PAGE_FLAG_DISK_ONLY))
7002 * Call the handler, if it exists, to update the
7003 * page to the latest values.
7005 if (page_index->sense_handler != NULL)
7006 page_index->sense_handler(ctsio, page_index,pc);
7008 memcpy(ctsio->kern_data_ptr + data_used,
7009 page_index->page_data +
7010 (page_index->page_len * pc),
7011 page_index->page_len);
7012 data_used += page_index->page_len;
7018 ctl_set_success(ctsio);
7019 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7020 ctsio->be_move_done = ctl_config_move_done;
7021 ctl_datamove((union ctl_io *)ctsio);
7022 return (CTL_RETVAL_COMPLETE);
7026 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
7027 struct ctl_page_index *page_index,
7030 struct ctl_lun *lun;
7031 struct scsi_log_param_header *phdr;
7035 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7036 data = page_index->page_data;
7038 if (lun->backend->lun_attr != NULL &&
7039 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
7041 phdr = (struct scsi_log_param_header *)data;
7042 scsi_ulto2b(0x0001, phdr->param_code);
7043 phdr->param_control = SLP_LBIN | SLP_LP;
7044 phdr->param_len = 8;
7045 data = (uint8_t *)(phdr + 1);
7046 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
7047 data[4] = 0x02; /* per-pool */
7048 data += phdr->param_len;
7051 if (lun->backend->lun_attr != NULL &&
7052 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
7054 phdr = (struct scsi_log_param_header *)data;
7055 scsi_ulto2b(0x0002, phdr->param_code);
7056 phdr->param_control = SLP_LBIN | SLP_LP;
7057 phdr->param_len = 8;
7058 data = (uint8_t *)(phdr + 1);
7059 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
7060 data[4] = 0x01; /* per-LUN */
7061 data += phdr->param_len;
7064 if (lun->backend->lun_attr != NULL &&
7065 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
7067 phdr = (struct scsi_log_param_header *)data;
7068 scsi_ulto2b(0x00f1, phdr->param_code);
7069 phdr->param_control = SLP_LBIN | SLP_LP;
7070 phdr->param_len = 8;
7071 data = (uint8_t *)(phdr + 1);
7072 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
7073 data[4] = 0x02; /* per-pool */
7074 data += phdr->param_len;
7077 if (lun->backend->lun_attr != NULL &&
7078 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
7080 phdr = (struct scsi_log_param_header *)data;
7081 scsi_ulto2b(0x00f2, phdr->param_code);
7082 phdr->param_control = SLP_LBIN | SLP_LP;
7083 phdr->param_len = 8;
7084 data = (uint8_t *)(phdr + 1);
7085 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
7086 data[4] = 0x02; /* per-pool */
7087 data += phdr->param_len;
7090 page_index->page_len = data - page_index->page_data;
7095 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
7096 struct ctl_page_index *page_index,
7099 struct ctl_lun *lun;
7100 struct stat_page *data;
7101 uint64_t rn, wn, rb, wb;
7102 struct bintime rt, wt;
7105 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7106 data = (struct stat_page *)page_index->page_data;
7108 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
7109 data->sap.hdr.param_control = SLP_LBIN;
7110 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
7111 sizeof(struct scsi_log_param_header);
7112 rn = wn = rb = wb = 0;
7115 for (i = 0; i < CTL_MAX_PORTS; i++) {
7116 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
7117 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
7118 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
7119 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
7120 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
7121 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
7123 scsi_u64to8b(rn, data->sap.read_num);
7124 scsi_u64to8b(wn, data->sap.write_num);
7125 if (lun->stats.blocksize > 0) {
7126 scsi_u64to8b(wb / lun->stats.blocksize,
7127 data->sap.recvieved_lba);
7128 scsi_u64to8b(rb / lun->stats.blocksize,
7129 data->sap.transmitted_lba);
7131 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
7132 data->sap.read_int);
7133 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
7134 data->sap.write_int);
7135 scsi_u64to8b(0, data->sap.weighted_num);
7136 scsi_u64to8b(0, data->sap.weighted_int);
7137 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
7138 data->it.hdr.param_control = SLP_LBIN;
7139 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
7140 sizeof(struct scsi_log_param_header);
7142 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
7144 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
7145 data->it.hdr.param_control = SLP_LBIN;
7146 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
7147 sizeof(struct scsi_log_param_header);
7148 scsi_ulto4b(3, data->ti.exponent);
7149 scsi_ulto4b(1, data->ti.integer);
7151 page_index->page_len = sizeof(*data);
7156 ctl_log_sense(struct ctl_scsiio *ctsio)
7158 struct ctl_lun *lun;
7159 int i, pc, page_code, subpage;
7160 int alloc_len, total_len;
7161 struct ctl_page_index *page_index;
7162 struct scsi_log_sense *cdb;
7163 struct scsi_log_header *header;
7165 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
7167 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7168 cdb = (struct scsi_log_sense *)ctsio->cdb;
7169 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
7170 page_code = cdb->page & SLS_PAGE_CODE;
7171 subpage = cdb->subpage;
7172 alloc_len = scsi_2btoul(cdb->length);
7175 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
7176 page_index = &lun->log_pages.index[i];
7178 /* Look for the right page code */
7179 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
7182 /* Look for the right subpage or the subpage wildcard*/
7183 if (page_index->subpage != subpage)
7188 if (i >= CTL_NUM_LOG_PAGES) {
7189 ctl_set_invalid_field(ctsio,
7195 ctl_done((union ctl_io *)ctsio);
7196 return (CTL_RETVAL_COMPLETE);
7199 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
7201 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7202 ctsio->kern_sg_entries = 0;
7203 ctsio->kern_data_resid = 0;
7204 ctsio->kern_rel_offset = 0;
7205 if (total_len < alloc_len) {
7206 ctsio->residual = alloc_len - total_len;
7207 ctsio->kern_data_len = total_len;
7208 ctsio->kern_total_len = total_len;
7210 ctsio->residual = 0;
7211 ctsio->kern_data_len = alloc_len;
7212 ctsio->kern_total_len = alloc_len;
7215 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
7216 header->page = page_index->page_code;
7217 if (page_index->subpage) {
7218 header->page |= SL_SPF;
7219 header->subpage = page_index->subpage;
7221 scsi_ulto2b(page_index->page_len, header->datalen);
7224 * Call the handler, if it exists, to update the
7225 * page to the latest values.
7227 if (page_index->sense_handler != NULL)
7228 page_index->sense_handler(ctsio, page_index, pc);
7230 memcpy(header + 1, page_index->page_data, page_index->page_len);
7232 ctl_set_success(ctsio);
7233 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7234 ctsio->be_move_done = ctl_config_move_done;
7235 ctl_datamove((union ctl_io *)ctsio);
7236 return (CTL_RETVAL_COMPLETE);
7240 ctl_read_capacity(struct ctl_scsiio *ctsio)
7242 struct scsi_read_capacity *cdb;
7243 struct scsi_read_capacity_data *data;
7244 struct ctl_lun *lun;
7247 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
7249 cdb = (struct scsi_read_capacity *)ctsio->cdb;
7251 lba = scsi_4btoul(cdb->addr);
7252 if (((cdb->pmi & SRC_PMI) == 0)
7254 ctl_set_invalid_field(/*ctsio*/ ctsio,
7260 ctl_done((union ctl_io *)ctsio);
7261 return (CTL_RETVAL_COMPLETE);
7264 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7266 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7267 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7268 ctsio->residual = 0;
7269 ctsio->kern_data_len = sizeof(*data);
7270 ctsio->kern_total_len = sizeof(*data);
7271 ctsio->kern_data_resid = 0;
7272 ctsio->kern_rel_offset = 0;
7273 ctsio->kern_sg_entries = 0;
7276 * If the maximum LBA is greater than 0xfffffffe, the user must
7277 * issue a SERVICE ACTION IN (16) command, with the read capacity
7278 * serivce action set.
7280 if (lun->be_lun->maxlba > 0xfffffffe)
7281 scsi_ulto4b(0xffffffff, data->addr);
7283 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7286 * XXX KDM this may not be 512 bytes...
7288 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7290 ctl_set_success(ctsio);
7291 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7292 ctsio->be_move_done = ctl_config_move_done;
7293 ctl_datamove((union ctl_io *)ctsio);
7294 return (CTL_RETVAL_COMPLETE);
7298 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7300 struct scsi_read_capacity_16 *cdb;
7301 struct scsi_read_capacity_data_long *data;
7302 struct ctl_lun *lun;
7306 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7308 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7310 alloc_len = scsi_4btoul(cdb->alloc_len);
7311 lba = scsi_8btou64(cdb->addr);
7313 if ((cdb->reladr & SRC16_PMI)
7315 ctl_set_invalid_field(/*ctsio*/ ctsio,
7321 ctl_done((union ctl_io *)ctsio);
7322 return (CTL_RETVAL_COMPLETE);
7325 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7327 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7328 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7330 if (sizeof(*data) < alloc_len) {
7331 ctsio->residual = alloc_len - sizeof(*data);
7332 ctsio->kern_data_len = sizeof(*data);
7333 ctsio->kern_total_len = sizeof(*data);
7335 ctsio->residual = 0;
7336 ctsio->kern_data_len = alloc_len;
7337 ctsio->kern_total_len = alloc_len;
7339 ctsio->kern_data_resid = 0;
7340 ctsio->kern_rel_offset = 0;
7341 ctsio->kern_sg_entries = 0;
7343 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7344 /* XXX KDM this may not be 512 bytes... */
7345 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7346 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7347 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7348 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7349 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7351 ctl_set_success(ctsio);
7352 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7353 ctsio->be_move_done = ctl_config_move_done;
7354 ctl_datamove((union ctl_io *)ctsio);
7355 return (CTL_RETVAL_COMPLETE);
7359 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7361 struct scsi_get_lba_status *cdb;
7362 struct scsi_get_lba_status_data *data;
7363 struct ctl_lun *lun;
7364 struct ctl_lba_len_flags *lbalen;
7366 uint32_t alloc_len, total_len;
7369 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7371 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7372 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7373 lba = scsi_8btou64(cdb->addr);
7374 alloc_len = scsi_4btoul(cdb->alloc_len);
7376 if (lba > lun->be_lun->maxlba) {
7377 ctl_set_lba_out_of_range(ctsio);
7378 ctl_done((union ctl_io *)ctsio);
7379 return (CTL_RETVAL_COMPLETE);
7382 total_len = sizeof(*data) + sizeof(data->descr[0]);
7383 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7384 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7386 if (total_len < alloc_len) {
7387 ctsio->residual = alloc_len - total_len;
7388 ctsio->kern_data_len = total_len;
7389 ctsio->kern_total_len = total_len;
7391 ctsio->residual = 0;
7392 ctsio->kern_data_len = alloc_len;
7393 ctsio->kern_total_len = alloc_len;
7395 ctsio->kern_data_resid = 0;
7396 ctsio->kern_rel_offset = 0;
7397 ctsio->kern_sg_entries = 0;
7399 /* Fill dummy data in case backend can't tell anything. */
7400 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7401 scsi_u64to8b(lba, data->descr[0].addr);
7402 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7403 data->descr[0].length);
7404 data->descr[0].status = 0; /* Mapped or unknown. */
7406 ctl_set_success(ctsio);
7407 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7408 ctsio->be_move_done = ctl_config_move_done;
7410 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7412 lbalen->len = total_len;
7414 retval = lun->backend->config_read((union ctl_io *)ctsio);
7415 return (CTL_RETVAL_COMPLETE);
7419 ctl_read_defect(struct ctl_scsiio *ctsio)
7421 struct scsi_read_defect_data_10 *ccb10;
7422 struct scsi_read_defect_data_12 *ccb12;
7423 struct scsi_read_defect_data_hdr_10 *data10;
7424 struct scsi_read_defect_data_hdr_12 *data12;
7425 uint32_t alloc_len, data_len;
7428 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7430 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7431 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7432 format = ccb10->format;
7433 alloc_len = scsi_2btoul(ccb10->alloc_length);
7434 data_len = sizeof(*data10);
7436 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7437 format = ccb12->format;
7438 alloc_len = scsi_4btoul(ccb12->alloc_length);
7439 data_len = sizeof(*data12);
7441 if (alloc_len == 0) {
7442 ctl_set_success(ctsio);
7443 ctl_done((union ctl_io *)ctsio);
7444 return (CTL_RETVAL_COMPLETE);
7447 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7448 if (data_len < alloc_len) {
7449 ctsio->residual = alloc_len - data_len;
7450 ctsio->kern_data_len = data_len;
7451 ctsio->kern_total_len = data_len;
7453 ctsio->residual = 0;
7454 ctsio->kern_data_len = alloc_len;
7455 ctsio->kern_total_len = alloc_len;
7457 ctsio->kern_data_resid = 0;
7458 ctsio->kern_rel_offset = 0;
7459 ctsio->kern_sg_entries = 0;
7461 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7462 data10 = (struct scsi_read_defect_data_hdr_10 *)
7463 ctsio->kern_data_ptr;
7464 data10->format = format;
7465 scsi_ulto2b(0, data10->length);
7467 data12 = (struct scsi_read_defect_data_hdr_12 *)
7468 ctsio->kern_data_ptr;
7469 data12->format = format;
7470 scsi_ulto2b(0, data12->generation);
7471 scsi_ulto4b(0, data12->length);
7474 ctl_set_success(ctsio);
7475 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7476 ctsio->be_move_done = ctl_config_move_done;
7477 ctl_datamove((union ctl_io *)ctsio);
7478 return (CTL_RETVAL_COMPLETE);
7482 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7484 struct scsi_maintenance_in *cdb;
7486 int alloc_len, ext, total_len = 0, g, p, pc, pg, gs, os;
7487 int num_target_port_groups, num_target_ports;
7488 struct ctl_lun *lun;
7489 struct ctl_softc *softc;
7490 struct ctl_port *port;
7491 struct scsi_target_group_data *rtg_ptr;
7492 struct scsi_target_group_data_extended *rtg_ext_ptr;
7493 struct scsi_target_port_group_descriptor *tpg_desc;
7495 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7497 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7498 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7499 softc = lun->ctl_softc;
7501 retval = CTL_RETVAL_COMPLETE;
7503 switch (cdb->byte2 & STG_PDF_MASK) {
7504 case STG_PDF_LENGTH:
7507 case STG_PDF_EXTENDED:
7511 ctl_set_invalid_field(/*ctsio*/ ctsio,
7517 ctl_done((union ctl_io *)ctsio);
7521 if (softc->is_single)
7522 num_target_port_groups = 1;
7524 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7525 num_target_ports = 0;
7526 mtx_lock(&softc->ctl_lock);
7527 STAILQ_FOREACH(port, &softc->port_list, links) {
7528 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7530 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7534 mtx_unlock(&softc->ctl_lock);
7537 total_len = sizeof(struct scsi_target_group_data_extended);
7539 total_len = sizeof(struct scsi_target_group_data);
7540 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7541 num_target_port_groups +
7542 sizeof(struct scsi_target_port_descriptor) *
7543 num_target_ports * num_target_port_groups;
7545 alloc_len = scsi_4btoul(cdb->length);
7547 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7549 ctsio->kern_sg_entries = 0;
7551 if (total_len < alloc_len) {
7552 ctsio->residual = alloc_len - total_len;
7553 ctsio->kern_data_len = total_len;
7554 ctsio->kern_total_len = total_len;
7556 ctsio->residual = 0;
7557 ctsio->kern_data_len = alloc_len;
7558 ctsio->kern_total_len = alloc_len;
7560 ctsio->kern_data_resid = 0;
7561 ctsio->kern_rel_offset = 0;
7564 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7565 ctsio->kern_data_ptr;
7566 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7567 rtg_ext_ptr->format_type = 0x10;
7568 rtg_ext_ptr->implicit_transition_time = 0;
7569 tpg_desc = &rtg_ext_ptr->groups[0];
7571 rtg_ptr = (struct scsi_target_group_data *)
7572 ctsio->kern_data_ptr;
7573 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7574 tpg_desc = &rtg_ptr->groups[0];
7577 mtx_lock(&softc->ctl_lock);
7578 pg = softc->port_offset / CTL_MAX_PORTS;
7579 if (softc->flags & CTL_FLAG_ACTIVE_SHELF) {
7580 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY) {
7581 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7582 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7583 } else if (lun->flags & CTL_LUN_PRIMARY_SC) {
7584 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7585 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7587 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7588 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7591 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7592 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7594 for (g = 0; g < num_target_port_groups; g++) {
7595 tpg_desc->pref_state = (g == pg) ? gs : os;
7596 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP;
7597 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7598 tpg_desc->status = TPG_IMPLICIT;
7600 STAILQ_FOREACH(port, &softc->port_list, links) {
7601 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7603 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7605 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
7606 scsi_ulto2b(p, tpg_desc->descriptors[pc].
7607 relative_target_port_identifier);
7610 tpg_desc->target_port_count = pc;
7611 tpg_desc = (struct scsi_target_port_group_descriptor *)
7612 &tpg_desc->descriptors[pc];
7614 mtx_unlock(&softc->ctl_lock);
7616 ctl_set_success(ctsio);
7617 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7618 ctsio->be_move_done = ctl_config_move_done;
7619 ctl_datamove((union ctl_io *)ctsio);
7624 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7626 struct ctl_lun *lun;
7627 struct scsi_report_supported_opcodes *cdb;
7628 const struct ctl_cmd_entry *entry, *sentry;
7629 struct scsi_report_supported_opcodes_all *all;
7630 struct scsi_report_supported_opcodes_descr *descr;
7631 struct scsi_report_supported_opcodes_one *one;
7633 int alloc_len, total_len;
7634 int opcode, service_action, i, j, num;
7636 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7638 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7639 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7641 retval = CTL_RETVAL_COMPLETE;
7643 opcode = cdb->requested_opcode;
7644 service_action = scsi_2btoul(cdb->requested_service_action);
7645 switch (cdb->options & RSO_OPTIONS_MASK) {
7646 case RSO_OPTIONS_ALL:
7648 for (i = 0; i < 256; i++) {
7649 entry = &ctl_cmd_table[i];
7650 if (entry->flags & CTL_CMD_FLAG_SA5) {
7651 for (j = 0; j < 32; j++) {
7652 sentry = &((const struct ctl_cmd_entry *)
7654 if (ctl_cmd_applicable(
7655 lun->be_lun->lun_type, sentry))
7659 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7664 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7665 num * sizeof(struct scsi_report_supported_opcodes_descr);
7667 case RSO_OPTIONS_OC:
7668 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7669 ctl_set_invalid_field(/*ctsio*/ ctsio,
7675 ctl_done((union ctl_io *)ctsio);
7676 return (CTL_RETVAL_COMPLETE);
7678 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7680 case RSO_OPTIONS_OC_SA:
7681 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7682 service_action >= 32) {
7683 ctl_set_invalid_field(/*ctsio*/ ctsio,
7689 ctl_done((union ctl_io *)ctsio);
7690 return (CTL_RETVAL_COMPLETE);
7692 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7695 ctl_set_invalid_field(/*ctsio*/ ctsio,
7701 ctl_done((union ctl_io *)ctsio);
7702 return (CTL_RETVAL_COMPLETE);
7705 alloc_len = scsi_4btoul(cdb->length);
7707 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7709 ctsio->kern_sg_entries = 0;
7711 if (total_len < alloc_len) {
7712 ctsio->residual = alloc_len - total_len;
7713 ctsio->kern_data_len = total_len;
7714 ctsio->kern_total_len = total_len;
7716 ctsio->residual = 0;
7717 ctsio->kern_data_len = alloc_len;
7718 ctsio->kern_total_len = alloc_len;
7720 ctsio->kern_data_resid = 0;
7721 ctsio->kern_rel_offset = 0;
7723 switch (cdb->options & RSO_OPTIONS_MASK) {
7724 case RSO_OPTIONS_ALL:
7725 all = (struct scsi_report_supported_opcodes_all *)
7726 ctsio->kern_data_ptr;
7728 for (i = 0; i < 256; i++) {
7729 entry = &ctl_cmd_table[i];
7730 if (entry->flags & CTL_CMD_FLAG_SA5) {
7731 for (j = 0; j < 32; j++) {
7732 sentry = &((const struct ctl_cmd_entry *)
7734 if (!ctl_cmd_applicable(
7735 lun->be_lun->lun_type, sentry))
7737 descr = &all->descr[num++];
7739 scsi_ulto2b(j, descr->service_action);
7740 descr->flags = RSO_SERVACTV;
7741 scsi_ulto2b(sentry->length,
7745 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7748 descr = &all->descr[num++];
7750 scsi_ulto2b(0, descr->service_action);
7752 scsi_ulto2b(entry->length, descr->cdb_length);
7756 num * sizeof(struct scsi_report_supported_opcodes_descr),
7759 case RSO_OPTIONS_OC:
7760 one = (struct scsi_report_supported_opcodes_one *)
7761 ctsio->kern_data_ptr;
7762 entry = &ctl_cmd_table[opcode];
7764 case RSO_OPTIONS_OC_SA:
7765 one = (struct scsi_report_supported_opcodes_one *)
7766 ctsio->kern_data_ptr;
7767 entry = &ctl_cmd_table[opcode];
7768 entry = &((const struct ctl_cmd_entry *)
7769 entry->execute)[service_action];
7771 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7773 scsi_ulto2b(entry->length, one->cdb_length);
7774 one->cdb_usage[0] = opcode;
7775 memcpy(&one->cdb_usage[1], entry->usage,
7782 ctl_set_success(ctsio);
7783 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7784 ctsio->be_move_done = ctl_config_move_done;
7785 ctl_datamove((union ctl_io *)ctsio);
7790 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7792 struct scsi_report_supported_tmf *cdb;
7793 struct scsi_report_supported_tmf_data *data;
7795 int alloc_len, total_len;
7797 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7799 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7801 retval = CTL_RETVAL_COMPLETE;
7803 total_len = sizeof(struct scsi_report_supported_tmf_data);
7804 alloc_len = scsi_4btoul(cdb->length);
7806 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7808 ctsio->kern_sg_entries = 0;
7810 if (total_len < alloc_len) {
7811 ctsio->residual = alloc_len - total_len;
7812 ctsio->kern_data_len = total_len;
7813 ctsio->kern_total_len = total_len;
7815 ctsio->residual = 0;
7816 ctsio->kern_data_len = alloc_len;
7817 ctsio->kern_total_len = alloc_len;
7819 ctsio->kern_data_resid = 0;
7820 ctsio->kern_rel_offset = 0;
7822 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7823 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_TRS;
7824 data->byte2 |= RST_ITNRS;
7826 ctl_set_success(ctsio);
7827 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7828 ctsio->be_move_done = ctl_config_move_done;
7829 ctl_datamove((union ctl_io *)ctsio);
7834 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7836 struct scsi_report_timestamp *cdb;
7837 struct scsi_report_timestamp_data *data;
7841 int alloc_len, total_len;
7843 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7845 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7847 retval = CTL_RETVAL_COMPLETE;
7849 total_len = sizeof(struct scsi_report_timestamp_data);
7850 alloc_len = scsi_4btoul(cdb->length);
7852 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7854 ctsio->kern_sg_entries = 0;
7856 if (total_len < alloc_len) {
7857 ctsio->residual = alloc_len - total_len;
7858 ctsio->kern_data_len = total_len;
7859 ctsio->kern_total_len = total_len;
7861 ctsio->residual = 0;
7862 ctsio->kern_data_len = alloc_len;
7863 ctsio->kern_total_len = alloc_len;
7865 ctsio->kern_data_resid = 0;
7866 ctsio->kern_rel_offset = 0;
7868 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7869 scsi_ulto2b(sizeof(*data) - 2, data->length);
7870 data->origin = RTS_ORIG_OUTSIDE;
7872 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7873 scsi_ulto4b(timestamp >> 16, data->timestamp);
7874 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7876 ctl_set_success(ctsio);
7877 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7878 ctsio->be_move_done = ctl_config_move_done;
7879 ctl_datamove((union ctl_io *)ctsio);
7884 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7886 struct scsi_per_res_in *cdb;
7887 int alloc_len, total_len = 0;
7888 /* struct scsi_per_res_in_rsrv in_data; */
7889 struct ctl_lun *lun;
7890 struct ctl_softc *softc;
7893 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7895 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7897 alloc_len = scsi_2btoul(cdb->length);
7899 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7900 softc = lun->ctl_softc;
7903 mtx_lock(&lun->lun_lock);
7904 switch (cdb->action) {
7905 case SPRI_RK: /* read keys */
7906 total_len = sizeof(struct scsi_per_res_in_keys) +
7908 sizeof(struct scsi_per_res_key);
7910 case SPRI_RR: /* read reservation */
7911 if (lun->flags & CTL_LUN_PR_RESERVED)
7912 total_len = sizeof(struct scsi_per_res_in_rsrv);
7914 total_len = sizeof(struct scsi_per_res_in_header);
7916 case SPRI_RC: /* report capabilities */
7917 total_len = sizeof(struct scsi_per_res_cap);
7919 case SPRI_RS: /* read full status */
7920 total_len = sizeof(struct scsi_per_res_in_header) +
7921 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7925 panic("Invalid PR type %x", cdb->action);
7927 mtx_unlock(&lun->lun_lock);
7929 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7931 if (total_len < alloc_len) {
7932 ctsio->residual = alloc_len - total_len;
7933 ctsio->kern_data_len = total_len;
7934 ctsio->kern_total_len = total_len;
7936 ctsio->residual = 0;
7937 ctsio->kern_data_len = alloc_len;
7938 ctsio->kern_total_len = alloc_len;
7941 ctsio->kern_data_resid = 0;
7942 ctsio->kern_rel_offset = 0;
7943 ctsio->kern_sg_entries = 0;
7945 mtx_lock(&lun->lun_lock);
7946 switch (cdb->action) {
7947 case SPRI_RK: { // read keys
7948 struct scsi_per_res_in_keys *res_keys;
7951 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7954 * We had to drop the lock to allocate our buffer, which
7955 * leaves time for someone to come in with another
7956 * persistent reservation. (That is unlikely, though,
7957 * since this should be the only persistent reservation
7958 * command active right now.)
7960 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7961 (lun->pr_key_count *
7962 sizeof(struct scsi_per_res_key)))){
7963 mtx_unlock(&lun->lun_lock);
7964 free(ctsio->kern_data_ptr, M_CTL);
7965 printf("%s: reservation length changed, retrying\n",
7970 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7972 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7973 lun->pr_key_count, res_keys->header.length);
7975 for (i = 0, key_count = 0; i < 2*CTL_MAX_INITIATORS; i++) {
7976 if ((key = ctl_get_prkey(lun, i)) == 0)
7980 * We used lun->pr_key_count to calculate the
7981 * size to allocate. If it turns out the number of
7982 * initiators with the registered flag set is
7983 * larger than that (i.e. they haven't been kept in
7984 * sync), we've got a problem.
7986 if (key_count >= lun->pr_key_count) {
7988 csevent_log(CSC_CTL | CSC_SHELF_SW |
7990 csevent_LogType_Fault,
7991 csevent_AlertLevel_Yellow,
7992 csevent_FRU_ShelfController,
7993 csevent_FRU_Firmware,
7994 csevent_FRU_Unknown,
7995 "registered keys %d >= key "
7996 "count %d", key_count,
8002 scsi_u64to8b(key, res_keys->keys[key_count].key);
8007 case SPRI_RR: { // read reservation
8008 struct scsi_per_res_in_rsrv *res;
8009 int tmp_len, header_only;
8011 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
8013 scsi_ulto4b(lun->PRGeneration, res->header.generation);
8015 if (lun->flags & CTL_LUN_PR_RESERVED)
8017 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
8018 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
8019 res->header.length);
8022 tmp_len = sizeof(struct scsi_per_res_in_header);
8023 scsi_ulto4b(0, res->header.length);
8028 * We had to drop the lock to allocate our buffer, which
8029 * leaves time for someone to come in with another
8030 * persistent reservation. (That is unlikely, though,
8031 * since this should be the only persistent reservation
8032 * command active right now.)
8034 if (tmp_len != total_len) {
8035 mtx_unlock(&lun->lun_lock);
8036 free(ctsio->kern_data_ptr, M_CTL);
8037 printf("%s: reservation status changed, retrying\n",
8043 * No reservation held, so we're done.
8045 if (header_only != 0)
8049 * If the registration is an All Registrants type, the key
8050 * is 0, since it doesn't really matter.
8052 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8053 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
8054 res->data.reservation);
8056 res->data.scopetype = lun->res_type;
8059 case SPRI_RC: //report capabilities
8061 struct scsi_per_res_cap *res_cap;
8064 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
8065 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
8066 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
8067 type_mask = SPRI_TM_WR_EX_AR |
8073 scsi_ulto2b(type_mask, res_cap->type_mask);
8076 case SPRI_RS: { // read full status
8077 struct scsi_per_res_in_full *res_status;
8078 struct scsi_per_res_in_full_desc *res_desc;
8079 struct ctl_port *port;
8082 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
8085 * We had to drop the lock to allocate our buffer, which
8086 * leaves time for someone to come in with another
8087 * persistent reservation. (That is unlikely, though,
8088 * since this should be the only persistent reservation
8089 * command active right now.)
8091 if (total_len < (sizeof(struct scsi_per_res_in_header) +
8092 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
8093 lun->pr_key_count)){
8094 mtx_unlock(&lun->lun_lock);
8095 free(ctsio->kern_data_ptr, M_CTL);
8096 printf("%s: reservation length changed, retrying\n",
8101 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
8103 res_desc = &res_status->desc[0];
8104 for (i = 0; i < 2*CTL_MAX_INITIATORS; i++) {
8105 if ((key = ctl_get_prkey(lun, i)) == 0)
8108 scsi_u64to8b(key, res_desc->res_key.key);
8109 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
8110 (lun->pr_res_idx == i ||
8111 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
8112 res_desc->flags = SPRI_FULL_R_HOLDER;
8113 res_desc->scopetype = lun->res_type;
8115 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
8116 res_desc->rel_trgt_port_id);
8118 port = softc->ctl_ports[
8119 ctl_port_idx(i / CTL_MAX_INIT_PER_PORT)];
8121 len = ctl_create_iid(port,
8122 i % CTL_MAX_INIT_PER_PORT,
8123 res_desc->transport_id);
8124 scsi_ulto4b(len, res_desc->additional_length);
8125 res_desc = (struct scsi_per_res_in_full_desc *)
8126 &res_desc->transport_id[len];
8128 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
8129 res_status->header.length);
8134 * This is a bug, because we just checked for this above,
8135 * and should have returned an error.
8137 panic("Invalid PR type %x", cdb->action);
8138 break; /* NOTREACHED */
8140 mtx_unlock(&lun->lun_lock);
8142 ctl_set_success(ctsio);
8143 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8144 ctsio->be_move_done = ctl_config_move_done;
8145 ctl_datamove((union ctl_io *)ctsio);
8146 return (CTL_RETVAL_COMPLETE);
8150 ctl_est_res_ua(struct ctl_lun *lun, uint32_t residx, ctl_ua_type ua)
8152 int off = lun->ctl_softc->persis_offset;
8154 if (residx >= off && residx < off + CTL_MAX_INITIATORS)
8155 ctl_est_ua(lun, residx - off, ua);
8159 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
8163 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
8164 uint64_t sa_res_key, uint8_t type, uint32_t residx,
8165 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
8166 struct scsi_per_res_out_parms* param)
8168 union ctl_ha_msg persis_io;
8174 mtx_lock(&lun->lun_lock);
8175 if (sa_res_key == 0) {
8176 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8177 /* validate scope and type */
8178 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8180 mtx_unlock(&lun->lun_lock);
8181 ctl_set_invalid_field(/*ctsio*/ ctsio,
8187 ctl_done((union ctl_io *)ctsio);
8191 if (type>8 || type==2 || type==4 || type==0) {
8192 mtx_unlock(&lun->lun_lock);
8193 ctl_set_invalid_field(/*ctsio*/ ctsio,
8199 ctl_done((union ctl_io *)ctsio);
8204 * Unregister everybody else and build UA for
8207 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8208 if (i == residx || ctl_get_prkey(lun, i) == 0)
8211 ctl_clr_prkey(lun, i);
8212 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8214 lun->pr_key_count = 1;
8215 lun->res_type = type;
8216 if (lun->res_type != SPR_TYPE_WR_EX_AR
8217 && lun->res_type != SPR_TYPE_EX_AC_AR)
8218 lun->pr_res_idx = residx;
8220 /* send msg to other side */
8221 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8222 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8223 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8224 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8225 persis_io.pr.pr_info.res_type = type;
8226 memcpy(persis_io.pr.pr_info.sa_res_key,
8227 param->serv_act_res_key,
8228 sizeof(param->serv_act_res_key));
8229 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8230 &persis_io, sizeof(persis_io), 0)) >
8231 CTL_HA_STATUS_SUCCESS) {
8232 printf("CTL:Persis Out error returned "
8233 "from ctl_ha_msg_send %d\n",
8237 /* not all registrants */
8238 mtx_unlock(&lun->lun_lock);
8239 free(ctsio->kern_data_ptr, M_CTL);
8240 ctl_set_invalid_field(ctsio,
8246 ctl_done((union ctl_io *)ctsio);
8249 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8250 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
8253 if (res_key == sa_res_key) {
8256 * The spec implies this is not good but doesn't
8257 * say what to do. There are two choices either
8258 * generate a res conflict or check condition
8259 * with illegal field in parameter data. Since
8260 * that is what is done when the sa_res_key is
8261 * zero I'll take that approach since this has
8262 * to do with the sa_res_key.
8264 mtx_unlock(&lun->lun_lock);
8265 free(ctsio->kern_data_ptr, M_CTL);
8266 ctl_set_invalid_field(ctsio,
8272 ctl_done((union ctl_io *)ctsio);
8276 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8277 if (ctl_get_prkey(lun, i) != sa_res_key)
8281 ctl_clr_prkey(lun, i);
8282 lun->pr_key_count--;
8283 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8286 mtx_unlock(&lun->lun_lock);
8287 free(ctsio->kern_data_ptr, M_CTL);
8288 ctl_set_reservation_conflict(ctsio);
8289 ctl_done((union ctl_io *)ctsio);
8290 return (CTL_RETVAL_COMPLETE);
8292 /* send msg to other side */
8293 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8294 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8295 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8296 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8297 persis_io.pr.pr_info.res_type = type;
8298 memcpy(persis_io.pr.pr_info.sa_res_key,
8299 param->serv_act_res_key,
8300 sizeof(param->serv_act_res_key));
8301 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8302 &persis_io, sizeof(persis_io), 0)) >
8303 CTL_HA_STATUS_SUCCESS) {
8304 printf("CTL:Persis Out error returned from "
8305 "ctl_ha_msg_send %d\n", isc_retval);
8308 /* Reserved but not all registrants */
8309 /* sa_res_key is res holder */
8310 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
8311 /* validate scope and type */
8312 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
8314 mtx_unlock(&lun->lun_lock);
8315 ctl_set_invalid_field(/*ctsio*/ ctsio,
8321 ctl_done((union ctl_io *)ctsio);
8325 if (type>8 || type==2 || type==4 || type==0) {
8326 mtx_unlock(&lun->lun_lock);
8327 ctl_set_invalid_field(/*ctsio*/ ctsio,
8333 ctl_done((union ctl_io *)ctsio);
8339 * if sa_res_key != res_key remove all
8340 * registrants w/sa_res_key and generate UA
8341 * for these registrants(Registrations
8342 * Preempted) if it wasn't an exclusive
8343 * reservation generate UA(Reservations
8344 * Preempted) for all other registered nexuses
8345 * if the type has changed. Establish the new
8346 * reservation and holder. If res_key and
8347 * sa_res_key are the same do the above
8348 * except don't unregister the res holder.
8351 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8352 if (i == residx || ctl_get_prkey(lun, i) == 0)
8355 if (sa_res_key == ctl_get_prkey(lun, i)) {
8356 ctl_clr_prkey(lun, i);
8357 lun->pr_key_count--;
8358 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8359 } else if (type != lun->res_type
8360 && (lun->res_type == SPR_TYPE_WR_EX_RO
8361 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
8362 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE);
8365 lun->res_type = type;
8366 if (lun->res_type != SPR_TYPE_WR_EX_AR
8367 && lun->res_type != SPR_TYPE_EX_AC_AR)
8368 lun->pr_res_idx = residx;
8370 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8372 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8373 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8374 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8375 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8376 persis_io.pr.pr_info.res_type = type;
8377 memcpy(persis_io.pr.pr_info.sa_res_key,
8378 param->serv_act_res_key,
8379 sizeof(param->serv_act_res_key));
8380 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8381 &persis_io, sizeof(persis_io), 0)) >
8382 CTL_HA_STATUS_SUCCESS) {
8383 printf("CTL:Persis Out error returned "
8384 "from ctl_ha_msg_send %d\n",
8389 * sa_res_key is not the res holder just
8390 * remove registrants
8394 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8395 if (sa_res_key != ctl_get_prkey(lun, i))
8399 ctl_clr_prkey(lun, i);
8400 lun->pr_key_count--;
8401 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8405 mtx_unlock(&lun->lun_lock);
8406 free(ctsio->kern_data_ptr, M_CTL);
8407 ctl_set_reservation_conflict(ctsio);
8408 ctl_done((union ctl_io *)ctsio);
8411 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8412 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8413 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8414 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8415 persis_io.pr.pr_info.res_type = type;
8416 memcpy(persis_io.pr.pr_info.sa_res_key,
8417 param->serv_act_res_key,
8418 sizeof(param->serv_act_res_key));
8419 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8420 &persis_io, sizeof(persis_io), 0)) >
8421 CTL_HA_STATUS_SUCCESS) {
8422 printf("CTL:Persis Out error returned "
8423 "from ctl_ha_msg_send %d\n",
8429 lun->PRGeneration++;
8430 mtx_unlock(&lun->lun_lock);
8436 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8438 uint64_t sa_res_key;
8441 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8443 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8444 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8445 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8446 if (sa_res_key == 0) {
8448 * Unregister everybody else and build UA for
8451 for(i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8452 if (i == msg->pr.pr_info.residx ||
8453 ctl_get_prkey(lun, i) == 0)
8456 ctl_clr_prkey(lun, i);
8457 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8460 lun->pr_key_count = 1;
8461 lun->res_type = msg->pr.pr_info.res_type;
8462 if (lun->res_type != SPR_TYPE_WR_EX_AR
8463 && lun->res_type != SPR_TYPE_EX_AC_AR)
8464 lun->pr_res_idx = msg->pr.pr_info.residx;
8466 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8467 if (sa_res_key == ctl_get_prkey(lun, i))
8470 ctl_clr_prkey(lun, i);
8471 lun->pr_key_count--;
8472 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8476 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
8477 if (i == msg->pr.pr_info.residx ||
8478 ctl_get_prkey(lun, i) == 0)
8481 if (sa_res_key == ctl_get_prkey(lun, i)) {
8482 ctl_clr_prkey(lun, i);
8483 lun->pr_key_count--;
8484 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8485 } else if (msg->pr.pr_info.res_type != lun->res_type
8486 && (lun->res_type == SPR_TYPE_WR_EX_RO
8487 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8488 ctl_est_res_ua(lun, i, CTL_UA_RES_RELEASE);
8491 lun->res_type = msg->pr.pr_info.res_type;
8492 if (lun->res_type != SPR_TYPE_WR_EX_AR
8493 && lun->res_type != SPR_TYPE_EX_AC_AR)
8494 lun->pr_res_idx = msg->pr.pr_info.residx;
8496 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8498 lun->PRGeneration++;
8504 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8508 u_int32_t param_len;
8509 struct scsi_per_res_out *cdb;
8510 struct ctl_lun *lun;
8511 struct scsi_per_res_out_parms* param;
8512 struct ctl_softc *softc;
8514 uint64_t res_key, sa_res_key, key;
8516 union ctl_ha_msg persis_io;
8519 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8521 retval = CTL_RETVAL_COMPLETE;
8523 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8524 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8525 softc = lun->ctl_softc;
8528 * We only support whole-LUN scope. The scope & type are ignored for
8529 * register, register and ignore existing key and clear.
8530 * We sometimes ignore scope and type on preempts too!!
8531 * Verify reservation type here as well.
8533 type = cdb->scope_type & SPR_TYPE_MASK;
8534 if ((cdb->action == SPRO_RESERVE)
8535 || (cdb->action == SPRO_RELEASE)) {
8536 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8537 ctl_set_invalid_field(/*ctsio*/ ctsio,
8543 ctl_done((union ctl_io *)ctsio);
8544 return (CTL_RETVAL_COMPLETE);
8547 if (type>8 || type==2 || type==4 || type==0) {
8548 ctl_set_invalid_field(/*ctsio*/ ctsio,
8554 ctl_done((union ctl_io *)ctsio);
8555 return (CTL_RETVAL_COMPLETE);
8559 param_len = scsi_4btoul(cdb->length);
8561 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8562 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8563 ctsio->kern_data_len = param_len;
8564 ctsio->kern_total_len = param_len;
8565 ctsio->kern_data_resid = 0;
8566 ctsio->kern_rel_offset = 0;
8567 ctsio->kern_sg_entries = 0;
8568 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8569 ctsio->be_move_done = ctl_config_move_done;
8570 ctl_datamove((union ctl_io *)ctsio);
8572 return (CTL_RETVAL_COMPLETE);
8575 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8577 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
8578 res_key = scsi_8btou64(param->res_key.key);
8579 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8582 * Validate the reservation key here except for SPRO_REG_IGNO
8583 * This must be done for all other service actions
8585 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8586 mtx_lock(&lun->lun_lock);
8587 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8588 if (res_key != key) {
8590 * The current key passed in doesn't match
8591 * the one the initiator previously
8594 mtx_unlock(&lun->lun_lock);
8595 free(ctsio->kern_data_ptr, M_CTL);
8596 ctl_set_reservation_conflict(ctsio);
8597 ctl_done((union ctl_io *)ctsio);
8598 return (CTL_RETVAL_COMPLETE);
8600 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8602 * We are not registered
8604 mtx_unlock(&lun->lun_lock);
8605 free(ctsio->kern_data_ptr, M_CTL);
8606 ctl_set_reservation_conflict(ctsio);
8607 ctl_done((union ctl_io *)ctsio);
8608 return (CTL_RETVAL_COMPLETE);
8609 } else if (res_key != 0) {
8611 * We are not registered and trying to register but
8612 * the register key isn't zero.
8614 mtx_unlock(&lun->lun_lock);
8615 free(ctsio->kern_data_ptr, M_CTL);
8616 ctl_set_reservation_conflict(ctsio);
8617 ctl_done((union ctl_io *)ctsio);
8618 return (CTL_RETVAL_COMPLETE);
8620 mtx_unlock(&lun->lun_lock);
8623 switch (cdb->action & SPRO_ACTION_MASK) {
8625 case SPRO_REG_IGNO: {
8628 printf("Registration received\n");
8632 * We don't support any of these options, as we report in
8633 * the read capabilities request (see
8634 * ctl_persistent_reserve_in(), above).
8636 if ((param->flags & SPR_SPEC_I_PT)
8637 || (param->flags & SPR_ALL_TG_PT)
8638 || (param->flags & SPR_APTPL)) {
8641 if (param->flags & SPR_APTPL)
8643 else if (param->flags & SPR_ALL_TG_PT)
8645 else /* SPR_SPEC_I_PT */
8648 free(ctsio->kern_data_ptr, M_CTL);
8649 ctl_set_invalid_field(ctsio,
8655 ctl_done((union ctl_io *)ctsio);
8656 return (CTL_RETVAL_COMPLETE);
8659 mtx_lock(&lun->lun_lock);
8662 * The initiator wants to clear the
8665 if (sa_res_key == 0) {
8667 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8668 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8669 && ctl_get_prkey(lun, residx) == 0)) {
8670 mtx_unlock(&lun->lun_lock);
8674 ctl_clr_prkey(lun, residx);
8675 lun->pr_key_count--;
8677 if (residx == lun->pr_res_idx) {
8678 lun->flags &= ~CTL_LUN_PR_RESERVED;
8679 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8681 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8682 || lun->res_type == SPR_TYPE_EX_AC_RO)
8683 && lun->pr_key_count) {
8685 * If the reservation is a registrants
8686 * only type we need to generate a UA
8687 * for other registered inits. The
8688 * sense code should be RESERVATIONS
8692 for (i = 0; i < CTL_MAX_INITIATORS;i++){
8693 if (ctl_get_prkey(lun, i +
8694 softc->persis_offset) == 0)
8697 CTL_UA_RES_RELEASE);
8701 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8702 if (lun->pr_key_count==0) {
8703 lun->flags &= ~CTL_LUN_PR_RESERVED;
8705 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8708 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8709 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8710 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8711 persis_io.pr.pr_info.residx = residx;
8712 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8713 &persis_io, sizeof(persis_io), 0 )) >
8714 CTL_HA_STATUS_SUCCESS) {
8715 printf("CTL:Persis Out error returned from "
8716 "ctl_ha_msg_send %d\n", isc_retval);
8718 } else /* sa_res_key != 0 */ {
8721 * If we aren't registered currently then increment
8722 * the key count and set the registered flag.
8724 ctl_alloc_prkey(lun, residx);
8725 if (ctl_get_prkey(lun, residx) == 0)
8726 lun->pr_key_count++;
8727 ctl_set_prkey(lun, residx, sa_res_key);
8729 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8730 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8731 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8732 persis_io.pr.pr_info.residx = residx;
8733 memcpy(persis_io.pr.pr_info.sa_res_key,
8734 param->serv_act_res_key,
8735 sizeof(param->serv_act_res_key));
8736 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8737 &persis_io, sizeof(persis_io), 0)) >
8738 CTL_HA_STATUS_SUCCESS) {
8739 printf("CTL:Persis Out error returned from "
8740 "ctl_ha_msg_send %d\n", isc_retval);
8743 lun->PRGeneration++;
8744 mtx_unlock(&lun->lun_lock);
8750 printf("Reserve executed type %d\n", type);
8752 mtx_lock(&lun->lun_lock);
8753 if (lun->flags & CTL_LUN_PR_RESERVED) {
8755 * if this isn't the reservation holder and it's
8756 * not a "all registrants" type or if the type is
8757 * different then we have a conflict
8759 if ((lun->pr_res_idx != residx
8760 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8761 || lun->res_type != type) {
8762 mtx_unlock(&lun->lun_lock);
8763 free(ctsio->kern_data_ptr, M_CTL);
8764 ctl_set_reservation_conflict(ctsio);
8765 ctl_done((union ctl_io *)ctsio);
8766 return (CTL_RETVAL_COMPLETE);
8768 mtx_unlock(&lun->lun_lock);
8769 } else /* create a reservation */ {
8771 * If it's not an "all registrants" type record
8772 * reservation holder
8774 if (type != SPR_TYPE_WR_EX_AR
8775 && type != SPR_TYPE_EX_AC_AR)
8776 lun->pr_res_idx = residx; /* Res holder */
8778 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8780 lun->flags |= CTL_LUN_PR_RESERVED;
8781 lun->res_type = type;
8783 mtx_unlock(&lun->lun_lock);
8785 /* send msg to other side */
8786 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8787 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8788 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8789 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8790 persis_io.pr.pr_info.res_type = type;
8791 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
8792 &persis_io, sizeof(persis_io), 0)) >
8793 CTL_HA_STATUS_SUCCESS) {
8794 printf("CTL:Persis Out error returned from "
8795 "ctl_ha_msg_send %d\n", isc_retval);
8801 mtx_lock(&lun->lun_lock);
8802 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8803 /* No reservation exists return good status */
8804 mtx_unlock(&lun->lun_lock);
8808 * Is this nexus a reservation holder?
8810 if (lun->pr_res_idx != residx
8811 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8813 * not a res holder return good status but
8816 mtx_unlock(&lun->lun_lock);
8820 if (lun->res_type != type) {
8821 mtx_unlock(&lun->lun_lock);
8822 free(ctsio->kern_data_ptr, M_CTL);
8823 ctl_set_illegal_pr_release(ctsio);
8824 ctl_done((union ctl_io *)ctsio);
8825 return (CTL_RETVAL_COMPLETE);
8828 /* okay to release */
8829 lun->flags &= ~CTL_LUN_PR_RESERVED;
8830 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8834 * if this isn't an exclusive access
8835 * res generate UA for all other
8838 if (type != SPR_TYPE_EX_AC
8839 && type != SPR_TYPE_WR_EX) {
8840 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8843 i + softc->persis_offset) == 0)
8845 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8848 mtx_unlock(&lun->lun_lock);
8849 /* Send msg to other side */
8850 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8851 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8852 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8853 if ((isc_retval=ctl_ha_msg_send( CTL_HA_CHAN_CTL, &persis_io,
8854 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8855 printf("CTL:Persis Out error returned from "
8856 "ctl_ha_msg_send %d\n", isc_retval);
8861 /* send msg to other side */
8863 mtx_lock(&lun->lun_lock);
8864 lun->flags &= ~CTL_LUN_PR_RESERVED;
8866 lun->pr_key_count = 0;
8867 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8869 ctl_clr_prkey(lun, residx);
8870 for (i=0; i < 2*CTL_MAX_INITIATORS; i++)
8871 if (ctl_get_prkey(lun, i) != 0) {
8872 ctl_clr_prkey(lun, i);
8873 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
8875 lun->PRGeneration++;
8876 mtx_unlock(&lun->lun_lock);
8877 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8878 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8879 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8880 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8881 sizeof(persis_io), 0)) > CTL_HA_STATUS_SUCCESS) {
8882 printf("CTL:Persis Out error returned from "
8883 "ctl_ha_msg_send %d\n", isc_retval);
8888 case SPRO_PRE_ABO: {
8891 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8892 residx, ctsio, cdb, param);
8894 return (CTL_RETVAL_COMPLETE);
8898 panic("Invalid PR type %x", cdb->action);
8902 free(ctsio->kern_data_ptr, M_CTL);
8903 ctl_set_success(ctsio);
8904 ctl_done((union ctl_io *)ctsio);
8910 * This routine is for handling a message from the other SC pertaining to
8911 * persistent reserve out. All the error checking will have been done
8912 * so only perorming the action need be done here to keep the two
8916 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8918 struct ctl_lun *lun;
8919 struct ctl_softc *softc;
8923 softc = control_softc;
8925 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8926 lun = softc->ctl_luns[targ_lun];
8927 mtx_lock(&lun->lun_lock);
8928 switch(msg->pr.pr_info.action) {
8929 case CTL_PR_REG_KEY:
8930 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8931 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8932 lun->pr_key_count++;
8933 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8934 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8935 lun->PRGeneration++;
8938 case CTL_PR_UNREG_KEY:
8939 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8940 lun->pr_key_count--;
8942 /* XXX Need to see if the reservation has been released */
8943 /* if so do we need to generate UA? */
8944 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8945 lun->flags &= ~CTL_LUN_PR_RESERVED;
8946 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8948 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8949 || lun->res_type == SPR_TYPE_EX_AC_RO)
8950 && lun->pr_key_count) {
8952 * If the reservation is a registrants
8953 * only type we need to generate a UA
8954 * for other registered inits. The
8955 * sense code should be RESERVATIONS
8959 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8960 if (ctl_get_prkey(lun, i +
8961 softc->persis_offset) == 0)
8964 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8968 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8969 if (lun->pr_key_count==0) {
8970 lun->flags &= ~CTL_LUN_PR_RESERVED;
8972 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8975 lun->PRGeneration++;
8978 case CTL_PR_RESERVE:
8979 lun->flags |= CTL_LUN_PR_RESERVED;
8980 lun->res_type = msg->pr.pr_info.res_type;
8981 lun->pr_res_idx = msg->pr.pr_info.residx;
8985 case CTL_PR_RELEASE:
8987 * if this isn't an exclusive access res generate UA for all
8988 * other registrants.
8990 if (lun->res_type != SPR_TYPE_EX_AC
8991 && lun->res_type != SPR_TYPE_WR_EX) {
8992 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8993 if (ctl_get_prkey(lun, i + softc->persis_offset) != 0)
8994 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8997 lun->flags &= ~CTL_LUN_PR_RESERVED;
8998 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
9002 case CTL_PR_PREEMPT:
9003 ctl_pro_preempt_other(lun, msg);
9006 lun->flags &= ~CTL_LUN_PR_RESERVED;
9008 lun->pr_key_count = 0;
9009 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
9011 for (i=0; i < 2*CTL_MAX_INITIATORS; i++) {
9012 if (ctl_get_prkey(lun, i) == 0)
9014 ctl_clr_prkey(lun, i);
9015 ctl_est_res_ua(lun, i, CTL_UA_REG_PREEMPT);
9017 lun->PRGeneration++;
9021 mtx_unlock(&lun->lun_lock);
9025 ctl_read_write(struct ctl_scsiio *ctsio)
9027 struct ctl_lun *lun;
9028 struct ctl_lba_len_flags *lbalen;
9030 uint32_t num_blocks;
9034 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9036 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
9039 retval = CTL_RETVAL_COMPLETE;
9041 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
9042 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
9043 switch (ctsio->cdb[0]) {
9046 struct scsi_rw_6 *cdb;
9048 cdb = (struct scsi_rw_6 *)ctsio->cdb;
9050 lba = scsi_3btoul(cdb->addr);
9051 /* only 5 bits are valid in the most significant address byte */
9053 num_blocks = cdb->length;
9055 * This is correct according to SBC-2.
9057 if (num_blocks == 0)
9063 struct scsi_rw_10 *cdb;
9065 cdb = (struct scsi_rw_10 *)ctsio->cdb;
9066 if (cdb->byte2 & SRW10_FUA)
9067 flags |= CTL_LLF_FUA;
9068 if (cdb->byte2 & SRW10_DPO)
9069 flags |= CTL_LLF_DPO;
9070 lba = scsi_4btoul(cdb->addr);
9071 num_blocks = scsi_2btoul(cdb->length);
9074 case WRITE_VERIFY_10: {
9075 struct scsi_write_verify_10 *cdb;
9077 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
9078 flags |= CTL_LLF_FUA;
9079 if (cdb->byte2 & SWV_DPO)
9080 flags |= CTL_LLF_DPO;
9081 lba = scsi_4btoul(cdb->addr);
9082 num_blocks = scsi_2btoul(cdb->length);
9087 struct scsi_rw_12 *cdb;
9089 cdb = (struct scsi_rw_12 *)ctsio->cdb;
9090 if (cdb->byte2 & SRW12_FUA)
9091 flags |= CTL_LLF_FUA;
9092 if (cdb->byte2 & SRW12_DPO)
9093 flags |= CTL_LLF_DPO;
9094 lba = scsi_4btoul(cdb->addr);
9095 num_blocks = scsi_4btoul(cdb->length);
9098 case WRITE_VERIFY_12: {
9099 struct scsi_write_verify_12 *cdb;
9101 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
9102 flags |= CTL_LLF_FUA;
9103 if (cdb->byte2 & SWV_DPO)
9104 flags |= CTL_LLF_DPO;
9105 lba = scsi_4btoul(cdb->addr);
9106 num_blocks = scsi_4btoul(cdb->length);
9111 struct scsi_rw_16 *cdb;
9113 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9114 if (cdb->byte2 & SRW12_FUA)
9115 flags |= CTL_LLF_FUA;
9116 if (cdb->byte2 & SRW12_DPO)
9117 flags |= CTL_LLF_DPO;
9118 lba = scsi_8btou64(cdb->addr);
9119 num_blocks = scsi_4btoul(cdb->length);
9122 case WRITE_ATOMIC_16: {
9123 struct scsi_rw_16 *cdb;
9125 if (lun->be_lun->atomicblock == 0) {
9126 ctl_set_invalid_opcode(ctsio);
9127 ctl_done((union ctl_io *)ctsio);
9128 return (CTL_RETVAL_COMPLETE);
9131 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9132 if (cdb->byte2 & SRW12_FUA)
9133 flags |= CTL_LLF_FUA;
9134 if (cdb->byte2 & SRW12_DPO)
9135 flags |= CTL_LLF_DPO;
9136 lba = scsi_8btou64(cdb->addr);
9137 num_blocks = scsi_4btoul(cdb->length);
9138 if (num_blocks > lun->be_lun->atomicblock) {
9139 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
9140 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
9142 ctl_done((union ctl_io *)ctsio);
9143 return (CTL_RETVAL_COMPLETE);
9147 case WRITE_VERIFY_16: {
9148 struct scsi_write_verify_16 *cdb;
9150 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
9151 flags |= CTL_LLF_FUA;
9152 if (cdb->byte2 & SWV_DPO)
9153 flags |= CTL_LLF_DPO;
9154 lba = scsi_8btou64(cdb->addr);
9155 num_blocks = scsi_4btoul(cdb->length);
9160 * We got a command we don't support. This shouldn't
9161 * happen, commands should be filtered out above us.
9163 ctl_set_invalid_opcode(ctsio);
9164 ctl_done((union ctl_io *)ctsio);
9166 return (CTL_RETVAL_COMPLETE);
9167 break; /* NOTREACHED */
9171 * The first check is to make sure we're in bounds, the second
9172 * check is to catch wrap-around problems. If the lba + num blocks
9173 * is less than the lba, then we've wrapped around and the block
9174 * range is invalid anyway.
9176 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9177 || ((lba + num_blocks) < lba)) {
9178 ctl_set_lba_out_of_range(ctsio);
9179 ctl_done((union ctl_io *)ctsio);
9180 return (CTL_RETVAL_COMPLETE);
9184 * According to SBC-3, a transfer length of 0 is not an error.
9185 * Note that this cannot happen with WRITE(6) or READ(6), since 0
9186 * translates to 256 blocks for those commands.
9188 if (num_blocks == 0) {
9189 ctl_set_success(ctsio);
9190 ctl_done((union ctl_io *)ctsio);
9191 return (CTL_RETVAL_COMPLETE);
9194 /* Set FUA and/or DPO if caches are disabled. */
9196 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9198 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
9200 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9202 flags |= CTL_LLF_FUA;
9205 lbalen = (struct ctl_lba_len_flags *)
9206 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9208 lbalen->len = num_blocks;
9209 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
9211 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9212 ctsio->kern_rel_offset = 0;
9214 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
9216 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9222 ctl_cnw_cont(union ctl_io *io)
9224 struct ctl_scsiio *ctsio;
9225 struct ctl_lun *lun;
9226 struct ctl_lba_len_flags *lbalen;
9229 ctsio = &io->scsiio;
9230 ctsio->io_hdr.status = CTL_STATUS_NONE;
9231 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
9232 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9233 lbalen = (struct ctl_lba_len_flags *)
9234 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9235 lbalen->flags &= ~CTL_LLF_COMPARE;
9236 lbalen->flags |= CTL_LLF_WRITE;
9238 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
9239 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9244 ctl_cnw(struct ctl_scsiio *ctsio)
9246 struct ctl_lun *lun;
9247 struct ctl_lba_len_flags *lbalen;
9249 uint32_t num_blocks;
9252 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9254 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
9257 retval = CTL_RETVAL_COMPLETE;
9259 switch (ctsio->cdb[0]) {
9260 case COMPARE_AND_WRITE: {
9261 struct scsi_compare_and_write *cdb;
9263 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
9264 if (cdb->byte2 & SRW10_FUA)
9265 flags |= CTL_LLF_FUA;
9266 if (cdb->byte2 & SRW10_DPO)
9267 flags |= CTL_LLF_DPO;
9268 lba = scsi_8btou64(cdb->addr);
9269 num_blocks = cdb->length;
9274 * We got a command we don't support. This shouldn't
9275 * happen, commands should be filtered out above us.
9277 ctl_set_invalid_opcode(ctsio);
9278 ctl_done((union ctl_io *)ctsio);
9280 return (CTL_RETVAL_COMPLETE);
9281 break; /* NOTREACHED */
9285 * The first check is to make sure we're in bounds, the second
9286 * check is to catch wrap-around problems. If the lba + num blocks
9287 * is less than the lba, then we've wrapped around and the block
9288 * range is invalid anyway.
9290 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9291 || ((lba + num_blocks) < lba)) {
9292 ctl_set_lba_out_of_range(ctsio);
9293 ctl_done((union ctl_io *)ctsio);
9294 return (CTL_RETVAL_COMPLETE);
9298 * According to SBC-3, a transfer length of 0 is not an error.
9300 if (num_blocks == 0) {
9301 ctl_set_success(ctsio);
9302 ctl_done((union ctl_io *)ctsio);
9303 return (CTL_RETVAL_COMPLETE);
9306 /* Set FUA if write cache is disabled. */
9307 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
9309 flags |= CTL_LLF_FUA;
9311 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
9312 ctsio->kern_rel_offset = 0;
9315 * Set the IO_CONT flag, so that if this I/O gets passed to
9316 * ctl_data_submit_done(), it'll get passed back to
9317 * ctl_ctl_cnw_cont() for further processing.
9319 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
9320 ctsio->io_cont = ctl_cnw_cont;
9322 lbalen = (struct ctl_lba_len_flags *)
9323 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9325 lbalen->len = num_blocks;
9326 lbalen->flags = CTL_LLF_COMPARE | flags;
9328 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
9329 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9334 ctl_verify(struct ctl_scsiio *ctsio)
9336 struct ctl_lun *lun;
9337 struct ctl_lba_len_flags *lbalen;
9339 uint32_t num_blocks;
9343 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9345 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9348 flags = CTL_LLF_FUA;
9349 retval = CTL_RETVAL_COMPLETE;
9351 switch (ctsio->cdb[0]) {
9353 struct scsi_verify_10 *cdb;
9355 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9356 if (cdb->byte2 & SVFY_BYTCHK)
9358 if (cdb->byte2 & SVFY_DPO)
9359 flags |= CTL_LLF_DPO;
9360 lba = scsi_4btoul(cdb->addr);
9361 num_blocks = scsi_2btoul(cdb->length);
9365 struct scsi_verify_12 *cdb;
9367 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9368 if (cdb->byte2 & SVFY_BYTCHK)
9370 if (cdb->byte2 & SVFY_DPO)
9371 flags |= CTL_LLF_DPO;
9372 lba = scsi_4btoul(cdb->addr);
9373 num_blocks = scsi_4btoul(cdb->length);
9377 struct scsi_rw_16 *cdb;
9379 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9380 if (cdb->byte2 & SVFY_BYTCHK)
9382 if (cdb->byte2 & SVFY_DPO)
9383 flags |= CTL_LLF_DPO;
9384 lba = scsi_8btou64(cdb->addr);
9385 num_blocks = scsi_4btoul(cdb->length);
9390 * We got a command we don't support. This shouldn't
9391 * happen, commands should be filtered out above us.
9393 ctl_set_invalid_opcode(ctsio);
9394 ctl_done((union ctl_io *)ctsio);
9395 return (CTL_RETVAL_COMPLETE);
9399 * The first check is to make sure we're in bounds, the second
9400 * check is to catch wrap-around problems. If the lba + num blocks
9401 * is less than the lba, then we've wrapped around and the block
9402 * range is invalid anyway.
9404 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9405 || ((lba + num_blocks) < lba)) {
9406 ctl_set_lba_out_of_range(ctsio);
9407 ctl_done((union ctl_io *)ctsio);
9408 return (CTL_RETVAL_COMPLETE);
9412 * According to SBC-3, a transfer length of 0 is not an error.
9414 if (num_blocks == 0) {
9415 ctl_set_success(ctsio);
9416 ctl_done((union ctl_io *)ctsio);
9417 return (CTL_RETVAL_COMPLETE);
9420 lbalen = (struct ctl_lba_len_flags *)
9421 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9423 lbalen->len = num_blocks;
9425 lbalen->flags = CTL_LLF_COMPARE | flags;
9426 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9428 lbalen->flags = CTL_LLF_VERIFY | flags;
9429 ctsio->kern_total_len = 0;
9431 ctsio->kern_rel_offset = 0;
9433 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9434 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9439 ctl_report_luns(struct ctl_scsiio *ctsio)
9441 struct ctl_softc *softc = control_softc;
9442 struct scsi_report_luns *cdb;
9443 struct scsi_report_luns_data *lun_data;
9444 struct ctl_lun *lun, *request_lun;
9445 struct ctl_port *port;
9446 int num_luns, retval;
9447 uint32_t alloc_len, lun_datalen;
9448 int num_filled, well_known;
9449 uint32_t initidx, targ_lun_id, lun_id;
9451 retval = CTL_RETVAL_COMPLETE;
9454 cdb = (struct scsi_report_luns *)ctsio->cdb;
9455 port = ctl_io_port(&ctsio->io_hdr);
9457 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9459 mtx_lock(&softc->ctl_lock);
9461 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
9462 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
9465 mtx_unlock(&softc->ctl_lock);
9467 switch (cdb->select_report) {
9468 case RPL_REPORT_DEFAULT:
9469 case RPL_REPORT_ALL:
9471 case RPL_REPORT_WELLKNOWN:
9476 ctl_set_invalid_field(ctsio,
9482 ctl_done((union ctl_io *)ctsio);
9484 break; /* NOTREACHED */
9487 alloc_len = scsi_4btoul(cdb->length);
9489 * The initiator has to allocate at least 16 bytes for this request,
9490 * so he can at least get the header and the first LUN. Otherwise
9491 * we reject the request (per SPC-3 rev 14, section 6.21).
9493 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9494 sizeof(struct scsi_report_luns_lundata))) {
9495 ctl_set_invalid_field(ctsio,
9501 ctl_done((union ctl_io *)ctsio);
9505 request_lun = (struct ctl_lun *)
9506 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9508 lun_datalen = sizeof(*lun_data) +
9509 (num_luns * sizeof(struct scsi_report_luns_lundata));
9511 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9512 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9513 ctsio->kern_sg_entries = 0;
9515 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9517 mtx_lock(&softc->ctl_lock);
9518 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9519 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9520 if (lun_id >= CTL_MAX_LUNS)
9522 lun = softc->ctl_luns[lun_id];
9526 if (targ_lun_id <= 0xff) {
9528 * Peripheral addressing method, bus number 0.
9530 lun_data->luns[num_filled].lundata[0] =
9531 RPL_LUNDATA_ATYP_PERIPH;
9532 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9534 } else if (targ_lun_id <= 0x3fff) {
9536 * Flat addressing method.
9538 lun_data->luns[num_filled].lundata[0] =
9539 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9540 lun_data->luns[num_filled].lundata[1] =
9541 (targ_lun_id & 0xff);
9543 } else if (targ_lun_id <= 0xffffff) {
9545 * Extended flat addressing method.
9547 lun_data->luns[num_filled].lundata[0] =
9548 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9549 scsi_ulto3b(targ_lun_id,
9550 &lun_data->luns[num_filled].lundata[1]);
9553 printf("ctl_report_luns: bogus LUN number %jd, "
9554 "skipping\n", (intmax_t)targ_lun_id);
9557 * According to SPC-3, rev 14 section 6.21:
9559 * "The execution of a REPORT LUNS command to any valid and
9560 * installed logical unit shall clear the REPORTED LUNS DATA
9561 * HAS CHANGED unit attention condition for all logical
9562 * units of that target with respect to the requesting
9563 * initiator. A valid and installed logical unit is one
9564 * having a PERIPHERAL QUALIFIER of 000b in the standard
9565 * INQUIRY data (see 6.4.2)."
9567 * If request_lun is NULL, the LUN this report luns command
9568 * was issued to is either disabled or doesn't exist. In that
9569 * case, we shouldn't clear any pending lun change unit
9572 if (request_lun != NULL) {
9573 mtx_lock(&lun->lun_lock);
9574 ctl_clr_ua(lun, initidx, CTL_UA_RES_RELEASE);
9575 mtx_unlock(&lun->lun_lock);
9578 mtx_unlock(&softc->ctl_lock);
9581 * It's quite possible that we've returned fewer LUNs than we allocated
9582 * space for. Trim it.
9584 lun_datalen = sizeof(*lun_data) +
9585 (num_filled * sizeof(struct scsi_report_luns_lundata));
9587 if (lun_datalen < alloc_len) {
9588 ctsio->residual = alloc_len - lun_datalen;
9589 ctsio->kern_data_len = lun_datalen;
9590 ctsio->kern_total_len = lun_datalen;
9592 ctsio->residual = 0;
9593 ctsio->kern_data_len = alloc_len;
9594 ctsio->kern_total_len = alloc_len;
9596 ctsio->kern_data_resid = 0;
9597 ctsio->kern_rel_offset = 0;
9598 ctsio->kern_sg_entries = 0;
9601 * We set this to the actual data length, regardless of how much
9602 * space we actually have to return results. If the user looks at
9603 * this value, he'll know whether or not he allocated enough space
9604 * and reissue the command if necessary. We don't support well
9605 * known logical units, so if the user asks for that, return none.
9607 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9610 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9613 ctl_set_success(ctsio);
9614 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9615 ctsio->be_move_done = ctl_config_move_done;
9616 ctl_datamove((union ctl_io *)ctsio);
9621 ctl_request_sense(struct ctl_scsiio *ctsio)
9623 struct scsi_request_sense *cdb;
9624 struct scsi_sense_data *sense_ptr;
9625 struct ctl_softc *ctl_softc;
9626 struct ctl_lun *lun;
9629 scsi_sense_data_type sense_format;
9630 ctl_ua_type ua_type;
9632 cdb = (struct scsi_request_sense *)ctsio->cdb;
9634 ctl_softc = control_softc;
9635 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9637 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9640 * Determine which sense format the user wants.
9642 if (cdb->byte2 & SRS_DESC)
9643 sense_format = SSD_TYPE_DESC;
9645 sense_format = SSD_TYPE_FIXED;
9647 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9648 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9649 ctsio->kern_sg_entries = 0;
9652 * struct scsi_sense_data, which is currently set to 256 bytes, is
9653 * larger than the largest allowed value for the length field in the
9654 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9656 ctsio->residual = 0;
9657 ctsio->kern_data_len = cdb->length;
9658 ctsio->kern_total_len = cdb->length;
9660 ctsio->kern_data_resid = 0;
9661 ctsio->kern_rel_offset = 0;
9662 ctsio->kern_sg_entries = 0;
9665 * If we don't have a LUN, we don't have any pending sense.
9671 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9673 * Check for pending sense, and then for pending unit attentions.
9674 * Pending sense gets returned first, then pending unit attentions.
9676 mtx_lock(&lun->lun_lock);
9678 if (ctl_is_set(lun->have_ca, initidx)) {
9679 scsi_sense_data_type stored_format;
9682 * Check to see which sense format was used for the stored
9685 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9688 * If the user requested a different sense format than the
9689 * one we stored, then we need to convert it to the other
9690 * format. If we're going from descriptor to fixed format
9691 * sense data, we may lose things in translation, depending
9692 * on what options were used.
9694 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9695 * for some reason we'll just copy it out as-is.
9697 if ((stored_format == SSD_TYPE_FIXED)
9698 && (sense_format == SSD_TYPE_DESC))
9699 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9700 &lun->pending_sense[initidx],
9701 (struct scsi_sense_data_desc *)sense_ptr);
9702 else if ((stored_format == SSD_TYPE_DESC)
9703 && (sense_format == SSD_TYPE_FIXED))
9704 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9705 &lun->pending_sense[initidx],
9706 (struct scsi_sense_data_fixed *)sense_ptr);
9708 memcpy(sense_ptr, &lun->pending_sense[initidx],
9709 MIN(sizeof(*sense_ptr),
9710 sizeof(lun->pending_sense[initidx])));
9712 ctl_clear_mask(lun->have_ca, initidx);
9717 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9718 if (ua_type != CTL_UA_NONE)
9720 if (ua_type == CTL_UA_LUN_CHANGE) {
9721 mtx_unlock(&lun->lun_lock);
9722 mtx_lock(&ctl_softc->ctl_lock);
9723 ctl_clear_ua(ctl_softc, initidx, ua_type);
9724 mtx_unlock(&ctl_softc->ctl_lock);
9725 mtx_lock(&lun->lun_lock);
9729 mtx_unlock(&lun->lun_lock);
9732 * We already have a pending error, return it.
9734 if (have_error != 0) {
9736 * We report the SCSI status as OK, since the status of the
9737 * request sense command itself is OK.
9738 * We report 0 for the sense length, because we aren't doing
9739 * autosense in this case. We're reporting sense as
9742 ctl_set_success(ctsio);
9743 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9744 ctsio->be_move_done = ctl_config_move_done;
9745 ctl_datamove((union ctl_io *)ctsio);
9746 return (CTL_RETVAL_COMPLETE);
9752 * No sense information to report, so we report that everything is
9755 ctl_set_sense_data(sense_ptr,
9758 /*current_error*/ 1,
9759 /*sense_key*/ SSD_KEY_NO_SENSE,
9765 * We report 0 for the sense length, because we aren't doing
9766 * autosense in this case. We're reporting sense as parameter data.
9768 ctl_set_success(ctsio);
9769 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9770 ctsio->be_move_done = ctl_config_move_done;
9771 ctl_datamove((union ctl_io *)ctsio);
9772 return (CTL_RETVAL_COMPLETE);
9776 ctl_tur(struct ctl_scsiio *ctsio)
9779 CTL_DEBUG_PRINT(("ctl_tur\n"));
9781 ctl_set_success(ctsio);
9782 ctl_done((union ctl_io *)ctsio);
9784 return (CTL_RETVAL_COMPLETE);
9789 ctl_cmddt_inquiry(struct ctl_scsiio *ctsio)
9796 * SCSI VPD page 0x00, the Supported VPD Pages page.
9799 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9801 struct scsi_vpd_supported_pages *pages;
9803 struct ctl_lun *lun;
9806 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9808 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9809 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9810 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9811 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9812 ctsio->kern_sg_entries = 0;
9814 if (sup_page_size < alloc_len) {
9815 ctsio->residual = alloc_len - sup_page_size;
9816 ctsio->kern_data_len = sup_page_size;
9817 ctsio->kern_total_len = sup_page_size;
9819 ctsio->residual = 0;
9820 ctsio->kern_data_len = alloc_len;
9821 ctsio->kern_total_len = alloc_len;
9823 ctsio->kern_data_resid = 0;
9824 ctsio->kern_rel_offset = 0;
9825 ctsio->kern_sg_entries = 0;
9828 * The control device is always connected. The disk device, on the
9829 * other hand, may not be online all the time. Need to change this
9830 * to figure out whether the disk device is actually online or not.
9833 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9834 lun->be_lun->lun_type;
9836 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9839 /* Supported VPD pages */
9840 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9842 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9843 /* Device Identification */
9844 pages->page_list[p++] = SVPD_DEVICE_ID;
9845 /* Extended INQUIRY Data */
9846 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9847 /* Mode Page Policy */
9848 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9850 pages->page_list[p++] = SVPD_SCSI_PORTS;
9851 /* Third-party Copy */
9852 pages->page_list[p++] = SVPD_SCSI_TPC;
9853 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9855 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9856 /* Block Device Characteristics */
9857 pages->page_list[p++] = SVPD_BDC;
9858 /* Logical Block Provisioning */
9859 pages->page_list[p++] = SVPD_LBP;
9863 ctl_set_success(ctsio);
9864 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9865 ctsio->be_move_done = ctl_config_move_done;
9866 ctl_datamove((union ctl_io *)ctsio);
9867 return (CTL_RETVAL_COMPLETE);
9871 * SCSI VPD page 0x80, the Unit Serial Number page.
9874 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9876 struct scsi_vpd_unit_serial_number *sn_ptr;
9877 struct ctl_lun *lun;
9880 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9882 data_len = 4 + CTL_SN_LEN;
9883 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9884 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9885 if (data_len < alloc_len) {
9886 ctsio->residual = alloc_len - data_len;
9887 ctsio->kern_data_len = data_len;
9888 ctsio->kern_total_len = data_len;
9890 ctsio->residual = 0;
9891 ctsio->kern_data_len = alloc_len;
9892 ctsio->kern_total_len = alloc_len;
9894 ctsio->kern_data_resid = 0;
9895 ctsio->kern_rel_offset = 0;
9896 ctsio->kern_sg_entries = 0;
9899 * The control device is always connected. The disk device, on the
9900 * other hand, may not be online all the time. Need to change this
9901 * to figure out whether the disk device is actually online or not.
9904 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9905 lun->be_lun->lun_type;
9907 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9909 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9910 sn_ptr->length = CTL_SN_LEN;
9912 * If we don't have a LUN, we just leave the serial number as
9916 strncpy((char *)sn_ptr->serial_num,
9917 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9919 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9921 ctl_set_success(ctsio);
9922 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9923 ctsio->be_move_done = ctl_config_move_done;
9924 ctl_datamove((union ctl_io *)ctsio);
9925 return (CTL_RETVAL_COMPLETE);
9930 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9933 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9935 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9936 struct ctl_lun *lun;
9939 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9941 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9942 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9943 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9944 ctsio->kern_sg_entries = 0;
9946 if (data_len < alloc_len) {
9947 ctsio->residual = alloc_len - data_len;
9948 ctsio->kern_data_len = data_len;
9949 ctsio->kern_total_len = data_len;
9951 ctsio->residual = 0;
9952 ctsio->kern_data_len = alloc_len;
9953 ctsio->kern_total_len = alloc_len;
9955 ctsio->kern_data_resid = 0;
9956 ctsio->kern_rel_offset = 0;
9957 ctsio->kern_sg_entries = 0;
9960 * The control device is always connected. The disk device, on the
9961 * other hand, may not be online all the time.
9964 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9965 lun->be_lun->lun_type;
9967 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9968 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9969 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9971 * We support head of queue, ordered and simple tags.
9973 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9975 * Volatile cache supported.
9977 eid_ptr->flags3 = SVPD_EID_V_SUP;
9980 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9981 * attention for a particular IT nexus on all LUNs once we report
9982 * it to that nexus once. This bit is required as of SPC-4.
9984 eid_ptr->flags4 = SVPD_EID_LUICLT;
9987 * XXX KDM in order to correctly answer this, we would need
9988 * information from the SIM to determine how much sense data it
9989 * can send. So this would really be a path inquiry field, most
9990 * likely. This can be set to a maximum of 252 according to SPC-4,
9991 * but the hardware may or may not be able to support that much.
9992 * 0 just means that the maximum sense data length is not reported.
9994 eid_ptr->max_sense_length = 0;
9996 ctl_set_success(ctsio);
9997 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9998 ctsio->be_move_done = ctl_config_move_done;
9999 ctl_datamove((union ctl_io *)ctsio);
10000 return (CTL_RETVAL_COMPLETE);
10004 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
10006 struct scsi_vpd_mode_page_policy *mpp_ptr;
10007 struct ctl_lun *lun;
10010 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10012 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
10013 sizeof(struct scsi_vpd_mode_page_policy_descr);
10015 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10016 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
10017 ctsio->kern_sg_entries = 0;
10019 if (data_len < alloc_len) {
10020 ctsio->residual = alloc_len - data_len;
10021 ctsio->kern_data_len = data_len;
10022 ctsio->kern_total_len = data_len;
10024 ctsio->residual = 0;
10025 ctsio->kern_data_len = alloc_len;
10026 ctsio->kern_total_len = alloc_len;
10028 ctsio->kern_data_resid = 0;
10029 ctsio->kern_rel_offset = 0;
10030 ctsio->kern_sg_entries = 0;
10033 * The control device is always connected. The disk device, on the
10034 * other hand, may not be online all the time.
10037 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10038 lun->be_lun->lun_type;
10040 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10041 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
10042 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
10043 mpp_ptr->descr[0].page_code = 0x3f;
10044 mpp_ptr->descr[0].subpage_code = 0xff;
10045 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
10047 ctl_set_success(ctsio);
10048 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10049 ctsio->be_move_done = ctl_config_move_done;
10050 ctl_datamove((union ctl_io *)ctsio);
10051 return (CTL_RETVAL_COMPLETE);
10055 * SCSI VPD page 0x83, the Device Identification page.
10058 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
10060 struct scsi_vpd_device_id *devid_ptr;
10061 struct scsi_vpd_id_descriptor *desc;
10062 struct ctl_softc *softc;
10063 struct ctl_lun *lun;
10064 struct ctl_port *port;
10068 softc = control_softc;
10070 port = softc->ctl_ports[ctl_port_idx(ctsio->io_hdr.nexus.targ_port)];
10071 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10073 data_len = sizeof(struct scsi_vpd_device_id) +
10074 sizeof(struct scsi_vpd_id_descriptor) +
10075 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
10076 sizeof(struct scsi_vpd_id_descriptor) +
10077 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
10078 if (lun && lun->lun_devid)
10079 data_len += lun->lun_devid->len;
10080 if (port->port_devid)
10081 data_len += port->port_devid->len;
10082 if (port->target_devid)
10083 data_len += port->target_devid->len;
10085 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10086 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
10087 ctsio->kern_sg_entries = 0;
10089 if (data_len < alloc_len) {
10090 ctsio->residual = alloc_len - data_len;
10091 ctsio->kern_data_len = data_len;
10092 ctsio->kern_total_len = data_len;
10094 ctsio->residual = 0;
10095 ctsio->kern_data_len = alloc_len;
10096 ctsio->kern_total_len = alloc_len;
10098 ctsio->kern_data_resid = 0;
10099 ctsio->kern_rel_offset = 0;
10100 ctsio->kern_sg_entries = 0;
10103 * The control device is always connected. The disk device, on the
10104 * other hand, may not be online all the time.
10107 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10108 lun->be_lun->lun_type;
10110 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10111 devid_ptr->page_code = SVPD_DEVICE_ID;
10112 scsi_ulto2b(data_len - 4, devid_ptr->length);
10114 if (port->port_type == CTL_PORT_FC)
10115 proto = SCSI_PROTO_FC << 4;
10116 else if (port->port_type == CTL_PORT_ISCSI)
10117 proto = SCSI_PROTO_ISCSI << 4;
10119 proto = SCSI_PROTO_SPI << 4;
10120 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
10123 * We're using a LUN association here. i.e., this device ID is a
10124 * per-LUN identifier.
10126 if (lun && lun->lun_devid) {
10127 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
10128 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10129 lun->lun_devid->len);
10133 * This is for the WWPN which is a port association.
10135 if (port->port_devid) {
10136 memcpy(desc, port->port_devid->data, port->port_devid->len);
10137 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
10138 port->port_devid->len);
10142 * This is for the Relative Target Port(type 4h) identifier
10144 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10145 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10146 SVPD_ID_TYPE_RELTARG;
10148 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
10149 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10150 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
10153 * This is for the Target Port Group(type 5h) identifier
10155 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
10156 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
10157 SVPD_ID_TYPE_TPORTGRP;
10159 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / CTL_MAX_PORTS + 1,
10160 &desc->identifier[2]);
10161 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
10162 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
10165 * This is for the Target identifier
10167 if (port->target_devid) {
10168 memcpy(desc, port->target_devid->data, port->target_devid->len);
10171 ctl_set_success(ctsio);
10172 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10173 ctsio->be_move_done = ctl_config_move_done;
10174 ctl_datamove((union ctl_io *)ctsio);
10175 return (CTL_RETVAL_COMPLETE);
10179 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
10181 struct ctl_softc *softc = control_softc;
10182 struct scsi_vpd_scsi_ports *sp;
10183 struct scsi_vpd_port_designation *pd;
10184 struct scsi_vpd_port_designation_cont *pdc;
10185 struct ctl_lun *lun;
10186 struct ctl_port *port;
10187 int data_len, num_target_ports, iid_len, id_len, g, pg, p;
10188 int num_target_port_groups;
10190 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10192 if (softc->is_single)
10193 num_target_port_groups = 1;
10195 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
10196 num_target_ports = 0;
10199 mtx_lock(&softc->ctl_lock);
10200 STAILQ_FOREACH(port, &softc->port_list, links) {
10201 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10204 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
10206 num_target_ports++;
10207 if (port->init_devid)
10208 iid_len += port->init_devid->len;
10209 if (port->port_devid)
10210 id_len += port->port_devid->len;
10212 mtx_unlock(&softc->ctl_lock);
10214 data_len = sizeof(struct scsi_vpd_scsi_ports) + num_target_port_groups *
10215 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
10216 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
10217 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10218 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
10219 ctsio->kern_sg_entries = 0;
10221 if (data_len < alloc_len) {
10222 ctsio->residual = alloc_len - data_len;
10223 ctsio->kern_data_len = data_len;
10224 ctsio->kern_total_len = data_len;
10226 ctsio->residual = 0;
10227 ctsio->kern_data_len = alloc_len;
10228 ctsio->kern_total_len = alloc_len;
10230 ctsio->kern_data_resid = 0;
10231 ctsio->kern_rel_offset = 0;
10232 ctsio->kern_sg_entries = 0;
10235 * The control device is always connected. The disk device, on the
10236 * other hand, may not be online all the time. Need to change this
10237 * to figure out whether the disk device is actually online or not.
10240 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
10241 lun->be_lun->lun_type;
10243 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10245 sp->page_code = SVPD_SCSI_PORTS;
10246 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
10248 pd = &sp->design[0];
10250 mtx_lock(&softc->ctl_lock);
10251 pg = softc->port_offset / CTL_MAX_PORTS;
10252 for (g = 0; g < num_target_port_groups; g++) {
10253 STAILQ_FOREACH(port, &softc->port_list, links) {
10254 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
10257 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
10259 p = port->targ_port % CTL_MAX_PORTS + g * CTL_MAX_PORTS;
10260 scsi_ulto2b(p, pd->relative_port_id);
10261 if (port->init_devid && g == pg) {
10262 iid_len = port->init_devid->len;
10263 memcpy(pd->initiator_transportid,
10264 port->init_devid->data, port->init_devid->len);
10267 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
10268 pdc = (struct scsi_vpd_port_designation_cont *)
10269 (&pd->initiator_transportid[iid_len]);
10270 if (port->port_devid && g == pg) {
10271 id_len = port->port_devid->len;
10272 memcpy(pdc->target_port_descriptors,
10273 port->port_devid->data, port->port_devid->len);
10276 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
10277 pd = (struct scsi_vpd_port_designation *)
10278 ((uint8_t *)pdc->target_port_descriptors + id_len);
10281 mtx_unlock(&softc->ctl_lock);
10283 ctl_set_success(ctsio);
10284 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10285 ctsio->be_move_done = ctl_config_move_done;
10286 ctl_datamove((union ctl_io *)ctsio);
10287 return (CTL_RETVAL_COMPLETE);
10291 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
10293 struct scsi_vpd_block_limits *bl_ptr;
10294 struct ctl_lun *lun;
10297 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10299 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
10300 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
10301 ctsio->kern_sg_entries = 0;
10303 if (sizeof(*bl_ptr) < alloc_len) {
10304 ctsio->residual = alloc_len - sizeof(*bl_ptr);
10305 ctsio->kern_data_len = sizeof(*bl_ptr);
10306 ctsio->kern_total_len = sizeof(*bl_ptr);
10308 ctsio->residual = 0;
10309 ctsio->kern_data_len = alloc_len;
10310 ctsio->kern_total_len = alloc_len;
10312 ctsio->kern_data_resid = 0;
10313 ctsio->kern_rel_offset = 0;
10314 ctsio->kern_sg_entries = 0;
10317 * The control device is always connected. The disk device, on the
10318 * other hand, may not be online all the time. Need to change this
10319 * to figure out whether the disk device is actually online or not.
10322 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10323 lun->be_lun->lun_type;
10325 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10327 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
10328 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
10329 bl_ptr->max_cmp_write_len = 0xff;
10330 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
10332 bs = lun->be_lun->blocksize;
10333 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
10334 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10335 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
10336 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
10337 if (lun->be_lun->ublockexp != 0) {
10338 scsi_ulto4b((1 << lun->be_lun->ublockexp),
10339 bl_ptr->opt_unmap_grain);
10340 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
10341 bl_ptr->unmap_grain_align);
10344 scsi_ulto4b(lun->be_lun->atomicblock,
10345 bl_ptr->max_atomic_transfer_length);
10346 scsi_ulto4b(0, bl_ptr->atomic_alignment);
10347 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
10349 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
10351 ctl_set_success(ctsio);
10352 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10353 ctsio->be_move_done = ctl_config_move_done;
10354 ctl_datamove((union ctl_io *)ctsio);
10355 return (CTL_RETVAL_COMPLETE);
10359 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
10361 struct scsi_vpd_block_device_characteristics *bdc_ptr;
10362 struct ctl_lun *lun;
10366 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10368 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
10369 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
10370 ctsio->kern_sg_entries = 0;
10372 if (sizeof(*bdc_ptr) < alloc_len) {
10373 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
10374 ctsio->kern_data_len = sizeof(*bdc_ptr);
10375 ctsio->kern_total_len = sizeof(*bdc_ptr);
10377 ctsio->residual = 0;
10378 ctsio->kern_data_len = alloc_len;
10379 ctsio->kern_total_len = alloc_len;
10381 ctsio->kern_data_resid = 0;
10382 ctsio->kern_rel_offset = 0;
10383 ctsio->kern_sg_entries = 0;
10386 * The control device is always connected. The disk device, on the
10387 * other hand, may not be online all the time. Need to change this
10388 * to figure out whether the disk device is actually online or not.
10391 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10392 lun->be_lun->lun_type;
10394 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10395 bdc_ptr->page_code = SVPD_BDC;
10396 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10398 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10399 i = strtol(value, NULL, 0);
10401 i = CTL_DEFAULT_ROTATION_RATE;
10402 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10404 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10405 i = strtol(value, NULL, 0);
10408 bdc_ptr->wab_wac_ff = (i & 0x0f);
10409 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10411 ctl_set_success(ctsio);
10412 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10413 ctsio->be_move_done = ctl_config_move_done;
10414 ctl_datamove((union ctl_io *)ctsio);
10415 return (CTL_RETVAL_COMPLETE);
10419 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10421 struct scsi_vpd_logical_block_prov *lbp_ptr;
10422 struct ctl_lun *lun;
10424 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10426 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10427 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10428 ctsio->kern_sg_entries = 0;
10430 if (sizeof(*lbp_ptr) < alloc_len) {
10431 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10432 ctsio->kern_data_len = sizeof(*lbp_ptr);
10433 ctsio->kern_total_len = sizeof(*lbp_ptr);
10435 ctsio->residual = 0;
10436 ctsio->kern_data_len = alloc_len;
10437 ctsio->kern_total_len = alloc_len;
10439 ctsio->kern_data_resid = 0;
10440 ctsio->kern_rel_offset = 0;
10441 ctsio->kern_sg_entries = 0;
10444 * The control device is always connected. The disk device, on the
10445 * other hand, may not be online all the time. Need to change this
10446 * to figure out whether the disk device is actually online or not.
10449 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10450 lun->be_lun->lun_type;
10452 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10454 lbp_ptr->page_code = SVPD_LBP;
10455 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10456 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10457 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10458 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10459 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10460 lbp_ptr->prov_type = SVPD_LBP_THIN;
10463 ctl_set_success(ctsio);
10464 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10465 ctsio->be_move_done = ctl_config_move_done;
10466 ctl_datamove((union ctl_io *)ctsio);
10467 return (CTL_RETVAL_COMPLETE);
10471 * INQUIRY with the EVPD bit set.
10474 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10476 struct ctl_lun *lun;
10477 struct scsi_inquiry *cdb;
10478 int alloc_len, retval;
10480 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10481 cdb = (struct scsi_inquiry *)ctsio->cdb;
10482 alloc_len = scsi_2btoul(cdb->length);
10484 switch (cdb->page_code) {
10485 case SVPD_SUPPORTED_PAGES:
10486 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10488 case SVPD_UNIT_SERIAL_NUMBER:
10489 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10491 case SVPD_DEVICE_ID:
10492 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10494 case SVPD_EXTENDED_INQUIRY_DATA:
10495 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10497 case SVPD_MODE_PAGE_POLICY:
10498 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10500 case SVPD_SCSI_PORTS:
10501 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10503 case SVPD_SCSI_TPC:
10504 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10506 case SVPD_BLOCK_LIMITS:
10507 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10509 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10512 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10514 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10517 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10519 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10523 ctl_set_invalid_field(ctsio,
10529 ctl_done((union ctl_io *)ctsio);
10530 retval = CTL_RETVAL_COMPLETE;
10538 * Standard INQUIRY data.
10541 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10543 struct scsi_inquiry_data *inq_ptr;
10544 struct scsi_inquiry *cdb;
10545 struct ctl_softc *softc;
10546 struct ctl_lun *lun;
10548 uint32_t alloc_len, data_len;
10549 ctl_port_type port_type;
10551 softc = control_softc;
10554 * Figure out whether we're talking to a Fibre Channel port or not.
10555 * We treat the ioctl front end, and any SCSI adapters, as packetized
10558 port_type = softc->ctl_ports[
10559 ctl_port_idx(ctsio->io_hdr.nexus.targ_port)]->port_type;
10560 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10561 port_type = CTL_PORT_SCSI;
10563 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10564 cdb = (struct scsi_inquiry *)ctsio->cdb;
10565 alloc_len = scsi_2btoul(cdb->length);
10568 * We malloc the full inquiry data size here and fill it
10569 * in. If the user only asks for less, we'll give him
10572 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10573 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10574 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10575 ctsio->kern_sg_entries = 0;
10576 ctsio->kern_data_resid = 0;
10577 ctsio->kern_rel_offset = 0;
10579 if (data_len < alloc_len) {
10580 ctsio->residual = alloc_len - data_len;
10581 ctsio->kern_data_len = data_len;
10582 ctsio->kern_total_len = data_len;
10584 ctsio->residual = 0;
10585 ctsio->kern_data_len = alloc_len;
10586 ctsio->kern_total_len = alloc_len;
10590 * If we have a LUN configured, report it as connected. Otherwise,
10591 * report that it is offline or no device is supported, depending
10592 * on the value of inquiry_pq_no_lun.
10594 * According to the spec (SPC-4 r34), the peripheral qualifier
10595 * SID_QUAL_LU_OFFLINE (001b) is used in the following scenario:
10597 * "A peripheral device having the specified peripheral device type
10598 * is not connected to this logical unit. However, the device
10599 * server is capable of supporting the specified peripheral device
10600 * type on this logical unit."
10602 * According to the same spec, the peripheral qualifier
10603 * SID_QUAL_BAD_LU (011b) is used in this scenario:
10605 * "The device server is not capable of supporting a peripheral
10606 * device on this logical unit. For this peripheral qualifier the
10607 * peripheral device type shall be set to 1Fh. All other peripheral
10608 * device type values are reserved for this peripheral qualifier."
10610 * Given the text, it would seem that we probably want to report that
10611 * the LUN is offline here. There is no LUN connected, but we can
10612 * support a LUN at the given LUN number.
10614 * In the real world, though, it sounds like things are a little
10617 * - Linux, when presented with a LUN with the offline peripheral
10618 * qualifier, will create an sg driver instance for it. So when
10619 * you attach it to CTL, you wind up with a ton of sg driver
10620 * instances. (One for every LUN that Linux bothered to probe.)
10621 * Linux does this despite the fact that it issues a REPORT LUNs
10622 * to LUN 0 to get the inventory of supported LUNs.
10624 * - There is other anecdotal evidence (from Emulex folks) about
10625 * arrays that use the offline peripheral qualifier for LUNs that
10626 * are on the "passive" path in an active/passive array.
10628 * So the solution is provide a hopefully reasonable default
10629 * (return bad/no LUN) and allow the user to change the behavior
10630 * with a tunable/sysctl variable.
10633 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10634 lun->be_lun->lun_type;
10635 else if (softc->inquiry_pq_no_lun == 0)
10636 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10638 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10640 /* RMB in byte 2 is 0 */
10641 inq_ptr->version = SCSI_REV_SPC4;
10644 * According to SAM-3, even if a device only supports a single
10645 * level of LUN addressing, it should still set the HISUP bit:
10647 * 4.9.1 Logical unit numbers overview
10649 * All logical unit number formats described in this standard are
10650 * hierarchical in structure even when only a single level in that
10651 * hierarchy is used. The HISUP bit shall be set to one in the
10652 * standard INQUIRY data (see SPC-2) when any logical unit number
10653 * format described in this standard is used. Non-hierarchical
10654 * formats are outside the scope of this standard.
10656 * Therefore we set the HiSup bit here.
10658 * The reponse format is 2, per SPC-3.
10660 inq_ptr->response_format = SID_HiSup | 2;
10662 inq_ptr->additional_length = data_len -
10663 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10664 CTL_DEBUG_PRINT(("additional_length = %d\n",
10665 inq_ptr->additional_length));
10667 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10668 /* 16 bit addressing */
10669 if (port_type == CTL_PORT_SCSI)
10670 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10671 /* XXX set the SID_MultiP bit here if we're actually going to
10672 respond on multiple ports */
10673 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10675 /* 16 bit data bus, synchronous transfers */
10676 if (port_type == CTL_PORT_SCSI)
10677 inq_ptr->flags = SID_WBus16 | SID_Sync;
10679 * XXX KDM do we want to support tagged queueing on the control
10683 || (lun->be_lun->lun_type != T_PROCESSOR))
10684 inq_ptr->flags |= SID_CmdQue;
10686 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10687 * We have 8 bytes for the vendor name, and 16 bytes for the device
10688 * name and 4 bytes for the revision.
10690 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10691 "vendor")) == NULL) {
10692 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10694 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10695 strncpy(inq_ptr->vendor, val,
10696 min(sizeof(inq_ptr->vendor), strlen(val)));
10699 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10700 sizeof(inq_ptr->product));
10701 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10702 switch (lun->be_lun->lun_type) {
10704 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10705 sizeof(inq_ptr->product));
10708 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10709 sizeof(inq_ptr->product));
10712 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10713 sizeof(inq_ptr->product));
10717 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10718 strncpy(inq_ptr->product, val,
10719 min(sizeof(inq_ptr->product), strlen(val)));
10723 * XXX make this a macro somewhere so it automatically gets
10724 * incremented when we make changes.
10726 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10727 "revision")) == NULL) {
10728 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10730 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10731 strncpy(inq_ptr->revision, val,
10732 min(sizeof(inq_ptr->revision), strlen(val)));
10736 * For parallel SCSI, we support double transition and single
10737 * transition clocking. We also support QAS (Quick Arbitration
10738 * and Selection) and Information Unit transfers on both the
10739 * control and array devices.
10741 if (port_type == CTL_PORT_SCSI)
10742 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10745 /* SAM-5 (no version claimed) */
10746 scsi_ulto2b(0x00A0, inq_ptr->version1);
10747 /* SPC-4 (no version claimed) */
10748 scsi_ulto2b(0x0460, inq_ptr->version2);
10749 if (port_type == CTL_PORT_FC) {
10750 /* FCP-2 ANSI INCITS.350:2003 */
10751 scsi_ulto2b(0x0917, inq_ptr->version3);
10752 } else if (port_type == CTL_PORT_SCSI) {
10753 /* SPI-4 ANSI INCITS.362:200x */
10754 scsi_ulto2b(0x0B56, inq_ptr->version3);
10755 } else if (port_type == CTL_PORT_ISCSI) {
10756 /* iSCSI (no version claimed) */
10757 scsi_ulto2b(0x0960, inq_ptr->version3);
10758 } else if (port_type == CTL_PORT_SAS) {
10759 /* SAS (no version claimed) */
10760 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10764 /* SBC-4 (no version claimed) */
10765 scsi_ulto2b(0x0600, inq_ptr->version4);
10767 switch (lun->be_lun->lun_type) {
10769 /* SBC-4 (no version claimed) */
10770 scsi_ulto2b(0x0600, inq_ptr->version4);
10778 ctl_set_success(ctsio);
10779 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10780 ctsio->be_move_done = ctl_config_move_done;
10781 ctl_datamove((union ctl_io *)ctsio);
10782 return (CTL_RETVAL_COMPLETE);
10786 ctl_inquiry(struct ctl_scsiio *ctsio)
10788 struct scsi_inquiry *cdb;
10791 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10793 cdb = (struct scsi_inquiry *)ctsio->cdb;
10794 if (cdb->byte2 & SI_EVPD)
10795 retval = ctl_inquiry_evpd(ctsio);
10796 else if (cdb->page_code == 0)
10797 retval = ctl_inquiry_std(ctsio);
10799 ctl_set_invalid_field(ctsio,
10805 ctl_done((union ctl_io *)ctsio);
10806 return (CTL_RETVAL_COMPLETE);
10813 * For known CDB types, parse the LBA and length.
10816 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10818 if (io->io_hdr.io_type != CTL_IO_SCSI)
10821 switch (io->scsiio.cdb[0]) {
10822 case COMPARE_AND_WRITE: {
10823 struct scsi_compare_and_write *cdb;
10825 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10827 *lba = scsi_8btou64(cdb->addr);
10828 *len = cdb->length;
10833 struct scsi_rw_6 *cdb;
10835 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10837 *lba = scsi_3btoul(cdb->addr);
10838 /* only 5 bits are valid in the most significant address byte */
10840 *len = cdb->length;
10845 struct scsi_rw_10 *cdb;
10847 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10849 *lba = scsi_4btoul(cdb->addr);
10850 *len = scsi_2btoul(cdb->length);
10853 case WRITE_VERIFY_10: {
10854 struct scsi_write_verify_10 *cdb;
10856 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10858 *lba = scsi_4btoul(cdb->addr);
10859 *len = scsi_2btoul(cdb->length);
10864 struct scsi_rw_12 *cdb;
10866 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10868 *lba = scsi_4btoul(cdb->addr);
10869 *len = scsi_4btoul(cdb->length);
10872 case WRITE_VERIFY_12: {
10873 struct scsi_write_verify_12 *cdb;
10875 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10877 *lba = scsi_4btoul(cdb->addr);
10878 *len = scsi_4btoul(cdb->length);
10883 case WRITE_ATOMIC_16: {
10884 struct scsi_rw_16 *cdb;
10886 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10888 *lba = scsi_8btou64(cdb->addr);
10889 *len = scsi_4btoul(cdb->length);
10892 case WRITE_VERIFY_16: {
10893 struct scsi_write_verify_16 *cdb;
10895 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10897 *lba = scsi_8btou64(cdb->addr);
10898 *len = scsi_4btoul(cdb->length);
10901 case WRITE_SAME_10: {
10902 struct scsi_write_same_10 *cdb;
10904 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10906 *lba = scsi_4btoul(cdb->addr);
10907 *len = scsi_2btoul(cdb->length);
10910 case WRITE_SAME_16: {
10911 struct scsi_write_same_16 *cdb;
10913 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10915 *lba = scsi_8btou64(cdb->addr);
10916 *len = scsi_4btoul(cdb->length);
10920 struct scsi_verify_10 *cdb;
10922 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10924 *lba = scsi_4btoul(cdb->addr);
10925 *len = scsi_2btoul(cdb->length);
10929 struct scsi_verify_12 *cdb;
10931 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10933 *lba = scsi_4btoul(cdb->addr);
10934 *len = scsi_4btoul(cdb->length);
10938 struct scsi_verify_16 *cdb;
10940 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10942 *lba = scsi_8btou64(cdb->addr);
10943 *len = scsi_4btoul(cdb->length);
10951 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10952 struct scsi_get_lba_status *cdb;
10954 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10955 *lba = scsi_8btou64(cdb->addr);
10961 break; /* NOTREACHED */
10968 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10971 uint64_t endlba1, endlba2;
10973 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10974 endlba2 = lba2 + len2 - 1;
10976 if ((endlba1 < lba2) || (endlba2 < lba1))
10977 return (CTL_ACTION_PASS);
10979 return (CTL_ACTION_BLOCK);
10983 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10985 struct ctl_ptr_len_flags *ptrlen;
10986 struct scsi_unmap_desc *buf, *end, *range;
10990 /* If not UNMAP -- go other way. */
10991 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10992 io->scsiio.cdb[0] != UNMAP)
10993 return (CTL_ACTION_ERROR);
10995 /* If UNMAP without data -- block and wait for data. */
10996 ptrlen = (struct ctl_ptr_len_flags *)
10997 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10998 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10999 ptrlen->ptr == NULL)
11000 return (CTL_ACTION_BLOCK);
11002 /* UNMAP with data -- check for collision. */
11003 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
11004 end = buf + ptrlen->len / sizeof(*buf);
11005 for (range = buf; range < end; range++) {
11006 lba = scsi_8btou64(range->lba);
11007 len = scsi_4btoul(range->length);
11008 if ((lba < lba2 + len2) && (lba + len > lba2))
11009 return (CTL_ACTION_BLOCK);
11011 return (CTL_ACTION_PASS);
11015 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
11017 uint64_t lba1, lba2;
11018 uint64_t len1, len2;
11021 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
11022 return (CTL_ACTION_ERROR);
11024 retval = ctl_extent_check_unmap(io1, lba2, len2);
11025 if (retval != CTL_ACTION_ERROR)
11028 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
11029 return (CTL_ACTION_ERROR);
11031 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
11035 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
11037 uint64_t lba1, lba2;
11038 uint64_t len1, len2;
11040 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
11041 return (CTL_ACTION_ERROR);
11042 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
11043 return (CTL_ACTION_ERROR);
11045 if (lba1 + len1 == lba2)
11046 return (CTL_ACTION_BLOCK);
11047 return (CTL_ACTION_PASS);
11051 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
11052 union ctl_io *ooa_io)
11054 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
11055 ctl_serialize_action *serialize_row;
11058 * The initiator attempted multiple untagged commands at the same
11059 * time. Can't do that.
11061 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11062 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11063 && ((pending_io->io_hdr.nexus.targ_port ==
11064 ooa_io->io_hdr.nexus.targ_port)
11065 && (pending_io->io_hdr.nexus.initid.id ==
11066 ooa_io->io_hdr.nexus.initid.id))
11067 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11068 CTL_FLAG_STATUS_SENT)) == 0))
11069 return (CTL_ACTION_OVERLAP);
11072 * The initiator attempted to send multiple tagged commands with
11073 * the same ID. (It's fine if different initiators have the same
11076 * Even if all of those conditions are true, we don't kill the I/O
11077 * if the command ahead of us has been aborted. We won't end up
11078 * sending it to the FETD, and it's perfectly legal to resend a
11079 * command with the same tag number as long as the previous
11080 * instance of this tag number has been aborted somehow.
11082 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11083 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11084 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11085 && ((pending_io->io_hdr.nexus.targ_port ==
11086 ooa_io->io_hdr.nexus.targ_port)
11087 && (pending_io->io_hdr.nexus.initid.id ==
11088 ooa_io->io_hdr.nexus.initid.id))
11089 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11090 CTL_FLAG_STATUS_SENT)) == 0))
11091 return (CTL_ACTION_OVERLAP_TAG);
11094 * If we get a head of queue tag, SAM-3 says that we should
11095 * immediately execute it.
11097 * What happens if this command would normally block for some other
11098 * reason? e.g. a request sense with a head of queue tag
11099 * immediately after a write. Normally that would block, but this
11100 * will result in its getting executed immediately...
11102 * We currently return "pass" instead of "skip", so we'll end up
11103 * going through the rest of the queue to check for overlapped tags.
11105 * XXX KDM check for other types of blockage first??
11107 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11108 return (CTL_ACTION_PASS);
11111 * Ordered tags have to block until all items ahead of them
11112 * have completed. If we get called with an ordered tag, we always
11113 * block, if something else is ahead of us in the queue.
11115 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11116 return (CTL_ACTION_BLOCK);
11119 * Simple tags get blocked until all head of queue and ordered tags
11120 * ahead of them have completed. I'm lumping untagged commands in
11121 * with simple tags here. XXX KDM is that the right thing to do?
11123 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11124 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11125 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11126 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11127 return (CTL_ACTION_BLOCK);
11129 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11130 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11132 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11134 switch (serialize_row[pending_entry->seridx]) {
11135 case CTL_SER_BLOCK:
11136 return (CTL_ACTION_BLOCK);
11137 case CTL_SER_EXTENT:
11138 return (ctl_extent_check(ooa_io, pending_io,
11139 (lun->serseq == CTL_LUN_SERSEQ_ON)));
11140 case CTL_SER_EXTENTOPT:
11141 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11142 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11143 return (ctl_extent_check(ooa_io, pending_io,
11144 (lun->serseq == CTL_LUN_SERSEQ_ON)));
11145 return (CTL_ACTION_PASS);
11146 case CTL_SER_EXTENTSEQ:
11147 if (lun->serseq != CTL_LUN_SERSEQ_OFF)
11148 return (ctl_extent_check_seq(ooa_io, pending_io));
11149 return (CTL_ACTION_PASS);
11151 return (CTL_ACTION_PASS);
11152 case CTL_SER_BLOCKOPT:
11153 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11154 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11155 return (CTL_ACTION_BLOCK);
11156 return (CTL_ACTION_PASS);
11158 return (CTL_ACTION_SKIP);
11160 panic("invalid serialization value %d",
11161 serialize_row[pending_entry->seridx]);
11164 return (CTL_ACTION_ERROR);
11168 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11170 * - pending_io is generally either incoming, or on the blocked queue
11171 * - starting I/O is the I/O we want to start the check with.
11174 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11175 union ctl_io *starting_io)
11177 union ctl_io *ooa_io;
11180 mtx_assert(&lun->lun_lock, MA_OWNED);
11183 * Run back along the OOA queue, starting with the current
11184 * blocked I/O and going through every I/O before it on the
11185 * queue. If starting_io is NULL, we'll just end up returning
11188 for (ooa_io = starting_io; ooa_io != NULL;
11189 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11193 * This routine just checks to see whether
11194 * cur_blocked is blocked by ooa_io, which is ahead
11195 * of it in the queue. It doesn't queue/dequeue
11198 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11200 case CTL_ACTION_BLOCK:
11201 case CTL_ACTION_OVERLAP:
11202 case CTL_ACTION_OVERLAP_TAG:
11203 case CTL_ACTION_SKIP:
11204 case CTL_ACTION_ERROR:
11206 break; /* NOTREACHED */
11207 case CTL_ACTION_PASS:
11210 panic("invalid action %d", action);
11211 break; /* NOTREACHED */
11215 return (CTL_ACTION_PASS);
11220 * - An I/O has just completed, and has been removed from the per-LUN OOA
11221 * queue, so some items on the blocked queue may now be unblocked.
11224 ctl_check_blocked(struct ctl_lun *lun)
11226 union ctl_io *cur_blocked, *next_blocked;
11228 mtx_assert(&lun->lun_lock, MA_OWNED);
11231 * Run forward from the head of the blocked queue, checking each
11232 * entry against the I/Os prior to it on the OOA queue to see if
11233 * there is still any blockage.
11235 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11236 * with our removing a variable on it while it is traversing the
11239 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11240 cur_blocked != NULL; cur_blocked = next_blocked) {
11241 union ctl_io *prev_ooa;
11244 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11247 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11248 ctl_ooaq, ooa_links);
11251 * If cur_blocked happens to be the first item in the OOA
11252 * queue now, prev_ooa will be NULL, and the action
11253 * returned will just be CTL_ACTION_PASS.
11255 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11258 case CTL_ACTION_BLOCK:
11259 /* Nothing to do here, still blocked */
11261 case CTL_ACTION_OVERLAP:
11262 case CTL_ACTION_OVERLAP_TAG:
11264 * This shouldn't happen! In theory we've already
11265 * checked this command for overlap...
11268 case CTL_ACTION_PASS:
11269 case CTL_ACTION_SKIP: {
11270 const struct ctl_cmd_entry *entry;
11274 * The skip case shouldn't happen, this transaction
11275 * should have never made it onto the blocked queue.
11278 * This I/O is no longer blocked, we can remove it
11279 * from the blocked queue. Since this is a TAILQ
11280 * (doubly linked list), we can do O(1) removals
11281 * from any place on the list.
11283 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11285 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11287 if (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC){
11289 * Need to send IO back to original side to
11292 union ctl_ha_msg msg_info;
11294 msg_info.hdr.original_sc =
11295 cur_blocked->io_hdr.original_sc;
11296 msg_info.hdr.serializing_sc = cur_blocked;
11297 msg_info.hdr.msg_type = CTL_MSG_R2R;
11298 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11299 &msg_info, sizeof(msg_info), 0)) >
11300 CTL_HA_STATUS_SUCCESS) {
11301 printf("CTL:Check Blocked error from "
11302 "ctl_ha_msg_send %d\n",
11307 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11310 * Check this I/O for LUN state changes that may
11311 * have happened while this command was blocked.
11312 * The LUN state may have been changed by a command
11313 * ahead of us in the queue, so we need to re-check
11314 * for any states that can be caused by SCSI
11317 if (ctl_scsiio_lun_check(lun, entry,
11318 &cur_blocked->scsiio) == 0) {
11319 cur_blocked->io_hdr.flags |=
11320 CTL_FLAG_IS_WAS_ON_RTR;
11321 ctl_enqueue_rtr(cur_blocked);
11323 ctl_done(cur_blocked);
11328 * This probably shouldn't happen -- we shouldn't
11329 * get CTL_ACTION_ERROR, or anything else.
11335 return (CTL_RETVAL_COMPLETE);
11339 * This routine (with one exception) checks LUN flags that can be set by
11340 * commands ahead of us in the OOA queue. These flags have to be checked
11341 * when a command initially comes in, and when we pull a command off the
11342 * blocked queue and are preparing to execute it. The reason we have to
11343 * check these flags for commands on the blocked queue is that the LUN
11344 * state may have been changed by a command ahead of us while we're on the
11347 * Ordering is somewhat important with these checks, so please pay
11348 * careful attention to the placement of any new checks.
11351 ctl_scsiio_lun_check(struct ctl_lun *lun,
11352 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11354 struct ctl_softc *softc = lun->ctl_softc;
11360 mtx_assert(&lun->lun_lock, MA_OWNED);
11363 * If this shelf is a secondary shelf controller, we have to reject
11364 * any media access commands.
11366 if ((softc->flags & CTL_FLAG_ACTIVE_SHELF) == 0 &&
11367 (entry->flags & CTL_CMD_FLAG_OK_ON_SECONDARY) == 0) {
11368 ctl_set_lun_standby(ctsio);
11373 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11374 if (lun->flags & CTL_LUN_READONLY) {
11375 ctl_set_sense(ctsio, /*current_error*/ 1,
11376 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11377 /*asc*/ 0x27, /*ascq*/ 0x01, SSD_ELEM_NONE);
11381 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11382 .eca_and_aen & SCP_SWP) != 0) {
11383 ctl_set_sense(ctsio, /*current_error*/ 1,
11384 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11385 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11392 * Check for a reservation conflict. If this command isn't allowed
11393 * even on reserved LUNs, and if this initiator isn't the one who
11394 * reserved us, reject the command with a reservation conflict.
11396 residx = ctl_get_resindex(&ctsio->io_hdr.nexus);
11397 if ((lun->flags & CTL_LUN_RESERVED)
11398 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11399 if (lun->res_idx != residx) {
11400 ctl_set_reservation_conflict(ctsio);
11406 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11407 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11408 /* No reservation or command is allowed. */;
11409 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11410 (lun->res_type == SPR_TYPE_WR_EX ||
11411 lun->res_type == SPR_TYPE_WR_EX_RO ||
11412 lun->res_type == SPR_TYPE_WR_EX_AR)) {
11413 /* The command is allowed for Write Exclusive resv. */;
11416 * if we aren't registered or it's a res holder type
11417 * reservation and this isn't the res holder then set a
11420 if (ctl_get_prkey(lun, residx) == 0
11421 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11422 ctl_set_reservation_conflict(ctsio);
11429 if ((lun->flags & CTL_LUN_OFFLINE)
11430 && ((entry->flags & CTL_CMD_FLAG_OK_ON_OFFLINE) == 0)) {
11431 ctl_set_lun_not_ready(ctsio);
11437 * If the LUN is stopped, see if this particular command is allowed
11438 * for a stopped lun. Otherwise, reject it with 0x04,0x02.
11440 if ((lun->flags & CTL_LUN_STOPPED)
11441 && ((entry->flags & CTL_CMD_FLAG_OK_ON_STOPPED) == 0)) {
11442 /* "Logical unit not ready, initializing cmd. required" */
11443 ctl_set_lun_stopped(ctsio);
11448 if ((lun->flags & CTL_LUN_INOPERABLE)
11449 && ((entry->flags & CTL_CMD_FLAG_OK_ON_INOPERABLE) == 0)) {
11450 /* "Medium format corrupted" */
11451 ctl_set_medium_format_corrupted(ctsio);
11462 ctl_failover_io(union ctl_io *io, int have_lock)
11464 ctl_set_busy(&io->scsiio);
11471 struct ctl_lun *lun;
11472 struct ctl_softc *softc;
11473 union ctl_io *next_io, *pending_io;
11477 softc = control_softc;
11479 mtx_lock(&softc->ctl_lock);
11481 * Remove any cmds from the other SC from the rtr queue. These
11482 * will obviously only be for LUNs for which we're the primary.
11483 * We can't send status or get/send data for these commands.
11484 * Since they haven't been executed yet, we can just remove them.
11485 * We'll either abort them or delete them below, depending on
11486 * which HA mode we're in.
11489 mtx_lock(&softc->queue_lock);
11490 for (io = (union ctl_io *)STAILQ_FIRST(&softc->rtr_queue);
11491 io != NULL; io = next_io) {
11492 next_io = (union ctl_io *)STAILQ_NEXT(&io->io_hdr, links);
11493 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11494 STAILQ_REMOVE(&softc->rtr_queue, &io->io_hdr,
11495 ctl_io_hdr, links);
11497 mtx_unlock(&softc->queue_lock);
11500 for (lun_idx=0; lun_idx < softc->num_luns; lun_idx++) {
11501 lun = softc->ctl_luns[lun_idx];
11506 * Processor LUNs are primary on both sides.
11507 * XXX will this always be true?
11509 if (lun->be_lun->lun_type == T_PROCESSOR)
11512 if ((lun->flags & CTL_LUN_PRIMARY_SC)
11513 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11514 printf("FAILOVER: primary lun %d\n", lun_idx);
11516 * Remove all commands from the other SC. First from the
11517 * blocked queue then from the ooa queue. Once we have
11518 * removed them. Call ctl_check_blocked to see if there
11519 * is anything that can run.
11521 for (io = (union ctl_io *)TAILQ_FIRST(
11522 &lun->blocked_queue); io != NULL; io = next_io) {
11524 next_io = (union ctl_io *)TAILQ_NEXT(
11525 &io->io_hdr, blocked_links);
11527 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11528 TAILQ_REMOVE(&lun->blocked_queue,
11529 &io->io_hdr,blocked_links);
11530 io->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11531 TAILQ_REMOVE(&lun->ooa_queue,
11532 &io->io_hdr, ooa_links);
11538 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11539 io != NULL; io = next_io) {
11541 next_io = (union ctl_io *)TAILQ_NEXT(
11542 &io->io_hdr, ooa_links);
11544 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) {
11546 TAILQ_REMOVE(&lun->ooa_queue,
11553 ctl_check_blocked(lun);
11554 } else if ((lun->flags & CTL_LUN_PRIMARY_SC)
11555 && (softc->ha_mode == CTL_HA_MODE_XFER)) {
11557 printf("FAILOVER: primary lun %d\n", lun_idx);
11559 * Abort all commands from the other SC. We can't
11560 * send status back for them now. These should get
11561 * cleaned up when they are completed or come out
11562 * for a datamove operation.
11564 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue);
11565 io != NULL; io = next_io) {
11566 next_io = (union ctl_io *)TAILQ_NEXT(
11567 &io->io_hdr, ooa_links);
11569 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
11570 io->io_hdr.flags |= CTL_FLAG_ABORT;
11572 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11573 && (softc->ha_mode == CTL_HA_MODE_XFER)) {
11575 printf("FAILOVER: secondary lun %d\n", lun_idx);
11577 lun->flags |= CTL_LUN_PRIMARY_SC;
11580 * We send all I/O that was sent to this controller
11581 * and redirected to the other side back with
11582 * busy status, and have the initiator retry it.
11583 * Figuring out how much data has been transferred,
11584 * etc. and picking up where we left off would be
11587 * XXX KDM need to remove I/O from the blocked
11590 for (pending_io = (union ctl_io *)TAILQ_FIRST(
11591 &lun->ooa_queue); pending_io != NULL;
11592 pending_io = next_io) {
11594 next_io = (union ctl_io *)TAILQ_NEXT(
11595 &pending_io->io_hdr, ooa_links);
11597 pending_io->io_hdr.flags &=
11598 ~CTL_FLAG_SENT_2OTHER_SC;
11600 if (pending_io->io_hdr.flags &
11601 CTL_FLAG_IO_ACTIVE) {
11602 pending_io->io_hdr.flags |=
11605 ctl_set_busy(&pending_io->scsiio);
11606 ctl_done(pending_io);
11610 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
11611 } else if (((lun->flags & CTL_LUN_PRIMARY_SC) == 0)
11612 && (softc->ha_mode == CTL_HA_MODE_SER_ONLY)) {
11613 printf("FAILOVER: secondary lun %d\n", lun_idx);
11615 * if the first io on the OOA is not on the RtR queue
11618 lun->flags |= CTL_LUN_PRIMARY_SC;
11620 pending_io = (union ctl_io *)TAILQ_FIRST(
11622 if (pending_io==NULL) {
11623 printf("Nothing on OOA queue\n");
11627 pending_io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11628 if ((pending_io->io_hdr.flags &
11629 CTL_FLAG_IS_WAS_ON_RTR) == 0) {
11630 pending_io->io_hdr.flags |=
11631 CTL_FLAG_IS_WAS_ON_RTR;
11632 ctl_enqueue_rtr(pending_io);
11637 printf("Tag 0x%04x is running\n",
11638 pending_io->scsiio.tag_num);
11642 next_io = (union ctl_io *)TAILQ_NEXT(
11643 &pending_io->io_hdr, ooa_links);
11644 for (pending_io=next_io; pending_io != NULL;
11645 pending_io = next_io) {
11646 pending_io->io_hdr.flags &=
11647 ~CTL_FLAG_SENT_2OTHER_SC;
11648 next_io = (union ctl_io *)TAILQ_NEXT(
11649 &pending_io->io_hdr, ooa_links);
11650 if (pending_io->io_hdr.flags &
11651 CTL_FLAG_IS_WAS_ON_RTR) {
11653 printf("Tag 0x%04x is running\n",
11654 pending_io->scsiio.tag_num);
11659 switch (ctl_check_ooa(lun, pending_io,
11660 (union ctl_io *)TAILQ_PREV(
11661 &pending_io->io_hdr, ctl_ooaq,
11664 case CTL_ACTION_BLOCK:
11665 TAILQ_INSERT_TAIL(&lun->blocked_queue,
11666 &pending_io->io_hdr,
11668 pending_io->io_hdr.flags |=
11671 case CTL_ACTION_PASS:
11672 case CTL_ACTION_SKIP:
11673 pending_io->io_hdr.flags |=
11674 CTL_FLAG_IS_WAS_ON_RTR;
11675 ctl_enqueue_rtr(pending_io);
11677 case CTL_ACTION_OVERLAP:
11678 ctl_set_overlapped_cmd(
11679 (struct ctl_scsiio *)pending_io);
11680 ctl_done(pending_io);
11682 case CTL_ACTION_OVERLAP_TAG:
11683 ctl_set_overlapped_tag(
11684 (struct ctl_scsiio *)pending_io,
11685 pending_io->scsiio.tag_num & 0xff);
11686 ctl_done(pending_io);
11688 case CTL_ACTION_ERROR:
11690 ctl_set_internal_failure(
11691 (struct ctl_scsiio *)pending_io,
11694 ctl_done(pending_io);
11699 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
11701 panic("Unhandled HA mode failover, LUN flags = %#x, "
11702 "ha_mode = #%x", lun->flags, softc->ha_mode);
11706 mtx_unlock(&softc->ctl_lock);
11710 ctl_clear_ua(struct ctl_softc *ctl_softc, uint32_t initidx,
11711 ctl_ua_type ua_type)
11713 struct ctl_lun *lun;
11716 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
11718 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
11719 mtx_lock(&lun->lun_lock);
11720 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
11722 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua_type;
11723 mtx_unlock(&lun->lun_lock);
11728 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11730 struct ctl_lun *lun;
11731 const struct ctl_cmd_entry *entry;
11732 uint32_t initidx, targ_lun;
11739 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11740 if ((targ_lun < CTL_MAX_LUNS)
11741 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11743 * If the LUN is invalid, pretend that it doesn't exist.
11744 * It will go away as soon as all pending I/O has been
11747 mtx_lock(&lun->lun_lock);
11748 if (lun->flags & CTL_LUN_DISABLED) {
11749 mtx_unlock(&lun->lun_lock);
11751 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11752 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11754 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11755 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11757 if (lun->be_lun->lun_type == T_PROCESSOR) {
11758 ctsio->io_hdr.flags |= CTL_FLAG_CONTROL_DEV;
11762 * Every I/O goes into the OOA queue for a
11763 * particular LUN, and stays there until completion.
11766 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11767 lun->idle_time += getsbinuptime() -
11771 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11775 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11776 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11779 /* Get command entry and return error if it is unsuppotyed. */
11780 entry = ctl_validate_command(ctsio);
11781 if (entry == NULL) {
11783 mtx_unlock(&lun->lun_lock);
11787 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11788 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11791 * Check to see whether we can send this command to LUNs that don't
11792 * exist. This should pretty much only be the case for inquiry
11793 * and request sense. Further checks, below, really require having
11794 * a LUN, so we can't really check the command anymore. Just put
11795 * it on the rtr queue.
11798 if (entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) {
11799 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11800 ctl_enqueue_rtr((union ctl_io *)ctsio);
11804 ctl_set_unsupported_lun(ctsio);
11805 ctl_done((union ctl_io *)ctsio);
11806 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11810 * Make sure we support this particular command on this LUN.
11811 * e.g., we don't support writes to the control LUN.
11813 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11814 mtx_unlock(&lun->lun_lock);
11815 ctl_set_invalid_opcode(ctsio);
11816 ctl_done((union ctl_io *)ctsio);
11821 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11825 * If we've got a request sense, it'll clear the contingent
11826 * allegiance condition. Otherwise, if we have a CA condition for
11827 * this initiator, clear it, because it sent down a command other
11828 * than request sense.
11830 if ((ctsio->cdb[0] != REQUEST_SENSE)
11831 && (ctl_is_set(lun->have_ca, initidx)))
11832 ctl_clear_mask(lun->have_ca, initidx);
11836 * If the command has this flag set, it handles its own unit
11837 * attention reporting, we shouldn't do anything. Otherwise we
11838 * check for any pending unit attentions, and send them back to the
11839 * initiator. We only do this when a command initially comes in,
11840 * not when we pull it off the blocked queue.
11842 * According to SAM-3, section 5.3.2, the order that things get
11843 * presented back to the host is basically unit attentions caused
11844 * by some sort of reset event, busy status, reservation conflicts
11845 * or task set full, and finally any other status.
11847 * One issue here is that some of the unit attentions we report
11848 * don't fall into the "reset" category (e.g. "reported luns data
11849 * has changed"). So reporting it here, before the reservation
11850 * check, may be technically wrong. I guess the only thing to do
11851 * would be to check for and report the reset events here, and then
11852 * check for the other unit attention types after we check for a
11853 * reservation conflict.
11855 * XXX KDM need to fix this
11857 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11858 ctl_ua_type ua_type;
11859 scsi_sense_data_type sense_format;
11861 if (lun->flags & CTL_LUN_SENSE_DESC)
11862 sense_format = SSD_TYPE_DESC;
11864 sense_format = SSD_TYPE_FIXED;
11866 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11868 if (ua_type != CTL_UA_NONE) {
11869 mtx_unlock(&lun->lun_lock);
11870 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11871 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11872 ctsio->sense_len = SSD_FULL_SIZE;
11873 ctl_done((union ctl_io *)ctsio);
11879 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11880 mtx_unlock(&lun->lun_lock);
11881 ctl_done((union ctl_io *)ctsio);
11886 * XXX CHD this is where we want to send IO to other side if
11887 * this LUN is secondary on this SC. We will need to make a copy
11888 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11889 * the copy we send as FROM_OTHER.
11890 * We also need to stuff the address of the original IO so we can
11891 * find it easily. Something similar will need be done on the other
11892 * side so when we are done we can find the copy.
11894 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11895 union ctl_ha_msg msg_info;
11898 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11900 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11901 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11903 printf("1. ctsio %p\n", ctsio);
11905 msg_info.hdr.serializing_sc = NULL;
11906 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11907 msg_info.scsi.tag_num = ctsio->tag_num;
11908 msg_info.scsi.tag_type = ctsio->tag_type;
11909 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11911 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11913 if ((isc_retval=ctl_ha_msg_send(CTL_HA_CHAN_CTL,
11914 (void *)&msg_info, sizeof(msg_info), 0)) >
11915 CTL_HA_STATUS_SUCCESS) {
11916 printf("CTL:precheck, ctl_ha_msg_send returned %d\n",
11918 printf("CTL:opcode is %x\n", ctsio->cdb[0]);
11921 printf("CTL:Precheck sent msg, opcode is %x\n",opcode);
11926 * XXX KDM this I/O is off the incoming queue, but hasn't
11927 * been inserted on any other queue. We may need to come
11928 * up with a holding queue while we wait for serialization
11929 * so that we have an idea of what we're waiting for from
11932 mtx_unlock(&lun->lun_lock);
11936 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11937 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11938 ctl_ooaq, ooa_links))) {
11939 case CTL_ACTION_BLOCK:
11940 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11941 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11943 mtx_unlock(&lun->lun_lock);
11945 case CTL_ACTION_PASS:
11946 case CTL_ACTION_SKIP:
11947 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11948 mtx_unlock(&lun->lun_lock);
11949 ctl_enqueue_rtr((union ctl_io *)ctsio);
11951 case CTL_ACTION_OVERLAP:
11952 mtx_unlock(&lun->lun_lock);
11953 ctl_set_overlapped_cmd(ctsio);
11954 ctl_done((union ctl_io *)ctsio);
11956 case CTL_ACTION_OVERLAP_TAG:
11957 mtx_unlock(&lun->lun_lock);
11958 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11959 ctl_done((union ctl_io *)ctsio);
11961 case CTL_ACTION_ERROR:
11963 mtx_unlock(&lun->lun_lock);
11964 ctl_set_internal_failure(ctsio,
11966 /*retry_count*/ 0);
11967 ctl_done((union ctl_io *)ctsio);
11973 const struct ctl_cmd_entry *
11974 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11976 const struct ctl_cmd_entry *entry;
11977 int service_action;
11979 entry = &ctl_cmd_table[ctsio->cdb[0]];
11981 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11982 if (entry->flags & CTL_CMD_FLAG_SA5) {
11983 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11984 entry = &((const struct ctl_cmd_entry *)
11985 entry->execute)[service_action];
11990 const struct ctl_cmd_entry *
11991 ctl_validate_command(struct ctl_scsiio *ctsio)
11993 const struct ctl_cmd_entry *entry;
11997 entry = ctl_get_cmd_entry(ctsio, &sa);
11998 if (entry->execute == NULL) {
12000 ctl_set_invalid_field(ctsio,
12007 ctl_set_invalid_opcode(ctsio);
12008 ctl_done((union ctl_io *)ctsio);
12011 KASSERT(entry->length > 0,
12012 ("Not defined length for command 0x%02x/0x%02x",
12013 ctsio->cdb[0], ctsio->cdb[1]));
12014 for (i = 1; i < entry->length; i++) {
12015 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
12018 ctl_set_invalid_field(ctsio,
12023 /*bit*/ fls(diff) - 1);
12024 ctl_done((union ctl_io *)ctsio);
12031 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
12034 switch (lun_type) {
12036 if (((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0) &&
12037 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
12041 if (((entry->flags & CTL_CMD_FLAG_OK_ON_SLUN) == 0) &&
12042 ((entry->flags & CTL_CMD_FLAG_OK_ON_ALL_LUNS) == 0))
12052 ctl_scsiio(struct ctl_scsiio *ctsio)
12055 const struct ctl_cmd_entry *entry;
12057 retval = CTL_RETVAL_COMPLETE;
12059 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
12061 entry = ctl_get_cmd_entry(ctsio, NULL);
12064 * If this I/O has been aborted, just send it straight to
12065 * ctl_done() without executing it.
12067 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
12068 ctl_done((union ctl_io *)ctsio);
12073 * All the checks should have been handled by ctl_scsiio_precheck().
12074 * We should be clear now to just execute the I/O.
12076 retval = entry->execute(ctsio);
12083 * Since we only implement one target right now, a bus reset simply resets
12084 * our single target.
12087 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
12089 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
12093 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
12094 ctl_ua_type ua_type)
12096 struct ctl_lun *lun;
12099 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12100 union ctl_ha_msg msg_info;
12102 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
12103 msg_info.hdr.nexus = io->io_hdr.nexus;
12104 if (ua_type==CTL_UA_TARG_RESET)
12105 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
12107 msg_info.task.task_action = CTL_TASK_BUS_RESET;
12108 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12109 msg_info.hdr.original_sc = NULL;
12110 msg_info.hdr.serializing_sc = NULL;
12111 if (CTL_HA_STATUS_SUCCESS != ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12112 (void *)&msg_info, sizeof(msg_info), 0)) {
12117 mtx_lock(&softc->ctl_lock);
12118 STAILQ_FOREACH(lun, &softc->lun_list, links)
12119 retval += ctl_lun_reset(lun, io, ua_type);
12120 mtx_unlock(&softc->ctl_lock);
12126 * The LUN should always be set. The I/O is optional, and is used to
12127 * distinguish between I/Os sent by this initiator, and by other
12128 * initiators. We set unit attention for initiators other than this one.
12129 * SAM-3 is vague on this point. It does say that a unit attention should
12130 * be established for other initiators when a LUN is reset (see section
12131 * 5.7.3), but it doesn't specifically say that the unit attention should
12132 * be established for this particular initiator when a LUN is reset. Here
12133 * is the relevant text, from SAM-3 rev 8:
12135 * 5.7.2 When a SCSI initiator port aborts its own tasks
12137 * When a SCSI initiator port causes its own task(s) to be aborted, no
12138 * notification that the task(s) have been aborted shall be returned to
12139 * the SCSI initiator port other than the completion response for the
12140 * command or task management function action that caused the task(s) to
12141 * be aborted and notification(s) associated with related effects of the
12142 * action (e.g., a reset unit attention condition).
12144 * XXX KDM for now, we're setting unit attention for all initiators.
12147 ctl_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
12157 mtx_lock(&lun->lun_lock);
12159 * Run through the OOA queue and abort each I/O.
12161 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12162 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12163 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
12167 * This version sets unit attention for every
12170 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12171 ctl_est_ua_all(lun, initidx, ua_type);
12173 ctl_est_ua_all(lun, -1, ua_type);
12177 * A reset (any kind, really) clears reservations established with
12178 * RESERVE/RELEASE. It does not clear reservations established
12179 * with PERSISTENT RESERVE OUT, but we don't support that at the
12180 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
12181 * reservations made with the RESERVE/RELEASE commands, because
12182 * those commands are obsolete in SPC-3.
12184 lun->flags &= ~CTL_LUN_RESERVED;
12187 for (i = 0; i < CTL_MAX_INITIATORS; i++)
12188 ctl_clear_mask(lun->have_ca, i);
12190 mtx_unlock(&lun->lun_lock);
12196 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12201 mtx_assert(&lun->lun_lock, MA_OWNED);
12204 * Run through the OOA queue and attempt to find the given I/O.
12205 * The target port, initiator ID, tag type and tag number have to
12206 * match the values that we got from the initiator. If we have an
12207 * untagged command to abort, simply abort the first untagged command
12208 * we come to. We only allow one untagged command at a time of course.
12210 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12211 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12213 if ((targ_port == UINT32_MAX ||
12214 targ_port == xio->io_hdr.nexus.targ_port) &&
12215 (init_id == UINT32_MAX ||
12216 init_id == xio->io_hdr.nexus.initid.id)) {
12217 if (targ_port != xio->io_hdr.nexus.targ_port ||
12218 init_id != xio->io_hdr.nexus.initid.id)
12219 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12220 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12221 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12222 union ctl_ha_msg msg_info;
12224 msg_info.hdr.nexus = xio->io_hdr.nexus;
12225 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12226 msg_info.task.tag_num = xio->scsiio.tag_num;
12227 msg_info.task.tag_type = xio->scsiio.tag_type;
12228 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12229 msg_info.hdr.original_sc = NULL;
12230 msg_info.hdr.serializing_sc = NULL;
12231 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12232 (void *)&msg_info, sizeof(msg_info), 0);
12239 ctl_abort_task_set(union ctl_io *io)
12241 struct ctl_softc *softc = control_softc;
12242 struct ctl_lun *lun;
12248 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12249 mtx_lock(&softc->ctl_lock);
12250 if ((targ_lun < CTL_MAX_LUNS) && (softc->ctl_luns[targ_lun] != NULL))
12251 lun = softc->ctl_luns[targ_lun];
12253 mtx_unlock(&softc->ctl_lock);
12257 mtx_lock(&lun->lun_lock);
12258 mtx_unlock(&softc->ctl_lock);
12259 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12260 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12261 io->io_hdr.nexus.initid.id,
12262 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12263 } else { /* CTL_TASK_CLEAR_TASK_SET */
12264 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12265 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12267 mtx_unlock(&lun->lun_lock);
12272 ctl_i_t_nexus_reset(union ctl_io *io)
12274 struct ctl_softc *softc = control_softc;
12275 struct ctl_lun *lun;
12276 uint32_t initidx, residx;
12278 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12279 residx = ctl_get_resindex(&io->io_hdr.nexus);
12280 mtx_lock(&softc->ctl_lock);
12281 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12282 mtx_lock(&lun->lun_lock);
12283 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12284 io->io_hdr.nexus.initid.id,
12285 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12287 ctl_clear_mask(lun->have_ca, initidx);
12289 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
12290 lun->flags &= ~CTL_LUN_RESERVED;
12291 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12292 mtx_unlock(&lun->lun_lock);
12294 mtx_unlock(&softc->ctl_lock);
12299 ctl_abort_task(union ctl_io *io)
12302 struct ctl_lun *lun;
12303 struct ctl_softc *softc;
12306 char printbuf[128];
12311 softc = control_softc;
12317 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12318 mtx_lock(&softc->ctl_lock);
12319 if ((targ_lun < CTL_MAX_LUNS)
12320 && (softc->ctl_luns[targ_lun] != NULL))
12321 lun = softc->ctl_luns[targ_lun];
12323 mtx_unlock(&softc->ctl_lock);
12328 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12329 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12332 mtx_lock(&lun->lun_lock);
12333 mtx_unlock(&softc->ctl_lock);
12335 * Run through the OOA queue and attempt to find the given I/O.
12336 * The target port, initiator ID, tag type and tag number have to
12337 * match the values that we got from the initiator. If we have an
12338 * untagged command to abort, simply abort the first untagged command
12339 * we come to. We only allow one untagged command at a time of course.
12341 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12342 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12344 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12346 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12347 lun->lun, xio->scsiio.tag_num,
12348 xio->scsiio.tag_type,
12349 (xio->io_hdr.blocked_links.tqe_prev
12350 == NULL) ? "" : " BLOCKED",
12351 (xio->io_hdr.flags &
12352 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12353 (xio->io_hdr.flags &
12354 CTL_FLAG_ABORT) ? " ABORT" : "",
12355 (xio->io_hdr.flags &
12356 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12357 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12359 printf("%s\n", sbuf_data(&sb));
12362 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12363 || (xio->io_hdr.nexus.initid.id != io->io_hdr.nexus.initid.id)
12364 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12368 * If the abort says that the task is untagged, the
12369 * task in the queue must be untagged. Otherwise,
12370 * we just check to see whether the tag numbers
12371 * match. This is because the QLogic firmware
12372 * doesn't pass back the tag type in an abort
12376 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12377 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12378 || (xio->scsiio.tag_num == io->taskio.tag_num))
12381 * XXX KDM we've got problems with FC, because it
12382 * doesn't send down a tag type with aborts. So we
12383 * can only really go by the tag number...
12384 * This may cause problems with parallel SCSI.
12385 * Need to figure that out!!
12387 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12388 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12390 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12391 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12392 union ctl_ha_msg msg_info;
12394 io->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
12395 msg_info.hdr.nexus = io->io_hdr.nexus;
12396 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12397 msg_info.task.tag_num = io->taskio.tag_num;
12398 msg_info.task.tag_type = io->taskio.tag_type;
12399 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12400 msg_info.hdr.original_sc = NULL;
12401 msg_info.hdr.serializing_sc = NULL;
12403 printf("Sent Abort to other side\n");
12405 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12406 (void *)&msg_info, sizeof(msg_info), 0) !=
12407 CTL_HA_STATUS_SUCCESS) {
12411 printf("ctl_abort_task: found I/O to abort\n");
12415 mtx_unlock(&lun->lun_lock);
12419 * This isn't really an error. It's entirely possible for
12420 * the abort and command completion to cross on the wire.
12421 * This is more of an informative/diagnostic error.
12424 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12425 "%d:%d:%d:%d tag %d type %d\n",
12426 io->io_hdr.nexus.initid.id,
12427 io->io_hdr.nexus.targ_port,
12428 io->io_hdr.nexus.targ_target.id,
12429 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12430 io->taskio.tag_type);
12437 ctl_run_task(union ctl_io *io)
12439 struct ctl_softc *softc = control_softc;
12441 const char *task_desc;
12443 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12445 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12446 ("ctl_run_task: Unextected io_type %d\n",
12447 io->io_hdr.io_type));
12449 task_desc = ctl_scsi_task_string(&io->taskio);
12450 if (task_desc != NULL) {
12452 csevent_log(CSC_CTL | CSC_SHELF_SW |
12454 csevent_LogType_Trace,
12455 csevent_Severity_Information,
12456 csevent_AlertLevel_Green,
12457 csevent_FRU_Firmware,
12458 csevent_FRU_Unknown,
12459 "CTL: received task: %s",task_desc);
12463 csevent_log(CSC_CTL | CSC_SHELF_SW |
12465 csevent_LogType_Trace,
12466 csevent_Severity_Information,
12467 csevent_AlertLevel_Green,
12468 csevent_FRU_Firmware,
12469 csevent_FRU_Unknown,
12470 "CTL: received unknown task "
12472 io->taskio.task_action,
12473 io->taskio.task_action);
12476 switch (io->taskio.task_action) {
12477 case CTL_TASK_ABORT_TASK:
12478 retval = ctl_abort_task(io);
12480 case CTL_TASK_ABORT_TASK_SET:
12481 case CTL_TASK_CLEAR_TASK_SET:
12482 retval = ctl_abort_task_set(io);
12484 case CTL_TASK_CLEAR_ACA:
12486 case CTL_TASK_I_T_NEXUS_RESET:
12487 retval = ctl_i_t_nexus_reset(io);
12489 case CTL_TASK_LUN_RESET: {
12490 struct ctl_lun *lun;
12493 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12494 mtx_lock(&softc->ctl_lock);
12495 if ((targ_lun < CTL_MAX_LUNS)
12496 && (softc->ctl_luns[targ_lun] != NULL))
12497 lun = softc->ctl_luns[targ_lun];
12499 mtx_unlock(&softc->ctl_lock);
12504 if (!(io->io_hdr.flags &
12505 CTL_FLAG_FROM_OTHER_SC)) {
12506 union ctl_ha_msg msg_info;
12508 io->io_hdr.flags |=
12509 CTL_FLAG_SENT_2OTHER_SC;
12510 msg_info.hdr.msg_type =
12511 CTL_MSG_MANAGE_TASKS;
12512 msg_info.hdr.nexus = io->io_hdr.nexus;
12513 msg_info.task.task_action =
12514 CTL_TASK_LUN_RESET;
12515 msg_info.hdr.original_sc = NULL;
12516 msg_info.hdr.serializing_sc = NULL;
12517 if (CTL_HA_STATUS_SUCCESS !=
12518 ctl_ha_msg_send(CTL_HA_CHAN_CTL,
12520 sizeof(msg_info), 0)) {
12524 retval = ctl_lun_reset(lun, io,
12526 mtx_unlock(&softc->ctl_lock);
12529 case CTL_TASK_TARGET_RESET:
12530 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12532 case CTL_TASK_BUS_RESET:
12533 retval = ctl_bus_reset(softc, io);
12535 case CTL_TASK_PORT_LOGIN:
12537 case CTL_TASK_PORT_LOGOUT:
12540 printf("ctl_run_task: got unknown task management event %d\n",
12541 io->taskio.task_action);
12545 io->io_hdr.status = CTL_SUCCESS;
12547 io->io_hdr.status = CTL_ERROR;
12552 * For HA operation. Handle commands that come in from the other
12556 ctl_handle_isc(union ctl_io *io)
12559 struct ctl_lun *lun;
12560 struct ctl_softc *softc;
12563 softc = control_softc;
12565 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12566 lun = softc->ctl_luns[targ_lun];
12568 switch (io->io_hdr.msg_type) {
12569 case CTL_MSG_SERIALIZE:
12570 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12572 case CTL_MSG_R2R: {
12573 const struct ctl_cmd_entry *entry;
12576 * This is only used in SER_ONLY mode.
12579 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12580 mtx_lock(&lun->lun_lock);
12581 if (ctl_scsiio_lun_check(lun,
12582 entry, (struct ctl_scsiio *)io) != 0) {
12583 mtx_unlock(&lun->lun_lock);
12587 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12588 mtx_unlock(&lun->lun_lock);
12589 ctl_enqueue_rtr(io);
12592 case CTL_MSG_FINISH_IO:
12593 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12598 mtx_lock(&lun->lun_lock);
12599 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12601 ctl_check_blocked(lun);
12602 mtx_unlock(&lun->lun_lock);
12605 case CTL_MSG_PERS_ACTION:
12606 ctl_hndl_per_res_out_on_other_sc(
12607 (union ctl_ha_msg *)&io->presio.pr_msg);
12610 case CTL_MSG_BAD_JUJU:
12614 case CTL_MSG_DATAMOVE:
12615 /* Only used in XFER mode */
12617 ctl_datamove_remote(io);
12619 case CTL_MSG_DATAMOVE_DONE:
12620 /* Only used in XFER mode */
12622 io->scsiio.be_move_done(io);
12626 printf("%s: Invalid message type %d\n",
12627 __func__, io->io_hdr.msg_type);
12637 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12638 * there is no match.
12640 static ctl_lun_error_pattern
12641 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12643 const struct ctl_cmd_entry *entry;
12644 ctl_lun_error_pattern filtered_pattern, pattern;
12646 pattern = desc->error_pattern;
12649 * XXX KDM we need more data passed into this function to match a
12650 * custom pattern, and we actually need to implement custom pattern
12653 if (pattern & CTL_LUN_PAT_CMD)
12654 return (CTL_LUN_PAT_CMD);
12656 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12657 return (CTL_LUN_PAT_ANY);
12659 entry = ctl_get_cmd_entry(ctsio, NULL);
12661 filtered_pattern = entry->pattern & pattern;
12664 * If the user requested specific flags in the pattern (e.g.
12665 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12668 * If the user did not specify any flags, it doesn't matter whether
12669 * or not the command supports the flags.
12671 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12672 (pattern & ~CTL_LUN_PAT_MASK))
12673 return (CTL_LUN_PAT_NONE);
12676 * If the user asked for a range check, see if the requested LBA
12677 * range overlaps with this command's LBA range.
12679 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12685 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12687 return (CTL_LUN_PAT_NONE);
12689 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12690 desc->lba_range.len, FALSE);
12692 * A "pass" means that the LBA ranges don't overlap, so
12693 * this doesn't match the user's range criteria.
12695 if (action == CTL_ACTION_PASS)
12696 return (CTL_LUN_PAT_NONE);
12699 return (filtered_pattern);
12703 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12705 struct ctl_error_desc *desc, *desc2;
12707 mtx_assert(&lun->lun_lock, MA_OWNED);
12709 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12710 ctl_lun_error_pattern pattern;
12712 * Check to see whether this particular command matches
12713 * the pattern in the descriptor.
12715 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12716 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12719 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12720 case CTL_LUN_INJ_ABORTED:
12721 ctl_set_aborted(&io->scsiio);
12723 case CTL_LUN_INJ_MEDIUM_ERR:
12724 ctl_set_medium_error(&io->scsiio);
12726 case CTL_LUN_INJ_UA:
12727 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12729 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12731 case CTL_LUN_INJ_CUSTOM:
12733 * We're assuming the user knows what he is doing.
12734 * Just copy the sense information without doing
12737 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12738 MIN(sizeof(desc->custom_sense),
12739 sizeof(io->scsiio.sense_data)));
12740 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12741 io->scsiio.sense_len = SSD_FULL_SIZE;
12742 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12744 case CTL_LUN_INJ_NONE:
12747 * If this is an error injection type we don't know
12748 * about, clear the continuous flag (if it is set)
12749 * so it will get deleted below.
12751 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12755 * By default, each error injection action is a one-shot
12757 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12760 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12766 #ifdef CTL_IO_DELAY
12768 ctl_datamove_timer_wakeup(void *arg)
12772 io = (union ctl_io *)arg;
12776 #endif /* CTL_IO_DELAY */
12779 ctl_datamove(union ctl_io *io)
12781 void (*fe_datamove)(union ctl_io *io);
12783 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12785 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12788 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12793 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12794 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12796 sbuf_cat(&sb, path_str);
12797 switch (io->io_hdr.io_type) {
12799 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12800 sbuf_printf(&sb, "\n");
12801 sbuf_cat(&sb, path_str);
12802 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12803 io->scsiio.tag_num, io->scsiio.tag_type);
12806 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12807 "Tag Type: %d\n", io->taskio.task_action,
12808 io->taskio.tag_num, io->taskio.tag_type);
12811 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12812 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12815 sbuf_cat(&sb, path_str);
12816 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12817 (intmax_t)time_uptime - io->io_hdr.start_time);
12819 printf("%s", sbuf_data(&sb));
12821 #endif /* CTL_TIME_IO */
12823 #ifdef CTL_IO_DELAY
12824 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12825 struct ctl_lun *lun;
12827 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12829 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12831 struct ctl_lun *lun;
12833 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12835 && (lun->delay_info.datamove_delay > 0)) {
12836 struct callout *callout;
12838 callout = (struct callout *)&io->io_hdr.timer_bytes;
12839 callout_init(callout, /*mpsafe*/ 1);
12840 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12841 callout_reset(callout,
12842 lun->delay_info.datamove_delay * hz,
12843 ctl_datamove_timer_wakeup, io);
12844 if (lun->delay_info.datamove_type ==
12845 CTL_DELAY_TYPE_ONESHOT)
12846 lun->delay_info.datamove_delay = 0;
12853 * This command has been aborted. Set the port status, so we fail
12856 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12857 printf("ctl_datamove: tag 0x%04x on (%ju:%d:%ju:%d) aborted\n",
12858 io->scsiio.tag_num,(uintmax_t)io->io_hdr.nexus.initid.id,
12859 io->io_hdr.nexus.targ_port,
12860 (uintmax_t)io->io_hdr.nexus.targ_target.id,
12861 io->io_hdr.nexus.targ_lun);
12862 io->io_hdr.port_status = 31337;
12864 * Note that the backend, in this case, will get the
12865 * callback in its context. In other cases it may get
12866 * called in the frontend's interrupt thread context.
12868 io->scsiio.be_move_done(io);
12872 /* Don't confuse frontend with zero length data move. */
12873 if (io->scsiio.kern_data_len == 0) {
12874 io->scsiio.be_move_done(io);
12879 * If we're in XFER mode and this I/O is from the other shelf
12880 * controller, we need to send the DMA to the other side to
12881 * actually transfer the data to/from the host. In serialize only
12882 * mode the transfer happens below CTL and ctl_datamove() is only
12883 * called on the machine that originally received the I/O.
12885 if ((control_softc->ha_mode == CTL_HA_MODE_XFER)
12886 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12887 union ctl_ha_msg msg;
12888 uint32_t sg_entries_sent;
12892 memset(&msg, 0, sizeof(msg));
12893 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
12894 msg.hdr.original_sc = io->io_hdr.original_sc;
12895 msg.hdr.serializing_sc = io;
12896 msg.hdr.nexus = io->io_hdr.nexus;
12897 msg.dt.flags = io->io_hdr.flags;
12899 * We convert everything into a S/G list here. We can't
12900 * pass by reference, only by value between controllers.
12901 * So we can't pass a pointer to the S/G list, only as many
12902 * S/G entries as we can fit in here. If it's possible for
12903 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
12904 * then we need to break this up into multiple transfers.
12906 if (io->scsiio.kern_sg_entries == 0) {
12907 msg.dt.kern_sg_entries = 1;
12909 * If this is in cached memory, flush the cache
12910 * before we send the DMA request to the other
12911 * controller. We want to do this in either the
12912 * read or the write case. The read case is
12913 * straightforward. In the write case, we want to
12914 * make sure nothing is in the local cache that
12915 * could overwrite the DMAed data.
12917 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12919 * XXX KDM use bus_dmamap_sync() here.
12924 * Convert to a physical address if this is a
12927 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
12928 msg.dt.sg_list[0].addr =
12929 io->scsiio.kern_data_ptr;
12932 * XXX KDM use busdma here!
12935 msg.dt.sg_list[0].addr = (void *)
12936 vtophys(io->scsiio.kern_data_ptr);
12940 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
12943 struct ctl_sg_entry *sgl;
12946 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
12947 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
12948 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
12950 * XXX KDM use bus_dmamap_sync() here.
12955 msg.dt.kern_data_len = io->scsiio.kern_data_len;
12956 msg.dt.kern_total_len = io->scsiio.kern_total_len;
12957 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
12958 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
12959 msg.dt.sg_sequence = 0;
12962 * Loop until we've sent all of the S/G entries. On the
12963 * other end, we'll recompose these S/G entries into one
12964 * contiguous list before passing it to the
12966 for (sg_entries_sent = 0; sg_entries_sent <
12967 msg.dt.kern_sg_entries; msg.dt.sg_sequence++) {
12968 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list)/
12969 sizeof(msg.dt.sg_list[0])),
12970 msg.dt.kern_sg_entries - sg_entries_sent);
12972 if (do_sg_copy != 0) {
12973 struct ctl_sg_entry *sgl;
12976 sgl = (struct ctl_sg_entry *)
12977 io->scsiio.kern_data_ptr;
12979 * If this is in cached memory, flush the cache
12980 * before we send the DMA request to the other
12981 * controller. We want to do this in either
12982 * the * read or the write case. The read
12983 * case is straightforward. In the write
12984 * case, we want to make sure nothing is
12985 * in the local cache that could overwrite
12989 for (i = sg_entries_sent, j = 0;
12990 i < msg.dt.cur_sg_entries; i++, j++) {
12991 if ((io->io_hdr.flags &
12992 CTL_FLAG_NO_DATASYNC) == 0) {
12994 * XXX KDM use bus_dmamap_sync()
12997 if ((io->io_hdr.flags &
12998 CTL_FLAG_BUS_ADDR) == 0) {
13000 * XXX KDM use busdma.
13003 msg.dt.sg_list[j].addr =(void *)
13004 vtophys(sgl[i].addr);
13007 msg.dt.sg_list[j].addr =
13010 msg.dt.sg_list[j].len = sgl[i].len;
13014 sg_entries_sent += msg.dt.cur_sg_entries;
13015 if (sg_entries_sent >= msg.dt.kern_sg_entries)
13016 msg.dt.sg_last = 1;
13018 msg.dt.sg_last = 0;
13021 * XXX KDM drop and reacquire the lock here?
13023 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13024 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13026 * XXX do something here.
13030 msg.dt.sent_sg_entries = sg_entries_sent;
13032 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13033 if (io->io_hdr.flags & CTL_FLAG_FAILOVER)
13034 ctl_failover_io(io, /*have_lock*/ 0);
13039 * Lookup the fe_datamove() function for this particular
13043 control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13050 ctl_send_datamove_done(union ctl_io *io, int have_lock)
13052 union ctl_ha_msg msg;
13055 memset(&msg, 0, sizeof(msg));
13057 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
13058 msg.hdr.original_sc = io;
13059 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13060 msg.hdr.nexus = io->io_hdr.nexus;
13061 msg.hdr.status = io->io_hdr.status;
13062 msg.scsi.tag_num = io->scsiio.tag_num;
13063 msg.scsi.tag_type = io->scsiio.tag_type;
13064 msg.scsi.scsi_status = io->scsiio.scsi_status;
13065 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13066 sizeof(io->scsiio.sense_data));
13067 msg.scsi.sense_len = io->scsiio.sense_len;
13068 msg.scsi.sense_residual = io->scsiio.sense_residual;
13069 msg.scsi.fetd_status = io->io_hdr.port_status;
13070 msg.scsi.residual = io->scsiio.residual;
13071 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13073 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13074 ctl_failover_io(io, /*have_lock*/ have_lock);
13078 isc_status = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0);
13079 if (isc_status > CTL_HA_STATUS_SUCCESS) {
13080 /* XXX do something if this fails */
13086 * The DMA to the remote side is done, now we need to tell the other side
13087 * we're done so it can continue with its data movement.
13090 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
13096 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13097 printf("%s: ISC DMA write failed with error %d", __func__,
13099 ctl_set_internal_failure(&io->scsiio,
13101 /*retry_count*/ rq->ret);
13104 ctl_dt_req_free(rq);
13107 * In this case, we had to malloc the memory locally. Free it.
13109 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
13111 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13112 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13115 * The data is in local and remote memory, so now we need to send
13116 * status (good or back) back to the other side.
13118 ctl_send_datamove_done(io, /*have_lock*/ 0);
13122 * We've moved the data from the host/controller into local memory. Now we
13123 * need to push it over to the remote controller's memory.
13126 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
13132 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
13133 ctl_datamove_remote_write_cb);
13139 ctl_datamove_remote_write(union ctl_io *io)
13142 void (*fe_datamove)(union ctl_io *io);
13145 * - Get the data from the host/HBA into local memory.
13146 * - DMA memory from the local controller to the remote controller.
13147 * - Send status back to the remote controller.
13150 retval = ctl_datamove_remote_sgl_setup(io);
13154 /* Switch the pointer over so the FETD knows what to do */
13155 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13158 * Use a custom move done callback, since we need to send completion
13159 * back to the other controller, not to the backend on this side.
13161 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
13163 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13172 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
13181 * In this case, we had to malloc the memory locally. Free it.
13183 if ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0) {
13185 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13186 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13190 scsi_path_string(io, path_str, sizeof(path_str));
13191 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13192 sbuf_cat(&sb, path_str);
13193 scsi_command_string(&io->scsiio, NULL, &sb);
13194 sbuf_printf(&sb, "\n");
13195 sbuf_cat(&sb, path_str);
13196 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13197 io->scsiio.tag_num, io->scsiio.tag_type);
13198 sbuf_cat(&sb, path_str);
13199 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
13200 io->io_hdr.flags, io->io_hdr.status);
13202 printk("%s", sbuf_data(&sb));
13207 * The read is done, now we need to send status (good or bad) back
13208 * to the other side.
13210 ctl_send_datamove_done(io, /*have_lock*/ 0);
13216 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
13219 void (*fe_datamove)(union ctl_io *io);
13223 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
13224 printf("%s: ISC DMA read failed with error %d", __func__,
13226 ctl_set_internal_failure(&io->scsiio,
13228 /*retry_count*/ rq->ret);
13231 ctl_dt_req_free(rq);
13233 /* Switch the pointer over so the FETD knows what to do */
13234 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
13237 * Use a custom move done callback, since we need to send completion
13238 * back to the other controller, not to the backend on this side.
13240 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
13242 /* XXX KDM add checks like the ones in ctl_datamove? */
13244 fe_datamove = control_softc->ctl_ports[ctl_port_idx(io->io_hdr.nexus.targ_port)]->fe_datamove;
13250 ctl_datamove_remote_sgl_setup(union ctl_io *io)
13252 struct ctl_sg_entry *local_sglist, *remote_sglist;
13253 struct ctl_sg_entry *local_dma_sglist, *remote_dma_sglist;
13254 struct ctl_softc *softc;
13259 softc = control_softc;
13261 local_sglist = io->io_hdr.local_sglist;
13262 local_dma_sglist = io->io_hdr.local_dma_sglist;
13263 remote_sglist = io->io_hdr.remote_sglist;
13264 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13266 if (io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) {
13267 for (i = 0; i < io->scsiio.kern_sg_entries; i++) {
13268 local_sglist[i].len = remote_sglist[i].len;
13271 * XXX Detect the situation where the RS-level I/O
13272 * redirector on the other side has already read the
13273 * data off of the AOR RS on this side, and
13274 * transferred it to remote (mirror) memory on the
13275 * other side. Since we already have the data in
13276 * memory here, we just need to use it.
13278 * XXX KDM this can probably be removed once we
13279 * get the cache device code in and take the
13280 * current AOR implementation out.
13283 if ((remote_sglist[i].addr >=
13284 (void *)vtophys(softc->mirr->addr))
13285 && (remote_sglist[i].addr <
13286 ((void *)vtophys(softc->mirr->addr) +
13287 CacheMirrorOffset))) {
13288 local_sglist[i].addr = remote_sglist[i].addr -
13290 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13292 io->io_hdr.flags |= CTL_FLAG_REDIR_DONE;
13294 local_sglist[i].addr = remote_sglist[i].addr +
13299 printf("%s: local %p, remote %p, len %d\n",
13300 __func__, local_sglist[i].addr,
13301 remote_sglist[i].addr, local_sglist[i].len);
13305 uint32_t len_to_go;
13308 * In this case, we don't have automatically allocated
13309 * memory for this I/O on this controller. This typically
13310 * happens with internal CTL I/O -- e.g. inquiry, mode
13311 * sense, etc. Anything coming from RAIDCore will have
13312 * a mirror area available.
13314 len_to_go = io->scsiio.kern_data_len;
13317 * Clear the no datasync flag, we have to use malloced
13320 io->io_hdr.flags &= ~CTL_FLAG_NO_DATASYNC;
13323 * The difficult thing here is that the size of the various
13324 * S/G segments may be different than the size from the
13325 * remote controller. That'll make it harder when DMAing
13326 * the data back to the other side.
13328 for (i = 0; (i < sizeof(io->io_hdr.remote_sglist) /
13329 sizeof(io->io_hdr.remote_sglist[0])) &&
13330 (len_to_go > 0); i++) {
13331 local_sglist[i].len = MIN(len_to_go, 131072);
13332 CTL_SIZE_8B(local_dma_sglist[i].len,
13333 local_sglist[i].len);
13334 local_sglist[i].addr =
13335 malloc(local_dma_sglist[i].len, M_CTL,M_WAITOK);
13337 local_dma_sglist[i].addr = local_sglist[i].addr;
13339 if (local_sglist[i].addr == NULL) {
13342 printf("malloc failed for %zd bytes!",
13343 local_dma_sglist[i].len);
13344 for (j = 0; j < i; j++) {
13345 free(local_sglist[j].addr, M_CTL);
13347 ctl_set_internal_failure(&io->scsiio,
13349 /*retry_count*/ 4857);
13351 goto bailout_error;
13354 /* XXX KDM do we need a sync here? */
13356 len_to_go -= local_sglist[i].len;
13359 * Reset the number of S/G entries accordingly. The
13360 * original number of S/G entries is available in
13363 io->scsiio.kern_sg_entries = i;
13366 printf("%s: kern_sg_entries = %d\n", __func__,
13367 io->scsiio.kern_sg_entries);
13368 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13369 printf("%s: sg[%d] = %p, %d (DMA: %d)\n", __func__, i,
13370 local_sglist[i].addr, local_sglist[i].len,
13371 local_dma_sglist[i].len);
13380 ctl_send_datamove_done(io, /*have_lock*/ 0);
13386 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
13387 ctl_ha_dt_cb callback)
13389 struct ctl_ha_dt_req *rq;
13390 struct ctl_sg_entry *remote_sglist, *local_sglist;
13391 struct ctl_sg_entry *remote_dma_sglist, *local_dma_sglist;
13392 uint32_t local_used, remote_used, total_used;
13398 rq = ctl_dt_req_alloc();
13401 * If we failed to allocate the request, and if the DMA didn't fail
13402 * anyway, set busy status. This is just a resource allocation
13406 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE))
13407 ctl_set_busy(&io->scsiio);
13409 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE) {
13412 ctl_dt_req_free(rq);
13415 * The data move failed. We need to return status back
13416 * to the other controller. No point in trying to DMA
13417 * data to the remote controller.
13420 ctl_send_datamove_done(io, /*have_lock*/ 0);
13427 local_sglist = io->io_hdr.local_sglist;
13428 local_dma_sglist = io->io_hdr.local_dma_sglist;
13429 remote_sglist = io->io_hdr.remote_sglist;
13430 remote_dma_sglist = io->io_hdr.remote_dma_sglist;
13435 if (io->io_hdr.flags & CTL_FLAG_REDIR_DONE) {
13436 rq->ret = CTL_HA_STATUS_SUCCESS;
13443 * Pull/push the data over the wire from/to the other controller.
13444 * This takes into account the possibility that the local and
13445 * remote sglists may not be identical in terms of the size of
13446 * the elements and the number of elements.
13448 * One fundamental assumption here is that the length allocated for
13449 * both the local and remote sglists is identical. Otherwise, we've
13450 * essentially got a coding error of some sort.
13452 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13454 uint32_t cur_len, dma_length;
13457 rq->id = CTL_HA_DATA_CTL;
13458 rq->command = command;
13462 * Both pointers should be aligned. But it is possible
13463 * that the allocation length is not. They should both
13464 * also have enough slack left over at the end, though,
13465 * to round up to the next 8 byte boundary.
13467 cur_len = MIN(local_sglist[i].len - local_used,
13468 remote_sglist[j].len - remote_used);
13471 * In this case, we have a size issue and need to decrease
13472 * the size, except in the case where we actually have less
13473 * than 8 bytes left. In that case, we need to increase
13474 * the DMA length to get the last bit.
13476 if ((cur_len & 0x7) != 0) {
13477 if (cur_len > 0x7) {
13478 cur_len = cur_len - (cur_len & 0x7);
13479 dma_length = cur_len;
13481 CTL_SIZE_8B(dma_length, cur_len);
13485 dma_length = cur_len;
13488 * If we had to allocate memory for this I/O, instead of using
13489 * the non-cached mirror memory, we'll need to flush the cache
13490 * before trying to DMA to the other controller.
13492 * We could end up doing this multiple times for the same
13493 * segment if we have a larger local segment than remote
13494 * segment. That shouldn't be an issue.
13496 if ((io->io_hdr.flags & CTL_FLAG_NO_DATASYNC) == 0) {
13498 * XXX KDM use bus_dmamap_sync() here.
13502 rq->size = dma_length;
13504 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13505 tmp_ptr += local_used;
13507 /* Use physical addresses when talking to ISC hardware */
13508 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13509 /* XXX KDM use busdma */
13511 rq->local = vtophys(tmp_ptr);
13514 rq->local = tmp_ptr;
13516 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13517 tmp_ptr += remote_used;
13518 rq->remote = tmp_ptr;
13520 rq->callback = NULL;
13522 local_used += cur_len;
13523 if (local_used >= local_sglist[i].len) {
13528 remote_used += cur_len;
13529 if (remote_used >= remote_sglist[j].len) {
13533 total_used += cur_len;
13535 if (total_used >= io->scsiio.kern_data_len)
13536 rq->callback = callback;
13538 if ((rq->size & 0x7) != 0) {
13539 printf("%s: warning: size %d is not on 8b boundary\n",
13540 __func__, rq->size);
13542 if (((uintptr_t)rq->local & 0x7) != 0) {
13543 printf("%s: warning: local %p not on 8b boundary\n",
13544 __func__, rq->local);
13546 if (((uintptr_t)rq->remote & 0x7) != 0) {
13547 printf("%s: warning: remote %p not on 8b boundary\n",
13548 __func__, rq->local);
13551 printf("%s: %s: local %#x remote %#x size %d\n", __func__,
13552 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13553 rq->local, rq->remote, rq->size);
13556 isc_ret = ctl_dt_single(rq);
13557 if (isc_ret == CTL_HA_STATUS_WAIT)
13560 if (isc_ret == CTL_HA_STATUS_DISCONNECT) {
13561 rq->ret = CTL_HA_STATUS_SUCCESS;
13575 ctl_datamove_remote_read(union ctl_io *io)
13581 * This will send an error to the other controller in the case of a
13584 retval = ctl_datamove_remote_sgl_setup(io);
13588 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13589 ctl_datamove_remote_read_cb);
13591 && ((io->io_hdr.flags & CTL_FLAG_AUTO_MIRROR) == 0)) {
13593 * Make sure we free memory if there was an error.. The
13594 * ctl_datamove_remote_xfer() function will send the
13595 * datamove done message, or call the callback with an
13596 * error if there is a problem.
13598 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13599 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13606 * Process a datamove request from the other controller. This is used for
13607 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13608 * first. Once that is complete, the data gets DMAed into the remote
13609 * controller's memory. For reads, we DMA from the remote controller's
13610 * memory into our memory first, and then move it out to the FETD.
13613 ctl_datamove_remote(union ctl_io *io)
13615 struct ctl_softc *softc;
13617 softc = control_softc;
13619 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
13622 * Note that we look for an aborted I/O here, but don't do some of
13623 * the other checks that ctl_datamove() normally does.
13624 * We don't need to run the datamove delay code, since that should
13625 * have been done if need be on the other controller.
13627 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13628 printf("%s: tag 0x%04x on (%d:%d:%d:%d) aborted\n", __func__,
13629 io->scsiio.tag_num, io->io_hdr.nexus.initid.id,
13630 io->io_hdr.nexus.targ_port,
13631 io->io_hdr.nexus.targ_target.id,
13632 io->io_hdr.nexus.targ_lun);
13633 io->io_hdr.port_status = 31338;
13634 ctl_send_datamove_done(io, /*have_lock*/ 0);
13638 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT) {
13639 ctl_datamove_remote_write(io);
13640 } else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN){
13641 ctl_datamove_remote_read(io);
13643 union ctl_ha_msg msg;
13644 struct scsi_sense_data *sense;
13648 memset(&msg, 0, sizeof(msg));
13650 msg.hdr.msg_type = CTL_MSG_BAD_JUJU;
13651 msg.hdr.status = CTL_SCSI_ERROR;
13652 msg.scsi.scsi_status = SCSI_STATUS_CHECK_COND;
13654 retry_count = 4243;
13656 sense = &msg.scsi.sense_data;
13657 sks[0] = SSD_SCS_VALID;
13658 sks[1] = (retry_count >> 8) & 0xff;
13659 sks[2] = retry_count & 0xff;
13661 /* "Internal target failure" */
13662 scsi_set_sense_data(sense,
13663 /*sense_format*/ SSD_TYPE_NONE,
13664 /*current_error*/ 1,
13665 /*sense_key*/ SSD_KEY_HARDWARE_ERROR,
13668 /*type*/ SSD_ELEM_SKS,
13669 /*size*/ sizeof(sks),
13673 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
13674 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13675 ctl_failover_io(io, /*have_lock*/ 1);
13679 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg), 0) >
13680 CTL_HA_STATUS_SUCCESS) {
13681 /* XXX KDM what to do if this fails? */
13689 ctl_process_done(union ctl_io *io)
13691 struct ctl_lun *lun;
13692 struct ctl_softc *softc = control_softc;
13693 void (*fe_done)(union ctl_io *io);
13694 uint32_t targ_port = ctl_port_idx(io->io_hdr.nexus.targ_port);
13696 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13698 fe_done = softc->ctl_ports[targ_port]->fe_done;
13701 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13706 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13707 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13709 sbuf_cat(&sb, path_str);
13710 switch (io->io_hdr.io_type) {
13712 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13713 sbuf_printf(&sb, "\n");
13714 sbuf_cat(&sb, path_str);
13715 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13716 io->scsiio.tag_num, io->scsiio.tag_type);
13719 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13720 "Tag Type: %d\n", io->taskio.task_action,
13721 io->taskio.tag_num, io->taskio.tag_type);
13724 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13725 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13728 sbuf_cat(&sb, path_str);
13729 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13730 (intmax_t)time_uptime - io->io_hdr.start_time);
13732 printf("%s", sbuf_data(&sb));
13734 #endif /* CTL_TIME_IO */
13736 switch (io->io_hdr.io_type) {
13740 if (bootverbose || (ctl_debug & CTL_DEBUG_INFO))
13741 ctl_io_error_print(io, NULL);
13742 if (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)
13746 return (CTL_RETVAL_COMPLETE);
13748 panic("ctl_process_done: invalid io type %d\n",
13749 io->io_hdr.io_type);
13750 break; /* NOTREACHED */
13753 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13755 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13756 io->io_hdr.nexus.targ_mapped_lun));
13760 mtx_lock(&lun->lun_lock);
13763 * Check to see if we have any errors to inject here. We only
13764 * inject errors for commands that don't already have errors set.
13766 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
13767 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13768 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13769 ctl_inject_error(lun, io);
13772 * XXX KDM how do we treat commands that aren't completed
13775 * XXX KDM should we also track I/O latency?
13777 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13778 io->io_hdr.io_type == CTL_IO_SCSI) {
13780 struct bintime cur_bt;
13784 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13786 type = CTL_STATS_READ;
13787 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13789 type = CTL_STATS_WRITE;
13791 type = CTL_STATS_NO_IO;
13793 lun->stats.ports[targ_port].bytes[type] +=
13794 io->scsiio.kern_total_len;
13795 lun->stats.ports[targ_port].operations[type]++;
13797 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13798 &io->io_hdr.dma_bt);
13799 lun->stats.ports[targ_port].num_dmas[type] +=
13800 io->io_hdr.num_dmas;
13801 getbintime(&cur_bt);
13802 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13803 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13808 * Remove this from the OOA queue.
13810 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13812 if (TAILQ_EMPTY(&lun->ooa_queue))
13813 lun->last_busy = getsbinuptime();
13817 * Run through the blocked queue on this LUN and see if anything
13818 * has become unblocked, now that this transaction is done.
13820 ctl_check_blocked(lun);
13823 * If the LUN has been invalidated, free it if there is nothing
13824 * left on its OOA queue.
13826 if ((lun->flags & CTL_LUN_INVALID)
13827 && TAILQ_EMPTY(&lun->ooa_queue)) {
13828 mtx_unlock(&lun->lun_lock);
13829 mtx_lock(&softc->ctl_lock);
13831 mtx_unlock(&softc->ctl_lock);
13833 mtx_unlock(&lun->lun_lock);
13838 * If this command has been aborted, make sure we set the status
13839 * properly. The FETD is responsible for freeing the I/O and doing
13840 * whatever it needs to do to clean up its state.
13842 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13843 ctl_set_task_aborted(&io->scsiio);
13846 * If enabled, print command error status.
13847 * We don't print UAs unless debugging was enabled explicitly.
13850 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
13852 if (!bootverbose && (ctl_debug & CTL_DEBUG_INFO) == 0)
13854 if ((ctl_debug & CTL_DEBUG_INFO) == 0 &&
13855 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SCSI_ERROR) &&
13856 (io->scsiio.scsi_status == SCSI_STATUS_CHECK_COND)) {
13857 int error_code, sense_key, asc, ascq;
13859 scsi_extract_sense_len(&io->scsiio.sense_data,
13860 io->scsiio.sense_len, &error_code, &sense_key,
13861 &asc, &ascq, /*show_errors*/ 0);
13862 if (sense_key == SSD_KEY_UNIT_ATTENTION)
13866 ctl_io_error_print(io, NULL);
13870 * Tell the FETD or the other shelf controller we're done with this
13871 * command. Note that only SCSI commands get to this point. Task
13872 * management commands are completed above.
13874 * We only send status to the other controller if we're in XFER
13875 * mode. In SER_ONLY mode, the I/O is done on the controller that
13876 * received the I/O (from CTL's perspective), and so the status is
13879 * XXX KDM if we hold the lock here, we could cause a deadlock
13880 * if the frontend comes back in in this context to queue
13883 if ((softc->ha_mode == CTL_HA_MODE_XFER)
13884 && (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
13885 union ctl_ha_msg msg;
13887 memset(&msg, 0, sizeof(msg));
13888 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13889 msg.hdr.original_sc = io->io_hdr.original_sc;
13890 msg.hdr.nexus = io->io_hdr.nexus;
13891 msg.hdr.status = io->io_hdr.status;
13892 msg.scsi.scsi_status = io->scsiio.scsi_status;
13893 msg.scsi.tag_num = io->scsiio.tag_num;
13894 msg.scsi.tag_type = io->scsiio.tag_type;
13895 msg.scsi.sense_len = io->scsiio.sense_len;
13896 msg.scsi.sense_residual = io->scsiio.sense_residual;
13897 msg.scsi.residual = io->scsiio.residual;
13898 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
13899 sizeof(io->scsiio.sense_data));
13901 * We copy this whether or not this is an I/O-related
13902 * command. Otherwise, we'd have to go and check to see
13903 * whether it's a read/write command, and it really isn't
13906 memcpy(&msg.scsi.lbalen,
13907 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN].bytes,
13908 sizeof(msg.scsi.lbalen));
13910 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13911 sizeof(msg), 0) > CTL_HA_STATUS_SUCCESS) {
13912 /* XXX do something here */
13919 return (CTL_RETVAL_COMPLETE);
13924 * Front end should call this if it doesn't do autosense. When the request
13925 * sense comes back in from the initiator, we'll dequeue this and send it.
13928 ctl_queue_sense(union ctl_io *io)
13930 struct ctl_lun *lun;
13931 struct ctl_port *port;
13932 struct ctl_softc *softc;
13933 uint32_t initidx, targ_lun;
13935 softc = control_softc;
13937 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13940 * LUN lookup will likely move to the ctl_work_thread() once we
13941 * have our new queueing infrastructure (that doesn't put things on
13942 * a per-LUN queue initially). That is so that we can handle
13943 * things like an INQUIRY to a LUN that we don't have enabled. We
13944 * can't deal with that right now.
13946 mtx_lock(&softc->ctl_lock);
13949 * If we don't have a LUN for this, just toss the sense
13952 port = ctl_io_port(&ctsio->io_hdr);
13953 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13954 if ((targ_lun < CTL_MAX_LUNS)
13955 && (softc->ctl_luns[targ_lun] != NULL))
13956 lun = softc->ctl_luns[targ_lun];
13960 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13962 mtx_lock(&lun->lun_lock);
13964 * Already have CA set for this LUN...toss the sense information.
13966 if (ctl_is_set(lun->have_ca, initidx)) {
13967 mtx_unlock(&lun->lun_lock);
13971 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13972 MIN(sizeof(lun->pending_sense[initidx]),
13973 sizeof(io->scsiio.sense_data)));
13974 ctl_set_mask(lun->have_ca, initidx);
13975 mtx_unlock(&lun->lun_lock);
13978 mtx_unlock(&softc->ctl_lock);
13982 return (CTL_RETVAL_COMPLETE);
13987 * Primary command inlet from frontend ports. All SCSI and task I/O
13988 * requests must go through this function.
13991 ctl_queue(union ctl_io *io)
13993 struct ctl_port *port;
13995 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13998 io->io_hdr.start_time = time_uptime;
13999 getbintime(&io->io_hdr.start_bt);
14000 #endif /* CTL_TIME_IO */
14002 /* Map FE-specific LUN ID into global one. */
14003 port = ctl_io_port(&io->io_hdr);
14004 io->io_hdr.nexus.targ_mapped_lun =
14005 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
14007 switch (io->io_hdr.io_type) {
14010 if (ctl_debug & CTL_DEBUG_CDB)
14012 ctl_enqueue_incoming(io);
14015 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
14019 return (CTL_RETVAL_COMPLETE);
14022 #ifdef CTL_IO_DELAY
14024 ctl_done_timer_wakeup(void *arg)
14028 io = (union ctl_io *)arg;
14031 #endif /* CTL_IO_DELAY */
14034 ctl_done(union ctl_io *io)
14038 * Enable this to catch duplicate completion issues.
14041 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
14042 printf("%s: type %d msg %d cdb %x iptl: "
14043 "%d:%d:%d:%d tag 0x%04x "
14044 "flag %#x status %x\n",
14046 io->io_hdr.io_type,
14047 io->io_hdr.msg_type,
14049 io->io_hdr.nexus.initid.id,
14050 io->io_hdr.nexus.targ_port,
14051 io->io_hdr.nexus.targ_target.id,
14052 io->io_hdr.nexus.targ_lun,
14053 (io->io_hdr.io_type ==
14055 io->taskio.tag_num :
14056 io->scsiio.tag_num,
14058 io->io_hdr.status);
14060 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
14064 * This is an internal copy of an I/O, and should not go through
14065 * the normal done processing logic.
14067 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
14071 * We need to send a msg to the serializing shelf to finish the IO
14072 * as well. We don't send a finish message to the other shelf if
14073 * this is a task management command. Task management commands
14074 * aren't serialized in the OOA queue, but rather just executed on
14075 * both shelf controllers for commands that originated on that
14078 if ((io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)
14079 && (io->io_hdr.io_type != CTL_IO_TASK)) {
14080 union ctl_ha_msg msg_io;
14082 msg_io.hdr.msg_type = CTL_MSG_FINISH_IO;
14083 msg_io.hdr.serializing_sc = io->io_hdr.serializing_sc;
14084 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_io,
14085 sizeof(msg_io), 0 ) != CTL_HA_STATUS_SUCCESS) {
14087 /* continue on to finish IO */
14089 #ifdef CTL_IO_DELAY
14090 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
14091 struct ctl_lun *lun;
14093 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14095 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
14097 struct ctl_lun *lun;
14099 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14102 && (lun->delay_info.done_delay > 0)) {
14103 struct callout *callout;
14105 callout = (struct callout *)&io->io_hdr.timer_bytes;
14106 callout_init(callout, /*mpsafe*/ 1);
14107 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
14108 callout_reset(callout,
14109 lun->delay_info.done_delay * hz,
14110 ctl_done_timer_wakeup, io);
14111 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
14112 lun->delay_info.done_delay = 0;
14116 #endif /* CTL_IO_DELAY */
14118 ctl_enqueue_done(io);
14122 ctl_isc(struct ctl_scsiio *ctsio)
14124 struct ctl_lun *lun;
14127 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
14129 CTL_DEBUG_PRINT(("ctl_isc: command: %02x\n", ctsio->cdb[0]));
14131 CTL_DEBUG_PRINT(("ctl_isc: calling data_submit()\n"));
14133 retval = lun->backend->data_submit((union ctl_io *)ctsio);
14140 ctl_work_thread(void *arg)
14142 struct ctl_thread *thr = (struct ctl_thread *)arg;
14143 struct ctl_softc *softc = thr->ctl_softc;
14147 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
14153 * We handle the queues in this order:
14155 * - done queue (to free up resources, unblock other commands)
14159 * If those queues are empty, we break out of the loop and
14162 mtx_lock(&thr->queue_lock);
14163 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
14165 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
14166 mtx_unlock(&thr->queue_lock);
14167 ctl_handle_isc(io);
14170 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
14172 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
14173 /* clear any blocked commands, call fe_done */
14174 mtx_unlock(&thr->queue_lock);
14175 retval = ctl_process_done(io);
14178 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
14180 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
14181 mtx_unlock(&thr->queue_lock);
14182 if (io->io_hdr.io_type == CTL_IO_TASK)
14185 ctl_scsiio_precheck(softc, &io->scsiio);
14188 if (!ctl_pause_rtr) {
14189 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
14191 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
14192 mtx_unlock(&thr->queue_lock);
14193 retval = ctl_scsiio(&io->scsiio);
14194 if (retval != CTL_RETVAL_COMPLETE)
14195 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
14200 /* Sleep until we have something to do. */
14201 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
14206 ctl_lun_thread(void *arg)
14208 struct ctl_softc *softc = (struct ctl_softc *)arg;
14209 struct ctl_be_lun *be_lun;
14212 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
14216 mtx_lock(&softc->ctl_lock);
14217 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
14218 if (be_lun != NULL) {
14219 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
14220 mtx_unlock(&softc->ctl_lock);
14221 ctl_create_lun(be_lun);
14225 /* Sleep until we have something to do. */
14226 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
14227 PDROP | PRIBIO, "-", 0);
14232 ctl_thresh_thread(void *arg)
14234 struct ctl_softc *softc = (struct ctl_softc *)arg;
14235 struct ctl_lun *lun;
14236 struct ctl_be_lun *be_lun;
14237 struct scsi_da_rw_recovery_page *rwpage;
14238 struct ctl_logical_block_provisioning_page *page;
14240 uint64_t thres, val;
14243 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
14246 mtx_lock(&softc->ctl_lock);
14247 STAILQ_FOREACH(lun, &softc->lun_list, links) {
14248 be_lun = lun->be_lun;
14249 if ((lun->flags & CTL_LUN_DISABLED) ||
14250 (lun->flags & CTL_LUN_OFFLINE) ||
14251 lun->backend->lun_attr == NULL)
14253 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
14254 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
14257 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
14258 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
14259 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
14261 thres = scsi_4btoul(page->descr[i].count);
14262 thres <<= CTL_LBP_EXPONENT;
14263 switch (page->descr[i].resource) {
14265 attr = "blocksavail";
14268 attr = "blocksused";
14271 attr = "poolblocksavail";
14274 attr = "poolblocksused";
14279 mtx_unlock(&softc->ctl_lock); // XXX
14280 val = lun->backend->lun_attr(
14281 lun->be_lun->be_lun, attr);
14282 mtx_lock(&softc->ctl_lock);
14283 if (val == UINT64_MAX)
14285 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
14286 == SLBPPD_ARMING_INC)
14287 e |= (val >= thres);
14289 e |= (val <= thres);
14291 mtx_lock(&lun->lun_lock);
14293 if (lun->lasttpt == 0 ||
14294 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
14295 lun->lasttpt = time_uptime;
14296 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
14300 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
14302 mtx_unlock(&lun->lun_lock);
14304 mtx_unlock(&softc->ctl_lock);
14305 pause("-", CTL_LBP_PERIOD * hz);
14310 ctl_enqueue_incoming(union ctl_io *io)
14312 struct ctl_softc *softc = control_softc;
14313 struct ctl_thread *thr;
14316 idx = (io->io_hdr.nexus.targ_port * 127 +
14317 io->io_hdr.nexus.initid.id) % worker_threads;
14318 thr = &softc->threads[idx];
14319 mtx_lock(&thr->queue_lock);
14320 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
14321 mtx_unlock(&thr->queue_lock);
14326 ctl_enqueue_rtr(union ctl_io *io)
14328 struct ctl_softc *softc = control_softc;
14329 struct ctl_thread *thr;
14331 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14332 mtx_lock(&thr->queue_lock);
14333 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
14334 mtx_unlock(&thr->queue_lock);
14339 ctl_enqueue_done(union ctl_io *io)
14341 struct ctl_softc *softc = control_softc;
14342 struct ctl_thread *thr;
14344 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14345 mtx_lock(&thr->queue_lock);
14346 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
14347 mtx_unlock(&thr->queue_lock);
14352 ctl_enqueue_isc(union ctl_io *io)
14354 struct ctl_softc *softc = control_softc;
14355 struct ctl_thread *thr;
14357 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
14358 mtx_lock(&thr->queue_lock);
14359 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
14360 mtx_unlock(&thr->queue_lock);
14364 /* Initialization and failover */
14367 ctl_init_isc_msg(void)
14369 printf("CTL: Still calling this thing\n");
14374 * Initializes component into configuration defined by bootMode
14376 * returns hasc_Status:
14378 * ERROR - fatal error
14380 static ctl_ha_comp_status
14381 ctl_isc_init(struct ctl_ha_component *c)
14383 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14390 * Starts component in state requested. If component starts successfully,
14391 * it must set its own state to the requestrd state
14392 * When requested state is HASC_STATE_HA, the component may refine it
14393 * by adding _SLAVE or _MASTER flags.
14394 * Currently allowed state transitions are:
14395 * UNKNOWN->HA - initial startup
14396 * UNKNOWN->SINGLE - initial startup when no parter detected
14397 * HA->SINGLE - failover
14398 * returns ctl_ha_comp_status:
14399 * OK - component successfully started in requested state
14400 * FAILED - could not start the requested state, failover may
14402 * ERROR - fatal error detected, no future startup possible
14404 static ctl_ha_comp_status
14405 ctl_isc_start(struct ctl_ha_component *c, ctl_ha_state state)
14407 ctl_ha_comp_status ret = CTL_HA_COMP_STATUS_OK;
14409 printf("%s: go\n", __func__);
14411 // UNKNOWN->HA or UNKNOWN->SINGLE (bootstrap)
14412 if (c->state == CTL_HA_STATE_UNKNOWN ) {
14413 control_softc->is_single = 0;
14414 if (ctl_ha_msg_create(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
14415 != CTL_HA_STATUS_SUCCESS) {
14416 printf("ctl_isc_start: ctl_ha_msg_create failed.\n");
14417 ret = CTL_HA_COMP_STATUS_ERROR;
14419 } else if (CTL_HA_STATE_IS_HA(c->state)
14420 && CTL_HA_STATE_IS_SINGLE(state)){
14421 // HA->SINGLE transition
14423 control_softc->is_single = 1;
14425 printf("ctl_isc_start:Invalid state transition %X->%X\n",
14427 ret = CTL_HA_COMP_STATUS_ERROR;
14429 if (CTL_HA_STATE_IS_SINGLE(state))
14430 control_softc->is_single = 1;
14438 * Quiesce component
14439 * The component must clear any error conditions (set status to OK) and
14440 * prepare itself to another Start call
14441 * returns ctl_ha_comp_status:
14445 static ctl_ha_comp_status
14446 ctl_isc_quiesce(struct ctl_ha_component *c)
14448 int ret = CTL_HA_COMP_STATUS_OK;
14455 struct ctl_ha_component ctl_ha_component_ctlisc =
14458 .state = CTL_HA_STATE_UNKNOWN,
14459 .init = ctl_isc_init,
14460 .start = ctl_isc_start,
14461 .quiesce = ctl_isc_quiesce