2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
38 * CAM Target Layer, a SCSI device emulation subsystem.
40 * Author: Ken Merry <ken@FreeBSD.org>
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/ctype.h>
49 #include <sys/kernel.h>
50 #include <sys/types.h>
51 #include <sys/kthread.h>
53 #include <sys/fcntl.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/malloc.h>
60 #include <sys/ioccom.h>
61 #include <sys/queue.h>
64 #include <sys/endian.h>
65 #include <sys/sysctl.h>
69 #include <cam/scsi/scsi_all.h>
70 #include <cam/scsi/scsi_cd.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_util.h>
76 #include <cam/ctl/ctl_backend.h>
77 #include <cam/ctl/ctl_ioctl.h>
78 #include <cam/ctl/ctl_ha.h>
79 #include <cam/ctl/ctl_private.h>
80 #include <cam/ctl/ctl_debug.h>
81 #include <cam/ctl/ctl_scsi_all.h>
82 #include <cam/ctl/ctl_error.h>
84 struct ctl_softc *control_softc = NULL;
87 * Template mode pages.
91 * Note that these are default values only. The actual values will be
92 * filled in when the user does a mode sense.
94 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
95 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
96 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
97 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
98 /*read_retry_count*/0,
100 /*head_offset_count*/0,
101 /*data_strobe_offset_cnt*/0,
102 /*byte8*/SMS_RWER_LBPERE,
103 /*write_retry_count*/0,
105 /*recovery_time_limit*/{0, 0},
108 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
109 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
110 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
111 /*byte3*/SMS_RWER_PER,
112 /*read_retry_count*/0,
113 /*correction_span*/0,
114 /*head_offset_count*/0,
115 /*data_strobe_offset_cnt*/0,
116 /*byte8*/SMS_RWER_LBPERE,
117 /*write_retry_count*/0,
119 /*recovery_time_limit*/{0, 0},
122 const static struct scsi_format_page format_page_default = {
123 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
124 /*page_length*/sizeof(struct scsi_format_page) - 2,
125 /*tracks_per_zone*/ {0, 0},
126 /*alt_sectors_per_zone*/ {0, 0},
127 /*alt_tracks_per_zone*/ {0, 0},
128 /*alt_tracks_per_lun*/ {0, 0},
129 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
130 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
131 /*bytes_per_sector*/ {0, 0},
132 /*interleave*/ {0, 0},
133 /*track_skew*/ {0, 0},
134 /*cylinder_skew*/ {0, 0},
136 /*reserved*/ {0, 0, 0}
139 const static struct scsi_format_page format_page_changeable = {
140 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
141 /*page_length*/sizeof(struct scsi_format_page) - 2,
142 /*tracks_per_zone*/ {0, 0},
143 /*alt_sectors_per_zone*/ {0, 0},
144 /*alt_tracks_per_zone*/ {0, 0},
145 /*alt_tracks_per_lun*/ {0, 0},
146 /*sectors_per_track*/ {0, 0},
147 /*bytes_per_sector*/ {0, 0},
148 /*interleave*/ {0, 0},
149 /*track_skew*/ {0, 0},
150 /*cylinder_skew*/ {0, 0},
152 /*reserved*/ {0, 0, 0}
155 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
156 /*page_code*/SMS_RIGID_DISK_PAGE,
157 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
158 /*cylinders*/ {0, 0, 0},
159 /*heads*/ CTL_DEFAULT_HEADS,
160 /*start_write_precomp*/ {0, 0, 0},
161 /*start_reduced_current*/ {0, 0, 0},
162 /*step_rate*/ {0, 0},
163 /*landing_zone_cylinder*/ {0, 0, 0},
164 /*rpl*/ SRDP_RPL_DISABLED,
165 /*rotational_offset*/ 0,
167 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
168 CTL_DEFAULT_ROTATION_RATE & 0xff},
172 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
173 /*page_code*/SMS_RIGID_DISK_PAGE,
174 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
175 /*cylinders*/ {0, 0, 0},
177 /*start_write_precomp*/ {0, 0, 0},
178 /*start_reduced_current*/ {0, 0, 0},
179 /*step_rate*/ {0, 0},
180 /*landing_zone_cylinder*/ {0, 0, 0},
182 /*rotational_offset*/ 0,
184 /*rotation_rate*/ {0, 0},
188 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
189 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
190 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
192 /*read_retry_count*/0,
193 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
194 /*recovery_time_limit*/{0, 0},
197 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
198 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
199 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
200 /*byte3*/SMS_VER_PER,
201 /*read_retry_count*/0,
202 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
203 /*recovery_time_limit*/{0, 0},
206 const static struct scsi_caching_page caching_page_default = {
207 /*page_code*/SMS_CACHING_PAGE,
208 /*page_length*/sizeof(struct scsi_caching_page) - 2,
209 /*flags1*/ SCP_DISC | SCP_WCE,
211 /*disable_pf_transfer_len*/ {0xff, 0xff},
212 /*min_prefetch*/ {0, 0},
213 /*max_prefetch*/ {0xff, 0xff},
214 /*max_pf_ceiling*/ {0xff, 0xff},
216 /*cache_segments*/ 0,
217 /*cache_seg_size*/ {0, 0},
219 /*non_cache_seg_size*/ {0, 0, 0}
222 const static struct scsi_caching_page caching_page_changeable = {
223 /*page_code*/SMS_CACHING_PAGE,
224 /*page_length*/sizeof(struct scsi_caching_page) - 2,
225 /*flags1*/ SCP_WCE | SCP_RCD,
227 /*disable_pf_transfer_len*/ {0, 0},
228 /*min_prefetch*/ {0, 0},
229 /*max_prefetch*/ {0, 0},
230 /*max_pf_ceiling*/ {0, 0},
232 /*cache_segments*/ 0,
233 /*cache_seg_size*/ {0, 0},
235 /*non_cache_seg_size*/ {0, 0, 0}
238 const static struct scsi_control_page control_page_default = {
239 /*page_code*/SMS_CONTROL_MODE_PAGE,
240 /*page_length*/sizeof(struct scsi_control_page) - 2,
242 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
245 /*aen_holdoff_period*/{0, 0},
246 /*busy_timeout_period*/{0, 0},
247 /*extended_selftest_completion_time*/{0, 0}
250 const static struct scsi_control_page control_page_changeable = {
251 /*page_code*/SMS_CONTROL_MODE_PAGE,
252 /*page_length*/sizeof(struct scsi_control_page) - 2,
254 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
255 /*eca_and_aen*/SCP_SWP,
257 /*aen_holdoff_period*/{0, 0},
258 /*busy_timeout_period*/{0, 0},
259 /*extended_selftest_completion_time*/{0, 0}
262 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
264 const static struct scsi_control_ext_page control_ext_page_default = {
265 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
266 /*subpage_code*/0x01,
267 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
273 const static struct scsi_control_ext_page control_ext_page_changeable = {
274 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
275 /*subpage_code*/0x01,
276 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
282 const static struct scsi_info_exceptions_page ie_page_default = {
283 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
284 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
285 /*info_flags*/SIEP_FLAGS_EWASC,
286 /*mrie*/SIEP_MRIE_NO,
287 /*interval_timer*/{0, 0, 0, 0},
288 /*report_count*/{0, 0, 0, 1}
291 const static struct scsi_info_exceptions_page ie_page_changeable = {
292 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
293 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
294 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
297 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
298 /*report_count*/{0xff, 0xff, 0xff, 0xff}
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},
333 /*flags*/SLBPP_SITUA,
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}}
355 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
356 /*page_code*/SMS_CDDVD_CAPS_PAGE,
357 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
365 /*nvol_levels*/{0, 0},
366 /*buffer_size*/{8, 0},
371 /*copy_management*/0,
373 /*rotation_control*/0,
374 /*cur_write_speed*/0,
375 /*num_speed_descr*/0,
378 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
379 /*page_code*/SMS_CDDVD_CAPS_PAGE,
380 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
388 /*nvol_levels*/{0, 0},
389 /*buffer_size*/{0, 0},
394 /*copy_management*/0,
396 /*rotation_control*/0,
397 /*cur_write_speed*/0,
398 /*num_speed_descr*/0,
401 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
402 static int worker_threads = -1;
403 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
404 &worker_threads, 1, "Number of worker threads");
405 static int ctl_debug = CTL_DEBUG_NONE;
406 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
407 &ctl_debug, 0, "Enabled debug flags");
408 static int ctl_lun_map_size = 1024;
409 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
410 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
412 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS;
413 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN,
414 &ctl_time_io_secs, 0, "Log requests taking more seconds");
418 * Maximum number of LUNs we support. MUST be a power of 2.
420 #define CTL_DEFAULT_MAX_LUNS 1024
421 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
422 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns);
423 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN,
424 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs");
427 * Maximum number of ports registered at one time.
429 #define CTL_DEFAULT_MAX_PORTS 256
430 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
431 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports);
432 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN,
433 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports");
436 * Maximum number of initiators we support.
438 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports)
441 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
442 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
443 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
444 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
446 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
448 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
450 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
451 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
452 static int ctl_init(void);
453 static int ctl_shutdown(void);
454 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
455 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
456 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
457 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
458 struct ctl_ooa *ooa_hdr,
459 struct ctl_ooa_entry *kern_entries);
460 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
462 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
463 struct ctl_be_lun *be_lun);
464 static int ctl_free_lun(struct ctl_lun *lun);
465 static void ctl_create_lun(struct ctl_be_lun *be_lun);
467 static int ctl_do_mode_select(union ctl_io *io);
468 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
469 uint64_t res_key, uint64_t sa_res_key,
470 uint8_t type, uint32_t residx,
471 struct ctl_scsiio *ctsio,
472 struct scsi_per_res_out *cdb,
473 struct scsi_per_res_out_parms* param);
474 static void ctl_pro_preempt_other(struct ctl_lun *lun,
475 union ctl_ha_msg *msg);
476 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
477 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
478 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
479 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
480 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
481 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
482 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
484 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
486 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
487 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
488 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
489 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
490 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
491 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
493 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
494 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
495 union ctl_io *pending_io, union ctl_io *ooa_io);
496 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
497 union ctl_io *starting_io);
498 static int ctl_check_blocked(struct ctl_lun *lun);
499 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
500 const struct ctl_cmd_entry *entry,
501 struct ctl_scsiio *ctsio);
502 static void ctl_failover_lun(union ctl_io *io);
503 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
504 struct ctl_scsiio *ctsio);
505 static int ctl_scsiio(struct ctl_scsiio *ctsio);
507 static int ctl_target_reset(union ctl_io *io);
508 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
509 ctl_ua_type ua_type);
510 static int ctl_lun_reset(union ctl_io *io);
511 static int ctl_abort_task(union ctl_io *io);
512 static int ctl_abort_task_set(union ctl_io *io);
513 static int ctl_query_task(union ctl_io *io, int task_set);
514 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
515 ctl_ua_type ua_type);
516 static int ctl_i_t_nexus_reset(union ctl_io *io);
517 static int ctl_query_async_event(union ctl_io *io);
518 static void ctl_run_task(union ctl_io *io);
520 static void ctl_datamove_timer_wakeup(void *arg);
521 static void ctl_done_timer_wakeup(void *arg);
522 #endif /* CTL_IO_DELAY */
524 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
525 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
526 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
527 static void ctl_datamove_remote_write(union ctl_io *io);
528 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
529 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
530 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
531 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
532 ctl_ha_dt_cb callback);
533 static void ctl_datamove_remote_read(union ctl_io *io);
534 static void ctl_datamove_remote(union ctl_io *io);
535 static void ctl_process_done(union ctl_io *io);
536 static void ctl_lun_thread(void *arg);
537 static void ctl_thresh_thread(void *arg);
538 static void ctl_work_thread(void *arg);
539 static void ctl_enqueue_incoming(union ctl_io *io);
540 static void ctl_enqueue_rtr(union ctl_io *io);
541 static void ctl_enqueue_done(union ctl_io *io);
542 static void ctl_enqueue_isc(union ctl_io *io);
543 static const struct ctl_cmd_entry *
544 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
545 static const struct ctl_cmd_entry *
546 ctl_validate_command(struct ctl_scsiio *ctsio);
547 static int ctl_cmd_applicable(uint8_t lun_type,
548 const struct ctl_cmd_entry *entry);
549 static int ctl_ha_init(void);
550 static int ctl_ha_shutdown(void);
552 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
553 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
554 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
555 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
558 * Load the serialization table. This isn't very pretty, but is probably
559 * the easiest way to do it.
561 #include "ctl_ser_table.c"
564 * We only need to define open, close and ioctl routines for this driver.
566 static struct cdevsw ctl_cdevsw = {
567 .d_version = D_VERSION,
570 .d_close = ctl_close,
571 .d_ioctl = ctl_ioctl,
576 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
578 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
580 static moduledata_t ctl_moduledata = {
582 ctl_module_event_handler,
586 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
587 MODULE_VERSION(ctl, 1);
589 static struct ctl_frontend ha_frontend =
593 .shutdown = ctl_ha_shutdown,
599 struct ctl_softc *softc = control_softc;
601 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
602 &softc->othersc_pool) != 0)
604 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
605 ctl_pool_free(softc->othersc_pool);
608 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
609 != CTL_HA_STATUS_SUCCESS) {
610 ctl_ha_msg_destroy(softc);
611 ctl_pool_free(softc->othersc_pool);
618 ctl_ha_shutdown(void)
620 struct ctl_softc *softc = control_softc;
621 struct ctl_port *port;
623 ctl_ha_msg_shutdown(softc);
624 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
626 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
628 ctl_pool_free(softc->othersc_pool);
629 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
630 ctl_port_deregister(port);
631 free(port->port_name, M_CTL);
638 ctl_ha_datamove(union ctl_io *io)
640 struct ctl_lun *lun = CTL_LUN(io);
641 struct ctl_sg_entry *sgl;
642 union ctl_ha_msg msg;
643 uint32_t sg_entries_sent;
644 int do_sg_copy, i, j;
646 memset(&msg.dt, 0, sizeof(msg.dt));
647 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
648 msg.hdr.original_sc = io->io_hdr.original_sc;
649 msg.hdr.serializing_sc = io;
650 msg.hdr.nexus = io->io_hdr.nexus;
651 msg.hdr.status = io->io_hdr.status;
652 msg.dt.flags = io->io_hdr.flags;
655 * We convert everything into a S/G list here. We can't
656 * pass by reference, only by value between controllers.
657 * So we can't pass a pointer to the S/G list, only as many
658 * S/G entries as we can fit in here. If it's possible for
659 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
660 * then we need to break this up into multiple transfers.
662 if (io->scsiio.kern_sg_entries == 0) {
663 msg.dt.kern_sg_entries = 1;
665 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
666 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
668 /* XXX KDM use busdma here! */
669 msg.dt.sg_list[0].addr =
670 (void *)vtophys(io->scsiio.kern_data_ptr);
673 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
674 ("HA does not support BUS_ADDR"));
675 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
677 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
680 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
684 msg.dt.kern_data_len = io->scsiio.kern_data_len;
685 msg.dt.kern_total_len = io->scsiio.kern_total_len;
686 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
687 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
688 msg.dt.sg_sequence = 0;
691 * Loop until we've sent all of the S/G entries. On the
692 * other end, we'll recompose these S/G entries into one
693 * contiguous list before processing.
695 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
696 msg.dt.sg_sequence++) {
697 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
698 sizeof(msg.dt.sg_list[0])),
699 msg.dt.kern_sg_entries - sg_entries_sent);
700 if (do_sg_copy != 0) {
701 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
702 for (i = sg_entries_sent, j = 0;
703 i < msg.dt.cur_sg_entries; i++, j++) {
705 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
706 msg.dt.sg_list[j].addr = sgl[i].addr;
708 /* XXX KDM use busdma here! */
709 msg.dt.sg_list[j].addr =
710 (void *)vtophys(sgl[i].addr);
713 KASSERT((io->io_hdr.flags &
714 CTL_FLAG_BUS_ADDR) == 0,
715 ("HA does not support BUS_ADDR"));
716 msg.dt.sg_list[j].addr = sgl[i].addr;
718 msg.dt.sg_list[j].len = sgl[i].len;
722 sg_entries_sent += msg.dt.cur_sg_entries;
723 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
724 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
725 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
726 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
727 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
728 io->io_hdr.port_status = 31341;
729 io->scsiio.be_move_done(io);
732 msg.dt.sent_sg_entries = sg_entries_sent;
736 * Officially handover the request from us to peer.
737 * If failover has just happened, then we must return error.
738 * If failover happen just after, then it is not our problem.
741 mtx_lock(&lun->lun_lock);
742 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
744 mtx_unlock(&lun->lun_lock);
745 io->io_hdr.port_status = 31342;
746 io->scsiio.be_move_done(io);
749 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
750 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
752 mtx_unlock(&lun->lun_lock);
756 ctl_ha_done(union ctl_io *io)
758 union ctl_ha_msg msg;
760 if (io->io_hdr.io_type == CTL_IO_SCSI) {
761 memset(&msg, 0, sizeof(msg));
762 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
763 msg.hdr.original_sc = io->io_hdr.original_sc;
764 msg.hdr.nexus = io->io_hdr.nexus;
765 msg.hdr.status = io->io_hdr.status;
766 msg.scsi.scsi_status = io->scsiio.scsi_status;
767 msg.scsi.tag_num = io->scsiio.tag_num;
768 msg.scsi.tag_type = io->scsiio.tag_type;
769 msg.scsi.sense_len = io->scsiio.sense_len;
770 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
771 io->scsiio.sense_len);
772 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
773 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
774 msg.scsi.sense_len, M_WAITOK);
780 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
781 union ctl_ha_msg *msg_info)
783 struct ctl_scsiio *ctsio;
785 if (msg_info->hdr.original_sc == NULL) {
786 printf("%s: original_sc == NULL!\n", __func__);
787 /* XXX KDM now what? */
791 ctsio = &msg_info->hdr.original_sc->scsiio;
792 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
793 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
794 ctsio->io_hdr.status = msg_info->hdr.status;
795 ctsio->scsi_status = msg_info->scsi.scsi_status;
796 ctsio->sense_len = msg_info->scsi.sense_len;
797 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
798 msg_info->scsi.sense_len);
799 ctl_enqueue_isc((union ctl_io *)ctsio);
803 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
804 union ctl_ha_msg *msg_info)
806 struct ctl_scsiio *ctsio;
808 if (msg_info->hdr.serializing_sc == NULL) {
809 printf("%s: serializing_sc == NULL!\n", __func__);
810 /* XXX KDM now what? */
814 ctsio = &msg_info->hdr.serializing_sc->scsiio;
815 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
816 ctl_enqueue_isc((union ctl_io *)ctsio);
820 ctl_isc_announce_lun(struct ctl_lun *lun)
822 struct ctl_softc *softc = lun->ctl_softc;
823 union ctl_ha_msg *msg;
824 struct ctl_ha_msg_lun_pr_key pr_key;
827 if (softc->ha_link != CTL_HA_LINK_ONLINE)
829 mtx_lock(&lun->lun_lock);
830 i = sizeof(msg->lun);
832 i += lun->lun_devid->len;
833 i += sizeof(pr_key) * lun->pr_key_count;
835 mtx_unlock(&lun->lun_lock);
836 msg = malloc(i, M_CTL, M_WAITOK);
837 mtx_lock(&lun->lun_lock);
838 k = sizeof(msg->lun);
840 k += lun->lun_devid->len;
841 k += sizeof(pr_key) * lun->pr_key_count;
847 bzero(&msg->lun, sizeof(msg->lun));
848 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
849 msg->hdr.nexus.targ_lun = lun->lun;
850 msg->hdr.nexus.targ_mapped_lun = lun->lun;
851 msg->lun.flags = lun->flags;
852 msg->lun.pr_generation = lun->pr_generation;
853 msg->lun.pr_res_idx = lun->pr_res_idx;
854 msg->lun.pr_res_type = lun->pr_res_type;
855 msg->lun.pr_key_count = lun->pr_key_count;
857 if (lun->lun_devid) {
858 msg->lun.lun_devid_len = lun->lun_devid->len;
859 memcpy(&msg->lun.data[i], lun->lun_devid->data,
860 msg->lun.lun_devid_len);
861 i += msg->lun.lun_devid_len;
863 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
864 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
867 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
870 mtx_unlock(&lun->lun_lock);
871 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
875 if (lun->flags & CTL_LUN_PRIMARY_SC) {
876 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
877 ctl_isc_announce_mode(lun, -1,
878 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
879 lun->mode_pages.index[i].subpage);
885 ctl_isc_announce_port(struct ctl_port *port)
887 struct ctl_softc *softc = port->ctl_softc;
888 union ctl_ha_msg *msg;
891 if (port->targ_port < softc->port_min ||
892 port->targ_port >= softc->port_max ||
893 softc->ha_link != CTL_HA_LINK_ONLINE)
895 i = sizeof(msg->port) + strlen(port->port_name) + 1;
897 i += port->lun_map_size * sizeof(uint32_t);
898 if (port->port_devid)
899 i += port->port_devid->len;
900 if (port->target_devid)
901 i += port->target_devid->len;
902 if (port->init_devid)
903 i += port->init_devid->len;
904 msg = malloc(i, M_CTL, M_WAITOK);
905 bzero(&msg->port, sizeof(msg->port));
906 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
907 msg->hdr.nexus.targ_port = port->targ_port;
908 msg->port.port_type = port->port_type;
909 msg->port.physical_port = port->physical_port;
910 msg->port.virtual_port = port->virtual_port;
911 msg->port.status = port->status;
913 msg->port.name_len = sprintf(&msg->port.data[i],
914 "%d:%s", softc->ha_id, port->port_name) + 1;
915 i += msg->port.name_len;
917 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
918 memcpy(&msg->port.data[i], port->lun_map,
919 msg->port.lun_map_len);
920 i += msg->port.lun_map_len;
922 if (port->port_devid) {
923 msg->port.port_devid_len = port->port_devid->len;
924 memcpy(&msg->port.data[i], port->port_devid->data,
925 msg->port.port_devid_len);
926 i += msg->port.port_devid_len;
928 if (port->target_devid) {
929 msg->port.target_devid_len = port->target_devid->len;
930 memcpy(&msg->port.data[i], port->target_devid->data,
931 msg->port.target_devid_len);
932 i += msg->port.target_devid_len;
934 if (port->init_devid) {
935 msg->port.init_devid_len = port->init_devid->len;
936 memcpy(&msg->port.data[i], port->init_devid->data,
937 msg->port.init_devid_len);
938 i += msg->port.init_devid_len;
940 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
946 ctl_isc_announce_iid(struct ctl_port *port, int iid)
948 struct ctl_softc *softc = port->ctl_softc;
949 union ctl_ha_msg *msg;
952 if (port->targ_port < softc->port_min ||
953 port->targ_port >= softc->port_max ||
954 softc->ha_link != CTL_HA_LINK_ONLINE)
956 mtx_lock(&softc->ctl_lock);
957 i = sizeof(msg->iid);
959 if (port->wwpn_iid[iid].name)
960 l = strlen(port->wwpn_iid[iid].name) + 1;
962 msg = malloc(i, M_CTL, M_NOWAIT);
964 mtx_unlock(&softc->ctl_lock);
967 bzero(&msg->iid, sizeof(msg->iid));
968 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
969 msg->hdr.nexus.targ_port = port->targ_port;
970 msg->hdr.nexus.initid = iid;
971 msg->iid.in_use = port->wwpn_iid[iid].in_use;
972 msg->iid.name_len = l;
973 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
974 if (port->wwpn_iid[iid].name)
975 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
976 mtx_unlock(&softc->ctl_lock);
977 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
982 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
983 uint8_t page, uint8_t subpage)
985 struct ctl_softc *softc = lun->ctl_softc;
986 union ctl_ha_msg msg;
989 if (softc->ha_link != CTL_HA_LINK_ONLINE)
991 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
992 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
993 page && lun->mode_pages.index[i].subpage == subpage)
996 if (i == CTL_NUM_MODE_PAGES)
999 /* Don't try to replicate pages not present on this device. */
1000 if (lun->mode_pages.index[i].page_data == NULL)
1003 bzero(&msg.mode, sizeof(msg.mode));
1004 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
1005 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
1006 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
1007 msg.hdr.nexus.targ_lun = lun->lun;
1008 msg.hdr.nexus.targ_mapped_lun = lun->lun;
1009 msg.mode.page_code = page;
1010 msg.mode.subpage = subpage;
1011 msg.mode.page_len = lun->mode_pages.index[i].page_len;
1012 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
1014 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
1019 ctl_isc_ha_link_up(struct ctl_softc *softc)
1021 struct ctl_port *port;
1022 struct ctl_lun *lun;
1023 union ctl_ha_msg msg;
1026 /* Announce this node parameters to peer for validation. */
1027 msg.login.msg_type = CTL_MSG_LOGIN;
1028 msg.login.version = CTL_HA_VERSION;
1029 msg.login.ha_mode = softc->ha_mode;
1030 msg.login.ha_id = softc->ha_id;
1031 msg.login.max_luns = ctl_max_luns;
1032 msg.login.max_ports = ctl_max_ports;
1033 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1034 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1037 STAILQ_FOREACH(port, &softc->port_list, links) {
1038 ctl_isc_announce_port(port);
1039 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1040 if (port->wwpn_iid[i].in_use)
1041 ctl_isc_announce_iid(port, i);
1044 STAILQ_FOREACH(lun, &softc->lun_list, links)
1045 ctl_isc_announce_lun(lun);
1049 ctl_isc_ha_link_down(struct ctl_softc *softc)
1051 struct ctl_port *port;
1052 struct ctl_lun *lun;
1056 mtx_lock(&softc->ctl_lock);
1057 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1058 mtx_lock(&lun->lun_lock);
1059 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1060 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1061 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1063 mtx_unlock(&lun->lun_lock);
1065 mtx_unlock(&softc->ctl_lock);
1066 io = ctl_alloc_io(softc->othersc_pool);
1067 mtx_lock(&softc->ctl_lock);
1069 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1070 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1071 ctl_enqueue_isc(io);
1074 STAILQ_FOREACH(port, &softc->port_list, links) {
1075 if (port->targ_port >= softc->port_min &&
1076 port->targ_port < softc->port_max)
1078 port->status &= ~CTL_PORT_STATUS_ONLINE;
1079 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1080 port->wwpn_iid[i].in_use = 0;
1081 free(port->wwpn_iid[i].name, M_CTL);
1082 port->wwpn_iid[i].name = NULL;
1085 mtx_unlock(&softc->ctl_lock);
1089 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1091 struct ctl_lun *lun;
1092 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1094 mtx_lock(&softc->ctl_lock);
1095 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns ||
1096 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1097 mtx_unlock(&softc->ctl_lock);
1100 mtx_lock(&lun->lun_lock);
1101 mtx_unlock(&softc->ctl_lock);
1102 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1103 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1104 if (msg->ua.ua_all) {
1106 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1108 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1111 ctl_est_ua(lun, iid, msg->ua.ua_type);
1113 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1115 mtx_unlock(&lun->lun_lock);
1119 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1121 struct ctl_lun *lun;
1122 struct ctl_ha_msg_lun_pr_key pr_key;
1124 ctl_lun_flags oflags;
1127 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1128 mtx_lock(&softc->ctl_lock);
1129 if (targ_lun >= ctl_max_luns ||
1130 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1131 mtx_unlock(&softc->ctl_lock);
1134 mtx_lock(&lun->lun_lock);
1135 mtx_unlock(&softc->ctl_lock);
1136 if (lun->flags & CTL_LUN_DISABLED) {
1137 mtx_unlock(&lun->lun_lock);
1140 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1141 if (msg->lun.lun_devid_len != i || (i > 0 &&
1142 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1143 mtx_unlock(&lun->lun_lock);
1144 printf("%s: Received conflicting HA LUN %d\n",
1145 __func__, targ_lun);
1148 /* Record whether peer is primary. */
1149 oflags = lun->flags;
1150 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1151 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1152 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1154 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1155 if (oflags != lun->flags)
1156 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1158 /* If peer is primary and we are not -- use data */
1159 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1160 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1161 lun->pr_generation = msg->lun.pr_generation;
1162 lun->pr_res_idx = msg->lun.pr_res_idx;
1163 lun->pr_res_type = msg->lun.pr_res_type;
1164 lun->pr_key_count = msg->lun.pr_key_count;
1165 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1166 ctl_clr_prkey(lun, k);
1167 for (k = 0; k < msg->lun.pr_key_count; k++) {
1168 memcpy(&pr_key, &msg->lun.data[i],
1170 ctl_alloc_prkey(lun, pr_key.pr_iid);
1171 ctl_set_prkey(lun, pr_key.pr_iid,
1173 i += sizeof(pr_key);
1177 mtx_unlock(&lun->lun_lock);
1178 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1180 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1181 "primary" : "secondary"));
1183 /* If we are primary but peer doesn't know -- notify */
1184 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1185 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1186 ctl_isc_announce_lun(lun);
1191 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1193 struct ctl_port *port;
1194 struct ctl_lun *lun;
1197 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1199 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1200 msg->hdr.nexus.targ_port));
1202 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1203 port->frontend = &ha_frontend;
1204 port->targ_port = msg->hdr.nexus.targ_port;
1205 port->fe_datamove = ctl_ha_datamove;
1206 port->fe_done = ctl_ha_done;
1207 } else if (port->frontend == &ha_frontend) {
1208 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1209 msg->hdr.nexus.targ_port));
1212 printf("%s: Received conflicting HA port %d\n",
1213 __func__, msg->hdr.nexus.targ_port);
1216 port->port_type = msg->port.port_type;
1217 port->physical_port = msg->port.physical_port;
1218 port->virtual_port = msg->port.virtual_port;
1219 port->status = msg->port.status;
1221 free(port->port_name, M_CTL);
1222 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1224 i += msg->port.name_len;
1225 if (msg->port.lun_map_len != 0) {
1226 if (port->lun_map == NULL ||
1227 port->lun_map_size * sizeof(uint32_t) <
1228 msg->port.lun_map_len) {
1229 port->lun_map_size = 0;
1230 free(port->lun_map, M_CTL);
1231 port->lun_map = malloc(msg->port.lun_map_len,
1234 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1235 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1236 i += msg->port.lun_map_len;
1238 port->lun_map_size = 0;
1239 free(port->lun_map, M_CTL);
1240 port->lun_map = NULL;
1242 if (msg->port.port_devid_len != 0) {
1243 if (port->port_devid == NULL ||
1244 port->port_devid->len < msg->port.port_devid_len) {
1245 free(port->port_devid, M_CTL);
1246 port->port_devid = malloc(sizeof(struct ctl_devid) +
1247 msg->port.port_devid_len, M_CTL, M_WAITOK);
1249 memcpy(port->port_devid->data, &msg->port.data[i],
1250 msg->port.port_devid_len);
1251 port->port_devid->len = msg->port.port_devid_len;
1252 i += msg->port.port_devid_len;
1254 free(port->port_devid, M_CTL);
1255 port->port_devid = NULL;
1257 if (msg->port.target_devid_len != 0) {
1258 if (port->target_devid == NULL ||
1259 port->target_devid->len < msg->port.target_devid_len) {
1260 free(port->target_devid, M_CTL);
1261 port->target_devid = malloc(sizeof(struct ctl_devid) +
1262 msg->port.target_devid_len, M_CTL, M_WAITOK);
1264 memcpy(port->target_devid->data, &msg->port.data[i],
1265 msg->port.target_devid_len);
1266 port->target_devid->len = msg->port.target_devid_len;
1267 i += msg->port.target_devid_len;
1269 free(port->target_devid, M_CTL);
1270 port->target_devid = NULL;
1272 if (msg->port.init_devid_len != 0) {
1273 if (port->init_devid == NULL ||
1274 port->init_devid->len < msg->port.init_devid_len) {
1275 free(port->init_devid, M_CTL);
1276 port->init_devid = malloc(sizeof(struct ctl_devid) +
1277 msg->port.init_devid_len, M_CTL, M_WAITOK);
1279 memcpy(port->init_devid->data, &msg->port.data[i],
1280 msg->port.init_devid_len);
1281 port->init_devid->len = msg->port.init_devid_len;
1282 i += msg->port.init_devid_len;
1284 free(port->init_devid, M_CTL);
1285 port->init_devid = NULL;
1288 if (ctl_port_register(port) != 0) {
1289 printf("%s: ctl_port_register() failed with error\n",
1293 mtx_lock(&softc->ctl_lock);
1294 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1295 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1297 mtx_lock(&lun->lun_lock);
1298 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1299 mtx_unlock(&lun->lun_lock);
1301 mtx_unlock(&softc->ctl_lock);
1305 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1307 struct ctl_port *port;
1310 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1312 printf("%s: Received IID for unknown port %d\n",
1313 __func__, msg->hdr.nexus.targ_port);
1316 iid = msg->hdr.nexus.initid;
1317 if (port->wwpn_iid[iid].in_use != 0 &&
1318 msg->iid.in_use == 0)
1319 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1320 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1321 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1322 free(port->wwpn_iid[iid].name, M_CTL);
1323 if (msg->iid.name_len) {
1324 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1325 msg->iid.name_len, M_CTL);
1327 port->wwpn_iid[iid].name = NULL;
1331 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1334 if (msg->login.version != CTL_HA_VERSION) {
1335 printf("CTL HA peers have different versions %d != %d\n",
1336 msg->login.version, CTL_HA_VERSION);
1337 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1340 if (msg->login.ha_mode != softc->ha_mode) {
1341 printf("CTL HA peers have different ha_mode %d != %d\n",
1342 msg->login.ha_mode, softc->ha_mode);
1343 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1346 if (msg->login.ha_id == softc->ha_id) {
1347 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1348 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1351 if (msg->login.max_luns != ctl_max_luns ||
1352 msg->login.max_ports != ctl_max_ports ||
1353 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1354 printf("CTL HA peers have different limits\n");
1355 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1361 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1363 struct ctl_lun *lun;
1365 uint32_t initidx, targ_lun;
1367 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1368 mtx_lock(&softc->ctl_lock);
1369 if (targ_lun >= ctl_max_luns ||
1370 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1371 mtx_unlock(&softc->ctl_lock);
1374 mtx_lock(&lun->lun_lock);
1375 mtx_unlock(&softc->ctl_lock);
1376 if (lun->flags & CTL_LUN_DISABLED) {
1377 mtx_unlock(&lun->lun_lock);
1380 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1381 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1382 msg->mode.page_code &&
1383 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1386 if (i == CTL_NUM_MODE_PAGES) {
1387 mtx_unlock(&lun->lun_lock);
1390 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1391 lun->mode_pages.index[i].page_len);
1392 initidx = ctl_get_initindex(&msg->hdr.nexus);
1394 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1395 mtx_unlock(&lun->lun_lock);
1399 * ISC (Inter Shelf Communication) event handler. Events from the HA
1400 * subsystem come in here.
1403 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1405 struct ctl_softc *softc = control_softc;
1407 struct ctl_prio *presio;
1408 ctl_ha_status isc_status;
1410 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1411 if (event == CTL_HA_EVT_MSG_RECV) {
1412 union ctl_ha_msg *msg, msgbuf;
1414 if (param > sizeof(msgbuf))
1415 msg = malloc(param, M_CTL, M_WAITOK);
1418 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1420 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1421 printf("%s: Error receiving message: %d\n",
1422 __func__, isc_status);
1428 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1429 switch (msg->hdr.msg_type) {
1430 case CTL_MSG_SERIALIZE:
1431 io = ctl_alloc_io(softc->othersc_pool);
1433 // populate ctsio from msg
1434 io->io_hdr.io_type = CTL_IO_SCSI;
1435 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1436 io->io_hdr.original_sc = msg->hdr.original_sc;
1437 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1440 * If we're in serialization-only mode, we don't
1441 * want to go through full done processing. Thus
1444 * XXX KDM add another flag that is more specific.
1446 if (softc->ha_mode != CTL_HA_MODE_XFER)
1447 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1448 io->io_hdr.nexus = msg->hdr.nexus;
1450 printf("port %u, iid %u, lun %u\n",
1451 io->io_hdr.nexus.targ_port,
1452 io->io_hdr.nexus.initid,
1453 io->io_hdr.nexus.targ_lun);
1455 io->scsiio.tag_num = msg->scsi.tag_num;
1456 io->scsiio.tag_type = msg->scsi.tag_type;
1458 io->io_hdr.start_time = time_uptime;
1459 getbinuptime(&io->io_hdr.start_bt);
1460 #endif /* CTL_TIME_IO */
1461 io->scsiio.cdb_len = msg->scsi.cdb_len;
1462 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1464 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1465 const struct ctl_cmd_entry *entry;
1467 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1468 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1470 entry->flags & CTL_FLAG_DATA_MASK;
1472 ctl_enqueue_isc(io);
1475 /* Performed on the Originating SC, XFER mode only */
1476 case CTL_MSG_DATAMOVE: {
1477 struct ctl_sg_entry *sgl;
1480 io = msg->hdr.original_sc;
1482 printf("%s: original_sc == NULL!\n", __func__);
1483 /* XXX KDM do something here */
1486 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1487 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1489 * Keep track of this, we need to send it back over
1490 * when the datamove is complete.
1492 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1493 if (msg->hdr.status == CTL_SUCCESS)
1494 io->io_hdr.status = msg->hdr.status;
1496 if (msg->dt.sg_sequence == 0) {
1498 getbinuptime(&io->io_hdr.dma_start_bt);
1500 i = msg->dt.kern_sg_entries +
1501 msg->dt.kern_data_len /
1502 CTL_HA_DATAMOVE_SEGMENT + 1;
1503 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1505 io->io_hdr.remote_sglist = sgl;
1506 io->io_hdr.local_sglist =
1507 &sgl[msg->dt.kern_sg_entries];
1509 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1511 io->scsiio.kern_sg_entries =
1512 msg->dt.kern_sg_entries;
1513 io->scsiio.rem_sg_entries =
1514 msg->dt.kern_sg_entries;
1515 io->scsiio.kern_data_len =
1516 msg->dt.kern_data_len;
1517 io->scsiio.kern_total_len =
1518 msg->dt.kern_total_len;
1519 io->scsiio.kern_data_resid =
1520 msg->dt.kern_data_resid;
1521 io->scsiio.kern_rel_offset =
1522 msg->dt.kern_rel_offset;
1523 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1524 io->io_hdr.flags |= msg->dt.flags &
1527 sgl = (struct ctl_sg_entry *)
1528 io->scsiio.kern_data_ptr;
1530 for (i = msg->dt.sent_sg_entries, j = 0;
1531 i < (msg->dt.sent_sg_entries +
1532 msg->dt.cur_sg_entries); i++, j++) {
1533 sgl[i].addr = msg->dt.sg_list[j].addr;
1534 sgl[i].len = msg->dt.sg_list[j].len;
1537 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1538 __func__, sgl[i].addr, sgl[i].len, j, i);
1543 * If this is the last piece of the I/O, we've got
1544 * the full S/G list. Queue processing in the thread.
1545 * Otherwise wait for the next piece.
1547 if (msg->dt.sg_last != 0)
1548 ctl_enqueue_isc(io);
1551 /* Performed on the Serializing (primary) SC, XFER mode only */
1552 case CTL_MSG_DATAMOVE_DONE: {
1553 if (msg->hdr.serializing_sc == NULL) {
1554 printf("%s: serializing_sc == NULL!\n",
1556 /* XXX KDM now what? */
1560 * We grab the sense information here in case
1561 * there was a failure, so we can return status
1562 * back to the initiator.
1564 io = msg->hdr.serializing_sc;
1565 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1566 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1567 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1568 io->io_hdr.port_status = msg->scsi.port_status;
1569 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1570 if (msg->hdr.status != CTL_STATUS_NONE) {
1571 io->io_hdr.status = msg->hdr.status;
1572 io->scsiio.scsi_status = msg->scsi.scsi_status;
1573 io->scsiio.sense_len = msg->scsi.sense_len;
1574 memcpy(&io->scsiio.sense_data,
1575 &msg->scsi.sense_data,
1576 msg->scsi.sense_len);
1577 if (msg->hdr.status == CTL_SUCCESS)
1578 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1580 ctl_enqueue_isc(io);
1584 /* Preformed on Originating SC, SER_ONLY mode */
1586 io = msg->hdr.original_sc;
1588 printf("%s: original_sc == NULL!\n",
1592 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1593 io->io_hdr.msg_type = CTL_MSG_R2R;
1594 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1595 ctl_enqueue_isc(io);
1599 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1601 * Performed on the Originating (i.e. secondary) SC in XFER
1604 case CTL_MSG_FINISH_IO:
1605 if (softc->ha_mode == CTL_HA_MODE_XFER)
1606 ctl_isc_handler_finish_xfer(softc, msg);
1608 ctl_isc_handler_finish_ser_only(softc, msg);
1611 /* Preformed on Originating SC */
1612 case CTL_MSG_BAD_JUJU:
1613 io = msg->hdr.original_sc;
1615 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1619 ctl_copy_sense_data(msg, io);
1621 * IO should have already been cleaned up on other
1622 * SC so clear this flag so we won't send a message
1623 * back to finish the IO there.
1625 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1626 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1628 /* io = msg->hdr.serializing_sc; */
1629 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1630 ctl_enqueue_isc(io);
1633 /* Handle resets sent from the other side */
1634 case CTL_MSG_MANAGE_TASKS: {
1635 struct ctl_taskio *taskio;
1636 taskio = (struct ctl_taskio *)ctl_alloc_io(
1637 softc->othersc_pool);
1638 ctl_zero_io((union ctl_io *)taskio);
1639 taskio->io_hdr.io_type = CTL_IO_TASK;
1640 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1641 taskio->io_hdr.nexus = msg->hdr.nexus;
1642 taskio->task_action = msg->task.task_action;
1643 taskio->tag_num = msg->task.tag_num;
1644 taskio->tag_type = msg->task.tag_type;
1646 taskio->io_hdr.start_time = time_uptime;
1647 getbinuptime(&taskio->io_hdr.start_bt);
1648 #endif /* CTL_TIME_IO */
1649 ctl_run_task((union ctl_io *)taskio);
1652 /* Persistent Reserve action which needs attention */
1653 case CTL_MSG_PERS_ACTION:
1654 presio = (struct ctl_prio *)ctl_alloc_io(
1655 softc->othersc_pool);
1656 ctl_zero_io((union ctl_io *)presio);
1657 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1658 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1659 presio->io_hdr.nexus = msg->hdr.nexus;
1660 presio->pr_msg = msg->pr;
1661 ctl_enqueue_isc((union ctl_io *)presio);
1664 ctl_isc_ua(softc, msg, param);
1666 case CTL_MSG_PORT_SYNC:
1667 ctl_isc_port_sync(softc, msg, param);
1669 case CTL_MSG_LUN_SYNC:
1670 ctl_isc_lun_sync(softc, msg, param);
1672 case CTL_MSG_IID_SYNC:
1673 ctl_isc_iid_sync(softc, msg, param);
1676 ctl_isc_login(softc, msg, param);
1678 case CTL_MSG_MODE_SYNC:
1679 ctl_isc_mode_sync(softc, msg, param);
1682 printf("Received HA message of unknown type %d\n",
1684 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1689 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1690 printf("CTL: HA link status changed from %d to %d\n",
1691 softc->ha_link, param);
1692 if (param == softc->ha_link)
1694 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1695 softc->ha_link = param;
1696 ctl_isc_ha_link_down(softc);
1698 softc->ha_link = param;
1699 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1700 ctl_isc_ha_link_up(softc);
1704 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1710 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1713 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1714 src->scsi.sense_len);
1715 dest->scsiio.scsi_status = src->scsi.scsi_status;
1716 dest->scsiio.sense_len = src->scsi.sense_len;
1717 dest->io_hdr.status = src->hdr.status;
1721 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1724 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1725 src->scsiio.sense_len);
1726 dest->scsi.scsi_status = src->scsiio.scsi_status;
1727 dest->scsi.sense_len = src->scsiio.sense_len;
1728 dest->hdr.status = src->io_hdr.status;
1732 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1734 struct ctl_softc *softc = lun->ctl_softc;
1737 if (initidx < softc->init_min || initidx >= softc->init_max)
1739 mtx_assert(&lun->lun_lock, MA_OWNED);
1740 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1743 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1747 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1751 mtx_assert(&lun->lun_lock, MA_OWNED);
1752 if (lun->pending_ua[port] == NULL)
1754 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1755 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1757 lun->pending_ua[port][i] |= ua;
1762 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1764 struct ctl_softc *softc = lun->ctl_softc;
1767 mtx_assert(&lun->lun_lock, MA_OWNED);
1768 for (i = softc->port_min; i < softc->port_max; i++)
1769 ctl_est_ua_port(lun, i, except, ua);
1773 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1775 struct ctl_softc *softc = lun->ctl_softc;
1778 if (initidx < softc->init_min || initidx >= softc->init_max)
1780 mtx_assert(&lun->lun_lock, MA_OWNED);
1781 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1784 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1788 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1790 struct ctl_softc *softc = lun->ctl_softc;
1793 mtx_assert(&lun->lun_lock, MA_OWNED);
1794 for (i = softc->port_min; i < softc->port_max; i++) {
1795 if (lun->pending_ua[i] == NULL)
1797 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1798 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1800 lun->pending_ua[i][j] &= ~ua;
1806 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1807 ctl_ua_type ua_type)
1809 struct ctl_lun *lun;
1811 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1812 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1813 mtx_lock(&lun->lun_lock);
1814 ctl_clr_ua(lun, initidx, ua_type);
1815 mtx_unlock(&lun->lun_lock);
1820 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1822 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1823 struct ctl_lun *lun;
1824 struct ctl_lun_req ireq;
1827 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1828 error = sysctl_handle_int(oidp, &value, 0, req);
1829 if ((error != 0) || (req->newptr == NULL))
1832 mtx_lock(&softc->ctl_lock);
1834 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1836 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1837 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1838 mtx_unlock(&softc->ctl_lock);
1839 bzero(&ireq, sizeof(ireq));
1840 ireq.reqtype = CTL_LUNREQ_MODIFY;
1841 ireq.reqdata.modify.lun_id = lun->lun;
1842 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1844 if (ireq.status != CTL_LUN_OK) {
1845 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1846 __func__, ireq.status, ireq.error_str);
1848 mtx_lock(&softc->ctl_lock);
1850 mtx_unlock(&softc->ctl_lock);
1857 struct make_dev_args args;
1858 struct ctl_softc *softc;
1861 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1864 make_dev_args_init(&args);
1865 args.mda_devsw = &ctl_cdevsw;
1866 args.mda_uid = UID_ROOT;
1867 args.mda_gid = GID_OPERATOR;
1868 args.mda_mode = 0600;
1869 args.mda_si_drv1 = softc;
1870 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1872 free(softc, M_DEVBUF);
1873 control_softc = NULL;
1877 sysctl_ctx_init(&softc->sysctl_ctx);
1878 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1879 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1880 CTLFLAG_RD, 0, "CAM Target Layer");
1882 if (softc->sysctl_tree == NULL) {
1883 printf("%s: unable to allocate sysctl tree\n", __func__);
1884 destroy_dev(softc->dev);
1885 free(softc, M_DEVBUF);
1886 control_softc = NULL;
1890 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1891 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1892 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1895 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1896 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1897 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1899 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) {
1900 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n",
1901 ctl_max_luns, CTL_DEFAULT_MAX_LUNS);
1902 ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
1904 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns,
1905 M_DEVBUF, M_WAITOK | M_ZERO);
1906 softc->ctl_lun_mask = malloc(sizeof(uint32_t) *
1907 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1908 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) {
1909 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n",
1910 ctl_max_ports, CTL_DEFAULT_MAX_PORTS);
1911 ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
1913 softc->ctl_port_mask = malloc(sizeof(uint32_t) *
1914 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1915 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports,
1916 M_DEVBUF, M_WAITOK | M_ZERO);
1920 * In Copan's HA scheme, the "master" and "slave" roles are
1921 * figured out through the slot the controller is in. Although it
1922 * is an active/active system, someone has to be in charge.
1924 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1925 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1926 "HA head ID (0 - no HA)");
1927 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1928 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1929 softc->is_single = 1;
1930 softc->port_cnt = ctl_max_ports;
1931 softc->port_min = 0;
1933 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES;
1934 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1936 softc->port_max = softc->port_min + softc->port_cnt;
1937 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1938 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1940 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1941 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1942 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1944 STAILQ_INIT(&softc->lun_list);
1945 STAILQ_INIT(&softc->pending_lun_queue);
1946 STAILQ_INIT(&softc->fe_list);
1947 STAILQ_INIT(&softc->port_list);
1948 STAILQ_INIT(&softc->be_list);
1949 ctl_tpc_init(softc);
1951 if (worker_threads <= 0)
1952 worker_threads = max(1, mp_ncpus / 4);
1953 if (worker_threads > CTL_MAX_THREADS)
1954 worker_threads = CTL_MAX_THREADS;
1956 for (i = 0; i < worker_threads; i++) {
1957 struct ctl_thread *thr = &softc->threads[i];
1959 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1960 thr->ctl_softc = softc;
1961 STAILQ_INIT(&thr->incoming_queue);
1962 STAILQ_INIT(&thr->rtr_queue);
1963 STAILQ_INIT(&thr->done_queue);
1964 STAILQ_INIT(&thr->isc_queue);
1966 error = kproc_kthread_add(ctl_work_thread, thr,
1967 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1969 printf("error creating CTL work thread!\n");
1973 error = kproc_kthread_add(ctl_lun_thread, softc,
1974 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun");
1976 printf("error creating CTL lun thread!\n");
1979 error = kproc_kthread_add(ctl_thresh_thread, softc,
1980 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
1982 printf("error creating CTL threshold thread!\n");
1986 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1987 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1988 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1990 if (softc->is_single == 0) {
1991 if (ctl_frontend_register(&ha_frontend) != 0)
1992 softc->is_single = 1;
2000 struct ctl_softc *softc = control_softc;
2003 if (softc->is_single == 0)
2004 ctl_frontend_deregister(&ha_frontend);
2006 destroy_dev(softc->dev);
2008 /* Shutdown CTL threads. */
2009 softc->shutdown = 1;
2010 for (i = 0; i < worker_threads; i++) {
2011 struct ctl_thread *thr = &softc->threads[i];
2012 while (thr->thread != NULL) {
2014 if (thr->thread != NULL)
2015 pause("CTL thr shutdown", 1);
2017 mtx_destroy(&thr->queue_lock);
2019 while (softc->lun_thread != NULL) {
2020 wakeup(&softc->pending_lun_queue);
2021 if (softc->lun_thread != NULL)
2022 pause("CTL thr shutdown", 1);
2024 while (softc->thresh_thread != NULL) {
2025 wakeup(softc->thresh_thread);
2026 if (softc->thresh_thread != NULL)
2027 pause("CTL thr shutdown", 1);
2030 ctl_tpc_shutdown(softc);
2031 uma_zdestroy(softc->io_zone);
2032 mtx_destroy(&softc->ctl_lock);
2034 free(softc->ctl_luns, M_DEVBUF);
2035 free(softc->ctl_lun_mask, M_DEVBUF);
2036 free(softc->ctl_port_mask, M_DEVBUF);
2037 free(softc->ctl_ports, M_DEVBUF);
2039 sysctl_ctx_free(&softc->sysctl_ctx);
2041 free(softc, M_DEVBUF);
2042 control_softc = NULL;
2047 ctl_module_event_handler(module_t mod, int what, void *arg)
2052 return (ctl_init());
2054 return (ctl_shutdown());
2056 return (EOPNOTSUPP);
2061 * XXX KDM should we do some access checks here? Bump a reference count to
2062 * prevent a CTL module from being unloaded while someone has it open?
2065 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2071 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2077 * Remove an initiator by port number and initiator ID.
2078 * Returns 0 for success, -1 for failure.
2081 ctl_remove_initiator(struct ctl_port *port, int iid)
2083 struct ctl_softc *softc = port->ctl_softc;
2086 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2088 if (iid > CTL_MAX_INIT_PER_PORT) {
2089 printf("%s: initiator ID %u > maximun %u!\n",
2090 __func__, iid, CTL_MAX_INIT_PER_PORT);
2094 mtx_lock(&softc->ctl_lock);
2095 last = (--port->wwpn_iid[iid].in_use == 0);
2096 port->wwpn_iid[iid].last_use = time_uptime;
2097 mtx_unlock(&softc->ctl_lock);
2099 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2100 ctl_isc_announce_iid(port, iid);
2106 * Add an initiator to the initiator map.
2107 * Returns iid for success, < 0 for failure.
2110 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2112 struct ctl_softc *softc = port->ctl_softc;
2116 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2118 if (iid >= CTL_MAX_INIT_PER_PORT) {
2119 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2120 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2125 mtx_lock(&softc->ctl_lock);
2127 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2128 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2129 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2133 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2134 strcmp(name, port->wwpn_iid[i].name) == 0) {
2142 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2143 if (port->wwpn_iid[i].in_use == 0 &&
2144 port->wwpn_iid[i].wwpn == 0 &&
2145 port->wwpn_iid[i].name == NULL) {
2154 best_time = INT32_MAX;
2155 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2156 if (port->wwpn_iid[i].in_use == 0) {
2157 if (port->wwpn_iid[i].last_use < best_time) {
2159 best_time = port->wwpn_iid[i].last_use;
2167 mtx_unlock(&softc->ctl_lock);
2172 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2174 * This is not an error yet.
2176 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2178 printf("%s: port %d iid %u WWPN %#jx arrived"
2179 " again\n", __func__, port->targ_port,
2180 iid, (uintmax_t)wwpn);
2184 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2185 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2187 printf("%s: port %d iid %u name '%s' arrived"
2188 " again\n", __func__, port->targ_port,
2195 * This is an error, but what do we do about it? The
2196 * driver is telling us we have a new WWPN for this
2197 * initiator ID, so we pretty much need to use it.
2199 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2200 " but WWPN %#jx '%s' is still at that address\n",
2201 __func__, port->targ_port, iid, wwpn, name,
2202 (uintmax_t)port->wwpn_iid[iid].wwpn,
2203 port->wwpn_iid[iid].name);
2206 free(port->wwpn_iid[iid].name, M_CTL);
2207 port->wwpn_iid[iid].name = name;
2208 port->wwpn_iid[iid].wwpn = wwpn;
2209 port->wwpn_iid[iid].in_use++;
2210 mtx_unlock(&softc->ctl_lock);
2211 ctl_isc_announce_iid(port, iid);
2217 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2221 switch (port->port_type) {
2224 struct scsi_transportid_fcp *id =
2225 (struct scsi_transportid_fcp *)buf;
2226 if (port->wwpn_iid[iid].wwpn == 0)
2228 memset(id, 0, sizeof(*id));
2229 id->format_protocol = SCSI_PROTO_FC;
2230 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2231 return (sizeof(*id));
2233 case CTL_PORT_ISCSI:
2235 struct scsi_transportid_iscsi_port *id =
2236 (struct scsi_transportid_iscsi_port *)buf;
2237 if (port->wwpn_iid[iid].name == NULL)
2240 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2242 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2243 len = roundup2(min(len, 252), 4);
2244 scsi_ulto2b(len, id->additional_length);
2245 return (sizeof(*id) + len);
2249 struct scsi_transportid_sas *id =
2250 (struct scsi_transportid_sas *)buf;
2251 if (port->wwpn_iid[iid].wwpn == 0)
2253 memset(id, 0, sizeof(*id));
2254 id->format_protocol = SCSI_PROTO_SAS;
2255 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2256 return (sizeof(*id));
2260 struct scsi_transportid_spi *id =
2261 (struct scsi_transportid_spi *)buf;
2262 memset(id, 0, sizeof(*id));
2263 id->format_protocol = SCSI_PROTO_SPI;
2264 scsi_ulto2b(iid, id->scsi_addr);
2265 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2266 return (sizeof(*id));
2272 * Serialize a command that went down the "wrong" side, and so was sent to
2273 * this controller for execution. The logic is a little different than the
2274 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2275 * sent back to the other side, but in the success case, we execute the
2276 * command on this side (XFER mode) or tell the other side to execute it
2280 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2282 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2283 struct ctl_port *port = CTL_PORT(ctsio);
2284 union ctl_ha_msg msg_info;
2285 struct ctl_lun *lun;
2286 const struct ctl_cmd_entry *entry;
2289 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2291 /* Make sure that we know about this port. */
2292 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2293 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2298 /* Make sure that we know about this LUN. */
2299 mtx_lock(&softc->ctl_lock);
2300 if (targ_lun >= ctl_max_luns ||
2301 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2302 mtx_unlock(&softc->ctl_lock);
2305 * The other node would not send this request to us unless
2306 * received announce that we are primary node for this LUN.
2307 * If this LUN does not exist now, it is probably result of
2308 * a race, so respond to initiator in the most opaque way.
2310 ctl_set_busy(ctsio);
2313 mtx_lock(&lun->lun_lock);
2314 mtx_unlock(&softc->ctl_lock);
2317 * If the LUN is invalid, pretend that it doesn't exist.
2318 * It will go away as soon as all pending I/Os completed.
2320 if (lun->flags & CTL_LUN_DISABLED) {
2321 mtx_unlock(&lun->lun_lock);
2322 ctl_set_busy(ctsio);
2326 entry = ctl_get_cmd_entry(ctsio, NULL);
2327 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2328 mtx_unlock(&lun->lun_lock);
2332 CTL_LUN(ctsio) = lun;
2333 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2336 * Every I/O goes into the OOA queue for a
2337 * particular LUN, and stays there until completion.
2340 if (TAILQ_EMPTY(&lun->ooa_queue))
2341 lun->idle_time += getsbinuptime() - lun->last_busy;
2343 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2345 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2346 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2348 case CTL_ACTION_BLOCK:
2349 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2350 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2352 mtx_unlock(&lun->lun_lock);
2354 case CTL_ACTION_PASS:
2355 case CTL_ACTION_SKIP:
2356 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2357 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2358 ctl_enqueue_rtr((union ctl_io *)ctsio);
2359 mtx_unlock(&lun->lun_lock);
2361 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2362 mtx_unlock(&lun->lun_lock);
2364 /* send msg back to other side */
2365 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2366 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2367 msg_info.hdr.msg_type = CTL_MSG_R2R;
2368 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2369 sizeof(msg_info.hdr), M_WAITOK);
2372 case CTL_ACTION_OVERLAP:
2373 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2374 mtx_unlock(&lun->lun_lock);
2375 ctl_set_overlapped_cmd(ctsio);
2377 case CTL_ACTION_OVERLAP_TAG:
2378 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2379 mtx_unlock(&lun->lun_lock);
2380 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2382 case CTL_ACTION_ERROR:
2384 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2385 mtx_unlock(&lun->lun_lock);
2387 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2390 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2391 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2392 msg_info.hdr.serializing_sc = NULL;
2393 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2394 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2395 sizeof(msg_info.scsi), M_WAITOK);
2396 ctl_free_io((union ctl_io *)ctsio);
2402 * Returns 0 for success, errno for failure.
2405 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2406 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2410 mtx_lock(&lun->lun_lock);
2411 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2412 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2414 struct ctl_ooa_entry *entry;
2417 * If we've got more than we can fit, just count the
2418 * remaining entries.
2420 if (*cur_fill_num >= ooa_hdr->alloc_num)
2423 entry = &kern_entries[*cur_fill_num];
2425 entry->tag_num = io->scsiio.tag_num;
2426 entry->lun_num = lun->lun;
2428 entry->start_bt = io->io_hdr.start_bt;
2430 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2431 entry->cdb_len = io->scsiio.cdb_len;
2432 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2433 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2435 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2436 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2438 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2439 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2441 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2442 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2444 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2445 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2447 mtx_unlock(&lun->lun_lock);
2451 ctl_copyin_alloc(void *user_addr, unsigned int len, char *error_str,
2452 size_t error_str_len)
2456 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2458 if (copyin(user_addr, kptr, len) != 0) {
2459 snprintf(error_str, error_str_len, "Error copying %d bytes "
2460 "from user address %p to kernel address %p", len,
2470 ctl_free_args(int num_args, struct ctl_be_arg *args)
2477 for (i = 0; i < num_args; i++) {
2478 free(args[i].kname, M_CTL);
2479 free(args[i].kvalue, M_CTL);
2485 static struct ctl_be_arg *
2486 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2487 char *error_str, size_t error_str_len)
2489 struct ctl_be_arg *args;
2492 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2493 error_str, error_str_len);
2498 for (i = 0; i < num_args; i++) {
2499 args[i].kname = NULL;
2500 args[i].kvalue = NULL;
2503 for (i = 0; i < num_args; i++) {
2506 if (args[i].namelen == 0) {
2507 snprintf(error_str, error_str_len, "Argument %d "
2508 "name length is zero", i);
2512 args[i].kname = ctl_copyin_alloc(args[i].name,
2513 args[i].namelen, error_str, error_str_len);
2514 if (args[i].kname == NULL)
2517 if (args[i].kname[args[i].namelen - 1] != '\0') {
2518 snprintf(error_str, error_str_len, "Argument %d "
2519 "name is not NUL-terminated", i);
2523 if (args[i].flags & CTL_BEARG_RD) {
2524 if (args[i].vallen == 0) {
2525 snprintf(error_str, error_str_len, "Argument %d "
2526 "value length is zero", i);
2530 tmpptr = ctl_copyin_alloc(args[i].value,
2531 args[i].vallen, error_str, error_str_len);
2535 if ((args[i].flags & CTL_BEARG_ASCII)
2536 && (tmpptr[args[i].vallen - 1] != '\0')) {
2537 snprintf(error_str, error_str_len, "Argument "
2538 "%d value is not NUL-terminated", i);
2539 free(tmpptr, M_CTL);
2542 args[i].kvalue = tmpptr;
2544 args[i].kvalue = malloc(args[i].vallen,
2545 M_CTL, M_WAITOK | M_ZERO);
2552 ctl_free_args(num_args, args);
2558 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2562 for (i = 0; i < num_args; i++) {
2563 if (args[i].flags & CTL_BEARG_WR)
2564 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2569 * Escape characters that are illegal or not recommended in XML.
2572 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2574 char *end = str + size;
2579 for (; *str && str < end; str++) {
2582 retval = sbuf_printf(sb, "&");
2585 retval = sbuf_printf(sb, ">");
2588 retval = sbuf_printf(sb, "<");
2591 retval = sbuf_putc(sb, *str);
2604 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2606 struct scsi_vpd_id_descriptor *desc;
2609 if (id == NULL || id->len < 4)
2611 desc = (struct scsi_vpd_id_descriptor *)id->data;
2612 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2613 case SVPD_ID_TYPE_T10:
2614 sbuf_printf(sb, "t10.");
2616 case SVPD_ID_TYPE_EUI64:
2617 sbuf_printf(sb, "eui.");
2619 case SVPD_ID_TYPE_NAA:
2620 sbuf_printf(sb, "naa.");
2622 case SVPD_ID_TYPE_SCSI_NAME:
2625 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2626 case SVPD_ID_CODESET_BINARY:
2627 for (i = 0; i < desc->length; i++)
2628 sbuf_printf(sb, "%02x", desc->identifier[i]);
2630 case SVPD_ID_CODESET_ASCII:
2631 sbuf_printf(sb, "%.*s", (int)desc->length,
2632 (char *)desc->identifier);
2634 case SVPD_ID_CODESET_UTF8:
2635 sbuf_printf(sb, "%s", (char *)desc->identifier);
2641 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2644 struct ctl_softc *softc = dev->si_drv1;
2645 struct ctl_port *port;
2646 struct ctl_lun *lun;
2653 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2655 case CTL_ENABLE_PORT:
2656 case CTL_DISABLE_PORT:
2657 case CTL_SET_PORT_WWNS: {
2658 struct ctl_port *port;
2659 struct ctl_port_entry *entry;
2661 entry = (struct ctl_port_entry *)addr;
2663 mtx_lock(&softc->ctl_lock);
2664 STAILQ_FOREACH(port, &softc->port_list, links) {
2667 if (port->targ_port < softc->port_min ||
2668 port->targ_port >= softc->port_max)
2673 if ((entry->port_type == CTL_PORT_NONE)
2674 && (entry->targ_port == port->targ_port)) {
2676 * If the user only wants to enable or
2677 * disable or set WWNs on a specific port,
2678 * do the operation and we're done.
2682 } else if (entry->port_type & port->port_type) {
2684 * Compare the user's type mask with the
2685 * particular frontend type to see if we
2692 * Make sure the user isn't trying to set
2693 * WWNs on multiple ports at the same time.
2695 if (cmd == CTL_SET_PORT_WWNS) {
2696 printf("%s: Can't set WWNs on "
2697 "multiple ports\n", __func__);
2706 * XXX KDM we have to drop the lock here, because
2707 * the online/offline operations can potentially
2708 * block. We need to reference count the frontends
2709 * so they can't go away,
2711 if (cmd == CTL_ENABLE_PORT) {
2712 mtx_unlock(&softc->ctl_lock);
2713 ctl_port_online(port);
2714 mtx_lock(&softc->ctl_lock);
2715 } else if (cmd == CTL_DISABLE_PORT) {
2716 mtx_unlock(&softc->ctl_lock);
2717 ctl_port_offline(port);
2718 mtx_lock(&softc->ctl_lock);
2719 } else if (cmd == CTL_SET_PORT_WWNS) {
2720 ctl_port_set_wwns(port,
2721 (entry->flags & CTL_PORT_WWNN_VALID) ?
2723 (entry->flags & CTL_PORT_WWPN_VALID) ?
2724 1 : 0, entry->wwpn);
2729 mtx_unlock(&softc->ctl_lock);
2733 struct ctl_ooa *ooa_hdr;
2734 struct ctl_ooa_entry *entries;
2735 uint32_t cur_fill_num;
2737 ooa_hdr = (struct ctl_ooa *)addr;
2739 if ((ooa_hdr->alloc_len == 0)
2740 || (ooa_hdr->alloc_num == 0)) {
2741 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2742 "must be non-zero\n", __func__,
2743 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2748 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2749 sizeof(struct ctl_ooa_entry))) {
2750 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2751 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2752 __func__, ooa_hdr->alloc_len,
2753 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2758 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2759 if (entries == NULL) {
2760 printf("%s: could not allocate %d bytes for OOA "
2761 "dump\n", __func__, ooa_hdr->alloc_len);
2766 mtx_lock(&softc->ctl_lock);
2767 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2768 (ooa_hdr->lun_num >= ctl_max_luns ||
2769 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2770 mtx_unlock(&softc->ctl_lock);
2771 free(entries, M_CTL);
2772 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2773 __func__, (uintmax_t)ooa_hdr->lun_num);
2780 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2781 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2782 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2786 lun = softc->ctl_luns[ooa_hdr->lun_num];
2787 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2790 mtx_unlock(&softc->ctl_lock);
2792 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2793 ooa_hdr->fill_len = ooa_hdr->fill_num *
2794 sizeof(struct ctl_ooa_entry);
2795 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2797 printf("%s: error copying out %d bytes for OOA dump\n",
2798 __func__, ooa_hdr->fill_len);
2801 getbinuptime(&ooa_hdr->cur_bt);
2803 if (cur_fill_num > ooa_hdr->alloc_num) {
2804 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2805 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2807 ooa_hdr->dropped_num = 0;
2808 ooa_hdr->status = CTL_OOA_OK;
2811 free(entries, M_CTL);
2814 case CTL_DELAY_IO: {
2815 struct ctl_io_delay_info *delay_info;
2817 delay_info = (struct ctl_io_delay_info *)addr;
2820 mtx_lock(&softc->ctl_lock);
2821 if (delay_info->lun_id >= ctl_max_luns ||
2822 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2823 mtx_unlock(&softc->ctl_lock);
2824 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2827 mtx_lock(&lun->lun_lock);
2828 mtx_unlock(&softc->ctl_lock);
2829 delay_info->status = CTL_DELAY_STATUS_OK;
2830 switch (delay_info->delay_type) {
2831 case CTL_DELAY_TYPE_CONT:
2832 case CTL_DELAY_TYPE_ONESHOT:
2835 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2838 switch (delay_info->delay_loc) {
2839 case CTL_DELAY_LOC_DATAMOVE:
2840 lun->delay_info.datamove_type = delay_info->delay_type;
2841 lun->delay_info.datamove_delay = delay_info->delay_secs;
2843 case CTL_DELAY_LOC_DONE:
2844 lun->delay_info.done_type = delay_info->delay_type;
2845 lun->delay_info.done_delay = delay_info->delay_secs;
2848 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2851 mtx_unlock(&lun->lun_lock);
2853 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2854 #endif /* CTL_IO_DELAY */
2857 #ifdef CTL_LEGACY_STATS
2858 case CTL_GETSTATS: {
2859 struct ctl_stats *stats = (struct ctl_stats *)addr;
2863 * XXX KDM no locking here. If the LUN list changes,
2864 * things can blow up.
2867 stats->status = CTL_SS_OK;
2868 stats->fill_len = 0;
2869 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2870 if (stats->fill_len + sizeof(lun->legacy_stats) >
2872 stats->status = CTL_SS_NEED_MORE_SPACE;
2875 retval = copyout(&lun->legacy_stats, &stats->lun_stats[i++],
2876 sizeof(lun->legacy_stats));
2879 stats->fill_len += sizeof(lun->legacy_stats);
2881 stats->num_luns = softc->num_luns;
2882 stats->flags = CTL_STATS_FLAG_NONE;
2884 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
2886 getnanouptime(&stats->timestamp);
2889 #endif /* CTL_LEGACY_STATS */
2890 case CTL_ERROR_INJECT: {
2891 struct ctl_error_desc *err_desc, *new_err_desc;
2893 err_desc = (struct ctl_error_desc *)addr;
2895 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2897 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2899 mtx_lock(&softc->ctl_lock);
2900 if (err_desc->lun_id >= ctl_max_luns ||
2901 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2902 mtx_unlock(&softc->ctl_lock);
2903 free(new_err_desc, M_CTL);
2904 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2905 __func__, (uintmax_t)err_desc->lun_id);
2909 mtx_lock(&lun->lun_lock);
2910 mtx_unlock(&softc->ctl_lock);
2913 * We could do some checking here to verify the validity
2914 * of the request, but given the complexity of error
2915 * injection requests, the checking logic would be fairly
2918 * For now, if the request is invalid, it just won't get
2919 * executed and might get deleted.
2921 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2924 * XXX KDM check to make sure the serial number is unique,
2925 * in case we somehow manage to wrap. That shouldn't
2926 * happen for a very long time, but it's the right thing to
2929 new_err_desc->serial = lun->error_serial;
2930 err_desc->serial = lun->error_serial;
2931 lun->error_serial++;
2933 mtx_unlock(&lun->lun_lock);
2936 case CTL_ERROR_INJECT_DELETE: {
2937 struct ctl_error_desc *delete_desc, *desc, *desc2;
2940 delete_desc = (struct ctl_error_desc *)addr;
2943 mtx_lock(&softc->ctl_lock);
2944 if (delete_desc->lun_id >= ctl_max_luns ||
2945 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2946 mtx_unlock(&softc->ctl_lock);
2947 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2948 __func__, (uintmax_t)delete_desc->lun_id);
2952 mtx_lock(&lun->lun_lock);
2953 mtx_unlock(&softc->ctl_lock);
2954 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2955 if (desc->serial != delete_desc->serial)
2958 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2963 mtx_unlock(&lun->lun_lock);
2964 if (delete_done == 0) {
2965 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2966 "error serial %ju on LUN %u\n", __func__,
2967 delete_desc->serial, delete_desc->lun_id);
2973 case CTL_DUMP_STRUCTS: {
2975 struct ctl_port *port;
2976 struct ctl_frontend *fe;
2978 mtx_lock(&softc->ctl_lock);
2979 printf("CTL Persistent Reservation information start:\n");
2980 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2981 mtx_lock(&lun->lun_lock);
2982 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2983 mtx_unlock(&lun->lun_lock);
2987 for (j = 0; j < ctl_max_ports; j++) {
2988 if (lun->pr_keys[j] == NULL)
2990 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2991 if (lun->pr_keys[j][k] == 0)
2993 printf(" LUN %ju port %d iid %d key "
2994 "%#jx\n", lun->lun, j, k,
2995 (uintmax_t)lun->pr_keys[j][k]);
2998 mtx_unlock(&lun->lun_lock);
3000 printf("CTL Persistent Reservation information end\n");
3001 printf("CTL Ports:\n");
3002 STAILQ_FOREACH(port, &softc->port_list, links) {
3003 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
3004 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
3005 port->frontend->name, port->port_type,
3006 port->physical_port, port->virtual_port,
3007 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
3008 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3009 if (port->wwpn_iid[j].in_use == 0 &&
3010 port->wwpn_iid[j].wwpn == 0 &&
3011 port->wwpn_iid[j].name == NULL)
3014 printf(" iid %u use %d WWPN %#jx '%s'\n",
3015 j, port->wwpn_iid[j].in_use,
3016 (uintmax_t)port->wwpn_iid[j].wwpn,
3017 port->wwpn_iid[j].name);
3020 printf("CTL Port information end\n");
3021 mtx_unlock(&softc->ctl_lock);
3023 * XXX KDM calling this without a lock. We'd likely want
3024 * to drop the lock before calling the frontend's dump
3027 printf("CTL Frontends:\n");
3028 STAILQ_FOREACH(fe, &softc->fe_list, links) {
3029 printf(" Frontend '%s'\n", fe->name);
3030 if (fe->fe_dump != NULL)
3033 printf("CTL Frontend information end\n");
3037 struct ctl_lun_req *lun_req;
3038 struct ctl_backend_driver *backend;
3040 lun_req = (struct ctl_lun_req *)addr;
3042 backend = ctl_backend_find(lun_req->backend);
3043 if (backend == NULL) {
3044 lun_req->status = CTL_LUN_ERROR;
3045 snprintf(lun_req->error_str,
3046 sizeof(lun_req->error_str),
3047 "Backend \"%s\" not found.",
3051 if (lun_req->num_be_args > 0) {
3052 lun_req->kern_be_args = ctl_copyin_args(
3053 lun_req->num_be_args,
3056 sizeof(lun_req->error_str));
3057 if (lun_req->kern_be_args == NULL) {
3058 lun_req->status = CTL_LUN_ERROR;
3063 retval = backend->ioctl(dev, cmd, addr, flag, td);
3065 if (lun_req->num_be_args > 0) {
3066 ctl_copyout_args(lun_req->num_be_args,
3067 lun_req->kern_be_args);
3068 ctl_free_args(lun_req->num_be_args,
3069 lun_req->kern_be_args);
3073 case CTL_LUN_LIST: {
3075 struct ctl_lun_list *list;
3076 struct ctl_option *opt;
3078 list = (struct ctl_lun_list *)addr;
3081 * Allocate a fixed length sbuf here, based on the length
3082 * of the user's buffer. We could allocate an auto-extending
3083 * buffer, and then tell the user how much larger our
3084 * amount of data is than his buffer, but that presents
3087 * 1. The sbuf(9) routines use a blocking malloc, and so
3088 * we can't hold a lock while calling them with an
3089 * auto-extending buffer.
3091 * 2. There is not currently a LUN reference counting
3092 * mechanism, outside of outstanding transactions on
3093 * the LUN's OOA queue. So a LUN could go away on us
3094 * while we're getting the LUN number, backend-specific
3095 * information, etc. Thus, given the way things
3096 * currently work, we need to hold the CTL lock while
3097 * grabbing LUN information.
3099 * So, from the user's standpoint, the best thing to do is
3100 * allocate what he thinks is a reasonable buffer length,
3101 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3102 * double the buffer length and try again. (And repeat
3103 * that until he succeeds.)
3105 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3107 list->status = CTL_LUN_LIST_ERROR;
3108 snprintf(list->error_str, sizeof(list->error_str),
3109 "Unable to allocate %d bytes for LUN list",
3114 sbuf_printf(sb, "<ctllunlist>\n");
3116 mtx_lock(&softc->ctl_lock);
3117 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3118 mtx_lock(&lun->lun_lock);
3119 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3120 (uintmax_t)lun->lun);
3123 * Bail out as soon as we see that we've overfilled
3129 retval = sbuf_printf(sb, "\t<backend_type>%s"
3130 "</backend_type>\n",
3131 (lun->backend == NULL) ? "none" :
3132 lun->backend->name);
3137 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3138 lun->be_lun->lun_type);
3143 if (lun->backend == NULL) {
3144 retval = sbuf_printf(sb, "</lun>\n");
3150 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3151 (lun->be_lun->maxlba > 0) ?
3152 lun->be_lun->maxlba + 1 : 0);
3157 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3158 lun->be_lun->blocksize);
3163 retval = sbuf_printf(sb, "\t<serial_number>");
3168 retval = ctl_sbuf_printf_esc(sb,
3169 lun->be_lun->serial_num,
3170 sizeof(lun->be_lun->serial_num));
3175 retval = sbuf_printf(sb, "</serial_number>\n");
3180 retval = sbuf_printf(sb, "\t<device_id>");
3185 retval = ctl_sbuf_printf_esc(sb,
3186 lun->be_lun->device_id,
3187 sizeof(lun->be_lun->device_id));
3192 retval = sbuf_printf(sb, "</device_id>\n");
3197 if (lun->backend->lun_info != NULL) {
3198 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3202 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3203 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3204 opt->name, opt->value, opt->name);
3209 retval = sbuf_printf(sb, "</lun>\n");
3213 mtx_unlock(&lun->lun_lock);
3216 mtx_unlock(&lun->lun_lock);
3217 mtx_unlock(&softc->ctl_lock);
3220 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3223 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3224 snprintf(list->error_str, sizeof(list->error_str),
3225 "Out of space, %d bytes is too small",
3232 retval = copyout(sbuf_data(sb), list->lun_xml,
3235 list->fill_len = sbuf_len(sb) + 1;
3236 list->status = CTL_LUN_LIST_OK;
3241 struct ctl_iscsi *ci;
3242 struct ctl_frontend *fe;
3244 ci = (struct ctl_iscsi *)addr;
3246 fe = ctl_frontend_find("iscsi");
3248 ci->status = CTL_ISCSI_ERROR;
3249 snprintf(ci->error_str, sizeof(ci->error_str),
3250 "Frontend \"iscsi\" not found.");
3254 retval = fe->ioctl(dev, cmd, addr, flag, td);
3257 case CTL_PORT_REQ: {
3258 struct ctl_req *req;
3259 struct ctl_frontend *fe;
3261 req = (struct ctl_req *)addr;
3263 fe = ctl_frontend_find(req->driver);
3265 req->status = CTL_LUN_ERROR;
3266 snprintf(req->error_str, sizeof(req->error_str),
3267 "Frontend \"%s\" not found.", req->driver);
3270 if (req->num_args > 0) {
3271 req->kern_args = ctl_copyin_args(req->num_args,
3272 req->args, req->error_str, sizeof(req->error_str));
3273 if (req->kern_args == NULL) {
3274 req->status = CTL_LUN_ERROR;
3280 retval = fe->ioctl(dev, cmd, addr, flag, td);
3284 if (req->num_args > 0) {
3285 ctl_copyout_args(req->num_args, req->kern_args);
3286 ctl_free_args(req->num_args, req->kern_args);
3290 case CTL_PORT_LIST: {
3292 struct ctl_port *port;
3293 struct ctl_lun_list *list;
3294 struct ctl_option *opt;
3298 list = (struct ctl_lun_list *)addr;
3300 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3302 list->status = CTL_LUN_LIST_ERROR;
3303 snprintf(list->error_str, sizeof(list->error_str),
3304 "Unable to allocate %d bytes for LUN list",
3309 sbuf_printf(sb, "<ctlportlist>\n");
3311 mtx_lock(&softc->ctl_lock);
3312 STAILQ_FOREACH(port, &softc->port_list, links) {
3313 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3314 (uintmax_t)port->targ_port);
3317 * Bail out as soon as we see that we've overfilled
3323 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3324 "</frontend_type>\n", port->frontend->name);
3328 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3333 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3334 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3338 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3343 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3344 port->physical_port);
3348 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3349 port->virtual_port);
3353 if (port->target_devid != NULL) {
3354 sbuf_printf(sb, "\t<target>");
3355 ctl_id_sbuf(port->target_devid, sb);
3356 sbuf_printf(sb, "</target>\n");
3359 if (port->port_devid != NULL) {
3360 sbuf_printf(sb, "\t<port>");
3361 ctl_id_sbuf(port->port_devid, sb);
3362 sbuf_printf(sb, "</port>\n");
3365 if (port->port_info != NULL) {
3366 retval = port->port_info(port->onoff_arg, sb);
3370 STAILQ_FOREACH(opt, &port->options, links) {
3371 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3372 opt->name, opt->value, opt->name);
3377 if (port->lun_map != NULL) {
3378 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3379 for (j = 0; j < port->lun_map_size; j++) {
3380 plun = ctl_lun_map_from_port(port, j);
3381 if (plun == UINT32_MAX)
3384 "\t<lun id=\"%u\">%u</lun>\n",
3389 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3390 if (port->wwpn_iid[j].in_use == 0 ||
3391 (port->wwpn_iid[j].wwpn == 0 &&
3392 port->wwpn_iid[j].name == NULL))
3395 if (port->wwpn_iid[j].name != NULL)
3396 retval = sbuf_printf(sb,
3397 "\t<initiator id=\"%u\">%s</initiator>\n",
3398 j, port->wwpn_iid[j].name);
3400 retval = sbuf_printf(sb,
3401 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3402 j, port->wwpn_iid[j].wwpn);
3409 retval = sbuf_printf(sb, "</targ_port>\n");
3413 mtx_unlock(&softc->ctl_lock);
3416 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3419 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3420 snprintf(list->error_str, sizeof(list->error_str),
3421 "Out of space, %d bytes is too small",
3428 retval = copyout(sbuf_data(sb), list->lun_xml,
3431 list->fill_len = sbuf_len(sb) + 1;
3432 list->status = CTL_LUN_LIST_OK;
3437 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3438 struct ctl_port *port;
3440 mtx_lock(&softc->ctl_lock);
3441 if (lm->port < softc->port_min ||
3442 lm->port >= softc->port_max ||
3443 (port = softc->ctl_ports[lm->port]) == NULL) {
3444 mtx_unlock(&softc->ctl_lock);
3447 if (port->status & CTL_PORT_STATUS_ONLINE) {
3448 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3449 if (ctl_lun_map_to_port(port, lun->lun) ==
3452 mtx_lock(&lun->lun_lock);
3453 ctl_est_ua_port(lun, lm->port, -1,
3455 mtx_unlock(&lun->lun_lock);
3458 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3459 if (lm->plun != UINT32_MAX) {
3460 if (lm->lun == UINT32_MAX)
3461 retval = ctl_lun_map_unset(port, lm->plun);
3462 else if (lm->lun < ctl_max_luns &&
3463 softc->ctl_luns[lm->lun] != NULL)
3464 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3468 if (lm->lun == UINT32_MAX)
3469 retval = ctl_lun_map_deinit(port);
3471 retval = ctl_lun_map_init(port);
3473 if (port->status & CTL_PORT_STATUS_ONLINE)
3474 ctl_isc_announce_port(port);
3477 case CTL_GET_LUN_STATS: {
3478 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3482 * XXX KDM no locking here. If the LUN list changes,
3483 * things can blow up.
3486 stats->status = CTL_SS_OK;
3487 stats->fill_len = 0;
3488 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3489 if (lun->lun < stats->first_item)
3491 if (stats->fill_len + sizeof(lun->stats) >
3493 stats->status = CTL_SS_NEED_MORE_SPACE;
3496 retval = copyout(&lun->stats, &stats->stats[i++],
3497 sizeof(lun->stats));
3500 stats->fill_len += sizeof(lun->stats);
3502 stats->num_items = softc->num_luns;
3503 stats->flags = CTL_STATS_FLAG_NONE;
3505 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3507 getnanouptime(&stats->timestamp);
3510 case CTL_GET_PORT_STATS: {
3511 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3515 * XXX KDM no locking here. If the LUN list changes,
3516 * things can blow up.
3519 stats->status = CTL_SS_OK;
3520 stats->fill_len = 0;
3521 STAILQ_FOREACH(port, &softc->port_list, links) {
3522 if (port->targ_port < stats->first_item)
3524 if (stats->fill_len + sizeof(port->stats) >
3526 stats->status = CTL_SS_NEED_MORE_SPACE;
3529 retval = copyout(&port->stats, &stats->stats[i++],
3530 sizeof(port->stats));
3533 stats->fill_len += sizeof(port->stats);
3535 stats->num_items = softc->num_ports;
3536 stats->flags = CTL_STATS_FLAG_NONE;
3538 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3540 getnanouptime(&stats->timestamp);
3544 /* XXX KDM should we fix this? */
3546 struct ctl_backend_driver *backend;
3553 * We encode the backend type as the ioctl type for backend
3554 * ioctls. So parse it out here, and then search for a
3555 * backend of this type.
3557 type = _IOC_TYPE(cmd);
3559 STAILQ_FOREACH(backend, &softc->be_list, links) {
3560 if (backend->type == type) {
3566 printf("ctl: unknown ioctl command %#lx or backend "
3571 retval = backend->ioctl(dev, cmd, addr, flag, td);
3581 ctl_get_initindex(struct ctl_nexus *nexus)
3583 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3587 ctl_lun_map_init(struct ctl_port *port)
3589 struct ctl_softc *softc = port->ctl_softc;
3590 struct ctl_lun *lun;
3591 int size = ctl_lun_map_size;
3594 if (port->lun_map == NULL || port->lun_map_size < size) {
3595 port->lun_map_size = 0;
3596 free(port->lun_map, M_CTL);
3597 port->lun_map = malloc(size * sizeof(uint32_t),
3600 if (port->lun_map == NULL)
3602 for (i = 0; i < size; i++)
3603 port->lun_map[i] = UINT32_MAX;
3604 port->lun_map_size = size;
3605 if (port->status & CTL_PORT_STATUS_ONLINE) {
3606 if (port->lun_disable != NULL) {
3607 STAILQ_FOREACH(lun, &softc->lun_list, links)
3608 port->lun_disable(port->targ_lun_arg, lun->lun);
3610 ctl_isc_announce_port(port);
3616 ctl_lun_map_deinit(struct ctl_port *port)
3618 struct ctl_softc *softc = port->ctl_softc;
3619 struct ctl_lun *lun;
3621 if (port->lun_map == NULL)
3623 port->lun_map_size = 0;
3624 free(port->lun_map, M_CTL);
3625 port->lun_map = NULL;
3626 if (port->status & CTL_PORT_STATUS_ONLINE) {
3627 if (port->lun_enable != NULL) {
3628 STAILQ_FOREACH(lun, &softc->lun_list, links)
3629 port->lun_enable(port->targ_lun_arg, lun->lun);
3631 ctl_isc_announce_port(port);
3637 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3642 if (port->lun_map == NULL) {
3643 status = ctl_lun_map_init(port);
3647 if (plun >= port->lun_map_size)
3649 old = port->lun_map[plun];
3650 port->lun_map[plun] = glun;
3651 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3652 if (port->lun_enable != NULL)
3653 port->lun_enable(port->targ_lun_arg, plun);
3654 ctl_isc_announce_port(port);
3660 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3664 if (port->lun_map == NULL || plun >= port->lun_map_size)
3666 old = port->lun_map[plun];
3667 port->lun_map[plun] = UINT32_MAX;
3668 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3669 if (port->lun_disable != NULL)
3670 port->lun_disable(port->targ_lun_arg, plun);
3671 ctl_isc_announce_port(port);
3677 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3681 return (UINT32_MAX);
3682 if (port->lun_map == NULL)
3684 if (lun_id > port->lun_map_size)
3685 return (UINT32_MAX);
3686 return (port->lun_map[lun_id]);
3690 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3695 return (UINT32_MAX);
3696 if (port->lun_map == NULL)
3698 for (i = 0; i < port->lun_map_size; i++) {
3699 if (port->lun_map[i] == lun_id)
3702 return (UINT32_MAX);
3706 ctl_decode_lun(uint64_t encoded)
3709 uint32_t result = 0xffffffff;
3711 be64enc(lun, encoded);
3712 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3713 case RPL_LUNDATA_ATYP_PERIPH:
3714 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3715 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3718 case RPL_LUNDATA_ATYP_FLAT:
3719 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3720 lun[6] == 0 && lun[7] == 0)
3721 result = ((lun[0] & 0x3f) << 8) + lun[1];
3723 case RPL_LUNDATA_ATYP_EXTLUN:
3724 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3726 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3731 result = (lun[1] << 16) + (lun[2] << 8) +
3735 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3736 result = (lun[2] << 24) +
3737 (lun[3] << 16) + (lun[4] << 8) +
3742 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3743 result = 0xffffffff;
3752 ctl_encode_lun(uint32_t decoded)
3754 uint64_t l = decoded;
3757 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3759 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3761 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3763 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3767 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3771 for (i = first; i < last; i++) {
3772 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3779 ctl_set_mask(uint32_t *mask, uint32_t bit)
3781 uint32_t chunk, piece;
3784 piece = bit % (sizeof(uint32_t) * 8);
3786 if ((mask[chunk] & (1 << piece)) != 0)
3789 mask[chunk] |= (1 << piece);
3795 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3797 uint32_t chunk, piece;
3800 piece = bit % (sizeof(uint32_t) * 8);
3802 if ((mask[chunk] & (1 << piece)) == 0)
3805 mask[chunk] &= ~(1 << piece);
3811 ctl_is_set(uint32_t *mask, uint32_t bit)
3813 uint32_t chunk, piece;
3816 piece = bit % (sizeof(uint32_t) * 8);
3818 if ((mask[chunk] & (1 << piece)) == 0)
3825 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3829 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3832 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3836 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3840 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3843 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3847 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3852 i = residx/CTL_MAX_INIT_PER_PORT;
3853 if (lun->pr_keys[i] != NULL)
3855 mtx_unlock(&lun->lun_lock);
3856 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3858 mtx_lock(&lun->lun_lock);
3859 if (lun->pr_keys[i] == NULL)
3860 lun->pr_keys[i] = p;
3866 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3870 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3871 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3872 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3876 * ctl_softc, pool_name, total_ctl_io are passed in.
3877 * npool is passed out.
3880 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3881 uint32_t total_ctl_io, void **npool)
3883 struct ctl_io_pool *pool;
3885 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3890 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3891 pool->ctl_softc = ctl_softc;
3893 pool->zone = uma_zsecond_create(pool->name, NULL,
3894 NULL, NULL, NULL, ctl_softc->io_zone);
3895 /* uma_prealloc(pool->zone, total_ctl_io); */
3897 pool->zone = ctl_softc->io_zone;
3905 ctl_pool_free(struct ctl_io_pool *pool)
3912 uma_zdestroy(pool->zone);
3918 ctl_alloc_io(void *pool_ref)
3920 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3923 io = uma_zalloc(pool->zone, M_WAITOK);
3925 io->io_hdr.pool = pool_ref;
3926 CTL_SOFTC(io) = pool->ctl_softc;
3932 ctl_alloc_io_nowait(void *pool_ref)
3934 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3937 io = uma_zalloc(pool->zone, M_NOWAIT);
3939 io->io_hdr.pool = pool_ref;
3940 CTL_SOFTC(io) = pool->ctl_softc;
3946 ctl_free_io(union ctl_io *io)
3948 struct ctl_io_pool *pool;
3953 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3954 uma_zfree(pool->zone, io);
3958 ctl_zero_io(union ctl_io *io)
3960 struct ctl_io_pool *pool;
3966 * May need to preserve linked list pointers at some point too.
3968 pool = io->io_hdr.pool;
3969 memset(io, 0, sizeof(*io));
3970 io->io_hdr.pool = pool;
3971 CTL_SOFTC(io) = pool->ctl_softc;
3975 ctl_expand_number(const char *buf, uint64_t *num)
3981 number = strtoq(buf, &endptr, 0);
3983 switch (tolower((unsigned char)*endptr)) {
4003 case '\0': /* No unit. */
4007 /* Unrecognized unit. */
4011 if ((number << shift) >> shift != number) {
4015 *num = number << shift;
4021 * This routine could be used in the future to load default and/or saved
4022 * mode page parameters for a particuar lun.
4025 ctl_init_page_index(struct ctl_lun *lun)
4028 struct ctl_page_index *page_index;
4032 memcpy(&lun->mode_pages.index, page_index_template,
4033 sizeof(page_index_template));
4035 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4037 page_index = &lun->mode_pages.index[i];
4038 if (lun->be_lun->lun_type == T_DIRECT &&
4039 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4041 if (lun->be_lun->lun_type == T_PROCESSOR &&
4042 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4044 if (lun->be_lun->lun_type == T_CDROM &&
4045 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4048 page_code = page_index->page_code & SMPH_PC_MASK;
4049 switch (page_code) {
4050 case SMS_RW_ERROR_RECOVERY_PAGE: {
4051 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4052 ("subpage %#x for page %#x is incorrect!",
4053 page_index->subpage, page_code));
4054 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4055 &rw_er_page_default,
4056 sizeof(rw_er_page_default));
4057 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4058 &rw_er_page_changeable,
4059 sizeof(rw_er_page_changeable));
4060 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4061 &rw_er_page_default,
4062 sizeof(rw_er_page_default));
4063 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4064 &rw_er_page_default,
4065 sizeof(rw_er_page_default));
4066 page_index->page_data =
4067 (uint8_t *)lun->mode_pages.rw_er_page;
4070 case SMS_FORMAT_DEVICE_PAGE: {
4071 struct scsi_format_page *format_page;
4073 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4074 ("subpage %#x for page %#x is incorrect!",
4075 page_index->subpage, page_code));
4078 * Sectors per track are set above. Bytes per
4079 * sector need to be set here on a per-LUN basis.
4081 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4082 &format_page_default,
4083 sizeof(format_page_default));
4084 memcpy(&lun->mode_pages.format_page[
4085 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4086 sizeof(format_page_changeable));
4087 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4088 &format_page_default,
4089 sizeof(format_page_default));
4090 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4091 &format_page_default,
4092 sizeof(format_page_default));
4094 format_page = &lun->mode_pages.format_page[
4096 scsi_ulto2b(lun->be_lun->blocksize,
4097 format_page->bytes_per_sector);
4099 format_page = &lun->mode_pages.format_page[
4101 scsi_ulto2b(lun->be_lun->blocksize,
4102 format_page->bytes_per_sector);
4104 format_page = &lun->mode_pages.format_page[
4106 scsi_ulto2b(lun->be_lun->blocksize,
4107 format_page->bytes_per_sector);
4109 page_index->page_data =
4110 (uint8_t *)lun->mode_pages.format_page;
4113 case SMS_RIGID_DISK_PAGE: {
4114 struct scsi_rigid_disk_page *rigid_disk_page;
4115 uint32_t sectors_per_cylinder;
4119 #endif /* !__XSCALE__ */
4121 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4122 ("subpage %#x for page %#x is incorrect!",
4123 page_index->subpage, page_code));
4126 * Rotation rate and sectors per track are set
4127 * above. We calculate the cylinders here based on
4128 * capacity. Due to the number of heads and
4129 * sectors per track we're using, smaller arrays
4130 * may turn out to have 0 cylinders. Linux and
4131 * FreeBSD don't pay attention to these mode pages
4132 * to figure out capacity, but Solaris does. It
4133 * seems to deal with 0 cylinders just fine, and
4134 * works out a fake geometry based on the capacity.
4136 memcpy(&lun->mode_pages.rigid_disk_page[
4137 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4138 sizeof(rigid_disk_page_default));
4139 memcpy(&lun->mode_pages.rigid_disk_page[
4140 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4141 sizeof(rigid_disk_page_changeable));
4143 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4147 * The divide method here will be more accurate,
4148 * probably, but results in floating point being
4149 * used in the kernel on i386 (__udivdi3()). On the
4150 * XScale, though, __udivdi3() is implemented in
4153 * The shift method for cylinder calculation is
4154 * accurate if sectors_per_cylinder is a power of
4155 * 2. Otherwise it might be slightly off -- you
4156 * might have a bit of a truncation problem.
4159 cylinders = (lun->be_lun->maxlba + 1) /
4160 sectors_per_cylinder;
4162 for (shift = 31; shift > 0; shift--) {
4163 if (sectors_per_cylinder & (1 << shift))
4166 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4170 * We've basically got 3 bytes, or 24 bits for the
4171 * cylinder size in the mode page. If we're over,
4172 * just round down to 2^24.
4174 if (cylinders > 0xffffff)
4175 cylinders = 0xffffff;
4177 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4179 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4181 if ((value = ctl_get_opt(&lun->be_lun->options,
4183 scsi_ulto2b(strtol(value, NULL, 0),
4184 rigid_disk_page->rotation_rate);
4187 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4188 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4189 sizeof(rigid_disk_page_default));
4190 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4191 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4192 sizeof(rigid_disk_page_default));
4194 page_index->page_data =
4195 (uint8_t *)lun->mode_pages.rigid_disk_page;
4198 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4199 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4200 ("subpage %#x for page %#x is incorrect!",
4201 page_index->subpage, page_code));
4202 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4203 &verify_er_page_default,
4204 sizeof(verify_er_page_default));
4205 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4206 &verify_er_page_changeable,
4207 sizeof(verify_er_page_changeable));
4208 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4209 &verify_er_page_default,
4210 sizeof(verify_er_page_default));
4211 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4212 &verify_er_page_default,
4213 sizeof(verify_er_page_default));
4214 page_index->page_data =
4215 (uint8_t *)lun->mode_pages.verify_er_page;
4218 case SMS_CACHING_PAGE: {
4219 struct scsi_caching_page *caching_page;
4221 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4222 ("subpage %#x for page %#x is incorrect!",
4223 page_index->subpage, page_code));
4224 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4225 &caching_page_default,
4226 sizeof(caching_page_default));
4227 memcpy(&lun->mode_pages.caching_page[
4228 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4229 sizeof(caching_page_changeable));
4230 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4231 &caching_page_default,
4232 sizeof(caching_page_default));
4233 caching_page = &lun->mode_pages.caching_page[
4235 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4236 if (value != NULL && strcmp(value, "off") == 0)
4237 caching_page->flags1 &= ~SCP_WCE;
4238 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4239 if (value != NULL && strcmp(value, "off") == 0)
4240 caching_page->flags1 |= SCP_RCD;
4241 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4242 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4243 sizeof(caching_page_default));
4244 page_index->page_data =
4245 (uint8_t *)lun->mode_pages.caching_page;
4248 case SMS_CONTROL_MODE_PAGE: {
4249 switch (page_index->subpage) {
4250 case SMS_SUBPAGE_PAGE_0: {
4251 struct scsi_control_page *control_page;
4253 memcpy(&lun->mode_pages.control_page[
4255 &control_page_default,
4256 sizeof(control_page_default));
4257 memcpy(&lun->mode_pages.control_page[
4258 CTL_PAGE_CHANGEABLE],
4259 &control_page_changeable,
4260 sizeof(control_page_changeable));
4261 memcpy(&lun->mode_pages.control_page[
4263 &control_page_default,
4264 sizeof(control_page_default));
4265 control_page = &lun->mode_pages.control_page[
4267 value = ctl_get_opt(&lun->be_lun->options,
4269 if (value != NULL &&
4270 strcmp(value, "unrestricted") == 0) {
4271 control_page->queue_flags &=
4272 ~SCP_QUEUE_ALG_MASK;
4273 control_page->queue_flags |=
4274 SCP_QUEUE_ALG_UNRESTRICTED;
4276 memcpy(&lun->mode_pages.control_page[
4278 &lun->mode_pages.control_page[
4280 sizeof(control_page_default));
4281 page_index->page_data =
4282 (uint8_t *)lun->mode_pages.control_page;
4286 memcpy(&lun->mode_pages.control_ext_page[
4288 &control_ext_page_default,
4289 sizeof(control_ext_page_default));
4290 memcpy(&lun->mode_pages.control_ext_page[
4291 CTL_PAGE_CHANGEABLE],
4292 &control_ext_page_changeable,
4293 sizeof(control_ext_page_changeable));
4294 memcpy(&lun->mode_pages.control_ext_page[
4296 &control_ext_page_default,
4297 sizeof(control_ext_page_default));
4298 memcpy(&lun->mode_pages.control_ext_page[
4300 &lun->mode_pages.control_ext_page[
4302 sizeof(control_ext_page_default));
4303 page_index->page_data =
4304 (uint8_t *)lun->mode_pages.control_ext_page;
4307 panic("subpage %#x for page %#x is incorrect!",
4308 page_index->subpage, page_code);
4312 case SMS_INFO_EXCEPTIONS_PAGE: {
4313 switch (page_index->subpage) {
4314 case SMS_SUBPAGE_PAGE_0:
4315 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4317 sizeof(ie_page_default));
4318 memcpy(&lun->mode_pages.ie_page[
4319 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4320 sizeof(ie_page_changeable));
4321 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4323 sizeof(ie_page_default));
4324 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4326 sizeof(ie_page_default));
4327 page_index->page_data =
4328 (uint8_t *)lun->mode_pages.ie_page;
4331 struct ctl_logical_block_provisioning_page *page;
4333 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4335 sizeof(lbp_page_default));
4336 memcpy(&lun->mode_pages.lbp_page[
4337 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4338 sizeof(lbp_page_changeable));
4339 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4341 sizeof(lbp_page_default));
4342 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4343 value = ctl_get_opt(&lun->be_lun->options,
4345 if (value != NULL &&
4346 ctl_expand_number(value, &ival) == 0) {
4347 page->descr[0].flags |= SLBPPD_ENABLED |
4349 if (lun->be_lun->blocksize)
4350 ival /= lun->be_lun->blocksize;
4353 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4354 page->descr[0].count);
4356 value = ctl_get_opt(&lun->be_lun->options,
4358 if (value != NULL &&
4359 ctl_expand_number(value, &ival) == 0) {
4360 page->descr[1].flags |= SLBPPD_ENABLED |
4362 if (lun->be_lun->blocksize)
4363 ival /= lun->be_lun->blocksize;
4366 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4367 page->descr[1].count);
4369 value = ctl_get_opt(&lun->be_lun->options,
4370 "pool-avail-threshold");
4371 if (value != NULL &&
4372 ctl_expand_number(value, &ival) == 0) {
4373 page->descr[2].flags |= SLBPPD_ENABLED |
4375 if (lun->be_lun->blocksize)
4376 ival /= lun->be_lun->blocksize;
4379 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4380 page->descr[2].count);
4382 value = ctl_get_opt(&lun->be_lun->options,
4383 "pool-used-threshold");
4384 if (value != NULL &&
4385 ctl_expand_number(value, &ival) == 0) {
4386 page->descr[3].flags |= SLBPPD_ENABLED |
4388 if (lun->be_lun->blocksize)
4389 ival /= lun->be_lun->blocksize;
4392 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4393 page->descr[3].count);
4395 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4396 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4397 sizeof(lbp_page_default));
4398 page_index->page_data =
4399 (uint8_t *)lun->mode_pages.lbp_page;
4403 panic("subpage %#x for page %#x is incorrect!",
4404 page_index->subpage, page_code);
4408 case SMS_CDDVD_CAPS_PAGE:{
4409 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4410 ("subpage %#x for page %#x is incorrect!",
4411 page_index->subpage, page_code));
4412 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4413 &cddvd_page_default,
4414 sizeof(cddvd_page_default));
4415 memcpy(&lun->mode_pages.cddvd_page[
4416 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4417 sizeof(cddvd_page_changeable));
4418 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4419 &cddvd_page_default,
4420 sizeof(cddvd_page_default));
4421 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4422 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4423 sizeof(cddvd_page_default));
4424 page_index->page_data =
4425 (uint8_t *)lun->mode_pages.cddvd_page;
4429 panic("invalid page code value %#x", page_code);
4433 return (CTL_RETVAL_COMPLETE);
4437 ctl_init_log_page_index(struct ctl_lun *lun)
4439 struct ctl_page_index *page_index;
4442 memcpy(&lun->log_pages.index, log_page_index_template,
4443 sizeof(log_page_index_template));
4446 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4448 page_index = &lun->log_pages.index[i];
4449 if (lun->be_lun->lun_type == T_DIRECT &&
4450 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4452 if (lun->be_lun->lun_type == T_PROCESSOR &&
4453 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4455 if (lun->be_lun->lun_type == T_CDROM &&
4456 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4459 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4460 lun->backend->lun_attr == NULL)
4463 if (page_index->page_code != prev) {
4464 lun->log_pages.pages_page[j] = page_index->page_code;
4465 prev = page_index->page_code;
4468 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4469 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4472 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4473 lun->log_pages.index[0].page_len = j;
4474 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4475 lun->log_pages.index[1].page_len = k * 2;
4476 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4477 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4478 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4479 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4480 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4481 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4483 return (CTL_RETVAL_COMPLETE);
4487 hex2bin(const char *str, uint8_t *buf, int buf_size)
4492 memset(buf, 0, buf_size);
4493 while (isspace(str[0]))
4495 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4498 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4499 while (str[i] == '-') /* Skip dashes in UUIDs. */
4504 else if (isalpha(c))
4505 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4511 buf[i / 2] |= (c << 4);
4515 return ((i + 1) / 2);
4522 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4523 * wants us to allocate the LUN and he can block.
4524 * - ctl_softc is always set
4525 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4527 * Returns 0 for success, non-zero (errno) for failure.
4530 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4531 struct ctl_be_lun *const be_lun)
4533 struct ctl_lun *nlun, *lun;
4534 struct scsi_vpd_id_descriptor *desc;
4535 struct scsi_vpd_id_t10 *t10id;
4536 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4537 int lun_number, lun_malloced;
4538 int devidlen, idlen1, idlen2 = 0, len;
4544 * We currently only support Direct Access or Processor LUN types.
4546 switch (be_lun->lun_type) {
4554 be_lun->lun_config_status(be_lun->be_lun,
4555 CTL_LUN_CONFIG_FAILURE);
4558 if (ctl_lun == NULL) {
4559 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4566 memset(lun, 0, sizeof(*lun));
4568 lun->flags = CTL_LUN_MALLOCED;
4570 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4571 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4572 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4573 M_DEVBUF, M_WAITOK | M_ZERO);
4574 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4575 M_DEVBUF, M_WAITOK | M_ZERO);
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 uuid = ctl_get_opt(&be_lun->options, "uuid");
4597 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4599 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4600 M_CTL, M_WAITOK | M_ZERO);
4601 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4602 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4603 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4604 desc->length = idlen1;
4605 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4606 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4607 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4608 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4610 strncpy(t10id->vendor, vendor,
4611 min(sizeof(t10id->vendor), strlen(vendor)));
4613 strncpy((char *)t10id->vendor_spec_id,
4614 (char *)be_lun->device_id, devidlen);
4615 if (scsiname != NULL) {
4616 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4618 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4619 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4620 SVPD_ID_TYPE_SCSI_NAME;
4621 desc->length = idlen2;
4622 strlcpy(desc->identifier, scsiname, idlen2);
4625 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4627 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4628 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4630 desc->length = hex2bin(eui, desc->identifier, 16);
4631 desc->length = desc->length > 12 ? 16 :
4632 (desc->length > 8 ? 12 : 8);
4633 len -= 16 - desc->length;
4636 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4638 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4639 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4641 desc->length = hex2bin(naa, desc->identifier, 16);
4642 desc->length = desc->length > 8 ? 16 : 8;
4643 len -= 16 - desc->length;
4646 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4648 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4649 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4651 desc->identifier[0] = 0x10;
4652 hex2bin(uuid, &desc->identifier[2], 16);
4655 lun->lun_devid->len = len;
4657 mtx_lock(&ctl_softc->ctl_lock);
4659 * See if the caller requested a particular LUN number. If so, see
4660 * if it is available. Otherwise, allocate the first available LUN.
4662 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4663 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4664 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4665 mtx_unlock(&ctl_softc->ctl_lock);
4666 if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4667 printf("ctl: requested LUN ID %d is higher "
4668 "than ctl_max_luns - 1 (%d)\n",
4669 be_lun->req_lun_id, ctl_max_luns - 1);
4672 * XXX KDM return an error, or just assign
4673 * another LUN ID in this case??
4675 printf("ctl: requested LUN ID %d is already "
4676 "in use\n", be_lun->req_lun_id);
4679 free(lun->lun_devid, M_CTL);
4680 if (lun->flags & CTL_LUN_MALLOCED)
4682 be_lun->lun_config_status(be_lun->be_lun,
4683 CTL_LUN_CONFIG_FAILURE);
4686 lun_number = be_lun->req_lun_id;
4688 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4689 if (lun_number == -1) {
4690 mtx_unlock(&ctl_softc->ctl_lock);
4691 printf("ctl: can't allocate LUN, out of LUNs\n");
4695 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4696 mtx_unlock(&ctl_softc->ctl_lock);
4698 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4699 lun->lun = lun_number;
4700 lun->be_lun = be_lun;
4702 * The processor LUN is always enabled. Disk LUNs come on line
4703 * disabled, and must be enabled by the backend.
4705 lun->flags |= CTL_LUN_DISABLED;
4706 lun->backend = be_lun->be;
4707 be_lun->ctl_lun = lun;
4708 be_lun->lun_id = lun_number;
4709 atomic_add_int(&be_lun->be->num_luns, 1);
4710 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4711 lun->flags |= CTL_LUN_EJECTED;
4712 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4713 lun->flags |= CTL_LUN_NO_MEDIA;
4714 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4715 lun->flags |= CTL_LUN_STOPPED;
4717 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4718 lun->flags |= CTL_LUN_PRIMARY_SC;
4720 value = ctl_get_opt(&be_lun->options, "removable");
4721 if (value != NULL) {
4722 if (strcmp(value, "on") == 0)
4723 lun->flags |= CTL_LUN_REMOVABLE;
4724 } else if (be_lun->lun_type == T_CDROM)
4725 lun->flags |= CTL_LUN_REMOVABLE;
4727 lun->ctl_softc = ctl_softc;
4729 lun->last_busy = getsbinuptime();
4731 TAILQ_INIT(&lun->ooa_queue);
4732 TAILQ_INIT(&lun->blocked_queue);
4733 STAILQ_INIT(&lun->error_list);
4734 lun->ie_reported = 1;
4735 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4736 ctl_tpc_lun_init(lun);
4737 if (lun->flags & CTL_LUN_REMOVABLE) {
4738 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4743 * Initialize the mode and log page index.
4745 ctl_init_page_index(lun);
4746 ctl_init_log_page_index(lun);
4748 /* Setup statistics gathering */
4749 #ifdef CTL_LEGACY_STATS
4750 lun->legacy_stats.device_type = be_lun->lun_type;
4751 lun->legacy_stats.lun_number = lun_number;
4752 lun->legacy_stats.blocksize = be_lun->blocksize;
4753 if (be_lun->blocksize == 0)
4754 lun->legacy_stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4755 lun->legacy_stats.ports = malloc(sizeof(struct ctl_lun_io_port_stats) *
4756 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4757 for (len = 0; len < ctl_max_ports; len++)
4758 lun->legacy_stats.ports[len].targ_port = len;
4759 #endif /* CTL_LEGACY_STATS */
4760 lun->stats.item = lun_number;
4763 * Now, before we insert this lun on the lun list, set the lun
4764 * inventory changed UA for all other luns.
4766 mtx_lock(&ctl_softc->ctl_lock);
4767 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4768 mtx_lock(&nlun->lun_lock);
4769 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4770 mtx_unlock(&nlun->lun_lock);
4772 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4773 ctl_softc->ctl_luns[lun_number] = lun;
4774 ctl_softc->num_luns++;
4775 mtx_unlock(&ctl_softc->ctl_lock);
4777 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4784 * - LUN has already been marked invalid and any pending I/O has been taken
4788 ctl_free_lun(struct ctl_lun *lun)
4790 struct ctl_softc *softc = lun->ctl_softc;
4791 struct ctl_lun *nlun;
4794 KASSERT(TAILQ_EMPTY(&lun->ooa_queue),
4795 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4797 mtx_lock(&softc->ctl_lock);
4798 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4799 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4800 softc->ctl_luns[lun->lun] = NULL;
4802 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4803 mtx_lock(&nlun->lun_lock);
4804 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4805 mtx_unlock(&nlun->lun_lock);
4807 mtx_unlock(&softc->ctl_lock);
4810 * Tell the backend to free resources, if this LUN has a backend.
4812 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4813 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4815 lun->ie_reportcnt = UINT32_MAX;
4816 callout_drain(&lun->ie_callout);
4817 ctl_tpc_lun_shutdown(lun);
4818 mtx_destroy(&lun->lun_lock);
4819 free(lun->lun_devid, M_CTL);
4820 for (i = 0; i < ctl_max_ports; i++)
4821 free(lun->pending_ua[i], M_CTL);
4822 free(lun->pending_ua, M_DEVBUF);
4823 for (i = 0; i < ctl_max_ports; i++)
4824 free(lun->pr_keys[i], M_CTL);
4825 free(lun->pr_keys, M_DEVBUF);
4826 free(lun->write_buffer, M_CTL);
4827 free(lun->prevent, M_CTL);
4828 if (lun->flags & CTL_LUN_MALLOCED)
4835 ctl_create_lun(struct ctl_be_lun *be_lun)
4839 * ctl_alloc_lun() should handle all potential failure cases.
4841 ctl_alloc_lun(control_softc, NULL, be_lun);
4845 ctl_add_lun(struct ctl_be_lun *be_lun)
4847 struct ctl_softc *softc = control_softc;
4849 mtx_lock(&softc->ctl_lock);
4850 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4851 mtx_unlock(&softc->ctl_lock);
4852 wakeup(&softc->pending_lun_queue);
4858 ctl_enable_lun(struct ctl_be_lun *be_lun)
4860 struct ctl_softc *softc;
4861 struct ctl_port *port, *nport;
4862 struct ctl_lun *lun;
4865 lun = (struct ctl_lun *)be_lun->ctl_lun;
4866 softc = lun->ctl_softc;
4868 mtx_lock(&softc->ctl_lock);
4869 mtx_lock(&lun->lun_lock);
4870 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4872 * eh? Why did we get called if the LUN is already
4875 mtx_unlock(&lun->lun_lock);
4876 mtx_unlock(&softc->ctl_lock);
4879 lun->flags &= ~CTL_LUN_DISABLED;
4880 mtx_unlock(&lun->lun_lock);
4882 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4883 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4884 port->lun_map != NULL || port->lun_enable == NULL)
4888 * Drop the lock while we call the FETD's enable routine.
4889 * This can lead to a callback into CTL (at least in the
4890 * case of the internal initiator frontend.
4892 mtx_unlock(&softc->ctl_lock);
4893 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4894 mtx_lock(&softc->ctl_lock);
4896 printf("%s: FETD %s port %d returned error "
4897 "%d for lun_enable on lun %jd\n",
4898 __func__, port->port_name, port->targ_port,
4899 retval, (intmax_t)lun->lun);
4903 mtx_unlock(&softc->ctl_lock);
4904 ctl_isc_announce_lun(lun);
4910 ctl_disable_lun(struct ctl_be_lun *be_lun)
4912 struct ctl_softc *softc;
4913 struct ctl_port *port;
4914 struct ctl_lun *lun;
4917 lun = (struct ctl_lun *)be_lun->ctl_lun;
4918 softc = lun->ctl_softc;
4920 mtx_lock(&softc->ctl_lock);
4921 mtx_lock(&lun->lun_lock);
4922 if (lun->flags & CTL_LUN_DISABLED) {
4923 mtx_unlock(&lun->lun_lock);
4924 mtx_unlock(&softc->ctl_lock);
4927 lun->flags |= CTL_LUN_DISABLED;
4928 mtx_unlock(&lun->lun_lock);
4930 STAILQ_FOREACH(port, &softc->port_list, links) {
4931 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4932 port->lun_map != NULL || port->lun_disable == NULL)
4936 * Drop the lock before we call the frontend's disable
4937 * routine, to avoid lock order reversals.
4939 * XXX KDM what happens if the frontend list changes while
4940 * we're traversing it? It's unlikely, but should be handled.
4942 mtx_unlock(&softc->ctl_lock);
4943 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4944 mtx_lock(&softc->ctl_lock);
4946 printf("%s: FETD %s port %d returned error "
4947 "%d for lun_disable on lun %jd\n",
4948 __func__, port->port_name, port->targ_port,
4949 retval, (intmax_t)lun->lun);
4953 mtx_unlock(&softc->ctl_lock);
4954 ctl_isc_announce_lun(lun);
4960 ctl_start_lun(struct ctl_be_lun *be_lun)
4962 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4964 mtx_lock(&lun->lun_lock);
4965 lun->flags &= ~CTL_LUN_STOPPED;
4966 mtx_unlock(&lun->lun_lock);
4971 ctl_stop_lun(struct ctl_be_lun *be_lun)
4973 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4975 mtx_lock(&lun->lun_lock);
4976 lun->flags |= CTL_LUN_STOPPED;
4977 mtx_unlock(&lun->lun_lock);
4982 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4984 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4986 mtx_lock(&lun->lun_lock);
4987 lun->flags |= CTL_LUN_NO_MEDIA;
4988 mtx_unlock(&lun->lun_lock);
4993 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4995 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4996 union ctl_ha_msg msg;
4998 mtx_lock(&lun->lun_lock);
4999 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
5000 if (lun->flags & CTL_LUN_REMOVABLE)
5001 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
5002 mtx_unlock(&lun->lun_lock);
5003 if ((lun->flags & CTL_LUN_REMOVABLE) &&
5004 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5005 bzero(&msg.ua, sizeof(msg.ua));
5006 msg.hdr.msg_type = CTL_MSG_UA;
5007 msg.hdr.nexus.initid = -1;
5008 msg.hdr.nexus.targ_port = -1;
5009 msg.hdr.nexus.targ_lun = lun->lun;
5010 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5013 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
5014 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5021 ctl_lun_ejected(struct ctl_be_lun *be_lun)
5023 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5025 mtx_lock(&lun->lun_lock);
5026 lun->flags |= CTL_LUN_EJECTED;
5027 mtx_unlock(&lun->lun_lock);
5032 ctl_lun_primary(struct ctl_be_lun *be_lun)
5034 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5036 mtx_lock(&lun->lun_lock);
5037 lun->flags |= CTL_LUN_PRIMARY_SC;
5038 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5039 mtx_unlock(&lun->lun_lock);
5040 ctl_isc_announce_lun(lun);
5045 ctl_lun_secondary(struct ctl_be_lun *be_lun)
5047 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5049 mtx_lock(&lun->lun_lock);
5050 lun->flags &= ~CTL_LUN_PRIMARY_SC;
5051 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5052 mtx_unlock(&lun->lun_lock);
5053 ctl_isc_announce_lun(lun);
5058 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5060 struct ctl_softc *softc;
5061 struct ctl_lun *lun;
5063 lun = (struct ctl_lun *)be_lun->ctl_lun;
5064 softc = lun->ctl_softc;
5066 mtx_lock(&lun->lun_lock);
5069 * The LUN needs to be disabled before it can be marked invalid.
5071 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5072 mtx_unlock(&lun->lun_lock);
5076 * Mark the LUN invalid.
5078 lun->flags |= CTL_LUN_INVALID;
5081 * If there is nothing in the OOA queue, go ahead and free the LUN.
5082 * If we have something in the OOA queue, we'll free it when the
5083 * last I/O completes.
5085 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5086 mtx_unlock(&lun->lun_lock);
5089 mtx_unlock(&lun->lun_lock);
5095 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5097 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5098 union ctl_ha_msg msg;
5100 mtx_lock(&lun->lun_lock);
5101 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5102 mtx_unlock(&lun->lun_lock);
5103 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5104 /* Send msg to other side. */
5105 bzero(&msg.ua, sizeof(msg.ua));
5106 msg.hdr.msg_type = CTL_MSG_UA;
5107 msg.hdr.nexus.initid = -1;
5108 msg.hdr.nexus.targ_port = -1;
5109 msg.hdr.nexus.targ_lun = lun->lun;
5110 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5113 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5114 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5120 * Backend "memory move is complete" callback for requests that never
5121 * make it down to say RAIDCore's configuration code.
5124 ctl_config_move_done(union ctl_io *io)
5128 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5129 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5130 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5132 if ((io->io_hdr.port_status != 0) &&
5133 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5134 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5135 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5136 /*retry_count*/ io->io_hdr.port_status);
5137 } else if (io->scsiio.kern_data_resid != 0 &&
5138 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5139 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5140 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5141 ctl_set_invalid_field_ciu(&io->scsiio);
5144 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5146 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5147 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5148 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5149 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5151 * XXX KDM just assuming a single pointer here, and not a
5152 * S/G list. If we start using S/G lists for config data,
5153 * we'll need to know how to clean them up here as well.
5155 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5156 free(io->scsiio.kern_data_ptr, M_CTL);
5158 retval = CTL_RETVAL_COMPLETE;
5161 * XXX KDM now we need to continue data movement. Some
5163 * - call ctl_scsiio() again? We don't do this for data
5164 * writes, because for those at least we know ahead of
5165 * time where the write will go and how long it is. For
5166 * config writes, though, that information is largely
5167 * contained within the write itself, thus we need to
5168 * parse out the data again.
5170 * - Call some other function once the data is in?
5174 * XXX KDM call ctl_scsiio() again for now, and check flag
5175 * bits to see whether we're allocated or not.
5177 retval = ctl_scsiio(&io->scsiio);
5183 * This gets called by a backend driver when it is done with a
5184 * data_submit method.
5187 ctl_data_submit_done(union ctl_io *io)
5190 * If the IO_CONT flag is set, we need to call the supplied
5191 * function to continue processing the I/O, instead of completing
5194 * If there is an error, though, we don't want to keep processing.
5195 * Instead, just send status back to the initiator.
5197 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5198 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5199 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5200 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5201 io->scsiio.io_cont(io);
5208 * This gets called by a backend driver when it is done with a
5209 * configuration write.
5212 ctl_config_write_done(union ctl_io *io)
5217 * If the IO_CONT flag is set, we need to call the supplied
5218 * function to continue processing the I/O, instead of completing
5221 * If there is an error, though, we don't want to keep processing.
5222 * Instead, just send status back to the initiator.
5224 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5225 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5226 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5227 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5228 io->scsiio.io_cont(io);
5232 * Since a configuration write can be done for commands that actually
5233 * have data allocated, like write buffer, and commands that have
5234 * no data, like start/stop unit, we need to check here.
5236 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5237 buf = io->scsiio.kern_data_ptr;
5246 ctl_config_read_done(union ctl_io *io)
5251 * If there is some error -- we are done, skip data transfer.
5253 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5254 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5255 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5256 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5257 buf = io->scsiio.kern_data_ptr;
5267 * If the IO_CONT flag is set, we need to call the supplied
5268 * function to continue processing the I/O, instead of completing
5271 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5272 io->scsiio.io_cont(io);
5280 * SCSI release command.
5283 ctl_scsi_release(struct ctl_scsiio *ctsio)
5285 struct ctl_lun *lun = CTL_LUN(ctsio);
5288 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5290 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5293 * XXX KDM right now, we only support LUN reservation. We don't
5294 * support 3rd party reservations, or extent reservations, which
5295 * might actually need the parameter list. If we've gotten this
5296 * far, we've got a LUN reservation. Anything else got kicked out
5297 * above. So, according to SPC, ignore the length.
5300 mtx_lock(&lun->lun_lock);
5303 * According to SPC, it is not an error for an intiator to attempt
5304 * to release a reservation on a LUN that isn't reserved, or that
5305 * is reserved by another initiator. The reservation can only be
5306 * released, though, by the initiator who made it or by one of
5307 * several reset type events.
5309 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5310 lun->flags &= ~CTL_LUN_RESERVED;
5312 mtx_unlock(&lun->lun_lock);
5314 ctl_set_success(ctsio);
5315 ctl_done((union ctl_io *)ctsio);
5316 return (CTL_RETVAL_COMPLETE);
5320 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5322 struct ctl_lun *lun = CTL_LUN(ctsio);
5325 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5327 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5330 * XXX KDM right now, we only support LUN reservation. We don't
5331 * support 3rd party reservations, or extent reservations, which
5332 * might actually need the parameter list. If we've gotten this
5333 * far, we've got a LUN reservation. Anything else got kicked out
5334 * above. So, according to SPC, ignore the length.
5337 mtx_lock(&lun->lun_lock);
5338 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5339 ctl_set_reservation_conflict(ctsio);
5343 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5344 if (lun->flags & CTL_LUN_PR_RESERVED) {
5345 ctl_set_success(ctsio);
5349 lun->flags |= CTL_LUN_RESERVED;
5350 lun->res_idx = residx;
5351 ctl_set_success(ctsio);
5354 mtx_unlock(&lun->lun_lock);
5355 ctl_done((union ctl_io *)ctsio);
5356 return (CTL_RETVAL_COMPLETE);
5360 ctl_start_stop(struct ctl_scsiio *ctsio)
5362 struct ctl_lun *lun = CTL_LUN(ctsio);
5363 struct scsi_start_stop_unit *cdb;
5366 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5368 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5370 if ((cdb->how & SSS_PC_MASK) == 0) {
5371 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5372 (cdb->how & SSS_START) == 0) {
5375 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5376 if (ctl_get_prkey(lun, residx) == 0 ||
5377 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5379 ctl_set_reservation_conflict(ctsio);
5380 ctl_done((union ctl_io *)ctsio);
5381 return (CTL_RETVAL_COMPLETE);
5385 if ((cdb->how & SSS_LOEJ) &&
5386 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5387 ctl_set_invalid_field(ctsio,
5393 ctl_done((union ctl_io *)ctsio);
5394 return (CTL_RETVAL_COMPLETE);
5397 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5398 lun->prevent_count > 0) {
5399 /* "Medium removal prevented" */
5400 ctl_set_sense(ctsio, /*current_error*/ 1,
5401 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5402 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5403 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5404 ctl_done((union ctl_io *)ctsio);
5405 return (CTL_RETVAL_COMPLETE);
5409 retval = lun->backend->config_write((union ctl_io *)ctsio);
5414 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5416 struct ctl_lun *lun = CTL_LUN(ctsio);
5417 struct scsi_prevent *cdb;
5421 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5423 cdb = (struct scsi_prevent *)ctsio->cdb;
5425 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5426 ctl_set_invalid_opcode(ctsio);
5427 ctl_done((union ctl_io *)ctsio);
5428 return (CTL_RETVAL_COMPLETE);
5431 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5432 mtx_lock(&lun->lun_lock);
5433 if ((cdb->how & PR_PREVENT) &&
5434 ctl_is_set(lun->prevent, initidx) == 0) {
5435 ctl_set_mask(lun->prevent, initidx);
5436 lun->prevent_count++;
5437 } else if ((cdb->how & PR_PREVENT) == 0 &&
5438 ctl_is_set(lun->prevent, initidx)) {
5439 ctl_clear_mask(lun->prevent, initidx);
5440 lun->prevent_count--;
5442 mtx_unlock(&lun->lun_lock);
5443 retval = lun->backend->config_write((union ctl_io *)ctsio);
5448 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5449 * we don't really do anything with the LBA and length fields if the user
5450 * passes them in. Instead we'll just flush out the cache for the entire
5454 ctl_sync_cache(struct ctl_scsiio *ctsio)
5456 struct ctl_lun *lun = CTL_LUN(ctsio);
5457 struct ctl_lba_len_flags *lbalen;
5458 uint64_t starting_lba;
5459 uint32_t block_count;
5463 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5467 switch (ctsio->cdb[0]) {
5468 case SYNCHRONIZE_CACHE: {
5469 struct scsi_sync_cache *cdb;
5470 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5472 starting_lba = scsi_4btoul(cdb->begin_lba);
5473 block_count = scsi_2btoul(cdb->lb_count);
5477 case SYNCHRONIZE_CACHE_16: {
5478 struct scsi_sync_cache_16 *cdb;
5479 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5481 starting_lba = scsi_8btou64(cdb->begin_lba);
5482 block_count = scsi_4btoul(cdb->lb_count);
5487 ctl_set_invalid_opcode(ctsio);
5488 ctl_done((union ctl_io *)ctsio);
5490 break; /* NOTREACHED */
5494 * We check the LBA and length, but don't do anything with them.
5495 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5496 * get flushed. This check will just help satisfy anyone who wants
5497 * to see an error for an out of range LBA.
5499 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5500 ctl_set_lba_out_of_range(ctsio,
5501 MAX(starting_lba, lun->be_lun->maxlba + 1));
5502 ctl_done((union ctl_io *)ctsio);
5506 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5507 lbalen->lba = starting_lba;
5508 lbalen->len = block_count;
5509 lbalen->flags = byte2;
5510 retval = lun->backend->config_write((union ctl_io *)ctsio);
5517 ctl_format(struct ctl_scsiio *ctsio)
5519 struct scsi_format *cdb;
5520 int length, defect_list_len;
5522 CTL_DEBUG_PRINT(("ctl_format\n"));
5524 cdb = (struct scsi_format *)ctsio->cdb;
5527 if (cdb->byte2 & SF_FMTDATA) {
5528 if (cdb->byte2 & SF_LONGLIST)
5529 length = sizeof(struct scsi_format_header_long);
5531 length = sizeof(struct scsi_format_header_short);
5534 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5536 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5537 ctsio->kern_data_len = length;
5538 ctsio->kern_total_len = length;
5539 ctsio->kern_rel_offset = 0;
5540 ctsio->kern_sg_entries = 0;
5541 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5542 ctsio->be_move_done = ctl_config_move_done;
5543 ctl_datamove((union ctl_io *)ctsio);
5545 return (CTL_RETVAL_COMPLETE);
5548 defect_list_len = 0;
5550 if (cdb->byte2 & SF_FMTDATA) {
5551 if (cdb->byte2 & SF_LONGLIST) {
5552 struct scsi_format_header_long *header;
5554 header = (struct scsi_format_header_long *)
5555 ctsio->kern_data_ptr;
5557 defect_list_len = scsi_4btoul(header->defect_list_len);
5558 if (defect_list_len != 0) {
5559 ctl_set_invalid_field(ctsio,
5568 struct scsi_format_header_short *header;
5570 header = (struct scsi_format_header_short *)
5571 ctsio->kern_data_ptr;
5573 defect_list_len = scsi_2btoul(header->defect_list_len);
5574 if (defect_list_len != 0) {
5575 ctl_set_invalid_field(ctsio,
5586 ctl_set_success(ctsio);
5589 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5590 free(ctsio->kern_data_ptr, M_CTL);
5591 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5594 ctl_done((union ctl_io *)ctsio);
5595 return (CTL_RETVAL_COMPLETE);
5599 ctl_read_buffer(struct ctl_scsiio *ctsio)
5601 struct ctl_lun *lun = CTL_LUN(ctsio);
5602 uint64_t buffer_offset;
5605 static uint8_t descr[4];
5606 static uint8_t echo_descr[4] = { 0 };
5608 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5610 switch (ctsio->cdb[0]) {
5612 struct scsi_read_buffer *cdb;
5614 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5615 buffer_offset = scsi_3btoul(cdb->offset);
5616 len = scsi_3btoul(cdb->length);
5620 case READ_BUFFER_16: {
5621 struct scsi_read_buffer_16 *cdb;
5623 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5624 buffer_offset = scsi_8btou64(cdb->offset);
5625 len = scsi_4btoul(cdb->length);
5629 default: /* This shouldn't happen. */
5630 ctl_set_invalid_opcode(ctsio);
5631 ctl_done((union ctl_io *)ctsio);
5632 return (CTL_RETVAL_COMPLETE);
5635 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5636 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5637 ctl_set_invalid_field(ctsio,
5643 ctl_done((union ctl_io *)ctsio);
5644 return (CTL_RETVAL_COMPLETE);
5647 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5649 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5650 ctsio->kern_data_ptr = descr;
5651 len = min(len, sizeof(descr));
5652 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5653 ctsio->kern_data_ptr = echo_descr;
5654 len = min(len, sizeof(echo_descr));
5656 if (lun->write_buffer == NULL) {
5657 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5660 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5662 ctsio->kern_data_len = len;
5663 ctsio->kern_total_len = len;
5664 ctsio->kern_rel_offset = 0;
5665 ctsio->kern_sg_entries = 0;
5666 ctl_set_success(ctsio);
5667 ctsio->be_move_done = ctl_config_move_done;
5668 ctl_datamove((union ctl_io *)ctsio);
5669 return (CTL_RETVAL_COMPLETE);
5673 ctl_write_buffer(struct ctl_scsiio *ctsio)
5675 struct ctl_lun *lun = CTL_LUN(ctsio);
5676 struct scsi_write_buffer *cdb;
5677 int buffer_offset, len;
5679 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5681 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5683 len = scsi_3btoul(cdb->length);
5684 buffer_offset = scsi_3btoul(cdb->offset);
5686 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5687 ctl_set_invalid_field(ctsio,
5693 ctl_done((union ctl_io *)ctsio);
5694 return (CTL_RETVAL_COMPLETE);
5698 * If we've got a kernel request that hasn't been malloced yet,
5699 * malloc it and tell the caller the data buffer is here.
5701 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5702 if (lun->write_buffer == NULL) {
5703 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5706 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5707 ctsio->kern_data_len = len;
5708 ctsio->kern_total_len = len;
5709 ctsio->kern_rel_offset = 0;
5710 ctsio->kern_sg_entries = 0;
5711 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5712 ctsio->be_move_done = ctl_config_move_done;
5713 ctl_datamove((union ctl_io *)ctsio);
5715 return (CTL_RETVAL_COMPLETE);
5718 ctl_set_success(ctsio);
5719 ctl_done((union ctl_io *)ctsio);
5720 return (CTL_RETVAL_COMPLETE);
5724 ctl_write_same(struct ctl_scsiio *ctsio)
5726 struct ctl_lun *lun = CTL_LUN(ctsio);
5727 struct ctl_lba_len_flags *lbalen;
5729 uint32_t num_blocks;
5733 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5735 switch (ctsio->cdb[0]) {
5736 case WRITE_SAME_10: {
5737 struct scsi_write_same_10 *cdb;
5739 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5741 lba = scsi_4btoul(cdb->addr);
5742 num_blocks = scsi_2btoul(cdb->length);
5746 case WRITE_SAME_16: {
5747 struct scsi_write_same_16 *cdb;
5749 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5751 lba = scsi_8btou64(cdb->addr);
5752 num_blocks = scsi_4btoul(cdb->length);
5758 * We got a command we don't support. This shouldn't
5759 * happen, commands should be filtered out above us.
5761 ctl_set_invalid_opcode(ctsio);
5762 ctl_done((union ctl_io *)ctsio);
5764 return (CTL_RETVAL_COMPLETE);
5765 break; /* NOTREACHED */
5768 /* ANCHOR flag can be used only together with UNMAP */
5769 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5770 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5771 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5772 ctl_done((union ctl_io *)ctsio);
5773 return (CTL_RETVAL_COMPLETE);
5777 * The first check is to make sure we're in bounds, the second
5778 * check is to catch wrap-around problems. If the lba + num blocks
5779 * is less than the lba, then we've wrapped around and the block
5780 * range is invalid anyway.
5782 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5783 || ((lba + num_blocks) < lba)) {
5784 ctl_set_lba_out_of_range(ctsio,
5785 MAX(lba, lun->be_lun->maxlba + 1));
5786 ctl_done((union ctl_io *)ctsio);
5787 return (CTL_RETVAL_COMPLETE);
5790 /* Zero number of blocks means "to the last logical block" */
5791 if (num_blocks == 0) {
5792 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5793 ctl_set_invalid_field(ctsio,
5799 ctl_done((union ctl_io *)ctsio);
5800 return (CTL_RETVAL_COMPLETE);
5802 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5805 len = lun->be_lun->blocksize;
5808 * If we've got a kernel request that hasn't been malloced yet,
5809 * malloc it and tell the caller the data buffer is here.
5811 if ((byte2 & SWS_NDOB) == 0 &&
5812 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5813 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5814 ctsio->kern_data_len = len;
5815 ctsio->kern_total_len = len;
5816 ctsio->kern_rel_offset = 0;
5817 ctsio->kern_sg_entries = 0;
5818 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5819 ctsio->be_move_done = ctl_config_move_done;
5820 ctl_datamove((union ctl_io *)ctsio);
5822 return (CTL_RETVAL_COMPLETE);
5825 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5827 lbalen->len = num_blocks;
5828 lbalen->flags = byte2;
5829 retval = lun->backend->config_write((union ctl_io *)ctsio);
5835 ctl_unmap(struct ctl_scsiio *ctsio)
5837 struct ctl_lun *lun = CTL_LUN(ctsio);
5838 struct scsi_unmap *cdb;
5839 struct ctl_ptr_len_flags *ptrlen;
5840 struct scsi_unmap_header *hdr;
5841 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5843 uint32_t num_blocks;
5847 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5849 cdb = (struct scsi_unmap *)ctsio->cdb;
5850 len = scsi_2btoul(cdb->length);
5854 * If we've got a kernel request that hasn't been malloced yet,
5855 * malloc it and tell the caller the data buffer is here.
5857 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5858 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5859 ctsio->kern_data_len = len;
5860 ctsio->kern_total_len = len;
5861 ctsio->kern_rel_offset = 0;
5862 ctsio->kern_sg_entries = 0;
5863 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5864 ctsio->be_move_done = ctl_config_move_done;
5865 ctl_datamove((union ctl_io *)ctsio);
5867 return (CTL_RETVAL_COMPLETE);
5870 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5871 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5872 if (len < sizeof (*hdr) ||
5873 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5874 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5875 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5876 ctl_set_invalid_field(ctsio,
5884 len = scsi_2btoul(hdr->desc_length);
5885 buf = (struct scsi_unmap_desc *)(hdr + 1);
5886 end = buf + len / sizeof(*buf);
5889 for (range = buf; range < end; range++) {
5890 lba = scsi_8btou64(range->lba);
5891 num_blocks = scsi_4btoul(range->length);
5892 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5893 || ((lba + num_blocks) < lba)) {
5894 ctl_set_lba_out_of_range(ctsio,
5895 MAX(lba, lun->be_lun->maxlba + 1));
5896 ctl_done((union ctl_io *)ctsio);
5897 return (CTL_RETVAL_COMPLETE);
5899 if (num_blocks != 0)
5904 * Block backend can not handle zero last range.
5905 * Filter it out and return if there is nothing left.
5907 len = (uint8_t *)endnz - (uint8_t *)buf;
5909 ctl_set_success(ctsio);
5913 mtx_lock(&lun->lun_lock);
5914 ptrlen = (struct ctl_ptr_len_flags *)
5915 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5916 ptrlen->ptr = (void *)buf;
5918 ptrlen->flags = byte2;
5919 ctl_check_blocked(lun);
5920 mtx_unlock(&lun->lun_lock);
5922 retval = lun->backend->config_write((union ctl_io *)ctsio);
5926 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5927 free(ctsio->kern_data_ptr, M_CTL);
5928 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5930 ctl_done((union ctl_io *)ctsio);
5931 return (CTL_RETVAL_COMPLETE);
5935 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5936 struct ctl_page_index *page_index, uint8_t *page_ptr)
5938 struct ctl_lun *lun = CTL_LUN(ctsio);
5939 uint8_t *current_cp;
5943 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5946 current_cp = (page_index->page_data + (page_index->page_len *
5949 mtx_lock(&lun->lun_lock);
5950 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5951 memcpy(current_cp, page_ptr, page_index->page_len);
5955 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5956 mtx_unlock(&lun->lun_lock);
5958 ctl_isc_announce_mode(lun,
5959 ctl_get_initindex(&ctsio->io_hdr.nexus),
5960 page_index->page_code, page_index->subpage);
5962 return (CTL_RETVAL_COMPLETE);
5966 ctl_ie_timer(void *arg)
5968 struct ctl_lun *lun = arg;
5971 if (lun->ie_asc == 0)
5974 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5975 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5977 lun->ie_reported = 0;
5979 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5980 lun->ie_reportcnt++;
5981 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5982 if (t == 0 || t == UINT32_MAX)
5983 t = 3000; /* 5 min */
5984 callout_schedule(&lun->ie_callout, t * hz / 10);
5989 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5990 struct ctl_page_index *page_index, uint8_t *page_ptr)
5992 struct ctl_lun *lun = CTL_LUN(ctsio);
5993 struct scsi_info_exceptions_page *pg;
5996 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5998 pg = (struct scsi_info_exceptions_page *)page_ptr;
5999 mtx_lock(&lun->lun_lock);
6000 if (pg->info_flags & SIEP_FLAGS_TEST) {
6002 lun->ie_ascq = 0xff;
6003 if (pg->mrie == SIEP_MRIE_UA) {
6004 ctl_est_ua_all(lun, -1, CTL_UA_IE);
6005 lun->ie_reported = 1;
6007 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6008 lun->ie_reported = -1;
6010 lun->ie_reportcnt = 1;
6011 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
6012 lun->ie_reportcnt++;
6013 t = scsi_4btoul(pg->interval_timer);
6014 if (t == 0 || t == UINT32_MAX)
6015 t = 3000; /* 5 min */
6016 callout_reset(&lun->ie_callout, t * hz / 10,
6022 lun->ie_reported = 1;
6023 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6024 lun->ie_reportcnt = UINT32_MAX;
6025 callout_stop(&lun->ie_callout);
6027 mtx_unlock(&lun->lun_lock);
6028 return (CTL_RETVAL_COMPLETE);
6032 ctl_do_mode_select(union ctl_io *io)
6034 struct ctl_lun *lun = CTL_LUN(io);
6035 struct scsi_mode_page_header *page_header;
6036 struct ctl_page_index *page_index;
6037 struct ctl_scsiio *ctsio;
6038 int page_len, page_len_offset, page_len_size;
6039 union ctl_modepage_info *modepage_info;
6040 uint16_t *len_left, *len_used;
6043 ctsio = &io->scsiio;
6047 modepage_info = (union ctl_modepage_info *)
6048 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6049 len_left = &modepage_info->header.len_left;
6050 len_used = &modepage_info->header.len_used;
6054 page_header = (struct scsi_mode_page_header *)
6055 (ctsio->kern_data_ptr + *len_used);
6057 if (*len_left == 0) {
6058 free(ctsio->kern_data_ptr, M_CTL);
6059 ctl_set_success(ctsio);
6060 ctl_done((union ctl_io *)ctsio);
6061 return (CTL_RETVAL_COMPLETE);
6062 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6064 free(ctsio->kern_data_ptr, M_CTL);
6065 ctl_set_param_len_error(ctsio);
6066 ctl_done((union ctl_io *)ctsio);
6067 return (CTL_RETVAL_COMPLETE);
6069 } else if ((page_header->page_code & SMPH_SPF)
6070 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6072 free(ctsio->kern_data_ptr, M_CTL);
6073 ctl_set_param_len_error(ctsio);
6074 ctl_done((union ctl_io *)ctsio);
6075 return (CTL_RETVAL_COMPLETE);
6080 * XXX KDM should we do something with the block descriptor?
6082 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6083 page_index = &lun->mode_pages.index[i];
6084 if (lun->be_lun->lun_type == T_DIRECT &&
6085 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6087 if (lun->be_lun->lun_type == T_PROCESSOR &&
6088 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6090 if (lun->be_lun->lun_type == T_CDROM &&
6091 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6094 if ((page_index->page_code & SMPH_PC_MASK) !=
6095 (page_header->page_code & SMPH_PC_MASK))
6099 * If neither page has a subpage code, then we've got a
6102 if (((page_index->page_code & SMPH_SPF) == 0)
6103 && ((page_header->page_code & SMPH_SPF) == 0)) {
6104 page_len = page_header->page_length;
6109 * If both pages have subpages, then the subpage numbers
6112 if ((page_index->page_code & SMPH_SPF)
6113 && (page_header->page_code & SMPH_SPF)) {
6114 struct scsi_mode_page_header_sp *sph;
6116 sph = (struct scsi_mode_page_header_sp *)page_header;
6117 if (page_index->subpage == sph->subpage) {
6118 page_len = scsi_2btoul(sph->page_length);
6125 * If we couldn't find the page, or if we don't have a mode select
6126 * handler for it, send back an error to the user.
6128 if ((i >= CTL_NUM_MODE_PAGES)
6129 || (page_index->select_handler == NULL)) {
6130 ctl_set_invalid_field(ctsio,
6133 /*field*/ *len_used,
6136 free(ctsio->kern_data_ptr, M_CTL);
6137 ctl_done((union ctl_io *)ctsio);
6138 return (CTL_RETVAL_COMPLETE);
6141 if (page_index->page_code & SMPH_SPF) {
6142 page_len_offset = 2;
6146 page_len_offset = 1;
6150 * If the length the initiator gives us isn't the one we specify in
6151 * the mode page header, or if they didn't specify enough data in
6152 * the CDB to avoid truncating this page, kick out the request.
6154 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6155 ctl_set_invalid_field(ctsio,
6158 /*field*/ *len_used + page_len_offset,
6161 free(ctsio->kern_data_ptr, M_CTL);
6162 ctl_done((union ctl_io *)ctsio);
6163 return (CTL_RETVAL_COMPLETE);
6165 if (*len_left < page_index->page_len) {
6166 free(ctsio->kern_data_ptr, M_CTL);
6167 ctl_set_param_len_error(ctsio);
6168 ctl_done((union ctl_io *)ctsio);
6169 return (CTL_RETVAL_COMPLETE);
6173 * Run through the mode page, checking to make sure that the bits
6174 * the user changed are actually legal for him to change.
6176 for (i = 0; i < page_index->page_len; i++) {
6177 uint8_t *user_byte, *change_mask, *current_byte;
6181 user_byte = (uint8_t *)page_header + i;
6182 change_mask = page_index->page_data +
6183 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6184 current_byte = page_index->page_data +
6185 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6188 * Check to see whether the user set any bits in this byte
6189 * that he is not allowed to set.
6191 if ((*user_byte & ~(*change_mask)) ==
6192 (*current_byte & ~(*change_mask)))
6196 * Go through bit by bit to determine which one is illegal.
6199 for (j = 7; j >= 0; j--) {
6200 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6201 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6206 ctl_set_invalid_field(ctsio,
6209 /*field*/ *len_used + i,
6212 free(ctsio->kern_data_ptr, M_CTL);
6213 ctl_done((union ctl_io *)ctsio);
6214 return (CTL_RETVAL_COMPLETE);
6218 * Decrement these before we call the page handler, since we may
6219 * end up getting called back one way or another before the handler
6220 * returns to this context.
6222 *len_left -= page_index->page_len;
6223 *len_used += page_index->page_len;
6225 retval = page_index->select_handler(ctsio, page_index,
6226 (uint8_t *)page_header);
6229 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6230 * wait until this queued command completes to finish processing
6231 * the mode page. If it returns anything other than
6232 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6233 * already set the sense information, freed the data pointer, and
6234 * completed the io for us.
6236 if (retval != CTL_RETVAL_COMPLETE)
6237 goto bailout_no_done;
6240 * If the initiator sent us more than one page, parse the next one.
6245 ctl_set_success(ctsio);
6246 free(ctsio->kern_data_ptr, M_CTL);
6247 ctl_done((union ctl_io *)ctsio);
6251 return (CTL_RETVAL_COMPLETE);
6256 ctl_mode_select(struct ctl_scsiio *ctsio)
6258 struct ctl_lun *lun = CTL_LUN(ctsio);
6259 union ctl_modepage_info *modepage_info;
6260 int bd_len, i, header_size, param_len, pf, rtd, sp;
6263 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6264 switch (ctsio->cdb[0]) {
6265 case MODE_SELECT_6: {
6266 struct scsi_mode_select_6 *cdb;
6268 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6270 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6271 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6272 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6273 param_len = cdb->length;
6274 header_size = sizeof(struct scsi_mode_header_6);
6277 case MODE_SELECT_10: {
6278 struct scsi_mode_select_10 *cdb;
6280 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6282 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6283 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6284 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6285 param_len = scsi_2btoul(cdb->length);
6286 header_size = sizeof(struct scsi_mode_header_10);
6290 ctl_set_invalid_opcode(ctsio);
6291 ctl_done((union ctl_io *)ctsio);
6292 return (CTL_RETVAL_COMPLETE);
6296 if (param_len != 0) {
6297 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6298 /*command*/ 1, /*field*/ 0,
6299 /*bit_valid*/ 0, /*bit*/ 0);
6300 ctl_done((union ctl_io *)ctsio);
6301 return (CTL_RETVAL_COMPLETE);
6304 /* Revert to defaults. */
6305 ctl_init_page_index(lun);
6306 mtx_lock(&lun->lun_lock);
6307 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6308 mtx_unlock(&lun->lun_lock);
6309 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6310 ctl_isc_announce_mode(lun, -1,
6311 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6312 lun->mode_pages.index[i].subpage);
6314 ctl_set_success(ctsio);
6315 ctl_done((union ctl_io *)ctsio);
6316 return (CTL_RETVAL_COMPLETE);
6321 * "A parameter list length of zero indicates that the Data-Out Buffer
6322 * shall be empty. This condition shall not be considered as an error."
6324 if (param_len == 0) {
6325 ctl_set_success(ctsio);
6326 ctl_done((union ctl_io *)ctsio);
6327 return (CTL_RETVAL_COMPLETE);
6331 * Since we'll hit this the first time through, prior to
6332 * allocation, we don't need to free a data buffer here.
6334 if (param_len < header_size) {
6335 ctl_set_param_len_error(ctsio);
6336 ctl_done((union ctl_io *)ctsio);
6337 return (CTL_RETVAL_COMPLETE);
6341 * Allocate the data buffer and grab the user's data. In theory,
6342 * we shouldn't have to sanity check the parameter list length here
6343 * because the maximum size is 64K. We should be able to malloc
6344 * that much without too many problems.
6346 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6347 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6348 ctsio->kern_data_len = param_len;
6349 ctsio->kern_total_len = param_len;
6350 ctsio->kern_rel_offset = 0;
6351 ctsio->kern_sg_entries = 0;
6352 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6353 ctsio->be_move_done = ctl_config_move_done;
6354 ctl_datamove((union ctl_io *)ctsio);
6356 return (CTL_RETVAL_COMPLETE);
6359 switch (ctsio->cdb[0]) {
6360 case MODE_SELECT_6: {
6361 struct scsi_mode_header_6 *mh6;
6363 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6364 bd_len = mh6->blk_desc_len;
6367 case MODE_SELECT_10: {
6368 struct scsi_mode_header_10 *mh10;
6370 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6371 bd_len = scsi_2btoul(mh10->blk_desc_len);
6375 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6378 if (param_len < (header_size + bd_len)) {
6379 free(ctsio->kern_data_ptr, M_CTL);
6380 ctl_set_param_len_error(ctsio);
6381 ctl_done((union ctl_io *)ctsio);
6382 return (CTL_RETVAL_COMPLETE);
6386 * Set the IO_CONT flag, so that if this I/O gets passed to
6387 * ctl_config_write_done(), it'll get passed back to
6388 * ctl_do_mode_select() for further processing, or completion if
6391 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6392 ctsio->io_cont = ctl_do_mode_select;
6394 modepage_info = (union ctl_modepage_info *)
6395 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6396 memset(modepage_info, 0, sizeof(*modepage_info));
6397 modepage_info->header.len_left = param_len - header_size - bd_len;
6398 modepage_info->header.len_used = header_size + bd_len;
6400 return (ctl_do_mode_select((union ctl_io *)ctsio));
6404 ctl_mode_sense(struct ctl_scsiio *ctsio)
6406 struct ctl_lun *lun = CTL_LUN(ctsio);
6407 int pc, page_code, dbd, llba, subpage;
6408 int alloc_len, page_len, header_len, total_len;
6409 struct scsi_mode_block_descr *block_desc;
6410 struct ctl_page_index *page_index;
6416 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6418 switch (ctsio->cdb[0]) {
6419 case MODE_SENSE_6: {
6420 struct scsi_mode_sense_6 *cdb;
6422 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6424 header_len = sizeof(struct scsi_mode_hdr_6);
6425 if (cdb->byte2 & SMS_DBD)
6428 header_len += sizeof(struct scsi_mode_block_descr);
6430 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6431 page_code = cdb->page & SMS_PAGE_CODE;
6432 subpage = cdb->subpage;
6433 alloc_len = cdb->length;
6436 case MODE_SENSE_10: {
6437 struct scsi_mode_sense_10 *cdb;
6439 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6441 header_len = sizeof(struct scsi_mode_hdr_10);
6443 if (cdb->byte2 & SMS_DBD)
6446 header_len += sizeof(struct scsi_mode_block_descr);
6447 if (cdb->byte2 & SMS10_LLBAA)
6449 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6450 page_code = cdb->page & SMS_PAGE_CODE;
6451 subpage = cdb->subpage;
6452 alloc_len = scsi_2btoul(cdb->length);
6456 ctl_set_invalid_opcode(ctsio);
6457 ctl_done((union ctl_io *)ctsio);
6458 return (CTL_RETVAL_COMPLETE);
6459 break; /* NOTREACHED */
6463 * We have to make a first pass through to calculate the size of
6464 * the pages that match the user's query. Then we allocate enough
6465 * memory to hold it, and actually copy the data into the buffer.
6467 switch (page_code) {
6468 case SMS_ALL_PAGES_PAGE: {
6474 * At the moment, values other than 0 and 0xff here are
6475 * reserved according to SPC-3.
6477 if ((subpage != SMS_SUBPAGE_PAGE_0)
6478 && (subpage != SMS_SUBPAGE_ALL)) {
6479 ctl_set_invalid_field(ctsio,
6485 ctl_done((union ctl_io *)ctsio);
6486 return (CTL_RETVAL_COMPLETE);
6489 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6490 page_index = &lun->mode_pages.index[i];
6492 /* Make sure the page is supported for this dev type */
6493 if (lun->be_lun->lun_type == T_DIRECT &&
6494 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6496 if (lun->be_lun->lun_type == T_PROCESSOR &&
6497 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6499 if (lun->be_lun->lun_type == T_CDROM &&
6500 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6504 * We don't use this subpage if the user didn't
6505 * request all subpages.
6507 if ((page_index->subpage != 0)
6508 && (subpage == SMS_SUBPAGE_PAGE_0))
6512 printf("found page %#x len %d\n",
6513 page_index->page_code & SMPH_PC_MASK,
6514 page_index->page_len);
6516 page_len += page_index->page_len;
6525 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6526 page_index = &lun->mode_pages.index[i];
6528 /* Make sure the page is supported for this dev type */
6529 if (lun->be_lun->lun_type == T_DIRECT &&
6530 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6532 if (lun->be_lun->lun_type == T_PROCESSOR &&
6533 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6535 if (lun->be_lun->lun_type == T_CDROM &&
6536 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6539 /* Look for the right page code */
6540 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6543 /* Look for the right subpage or the subpage wildcard*/
6544 if ((page_index->subpage != subpage)
6545 && (subpage != SMS_SUBPAGE_ALL))
6549 printf("found page %#x len %d\n",
6550 page_index->page_code & SMPH_PC_MASK,
6551 page_index->page_len);
6554 page_len += page_index->page_len;
6557 if (page_len == 0) {
6558 ctl_set_invalid_field(ctsio,
6564 ctl_done((union ctl_io *)ctsio);
6565 return (CTL_RETVAL_COMPLETE);
6571 total_len = header_len + page_len;
6573 printf("header_len = %d, page_len = %d, total_len = %d\n",
6574 header_len, page_len, total_len);
6577 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6578 ctsio->kern_sg_entries = 0;
6579 ctsio->kern_rel_offset = 0;
6580 ctsio->kern_data_len = min(total_len, alloc_len);
6581 ctsio->kern_total_len = ctsio->kern_data_len;
6583 switch (ctsio->cdb[0]) {
6584 case MODE_SENSE_6: {
6585 struct scsi_mode_hdr_6 *header;
6587 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6589 header->datalen = MIN(total_len - 1, 254);
6590 if (lun->be_lun->lun_type == T_DIRECT) {
6591 header->dev_specific = 0x10; /* DPOFUA */
6592 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6593 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6594 header->dev_specific |= 0x80; /* WP */
6597 header->block_descr_len = 0;
6599 header->block_descr_len =
6600 sizeof(struct scsi_mode_block_descr);
6601 block_desc = (struct scsi_mode_block_descr *)&header[1];
6604 case MODE_SENSE_10: {
6605 struct scsi_mode_hdr_10 *header;
6608 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6610 datalen = MIN(total_len - 2, 65533);
6611 scsi_ulto2b(datalen, header->datalen);
6612 if (lun->be_lun->lun_type == T_DIRECT) {
6613 header->dev_specific = 0x10; /* DPOFUA */
6614 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6615 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6616 header->dev_specific |= 0x80; /* WP */
6619 scsi_ulto2b(0, header->block_descr_len);
6621 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6622 header->block_descr_len);
6623 block_desc = (struct scsi_mode_block_descr *)&header[1];
6627 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6631 * If we've got a disk, use its blocksize in the block
6632 * descriptor. Otherwise, just set it to 0.
6635 if (lun->be_lun->lun_type == T_DIRECT)
6636 scsi_ulto3b(lun->be_lun->blocksize,
6637 block_desc->block_len);
6639 scsi_ulto3b(0, block_desc->block_len);
6642 switch (page_code) {
6643 case SMS_ALL_PAGES_PAGE: {
6646 data_used = header_len;
6647 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6648 struct ctl_page_index *page_index;
6650 page_index = &lun->mode_pages.index[i];
6651 if (lun->be_lun->lun_type == T_DIRECT &&
6652 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6654 if (lun->be_lun->lun_type == T_PROCESSOR &&
6655 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6657 if (lun->be_lun->lun_type == T_CDROM &&
6658 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6662 * We don't use this subpage if the user didn't
6663 * request all subpages. We already checked (above)
6664 * to make sure the user only specified a subpage
6665 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6667 if ((page_index->subpage != 0)
6668 && (subpage == SMS_SUBPAGE_PAGE_0))
6672 * Call the handler, if it exists, to update the
6673 * page to the latest values.
6675 if (page_index->sense_handler != NULL)
6676 page_index->sense_handler(ctsio, page_index,pc);
6678 memcpy(ctsio->kern_data_ptr + data_used,
6679 page_index->page_data +
6680 (page_index->page_len * pc),
6681 page_index->page_len);
6682 data_used += page_index->page_len;
6689 data_used = header_len;
6691 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6692 struct ctl_page_index *page_index;
6694 page_index = &lun->mode_pages.index[i];
6696 /* Look for the right page code */
6697 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6700 /* Look for the right subpage or the subpage wildcard*/
6701 if ((page_index->subpage != subpage)
6702 && (subpage != SMS_SUBPAGE_ALL))
6705 /* Make sure the page is supported for this dev type */
6706 if (lun->be_lun->lun_type == T_DIRECT &&
6707 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6709 if (lun->be_lun->lun_type == T_PROCESSOR &&
6710 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6712 if (lun->be_lun->lun_type == T_CDROM &&
6713 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6717 * Call the handler, if it exists, to update the
6718 * page to the latest values.
6720 if (page_index->sense_handler != NULL)
6721 page_index->sense_handler(ctsio, page_index,pc);
6723 memcpy(ctsio->kern_data_ptr + data_used,
6724 page_index->page_data +
6725 (page_index->page_len * pc),
6726 page_index->page_len);
6727 data_used += page_index->page_len;
6733 ctl_set_success(ctsio);
6734 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6735 ctsio->be_move_done = ctl_config_move_done;
6736 ctl_datamove((union ctl_io *)ctsio);
6737 return (CTL_RETVAL_COMPLETE);
6741 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6742 struct ctl_page_index *page_index,
6745 struct ctl_lun *lun = CTL_LUN(ctsio);
6746 struct scsi_log_param_header *phdr;
6750 data = page_index->page_data;
6752 if (lun->backend->lun_attr != NULL &&
6753 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6755 phdr = (struct scsi_log_param_header *)data;
6756 scsi_ulto2b(0x0001, phdr->param_code);
6757 phdr->param_control = SLP_LBIN | SLP_LP;
6758 phdr->param_len = 8;
6759 data = (uint8_t *)(phdr + 1);
6760 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6761 data[4] = 0x02; /* per-pool */
6762 data += phdr->param_len;
6765 if (lun->backend->lun_attr != NULL &&
6766 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6768 phdr = (struct scsi_log_param_header *)data;
6769 scsi_ulto2b(0x0002, phdr->param_code);
6770 phdr->param_control = SLP_LBIN | SLP_LP;
6771 phdr->param_len = 8;
6772 data = (uint8_t *)(phdr + 1);
6773 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6774 data[4] = 0x01; /* per-LUN */
6775 data += phdr->param_len;
6778 if (lun->backend->lun_attr != NULL &&
6779 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6781 phdr = (struct scsi_log_param_header *)data;
6782 scsi_ulto2b(0x00f1, phdr->param_code);
6783 phdr->param_control = SLP_LBIN | SLP_LP;
6784 phdr->param_len = 8;
6785 data = (uint8_t *)(phdr + 1);
6786 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6787 data[4] = 0x02; /* per-pool */
6788 data += phdr->param_len;
6791 if (lun->backend->lun_attr != NULL &&
6792 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6794 phdr = (struct scsi_log_param_header *)data;
6795 scsi_ulto2b(0x00f2, phdr->param_code);
6796 phdr->param_control = SLP_LBIN | SLP_LP;
6797 phdr->param_len = 8;
6798 data = (uint8_t *)(phdr + 1);
6799 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6800 data[4] = 0x02; /* per-pool */
6801 data += phdr->param_len;
6804 page_index->page_len = data - page_index->page_data;
6809 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6810 struct ctl_page_index *page_index,
6813 struct ctl_lun *lun = CTL_LUN(ctsio);
6814 struct stat_page *data;
6817 data = (struct stat_page *)page_index->page_data;
6819 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6820 data->sap.hdr.param_control = SLP_LBIN;
6821 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6822 sizeof(struct scsi_log_param_header);
6823 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6824 data->sap.read_num);
6825 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6826 data->sap.write_num);
6827 if (lun->be_lun->blocksize > 0) {
6828 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6829 lun->be_lun->blocksize, data->sap.recvieved_lba);
6830 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6831 lun->be_lun->blocksize, data->sap.transmitted_lba);
6833 t = &lun->stats.time[CTL_STATS_READ];
6834 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6835 data->sap.read_int);
6836 t = &lun->stats.time[CTL_STATS_WRITE];
6837 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6838 data->sap.write_int);
6839 scsi_u64to8b(0, data->sap.weighted_num);
6840 scsi_u64to8b(0, data->sap.weighted_int);
6841 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6842 data->it.hdr.param_control = SLP_LBIN;
6843 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6844 sizeof(struct scsi_log_param_header);
6846 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6848 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6849 data->it.hdr.param_control = SLP_LBIN;
6850 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6851 sizeof(struct scsi_log_param_header);
6852 scsi_ulto4b(3, data->ti.exponent);
6853 scsi_ulto4b(1, data->ti.integer);
6858 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6859 struct ctl_page_index *page_index,
6862 struct ctl_lun *lun = CTL_LUN(ctsio);
6863 struct scsi_log_informational_exceptions *data;
6865 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6867 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6868 data->hdr.param_control = SLP_LBIN;
6869 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6870 sizeof(struct scsi_log_param_header);
6871 data->ie_asc = lun->ie_asc;
6872 data->ie_ascq = lun->ie_ascq;
6873 data->temperature = 0xff;
6878 ctl_log_sense(struct ctl_scsiio *ctsio)
6880 struct ctl_lun *lun = CTL_LUN(ctsio);
6881 int i, pc, page_code, subpage;
6882 int alloc_len, total_len;
6883 struct ctl_page_index *page_index;
6884 struct scsi_log_sense *cdb;
6885 struct scsi_log_header *header;
6887 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6889 cdb = (struct scsi_log_sense *)ctsio->cdb;
6890 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6891 page_code = cdb->page & SLS_PAGE_CODE;
6892 subpage = cdb->subpage;
6893 alloc_len = scsi_2btoul(cdb->length);
6896 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6897 page_index = &lun->log_pages.index[i];
6899 /* Look for the right page code */
6900 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6903 /* Look for the right subpage or the subpage wildcard*/
6904 if (page_index->subpage != subpage)
6909 if (i >= CTL_NUM_LOG_PAGES) {
6910 ctl_set_invalid_field(ctsio,
6916 ctl_done((union ctl_io *)ctsio);
6917 return (CTL_RETVAL_COMPLETE);
6920 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6922 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6923 ctsio->kern_sg_entries = 0;
6924 ctsio->kern_rel_offset = 0;
6925 ctsio->kern_data_len = min(total_len, alloc_len);
6926 ctsio->kern_total_len = ctsio->kern_data_len;
6928 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6929 header->page = page_index->page_code;
6930 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6931 header->page |= SL_DS;
6932 if (page_index->subpage) {
6933 header->page |= SL_SPF;
6934 header->subpage = page_index->subpage;
6936 scsi_ulto2b(page_index->page_len, header->datalen);
6939 * Call the handler, if it exists, to update the
6940 * page to the latest values.
6942 if (page_index->sense_handler != NULL)
6943 page_index->sense_handler(ctsio, page_index, pc);
6945 memcpy(header + 1, page_index->page_data, page_index->page_len);
6947 ctl_set_success(ctsio);
6948 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6949 ctsio->be_move_done = ctl_config_move_done;
6950 ctl_datamove((union ctl_io *)ctsio);
6951 return (CTL_RETVAL_COMPLETE);
6955 ctl_read_capacity(struct ctl_scsiio *ctsio)
6957 struct ctl_lun *lun = CTL_LUN(ctsio);
6958 struct scsi_read_capacity *cdb;
6959 struct scsi_read_capacity_data *data;
6962 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6964 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6966 lba = scsi_4btoul(cdb->addr);
6967 if (((cdb->pmi & SRC_PMI) == 0)
6969 ctl_set_invalid_field(/*ctsio*/ ctsio,
6975 ctl_done((union ctl_io *)ctsio);
6976 return (CTL_RETVAL_COMPLETE);
6979 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6980 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6981 ctsio->kern_data_len = sizeof(*data);
6982 ctsio->kern_total_len = sizeof(*data);
6983 ctsio->kern_rel_offset = 0;
6984 ctsio->kern_sg_entries = 0;
6987 * If the maximum LBA is greater than 0xfffffffe, the user must
6988 * issue a SERVICE ACTION IN (16) command, with the read capacity
6989 * serivce action set.
6991 if (lun->be_lun->maxlba > 0xfffffffe)
6992 scsi_ulto4b(0xffffffff, data->addr);
6994 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6997 * XXX KDM this may not be 512 bytes...
6999 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7001 ctl_set_success(ctsio);
7002 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7003 ctsio->be_move_done = ctl_config_move_done;
7004 ctl_datamove((union ctl_io *)ctsio);
7005 return (CTL_RETVAL_COMPLETE);
7009 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7011 struct ctl_lun *lun = CTL_LUN(ctsio);
7012 struct scsi_read_capacity_16 *cdb;
7013 struct scsi_read_capacity_data_long *data;
7017 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7019 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7021 alloc_len = scsi_4btoul(cdb->alloc_len);
7022 lba = scsi_8btou64(cdb->addr);
7024 if ((cdb->reladr & SRC16_PMI)
7026 ctl_set_invalid_field(/*ctsio*/ ctsio,
7032 ctl_done((union ctl_io *)ctsio);
7033 return (CTL_RETVAL_COMPLETE);
7036 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7037 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7038 ctsio->kern_rel_offset = 0;
7039 ctsio->kern_sg_entries = 0;
7040 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
7041 ctsio->kern_total_len = ctsio->kern_data_len;
7043 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7044 /* XXX KDM this may not be 512 bytes... */
7045 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7046 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7047 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7048 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7049 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7051 ctl_set_success(ctsio);
7052 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7053 ctsio->be_move_done = ctl_config_move_done;
7054 ctl_datamove((union ctl_io *)ctsio);
7055 return (CTL_RETVAL_COMPLETE);
7059 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7061 struct ctl_lun *lun = CTL_LUN(ctsio);
7062 struct scsi_get_lba_status *cdb;
7063 struct scsi_get_lba_status_data *data;
7064 struct ctl_lba_len_flags *lbalen;
7066 uint32_t alloc_len, total_len;
7069 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7071 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7072 lba = scsi_8btou64(cdb->addr);
7073 alloc_len = scsi_4btoul(cdb->alloc_len);
7075 if (lba > lun->be_lun->maxlba) {
7076 ctl_set_lba_out_of_range(ctsio, lba);
7077 ctl_done((union ctl_io *)ctsio);
7078 return (CTL_RETVAL_COMPLETE);
7081 total_len = sizeof(*data) + sizeof(data->descr[0]);
7082 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7083 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7084 ctsio->kern_rel_offset = 0;
7085 ctsio->kern_sg_entries = 0;
7086 ctsio->kern_data_len = min(total_len, alloc_len);
7087 ctsio->kern_total_len = ctsio->kern_data_len;
7089 /* Fill dummy data in case backend can't tell anything. */
7090 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7091 scsi_u64to8b(lba, data->descr[0].addr);
7092 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7093 data->descr[0].length);
7094 data->descr[0].status = 0; /* Mapped or unknown. */
7096 ctl_set_success(ctsio);
7097 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7098 ctsio->be_move_done = ctl_config_move_done;
7100 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7102 lbalen->len = total_len;
7104 retval = lun->backend->config_read((union ctl_io *)ctsio);
7109 ctl_read_defect(struct ctl_scsiio *ctsio)
7111 struct scsi_read_defect_data_10 *ccb10;
7112 struct scsi_read_defect_data_12 *ccb12;
7113 struct scsi_read_defect_data_hdr_10 *data10;
7114 struct scsi_read_defect_data_hdr_12 *data12;
7115 uint32_t alloc_len, data_len;
7118 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7120 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7121 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7122 format = ccb10->format;
7123 alloc_len = scsi_2btoul(ccb10->alloc_length);
7124 data_len = sizeof(*data10);
7126 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7127 format = ccb12->format;
7128 alloc_len = scsi_4btoul(ccb12->alloc_length);
7129 data_len = sizeof(*data12);
7131 if (alloc_len == 0) {
7132 ctl_set_success(ctsio);
7133 ctl_done((union ctl_io *)ctsio);
7134 return (CTL_RETVAL_COMPLETE);
7137 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7138 ctsio->kern_rel_offset = 0;
7139 ctsio->kern_sg_entries = 0;
7140 ctsio->kern_data_len = min(data_len, alloc_len);
7141 ctsio->kern_total_len = ctsio->kern_data_len;
7143 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7144 data10 = (struct scsi_read_defect_data_hdr_10 *)
7145 ctsio->kern_data_ptr;
7146 data10->format = format;
7147 scsi_ulto2b(0, data10->length);
7149 data12 = (struct scsi_read_defect_data_hdr_12 *)
7150 ctsio->kern_data_ptr;
7151 data12->format = format;
7152 scsi_ulto2b(0, data12->generation);
7153 scsi_ulto4b(0, data12->length);
7156 ctl_set_success(ctsio);
7157 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7158 ctsio->be_move_done = ctl_config_move_done;
7159 ctl_datamove((union ctl_io *)ctsio);
7160 return (CTL_RETVAL_COMPLETE);
7164 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7166 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7167 struct ctl_lun *lun = CTL_LUN(ctsio);
7168 struct scsi_maintenance_in *cdb;
7170 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7171 int num_ha_groups, num_target_ports, shared_group;
7172 struct ctl_port *port;
7173 struct scsi_target_group_data *rtg_ptr;
7174 struct scsi_target_group_data_extended *rtg_ext_ptr;
7175 struct scsi_target_port_group_descriptor *tpg_desc;
7177 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7179 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7180 retval = CTL_RETVAL_COMPLETE;
7182 switch (cdb->byte2 & STG_PDF_MASK) {
7183 case STG_PDF_LENGTH:
7186 case STG_PDF_EXTENDED:
7190 ctl_set_invalid_field(/*ctsio*/ ctsio,
7196 ctl_done((union ctl_io *)ctsio);
7200 num_target_ports = 0;
7201 shared_group = (softc->is_single != 0);
7202 mtx_lock(&softc->ctl_lock);
7203 STAILQ_FOREACH(port, &softc->port_list, links) {
7204 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7206 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7209 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7212 mtx_unlock(&softc->ctl_lock);
7213 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7216 total_len = sizeof(struct scsi_target_group_data_extended);
7218 total_len = sizeof(struct scsi_target_group_data);
7219 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7220 (shared_group + num_ha_groups) +
7221 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7223 alloc_len = scsi_4btoul(cdb->length);
7225 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7226 ctsio->kern_sg_entries = 0;
7227 ctsio->kern_rel_offset = 0;
7228 ctsio->kern_data_len = min(total_len, alloc_len);
7229 ctsio->kern_total_len = ctsio->kern_data_len;
7232 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7233 ctsio->kern_data_ptr;
7234 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7235 rtg_ext_ptr->format_type = 0x10;
7236 rtg_ext_ptr->implicit_transition_time = 0;
7237 tpg_desc = &rtg_ext_ptr->groups[0];
7239 rtg_ptr = (struct scsi_target_group_data *)
7240 ctsio->kern_data_ptr;
7241 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7242 tpg_desc = &rtg_ptr->groups[0];
7245 mtx_lock(&softc->ctl_lock);
7246 pg = softc->port_min / softc->port_cnt;
7247 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7248 /* Some shelf is known to be primary. */
7249 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7250 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7251 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7252 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7253 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7254 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7256 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7257 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7258 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7261 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7264 /* No known primary shelf. */
7265 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7266 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7267 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7268 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7269 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7270 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7272 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7276 tpg_desc->pref_state = ts;
7277 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7278 TPG_U_SUP | TPG_T_SUP;
7279 scsi_ulto2b(1, tpg_desc->target_port_group);
7280 tpg_desc->status = TPG_IMPLICIT;
7282 STAILQ_FOREACH(port, &softc->port_list, links) {
7283 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7285 if (!softc->is_single &&
7286 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7288 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7290 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7291 relative_target_port_identifier);
7294 tpg_desc->target_port_count = pc;
7295 tpg_desc = (struct scsi_target_port_group_descriptor *)
7296 &tpg_desc->descriptors[pc];
7298 for (g = 0; g < num_ha_groups; g++) {
7299 tpg_desc->pref_state = (g == pg) ? ts : os;
7300 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7301 TPG_U_SUP | TPG_T_SUP;
7302 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7303 tpg_desc->status = TPG_IMPLICIT;
7305 STAILQ_FOREACH(port, &softc->port_list, links) {
7306 if (port->targ_port < g * softc->port_cnt ||
7307 port->targ_port >= (g + 1) * softc->port_cnt)
7309 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7311 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7313 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7315 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7316 relative_target_port_identifier);
7319 tpg_desc->target_port_count = pc;
7320 tpg_desc = (struct scsi_target_port_group_descriptor *)
7321 &tpg_desc->descriptors[pc];
7323 mtx_unlock(&softc->ctl_lock);
7325 ctl_set_success(ctsio);
7326 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7327 ctsio->be_move_done = ctl_config_move_done;
7328 ctl_datamove((union ctl_io *)ctsio);
7333 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7335 struct ctl_lun *lun = CTL_LUN(ctsio);
7336 struct scsi_report_supported_opcodes *cdb;
7337 const struct ctl_cmd_entry *entry, *sentry;
7338 struct scsi_report_supported_opcodes_all *all;
7339 struct scsi_report_supported_opcodes_descr *descr;
7340 struct scsi_report_supported_opcodes_one *one;
7342 int alloc_len, total_len;
7343 int opcode, service_action, i, j, num;
7345 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7347 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7348 retval = CTL_RETVAL_COMPLETE;
7350 opcode = cdb->requested_opcode;
7351 service_action = scsi_2btoul(cdb->requested_service_action);
7352 switch (cdb->options & RSO_OPTIONS_MASK) {
7353 case RSO_OPTIONS_ALL:
7355 for (i = 0; i < 256; i++) {
7356 entry = &ctl_cmd_table[i];
7357 if (entry->flags & CTL_CMD_FLAG_SA5) {
7358 for (j = 0; j < 32; j++) {
7359 sentry = &((const struct ctl_cmd_entry *)
7361 if (ctl_cmd_applicable(
7362 lun->be_lun->lun_type, sentry))
7366 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7371 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7372 num * sizeof(struct scsi_report_supported_opcodes_descr);
7374 case RSO_OPTIONS_OC:
7375 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7376 ctl_set_invalid_field(/*ctsio*/ ctsio,
7382 ctl_done((union ctl_io *)ctsio);
7383 return (CTL_RETVAL_COMPLETE);
7385 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7387 case RSO_OPTIONS_OC_SA:
7388 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7389 service_action >= 32) {
7390 ctl_set_invalid_field(/*ctsio*/ ctsio,
7396 ctl_done((union ctl_io *)ctsio);
7397 return (CTL_RETVAL_COMPLETE);
7400 case RSO_OPTIONS_OC_ASA:
7401 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7404 ctl_set_invalid_field(/*ctsio*/ ctsio,
7410 ctl_done((union ctl_io *)ctsio);
7411 return (CTL_RETVAL_COMPLETE);
7414 alloc_len = scsi_4btoul(cdb->length);
7416 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7417 ctsio->kern_sg_entries = 0;
7418 ctsio->kern_rel_offset = 0;
7419 ctsio->kern_data_len = min(total_len, alloc_len);
7420 ctsio->kern_total_len = ctsio->kern_data_len;
7422 switch (cdb->options & RSO_OPTIONS_MASK) {
7423 case RSO_OPTIONS_ALL:
7424 all = (struct scsi_report_supported_opcodes_all *)
7425 ctsio->kern_data_ptr;
7427 for (i = 0; i < 256; i++) {
7428 entry = &ctl_cmd_table[i];
7429 if (entry->flags & CTL_CMD_FLAG_SA5) {
7430 for (j = 0; j < 32; j++) {
7431 sentry = &((const struct ctl_cmd_entry *)
7433 if (!ctl_cmd_applicable(
7434 lun->be_lun->lun_type, sentry))
7436 descr = &all->descr[num++];
7438 scsi_ulto2b(j, descr->service_action);
7439 descr->flags = RSO_SERVACTV;
7440 scsi_ulto2b(sentry->length,
7444 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7447 descr = &all->descr[num++];
7449 scsi_ulto2b(0, descr->service_action);
7451 scsi_ulto2b(entry->length, descr->cdb_length);
7455 num * sizeof(struct scsi_report_supported_opcodes_descr),
7458 case RSO_OPTIONS_OC:
7459 one = (struct scsi_report_supported_opcodes_one *)
7460 ctsio->kern_data_ptr;
7461 entry = &ctl_cmd_table[opcode];
7463 case RSO_OPTIONS_OC_SA:
7464 one = (struct scsi_report_supported_opcodes_one *)
7465 ctsio->kern_data_ptr;
7466 entry = &ctl_cmd_table[opcode];
7467 entry = &((const struct ctl_cmd_entry *)
7468 entry->execute)[service_action];
7470 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7472 scsi_ulto2b(entry->length, one->cdb_length);
7473 one->cdb_usage[0] = opcode;
7474 memcpy(&one->cdb_usage[1], entry->usage,
7479 case RSO_OPTIONS_OC_ASA:
7480 one = (struct scsi_report_supported_opcodes_one *)
7481 ctsio->kern_data_ptr;
7482 entry = &ctl_cmd_table[opcode];
7483 if (entry->flags & CTL_CMD_FLAG_SA5) {
7484 entry = &((const struct ctl_cmd_entry *)
7485 entry->execute)[service_action];
7486 } else if (service_action != 0) {
7493 ctl_set_success(ctsio);
7494 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7495 ctsio->be_move_done = ctl_config_move_done;
7496 ctl_datamove((union ctl_io *)ctsio);
7501 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7503 struct scsi_report_supported_tmf *cdb;
7504 struct scsi_report_supported_tmf_ext_data *data;
7506 int alloc_len, total_len;
7508 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7510 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7512 retval = CTL_RETVAL_COMPLETE;
7514 if (cdb->options & RST_REPD)
7515 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7517 total_len = sizeof(struct scsi_report_supported_tmf_data);
7518 alloc_len = scsi_4btoul(cdb->length);
7520 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7521 ctsio->kern_sg_entries = 0;
7522 ctsio->kern_rel_offset = 0;
7523 ctsio->kern_data_len = min(total_len, alloc_len);
7524 ctsio->kern_total_len = ctsio->kern_data_len;
7526 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7527 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7529 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7530 data->length = total_len - 4;
7532 ctl_set_success(ctsio);
7533 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7534 ctsio->be_move_done = ctl_config_move_done;
7535 ctl_datamove((union ctl_io *)ctsio);
7540 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7542 struct scsi_report_timestamp *cdb;
7543 struct scsi_report_timestamp_data *data;
7547 int alloc_len, total_len;
7549 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7551 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7553 retval = CTL_RETVAL_COMPLETE;
7555 total_len = sizeof(struct scsi_report_timestamp_data);
7556 alloc_len = scsi_4btoul(cdb->length);
7558 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7559 ctsio->kern_sg_entries = 0;
7560 ctsio->kern_rel_offset = 0;
7561 ctsio->kern_data_len = min(total_len, alloc_len);
7562 ctsio->kern_total_len = ctsio->kern_data_len;
7564 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7565 scsi_ulto2b(sizeof(*data) - 2, data->length);
7566 data->origin = RTS_ORIG_OUTSIDE;
7568 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7569 scsi_ulto4b(timestamp >> 16, data->timestamp);
7570 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7572 ctl_set_success(ctsio);
7573 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7574 ctsio->be_move_done = ctl_config_move_done;
7575 ctl_datamove((union ctl_io *)ctsio);
7580 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7582 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7583 struct ctl_lun *lun = CTL_LUN(ctsio);
7584 struct scsi_per_res_in *cdb;
7585 int alloc_len, total_len = 0;
7586 /* struct scsi_per_res_in_rsrv in_data; */
7589 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7591 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7593 alloc_len = scsi_2btoul(cdb->length);
7596 mtx_lock(&lun->lun_lock);
7597 switch (cdb->action) {
7598 case SPRI_RK: /* read keys */
7599 total_len = sizeof(struct scsi_per_res_in_keys) +
7601 sizeof(struct scsi_per_res_key);
7603 case SPRI_RR: /* read reservation */
7604 if (lun->flags & CTL_LUN_PR_RESERVED)
7605 total_len = sizeof(struct scsi_per_res_in_rsrv);
7607 total_len = sizeof(struct scsi_per_res_in_header);
7609 case SPRI_RC: /* report capabilities */
7610 total_len = sizeof(struct scsi_per_res_cap);
7612 case SPRI_RS: /* read full status */
7613 total_len = sizeof(struct scsi_per_res_in_header) +
7614 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7618 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7620 mtx_unlock(&lun->lun_lock);
7622 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7623 ctsio->kern_rel_offset = 0;
7624 ctsio->kern_sg_entries = 0;
7625 ctsio->kern_data_len = min(total_len, alloc_len);
7626 ctsio->kern_total_len = ctsio->kern_data_len;
7628 mtx_lock(&lun->lun_lock);
7629 switch (cdb->action) {
7630 case SPRI_RK: { // read keys
7631 struct scsi_per_res_in_keys *res_keys;
7634 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7637 * We had to drop the lock to allocate our buffer, which
7638 * leaves time for someone to come in with another
7639 * persistent reservation. (That is unlikely, though,
7640 * since this should be the only persistent reservation
7641 * command active right now.)
7643 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7644 (lun->pr_key_count *
7645 sizeof(struct scsi_per_res_key)))){
7646 mtx_unlock(&lun->lun_lock);
7647 free(ctsio->kern_data_ptr, M_CTL);
7648 printf("%s: reservation length changed, retrying\n",
7653 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7655 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7656 lun->pr_key_count, res_keys->header.length);
7658 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7659 if ((key = ctl_get_prkey(lun, i)) == 0)
7663 * We used lun->pr_key_count to calculate the
7664 * size to allocate. If it turns out the number of
7665 * initiators with the registered flag set is
7666 * larger than that (i.e. they haven't been kept in
7667 * sync), we've got a problem.
7669 if (key_count >= lun->pr_key_count) {
7673 scsi_u64to8b(key, res_keys->keys[key_count].key);
7678 case SPRI_RR: { // read reservation
7679 struct scsi_per_res_in_rsrv *res;
7680 int tmp_len, header_only;
7682 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7684 scsi_ulto4b(lun->pr_generation, res->header.generation);
7686 if (lun->flags & CTL_LUN_PR_RESERVED)
7688 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7689 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7690 res->header.length);
7693 tmp_len = sizeof(struct scsi_per_res_in_header);
7694 scsi_ulto4b(0, res->header.length);
7699 * We had to drop the lock to allocate our buffer, which
7700 * leaves time for someone to come in with another
7701 * persistent reservation. (That is unlikely, though,
7702 * since this should be the only persistent reservation
7703 * command active right now.)
7705 if (tmp_len != total_len) {
7706 mtx_unlock(&lun->lun_lock);
7707 free(ctsio->kern_data_ptr, M_CTL);
7708 printf("%s: reservation status changed, retrying\n",
7714 * No reservation held, so we're done.
7716 if (header_only != 0)
7720 * If the registration is an All Registrants type, the key
7721 * is 0, since it doesn't really matter.
7723 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7724 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7725 res->data.reservation);
7727 res->data.scopetype = lun->pr_res_type;
7730 case SPRI_RC: //report capabilities
7732 struct scsi_per_res_cap *res_cap;
7735 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7736 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7737 res_cap->flags1 = SPRI_CRH;
7738 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7739 type_mask = SPRI_TM_WR_EX_AR |
7745 scsi_ulto2b(type_mask, res_cap->type_mask);
7748 case SPRI_RS: { // read full status
7749 struct scsi_per_res_in_full *res_status;
7750 struct scsi_per_res_in_full_desc *res_desc;
7751 struct ctl_port *port;
7754 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7757 * We had to drop the lock to allocate our buffer, which
7758 * leaves time for someone to come in with another
7759 * persistent reservation. (That is unlikely, though,
7760 * since this should be the only persistent reservation
7761 * command active right now.)
7763 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7764 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7765 lun->pr_key_count)){
7766 mtx_unlock(&lun->lun_lock);
7767 free(ctsio->kern_data_ptr, M_CTL);
7768 printf("%s: reservation length changed, retrying\n",
7773 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7775 res_desc = &res_status->desc[0];
7776 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7777 if ((key = ctl_get_prkey(lun, i)) == 0)
7780 scsi_u64to8b(key, res_desc->res_key.key);
7781 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7782 (lun->pr_res_idx == i ||
7783 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7784 res_desc->flags = SPRI_FULL_R_HOLDER;
7785 res_desc->scopetype = lun->pr_res_type;
7787 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7788 res_desc->rel_trgt_port_id);
7790 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7792 len = ctl_create_iid(port,
7793 i % CTL_MAX_INIT_PER_PORT,
7794 res_desc->transport_id);
7795 scsi_ulto4b(len, res_desc->additional_length);
7796 res_desc = (struct scsi_per_res_in_full_desc *)
7797 &res_desc->transport_id[len];
7799 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7800 res_status->header.length);
7804 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7806 mtx_unlock(&lun->lun_lock);
7808 ctl_set_success(ctsio);
7809 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7810 ctsio->be_move_done = ctl_config_move_done;
7811 ctl_datamove((union ctl_io *)ctsio);
7812 return (CTL_RETVAL_COMPLETE);
7816 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7820 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7821 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7822 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7823 struct scsi_per_res_out_parms* param)
7825 union ctl_ha_msg persis_io;
7828 mtx_lock(&lun->lun_lock);
7829 if (sa_res_key == 0) {
7830 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7831 /* validate scope and type */
7832 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7834 mtx_unlock(&lun->lun_lock);
7835 ctl_set_invalid_field(/*ctsio*/ ctsio,
7841 ctl_done((union ctl_io *)ctsio);
7845 if (type>8 || type==2 || type==4 || type==0) {
7846 mtx_unlock(&lun->lun_lock);
7847 ctl_set_invalid_field(/*ctsio*/ ctsio,
7853 ctl_done((union ctl_io *)ctsio);
7858 * Unregister everybody else and build UA for
7861 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7862 if (i == residx || ctl_get_prkey(lun, i) == 0)
7865 ctl_clr_prkey(lun, i);
7866 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7868 lun->pr_key_count = 1;
7869 lun->pr_res_type = type;
7870 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7871 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7872 lun->pr_res_idx = residx;
7873 lun->pr_generation++;
7874 mtx_unlock(&lun->lun_lock);
7876 /* send msg to other side */
7877 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7878 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7879 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7880 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7881 persis_io.pr.pr_info.res_type = type;
7882 memcpy(persis_io.pr.pr_info.sa_res_key,
7883 param->serv_act_res_key,
7884 sizeof(param->serv_act_res_key));
7885 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7886 sizeof(persis_io.pr), M_WAITOK);
7888 /* not all registrants */
7889 mtx_unlock(&lun->lun_lock);
7890 free(ctsio->kern_data_ptr, M_CTL);
7891 ctl_set_invalid_field(ctsio,
7897 ctl_done((union ctl_io *)ctsio);
7900 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7901 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7904 if (res_key == sa_res_key) {
7907 * The spec implies this is not good but doesn't
7908 * say what to do. There are two choices either
7909 * generate a res conflict or check condition
7910 * with illegal field in parameter data. Since
7911 * that is what is done when the sa_res_key is
7912 * zero I'll take that approach since this has
7913 * to do with the sa_res_key.
7915 mtx_unlock(&lun->lun_lock);
7916 free(ctsio->kern_data_ptr, M_CTL);
7917 ctl_set_invalid_field(ctsio,
7923 ctl_done((union ctl_io *)ctsio);
7927 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7928 if (ctl_get_prkey(lun, i) != sa_res_key)
7932 ctl_clr_prkey(lun, i);
7933 lun->pr_key_count--;
7934 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7937 mtx_unlock(&lun->lun_lock);
7938 free(ctsio->kern_data_ptr, M_CTL);
7939 ctl_set_reservation_conflict(ctsio);
7940 ctl_done((union ctl_io *)ctsio);
7941 return (CTL_RETVAL_COMPLETE);
7943 lun->pr_generation++;
7944 mtx_unlock(&lun->lun_lock);
7946 /* send msg to other side */
7947 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7948 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7949 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7950 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7951 persis_io.pr.pr_info.res_type = type;
7952 memcpy(persis_io.pr.pr_info.sa_res_key,
7953 param->serv_act_res_key,
7954 sizeof(param->serv_act_res_key));
7955 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7956 sizeof(persis_io.pr), M_WAITOK);
7958 /* Reserved but not all registrants */
7959 /* sa_res_key is res holder */
7960 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7961 /* validate scope and type */
7962 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7964 mtx_unlock(&lun->lun_lock);
7965 ctl_set_invalid_field(/*ctsio*/ ctsio,
7971 ctl_done((union ctl_io *)ctsio);
7975 if (type>8 || type==2 || type==4 || type==0) {
7976 mtx_unlock(&lun->lun_lock);
7977 ctl_set_invalid_field(/*ctsio*/ ctsio,
7983 ctl_done((union ctl_io *)ctsio);
7989 * if sa_res_key != res_key remove all
7990 * registrants w/sa_res_key and generate UA
7991 * for these registrants(Registrations
7992 * Preempted) if it wasn't an exclusive
7993 * reservation generate UA(Reservations
7994 * Preempted) for all other registered nexuses
7995 * if the type has changed. Establish the new
7996 * reservation and holder. If res_key and
7997 * sa_res_key are the same do the above
7998 * except don't unregister the res holder.
8001 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8002 if (i == residx || ctl_get_prkey(lun, i) == 0)
8005 if (sa_res_key == ctl_get_prkey(lun, i)) {
8006 ctl_clr_prkey(lun, i);
8007 lun->pr_key_count--;
8008 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8009 } else if (type != lun->pr_res_type &&
8010 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8011 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8012 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8015 lun->pr_res_type = type;
8016 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8017 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8018 lun->pr_res_idx = residx;
8020 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8021 lun->pr_generation++;
8022 mtx_unlock(&lun->lun_lock);
8024 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8025 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8026 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8027 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8028 persis_io.pr.pr_info.res_type = type;
8029 memcpy(persis_io.pr.pr_info.sa_res_key,
8030 param->serv_act_res_key,
8031 sizeof(param->serv_act_res_key));
8032 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8033 sizeof(persis_io.pr), M_WAITOK);
8036 * sa_res_key is not the res holder just
8037 * remove registrants
8041 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8042 if (sa_res_key != ctl_get_prkey(lun, i))
8046 ctl_clr_prkey(lun, i);
8047 lun->pr_key_count--;
8048 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8052 mtx_unlock(&lun->lun_lock);
8053 free(ctsio->kern_data_ptr, M_CTL);
8054 ctl_set_reservation_conflict(ctsio);
8055 ctl_done((union ctl_io *)ctsio);
8058 lun->pr_generation++;
8059 mtx_unlock(&lun->lun_lock);
8061 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8062 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8063 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8064 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8065 persis_io.pr.pr_info.res_type = type;
8066 memcpy(persis_io.pr.pr_info.sa_res_key,
8067 param->serv_act_res_key,
8068 sizeof(param->serv_act_res_key));
8069 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8070 sizeof(persis_io.pr), M_WAITOK);
8077 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8079 uint64_t sa_res_key;
8082 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8084 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8085 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8086 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8087 if (sa_res_key == 0) {
8089 * Unregister everybody else and build UA for
8092 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8093 if (i == msg->pr.pr_info.residx ||
8094 ctl_get_prkey(lun, i) == 0)
8097 ctl_clr_prkey(lun, i);
8098 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8101 lun->pr_key_count = 1;
8102 lun->pr_res_type = msg->pr.pr_info.res_type;
8103 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8104 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8105 lun->pr_res_idx = msg->pr.pr_info.residx;
8107 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8108 if (sa_res_key == ctl_get_prkey(lun, i))
8111 ctl_clr_prkey(lun, i);
8112 lun->pr_key_count--;
8113 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8117 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8118 if (i == msg->pr.pr_info.residx ||
8119 ctl_get_prkey(lun, i) == 0)
8122 if (sa_res_key == ctl_get_prkey(lun, i)) {
8123 ctl_clr_prkey(lun, i);
8124 lun->pr_key_count--;
8125 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8126 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8127 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8128 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8129 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8132 lun->pr_res_type = msg->pr.pr_info.res_type;
8133 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8134 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8135 lun->pr_res_idx = msg->pr.pr_info.residx;
8137 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8139 lun->pr_generation++;
8145 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8147 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8148 struct ctl_lun *lun = CTL_LUN(ctsio);
8150 u_int32_t param_len;
8151 struct scsi_per_res_out *cdb;
8152 struct scsi_per_res_out_parms* param;
8154 uint64_t res_key, sa_res_key, key;
8156 union ctl_ha_msg persis_io;
8159 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8161 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8162 retval = CTL_RETVAL_COMPLETE;
8165 * We only support whole-LUN scope. The scope & type are ignored for
8166 * register, register and ignore existing key and clear.
8167 * We sometimes ignore scope and type on preempts too!!
8168 * Verify reservation type here as well.
8170 type = cdb->scope_type & SPR_TYPE_MASK;
8171 if ((cdb->action == SPRO_RESERVE)
8172 || (cdb->action == SPRO_RELEASE)) {
8173 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8174 ctl_set_invalid_field(/*ctsio*/ ctsio,
8180 ctl_done((union ctl_io *)ctsio);
8181 return (CTL_RETVAL_COMPLETE);
8184 if (type>8 || type==2 || type==4 || type==0) {
8185 ctl_set_invalid_field(/*ctsio*/ ctsio,
8191 ctl_done((union ctl_io *)ctsio);
8192 return (CTL_RETVAL_COMPLETE);
8196 param_len = scsi_4btoul(cdb->length);
8198 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8199 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8200 ctsio->kern_data_len = param_len;
8201 ctsio->kern_total_len = param_len;
8202 ctsio->kern_rel_offset = 0;
8203 ctsio->kern_sg_entries = 0;
8204 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8205 ctsio->be_move_done = ctl_config_move_done;
8206 ctl_datamove((union ctl_io *)ctsio);
8208 return (CTL_RETVAL_COMPLETE);
8211 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8213 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8214 res_key = scsi_8btou64(param->res_key.key);
8215 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8218 * Validate the reservation key here except for SPRO_REG_IGNO
8219 * This must be done for all other service actions
8221 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8222 mtx_lock(&lun->lun_lock);
8223 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8224 if (res_key != key) {
8226 * The current key passed in doesn't match
8227 * the one the initiator previously
8230 mtx_unlock(&lun->lun_lock);
8231 free(ctsio->kern_data_ptr, M_CTL);
8232 ctl_set_reservation_conflict(ctsio);
8233 ctl_done((union ctl_io *)ctsio);
8234 return (CTL_RETVAL_COMPLETE);
8236 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8238 * We are not registered
8240 mtx_unlock(&lun->lun_lock);
8241 free(ctsio->kern_data_ptr, M_CTL);
8242 ctl_set_reservation_conflict(ctsio);
8243 ctl_done((union ctl_io *)ctsio);
8244 return (CTL_RETVAL_COMPLETE);
8245 } else if (res_key != 0) {
8247 * We are not registered and trying to register but
8248 * the register key isn't zero.
8250 mtx_unlock(&lun->lun_lock);
8251 free(ctsio->kern_data_ptr, M_CTL);
8252 ctl_set_reservation_conflict(ctsio);
8253 ctl_done((union ctl_io *)ctsio);
8254 return (CTL_RETVAL_COMPLETE);
8256 mtx_unlock(&lun->lun_lock);
8259 switch (cdb->action & SPRO_ACTION_MASK) {
8261 case SPRO_REG_IGNO: {
8264 printf("Registration received\n");
8268 * We don't support any of these options, as we report in
8269 * the read capabilities request (see
8270 * ctl_persistent_reserve_in(), above).
8272 if ((param->flags & SPR_SPEC_I_PT)
8273 || (param->flags & SPR_ALL_TG_PT)
8274 || (param->flags & SPR_APTPL)) {
8277 if (param->flags & SPR_APTPL)
8279 else if (param->flags & SPR_ALL_TG_PT)
8281 else /* SPR_SPEC_I_PT */
8284 free(ctsio->kern_data_ptr, M_CTL);
8285 ctl_set_invalid_field(ctsio,
8291 ctl_done((union ctl_io *)ctsio);
8292 return (CTL_RETVAL_COMPLETE);
8295 mtx_lock(&lun->lun_lock);
8298 * The initiator wants to clear the
8301 if (sa_res_key == 0) {
8303 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8304 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8305 && ctl_get_prkey(lun, residx) == 0)) {
8306 mtx_unlock(&lun->lun_lock);
8310 ctl_clr_prkey(lun, residx);
8311 lun->pr_key_count--;
8313 if (residx == lun->pr_res_idx) {
8314 lun->flags &= ~CTL_LUN_PR_RESERVED;
8315 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8317 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8318 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8319 lun->pr_key_count) {
8321 * If the reservation is a registrants
8322 * only type we need to generate a UA
8323 * for other registered inits. The
8324 * sense code should be RESERVATIONS
8328 for (i = softc->init_min; i < softc->init_max; i++){
8329 if (ctl_get_prkey(lun, i) == 0)
8332 CTL_UA_RES_RELEASE);
8335 lun->pr_res_type = 0;
8336 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8337 if (lun->pr_key_count==0) {
8338 lun->flags &= ~CTL_LUN_PR_RESERVED;
8339 lun->pr_res_type = 0;
8340 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8343 lun->pr_generation++;
8344 mtx_unlock(&lun->lun_lock);
8346 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8347 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8348 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8349 persis_io.pr.pr_info.residx = residx;
8350 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8351 sizeof(persis_io.pr), M_WAITOK);
8352 } else /* sa_res_key != 0 */ {
8355 * If we aren't registered currently then increment
8356 * the key count and set the registered flag.
8358 ctl_alloc_prkey(lun, residx);
8359 if (ctl_get_prkey(lun, residx) == 0)
8360 lun->pr_key_count++;
8361 ctl_set_prkey(lun, residx, sa_res_key);
8362 lun->pr_generation++;
8363 mtx_unlock(&lun->lun_lock);
8365 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8366 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8367 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8368 persis_io.pr.pr_info.residx = residx;
8369 memcpy(persis_io.pr.pr_info.sa_res_key,
8370 param->serv_act_res_key,
8371 sizeof(param->serv_act_res_key));
8372 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8373 sizeof(persis_io.pr), M_WAITOK);
8380 printf("Reserve executed type %d\n", type);
8382 mtx_lock(&lun->lun_lock);
8383 if (lun->flags & CTL_LUN_PR_RESERVED) {
8385 * if this isn't the reservation holder and it's
8386 * not a "all registrants" type or if the type is
8387 * different then we have a conflict
8389 if ((lun->pr_res_idx != residx
8390 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8391 || lun->pr_res_type != type) {
8392 mtx_unlock(&lun->lun_lock);
8393 free(ctsio->kern_data_ptr, M_CTL);
8394 ctl_set_reservation_conflict(ctsio);
8395 ctl_done((union ctl_io *)ctsio);
8396 return (CTL_RETVAL_COMPLETE);
8398 mtx_unlock(&lun->lun_lock);
8399 } else /* create a reservation */ {
8401 * If it's not an "all registrants" type record
8402 * reservation holder
8404 if (type != SPR_TYPE_WR_EX_AR
8405 && type != SPR_TYPE_EX_AC_AR)
8406 lun->pr_res_idx = residx; /* Res holder */
8408 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8410 lun->flags |= CTL_LUN_PR_RESERVED;
8411 lun->pr_res_type = type;
8413 mtx_unlock(&lun->lun_lock);
8415 /* send msg to other side */
8416 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8417 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8418 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8419 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8420 persis_io.pr.pr_info.res_type = type;
8421 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8422 sizeof(persis_io.pr), M_WAITOK);
8427 mtx_lock(&lun->lun_lock);
8428 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8429 /* No reservation exists return good status */
8430 mtx_unlock(&lun->lun_lock);
8434 * Is this nexus a reservation holder?
8436 if (lun->pr_res_idx != residx
8437 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8439 * not a res holder return good status but
8442 mtx_unlock(&lun->lun_lock);
8446 if (lun->pr_res_type != type) {
8447 mtx_unlock(&lun->lun_lock);
8448 free(ctsio->kern_data_ptr, M_CTL);
8449 ctl_set_illegal_pr_release(ctsio);
8450 ctl_done((union ctl_io *)ctsio);
8451 return (CTL_RETVAL_COMPLETE);
8454 /* okay to release */
8455 lun->flags &= ~CTL_LUN_PR_RESERVED;
8456 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8457 lun->pr_res_type = 0;
8460 * If this isn't an exclusive access reservation and NUAR
8461 * is not set, generate UA for all other registrants.
8463 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8464 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8465 for (i = softc->init_min; i < softc->init_max; i++) {
8466 if (i == residx || ctl_get_prkey(lun, i) == 0)
8468 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8471 mtx_unlock(&lun->lun_lock);
8473 /* Send msg to other side */
8474 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8475 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8476 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8477 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8478 sizeof(persis_io.pr), M_WAITOK);
8482 /* send msg to other side */
8484 mtx_lock(&lun->lun_lock);
8485 lun->flags &= ~CTL_LUN_PR_RESERVED;
8486 lun->pr_res_type = 0;
8487 lun->pr_key_count = 0;
8488 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8490 ctl_clr_prkey(lun, residx);
8491 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8492 if (ctl_get_prkey(lun, i) != 0) {
8493 ctl_clr_prkey(lun, i);
8494 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8496 lun->pr_generation++;
8497 mtx_unlock(&lun->lun_lock);
8499 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8500 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8501 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8502 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8503 sizeof(persis_io.pr), M_WAITOK);
8507 case SPRO_PRE_ABO: {
8510 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8511 residx, ctsio, cdb, param);
8513 return (CTL_RETVAL_COMPLETE);
8517 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8521 free(ctsio->kern_data_ptr, M_CTL);
8522 ctl_set_success(ctsio);
8523 ctl_done((union ctl_io *)ctsio);
8529 * This routine is for handling a message from the other SC pertaining to
8530 * persistent reserve out. All the error checking will have been done
8531 * so only perorming the action need be done here to keep the two
8535 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8537 struct ctl_softc *softc = CTL_SOFTC(io);
8538 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8539 struct ctl_lun *lun;
8541 uint32_t residx, targ_lun;
8543 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8544 mtx_lock(&softc->ctl_lock);
8545 if (targ_lun >= ctl_max_luns ||
8546 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8547 mtx_unlock(&softc->ctl_lock);
8550 mtx_lock(&lun->lun_lock);
8551 mtx_unlock(&softc->ctl_lock);
8552 if (lun->flags & CTL_LUN_DISABLED) {
8553 mtx_unlock(&lun->lun_lock);
8556 residx = ctl_get_initindex(&msg->hdr.nexus);
8557 switch(msg->pr.pr_info.action) {
8558 case CTL_PR_REG_KEY:
8559 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8560 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8561 lun->pr_key_count++;
8562 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8563 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8564 lun->pr_generation++;
8567 case CTL_PR_UNREG_KEY:
8568 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8569 lun->pr_key_count--;
8571 /* XXX Need to see if the reservation has been released */
8572 /* if so do we need to generate UA? */
8573 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8574 lun->flags &= ~CTL_LUN_PR_RESERVED;
8575 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8577 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8578 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8579 lun->pr_key_count) {
8581 * If the reservation is a registrants
8582 * only type we need to generate a UA
8583 * for other registered inits. The
8584 * sense code should be RESERVATIONS
8588 for (i = softc->init_min; i < softc->init_max; i++) {
8589 if (ctl_get_prkey(lun, i) == 0)
8592 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8595 lun->pr_res_type = 0;
8596 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8597 if (lun->pr_key_count==0) {
8598 lun->flags &= ~CTL_LUN_PR_RESERVED;
8599 lun->pr_res_type = 0;
8600 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8603 lun->pr_generation++;
8606 case CTL_PR_RESERVE:
8607 lun->flags |= CTL_LUN_PR_RESERVED;
8608 lun->pr_res_type = msg->pr.pr_info.res_type;
8609 lun->pr_res_idx = msg->pr.pr_info.residx;
8613 case CTL_PR_RELEASE:
8615 * If this isn't an exclusive access reservation and NUAR
8616 * is not set, generate UA for all other registrants.
8618 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8619 lun->pr_res_type != SPR_TYPE_WR_EX &&
8620 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8621 for (i = softc->init_min; i < softc->init_max; i++)
8622 if (i == residx || ctl_get_prkey(lun, i) == 0)
8624 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8627 lun->flags &= ~CTL_LUN_PR_RESERVED;
8628 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8629 lun->pr_res_type = 0;
8632 case CTL_PR_PREEMPT:
8633 ctl_pro_preempt_other(lun, msg);
8636 lun->flags &= ~CTL_LUN_PR_RESERVED;
8637 lun->pr_res_type = 0;
8638 lun->pr_key_count = 0;
8639 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8641 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8642 if (ctl_get_prkey(lun, i) == 0)
8644 ctl_clr_prkey(lun, i);
8645 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8647 lun->pr_generation++;
8651 mtx_unlock(&lun->lun_lock);
8655 ctl_read_write(struct ctl_scsiio *ctsio)
8657 struct ctl_lun *lun = CTL_LUN(ctsio);
8658 struct ctl_lba_len_flags *lbalen;
8660 uint32_t num_blocks;
8664 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8667 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8668 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8669 switch (ctsio->cdb[0]) {
8672 struct scsi_rw_6 *cdb;
8674 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8676 lba = scsi_3btoul(cdb->addr);
8677 /* only 5 bits are valid in the most significant address byte */
8679 num_blocks = cdb->length;
8681 * This is correct according to SBC-2.
8683 if (num_blocks == 0)
8689 struct scsi_rw_10 *cdb;
8691 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8692 if (cdb->byte2 & SRW10_FUA)
8693 flags |= CTL_LLF_FUA;
8694 if (cdb->byte2 & SRW10_DPO)
8695 flags |= CTL_LLF_DPO;
8696 lba = scsi_4btoul(cdb->addr);
8697 num_blocks = scsi_2btoul(cdb->length);
8700 case WRITE_VERIFY_10: {
8701 struct scsi_write_verify_10 *cdb;
8703 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8704 flags |= CTL_LLF_FUA;
8705 if (cdb->byte2 & SWV_DPO)
8706 flags |= CTL_LLF_DPO;
8707 lba = scsi_4btoul(cdb->addr);
8708 num_blocks = scsi_2btoul(cdb->length);
8713 struct scsi_rw_12 *cdb;
8715 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8716 if (cdb->byte2 & SRW12_FUA)
8717 flags |= CTL_LLF_FUA;
8718 if (cdb->byte2 & SRW12_DPO)
8719 flags |= CTL_LLF_DPO;
8720 lba = scsi_4btoul(cdb->addr);
8721 num_blocks = scsi_4btoul(cdb->length);
8724 case WRITE_VERIFY_12: {
8725 struct scsi_write_verify_12 *cdb;
8727 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8728 flags |= CTL_LLF_FUA;
8729 if (cdb->byte2 & SWV_DPO)
8730 flags |= CTL_LLF_DPO;
8731 lba = scsi_4btoul(cdb->addr);
8732 num_blocks = scsi_4btoul(cdb->length);
8737 struct scsi_rw_16 *cdb;
8739 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8740 if (cdb->byte2 & SRW12_FUA)
8741 flags |= CTL_LLF_FUA;
8742 if (cdb->byte2 & SRW12_DPO)
8743 flags |= CTL_LLF_DPO;
8744 lba = scsi_8btou64(cdb->addr);
8745 num_blocks = scsi_4btoul(cdb->length);
8748 case WRITE_ATOMIC_16: {
8749 struct scsi_write_atomic_16 *cdb;
8751 if (lun->be_lun->atomicblock == 0) {
8752 ctl_set_invalid_opcode(ctsio);
8753 ctl_done((union ctl_io *)ctsio);
8754 return (CTL_RETVAL_COMPLETE);
8757 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8758 if (cdb->byte2 & SRW12_FUA)
8759 flags |= CTL_LLF_FUA;
8760 if (cdb->byte2 & SRW12_DPO)
8761 flags |= CTL_LLF_DPO;
8762 lba = scsi_8btou64(cdb->addr);
8763 num_blocks = scsi_2btoul(cdb->length);
8764 if (num_blocks > lun->be_lun->atomicblock) {
8765 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8766 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8768 ctl_done((union ctl_io *)ctsio);
8769 return (CTL_RETVAL_COMPLETE);
8773 case WRITE_VERIFY_16: {
8774 struct scsi_write_verify_16 *cdb;
8776 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8777 flags |= CTL_LLF_FUA;
8778 if (cdb->byte2 & SWV_DPO)
8779 flags |= CTL_LLF_DPO;
8780 lba = scsi_8btou64(cdb->addr);
8781 num_blocks = scsi_4btoul(cdb->length);
8786 * We got a command we don't support. This shouldn't
8787 * happen, commands should be filtered out above us.
8789 ctl_set_invalid_opcode(ctsio);
8790 ctl_done((union ctl_io *)ctsio);
8792 return (CTL_RETVAL_COMPLETE);
8793 break; /* NOTREACHED */
8797 * The first check is to make sure we're in bounds, the second
8798 * check is to catch wrap-around problems. If the lba + num blocks
8799 * is less than the lba, then we've wrapped around and the block
8800 * range is invalid anyway.
8802 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8803 || ((lba + num_blocks) < lba)) {
8804 ctl_set_lba_out_of_range(ctsio,
8805 MAX(lba, lun->be_lun->maxlba + 1));
8806 ctl_done((union ctl_io *)ctsio);
8807 return (CTL_RETVAL_COMPLETE);
8811 * According to SBC-3, a transfer length of 0 is not an error.
8812 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8813 * translates to 256 blocks for those commands.
8815 if (num_blocks == 0) {
8816 ctl_set_success(ctsio);
8817 ctl_done((union ctl_io *)ctsio);
8818 return (CTL_RETVAL_COMPLETE);
8821 /* Set FUA and/or DPO if caches are disabled. */
8823 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8824 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8826 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8827 flags |= CTL_LLF_FUA;
8830 lbalen = (struct ctl_lba_len_flags *)
8831 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8833 lbalen->len = num_blocks;
8834 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8836 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8837 ctsio->kern_rel_offset = 0;
8839 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8841 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8846 ctl_cnw_cont(union ctl_io *io)
8848 struct ctl_lun *lun = CTL_LUN(io);
8849 struct ctl_scsiio *ctsio;
8850 struct ctl_lba_len_flags *lbalen;
8853 ctsio = &io->scsiio;
8854 ctsio->io_hdr.status = CTL_STATUS_NONE;
8855 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8856 lbalen = (struct ctl_lba_len_flags *)
8857 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8858 lbalen->flags &= ~CTL_LLF_COMPARE;
8859 lbalen->flags |= CTL_LLF_WRITE;
8861 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8862 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8867 ctl_cnw(struct ctl_scsiio *ctsio)
8869 struct ctl_lun *lun = CTL_LUN(ctsio);
8870 struct ctl_lba_len_flags *lbalen;
8872 uint32_t num_blocks;
8875 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8878 switch (ctsio->cdb[0]) {
8879 case COMPARE_AND_WRITE: {
8880 struct scsi_compare_and_write *cdb;
8882 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8883 if (cdb->byte2 & SRW10_FUA)
8884 flags |= CTL_LLF_FUA;
8885 if (cdb->byte2 & SRW10_DPO)
8886 flags |= CTL_LLF_DPO;
8887 lba = scsi_8btou64(cdb->addr);
8888 num_blocks = cdb->length;
8893 * We got a command we don't support. This shouldn't
8894 * happen, commands should be filtered out above us.
8896 ctl_set_invalid_opcode(ctsio);
8897 ctl_done((union ctl_io *)ctsio);
8899 return (CTL_RETVAL_COMPLETE);
8900 break; /* NOTREACHED */
8904 * The first check is to make sure we're in bounds, the second
8905 * check is to catch wrap-around problems. If the lba + num blocks
8906 * is less than the lba, then we've wrapped around and the block
8907 * range is invalid anyway.
8909 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8910 || ((lba + num_blocks) < lba)) {
8911 ctl_set_lba_out_of_range(ctsio,
8912 MAX(lba, lun->be_lun->maxlba + 1));
8913 ctl_done((union ctl_io *)ctsio);
8914 return (CTL_RETVAL_COMPLETE);
8918 * According to SBC-3, a transfer length of 0 is not an error.
8920 if (num_blocks == 0) {
8921 ctl_set_success(ctsio);
8922 ctl_done((union ctl_io *)ctsio);
8923 return (CTL_RETVAL_COMPLETE);
8926 /* Set FUA if write cache is disabled. */
8927 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8928 flags |= CTL_LLF_FUA;
8930 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8931 ctsio->kern_rel_offset = 0;
8934 * Set the IO_CONT flag, so that if this I/O gets passed to
8935 * ctl_data_submit_done(), it'll get passed back to
8936 * ctl_ctl_cnw_cont() for further processing.
8938 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8939 ctsio->io_cont = ctl_cnw_cont;
8941 lbalen = (struct ctl_lba_len_flags *)
8942 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8944 lbalen->len = num_blocks;
8945 lbalen->flags = CTL_LLF_COMPARE | flags;
8947 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8948 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8953 ctl_verify(struct ctl_scsiio *ctsio)
8955 struct ctl_lun *lun = CTL_LUN(ctsio);
8956 struct ctl_lba_len_flags *lbalen;
8958 uint32_t num_blocks;
8962 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8965 flags = CTL_LLF_FUA;
8966 switch (ctsio->cdb[0]) {
8968 struct scsi_verify_10 *cdb;
8970 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8971 if (cdb->byte2 & SVFY_BYTCHK)
8973 if (cdb->byte2 & SVFY_DPO)
8974 flags |= CTL_LLF_DPO;
8975 lba = scsi_4btoul(cdb->addr);
8976 num_blocks = scsi_2btoul(cdb->length);
8980 struct scsi_verify_12 *cdb;
8982 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8983 if (cdb->byte2 & SVFY_BYTCHK)
8985 if (cdb->byte2 & SVFY_DPO)
8986 flags |= CTL_LLF_DPO;
8987 lba = scsi_4btoul(cdb->addr);
8988 num_blocks = scsi_4btoul(cdb->length);
8992 struct scsi_rw_16 *cdb;
8994 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8995 if (cdb->byte2 & SVFY_BYTCHK)
8997 if (cdb->byte2 & SVFY_DPO)
8998 flags |= CTL_LLF_DPO;
8999 lba = scsi_8btou64(cdb->addr);
9000 num_blocks = scsi_4btoul(cdb->length);
9005 * We got a command we don't support. This shouldn't
9006 * happen, commands should be filtered out above us.
9008 ctl_set_invalid_opcode(ctsio);
9009 ctl_done((union ctl_io *)ctsio);
9010 return (CTL_RETVAL_COMPLETE);
9014 * The first check is to make sure we're in bounds, the second
9015 * check is to catch wrap-around problems. If the lba + num blocks
9016 * is less than the lba, then we've wrapped around and the block
9017 * range is invalid anyway.
9019 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9020 || ((lba + num_blocks) < lba)) {
9021 ctl_set_lba_out_of_range(ctsio,
9022 MAX(lba, lun->be_lun->maxlba + 1));
9023 ctl_done((union ctl_io *)ctsio);
9024 return (CTL_RETVAL_COMPLETE);
9028 * According to SBC-3, a transfer length of 0 is not an error.
9030 if (num_blocks == 0) {
9031 ctl_set_success(ctsio);
9032 ctl_done((union ctl_io *)ctsio);
9033 return (CTL_RETVAL_COMPLETE);
9036 lbalen = (struct ctl_lba_len_flags *)
9037 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9039 lbalen->len = num_blocks;
9041 lbalen->flags = CTL_LLF_COMPARE | flags;
9042 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9044 lbalen->flags = CTL_LLF_VERIFY | flags;
9045 ctsio->kern_total_len = 0;
9047 ctsio->kern_rel_offset = 0;
9049 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9050 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9055 ctl_report_luns(struct ctl_scsiio *ctsio)
9057 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9058 struct ctl_port *port = CTL_PORT(ctsio);
9059 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9060 struct scsi_report_luns *cdb;
9061 struct scsi_report_luns_data *lun_data;
9062 int num_filled, num_luns, num_port_luns, retval;
9063 uint32_t alloc_len, lun_datalen;
9064 uint32_t initidx, targ_lun_id, lun_id;
9066 retval = CTL_RETVAL_COMPLETE;
9067 cdb = (struct scsi_report_luns *)ctsio->cdb;
9069 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9072 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9073 mtx_lock(&softc->ctl_lock);
9074 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9075 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9078 mtx_unlock(&softc->ctl_lock);
9080 switch (cdb->select_report) {
9081 case RPL_REPORT_DEFAULT:
9082 case RPL_REPORT_ALL:
9083 case RPL_REPORT_NONSUBSID:
9085 case RPL_REPORT_WELLKNOWN:
9086 case RPL_REPORT_ADMIN:
9087 case RPL_REPORT_CONGLOM:
9091 ctl_set_invalid_field(ctsio,
9097 ctl_done((union ctl_io *)ctsio);
9099 break; /* NOTREACHED */
9102 alloc_len = scsi_4btoul(cdb->length);
9104 * The initiator has to allocate at least 16 bytes for this request,
9105 * so he can at least get the header and the first LUN. Otherwise
9106 * we reject the request (per SPC-3 rev 14, section 6.21).
9108 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9109 sizeof(struct scsi_report_luns_lundata))) {
9110 ctl_set_invalid_field(ctsio,
9116 ctl_done((union ctl_io *)ctsio);
9120 lun_datalen = sizeof(*lun_data) +
9121 (num_luns * sizeof(struct scsi_report_luns_lundata));
9123 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9124 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9125 ctsio->kern_sg_entries = 0;
9127 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9129 mtx_lock(&softc->ctl_lock);
9130 for (targ_lun_id = 0, num_filled = 0;
9131 targ_lun_id < num_port_luns && num_filled < num_luns;
9133 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9134 if (lun_id == UINT32_MAX)
9136 lun = softc->ctl_luns[lun_id];
9140 be64enc(lun_data->luns[num_filled++].lundata,
9141 ctl_encode_lun(targ_lun_id));
9144 * According to SPC-3, rev 14 section 6.21:
9146 * "The execution of a REPORT LUNS command to any valid and
9147 * installed logical unit shall clear the REPORTED LUNS DATA
9148 * HAS CHANGED unit attention condition for all logical
9149 * units of that target with respect to the requesting
9150 * initiator. A valid and installed logical unit is one
9151 * having a PERIPHERAL QUALIFIER of 000b in the standard
9152 * INQUIRY data (see 6.4.2)."
9154 * If request_lun is NULL, the LUN this report luns command
9155 * was issued to is either disabled or doesn't exist. In that
9156 * case, we shouldn't clear any pending lun change unit
9159 if (request_lun != NULL) {
9160 mtx_lock(&lun->lun_lock);
9161 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9162 mtx_unlock(&lun->lun_lock);
9165 mtx_unlock(&softc->ctl_lock);
9168 * It's quite possible that we've returned fewer LUNs than we allocated
9169 * space for. Trim it.
9171 lun_datalen = sizeof(*lun_data) +
9172 (num_filled * sizeof(struct scsi_report_luns_lundata));
9173 ctsio->kern_rel_offset = 0;
9174 ctsio->kern_sg_entries = 0;
9175 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9176 ctsio->kern_total_len = ctsio->kern_data_len;
9179 * We set this to the actual data length, regardless of how much
9180 * space we actually have to return results. If the user looks at
9181 * this value, he'll know whether or not he allocated enough space
9182 * and reissue the command if necessary. We don't support well
9183 * known logical units, so if the user asks for that, return none.
9185 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9188 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9191 ctl_set_success(ctsio);
9192 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9193 ctsio->be_move_done = ctl_config_move_done;
9194 ctl_datamove((union ctl_io *)ctsio);
9199 ctl_request_sense(struct ctl_scsiio *ctsio)
9201 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9202 struct ctl_lun *lun = CTL_LUN(ctsio);
9203 struct scsi_request_sense *cdb;
9204 struct scsi_sense_data *sense_ptr, *ps;
9207 u_int sense_len = SSD_FULL_SIZE;
9208 scsi_sense_data_type sense_format;
9209 ctl_ua_type ua_type;
9210 uint8_t asc = 0, ascq = 0;
9212 cdb = (struct scsi_request_sense *)ctsio->cdb;
9214 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9217 * Determine which sense format the user wants.
9219 if (cdb->byte2 & SRS_DESC)
9220 sense_format = SSD_TYPE_DESC;
9222 sense_format = SSD_TYPE_FIXED;
9224 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9225 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9226 ctsio->kern_sg_entries = 0;
9227 ctsio->kern_rel_offset = 0;
9230 * struct scsi_sense_data, which is currently set to 256 bytes, is
9231 * larger than the largest allowed value for the length field in the
9232 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9234 ctsio->kern_data_len = cdb->length;
9235 ctsio->kern_total_len = cdb->length;
9238 * If we don't have a LUN, we don't have any pending sense.
9241 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9242 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9243 /* "Logical unit not supported" */
9244 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9245 /*current_error*/ 1,
9246 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9254 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9256 * Check for pending sense, and then for pending unit attentions.
9257 * Pending sense gets returned first, then pending unit attentions.
9259 mtx_lock(&lun->lun_lock);
9260 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9262 ps += initidx % CTL_MAX_INIT_PER_PORT;
9263 if (ps != NULL && ps->error_code != 0) {
9264 scsi_sense_data_type stored_format;
9267 * Check to see which sense format was used for the stored
9270 stored_format = scsi_sense_type(ps);
9273 * If the user requested a different sense format than the
9274 * one we stored, then we need to convert it to the other
9275 * format. If we're going from descriptor to fixed format
9276 * sense data, we may lose things in translation, depending
9277 * on what options were used.
9279 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9280 * for some reason we'll just copy it out as-is.
9282 if ((stored_format == SSD_TYPE_FIXED)
9283 && (sense_format == SSD_TYPE_DESC))
9284 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9285 ps, (struct scsi_sense_data_desc *)sense_ptr);
9286 else if ((stored_format == SSD_TYPE_DESC)
9287 && (sense_format == SSD_TYPE_FIXED))
9288 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9289 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9291 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9296 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9298 if (ua_type != CTL_UA_NONE)
9301 if (have_error == 0) {
9303 * Report informational exception if have one and allowed.
9305 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9307 ascq = lun->ie_ascq;
9309 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9310 /*current_error*/ 1,
9311 /*sense_key*/ SSD_KEY_NO_SENSE,
9316 mtx_unlock(&lun->lun_lock);
9320 * We report the SCSI status as OK, since the status of the command
9321 * itself is OK. We're reporting sense as parameter data.
9323 ctl_set_success(ctsio);
9324 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9325 ctsio->be_move_done = ctl_config_move_done;
9326 ctl_datamove((union ctl_io *)ctsio);
9327 return (CTL_RETVAL_COMPLETE);
9331 ctl_tur(struct ctl_scsiio *ctsio)
9334 CTL_DEBUG_PRINT(("ctl_tur\n"));
9336 ctl_set_success(ctsio);
9337 ctl_done((union ctl_io *)ctsio);
9339 return (CTL_RETVAL_COMPLETE);
9343 * SCSI VPD page 0x00, the Supported VPD Pages page.
9346 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9348 struct ctl_lun *lun = CTL_LUN(ctsio);
9349 struct scsi_vpd_supported_pages *pages;
9353 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9354 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9355 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9356 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9357 ctsio->kern_rel_offset = 0;
9358 ctsio->kern_sg_entries = 0;
9359 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9360 ctsio->kern_total_len = ctsio->kern_data_len;
9363 * The control device is always connected. The disk device, on the
9364 * other hand, may not be online all the time. Need to change this
9365 * to figure out whether the disk device is actually online or not.
9368 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9369 lun->be_lun->lun_type;
9371 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9374 /* Supported VPD pages */
9375 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9377 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9378 /* Device Identification */
9379 pages->page_list[p++] = SVPD_DEVICE_ID;
9380 /* Extended INQUIRY Data */
9381 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9382 /* Mode Page Policy */
9383 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9385 pages->page_list[p++] = SVPD_SCSI_PORTS;
9386 /* Third-party Copy */
9387 pages->page_list[p++] = SVPD_SCSI_TPC;
9388 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9390 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9391 /* Block Device Characteristics */
9392 pages->page_list[p++] = SVPD_BDC;
9393 /* Logical Block Provisioning */
9394 pages->page_list[p++] = SVPD_LBP;
9398 ctl_set_success(ctsio);
9399 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9400 ctsio->be_move_done = ctl_config_move_done;
9401 ctl_datamove((union ctl_io *)ctsio);
9402 return (CTL_RETVAL_COMPLETE);
9406 * SCSI VPD page 0x80, the Unit Serial Number page.
9409 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9411 struct ctl_lun *lun = CTL_LUN(ctsio);
9412 struct scsi_vpd_unit_serial_number *sn_ptr;
9415 data_len = 4 + CTL_SN_LEN;
9416 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9417 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9418 ctsio->kern_rel_offset = 0;
9419 ctsio->kern_sg_entries = 0;
9420 ctsio->kern_data_len = min(data_len, alloc_len);
9421 ctsio->kern_total_len = ctsio->kern_data_len;
9424 * The control device is always connected. The disk device, on the
9425 * other hand, may not be online all the time. Need to change this
9426 * to figure out whether the disk device is actually online or not.
9429 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9430 lun->be_lun->lun_type;
9432 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9434 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9435 sn_ptr->length = CTL_SN_LEN;
9437 * If we don't have a LUN, we just leave the serial number as
9441 strncpy((char *)sn_ptr->serial_num,
9442 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9444 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9446 ctl_set_success(ctsio);
9447 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9448 ctsio->be_move_done = ctl_config_move_done;
9449 ctl_datamove((union ctl_io *)ctsio);
9450 return (CTL_RETVAL_COMPLETE);
9455 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9458 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9460 struct ctl_lun *lun = CTL_LUN(ctsio);
9461 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9464 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9465 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9466 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9467 ctsio->kern_sg_entries = 0;
9468 ctsio->kern_rel_offset = 0;
9469 ctsio->kern_data_len = min(data_len, alloc_len);
9470 ctsio->kern_total_len = ctsio->kern_data_len;
9473 * The control device is always connected. The disk device, on the
9474 * other hand, may not be online all the time.
9477 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9478 lun->be_lun->lun_type;
9480 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9481 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9482 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9484 * We support head of queue, ordered and simple tags.
9486 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9488 * Volatile cache supported.
9490 eid_ptr->flags3 = SVPD_EID_V_SUP;
9493 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9494 * attention for a particular IT nexus on all LUNs once we report
9495 * it to that nexus once. This bit is required as of SPC-4.
9497 eid_ptr->flags4 = SVPD_EID_LUICLR;
9500 * We support revert to defaults (RTD) bit in MODE SELECT.
9502 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9505 * XXX KDM in order to correctly answer this, we would need
9506 * information from the SIM to determine how much sense data it
9507 * can send. So this would really be a path inquiry field, most
9508 * likely. This can be set to a maximum of 252 according to SPC-4,
9509 * but the hardware may or may not be able to support that much.
9510 * 0 just means that the maximum sense data length is not reported.
9512 eid_ptr->max_sense_length = 0;
9514 ctl_set_success(ctsio);
9515 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9516 ctsio->be_move_done = ctl_config_move_done;
9517 ctl_datamove((union ctl_io *)ctsio);
9518 return (CTL_RETVAL_COMPLETE);
9522 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9524 struct ctl_lun *lun = CTL_LUN(ctsio);
9525 struct scsi_vpd_mode_page_policy *mpp_ptr;
9528 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9529 sizeof(struct scsi_vpd_mode_page_policy_descr);
9531 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9532 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9533 ctsio->kern_rel_offset = 0;
9534 ctsio->kern_sg_entries = 0;
9535 ctsio->kern_data_len = min(data_len, alloc_len);
9536 ctsio->kern_total_len = ctsio->kern_data_len;
9539 * The control device is always connected. The disk device, on the
9540 * other hand, may not be online all the time.
9543 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9544 lun->be_lun->lun_type;
9546 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9547 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9548 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9549 mpp_ptr->descr[0].page_code = 0x3f;
9550 mpp_ptr->descr[0].subpage_code = 0xff;
9551 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9553 ctl_set_success(ctsio);
9554 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9555 ctsio->be_move_done = ctl_config_move_done;
9556 ctl_datamove((union ctl_io *)ctsio);
9557 return (CTL_RETVAL_COMPLETE);
9561 * SCSI VPD page 0x83, the Device Identification page.
9564 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9566 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9567 struct ctl_port *port = CTL_PORT(ctsio);
9568 struct ctl_lun *lun = CTL_LUN(ctsio);
9569 struct scsi_vpd_device_id *devid_ptr;
9570 struct scsi_vpd_id_descriptor *desc;
9574 data_len = sizeof(struct scsi_vpd_device_id) +
9575 sizeof(struct scsi_vpd_id_descriptor) +
9576 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9577 sizeof(struct scsi_vpd_id_descriptor) +
9578 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9579 if (lun && lun->lun_devid)
9580 data_len += lun->lun_devid->len;
9581 if (port && port->port_devid)
9582 data_len += port->port_devid->len;
9583 if (port && port->target_devid)
9584 data_len += port->target_devid->len;
9586 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9587 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9588 ctsio->kern_sg_entries = 0;
9589 ctsio->kern_rel_offset = 0;
9590 ctsio->kern_sg_entries = 0;
9591 ctsio->kern_data_len = min(data_len, alloc_len);
9592 ctsio->kern_total_len = ctsio->kern_data_len;
9595 * The control device is always connected. The disk device, on the
9596 * other hand, may not be online all the time.
9599 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9600 lun->be_lun->lun_type;
9602 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9603 devid_ptr->page_code = SVPD_DEVICE_ID;
9604 scsi_ulto2b(data_len - 4, devid_ptr->length);
9606 if (port && port->port_type == CTL_PORT_FC)
9607 proto = SCSI_PROTO_FC << 4;
9608 else if (port && port->port_type == CTL_PORT_SAS)
9609 proto = SCSI_PROTO_SAS << 4;
9610 else if (port && port->port_type == CTL_PORT_ISCSI)
9611 proto = SCSI_PROTO_ISCSI << 4;
9613 proto = SCSI_PROTO_SPI << 4;
9614 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9617 * We're using a LUN association here. i.e., this device ID is a
9618 * per-LUN identifier.
9620 if (lun && lun->lun_devid) {
9621 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9622 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9623 lun->lun_devid->len);
9627 * This is for the WWPN which is a port association.
9629 if (port && port->port_devid) {
9630 memcpy(desc, port->port_devid->data, port->port_devid->len);
9631 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9632 port->port_devid->len);
9636 * This is for the Relative Target Port(type 4h) identifier
9638 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9639 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9640 SVPD_ID_TYPE_RELTARG;
9642 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9643 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9644 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9647 * This is for the Target Port Group(type 5h) identifier
9649 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9650 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9651 SVPD_ID_TYPE_TPORTGRP;
9653 if (softc->is_single ||
9654 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9657 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9658 scsi_ulto2b(g, &desc->identifier[2]);
9659 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9660 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9663 * This is for the Target identifier
9665 if (port && port->target_devid) {
9666 memcpy(desc, port->target_devid->data, port->target_devid->len);
9669 ctl_set_success(ctsio);
9670 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9671 ctsio->be_move_done = ctl_config_move_done;
9672 ctl_datamove((union ctl_io *)ctsio);
9673 return (CTL_RETVAL_COMPLETE);
9677 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9679 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9680 struct ctl_lun *lun = CTL_LUN(ctsio);
9681 struct scsi_vpd_scsi_ports *sp;
9682 struct scsi_vpd_port_designation *pd;
9683 struct scsi_vpd_port_designation_cont *pdc;
9684 struct ctl_port *port;
9685 int data_len, num_target_ports, iid_len, id_len;
9687 num_target_ports = 0;
9690 mtx_lock(&softc->ctl_lock);
9691 STAILQ_FOREACH(port, &softc->port_list, links) {
9692 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9695 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9698 if (port->init_devid)
9699 iid_len += port->init_devid->len;
9700 if (port->port_devid)
9701 id_len += port->port_devid->len;
9703 mtx_unlock(&softc->ctl_lock);
9705 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9706 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9707 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9708 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9709 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9710 ctsio->kern_sg_entries = 0;
9711 ctsio->kern_rel_offset = 0;
9712 ctsio->kern_sg_entries = 0;
9713 ctsio->kern_data_len = min(data_len, alloc_len);
9714 ctsio->kern_total_len = ctsio->kern_data_len;
9717 * The control device is always connected. The disk device, on the
9718 * other hand, may not be online all the time. Need to change this
9719 * to figure out whether the disk device is actually online or not.
9722 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9723 lun->be_lun->lun_type;
9725 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9727 sp->page_code = SVPD_SCSI_PORTS;
9728 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9730 pd = &sp->design[0];
9732 mtx_lock(&softc->ctl_lock);
9733 STAILQ_FOREACH(port, &softc->port_list, links) {
9734 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9737 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9739 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9740 if (port->init_devid) {
9741 iid_len = port->init_devid->len;
9742 memcpy(pd->initiator_transportid,
9743 port->init_devid->data, port->init_devid->len);
9746 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9747 pdc = (struct scsi_vpd_port_designation_cont *)
9748 (&pd->initiator_transportid[iid_len]);
9749 if (port->port_devid) {
9750 id_len = port->port_devid->len;
9751 memcpy(pdc->target_port_descriptors,
9752 port->port_devid->data, port->port_devid->len);
9755 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9756 pd = (struct scsi_vpd_port_designation *)
9757 ((uint8_t *)pdc->target_port_descriptors + id_len);
9759 mtx_unlock(&softc->ctl_lock);
9761 ctl_set_success(ctsio);
9762 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9763 ctsio->be_move_done = ctl_config_move_done;
9764 ctl_datamove((union ctl_io *)ctsio);
9765 return (CTL_RETVAL_COMPLETE);
9769 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9771 struct ctl_lun *lun = CTL_LUN(ctsio);
9772 struct scsi_vpd_block_limits *bl_ptr;
9775 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9776 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9777 ctsio->kern_sg_entries = 0;
9778 ctsio->kern_rel_offset = 0;
9779 ctsio->kern_sg_entries = 0;
9780 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9781 ctsio->kern_total_len = ctsio->kern_data_len;
9784 * The control device is always connected. The disk device, on the
9785 * other hand, may not be online all the time. Need to change this
9786 * to figure out whether the disk device is actually online or not.
9789 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9790 lun->be_lun->lun_type;
9792 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9794 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9795 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9796 bl_ptr->max_cmp_write_len = 0xff;
9797 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9799 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9800 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9802 ctl_get_opt_number(&lun->be_lun->options,
9803 "unmap_max_lba", &ival);
9804 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9806 ctl_get_opt_number(&lun->be_lun->options,
9807 "unmap_max_descr", &ival);
9808 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9809 if (lun->be_lun->ublockexp != 0) {
9810 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9811 bl_ptr->opt_unmap_grain);
9812 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9813 bl_ptr->unmap_grain_align);
9816 scsi_ulto4b(lun->be_lun->atomicblock,
9817 bl_ptr->max_atomic_transfer_length);
9818 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9819 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9820 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9821 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9823 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9824 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9827 ctl_set_success(ctsio);
9828 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9829 ctsio->be_move_done = ctl_config_move_done;
9830 ctl_datamove((union ctl_io *)ctsio);
9831 return (CTL_RETVAL_COMPLETE);
9835 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9837 struct ctl_lun *lun = CTL_LUN(ctsio);
9838 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9842 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9843 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9844 ctsio->kern_sg_entries = 0;
9845 ctsio->kern_rel_offset = 0;
9846 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9847 ctsio->kern_total_len = ctsio->kern_data_len;
9850 * The control device is always connected. The disk device, on the
9851 * other hand, may not be online all the time. Need to change this
9852 * to figure out whether the disk device is actually online or not.
9855 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9856 lun->be_lun->lun_type;
9858 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9859 bdc_ptr->page_code = SVPD_BDC;
9860 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9862 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9863 i = strtol(value, NULL, 0);
9865 i = CTL_DEFAULT_ROTATION_RATE;
9866 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9868 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9869 i = strtol(value, NULL, 0);
9872 bdc_ptr->wab_wac_ff = (i & 0x0f);
9873 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9875 ctl_set_success(ctsio);
9876 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9877 ctsio->be_move_done = ctl_config_move_done;
9878 ctl_datamove((union ctl_io *)ctsio);
9879 return (CTL_RETVAL_COMPLETE);
9883 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9885 struct ctl_lun *lun = CTL_LUN(ctsio);
9886 struct scsi_vpd_logical_block_prov *lbp_ptr;
9889 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9890 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9891 ctsio->kern_sg_entries = 0;
9892 ctsio->kern_rel_offset = 0;
9893 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9894 ctsio->kern_total_len = ctsio->kern_data_len;
9897 * The control device is always connected. The disk device, on the
9898 * other hand, may not be online all the time. Need to change this
9899 * to figure out whether the disk device is actually online or not.
9902 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9903 lun->be_lun->lun_type;
9905 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9907 lbp_ptr->page_code = SVPD_LBP;
9908 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9909 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9910 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9911 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9912 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9913 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
9914 if (value != NULL) {
9915 if (strcmp(value, "resource") == 0)
9916 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9917 else if (strcmp(value, "thin") == 0)
9918 lbp_ptr->prov_type = SVPD_LBP_THIN;
9920 lbp_ptr->prov_type = SVPD_LBP_THIN;
9923 ctl_set_success(ctsio);
9924 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9925 ctsio->be_move_done = ctl_config_move_done;
9926 ctl_datamove((union ctl_io *)ctsio);
9927 return (CTL_RETVAL_COMPLETE);
9931 * INQUIRY with the EVPD bit set.
9934 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9936 struct ctl_lun *lun = CTL_LUN(ctsio);
9937 struct scsi_inquiry *cdb;
9938 int alloc_len, retval;
9940 cdb = (struct scsi_inquiry *)ctsio->cdb;
9941 alloc_len = scsi_2btoul(cdb->length);
9943 switch (cdb->page_code) {
9944 case SVPD_SUPPORTED_PAGES:
9945 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9947 case SVPD_UNIT_SERIAL_NUMBER:
9948 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9950 case SVPD_DEVICE_ID:
9951 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9953 case SVPD_EXTENDED_INQUIRY_DATA:
9954 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9956 case SVPD_MODE_PAGE_POLICY:
9957 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9959 case SVPD_SCSI_PORTS:
9960 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9963 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9965 case SVPD_BLOCK_LIMITS:
9966 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9968 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9971 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9973 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
9976 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9978 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9982 ctl_set_invalid_field(ctsio,
9988 ctl_done((union ctl_io *)ctsio);
9989 retval = CTL_RETVAL_COMPLETE;
9997 * Standard INQUIRY data.
10000 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10002 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10003 struct ctl_port *port = CTL_PORT(ctsio);
10004 struct ctl_lun *lun = CTL_LUN(ctsio);
10005 struct scsi_inquiry_data *inq_ptr;
10006 struct scsi_inquiry *cdb;
10008 uint32_t alloc_len, data_len;
10009 ctl_port_type port_type;
10011 port_type = port->port_type;
10012 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10013 port_type = CTL_PORT_SCSI;
10015 cdb = (struct scsi_inquiry *)ctsio->cdb;
10016 alloc_len = scsi_2btoul(cdb->length);
10019 * We malloc the full inquiry data size here and fill it
10020 * in. If the user only asks for less, we'll give him
10023 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10024 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10025 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10026 ctsio->kern_sg_entries = 0;
10027 ctsio->kern_rel_offset = 0;
10028 ctsio->kern_data_len = min(data_len, alloc_len);
10029 ctsio->kern_total_len = ctsio->kern_data_len;
10032 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10033 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10034 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10035 lun->be_lun->lun_type;
10037 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10038 lun->be_lun->lun_type;
10040 if (lun->flags & CTL_LUN_REMOVABLE)
10041 inq_ptr->dev_qual2 |= SID_RMB;
10043 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10045 /* RMB in byte 2 is 0 */
10046 inq_ptr->version = SCSI_REV_SPC5;
10049 * According to SAM-3, even if a device only supports a single
10050 * level of LUN addressing, it should still set the HISUP bit:
10052 * 4.9.1 Logical unit numbers overview
10054 * All logical unit number formats described in this standard are
10055 * hierarchical in structure even when only a single level in that
10056 * hierarchy is used. The HISUP bit shall be set to one in the
10057 * standard INQUIRY data (see SPC-2) when any logical unit number
10058 * format described in this standard is used. Non-hierarchical
10059 * formats are outside the scope of this standard.
10061 * Therefore we set the HiSup bit here.
10063 * The response format is 2, per SPC-3.
10065 inq_ptr->response_format = SID_HiSup | 2;
10067 inq_ptr->additional_length = data_len -
10068 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10069 CTL_DEBUG_PRINT(("additional_length = %d\n",
10070 inq_ptr->additional_length));
10072 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10073 if (port_type == CTL_PORT_SCSI)
10074 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10075 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10076 inq_ptr->flags = SID_CmdQue;
10077 if (port_type == CTL_PORT_SCSI)
10078 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10081 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10082 * We have 8 bytes for the vendor name, and 16 bytes for the device
10083 * name and 4 bytes for the revision.
10085 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10086 "vendor")) == NULL) {
10087 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10089 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10090 strncpy(inq_ptr->vendor, val,
10091 min(sizeof(inq_ptr->vendor), strlen(val)));
10094 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10095 sizeof(inq_ptr->product));
10096 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10097 switch (lun->be_lun->lun_type) {
10099 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10100 sizeof(inq_ptr->product));
10103 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10104 sizeof(inq_ptr->product));
10107 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10108 sizeof(inq_ptr->product));
10111 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10112 sizeof(inq_ptr->product));
10116 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10117 strncpy(inq_ptr->product, val,
10118 min(sizeof(inq_ptr->product), strlen(val)));
10122 * XXX make this a macro somewhere so it automatically gets
10123 * incremented when we make changes.
10125 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10126 "revision")) == NULL) {
10127 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10129 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10130 strncpy(inq_ptr->revision, val,
10131 min(sizeof(inq_ptr->revision), strlen(val)));
10135 * For parallel SCSI, we support double transition and single
10136 * transition clocking. We also support QAS (Quick Arbitration
10137 * and Selection) and Information Unit transfers on both the
10138 * control and array devices.
10140 if (port_type == CTL_PORT_SCSI)
10141 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10144 /* SAM-6 (no version claimed) */
10145 scsi_ulto2b(0x00C0, inq_ptr->version1);
10146 /* SPC-5 (no version claimed) */
10147 scsi_ulto2b(0x05C0, inq_ptr->version2);
10148 if (port_type == CTL_PORT_FC) {
10149 /* FCP-2 ANSI INCITS.350:2003 */
10150 scsi_ulto2b(0x0917, inq_ptr->version3);
10151 } else if (port_type == CTL_PORT_SCSI) {
10152 /* SPI-4 ANSI INCITS.362:200x */
10153 scsi_ulto2b(0x0B56, inq_ptr->version3);
10154 } else if (port_type == CTL_PORT_ISCSI) {
10155 /* iSCSI (no version claimed) */
10156 scsi_ulto2b(0x0960, inq_ptr->version3);
10157 } else if (port_type == CTL_PORT_SAS) {
10158 /* SAS (no version claimed) */
10159 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10160 } else if (port_type == CTL_PORT_UMASS) {
10161 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10162 scsi_ulto2b(0x1730, inq_ptr->version3);
10166 /* SBC-4 (no version claimed) */
10167 scsi_ulto2b(0x0600, inq_ptr->version4);
10169 switch (lun->be_lun->lun_type) {
10171 /* SBC-4 (no version claimed) */
10172 scsi_ulto2b(0x0600, inq_ptr->version4);
10177 /* MMC-6 (no version claimed) */
10178 scsi_ulto2b(0x04E0, inq_ptr->version4);
10185 ctl_set_success(ctsio);
10186 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10187 ctsio->be_move_done = ctl_config_move_done;
10188 ctl_datamove((union ctl_io *)ctsio);
10189 return (CTL_RETVAL_COMPLETE);
10193 ctl_inquiry(struct ctl_scsiio *ctsio)
10195 struct scsi_inquiry *cdb;
10198 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10200 cdb = (struct scsi_inquiry *)ctsio->cdb;
10201 if (cdb->byte2 & SI_EVPD)
10202 retval = ctl_inquiry_evpd(ctsio);
10203 else if (cdb->page_code == 0)
10204 retval = ctl_inquiry_std(ctsio);
10206 ctl_set_invalid_field(ctsio,
10212 ctl_done((union ctl_io *)ctsio);
10213 return (CTL_RETVAL_COMPLETE);
10220 ctl_get_config(struct ctl_scsiio *ctsio)
10222 struct ctl_lun *lun = CTL_LUN(ctsio);
10223 struct scsi_get_config_header *hdr;
10224 struct scsi_get_config_feature *feature;
10225 struct scsi_get_config *cdb;
10226 uint32_t alloc_len, data_len;
10229 cdb = (struct scsi_get_config *)ctsio->cdb;
10230 rt = (cdb->rt & SGC_RT_MASK);
10231 starting = scsi_2btoul(cdb->starting_feature);
10232 alloc_len = scsi_2btoul(cdb->length);
10234 data_len = sizeof(struct scsi_get_config_header) +
10235 sizeof(struct scsi_get_config_feature) + 8 +
10236 sizeof(struct scsi_get_config_feature) + 8 +
10237 sizeof(struct scsi_get_config_feature) + 4 +
10238 sizeof(struct scsi_get_config_feature) + 4 +
10239 sizeof(struct scsi_get_config_feature) + 8 +
10240 sizeof(struct scsi_get_config_feature) +
10241 sizeof(struct scsi_get_config_feature) + 4 +
10242 sizeof(struct scsi_get_config_feature) + 4 +
10243 sizeof(struct scsi_get_config_feature) + 4 +
10244 sizeof(struct scsi_get_config_feature) + 4 +
10245 sizeof(struct scsi_get_config_feature) + 4 +
10246 sizeof(struct scsi_get_config_feature) + 4;
10247 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10248 ctsio->kern_sg_entries = 0;
10249 ctsio->kern_rel_offset = 0;
10251 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10252 if (lun->flags & CTL_LUN_NO_MEDIA)
10253 scsi_ulto2b(0x0000, hdr->current_profile);
10255 scsi_ulto2b(0x0010, hdr->current_profile);
10256 feature = (struct scsi_get_config_feature *)(hdr + 1);
10258 if (starting > 0x003b)
10260 if (starting > 0x003a)
10262 if (starting > 0x002b)
10264 if (starting > 0x002a)
10266 if (starting > 0x001f)
10268 if (starting > 0x001e)
10270 if (starting > 0x001d)
10272 if (starting > 0x0010)
10274 if (starting > 0x0003)
10276 if (starting > 0x0002)
10278 if (starting > 0x0001)
10280 if (starting > 0x0000)
10284 scsi_ulto2b(0x0000, feature->feature_code);
10285 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10286 feature->add_length = 8;
10287 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10288 feature->feature_data[2] = 0x00;
10289 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10290 feature->feature_data[6] = 0x01;
10291 feature = (struct scsi_get_config_feature *)
10292 &feature->feature_data[feature->add_length];
10295 scsi_ulto2b(0x0001, feature->feature_code);
10296 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10297 feature->add_length = 8;
10298 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10299 feature->feature_data[4] = 0x03;
10300 feature = (struct scsi_get_config_feature *)
10301 &feature->feature_data[feature->add_length];
10304 scsi_ulto2b(0x0002, feature->feature_code);
10305 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10306 feature->add_length = 4;
10307 feature->feature_data[0] = 0x02;
10308 feature = (struct scsi_get_config_feature *)
10309 &feature->feature_data[feature->add_length];
10311 f3: /* Removable Medium */
10312 scsi_ulto2b(0x0003, feature->feature_code);
10313 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10314 feature->add_length = 4;
10315 feature->feature_data[0] = 0x39;
10316 feature = (struct scsi_get_config_feature *)
10317 &feature->feature_data[feature->add_length];
10319 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10322 f10: /* Random Read */
10323 scsi_ulto2b(0x0010, feature->feature_code);
10324 feature->flags = 0x00;
10325 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10326 feature->flags |= SGC_F_CURRENT;
10327 feature->add_length = 8;
10328 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10329 scsi_ulto2b(1, &feature->feature_data[4]);
10330 feature->feature_data[6] = 0x00;
10331 feature = (struct scsi_get_config_feature *)
10332 &feature->feature_data[feature->add_length];
10334 f1d: /* Multi-Read */
10335 scsi_ulto2b(0x001D, feature->feature_code);
10336 feature->flags = 0x00;
10337 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10338 feature->flags |= SGC_F_CURRENT;
10339 feature->add_length = 0;
10340 feature = (struct scsi_get_config_feature *)
10341 &feature->feature_data[feature->add_length];
10344 scsi_ulto2b(0x001E, feature->feature_code);
10345 feature->flags = 0x00;
10346 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10347 feature->flags |= SGC_F_CURRENT;
10348 feature->add_length = 4;
10349 feature->feature_data[0] = 0x00;
10350 feature = (struct scsi_get_config_feature *)
10351 &feature->feature_data[feature->add_length];
10353 f1f: /* DVD Read */
10354 scsi_ulto2b(0x001F, feature->feature_code);
10355 feature->flags = 0x08;
10356 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10357 feature->flags |= SGC_F_CURRENT;
10358 feature->add_length = 4;
10359 feature->feature_data[0] = 0x01;
10360 feature->feature_data[2] = 0x03;
10361 feature = (struct scsi_get_config_feature *)
10362 &feature->feature_data[feature->add_length];
10365 scsi_ulto2b(0x002A, feature->feature_code);
10366 feature->flags = 0x04;
10367 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10368 feature->flags |= SGC_F_CURRENT;
10369 feature->add_length = 4;
10370 feature->feature_data[0] = 0x00;
10371 feature->feature_data[1] = 0x00;
10372 feature = (struct scsi_get_config_feature *)
10373 &feature->feature_data[feature->add_length];
10376 scsi_ulto2b(0x002B, feature->feature_code);
10377 feature->flags = 0x00;
10378 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10379 feature->flags |= SGC_F_CURRENT;
10380 feature->add_length = 4;
10381 feature->feature_data[0] = 0x00;
10382 feature = (struct scsi_get_config_feature *)
10383 &feature->feature_data[feature->add_length];
10385 f3a: /* DVD+RW Dual Layer */
10386 scsi_ulto2b(0x003A, feature->feature_code);
10387 feature->flags = 0x00;
10388 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10389 feature->flags |= SGC_F_CURRENT;
10390 feature->add_length = 4;
10391 feature->feature_data[0] = 0x00;
10392 feature->feature_data[1] = 0x00;
10393 feature = (struct scsi_get_config_feature *)
10394 &feature->feature_data[feature->add_length];
10396 f3b: /* DVD+R Dual Layer */
10397 scsi_ulto2b(0x003B, feature->feature_code);
10398 feature->flags = 0x00;
10399 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10400 feature->flags |= SGC_F_CURRENT;
10401 feature->add_length = 4;
10402 feature->feature_data[0] = 0x00;
10403 feature = (struct scsi_get_config_feature *)
10404 &feature->feature_data[feature->add_length];
10407 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10408 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10409 feature = (struct scsi_get_config_feature *)(hdr + 1);
10410 if (scsi_2btoul(feature->feature_code) == starting)
10411 feature = (struct scsi_get_config_feature *)
10412 &feature->feature_data[feature->add_length];
10413 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10415 scsi_ulto4b(data_len - 4, hdr->data_length);
10416 ctsio->kern_data_len = min(data_len, alloc_len);
10417 ctsio->kern_total_len = ctsio->kern_data_len;
10419 ctl_set_success(ctsio);
10420 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10421 ctsio->be_move_done = ctl_config_move_done;
10422 ctl_datamove((union ctl_io *)ctsio);
10423 return (CTL_RETVAL_COMPLETE);
10427 ctl_get_event_status(struct ctl_scsiio *ctsio)
10429 struct scsi_get_event_status_header *hdr;
10430 struct scsi_get_event_status *cdb;
10431 uint32_t alloc_len, data_len;
10434 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10435 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10436 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10437 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10438 ctl_done((union ctl_io *)ctsio);
10439 return (CTL_RETVAL_COMPLETE);
10441 notif_class = cdb->notif_class;
10442 alloc_len = scsi_2btoul(cdb->length);
10444 data_len = sizeof(struct scsi_get_event_status_header);
10445 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10446 ctsio->kern_sg_entries = 0;
10447 ctsio->kern_rel_offset = 0;
10448 ctsio->kern_data_len = min(data_len, alloc_len);
10449 ctsio->kern_total_len = ctsio->kern_data_len;
10451 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10452 scsi_ulto2b(0, hdr->descr_length);
10453 hdr->nea_class = SGESN_NEA;
10454 hdr->supported_class = 0;
10456 ctl_set_success(ctsio);
10457 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10458 ctsio->be_move_done = ctl_config_move_done;
10459 ctl_datamove((union ctl_io *)ctsio);
10460 return (CTL_RETVAL_COMPLETE);
10464 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10466 struct scsi_mechanism_status_header *hdr;
10467 struct scsi_mechanism_status *cdb;
10468 uint32_t alloc_len, data_len;
10470 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10471 alloc_len = scsi_2btoul(cdb->length);
10473 data_len = sizeof(struct scsi_mechanism_status_header);
10474 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10475 ctsio->kern_sg_entries = 0;
10476 ctsio->kern_rel_offset = 0;
10477 ctsio->kern_data_len = min(data_len, alloc_len);
10478 ctsio->kern_total_len = ctsio->kern_data_len;
10480 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10481 hdr->state1 = 0x00;
10482 hdr->state2 = 0xe0;
10483 scsi_ulto3b(0, hdr->lba);
10484 hdr->slots_num = 0;
10485 scsi_ulto2b(0, hdr->slots_length);
10487 ctl_set_success(ctsio);
10488 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10489 ctsio->be_move_done = ctl_config_move_done;
10490 ctl_datamove((union ctl_io *)ctsio);
10491 return (CTL_RETVAL_COMPLETE);
10495 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10500 buf[1] = bin2bcd((lba / 75) / 60);
10501 buf[2] = bin2bcd((lba / 75) % 60);
10502 buf[3] = bin2bcd(lba % 75);
10506 ctl_read_toc(struct ctl_scsiio *ctsio)
10508 struct ctl_lun *lun = CTL_LUN(ctsio);
10509 struct scsi_read_toc_hdr *hdr;
10510 struct scsi_read_toc_type01_descr *descr;
10511 struct scsi_read_toc *cdb;
10512 uint32_t alloc_len, data_len;
10515 cdb = (struct scsi_read_toc *)ctsio->cdb;
10516 msf = (cdb->byte2 & CD_MSF) != 0;
10517 format = cdb->format;
10518 alloc_len = scsi_2btoul(cdb->data_len);
10520 data_len = sizeof(struct scsi_read_toc_hdr);
10522 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10524 data_len += sizeof(struct scsi_read_toc_type01_descr);
10525 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10526 ctsio->kern_sg_entries = 0;
10527 ctsio->kern_rel_offset = 0;
10528 ctsio->kern_data_len = min(data_len, alloc_len);
10529 ctsio->kern_total_len = ctsio->kern_data_len;
10531 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10533 scsi_ulto2b(0x12, hdr->data_length);
10536 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10537 descr->addr_ctl = 0x14;
10538 descr->track_number = 1;
10540 ctl_ultomsf(0, descr->track_start);
10542 scsi_ulto4b(0, descr->track_start);
10544 descr->addr_ctl = 0x14;
10545 descr->track_number = 0xaa;
10547 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10549 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10551 scsi_ulto2b(0x0a, hdr->data_length);
10554 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10555 descr->addr_ctl = 0x14;
10556 descr->track_number = 1;
10558 ctl_ultomsf(0, descr->track_start);
10560 scsi_ulto4b(0, descr->track_start);
10563 ctl_set_success(ctsio);
10564 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10565 ctsio->be_move_done = ctl_config_move_done;
10566 ctl_datamove((union ctl_io *)ctsio);
10567 return (CTL_RETVAL_COMPLETE);
10571 * For known CDB types, parse the LBA and length.
10574 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10576 if (io->io_hdr.io_type != CTL_IO_SCSI)
10579 switch (io->scsiio.cdb[0]) {
10580 case COMPARE_AND_WRITE: {
10581 struct scsi_compare_and_write *cdb;
10583 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10585 *lba = scsi_8btou64(cdb->addr);
10586 *len = cdb->length;
10591 struct scsi_rw_6 *cdb;
10593 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10595 *lba = scsi_3btoul(cdb->addr);
10596 /* only 5 bits are valid in the most significant address byte */
10598 *len = cdb->length;
10603 struct scsi_rw_10 *cdb;
10605 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10607 *lba = scsi_4btoul(cdb->addr);
10608 *len = scsi_2btoul(cdb->length);
10611 case WRITE_VERIFY_10: {
10612 struct scsi_write_verify_10 *cdb;
10614 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10616 *lba = scsi_4btoul(cdb->addr);
10617 *len = scsi_2btoul(cdb->length);
10622 struct scsi_rw_12 *cdb;
10624 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10626 *lba = scsi_4btoul(cdb->addr);
10627 *len = scsi_4btoul(cdb->length);
10630 case WRITE_VERIFY_12: {
10631 struct scsi_write_verify_12 *cdb;
10633 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10635 *lba = scsi_4btoul(cdb->addr);
10636 *len = scsi_4btoul(cdb->length);
10641 struct scsi_rw_16 *cdb;
10643 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10645 *lba = scsi_8btou64(cdb->addr);
10646 *len = scsi_4btoul(cdb->length);
10649 case WRITE_ATOMIC_16: {
10650 struct scsi_write_atomic_16 *cdb;
10652 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10654 *lba = scsi_8btou64(cdb->addr);
10655 *len = scsi_2btoul(cdb->length);
10658 case WRITE_VERIFY_16: {
10659 struct scsi_write_verify_16 *cdb;
10661 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10663 *lba = scsi_8btou64(cdb->addr);
10664 *len = scsi_4btoul(cdb->length);
10667 case WRITE_SAME_10: {
10668 struct scsi_write_same_10 *cdb;
10670 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10672 *lba = scsi_4btoul(cdb->addr);
10673 *len = scsi_2btoul(cdb->length);
10676 case WRITE_SAME_16: {
10677 struct scsi_write_same_16 *cdb;
10679 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10681 *lba = scsi_8btou64(cdb->addr);
10682 *len = scsi_4btoul(cdb->length);
10686 struct scsi_verify_10 *cdb;
10688 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10690 *lba = scsi_4btoul(cdb->addr);
10691 *len = scsi_2btoul(cdb->length);
10695 struct scsi_verify_12 *cdb;
10697 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10699 *lba = scsi_4btoul(cdb->addr);
10700 *len = scsi_4btoul(cdb->length);
10704 struct scsi_verify_16 *cdb;
10706 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10708 *lba = scsi_8btou64(cdb->addr);
10709 *len = scsi_4btoul(cdb->length);
10717 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10718 struct scsi_get_lba_status *cdb;
10720 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10721 *lba = scsi_8btou64(cdb->addr);
10727 break; /* NOTREACHED */
10734 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10737 uint64_t endlba1, endlba2;
10739 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10740 endlba2 = lba2 + len2 - 1;
10742 if ((endlba1 < lba2) || (endlba2 < lba1))
10743 return (CTL_ACTION_PASS);
10745 return (CTL_ACTION_BLOCK);
10749 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10751 struct ctl_ptr_len_flags *ptrlen;
10752 struct scsi_unmap_desc *buf, *end, *range;
10756 /* If not UNMAP -- go other way. */
10757 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10758 io->scsiio.cdb[0] != UNMAP)
10759 return (CTL_ACTION_ERROR);
10761 /* If UNMAP without data -- block and wait for data. */
10762 ptrlen = (struct ctl_ptr_len_flags *)
10763 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10764 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10765 ptrlen->ptr == NULL)
10766 return (CTL_ACTION_BLOCK);
10768 /* UNMAP with data -- check for collision. */
10769 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10770 end = buf + ptrlen->len / sizeof(*buf);
10771 for (range = buf; range < end; range++) {
10772 lba = scsi_8btou64(range->lba);
10773 len = scsi_4btoul(range->length);
10774 if ((lba < lba2 + len2) && (lba + len > lba2))
10775 return (CTL_ACTION_BLOCK);
10777 return (CTL_ACTION_PASS);
10781 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10783 uint64_t lba1, lba2;
10784 uint64_t len1, len2;
10787 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10788 return (CTL_ACTION_ERROR);
10790 retval = ctl_extent_check_unmap(io1, lba2, len2);
10791 if (retval != CTL_ACTION_ERROR)
10794 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10795 return (CTL_ACTION_ERROR);
10797 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10799 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10803 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10805 uint64_t lba1, lba2;
10806 uint64_t len1, len2;
10808 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10809 return (CTL_ACTION_PASS);
10810 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10811 return (CTL_ACTION_ERROR);
10812 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10813 return (CTL_ACTION_ERROR);
10815 if (lba1 + len1 == lba2)
10816 return (CTL_ACTION_BLOCK);
10817 return (CTL_ACTION_PASS);
10821 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10822 union ctl_io *ooa_io)
10824 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10825 const ctl_serialize_action *serialize_row;
10828 * The initiator attempted multiple untagged commands at the same
10829 * time. Can't do that.
10831 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10832 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10833 && ((pending_io->io_hdr.nexus.targ_port ==
10834 ooa_io->io_hdr.nexus.targ_port)
10835 && (pending_io->io_hdr.nexus.initid ==
10836 ooa_io->io_hdr.nexus.initid))
10837 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10838 CTL_FLAG_STATUS_SENT)) == 0))
10839 return (CTL_ACTION_OVERLAP);
10842 * The initiator attempted to send multiple tagged commands with
10843 * the same ID. (It's fine if different initiators have the same
10846 * Even if all of those conditions are true, we don't kill the I/O
10847 * if the command ahead of us has been aborted. We won't end up
10848 * sending it to the FETD, and it's perfectly legal to resend a
10849 * command with the same tag number as long as the previous
10850 * instance of this tag number has been aborted somehow.
10852 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10853 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10854 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10855 && ((pending_io->io_hdr.nexus.targ_port ==
10856 ooa_io->io_hdr.nexus.targ_port)
10857 && (pending_io->io_hdr.nexus.initid ==
10858 ooa_io->io_hdr.nexus.initid))
10859 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10860 CTL_FLAG_STATUS_SENT)) == 0))
10861 return (CTL_ACTION_OVERLAP_TAG);
10864 * If we get a head of queue tag, SAM-3 says that we should
10865 * immediately execute it.
10867 * What happens if this command would normally block for some other
10868 * reason? e.g. a request sense with a head of queue tag
10869 * immediately after a write. Normally that would block, but this
10870 * will result in its getting executed immediately...
10872 * We currently return "pass" instead of "skip", so we'll end up
10873 * going through the rest of the queue to check for overlapped tags.
10875 * XXX KDM check for other types of blockage first??
10877 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10878 return (CTL_ACTION_PASS);
10881 * Ordered tags have to block until all items ahead of them
10882 * have completed. If we get called with an ordered tag, we always
10883 * block, if something else is ahead of us in the queue.
10885 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10886 return (CTL_ACTION_BLOCK);
10889 * Simple tags get blocked until all head of queue and ordered tags
10890 * ahead of them have completed. I'm lumping untagged commands in
10891 * with simple tags here. XXX KDM is that the right thing to do?
10893 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10894 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10895 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10896 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10897 return (CTL_ACTION_BLOCK);
10899 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10900 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
10901 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
10902 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
10903 pending_io->scsiio.cdb[1], pending_io));
10904 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10905 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
10906 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
10907 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
10908 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
10909 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
10910 ooa_io->scsiio.cdb[1], ooa_io));
10912 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10914 switch (serialize_row[pending_entry->seridx]) {
10915 case CTL_SER_BLOCK:
10916 return (CTL_ACTION_BLOCK);
10917 case CTL_SER_EXTENT:
10918 return (ctl_extent_check(ooa_io, pending_io,
10919 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10920 case CTL_SER_EXTENTOPT:
10921 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10922 SCP_QUEUE_ALG_UNRESTRICTED)
10923 return (ctl_extent_check(ooa_io, pending_io,
10925 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10926 return (CTL_ACTION_PASS);
10927 case CTL_SER_EXTENTSEQ:
10928 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10929 return (ctl_extent_check_seq(ooa_io, pending_io));
10930 return (CTL_ACTION_PASS);
10932 return (CTL_ACTION_PASS);
10933 case CTL_SER_BLOCKOPT:
10934 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10935 SCP_QUEUE_ALG_UNRESTRICTED)
10936 return (CTL_ACTION_BLOCK);
10937 return (CTL_ACTION_PASS);
10939 return (CTL_ACTION_SKIP);
10941 panic("%s: Invalid serialization value %d for %d => %d",
10942 __func__, serialize_row[pending_entry->seridx],
10943 pending_entry->seridx, ooa_entry->seridx);
10946 return (CTL_ACTION_ERROR);
10950 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10952 * - pending_io is generally either incoming, or on the blocked queue
10953 * - starting I/O is the I/O we want to start the check with.
10956 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10957 union ctl_io *starting_io)
10959 union ctl_io *ooa_io;
10962 mtx_assert(&lun->lun_lock, MA_OWNED);
10965 * Run back along the OOA queue, starting with the current
10966 * blocked I/O and going through every I/O before it on the
10967 * queue. If starting_io is NULL, we'll just end up returning
10970 for (ooa_io = starting_io; ooa_io != NULL;
10971 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10975 * This routine just checks to see whether
10976 * cur_blocked is blocked by ooa_io, which is ahead
10977 * of it in the queue. It doesn't queue/dequeue
10980 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10982 case CTL_ACTION_BLOCK:
10983 case CTL_ACTION_OVERLAP:
10984 case CTL_ACTION_OVERLAP_TAG:
10985 case CTL_ACTION_SKIP:
10986 case CTL_ACTION_ERROR:
10988 break; /* NOTREACHED */
10989 case CTL_ACTION_PASS:
10992 panic("%s: Invalid action %d\n", __func__, action);
10996 return (CTL_ACTION_PASS);
11001 * - An I/O has just completed, and has been removed from the per-LUN OOA
11002 * queue, so some items on the blocked queue may now be unblocked.
11005 ctl_check_blocked(struct ctl_lun *lun)
11007 struct ctl_softc *softc = lun->ctl_softc;
11008 union ctl_io *cur_blocked, *next_blocked;
11010 mtx_assert(&lun->lun_lock, MA_OWNED);
11013 * Run forward from the head of the blocked queue, checking each
11014 * entry against the I/Os prior to it on the OOA queue to see if
11015 * there is still any blockage.
11017 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11018 * with our removing a variable on it while it is traversing the
11021 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11022 cur_blocked != NULL; cur_blocked = next_blocked) {
11023 union ctl_io *prev_ooa;
11026 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11029 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11030 ctl_ooaq, ooa_links);
11033 * If cur_blocked happens to be the first item in the OOA
11034 * queue now, prev_ooa will be NULL, and the action
11035 * returned will just be CTL_ACTION_PASS.
11037 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11040 case CTL_ACTION_BLOCK:
11041 /* Nothing to do here, still blocked */
11043 case CTL_ACTION_OVERLAP:
11044 case CTL_ACTION_OVERLAP_TAG:
11046 * This shouldn't happen! In theory we've already
11047 * checked this command for overlap...
11050 case CTL_ACTION_PASS:
11051 case CTL_ACTION_SKIP: {
11052 const struct ctl_cmd_entry *entry;
11055 * The skip case shouldn't happen, this transaction
11056 * should have never made it onto the blocked queue.
11059 * This I/O is no longer blocked, we can remove it
11060 * from the blocked queue. Since this is a TAILQ
11061 * (doubly linked list), we can do O(1) removals
11062 * from any place on the list.
11064 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11066 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11068 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11069 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11071 * Need to send IO back to original side to
11074 union ctl_ha_msg msg_info;
11076 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11077 msg_info.hdr.original_sc =
11078 cur_blocked->io_hdr.original_sc;
11079 msg_info.hdr.serializing_sc = cur_blocked;
11080 msg_info.hdr.msg_type = CTL_MSG_R2R;
11081 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11082 sizeof(msg_info.hdr), M_NOWAIT);
11085 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11088 * Check this I/O for LUN state changes that may
11089 * have happened while this command was blocked.
11090 * The LUN state may have been changed by a command
11091 * ahead of us in the queue, so we need to re-check
11092 * for any states that can be caused by SCSI
11095 if (ctl_scsiio_lun_check(lun, entry,
11096 &cur_blocked->scsiio) == 0) {
11097 cur_blocked->io_hdr.flags |=
11098 CTL_FLAG_IS_WAS_ON_RTR;
11099 ctl_enqueue_rtr(cur_blocked);
11101 ctl_done(cur_blocked);
11106 * This probably shouldn't happen -- we shouldn't
11107 * get CTL_ACTION_ERROR, or anything else.
11113 return (CTL_RETVAL_COMPLETE);
11117 * This routine (with one exception) checks LUN flags that can be set by
11118 * commands ahead of us in the OOA queue. These flags have to be checked
11119 * when a command initially comes in, and when we pull a command off the
11120 * blocked queue and are preparing to execute it. The reason we have to
11121 * check these flags for commands on the blocked queue is that the LUN
11122 * state may have been changed by a command ahead of us while we're on the
11125 * Ordering is somewhat important with these checks, so please pay
11126 * careful attention to the placement of any new checks.
11129 ctl_scsiio_lun_check(struct ctl_lun *lun,
11130 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11132 struct ctl_softc *softc = lun->ctl_softc;
11138 mtx_assert(&lun->lun_lock, MA_OWNED);
11141 * If this shelf is a secondary shelf controller, we may have to
11142 * reject some commands disallowed by HA mode and link state.
11144 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11145 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11146 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11147 ctl_set_lun_unavail(ctsio);
11151 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11152 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11153 ctl_set_lun_transit(ctsio);
11157 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11158 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11159 ctl_set_lun_standby(ctsio);
11164 /* The rest of checks are only done on executing side */
11165 if (softc->ha_mode == CTL_HA_MODE_XFER)
11169 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11171 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11172 ctl_set_hw_write_protected(ctsio);
11176 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11177 ctl_set_sense(ctsio, /*current_error*/ 1,
11178 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11179 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11186 * Check for a reservation conflict. If this command isn't allowed
11187 * even on reserved LUNs, and if this initiator isn't the one who
11188 * reserved us, reject the command with a reservation conflict.
11190 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11191 if ((lun->flags & CTL_LUN_RESERVED)
11192 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11193 if (lun->res_idx != residx) {
11194 ctl_set_reservation_conflict(ctsio);
11200 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11201 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11202 /* No reservation or command is allowed. */;
11203 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11204 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11205 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11206 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11207 /* The command is allowed for Write Exclusive resv. */;
11210 * if we aren't registered or it's a res holder type
11211 * reservation and this isn't the res holder then set a
11214 if (ctl_get_prkey(lun, residx) == 0 ||
11215 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11216 ctl_set_reservation_conflict(ctsio);
11222 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11223 if (lun->flags & CTL_LUN_EJECTED)
11224 ctl_set_lun_ejected(ctsio);
11225 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11226 if (lun->flags & CTL_LUN_REMOVABLE)
11227 ctl_set_lun_no_media(ctsio);
11229 ctl_set_lun_int_reqd(ctsio);
11230 } else if (lun->flags & CTL_LUN_STOPPED)
11231 ctl_set_lun_stopped(ctsio);
11243 ctl_failover_io(union ctl_io *io, int have_lock)
11245 ctl_set_busy(&io->scsiio);
11250 ctl_failover_lun(union ctl_io *rio)
11252 struct ctl_softc *softc = CTL_SOFTC(rio);
11253 struct ctl_lun *lun;
11254 struct ctl_io_hdr *io, *next_io;
11257 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11258 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11260 /* Find and lock the LUN. */
11261 mtx_lock(&softc->ctl_lock);
11262 if (targ_lun > ctl_max_luns ||
11263 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11264 mtx_unlock(&softc->ctl_lock);
11267 mtx_lock(&lun->lun_lock);
11268 mtx_unlock(&softc->ctl_lock);
11269 if (lun->flags & CTL_LUN_DISABLED) {
11270 mtx_unlock(&lun->lun_lock);
11274 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11275 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11276 /* We are master */
11277 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11278 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11279 io->flags |= CTL_FLAG_ABORT;
11280 io->flags |= CTL_FLAG_FAILOVER;
11281 } else { /* This can be only due to DATAMOVE */
11282 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11283 io->flags &= ~CTL_FLAG_DMA_INPROG;
11284 io->flags |= CTL_FLAG_IO_ACTIVE;
11285 io->port_status = 31340;
11286 ctl_enqueue_isc((union ctl_io *)io);
11290 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11291 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11292 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11293 io->flags |= CTL_FLAG_FAILOVER;
11295 ctl_set_busy(&((union ctl_io *)io)->
11297 ctl_done((union ctl_io *)io);
11301 } else { /* SERIALIZE modes */
11302 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11304 /* We are master */
11305 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11306 TAILQ_REMOVE(&lun->blocked_queue, io,
11308 io->flags &= ~CTL_FLAG_BLOCKED;
11309 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11310 ctl_free_io((union ctl_io *)io);
11313 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11314 /* We are master */
11315 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11316 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11317 ctl_free_io((union ctl_io *)io);
11320 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11321 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11322 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11323 ctl_set_busy(&((union ctl_io *)io)->
11325 ctl_done((union ctl_io *)io);
11329 ctl_check_blocked(lun);
11331 mtx_unlock(&lun->lun_lock);
11335 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11337 struct ctl_lun *lun;
11338 const struct ctl_cmd_entry *entry;
11339 uint32_t initidx, targ_lun;
11343 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11344 if (targ_lun < ctl_max_luns)
11345 lun = softc->ctl_luns[targ_lun];
11348 * If the LUN is invalid, pretend that it doesn't exist.
11349 * It will go away as soon as all pending I/O has been
11352 mtx_lock(&lun->lun_lock);
11353 if (lun->flags & CTL_LUN_DISABLED) {
11354 mtx_unlock(&lun->lun_lock);
11358 CTL_LUN(ctsio) = lun;
11360 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11363 * Every I/O goes into the OOA queue for a particular LUN,
11364 * and stays there until completion.
11367 if (TAILQ_EMPTY(&lun->ooa_queue))
11368 lun->idle_time += getsbinuptime() - lun->last_busy;
11370 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11373 /* Get command entry and return error if it is unsuppotyed. */
11374 entry = ctl_validate_command(ctsio);
11375 if (entry == NULL) {
11377 mtx_unlock(&lun->lun_lock);
11381 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11382 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11385 * Check to see whether we can send this command to LUNs that don't
11386 * exist. This should pretty much only be the case for inquiry
11387 * and request sense. Further checks, below, really require having
11388 * a LUN, so we can't really check the command anymore. Just put
11389 * it on the rtr queue.
11392 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11393 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11394 ctl_enqueue_rtr((union ctl_io *)ctsio);
11398 ctl_set_unsupported_lun(ctsio);
11399 ctl_done((union ctl_io *)ctsio);
11400 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11404 * Make sure we support this particular command on this LUN.
11405 * e.g., we don't support writes to the control LUN.
11407 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11408 mtx_unlock(&lun->lun_lock);
11409 ctl_set_invalid_opcode(ctsio);
11410 ctl_done((union ctl_io *)ctsio);
11415 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11418 * If we've got a request sense, it'll clear the contingent
11419 * allegiance condition. Otherwise, if we have a CA condition for
11420 * this initiator, clear it, because it sent down a command other
11421 * than request sense.
11423 if (ctsio->cdb[0] != REQUEST_SENSE) {
11424 struct scsi_sense_data *ps;
11426 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11428 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11432 * If the command has this flag set, it handles its own unit
11433 * attention reporting, we shouldn't do anything. Otherwise we
11434 * check for any pending unit attentions, and send them back to the
11435 * initiator. We only do this when a command initially comes in,
11436 * not when we pull it off the blocked queue.
11438 * According to SAM-3, section 5.3.2, the order that things get
11439 * presented back to the host is basically unit attentions caused
11440 * by some sort of reset event, busy status, reservation conflicts
11441 * or task set full, and finally any other status.
11443 * One issue here is that some of the unit attentions we report
11444 * don't fall into the "reset" category (e.g. "reported luns data
11445 * has changed"). So reporting it here, before the reservation
11446 * check, may be technically wrong. I guess the only thing to do
11447 * would be to check for and report the reset events here, and then
11448 * check for the other unit attention types after we check for a
11449 * reservation conflict.
11451 * XXX KDM need to fix this
11453 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11454 ctl_ua_type ua_type;
11455 u_int sense_len = 0;
11457 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11458 &sense_len, SSD_TYPE_NONE);
11459 if (ua_type != CTL_UA_NONE) {
11460 mtx_unlock(&lun->lun_lock);
11461 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11462 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11463 ctsio->sense_len = sense_len;
11464 ctl_done((union ctl_io *)ctsio);
11470 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11471 mtx_unlock(&lun->lun_lock);
11472 ctl_done((union ctl_io *)ctsio);
11477 * XXX CHD this is where we want to send IO to other side if
11478 * this LUN is secondary on this SC. We will need to make a copy
11479 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11480 * the copy we send as FROM_OTHER.
11481 * We also need to stuff the address of the original IO so we can
11482 * find it easily. Something similar will need be done on the other
11483 * side so when we are done we can find the copy.
11485 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11486 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11487 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11488 union ctl_ha_msg msg_info;
11491 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11492 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11493 mtx_unlock(&lun->lun_lock);
11495 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11496 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11497 msg_info.hdr.serializing_sc = NULL;
11498 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11499 msg_info.scsi.tag_num = ctsio->tag_num;
11500 msg_info.scsi.tag_type = ctsio->tag_type;
11501 msg_info.scsi.cdb_len = ctsio->cdb_len;
11502 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11504 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11505 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11506 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11507 ctl_set_busy(ctsio);
11508 ctl_done((union ctl_io *)ctsio);
11514 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11515 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11516 ctl_ooaq, ooa_links))) {
11517 case CTL_ACTION_BLOCK:
11518 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11519 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11521 mtx_unlock(&lun->lun_lock);
11523 case CTL_ACTION_PASS:
11524 case CTL_ACTION_SKIP:
11525 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11526 mtx_unlock(&lun->lun_lock);
11527 ctl_enqueue_rtr((union ctl_io *)ctsio);
11529 case CTL_ACTION_OVERLAP:
11530 mtx_unlock(&lun->lun_lock);
11531 ctl_set_overlapped_cmd(ctsio);
11532 ctl_done((union ctl_io *)ctsio);
11534 case CTL_ACTION_OVERLAP_TAG:
11535 mtx_unlock(&lun->lun_lock);
11536 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11537 ctl_done((union ctl_io *)ctsio);
11539 case CTL_ACTION_ERROR:
11541 mtx_unlock(&lun->lun_lock);
11542 ctl_set_internal_failure(ctsio,
11544 /*retry_count*/ 0);
11545 ctl_done((union ctl_io *)ctsio);
11551 const struct ctl_cmd_entry *
11552 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11554 const struct ctl_cmd_entry *entry;
11555 int service_action;
11557 entry = &ctl_cmd_table[ctsio->cdb[0]];
11559 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11560 if (entry->flags & CTL_CMD_FLAG_SA5) {
11561 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11562 entry = &((const struct ctl_cmd_entry *)
11563 entry->execute)[service_action];
11568 const struct ctl_cmd_entry *
11569 ctl_validate_command(struct ctl_scsiio *ctsio)
11571 const struct ctl_cmd_entry *entry;
11575 entry = ctl_get_cmd_entry(ctsio, &sa);
11576 if (entry->execute == NULL) {
11578 ctl_set_invalid_field(ctsio,
11585 ctl_set_invalid_opcode(ctsio);
11586 ctl_done((union ctl_io *)ctsio);
11589 KASSERT(entry->length > 0,
11590 ("Not defined length for command 0x%02x/0x%02x",
11591 ctsio->cdb[0], ctsio->cdb[1]));
11592 for (i = 1; i < entry->length; i++) {
11593 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11596 ctl_set_invalid_field(ctsio,
11601 /*bit*/ fls(diff) - 1);
11602 ctl_done((union ctl_io *)ctsio);
11609 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11612 switch (lun_type) {
11614 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11618 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11622 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11632 ctl_scsiio(struct ctl_scsiio *ctsio)
11635 const struct ctl_cmd_entry *entry;
11637 retval = CTL_RETVAL_COMPLETE;
11639 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11641 entry = ctl_get_cmd_entry(ctsio, NULL);
11644 * If this I/O has been aborted, just send it straight to
11645 * ctl_done() without executing it.
11647 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11648 ctl_done((union ctl_io *)ctsio);
11653 * All the checks should have been handled by ctl_scsiio_precheck().
11654 * We should be clear now to just execute the I/O.
11656 retval = entry->execute(ctsio);
11663 ctl_target_reset(union ctl_io *io)
11665 struct ctl_softc *softc = CTL_SOFTC(io);
11666 struct ctl_port *port = CTL_PORT(io);
11667 struct ctl_lun *lun;
11669 ctl_ua_type ua_type;
11671 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11672 union ctl_ha_msg msg_info;
11674 msg_info.hdr.nexus = io->io_hdr.nexus;
11675 msg_info.task.task_action = io->taskio.task_action;
11676 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11677 msg_info.hdr.original_sc = NULL;
11678 msg_info.hdr.serializing_sc = NULL;
11679 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11680 sizeof(msg_info.task), M_WAITOK);
11683 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11684 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11685 ua_type = CTL_UA_TARG_RESET;
11687 ua_type = CTL_UA_BUS_RESET;
11688 mtx_lock(&softc->ctl_lock);
11689 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11690 if (port != NULL &&
11691 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11693 ctl_do_lun_reset(lun, initidx, ua_type);
11695 mtx_unlock(&softc->ctl_lock);
11696 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11701 * The LUN should always be set. The I/O is optional, and is used to
11702 * distinguish between I/Os sent by this initiator, and by other
11703 * initiators. We set unit attention for initiators other than this one.
11704 * SAM-3 is vague on this point. It does say that a unit attention should
11705 * be established for other initiators when a LUN is reset (see section
11706 * 5.7.3), but it doesn't specifically say that the unit attention should
11707 * be established for this particular initiator when a LUN is reset. Here
11708 * is the relevant text, from SAM-3 rev 8:
11710 * 5.7.2 When a SCSI initiator port aborts its own tasks
11712 * When a SCSI initiator port causes its own task(s) to be aborted, no
11713 * notification that the task(s) have been aborted shall be returned to
11714 * the SCSI initiator port other than the completion response for the
11715 * command or task management function action that caused the task(s) to
11716 * be aborted and notification(s) associated with related effects of the
11717 * action (e.g., a reset unit attention condition).
11719 * XXX KDM for now, we're setting unit attention for all initiators.
11722 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11727 mtx_lock(&lun->lun_lock);
11729 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11730 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11731 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11734 for (i = 0; i < ctl_max_ports; i++) {
11735 free(lun->pending_sense[i], M_CTL);
11736 lun->pending_sense[i] = NULL;
11738 /* Clear reservation. */
11739 lun->flags &= ~CTL_LUN_RESERVED;
11740 /* Clear prevent media removal. */
11741 if (lun->prevent) {
11742 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11743 ctl_clear_mask(lun->prevent, i);
11744 lun->prevent_count = 0;
11746 /* Clear TPC status */
11747 ctl_tpc_lun_clear(lun, -1);
11748 /* Establish UA. */
11750 ctl_est_ua_all(lun, initidx, ua_type);
11752 ctl_est_ua_all(lun, -1, ua_type);
11754 mtx_unlock(&lun->lun_lock);
11758 ctl_lun_reset(union ctl_io *io)
11760 struct ctl_softc *softc = CTL_SOFTC(io);
11761 struct ctl_lun *lun;
11762 uint32_t targ_lun, initidx;
11764 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11765 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11766 mtx_lock(&softc->ctl_lock);
11767 if (targ_lun >= ctl_max_luns ||
11768 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11769 mtx_unlock(&softc->ctl_lock);
11770 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11773 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11774 mtx_unlock(&softc->ctl_lock);
11775 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11777 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11778 union ctl_ha_msg msg_info;
11780 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11781 msg_info.hdr.nexus = io->io_hdr.nexus;
11782 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11783 msg_info.hdr.original_sc = NULL;
11784 msg_info.hdr.serializing_sc = NULL;
11785 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11786 sizeof(msg_info.task), M_WAITOK);
11792 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11797 mtx_assert(&lun->lun_lock, MA_OWNED);
11800 * Run through the OOA queue and attempt to find the given I/O.
11801 * The target port, initiator ID, tag type and tag number have to
11802 * match the values that we got from the initiator. If we have an
11803 * untagged command to abort, simply abort the first untagged command
11804 * we come to. We only allow one untagged command at a time of course.
11806 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11807 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11809 if ((targ_port == UINT32_MAX ||
11810 targ_port == xio->io_hdr.nexus.targ_port) &&
11811 (init_id == UINT32_MAX ||
11812 init_id == xio->io_hdr.nexus.initid)) {
11813 if (targ_port != xio->io_hdr.nexus.targ_port ||
11814 init_id != xio->io_hdr.nexus.initid)
11815 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11816 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11817 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11818 union ctl_ha_msg msg_info;
11820 msg_info.hdr.nexus = xio->io_hdr.nexus;
11821 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11822 msg_info.task.tag_num = xio->scsiio.tag_num;
11823 msg_info.task.tag_type = xio->scsiio.tag_type;
11824 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11825 msg_info.hdr.original_sc = NULL;
11826 msg_info.hdr.serializing_sc = NULL;
11827 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11828 sizeof(msg_info.task), M_NOWAIT);
11835 ctl_abort_task_set(union ctl_io *io)
11837 struct ctl_softc *softc = CTL_SOFTC(io);
11838 struct ctl_lun *lun;
11844 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11845 mtx_lock(&softc->ctl_lock);
11846 if (targ_lun >= ctl_max_luns ||
11847 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11848 mtx_unlock(&softc->ctl_lock);
11849 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11853 mtx_lock(&lun->lun_lock);
11854 mtx_unlock(&softc->ctl_lock);
11855 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11856 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11857 io->io_hdr.nexus.initid,
11858 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11859 } else { /* CTL_TASK_CLEAR_TASK_SET */
11860 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11861 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11863 mtx_unlock(&lun->lun_lock);
11864 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11869 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11870 ctl_ua_type ua_type)
11872 struct ctl_lun *lun;
11873 struct scsi_sense_data *ps;
11876 p = initidx / CTL_MAX_INIT_PER_PORT;
11877 i = initidx % CTL_MAX_INIT_PER_PORT;
11878 mtx_lock(&softc->ctl_lock);
11879 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11880 mtx_lock(&lun->lun_lock);
11882 ctl_abort_tasks_lun(lun, p, i, 1);
11884 ps = lun->pending_sense[p];
11886 ps[i].error_code = 0;
11887 /* Clear reservation. */
11888 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11889 lun->flags &= ~CTL_LUN_RESERVED;
11890 /* Clear prevent media removal. */
11891 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11892 ctl_clear_mask(lun->prevent, initidx);
11893 lun->prevent_count--;
11895 /* Clear TPC status */
11896 ctl_tpc_lun_clear(lun, initidx);
11897 /* Establish UA. */
11898 ctl_est_ua(lun, initidx, ua_type);
11899 mtx_unlock(&lun->lun_lock);
11901 mtx_unlock(&softc->ctl_lock);
11905 ctl_i_t_nexus_reset(union ctl_io *io)
11907 struct ctl_softc *softc = CTL_SOFTC(io);
11910 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11911 union ctl_ha_msg msg_info;
11913 msg_info.hdr.nexus = io->io_hdr.nexus;
11914 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11915 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11916 msg_info.hdr.original_sc = NULL;
11917 msg_info.hdr.serializing_sc = NULL;
11918 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11919 sizeof(msg_info.task), M_WAITOK);
11922 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11923 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
11924 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11929 ctl_abort_task(union ctl_io *io)
11931 struct ctl_softc *softc = CTL_SOFTC(io);
11933 struct ctl_lun *lun;
11936 char printbuf[128];
11946 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11947 mtx_lock(&softc->ctl_lock);
11948 if (targ_lun >= ctl_max_luns ||
11949 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11950 mtx_unlock(&softc->ctl_lock);
11951 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11956 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11957 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11960 mtx_lock(&lun->lun_lock);
11961 mtx_unlock(&softc->ctl_lock);
11963 * Run through the OOA queue and attempt to find the given I/O.
11964 * The target port, initiator ID, tag type and tag number have to
11965 * match the values that we got from the initiator. If we have an
11966 * untagged command to abort, simply abort the first untagged command
11967 * we come to. We only allow one untagged command at a time of course.
11969 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11970 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11972 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11974 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11975 lun->lun, xio->scsiio.tag_num,
11976 xio->scsiio.tag_type,
11977 (xio->io_hdr.blocked_links.tqe_prev
11978 == NULL) ? "" : " BLOCKED",
11979 (xio->io_hdr.flags &
11980 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11981 (xio->io_hdr.flags &
11982 CTL_FLAG_ABORT) ? " ABORT" : "",
11983 (xio->io_hdr.flags &
11984 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11985 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11987 printf("%s\n", sbuf_data(&sb));
11990 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11991 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11992 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11996 * If the abort says that the task is untagged, the
11997 * task in the queue must be untagged. Otherwise,
11998 * we just check to see whether the tag numbers
11999 * match. This is because the QLogic firmware
12000 * doesn't pass back the tag type in an abort
12004 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12005 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12006 || (xio->scsiio.tag_num == io->taskio.tag_num))
12009 * XXX KDM we've got problems with FC, because it
12010 * doesn't send down a tag type with aborts. So we
12011 * can only really go by the tag number...
12012 * This may cause problems with parallel SCSI.
12013 * Need to figure that out!!
12015 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12016 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12018 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12019 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12020 union ctl_ha_msg msg_info;
12022 msg_info.hdr.nexus = io->io_hdr.nexus;
12023 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12024 msg_info.task.tag_num = io->taskio.tag_num;
12025 msg_info.task.tag_type = io->taskio.tag_type;
12026 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12027 msg_info.hdr.original_sc = NULL;
12028 msg_info.hdr.serializing_sc = NULL;
12030 printf("Sent Abort to other side\n");
12032 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12033 sizeof(msg_info.task), M_NOWAIT);
12036 printf("ctl_abort_task: found I/O to abort\n");
12040 mtx_unlock(&lun->lun_lock);
12044 * This isn't really an error. It's entirely possible for
12045 * the abort and command completion to cross on the wire.
12046 * This is more of an informative/diagnostic error.
12049 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12050 "%u:%u:%u tag %d type %d\n",
12051 io->io_hdr.nexus.initid,
12052 io->io_hdr.nexus.targ_port,
12053 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12054 io->taskio.tag_type);
12057 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12062 ctl_query_task(union ctl_io *io, int task_set)
12064 struct ctl_softc *softc = CTL_SOFTC(io);
12066 struct ctl_lun *lun;
12070 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12071 mtx_lock(&softc->ctl_lock);
12072 if (targ_lun >= ctl_max_luns ||
12073 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12074 mtx_unlock(&softc->ctl_lock);
12075 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12078 mtx_lock(&lun->lun_lock);
12079 mtx_unlock(&softc->ctl_lock);
12080 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12081 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12083 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12084 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12085 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12088 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12093 mtx_unlock(&lun->lun_lock);
12095 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12097 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12102 ctl_query_async_event(union ctl_io *io)
12104 struct ctl_softc *softc = CTL_SOFTC(io);
12105 struct ctl_lun *lun;
12107 uint32_t targ_lun, initidx;
12109 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12110 mtx_lock(&softc->ctl_lock);
12111 if (targ_lun >= ctl_max_luns ||
12112 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12113 mtx_unlock(&softc->ctl_lock);
12114 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12117 mtx_lock(&lun->lun_lock);
12118 mtx_unlock(&softc->ctl_lock);
12119 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12120 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12121 mtx_unlock(&lun->lun_lock);
12122 if (ua != CTL_UA_NONE)
12123 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12125 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12130 ctl_run_task(union ctl_io *io)
12134 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12135 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12136 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12137 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12138 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12139 switch (io->taskio.task_action) {
12140 case CTL_TASK_ABORT_TASK:
12141 retval = ctl_abort_task(io);
12143 case CTL_TASK_ABORT_TASK_SET:
12144 case CTL_TASK_CLEAR_TASK_SET:
12145 retval = ctl_abort_task_set(io);
12147 case CTL_TASK_CLEAR_ACA:
12149 case CTL_TASK_I_T_NEXUS_RESET:
12150 retval = ctl_i_t_nexus_reset(io);
12152 case CTL_TASK_LUN_RESET:
12153 retval = ctl_lun_reset(io);
12155 case CTL_TASK_TARGET_RESET:
12156 case CTL_TASK_BUS_RESET:
12157 retval = ctl_target_reset(io);
12159 case CTL_TASK_PORT_LOGIN:
12161 case CTL_TASK_PORT_LOGOUT:
12163 case CTL_TASK_QUERY_TASK:
12164 retval = ctl_query_task(io, 0);
12166 case CTL_TASK_QUERY_TASK_SET:
12167 retval = ctl_query_task(io, 1);
12169 case CTL_TASK_QUERY_ASYNC_EVENT:
12170 retval = ctl_query_async_event(io);
12173 printf("%s: got unknown task management event %d\n",
12174 __func__, io->taskio.task_action);
12178 io->io_hdr.status = CTL_SUCCESS;
12180 io->io_hdr.status = CTL_ERROR;
12185 * For HA operation. Handle commands that come in from the other
12189 ctl_handle_isc(union ctl_io *io)
12191 struct ctl_softc *softc = CTL_SOFTC(io);
12192 struct ctl_lun *lun;
12193 const struct ctl_cmd_entry *entry;
12196 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12197 switch (io->io_hdr.msg_type) {
12198 case CTL_MSG_SERIALIZE:
12199 ctl_serialize_other_sc_cmd(&io->scsiio);
12201 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12202 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12203 if (targ_lun >= ctl_max_luns ||
12204 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12208 mtx_lock(&lun->lun_lock);
12209 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12210 mtx_unlock(&lun->lun_lock);
12214 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12215 mtx_unlock(&lun->lun_lock);
12216 ctl_enqueue_rtr(io);
12218 case CTL_MSG_FINISH_IO:
12219 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12223 if (targ_lun >= ctl_max_luns ||
12224 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12228 mtx_lock(&lun->lun_lock);
12229 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12230 ctl_check_blocked(lun);
12231 mtx_unlock(&lun->lun_lock);
12234 case CTL_MSG_PERS_ACTION:
12235 ctl_hndl_per_res_out_on_other_sc(io);
12238 case CTL_MSG_BAD_JUJU:
12241 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12242 ctl_datamove_remote(io);
12244 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12245 io->scsiio.be_move_done(io);
12247 case CTL_MSG_FAILOVER:
12248 ctl_failover_lun(io);
12252 printf("%s: Invalid message type %d\n",
12253 __func__, io->io_hdr.msg_type);
12262 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12263 * there is no match.
12265 static ctl_lun_error_pattern
12266 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12268 const struct ctl_cmd_entry *entry;
12269 ctl_lun_error_pattern filtered_pattern, pattern;
12271 pattern = desc->error_pattern;
12274 * XXX KDM we need more data passed into this function to match a
12275 * custom pattern, and we actually need to implement custom pattern
12278 if (pattern & CTL_LUN_PAT_CMD)
12279 return (CTL_LUN_PAT_CMD);
12281 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12282 return (CTL_LUN_PAT_ANY);
12284 entry = ctl_get_cmd_entry(ctsio, NULL);
12286 filtered_pattern = entry->pattern & pattern;
12289 * If the user requested specific flags in the pattern (e.g.
12290 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12293 * If the user did not specify any flags, it doesn't matter whether
12294 * or not the command supports the flags.
12296 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12297 (pattern & ~CTL_LUN_PAT_MASK))
12298 return (CTL_LUN_PAT_NONE);
12301 * If the user asked for a range check, see if the requested LBA
12302 * range overlaps with this command's LBA range.
12304 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12310 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12312 return (CTL_LUN_PAT_NONE);
12314 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12315 desc->lba_range.len, FALSE);
12317 * A "pass" means that the LBA ranges don't overlap, so
12318 * this doesn't match the user's range criteria.
12320 if (action == CTL_ACTION_PASS)
12321 return (CTL_LUN_PAT_NONE);
12324 return (filtered_pattern);
12328 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12330 struct ctl_error_desc *desc, *desc2;
12332 mtx_assert(&lun->lun_lock, MA_OWNED);
12334 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12335 ctl_lun_error_pattern pattern;
12337 * Check to see whether this particular command matches
12338 * the pattern in the descriptor.
12340 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12341 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12344 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12345 case CTL_LUN_INJ_ABORTED:
12346 ctl_set_aborted(&io->scsiio);
12348 case CTL_LUN_INJ_MEDIUM_ERR:
12349 ctl_set_medium_error(&io->scsiio,
12350 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12351 CTL_FLAG_DATA_OUT);
12353 case CTL_LUN_INJ_UA:
12354 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12356 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12358 case CTL_LUN_INJ_CUSTOM:
12360 * We're assuming the user knows what he is doing.
12361 * Just copy the sense information without doing
12364 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12365 MIN(sizeof(desc->custom_sense),
12366 sizeof(io->scsiio.sense_data)));
12367 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12368 io->scsiio.sense_len = SSD_FULL_SIZE;
12369 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12371 case CTL_LUN_INJ_NONE:
12374 * If this is an error injection type we don't know
12375 * about, clear the continuous flag (if it is set)
12376 * so it will get deleted below.
12378 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12382 * By default, each error injection action is a one-shot
12384 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12387 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12393 #ifdef CTL_IO_DELAY
12395 ctl_datamove_timer_wakeup(void *arg)
12399 io = (union ctl_io *)arg;
12403 #endif /* CTL_IO_DELAY */
12406 ctl_datamove(union ctl_io *io)
12408 void (*fe_datamove)(union ctl_io *io);
12410 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12412 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12414 /* No data transferred yet. Frontend must update this when done. */
12415 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12418 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12423 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12424 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12426 sbuf_cat(&sb, path_str);
12427 switch (io->io_hdr.io_type) {
12429 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12430 sbuf_printf(&sb, "\n");
12431 sbuf_cat(&sb, path_str);
12432 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12433 io->scsiio.tag_num, io->scsiio.tag_type);
12436 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12437 "Tag Type: %d\n", io->taskio.task_action,
12438 io->taskio.tag_num, io->taskio.tag_type);
12441 panic("%s: Invalid CTL I/O type %d\n",
12442 __func__, io->io_hdr.io_type);
12444 sbuf_cat(&sb, path_str);
12445 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12446 (intmax_t)time_uptime - io->io_hdr.start_time);
12448 printf("%s", sbuf_data(&sb));
12450 #endif /* CTL_TIME_IO */
12452 #ifdef CTL_IO_DELAY
12453 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12454 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12456 struct ctl_lun *lun;
12460 && (lun->delay_info.datamove_delay > 0)) {
12462 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12463 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12464 callout_reset(&io->io_hdr.delay_callout,
12465 lun->delay_info.datamove_delay * hz,
12466 ctl_datamove_timer_wakeup, io);
12467 if (lun->delay_info.datamove_type ==
12468 CTL_DELAY_TYPE_ONESHOT)
12469 lun->delay_info.datamove_delay = 0;
12476 * This command has been aborted. Set the port status, so we fail
12479 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12480 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12481 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12482 io->io_hdr.nexus.targ_port,
12483 io->io_hdr.nexus.targ_lun);
12484 io->io_hdr.port_status = 31337;
12486 * Note that the backend, in this case, will get the
12487 * callback in its context. In other cases it may get
12488 * called in the frontend's interrupt thread context.
12490 io->scsiio.be_move_done(io);
12494 /* Don't confuse frontend with zero length data move. */
12495 if (io->scsiio.kern_data_len == 0) {
12496 io->scsiio.be_move_done(io);
12500 fe_datamove = CTL_PORT(io)->fe_datamove;
12505 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12507 union ctl_ha_msg msg;
12509 struct bintime cur_bt;
12512 memset(&msg, 0, sizeof(msg));
12513 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12514 msg.hdr.original_sc = io;
12515 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12516 msg.hdr.nexus = io->io_hdr.nexus;
12517 msg.hdr.status = io->io_hdr.status;
12518 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12519 msg.scsi.tag_num = io->scsiio.tag_num;
12520 msg.scsi.tag_type = io->scsiio.tag_type;
12521 msg.scsi.scsi_status = io->scsiio.scsi_status;
12522 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12523 io->scsiio.sense_len);
12524 msg.scsi.sense_len = io->scsiio.sense_len;
12525 msg.scsi.port_status = io->io_hdr.port_status;
12526 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12527 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12528 ctl_failover_io(io, /*have_lock*/ have_lock);
12531 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12532 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12533 msg.scsi.sense_len, M_WAITOK);
12536 getbinuptime(&cur_bt);
12537 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12538 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12540 io->io_hdr.num_dmas++;
12544 * The DMA to the remote side is done, now we need to tell the other side
12545 * we're done so it can continue with its data movement.
12548 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12555 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12556 printf("%s: ISC DMA write failed with error %d", __func__,
12558 ctl_set_internal_failure(&io->scsiio,
12560 /*retry_count*/ rq->ret);
12563 ctl_dt_req_free(rq);
12565 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12566 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12567 free(io->io_hdr.remote_sglist, M_CTL);
12568 io->io_hdr.remote_sglist = NULL;
12569 io->io_hdr.local_sglist = NULL;
12572 * The data is in local and remote memory, so now we need to send
12573 * status (good or back) back to the other side.
12575 ctl_send_datamove_done(io, /*have_lock*/ 0);
12579 * We've moved the data from the host/controller into local memory. Now we
12580 * need to push it over to the remote controller's memory.
12583 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12587 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12588 ctl_datamove_remote_write_cb);
12593 ctl_datamove_remote_write(union ctl_io *io)
12596 void (*fe_datamove)(union ctl_io *io);
12599 * - Get the data from the host/HBA into local memory.
12600 * - DMA memory from the local controller to the remote controller.
12601 * - Send status back to the remote controller.
12604 retval = ctl_datamove_remote_sgl_setup(io);
12608 /* Switch the pointer over so the FETD knows what to do */
12609 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12612 * Use a custom move done callback, since we need to send completion
12613 * back to the other controller, not to the backend on this side.
12615 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12617 fe_datamove = CTL_PORT(io)->fe_datamove;
12622 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12631 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12632 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12633 free(io->io_hdr.remote_sglist, M_CTL);
12634 io->io_hdr.remote_sglist = NULL;
12635 io->io_hdr.local_sglist = NULL;
12638 scsi_path_string(io, path_str, sizeof(path_str));
12639 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12640 sbuf_cat(&sb, path_str);
12641 scsi_command_string(&io->scsiio, NULL, &sb);
12642 sbuf_printf(&sb, "\n");
12643 sbuf_cat(&sb, path_str);
12644 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12645 io->scsiio.tag_num, io->scsiio.tag_type);
12646 sbuf_cat(&sb, path_str);
12647 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12648 io->io_hdr.flags, io->io_hdr.status);
12650 printk("%s", sbuf_data(&sb));
12655 * The read is done, now we need to send status (good or bad) back
12656 * to the other side.
12658 ctl_send_datamove_done(io, /*have_lock*/ 0);
12664 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12667 void (*fe_datamove)(union ctl_io *io);
12671 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12672 printf("%s: ISC DMA read failed with error %d\n", __func__,
12674 ctl_set_internal_failure(&io->scsiio,
12676 /*retry_count*/ rq->ret);
12679 ctl_dt_req_free(rq);
12681 /* Switch the pointer over so the FETD knows what to do */
12682 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12685 * Use a custom move done callback, since we need to send completion
12686 * back to the other controller, not to the backend on this side.
12688 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12690 /* XXX KDM add checks like the ones in ctl_datamove? */
12692 fe_datamove = CTL_PORT(io)->fe_datamove;
12697 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12699 struct ctl_sg_entry *local_sglist;
12700 uint32_t len_to_go;
12705 local_sglist = io->io_hdr.local_sglist;
12706 len_to_go = io->scsiio.kern_data_len;
12709 * The difficult thing here is that the size of the various
12710 * S/G segments may be different than the size from the
12711 * remote controller. That'll make it harder when DMAing
12712 * the data back to the other side.
12714 for (i = 0; len_to_go > 0; i++) {
12715 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12716 local_sglist[i].addr =
12717 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12719 len_to_go -= local_sglist[i].len;
12722 * Reset the number of S/G entries accordingly. The original
12723 * number of S/G entries is available in rem_sg_entries.
12725 io->scsiio.kern_sg_entries = i;
12728 printf("%s: kern_sg_entries = %d\n", __func__,
12729 io->scsiio.kern_sg_entries);
12730 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12731 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12732 local_sglist[i].addr, local_sglist[i].len);
12739 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12740 ctl_ha_dt_cb callback)
12742 struct ctl_ha_dt_req *rq;
12743 struct ctl_sg_entry *remote_sglist, *local_sglist;
12744 uint32_t local_used, remote_used, total_used;
12747 rq = ctl_dt_req_alloc();
12750 * If we failed to allocate the request, and if the DMA didn't fail
12751 * anyway, set busy status. This is just a resource allocation
12755 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12756 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12757 ctl_set_busy(&io->scsiio);
12759 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12760 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12763 ctl_dt_req_free(rq);
12766 * The data move failed. We need to return status back
12767 * to the other controller. No point in trying to DMA
12768 * data to the remote controller.
12771 ctl_send_datamove_done(io, /*have_lock*/ 0);
12776 local_sglist = io->io_hdr.local_sglist;
12777 remote_sglist = io->io_hdr.remote_sglist;
12783 * Pull/push the data over the wire from/to the other controller.
12784 * This takes into account the possibility that the local and
12785 * remote sglists may not be identical in terms of the size of
12786 * the elements and the number of elements.
12788 * One fundamental assumption here is that the length allocated for
12789 * both the local and remote sglists is identical. Otherwise, we've
12790 * essentially got a coding error of some sort.
12792 isc_ret = CTL_HA_STATUS_SUCCESS;
12793 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12797 rq->command = command;
12801 * Both pointers should be aligned. But it is possible
12802 * that the allocation length is not. They should both
12803 * also have enough slack left over at the end, though,
12804 * to round up to the next 8 byte boundary.
12806 cur_len = MIN(local_sglist[i].len - local_used,
12807 remote_sglist[j].len - remote_used);
12808 rq->size = cur_len;
12810 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12811 tmp_ptr += local_used;
12814 /* Use physical addresses when talking to ISC hardware */
12815 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12816 /* XXX KDM use busdma */
12817 rq->local = vtophys(tmp_ptr);
12819 rq->local = tmp_ptr;
12821 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12822 ("HA does not support BUS_ADDR"));
12823 rq->local = tmp_ptr;
12826 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12827 tmp_ptr += remote_used;
12828 rq->remote = tmp_ptr;
12830 rq->callback = NULL;
12832 local_used += cur_len;
12833 if (local_used >= local_sglist[i].len) {
12838 remote_used += cur_len;
12839 if (remote_used >= remote_sglist[j].len) {
12843 total_used += cur_len;
12845 if (total_used >= io->scsiio.kern_data_len)
12846 rq->callback = callback;
12849 printf("%s: %s: local %p remote %p size %d\n", __func__,
12850 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12851 rq->local, rq->remote, rq->size);
12854 isc_ret = ctl_dt_single(rq);
12855 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12858 if (isc_ret != CTL_HA_STATUS_WAIT) {
12867 ctl_datamove_remote_read(union ctl_io *io)
12873 * This will send an error to the other controller in the case of a
12876 retval = ctl_datamove_remote_sgl_setup(io);
12880 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12881 ctl_datamove_remote_read_cb);
12884 * Make sure we free memory if there was an error.. The
12885 * ctl_datamove_remote_xfer() function will send the
12886 * datamove done message, or call the callback with an
12887 * error if there is a problem.
12889 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12890 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12891 free(io->io_hdr.remote_sglist, M_CTL);
12892 io->io_hdr.remote_sglist = NULL;
12893 io->io_hdr.local_sglist = NULL;
12898 * Process a datamove request from the other controller. This is used for
12899 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12900 * first. Once that is complete, the data gets DMAed into the remote
12901 * controller's memory. For reads, we DMA from the remote controller's
12902 * memory into our memory first, and then move it out to the FETD.
12905 ctl_datamove_remote(union ctl_io *io)
12908 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12910 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12911 ctl_failover_io(io, /*have_lock*/ 0);
12916 * Note that we look for an aborted I/O here, but don't do some of
12917 * the other checks that ctl_datamove() normally does.
12918 * We don't need to run the datamove delay code, since that should
12919 * have been done if need be on the other controller.
12921 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12922 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12923 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12924 io->io_hdr.nexus.targ_port,
12925 io->io_hdr.nexus.targ_lun);
12926 io->io_hdr.port_status = 31338;
12927 ctl_send_datamove_done(io, /*have_lock*/ 0);
12931 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12932 ctl_datamove_remote_write(io);
12933 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12934 ctl_datamove_remote_read(io);
12936 io->io_hdr.port_status = 31339;
12937 ctl_send_datamove_done(io, /*have_lock*/ 0);
12942 ctl_process_done(union ctl_io *io)
12944 struct ctl_softc *softc = CTL_SOFTC(io);
12945 struct ctl_port *port = CTL_PORT(io);
12946 struct ctl_lun *lun = CTL_LUN(io);
12947 void (*fe_done)(union ctl_io *io);
12948 union ctl_ha_msg msg;
12950 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12951 fe_done = port->fe_done;
12954 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12959 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12960 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12962 sbuf_cat(&sb, path_str);
12963 switch (io->io_hdr.io_type) {
12965 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12966 sbuf_printf(&sb, "\n");
12967 sbuf_cat(&sb, path_str);
12968 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12969 io->scsiio.tag_num, io->scsiio.tag_type);
12972 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12973 "Tag Type: %d\n", io->taskio.task_action,
12974 io->taskio.tag_num, io->taskio.tag_type);
12977 panic("%s: Invalid CTL I/O type %d\n",
12978 __func__, io->io_hdr.io_type);
12980 sbuf_cat(&sb, path_str);
12981 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12982 (intmax_t)time_uptime - io->io_hdr.start_time);
12984 printf("%s", sbuf_data(&sb));
12986 #endif /* CTL_TIME_IO */
12988 switch (io->io_hdr.io_type) {
12992 if (ctl_debug & CTL_DEBUG_INFO)
12993 ctl_io_error_print(io, NULL);
12997 panic("%s: Invalid CTL I/O type %d\n",
12998 __func__, io->io_hdr.io_type);
13002 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13003 io->io_hdr.nexus.targ_mapped_lun));
13007 mtx_lock(&lun->lun_lock);
13010 * Check to see if we have any informational exception and status
13011 * of this command can be modified to report it in form of either
13012 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13014 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13015 io->io_hdr.status == CTL_SUCCESS &&
13016 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13017 uint8_t mrie = lun->MODE_IE.mrie;
13018 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13019 (lun->MODE_VER.byte3 & SMS_VER_PER));
13020 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13021 mrie == SIEP_MRIE_REC_UNCOND ||
13022 mrie == SIEP_MRIE_NO_SENSE) &&
13023 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13024 CTL_CMD_FLAG_NO_SENSE) == 0) {
13025 ctl_set_sense(&io->scsiio,
13026 /*current_error*/ 1,
13027 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13028 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13029 /*asc*/ lun->ie_asc,
13030 /*ascq*/ lun->ie_ascq,
13032 lun->ie_reported = 1;
13034 } else if (lun->ie_reported < 0)
13035 lun->ie_reported = 0;
13038 * Check to see if we have any errors to inject here. We only
13039 * inject errors for commands that don't already have errors set.
13041 if (!STAILQ_EMPTY(&lun->error_list) &&
13042 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13043 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13044 ctl_inject_error(lun, io);
13047 * XXX KDM how do we treat commands that aren't completed
13050 * XXX KDM should we also track I/O latency?
13052 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13053 io->io_hdr.io_type == CTL_IO_SCSI) {
13059 bintime_sub(&bt, &io->io_hdr.start_bt);
13061 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13063 type = CTL_STATS_READ;
13064 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13066 type = CTL_STATS_WRITE;
13068 type = CTL_STATS_NO_IO;
13070 #ifdef CTL_LEGACY_STATS
13071 uint32_t targ_port = port->targ_port;
13072 lun->legacy_stats.ports[targ_port].bytes[type] +=
13073 io->scsiio.kern_total_len;
13074 lun->legacy_stats.ports[targ_port].operations[type] ++;
13075 lun->legacy_stats.ports[targ_port].num_dmas[type] +=
13076 io->io_hdr.num_dmas;
13078 bintime_add(&lun->legacy_stats.ports[targ_port].dma_time[type],
13079 &io->io_hdr.dma_bt);
13080 bintime_add(&lun->legacy_stats.ports[targ_port].time[type],
13083 #endif /* CTL_LEGACY_STATS */
13085 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13086 lun->stats.operations[type] ++;
13087 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13089 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13090 bintime_add(&lun->stats.time[type], &bt);
13093 mtx_lock(&port->port_lock);
13094 port->stats.bytes[type] += io->scsiio.kern_total_len;
13095 port->stats.operations[type] ++;
13096 port->stats.dmas[type] += io->io_hdr.num_dmas;
13098 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13099 bintime_add(&port->stats.time[type], &bt);
13101 mtx_unlock(&port->port_lock);
13105 * Remove this from the OOA queue.
13107 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13109 if (TAILQ_EMPTY(&lun->ooa_queue))
13110 lun->last_busy = getsbinuptime();
13114 * Run through the blocked queue on this LUN and see if anything
13115 * has become unblocked, now that this transaction is done.
13117 ctl_check_blocked(lun);
13120 * If the LUN has been invalidated, free it if there is nothing
13121 * left on its OOA queue.
13123 if ((lun->flags & CTL_LUN_INVALID)
13124 && TAILQ_EMPTY(&lun->ooa_queue)) {
13125 mtx_unlock(&lun->lun_lock);
13128 mtx_unlock(&lun->lun_lock);
13133 * If this command has been aborted, make sure we set the status
13134 * properly. The FETD is responsible for freeing the I/O and doing
13135 * whatever it needs to do to clean up its state.
13137 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13138 ctl_set_task_aborted(&io->scsiio);
13141 * If enabled, print command error status.
13143 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13144 (ctl_debug & CTL_DEBUG_INFO) != 0)
13145 ctl_io_error_print(io, NULL);
13148 * Tell the FETD or the other shelf controller we're done with this
13149 * command. Note that only SCSI commands get to this point. Task
13150 * management commands are completed above.
13152 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13153 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13154 memset(&msg, 0, sizeof(msg));
13155 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13156 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13157 msg.hdr.nexus = io->io_hdr.nexus;
13158 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13159 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13167 * Front end should call this if it doesn't do autosense. When the request
13168 * sense comes back in from the initiator, we'll dequeue this and send it.
13171 ctl_queue_sense(union ctl_io *io)
13173 struct ctl_softc *softc = CTL_SOFTC(io);
13174 struct ctl_port *port = CTL_PORT(io);
13175 struct ctl_lun *lun;
13176 struct scsi_sense_data *ps;
13177 uint32_t initidx, p, targ_lun;
13179 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13181 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13184 * LUN lookup will likely move to the ctl_work_thread() once we
13185 * have our new queueing infrastructure (that doesn't put things on
13186 * a per-LUN queue initially). That is so that we can handle
13187 * things like an INQUIRY to a LUN that we don't have enabled. We
13188 * can't deal with that right now.
13189 * If we don't have a LUN for this, just toss the sense information.
13191 mtx_lock(&softc->ctl_lock);
13192 if (targ_lun >= ctl_max_luns ||
13193 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13194 mtx_unlock(&softc->ctl_lock);
13197 mtx_lock(&lun->lun_lock);
13198 mtx_unlock(&softc->ctl_lock);
13200 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13201 p = initidx / CTL_MAX_INIT_PER_PORT;
13202 if (lun->pending_sense[p] == NULL) {
13203 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13204 M_CTL, M_NOWAIT | M_ZERO);
13206 if ((ps = lun->pending_sense[p]) != NULL) {
13207 ps += initidx % CTL_MAX_INIT_PER_PORT;
13208 memset(ps, 0, sizeof(*ps));
13209 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13211 mtx_unlock(&lun->lun_lock);
13215 return (CTL_RETVAL_COMPLETE);
13219 * Primary command inlet from frontend ports. All SCSI and task I/O
13220 * requests must go through this function.
13223 ctl_queue(union ctl_io *io)
13225 struct ctl_port *port = CTL_PORT(io);
13227 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13230 io->io_hdr.start_time = time_uptime;
13231 getbinuptime(&io->io_hdr.start_bt);
13232 #endif /* CTL_TIME_IO */
13234 /* Map FE-specific LUN ID into global one. */
13235 io->io_hdr.nexus.targ_mapped_lun =
13236 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13238 switch (io->io_hdr.io_type) {
13241 if (ctl_debug & CTL_DEBUG_CDB)
13243 ctl_enqueue_incoming(io);
13246 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13250 return (CTL_RETVAL_COMPLETE);
13253 #ifdef CTL_IO_DELAY
13255 ctl_done_timer_wakeup(void *arg)
13259 io = (union ctl_io *)arg;
13262 #endif /* CTL_IO_DELAY */
13265 ctl_serseq_done(union ctl_io *io)
13267 struct ctl_lun *lun = CTL_LUN(io);;
13269 if (lun->be_lun == NULL ||
13270 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13272 mtx_lock(&lun->lun_lock);
13273 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13274 ctl_check_blocked(lun);
13275 mtx_unlock(&lun->lun_lock);
13279 ctl_done(union ctl_io *io)
13283 * Enable this to catch duplicate completion issues.
13286 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13287 printf("%s: type %d msg %d cdb %x iptl: "
13288 "%u:%u:%u tag 0x%04x "
13289 "flag %#x status %x\n",
13291 io->io_hdr.io_type,
13292 io->io_hdr.msg_type,
13294 io->io_hdr.nexus.initid,
13295 io->io_hdr.nexus.targ_port,
13296 io->io_hdr.nexus.targ_lun,
13297 (io->io_hdr.io_type ==
13299 io->taskio.tag_num :
13300 io->scsiio.tag_num,
13302 io->io_hdr.status);
13304 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13308 * This is an internal copy of an I/O, and should not go through
13309 * the normal done processing logic.
13311 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13314 #ifdef CTL_IO_DELAY
13315 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13316 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13318 struct ctl_lun *lun = CTL_LUN(io);
13321 && (lun->delay_info.done_delay > 0)) {
13323 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13324 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13325 callout_reset(&io->io_hdr.delay_callout,
13326 lun->delay_info.done_delay * hz,
13327 ctl_done_timer_wakeup, io);
13328 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13329 lun->delay_info.done_delay = 0;
13333 #endif /* CTL_IO_DELAY */
13335 ctl_enqueue_done(io);
13339 ctl_work_thread(void *arg)
13341 struct ctl_thread *thr = (struct ctl_thread *)arg;
13342 struct ctl_softc *softc = thr->ctl_softc;
13346 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13348 while (!softc->shutdown) {
13350 * We handle the queues in this order:
13352 * - done queue (to free up resources, unblock other commands)
13356 * If those queues are empty, we break out of the loop and
13359 mtx_lock(&thr->queue_lock);
13360 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13362 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13363 mtx_unlock(&thr->queue_lock);
13364 ctl_handle_isc(io);
13367 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13369 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13370 /* clear any blocked commands, call fe_done */
13371 mtx_unlock(&thr->queue_lock);
13372 ctl_process_done(io);
13375 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13377 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13378 mtx_unlock(&thr->queue_lock);
13379 if (io->io_hdr.io_type == CTL_IO_TASK)
13382 ctl_scsiio_precheck(softc, &io->scsiio);
13385 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13387 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13388 mtx_unlock(&thr->queue_lock);
13389 retval = ctl_scsiio(&io->scsiio);
13390 if (retval != CTL_RETVAL_COMPLETE)
13391 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13395 /* Sleep until we have something to do. */
13396 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13398 thr->thread = NULL;
13403 ctl_lun_thread(void *arg)
13405 struct ctl_softc *softc = (struct ctl_softc *)arg;
13406 struct ctl_be_lun *be_lun;
13408 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13410 while (!softc->shutdown) {
13411 mtx_lock(&softc->ctl_lock);
13412 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13413 if (be_lun != NULL) {
13414 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13415 mtx_unlock(&softc->ctl_lock);
13416 ctl_create_lun(be_lun);
13420 /* Sleep until we have something to do. */
13421 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13422 PDROP | PRIBIO, "-", 0);
13424 softc->lun_thread = NULL;
13429 ctl_thresh_thread(void *arg)
13431 struct ctl_softc *softc = (struct ctl_softc *)arg;
13432 struct ctl_lun *lun;
13433 struct ctl_logical_block_provisioning_page *page;
13435 union ctl_ha_msg msg;
13436 uint64_t thres, val;
13439 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13441 while (!softc->shutdown) {
13442 mtx_lock(&softc->ctl_lock);
13443 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13444 if ((lun->flags & CTL_LUN_DISABLED) ||
13445 (lun->flags & CTL_LUN_NO_MEDIA) ||
13446 lun->backend->lun_attr == NULL)
13448 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13449 softc->ha_mode == CTL_HA_MODE_XFER)
13451 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13454 page = &lun->MODE_LBP;
13455 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13456 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13458 thres = scsi_4btoul(page->descr[i].count);
13459 thres <<= CTL_LBP_EXPONENT;
13460 switch (page->descr[i].resource) {
13462 attr = "blocksavail";
13465 attr = "blocksused";
13468 attr = "poolblocksavail";
13471 attr = "poolblocksused";
13476 mtx_unlock(&softc->ctl_lock); // XXX
13477 val = lun->backend->lun_attr(
13478 lun->be_lun->be_lun, attr);
13479 mtx_lock(&softc->ctl_lock);
13480 if (val == UINT64_MAX)
13482 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13483 == SLBPPD_ARMING_INC)
13484 e = (val >= thres);
13486 e = (val <= thres);
13490 mtx_lock(&lun->lun_lock);
13492 scsi_u64to8b((uint8_t *)&page->descr[i] -
13493 (uint8_t *)page, lun->ua_tpt_info);
13494 if (lun->lasttpt == 0 ||
13495 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13496 lun->lasttpt = time_uptime;
13497 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13503 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13506 mtx_unlock(&lun->lun_lock);
13508 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13509 /* Send msg to other side. */
13510 bzero(&msg.ua, sizeof(msg.ua));
13511 msg.hdr.msg_type = CTL_MSG_UA;
13512 msg.hdr.nexus.initid = -1;
13513 msg.hdr.nexus.targ_port = -1;
13514 msg.hdr.nexus.targ_lun = lun->lun;
13515 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13517 msg.ua.ua_set = (set > 0);
13518 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13519 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13520 mtx_unlock(&softc->ctl_lock); // XXX
13521 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13522 sizeof(msg.ua), M_WAITOK);
13523 mtx_lock(&softc->ctl_lock);
13526 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13527 PDROP | PRIBIO, "-", CTL_LBP_PERIOD * hz);
13529 softc->thresh_thread = NULL;
13534 ctl_enqueue_incoming(union ctl_io *io)
13536 struct ctl_softc *softc = CTL_SOFTC(io);
13537 struct ctl_thread *thr;
13540 idx = (io->io_hdr.nexus.targ_port * 127 +
13541 io->io_hdr.nexus.initid) % worker_threads;
13542 thr = &softc->threads[idx];
13543 mtx_lock(&thr->queue_lock);
13544 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13545 mtx_unlock(&thr->queue_lock);
13550 ctl_enqueue_rtr(union ctl_io *io)
13552 struct ctl_softc *softc = CTL_SOFTC(io);
13553 struct ctl_thread *thr;
13555 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13556 mtx_lock(&thr->queue_lock);
13557 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13558 mtx_unlock(&thr->queue_lock);
13563 ctl_enqueue_done(union ctl_io *io)
13565 struct ctl_softc *softc = CTL_SOFTC(io);
13566 struct ctl_thread *thr;
13568 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13569 mtx_lock(&thr->queue_lock);
13570 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13571 mtx_unlock(&thr->queue_lock);
13576 ctl_enqueue_isc(union ctl_io *io)
13578 struct ctl_softc *softc = CTL_SOFTC(io);
13579 struct ctl_thread *thr;
13581 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13582 mtx_lock(&thr->queue_lock);
13583 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13584 mtx_unlock(&thr->queue_lock);