2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2015 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>
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
55 #include <sys/fcntl.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
66 #include <sys/endian.h>
67 #include <sys/sysctl.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_cd.h>
73 #include <cam/scsi/scsi_da.h>
74 #include <cam/ctl/ctl_io.h>
75 #include <cam/ctl/ctl.h>
76 #include <cam/ctl/ctl_frontend.h>
77 #include <cam/ctl/ctl_util.h>
78 #include <cam/ctl/ctl_backend.h>
79 #include <cam/ctl/ctl_ioctl.h>
80 #include <cam/ctl/ctl_ha.h>
81 #include <cam/ctl/ctl_private.h>
82 #include <cam/ctl/ctl_debug.h>
83 #include <cam/ctl/ctl_scsi_all.h>
84 #include <cam/ctl/ctl_error.h>
86 struct ctl_softc *control_softc = NULL;
89 * Template mode pages.
93 * Note that these are default values only. The actual values will be
94 * filled in when the user does a mode sense.
96 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
97 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
98 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
99 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
100 /*read_retry_count*/0,
101 /*correction_span*/0,
102 /*head_offset_count*/0,
103 /*data_strobe_offset_cnt*/0,
104 /*byte8*/SMS_RWER_LBPERE,
105 /*write_retry_count*/0,
107 /*recovery_time_limit*/{0, 0},
110 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
111 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
112 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
113 /*byte3*/SMS_RWER_PER,
114 /*read_retry_count*/0,
115 /*correction_span*/0,
116 /*head_offset_count*/0,
117 /*data_strobe_offset_cnt*/0,
118 /*byte8*/SMS_RWER_LBPERE,
119 /*write_retry_count*/0,
121 /*recovery_time_limit*/{0, 0},
124 const static struct scsi_format_page format_page_default = {
125 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
126 /*page_length*/sizeof(struct scsi_format_page) - 2,
127 /*tracks_per_zone*/ {0, 0},
128 /*alt_sectors_per_zone*/ {0, 0},
129 /*alt_tracks_per_zone*/ {0, 0},
130 /*alt_tracks_per_lun*/ {0, 0},
131 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
132 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
133 /*bytes_per_sector*/ {0, 0},
134 /*interleave*/ {0, 0},
135 /*track_skew*/ {0, 0},
136 /*cylinder_skew*/ {0, 0},
138 /*reserved*/ {0, 0, 0}
141 const static struct scsi_format_page format_page_changeable = {
142 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
143 /*page_length*/sizeof(struct scsi_format_page) - 2,
144 /*tracks_per_zone*/ {0, 0},
145 /*alt_sectors_per_zone*/ {0, 0},
146 /*alt_tracks_per_zone*/ {0, 0},
147 /*alt_tracks_per_lun*/ {0, 0},
148 /*sectors_per_track*/ {0, 0},
149 /*bytes_per_sector*/ {0, 0},
150 /*interleave*/ {0, 0},
151 /*track_skew*/ {0, 0},
152 /*cylinder_skew*/ {0, 0},
154 /*reserved*/ {0, 0, 0}
157 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
158 /*page_code*/SMS_RIGID_DISK_PAGE,
159 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
160 /*cylinders*/ {0, 0, 0},
161 /*heads*/ CTL_DEFAULT_HEADS,
162 /*start_write_precomp*/ {0, 0, 0},
163 /*start_reduced_current*/ {0, 0, 0},
164 /*step_rate*/ {0, 0},
165 /*landing_zone_cylinder*/ {0, 0, 0},
166 /*rpl*/ SRDP_RPL_DISABLED,
167 /*rotational_offset*/ 0,
169 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
170 CTL_DEFAULT_ROTATION_RATE & 0xff},
174 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
175 /*page_code*/SMS_RIGID_DISK_PAGE,
176 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
177 /*cylinders*/ {0, 0, 0},
179 /*start_write_precomp*/ {0, 0, 0},
180 /*start_reduced_current*/ {0, 0, 0},
181 /*step_rate*/ {0, 0},
182 /*landing_zone_cylinder*/ {0, 0, 0},
184 /*rotational_offset*/ 0,
186 /*rotation_rate*/ {0, 0},
190 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
191 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
192 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
194 /*read_retry_count*/0,
195 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
196 /*recovery_time_limit*/{0, 0},
199 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
200 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
201 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
202 /*byte3*/SMS_VER_PER,
203 /*read_retry_count*/0,
204 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
205 /*recovery_time_limit*/{0, 0},
208 const static struct scsi_caching_page caching_page_default = {
209 /*page_code*/SMS_CACHING_PAGE,
210 /*page_length*/sizeof(struct scsi_caching_page) - 2,
211 /*flags1*/ SCP_DISC | SCP_WCE,
213 /*disable_pf_transfer_len*/ {0xff, 0xff},
214 /*min_prefetch*/ {0, 0},
215 /*max_prefetch*/ {0xff, 0xff},
216 /*max_pf_ceiling*/ {0xff, 0xff},
218 /*cache_segments*/ 0,
219 /*cache_seg_size*/ {0, 0},
221 /*non_cache_seg_size*/ {0, 0, 0}
224 const static struct scsi_caching_page caching_page_changeable = {
225 /*page_code*/SMS_CACHING_PAGE,
226 /*page_length*/sizeof(struct scsi_caching_page) - 2,
227 /*flags1*/ SCP_WCE | SCP_RCD,
229 /*disable_pf_transfer_len*/ {0, 0},
230 /*min_prefetch*/ {0, 0},
231 /*max_prefetch*/ {0, 0},
232 /*max_pf_ceiling*/ {0, 0},
234 /*cache_segments*/ 0,
235 /*cache_seg_size*/ {0, 0},
237 /*non_cache_seg_size*/ {0, 0, 0}
240 const static struct scsi_control_page control_page_default = {
241 /*page_code*/SMS_CONTROL_MODE_PAGE,
242 /*page_length*/sizeof(struct scsi_control_page) - 2,
244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
247 /*aen_holdoff_period*/{0, 0},
248 /*busy_timeout_period*/{0, 0},
249 /*extended_selftest_completion_time*/{0, 0}
252 const static struct scsi_control_page control_page_changeable = {
253 /*page_code*/SMS_CONTROL_MODE_PAGE,
254 /*page_length*/sizeof(struct scsi_control_page) - 2,
256 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
257 /*eca_and_aen*/SCP_SWP,
259 /*aen_holdoff_period*/{0, 0},
260 /*busy_timeout_period*/{0, 0},
261 /*extended_selftest_completion_time*/{0, 0}
264 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
266 const static struct scsi_control_ext_page control_ext_page_default = {
267 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
268 /*subpage_code*/0x01,
269 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
275 const static struct scsi_control_ext_page control_ext_page_changeable = {
276 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
277 /*subpage_code*/0x01,
278 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
284 const static struct scsi_info_exceptions_page ie_page_default = {
285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
286 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
287 /*info_flags*/SIEP_FLAGS_EWASC,
288 /*mrie*/SIEP_MRIE_NO,
289 /*interval_timer*/{0, 0, 0, 0},
290 /*report_count*/{0, 0, 0, 1}
293 const static struct scsi_info_exceptions_page ie_page_changeable = {
294 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
295 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
296 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
299 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
300 /*report_count*/{0xff, 0xff, 0xff, 0xff}
303 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
305 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
307 /*subpage_code*/0x02,
308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
315 /*count*/{0, 0, 0, 0}},
319 /*count*/{0, 0, 0, 0}},
323 /*count*/{0, 0, 0, 0}},
327 /*count*/{0, 0, 0, 0}}
331 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
333 /*subpage_code*/0x02,
334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
335 /*flags*/SLBPP_SITUA,
336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
341 /*count*/{0, 0, 0, 0}},
345 /*count*/{0, 0, 0, 0}},
349 /*count*/{0, 0, 0, 0}},
353 /*count*/{0, 0, 0, 0}}
357 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
358 /*page_code*/SMS_CDDVD_CAPS_PAGE,
359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
367 /*nvol_levels*/{0, 0},
368 /*buffer_size*/{8, 0},
373 /*copy_management*/0,
375 /*rotation_control*/0,
376 /*cur_write_speed*/0,
377 /*num_speed_descr*/0,
380 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
381 /*page_code*/SMS_CDDVD_CAPS_PAGE,
382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
390 /*nvol_levels*/{0, 0},
391 /*buffer_size*/{0, 0},
396 /*copy_management*/0,
398 /*rotation_control*/0,
399 /*cur_write_speed*/0,
400 /*num_speed_descr*/0,
403 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
404 static int worker_threads = -1;
405 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
406 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
407 &worker_threads, 1, "Number of worker threads");
408 static int ctl_debug = CTL_DEBUG_NONE;
409 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
410 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
411 &ctl_debug, 0, "Enabled debug flags");
412 static int ctl_lun_map_size = 1024;
413 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
414 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
417 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
418 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
419 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
420 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
422 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
424 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
426 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
427 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
428 static int ctl_init(void);
429 void ctl_shutdown(void);
430 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
431 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
432 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
433 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
434 struct ctl_ooa *ooa_hdr,
435 struct ctl_ooa_entry *kern_entries);
436 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
438 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
439 struct ctl_be_lun *be_lun);
440 static int ctl_free_lun(struct ctl_lun *lun);
441 static void ctl_create_lun(struct ctl_be_lun *be_lun);
442 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
444 static int ctl_do_mode_select(union ctl_io *io);
445 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
446 uint64_t res_key, uint64_t sa_res_key,
447 uint8_t type, uint32_t residx,
448 struct ctl_scsiio *ctsio,
449 struct scsi_per_res_out *cdb,
450 struct scsi_per_res_out_parms* param);
451 static void ctl_pro_preempt_other(struct ctl_lun *lun,
452 union ctl_ha_msg *msg);
453 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
454 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
455 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
456 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
457 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
458 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
459 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
461 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
463 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
464 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
465 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
466 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
467 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
468 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
470 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
471 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
472 union ctl_io *pending_io, union ctl_io *ooa_io);
473 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
474 union ctl_io *starting_io);
475 static int ctl_check_blocked(struct ctl_lun *lun);
476 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
477 const struct ctl_cmd_entry *entry,
478 struct ctl_scsiio *ctsio);
479 static void ctl_failover_lun(union ctl_io *io);
480 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
481 struct ctl_scsiio *ctsio);
482 static int ctl_scsiio(struct ctl_scsiio *ctsio);
484 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
485 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
486 ctl_ua_type ua_type);
487 static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
488 ctl_ua_type ua_type);
489 static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
490 static int ctl_abort_task(union ctl_io *io);
491 static int ctl_abort_task_set(union ctl_io *io);
492 static int ctl_query_task(union ctl_io *io, int task_set);
493 static int ctl_i_t_nexus_reset(union ctl_io *io);
494 static int ctl_query_async_event(union ctl_io *io);
495 static void ctl_run_task(union ctl_io *io);
497 static void ctl_datamove_timer_wakeup(void *arg);
498 static void ctl_done_timer_wakeup(void *arg);
499 #endif /* CTL_IO_DELAY */
501 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
502 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
503 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
504 static void ctl_datamove_remote_write(union ctl_io *io);
505 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
506 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
507 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
508 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
509 ctl_ha_dt_cb callback);
510 static void ctl_datamove_remote_read(union ctl_io *io);
511 static void ctl_datamove_remote(union ctl_io *io);
512 static void ctl_process_done(union ctl_io *io);
513 static void ctl_lun_thread(void *arg);
514 static void ctl_thresh_thread(void *arg);
515 static void ctl_work_thread(void *arg);
516 static void ctl_enqueue_incoming(union ctl_io *io);
517 static void ctl_enqueue_rtr(union ctl_io *io);
518 static void ctl_enqueue_done(union ctl_io *io);
519 static void ctl_enqueue_isc(union ctl_io *io);
520 static const struct ctl_cmd_entry *
521 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
522 static const struct ctl_cmd_entry *
523 ctl_validate_command(struct ctl_scsiio *ctsio);
524 static int ctl_cmd_applicable(uint8_t lun_type,
525 const struct ctl_cmd_entry *entry);
527 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
528 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
529 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
530 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
533 * Load the serialization table. This isn't very pretty, but is probably
534 * the easiest way to do it.
536 #include "ctl_ser_table.c"
539 * We only need to define open, close and ioctl routines for this driver.
541 static struct cdevsw ctl_cdevsw = {
542 .d_version = D_VERSION,
545 .d_close = ctl_close,
546 .d_ioctl = ctl_ioctl,
551 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
553 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
555 static moduledata_t ctl_moduledata = {
557 ctl_module_event_handler,
561 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
562 MODULE_VERSION(ctl, 1);
564 static struct ctl_frontend ha_frontend =
570 ctl_ha_datamove(union ctl_io *io)
573 struct ctl_sg_entry *sgl;
574 union ctl_ha_msg msg;
575 uint32_t sg_entries_sent;
576 int do_sg_copy, i, j;
578 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
579 memset(&msg.dt, 0, sizeof(msg.dt));
580 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
581 msg.hdr.original_sc = io->io_hdr.original_sc;
582 msg.hdr.serializing_sc = io;
583 msg.hdr.nexus = io->io_hdr.nexus;
584 msg.hdr.status = io->io_hdr.status;
585 msg.dt.flags = io->io_hdr.flags;
588 * We convert everything into a S/G list here. We can't
589 * pass by reference, only by value between controllers.
590 * So we can't pass a pointer to the S/G list, only as many
591 * S/G entries as we can fit in here. If it's possible for
592 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
593 * then we need to break this up into multiple transfers.
595 if (io->scsiio.kern_sg_entries == 0) {
596 msg.dt.kern_sg_entries = 1;
598 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
599 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
601 /* XXX KDM use busdma here! */
602 msg.dt.sg_list[0].addr =
603 (void *)vtophys(io->scsiio.kern_data_ptr);
606 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
607 ("HA does not support BUS_ADDR"));
608 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
610 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
613 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
617 msg.dt.kern_data_len = io->scsiio.kern_data_len;
618 msg.dt.kern_total_len = io->scsiio.kern_total_len;
619 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
620 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
621 msg.dt.sg_sequence = 0;
624 * Loop until we've sent all of the S/G entries. On the
625 * other end, we'll recompose these S/G entries into one
626 * contiguous list before processing.
628 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
629 msg.dt.sg_sequence++) {
630 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
631 sizeof(msg.dt.sg_list[0])),
632 msg.dt.kern_sg_entries - sg_entries_sent);
633 if (do_sg_copy != 0) {
634 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
635 for (i = sg_entries_sent, j = 0;
636 i < msg.dt.cur_sg_entries; i++, j++) {
638 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
639 msg.dt.sg_list[j].addr = sgl[i].addr;
641 /* XXX KDM use busdma here! */
642 msg.dt.sg_list[j].addr =
643 (void *)vtophys(sgl[i].addr);
646 KASSERT((io->io_hdr.flags &
647 CTL_FLAG_BUS_ADDR) == 0,
648 ("HA does not support BUS_ADDR"));
649 msg.dt.sg_list[j].addr = sgl[i].addr;
651 msg.dt.sg_list[j].len = sgl[i].len;
655 sg_entries_sent += msg.dt.cur_sg_entries;
656 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
657 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
658 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
659 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
660 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
661 io->io_hdr.port_status = 31341;
662 io->scsiio.be_move_done(io);
665 msg.dt.sent_sg_entries = sg_entries_sent;
669 * Officially handover the request from us to peer.
670 * If failover has just happened, then we must return error.
671 * If failover happen just after, then it is not our problem.
674 mtx_lock(&lun->lun_lock);
675 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
677 mtx_unlock(&lun->lun_lock);
678 io->io_hdr.port_status = 31342;
679 io->scsiio.be_move_done(io);
682 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
683 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
685 mtx_unlock(&lun->lun_lock);
689 ctl_ha_done(union ctl_io *io)
691 union ctl_ha_msg msg;
693 if (io->io_hdr.io_type == CTL_IO_SCSI) {
694 memset(&msg, 0, sizeof(msg));
695 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
696 msg.hdr.original_sc = io->io_hdr.original_sc;
697 msg.hdr.nexus = io->io_hdr.nexus;
698 msg.hdr.status = io->io_hdr.status;
699 msg.scsi.scsi_status = io->scsiio.scsi_status;
700 msg.scsi.tag_num = io->scsiio.tag_num;
701 msg.scsi.tag_type = io->scsiio.tag_type;
702 msg.scsi.sense_len = io->scsiio.sense_len;
703 msg.scsi.sense_residual = io->scsiio.sense_residual;
704 msg.scsi.residual = io->scsiio.residual;
705 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
706 io->scsiio.sense_len);
707 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
708 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
709 msg.scsi.sense_len, M_WAITOK);
715 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
716 union ctl_ha_msg *msg_info)
718 struct ctl_scsiio *ctsio;
720 if (msg_info->hdr.original_sc == NULL) {
721 printf("%s: original_sc == NULL!\n", __func__);
722 /* XXX KDM now what? */
726 ctsio = &msg_info->hdr.original_sc->scsiio;
727 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
728 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
729 ctsio->io_hdr.status = msg_info->hdr.status;
730 ctsio->scsi_status = msg_info->scsi.scsi_status;
731 ctsio->sense_len = msg_info->scsi.sense_len;
732 ctsio->sense_residual = msg_info->scsi.sense_residual;
733 ctsio->residual = msg_info->scsi.residual;
734 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
735 msg_info->scsi.sense_len);
736 ctl_enqueue_isc((union ctl_io *)ctsio);
740 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
741 union ctl_ha_msg *msg_info)
743 struct ctl_scsiio *ctsio;
745 if (msg_info->hdr.serializing_sc == NULL) {
746 printf("%s: serializing_sc == NULL!\n", __func__);
747 /* XXX KDM now what? */
751 ctsio = &msg_info->hdr.serializing_sc->scsiio;
752 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
753 ctl_enqueue_isc((union ctl_io *)ctsio);
757 ctl_isc_announce_lun(struct ctl_lun *lun)
759 struct ctl_softc *softc = lun->ctl_softc;
760 union ctl_ha_msg *msg;
761 struct ctl_ha_msg_lun_pr_key pr_key;
764 if (softc->ha_link != CTL_HA_LINK_ONLINE)
766 mtx_lock(&lun->lun_lock);
767 i = sizeof(msg->lun);
769 i += lun->lun_devid->len;
770 i += sizeof(pr_key) * lun->pr_key_count;
772 mtx_unlock(&lun->lun_lock);
773 msg = malloc(i, M_CTL, M_WAITOK);
774 mtx_lock(&lun->lun_lock);
775 k = sizeof(msg->lun);
777 k += lun->lun_devid->len;
778 k += sizeof(pr_key) * lun->pr_key_count;
784 bzero(&msg->lun, sizeof(msg->lun));
785 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
786 msg->hdr.nexus.targ_lun = lun->lun;
787 msg->hdr.nexus.targ_mapped_lun = lun->lun;
788 msg->lun.flags = lun->flags;
789 msg->lun.pr_generation = lun->pr_generation;
790 msg->lun.pr_res_idx = lun->pr_res_idx;
791 msg->lun.pr_res_type = lun->pr_res_type;
792 msg->lun.pr_key_count = lun->pr_key_count;
794 if (lun->lun_devid) {
795 msg->lun.lun_devid_len = lun->lun_devid->len;
796 memcpy(&msg->lun.data[i], lun->lun_devid->data,
797 msg->lun.lun_devid_len);
798 i += msg->lun.lun_devid_len;
800 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
801 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
804 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
807 mtx_unlock(&lun->lun_lock);
808 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
812 if (lun->flags & CTL_LUN_PRIMARY_SC) {
813 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
814 ctl_isc_announce_mode(lun, -1,
815 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
816 lun->mode_pages.index[i].subpage);
822 ctl_isc_announce_port(struct ctl_port *port)
824 struct ctl_softc *softc = port->ctl_softc;
825 union ctl_ha_msg *msg;
828 if (port->targ_port < softc->port_min ||
829 port->targ_port >= softc->port_max ||
830 softc->ha_link != CTL_HA_LINK_ONLINE)
832 i = sizeof(msg->port) + strlen(port->port_name) + 1;
834 i += port->lun_map_size * sizeof(uint32_t);
835 if (port->port_devid)
836 i += port->port_devid->len;
837 if (port->target_devid)
838 i += port->target_devid->len;
839 if (port->init_devid)
840 i += port->init_devid->len;
841 msg = malloc(i, M_CTL, M_WAITOK);
842 bzero(&msg->port, sizeof(msg->port));
843 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
844 msg->hdr.nexus.targ_port = port->targ_port;
845 msg->port.port_type = port->port_type;
846 msg->port.physical_port = port->physical_port;
847 msg->port.virtual_port = port->virtual_port;
848 msg->port.status = port->status;
850 msg->port.name_len = sprintf(&msg->port.data[i],
851 "%d:%s", softc->ha_id, port->port_name) + 1;
852 i += msg->port.name_len;
854 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
855 memcpy(&msg->port.data[i], port->lun_map,
856 msg->port.lun_map_len);
857 i += msg->port.lun_map_len;
859 if (port->port_devid) {
860 msg->port.port_devid_len = port->port_devid->len;
861 memcpy(&msg->port.data[i], port->port_devid->data,
862 msg->port.port_devid_len);
863 i += msg->port.port_devid_len;
865 if (port->target_devid) {
866 msg->port.target_devid_len = port->target_devid->len;
867 memcpy(&msg->port.data[i], port->target_devid->data,
868 msg->port.target_devid_len);
869 i += msg->port.target_devid_len;
871 if (port->init_devid) {
872 msg->port.init_devid_len = port->init_devid->len;
873 memcpy(&msg->port.data[i], port->init_devid->data,
874 msg->port.init_devid_len);
875 i += msg->port.init_devid_len;
877 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
883 ctl_isc_announce_iid(struct ctl_port *port, int iid)
885 struct ctl_softc *softc = port->ctl_softc;
886 union ctl_ha_msg *msg;
889 if (port->targ_port < softc->port_min ||
890 port->targ_port >= softc->port_max ||
891 softc->ha_link != CTL_HA_LINK_ONLINE)
893 mtx_lock(&softc->ctl_lock);
894 i = sizeof(msg->iid);
896 if (port->wwpn_iid[iid].name)
897 l = strlen(port->wwpn_iid[iid].name) + 1;
899 msg = malloc(i, M_CTL, M_NOWAIT);
901 mtx_unlock(&softc->ctl_lock);
904 bzero(&msg->iid, sizeof(msg->iid));
905 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
906 msg->hdr.nexus.targ_port = port->targ_port;
907 msg->hdr.nexus.initid = iid;
908 msg->iid.in_use = port->wwpn_iid[iid].in_use;
909 msg->iid.name_len = l;
910 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
911 if (port->wwpn_iid[iid].name)
912 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
913 mtx_unlock(&softc->ctl_lock);
914 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
919 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
920 uint8_t page, uint8_t subpage)
922 struct ctl_softc *softc = lun->ctl_softc;
923 union ctl_ha_msg msg;
926 if (softc->ha_link != CTL_HA_LINK_ONLINE)
928 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
929 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
930 page && lun->mode_pages.index[i].subpage == subpage)
933 if (i == CTL_NUM_MODE_PAGES)
936 /* Don't try to replicate pages not present on this device. */
937 if (lun->mode_pages.index[i].page_data == NULL)
940 bzero(&msg.mode, sizeof(msg.mode));
941 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
942 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
943 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
944 msg.hdr.nexus.targ_lun = lun->lun;
945 msg.hdr.nexus.targ_mapped_lun = lun->lun;
946 msg.mode.page_code = page;
947 msg.mode.subpage = subpage;
948 msg.mode.page_len = lun->mode_pages.index[i].page_len;
949 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
951 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
956 ctl_isc_ha_link_up(struct ctl_softc *softc)
958 struct ctl_port *port;
960 union ctl_ha_msg msg;
963 /* Announce this node parameters to peer for validation. */
964 msg.login.msg_type = CTL_MSG_LOGIN;
965 msg.login.version = CTL_HA_VERSION;
966 msg.login.ha_mode = softc->ha_mode;
967 msg.login.ha_id = softc->ha_id;
968 msg.login.max_luns = CTL_MAX_LUNS;
969 msg.login.max_ports = CTL_MAX_PORTS;
970 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
971 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
974 STAILQ_FOREACH(port, &softc->port_list, links) {
975 ctl_isc_announce_port(port);
976 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
977 if (port->wwpn_iid[i].in_use)
978 ctl_isc_announce_iid(port, i);
981 STAILQ_FOREACH(lun, &softc->lun_list, links)
982 ctl_isc_announce_lun(lun);
986 ctl_isc_ha_link_down(struct ctl_softc *softc)
988 struct ctl_port *port;
993 mtx_lock(&softc->ctl_lock);
994 STAILQ_FOREACH(lun, &softc->lun_list, links) {
995 mtx_lock(&lun->lun_lock);
996 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
997 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
998 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1000 mtx_unlock(&lun->lun_lock);
1002 mtx_unlock(&softc->ctl_lock);
1003 io = ctl_alloc_io(softc->othersc_pool);
1004 mtx_lock(&softc->ctl_lock);
1006 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1007 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1008 ctl_enqueue_isc(io);
1011 STAILQ_FOREACH(port, &softc->port_list, links) {
1012 if (port->targ_port >= softc->port_min &&
1013 port->targ_port < softc->port_max)
1015 port->status &= ~CTL_PORT_STATUS_ONLINE;
1016 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1017 port->wwpn_iid[i].in_use = 0;
1018 free(port->wwpn_iid[i].name, M_CTL);
1019 port->wwpn_iid[i].name = NULL;
1022 mtx_unlock(&softc->ctl_lock);
1026 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1028 struct ctl_lun *lun;
1029 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1031 mtx_lock(&softc->ctl_lock);
1032 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS ||
1033 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1034 mtx_unlock(&softc->ctl_lock);
1037 mtx_lock(&lun->lun_lock);
1038 mtx_unlock(&softc->ctl_lock);
1039 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1040 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1041 if (msg->ua.ua_all) {
1043 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1045 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1048 ctl_est_ua(lun, iid, msg->ua.ua_type);
1050 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1052 mtx_unlock(&lun->lun_lock);
1056 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1058 struct ctl_lun *lun;
1059 struct ctl_ha_msg_lun_pr_key pr_key;
1061 ctl_lun_flags oflags;
1064 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1065 mtx_lock(&softc->ctl_lock);
1066 if (targ_lun >= CTL_MAX_LUNS ||
1067 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1068 mtx_unlock(&softc->ctl_lock);
1071 mtx_lock(&lun->lun_lock);
1072 mtx_unlock(&softc->ctl_lock);
1073 if (lun->flags & CTL_LUN_DISABLED) {
1074 mtx_unlock(&lun->lun_lock);
1077 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1078 if (msg->lun.lun_devid_len != i || (i > 0 &&
1079 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1080 mtx_unlock(&lun->lun_lock);
1081 printf("%s: Received conflicting HA LUN %d\n",
1082 __func__, targ_lun);
1085 /* Record whether peer is primary. */
1086 oflags = lun->flags;
1087 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1088 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1089 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1091 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1092 if (oflags != lun->flags)
1093 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1095 /* If peer is primary and we are not -- use data */
1096 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1097 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1098 lun->pr_generation = msg->lun.pr_generation;
1099 lun->pr_res_idx = msg->lun.pr_res_idx;
1100 lun->pr_res_type = msg->lun.pr_res_type;
1101 lun->pr_key_count = msg->lun.pr_key_count;
1102 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1103 ctl_clr_prkey(lun, k);
1104 for (k = 0; k < msg->lun.pr_key_count; k++) {
1105 memcpy(&pr_key, &msg->lun.data[i],
1107 ctl_alloc_prkey(lun, pr_key.pr_iid);
1108 ctl_set_prkey(lun, pr_key.pr_iid,
1110 i += sizeof(pr_key);
1114 mtx_unlock(&lun->lun_lock);
1115 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1117 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1118 "primary" : "secondary"));
1120 /* If we are primary but peer doesn't know -- notify */
1121 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1122 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1123 ctl_isc_announce_lun(lun);
1128 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1130 struct ctl_port *port;
1131 struct ctl_lun *lun;
1134 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1136 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1137 msg->hdr.nexus.targ_port));
1139 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1140 port->frontend = &ha_frontend;
1141 port->targ_port = msg->hdr.nexus.targ_port;
1142 port->fe_datamove = ctl_ha_datamove;
1143 port->fe_done = ctl_ha_done;
1144 } else if (port->frontend == &ha_frontend) {
1145 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1146 msg->hdr.nexus.targ_port));
1149 printf("%s: Received conflicting HA port %d\n",
1150 __func__, msg->hdr.nexus.targ_port);
1153 port->port_type = msg->port.port_type;
1154 port->physical_port = msg->port.physical_port;
1155 port->virtual_port = msg->port.virtual_port;
1156 port->status = msg->port.status;
1158 free(port->port_name, M_CTL);
1159 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1161 i += msg->port.name_len;
1162 if (msg->port.lun_map_len != 0) {
1163 if (port->lun_map == NULL ||
1164 port->lun_map_size * sizeof(uint32_t) <
1165 msg->port.lun_map_len) {
1166 port->lun_map_size = 0;
1167 free(port->lun_map, M_CTL);
1168 port->lun_map = malloc(msg->port.lun_map_len,
1171 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1172 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1173 i += msg->port.lun_map_len;
1175 port->lun_map_size = 0;
1176 free(port->lun_map, M_CTL);
1177 port->lun_map = NULL;
1179 if (msg->port.port_devid_len != 0) {
1180 if (port->port_devid == NULL ||
1181 port->port_devid->len < msg->port.port_devid_len) {
1182 free(port->port_devid, M_CTL);
1183 port->port_devid = malloc(sizeof(struct ctl_devid) +
1184 msg->port.port_devid_len, M_CTL, M_WAITOK);
1186 memcpy(port->port_devid->data, &msg->port.data[i],
1187 msg->port.port_devid_len);
1188 port->port_devid->len = msg->port.port_devid_len;
1189 i += msg->port.port_devid_len;
1191 free(port->port_devid, M_CTL);
1192 port->port_devid = NULL;
1194 if (msg->port.target_devid_len != 0) {
1195 if (port->target_devid == NULL ||
1196 port->target_devid->len < msg->port.target_devid_len) {
1197 free(port->target_devid, M_CTL);
1198 port->target_devid = malloc(sizeof(struct ctl_devid) +
1199 msg->port.target_devid_len, M_CTL, M_WAITOK);
1201 memcpy(port->target_devid->data, &msg->port.data[i],
1202 msg->port.target_devid_len);
1203 port->target_devid->len = msg->port.target_devid_len;
1204 i += msg->port.target_devid_len;
1206 free(port->target_devid, M_CTL);
1207 port->target_devid = NULL;
1209 if (msg->port.init_devid_len != 0) {
1210 if (port->init_devid == NULL ||
1211 port->init_devid->len < msg->port.init_devid_len) {
1212 free(port->init_devid, M_CTL);
1213 port->init_devid = malloc(sizeof(struct ctl_devid) +
1214 msg->port.init_devid_len, M_CTL, M_WAITOK);
1216 memcpy(port->init_devid->data, &msg->port.data[i],
1217 msg->port.init_devid_len);
1218 port->init_devid->len = msg->port.init_devid_len;
1219 i += msg->port.init_devid_len;
1221 free(port->init_devid, M_CTL);
1222 port->init_devid = NULL;
1225 if (ctl_port_register(port) != 0) {
1226 printf("%s: ctl_port_register() failed with error\n",
1230 mtx_lock(&softc->ctl_lock);
1231 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1232 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1234 mtx_lock(&lun->lun_lock);
1235 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1236 mtx_unlock(&lun->lun_lock);
1238 mtx_unlock(&softc->ctl_lock);
1242 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1244 struct ctl_port *port;
1247 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1249 printf("%s: Received IID for unknown port %d\n",
1250 __func__, msg->hdr.nexus.targ_port);
1253 iid = msg->hdr.nexus.initid;
1254 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1255 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1256 free(port->wwpn_iid[iid].name, M_CTL);
1257 if (msg->iid.name_len) {
1258 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1259 msg->iid.name_len, M_CTL);
1261 port->wwpn_iid[iid].name = NULL;
1265 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1268 if (msg->login.version != CTL_HA_VERSION) {
1269 printf("CTL HA peers have different versions %d != %d\n",
1270 msg->login.version, CTL_HA_VERSION);
1271 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1274 if (msg->login.ha_mode != softc->ha_mode) {
1275 printf("CTL HA peers have different ha_mode %d != %d\n",
1276 msg->login.ha_mode, softc->ha_mode);
1277 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1280 if (msg->login.ha_id == softc->ha_id) {
1281 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1282 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1285 if (msg->login.max_luns != CTL_MAX_LUNS ||
1286 msg->login.max_ports != CTL_MAX_PORTS ||
1287 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1288 printf("CTL HA peers have different limits\n");
1289 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1295 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1297 struct ctl_lun *lun;
1299 uint32_t initidx, targ_lun;
1301 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1302 mtx_lock(&softc->ctl_lock);
1303 if (targ_lun >= CTL_MAX_LUNS ||
1304 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1305 mtx_unlock(&softc->ctl_lock);
1308 mtx_lock(&lun->lun_lock);
1309 mtx_unlock(&softc->ctl_lock);
1310 if (lun->flags & CTL_LUN_DISABLED) {
1311 mtx_unlock(&lun->lun_lock);
1314 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1315 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1316 msg->mode.page_code &&
1317 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1320 if (i == CTL_NUM_MODE_PAGES) {
1321 mtx_unlock(&lun->lun_lock);
1324 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1325 lun->mode_pages.index[i].page_len);
1326 initidx = ctl_get_initindex(&msg->hdr.nexus);
1328 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1329 mtx_unlock(&lun->lun_lock);
1333 * ISC (Inter Shelf Communication) event handler. Events from the HA
1334 * subsystem come in here.
1337 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1339 struct ctl_softc *softc = control_softc;
1341 struct ctl_prio *presio;
1342 ctl_ha_status isc_status;
1344 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1345 if (event == CTL_HA_EVT_MSG_RECV) {
1346 union ctl_ha_msg *msg, msgbuf;
1348 if (param > sizeof(msgbuf))
1349 msg = malloc(param, M_CTL, M_WAITOK);
1352 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1354 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1355 printf("%s: Error receiving message: %d\n",
1356 __func__, isc_status);
1362 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1363 switch (msg->hdr.msg_type) {
1364 case CTL_MSG_SERIALIZE:
1365 io = ctl_alloc_io(softc->othersc_pool);
1367 // populate ctsio from msg
1368 io->io_hdr.io_type = CTL_IO_SCSI;
1369 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1370 io->io_hdr.original_sc = msg->hdr.original_sc;
1371 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1374 * If we're in serialization-only mode, we don't
1375 * want to go through full done processing. Thus
1378 * XXX KDM add another flag that is more specific.
1380 if (softc->ha_mode != CTL_HA_MODE_XFER)
1381 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1382 io->io_hdr.nexus = msg->hdr.nexus;
1384 printf("port %u, iid %u, lun %u\n",
1385 io->io_hdr.nexus.targ_port,
1386 io->io_hdr.nexus.initid,
1387 io->io_hdr.nexus.targ_lun);
1389 io->scsiio.tag_num = msg->scsi.tag_num;
1390 io->scsiio.tag_type = msg->scsi.tag_type;
1392 io->io_hdr.start_time = time_uptime;
1393 getbinuptime(&io->io_hdr.start_bt);
1394 #endif /* CTL_TIME_IO */
1395 io->scsiio.cdb_len = msg->scsi.cdb_len;
1396 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1398 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1399 const struct ctl_cmd_entry *entry;
1401 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1402 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1404 entry->flags & CTL_FLAG_DATA_MASK;
1406 ctl_enqueue_isc(io);
1409 /* Performed on the Originating SC, XFER mode only */
1410 case CTL_MSG_DATAMOVE: {
1411 struct ctl_sg_entry *sgl;
1414 io = msg->hdr.original_sc;
1416 printf("%s: original_sc == NULL!\n", __func__);
1417 /* XXX KDM do something here */
1420 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1421 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1423 * Keep track of this, we need to send it back over
1424 * when the datamove is complete.
1426 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1427 if (msg->hdr.status == CTL_SUCCESS)
1428 io->io_hdr.status = msg->hdr.status;
1430 if (msg->dt.sg_sequence == 0) {
1432 getbinuptime(&io->io_hdr.dma_start_bt);
1434 i = msg->dt.kern_sg_entries +
1435 msg->dt.kern_data_len /
1436 CTL_HA_DATAMOVE_SEGMENT + 1;
1437 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1439 io->io_hdr.remote_sglist = sgl;
1440 io->io_hdr.local_sglist =
1441 &sgl[msg->dt.kern_sg_entries];
1443 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1445 io->scsiio.kern_sg_entries =
1446 msg->dt.kern_sg_entries;
1447 io->scsiio.rem_sg_entries =
1448 msg->dt.kern_sg_entries;
1449 io->scsiio.kern_data_len =
1450 msg->dt.kern_data_len;
1451 io->scsiio.kern_total_len =
1452 msg->dt.kern_total_len;
1453 io->scsiio.kern_data_resid =
1454 msg->dt.kern_data_resid;
1455 io->scsiio.kern_rel_offset =
1456 msg->dt.kern_rel_offset;
1457 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1458 io->io_hdr.flags |= msg->dt.flags &
1461 sgl = (struct ctl_sg_entry *)
1462 io->scsiio.kern_data_ptr;
1464 for (i = msg->dt.sent_sg_entries, j = 0;
1465 i < (msg->dt.sent_sg_entries +
1466 msg->dt.cur_sg_entries); i++, j++) {
1467 sgl[i].addr = msg->dt.sg_list[j].addr;
1468 sgl[i].len = msg->dt.sg_list[j].len;
1471 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1472 __func__, sgl[i].addr, sgl[i].len, j, i);
1477 * If this is the last piece of the I/O, we've got
1478 * the full S/G list. Queue processing in the thread.
1479 * Otherwise wait for the next piece.
1481 if (msg->dt.sg_last != 0)
1482 ctl_enqueue_isc(io);
1485 /* Performed on the Serializing (primary) SC, XFER mode only */
1486 case CTL_MSG_DATAMOVE_DONE: {
1487 if (msg->hdr.serializing_sc == NULL) {
1488 printf("%s: serializing_sc == NULL!\n",
1490 /* XXX KDM now what? */
1494 * We grab the sense information here in case
1495 * there was a failure, so we can return status
1496 * back to the initiator.
1498 io = msg->hdr.serializing_sc;
1499 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1500 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1501 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1502 io->io_hdr.port_status = msg->scsi.fetd_status;
1503 io->scsiio.residual = msg->scsi.residual;
1504 if (msg->hdr.status != CTL_STATUS_NONE) {
1505 io->io_hdr.status = msg->hdr.status;
1506 io->scsiio.scsi_status = msg->scsi.scsi_status;
1507 io->scsiio.sense_len = msg->scsi.sense_len;
1508 io->scsiio.sense_residual =msg->scsi.sense_residual;
1509 memcpy(&io->scsiio.sense_data,
1510 &msg->scsi.sense_data,
1511 msg->scsi.sense_len);
1512 if (msg->hdr.status == CTL_SUCCESS)
1513 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1515 ctl_enqueue_isc(io);
1519 /* Preformed on Originating SC, SER_ONLY mode */
1521 io = msg->hdr.original_sc;
1523 printf("%s: original_sc == NULL!\n",
1527 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1528 io->io_hdr.msg_type = CTL_MSG_R2R;
1529 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1530 ctl_enqueue_isc(io);
1534 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1536 * Performed on the Originating (i.e. secondary) SC in XFER
1539 case CTL_MSG_FINISH_IO:
1540 if (softc->ha_mode == CTL_HA_MODE_XFER)
1541 ctl_isc_handler_finish_xfer(softc, msg);
1543 ctl_isc_handler_finish_ser_only(softc, msg);
1546 /* Preformed on Originating SC */
1547 case CTL_MSG_BAD_JUJU:
1548 io = msg->hdr.original_sc;
1550 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1554 ctl_copy_sense_data(msg, io);
1556 * IO should have already been cleaned up on other
1557 * SC so clear this flag so we won't send a message
1558 * back to finish the IO there.
1560 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1561 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1563 /* io = msg->hdr.serializing_sc; */
1564 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1565 ctl_enqueue_isc(io);
1568 /* Handle resets sent from the other side */
1569 case CTL_MSG_MANAGE_TASKS: {
1570 struct ctl_taskio *taskio;
1571 taskio = (struct ctl_taskio *)ctl_alloc_io(
1572 softc->othersc_pool);
1573 ctl_zero_io((union ctl_io *)taskio);
1574 taskio->io_hdr.io_type = CTL_IO_TASK;
1575 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1576 taskio->io_hdr.nexus = msg->hdr.nexus;
1577 taskio->task_action = msg->task.task_action;
1578 taskio->tag_num = msg->task.tag_num;
1579 taskio->tag_type = msg->task.tag_type;
1581 taskio->io_hdr.start_time = time_uptime;
1582 getbinuptime(&taskio->io_hdr.start_bt);
1583 #endif /* CTL_TIME_IO */
1584 ctl_run_task((union ctl_io *)taskio);
1587 /* Persistent Reserve action which needs attention */
1588 case CTL_MSG_PERS_ACTION:
1589 presio = (struct ctl_prio *)ctl_alloc_io(
1590 softc->othersc_pool);
1591 ctl_zero_io((union ctl_io *)presio);
1592 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1593 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1594 presio->io_hdr.nexus = msg->hdr.nexus;
1595 presio->pr_msg = msg->pr;
1596 ctl_enqueue_isc((union ctl_io *)presio);
1599 ctl_isc_ua(softc, msg, param);
1601 case CTL_MSG_PORT_SYNC:
1602 ctl_isc_port_sync(softc, msg, param);
1604 case CTL_MSG_LUN_SYNC:
1605 ctl_isc_lun_sync(softc, msg, param);
1607 case CTL_MSG_IID_SYNC:
1608 ctl_isc_iid_sync(softc, msg, param);
1611 ctl_isc_login(softc, msg, param);
1613 case CTL_MSG_MODE_SYNC:
1614 ctl_isc_mode_sync(softc, msg, param);
1617 printf("Received HA message of unknown type %d\n",
1619 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1624 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1625 printf("CTL: HA link status changed from %d to %d\n",
1626 softc->ha_link, param);
1627 if (param == softc->ha_link)
1629 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1630 softc->ha_link = param;
1631 ctl_isc_ha_link_down(softc);
1633 softc->ha_link = param;
1634 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1635 ctl_isc_ha_link_up(softc);
1639 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1645 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1648 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1649 src->scsi.sense_len);
1650 dest->scsiio.scsi_status = src->scsi.scsi_status;
1651 dest->scsiio.sense_len = src->scsi.sense_len;
1652 dest->io_hdr.status = src->hdr.status;
1656 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1659 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1660 src->scsiio.sense_len);
1661 dest->scsi.scsi_status = src->scsiio.scsi_status;
1662 dest->scsi.sense_len = src->scsiio.sense_len;
1663 dest->hdr.status = src->io_hdr.status;
1667 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1669 struct ctl_softc *softc = lun->ctl_softc;
1672 if (initidx < softc->init_min || initidx >= softc->init_max)
1674 mtx_assert(&lun->lun_lock, MA_OWNED);
1675 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1678 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1682 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1686 mtx_assert(&lun->lun_lock, MA_OWNED);
1687 if (lun->pending_ua[port] == NULL)
1689 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1690 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1692 lun->pending_ua[port][i] |= ua;
1697 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1699 struct ctl_softc *softc = lun->ctl_softc;
1702 mtx_assert(&lun->lun_lock, MA_OWNED);
1703 for (i = softc->port_min; i < softc->port_max; i++)
1704 ctl_est_ua_port(lun, i, except, ua);
1708 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1710 struct ctl_softc *softc = lun->ctl_softc;
1713 if (initidx < softc->init_min || initidx >= softc->init_max)
1715 mtx_assert(&lun->lun_lock, MA_OWNED);
1716 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1719 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1723 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1725 struct ctl_softc *softc = lun->ctl_softc;
1728 mtx_assert(&lun->lun_lock, MA_OWNED);
1729 for (i = softc->port_min; i < softc->port_max; i++) {
1730 if (lun->pending_ua[i] == NULL)
1732 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1733 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1735 lun->pending_ua[i][j] &= ~ua;
1741 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1742 ctl_ua_type ua_type)
1744 struct ctl_lun *lun;
1746 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1747 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1748 mtx_lock(&lun->lun_lock);
1749 ctl_clr_ua(lun, initidx, ua_type);
1750 mtx_unlock(&lun->lun_lock);
1755 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1757 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1758 struct ctl_lun *lun;
1759 struct ctl_lun_req ireq;
1762 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1763 error = sysctl_handle_int(oidp, &value, 0, req);
1764 if ((error != 0) || (req->newptr == NULL))
1767 mtx_lock(&softc->ctl_lock);
1769 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1771 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1772 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1773 mtx_unlock(&softc->ctl_lock);
1774 bzero(&ireq, sizeof(ireq));
1775 ireq.reqtype = CTL_LUNREQ_MODIFY;
1776 ireq.reqdata.modify.lun_id = lun->lun;
1777 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1779 if (ireq.status != CTL_LUN_OK) {
1780 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1781 __func__, ireq.status, ireq.error_str);
1783 mtx_lock(&softc->ctl_lock);
1785 mtx_unlock(&softc->ctl_lock);
1792 struct make_dev_args args;
1793 struct ctl_softc *softc;
1797 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1800 make_dev_args_init(&args);
1801 args.mda_devsw = &ctl_cdevsw;
1802 args.mda_uid = UID_ROOT;
1803 args.mda_gid = GID_OPERATOR;
1804 args.mda_mode = 0600;
1805 args.mda_si_drv1 = softc;
1806 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1808 free(control_softc, M_DEVBUF);
1812 sysctl_ctx_init(&softc->sysctl_ctx);
1813 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1814 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1815 CTLFLAG_RD, 0, "CAM Target Layer");
1817 if (softc->sysctl_tree == NULL) {
1818 printf("%s: unable to allocate sysctl tree\n", __func__);
1819 destroy_dev(softc->dev);
1820 free(control_softc, M_DEVBUF);
1821 control_softc = NULL;
1825 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1826 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1827 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1830 TUNABLE_INT_FETCH("kern.cam.ctl.ha_mode", (int *)&softc->ha_mode);
1831 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1832 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1833 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1836 * In Copan's HA scheme, the "master" and "slave" roles are
1837 * figured out through the slot the controller is in. Although it
1838 * is an active/active system, someone has to be in charge.
1840 TUNABLE_INT_FETCH("kern.cam.ctl.ha_id", &softc->ha_id);
1841 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1842 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1843 "HA head ID (0 - no HA)");
1844 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1845 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1846 softc->is_single = 1;
1847 softc->port_cnt = CTL_MAX_PORTS;
1848 softc->port_min = 0;
1850 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1851 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1853 softc->port_max = softc->port_min + softc->port_cnt;
1854 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1855 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1857 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1858 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1859 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1861 STAILQ_INIT(&softc->lun_list);
1862 STAILQ_INIT(&softc->pending_lun_queue);
1863 STAILQ_INIT(&softc->fe_list);
1864 STAILQ_INIT(&softc->port_list);
1865 STAILQ_INIT(&softc->be_list);
1866 ctl_tpc_init(softc);
1868 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1871 printf("ctl: can't allocate %d entry other SC pool, "
1872 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1875 softc->othersc_pool = other_pool;
1877 if (worker_threads <= 0)
1878 worker_threads = max(1, mp_ncpus / 4);
1879 if (worker_threads > CTL_MAX_THREADS)
1880 worker_threads = CTL_MAX_THREADS;
1882 for (i = 0; i < worker_threads; i++) {
1883 struct ctl_thread *thr = &softc->threads[i];
1885 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1886 thr->ctl_softc = softc;
1887 STAILQ_INIT(&thr->incoming_queue);
1888 STAILQ_INIT(&thr->rtr_queue);
1889 STAILQ_INIT(&thr->done_queue);
1890 STAILQ_INIT(&thr->isc_queue);
1892 error = kproc_kthread_add(ctl_work_thread, thr,
1893 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1895 printf("error creating CTL work thread!\n");
1896 ctl_pool_free(other_pool);
1900 error = kproc_kthread_add(ctl_lun_thread, softc,
1901 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1903 printf("error creating CTL lun thread!\n");
1904 ctl_pool_free(other_pool);
1907 error = kproc_kthread_add(ctl_thresh_thread, softc,
1908 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1910 printf("error creating CTL threshold thread!\n");
1911 ctl_pool_free(other_pool);
1915 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1916 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1917 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1919 if (softc->is_single == 0) {
1920 ctl_frontend_register(&ha_frontend);
1921 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1922 printf("ctl_init: ctl_ha_msg_init failed.\n");
1923 softc->is_single = 1;
1925 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1926 != CTL_HA_STATUS_SUCCESS) {
1927 printf("ctl_init: ctl_ha_msg_register failed.\n");
1928 softc->is_single = 1;
1937 struct ctl_softc *softc = control_softc;
1938 struct ctl_lun *lun, *next_lun;
1940 if (softc->is_single == 0) {
1941 ctl_ha_msg_shutdown(softc);
1942 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1943 != CTL_HA_STATUS_SUCCESS)
1944 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1945 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1946 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1947 ctl_frontend_deregister(&ha_frontend);
1950 mtx_lock(&softc->ctl_lock);
1952 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun)
1955 mtx_unlock(&softc->ctl_lock);
1958 ctl_shutdown_thread(softc->work_thread);
1959 mtx_destroy(&softc->queue_lock);
1962 ctl_tpc_shutdown(softc);
1963 uma_zdestroy(softc->io_zone);
1964 mtx_destroy(&softc->ctl_lock);
1966 destroy_dev(softc->dev);
1968 sysctl_ctx_free(&softc->sysctl_ctx);
1970 free(control_softc, M_DEVBUF);
1971 control_softc = NULL;
1975 ctl_module_event_handler(module_t mod, int what, void *arg)
1980 return (ctl_init());
1984 return (EOPNOTSUPP);
1989 * XXX KDM should we do some access checks here? Bump a reference count to
1990 * prevent a CTL module from being unloaded while someone has it open?
1993 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1999 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2005 * Remove an initiator by port number and initiator ID.
2006 * Returns 0 for success, -1 for failure.
2009 ctl_remove_initiator(struct ctl_port *port, int iid)
2011 struct ctl_softc *softc = port->ctl_softc;
2013 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2015 if (iid > CTL_MAX_INIT_PER_PORT) {
2016 printf("%s: initiator ID %u > maximun %u!\n",
2017 __func__, iid, CTL_MAX_INIT_PER_PORT);
2021 mtx_lock(&softc->ctl_lock);
2022 port->wwpn_iid[iid].in_use--;
2023 port->wwpn_iid[iid].last_use = time_uptime;
2024 mtx_unlock(&softc->ctl_lock);
2025 ctl_isc_announce_iid(port, iid);
2031 * Add an initiator to the initiator map.
2032 * Returns iid for success, < 0 for failure.
2035 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2037 struct ctl_softc *softc = port->ctl_softc;
2041 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2043 if (iid >= CTL_MAX_INIT_PER_PORT) {
2044 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2045 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2050 mtx_lock(&softc->ctl_lock);
2052 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2053 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2054 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2058 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2059 strcmp(name, port->wwpn_iid[i].name) == 0) {
2067 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2068 if (port->wwpn_iid[i].in_use == 0 &&
2069 port->wwpn_iid[i].wwpn == 0 &&
2070 port->wwpn_iid[i].name == NULL) {
2079 best_time = INT32_MAX;
2080 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2081 if (port->wwpn_iid[i].in_use == 0) {
2082 if (port->wwpn_iid[i].last_use < best_time) {
2084 best_time = port->wwpn_iid[i].last_use;
2092 mtx_unlock(&softc->ctl_lock);
2097 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2099 * This is not an error yet.
2101 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2103 printf("%s: port %d iid %u WWPN %#jx arrived"
2104 " again\n", __func__, port->targ_port,
2105 iid, (uintmax_t)wwpn);
2109 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2110 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2112 printf("%s: port %d iid %u name '%s' arrived"
2113 " again\n", __func__, port->targ_port,
2120 * This is an error, but what do we do about it? The
2121 * driver is telling us we have a new WWPN for this
2122 * initiator ID, so we pretty much need to use it.
2124 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2125 " but WWPN %#jx '%s' is still at that address\n",
2126 __func__, port->targ_port, iid, wwpn, name,
2127 (uintmax_t)port->wwpn_iid[iid].wwpn,
2128 port->wwpn_iid[iid].name);
2131 * XXX KDM clear have_ca and ua_pending on each LUN for
2136 free(port->wwpn_iid[iid].name, M_CTL);
2137 port->wwpn_iid[iid].name = name;
2138 port->wwpn_iid[iid].wwpn = wwpn;
2139 port->wwpn_iid[iid].in_use++;
2140 mtx_unlock(&softc->ctl_lock);
2141 ctl_isc_announce_iid(port, iid);
2147 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2151 switch (port->port_type) {
2154 struct scsi_transportid_fcp *id =
2155 (struct scsi_transportid_fcp *)buf;
2156 if (port->wwpn_iid[iid].wwpn == 0)
2158 memset(id, 0, sizeof(*id));
2159 id->format_protocol = SCSI_PROTO_FC;
2160 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2161 return (sizeof(*id));
2163 case CTL_PORT_ISCSI:
2165 struct scsi_transportid_iscsi_port *id =
2166 (struct scsi_transportid_iscsi_port *)buf;
2167 if (port->wwpn_iid[iid].name == NULL)
2170 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2172 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2173 len = roundup2(min(len, 252), 4);
2174 scsi_ulto2b(len, id->additional_length);
2175 return (sizeof(*id) + len);
2179 struct scsi_transportid_sas *id =
2180 (struct scsi_transportid_sas *)buf;
2181 if (port->wwpn_iid[iid].wwpn == 0)
2183 memset(id, 0, sizeof(*id));
2184 id->format_protocol = SCSI_PROTO_SAS;
2185 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2186 return (sizeof(*id));
2190 struct scsi_transportid_spi *id =
2191 (struct scsi_transportid_spi *)buf;
2192 memset(id, 0, sizeof(*id));
2193 id->format_protocol = SCSI_PROTO_SPI;
2194 scsi_ulto2b(iid, id->scsi_addr);
2195 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2196 return (sizeof(*id));
2202 * Serialize a command that went down the "wrong" side, and so was sent to
2203 * this controller for execution. The logic is a little different than the
2204 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2205 * sent back to the other side, but in the success case, we execute the
2206 * command on this side (XFER mode) or tell the other side to execute it
2210 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2212 struct ctl_softc *softc = control_softc;
2213 union ctl_ha_msg msg_info;
2214 struct ctl_port *port;
2215 struct ctl_lun *lun;
2216 const struct ctl_cmd_entry *entry;
2219 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2220 mtx_lock(&softc->ctl_lock);
2222 /* Make sure that we know about this port. */
2223 port = ctl_io_port(&ctsio->io_hdr);
2224 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2225 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2230 /* Make sure that we know about this LUN. */
2231 if (targ_lun >= CTL_MAX_LUNS ||
2232 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2233 mtx_unlock(&softc->ctl_lock);
2236 * The other node would not send this request to us unless
2237 * received announce that we are primary node for this LUN.
2238 * If this LUN does not exist now, it is probably result of
2239 * a race, so respond to initiator in the most opaque way.
2241 ctl_set_busy(ctsio);
2244 mtx_lock(&lun->lun_lock);
2245 mtx_unlock(&softc->ctl_lock);
2248 * If the LUN is invalid, pretend that it doesn't exist.
2249 * It will go away as soon as all pending I/Os completed.
2251 if (lun->flags & CTL_LUN_DISABLED) {
2252 mtx_unlock(&lun->lun_lock);
2253 ctl_set_busy(ctsio);
2257 entry = ctl_get_cmd_entry(ctsio, NULL);
2258 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2259 mtx_unlock(&lun->lun_lock);
2263 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
2264 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
2267 * Every I/O goes into the OOA queue for a
2268 * particular LUN, and stays there until completion.
2271 if (TAILQ_EMPTY(&lun->ooa_queue))
2272 lun->idle_time += getsbinuptime() - lun->last_busy;
2274 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2276 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2277 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2279 case CTL_ACTION_BLOCK:
2280 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2281 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2283 mtx_unlock(&lun->lun_lock);
2285 case CTL_ACTION_PASS:
2286 case CTL_ACTION_SKIP:
2287 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2288 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2289 ctl_enqueue_rtr((union ctl_io *)ctsio);
2290 mtx_unlock(&lun->lun_lock);
2292 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2293 mtx_unlock(&lun->lun_lock);
2295 /* send msg back to other side */
2296 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2297 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2298 msg_info.hdr.msg_type = CTL_MSG_R2R;
2299 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2300 sizeof(msg_info.hdr), M_WAITOK);
2303 case CTL_ACTION_OVERLAP:
2304 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2305 mtx_unlock(&lun->lun_lock);
2306 ctl_set_overlapped_cmd(ctsio);
2308 case CTL_ACTION_OVERLAP_TAG:
2309 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2310 mtx_unlock(&lun->lun_lock);
2311 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2313 case CTL_ACTION_ERROR:
2315 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2316 mtx_unlock(&lun->lun_lock);
2318 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2321 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2322 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2323 msg_info.hdr.serializing_sc = NULL;
2324 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2325 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2326 sizeof(msg_info.scsi), M_WAITOK);
2327 ctl_free_io((union ctl_io *)ctsio);
2333 * Returns 0 for success, errno for failure.
2336 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2337 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2341 mtx_lock(&lun->lun_lock);
2342 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2343 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2345 struct ctl_ooa_entry *entry;
2348 * If we've got more than we can fit, just count the
2349 * remaining entries.
2351 if (*cur_fill_num >= ooa_hdr->alloc_num)
2354 entry = &kern_entries[*cur_fill_num];
2356 entry->tag_num = io->scsiio.tag_num;
2357 entry->lun_num = lun->lun;
2359 entry->start_bt = io->io_hdr.start_bt;
2361 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2362 entry->cdb_len = io->scsiio.cdb_len;
2363 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2364 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2366 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2367 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2369 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2370 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2372 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2373 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2375 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2376 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2378 mtx_unlock(&lun->lun_lock);
2382 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2383 size_t error_str_len)
2387 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2389 if (copyin(user_addr, kptr, len) != 0) {
2390 snprintf(error_str, error_str_len, "Error copying %d bytes "
2391 "from user address %p to kernel address %p", len,
2401 ctl_free_args(int num_args, struct ctl_be_arg *args)
2408 for (i = 0; i < num_args; i++) {
2409 free(args[i].kname, M_CTL);
2410 free(args[i].kvalue, M_CTL);
2416 static struct ctl_be_arg *
2417 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2418 char *error_str, size_t error_str_len)
2420 struct ctl_be_arg *args;
2423 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2424 error_str, error_str_len);
2429 for (i = 0; i < num_args; i++) {
2430 args[i].kname = NULL;
2431 args[i].kvalue = NULL;
2434 for (i = 0; i < num_args; i++) {
2437 args[i].kname = ctl_copyin_alloc(args[i].name,
2438 args[i].namelen, error_str, error_str_len);
2439 if (args[i].kname == NULL)
2442 if (args[i].kname[args[i].namelen - 1] != '\0') {
2443 snprintf(error_str, error_str_len, "Argument %d "
2444 "name is not NUL-terminated", i);
2448 if (args[i].flags & CTL_BEARG_RD) {
2449 tmpptr = ctl_copyin_alloc(args[i].value,
2450 args[i].vallen, error_str, error_str_len);
2453 if ((args[i].flags & CTL_BEARG_ASCII)
2454 && (tmpptr[args[i].vallen - 1] != '\0')) {
2455 snprintf(error_str, error_str_len, "Argument "
2456 "%d value is not NUL-terminated", i);
2457 free(tmpptr, M_CTL);
2460 args[i].kvalue = tmpptr;
2462 args[i].kvalue = malloc(args[i].vallen,
2463 M_CTL, M_WAITOK | M_ZERO);
2470 ctl_free_args(num_args, args);
2476 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2480 for (i = 0; i < num_args; i++) {
2481 if (args[i].flags & CTL_BEARG_WR)
2482 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2487 * Escape characters that are illegal or not recommended in XML.
2490 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2492 char *end = str + size;
2497 for (; *str && str < end; str++) {
2500 retval = sbuf_printf(sb, "&");
2503 retval = sbuf_printf(sb, ">");
2506 retval = sbuf_printf(sb, "<");
2509 retval = sbuf_putc(sb, *str);
2522 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2524 struct scsi_vpd_id_descriptor *desc;
2527 if (id == NULL || id->len < 4)
2529 desc = (struct scsi_vpd_id_descriptor *)id->data;
2530 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2531 case SVPD_ID_TYPE_T10:
2532 sbuf_printf(sb, "t10.");
2534 case SVPD_ID_TYPE_EUI64:
2535 sbuf_printf(sb, "eui.");
2537 case SVPD_ID_TYPE_NAA:
2538 sbuf_printf(sb, "naa.");
2540 case SVPD_ID_TYPE_SCSI_NAME:
2543 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2544 case SVPD_ID_CODESET_BINARY:
2545 for (i = 0; i < desc->length; i++)
2546 sbuf_printf(sb, "%02x", desc->identifier[i]);
2548 case SVPD_ID_CODESET_ASCII:
2549 sbuf_printf(sb, "%.*s", (int)desc->length,
2550 (char *)desc->identifier);
2552 case SVPD_ID_CODESET_UTF8:
2553 sbuf_printf(sb, "%s", (char *)desc->identifier);
2559 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2562 struct ctl_softc *softc = dev->si_drv1;
2563 struct ctl_lun *lun;
2570 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2572 case CTL_ENABLE_PORT:
2573 case CTL_DISABLE_PORT:
2574 case CTL_SET_PORT_WWNS: {
2575 struct ctl_port *port;
2576 struct ctl_port_entry *entry;
2578 entry = (struct ctl_port_entry *)addr;
2580 mtx_lock(&softc->ctl_lock);
2581 STAILQ_FOREACH(port, &softc->port_list, links) {
2584 if (port->targ_port < softc->port_min ||
2585 port->targ_port >= softc->port_max)
2590 if ((entry->port_type == CTL_PORT_NONE)
2591 && (entry->targ_port == port->targ_port)) {
2593 * If the user only wants to enable or
2594 * disable or set WWNs on a specific port,
2595 * do the operation and we're done.
2599 } else if (entry->port_type & port->port_type) {
2601 * Compare the user's type mask with the
2602 * particular frontend type to see if we
2609 * Make sure the user isn't trying to set
2610 * WWNs on multiple ports at the same time.
2612 if (cmd == CTL_SET_PORT_WWNS) {
2613 printf("%s: Can't set WWNs on "
2614 "multiple ports\n", __func__);
2623 * XXX KDM we have to drop the lock here, because
2624 * the online/offline operations can potentially
2625 * block. We need to reference count the frontends
2626 * so they can't go away,
2628 if (cmd == CTL_ENABLE_PORT) {
2629 mtx_unlock(&softc->ctl_lock);
2630 ctl_port_online(port);
2631 mtx_lock(&softc->ctl_lock);
2632 } else if (cmd == CTL_DISABLE_PORT) {
2633 mtx_unlock(&softc->ctl_lock);
2634 ctl_port_offline(port);
2635 mtx_lock(&softc->ctl_lock);
2636 } else if (cmd == CTL_SET_PORT_WWNS) {
2637 ctl_port_set_wwns(port,
2638 (entry->flags & CTL_PORT_WWNN_VALID) ?
2640 (entry->flags & CTL_PORT_WWPN_VALID) ?
2641 1 : 0, entry->wwpn);
2646 mtx_unlock(&softc->ctl_lock);
2650 struct ctl_ooa *ooa_hdr;
2651 struct ctl_ooa_entry *entries;
2652 uint32_t cur_fill_num;
2654 ooa_hdr = (struct ctl_ooa *)addr;
2656 if ((ooa_hdr->alloc_len == 0)
2657 || (ooa_hdr->alloc_num == 0)) {
2658 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2659 "must be non-zero\n", __func__,
2660 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2665 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2666 sizeof(struct ctl_ooa_entry))) {
2667 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2668 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2669 __func__, ooa_hdr->alloc_len,
2670 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2675 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2676 if (entries == NULL) {
2677 printf("%s: could not allocate %d bytes for OOA "
2678 "dump\n", __func__, ooa_hdr->alloc_len);
2683 mtx_lock(&softc->ctl_lock);
2684 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2685 (ooa_hdr->lun_num >= CTL_MAX_LUNS ||
2686 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2687 mtx_unlock(&softc->ctl_lock);
2688 free(entries, M_CTL);
2689 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2690 __func__, (uintmax_t)ooa_hdr->lun_num);
2697 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2698 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2699 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2703 lun = softc->ctl_luns[ooa_hdr->lun_num];
2704 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2707 mtx_unlock(&softc->ctl_lock);
2709 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2710 ooa_hdr->fill_len = ooa_hdr->fill_num *
2711 sizeof(struct ctl_ooa_entry);
2712 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2714 printf("%s: error copying out %d bytes for OOA dump\n",
2715 __func__, ooa_hdr->fill_len);
2718 getbinuptime(&ooa_hdr->cur_bt);
2720 if (cur_fill_num > ooa_hdr->alloc_num) {
2721 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2722 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2724 ooa_hdr->dropped_num = 0;
2725 ooa_hdr->status = CTL_OOA_OK;
2728 free(entries, M_CTL);
2731 case CTL_DELAY_IO: {
2732 struct ctl_io_delay_info *delay_info;
2734 delay_info = (struct ctl_io_delay_info *)addr;
2737 mtx_lock(&softc->ctl_lock);
2738 if (delay_info->lun_id >= CTL_MAX_LUNS ||
2739 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2740 mtx_unlock(&softc->ctl_lock);
2741 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2744 mtx_lock(&lun->lun_lock);
2745 mtx_unlock(&softc->ctl_lock);
2746 delay_info->status = CTL_DELAY_STATUS_OK;
2747 switch (delay_info->delay_type) {
2748 case CTL_DELAY_TYPE_CONT:
2749 case CTL_DELAY_TYPE_ONESHOT:
2752 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2755 switch (delay_info->delay_loc) {
2756 case CTL_DELAY_LOC_DATAMOVE:
2757 lun->delay_info.datamove_type = delay_info->delay_type;
2758 lun->delay_info.datamove_delay = delay_info->delay_secs;
2760 case CTL_DELAY_LOC_DONE:
2761 lun->delay_info.done_type = delay_info->delay_type;
2762 lun->delay_info.done_delay = delay_info->delay_secs;
2765 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2768 mtx_unlock(&lun->lun_lock);
2770 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2771 #endif /* CTL_IO_DELAY */
2774 case CTL_GETSTATS: {
2775 struct ctl_stats *stats;
2778 stats = (struct ctl_stats *)addr;
2780 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2782 stats->status = CTL_SS_NEED_MORE_SPACE;
2783 stats->num_luns = softc->num_luns;
2787 * XXX KDM no locking here. If the LUN list changes,
2788 * things can blow up.
2791 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2792 retval = copyout(&lun->stats, &stats->lun_stats[i++],
2793 sizeof(lun->stats));
2797 stats->num_luns = softc->num_luns;
2798 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2800 stats->status = CTL_SS_OK;
2802 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2804 stats->flags = CTL_STATS_FLAG_NONE;
2806 getnanouptime(&stats->timestamp);
2809 case CTL_ERROR_INJECT: {
2810 struct ctl_error_desc *err_desc, *new_err_desc;
2812 err_desc = (struct ctl_error_desc *)addr;
2814 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2816 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2818 mtx_lock(&softc->ctl_lock);
2819 if (err_desc->lun_id >= CTL_MAX_LUNS ||
2820 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2821 mtx_unlock(&softc->ctl_lock);
2822 free(new_err_desc, M_CTL);
2823 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2824 __func__, (uintmax_t)err_desc->lun_id);
2828 mtx_lock(&lun->lun_lock);
2829 mtx_unlock(&softc->ctl_lock);
2832 * We could do some checking here to verify the validity
2833 * of the request, but given the complexity of error
2834 * injection requests, the checking logic would be fairly
2837 * For now, if the request is invalid, it just won't get
2838 * executed and might get deleted.
2840 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2843 * XXX KDM check to make sure the serial number is unique,
2844 * in case we somehow manage to wrap. That shouldn't
2845 * happen for a very long time, but it's the right thing to
2848 new_err_desc->serial = lun->error_serial;
2849 err_desc->serial = lun->error_serial;
2850 lun->error_serial++;
2852 mtx_unlock(&lun->lun_lock);
2855 case CTL_ERROR_INJECT_DELETE: {
2856 struct ctl_error_desc *delete_desc, *desc, *desc2;
2859 delete_desc = (struct ctl_error_desc *)addr;
2862 mtx_lock(&softc->ctl_lock);
2863 if (delete_desc->lun_id >= CTL_MAX_LUNS ||
2864 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2865 mtx_unlock(&softc->ctl_lock);
2866 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2867 __func__, (uintmax_t)delete_desc->lun_id);
2871 mtx_lock(&lun->lun_lock);
2872 mtx_unlock(&softc->ctl_lock);
2873 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2874 if (desc->serial != delete_desc->serial)
2877 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2882 mtx_unlock(&lun->lun_lock);
2883 if (delete_done == 0) {
2884 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2885 "error serial %ju on LUN %u\n", __func__,
2886 delete_desc->serial, delete_desc->lun_id);
2892 case CTL_DUMP_STRUCTS: {
2894 struct ctl_port *port;
2895 struct ctl_frontend *fe;
2897 mtx_lock(&softc->ctl_lock);
2898 printf("CTL Persistent Reservation information start:\n");
2899 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2900 mtx_lock(&lun->lun_lock);
2901 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2902 mtx_unlock(&lun->lun_lock);
2906 for (j = 0; j < CTL_MAX_PORTS; j++) {
2907 if (lun->pr_keys[j] == NULL)
2909 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2910 if (lun->pr_keys[j][k] == 0)
2912 printf(" LUN %ju port %d iid %d key "
2913 "%#jx\n", lun->lun, j, k,
2914 (uintmax_t)lun->pr_keys[j][k]);
2917 mtx_unlock(&lun->lun_lock);
2919 printf("CTL Persistent Reservation information end\n");
2920 printf("CTL Ports:\n");
2921 STAILQ_FOREACH(port, &softc->port_list, links) {
2922 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2923 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2924 port->frontend->name, port->port_type,
2925 port->physical_port, port->virtual_port,
2926 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2927 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2928 if (port->wwpn_iid[j].in_use == 0 &&
2929 port->wwpn_iid[j].wwpn == 0 &&
2930 port->wwpn_iid[j].name == NULL)
2933 printf(" iid %u use %d WWPN %#jx '%s'\n",
2934 j, port->wwpn_iid[j].in_use,
2935 (uintmax_t)port->wwpn_iid[j].wwpn,
2936 port->wwpn_iid[j].name);
2939 printf("CTL Port information end\n");
2940 mtx_unlock(&softc->ctl_lock);
2942 * XXX KDM calling this without a lock. We'd likely want
2943 * to drop the lock before calling the frontend's dump
2946 printf("CTL Frontends:\n");
2947 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2948 printf(" Frontend '%s'\n", fe->name);
2949 if (fe->fe_dump != NULL)
2952 printf("CTL Frontend information end\n");
2956 struct ctl_lun_req *lun_req;
2957 struct ctl_backend_driver *backend;
2959 lun_req = (struct ctl_lun_req *)addr;
2961 backend = ctl_backend_find(lun_req->backend);
2962 if (backend == NULL) {
2963 lun_req->status = CTL_LUN_ERROR;
2964 snprintf(lun_req->error_str,
2965 sizeof(lun_req->error_str),
2966 "Backend \"%s\" not found.",
2970 if (lun_req->num_be_args > 0) {
2971 lun_req->kern_be_args = ctl_copyin_args(
2972 lun_req->num_be_args,
2975 sizeof(lun_req->error_str));
2976 if (lun_req->kern_be_args == NULL) {
2977 lun_req->status = CTL_LUN_ERROR;
2982 retval = backend->ioctl(dev, cmd, addr, flag, td);
2984 if (lun_req->num_be_args > 0) {
2985 ctl_copyout_args(lun_req->num_be_args,
2986 lun_req->kern_be_args);
2987 ctl_free_args(lun_req->num_be_args,
2988 lun_req->kern_be_args);
2992 case CTL_LUN_LIST: {
2994 struct ctl_lun_list *list;
2995 struct ctl_option *opt;
2997 list = (struct ctl_lun_list *)addr;
3000 * Allocate a fixed length sbuf here, based on the length
3001 * of the user's buffer. We could allocate an auto-extending
3002 * buffer, and then tell the user how much larger our
3003 * amount of data is than his buffer, but that presents
3006 * 1. The sbuf(9) routines use a blocking malloc, and so
3007 * we can't hold a lock while calling them with an
3008 * auto-extending buffer.
3010 * 2. There is not currently a LUN reference counting
3011 * mechanism, outside of outstanding transactions on
3012 * the LUN's OOA queue. So a LUN could go away on us
3013 * while we're getting the LUN number, backend-specific
3014 * information, etc. Thus, given the way things
3015 * currently work, we need to hold the CTL lock while
3016 * grabbing LUN information.
3018 * So, from the user's standpoint, the best thing to do is
3019 * allocate what he thinks is a reasonable buffer length,
3020 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3021 * double the buffer length and try again. (And repeat
3022 * that until he succeeds.)
3024 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3026 list->status = CTL_LUN_LIST_ERROR;
3027 snprintf(list->error_str, sizeof(list->error_str),
3028 "Unable to allocate %d bytes for LUN list",
3033 sbuf_printf(sb, "<ctllunlist>\n");
3035 mtx_lock(&softc->ctl_lock);
3036 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3037 mtx_lock(&lun->lun_lock);
3038 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3039 (uintmax_t)lun->lun);
3042 * Bail out as soon as we see that we've overfilled
3048 retval = sbuf_printf(sb, "\t<backend_type>%s"
3049 "</backend_type>\n",
3050 (lun->backend == NULL) ? "none" :
3051 lun->backend->name);
3056 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3057 lun->be_lun->lun_type);
3062 if (lun->backend == NULL) {
3063 retval = sbuf_printf(sb, "</lun>\n");
3069 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3070 (lun->be_lun->maxlba > 0) ?
3071 lun->be_lun->maxlba + 1 : 0);
3076 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3077 lun->be_lun->blocksize);
3082 retval = sbuf_printf(sb, "\t<serial_number>");
3087 retval = ctl_sbuf_printf_esc(sb,
3088 lun->be_lun->serial_num,
3089 sizeof(lun->be_lun->serial_num));
3094 retval = sbuf_printf(sb, "</serial_number>\n");
3099 retval = sbuf_printf(sb, "\t<device_id>");
3104 retval = ctl_sbuf_printf_esc(sb,
3105 lun->be_lun->device_id,
3106 sizeof(lun->be_lun->device_id));
3111 retval = sbuf_printf(sb, "</device_id>\n");
3116 if (lun->backend->lun_info != NULL) {
3117 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3121 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3122 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3123 opt->name, opt->value, opt->name);
3128 retval = sbuf_printf(sb, "</lun>\n");
3132 mtx_unlock(&lun->lun_lock);
3135 mtx_unlock(&lun->lun_lock);
3136 mtx_unlock(&softc->ctl_lock);
3139 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3142 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3143 snprintf(list->error_str, sizeof(list->error_str),
3144 "Out of space, %d bytes is too small",
3151 retval = copyout(sbuf_data(sb), list->lun_xml,
3154 list->fill_len = sbuf_len(sb) + 1;
3155 list->status = CTL_LUN_LIST_OK;
3160 struct ctl_iscsi *ci;
3161 struct ctl_frontend *fe;
3163 ci = (struct ctl_iscsi *)addr;
3165 fe = ctl_frontend_find("iscsi");
3167 ci->status = CTL_ISCSI_ERROR;
3168 snprintf(ci->error_str, sizeof(ci->error_str),
3169 "Frontend \"iscsi\" not found.");
3173 retval = fe->ioctl(dev, cmd, addr, flag, td);
3176 case CTL_PORT_REQ: {
3177 struct ctl_req *req;
3178 struct ctl_frontend *fe;
3180 req = (struct ctl_req *)addr;
3182 fe = ctl_frontend_find(req->driver);
3184 req->status = CTL_LUN_ERROR;
3185 snprintf(req->error_str, sizeof(req->error_str),
3186 "Frontend \"%s\" not found.", req->driver);
3189 if (req->num_args > 0) {
3190 req->kern_args = ctl_copyin_args(req->num_args,
3191 req->args, req->error_str, sizeof(req->error_str));
3192 if (req->kern_args == NULL) {
3193 req->status = CTL_LUN_ERROR;
3199 retval = fe->ioctl(dev, cmd, addr, flag, td);
3203 if (req->num_args > 0) {
3204 ctl_copyout_args(req->num_args, req->kern_args);
3205 ctl_free_args(req->num_args, req->kern_args);
3209 case CTL_PORT_LIST: {
3211 struct ctl_port *port;
3212 struct ctl_lun_list *list;
3213 struct ctl_option *opt;
3217 list = (struct ctl_lun_list *)addr;
3219 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3221 list->status = CTL_LUN_LIST_ERROR;
3222 snprintf(list->error_str, sizeof(list->error_str),
3223 "Unable to allocate %d bytes for LUN list",
3228 sbuf_printf(sb, "<ctlportlist>\n");
3230 mtx_lock(&softc->ctl_lock);
3231 STAILQ_FOREACH(port, &softc->port_list, links) {
3232 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3233 (uintmax_t)port->targ_port);
3236 * Bail out as soon as we see that we've overfilled
3242 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3243 "</frontend_type>\n", port->frontend->name);
3247 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3252 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3253 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3257 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3262 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3263 port->physical_port);
3267 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3268 port->virtual_port);
3272 if (port->target_devid != NULL) {
3273 sbuf_printf(sb, "\t<target>");
3274 ctl_id_sbuf(port->target_devid, sb);
3275 sbuf_printf(sb, "</target>\n");
3278 if (port->port_devid != NULL) {
3279 sbuf_printf(sb, "\t<port>");
3280 ctl_id_sbuf(port->port_devid, sb);
3281 sbuf_printf(sb, "</port>\n");
3284 if (port->port_info != NULL) {
3285 retval = port->port_info(port->onoff_arg, sb);
3289 STAILQ_FOREACH(opt, &port->options, links) {
3290 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3291 opt->name, opt->value, opt->name);
3296 if (port->lun_map != NULL) {
3297 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3298 for (j = 0; j < port->lun_map_size; j++) {
3299 plun = ctl_lun_map_from_port(port, j);
3300 if (plun == UINT32_MAX)
3303 "\t<lun id=\"%u\">%u</lun>\n",
3308 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3309 if (port->wwpn_iid[j].in_use == 0 ||
3310 (port->wwpn_iid[j].wwpn == 0 &&
3311 port->wwpn_iid[j].name == NULL))
3314 if (port->wwpn_iid[j].name != NULL)
3315 retval = sbuf_printf(sb,
3316 "\t<initiator id=\"%u\">%s</initiator>\n",
3317 j, port->wwpn_iid[j].name);
3319 retval = sbuf_printf(sb,
3320 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3321 j, port->wwpn_iid[j].wwpn);
3328 retval = sbuf_printf(sb, "</targ_port>\n");
3332 mtx_unlock(&softc->ctl_lock);
3335 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3338 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3339 snprintf(list->error_str, sizeof(list->error_str),
3340 "Out of space, %d bytes is too small",
3347 retval = copyout(sbuf_data(sb), list->lun_xml,
3350 list->fill_len = sbuf_len(sb) + 1;
3351 list->status = CTL_LUN_LIST_OK;
3356 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3357 struct ctl_port *port;
3359 mtx_lock(&softc->ctl_lock);
3360 if (lm->port < softc->port_min ||
3361 lm->port >= softc->port_max ||
3362 (port = softc->ctl_ports[lm->port]) == NULL) {
3363 mtx_unlock(&softc->ctl_lock);
3366 if (port->status & CTL_PORT_STATUS_ONLINE) {
3367 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3368 if (ctl_lun_map_to_port(port, lun->lun) ==
3371 mtx_lock(&lun->lun_lock);
3372 ctl_est_ua_port(lun, lm->port, -1,
3374 mtx_unlock(&lun->lun_lock);
3377 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3378 if (lm->plun != UINT32_MAX) {
3379 if (lm->lun == UINT32_MAX)
3380 retval = ctl_lun_map_unset(port, lm->plun);
3381 else if (lm->lun < CTL_MAX_LUNS &&
3382 softc->ctl_luns[lm->lun] != NULL)
3383 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3387 if (lm->lun == UINT32_MAX)
3388 retval = ctl_lun_map_deinit(port);
3390 retval = ctl_lun_map_init(port);
3392 if (port->status & CTL_PORT_STATUS_ONLINE)
3393 ctl_isc_announce_port(port);
3397 /* XXX KDM should we fix this? */
3399 struct ctl_backend_driver *backend;
3406 * We encode the backend type as the ioctl type for backend
3407 * ioctls. So parse it out here, and then search for a
3408 * backend of this type.
3410 type = _IOC_TYPE(cmd);
3412 STAILQ_FOREACH(backend, &softc->be_list, links) {
3413 if (backend->type == type) {
3419 printf("ctl: unknown ioctl command %#lx or backend "
3424 retval = backend->ioctl(dev, cmd, addr, flag, td);
3434 ctl_get_initindex(struct ctl_nexus *nexus)
3436 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3440 ctl_lun_map_init(struct ctl_port *port)
3442 struct ctl_softc *softc = port->ctl_softc;
3443 struct ctl_lun *lun;
3444 int size = ctl_lun_map_size;
3447 if (port->lun_map == NULL || port->lun_map_size < size) {
3448 port->lun_map_size = 0;
3449 free(port->lun_map, M_CTL);
3450 port->lun_map = malloc(size * sizeof(uint32_t),
3453 if (port->lun_map == NULL)
3455 for (i = 0; i < size; i++)
3456 port->lun_map[i] = UINT32_MAX;
3457 port->lun_map_size = size;
3458 if (port->status & CTL_PORT_STATUS_ONLINE) {
3459 if (port->lun_disable != NULL) {
3460 STAILQ_FOREACH(lun, &softc->lun_list, links)
3461 port->lun_disable(port->targ_lun_arg, lun->lun);
3463 ctl_isc_announce_port(port);
3469 ctl_lun_map_deinit(struct ctl_port *port)
3471 struct ctl_softc *softc = port->ctl_softc;
3472 struct ctl_lun *lun;
3474 if (port->lun_map == NULL)
3476 port->lun_map_size = 0;
3477 free(port->lun_map, M_CTL);
3478 port->lun_map = NULL;
3479 if (port->status & CTL_PORT_STATUS_ONLINE) {
3480 if (port->lun_enable != NULL) {
3481 STAILQ_FOREACH(lun, &softc->lun_list, links)
3482 port->lun_enable(port->targ_lun_arg, lun->lun);
3484 ctl_isc_announce_port(port);
3490 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3495 if (port->lun_map == NULL) {
3496 status = ctl_lun_map_init(port);
3500 if (plun >= port->lun_map_size)
3502 old = port->lun_map[plun];
3503 port->lun_map[plun] = glun;
3504 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3505 if (port->lun_enable != NULL)
3506 port->lun_enable(port->targ_lun_arg, plun);
3507 ctl_isc_announce_port(port);
3513 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3517 if (port->lun_map == NULL || plun >= port->lun_map_size)
3519 old = port->lun_map[plun];
3520 port->lun_map[plun] = UINT32_MAX;
3521 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3522 if (port->lun_disable != NULL)
3523 port->lun_disable(port->targ_lun_arg, plun);
3524 ctl_isc_announce_port(port);
3530 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3534 return (UINT32_MAX);
3535 if (port->lun_map == NULL)
3537 if (lun_id > port->lun_map_size)
3538 return (UINT32_MAX);
3539 return (port->lun_map[lun_id]);
3543 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3548 return (UINT32_MAX);
3549 if (port->lun_map == NULL)
3551 for (i = 0; i < port->lun_map_size; i++) {
3552 if (port->lun_map[i] == lun_id)
3555 return (UINT32_MAX);
3559 ctl_decode_lun(uint64_t encoded)
3562 uint32_t result = 0xffffffff;
3564 be64enc(lun, encoded);
3565 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3566 case RPL_LUNDATA_ATYP_PERIPH:
3567 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3568 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3571 case RPL_LUNDATA_ATYP_FLAT:
3572 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3573 lun[6] == 0 && lun[7] == 0)
3574 result = ((lun[0] & 0x3f) << 8) + lun[1];
3576 case RPL_LUNDATA_ATYP_EXTLUN:
3577 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3579 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3584 result = (lun[1] << 16) + (lun[2] << 8) +
3588 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3589 result = (lun[2] << 24) +
3590 (lun[3] << 16) + (lun[4] << 8) +
3595 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3596 result = 0xffffffff;
3605 ctl_encode_lun(uint32_t decoded)
3607 uint64_t l = decoded;
3610 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3612 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3614 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3616 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3619 static struct ctl_port *
3620 ctl_io_port(struct ctl_io_hdr *io_hdr)
3623 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3627 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3631 for (i = first; i < last; i++) {
3632 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3639 ctl_set_mask(uint32_t *mask, uint32_t bit)
3641 uint32_t chunk, piece;
3644 piece = bit % (sizeof(uint32_t) * 8);
3646 if ((mask[chunk] & (1 << piece)) != 0)
3649 mask[chunk] |= (1 << piece);
3655 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3657 uint32_t chunk, piece;
3660 piece = bit % (sizeof(uint32_t) * 8);
3662 if ((mask[chunk] & (1 << piece)) == 0)
3665 mask[chunk] &= ~(1 << piece);
3671 ctl_is_set(uint32_t *mask, uint32_t bit)
3673 uint32_t chunk, piece;
3676 piece = bit % (sizeof(uint32_t) * 8);
3678 if ((mask[chunk] & (1 << piece)) == 0)
3685 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3689 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3692 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3696 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3700 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3703 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3707 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3712 i = residx/CTL_MAX_INIT_PER_PORT;
3713 if (lun->pr_keys[i] != NULL)
3715 mtx_unlock(&lun->lun_lock);
3716 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3718 mtx_lock(&lun->lun_lock);
3719 if (lun->pr_keys[i] == NULL)
3720 lun->pr_keys[i] = p;
3726 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3730 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3731 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3732 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3736 * ctl_softc, pool_name, total_ctl_io are passed in.
3737 * npool is passed out.
3740 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3741 uint32_t total_ctl_io, void **npool)
3744 struct ctl_io_pool *pool;
3746 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3751 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3752 pool->ctl_softc = ctl_softc;
3753 pool->zone = uma_zsecond_create(pool->name, NULL,
3754 NULL, NULL, NULL, ctl_softc->io_zone);
3755 /* uma_prealloc(pool->zone, total_ctl_io); */
3759 *npool = ctl_softc->io_zone;
3765 ctl_pool_free(struct ctl_io_pool *pool)
3772 uma_zdestroy(pool->zone);
3778 ctl_alloc_io(void *pool_ref)
3782 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3784 io = uma_zalloc(pool->zone, M_WAITOK);
3786 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3789 io->io_hdr.pool = pool_ref;
3794 ctl_alloc_io_nowait(void *pool_ref)
3798 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3800 io = uma_zalloc(pool->zone, M_NOWAIT);
3802 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3805 io->io_hdr.pool = pool_ref;
3810 ctl_free_io(union ctl_io *io)
3813 struct ctl_io_pool *pool;
3820 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3821 uma_zfree(pool->zone, io);
3823 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3828 ctl_zero_io(union ctl_io *io)
3836 * May need to preserve linked list pointers at some point too.
3838 pool_ref = io->io_hdr.pool;
3839 memset(io, 0, sizeof(*io));
3840 io->io_hdr.pool = pool_ref;
3844 ctl_expand_number(const char *buf, uint64_t *num)
3850 number = strtoq(buf, &endptr, 0);
3852 switch (tolower((unsigned char)*endptr)) {
3872 case '\0': /* No unit. */
3876 /* Unrecognized unit. */
3880 if ((number << shift) >> shift != number) {
3884 *num = number << shift;
3890 * This routine could be used in the future to load default and/or saved
3891 * mode page parameters for a particuar lun.
3894 ctl_init_page_index(struct ctl_lun *lun)
3897 struct ctl_page_index *page_index;
3901 memcpy(&lun->mode_pages.index, page_index_template,
3902 sizeof(page_index_template));
3904 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3906 page_index = &lun->mode_pages.index[i];
3907 if (lun->be_lun->lun_type == T_DIRECT &&
3908 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3910 if (lun->be_lun->lun_type == T_PROCESSOR &&
3911 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3913 if (lun->be_lun->lun_type == T_CDROM &&
3914 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3917 page_code = page_index->page_code & SMPH_PC_MASK;
3918 switch (page_code) {
3919 case SMS_RW_ERROR_RECOVERY_PAGE: {
3920 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3921 ("subpage %#x for page %#x is incorrect!",
3922 page_index->subpage, page_code));
3923 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3924 &rw_er_page_default,
3925 sizeof(rw_er_page_default));
3926 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3927 &rw_er_page_changeable,
3928 sizeof(rw_er_page_changeable));
3929 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3930 &rw_er_page_default,
3931 sizeof(rw_er_page_default));
3932 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3933 &rw_er_page_default,
3934 sizeof(rw_er_page_default));
3935 page_index->page_data =
3936 (uint8_t *)lun->mode_pages.rw_er_page;
3939 case SMS_FORMAT_DEVICE_PAGE: {
3940 struct scsi_format_page *format_page;
3942 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3943 ("subpage %#x for page %#x is incorrect!",
3944 page_index->subpage, page_code));
3947 * Sectors per track are set above. Bytes per
3948 * sector need to be set here on a per-LUN basis.
3950 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3951 &format_page_default,
3952 sizeof(format_page_default));
3953 memcpy(&lun->mode_pages.format_page[
3954 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3955 sizeof(format_page_changeable));
3956 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3957 &format_page_default,
3958 sizeof(format_page_default));
3959 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3960 &format_page_default,
3961 sizeof(format_page_default));
3963 format_page = &lun->mode_pages.format_page[
3965 scsi_ulto2b(lun->be_lun->blocksize,
3966 format_page->bytes_per_sector);
3968 format_page = &lun->mode_pages.format_page[
3970 scsi_ulto2b(lun->be_lun->blocksize,
3971 format_page->bytes_per_sector);
3973 format_page = &lun->mode_pages.format_page[
3975 scsi_ulto2b(lun->be_lun->blocksize,
3976 format_page->bytes_per_sector);
3978 page_index->page_data =
3979 (uint8_t *)lun->mode_pages.format_page;
3982 case SMS_RIGID_DISK_PAGE: {
3983 struct scsi_rigid_disk_page *rigid_disk_page;
3984 uint32_t sectors_per_cylinder;
3988 #endif /* !__XSCALE__ */
3990 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3991 ("subpage %#x for page %#x is incorrect!",
3992 page_index->subpage, page_code));
3995 * Rotation rate and sectors per track are set
3996 * above. We calculate the cylinders here based on
3997 * capacity. Due to the number of heads and
3998 * sectors per track we're using, smaller arrays
3999 * may turn out to have 0 cylinders. Linux and
4000 * FreeBSD don't pay attention to these mode pages
4001 * to figure out capacity, but Solaris does. It
4002 * seems to deal with 0 cylinders just fine, and
4003 * works out a fake geometry based on the capacity.
4005 memcpy(&lun->mode_pages.rigid_disk_page[
4006 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4007 sizeof(rigid_disk_page_default));
4008 memcpy(&lun->mode_pages.rigid_disk_page[
4009 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4010 sizeof(rigid_disk_page_changeable));
4012 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4016 * The divide method here will be more accurate,
4017 * probably, but results in floating point being
4018 * used in the kernel on i386 (__udivdi3()). On the
4019 * XScale, though, __udivdi3() is implemented in
4022 * The shift method for cylinder calculation is
4023 * accurate if sectors_per_cylinder is a power of
4024 * 2. Otherwise it might be slightly off -- you
4025 * might have a bit of a truncation problem.
4028 cylinders = (lun->be_lun->maxlba + 1) /
4029 sectors_per_cylinder;
4031 for (shift = 31; shift > 0; shift--) {
4032 if (sectors_per_cylinder & (1 << shift))
4035 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4039 * We've basically got 3 bytes, or 24 bits for the
4040 * cylinder size in the mode page. If we're over,
4041 * just round down to 2^24.
4043 if (cylinders > 0xffffff)
4044 cylinders = 0xffffff;
4046 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4048 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4050 if ((value = ctl_get_opt(&lun->be_lun->options,
4052 scsi_ulto2b(strtol(value, NULL, 0),
4053 rigid_disk_page->rotation_rate);
4056 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4057 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4058 sizeof(rigid_disk_page_default));
4059 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4060 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4061 sizeof(rigid_disk_page_default));
4063 page_index->page_data =
4064 (uint8_t *)lun->mode_pages.rigid_disk_page;
4067 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4068 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4069 ("subpage %#x for page %#x is incorrect!",
4070 page_index->subpage, page_code));
4071 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4072 &verify_er_page_default,
4073 sizeof(verify_er_page_default));
4074 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4075 &verify_er_page_changeable,
4076 sizeof(verify_er_page_changeable));
4077 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4078 &verify_er_page_default,
4079 sizeof(verify_er_page_default));
4080 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4081 &verify_er_page_default,
4082 sizeof(verify_er_page_default));
4083 page_index->page_data =
4084 (uint8_t *)lun->mode_pages.verify_er_page;
4087 case SMS_CACHING_PAGE: {
4088 struct scsi_caching_page *caching_page;
4090 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4091 ("subpage %#x for page %#x is incorrect!",
4092 page_index->subpage, page_code));
4093 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4094 &caching_page_default,
4095 sizeof(caching_page_default));
4096 memcpy(&lun->mode_pages.caching_page[
4097 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4098 sizeof(caching_page_changeable));
4099 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4100 &caching_page_default,
4101 sizeof(caching_page_default));
4102 caching_page = &lun->mode_pages.caching_page[
4104 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4105 if (value != NULL && strcmp(value, "off") == 0)
4106 caching_page->flags1 &= ~SCP_WCE;
4107 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4108 if (value != NULL && strcmp(value, "off") == 0)
4109 caching_page->flags1 |= SCP_RCD;
4110 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4111 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4112 sizeof(caching_page_default));
4113 page_index->page_data =
4114 (uint8_t *)lun->mode_pages.caching_page;
4117 case SMS_CONTROL_MODE_PAGE: {
4118 switch (page_index->subpage) {
4119 case SMS_SUBPAGE_PAGE_0: {
4120 struct scsi_control_page *control_page;
4122 memcpy(&lun->mode_pages.control_page[
4124 &control_page_default,
4125 sizeof(control_page_default));
4126 memcpy(&lun->mode_pages.control_page[
4127 CTL_PAGE_CHANGEABLE],
4128 &control_page_changeable,
4129 sizeof(control_page_changeable));
4130 memcpy(&lun->mode_pages.control_page[
4132 &control_page_default,
4133 sizeof(control_page_default));
4134 control_page = &lun->mode_pages.control_page[
4136 value = ctl_get_opt(&lun->be_lun->options,
4138 if (value != NULL &&
4139 strcmp(value, "unrestricted") == 0) {
4140 control_page->queue_flags &=
4141 ~SCP_QUEUE_ALG_MASK;
4142 control_page->queue_flags |=
4143 SCP_QUEUE_ALG_UNRESTRICTED;
4145 memcpy(&lun->mode_pages.control_page[
4147 &lun->mode_pages.control_page[
4149 sizeof(control_page_default));
4150 page_index->page_data =
4151 (uint8_t *)lun->mode_pages.control_page;
4155 memcpy(&lun->mode_pages.control_ext_page[
4157 &control_ext_page_default,
4158 sizeof(control_ext_page_default));
4159 memcpy(&lun->mode_pages.control_ext_page[
4160 CTL_PAGE_CHANGEABLE],
4161 &control_ext_page_changeable,
4162 sizeof(control_ext_page_changeable));
4163 memcpy(&lun->mode_pages.control_ext_page[
4165 &control_ext_page_default,
4166 sizeof(control_ext_page_default));
4167 memcpy(&lun->mode_pages.control_ext_page[
4169 &lun->mode_pages.control_ext_page[
4171 sizeof(control_ext_page_default));
4172 page_index->page_data =
4173 (uint8_t *)lun->mode_pages.control_ext_page;
4176 panic("subpage %#x for page %#x is incorrect!",
4177 page_index->subpage, page_code);
4181 case SMS_INFO_EXCEPTIONS_PAGE: {
4182 switch (page_index->subpage) {
4183 case SMS_SUBPAGE_PAGE_0:
4184 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4186 sizeof(ie_page_default));
4187 memcpy(&lun->mode_pages.ie_page[
4188 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4189 sizeof(ie_page_changeable));
4190 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4192 sizeof(ie_page_default));
4193 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4195 sizeof(ie_page_default));
4196 page_index->page_data =
4197 (uint8_t *)lun->mode_pages.ie_page;
4200 struct ctl_logical_block_provisioning_page *page;
4202 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4204 sizeof(lbp_page_default));
4205 memcpy(&lun->mode_pages.lbp_page[
4206 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4207 sizeof(lbp_page_changeable));
4208 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4210 sizeof(lbp_page_default));
4211 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4212 value = ctl_get_opt(&lun->be_lun->options,
4214 if (value != NULL &&
4215 ctl_expand_number(value, &ival) == 0) {
4216 page->descr[0].flags |= SLBPPD_ENABLED |
4218 if (lun->be_lun->blocksize)
4219 ival /= lun->be_lun->blocksize;
4222 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4223 page->descr[0].count);
4225 value = ctl_get_opt(&lun->be_lun->options,
4227 if (value != NULL &&
4228 ctl_expand_number(value, &ival) == 0) {
4229 page->descr[1].flags |= SLBPPD_ENABLED |
4231 if (lun->be_lun->blocksize)
4232 ival /= lun->be_lun->blocksize;
4235 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4236 page->descr[1].count);
4238 value = ctl_get_opt(&lun->be_lun->options,
4239 "pool-avail-threshold");
4240 if (value != NULL &&
4241 ctl_expand_number(value, &ival) == 0) {
4242 page->descr[2].flags |= SLBPPD_ENABLED |
4244 if (lun->be_lun->blocksize)
4245 ival /= lun->be_lun->blocksize;
4248 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4249 page->descr[2].count);
4251 value = ctl_get_opt(&lun->be_lun->options,
4252 "pool-used-threshold");
4253 if (value != NULL &&
4254 ctl_expand_number(value, &ival) == 0) {
4255 page->descr[3].flags |= SLBPPD_ENABLED |
4257 if (lun->be_lun->blocksize)
4258 ival /= lun->be_lun->blocksize;
4261 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4262 page->descr[3].count);
4264 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4265 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4266 sizeof(lbp_page_default));
4267 page_index->page_data =
4268 (uint8_t *)lun->mode_pages.lbp_page;
4272 panic("subpage %#x for page %#x is incorrect!",
4273 page_index->subpage, page_code);
4277 case SMS_CDDVD_CAPS_PAGE:{
4278 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4279 ("subpage %#x for page %#x is incorrect!",
4280 page_index->subpage, page_code));
4281 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4282 &cddvd_page_default,
4283 sizeof(cddvd_page_default));
4284 memcpy(&lun->mode_pages.cddvd_page[
4285 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4286 sizeof(cddvd_page_changeable));
4287 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4288 &cddvd_page_default,
4289 sizeof(cddvd_page_default));
4290 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4291 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4292 sizeof(cddvd_page_default));
4293 page_index->page_data =
4294 (uint8_t *)lun->mode_pages.cddvd_page;
4298 panic("invalid page code value %#x", page_code);
4302 return (CTL_RETVAL_COMPLETE);
4306 ctl_init_log_page_index(struct ctl_lun *lun)
4308 struct ctl_page_index *page_index;
4311 memcpy(&lun->log_pages.index, log_page_index_template,
4312 sizeof(log_page_index_template));
4315 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4317 page_index = &lun->log_pages.index[i];
4318 if (lun->be_lun->lun_type == T_DIRECT &&
4319 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4321 if (lun->be_lun->lun_type == T_PROCESSOR &&
4322 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4324 if (lun->be_lun->lun_type == T_CDROM &&
4325 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4328 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4329 lun->backend->lun_attr == NULL)
4332 if (page_index->page_code != prev) {
4333 lun->log_pages.pages_page[j] = page_index->page_code;
4334 prev = page_index->page_code;
4337 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4338 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4341 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4342 lun->log_pages.index[0].page_len = j;
4343 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4344 lun->log_pages.index[1].page_len = k * 2;
4345 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4346 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4347 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4348 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4349 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4350 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4352 return (CTL_RETVAL_COMPLETE);
4356 hex2bin(const char *str, uint8_t *buf, int buf_size)
4361 memset(buf, 0, buf_size);
4362 while (isspace(str[0]))
4364 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4367 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4368 while (str[i] == '-') /* Skip dashes in UUIDs. */
4373 else if (isalpha(c))
4374 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4380 buf[i / 2] |= (c << 4);
4384 return ((i + 1) / 2);
4391 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4392 * wants us to allocate the LUN and he can block.
4393 * - ctl_softc is always set
4394 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4396 * Returns 0 for success, non-zero (errno) for failure.
4399 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4400 struct ctl_be_lun *const be_lun)
4402 struct ctl_lun *nlun, *lun;
4403 struct scsi_vpd_id_descriptor *desc;
4404 struct scsi_vpd_id_t10 *t10id;
4405 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4406 int lun_number, i, lun_malloced;
4407 int devidlen, idlen1, idlen2 = 0, len;
4413 * We currently only support Direct Access or Processor LUN types.
4415 switch (be_lun->lun_type) {
4423 be_lun->lun_config_status(be_lun->be_lun,
4424 CTL_LUN_CONFIG_FAILURE);
4427 if (ctl_lun == NULL) {
4428 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4435 memset(lun, 0, sizeof(*lun));
4437 lun->flags = CTL_LUN_MALLOCED;
4439 /* Generate LUN ID. */
4440 devidlen = max(CTL_DEVID_MIN_LEN,
4441 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4442 idlen1 = sizeof(*t10id) + devidlen;
4443 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4444 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4445 if (scsiname != NULL) {
4446 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4447 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4449 eui = ctl_get_opt(&be_lun->options, "eui");
4451 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4453 naa = ctl_get_opt(&be_lun->options, "naa");
4455 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4457 uuid = ctl_get_opt(&be_lun->options, "uuid");
4459 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4461 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4462 M_CTL, M_WAITOK | M_ZERO);
4463 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4464 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4465 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4466 desc->length = idlen1;
4467 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4468 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4469 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4470 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4472 strncpy(t10id->vendor, vendor,
4473 min(sizeof(t10id->vendor), strlen(vendor)));
4475 strncpy((char *)t10id->vendor_spec_id,
4476 (char *)be_lun->device_id, devidlen);
4477 if (scsiname != NULL) {
4478 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4480 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4481 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4482 SVPD_ID_TYPE_SCSI_NAME;
4483 desc->length = idlen2;
4484 strlcpy(desc->identifier, scsiname, idlen2);
4487 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4489 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4490 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4492 desc->length = hex2bin(eui, desc->identifier, 16);
4493 desc->length = desc->length > 12 ? 16 :
4494 (desc->length > 8 ? 12 : 8);
4495 len -= 16 - desc->length;
4498 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4500 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4501 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4503 desc->length = hex2bin(naa, desc->identifier, 16);
4504 desc->length = desc->length > 8 ? 16 : 8;
4505 len -= 16 - desc->length;
4508 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4510 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4511 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4513 desc->identifier[0] = 0x10;
4514 hex2bin(uuid, &desc->identifier[2], 16);
4517 lun->lun_devid->len = len;
4519 mtx_lock(&ctl_softc->ctl_lock);
4521 * See if the caller requested a particular LUN number. If so, see
4522 * if it is available. Otherwise, allocate the first available LUN.
4524 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4525 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4526 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4527 mtx_unlock(&ctl_softc->ctl_lock);
4528 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4529 printf("ctl: requested LUN ID %d is higher "
4530 "than CTL_MAX_LUNS - 1 (%d)\n",
4531 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4534 * XXX KDM return an error, or just assign
4535 * another LUN ID in this case??
4537 printf("ctl: requested LUN ID %d is already "
4538 "in use\n", be_lun->req_lun_id);
4540 if (lun->flags & CTL_LUN_MALLOCED)
4542 be_lun->lun_config_status(be_lun->be_lun,
4543 CTL_LUN_CONFIG_FAILURE);
4546 lun_number = be_lun->req_lun_id;
4548 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4549 if (lun_number == -1) {
4550 mtx_unlock(&ctl_softc->ctl_lock);
4551 printf("ctl: can't allocate LUN, out of LUNs\n");
4552 if (lun->flags & CTL_LUN_MALLOCED)
4554 be_lun->lun_config_status(be_lun->be_lun,
4555 CTL_LUN_CONFIG_FAILURE);
4559 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4561 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4562 lun->lun = lun_number;
4563 lun->be_lun = be_lun;
4565 * The processor LUN is always enabled. Disk LUNs come on line
4566 * disabled, and must be enabled by the backend.
4568 lun->flags |= CTL_LUN_DISABLED;
4569 lun->backend = be_lun->be;
4570 be_lun->ctl_lun = lun;
4571 be_lun->lun_id = lun_number;
4572 atomic_add_int(&be_lun->be->num_luns, 1);
4573 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4574 lun->flags |= CTL_LUN_EJECTED;
4575 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4576 lun->flags |= CTL_LUN_NO_MEDIA;
4577 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4578 lun->flags |= CTL_LUN_STOPPED;
4580 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4581 lun->flags |= CTL_LUN_PRIMARY_SC;
4583 value = ctl_get_opt(&be_lun->options, "removable");
4584 if (value != NULL) {
4585 if (strcmp(value, "on") == 0)
4586 lun->flags |= CTL_LUN_REMOVABLE;
4587 } else if (be_lun->lun_type == T_CDROM)
4588 lun->flags |= CTL_LUN_REMOVABLE;
4590 lun->ctl_softc = ctl_softc;
4592 lun->last_busy = getsbinuptime();
4594 TAILQ_INIT(&lun->ooa_queue);
4595 TAILQ_INIT(&lun->blocked_queue);
4596 STAILQ_INIT(&lun->error_list);
4597 lun->ie_reported = 1;
4598 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4599 ctl_tpc_lun_init(lun);
4602 * Initialize the mode and log page index.
4604 ctl_init_page_index(lun);
4605 ctl_init_log_page_index(lun);
4608 * Now, before we insert this lun on the lun list, set the lun
4609 * inventory changed UA for all other luns.
4611 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4612 mtx_lock(&nlun->lun_lock);
4613 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4614 mtx_unlock(&nlun->lun_lock);
4617 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4619 ctl_softc->ctl_luns[lun_number] = lun;
4621 ctl_softc->num_luns++;
4623 /* Setup statistics gathering */
4624 lun->stats.device_type = be_lun->lun_type;
4625 lun->stats.lun_number = lun_number;
4626 lun->stats.blocksize = be_lun->blocksize;
4627 if (be_lun->blocksize == 0)
4628 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4629 for (i = 0;i < CTL_MAX_PORTS;i++)
4630 lun->stats.ports[i].targ_port = i;
4632 mtx_unlock(&ctl_softc->ctl_lock);
4634 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4641 * - LUN has already been marked invalid and any pending I/O has been taken
4645 ctl_free_lun(struct ctl_lun *lun)
4647 struct ctl_softc *softc;
4648 struct ctl_lun *nlun;
4651 softc = lun->ctl_softc;
4653 mtx_assert(&softc->ctl_lock, MA_OWNED);
4655 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4657 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4659 softc->ctl_luns[lun->lun] = NULL;
4661 if (!TAILQ_EMPTY(&lun->ooa_queue))
4662 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4667 * Tell the backend to free resources, if this LUN has a backend.
4669 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4670 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4672 lun->ie_reportcnt = UINT32_MAX;
4673 callout_drain(&lun->ie_callout);
4675 ctl_tpc_lun_shutdown(lun);
4676 mtx_destroy(&lun->lun_lock);
4677 free(lun->lun_devid, M_CTL);
4678 for (i = 0; i < CTL_MAX_PORTS; i++)
4679 free(lun->pending_ua[i], M_CTL);
4680 for (i = 0; i < CTL_MAX_PORTS; i++)
4681 free(lun->pr_keys[i], M_CTL);
4682 free(lun->write_buffer, M_CTL);
4683 if (lun->flags & CTL_LUN_MALLOCED)
4686 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4687 mtx_lock(&nlun->lun_lock);
4688 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4689 mtx_unlock(&nlun->lun_lock);
4696 ctl_create_lun(struct ctl_be_lun *be_lun)
4700 * ctl_alloc_lun() should handle all potential failure cases.
4702 ctl_alloc_lun(control_softc, NULL, be_lun);
4706 ctl_add_lun(struct ctl_be_lun *be_lun)
4708 struct ctl_softc *softc = control_softc;
4710 mtx_lock(&softc->ctl_lock);
4711 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4712 mtx_unlock(&softc->ctl_lock);
4713 wakeup(&softc->pending_lun_queue);
4719 ctl_enable_lun(struct ctl_be_lun *be_lun)
4721 struct ctl_softc *softc;
4722 struct ctl_port *port, *nport;
4723 struct ctl_lun *lun;
4726 lun = (struct ctl_lun *)be_lun->ctl_lun;
4727 softc = lun->ctl_softc;
4729 mtx_lock(&softc->ctl_lock);
4730 mtx_lock(&lun->lun_lock);
4731 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4733 * eh? Why did we get called if the LUN is already
4736 mtx_unlock(&lun->lun_lock);
4737 mtx_unlock(&softc->ctl_lock);
4740 lun->flags &= ~CTL_LUN_DISABLED;
4741 mtx_unlock(&lun->lun_lock);
4743 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4744 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4745 port->lun_map != NULL || port->lun_enable == NULL)
4749 * Drop the lock while we call the FETD's enable routine.
4750 * This can lead to a callback into CTL (at least in the
4751 * case of the internal initiator frontend.
4753 mtx_unlock(&softc->ctl_lock);
4754 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4755 mtx_lock(&softc->ctl_lock);
4757 printf("%s: FETD %s port %d returned error "
4758 "%d for lun_enable on lun %jd\n",
4759 __func__, port->port_name, port->targ_port,
4760 retval, (intmax_t)lun->lun);
4764 mtx_unlock(&softc->ctl_lock);
4765 ctl_isc_announce_lun(lun);
4771 ctl_disable_lun(struct ctl_be_lun *be_lun)
4773 struct ctl_softc *softc;
4774 struct ctl_port *port;
4775 struct ctl_lun *lun;
4778 lun = (struct ctl_lun *)be_lun->ctl_lun;
4779 softc = lun->ctl_softc;
4781 mtx_lock(&softc->ctl_lock);
4782 mtx_lock(&lun->lun_lock);
4783 if (lun->flags & CTL_LUN_DISABLED) {
4784 mtx_unlock(&lun->lun_lock);
4785 mtx_unlock(&softc->ctl_lock);
4788 lun->flags |= CTL_LUN_DISABLED;
4789 mtx_unlock(&lun->lun_lock);
4791 STAILQ_FOREACH(port, &softc->port_list, links) {
4792 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4793 port->lun_map != NULL || port->lun_disable == NULL)
4797 * Drop the lock before we call the frontend's disable
4798 * routine, to avoid lock order reversals.
4800 * XXX KDM what happens if the frontend list changes while
4801 * we're traversing it? It's unlikely, but should be handled.
4803 mtx_unlock(&softc->ctl_lock);
4804 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4805 mtx_lock(&softc->ctl_lock);
4807 printf("%s: FETD %s port %d returned error "
4808 "%d for lun_disable on lun %jd\n",
4809 __func__, port->port_name, port->targ_port,
4810 retval, (intmax_t)lun->lun);
4814 mtx_unlock(&softc->ctl_lock);
4815 ctl_isc_announce_lun(lun);
4821 ctl_start_lun(struct ctl_be_lun *be_lun)
4823 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4825 mtx_lock(&lun->lun_lock);
4826 lun->flags &= ~CTL_LUN_STOPPED;
4827 mtx_unlock(&lun->lun_lock);
4832 ctl_stop_lun(struct ctl_be_lun *be_lun)
4834 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4836 mtx_lock(&lun->lun_lock);
4837 lun->flags |= CTL_LUN_STOPPED;
4838 mtx_unlock(&lun->lun_lock);
4843 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4845 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4847 mtx_lock(&lun->lun_lock);
4848 lun->flags |= CTL_LUN_NO_MEDIA;
4849 mtx_unlock(&lun->lun_lock);
4854 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4856 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4857 union ctl_ha_msg msg;
4859 mtx_lock(&lun->lun_lock);
4860 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4861 if (lun->flags & CTL_LUN_REMOVABLE)
4862 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4863 mtx_unlock(&lun->lun_lock);
4864 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4865 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4866 bzero(&msg.ua, sizeof(msg.ua));
4867 msg.hdr.msg_type = CTL_MSG_UA;
4868 msg.hdr.nexus.initid = -1;
4869 msg.hdr.nexus.targ_port = -1;
4870 msg.hdr.nexus.targ_lun = lun->lun;
4871 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4874 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4875 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4882 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4884 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4886 mtx_lock(&lun->lun_lock);
4887 lun->flags |= CTL_LUN_EJECTED;
4888 mtx_unlock(&lun->lun_lock);
4893 ctl_lun_primary(struct ctl_be_lun *be_lun)
4895 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4897 mtx_lock(&lun->lun_lock);
4898 lun->flags |= CTL_LUN_PRIMARY_SC;
4899 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4900 mtx_unlock(&lun->lun_lock);
4901 ctl_isc_announce_lun(lun);
4906 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4908 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4910 mtx_lock(&lun->lun_lock);
4911 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4912 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4913 mtx_unlock(&lun->lun_lock);
4914 ctl_isc_announce_lun(lun);
4919 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4921 struct ctl_softc *softc;
4922 struct ctl_lun *lun;
4924 lun = (struct ctl_lun *)be_lun->ctl_lun;
4925 softc = lun->ctl_softc;
4927 mtx_lock(&lun->lun_lock);
4930 * The LUN needs to be disabled before it can be marked invalid.
4932 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4933 mtx_unlock(&lun->lun_lock);
4937 * Mark the LUN invalid.
4939 lun->flags |= CTL_LUN_INVALID;
4942 * If there is nothing in the OOA queue, go ahead and free the LUN.
4943 * If we have something in the OOA queue, we'll free it when the
4944 * last I/O completes.
4946 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4947 mtx_unlock(&lun->lun_lock);
4948 mtx_lock(&softc->ctl_lock);
4950 mtx_unlock(&softc->ctl_lock);
4952 mtx_unlock(&lun->lun_lock);
4958 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4960 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4961 union ctl_ha_msg msg;
4963 mtx_lock(&lun->lun_lock);
4964 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
4965 mtx_unlock(&lun->lun_lock);
4966 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4967 /* Send msg to other side. */
4968 bzero(&msg.ua, sizeof(msg.ua));
4969 msg.hdr.msg_type = CTL_MSG_UA;
4970 msg.hdr.nexus.initid = -1;
4971 msg.hdr.nexus.targ_port = -1;
4972 msg.hdr.nexus.targ_lun = lun->lun;
4973 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4976 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
4977 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4983 * Backend "memory move is complete" callback for requests that never
4984 * make it down to say RAIDCore's configuration code.
4987 ctl_config_move_done(union ctl_io *io)
4991 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4992 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4993 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4995 if ((io->io_hdr.port_status != 0) &&
4996 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4997 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4999 * For hardware error sense keys, the sense key
5000 * specific value is defined to be a retry count,
5001 * but we use it to pass back an internal FETD
5002 * error code. XXX KDM Hopefully the FETD is only
5003 * using 16 bits for an error code, since that's
5004 * all the space we have in the sks field.
5006 ctl_set_internal_failure(&io->scsiio,
5009 io->io_hdr.port_status);
5012 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5014 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5015 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5016 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5017 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5019 * XXX KDM just assuming a single pointer here, and not a
5020 * S/G list. If we start using S/G lists for config data,
5021 * we'll need to know how to clean them up here as well.
5023 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5024 free(io->scsiio.kern_data_ptr, M_CTL);
5026 retval = CTL_RETVAL_COMPLETE;
5029 * XXX KDM now we need to continue data movement. Some
5031 * - call ctl_scsiio() again? We don't do this for data
5032 * writes, because for those at least we know ahead of
5033 * time where the write will go and how long it is. For
5034 * config writes, though, that information is largely
5035 * contained within the write itself, thus we need to
5036 * parse out the data again.
5038 * - Call some other function once the data is in?
5042 * XXX KDM call ctl_scsiio() again for now, and check flag
5043 * bits to see whether we're allocated or not.
5045 retval = ctl_scsiio(&io->scsiio);
5051 * This gets called by a backend driver when it is done with a
5052 * data_submit method.
5055 ctl_data_submit_done(union ctl_io *io)
5058 * If the IO_CONT flag is set, we need to call the supplied
5059 * function to continue processing the I/O, instead of completing
5062 * If there is an error, though, we don't want to keep processing.
5063 * Instead, just send status back to the initiator.
5065 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5066 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5067 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5068 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5069 io->scsiio.io_cont(io);
5076 * This gets called by a backend driver when it is done with a
5077 * configuration write.
5080 ctl_config_write_done(union ctl_io *io)
5085 * If the IO_CONT flag is set, we need to call the supplied
5086 * function to continue processing the I/O, instead of completing
5089 * If there is an error, though, we don't want to keep processing.
5090 * Instead, just send status back to the initiator.
5092 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5093 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5094 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5095 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5096 io->scsiio.io_cont(io);
5100 * Since a configuration write can be done for commands that actually
5101 * have data allocated, like write buffer, and commands that have
5102 * no data, like start/stop unit, we need to check here.
5104 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5105 buf = io->scsiio.kern_data_ptr;
5114 ctl_config_read_done(union ctl_io *io)
5119 * If there is some error -- we are done, skip data transfer.
5121 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5122 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5123 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5124 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5125 buf = io->scsiio.kern_data_ptr;
5135 * If the IO_CONT flag is set, we need to call the supplied
5136 * function to continue processing the I/O, instead of completing
5139 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5140 io->scsiio.io_cont(io);
5148 * SCSI release command.
5151 ctl_scsi_release(struct ctl_scsiio *ctsio)
5153 struct ctl_lun *lun;
5156 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5158 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5159 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5162 * XXX KDM right now, we only support LUN reservation. We don't
5163 * support 3rd party reservations, or extent reservations, which
5164 * might actually need the parameter list. If we've gotten this
5165 * far, we've got a LUN reservation. Anything else got kicked out
5166 * above. So, according to SPC, ignore the length.
5169 mtx_lock(&lun->lun_lock);
5172 * According to SPC, it is not an error for an intiator to attempt
5173 * to release a reservation on a LUN that isn't reserved, or that
5174 * is reserved by another initiator. The reservation can only be
5175 * released, though, by the initiator who made it or by one of
5176 * several reset type events.
5178 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5179 lun->flags &= ~CTL_LUN_RESERVED;
5181 mtx_unlock(&lun->lun_lock);
5183 ctl_set_success(ctsio);
5184 ctl_done((union ctl_io *)ctsio);
5185 return (CTL_RETVAL_COMPLETE);
5189 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5191 struct ctl_lun *lun;
5194 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5196 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5197 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5200 * XXX KDM right now, we only support LUN reservation. We don't
5201 * support 3rd party reservations, or extent reservations, which
5202 * might actually need the parameter list. If we've gotten this
5203 * far, we've got a LUN reservation. Anything else got kicked out
5204 * above. So, according to SPC, ignore the length.
5207 mtx_lock(&lun->lun_lock);
5208 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5209 ctl_set_reservation_conflict(ctsio);
5213 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5214 if (lun->flags & CTL_LUN_PR_RESERVED) {
5215 ctl_set_success(ctsio);
5219 lun->flags |= CTL_LUN_RESERVED;
5220 lun->res_idx = residx;
5221 ctl_set_success(ctsio);
5224 mtx_unlock(&lun->lun_lock);
5225 ctl_done((union ctl_io *)ctsio);
5226 return (CTL_RETVAL_COMPLETE);
5230 ctl_start_stop(struct ctl_scsiio *ctsio)
5232 struct scsi_start_stop_unit *cdb;
5233 struct ctl_lun *lun;
5236 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5238 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5239 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5241 if ((cdb->how & SSS_PC_MASK) == 0) {
5242 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5243 (cdb->how & SSS_START) == 0) {
5246 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5247 if (ctl_get_prkey(lun, residx) == 0 ||
5248 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5250 ctl_set_reservation_conflict(ctsio);
5251 ctl_done((union ctl_io *)ctsio);
5252 return (CTL_RETVAL_COMPLETE);
5256 if ((cdb->how & SSS_LOEJ) &&
5257 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5258 ctl_set_invalid_field(ctsio,
5264 ctl_done((union ctl_io *)ctsio);
5265 return (CTL_RETVAL_COMPLETE);
5268 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5269 lun->prevent_count > 0) {
5270 /* "Medium removal prevented" */
5271 ctl_set_sense(ctsio, /*current_error*/ 1,
5272 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5273 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5274 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5275 ctl_done((union ctl_io *)ctsio);
5276 return (CTL_RETVAL_COMPLETE);
5280 retval = lun->backend->config_write((union ctl_io *)ctsio);
5285 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5287 struct ctl_lun *lun;
5288 struct scsi_prevent *cdb;
5292 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5294 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5295 cdb = (struct scsi_prevent *)ctsio->cdb;
5297 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) {
5298 ctl_set_invalid_opcode(ctsio);
5299 ctl_done((union ctl_io *)ctsio);
5300 return (CTL_RETVAL_COMPLETE);
5303 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5304 mtx_lock(&lun->lun_lock);
5305 if ((cdb->how & PR_PREVENT) &&
5306 ctl_is_set(lun->prevent, initidx) == 0) {
5307 ctl_set_mask(lun->prevent, initidx);
5308 lun->prevent_count++;
5309 } else if ((cdb->how & PR_PREVENT) == 0 &&
5310 ctl_is_set(lun->prevent, initidx)) {
5311 ctl_clear_mask(lun->prevent, initidx);
5312 lun->prevent_count--;
5314 mtx_unlock(&lun->lun_lock);
5315 retval = lun->backend->config_write((union ctl_io *)ctsio);
5320 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5321 * we don't really do anything with the LBA and length fields if the user
5322 * passes them in. Instead we'll just flush out the cache for the entire
5326 ctl_sync_cache(struct ctl_scsiio *ctsio)
5328 struct ctl_lun *lun;
5329 struct ctl_softc *softc;
5330 struct ctl_lba_len_flags *lbalen;
5331 uint64_t starting_lba;
5332 uint32_t block_count;
5336 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5338 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5339 softc = lun->ctl_softc;
5342 switch (ctsio->cdb[0]) {
5343 case SYNCHRONIZE_CACHE: {
5344 struct scsi_sync_cache *cdb;
5345 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5347 starting_lba = scsi_4btoul(cdb->begin_lba);
5348 block_count = scsi_2btoul(cdb->lb_count);
5352 case SYNCHRONIZE_CACHE_16: {
5353 struct scsi_sync_cache_16 *cdb;
5354 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5356 starting_lba = scsi_8btou64(cdb->begin_lba);
5357 block_count = scsi_4btoul(cdb->lb_count);
5362 ctl_set_invalid_opcode(ctsio);
5363 ctl_done((union ctl_io *)ctsio);
5365 break; /* NOTREACHED */
5369 * We check the LBA and length, but don't do anything with them.
5370 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5371 * get flushed. This check will just help satisfy anyone who wants
5372 * to see an error for an out of range LBA.
5374 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5375 ctl_set_lba_out_of_range(ctsio,
5376 MAX(starting_lba, lun->be_lun->maxlba + 1));
5377 ctl_done((union ctl_io *)ctsio);
5381 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5382 lbalen->lba = starting_lba;
5383 lbalen->len = block_count;
5384 lbalen->flags = byte2;
5385 retval = lun->backend->config_write((union ctl_io *)ctsio);
5392 ctl_format(struct ctl_scsiio *ctsio)
5394 struct scsi_format *cdb;
5395 struct ctl_lun *lun;
5396 int length, defect_list_len;
5398 CTL_DEBUG_PRINT(("ctl_format\n"));
5400 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5402 cdb = (struct scsi_format *)ctsio->cdb;
5405 if (cdb->byte2 & SF_FMTDATA) {
5406 if (cdb->byte2 & SF_LONGLIST)
5407 length = sizeof(struct scsi_format_header_long);
5409 length = sizeof(struct scsi_format_header_short);
5412 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5414 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5415 ctsio->kern_data_len = length;
5416 ctsio->kern_total_len = length;
5417 ctsio->kern_data_resid = 0;
5418 ctsio->kern_rel_offset = 0;
5419 ctsio->kern_sg_entries = 0;
5420 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5421 ctsio->be_move_done = ctl_config_move_done;
5422 ctl_datamove((union ctl_io *)ctsio);
5424 return (CTL_RETVAL_COMPLETE);
5427 defect_list_len = 0;
5429 if (cdb->byte2 & SF_FMTDATA) {
5430 if (cdb->byte2 & SF_LONGLIST) {
5431 struct scsi_format_header_long *header;
5433 header = (struct scsi_format_header_long *)
5434 ctsio->kern_data_ptr;
5436 defect_list_len = scsi_4btoul(header->defect_list_len);
5437 if (defect_list_len != 0) {
5438 ctl_set_invalid_field(ctsio,
5447 struct scsi_format_header_short *header;
5449 header = (struct scsi_format_header_short *)
5450 ctsio->kern_data_ptr;
5452 defect_list_len = scsi_2btoul(header->defect_list_len);
5453 if (defect_list_len != 0) {
5454 ctl_set_invalid_field(ctsio,
5465 ctl_set_success(ctsio);
5468 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5469 free(ctsio->kern_data_ptr, M_CTL);
5470 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5473 ctl_done((union ctl_io *)ctsio);
5474 return (CTL_RETVAL_COMPLETE);
5478 ctl_read_buffer(struct ctl_scsiio *ctsio)
5480 struct ctl_lun *lun;
5481 uint64_t buffer_offset;
5484 static uint8_t descr[4];
5485 static uint8_t echo_descr[4] = { 0 };
5487 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5488 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5489 switch (ctsio->cdb[0]) {
5491 struct scsi_read_buffer *cdb;
5493 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5494 buffer_offset = scsi_3btoul(cdb->offset);
5495 len = scsi_3btoul(cdb->length);
5499 case READ_BUFFER_16: {
5500 struct scsi_read_buffer_16 *cdb;
5502 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5503 buffer_offset = scsi_8btou64(cdb->offset);
5504 len = scsi_4btoul(cdb->length);
5508 default: /* This shouldn't happen. */
5509 ctl_set_invalid_opcode(ctsio);
5510 ctl_done((union ctl_io *)ctsio);
5511 return (CTL_RETVAL_COMPLETE);
5514 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5515 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5516 ctl_set_invalid_field(ctsio,
5522 ctl_done((union ctl_io *)ctsio);
5523 return (CTL_RETVAL_COMPLETE);
5526 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5528 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5529 ctsio->kern_data_ptr = descr;
5530 len = min(len, sizeof(descr));
5531 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5532 ctsio->kern_data_ptr = echo_descr;
5533 len = min(len, sizeof(echo_descr));
5535 if (lun->write_buffer == NULL) {
5536 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5539 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5541 ctsio->kern_data_len = len;
5542 ctsio->kern_total_len = len;
5543 ctsio->kern_data_resid = 0;
5544 ctsio->kern_rel_offset = 0;
5545 ctsio->kern_sg_entries = 0;
5546 ctl_set_success(ctsio);
5547 ctsio->be_move_done = ctl_config_move_done;
5548 ctl_datamove((union ctl_io *)ctsio);
5549 return (CTL_RETVAL_COMPLETE);
5553 ctl_write_buffer(struct ctl_scsiio *ctsio)
5555 struct scsi_write_buffer *cdb;
5556 struct ctl_lun *lun;
5557 int buffer_offset, len;
5559 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5561 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5562 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5564 len = scsi_3btoul(cdb->length);
5565 buffer_offset = scsi_3btoul(cdb->offset);
5567 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5568 ctl_set_invalid_field(ctsio,
5574 ctl_done((union ctl_io *)ctsio);
5575 return (CTL_RETVAL_COMPLETE);
5579 * If we've got a kernel request that hasn't been malloced yet,
5580 * malloc it and tell the caller the data buffer is here.
5582 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5583 if (lun->write_buffer == NULL) {
5584 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5587 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5588 ctsio->kern_data_len = len;
5589 ctsio->kern_total_len = len;
5590 ctsio->kern_data_resid = 0;
5591 ctsio->kern_rel_offset = 0;
5592 ctsio->kern_sg_entries = 0;
5593 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5594 ctsio->be_move_done = ctl_config_move_done;
5595 ctl_datamove((union ctl_io *)ctsio);
5597 return (CTL_RETVAL_COMPLETE);
5600 ctl_set_success(ctsio);
5601 ctl_done((union ctl_io *)ctsio);
5602 return (CTL_RETVAL_COMPLETE);
5606 ctl_write_same(struct ctl_scsiio *ctsio)
5608 struct ctl_lun *lun;
5609 struct ctl_lba_len_flags *lbalen;
5611 uint32_t num_blocks;
5615 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5617 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5619 switch (ctsio->cdb[0]) {
5620 case WRITE_SAME_10: {
5621 struct scsi_write_same_10 *cdb;
5623 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5625 lba = scsi_4btoul(cdb->addr);
5626 num_blocks = scsi_2btoul(cdb->length);
5630 case WRITE_SAME_16: {
5631 struct scsi_write_same_16 *cdb;
5633 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5635 lba = scsi_8btou64(cdb->addr);
5636 num_blocks = scsi_4btoul(cdb->length);
5642 * We got a command we don't support. This shouldn't
5643 * happen, commands should be filtered out above us.
5645 ctl_set_invalid_opcode(ctsio);
5646 ctl_done((union ctl_io *)ctsio);
5648 return (CTL_RETVAL_COMPLETE);
5649 break; /* NOTREACHED */
5652 /* ANCHOR flag can be used only together with UNMAP */
5653 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5654 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5655 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5656 ctl_done((union ctl_io *)ctsio);
5657 return (CTL_RETVAL_COMPLETE);
5661 * The first check is to make sure we're in bounds, the second
5662 * check is to catch wrap-around problems. If the lba + num blocks
5663 * is less than the lba, then we've wrapped around and the block
5664 * range is invalid anyway.
5666 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5667 || ((lba + num_blocks) < lba)) {
5668 ctl_set_lba_out_of_range(ctsio,
5669 MAX(lba, lun->be_lun->maxlba + 1));
5670 ctl_done((union ctl_io *)ctsio);
5671 return (CTL_RETVAL_COMPLETE);
5674 /* Zero number of blocks means "to the last logical block" */
5675 if (num_blocks == 0) {
5676 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5677 ctl_set_invalid_field(ctsio,
5683 ctl_done((union ctl_io *)ctsio);
5684 return (CTL_RETVAL_COMPLETE);
5686 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5689 len = lun->be_lun->blocksize;
5692 * If we've got a kernel request that hasn't been malloced yet,
5693 * malloc it and tell the caller the data buffer is here.
5695 if ((byte2 & SWS_NDOB) == 0 &&
5696 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5697 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5698 ctsio->kern_data_len = len;
5699 ctsio->kern_total_len = len;
5700 ctsio->kern_data_resid = 0;
5701 ctsio->kern_rel_offset = 0;
5702 ctsio->kern_sg_entries = 0;
5703 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5704 ctsio->be_move_done = ctl_config_move_done;
5705 ctl_datamove((union ctl_io *)ctsio);
5707 return (CTL_RETVAL_COMPLETE);
5710 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5712 lbalen->len = num_blocks;
5713 lbalen->flags = byte2;
5714 retval = lun->backend->config_write((union ctl_io *)ctsio);
5720 ctl_unmap(struct ctl_scsiio *ctsio)
5722 struct ctl_lun *lun;
5723 struct scsi_unmap *cdb;
5724 struct ctl_ptr_len_flags *ptrlen;
5725 struct scsi_unmap_header *hdr;
5726 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5728 uint32_t num_blocks;
5732 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5734 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5735 cdb = (struct scsi_unmap *)ctsio->cdb;
5737 len = scsi_2btoul(cdb->length);
5741 * If we've got a kernel request that hasn't been malloced yet,
5742 * malloc it and tell the caller the data buffer is here.
5744 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5745 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5746 ctsio->kern_data_len = len;
5747 ctsio->kern_total_len = len;
5748 ctsio->kern_data_resid = 0;
5749 ctsio->kern_rel_offset = 0;
5750 ctsio->kern_sg_entries = 0;
5751 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5752 ctsio->be_move_done = ctl_config_move_done;
5753 ctl_datamove((union ctl_io *)ctsio);
5755 return (CTL_RETVAL_COMPLETE);
5758 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5759 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5760 if (len < sizeof (*hdr) ||
5761 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5762 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5763 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5764 ctl_set_invalid_field(ctsio,
5772 len = scsi_2btoul(hdr->desc_length);
5773 buf = (struct scsi_unmap_desc *)(hdr + 1);
5774 end = buf + len / sizeof(*buf);
5777 for (range = buf; range < end; range++) {
5778 lba = scsi_8btou64(range->lba);
5779 num_blocks = scsi_4btoul(range->length);
5780 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5781 || ((lba + num_blocks) < lba)) {
5782 ctl_set_lba_out_of_range(ctsio,
5783 MAX(lba, lun->be_lun->maxlba + 1));
5784 ctl_done((union ctl_io *)ctsio);
5785 return (CTL_RETVAL_COMPLETE);
5787 if (num_blocks != 0)
5792 * Block backend can not handle zero last range.
5793 * Filter it out and return if there is nothing left.
5795 len = (uint8_t *)endnz - (uint8_t *)buf;
5797 ctl_set_success(ctsio);
5801 mtx_lock(&lun->lun_lock);
5802 ptrlen = (struct ctl_ptr_len_flags *)
5803 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5804 ptrlen->ptr = (void *)buf;
5806 ptrlen->flags = byte2;
5807 ctl_check_blocked(lun);
5808 mtx_unlock(&lun->lun_lock);
5810 retval = lun->backend->config_write((union ctl_io *)ctsio);
5814 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5815 free(ctsio->kern_data_ptr, M_CTL);
5816 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5818 ctl_done((union ctl_io *)ctsio);
5819 return (CTL_RETVAL_COMPLETE);
5823 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5824 struct ctl_page_index *page_index, uint8_t *page_ptr)
5826 struct ctl_lun *lun;
5827 uint8_t *current_cp, *saved_cp;
5831 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5832 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5835 current_cp = (page_index->page_data + (page_index->page_len *
5837 saved_cp = (page_index->page_data + (page_index->page_len *
5840 mtx_lock(&lun->lun_lock);
5841 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5842 memcpy(current_cp, page_ptr, page_index->page_len);
5843 memcpy(saved_cp, page_ptr, page_index->page_len);
5847 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5848 mtx_unlock(&lun->lun_lock);
5850 ctl_isc_announce_mode(lun,
5851 ctl_get_initindex(&ctsio->io_hdr.nexus),
5852 page_index->page_code, page_index->subpage);
5854 return (CTL_RETVAL_COMPLETE);
5858 ctl_ie_timer(void *arg)
5860 struct ctl_lun *lun = arg;
5863 if (lun->ie_asc == 0)
5866 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5867 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5869 lun->ie_reported = 0;
5871 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5872 lun->ie_reportcnt++;
5873 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5874 if (t == 0 || t == UINT32_MAX)
5875 t = 3000; /* 5 min */
5876 callout_schedule(&lun->ie_callout, t * hz / 10);
5881 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5882 struct ctl_page_index *page_index, uint8_t *page_ptr)
5884 struct scsi_info_exceptions_page *pg;
5885 struct ctl_lun *lun;
5888 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5890 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5891 pg = (struct scsi_info_exceptions_page *)page_ptr;
5892 mtx_lock(&lun->lun_lock);
5893 if (pg->info_flags & SIEP_FLAGS_TEST) {
5895 lun->ie_ascq = 0xff;
5896 if (pg->mrie == SIEP_MRIE_UA) {
5897 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5898 lun->ie_reported = 1;
5900 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5901 lun->ie_reported = -1;
5903 lun->ie_reportcnt = 1;
5904 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5905 lun->ie_reportcnt++;
5906 t = scsi_4btoul(pg->interval_timer);
5907 if (t == 0 || t == UINT32_MAX)
5908 t = 3000; /* 5 min */
5909 callout_reset(&lun->ie_callout, t * hz / 10,
5915 lun->ie_reported = 1;
5916 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5917 lun->ie_reportcnt = UINT32_MAX;
5918 callout_stop(&lun->ie_callout);
5920 mtx_unlock(&lun->lun_lock);
5921 return (CTL_RETVAL_COMPLETE);
5925 ctl_do_mode_select(union ctl_io *io)
5927 struct scsi_mode_page_header *page_header;
5928 struct ctl_page_index *page_index;
5929 struct ctl_scsiio *ctsio;
5930 int page_len, page_len_offset, page_len_size;
5931 union ctl_modepage_info *modepage_info;
5932 struct ctl_lun *lun;
5933 int *len_left, *len_used;
5936 ctsio = &io->scsiio;
5939 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5941 modepage_info = (union ctl_modepage_info *)
5942 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5943 len_left = &modepage_info->header.len_left;
5944 len_used = &modepage_info->header.len_used;
5948 page_header = (struct scsi_mode_page_header *)
5949 (ctsio->kern_data_ptr + *len_used);
5951 if (*len_left == 0) {
5952 free(ctsio->kern_data_ptr, M_CTL);
5953 ctl_set_success(ctsio);
5954 ctl_done((union ctl_io *)ctsio);
5955 return (CTL_RETVAL_COMPLETE);
5956 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5958 free(ctsio->kern_data_ptr, M_CTL);
5959 ctl_set_param_len_error(ctsio);
5960 ctl_done((union ctl_io *)ctsio);
5961 return (CTL_RETVAL_COMPLETE);
5963 } else if ((page_header->page_code & SMPH_SPF)
5964 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5966 free(ctsio->kern_data_ptr, M_CTL);
5967 ctl_set_param_len_error(ctsio);
5968 ctl_done((union ctl_io *)ctsio);
5969 return (CTL_RETVAL_COMPLETE);
5974 * XXX KDM should we do something with the block descriptor?
5976 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5977 page_index = &lun->mode_pages.index[i];
5978 if (lun->be_lun->lun_type == T_DIRECT &&
5979 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
5981 if (lun->be_lun->lun_type == T_PROCESSOR &&
5982 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
5984 if (lun->be_lun->lun_type == T_CDROM &&
5985 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
5988 if ((page_index->page_code & SMPH_PC_MASK) !=
5989 (page_header->page_code & SMPH_PC_MASK))
5993 * If neither page has a subpage code, then we've got a
5996 if (((page_index->page_code & SMPH_SPF) == 0)
5997 && ((page_header->page_code & SMPH_SPF) == 0)) {
5998 page_len = page_header->page_length;
6003 * If both pages have subpages, then the subpage numbers
6006 if ((page_index->page_code & SMPH_SPF)
6007 && (page_header->page_code & SMPH_SPF)) {
6008 struct scsi_mode_page_header_sp *sph;
6010 sph = (struct scsi_mode_page_header_sp *)page_header;
6011 if (page_index->subpage == sph->subpage) {
6012 page_len = scsi_2btoul(sph->page_length);
6019 * If we couldn't find the page, or if we don't have a mode select
6020 * handler for it, send back an error to the user.
6022 if ((i >= CTL_NUM_MODE_PAGES)
6023 || (page_index->select_handler == NULL)) {
6024 ctl_set_invalid_field(ctsio,
6027 /*field*/ *len_used,
6030 free(ctsio->kern_data_ptr, M_CTL);
6031 ctl_done((union ctl_io *)ctsio);
6032 return (CTL_RETVAL_COMPLETE);
6035 if (page_index->page_code & SMPH_SPF) {
6036 page_len_offset = 2;
6040 page_len_offset = 1;
6044 * If the length the initiator gives us isn't the one we specify in
6045 * the mode page header, or if they didn't specify enough data in
6046 * the CDB to avoid truncating this page, kick out the request.
6048 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6049 ctl_set_invalid_field(ctsio,
6052 /*field*/ *len_used + page_len_offset,
6055 free(ctsio->kern_data_ptr, M_CTL);
6056 ctl_done((union ctl_io *)ctsio);
6057 return (CTL_RETVAL_COMPLETE);
6059 if (*len_left < page_index->page_len) {
6060 free(ctsio->kern_data_ptr, M_CTL);
6061 ctl_set_param_len_error(ctsio);
6062 ctl_done((union ctl_io *)ctsio);
6063 return (CTL_RETVAL_COMPLETE);
6067 * Run through the mode page, checking to make sure that the bits
6068 * the user changed are actually legal for him to change.
6070 for (i = 0; i < page_index->page_len; i++) {
6071 uint8_t *user_byte, *change_mask, *current_byte;
6075 user_byte = (uint8_t *)page_header + i;
6076 change_mask = page_index->page_data +
6077 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6078 current_byte = page_index->page_data +
6079 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6082 * Check to see whether the user set any bits in this byte
6083 * that he is not allowed to set.
6085 if ((*user_byte & ~(*change_mask)) ==
6086 (*current_byte & ~(*change_mask)))
6090 * Go through bit by bit to determine which one is illegal.
6093 for (j = 7; j >= 0; j--) {
6094 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6095 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6100 ctl_set_invalid_field(ctsio,
6103 /*field*/ *len_used + i,
6106 free(ctsio->kern_data_ptr, M_CTL);
6107 ctl_done((union ctl_io *)ctsio);
6108 return (CTL_RETVAL_COMPLETE);
6112 * Decrement these before we call the page handler, since we may
6113 * end up getting called back one way or another before the handler
6114 * returns to this context.
6116 *len_left -= page_index->page_len;
6117 *len_used += page_index->page_len;
6119 retval = page_index->select_handler(ctsio, page_index,
6120 (uint8_t *)page_header);
6123 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6124 * wait until this queued command completes to finish processing
6125 * the mode page. If it returns anything other than
6126 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6127 * already set the sense information, freed the data pointer, and
6128 * completed the io for us.
6130 if (retval != CTL_RETVAL_COMPLETE)
6131 goto bailout_no_done;
6134 * If the initiator sent us more than one page, parse the next one.
6139 ctl_set_success(ctsio);
6140 free(ctsio->kern_data_ptr, M_CTL);
6141 ctl_done((union ctl_io *)ctsio);
6145 return (CTL_RETVAL_COMPLETE);
6150 ctl_mode_select(struct ctl_scsiio *ctsio)
6152 struct ctl_lun *lun;
6153 union ctl_modepage_info *modepage_info;
6154 int bd_len, i, header_size, param_len, pf, rtd, sp;
6157 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6158 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6159 switch (ctsio->cdb[0]) {
6160 case MODE_SELECT_6: {
6161 struct scsi_mode_select_6 *cdb;
6163 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6165 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6166 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6167 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6168 param_len = cdb->length;
6169 header_size = sizeof(struct scsi_mode_header_6);
6172 case MODE_SELECT_10: {
6173 struct scsi_mode_select_10 *cdb;
6175 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6177 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6178 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6179 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6180 param_len = scsi_2btoul(cdb->length);
6181 header_size = sizeof(struct scsi_mode_header_10);
6185 ctl_set_invalid_opcode(ctsio);
6186 ctl_done((union ctl_io *)ctsio);
6187 return (CTL_RETVAL_COMPLETE);
6191 if (param_len != 0) {
6192 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6193 /*command*/ 1, /*field*/ 0,
6194 /*bit_valid*/ 0, /*bit*/ 0);
6195 ctl_done((union ctl_io *)ctsio);
6196 return (CTL_RETVAL_COMPLETE);
6199 /* Revert to defaults. */
6200 ctl_init_page_index(lun);
6201 mtx_lock(&lun->lun_lock);
6202 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6203 mtx_unlock(&lun->lun_lock);
6204 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6205 ctl_isc_announce_mode(lun, -1,
6206 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6207 lun->mode_pages.index[i].subpage);
6209 ctl_set_success(ctsio);
6210 ctl_done((union ctl_io *)ctsio);
6211 return (CTL_RETVAL_COMPLETE);
6216 * "A parameter list length of zero indicates that the Data-Out Buffer
6217 * shall be empty. This condition shall not be considered as an error."
6219 if (param_len == 0) {
6220 ctl_set_success(ctsio);
6221 ctl_done((union ctl_io *)ctsio);
6222 return (CTL_RETVAL_COMPLETE);
6226 * Since we'll hit this the first time through, prior to
6227 * allocation, we don't need to free a data buffer here.
6229 if (param_len < header_size) {
6230 ctl_set_param_len_error(ctsio);
6231 ctl_done((union ctl_io *)ctsio);
6232 return (CTL_RETVAL_COMPLETE);
6236 * Allocate the data buffer and grab the user's data. In theory,
6237 * we shouldn't have to sanity check the parameter list length here
6238 * because the maximum size is 64K. We should be able to malloc
6239 * that much without too many problems.
6241 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6242 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6243 ctsio->kern_data_len = param_len;
6244 ctsio->kern_total_len = param_len;
6245 ctsio->kern_data_resid = 0;
6246 ctsio->kern_rel_offset = 0;
6247 ctsio->kern_sg_entries = 0;
6248 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6249 ctsio->be_move_done = ctl_config_move_done;
6250 ctl_datamove((union ctl_io *)ctsio);
6252 return (CTL_RETVAL_COMPLETE);
6255 switch (ctsio->cdb[0]) {
6256 case MODE_SELECT_6: {
6257 struct scsi_mode_header_6 *mh6;
6259 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6260 bd_len = mh6->blk_desc_len;
6263 case MODE_SELECT_10: {
6264 struct scsi_mode_header_10 *mh10;
6266 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6267 bd_len = scsi_2btoul(mh10->blk_desc_len);
6271 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6274 if (param_len < (header_size + bd_len)) {
6275 free(ctsio->kern_data_ptr, M_CTL);
6276 ctl_set_param_len_error(ctsio);
6277 ctl_done((union ctl_io *)ctsio);
6278 return (CTL_RETVAL_COMPLETE);
6282 * Set the IO_CONT flag, so that if this I/O gets passed to
6283 * ctl_config_write_done(), it'll get passed back to
6284 * ctl_do_mode_select() for further processing, or completion if
6287 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6288 ctsio->io_cont = ctl_do_mode_select;
6290 modepage_info = (union ctl_modepage_info *)
6291 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6292 memset(modepage_info, 0, sizeof(*modepage_info));
6293 modepage_info->header.len_left = param_len - header_size - bd_len;
6294 modepage_info->header.len_used = header_size + bd_len;
6296 return (ctl_do_mode_select((union ctl_io *)ctsio));
6300 ctl_mode_sense(struct ctl_scsiio *ctsio)
6302 struct ctl_lun *lun;
6303 int pc, page_code, dbd, llba, subpage;
6304 int alloc_len, page_len, header_len, total_len;
6305 struct scsi_mode_block_descr *block_desc;
6306 struct ctl_page_index *page_index;
6312 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6314 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6315 switch (ctsio->cdb[0]) {
6316 case MODE_SENSE_6: {
6317 struct scsi_mode_sense_6 *cdb;
6319 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6321 header_len = sizeof(struct scsi_mode_hdr_6);
6322 if (cdb->byte2 & SMS_DBD)
6325 header_len += sizeof(struct scsi_mode_block_descr);
6327 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6328 page_code = cdb->page & SMS_PAGE_CODE;
6329 subpage = cdb->subpage;
6330 alloc_len = cdb->length;
6333 case MODE_SENSE_10: {
6334 struct scsi_mode_sense_10 *cdb;
6336 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6338 header_len = sizeof(struct scsi_mode_hdr_10);
6340 if (cdb->byte2 & SMS_DBD)
6343 header_len += sizeof(struct scsi_mode_block_descr);
6344 if (cdb->byte2 & SMS10_LLBAA)
6346 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6347 page_code = cdb->page & SMS_PAGE_CODE;
6348 subpage = cdb->subpage;
6349 alloc_len = scsi_2btoul(cdb->length);
6353 ctl_set_invalid_opcode(ctsio);
6354 ctl_done((union ctl_io *)ctsio);
6355 return (CTL_RETVAL_COMPLETE);
6356 break; /* NOTREACHED */
6360 * We have to make a first pass through to calculate the size of
6361 * the pages that match the user's query. Then we allocate enough
6362 * memory to hold it, and actually copy the data into the buffer.
6364 switch (page_code) {
6365 case SMS_ALL_PAGES_PAGE: {
6371 * At the moment, values other than 0 and 0xff here are
6372 * reserved according to SPC-3.
6374 if ((subpage != SMS_SUBPAGE_PAGE_0)
6375 && (subpage != SMS_SUBPAGE_ALL)) {
6376 ctl_set_invalid_field(ctsio,
6382 ctl_done((union ctl_io *)ctsio);
6383 return (CTL_RETVAL_COMPLETE);
6386 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6387 page_index = &lun->mode_pages.index[i];
6389 /* Make sure the page is supported for this dev type */
6390 if (lun->be_lun->lun_type == T_DIRECT &&
6391 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6393 if (lun->be_lun->lun_type == T_PROCESSOR &&
6394 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6396 if (lun->be_lun->lun_type == T_CDROM &&
6397 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6401 * We don't use this subpage if the user didn't
6402 * request all subpages.
6404 if ((page_index->subpage != 0)
6405 && (subpage == SMS_SUBPAGE_PAGE_0))
6409 printf("found page %#x len %d\n",
6410 page_index->page_code & SMPH_PC_MASK,
6411 page_index->page_len);
6413 page_len += page_index->page_len;
6422 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6423 page_index = &lun->mode_pages.index[i];
6425 /* Make sure the page is supported for this dev type */
6426 if (lun->be_lun->lun_type == T_DIRECT &&
6427 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6429 if (lun->be_lun->lun_type == T_PROCESSOR &&
6430 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6432 if (lun->be_lun->lun_type == T_CDROM &&
6433 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6436 /* Look for the right page code */
6437 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6440 /* Look for the right subpage or the subpage wildcard*/
6441 if ((page_index->subpage != subpage)
6442 && (subpage != SMS_SUBPAGE_ALL))
6446 printf("found page %#x len %d\n",
6447 page_index->page_code & SMPH_PC_MASK,
6448 page_index->page_len);
6451 page_len += page_index->page_len;
6454 if (page_len == 0) {
6455 ctl_set_invalid_field(ctsio,
6461 ctl_done((union ctl_io *)ctsio);
6462 return (CTL_RETVAL_COMPLETE);
6468 total_len = header_len + page_len;
6470 printf("header_len = %d, page_len = %d, total_len = %d\n",
6471 header_len, page_len, total_len);
6474 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6475 ctsio->kern_sg_entries = 0;
6476 ctsio->kern_data_resid = 0;
6477 ctsio->kern_rel_offset = 0;
6478 if (total_len < alloc_len) {
6479 ctsio->residual = alloc_len - total_len;
6480 ctsio->kern_data_len = total_len;
6481 ctsio->kern_total_len = total_len;
6483 ctsio->residual = 0;
6484 ctsio->kern_data_len = alloc_len;
6485 ctsio->kern_total_len = alloc_len;
6488 switch (ctsio->cdb[0]) {
6489 case MODE_SENSE_6: {
6490 struct scsi_mode_hdr_6 *header;
6492 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6494 header->datalen = MIN(total_len - 1, 254);
6495 if (lun->be_lun->lun_type == T_DIRECT) {
6496 header->dev_specific = 0x10; /* DPOFUA */
6497 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6498 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6499 header->dev_specific |= 0x80; /* WP */
6502 header->block_descr_len = 0;
6504 header->block_descr_len =
6505 sizeof(struct scsi_mode_block_descr);
6506 block_desc = (struct scsi_mode_block_descr *)&header[1];
6509 case MODE_SENSE_10: {
6510 struct scsi_mode_hdr_10 *header;
6513 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6515 datalen = MIN(total_len - 2, 65533);
6516 scsi_ulto2b(datalen, header->datalen);
6517 if (lun->be_lun->lun_type == T_DIRECT) {
6518 header->dev_specific = 0x10; /* DPOFUA */
6519 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6520 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6521 header->dev_specific |= 0x80; /* WP */
6524 scsi_ulto2b(0, header->block_descr_len);
6526 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6527 header->block_descr_len);
6528 block_desc = (struct scsi_mode_block_descr *)&header[1];
6532 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6536 * If we've got a disk, use its blocksize in the block
6537 * descriptor. Otherwise, just set it to 0.
6540 if (lun->be_lun->lun_type == T_DIRECT)
6541 scsi_ulto3b(lun->be_lun->blocksize,
6542 block_desc->block_len);
6544 scsi_ulto3b(0, block_desc->block_len);
6547 switch (page_code) {
6548 case SMS_ALL_PAGES_PAGE: {
6551 data_used = header_len;
6552 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6553 struct ctl_page_index *page_index;
6555 page_index = &lun->mode_pages.index[i];
6556 if (lun->be_lun->lun_type == T_DIRECT &&
6557 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6559 if (lun->be_lun->lun_type == T_PROCESSOR &&
6560 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6562 if (lun->be_lun->lun_type == T_CDROM &&
6563 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6567 * We don't use this subpage if the user didn't
6568 * request all subpages. We already checked (above)
6569 * to make sure the user only specified a subpage
6570 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6572 if ((page_index->subpage != 0)
6573 && (subpage == SMS_SUBPAGE_PAGE_0))
6577 * Call the handler, if it exists, to update the
6578 * page to the latest values.
6580 if (page_index->sense_handler != NULL)
6581 page_index->sense_handler(ctsio, page_index,pc);
6583 memcpy(ctsio->kern_data_ptr + data_used,
6584 page_index->page_data +
6585 (page_index->page_len * pc),
6586 page_index->page_len);
6587 data_used += page_index->page_len;
6594 data_used = header_len;
6596 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6597 struct ctl_page_index *page_index;
6599 page_index = &lun->mode_pages.index[i];
6601 /* Look for the right page code */
6602 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6605 /* Look for the right subpage or the subpage wildcard*/
6606 if ((page_index->subpage != subpage)
6607 && (subpage != SMS_SUBPAGE_ALL))
6610 /* Make sure the page is supported for this dev type */
6611 if (lun->be_lun->lun_type == T_DIRECT &&
6612 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6614 if (lun->be_lun->lun_type == T_PROCESSOR &&
6615 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6617 if (lun->be_lun->lun_type == T_CDROM &&
6618 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6622 * Call the handler, if it exists, to update the
6623 * page to the latest values.
6625 if (page_index->sense_handler != NULL)
6626 page_index->sense_handler(ctsio, page_index,pc);
6628 memcpy(ctsio->kern_data_ptr + data_used,
6629 page_index->page_data +
6630 (page_index->page_len * pc),
6631 page_index->page_len);
6632 data_used += page_index->page_len;
6638 ctl_set_success(ctsio);
6639 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6640 ctsio->be_move_done = ctl_config_move_done;
6641 ctl_datamove((union ctl_io *)ctsio);
6642 return (CTL_RETVAL_COMPLETE);
6646 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6647 struct ctl_page_index *page_index,
6650 struct ctl_lun *lun;
6651 struct scsi_log_param_header *phdr;
6655 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6656 data = page_index->page_data;
6658 if (lun->backend->lun_attr != NULL &&
6659 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6661 phdr = (struct scsi_log_param_header *)data;
6662 scsi_ulto2b(0x0001, phdr->param_code);
6663 phdr->param_control = SLP_LBIN | SLP_LP;
6664 phdr->param_len = 8;
6665 data = (uint8_t *)(phdr + 1);
6666 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6667 data[4] = 0x02; /* per-pool */
6668 data += phdr->param_len;
6671 if (lun->backend->lun_attr != NULL &&
6672 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6674 phdr = (struct scsi_log_param_header *)data;
6675 scsi_ulto2b(0x0002, phdr->param_code);
6676 phdr->param_control = SLP_LBIN | SLP_LP;
6677 phdr->param_len = 8;
6678 data = (uint8_t *)(phdr + 1);
6679 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6680 data[4] = 0x01; /* per-LUN */
6681 data += phdr->param_len;
6684 if (lun->backend->lun_attr != NULL &&
6685 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6687 phdr = (struct scsi_log_param_header *)data;
6688 scsi_ulto2b(0x00f1, phdr->param_code);
6689 phdr->param_control = SLP_LBIN | SLP_LP;
6690 phdr->param_len = 8;
6691 data = (uint8_t *)(phdr + 1);
6692 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6693 data[4] = 0x02; /* per-pool */
6694 data += phdr->param_len;
6697 if (lun->backend->lun_attr != NULL &&
6698 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6700 phdr = (struct scsi_log_param_header *)data;
6701 scsi_ulto2b(0x00f2, phdr->param_code);
6702 phdr->param_control = SLP_LBIN | SLP_LP;
6703 phdr->param_len = 8;
6704 data = (uint8_t *)(phdr + 1);
6705 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6706 data[4] = 0x02; /* per-pool */
6707 data += phdr->param_len;
6710 page_index->page_len = data - page_index->page_data;
6715 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6716 struct ctl_page_index *page_index,
6719 struct ctl_lun *lun;
6720 struct stat_page *data;
6721 uint64_t rn, wn, rb, wb;
6722 struct bintime rt, wt;
6725 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6726 data = (struct stat_page *)page_index->page_data;
6728 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6729 data->sap.hdr.param_control = SLP_LBIN;
6730 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6731 sizeof(struct scsi_log_param_header);
6732 rn = wn = rb = wb = 0;
6735 for (i = 0; i < CTL_MAX_PORTS; i++) {
6736 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6737 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6738 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6739 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6740 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6741 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6743 scsi_u64to8b(rn, data->sap.read_num);
6744 scsi_u64to8b(wn, data->sap.write_num);
6745 if (lun->stats.blocksize > 0) {
6746 scsi_u64to8b(wb / lun->stats.blocksize,
6747 data->sap.recvieved_lba);
6748 scsi_u64to8b(rb / lun->stats.blocksize,
6749 data->sap.transmitted_lba);
6751 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6752 data->sap.read_int);
6753 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6754 data->sap.write_int);
6755 scsi_u64to8b(0, data->sap.weighted_num);
6756 scsi_u64to8b(0, data->sap.weighted_int);
6757 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6758 data->it.hdr.param_control = SLP_LBIN;
6759 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6760 sizeof(struct scsi_log_param_header);
6762 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6764 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6765 data->it.hdr.param_control = SLP_LBIN;
6766 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6767 sizeof(struct scsi_log_param_header);
6768 scsi_ulto4b(3, data->ti.exponent);
6769 scsi_ulto4b(1, data->ti.integer);
6774 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6775 struct ctl_page_index *page_index,
6778 struct ctl_lun *lun;
6779 struct scsi_log_informational_exceptions *data;
6781 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6782 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6784 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6785 data->hdr.param_control = SLP_LBIN;
6786 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6787 sizeof(struct scsi_log_param_header);
6788 data->ie_asc = lun->ie_asc;
6789 data->ie_ascq = lun->ie_ascq;
6790 data->temperature = 0xff;
6795 ctl_log_sense(struct ctl_scsiio *ctsio)
6797 struct ctl_lun *lun;
6798 int i, pc, page_code, subpage;
6799 int alloc_len, total_len;
6800 struct ctl_page_index *page_index;
6801 struct scsi_log_sense *cdb;
6802 struct scsi_log_header *header;
6804 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6806 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6807 cdb = (struct scsi_log_sense *)ctsio->cdb;
6808 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6809 page_code = cdb->page & SLS_PAGE_CODE;
6810 subpage = cdb->subpage;
6811 alloc_len = scsi_2btoul(cdb->length);
6814 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6815 page_index = &lun->log_pages.index[i];
6817 /* Look for the right page code */
6818 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6821 /* Look for the right subpage or the subpage wildcard*/
6822 if (page_index->subpage != subpage)
6827 if (i >= CTL_NUM_LOG_PAGES) {
6828 ctl_set_invalid_field(ctsio,
6834 ctl_done((union ctl_io *)ctsio);
6835 return (CTL_RETVAL_COMPLETE);
6838 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6840 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6841 ctsio->kern_sg_entries = 0;
6842 ctsio->kern_data_resid = 0;
6843 ctsio->kern_rel_offset = 0;
6844 if (total_len < alloc_len) {
6845 ctsio->residual = alloc_len - total_len;
6846 ctsio->kern_data_len = total_len;
6847 ctsio->kern_total_len = total_len;
6849 ctsio->residual = 0;
6850 ctsio->kern_data_len = alloc_len;
6851 ctsio->kern_total_len = alloc_len;
6854 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6855 header->page = page_index->page_code;
6856 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6857 header->page |= SL_DS;
6858 if (page_index->subpage) {
6859 header->page |= SL_SPF;
6860 header->subpage = page_index->subpage;
6862 scsi_ulto2b(page_index->page_len, header->datalen);
6865 * Call the handler, if it exists, to update the
6866 * page to the latest values.
6868 if (page_index->sense_handler != NULL)
6869 page_index->sense_handler(ctsio, page_index, pc);
6871 memcpy(header + 1, page_index->page_data, page_index->page_len);
6873 ctl_set_success(ctsio);
6874 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6875 ctsio->be_move_done = ctl_config_move_done;
6876 ctl_datamove((union ctl_io *)ctsio);
6877 return (CTL_RETVAL_COMPLETE);
6881 ctl_read_capacity(struct ctl_scsiio *ctsio)
6883 struct scsi_read_capacity *cdb;
6884 struct scsi_read_capacity_data *data;
6885 struct ctl_lun *lun;
6888 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6890 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6892 lba = scsi_4btoul(cdb->addr);
6893 if (((cdb->pmi & SRC_PMI) == 0)
6895 ctl_set_invalid_field(/*ctsio*/ ctsio,
6901 ctl_done((union ctl_io *)ctsio);
6902 return (CTL_RETVAL_COMPLETE);
6905 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6907 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6908 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6909 ctsio->residual = 0;
6910 ctsio->kern_data_len = sizeof(*data);
6911 ctsio->kern_total_len = sizeof(*data);
6912 ctsio->kern_data_resid = 0;
6913 ctsio->kern_rel_offset = 0;
6914 ctsio->kern_sg_entries = 0;
6917 * If the maximum LBA is greater than 0xfffffffe, the user must
6918 * issue a SERVICE ACTION IN (16) command, with the read capacity
6919 * serivce action set.
6921 if (lun->be_lun->maxlba > 0xfffffffe)
6922 scsi_ulto4b(0xffffffff, data->addr);
6924 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6927 * XXX KDM this may not be 512 bytes...
6929 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6931 ctl_set_success(ctsio);
6932 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6933 ctsio->be_move_done = ctl_config_move_done;
6934 ctl_datamove((union ctl_io *)ctsio);
6935 return (CTL_RETVAL_COMPLETE);
6939 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6941 struct scsi_read_capacity_16 *cdb;
6942 struct scsi_read_capacity_data_long *data;
6943 struct ctl_lun *lun;
6947 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6949 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6951 alloc_len = scsi_4btoul(cdb->alloc_len);
6952 lba = scsi_8btou64(cdb->addr);
6954 if ((cdb->reladr & SRC16_PMI)
6956 ctl_set_invalid_field(/*ctsio*/ ctsio,
6962 ctl_done((union ctl_io *)ctsio);
6963 return (CTL_RETVAL_COMPLETE);
6966 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6968 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6969 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6971 if (sizeof(*data) < alloc_len) {
6972 ctsio->residual = alloc_len - sizeof(*data);
6973 ctsio->kern_data_len = sizeof(*data);
6974 ctsio->kern_total_len = sizeof(*data);
6976 ctsio->residual = 0;
6977 ctsio->kern_data_len = alloc_len;
6978 ctsio->kern_total_len = alloc_len;
6980 ctsio->kern_data_resid = 0;
6981 ctsio->kern_rel_offset = 0;
6982 ctsio->kern_sg_entries = 0;
6984 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6985 /* XXX KDM this may not be 512 bytes... */
6986 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6987 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6988 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6989 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6990 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6992 ctl_set_success(ctsio);
6993 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6994 ctsio->be_move_done = ctl_config_move_done;
6995 ctl_datamove((union ctl_io *)ctsio);
6996 return (CTL_RETVAL_COMPLETE);
7000 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7002 struct scsi_get_lba_status *cdb;
7003 struct scsi_get_lba_status_data *data;
7004 struct ctl_lun *lun;
7005 struct ctl_lba_len_flags *lbalen;
7007 uint32_t alloc_len, total_len;
7010 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7012 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7013 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7014 lba = scsi_8btou64(cdb->addr);
7015 alloc_len = scsi_4btoul(cdb->alloc_len);
7017 if (lba > lun->be_lun->maxlba) {
7018 ctl_set_lba_out_of_range(ctsio, lba);
7019 ctl_done((union ctl_io *)ctsio);
7020 return (CTL_RETVAL_COMPLETE);
7023 total_len = sizeof(*data) + sizeof(data->descr[0]);
7024 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7025 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7027 if (total_len < alloc_len) {
7028 ctsio->residual = alloc_len - total_len;
7029 ctsio->kern_data_len = total_len;
7030 ctsio->kern_total_len = total_len;
7032 ctsio->residual = 0;
7033 ctsio->kern_data_len = alloc_len;
7034 ctsio->kern_total_len = alloc_len;
7036 ctsio->kern_data_resid = 0;
7037 ctsio->kern_rel_offset = 0;
7038 ctsio->kern_sg_entries = 0;
7040 /* Fill dummy data in case backend can't tell anything. */
7041 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7042 scsi_u64to8b(lba, data->descr[0].addr);
7043 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7044 data->descr[0].length);
7045 data->descr[0].status = 0; /* Mapped or unknown. */
7047 ctl_set_success(ctsio);
7048 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7049 ctsio->be_move_done = ctl_config_move_done;
7051 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7053 lbalen->len = total_len;
7055 retval = lun->backend->config_read((union ctl_io *)ctsio);
7056 return (CTL_RETVAL_COMPLETE);
7060 ctl_read_defect(struct ctl_scsiio *ctsio)
7062 struct scsi_read_defect_data_10 *ccb10;
7063 struct scsi_read_defect_data_12 *ccb12;
7064 struct scsi_read_defect_data_hdr_10 *data10;
7065 struct scsi_read_defect_data_hdr_12 *data12;
7066 uint32_t alloc_len, data_len;
7069 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7071 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7072 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7073 format = ccb10->format;
7074 alloc_len = scsi_2btoul(ccb10->alloc_length);
7075 data_len = sizeof(*data10);
7077 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7078 format = ccb12->format;
7079 alloc_len = scsi_4btoul(ccb12->alloc_length);
7080 data_len = sizeof(*data12);
7082 if (alloc_len == 0) {
7083 ctl_set_success(ctsio);
7084 ctl_done((union ctl_io *)ctsio);
7085 return (CTL_RETVAL_COMPLETE);
7088 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7089 if (data_len < alloc_len) {
7090 ctsio->residual = alloc_len - data_len;
7091 ctsio->kern_data_len = data_len;
7092 ctsio->kern_total_len = data_len;
7094 ctsio->residual = 0;
7095 ctsio->kern_data_len = alloc_len;
7096 ctsio->kern_total_len = alloc_len;
7098 ctsio->kern_data_resid = 0;
7099 ctsio->kern_rel_offset = 0;
7100 ctsio->kern_sg_entries = 0;
7102 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7103 data10 = (struct scsi_read_defect_data_hdr_10 *)
7104 ctsio->kern_data_ptr;
7105 data10->format = format;
7106 scsi_ulto2b(0, data10->length);
7108 data12 = (struct scsi_read_defect_data_hdr_12 *)
7109 ctsio->kern_data_ptr;
7110 data12->format = format;
7111 scsi_ulto2b(0, data12->generation);
7112 scsi_ulto4b(0, data12->length);
7115 ctl_set_success(ctsio);
7116 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7117 ctsio->be_move_done = ctl_config_move_done;
7118 ctl_datamove((union ctl_io *)ctsio);
7119 return (CTL_RETVAL_COMPLETE);
7123 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7125 struct scsi_maintenance_in *cdb;
7127 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7128 int num_ha_groups, num_target_ports, shared_group;
7129 struct ctl_lun *lun;
7130 struct ctl_softc *softc;
7131 struct ctl_port *port;
7132 struct scsi_target_group_data *rtg_ptr;
7133 struct scsi_target_group_data_extended *rtg_ext_ptr;
7134 struct scsi_target_port_group_descriptor *tpg_desc;
7136 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7138 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7139 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7140 softc = lun->ctl_softc;
7142 retval = CTL_RETVAL_COMPLETE;
7144 switch (cdb->byte2 & STG_PDF_MASK) {
7145 case STG_PDF_LENGTH:
7148 case STG_PDF_EXTENDED:
7152 ctl_set_invalid_field(/*ctsio*/ ctsio,
7158 ctl_done((union ctl_io *)ctsio);
7162 num_target_ports = 0;
7163 shared_group = (softc->is_single != 0);
7164 mtx_lock(&softc->ctl_lock);
7165 STAILQ_FOREACH(port, &softc->port_list, links) {
7166 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7168 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7171 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7174 mtx_unlock(&softc->ctl_lock);
7175 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7178 total_len = sizeof(struct scsi_target_group_data_extended);
7180 total_len = sizeof(struct scsi_target_group_data);
7181 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7182 (shared_group + num_ha_groups) +
7183 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7185 alloc_len = scsi_4btoul(cdb->length);
7187 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7189 ctsio->kern_sg_entries = 0;
7191 if (total_len < alloc_len) {
7192 ctsio->residual = alloc_len - total_len;
7193 ctsio->kern_data_len = total_len;
7194 ctsio->kern_total_len = total_len;
7196 ctsio->residual = 0;
7197 ctsio->kern_data_len = alloc_len;
7198 ctsio->kern_total_len = alloc_len;
7200 ctsio->kern_data_resid = 0;
7201 ctsio->kern_rel_offset = 0;
7204 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7205 ctsio->kern_data_ptr;
7206 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7207 rtg_ext_ptr->format_type = 0x10;
7208 rtg_ext_ptr->implicit_transition_time = 0;
7209 tpg_desc = &rtg_ext_ptr->groups[0];
7211 rtg_ptr = (struct scsi_target_group_data *)
7212 ctsio->kern_data_ptr;
7213 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7214 tpg_desc = &rtg_ptr->groups[0];
7217 mtx_lock(&softc->ctl_lock);
7218 pg = softc->port_min / softc->port_cnt;
7219 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7220 /* Some shelf is known to be primary. */
7221 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7222 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7223 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7224 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7225 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7226 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7228 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7229 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7230 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7233 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7236 /* No known primary shelf. */
7237 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7238 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7239 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7240 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7241 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7242 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7244 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7248 tpg_desc->pref_state = ts;
7249 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7250 TPG_U_SUP | TPG_T_SUP;
7251 scsi_ulto2b(1, tpg_desc->target_port_group);
7252 tpg_desc->status = TPG_IMPLICIT;
7254 STAILQ_FOREACH(port, &softc->port_list, links) {
7255 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7257 if (!softc->is_single &&
7258 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7260 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7262 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7263 relative_target_port_identifier);
7266 tpg_desc->target_port_count = pc;
7267 tpg_desc = (struct scsi_target_port_group_descriptor *)
7268 &tpg_desc->descriptors[pc];
7270 for (g = 0; g < num_ha_groups; g++) {
7271 tpg_desc->pref_state = (g == pg) ? ts : os;
7272 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7273 TPG_U_SUP | TPG_T_SUP;
7274 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7275 tpg_desc->status = TPG_IMPLICIT;
7277 STAILQ_FOREACH(port, &softc->port_list, links) {
7278 if (port->targ_port < g * softc->port_cnt ||
7279 port->targ_port >= (g + 1) * softc->port_cnt)
7281 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7283 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7285 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7287 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7288 relative_target_port_identifier);
7291 tpg_desc->target_port_count = pc;
7292 tpg_desc = (struct scsi_target_port_group_descriptor *)
7293 &tpg_desc->descriptors[pc];
7295 mtx_unlock(&softc->ctl_lock);
7297 ctl_set_success(ctsio);
7298 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7299 ctsio->be_move_done = ctl_config_move_done;
7300 ctl_datamove((union ctl_io *)ctsio);
7305 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7307 struct ctl_lun *lun;
7308 struct scsi_report_supported_opcodes *cdb;
7309 const struct ctl_cmd_entry *entry, *sentry;
7310 struct scsi_report_supported_opcodes_all *all;
7311 struct scsi_report_supported_opcodes_descr *descr;
7312 struct scsi_report_supported_opcodes_one *one;
7314 int alloc_len, total_len;
7315 int opcode, service_action, i, j, num;
7317 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7319 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7320 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7322 retval = CTL_RETVAL_COMPLETE;
7324 opcode = cdb->requested_opcode;
7325 service_action = scsi_2btoul(cdb->requested_service_action);
7326 switch (cdb->options & RSO_OPTIONS_MASK) {
7327 case RSO_OPTIONS_ALL:
7329 for (i = 0; i < 256; i++) {
7330 entry = &ctl_cmd_table[i];
7331 if (entry->flags & CTL_CMD_FLAG_SA5) {
7332 for (j = 0; j < 32; j++) {
7333 sentry = &((const struct ctl_cmd_entry *)
7335 if (ctl_cmd_applicable(
7336 lun->be_lun->lun_type, sentry))
7340 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7345 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7346 num * sizeof(struct scsi_report_supported_opcodes_descr);
7348 case RSO_OPTIONS_OC:
7349 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7350 ctl_set_invalid_field(/*ctsio*/ ctsio,
7356 ctl_done((union ctl_io *)ctsio);
7357 return (CTL_RETVAL_COMPLETE);
7359 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7361 case RSO_OPTIONS_OC_SA:
7362 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7363 service_action >= 32) {
7364 ctl_set_invalid_field(/*ctsio*/ ctsio,
7370 ctl_done((union ctl_io *)ctsio);
7371 return (CTL_RETVAL_COMPLETE);
7374 case RSO_OPTIONS_OC_ASA:
7375 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7378 ctl_set_invalid_field(/*ctsio*/ ctsio,
7384 ctl_done((union ctl_io *)ctsio);
7385 return (CTL_RETVAL_COMPLETE);
7388 alloc_len = scsi_4btoul(cdb->length);
7390 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7392 ctsio->kern_sg_entries = 0;
7394 if (total_len < alloc_len) {
7395 ctsio->residual = alloc_len - total_len;
7396 ctsio->kern_data_len = total_len;
7397 ctsio->kern_total_len = total_len;
7399 ctsio->residual = 0;
7400 ctsio->kern_data_len = alloc_len;
7401 ctsio->kern_total_len = alloc_len;
7403 ctsio->kern_data_resid = 0;
7404 ctsio->kern_rel_offset = 0;
7406 switch (cdb->options & RSO_OPTIONS_MASK) {
7407 case RSO_OPTIONS_ALL:
7408 all = (struct scsi_report_supported_opcodes_all *)
7409 ctsio->kern_data_ptr;
7411 for (i = 0; i < 256; i++) {
7412 entry = &ctl_cmd_table[i];
7413 if (entry->flags & CTL_CMD_FLAG_SA5) {
7414 for (j = 0; j < 32; j++) {
7415 sentry = &((const struct ctl_cmd_entry *)
7417 if (!ctl_cmd_applicable(
7418 lun->be_lun->lun_type, sentry))
7420 descr = &all->descr[num++];
7422 scsi_ulto2b(j, descr->service_action);
7423 descr->flags = RSO_SERVACTV;
7424 scsi_ulto2b(sentry->length,
7428 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7431 descr = &all->descr[num++];
7433 scsi_ulto2b(0, descr->service_action);
7435 scsi_ulto2b(entry->length, descr->cdb_length);
7439 num * sizeof(struct scsi_report_supported_opcodes_descr),
7442 case RSO_OPTIONS_OC:
7443 one = (struct scsi_report_supported_opcodes_one *)
7444 ctsio->kern_data_ptr;
7445 entry = &ctl_cmd_table[opcode];
7447 case RSO_OPTIONS_OC_SA:
7448 one = (struct scsi_report_supported_opcodes_one *)
7449 ctsio->kern_data_ptr;
7450 entry = &ctl_cmd_table[opcode];
7451 entry = &((const struct ctl_cmd_entry *)
7452 entry->execute)[service_action];
7454 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7456 scsi_ulto2b(entry->length, one->cdb_length);
7457 one->cdb_usage[0] = opcode;
7458 memcpy(&one->cdb_usage[1], entry->usage,
7463 case RSO_OPTIONS_OC_ASA:
7464 one = (struct scsi_report_supported_opcodes_one *)
7465 ctsio->kern_data_ptr;
7466 entry = &ctl_cmd_table[opcode];
7467 if (entry->flags & CTL_CMD_FLAG_SA5) {
7468 entry = &((const struct ctl_cmd_entry *)
7469 entry->execute)[service_action];
7470 } else if (service_action != 0) {
7477 ctl_set_success(ctsio);
7478 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7479 ctsio->be_move_done = ctl_config_move_done;
7480 ctl_datamove((union ctl_io *)ctsio);
7485 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7487 struct scsi_report_supported_tmf *cdb;
7488 struct scsi_report_supported_tmf_ext_data *data;
7490 int alloc_len, total_len;
7492 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7494 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7496 retval = CTL_RETVAL_COMPLETE;
7498 if (cdb->options & RST_REPD)
7499 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7501 total_len = sizeof(struct scsi_report_supported_tmf_data);
7502 alloc_len = scsi_4btoul(cdb->length);
7504 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7506 ctsio->kern_sg_entries = 0;
7508 if (total_len < alloc_len) {
7509 ctsio->residual = alloc_len - total_len;
7510 ctsio->kern_data_len = total_len;
7511 ctsio->kern_total_len = total_len;
7513 ctsio->residual = 0;
7514 ctsio->kern_data_len = alloc_len;
7515 ctsio->kern_total_len = alloc_len;
7517 ctsio->kern_data_resid = 0;
7518 ctsio->kern_rel_offset = 0;
7520 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7521 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7523 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7524 data->length = total_len - 4;
7526 ctl_set_success(ctsio);
7527 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7528 ctsio->be_move_done = ctl_config_move_done;
7529 ctl_datamove((union ctl_io *)ctsio);
7534 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7536 struct scsi_report_timestamp *cdb;
7537 struct scsi_report_timestamp_data *data;
7541 int alloc_len, total_len;
7543 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7545 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7547 retval = CTL_RETVAL_COMPLETE;
7549 total_len = sizeof(struct scsi_report_timestamp_data);
7550 alloc_len = scsi_4btoul(cdb->length);
7552 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7554 ctsio->kern_sg_entries = 0;
7556 if (total_len < alloc_len) {
7557 ctsio->residual = alloc_len - total_len;
7558 ctsio->kern_data_len = total_len;
7559 ctsio->kern_total_len = total_len;
7561 ctsio->residual = 0;
7562 ctsio->kern_data_len = alloc_len;
7563 ctsio->kern_total_len = alloc_len;
7565 ctsio->kern_data_resid = 0;
7566 ctsio->kern_rel_offset = 0;
7568 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7569 scsi_ulto2b(sizeof(*data) - 2, data->length);
7570 data->origin = RTS_ORIG_OUTSIDE;
7572 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7573 scsi_ulto4b(timestamp >> 16, data->timestamp);
7574 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7576 ctl_set_success(ctsio);
7577 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7578 ctsio->be_move_done = ctl_config_move_done;
7579 ctl_datamove((union ctl_io *)ctsio);
7584 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7586 struct scsi_per_res_in *cdb;
7587 int alloc_len, total_len = 0;
7588 /* struct scsi_per_res_in_rsrv in_data; */
7589 struct ctl_lun *lun;
7590 struct ctl_softc *softc;
7593 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7595 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7597 alloc_len = scsi_2btoul(cdb->length);
7599 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7600 softc = lun->ctl_softc;
7603 mtx_lock(&lun->lun_lock);
7604 switch (cdb->action) {
7605 case SPRI_RK: /* read keys */
7606 total_len = sizeof(struct scsi_per_res_in_keys) +
7608 sizeof(struct scsi_per_res_key);
7610 case SPRI_RR: /* read reservation */
7611 if (lun->flags & CTL_LUN_PR_RESERVED)
7612 total_len = sizeof(struct scsi_per_res_in_rsrv);
7614 total_len = sizeof(struct scsi_per_res_in_header);
7616 case SPRI_RC: /* report capabilities */
7617 total_len = sizeof(struct scsi_per_res_cap);
7619 case SPRI_RS: /* read full status */
7620 total_len = sizeof(struct scsi_per_res_in_header) +
7621 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7625 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7627 mtx_unlock(&lun->lun_lock);
7629 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7631 if (total_len < alloc_len) {
7632 ctsio->residual = alloc_len - total_len;
7633 ctsio->kern_data_len = total_len;
7634 ctsio->kern_total_len = total_len;
7636 ctsio->residual = 0;
7637 ctsio->kern_data_len = alloc_len;
7638 ctsio->kern_total_len = alloc_len;
7641 ctsio->kern_data_resid = 0;
7642 ctsio->kern_rel_offset = 0;
7643 ctsio->kern_sg_entries = 0;
7645 mtx_lock(&lun->lun_lock);
7646 switch (cdb->action) {
7647 case SPRI_RK: { // read keys
7648 struct scsi_per_res_in_keys *res_keys;
7651 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7654 * We had to drop the lock to allocate our buffer, which
7655 * leaves time for someone to come in with another
7656 * persistent reservation. (That is unlikely, though,
7657 * since this should be the only persistent reservation
7658 * command active right now.)
7660 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7661 (lun->pr_key_count *
7662 sizeof(struct scsi_per_res_key)))){
7663 mtx_unlock(&lun->lun_lock);
7664 free(ctsio->kern_data_ptr, M_CTL);
7665 printf("%s: reservation length changed, retrying\n",
7670 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7672 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7673 lun->pr_key_count, res_keys->header.length);
7675 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7676 if ((key = ctl_get_prkey(lun, i)) == 0)
7680 * We used lun->pr_key_count to calculate the
7681 * size to allocate. If it turns out the number of
7682 * initiators with the registered flag set is
7683 * larger than that (i.e. they haven't been kept in
7684 * sync), we've got a problem.
7686 if (key_count >= lun->pr_key_count) {
7690 scsi_u64to8b(key, res_keys->keys[key_count].key);
7695 case SPRI_RR: { // read reservation
7696 struct scsi_per_res_in_rsrv *res;
7697 int tmp_len, header_only;
7699 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7701 scsi_ulto4b(lun->pr_generation, res->header.generation);
7703 if (lun->flags & CTL_LUN_PR_RESERVED)
7705 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7706 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7707 res->header.length);
7710 tmp_len = sizeof(struct scsi_per_res_in_header);
7711 scsi_ulto4b(0, res->header.length);
7716 * We had to drop the lock to allocate our buffer, which
7717 * leaves time for someone to come in with another
7718 * persistent reservation. (That is unlikely, though,
7719 * since this should be the only persistent reservation
7720 * command active right now.)
7722 if (tmp_len != total_len) {
7723 mtx_unlock(&lun->lun_lock);
7724 free(ctsio->kern_data_ptr, M_CTL);
7725 printf("%s: reservation status changed, retrying\n",
7731 * No reservation held, so we're done.
7733 if (header_only != 0)
7737 * If the registration is an All Registrants type, the key
7738 * is 0, since it doesn't really matter.
7740 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7741 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7742 res->data.reservation);
7744 res->data.scopetype = lun->pr_res_type;
7747 case SPRI_RC: //report capabilities
7749 struct scsi_per_res_cap *res_cap;
7752 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7753 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7754 res_cap->flags1 = SPRI_CRH;
7755 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7756 type_mask = SPRI_TM_WR_EX_AR |
7762 scsi_ulto2b(type_mask, res_cap->type_mask);
7765 case SPRI_RS: { // read full status
7766 struct scsi_per_res_in_full *res_status;
7767 struct scsi_per_res_in_full_desc *res_desc;
7768 struct ctl_port *port;
7771 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7774 * We had to drop the lock to allocate our buffer, which
7775 * leaves time for someone to come in with another
7776 * persistent reservation. (That is unlikely, though,
7777 * since this should be the only persistent reservation
7778 * command active right now.)
7780 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7781 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7782 lun->pr_key_count)){
7783 mtx_unlock(&lun->lun_lock);
7784 free(ctsio->kern_data_ptr, M_CTL);
7785 printf("%s: reservation length changed, retrying\n",
7790 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7792 res_desc = &res_status->desc[0];
7793 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7794 if ((key = ctl_get_prkey(lun, i)) == 0)
7797 scsi_u64to8b(key, res_desc->res_key.key);
7798 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7799 (lun->pr_res_idx == i ||
7800 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7801 res_desc->flags = SPRI_FULL_R_HOLDER;
7802 res_desc->scopetype = lun->pr_res_type;
7804 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7805 res_desc->rel_trgt_port_id);
7807 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7809 len = ctl_create_iid(port,
7810 i % CTL_MAX_INIT_PER_PORT,
7811 res_desc->transport_id);
7812 scsi_ulto4b(len, res_desc->additional_length);
7813 res_desc = (struct scsi_per_res_in_full_desc *)
7814 &res_desc->transport_id[len];
7816 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7817 res_status->header.length);
7821 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7823 mtx_unlock(&lun->lun_lock);
7825 ctl_set_success(ctsio);
7826 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7827 ctsio->be_move_done = ctl_config_move_done;
7828 ctl_datamove((union ctl_io *)ctsio);
7829 return (CTL_RETVAL_COMPLETE);
7833 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7837 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7838 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7839 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7840 struct scsi_per_res_out_parms* param)
7842 union ctl_ha_msg persis_io;
7845 mtx_lock(&lun->lun_lock);
7846 if (sa_res_key == 0) {
7847 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7848 /* validate scope and type */
7849 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7851 mtx_unlock(&lun->lun_lock);
7852 ctl_set_invalid_field(/*ctsio*/ ctsio,
7858 ctl_done((union ctl_io *)ctsio);
7862 if (type>8 || type==2 || type==4 || type==0) {
7863 mtx_unlock(&lun->lun_lock);
7864 ctl_set_invalid_field(/*ctsio*/ ctsio,
7870 ctl_done((union ctl_io *)ctsio);
7875 * Unregister everybody else and build UA for
7878 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7879 if (i == residx || ctl_get_prkey(lun, i) == 0)
7882 ctl_clr_prkey(lun, i);
7883 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7885 lun->pr_key_count = 1;
7886 lun->pr_res_type = type;
7887 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7888 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7889 lun->pr_res_idx = residx;
7890 lun->pr_generation++;
7891 mtx_unlock(&lun->lun_lock);
7893 /* send msg to other side */
7894 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7895 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7896 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7897 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7898 persis_io.pr.pr_info.res_type = type;
7899 memcpy(persis_io.pr.pr_info.sa_res_key,
7900 param->serv_act_res_key,
7901 sizeof(param->serv_act_res_key));
7902 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7903 sizeof(persis_io.pr), M_WAITOK);
7905 /* not all registrants */
7906 mtx_unlock(&lun->lun_lock);
7907 free(ctsio->kern_data_ptr, M_CTL);
7908 ctl_set_invalid_field(ctsio,
7914 ctl_done((union ctl_io *)ctsio);
7917 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7918 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7921 if (res_key == sa_res_key) {
7924 * The spec implies this is not good but doesn't
7925 * say what to do. There are two choices either
7926 * generate a res conflict or check condition
7927 * with illegal field in parameter data. Since
7928 * that is what is done when the sa_res_key is
7929 * zero I'll take that approach since this has
7930 * to do with the sa_res_key.
7932 mtx_unlock(&lun->lun_lock);
7933 free(ctsio->kern_data_ptr, M_CTL);
7934 ctl_set_invalid_field(ctsio,
7940 ctl_done((union ctl_io *)ctsio);
7944 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7945 if (ctl_get_prkey(lun, i) != sa_res_key)
7949 ctl_clr_prkey(lun, i);
7950 lun->pr_key_count--;
7951 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7954 mtx_unlock(&lun->lun_lock);
7955 free(ctsio->kern_data_ptr, M_CTL);
7956 ctl_set_reservation_conflict(ctsio);
7957 ctl_done((union ctl_io *)ctsio);
7958 return (CTL_RETVAL_COMPLETE);
7960 lun->pr_generation++;
7961 mtx_unlock(&lun->lun_lock);
7963 /* send msg to other side */
7964 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7965 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7966 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7967 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7968 persis_io.pr.pr_info.res_type = type;
7969 memcpy(persis_io.pr.pr_info.sa_res_key,
7970 param->serv_act_res_key,
7971 sizeof(param->serv_act_res_key));
7972 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7973 sizeof(persis_io.pr), M_WAITOK);
7975 /* Reserved but not all registrants */
7976 /* sa_res_key is res holder */
7977 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7978 /* validate scope and type */
7979 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7981 mtx_unlock(&lun->lun_lock);
7982 ctl_set_invalid_field(/*ctsio*/ ctsio,
7988 ctl_done((union ctl_io *)ctsio);
7992 if (type>8 || type==2 || type==4 || type==0) {
7993 mtx_unlock(&lun->lun_lock);
7994 ctl_set_invalid_field(/*ctsio*/ ctsio,
8000 ctl_done((union ctl_io *)ctsio);
8006 * if sa_res_key != res_key remove all
8007 * registrants w/sa_res_key and generate UA
8008 * for these registrants(Registrations
8009 * Preempted) if it wasn't an exclusive
8010 * reservation generate UA(Reservations
8011 * Preempted) for all other registered nexuses
8012 * if the type has changed. Establish the new
8013 * reservation and holder. If res_key and
8014 * sa_res_key are the same do the above
8015 * except don't unregister the res holder.
8018 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8019 if (i == residx || ctl_get_prkey(lun, i) == 0)
8022 if (sa_res_key == ctl_get_prkey(lun, i)) {
8023 ctl_clr_prkey(lun, i);
8024 lun->pr_key_count--;
8025 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8026 } else if (type != lun->pr_res_type &&
8027 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8028 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8029 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8032 lun->pr_res_type = type;
8033 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8034 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8035 lun->pr_res_idx = residx;
8037 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8038 lun->pr_generation++;
8039 mtx_unlock(&lun->lun_lock);
8041 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8042 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8043 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8044 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8045 persis_io.pr.pr_info.res_type = type;
8046 memcpy(persis_io.pr.pr_info.sa_res_key,
8047 param->serv_act_res_key,
8048 sizeof(param->serv_act_res_key));
8049 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8050 sizeof(persis_io.pr), M_WAITOK);
8053 * sa_res_key is not the res holder just
8054 * remove registrants
8058 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8059 if (sa_res_key != ctl_get_prkey(lun, i))
8063 ctl_clr_prkey(lun, i);
8064 lun->pr_key_count--;
8065 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8069 mtx_unlock(&lun->lun_lock);
8070 free(ctsio->kern_data_ptr, M_CTL);
8071 ctl_set_reservation_conflict(ctsio);
8072 ctl_done((union ctl_io *)ctsio);
8075 lun->pr_generation++;
8076 mtx_unlock(&lun->lun_lock);
8078 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8079 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8080 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8081 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8082 persis_io.pr.pr_info.res_type = type;
8083 memcpy(persis_io.pr.pr_info.sa_res_key,
8084 param->serv_act_res_key,
8085 sizeof(param->serv_act_res_key));
8086 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8087 sizeof(persis_io.pr), M_WAITOK);
8094 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8096 uint64_t sa_res_key;
8099 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8101 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8102 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8103 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8104 if (sa_res_key == 0) {
8106 * Unregister everybody else and build UA for
8109 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8110 if (i == msg->pr.pr_info.residx ||
8111 ctl_get_prkey(lun, i) == 0)
8114 ctl_clr_prkey(lun, i);
8115 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8118 lun->pr_key_count = 1;
8119 lun->pr_res_type = msg->pr.pr_info.res_type;
8120 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8121 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8122 lun->pr_res_idx = msg->pr.pr_info.residx;
8124 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8125 if (sa_res_key == ctl_get_prkey(lun, i))
8128 ctl_clr_prkey(lun, i);
8129 lun->pr_key_count--;
8130 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8134 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8135 if (i == msg->pr.pr_info.residx ||
8136 ctl_get_prkey(lun, i) == 0)
8139 if (sa_res_key == ctl_get_prkey(lun, i)) {
8140 ctl_clr_prkey(lun, i);
8141 lun->pr_key_count--;
8142 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8143 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8144 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8145 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8146 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8149 lun->pr_res_type = msg->pr.pr_info.res_type;
8150 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8151 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8152 lun->pr_res_idx = msg->pr.pr_info.residx;
8154 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8156 lun->pr_generation++;
8162 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8165 u_int32_t param_len;
8166 struct scsi_per_res_out *cdb;
8167 struct ctl_lun *lun;
8168 struct scsi_per_res_out_parms* param;
8169 struct ctl_softc *softc;
8171 uint64_t res_key, sa_res_key, key;
8173 union ctl_ha_msg persis_io;
8176 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8178 retval = CTL_RETVAL_COMPLETE;
8180 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8181 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8182 softc = lun->ctl_softc;
8185 * We only support whole-LUN scope. The scope & type are ignored for
8186 * register, register and ignore existing key and clear.
8187 * We sometimes ignore scope and type on preempts too!!
8188 * Verify reservation type here as well.
8190 type = cdb->scope_type & SPR_TYPE_MASK;
8191 if ((cdb->action == SPRO_RESERVE)
8192 || (cdb->action == SPRO_RELEASE)) {
8193 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8194 ctl_set_invalid_field(/*ctsio*/ ctsio,
8200 ctl_done((union ctl_io *)ctsio);
8201 return (CTL_RETVAL_COMPLETE);
8204 if (type>8 || type==2 || type==4 || type==0) {
8205 ctl_set_invalid_field(/*ctsio*/ ctsio,
8211 ctl_done((union ctl_io *)ctsio);
8212 return (CTL_RETVAL_COMPLETE);
8216 param_len = scsi_4btoul(cdb->length);
8218 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8219 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8220 ctsio->kern_data_len = param_len;
8221 ctsio->kern_total_len = param_len;
8222 ctsio->kern_data_resid = 0;
8223 ctsio->kern_rel_offset = 0;
8224 ctsio->kern_sg_entries = 0;
8225 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8226 ctsio->be_move_done = ctl_config_move_done;
8227 ctl_datamove((union ctl_io *)ctsio);
8229 return (CTL_RETVAL_COMPLETE);
8232 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8234 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8235 res_key = scsi_8btou64(param->res_key.key);
8236 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8239 * Validate the reservation key here except for SPRO_REG_IGNO
8240 * This must be done for all other service actions
8242 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8243 mtx_lock(&lun->lun_lock);
8244 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8245 if (res_key != key) {
8247 * The current key passed in doesn't match
8248 * the one the initiator previously
8251 mtx_unlock(&lun->lun_lock);
8252 free(ctsio->kern_data_ptr, M_CTL);
8253 ctl_set_reservation_conflict(ctsio);
8254 ctl_done((union ctl_io *)ctsio);
8255 return (CTL_RETVAL_COMPLETE);
8257 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8259 * We are not registered
8261 mtx_unlock(&lun->lun_lock);
8262 free(ctsio->kern_data_ptr, M_CTL);
8263 ctl_set_reservation_conflict(ctsio);
8264 ctl_done((union ctl_io *)ctsio);
8265 return (CTL_RETVAL_COMPLETE);
8266 } else if (res_key != 0) {
8268 * We are not registered and trying to register but
8269 * the register key isn't zero.
8271 mtx_unlock(&lun->lun_lock);
8272 free(ctsio->kern_data_ptr, M_CTL);
8273 ctl_set_reservation_conflict(ctsio);
8274 ctl_done((union ctl_io *)ctsio);
8275 return (CTL_RETVAL_COMPLETE);
8277 mtx_unlock(&lun->lun_lock);
8280 switch (cdb->action & SPRO_ACTION_MASK) {
8282 case SPRO_REG_IGNO: {
8285 printf("Registration received\n");
8289 * We don't support any of these options, as we report in
8290 * the read capabilities request (see
8291 * ctl_persistent_reserve_in(), above).
8293 if ((param->flags & SPR_SPEC_I_PT)
8294 || (param->flags & SPR_ALL_TG_PT)
8295 || (param->flags & SPR_APTPL)) {
8298 if (param->flags & SPR_APTPL)
8300 else if (param->flags & SPR_ALL_TG_PT)
8302 else /* SPR_SPEC_I_PT */
8305 free(ctsio->kern_data_ptr, M_CTL);
8306 ctl_set_invalid_field(ctsio,
8312 ctl_done((union ctl_io *)ctsio);
8313 return (CTL_RETVAL_COMPLETE);
8316 mtx_lock(&lun->lun_lock);
8319 * The initiator wants to clear the
8322 if (sa_res_key == 0) {
8324 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8325 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8326 && ctl_get_prkey(lun, residx) == 0)) {
8327 mtx_unlock(&lun->lun_lock);
8331 ctl_clr_prkey(lun, residx);
8332 lun->pr_key_count--;
8334 if (residx == lun->pr_res_idx) {
8335 lun->flags &= ~CTL_LUN_PR_RESERVED;
8336 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8338 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8339 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8340 lun->pr_key_count) {
8342 * If the reservation is a registrants
8343 * only type we need to generate a UA
8344 * for other registered inits. The
8345 * sense code should be RESERVATIONS
8349 for (i = softc->init_min; i < softc->init_max; i++){
8350 if (ctl_get_prkey(lun, i) == 0)
8353 CTL_UA_RES_RELEASE);
8356 lun->pr_res_type = 0;
8357 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8358 if (lun->pr_key_count==0) {
8359 lun->flags &= ~CTL_LUN_PR_RESERVED;
8360 lun->pr_res_type = 0;
8361 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8364 lun->pr_generation++;
8365 mtx_unlock(&lun->lun_lock);
8367 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8368 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8369 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8370 persis_io.pr.pr_info.residx = residx;
8371 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8372 sizeof(persis_io.pr), M_WAITOK);
8373 } else /* sa_res_key != 0 */ {
8376 * If we aren't registered currently then increment
8377 * the key count and set the registered flag.
8379 ctl_alloc_prkey(lun, residx);
8380 if (ctl_get_prkey(lun, residx) == 0)
8381 lun->pr_key_count++;
8382 ctl_set_prkey(lun, residx, sa_res_key);
8383 lun->pr_generation++;
8384 mtx_unlock(&lun->lun_lock);
8386 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8387 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8388 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8389 persis_io.pr.pr_info.residx = residx;
8390 memcpy(persis_io.pr.pr_info.sa_res_key,
8391 param->serv_act_res_key,
8392 sizeof(param->serv_act_res_key));
8393 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8394 sizeof(persis_io.pr), M_WAITOK);
8401 printf("Reserve executed type %d\n", type);
8403 mtx_lock(&lun->lun_lock);
8404 if (lun->flags & CTL_LUN_PR_RESERVED) {
8406 * if this isn't the reservation holder and it's
8407 * not a "all registrants" type or if the type is
8408 * different then we have a conflict
8410 if ((lun->pr_res_idx != residx
8411 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8412 || lun->pr_res_type != type) {
8413 mtx_unlock(&lun->lun_lock);
8414 free(ctsio->kern_data_ptr, M_CTL);
8415 ctl_set_reservation_conflict(ctsio);
8416 ctl_done((union ctl_io *)ctsio);
8417 return (CTL_RETVAL_COMPLETE);
8419 mtx_unlock(&lun->lun_lock);
8420 } else /* create a reservation */ {
8422 * If it's not an "all registrants" type record
8423 * reservation holder
8425 if (type != SPR_TYPE_WR_EX_AR
8426 && type != SPR_TYPE_EX_AC_AR)
8427 lun->pr_res_idx = residx; /* Res holder */
8429 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8431 lun->flags |= CTL_LUN_PR_RESERVED;
8432 lun->pr_res_type = type;
8434 mtx_unlock(&lun->lun_lock);
8436 /* send msg to other side */
8437 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8438 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8439 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8440 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8441 persis_io.pr.pr_info.res_type = type;
8442 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8443 sizeof(persis_io.pr), M_WAITOK);
8448 mtx_lock(&lun->lun_lock);
8449 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8450 /* No reservation exists return good status */
8451 mtx_unlock(&lun->lun_lock);
8455 * Is this nexus a reservation holder?
8457 if (lun->pr_res_idx != residx
8458 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8460 * not a res holder return good status but
8463 mtx_unlock(&lun->lun_lock);
8467 if (lun->pr_res_type != type) {
8468 mtx_unlock(&lun->lun_lock);
8469 free(ctsio->kern_data_ptr, M_CTL);
8470 ctl_set_illegal_pr_release(ctsio);
8471 ctl_done((union ctl_io *)ctsio);
8472 return (CTL_RETVAL_COMPLETE);
8475 /* okay to release */
8476 lun->flags &= ~CTL_LUN_PR_RESERVED;
8477 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8478 lun->pr_res_type = 0;
8481 * If this isn't an exclusive access reservation and NUAR
8482 * is not set, generate UA for all other registrants.
8484 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8485 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8486 for (i = softc->init_min; i < softc->init_max; i++) {
8487 if (i == residx || ctl_get_prkey(lun, i) == 0)
8489 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8492 mtx_unlock(&lun->lun_lock);
8494 /* Send msg to other side */
8495 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8496 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8497 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8498 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8499 sizeof(persis_io.pr), M_WAITOK);
8503 /* send msg to other side */
8505 mtx_lock(&lun->lun_lock);
8506 lun->flags &= ~CTL_LUN_PR_RESERVED;
8507 lun->pr_res_type = 0;
8508 lun->pr_key_count = 0;
8509 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8511 ctl_clr_prkey(lun, residx);
8512 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8513 if (ctl_get_prkey(lun, i) != 0) {
8514 ctl_clr_prkey(lun, i);
8515 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8517 lun->pr_generation++;
8518 mtx_unlock(&lun->lun_lock);
8520 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8521 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8522 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8523 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8524 sizeof(persis_io.pr), M_WAITOK);
8528 case SPRO_PRE_ABO: {
8531 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8532 residx, ctsio, cdb, param);
8534 return (CTL_RETVAL_COMPLETE);
8538 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8542 free(ctsio->kern_data_ptr, M_CTL);
8543 ctl_set_success(ctsio);
8544 ctl_done((union ctl_io *)ctsio);
8550 * This routine is for handling a message from the other SC pertaining to
8551 * persistent reserve out. All the error checking will have been done
8552 * so only perorming the action need be done here to keep the two
8556 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8558 struct ctl_softc *softc = control_softc;
8559 struct ctl_lun *lun;
8561 uint32_t residx, targ_lun;
8563 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8564 mtx_lock(&softc->ctl_lock);
8565 if (targ_lun >= CTL_MAX_LUNS ||
8566 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8567 mtx_unlock(&softc->ctl_lock);
8570 mtx_lock(&lun->lun_lock);
8571 mtx_unlock(&softc->ctl_lock);
8572 if (lun->flags & CTL_LUN_DISABLED) {
8573 mtx_unlock(&lun->lun_lock);
8576 residx = ctl_get_initindex(&msg->hdr.nexus);
8577 switch(msg->pr.pr_info.action) {
8578 case CTL_PR_REG_KEY:
8579 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8580 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8581 lun->pr_key_count++;
8582 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8583 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8584 lun->pr_generation++;
8587 case CTL_PR_UNREG_KEY:
8588 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8589 lun->pr_key_count--;
8591 /* XXX Need to see if the reservation has been released */
8592 /* if so do we need to generate UA? */
8593 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8594 lun->flags &= ~CTL_LUN_PR_RESERVED;
8595 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8597 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8598 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8599 lun->pr_key_count) {
8601 * If the reservation is a registrants
8602 * only type we need to generate a UA
8603 * for other registered inits. The
8604 * sense code should be RESERVATIONS
8608 for (i = softc->init_min; i < softc->init_max; i++) {
8609 if (ctl_get_prkey(lun, i) == 0)
8612 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8615 lun->pr_res_type = 0;
8616 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8617 if (lun->pr_key_count==0) {
8618 lun->flags &= ~CTL_LUN_PR_RESERVED;
8619 lun->pr_res_type = 0;
8620 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8623 lun->pr_generation++;
8626 case CTL_PR_RESERVE:
8627 lun->flags |= CTL_LUN_PR_RESERVED;
8628 lun->pr_res_type = msg->pr.pr_info.res_type;
8629 lun->pr_res_idx = msg->pr.pr_info.residx;
8633 case CTL_PR_RELEASE:
8635 * If this isn't an exclusive access reservation and NUAR
8636 * is not set, generate UA for all other registrants.
8638 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8639 lun->pr_res_type != SPR_TYPE_WR_EX &&
8640 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8641 for (i = softc->init_min; i < softc->init_max; i++)
8642 if (i == residx || ctl_get_prkey(lun, i) == 0)
8644 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8647 lun->flags &= ~CTL_LUN_PR_RESERVED;
8648 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8649 lun->pr_res_type = 0;
8652 case CTL_PR_PREEMPT:
8653 ctl_pro_preempt_other(lun, msg);
8656 lun->flags &= ~CTL_LUN_PR_RESERVED;
8657 lun->pr_res_type = 0;
8658 lun->pr_key_count = 0;
8659 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8661 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8662 if (ctl_get_prkey(lun, i) == 0)
8664 ctl_clr_prkey(lun, i);
8665 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8667 lun->pr_generation++;
8671 mtx_unlock(&lun->lun_lock);
8675 ctl_read_write(struct ctl_scsiio *ctsio)
8677 struct ctl_lun *lun;
8678 struct ctl_lba_len_flags *lbalen;
8680 uint32_t num_blocks;
8684 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8686 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8689 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8690 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8691 switch (ctsio->cdb[0]) {
8694 struct scsi_rw_6 *cdb;
8696 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8698 lba = scsi_3btoul(cdb->addr);
8699 /* only 5 bits are valid in the most significant address byte */
8701 num_blocks = cdb->length;
8703 * This is correct according to SBC-2.
8705 if (num_blocks == 0)
8711 struct scsi_rw_10 *cdb;
8713 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8714 if (cdb->byte2 & SRW10_FUA)
8715 flags |= CTL_LLF_FUA;
8716 if (cdb->byte2 & SRW10_DPO)
8717 flags |= CTL_LLF_DPO;
8718 lba = scsi_4btoul(cdb->addr);
8719 num_blocks = scsi_2btoul(cdb->length);
8722 case WRITE_VERIFY_10: {
8723 struct scsi_write_verify_10 *cdb;
8725 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8726 flags |= CTL_LLF_FUA;
8727 if (cdb->byte2 & SWV_DPO)
8728 flags |= CTL_LLF_DPO;
8729 lba = scsi_4btoul(cdb->addr);
8730 num_blocks = scsi_2btoul(cdb->length);
8735 struct scsi_rw_12 *cdb;
8737 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8738 if (cdb->byte2 & SRW12_FUA)
8739 flags |= CTL_LLF_FUA;
8740 if (cdb->byte2 & SRW12_DPO)
8741 flags |= CTL_LLF_DPO;
8742 lba = scsi_4btoul(cdb->addr);
8743 num_blocks = scsi_4btoul(cdb->length);
8746 case WRITE_VERIFY_12: {
8747 struct scsi_write_verify_12 *cdb;
8749 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8750 flags |= CTL_LLF_FUA;
8751 if (cdb->byte2 & SWV_DPO)
8752 flags |= CTL_LLF_DPO;
8753 lba = scsi_4btoul(cdb->addr);
8754 num_blocks = scsi_4btoul(cdb->length);
8759 struct scsi_rw_16 *cdb;
8761 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8762 if (cdb->byte2 & SRW12_FUA)
8763 flags |= CTL_LLF_FUA;
8764 if (cdb->byte2 & SRW12_DPO)
8765 flags |= CTL_LLF_DPO;
8766 lba = scsi_8btou64(cdb->addr);
8767 num_blocks = scsi_4btoul(cdb->length);
8770 case WRITE_ATOMIC_16: {
8771 struct scsi_write_atomic_16 *cdb;
8773 if (lun->be_lun->atomicblock == 0) {
8774 ctl_set_invalid_opcode(ctsio);
8775 ctl_done((union ctl_io *)ctsio);
8776 return (CTL_RETVAL_COMPLETE);
8779 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8780 if (cdb->byte2 & SRW12_FUA)
8781 flags |= CTL_LLF_FUA;
8782 if (cdb->byte2 & SRW12_DPO)
8783 flags |= CTL_LLF_DPO;
8784 lba = scsi_8btou64(cdb->addr);
8785 num_blocks = scsi_2btoul(cdb->length);
8786 if (num_blocks > lun->be_lun->atomicblock) {
8787 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8788 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8790 ctl_done((union ctl_io *)ctsio);
8791 return (CTL_RETVAL_COMPLETE);
8795 case WRITE_VERIFY_16: {
8796 struct scsi_write_verify_16 *cdb;
8798 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8799 flags |= CTL_LLF_FUA;
8800 if (cdb->byte2 & SWV_DPO)
8801 flags |= CTL_LLF_DPO;
8802 lba = scsi_8btou64(cdb->addr);
8803 num_blocks = scsi_4btoul(cdb->length);
8808 * We got a command we don't support. This shouldn't
8809 * happen, commands should be filtered out above us.
8811 ctl_set_invalid_opcode(ctsio);
8812 ctl_done((union ctl_io *)ctsio);
8814 return (CTL_RETVAL_COMPLETE);
8815 break; /* NOTREACHED */
8819 * The first check is to make sure we're in bounds, the second
8820 * check is to catch wrap-around problems. If the lba + num blocks
8821 * is less than the lba, then we've wrapped around and the block
8822 * range is invalid anyway.
8824 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8825 || ((lba + num_blocks) < lba)) {
8826 ctl_set_lba_out_of_range(ctsio,
8827 MAX(lba, lun->be_lun->maxlba + 1));
8828 ctl_done((union ctl_io *)ctsio);
8829 return (CTL_RETVAL_COMPLETE);
8833 * According to SBC-3, a transfer length of 0 is not an error.
8834 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8835 * translates to 256 blocks for those commands.
8837 if (num_blocks == 0) {
8838 ctl_set_success(ctsio);
8839 ctl_done((union ctl_io *)ctsio);
8840 return (CTL_RETVAL_COMPLETE);
8843 /* Set FUA and/or DPO if caches are disabled. */
8845 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8846 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8848 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8849 flags |= CTL_LLF_FUA;
8852 lbalen = (struct ctl_lba_len_flags *)
8853 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8855 lbalen->len = num_blocks;
8856 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8858 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8859 ctsio->kern_rel_offset = 0;
8861 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8863 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8868 ctl_cnw_cont(union ctl_io *io)
8870 struct ctl_scsiio *ctsio;
8871 struct ctl_lun *lun;
8872 struct ctl_lba_len_flags *lbalen;
8875 ctsio = &io->scsiio;
8876 ctsio->io_hdr.status = CTL_STATUS_NONE;
8877 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8878 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8879 lbalen = (struct ctl_lba_len_flags *)
8880 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8881 lbalen->flags &= ~CTL_LLF_COMPARE;
8882 lbalen->flags |= CTL_LLF_WRITE;
8884 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8885 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8890 ctl_cnw(struct ctl_scsiio *ctsio)
8892 struct ctl_lun *lun;
8893 struct ctl_lba_len_flags *lbalen;
8895 uint32_t num_blocks;
8898 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8900 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8903 switch (ctsio->cdb[0]) {
8904 case COMPARE_AND_WRITE: {
8905 struct scsi_compare_and_write *cdb;
8907 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8908 if (cdb->byte2 & SRW10_FUA)
8909 flags |= CTL_LLF_FUA;
8910 if (cdb->byte2 & SRW10_DPO)
8911 flags |= CTL_LLF_DPO;
8912 lba = scsi_8btou64(cdb->addr);
8913 num_blocks = cdb->length;
8918 * We got a command we don't support. This shouldn't
8919 * happen, commands should be filtered out above us.
8921 ctl_set_invalid_opcode(ctsio);
8922 ctl_done((union ctl_io *)ctsio);
8924 return (CTL_RETVAL_COMPLETE);
8925 break; /* NOTREACHED */
8929 * The first check is to make sure we're in bounds, the second
8930 * check is to catch wrap-around problems. If the lba + num blocks
8931 * is less than the lba, then we've wrapped around and the block
8932 * range is invalid anyway.
8934 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8935 || ((lba + num_blocks) < lba)) {
8936 ctl_set_lba_out_of_range(ctsio,
8937 MAX(lba, lun->be_lun->maxlba + 1));
8938 ctl_done((union ctl_io *)ctsio);
8939 return (CTL_RETVAL_COMPLETE);
8943 * According to SBC-3, a transfer length of 0 is not an error.
8945 if (num_blocks == 0) {
8946 ctl_set_success(ctsio);
8947 ctl_done((union ctl_io *)ctsio);
8948 return (CTL_RETVAL_COMPLETE);
8951 /* Set FUA if write cache is disabled. */
8952 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8953 flags |= CTL_LLF_FUA;
8955 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8956 ctsio->kern_rel_offset = 0;
8959 * Set the IO_CONT flag, so that if this I/O gets passed to
8960 * ctl_data_submit_done(), it'll get passed back to
8961 * ctl_ctl_cnw_cont() for further processing.
8963 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8964 ctsio->io_cont = ctl_cnw_cont;
8966 lbalen = (struct ctl_lba_len_flags *)
8967 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8969 lbalen->len = num_blocks;
8970 lbalen->flags = CTL_LLF_COMPARE | flags;
8972 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8973 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8978 ctl_verify(struct ctl_scsiio *ctsio)
8980 struct ctl_lun *lun;
8981 struct ctl_lba_len_flags *lbalen;
8983 uint32_t num_blocks;
8987 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8989 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8992 flags = CTL_LLF_FUA;
8993 switch (ctsio->cdb[0]) {
8995 struct scsi_verify_10 *cdb;
8997 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8998 if (cdb->byte2 & SVFY_BYTCHK)
9000 if (cdb->byte2 & SVFY_DPO)
9001 flags |= CTL_LLF_DPO;
9002 lba = scsi_4btoul(cdb->addr);
9003 num_blocks = scsi_2btoul(cdb->length);
9007 struct scsi_verify_12 *cdb;
9009 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9010 if (cdb->byte2 & SVFY_BYTCHK)
9012 if (cdb->byte2 & SVFY_DPO)
9013 flags |= CTL_LLF_DPO;
9014 lba = scsi_4btoul(cdb->addr);
9015 num_blocks = scsi_4btoul(cdb->length);
9019 struct scsi_rw_16 *cdb;
9021 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9022 if (cdb->byte2 & SVFY_BYTCHK)
9024 if (cdb->byte2 & SVFY_DPO)
9025 flags |= CTL_LLF_DPO;
9026 lba = scsi_8btou64(cdb->addr);
9027 num_blocks = scsi_4btoul(cdb->length);
9032 * We got a command we don't support. This shouldn't
9033 * happen, commands should be filtered out above us.
9035 ctl_set_invalid_opcode(ctsio);
9036 ctl_done((union ctl_io *)ctsio);
9037 return (CTL_RETVAL_COMPLETE);
9041 * The first check is to make sure we're in bounds, the second
9042 * check is to catch wrap-around problems. If the lba + num blocks
9043 * is less than the lba, then we've wrapped around and the block
9044 * range is invalid anyway.
9046 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9047 || ((lba + num_blocks) < lba)) {
9048 ctl_set_lba_out_of_range(ctsio,
9049 MAX(lba, lun->be_lun->maxlba + 1));
9050 ctl_done((union ctl_io *)ctsio);
9051 return (CTL_RETVAL_COMPLETE);
9055 * According to SBC-3, a transfer length of 0 is not an error.
9057 if (num_blocks == 0) {
9058 ctl_set_success(ctsio);
9059 ctl_done((union ctl_io *)ctsio);
9060 return (CTL_RETVAL_COMPLETE);
9063 lbalen = (struct ctl_lba_len_flags *)
9064 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9066 lbalen->len = num_blocks;
9068 lbalen->flags = CTL_LLF_COMPARE | flags;
9069 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9071 lbalen->flags = CTL_LLF_VERIFY | flags;
9072 ctsio->kern_total_len = 0;
9074 ctsio->kern_rel_offset = 0;
9076 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9077 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9082 ctl_report_luns(struct ctl_scsiio *ctsio)
9084 struct ctl_softc *softc;
9085 struct scsi_report_luns *cdb;
9086 struct scsi_report_luns_data *lun_data;
9087 struct ctl_lun *lun, *request_lun;
9088 struct ctl_port *port;
9089 int num_filled, num_luns, num_port_luns, retval;
9090 uint32_t alloc_len, lun_datalen;
9091 uint32_t initidx, targ_lun_id, lun_id;
9093 retval = CTL_RETVAL_COMPLETE;
9094 cdb = (struct scsi_report_luns *)ctsio->cdb;
9095 port = ctl_io_port(&ctsio->io_hdr);
9096 softc = port->ctl_softc;
9098 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9101 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS;
9102 mtx_lock(&softc->ctl_lock);
9103 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9104 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9107 mtx_unlock(&softc->ctl_lock);
9109 switch (cdb->select_report) {
9110 case RPL_REPORT_DEFAULT:
9111 case RPL_REPORT_ALL:
9112 case RPL_REPORT_NONSUBSID:
9114 case RPL_REPORT_WELLKNOWN:
9115 case RPL_REPORT_ADMIN:
9116 case RPL_REPORT_CONGLOM:
9120 ctl_set_invalid_field(ctsio,
9126 ctl_done((union ctl_io *)ctsio);
9128 break; /* NOTREACHED */
9131 alloc_len = scsi_4btoul(cdb->length);
9133 * The initiator has to allocate at least 16 bytes for this request,
9134 * so he can at least get the header and the first LUN. Otherwise
9135 * we reject the request (per SPC-3 rev 14, section 6.21).
9137 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9138 sizeof(struct scsi_report_luns_lundata))) {
9139 ctl_set_invalid_field(ctsio,
9145 ctl_done((union ctl_io *)ctsio);
9149 request_lun = (struct ctl_lun *)
9150 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9152 lun_datalen = sizeof(*lun_data) +
9153 (num_luns * sizeof(struct scsi_report_luns_lundata));
9155 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9156 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9157 ctsio->kern_sg_entries = 0;
9159 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9161 mtx_lock(&softc->ctl_lock);
9162 for (targ_lun_id = 0, num_filled = 0;
9163 targ_lun_id < num_port_luns && num_filled < num_luns;
9165 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9166 if (lun_id == UINT32_MAX)
9168 lun = softc->ctl_luns[lun_id];
9172 be64enc(lun_data->luns[num_filled++].lundata,
9173 ctl_encode_lun(targ_lun_id));
9176 * According to SPC-3, rev 14 section 6.21:
9178 * "The execution of a REPORT LUNS command to any valid and
9179 * installed logical unit shall clear the REPORTED LUNS DATA
9180 * HAS CHANGED unit attention condition for all logical
9181 * units of that target with respect to the requesting
9182 * initiator. A valid and installed logical unit is one
9183 * having a PERIPHERAL QUALIFIER of 000b in the standard
9184 * INQUIRY data (see 6.4.2)."
9186 * If request_lun is NULL, the LUN this report luns command
9187 * was issued to is either disabled or doesn't exist. In that
9188 * case, we shouldn't clear any pending lun change unit
9191 if (request_lun != NULL) {
9192 mtx_lock(&lun->lun_lock);
9193 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9194 mtx_unlock(&lun->lun_lock);
9197 mtx_unlock(&softc->ctl_lock);
9200 * It's quite possible that we've returned fewer LUNs than we allocated
9201 * space for. Trim it.
9203 lun_datalen = sizeof(*lun_data) +
9204 (num_filled * sizeof(struct scsi_report_luns_lundata));
9206 if (lun_datalen < alloc_len) {
9207 ctsio->residual = alloc_len - lun_datalen;
9208 ctsio->kern_data_len = lun_datalen;
9209 ctsio->kern_total_len = lun_datalen;
9211 ctsio->residual = 0;
9212 ctsio->kern_data_len = alloc_len;
9213 ctsio->kern_total_len = alloc_len;
9215 ctsio->kern_data_resid = 0;
9216 ctsio->kern_rel_offset = 0;
9217 ctsio->kern_sg_entries = 0;
9220 * We set this to the actual data length, regardless of how much
9221 * space we actually have to return results. If the user looks at
9222 * this value, he'll know whether or not he allocated enough space
9223 * and reissue the command if necessary. We don't support well
9224 * known logical units, so if the user asks for that, return none.
9226 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9229 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9232 ctl_set_success(ctsio);
9233 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9234 ctsio->be_move_done = ctl_config_move_done;
9235 ctl_datamove((union ctl_io *)ctsio);
9240 ctl_request_sense(struct ctl_scsiio *ctsio)
9242 struct scsi_request_sense *cdb;
9243 struct scsi_sense_data *sense_ptr;
9244 struct ctl_softc *softc;
9245 struct ctl_lun *lun;
9248 u_int sense_len = SSD_FULL_SIZE;
9249 scsi_sense_data_type sense_format;
9250 ctl_ua_type ua_type;
9251 uint8_t asc = 0, ascq = 0;
9253 cdb = (struct scsi_request_sense *)ctsio->cdb;
9255 softc = control_softc;
9256 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9258 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9261 * Determine which sense format the user wants.
9263 if (cdb->byte2 & SRS_DESC)
9264 sense_format = SSD_TYPE_DESC;
9266 sense_format = SSD_TYPE_FIXED;
9268 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9269 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9270 ctsio->kern_sg_entries = 0;
9273 * struct scsi_sense_data, which is currently set to 256 bytes, is
9274 * larger than the largest allowed value for the length field in the
9275 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9277 ctsio->residual = 0;
9278 ctsio->kern_data_len = cdb->length;
9279 ctsio->kern_total_len = cdb->length;
9281 ctsio->kern_data_resid = 0;
9282 ctsio->kern_rel_offset = 0;
9283 ctsio->kern_sg_entries = 0;
9286 * If we don't have a LUN, we don't have any pending sense.
9289 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9290 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9291 /* "Logical unit not supported" */
9292 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9293 /*current_error*/ 1,
9294 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9302 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9304 * Check for pending sense, and then for pending unit attentions.
9305 * Pending sense gets returned first, then pending unit attentions.
9307 mtx_lock(&lun->lun_lock);
9309 if (ctl_is_set(lun->have_ca, initidx)) {
9310 scsi_sense_data_type stored_format;
9313 * Check to see which sense format was used for the stored
9316 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9319 * If the user requested a different sense format than the
9320 * one we stored, then we need to convert it to the other
9321 * format. If we're going from descriptor to fixed format
9322 * sense data, we may lose things in translation, depending
9323 * on what options were used.
9325 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9326 * for some reason we'll just copy it out as-is.
9328 if ((stored_format == SSD_TYPE_FIXED)
9329 && (sense_format == SSD_TYPE_DESC))
9330 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9331 &lun->pending_sense[initidx],
9332 (struct scsi_sense_data_desc *)sense_ptr);
9333 else if ((stored_format == SSD_TYPE_DESC)
9334 && (sense_format == SSD_TYPE_FIXED))
9335 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9336 &lun->pending_sense[initidx],
9337 (struct scsi_sense_data_fixed *)sense_ptr);
9339 memcpy(sense_ptr, &lun->pending_sense[initidx],
9340 MIN(sizeof(*sense_ptr),
9341 sizeof(lun->pending_sense[initidx])));
9343 ctl_clear_mask(lun->have_ca, initidx);
9347 if (have_error == 0) {
9348 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9350 if (ua_type != CTL_UA_NONE)
9353 if (have_error == 0) {
9355 * Report informational exception if have one and allowed.
9357 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9359 ascq = lun->ie_ascq;
9361 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9362 /*current_error*/ 1,
9363 /*sense_key*/ SSD_KEY_NO_SENSE,
9368 mtx_unlock(&lun->lun_lock);
9372 * We report the SCSI status as OK, since the status of the command
9373 * itself is OK. We're reporting sense as parameter data.
9375 ctl_set_success(ctsio);
9376 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9377 ctsio->be_move_done = ctl_config_move_done;
9378 ctl_datamove((union ctl_io *)ctsio);
9379 return (CTL_RETVAL_COMPLETE);
9383 ctl_tur(struct ctl_scsiio *ctsio)
9386 CTL_DEBUG_PRINT(("ctl_tur\n"));
9388 ctl_set_success(ctsio);
9389 ctl_done((union ctl_io *)ctsio);
9391 return (CTL_RETVAL_COMPLETE);
9395 * SCSI VPD page 0x00, the Supported VPD Pages page.
9398 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9400 struct scsi_vpd_supported_pages *pages;
9402 struct ctl_lun *lun;
9405 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9407 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9408 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9409 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9410 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9411 ctsio->kern_sg_entries = 0;
9413 if (sup_page_size < alloc_len) {
9414 ctsio->residual = alloc_len - sup_page_size;
9415 ctsio->kern_data_len = sup_page_size;
9416 ctsio->kern_total_len = sup_page_size;
9418 ctsio->residual = 0;
9419 ctsio->kern_data_len = alloc_len;
9420 ctsio->kern_total_len = alloc_len;
9422 ctsio->kern_data_resid = 0;
9423 ctsio->kern_rel_offset = 0;
9424 ctsio->kern_sg_entries = 0;
9427 * The control device is always connected. The disk device, on the
9428 * other hand, may not be online all the time. Need to change this
9429 * to figure out whether the disk device is actually online or not.
9432 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9433 lun->be_lun->lun_type;
9435 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9438 /* Supported VPD pages */
9439 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9441 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9442 /* Device Identification */
9443 pages->page_list[p++] = SVPD_DEVICE_ID;
9444 /* Extended INQUIRY Data */
9445 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9446 /* Mode Page Policy */
9447 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9449 pages->page_list[p++] = SVPD_SCSI_PORTS;
9450 /* Third-party Copy */
9451 pages->page_list[p++] = SVPD_SCSI_TPC;
9452 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9454 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9455 /* Block Device Characteristics */
9456 pages->page_list[p++] = SVPD_BDC;
9457 /* Logical Block Provisioning */
9458 pages->page_list[p++] = SVPD_LBP;
9462 ctl_set_success(ctsio);
9463 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9464 ctsio->be_move_done = ctl_config_move_done;
9465 ctl_datamove((union ctl_io *)ctsio);
9466 return (CTL_RETVAL_COMPLETE);
9470 * SCSI VPD page 0x80, the Unit Serial Number page.
9473 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9475 struct scsi_vpd_unit_serial_number *sn_ptr;
9476 struct ctl_lun *lun;
9479 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9481 data_len = 4 + CTL_SN_LEN;
9482 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9483 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9484 if (data_len < alloc_len) {
9485 ctsio->residual = alloc_len - data_len;
9486 ctsio->kern_data_len = data_len;
9487 ctsio->kern_total_len = data_len;
9489 ctsio->residual = 0;
9490 ctsio->kern_data_len = alloc_len;
9491 ctsio->kern_total_len = alloc_len;
9493 ctsio->kern_data_resid = 0;
9494 ctsio->kern_rel_offset = 0;
9495 ctsio->kern_sg_entries = 0;
9498 * The control device is always connected. The disk device, on the
9499 * other hand, may not be online all the time. Need to change this
9500 * to figure out whether the disk device is actually online or not.
9503 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9504 lun->be_lun->lun_type;
9506 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9508 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9509 sn_ptr->length = CTL_SN_LEN;
9511 * If we don't have a LUN, we just leave the serial number as
9515 strncpy((char *)sn_ptr->serial_num,
9516 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9518 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9520 ctl_set_success(ctsio);
9521 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9522 ctsio->be_move_done = ctl_config_move_done;
9523 ctl_datamove((union ctl_io *)ctsio);
9524 return (CTL_RETVAL_COMPLETE);
9529 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9532 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9534 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9535 struct ctl_lun *lun;
9538 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9540 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9541 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9542 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9543 ctsio->kern_sg_entries = 0;
9545 if (data_len < alloc_len) {
9546 ctsio->residual = alloc_len - data_len;
9547 ctsio->kern_data_len = data_len;
9548 ctsio->kern_total_len = data_len;
9550 ctsio->residual = 0;
9551 ctsio->kern_data_len = alloc_len;
9552 ctsio->kern_total_len = alloc_len;
9554 ctsio->kern_data_resid = 0;
9555 ctsio->kern_rel_offset = 0;
9556 ctsio->kern_sg_entries = 0;
9559 * The control device is always connected. The disk device, on the
9560 * other hand, may not be online all the time.
9563 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9564 lun->be_lun->lun_type;
9566 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9567 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9568 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9570 * We support head of queue, ordered and simple tags.
9572 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9574 * Volatile cache supported.
9576 eid_ptr->flags3 = SVPD_EID_V_SUP;
9579 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9580 * attention for a particular IT nexus on all LUNs once we report
9581 * it to that nexus once. This bit is required as of SPC-4.
9583 eid_ptr->flags4 = SVPD_EID_LUICLR;
9586 * We support revert to defaults (RTD) bit in MODE SELECT.
9588 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9591 * XXX KDM in order to correctly answer this, we would need
9592 * information from the SIM to determine how much sense data it
9593 * can send. So this would really be a path inquiry field, most
9594 * likely. This can be set to a maximum of 252 according to SPC-4,
9595 * but the hardware may or may not be able to support that much.
9596 * 0 just means that the maximum sense data length is not reported.
9598 eid_ptr->max_sense_length = 0;
9600 ctl_set_success(ctsio);
9601 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9602 ctsio->be_move_done = ctl_config_move_done;
9603 ctl_datamove((union ctl_io *)ctsio);
9604 return (CTL_RETVAL_COMPLETE);
9608 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9610 struct scsi_vpd_mode_page_policy *mpp_ptr;
9611 struct ctl_lun *lun;
9614 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9616 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9617 sizeof(struct scsi_vpd_mode_page_policy_descr);
9619 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9620 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9621 ctsio->kern_sg_entries = 0;
9623 if (data_len < alloc_len) {
9624 ctsio->residual = alloc_len - data_len;
9625 ctsio->kern_data_len = data_len;
9626 ctsio->kern_total_len = data_len;
9628 ctsio->residual = 0;
9629 ctsio->kern_data_len = alloc_len;
9630 ctsio->kern_total_len = alloc_len;
9632 ctsio->kern_data_resid = 0;
9633 ctsio->kern_rel_offset = 0;
9634 ctsio->kern_sg_entries = 0;
9637 * The control device is always connected. The disk device, on the
9638 * other hand, may not be online all the time.
9641 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9642 lun->be_lun->lun_type;
9644 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9645 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9646 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9647 mpp_ptr->descr[0].page_code = 0x3f;
9648 mpp_ptr->descr[0].subpage_code = 0xff;
9649 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9651 ctl_set_success(ctsio);
9652 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9653 ctsio->be_move_done = ctl_config_move_done;
9654 ctl_datamove((union ctl_io *)ctsio);
9655 return (CTL_RETVAL_COMPLETE);
9659 * SCSI VPD page 0x83, the Device Identification page.
9662 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9664 struct scsi_vpd_device_id *devid_ptr;
9665 struct scsi_vpd_id_descriptor *desc;
9666 struct ctl_softc *softc;
9667 struct ctl_lun *lun;
9668 struct ctl_port *port;
9672 softc = control_softc;
9674 port = ctl_io_port(&ctsio->io_hdr);
9675 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9677 data_len = sizeof(struct scsi_vpd_device_id) +
9678 sizeof(struct scsi_vpd_id_descriptor) +
9679 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9680 sizeof(struct scsi_vpd_id_descriptor) +
9681 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9682 if (lun && lun->lun_devid)
9683 data_len += lun->lun_devid->len;
9684 if (port && port->port_devid)
9685 data_len += port->port_devid->len;
9686 if (port && port->target_devid)
9687 data_len += port->target_devid->len;
9689 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9690 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9691 ctsio->kern_sg_entries = 0;
9693 if (data_len < alloc_len) {
9694 ctsio->residual = alloc_len - data_len;
9695 ctsio->kern_data_len = data_len;
9696 ctsio->kern_total_len = data_len;
9698 ctsio->residual = 0;
9699 ctsio->kern_data_len = alloc_len;
9700 ctsio->kern_total_len = alloc_len;
9702 ctsio->kern_data_resid = 0;
9703 ctsio->kern_rel_offset = 0;
9704 ctsio->kern_sg_entries = 0;
9707 * The control device is always connected. The disk device, on the
9708 * other hand, may not be online all the time.
9711 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9712 lun->be_lun->lun_type;
9714 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9715 devid_ptr->page_code = SVPD_DEVICE_ID;
9716 scsi_ulto2b(data_len - 4, devid_ptr->length);
9718 if (port && port->port_type == CTL_PORT_FC)
9719 proto = SCSI_PROTO_FC << 4;
9720 else if (port && port->port_type == CTL_PORT_ISCSI)
9721 proto = SCSI_PROTO_ISCSI << 4;
9723 proto = SCSI_PROTO_SPI << 4;
9724 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9727 * We're using a LUN association here. i.e., this device ID is a
9728 * per-LUN identifier.
9730 if (lun && lun->lun_devid) {
9731 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9732 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9733 lun->lun_devid->len);
9737 * This is for the WWPN which is a port association.
9739 if (port && port->port_devid) {
9740 memcpy(desc, port->port_devid->data, port->port_devid->len);
9741 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9742 port->port_devid->len);
9746 * This is for the Relative Target Port(type 4h) identifier
9748 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9749 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9750 SVPD_ID_TYPE_RELTARG;
9752 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9753 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9754 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9757 * This is for the Target Port Group(type 5h) identifier
9759 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9760 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9761 SVPD_ID_TYPE_TPORTGRP;
9763 if (softc->is_single ||
9764 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9767 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9768 scsi_ulto2b(g, &desc->identifier[2]);
9769 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9770 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9773 * This is for the Target identifier
9775 if (port && port->target_devid) {
9776 memcpy(desc, port->target_devid->data, port->target_devid->len);
9779 ctl_set_success(ctsio);
9780 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9781 ctsio->be_move_done = ctl_config_move_done;
9782 ctl_datamove((union ctl_io *)ctsio);
9783 return (CTL_RETVAL_COMPLETE);
9787 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9789 struct ctl_softc *softc = control_softc;
9790 struct scsi_vpd_scsi_ports *sp;
9791 struct scsi_vpd_port_designation *pd;
9792 struct scsi_vpd_port_designation_cont *pdc;
9793 struct ctl_lun *lun;
9794 struct ctl_port *port;
9795 int data_len, num_target_ports, iid_len, id_len;
9797 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9799 num_target_ports = 0;
9802 mtx_lock(&softc->ctl_lock);
9803 STAILQ_FOREACH(port, &softc->port_list, links) {
9804 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9807 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9810 if (port->init_devid)
9811 iid_len += port->init_devid->len;
9812 if (port->port_devid)
9813 id_len += port->port_devid->len;
9815 mtx_unlock(&softc->ctl_lock);
9817 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9818 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9819 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9820 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9821 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9822 ctsio->kern_sg_entries = 0;
9824 if (data_len < alloc_len) {
9825 ctsio->residual = alloc_len - data_len;
9826 ctsio->kern_data_len = data_len;
9827 ctsio->kern_total_len = data_len;
9829 ctsio->residual = 0;
9830 ctsio->kern_data_len = alloc_len;
9831 ctsio->kern_total_len = alloc_len;
9833 ctsio->kern_data_resid = 0;
9834 ctsio->kern_rel_offset = 0;
9835 ctsio->kern_sg_entries = 0;
9838 * The control device is always connected. The disk device, on the
9839 * other hand, may not be online all the time. Need to change this
9840 * to figure out whether the disk device is actually online or not.
9843 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9844 lun->be_lun->lun_type;
9846 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9848 sp->page_code = SVPD_SCSI_PORTS;
9849 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9851 pd = &sp->design[0];
9853 mtx_lock(&softc->ctl_lock);
9854 STAILQ_FOREACH(port, &softc->port_list, links) {
9855 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9858 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9860 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9861 if (port->init_devid) {
9862 iid_len = port->init_devid->len;
9863 memcpy(pd->initiator_transportid,
9864 port->init_devid->data, port->init_devid->len);
9867 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9868 pdc = (struct scsi_vpd_port_designation_cont *)
9869 (&pd->initiator_transportid[iid_len]);
9870 if (port->port_devid) {
9871 id_len = port->port_devid->len;
9872 memcpy(pdc->target_port_descriptors,
9873 port->port_devid->data, port->port_devid->len);
9876 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9877 pd = (struct scsi_vpd_port_designation *)
9878 ((uint8_t *)pdc->target_port_descriptors + id_len);
9880 mtx_unlock(&softc->ctl_lock);
9882 ctl_set_success(ctsio);
9883 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9884 ctsio->be_move_done = ctl_config_move_done;
9885 ctl_datamove((union ctl_io *)ctsio);
9886 return (CTL_RETVAL_COMPLETE);
9890 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9892 struct scsi_vpd_block_limits *bl_ptr;
9893 struct ctl_lun *lun;
9896 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9898 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9899 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9900 ctsio->kern_sg_entries = 0;
9902 if (sizeof(*bl_ptr) < alloc_len) {
9903 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9904 ctsio->kern_data_len = sizeof(*bl_ptr);
9905 ctsio->kern_total_len = sizeof(*bl_ptr);
9907 ctsio->residual = 0;
9908 ctsio->kern_data_len = alloc_len;
9909 ctsio->kern_total_len = alloc_len;
9911 ctsio->kern_data_resid = 0;
9912 ctsio->kern_rel_offset = 0;
9913 ctsio->kern_sg_entries = 0;
9916 * The control device is always connected. The disk device, on the
9917 * other hand, may not be online all the time. Need to change this
9918 * to figure out whether the disk device is actually online or not.
9921 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9922 lun->be_lun->lun_type;
9924 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9926 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9927 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9928 bl_ptr->max_cmp_write_len = 0xff;
9929 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9931 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9932 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9934 ctl_get_opt_number(&lun->be_lun->options,
9935 "unmap_max_lba", &ival);
9936 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9938 ctl_get_opt_number(&lun->be_lun->options,
9939 "unmap_max_descr", &ival);
9940 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9941 if (lun->be_lun->ublockexp != 0) {
9942 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9943 bl_ptr->opt_unmap_grain);
9944 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9945 bl_ptr->unmap_grain_align);
9948 scsi_ulto4b(lun->be_lun->atomicblock,
9949 bl_ptr->max_atomic_transfer_length);
9950 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9951 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9952 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9953 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9955 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9956 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9959 ctl_set_success(ctsio);
9960 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9961 ctsio->be_move_done = ctl_config_move_done;
9962 ctl_datamove((union ctl_io *)ctsio);
9963 return (CTL_RETVAL_COMPLETE);
9967 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9969 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9970 struct ctl_lun *lun;
9974 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9976 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9977 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9978 ctsio->kern_sg_entries = 0;
9980 if (sizeof(*bdc_ptr) < alloc_len) {
9981 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9982 ctsio->kern_data_len = sizeof(*bdc_ptr);
9983 ctsio->kern_total_len = sizeof(*bdc_ptr);
9985 ctsio->residual = 0;
9986 ctsio->kern_data_len = alloc_len;
9987 ctsio->kern_total_len = alloc_len;
9989 ctsio->kern_data_resid = 0;
9990 ctsio->kern_rel_offset = 0;
9991 ctsio->kern_sg_entries = 0;
9994 * The control device is always connected. The disk device, on the
9995 * other hand, may not be online all the time. Need to change this
9996 * to figure out whether the disk device is actually online or not.
9999 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10000 lun->be_lun->lun_type;
10002 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10003 bdc_ptr->page_code = SVPD_BDC;
10004 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10006 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10007 i = strtol(value, NULL, 0);
10009 i = CTL_DEFAULT_ROTATION_RATE;
10010 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10012 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10013 i = strtol(value, NULL, 0);
10016 bdc_ptr->wab_wac_ff = (i & 0x0f);
10017 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10019 ctl_set_success(ctsio);
10020 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10021 ctsio->be_move_done = ctl_config_move_done;
10022 ctl_datamove((union ctl_io *)ctsio);
10023 return (CTL_RETVAL_COMPLETE);
10027 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10029 struct scsi_vpd_logical_block_prov *lbp_ptr;
10030 struct ctl_lun *lun;
10033 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10035 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10036 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10037 ctsio->kern_sg_entries = 0;
10039 if (sizeof(*lbp_ptr) < alloc_len) {
10040 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10041 ctsio->kern_data_len = sizeof(*lbp_ptr);
10042 ctsio->kern_total_len = sizeof(*lbp_ptr);
10044 ctsio->residual = 0;
10045 ctsio->kern_data_len = alloc_len;
10046 ctsio->kern_total_len = alloc_len;
10048 ctsio->kern_data_resid = 0;
10049 ctsio->kern_rel_offset = 0;
10050 ctsio->kern_sg_entries = 0;
10053 * The control device is always connected. The disk device, on the
10054 * other hand, may not be online all the time. Need to change this
10055 * to figure out whether the disk device is actually online or not.
10058 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10059 lun->be_lun->lun_type;
10061 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10063 lbp_ptr->page_code = SVPD_LBP;
10064 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10065 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10066 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10067 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10068 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10069 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
10070 if (value != NULL) {
10071 if (strcmp(value, "resource") == 0)
10072 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10073 else if (strcmp(value, "thin") == 0)
10074 lbp_ptr->prov_type = SVPD_LBP_THIN;
10076 lbp_ptr->prov_type = SVPD_LBP_THIN;
10079 ctl_set_success(ctsio);
10080 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10081 ctsio->be_move_done = ctl_config_move_done;
10082 ctl_datamove((union ctl_io *)ctsio);
10083 return (CTL_RETVAL_COMPLETE);
10087 * INQUIRY with the EVPD bit set.
10090 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10092 struct ctl_lun *lun;
10093 struct scsi_inquiry *cdb;
10094 int alloc_len, retval;
10096 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10097 cdb = (struct scsi_inquiry *)ctsio->cdb;
10098 alloc_len = scsi_2btoul(cdb->length);
10100 switch (cdb->page_code) {
10101 case SVPD_SUPPORTED_PAGES:
10102 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10104 case SVPD_UNIT_SERIAL_NUMBER:
10105 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10107 case SVPD_DEVICE_ID:
10108 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10110 case SVPD_EXTENDED_INQUIRY_DATA:
10111 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10113 case SVPD_MODE_PAGE_POLICY:
10114 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10116 case SVPD_SCSI_PORTS:
10117 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10119 case SVPD_SCSI_TPC:
10120 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10122 case SVPD_BLOCK_LIMITS:
10123 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10125 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10128 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10130 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10133 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10135 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10139 ctl_set_invalid_field(ctsio,
10145 ctl_done((union ctl_io *)ctsio);
10146 retval = CTL_RETVAL_COMPLETE;
10154 * Standard INQUIRY data.
10157 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10159 struct scsi_inquiry_data *inq_ptr;
10160 struct scsi_inquiry *cdb;
10161 struct ctl_softc *softc = control_softc;
10162 struct ctl_port *port;
10163 struct ctl_lun *lun;
10165 uint32_t alloc_len, data_len;
10166 ctl_port_type port_type;
10168 port = ctl_io_port(&ctsio->io_hdr);
10169 port_type = port->port_type;
10170 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10171 port_type = CTL_PORT_SCSI;
10173 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10174 cdb = (struct scsi_inquiry *)ctsio->cdb;
10175 alloc_len = scsi_2btoul(cdb->length);
10178 * We malloc the full inquiry data size here and fill it
10179 * in. If the user only asks for less, we'll give him
10182 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10183 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10184 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10185 ctsio->kern_sg_entries = 0;
10186 ctsio->kern_data_resid = 0;
10187 ctsio->kern_rel_offset = 0;
10189 if (data_len < alloc_len) {
10190 ctsio->residual = alloc_len - data_len;
10191 ctsio->kern_data_len = data_len;
10192 ctsio->kern_total_len = data_len;
10194 ctsio->residual = 0;
10195 ctsio->kern_data_len = alloc_len;
10196 ctsio->kern_total_len = alloc_len;
10200 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10201 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10202 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10203 lun->be_lun->lun_type;
10205 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10206 lun->be_lun->lun_type;
10208 if (lun->flags & CTL_LUN_REMOVABLE)
10209 inq_ptr->dev_qual2 |= SID_RMB;
10211 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10213 /* RMB in byte 2 is 0 */
10214 inq_ptr->version = SCSI_REV_SPC5;
10217 * According to SAM-3, even if a device only supports a single
10218 * level of LUN addressing, it should still set the HISUP bit:
10220 * 4.9.1 Logical unit numbers overview
10222 * All logical unit number formats described in this standard are
10223 * hierarchical in structure even when only a single level in that
10224 * hierarchy is used. The HISUP bit shall be set to one in the
10225 * standard INQUIRY data (see SPC-2) when any logical unit number
10226 * format described in this standard is used. Non-hierarchical
10227 * formats are outside the scope of this standard.
10229 * Therefore we set the HiSup bit here.
10231 * The response format is 2, per SPC-3.
10233 inq_ptr->response_format = SID_HiSup | 2;
10235 inq_ptr->additional_length = data_len -
10236 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10237 CTL_DEBUG_PRINT(("additional_length = %d\n",
10238 inq_ptr->additional_length));
10240 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10241 if (port_type == CTL_PORT_SCSI)
10242 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10243 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10244 inq_ptr->flags = SID_CmdQue;
10245 if (port_type == CTL_PORT_SCSI)
10246 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10249 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10250 * We have 8 bytes for the vendor name, and 16 bytes for the device
10251 * name and 4 bytes for the revision.
10253 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10254 "vendor")) == NULL) {
10255 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10257 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10258 strncpy(inq_ptr->vendor, val,
10259 min(sizeof(inq_ptr->vendor), strlen(val)));
10262 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10263 sizeof(inq_ptr->product));
10264 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10265 switch (lun->be_lun->lun_type) {
10267 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10268 sizeof(inq_ptr->product));
10271 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10272 sizeof(inq_ptr->product));
10275 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10276 sizeof(inq_ptr->product));
10279 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10280 sizeof(inq_ptr->product));
10284 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10285 strncpy(inq_ptr->product, val,
10286 min(sizeof(inq_ptr->product), strlen(val)));
10290 * XXX make this a macro somewhere so it automatically gets
10291 * incremented when we make changes.
10293 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10294 "revision")) == NULL) {
10295 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10297 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10298 strncpy(inq_ptr->revision, val,
10299 min(sizeof(inq_ptr->revision), strlen(val)));
10303 * For parallel SCSI, we support double transition and single
10304 * transition clocking. We also support QAS (Quick Arbitration
10305 * and Selection) and Information Unit transfers on both the
10306 * control and array devices.
10308 if (port_type == CTL_PORT_SCSI)
10309 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10312 /* SAM-6 (no version claimed) */
10313 scsi_ulto2b(0x00C0, inq_ptr->version1);
10314 /* SPC-5 (no version claimed) */
10315 scsi_ulto2b(0x05C0, inq_ptr->version2);
10316 if (port_type == CTL_PORT_FC) {
10317 /* FCP-2 ANSI INCITS.350:2003 */
10318 scsi_ulto2b(0x0917, inq_ptr->version3);
10319 } else if (port_type == CTL_PORT_SCSI) {
10320 /* SPI-4 ANSI INCITS.362:200x */
10321 scsi_ulto2b(0x0B56, inq_ptr->version3);
10322 } else if (port_type == CTL_PORT_ISCSI) {
10323 /* iSCSI (no version claimed) */
10324 scsi_ulto2b(0x0960, inq_ptr->version3);
10325 } else if (port_type == CTL_PORT_SAS) {
10326 /* SAS (no version claimed) */
10327 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10331 /* SBC-4 (no version claimed) */
10332 scsi_ulto2b(0x0600, inq_ptr->version4);
10334 switch (lun->be_lun->lun_type) {
10336 /* SBC-4 (no version claimed) */
10337 scsi_ulto2b(0x0600, inq_ptr->version4);
10342 /* MMC-6 (no version claimed) */
10343 scsi_ulto2b(0x04E0, inq_ptr->version4);
10350 ctl_set_success(ctsio);
10351 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10352 ctsio->be_move_done = ctl_config_move_done;
10353 ctl_datamove((union ctl_io *)ctsio);
10354 return (CTL_RETVAL_COMPLETE);
10358 ctl_inquiry(struct ctl_scsiio *ctsio)
10360 struct scsi_inquiry *cdb;
10363 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10365 cdb = (struct scsi_inquiry *)ctsio->cdb;
10366 if (cdb->byte2 & SI_EVPD)
10367 retval = ctl_inquiry_evpd(ctsio);
10368 else if (cdb->page_code == 0)
10369 retval = ctl_inquiry_std(ctsio);
10371 ctl_set_invalid_field(ctsio,
10377 ctl_done((union ctl_io *)ctsio);
10378 return (CTL_RETVAL_COMPLETE);
10385 ctl_get_config(struct ctl_scsiio *ctsio)
10387 struct scsi_get_config_header *hdr;
10388 struct scsi_get_config_feature *feature;
10389 struct scsi_get_config *cdb;
10390 struct ctl_lun *lun;
10391 uint32_t alloc_len, data_len;
10394 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10395 cdb = (struct scsi_get_config *)ctsio->cdb;
10396 rt = (cdb->rt & SGC_RT_MASK);
10397 starting = scsi_2btoul(cdb->starting_feature);
10398 alloc_len = scsi_2btoul(cdb->length);
10400 data_len = sizeof(struct scsi_get_config_header) +
10401 sizeof(struct scsi_get_config_feature) + 8 +
10402 sizeof(struct scsi_get_config_feature) + 8 +
10403 sizeof(struct scsi_get_config_feature) + 4 +
10404 sizeof(struct scsi_get_config_feature) + 4 +
10405 sizeof(struct scsi_get_config_feature) + 8 +
10406 sizeof(struct scsi_get_config_feature) +
10407 sizeof(struct scsi_get_config_feature) + 4 +
10408 sizeof(struct scsi_get_config_feature) + 4 +
10409 sizeof(struct scsi_get_config_feature) + 4 +
10410 sizeof(struct scsi_get_config_feature) + 4 +
10411 sizeof(struct scsi_get_config_feature) + 4 +
10412 sizeof(struct scsi_get_config_feature) + 4;
10413 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10414 ctsio->kern_sg_entries = 0;
10415 ctsio->kern_data_resid = 0;
10416 ctsio->kern_rel_offset = 0;
10418 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10419 if (lun->flags & CTL_LUN_NO_MEDIA)
10420 scsi_ulto2b(0x0000, hdr->current_profile);
10422 scsi_ulto2b(0x0010, hdr->current_profile);
10423 feature = (struct scsi_get_config_feature *)(hdr + 1);
10425 if (starting > 0x003b)
10427 if (starting > 0x003a)
10429 if (starting > 0x002b)
10431 if (starting > 0x002a)
10433 if (starting > 0x001f)
10435 if (starting > 0x001e)
10437 if (starting > 0x001d)
10439 if (starting > 0x0010)
10441 if (starting > 0x0003)
10443 if (starting > 0x0002)
10445 if (starting > 0x0001)
10447 if (starting > 0x0000)
10451 scsi_ulto2b(0x0000, feature->feature_code);
10452 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10453 feature->add_length = 8;
10454 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10455 feature->feature_data[2] = 0x00;
10456 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10457 feature->feature_data[6] = 0x01;
10458 feature = (struct scsi_get_config_feature *)
10459 &feature->feature_data[feature->add_length];
10462 scsi_ulto2b(0x0001, feature->feature_code);
10463 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10464 feature->add_length = 8;
10465 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10466 feature->feature_data[4] = 0x03;
10467 feature = (struct scsi_get_config_feature *)
10468 &feature->feature_data[feature->add_length];
10471 scsi_ulto2b(0x0002, feature->feature_code);
10472 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10473 feature->add_length = 4;
10474 feature->feature_data[0] = 0x02;
10475 feature = (struct scsi_get_config_feature *)
10476 &feature->feature_data[feature->add_length];
10478 f3: /* Removable Medium */
10479 scsi_ulto2b(0x0003, feature->feature_code);
10480 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10481 feature->add_length = 4;
10482 feature->feature_data[0] = 0x39;
10483 feature = (struct scsi_get_config_feature *)
10484 &feature->feature_data[feature->add_length];
10486 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10489 f10: /* Random Read */
10490 scsi_ulto2b(0x0010, feature->feature_code);
10491 feature->flags = 0x00;
10492 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10493 feature->flags |= SGC_F_CURRENT;
10494 feature->add_length = 8;
10495 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10496 scsi_ulto2b(1, &feature->feature_data[4]);
10497 feature->feature_data[6] = 0x00;
10498 feature = (struct scsi_get_config_feature *)
10499 &feature->feature_data[feature->add_length];
10501 f1d: /* Multi-Read */
10502 scsi_ulto2b(0x001D, feature->feature_code);
10503 feature->flags = 0x00;
10504 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10505 feature->flags |= SGC_F_CURRENT;
10506 feature->add_length = 0;
10507 feature = (struct scsi_get_config_feature *)
10508 &feature->feature_data[feature->add_length];
10511 scsi_ulto2b(0x001E, feature->feature_code);
10512 feature->flags = 0x00;
10513 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10514 feature->flags |= SGC_F_CURRENT;
10515 feature->add_length = 4;
10516 feature->feature_data[0] = 0x00;
10517 feature = (struct scsi_get_config_feature *)
10518 &feature->feature_data[feature->add_length];
10520 f1f: /* DVD Read */
10521 scsi_ulto2b(0x001F, feature->feature_code);
10522 feature->flags = 0x08;
10523 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10524 feature->flags |= SGC_F_CURRENT;
10525 feature->add_length = 4;
10526 feature->feature_data[0] = 0x01;
10527 feature->feature_data[2] = 0x03;
10528 feature = (struct scsi_get_config_feature *)
10529 &feature->feature_data[feature->add_length];
10532 scsi_ulto2b(0x002A, feature->feature_code);
10533 feature->flags = 0x04;
10534 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10535 feature->flags |= SGC_F_CURRENT;
10536 feature->add_length = 4;
10537 feature->feature_data[0] = 0x00;
10538 feature->feature_data[1] = 0x00;
10539 feature = (struct scsi_get_config_feature *)
10540 &feature->feature_data[feature->add_length];
10543 scsi_ulto2b(0x002B, feature->feature_code);
10544 feature->flags = 0x00;
10545 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10546 feature->flags |= SGC_F_CURRENT;
10547 feature->add_length = 4;
10548 feature->feature_data[0] = 0x00;
10549 feature = (struct scsi_get_config_feature *)
10550 &feature->feature_data[feature->add_length];
10552 f3a: /* DVD+RW Dual Layer */
10553 scsi_ulto2b(0x003A, feature->feature_code);
10554 feature->flags = 0x00;
10555 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10556 feature->flags |= SGC_F_CURRENT;
10557 feature->add_length = 4;
10558 feature->feature_data[0] = 0x00;
10559 feature->feature_data[1] = 0x00;
10560 feature = (struct scsi_get_config_feature *)
10561 &feature->feature_data[feature->add_length];
10563 f3b: /* DVD+R Dual Layer */
10564 scsi_ulto2b(0x003B, feature->feature_code);
10565 feature->flags = 0x00;
10566 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10567 feature->flags |= SGC_F_CURRENT;
10568 feature->add_length = 4;
10569 feature->feature_data[0] = 0x00;
10570 feature = (struct scsi_get_config_feature *)
10571 &feature->feature_data[feature->add_length];
10574 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10575 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10576 feature = (struct scsi_get_config_feature *)(hdr + 1);
10577 if (scsi_2btoul(feature->feature_code) == starting)
10578 feature = (struct scsi_get_config_feature *)
10579 &feature->feature_data[feature->add_length];
10580 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10582 scsi_ulto4b(data_len - 4, hdr->data_length);
10583 if (data_len < alloc_len) {
10584 ctsio->residual = alloc_len - data_len;
10585 ctsio->kern_data_len = data_len;
10586 ctsio->kern_total_len = data_len;
10588 ctsio->residual = 0;
10589 ctsio->kern_data_len = alloc_len;
10590 ctsio->kern_total_len = alloc_len;
10593 ctl_set_success(ctsio);
10594 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10595 ctsio->be_move_done = ctl_config_move_done;
10596 ctl_datamove((union ctl_io *)ctsio);
10597 return (CTL_RETVAL_COMPLETE);
10601 ctl_get_event_status(struct ctl_scsiio *ctsio)
10603 struct scsi_get_event_status_header *hdr;
10604 struct scsi_get_event_status *cdb;
10605 struct ctl_lun *lun;
10606 uint32_t alloc_len, data_len;
10609 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10610 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10611 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10612 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10613 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10614 ctl_done((union ctl_io *)ctsio);
10615 return (CTL_RETVAL_COMPLETE);
10617 notif_class = cdb->notif_class;
10618 alloc_len = scsi_2btoul(cdb->length);
10620 data_len = sizeof(struct scsi_get_event_status_header);
10621 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10622 ctsio->kern_sg_entries = 0;
10623 ctsio->kern_data_resid = 0;
10624 ctsio->kern_rel_offset = 0;
10626 if (data_len < alloc_len) {
10627 ctsio->residual = alloc_len - data_len;
10628 ctsio->kern_data_len = data_len;
10629 ctsio->kern_total_len = data_len;
10631 ctsio->residual = 0;
10632 ctsio->kern_data_len = alloc_len;
10633 ctsio->kern_total_len = alloc_len;
10636 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10637 scsi_ulto2b(0, hdr->descr_length);
10638 hdr->nea_class = SGESN_NEA;
10639 hdr->supported_class = 0;
10641 ctl_set_success(ctsio);
10642 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10643 ctsio->be_move_done = ctl_config_move_done;
10644 ctl_datamove((union ctl_io *)ctsio);
10645 return (CTL_RETVAL_COMPLETE);
10649 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10651 struct scsi_mechanism_status_header *hdr;
10652 struct scsi_mechanism_status *cdb;
10653 struct ctl_lun *lun;
10654 uint32_t alloc_len, data_len;
10656 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10657 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10658 alloc_len = scsi_2btoul(cdb->length);
10660 data_len = sizeof(struct scsi_mechanism_status_header);
10661 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10662 ctsio->kern_sg_entries = 0;
10663 ctsio->kern_data_resid = 0;
10664 ctsio->kern_rel_offset = 0;
10666 if (data_len < alloc_len) {
10667 ctsio->residual = alloc_len - data_len;
10668 ctsio->kern_data_len = data_len;
10669 ctsio->kern_total_len = data_len;
10671 ctsio->residual = 0;
10672 ctsio->kern_data_len = alloc_len;
10673 ctsio->kern_total_len = alloc_len;
10676 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10677 hdr->state1 = 0x00;
10678 hdr->state2 = 0xe0;
10679 scsi_ulto3b(0, hdr->lba);
10680 hdr->slots_num = 0;
10681 scsi_ulto2b(0, hdr->slots_length);
10683 ctl_set_success(ctsio);
10684 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10685 ctsio->be_move_done = ctl_config_move_done;
10686 ctl_datamove((union ctl_io *)ctsio);
10687 return (CTL_RETVAL_COMPLETE);
10691 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10696 buf[1] = bin2bcd((lba / 75) / 60);
10697 buf[2] = bin2bcd((lba / 75) % 60);
10698 buf[3] = bin2bcd(lba % 75);
10702 ctl_read_toc(struct ctl_scsiio *ctsio)
10704 struct scsi_read_toc_hdr *hdr;
10705 struct scsi_read_toc_type01_descr *descr;
10706 struct scsi_read_toc *cdb;
10707 struct ctl_lun *lun;
10708 uint32_t alloc_len, data_len;
10711 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10712 cdb = (struct scsi_read_toc *)ctsio->cdb;
10713 msf = (cdb->byte2 & CD_MSF) != 0;
10714 format = cdb->format;
10715 alloc_len = scsi_2btoul(cdb->data_len);
10717 data_len = sizeof(struct scsi_read_toc_hdr);
10719 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10721 data_len += sizeof(struct scsi_read_toc_type01_descr);
10722 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10723 ctsio->kern_sg_entries = 0;
10724 ctsio->kern_data_resid = 0;
10725 ctsio->kern_rel_offset = 0;
10727 if (data_len < alloc_len) {
10728 ctsio->residual = alloc_len - data_len;
10729 ctsio->kern_data_len = data_len;
10730 ctsio->kern_total_len = data_len;
10732 ctsio->residual = 0;
10733 ctsio->kern_data_len = alloc_len;
10734 ctsio->kern_total_len = alloc_len;
10737 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10739 scsi_ulto2b(0x12, hdr->data_length);
10742 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10743 descr->addr_ctl = 0x14;
10744 descr->track_number = 1;
10746 ctl_ultomsf(0, descr->track_start);
10748 scsi_ulto4b(0, descr->track_start);
10750 descr->addr_ctl = 0x14;
10751 descr->track_number = 0xaa;
10753 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10755 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10757 scsi_ulto2b(0x0a, hdr->data_length);
10760 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10761 descr->addr_ctl = 0x14;
10762 descr->track_number = 1;
10764 ctl_ultomsf(0, descr->track_start);
10766 scsi_ulto4b(0, descr->track_start);
10769 ctl_set_success(ctsio);
10770 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10771 ctsio->be_move_done = ctl_config_move_done;
10772 ctl_datamove((union ctl_io *)ctsio);
10773 return (CTL_RETVAL_COMPLETE);
10777 * For known CDB types, parse the LBA and length.
10780 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10782 if (io->io_hdr.io_type != CTL_IO_SCSI)
10785 switch (io->scsiio.cdb[0]) {
10786 case COMPARE_AND_WRITE: {
10787 struct scsi_compare_and_write *cdb;
10789 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10791 *lba = scsi_8btou64(cdb->addr);
10792 *len = cdb->length;
10797 struct scsi_rw_6 *cdb;
10799 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10801 *lba = scsi_3btoul(cdb->addr);
10802 /* only 5 bits are valid in the most significant address byte */
10804 *len = cdb->length;
10809 struct scsi_rw_10 *cdb;
10811 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10813 *lba = scsi_4btoul(cdb->addr);
10814 *len = scsi_2btoul(cdb->length);
10817 case WRITE_VERIFY_10: {
10818 struct scsi_write_verify_10 *cdb;
10820 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10822 *lba = scsi_4btoul(cdb->addr);
10823 *len = scsi_2btoul(cdb->length);
10828 struct scsi_rw_12 *cdb;
10830 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10832 *lba = scsi_4btoul(cdb->addr);
10833 *len = scsi_4btoul(cdb->length);
10836 case WRITE_VERIFY_12: {
10837 struct scsi_write_verify_12 *cdb;
10839 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10841 *lba = scsi_4btoul(cdb->addr);
10842 *len = scsi_4btoul(cdb->length);
10847 struct scsi_rw_16 *cdb;
10849 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10851 *lba = scsi_8btou64(cdb->addr);
10852 *len = scsi_4btoul(cdb->length);
10855 case WRITE_ATOMIC_16: {
10856 struct scsi_write_atomic_16 *cdb;
10858 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10860 *lba = scsi_8btou64(cdb->addr);
10861 *len = scsi_2btoul(cdb->length);
10864 case WRITE_VERIFY_16: {
10865 struct scsi_write_verify_16 *cdb;
10867 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10869 *lba = scsi_8btou64(cdb->addr);
10870 *len = scsi_4btoul(cdb->length);
10873 case WRITE_SAME_10: {
10874 struct scsi_write_same_10 *cdb;
10876 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10878 *lba = scsi_4btoul(cdb->addr);
10879 *len = scsi_2btoul(cdb->length);
10882 case WRITE_SAME_16: {
10883 struct scsi_write_same_16 *cdb;
10885 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10887 *lba = scsi_8btou64(cdb->addr);
10888 *len = scsi_4btoul(cdb->length);
10892 struct scsi_verify_10 *cdb;
10894 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10896 *lba = scsi_4btoul(cdb->addr);
10897 *len = scsi_2btoul(cdb->length);
10901 struct scsi_verify_12 *cdb;
10903 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10905 *lba = scsi_4btoul(cdb->addr);
10906 *len = scsi_4btoul(cdb->length);
10910 struct scsi_verify_16 *cdb;
10912 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10914 *lba = scsi_8btou64(cdb->addr);
10915 *len = scsi_4btoul(cdb->length);
10923 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10924 struct scsi_get_lba_status *cdb;
10926 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10927 *lba = scsi_8btou64(cdb->addr);
10933 break; /* NOTREACHED */
10940 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10943 uint64_t endlba1, endlba2;
10945 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10946 endlba2 = lba2 + len2 - 1;
10948 if ((endlba1 < lba2) || (endlba2 < lba1))
10949 return (CTL_ACTION_PASS);
10951 return (CTL_ACTION_BLOCK);
10955 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10957 struct ctl_ptr_len_flags *ptrlen;
10958 struct scsi_unmap_desc *buf, *end, *range;
10962 /* If not UNMAP -- go other way. */
10963 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10964 io->scsiio.cdb[0] != UNMAP)
10965 return (CTL_ACTION_ERROR);
10967 /* If UNMAP without data -- block and wait for data. */
10968 ptrlen = (struct ctl_ptr_len_flags *)
10969 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10970 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10971 ptrlen->ptr == NULL)
10972 return (CTL_ACTION_BLOCK);
10974 /* UNMAP with data -- check for collision. */
10975 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10976 end = buf + ptrlen->len / sizeof(*buf);
10977 for (range = buf; range < end; range++) {
10978 lba = scsi_8btou64(range->lba);
10979 len = scsi_4btoul(range->length);
10980 if ((lba < lba2 + len2) && (lba + len > lba2))
10981 return (CTL_ACTION_BLOCK);
10983 return (CTL_ACTION_PASS);
10987 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10989 uint64_t lba1, lba2;
10990 uint64_t len1, len2;
10993 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10994 return (CTL_ACTION_ERROR);
10996 retval = ctl_extent_check_unmap(io1, lba2, len2);
10997 if (retval != CTL_ACTION_ERROR)
11000 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
11001 return (CTL_ACTION_ERROR);
11003 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
11005 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
11009 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
11011 uint64_t lba1, lba2;
11012 uint64_t len1, len2;
11014 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
11015 return (CTL_ACTION_PASS);
11016 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
11017 return (CTL_ACTION_ERROR);
11018 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
11019 return (CTL_ACTION_ERROR);
11021 if (lba1 + len1 == lba2)
11022 return (CTL_ACTION_BLOCK);
11023 return (CTL_ACTION_PASS);
11027 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
11028 union ctl_io *ooa_io)
11030 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
11031 const ctl_serialize_action *serialize_row;
11034 * The initiator attempted multiple untagged commands at the same
11035 * time. Can't do that.
11037 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11038 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11039 && ((pending_io->io_hdr.nexus.targ_port ==
11040 ooa_io->io_hdr.nexus.targ_port)
11041 && (pending_io->io_hdr.nexus.initid ==
11042 ooa_io->io_hdr.nexus.initid))
11043 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11044 CTL_FLAG_STATUS_SENT)) == 0))
11045 return (CTL_ACTION_OVERLAP);
11048 * The initiator attempted to send multiple tagged commands with
11049 * the same ID. (It's fine if different initiators have the same
11052 * Even if all of those conditions are true, we don't kill the I/O
11053 * if the command ahead of us has been aborted. We won't end up
11054 * sending it to the FETD, and it's perfectly legal to resend a
11055 * command with the same tag number as long as the previous
11056 * instance of this tag number has been aborted somehow.
11058 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11059 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11060 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11061 && ((pending_io->io_hdr.nexus.targ_port ==
11062 ooa_io->io_hdr.nexus.targ_port)
11063 && (pending_io->io_hdr.nexus.initid ==
11064 ooa_io->io_hdr.nexus.initid))
11065 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11066 CTL_FLAG_STATUS_SENT)) == 0))
11067 return (CTL_ACTION_OVERLAP_TAG);
11070 * If we get a head of queue tag, SAM-3 says that we should
11071 * immediately execute it.
11073 * What happens if this command would normally block for some other
11074 * reason? e.g. a request sense with a head of queue tag
11075 * immediately after a write. Normally that would block, but this
11076 * will result in its getting executed immediately...
11078 * We currently return "pass" instead of "skip", so we'll end up
11079 * going through the rest of the queue to check for overlapped tags.
11081 * XXX KDM check for other types of blockage first??
11083 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11084 return (CTL_ACTION_PASS);
11087 * Ordered tags have to block until all items ahead of them
11088 * have completed. If we get called with an ordered tag, we always
11089 * block, if something else is ahead of us in the queue.
11091 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11092 return (CTL_ACTION_BLOCK);
11095 * Simple tags get blocked until all head of queue and ordered tags
11096 * ahead of them have completed. I'm lumping untagged commands in
11097 * with simple tags here. XXX KDM is that the right thing to do?
11099 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11100 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11101 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11102 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11103 return (CTL_ACTION_BLOCK);
11105 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11106 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11107 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11108 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11109 pending_io->scsiio.cdb[1], pending_io));
11110 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11111 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11112 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11113 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11114 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11115 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11116 ooa_io->scsiio.cdb[1], ooa_io));
11118 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11120 switch (serialize_row[pending_entry->seridx]) {
11121 case CTL_SER_BLOCK:
11122 return (CTL_ACTION_BLOCK);
11123 case CTL_SER_EXTENT:
11124 return (ctl_extent_check(ooa_io, pending_io,
11125 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11126 case CTL_SER_EXTENTOPT:
11127 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11128 SCP_QUEUE_ALG_UNRESTRICTED)
11129 return (ctl_extent_check(ooa_io, pending_io,
11131 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11132 return (CTL_ACTION_PASS);
11133 case CTL_SER_EXTENTSEQ:
11134 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11135 return (ctl_extent_check_seq(ooa_io, pending_io));
11136 return (CTL_ACTION_PASS);
11138 return (CTL_ACTION_PASS);
11139 case CTL_SER_BLOCKOPT:
11140 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11141 SCP_QUEUE_ALG_UNRESTRICTED)
11142 return (CTL_ACTION_BLOCK);
11143 return (CTL_ACTION_PASS);
11145 return (CTL_ACTION_SKIP);
11147 panic("%s: Invalid serialization value %d for %d => %d",
11148 __func__, serialize_row[pending_entry->seridx],
11149 pending_entry->seridx, ooa_entry->seridx);
11152 return (CTL_ACTION_ERROR);
11156 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11158 * - pending_io is generally either incoming, or on the blocked queue
11159 * - starting I/O is the I/O we want to start the check with.
11162 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11163 union ctl_io *starting_io)
11165 union ctl_io *ooa_io;
11168 mtx_assert(&lun->lun_lock, MA_OWNED);
11171 * Run back along the OOA queue, starting with the current
11172 * blocked I/O and going through every I/O before it on the
11173 * queue. If starting_io is NULL, we'll just end up returning
11176 for (ooa_io = starting_io; ooa_io != NULL;
11177 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11181 * This routine just checks to see whether
11182 * cur_blocked is blocked by ooa_io, which is ahead
11183 * of it in the queue. It doesn't queue/dequeue
11186 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11188 case CTL_ACTION_BLOCK:
11189 case CTL_ACTION_OVERLAP:
11190 case CTL_ACTION_OVERLAP_TAG:
11191 case CTL_ACTION_SKIP:
11192 case CTL_ACTION_ERROR:
11194 break; /* NOTREACHED */
11195 case CTL_ACTION_PASS:
11198 panic("%s: Invalid action %d\n", __func__, action);
11202 return (CTL_ACTION_PASS);
11207 * - An I/O has just completed, and has been removed from the per-LUN OOA
11208 * queue, so some items on the blocked queue may now be unblocked.
11211 ctl_check_blocked(struct ctl_lun *lun)
11213 struct ctl_softc *softc = lun->ctl_softc;
11214 union ctl_io *cur_blocked, *next_blocked;
11216 mtx_assert(&lun->lun_lock, MA_OWNED);
11219 * Run forward from the head of the blocked queue, checking each
11220 * entry against the I/Os prior to it on the OOA queue to see if
11221 * there is still any blockage.
11223 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11224 * with our removing a variable on it while it is traversing the
11227 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11228 cur_blocked != NULL; cur_blocked = next_blocked) {
11229 union ctl_io *prev_ooa;
11232 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11235 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11236 ctl_ooaq, ooa_links);
11239 * If cur_blocked happens to be the first item in the OOA
11240 * queue now, prev_ooa will be NULL, and the action
11241 * returned will just be CTL_ACTION_PASS.
11243 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11246 case CTL_ACTION_BLOCK:
11247 /* Nothing to do here, still blocked */
11249 case CTL_ACTION_OVERLAP:
11250 case CTL_ACTION_OVERLAP_TAG:
11252 * This shouldn't happen! In theory we've already
11253 * checked this command for overlap...
11256 case CTL_ACTION_PASS:
11257 case CTL_ACTION_SKIP: {
11258 const struct ctl_cmd_entry *entry;
11261 * The skip case shouldn't happen, this transaction
11262 * should have never made it onto the blocked queue.
11265 * This I/O is no longer blocked, we can remove it
11266 * from the blocked queue. Since this is a TAILQ
11267 * (doubly linked list), we can do O(1) removals
11268 * from any place on the list.
11270 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11272 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11274 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11275 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11277 * Need to send IO back to original side to
11280 union ctl_ha_msg msg_info;
11282 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11283 msg_info.hdr.original_sc =
11284 cur_blocked->io_hdr.original_sc;
11285 msg_info.hdr.serializing_sc = cur_blocked;
11286 msg_info.hdr.msg_type = CTL_MSG_R2R;
11287 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11288 sizeof(msg_info.hdr), M_NOWAIT);
11291 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11294 * Check this I/O for LUN state changes that may
11295 * have happened while this command was blocked.
11296 * The LUN state may have been changed by a command
11297 * ahead of us in the queue, so we need to re-check
11298 * for any states that can be caused by SCSI
11301 if (ctl_scsiio_lun_check(lun, entry,
11302 &cur_blocked->scsiio) == 0) {
11303 cur_blocked->io_hdr.flags |=
11304 CTL_FLAG_IS_WAS_ON_RTR;
11305 ctl_enqueue_rtr(cur_blocked);
11307 ctl_done(cur_blocked);
11312 * This probably shouldn't happen -- we shouldn't
11313 * get CTL_ACTION_ERROR, or anything else.
11319 return (CTL_RETVAL_COMPLETE);
11323 * This routine (with one exception) checks LUN flags that can be set by
11324 * commands ahead of us in the OOA queue. These flags have to be checked
11325 * when a command initially comes in, and when we pull a command off the
11326 * blocked queue and are preparing to execute it. The reason we have to
11327 * check these flags for commands on the blocked queue is that the LUN
11328 * state may have been changed by a command ahead of us while we're on the
11331 * Ordering is somewhat important with these checks, so please pay
11332 * careful attention to the placement of any new checks.
11335 ctl_scsiio_lun_check(struct ctl_lun *lun,
11336 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11338 struct ctl_softc *softc = lun->ctl_softc;
11344 mtx_assert(&lun->lun_lock, MA_OWNED);
11347 * If this shelf is a secondary shelf controller, we may have to
11348 * reject some commands disallowed by HA mode and link state.
11350 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11351 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11352 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11353 ctl_set_lun_unavail(ctsio);
11357 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11358 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11359 ctl_set_lun_transit(ctsio);
11363 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11364 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11365 ctl_set_lun_standby(ctsio);
11370 /* The rest of checks are only done on executing side */
11371 if (softc->ha_mode == CTL_HA_MODE_XFER)
11375 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11377 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11378 ctl_set_hw_write_protected(ctsio);
11382 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11383 ctl_set_sense(ctsio, /*current_error*/ 1,
11384 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11385 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11392 * Check for a reservation conflict. If this command isn't allowed
11393 * even on reserved LUNs, and if this initiator isn't the one who
11394 * reserved us, reject the command with a reservation conflict.
11396 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11397 if ((lun->flags & CTL_LUN_RESERVED)
11398 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11399 if (lun->res_idx != residx) {
11400 ctl_set_reservation_conflict(ctsio);
11406 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11407 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11408 /* No reservation or command is allowed. */;
11409 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11410 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11411 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11412 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11413 /* The command is allowed for Write Exclusive resv. */;
11416 * if we aren't registered or it's a res holder type
11417 * reservation and this isn't the res holder then set a
11420 if (ctl_get_prkey(lun, residx) == 0 ||
11421 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11422 ctl_set_reservation_conflict(ctsio);
11428 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11429 if (lun->flags & CTL_LUN_EJECTED)
11430 ctl_set_lun_ejected(ctsio);
11431 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11432 if (lun->flags & CTL_LUN_REMOVABLE)
11433 ctl_set_lun_no_media(ctsio);
11435 ctl_set_lun_int_reqd(ctsio);
11436 } else if (lun->flags & CTL_LUN_STOPPED)
11437 ctl_set_lun_stopped(ctsio);
11449 ctl_failover_io(union ctl_io *io, int have_lock)
11451 ctl_set_busy(&io->scsiio);
11456 ctl_failover_lun(union ctl_io *rio)
11458 struct ctl_softc *softc = control_softc;
11459 struct ctl_lun *lun;
11460 struct ctl_io_hdr *io, *next_io;
11463 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11464 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11466 /* Find and lock the LUN. */
11467 mtx_lock(&softc->ctl_lock);
11468 if (targ_lun > CTL_MAX_LUNS ||
11469 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11470 mtx_unlock(&softc->ctl_lock);
11473 mtx_lock(&lun->lun_lock);
11474 mtx_unlock(&softc->ctl_lock);
11475 if (lun->flags & CTL_LUN_DISABLED) {
11476 mtx_unlock(&lun->lun_lock);
11480 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11481 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11482 /* We are master */
11483 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11484 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11485 io->flags |= CTL_FLAG_ABORT;
11486 io->flags |= CTL_FLAG_FAILOVER;
11487 } else { /* This can be only due to DATAMOVE */
11488 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11489 io->flags &= ~CTL_FLAG_DMA_INPROG;
11490 io->flags |= CTL_FLAG_IO_ACTIVE;
11491 io->port_status = 31340;
11492 ctl_enqueue_isc((union ctl_io *)io);
11496 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11497 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11498 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11499 io->flags |= CTL_FLAG_FAILOVER;
11501 ctl_set_busy(&((union ctl_io *)io)->
11503 ctl_done((union ctl_io *)io);
11507 } else { /* SERIALIZE modes */
11508 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11510 /* We are master */
11511 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11512 TAILQ_REMOVE(&lun->blocked_queue, io,
11514 io->flags &= ~CTL_FLAG_BLOCKED;
11515 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11516 ctl_free_io((union ctl_io *)io);
11519 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11520 /* We are master */
11521 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11522 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11523 ctl_free_io((union ctl_io *)io);
11526 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11527 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11528 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11529 ctl_set_busy(&((union ctl_io *)io)->
11531 ctl_done((union ctl_io *)io);
11535 ctl_check_blocked(lun);
11537 mtx_unlock(&lun->lun_lock);
11541 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11543 struct ctl_lun *lun;
11544 const struct ctl_cmd_entry *entry;
11545 uint32_t initidx, targ_lun;
11549 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11550 if (targ_lun < CTL_MAX_LUNS)
11551 lun = softc->ctl_luns[targ_lun];
11554 * If the LUN is invalid, pretend that it doesn't exist.
11555 * It will go away as soon as all pending I/O has been
11558 mtx_lock(&lun->lun_lock);
11559 if (lun->flags & CTL_LUN_DISABLED) {
11560 mtx_unlock(&lun->lun_lock);
11564 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11566 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11570 * Every I/O goes into the OOA queue for a particular LUN,
11571 * and stays there until completion.
11574 if (TAILQ_EMPTY(&lun->ooa_queue))
11575 lun->idle_time += getsbinuptime() - lun->last_busy;
11577 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11580 /* Get command entry and return error if it is unsuppotyed. */
11581 entry = ctl_validate_command(ctsio);
11582 if (entry == NULL) {
11584 mtx_unlock(&lun->lun_lock);
11588 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11589 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11592 * Check to see whether we can send this command to LUNs that don't
11593 * exist. This should pretty much only be the case for inquiry
11594 * and request sense. Further checks, below, really require having
11595 * a LUN, so we can't really check the command anymore. Just put
11596 * it on the rtr queue.
11599 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11600 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11601 ctl_enqueue_rtr((union ctl_io *)ctsio);
11605 ctl_set_unsupported_lun(ctsio);
11606 ctl_done((union ctl_io *)ctsio);
11607 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11611 * Make sure we support this particular command on this LUN.
11612 * e.g., we don't support writes to the control LUN.
11614 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11615 mtx_unlock(&lun->lun_lock);
11616 ctl_set_invalid_opcode(ctsio);
11617 ctl_done((union ctl_io *)ctsio);
11622 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11626 * If we've got a request sense, it'll clear the contingent
11627 * allegiance condition. Otherwise, if we have a CA condition for
11628 * this initiator, clear it, because it sent down a command other
11629 * than request sense.
11631 if ((ctsio->cdb[0] != REQUEST_SENSE)
11632 && (ctl_is_set(lun->have_ca, initidx)))
11633 ctl_clear_mask(lun->have_ca, initidx);
11637 * If the command has this flag set, it handles its own unit
11638 * attention reporting, we shouldn't do anything. Otherwise we
11639 * check for any pending unit attentions, and send them back to the
11640 * initiator. We only do this when a command initially comes in,
11641 * not when we pull it off the blocked queue.
11643 * According to SAM-3, section 5.3.2, the order that things get
11644 * presented back to the host is basically unit attentions caused
11645 * by some sort of reset event, busy status, reservation conflicts
11646 * or task set full, and finally any other status.
11648 * One issue here is that some of the unit attentions we report
11649 * don't fall into the "reset" category (e.g. "reported luns data
11650 * has changed"). So reporting it here, before the reservation
11651 * check, may be technically wrong. I guess the only thing to do
11652 * would be to check for and report the reset events here, and then
11653 * check for the other unit attention types after we check for a
11654 * reservation conflict.
11656 * XXX KDM need to fix this
11658 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11659 ctl_ua_type ua_type;
11660 u_int sense_len = 0;
11662 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11663 &sense_len, SSD_TYPE_NONE);
11664 if (ua_type != CTL_UA_NONE) {
11665 mtx_unlock(&lun->lun_lock);
11666 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11667 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11668 ctsio->sense_len = sense_len;
11669 ctl_done((union ctl_io *)ctsio);
11675 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11676 mtx_unlock(&lun->lun_lock);
11677 ctl_done((union ctl_io *)ctsio);
11682 * XXX CHD this is where we want to send IO to other side if
11683 * this LUN is secondary on this SC. We will need to make a copy
11684 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11685 * the copy we send as FROM_OTHER.
11686 * We also need to stuff the address of the original IO so we can
11687 * find it easily. Something similar will need be done on the other
11688 * side so when we are done we can find the copy.
11690 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11691 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11692 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11693 union ctl_ha_msg msg_info;
11696 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11697 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11698 mtx_unlock(&lun->lun_lock);
11700 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11701 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11702 msg_info.hdr.serializing_sc = NULL;
11703 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11704 msg_info.scsi.tag_num = ctsio->tag_num;
11705 msg_info.scsi.tag_type = ctsio->tag_type;
11706 msg_info.scsi.cdb_len = ctsio->cdb_len;
11707 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11709 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11710 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11711 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11712 ctl_set_busy(ctsio);
11713 ctl_done((union ctl_io *)ctsio);
11719 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11720 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11721 ctl_ooaq, ooa_links))) {
11722 case CTL_ACTION_BLOCK:
11723 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11724 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11726 mtx_unlock(&lun->lun_lock);
11728 case CTL_ACTION_PASS:
11729 case CTL_ACTION_SKIP:
11730 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11731 mtx_unlock(&lun->lun_lock);
11732 ctl_enqueue_rtr((union ctl_io *)ctsio);
11734 case CTL_ACTION_OVERLAP:
11735 mtx_unlock(&lun->lun_lock);
11736 ctl_set_overlapped_cmd(ctsio);
11737 ctl_done((union ctl_io *)ctsio);
11739 case CTL_ACTION_OVERLAP_TAG:
11740 mtx_unlock(&lun->lun_lock);
11741 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11742 ctl_done((union ctl_io *)ctsio);
11744 case CTL_ACTION_ERROR:
11746 mtx_unlock(&lun->lun_lock);
11747 ctl_set_internal_failure(ctsio,
11749 /*retry_count*/ 0);
11750 ctl_done((union ctl_io *)ctsio);
11756 const struct ctl_cmd_entry *
11757 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11759 const struct ctl_cmd_entry *entry;
11760 int service_action;
11762 entry = &ctl_cmd_table[ctsio->cdb[0]];
11764 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11765 if (entry->flags & CTL_CMD_FLAG_SA5) {
11766 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11767 entry = &((const struct ctl_cmd_entry *)
11768 entry->execute)[service_action];
11773 const struct ctl_cmd_entry *
11774 ctl_validate_command(struct ctl_scsiio *ctsio)
11776 const struct ctl_cmd_entry *entry;
11780 entry = ctl_get_cmd_entry(ctsio, &sa);
11781 if (entry->execute == NULL) {
11783 ctl_set_invalid_field(ctsio,
11790 ctl_set_invalid_opcode(ctsio);
11791 ctl_done((union ctl_io *)ctsio);
11794 KASSERT(entry->length > 0,
11795 ("Not defined length for command 0x%02x/0x%02x",
11796 ctsio->cdb[0], ctsio->cdb[1]));
11797 for (i = 1; i < entry->length; i++) {
11798 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11801 ctl_set_invalid_field(ctsio,
11806 /*bit*/ fls(diff) - 1);
11807 ctl_done((union ctl_io *)ctsio);
11814 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11817 switch (lun_type) {
11819 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11823 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11827 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11837 ctl_scsiio(struct ctl_scsiio *ctsio)
11840 const struct ctl_cmd_entry *entry;
11842 retval = CTL_RETVAL_COMPLETE;
11844 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11846 entry = ctl_get_cmd_entry(ctsio, NULL);
11849 * If this I/O has been aborted, just send it straight to
11850 * ctl_done() without executing it.
11852 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11853 ctl_done((union ctl_io *)ctsio);
11858 * All the checks should have been handled by ctl_scsiio_precheck().
11859 * We should be clear now to just execute the I/O.
11861 retval = entry->execute(ctsio);
11868 * Since we only implement one target right now, a bus reset simply resets
11869 * our single target.
11872 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11874 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11878 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11879 ctl_ua_type ua_type)
11881 struct ctl_port *port;
11882 struct ctl_lun *lun;
11885 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11886 union ctl_ha_msg msg_info;
11888 msg_info.hdr.nexus = io->io_hdr.nexus;
11889 if (ua_type==CTL_UA_TARG_RESET)
11890 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11892 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11893 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11894 msg_info.hdr.original_sc = NULL;
11895 msg_info.hdr.serializing_sc = NULL;
11896 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11897 sizeof(msg_info.task), M_WAITOK);
11901 mtx_lock(&softc->ctl_lock);
11902 port = ctl_io_port(&io->io_hdr);
11903 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11904 if (port != NULL &&
11905 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11907 retval += ctl_do_lun_reset(lun, io, ua_type);
11909 mtx_unlock(&softc->ctl_lock);
11910 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11915 * The LUN should always be set. The I/O is optional, and is used to
11916 * distinguish between I/Os sent by this initiator, and by other
11917 * initiators. We set unit attention for initiators other than this one.
11918 * SAM-3 is vague on this point. It does say that a unit attention should
11919 * be established for other initiators when a LUN is reset (see section
11920 * 5.7.3), but it doesn't specifically say that the unit attention should
11921 * be established for this particular initiator when a LUN is reset. Here
11922 * is the relevant text, from SAM-3 rev 8:
11924 * 5.7.2 When a SCSI initiator port aborts its own tasks
11926 * When a SCSI initiator port causes its own task(s) to be aborted, no
11927 * notification that the task(s) have been aborted shall be returned to
11928 * the SCSI initiator port other than the completion response for the
11929 * command or task management function action that caused the task(s) to
11930 * be aborted and notification(s) associated with related effects of the
11931 * action (e.g., a reset unit attention condition).
11933 * XXX KDM for now, we're setting unit attention for all initiators.
11936 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11944 mtx_lock(&lun->lun_lock);
11946 * Run through the OOA queue and abort each I/O.
11948 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11949 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11950 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11954 * This version sets unit attention for every
11957 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11958 ctl_est_ua_all(lun, initidx, ua_type);
11960 ctl_est_ua_all(lun, -1, ua_type);
11964 * A reset (any kind, really) clears reservations established with
11965 * RESERVE/RELEASE. It does not clear reservations established
11966 * with PERSISTENT RESERVE OUT, but we don't support that at the
11967 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11968 * reservations made with the RESERVE/RELEASE commands, because
11969 * those commands are obsolete in SPC-3.
11971 lun->flags &= ~CTL_LUN_RESERVED;
11974 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11975 ctl_clear_mask(lun->have_ca, i);
11977 lun->prevent_count = 0;
11978 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11979 ctl_clear_mask(lun->prevent, i);
11980 mtx_unlock(&lun->lun_lock);
11986 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11988 struct ctl_lun *lun;
11992 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11993 mtx_lock(&softc->ctl_lock);
11994 if (targ_lun >= CTL_MAX_LUNS ||
11995 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11996 mtx_unlock(&softc->ctl_lock);
11997 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12000 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
12001 mtx_unlock(&softc->ctl_lock);
12002 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12004 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
12005 union ctl_ha_msg msg_info;
12007 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12008 msg_info.hdr.nexus = io->io_hdr.nexus;
12009 msg_info.task.task_action = CTL_TASK_LUN_RESET;
12010 msg_info.hdr.original_sc = NULL;
12011 msg_info.hdr.serializing_sc = NULL;
12012 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12013 sizeof(msg_info.task), M_WAITOK);
12019 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12024 mtx_assert(&lun->lun_lock, MA_OWNED);
12027 * Run through the OOA queue and attempt to find the given I/O.
12028 * The target port, initiator ID, tag type and tag number have to
12029 * match the values that we got from the initiator. If we have an
12030 * untagged command to abort, simply abort the first untagged command
12031 * we come to. We only allow one untagged command at a time of course.
12033 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12034 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12036 if ((targ_port == UINT32_MAX ||
12037 targ_port == xio->io_hdr.nexus.targ_port) &&
12038 (init_id == UINT32_MAX ||
12039 init_id == xio->io_hdr.nexus.initid)) {
12040 if (targ_port != xio->io_hdr.nexus.targ_port ||
12041 init_id != xio->io_hdr.nexus.initid)
12042 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12043 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12044 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12045 union ctl_ha_msg msg_info;
12047 msg_info.hdr.nexus = xio->io_hdr.nexus;
12048 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12049 msg_info.task.tag_num = xio->scsiio.tag_num;
12050 msg_info.task.tag_type = xio->scsiio.tag_type;
12051 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12052 msg_info.hdr.original_sc = NULL;
12053 msg_info.hdr.serializing_sc = NULL;
12054 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12055 sizeof(msg_info.task), M_NOWAIT);
12062 ctl_abort_task_set(union ctl_io *io)
12064 struct ctl_softc *softc = control_softc;
12065 struct ctl_lun *lun;
12071 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12072 mtx_lock(&softc->ctl_lock);
12073 if (targ_lun >= CTL_MAX_LUNS ||
12074 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12075 mtx_unlock(&softc->ctl_lock);
12076 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12080 mtx_lock(&lun->lun_lock);
12081 mtx_unlock(&softc->ctl_lock);
12082 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12083 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12084 io->io_hdr.nexus.initid,
12085 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12086 } else { /* CTL_TASK_CLEAR_TASK_SET */
12087 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12088 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12090 mtx_unlock(&lun->lun_lock);
12091 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12096 ctl_i_t_nexus_reset(union ctl_io *io)
12098 struct ctl_softc *softc = control_softc;
12099 struct ctl_lun *lun;
12102 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12103 union ctl_ha_msg msg_info;
12105 msg_info.hdr.nexus = io->io_hdr.nexus;
12106 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12107 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12108 msg_info.hdr.original_sc = NULL;
12109 msg_info.hdr.serializing_sc = NULL;
12110 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12111 sizeof(msg_info.task), M_WAITOK);
12114 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12115 mtx_lock(&softc->ctl_lock);
12116 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12117 mtx_lock(&lun->lun_lock);
12118 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12119 io->io_hdr.nexus.initid, 1);
12121 ctl_clear_mask(lun->have_ca, initidx);
12123 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12124 lun->flags &= ~CTL_LUN_RESERVED;
12125 if (ctl_is_set(lun->prevent, initidx)) {
12126 ctl_clear_mask(lun->prevent, initidx);
12127 lun->prevent_count--;
12129 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12130 mtx_unlock(&lun->lun_lock);
12132 mtx_unlock(&softc->ctl_lock);
12133 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12138 ctl_abort_task(union ctl_io *io)
12141 struct ctl_lun *lun;
12142 struct ctl_softc *softc;
12145 char printbuf[128];
12150 softc = control_softc;
12156 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12157 mtx_lock(&softc->ctl_lock);
12158 if (targ_lun >= CTL_MAX_LUNS ||
12159 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12160 mtx_unlock(&softc->ctl_lock);
12161 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12166 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12167 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12170 mtx_lock(&lun->lun_lock);
12171 mtx_unlock(&softc->ctl_lock);
12173 * Run through the OOA queue and attempt to find the given I/O.
12174 * The target port, initiator ID, tag type and tag number have to
12175 * match the values that we got from the initiator. If we have an
12176 * untagged command to abort, simply abort the first untagged command
12177 * we come to. We only allow one untagged command at a time of course.
12179 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12180 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12182 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12184 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12185 lun->lun, xio->scsiio.tag_num,
12186 xio->scsiio.tag_type,
12187 (xio->io_hdr.blocked_links.tqe_prev
12188 == NULL) ? "" : " BLOCKED",
12189 (xio->io_hdr.flags &
12190 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12191 (xio->io_hdr.flags &
12192 CTL_FLAG_ABORT) ? " ABORT" : "",
12193 (xio->io_hdr.flags &
12194 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12195 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12197 printf("%s\n", sbuf_data(&sb));
12200 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12201 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12202 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12206 * If the abort says that the task is untagged, the
12207 * task in the queue must be untagged. Otherwise,
12208 * we just check to see whether the tag numbers
12209 * match. This is because the QLogic firmware
12210 * doesn't pass back the tag type in an abort
12214 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12215 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12216 || (xio->scsiio.tag_num == io->taskio.tag_num))
12219 * XXX KDM we've got problems with FC, because it
12220 * doesn't send down a tag type with aborts. So we
12221 * can only really go by the tag number...
12222 * This may cause problems with parallel SCSI.
12223 * Need to figure that out!!
12225 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12226 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12228 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12229 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12230 union ctl_ha_msg msg_info;
12232 msg_info.hdr.nexus = io->io_hdr.nexus;
12233 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12234 msg_info.task.tag_num = io->taskio.tag_num;
12235 msg_info.task.tag_type = io->taskio.tag_type;
12236 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12237 msg_info.hdr.original_sc = NULL;
12238 msg_info.hdr.serializing_sc = NULL;
12240 printf("Sent Abort to other side\n");
12242 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12243 sizeof(msg_info.task), M_NOWAIT);
12246 printf("ctl_abort_task: found I/O to abort\n");
12250 mtx_unlock(&lun->lun_lock);
12254 * This isn't really an error. It's entirely possible for
12255 * the abort and command completion to cross on the wire.
12256 * This is more of an informative/diagnostic error.
12259 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12260 "%u:%u:%u tag %d type %d\n",
12261 io->io_hdr.nexus.initid,
12262 io->io_hdr.nexus.targ_port,
12263 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12264 io->taskio.tag_type);
12267 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12272 ctl_query_task(union ctl_io *io, int task_set)
12275 struct ctl_lun *lun;
12276 struct ctl_softc *softc;
12280 softc = control_softc;
12281 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12282 mtx_lock(&softc->ctl_lock);
12283 if (targ_lun >= CTL_MAX_LUNS ||
12284 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12285 mtx_unlock(&softc->ctl_lock);
12286 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12289 mtx_lock(&lun->lun_lock);
12290 mtx_unlock(&softc->ctl_lock);
12291 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12292 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12294 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12295 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12296 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12299 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12304 mtx_unlock(&lun->lun_lock);
12306 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12308 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12313 ctl_query_async_event(union ctl_io *io)
12315 struct ctl_lun *lun;
12316 struct ctl_softc *softc;
12318 uint32_t targ_lun, initidx;
12320 softc = control_softc;
12321 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12322 mtx_lock(&softc->ctl_lock);
12323 if (targ_lun >= CTL_MAX_LUNS ||
12324 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12325 mtx_unlock(&softc->ctl_lock);
12326 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12329 mtx_lock(&lun->lun_lock);
12330 mtx_unlock(&softc->ctl_lock);
12331 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12332 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12333 mtx_unlock(&lun->lun_lock);
12334 if (ua != CTL_UA_NONE)
12335 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12337 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12342 ctl_run_task(union ctl_io *io)
12344 struct ctl_softc *softc = control_softc;
12347 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12348 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12349 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12350 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12351 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12352 switch (io->taskio.task_action) {
12353 case CTL_TASK_ABORT_TASK:
12354 retval = ctl_abort_task(io);
12356 case CTL_TASK_ABORT_TASK_SET:
12357 case CTL_TASK_CLEAR_TASK_SET:
12358 retval = ctl_abort_task_set(io);
12360 case CTL_TASK_CLEAR_ACA:
12362 case CTL_TASK_I_T_NEXUS_RESET:
12363 retval = ctl_i_t_nexus_reset(io);
12365 case CTL_TASK_LUN_RESET:
12366 retval = ctl_lun_reset(softc, io);
12368 case CTL_TASK_TARGET_RESET:
12369 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12371 case CTL_TASK_BUS_RESET:
12372 retval = ctl_bus_reset(softc, io);
12374 case CTL_TASK_PORT_LOGIN:
12376 case CTL_TASK_PORT_LOGOUT:
12378 case CTL_TASK_QUERY_TASK:
12379 retval = ctl_query_task(io, 0);
12381 case CTL_TASK_QUERY_TASK_SET:
12382 retval = ctl_query_task(io, 1);
12384 case CTL_TASK_QUERY_ASYNC_EVENT:
12385 retval = ctl_query_async_event(io);
12388 printf("%s: got unknown task management event %d\n",
12389 __func__, io->taskio.task_action);
12393 io->io_hdr.status = CTL_SUCCESS;
12395 io->io_hdr.status = CTL_ERROR;
12400 * For HA operation. Handle commands that come in from the other
12404 ctl_handle_isc(union ctl_io *io)
12406 struct ctl_softc *softc = control_softc;
12407 struct ctl_lun *lun;
12408 const struct ctl_cmd_entry *entry;
12411 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12412 switch (io->io_hdr.msg_type) {
12413 case CTL_MSG_SERIALIZE:
12414 ctl_serialize_other_sc_cmd(&io->scsiio);
12416 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12417 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12418 if (targ_lun >= CTL_MAX_LUNS ||
12419 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12423 mtx_lock(&lun->lun_lock);
12424 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12425 mtx_unlock(&lun->lun_lock);
12429 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12430 mtx_unlock(&lun->lun_lock);
12431 ctl_enqueue_rtr(io);
12433 case CTL_MSG_FINISH_IO:
12434 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12438 if (targ_lun >= CTL_MAX_LUNS ||
12439 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12443 mtx_lock(&lun->lun_lock);
12444 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12445 ctl_check_blocked(lun);
12446 mtx_unlock(&lun->lun_lock);
12449 case CTL_MSG_PERS_ACTION:
12450 ctl_hndl_per_res_out_on_other_sc(
12451 (union ctl_ha_msg *)&io->presio.pr_msg);
12454 case CTL_MSG_BAD_JUJU:
12457 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12458 ctl_datamove_remote(io);
12460 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12461 io->scsiio.be_move_done(io);
12463 case CTL_MSG_FAILOVER:
12464 ctl_failover_lun(io);
12468 printf("%s: Invalid message type %d\n",
12469 __func__, io->io_hdr.msg_type);
12478 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12479 * there is no match.
12481 static ctl_lun_error_pattern
12482 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12484 const struct ctl_cmd_entry *entry;
12485 ctl_lun_error_pattern filtered_pattern, pattern;
12487 pattern = desc->error_pattern;
12490 * XXX KDM we need more data passed into this function to match a
12491 * custom pattern, and we actually need to implement custom pattern
12494 if (pattern & CTL_LUN_PAT_CMD)
12495 return (CTL_LUN_PAT_CMD);
12497 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12498 return (CTL_LUN_PAT_ANY);
12500 entry = ctl_get_cmd_entry(ctsio, NULL);
12502 filtered_pattern = entry->pattern & pattern;
12505 * If the user requested specific flags in the pattern (e.g.
12506 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12509 * If the user did not specify any flags, it doesn't matter whether
12510 * or not the command supports the flags.
12512 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12513 (pattern & ~CTL_LUN_PAT_MASK))
12514 return (CTL_LUN_PAT_NONE);
12517 * If the user asked for a range check, see if the requested LBA
12518 * range overlaps with this command's LBA range.
12520 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12526 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12528 return (CTL_LUN_PAT_NONE);
12530 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12531 desc->lba_range.len, FALSE);
12533 * A "pass" means that the LBA ranges don't overlap, so
12534 * this doesn't match the user's range criteria.
12536 if (action == CTL_ACTION_PASS)
12537 return (CTL_LUN_PAT_NONE);
12540 return (filtered_pattern);
12544 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12546 struct ctl_error_desc *desc, *desc2;
12548 mtx_assert(&lun->lun_lock, MA_OWNED);
12550 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12551 ctl_lun_error_pattern pattern;
12553 * Check to see whether this particular command matches
12554 * the pattern in the descriptor.
12556 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12557 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12560 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12561 case CTL_LUN_INJ_ABORTED:
12562 ctl_set_aborted(&io->scsiio);
12564 case CTL_LUN_INJ_MEDIUM_ERR:
12565 ctl_set_medium_error(&io->scsiio,
12566 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12567 CTL_FLAG_DATA_OUT);
12569 case CTL_LUN_INJ_UA:
12570 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12572 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12574 case CTL_LUN_INJ_CUSTOM:
12576 * We're assuming the user knows what he is doing.
12577 * Just copy the sense information without doing
12580 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12581 MIN(sizeof(desc->custom_sense),
12582 sizeof(io->scsiio.sense_data)));
12583 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12584 io->scsiio.sense_len = SSD_FULL_SIZE;
12585 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12587 case CTL_LUN_INJ_NONE:
12590 * If this is an error injection type we don't know
12591 * about, clear the continuous flag (if it is set)
12592 * so it will get deleted below.
12594 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12598 * By default, each error injection action is a one-shot
12600 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12603 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12609 #ifdef CTL_IO_DELAY
12611 ctl_datamove_timer_wakeup(void *arg)
12615 io = (union ctl_io *)arg;
12619 #endif /* CTL_IO_DELAY */
12622 ctl_datamove(union ctl_io *io)
12624 struct ctl_lun *lun;
12625 void (*fe_datamove)(union ctl_io *io);
12627 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12629 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12631 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12633 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12638 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12639 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12641 sbuf_cat(&sb, path_str);
12642 switch (io->io_hdr.io_type) {
12644 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12645 sbuf_printf(&sb, "\n");
12646 sbuf_cat(&sb, path_str);
12647 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12648 io->scsiio.tag_num, io->scsiio.tag_type);
12651 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12652 "Tag Type: %d\n", io->taskio.task_action,
12653 io->taskio.tag_num, io->taskio.tag_type);
12656 panic("%s: Invalid CTL I/O type %d\n",
12657 __func__, io->io_hdr.io_type);
12659 sbuf_cat(&sb, path_str);
12660 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12661 (intmax_t)time_uptime - io->io_hdr.start_time);
12663 printf("%s", sbuf_data(&sb));
12665 #endif /* CTL_TIME_IO */
12667 #ifdef CTL_IO_DELAY
12668 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12669 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12672 && (lun->delay_info.datamove_delay > 0)) {
12674 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12675 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12676 callout_reset(&io->io_hdr.delay_callout,
12677 lun->delay_info.datamove_delay * hz,
12678 ctl_datamove_timer_wakeup, io);
12679 if (lun->delay_info.datamove_type ==
12680 CTL_DELAY_TYPE_ONESHOT)
12681 lun->delay_info.datamove_delay = 0;
12688 * This command has been aborted. Set the port status, so we fail
12691 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12692 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12693 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12694 io->io_hdr.nexus.targ_port,
12695 io->io_hdr.nexus.targ_lun);
12696 io->io_hdr.port_status = 31337;
12698 * Note that the backend, in this case, will get the
12699 * callback in its context. In other cases it may get
12700 * called in the frontend's interrupt thread context.
12702 io->scsiio.be_move_done(io);
12706 /* Don't confuse frontend with zero length data move. */
12707 if (io->scsiio.kern_data_len == 0) {
12708 io->scsiio.be_move_done(io);
12712 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12717 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12719 union ctl_ha_msg msg;
12721 struct bintime cur_bt;
12724 memset(&msg, 0, sizeof(msg));
12725 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12726 msg.hdr.original_sc = io;
12727 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12728 msg.hdr.nexus = io->io_hdr.nexus;
12729 msg.hdr.status = io->io_hdr.status;
12730 msg.scsi.tag_num = io->scsiio.tag_num;
12731 msg.scsi.tag_type = io->scsiio.tag_type;
12732 msg.scsi.scsi_status = io->scsiio.scsi_status;
12733 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12734 io->scsiio.sense_len);
12735 msg.scsi.sense_len = io->scsiio.sense_len;
12736 msg.scsi.sense_residual = io->scsiio.sense_residual;
12737 msg.scsi.fetd_status = io->io_hdr.port_status;
12738 msg.scsi.residual = io->scsiio.residual;
12739 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12740 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12741 ctl_failover_io(io, /*have_lock*/ have_lock);
12744 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12745 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12746 msg.scsi.sense_len, M_WAITOK);
12749 getbinuptime(&cur_bt);
12750 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12751 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12753 io->io_hdr.num_dmas++;
12757 * The DMA to the remote side is done, now we need to tell the other side
12758 * we're done so it can continue with its data movement.
12761 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12768 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12769 printf("%s: ISC DMA write failed with error %d", __func__,
12771 ctl_set_internal_failure(&io->scsiio,
12773 /*retry_count*/ rq->ret);
12776 ctl_dt_req_free(rq);
12778 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12779 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12780 free(io->io_hdr.remote_sglist, M_CTL);
12781 io->io_hdr.remote_sglist = NULL;
12782 io->io_hdr.local_sglist = NULL;
12785 * The data is in local and remote memory, so now we need to send
12786 * status (good or back) back to the other side.
12788 ctl_send_datamove_done(io, /*have_lock*/ 0);
12792 * We've moved the data from the host/controller into local memory. Now we
12793 * need to push it over to the remote controller's memory.
12796 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12800 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12801 ctl_datamove_remote_write_cb);
12806 ctl_datamove_remote_write(union ctl_io *io)
12809 void (*fe_datamove)(union ctl_io *io);
12812 * - Get the data from the host/HBA into local memory.
12813 * - DMA memory from the local controller to the remote controller.
12814 * - Send status back to the remote controller.
12817 retval = ctl_datamove_remote_sgl_setup(io);
12821 /* Switch the pointer over so the FETD knows what to do */
12822 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12825 * Use a custom move done callback, since we need to send completion
12826 * back to the other controller, not to the backend on this side.
12828 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12830 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12835 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12844 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12845 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12846 free(io->io_hdr.remote_sglist, M_CTL);
12847 io->io_hdr.remote_sglist = NULL;
12848 io->io_hdr.local_sglist = NULL;
12851 scsi_path_string(io, path_str, sizeof(path_str));
12852 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12853 sbuf_cat(&sb, path_str);
12854 scsi_command_string(&io->scsiio, NULL, &sb);
12855 sbuf_printf(&sb, "\n");
12856 sbuf_cat(&sb, path_str);
12857 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12858 io->scsiio.tag_num, io->scsiio.tag_type);
12859 sbuf_cat(&sb, path_str);
12860 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12861 io->io_hdr.flags, io->io_hdr.status);
12863 printk("%s", sbuf_data(&sb));
12868 * The read is done, now we need to send status (good or bad) back
12869 * to the other side.
12871 ctl_send_datamove_done(io, /*have_lock*/ 0);
12877 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12880 void (*fe_datamove)(union ctl_io *io);
12884 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12885 printf("%s: ISC DMA read failed with error %d\n", __func__,
12887 ctl_set_internal_failure(&io->scsiio,
12889 /*retry_count*/ rq->ret);
12892 ctl_dt_req_free(rq);
12894 /* Switch the pointer over so the FETD knows what to do */
12895 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12898 * Use a custom move done callback, since we need to send completion
12899 * back to the other controller, not to the backend on this side.
12901 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12903 /* XXX KDM add checks like the ones in ctl_datamove? */
12905 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12910 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12912 struct ctl_sg_entry *local_sglist;
12913 uint32_t len_to_go;
12918 local_sglist = io->io_hdr.local_sglist;
12919 len_to_go = io->scsiio.kern_data_len;
12922 * The difficult thing here is that the size of the various
12923 * S/G segments may be different than the size from the
12924 * remote controller. That'll make it harder when DMAing
12925 * the data back to the other side.
12927 for (i = 0; len_to_go > 0; i++) {
12928 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12929 local_sglist[i].addr =
12930 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12932 len_to_go -= local_sglist[i].len;
12935 * Reset the number of S/G entries accordingly. The original
12936 * number of S/G entries is available in rem_sg_entries.
12938 io->scsiio.kern_sg_entries = i;
12941 printf("%s: kern_sg_entries = %d\n", __func__,
12942 io->scsiio.kern_sg_entries);
12943 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12944 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12945 local_sglist[i].addr, local_sglist[i].len);
12952 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12953 ctl_ha_dt_cb callback)
12955 struct ctl_ha_dt_req *rq;
12956 struct ctl_sg_entry *remote_sglist, *local_sglist;
12957 uint32_t local_used, remote_used, total_used;
12960 rq = ctl_dt_req_alloc();
12963 * If we failed to allocate the request, and if the DMA didn't fail
12964 * anyway, set busy status. This is just a resource allocation
12968 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12969 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12970 ctl_set_busy(&io->scsiio);
12972 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12973 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12976 ctl_dt_req_free(rq);
12979 * The data move failed. We need to return status back
12980 * to the other controller. No point in trying to DMA
12981 * data to the remote controller.
12984 ctl_send_datamove_done(io, /*have_lock*/ 0);
12989 local_sglist = io->io_hdr.local_sglist;
12990 remote_sglist = io->io_hdr.remote_sglist;
12996 * Pull/push the data over the wire from/to the other controller.
12997 * This takes into account the possibility that the local and
12998 * remote sglists may not be identical in terms of the size of
12999 * the elements and the number of elements.
13001 * One fundamental assumption here is that the length allocated for
13002 * both the local and remote sglists is identical. Otherwise, we've
13003 * essentially got a coding error of some sort.
13005 isc_ret = CTL_HA_STATUS_SUCCESS;
13006 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13010 rq->command = command;
13014 * Both pointers should be aligned. But it is possible
13015 * that the allocation length is not. They should both
13016 * also have enough slack left over at the end, though,
13017 * to round up to the next 8 byte boundary.
13019 cur_len = MIN(local_sglist[i].len - local_used,
13020 remote_sglist[j].len - remote_used);
13021 rq->size = cur_len;
13023 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13024 tmp_ptr += local_used;
13027 /* Use physical addresses when talking to ISC hardware */
13028 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13029 /* XXX KDM use busdma */
13030 rq->local = vtophys(tmp_ptr);
13032 rq->local = tmp_ptr;
13034 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
13035 ("HA does not support BUS_ADDR"));
13036 rq->local = tmp_ptr;
13039 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13040 tmp_ptr += remote_used;
13041 rq->remote = tmp_ptr;
13043 rq->callback = NULL;
13045 local_used += cur_len;
13046 if (local_used >= local_sglist[i].len) {
13051 remote_used += cur_len;
13052 if (remote_used >= remote_sglist[j].len) {
13056 total_used += cur_len;
13058 if (total_used >= io->scsiio.kern_data_len)
13059 rq->callback = callback;
13062 printf("%s: %s: local %p remote %p size %d\n", __func__,
13063 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13064 rq->local, rq->remote, rq->size);
13067 isc_ret = ctl_dt_single(rq);
13068 if (isc_ret > CTL_HA_STATUS_SUCCESS)
13071 if (isc_ret != CTL_HA_STATUS_WAIT) {
13080 ctl_datamove_remote_read(union ctl_io *io)
13086 * This will send an error to the other controller in the case of a
13089 retval = ctl_datamove_remote_sgl_setup(io);
13093 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13094 ctl_datamove_remote_read_cb);
13097 * Make sure we free memory if there was an error.. The
13098 * ctl_datamove_remote_xfer() function will send the
13099 * datamove done message, or call the callback with an
13100 * error if there is a problem.
13102 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13103 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13104 free(io->io_hdr.remote_sglist, M_CTL);
13105 io->io_hdr.remote_sglist = NULL;
13106 io->io_hdr.local_sglist = NULL;
13111 * Process a datamove request from the other controller. This is used for
13112 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13113 * first. Once that is complete, the data gets DMAed into the remote
13114 * controller's memory. For reads, we DMA from the remote controller's
13115 * memory into our memory first, and then move it out to the FETD.
13118 ctl_datamove_remote(union ctl_io *io)
13121 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
13123 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13124 ctl_failover_io(io, /*have_lock*/ 0);
13129 * Note that we look for an aborted I/O here, but don't do some of
13130 * the other checks that ctl_datamove() normally does.
13131 * We don't need to run the datamove delay code, since that should
13132 * have been done if need be on the other controller.
13134 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13135 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13136 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13137 io->io_hdr.nexus.targ_port,
13138 io->io_hdr.nexus.targ_lun);
13139 io->io_hdr.port_status = 31338;
13140 ctl_send_datamove_done(io, /*have_lock*/ 0);
13144 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13145 ctl_datamove_remote_write(io);
13146 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13147 ctl_datamove_remote_read(io);
13149 io->io_hdr.port_status = 31339;
13150 ctl_send_datamove_done(io, /*have_lock*/ 0);
13155 ctl_process_done(union ctl_io *io)
13157 struct ctl_lun *lun;
13158 struct ctl_softc *softc = control_softc;
13159 void (*fe_done)(union ctl_io *io);
13160 union ctl_ha_msg msg;
13161 uint32_t targ_port = io->io_hdr.nexus.targ_port;
13163 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13164 fe_done = softc->ctl_ports[targ_port]->fe_done;
13167 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13172 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13173 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13175 sbuf_cat(&sb, path_str);
13176 switch (io->io_hdr.io_type) {
13178 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13179 sbuf_printf(&sb, "\n");
13180 sbuf_cat(&sb, path_str);
13181 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13182 io->scsiio.tag_num, io->scsiio.tag_type);
13185 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13186 "Tag Type: %d\n", io->taskio.task_action,
13187 io->taskio.tag_num, io->taskio.tag_type);
13190 panic("%s: Invalid CTL I/O type %d\n",
13191 __func__, io->io_hdr.io_type);
13193 sbuf_cat(&sb, path_str);
13194 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13195 (intmax_t)time_uptime - io->io_hdr.start_time);
13197 printf("%s", sbuf_data(&sb));
13199 #endif /* CTL_TIME_IO */
13201 switch (io->io_hdr.io_type) {
13205 if (ctl_debug & CTL_DEBUG_INFO)
13206 ctl_io_error_print(io, NULL);
13210 panic("%s: Invalid CTL I/O type %d\n",
13211 __func__, io->io_hdr.io_type);
13214 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13216 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13217 io->io_hdr.nexus.targ_mapped_lun));
13221 mtx_lock(&lun->lun_lock);
13224 * Check to see if we have any informational exception and status
13225 * of this command can be modified to report it in form of either
13226 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13228 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13229 io->io_hdr.status == CTL_SUCCESS &&
13230 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13231 uint8_t mrie = lun->MODE_IE.mrie;
13232 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13233 (lun->MODE_VER.byte3 & SMS_VER_PER));
13234 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13235 mrie == SIEP_MRIE_REC_UNCOND ||
13236 mrie == SIEP_MRIE_NO_SENSE) &&
13237 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13238 CTL_CMD_FLAG_NO_SENSE) == 0) {
13239 ctl_set_sense(&io->scsiio,
13240 /*current_error*/ 1,
13241 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13242 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13243 /*asc*/ lun->ie_asc,
13244 /*ascq*/ lun->ie_ascq,
13246 lun->ie_reported = 1;
13248 } else if (lun->ie_reported < 0)
13249 lun->ie_reported = 0;
13252 * Check to see if we have any errors to inject here. We only
13253 * inject errors for commands that don't already have errors set.
13255 if (!STAILQ_EMPTY(&lun->error_list) &&
13256 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13257 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13258 ctl_inject_error(lun, io);
13261 * XXX KDM how do we treat commands that aren't completed
13264 * XXX KDM should we also track I/O latency?
13266 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13267 io->io_hdr.io_type == CTL_IO_SCSI) {
13269 struct bintime cur_bt;
13273 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13275 type = CTL_STATS_READ;
13276 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13278 type = CTL_STATS_WRITE;
13280 type = CTL_STATS_NO_IO;
13282 lun->stats.ports[targ_port].bytes[type] +=
13283 io->scsiio.kern_total_len;
13284 lun->stats.ports[targ_port].operations[type]++;
13286 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13287 &io->io_hdr.dma_bt);
13288 getbinuptime(&cur_bt);
13289 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13290 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13292 lun->stats.ports[targ_port].num_dmas[type] +=
13293 io->io_hdr.num_dmas;
13297 * Remove this from the OOA queue.
13299 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13301 if (TAILQ_EMPTY(&lun->ooa_queue))
13302 lun->last_busy = getsbinuptime();
13306 * Run through the blocked queue on this LUN and see if anything
13307 * has become unblocked, now that this transaction is done.
13309 ctl_check_blocked(lun);
13312 * If the LUN has been invalidated, free it if there is nothing
13313 * left on its OOA queue.
13315 if ((lun->flags & CTL_LUN_INVALID)
13316 && TAILQ_EMPTY(&lun->ooa_queue)) {
13317 mtx_unlock(&lun->lun_lock);
13318 mtx_lock(&softc->ctl_lock);
13320 mtx_unlock(&softc->ctl_lock);
13322 mtx_unlock(&lun->lun_lock);
13327 * If this command has been aborted, make sure we set the status
13328 * properly. The FETD is responsible for freeing the I/O and doing
13329 * whatever it needs to do to clean up its state.
13331 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13332 ctl_set_task_aborted(&io->scsiio);
13335 * If enabled, print command error status.
13337 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13338 (ctl_debug & CTL_DEBUG_INFO) != 0)
13339 ctl_io_error_print(io, NULL);
13342 * Tell the FETD or the other shelf controller we're done with this
13343 * command. Note that only SCSI commands get to this point. Task
13344 * management commands are completed above.
13346 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13347 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13348 memset(&msg, 0, sizeof(msg));
13349 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13350 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13351 msg.hdr.nexus = io->io_hdr.nexus;
13352 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13353 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13362 * Front end should call this if it doesn't do autosense. When the request
13363 * sense comes back in from the initiator, we'll dequeue this and send it.
13366 ctl_queue_sense(union ctl_io *io)
13368 struct ctl_lun *lun;
13369 struct ctl_port *port;
13370 struct ctl_softc *softc;
13371 uint32_t initidx, targ_lun;
13373 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13375 softc = control_softc;
13376 port = ctl_io_port(&ctsio->io_hdr);
13377 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13380 * LUN lookup will likely move to the ctl_work_thread() once we
13381 * have our new queueing infrastructure (that doesn't put things on
13382 * a per-LUN queue initially). That is so that we can handle
13383 * things like an INQUIRY to a LUN that we don't have enabled. We
13384 * can't deal with that right now.
13385 * If we don't have a LUN for this, just toss the sense information.
13387 mtx_lock(&softc->ctl_lock);
13388 if (targ_lun >= CTL_MAX_LUNS ||
13389 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13390 mtx_unlock(&softc->ctl_lock);
13393 mtx_lock(&lun->lun_lock);
13394 mtx_unlock(&softc->ctl_lock);
13397 * Already have CA set for this LUN...toss the sense information.
13399 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13400 if (ctl_is_set(lun->have_ca, initidx)) {
13401 mtx_unlock(&lun->lun_lock);
13405 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13406 MIN(sizeof(lun->pending_sense[initidx]),
13407 sizeof(io->scsiio.sense_data)));
13408 ctl_set_mask(lun->have_ca, initidx);
13409 mtx_unlock(&lun->lun_lock);
13413 return (CTL_RETVAL_COMPLETE);
13418 * Primary command inlet from frontend ports. All SCSI and task I/O
13419 * requests must go through this function.
13422 ctl_queue(union ctl_io *io)
13424 struct ctl_port *port;
13426 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13429 io->io_hdr.start_time = time_uptime;
13430 getbinuptime(&io->io_hdr.start_bt);
13431 #endif /* CTL_TIME_IO */
13433 /* Map FE-specific LUN ID into global one. */
13434 port = ctl_io_port(&io->io_hdr);
13435 io->io_hdr.nexus.targ_mapped_lun =
13436 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13438 switch (io->io_hdr.io_type) {
13441 if (ctl_debug & CTL_DEBUG_CDB)
13443 ctl_enqueue_incoming(io);
13446 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13450 return (CTL_RETVAL_COMPLETE);
13453 #ifdef CTL_IO_DELAY
13455 ctl_done_timer_wakeup(void *arg)
13459 io = (union ctl_io *)arg;
13462 #endif /* CTL_IO_DELAY */
13465 ctl_serseq_done(union ctl_io *io)
13467 struct ctl_lun *lun;
13469 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13470 if (lun->be_lun == NULL ||
13471 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13473 mtx_lock(&lun->lun_lock);
13474 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13475 ctl_check_blocked(lun);
13476 mtx_unlock(&lun->lun_lock);
13480 ctl_done(union ctl_io *io)
13484 * Enable this to catch duplicate completion issues.
13487 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13488 printf("%s: type %d msg %d cdb %x iptl: "
13489 "%u:%u:%u tag 0x%04x "
13490 "flag %#x status %x\n",
13492 io->io_hdr.io_type,
13493 io->io_hdr.msg_type,
13495 io->io_hdr.nexus.initid,
13496 io->io_hdr.nexus.targ_port,
13497 io->io_hdr.nexus.targ_lun,
13498 (io->io_hdr.io_type ==
13500 io->taskio.tag_num :
13501 io->scsiio.tag_num,
13503 io->io_hdr.status);
13505 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13509 * This is an internal copy of an I/O, and should not go through
13510 * the normal done processing logic.
13512 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13515 #ifdef CTL_IO_DELAY
13516 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13517 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13519 struct ctl_lun *lun;
13521 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13524 && (lun->delay_info.done_delay > 0)) {
13526 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13527 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13528 callout_reset(&io->io_hdr.delay_callout,
13529 lun->delay_info.done_delay * hz,
13530 ctl_done_timer_wakeup, io);
13531 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13532 lun->delay_info.done_delay = 0;
13536 #endif /* CTL_IO_DELAY */
13538 ctl_enqueue_done(io);
13542 ctl_work_thread(void *arg)
13544 struct ctl_thread *thr = (struct ctl_thread *)arg;
13545 struct ctl_softc *softc = thr->ctl_softc;
13549 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13553 * We handle the queues in this order:
13555 * - done queue (to free up resources, unblock other commands)
13559 * If those queues are empty, we break out of the loop and
13562 mtx_lock(&thr->queue_lock);
13563 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13565 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13566 mtx_unlock(&thr->queue_lock);
13567 ctl_handle_isc(io);
13570 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13572 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13573 /* clear any blocked commands, call fe_done */
13574 mtx_unlock(&thr->queue_lock);
13575 ctl_process_done(io);
13578 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13580 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13581 mtx_unlock(&thr->queue_lock);
13582 if (io->io_hdr.io_type == CTL_IO_TASK)
13585 ctl_scsiio_precheck(softc, &io->scsiio);
13588 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13590 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13591 mtx_unlock(&thr->queue_lock);
13592 retval = ctl_scsiio(&io->scsiio);
13593 if (retval != CTL_RETVAL_COMPLETE)
13594 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13598 /* Sleep until we have something to do. */
13599 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13604 ctl_lun_thread(void *arg)
13606 struct ctl_softc *softc = (struct ctl_softc *)arg;
13607 struct ctl_be_lun *be_lun;
13609 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13612 mtx_lock(&softc->ctl_lock);
13613 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13614 if (be_lun != NULL) {
13615 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13616 mtx_unlock(&softc->ctl_lock);
13617 ctl_create_lun(be_lun);
13621 /* Sleep until we have something to do. */
13622 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13623 PDROP | PRIBIO, "-", 0);
13628 ctl_thresh_thread(void *arg)
13630 struct ctl_softc *softc = (struct ctl_softc *)arg;
13631 struct ctl_lun *lun;
13632 struct ctl_logical_block_provisioning_page *page;
13634 union ctl_ha_msg msg;
13635 uint64_t thres, val;
13638 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13641 mtx_lock(&softc->ctl_lock);
13642 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13643 if ((lun->flags & CTL_LUN_DISABLED) ||
13644 (lun->flags & CTL_LUN_NO_MEDIA) ||
13645 lun->backend->lun_attr == NULL)
13647 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13648 softc->ha_mode == CTL_HA_MODE_XFER)
13650 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13653 page = &lun->MODE_LBP;
13654 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13655 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13657 thres = scsi_4btoul(page->descr[i].count);
13658 thres <<= CTL_LBP_EXPONENT;
13659 switch (page->descr[i].resource) {
13661 attr = "blocksavail";
13664 attr = "blocksused";
13667 attr = "poolblocksavail";
13670 attr = "poolblocksused";
13675 mtx_unlock(&softc->ctl_lock); // XXX
13676 val = lun->backend->lun_attr(
13677 lun->be_lun->be_lun, attr);
13678 mtx_lock(&softc->ctl_lock);
13679 if (val == UINT64_MAX)
13681 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13682 == SLBPPD_ARMING_INC)
13683 e = (val >= thres);
13685 e = (val <= thres);
13689 mtx_lock(&lun->lun_lock);
13691 scsi_u64to8b((uint8_t *)&page->descr[i] -
13692 (uint8_t *)page, lun->ua_tpt_info);
13693 if (lun->lasttpt == 0 ||
13694 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13695 lun->lasttpt = time_uptime;
13696 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13702 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13705 mtx_unlock(&lun->lun_lock);
13707 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13708 /* Send msg to other side. */
13709 bzero(&msg.ua, sizeof(msg.ua));
13710 msg.hdr.msg_type = CTL_MSG_UA;
13711 msg.hdr.nexus.initid = -1;
13712 msg.hdr.nexus.targ_port = -1;
13713 msg.hdr.nexus.targ_lun = lun->lun;
13714 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13716 msg.ua.ua_set = (set > 0);
13717 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13718 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13719 mtx_unlock(&softc->ctl_lock); // XXX
13720 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13721 sizeof(msg.ua), M_WAITOK);
13722 mtx_lock(&softc->ctl_lock);
13725 mtx_unlock(&softc->ctl_lock);
13726 pause("-", CTL_LBP_PERIOD * hz);
13731 ctl_enqueue_incoming(union ctl_io *io)
13733 struct ctl_softc *softc = control_softc;
13734 struct ctl_thread *thr;
13737 idx = (io->io_hdr.nexus.targ_port * 127 +
13738 io->io_hdr.nexus.initid) % worker_threads;
13739 thr = &softc->threads[idx];
13740 mtx_lock(&thr->queue_lock);
13741 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13742 mtx_unlock(&thr->queue_lock);
13747 ctl_enqueue_rtr(union ctl_io *io)
13749 struct ctl_softc *softc = control_softc;
13750 struct ctl_thread *thr;
13752 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13753 mtx_lock(&thr->queue_lock);
13754 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13755 mtx_unlock(&thr->queue_lock);
13760 ctl_enqueue_done(union ctl_io *io)
13762 struct ctl_softc *softc = control_softc;
13763 struct ctl_thread *thr;
13765 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13766 mtx_lock(&thr->queue_lock);
13767 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13768 mtx_unlock(&thr->queue_lock);
13773 ctl_enqueue_isc(union ctl_io *io)
13775 struct ctl_softc *softc = control_softc;
13776 struct ctl_thread *thr;
13778 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13779 mtx_lock(&thr->queue_lock);
13780 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13781 mtx_unlock(&thr->queue_lock);