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 int param_len, pf, sp;
6153 int header_size, bd_len;
6154 union ctl_modepage_info *modepage_info;
6156 switch (ctsio->cdb[0]) {
6157 case MODE_SELECT_6: {
6158 struct scsi_mode_select_6 *cdb;
6160 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6162 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6163 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6164 param_len = cdb->length;
6165 header_size = sizeof(struct scsi_mode_header_6);
6168 case MODE_SELECT_10: {
6169 struct scsi_mode_select_10 *cdb;
6171 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6173 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6174 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6175 param_len = scsi_2btoul(cdb->length);
6176 header_size = sizeof(struct scsi_mode_header_10);
6180 ctl_set_invalid_opcode(ctsio);
6181 ctl_done((union ctl_io *)ctsio);
6182 return (CTL_RETVAL_COMPLETE);
6187 * "A parameter list length of zero indicates that the Data-Out Buffer
6188 * shall be empty. This condition shall not be considered as an error."
6190 if (param_len == 0) {
6191 ctl_set_success(ctsio);
6192 ctl_done((union ctl_io *)ctsio);
6193 return (CTL_RETVAL_COMPLETE);
6197 * Since we'll hit this the first time through, prior to
6198 * allocation, we don't need to free a data buffer here.
6200 if (param_len < header_size) {
6201 ctl_set_param_len_error(ctsio);
6202 ctl_done((union ctl_io *)ctsio);
6203 return (CTL_RETVAL_COMPLETE);
6207 * Allocate the data buffer and grab the user's data. In theory,
6208 * we shouldn't have to sanity check the parameter list length here
6209 * because the maximum size is 64K. We should be able to malloc
6210 * that much without too many problems.
6212 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6213 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6214 ctsio->kern_data_len = param_len;
6215 ctsio->kern_total_len = param_len;
6216 ctsio->kern_data_resid = 0;
6217 ctsio->kern_rel_offset = 0;
6218 ctsio->kern_sg_entries = 0;
6219 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6220 ctsio->be_move_done = ctl_config_move_done;
6221 ctl_datamove((union ctl_io *)ctsio);
6223 return (CTL_RETVAL_COMPLETE);
6226 switch (ctsio->cdb[0]) {
6227 case MODE_SELECT_6: {
6228 struct scsi_mode_header_6 *mh6;
6230 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6231 bd_len = mh6->blk_desc_len;
6234 case MODE_SELECT_10: {
6235 struct scsi_mode_header_10 *mh10;
6237 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6238 bd_len = scsi_2btoul(mh10->blk_desc_len);
6242 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6245 if (param_len < (header_size + bd_len)) {
6246 free(ctsio->kern_data_ptr, M_CTL);
6247 ctl_set_param_len_error(ctsio);
6248 ctl_done((union ctl_io *)ctsio);
6249 return (CTL_RETVAL_COMPLETE);
6253 * Set the IO_CONT flag, so that if this I/O gets passed to
6254 * ctl_config_write_done(), it'll get passed back to
6255 * ctl_do_mode_select() for further processing, or completion if
6258 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6259 ctsio->io_cont = ctl_do_mode_select;
6261 modepage_info = (union ctl_modepage_info *)
6262 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6263 memset(modepage_info, 0, sizeof(*modepage_info));
6264 modepage_info->header.len_left = param_len - header_size - bd_len;
6265 modepage_info->header.len_used = header_size + bd_len;
6267 return (ctl_do_mode_select((union ctl_io *)ctsio));
6271 ctl_mode_sense(struct ctl_scsiio *ctsio)
6273 struct ctl_lun *lun;
6274 int pc, page_code, dbd, llba, subpage;
6275 int alloc_len, page_len, header_len, total_len;
6276 struct scsi_mode_block_descr *block_desc;
6277 struct ctl_page_index *page_index;
6283 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6285 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6286 switch (ctsio->cdb[0]) {
6287 case MODE_SENSE_6: {
6288 struct scsi_mode_sense_6 *cdb;
6290 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6292 header_len = sizeof(struct scsi_mode_hdr_6);
6293 if (cdb->byte2 & SMS_DBD)
6296 header_len += sizeof(struct scsi_mode_block_descr);
6298 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6299 page_code = cdb->page & SMS_PAGE_CODE;
6300 subpage = cdb->subpage;
6301 alloc_len = cdb->length;
6304 case MODE_SENSE_10: {
6305 struct scsi_mode_sense_10 *cdb;
6307 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6309 header_len = sizeof(struct scsi_mode_hdr_10);
6311 if (cdb->byte2 & SMS_DBD)
6314 header_len += sizeof(struct scsi_mode_block_descr);
6315 if (cdb->byte2 & SMS10_LLBAA)
6317 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6318 page_code = cdb->page & SMS_PAGE_CODE;
6319 subpage = cdb->subpage;
6320 alloc_len = scsi_2btoul(cdb->length);
6324 ctl_set_invalid_opcode(ctsio);
6325 ctl_done((union ctl_io *)ctsio);
6326 return (CTL_RETVAL_COMPLETE);
6327 break; /* NOTREACHED */
6331 * We have to make a first pass through to calculate the size of
6332 * the pages that match the user's query. Then we allocate enough
6333 * memory to hold it, and actually copy the data into the buffer.
6335 switch (page_code) {
6336 case SMS_ALL_PAGES_PAGE: {
6342 * At the moment, values other than 0 and 0xff here are
6343 * reserved according to SPC-3.
6345 if ((subpage != SMS_SUBPAGE_PAGE_0)
6346 && (subpage != SMS_SUBPAGE_ALL)) {
6347 ctl_set_invalid_field(ctsio,
6353 ctl_done((union ctl_io *)ctsio);
6354 return (CTL_RETVAL_COMPLETE);
6357 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6358 page_index = &lun->mode_pages.index[i];
6360 /* Make sure the page is supported for this dev type */
6361 if (lun->be_lun->lun_type == T_DIRECT &&
6362 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6364 if (lun->be_lun->lun_type == T_PROCESSOR &&
6365 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6367 if (lun->be_lun->lun_type == T_CDROM &&
6368 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6372 * We don't use this subpage if the user didn't
6373 * request all subpages.
6375 if ((page_index->subpage != 0)
6376 && (subpage == SMS_SUBPAGE_PAGE_0))
6380 printf("found page %#x len %d\n",
6381 page_index->page_code & SMPH_PC_MASK,
6382 page_index->page_len);
6384 page_len += page_index->page_len;
6393 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6394 page_index = &lun->mode_pages.index[i];
6396 /* Make sure the page is supported for this dev type */
6397 if (lun->be_lun->lun_type == T_DIRECT &&
6398 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6400 if (lun->be_lun->lun_type == T_PROCESSOR &&
6401 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6403 if (lun->be_lun->lun_type == T_CDROM &&
6404 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6407 /* Look for the right page code */
6408 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6411 /* Look for the right subpage or the subpage wildcard*/
6412 if ((page_index->subpage != subpage)
6413 && (subpage != SMS_SUBPAGE_ALL))
6417 printf("found page %#x len %d\n",
6418 page_index->page_code & SMPH_PC_MASK,
6419 page_index->page_len);
6422 page_len += page_index->page_len;
6425 if (page_len == 0) {
6426 ctl_set_invalid_field(ctsio,
6432 ctl_done((union ctl_io *)ctsio);
6433 return (CTL_RETVAL_COMPLETE);
6439 total_len = header_len + page_len;
6441 printf("header_len = %d, page_len = %d, total_len = %d\n",
6442 header_len, page_len, total_len);
6445 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6446 ctsio->kern_sg_entries = 0;
6447 ctsio->kern_data_resid = 0;
6448 ctsio->kern_rel_offset = 0;
6449 if (total_len < alloc_len) {
6450 ctsio->residual = alloc_len - total_len;
6451 ctsio->kern_data_len = total_len;
6452 ctsio->kern_total_len = total_len;
6454 ctsio->residual = 0;
6455 ctsio->kern_data_len = alloc_len;
6456 ctsio->kern_total_len = alloc_len;
6459 switch (ctsio->cdb[0]) {
6460 case MODE_SENSE_6: {
6461 struct scsi_mode_hdr_6 *header;
6463 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6465 header->datalen = MIN(total_len - 1, 254);
6466 if (lun->be_lun->lun_type == T_DIRECT) {
6467 header->dev_specific = 0x10; /* DPOFUA */
6468 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6469 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6470 header->dev_specific |= 0x80; /* WP */
6473 header->block_descr_len = 0;
6475 header->block_descr_len =
6476 sizeof(struct scsi_mode_block_descr);
6477 block_desc = (struct scsi_mode_block_descr *)&header[1];
6480 case MODE_SENSE_10: {
6481 struct scsi_mode_hdr_10 *header;
6484 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6486 datalen = MIN(total_len - 2, 65533);
6487 scsi_ulto2b(datalen, header->datalen);
6488 if (lun->be_lun->lun_type == T_DIRECT) {
6489 header->dev_specific = 0x10; /* DPOFUA */
6490 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6491 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6492 header->dev_specific |= 0x80; /* WP */
6495 scsi_ulto2b(0, header->block_descr_len);
6497 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6498 header->block_descr_len);
6499 block_desc = (struct scsi_mode_block_descr *)&header[1];
6503 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6507 * If we've got a disk, use its blocksize in the block
6508 * descriptor. Otherwise, just set it to 0.
6511 if (lun->be_lun->lun_type == T_DIRECT)
6512 scsi_ulto3b(lun->be_lun->blocksize,
6513 block_desc->block_len);
6515 scsi_ulto3b(0, block_desc->block_len);
6518 switch (page_code) {
6519 case SMS_ALL_PAGES_PAGE: {
6522 data_used = header_len;
6523 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6524 struct ctl_page_index *page_index;
6526 page_index = &lun->mode_pages.index[i];
6527 if (lun->be_lun->lun_type == T_DIRECT &&
6528 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6530 if (lun->be_lun->lun_type == T_PROCESSOR &&
6531 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6533 if (lun->be_lun->lun_type == T_CDROM &&
6534 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6538 * We don't use this subpage if the user didn't
6539 * request all subpages. We already checked (above)
6540 * to make sure the user only specified a subpage
6541 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6543 if ((page_index->subpage != 0)
6544 && (subpage == SMS_SUBPAGE_PAGE_0))
6548 * Call the handler, if it exists, to update the
6549 * page to the latest values.
6551 if (page_index->sense_handler != NULL)
6552 page_index->sense_handler(ctsio, page_index,pc);
6554 memcpy(ctsio->kern_data_ptr + data_used,
6555 page_index->page_data +
6556 (page_index->page_len * pc),
6557 page_index->page_len);
6558 data_used += page_index->page_len;
6565 data_used = header_len;
6567 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6568 struct ctl_page_index *page_index;
6570 page_index = &lun->mode_pages.index[i];
6572 /* Look for the right page code */
6573 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6576 /* Look for the right subpage or the subpage wildcard*/
6577 if ((page_index->subpage != subpage)
6578 && (subpage != SMS_SUBPAGE_ALL))
6581 /* Make sure the page is supported for this dev type */
6582 if (lun->be_lun->lun_type == T_DIRECT &&
6583 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6585 if (lun->be_lun->lun_type == T_PROCESSOR &&
6586 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6588 if (lun->be_lun->lun_type == T_CDROM &&
6589 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6593 * Call the handler, if it exists, to update the
6594 * page to the latest values.
6596 if (page_index->sense_handler != NULL)
6597 page_index->sense_handler(ctsio, page_index,pc);
6599 memcpy(ctsio->kern_data_ptr + data_used,
6600 page_index->page_data +
6601 (page_index->page_len * pc),
6602 page_index->page_len);
6603 data_used += page_index->page_len;
6609 ctl_set_success(ctsio);
6610 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6611 ctsio->be_move_done = ctl_config_move_done;
6612 ctl_datamove((union ctl_io *)ctsio);
6613 return (CTL_RETVAL_COMPLETE);
6617 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6618 struct ctl_page_index *page_index,
6621 struct ctl_lun *lun;
6622 struct scsi_log_param_header *phdr;
6626 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6627 data = page_index->page_data;
6629 if (lun->backend->lun_attr != NULL &&
6630 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6632 phdr = (struct scsi_log_param_header *)data;
6633 scsi_ulto2b(0x0001, phdr->param_code);
6634 phdr->param_control = SLP_LBIN | SLP_LP;
6635 phdr->param_len = 8;
6636 data = (uint8_t *)(phdr + 1);
6637 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6638 data[4] = 0x02; /* per-pool */
6639 data += phdr->param_len;
6642 if (lun->backend->lun_attr != NULL &&
6643 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6645 phdr = (struct scsi_log_param_header *)data;
6646 scsi_ulto2b(0x0002, phdr->param_code);
6647 phdr->param_control = SLP_LBIN | SLP_LP;
6648 phdr->param_len = 8;
6649 data = (uint8_t *)(phdr + 1);
6650 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6651 data[4] = 0x01; /* per-LUN */
6652 data += phdr->param_len;
6655 if (lun->backend->lun_attr != NULL &&
6656 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6658 phdr = (struct scsi_log_param_header *)data;
6659 scsi_ulto2b(0x00f1, phdr->param_code);
6660 phdr->param_control = SLP_LBIN | SLP_LP;
6661 phdr->param_len = 8;
6662 data = (uint8_t *)(phdr + 1);
6663 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6664 data[4] = 0x02; /* per-pool */
6665 data += phdr->param_len;
6668 if (lun->backend->lun_attr != NULL &&
6669 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6671 phdr = (struct scsi_log_param_header *)data;
6672 scsi_ulto2b(0x00f2, phdr->param_code);
6673 phdr->param_control = SLP_LBIN | SLP_LP;
6674 phdr->param_len = 8;
6675 data = (uint8_t *)(phdr + 1);
6676 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6677 data[4] = 0x02; /* per-pool */
6678 data += phdr->param_len;
6681 page_index->page_len = data - page_index->page_data;
6686 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6687 struct ctl_page_index *page_index,
6690 struct ctl_lun *lun;
6691 struct stat_page *data;
6692 uint64_t rn, wn, rb, wb;
6693 struct bintime rt, wt;
6696 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6697 data = (struct stat_page *)page_index->page_data;
6699 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6700 data->sap.hdr.param_control = SLP_LBIN;
6701 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6702 sizeof(struct scsi_log_param_header);
6703 rn = wn = rb = wb = 0;
6706 for (i = 0; i < CTL_MAX_PORTS; i++) {
6707 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6708 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6709 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6710 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6711 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6712 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6714 scsi_u64to8b(rn, data->sap.read_num);
6715 scsi_u64to8b(wn, data->sap.write_num);
6716 if (lun->stats.blocksize > 0) {
6717 scsi_u64to8b(wb / lun->stats.blocksize,
6718 data->sap.recvieved_lba);
6719 scsi_u64to8b(rb / lun->stats.blocksize,
6720 data->sap.transmitted_lba);
6722 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6723 data->sap.read_int);
6724 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6725 data->sap.write_int);
6726 scsi_u64to8b(0, data->sap.weighted_num);
6727 scsi_u64to8b(0, data->sap.weighted_int);
6728 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6729 data->it.hdr.param_control = SLP_LBIN;
6730 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6731 sizeof(struct scsi_log_param_header);
6733 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6735 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6736 data->it.hdr.param_control = SLP_LBIN;
6737 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6738 sizeof(struct scsi_log_param_header);
6739 scsi_ulto4b(3, data->ti.exponent);
6740 scsi_ulto4b(1, data->ti.integer);
6745 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6746 struct ctl_page_index *page_index,
6749 struct ctl_lun *lun;
6750 struct scsi_log_informational_exceptions *data;
6752 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6753 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6755 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6756 data->hdr.param_control = SLP_LBIN;
6757 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6758 sizeof(struct scsi_log_param_header);
6759 data->ie_asc = lun->ie_asc;
6760 data->ie_ascq = lun->ie_ascq;
6761 data->temperature = 0xff;
6766 ctl_log_sense(struct ctl_scsiio *ctsio)
6768 struct ctl_lun *lun;
6769 int i, pc, page_code, subpage;
6770 int alloc_len, total_len;
6771 struct ctl_page_index *page_index;
6772 struct scsi_log_sense *cdb;
6773 struct scsi_log_header *header;
6775 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6777 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6778 cdb = (struct scsi_log_sense *)ctsio->cdb;
6779 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6780 page_code = cdb->page & SLS_PAGE_CODE;
6781 subpage = cdb->subpage;
6782 alloc_len = scsi_2btoul(cdb->length);
6785 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6786 page_index = &lun->log_pages.index[i];
6788 /* Look for the right page code */
6789 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6792 /* Look for the right subpage or the subpage wildcard*/
6793 if (page_index->subpage != subpage)
6798 if (i >= CTL_NUM_LOG_PAGES) {
6799 ctl_set_invalid_field(ctsio,
6805 ctl_done((union ctl_io *)ctsio);
6806 return (CTL_RETVAL_COMPLETE);
6809 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6811 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6812 ctsio->kern_sg_entries = 0;
6813 ctsio->kern_data_resid = 0;
6814 ctsio->kern_rel_offset = 0;
6815 if (total_len < alloc_len) {
6816 ctsio->residual = alloc_len - total_len;
6817 ctsio->kern_data_len = total_len;
6818 ctsio->kern_total_len = total_len;
6820 ctsio->residual = 0;
6821 ctsio->kern_data_len = alloc_len;
6822 ctsio->kern_total_len = alloc_len;
6825 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6826 header->page = page_index->page_code;
6827 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6828 header->page |= SL_DS;
6829 if (page_index->subpage) {
6830 header->page |= SL_SPF;
6831 header->subpage = page_index->subpage;
6833 scsi_ulto2b(page_index->page_len, header->datalen);
6836 * Call the handler, if it exists, to update the
6837 * page to the latest values.
6839 if (page_index->sense_handler != NULL)
6840 page_index->sense_handler(ctsio, page_index, pc);
6842 memcpy(header + 1, page_index->page_data, page_index->page_len);
6844 ctl_set_success(ctsio);
6845 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6846 ctsio->be_move_done = ctl_config_move_done;
6847 ctl_datamove((union ctl_io *)ctsio);
6848 return (CTL_RETVAL_COMPLETE);
6852 ctl_read_capacity(struct ctl_scsiio *ctsio)
6854 struct scsi_read_capacity *cdb;
6855 struct scsi_read_capacity_data *data;
6856 struct ctl_lun *lun;
6859 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6861 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6863 lba = scsi_4btoul(cdb->addr);
6864 if (((cdb->pmi & SRC_PMI) == 0)
6866 ctl_set_invalid_field(/*ctsio*/ ctsio,
6872 ctl_done((union ctl_io *)ctsio);
6873 return (CTL_RETVAL_COMPLETE);
6876 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6878 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6879 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6880 ctsio->residual = 0;
6881 ctsio->kern_data_len = sizeof(*data);
6882 ctsio->kern_total_len = sizeof(*data);
6883 ctsio->kern_data_resid = 0;
6884 ctsio->kern_rel_offset = 0;
6885 ctsio->kern_sg_entries = 0;
6888 * If the maximum LBA is greater than 0xfffffffe, the user must
6889 * issue a SERVICE ACTION IN (16) command, with the read capacity
6890 * serivce action set.
6892 if (lun->be_lun->maxlba > 0xfffffffe)
6893 scsi_ulto4b(0xffffffff, data->addr);
6895 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6898 * XXX KDM this may not be 512 bytes...
6900 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6902 ctl_set_success(ctsio);
6903 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6904 ctsio->be_move_done = ctl_config_move_done;
6905 ctl_datamove((union ctl_io *)ctsio);
6906 return (CTL_RETVAL_COMPLETE);
6910 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6912 struct scsi_read_capacity_16 *cdb;
6913 struct scsi_read_capacity_data_long *data;
6914 struct ctl_lun *lun;
6918 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6920 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6922 alloc_len = scsi_4btoul(cdb->alloc_len);
6923 lba = scsi_8btou64(cdb->addr);
6925 if ((cdb->reladr & SRC16_PMI)
6927 ctl_set_invalid_field(/*ctsio*/ ctsio,
6933 ctl_done((union ctl_io *)ctsio);
6934 return (CTL_RETVAL_COMPLETE);
6937 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6939 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6940 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6942 if (sizeof(*data) < alloc_len) {
6943 ctsio->residual = alloc_len - sizeof(*data);
6944 ctsio->kern_data_len = sizeof(*data);
6945 ctsio->kern_total_len = sizeof(*data);
6947 ctsio->residual = 0;
6948 ctsio->kern_data_len = alloc_len;
6949 ctsio->kern_total_len = alloc_len;
6951 ctsio->kern_data_resid = 0;
6952 ctsio->kern_rel_offset = 0;
6953 ctsio->kern_sg_entries = 0;
6955 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6956 /* XXX KDM this may not be 512 bytes... */
6957 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6958 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6959 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6960 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6961 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6963 ctl_set_success(ctsio);
6964 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6965 ctsio->be_move_done = ctl_config_move_done;
6966 ctl_datamove((union ctl_io *)ctsio);
6967 return (CTL_RETVAL_COMPLETE);
6971 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6973 struct scsi_get_lba_status *cdb;
6974 struct scsi_get_lba_status_data *data;
6975 struct ctl_lun *lun;
6976 struct ctl_lba_len_flags *lbalen;
6978 uint32_t alloc_len, total_len;
6981 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
6983 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6984 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
6985 lba = scsi_8btou64(cdb->addr);
6986 alloc_len = scsi_4btoul(cdb->alloc_len);
6988 if (lba > lun->be_lun->maxlba) {
6989 ctl_set_lba_out_of_range(ctsio, lba);
6990 ctl_done((union ctl_io *)ctsio);
6991 return (CTL_RETVAL_COMPLETE);
6994 total_len = sizeof(*data) + sizeof(data->descr[0]);
6995 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6996 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
6998 if (total_len < alloc_len) {
6999 ctsio->residual = alloc_len - total_len;
7000 ctsio->kern_data_len = total_len;
7001 ctsio->kern_total_len = total_len;
7003 ctsio->residual = 0;
7004 ctsio->kern_data_len = alloc_len;
7005 ctsio->kern_total_len = alloc_len;
7007 ctsio->kern_data_resid = 0;
7008 ctsio->kern_rel_offset = 0;
7009 ctsio->kern_sg_entries = 0;
7011 /* Fill dummy data in case backend can't tell anything. */
7012 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7013 scsi_u64to8b(lba, data->descr[0].addr);
7014 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7015 data->descr[0].length);
7016 data->descr[0].status = 0; /* Mapped or unknown. */
7018 ctl_set_success(ctsio);
7019 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7020 ctsio->be_move_done = ctl_config_move_done;
7022 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7024 lbalen->len = total_len;
7026 retval = lun->backend->config_read((union ctl_io *)ctsio);
7027 return (CTL_RETVAL_COMPLETE);
7031 ctl_read_defect(struct ctl_scsiio *ctsio)
7033 struct scsi_read_defect_data_10 *ccb10;
7034 struct scsi_read_defect_data_12 *ccb12;
7035 struct scsi_read_defect_data_hdr_10 *data10;
7036 struct scsi_read_defect_data_hdr_12 *data12;
7037 uint32_t alloc_len, data_len;
7040 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7042 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7043 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7044 format = ccb10->format;
7045 alloc_len = scsi_2btoul(ccb10->alloc_length);
7046 data_len = sizeof(*data10);
7048 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7049 format = ccb12->format;
7050 alloc_len = scsi_4btoul(ccb12->alloc_length);
7051 data_len = sizeof(*data12);
7053 if (alloc_len == 0) {
7054 ctl_set_success(ctsio);
7055 ctl_done((union ctl_io *)ctsio);
7056 return (CTL_RETVAL_COMPLETE);
7059 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7060 if (data_len < alloc_len) {
7061 ctsio->residual = alloc_len - data_len;
7062 ctsio->kern_data_len = data_len;
7063 ctsio->kern_total_len = data_len;
7065 ctsio->residual = 0;
7066 ctsio->kern_data_len = alloc_len;
7067 ctsio->kern_total_len = alloc_len;
7069 ctsio->kern_data_resid = 0;
7070 ctsio->kern_rel_offset = 0;
7071 ctsio->kern_sg_entries = 0;
7073 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7074 data10 = (struct scsi_read_defect_data_hdr_10 *)
7075 ctsio->kern_data_ptr;
7076 data10->format = format;
7077 scsi_ulto2b(0, data10->length);
7079 data12 = (struct scsi_read_defect_data_hdr_12 *)
7080 ctsio->kern_data_ptr;
7081 data12->format = format;
7082 scsi_ulto2b(0, data12->generation);
7083 scsi_ulto4b(0, data12->length);
7086 ctl_set_success(ctsio);
7087 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7088 ctsio->be_move_done = ctl_config_move_done;
7089 ctl_datamove((union ctl_io *)ctsio);
7090 return (CTL_RETVAL_COMPLETE);
7094 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7096 struct scsi_maintenance_in *cdb;
7098 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7099 int num_ha_groups, num_target_ports, shared_group;
7100 struct ctl_lun *lun;
7101 struct ctl_softc *softc;
7102 struct ctl_port *port;
7103 struct scsi_target_group_data *rtg_ptr;
7104 struct scsi_target_group_data_extended *rtg_ext_ptr;
7105 struct scsi_target_port_group_descriptor *tpg_desc;
7107 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7109 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7110 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7111 softc = lun->ctl_softc;
7113 retval = CTL_RETVAL_COMPLETE;
7115 switch (cdb->byte2 & STG_PDF_MASK) {
7116 case STG_PDF_LENGTH:
7119 case STG_PDF_EXTENDED:
7123 ctl_set_invalid_field(/*ctsio*/ ctsio,
7129 ctl_done((union ctl_io *)ctsio);
7133 num_target_ports = 0;
7134 shared_group = (softc->is_single != 0);
7135 mtx_lock(&softc->ctl_lock);
7136 STAILQ_FOREACH(port, &softc->port_list, links) {
7137 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7139 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7142 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7145 mtx_unlock(&softc->ctl_lock);
7146 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7149 total_len = sizeof(struct scsi_target_group_data_extended);
7151 total_len = sizeof(struct scsi_target_group_data);
7152 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7153 (shared_group + num_ha_groups) +
7154 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7156 alloc_len = scsi_4btoul(cdb->length);
7158 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7160 ctsio->kern_sg_entries = 0;
7162 if (total_len < alloc_len) {
7163 ctsio->residual = alloc_len - total_len;
7164 ctsio->kern_data_len = total_len;
7165 ctsio->kern_total_len = total_len;
7167 ctsio->residual = 0;
7168 ctsio->kern_data_len = alloc_len;
7169 ctsio->kern_total_len = alloc_len;
7171 ctsio->kern_data_resid = 0;
7172 ctsio->kern_rel_offset = 0;
7175 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7176 ctsio->kern_data_ptr;
7177 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7178 rtg_ext_ptr->format_type = 0x10;
7179 rtg_ext_ptr->implicit_transition_time = 0;
7180 tpg_desc = &rtg_ext_ptr->groups[0];
7182 rtg_ptr = (struct scsi_target_group_data *)
7183 ctsio->kern_data_ptr;
7184 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7185 tpg_desc = &rtg_ptr->groups[0];
7188 mtx_lock(&softc->ctl_lock);
7189 pg = softc->port_min / softc->port_cnt;
7190 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7191 /* Some shelf is known to be primary. */
7192 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7193 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7194 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7195 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7196 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7197 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7199 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7200 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7201 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7204 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7207 /* No known primary shelf. */
7208 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7209 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7210 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7211 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7212 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7213 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7215 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7219 tpg_desc->pref_state = ts;
7220 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7221 TPG_U_SUP | TPG_T_SUP;
7222 scsi_ulto2b(1, tpg_desc->target_port_group);
7223 tpg_desc->status = TPG_IMPLICIT;
7225 STAILQ_FOREACH(port, &softc->port_list, links) {
7226 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7228 if (!softc->is_single &&
7229 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7231 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7233 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7234 relative_target_port_identifier);
7237 tpg_desc->target_port_count = pc;
7238 tpg_desc = (struct scsi_target_port_group_descriptor *)
7239 &tpg_desc->descriptors[pc];
7241 for (g = 0; g < num_ha_groups; g++) {
7242 tpg_desc->pref_state = (g == pg) ? ts : os;
7243 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7244 TPG_U_SUP | TPG_T_SUP;
7245 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7246 tpg_desc->status = TPG_IMPLICIT;
7248 STAILQ_FOREACH(port, &softc->port_list, links) {
7249 if (port->targ_port < g * softc->port_cnt ||
7250 port->targ_port >= (g + 1) * softc->port_cnt)
7252 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7254 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7256 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7258 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7259 relative_target_port_identifier);
7262 tpg_desc->target_port_count = pc;
7263 tpg_desc = (struct scsi_target_port_group_descriptor *)
7264 &tpg_desc->descriptors[pc];
7266 mtx_unlock(&softc->ctl_lock);
7268 ctl_set_success(ctsio);
7269 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7270 ctsio->be_move_done = ctl_config_move_done;
7271 ctl_datamove((union ctl_io *)ctsio);
7276 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7278 struct ctl_lun *lun;
7279 struct scsi_report_supported_opcodes *cdb;
7280 const struct ctl_cmd_entry *entry, *sentry;
7281 struct scsi_report_supported_opcodes_all *all;
7282 struct scsi_report_supported_opcodes_descr *descr;
7283 struct scsi_report_supported_opcodes_one *one;
7285 int alloc_len, total_len;
7286 int opcode, service_action, i, j, num;
7288 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7290 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7291 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7293 retval = CTL_RETVAL_COMPLETE;
7295 opcode = cdb->requested_opcode;
7296 service_action = scsi_2btoul(cdb->requested_service_action);
7297 switch (cdb->options & RSO_OPTIONS_MASK) {
7298 case RSO_OPTIONS_ALL:
7300 for (i = 0; i < 256; i++) {
7301 entry = &ctl_cmd_table[i];
7302 if (entry->flags & CTL_CMD_FLAG_SA5) {
7303 for (j = 0; j < 32; j++) {
7304 sentry = &((const struct ctl_cmd_entry *)
7306 if (ctl_cmd_applicable(
7307 lun->be_lun->lun_type, sentry))
7311 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7316 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7317 num * sizeof(struct scsi_report_supported_opcodes_descr);
7319 case RSO_OPTIONS_OC:
7320 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7321 ctl_set_invalid_field(/*ctsio*/ ctsio,
7327 ctl_done((union ctl_io *)ctsio);
7328 return (CTL_RETVAL_COMPLETE);
7330 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7332 case RSO_OPTIONS_OC_SA:
7333 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7334 service_action >= 32) {
7335 ctl_set_invalid_field(/*ctsio*/ ctsio,
7341 ctl_done((union ctl_io *)ctsio);
7342 return (CTL_RETVAL_COMPLETE);
7345 case RSO_OPTIONS_OC_ASA:
7346 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7349 ctl_set_invalid_field(/*ctsio*/ ctsio,
7355 ctl_done((union ctl_io *)ctsio);
7356 return (CTL_RETVAL_COMPLETE);
7359 alloc_len = scsi_4btoul(cdb->length);
7361 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7363 ctsio->kern_sg_entries = 0;
7365 if (total_len < alloc_len) {
7366 ctsio->residual = alloc_len - total_len;
7367 ctsio->kern_data_len = total_len;
7368 ctsio->kern_total_len = total_len;
7370 ctsio->residual = 0;
7371 ctsio->kern_data_len = alloc_len;
7372 ctsio->kern_total_len = alloc_len;
7374 ctsio->kern_data_resid = 0;
7375 ctsio->kern_rel_offset = 0;
7377 switch (cdb->options & RSO_OPTIONS_MASK) {
7378 case RSO_OPTIONS_ALL:
7379 all = (struct scsi_report_supported_opcodes_all *)
7380 ctsio->kern_data_ptr;
7382 for (i = 0; i < 256; i++) {
7383 entry = &ctl_cmd_table[i];
7384 if (entry->flags & CTL_CMD_FLAG_SA5) {
7385 for (j = 0; j < 32; j++) {
7386 sentry = &((const struct ctl_cmd_entry *)
7388 if (!ctl_cmd_applicable(
7389 lun->be_lun->lun_type, sentry))
7391 descr = &all->descr[num++];
7393 scsi_ulto2b(j, descr->service_action);
7394 descr->flags = RSO_SERVACTV;
7395 scsi_ulto2b(sentry->length,
7399 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7402 descr = &all->descr[num++];
7404 scsi_ulto2b(0, descr->service_action);
7406 scsi_ulto2b(entry->length, descr->cdb_length);
7410 num * sizeof(struct scsi_report_supported_opcodes_descr),
7413 case RSO_OPTIONS_OC:
7414 one = (struct scsi_report_supported_opcodes_one *)
7415 ctsio->kern_data_ptr;
7416 entry = &ctl_cmd_table[opcode];
7418 case RSO_OPTIONS_OC_SA:
7419 one = (struct scsi_report_supported_opcodes_one *)
7420 ctsio->kern_data_ptr;
7421 entry = &ctl_cmd_table[opcode];
7422 entry = &((const struct ctl_cmd_entry *)
7423 entry->execute)[service_action];
7425 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7427 scsi_ulto2b(entry->length, one->cdb_length);
7428 one->cdb_usage[0] = opcode;
7429 memcpy(&one->cdb_usage[1], entry->usage,
7434 case RSO_OPTIONS_OC_ASA:
7435 one = (struct scsi_report_supported_opcodes_one *)
7436 ctsio->kern_data_ptr;
7437 entry = &ctl_cmd_table[opcode];
7438 if (entry->flags & CTL_CMD_FLAG_SA5) {
7439 entry = &((const struct ctl_cmd_entry *)
7440 entry->execute)[service_action];
7441 } else if (service_action != 0) {
7448 ctl_set_success(ctsio);
7449 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7450 ctsio->be_move_done = ctl_config_move_done;
7451 ctl_datamove((union ctl_io *)ctsio);
7456 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7458 struct scsi_report_supported_tmf *cdb;
7459 struct scsi_report_supported_tmf_ext_data *data;
7461 int alloc_len, total_len;
7463 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7465 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7467 retval = CTL_RETVAL_COMPLETE;
7469 if (cdb->options & RST_REPD)
7470 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7472 total_len = sizeof(struct scsi_report_supported_tmf_data);
7473 alloc_len = scsi_4btoul(cdb->length);
7475 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7477 ctsio->kern_sg_entries = 0;
7479 if (total_len < alloc_len) {
7480 ctsio->residual = alloc_len - total_len;
7481 ctsio->kern_data_len = total_len;
7482 ctsio->kern_total_len = total_len;
7484 ctsio->residual = 0;
7485 ctsio->kern_data_len = alloc_len;
7486 ctsio->kern_total_len = alloc_len;
7488 ctsio->kern_data_resid = 0;
7489 ctsio->kern_rel_offset = 0;
7491 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7492 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7494 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7495 data->length = total_len - 4;
7497 ctl_set_success(ctsio);
7498 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7499 ctsio->be_move_done = ctl_config_move_done;
7500 ctl_datamove((union ctl_io *)ctsio);
7505 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7507 struct scsi_report_timestamp *cdb;
7508 struct scsi_report_timestamp_data *data;
7512 int alloc_len, total_len;
7514 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7516 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7518 retval = CTL_RETVAL_COMPLETE;
7520 total_len = sizeof(struct scsi_report_timestamp_data);
7521 alloc_len = scsi_4btoul(cdb->length);
7523 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7525 ctsio->kern_sg_entries = 0;
7527 if (total_len < alloc_len) {
7528 ctsio->residual = alloc_len - total_len;
7529 ctsio->kern_data_len = total_len;
7530 ctsio->kern_total_len = total_len;
7532 ctsio->residual = 0;
7533 ctsio->kern_data_len = alloc_len;
7534 ctsio->kern_total_len = alloc_len;
7536 ctsio->kern_data_resid = 0;
7537 ctsio->kern_rel_offset = 0;
7539 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7540 scsi_ulto2b(sizeof(*data) - 2, data->length);
7541 data->origin = RTS_ORIG_OUTSIDE;
7543 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7544 scsi_ulto4b(timestamp >> 16, data->timestamp);
7545 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7547 ctl_set_success(ctsio);
7548 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7549 ctsio->be_move_done = ctl_config_move_done;
7550 ctl_datamove((union ctl_io *)ctsio);
7555 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7557 struct scsi_per_res_in *cdb;
7558 int alloc_len, total_len = 0;
7559 /* struct scsi_per_res_in_rsrv in_data; */
7560 struct ctl_lun *lun;
7561 struct ctl_softc *softc;
7564 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7566 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7568 alloc_len = scsi_2btoul(cdb->length);
7570 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7571 softc = lun->ctl_softc;
7574 mtx_lock(&lun->lun_lock);
7575 switch (cdb->action) {
7576 case SPRI_RK: /* read keys */
7577 total_len = sizeof(struct scsi_per_res_in_keys) +
7579 sizeof(struct scsi_per_res_key);
7581 case SPRI_RR: /* read reservation */
7582 if (lun->flags & CTL_LUN_PR_RESERVED)
7583 total_len = sizeof(struct scsi_per_res_in_rsrv);
7585 total_len = sizeof(struct scsi_per_res_in_header);
7587 case SPRI_RC: /* report capabilities */
7588 total_len = sizeof(struct scsi_per_res_cap);
7590 case SPRI_RS: /* read full status */
7591 total_len = sizeof(struct scsi_per_res_in_header) +
7592 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7596 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7598 mtx_unlock(&lun->lun_lock);
7600 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7602 if (total_len < alloc_len) {
7603 ctsio->residual = alloc_len - total_len;
7604 ctsio->kern_data_len = total_len;
7605 ctsio->kern_total_len = total_len;
7607 ctsio->residual = 0;
7608 ctsio->kern_data_len = alloc_len;
7609 ctsio->kern_total_len = alloc_len;
7612 ctsio->kern_data_resid = 0;
7613 ctsio->kern_rel_offset = 0;
7614 ctsio->kern_sg_entries = 0;
7616 mtx_lock(&lun->lun_lock);
7617 switch (cdb->action) {
7618 case SPRI_RK: { // read keys
7619 struct scsi_per_res_in_keys *res_keys;
7622 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7625 * We had to drop the lock to allocate our buffer, which
7626 * leaves time for someone to come in with another
7627 * persistent reservation. (That is unlikely, though,
7628 * since this should be the only persistent reservation
7629 * command active right now.)
7631 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7632 (lun->pr_key_count *
7633 sizeof(struct scsi_per_res_key)))){
7634 mtx_unlock(&lun->lun_lock);
7635 free(ctsio->kern_data_ptr, M_CTL);
7636 printf("%s: reservation length changed, retrying\n",
7641 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7643 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7644 lun->pr_key_count, res_keys->header.length);
7646 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7647 if ((key = ctl_get_prkey(lun, i)) == 0)
7651 * We used lun->pr_key_count to calculate the
7652 * size to allocate. If it turns out the number of
7653 * initiators with the registered flag set is
7654 * larger than that (i.e. they haven't been kept in
7655 * sync), we've got a problem.
7657 if (key_count >= lun->pr_key_count) {
7661 scsi_u64to8b(key, res_keys->keys[key_count].key);
7666 case SPRI_RR: { // read reservation
7667 struct scsi_per_res_in_rsrv *res;
7668 int tmp_len, header_only;
7670 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7672 scsi_ulto4b(lun->pr_generation, res->header.generation);
7674 if (lun->flags & CTL_LUN_PR_RESERVED)
7676 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7677 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7678 res->header.length);
7681 tmp_len = sizeof(struct scsi_per_res_in_header);
7682 scsi_ulto4b(0, res->header.length);
7687 * We had to drop the lock to allocate our buffer, which
7688 * leaves time for someone to come in with another
7689 * persistent reservation. (That is unlikely, though,
7690 * since this should be the only persistent reservation
7691 * command active right now.)
7693 if (tmp_len != total_len) {
7694 mtx_unlock(&lun->lun_lock);
7695 free(ctsio->kern_data_ptr, M_CTL);
7696 printf("%s: reservation status changed, retrying\n",
7702 * No reservation held, so we're done.
7704 if (header_only != 0)
7708 * If the registration is an All Registrants type, the key
7709 * is 0, since it doesn't really matter.
7711 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7712 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7713 res->data.reservation);
7715 res->data.scopetype = lun->pr_res_type;
7718 case SPRI_RC: //report capabilities
7720 struct scsi_per_res_cap *res_cap;
7723 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7724 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7725 res_cap->flags1 = SPRI_CRH;
7726 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7727 type_mask = SPRI_TM_WR_EX_AR |
7733 scsi_ulto2b(type_mask, res_cap->type_mask);
7736 case SPRI_RS: { // read full status
7737 struct scsi_per_res_in_full *res_status;
7738 struct scsi_per_res_in_full_desc *res_desc;
7739 struct ctl_port *port;
7742 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7745 * We had to drop the lock to allocate our buffer, which
7746 * leaves time for someone to come in with another
7747 * persistent reservation. (That is unlikely, though,
7748 * since this should be the only persistent reservation
7749 * command active right now.)
7751 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7752 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7753 lun->pr_key_count)){
7754 mtx_unlock(&lun->lun_lock);
7755 free(ctsio->kern_data_ptr, M_CTL);
7756 printf("%s: reservation length changed, retrying\n",
7761 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7763 res_desc = &res_status->desc[0];
7764 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7765 if ((key = ctl_get_prkey(lun, i)) == 0)
7768 scsi_u64to8b(key, res_desc->res_key.key);
7769 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7770 (lun->pr_res_idx == i ||
7771 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7772 res_desc->flags = SPRI_FULL_R_HOLDER;
7773 res_desc->scopetype = lun->pr_res_type;
7775 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7776 res_desc->rel_trgt_port_id);
7778 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7780 len = ctl_create_iid(port,
7781 i % CTL_MAX_INIT_PER_PORT,
7782 res_desc->transport_id);
7783 scsi_ulto4b(len, res_desc->additional_length);
7784 res_desc = (struct scsi_per_res_in_full_desc *)
7785 &res_desc->transport_id[len];
7787 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7788 res_status->header.length);
7792 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7794 mtx_unlock(&lun->lun_lock);
7796 ctl_set_success(ctsio);
7797 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7798 ctsio->be_move_done = ctl_config_move_done;
7799 ctl_datamove((union ctl_io *)ctsio);
7800 return (CTL_RETVAL_COMPLETE);
7804 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7808 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7809 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7810 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7811 struct scsi_per_res_out_parms* param)
7813 union ctl_ha_msg persis_io;
7816 mtx_lock(&lun->lun_lock);
7817 if (sa_res_key == 0) {
7818 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7819 /* validate scope and type */
7820 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7822 mtx_unlock(&lun->lun_lock);
7823 ctl_set_invalid_field(/*ctsio*/ ctsio,
7829 ctl_done((union ctl_io *)ctsio);
7833 if (type>8 || type==2 || type==4 || type==0) {
7834 mtx_unlock(&lun->lun_lock);
7835 ctl_set_invalid_field(/*ctsio*/ ctsio,
7841 ctl_done((union ctl_io *)ctsio);
7846 * Unregister everybody else and build UA for
7849 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7850 if (i == residx || ctl_get_prkey(lun, i) == 0)
7853 ctl_clr_prkey(lun, i);
7854 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7856 lun->pr_key_count = 1;
7857 lun->pr_res_type = type;
7858 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7859 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7860 lun->pr_res_idx = residx;
7861 lun->pr_generation++;
7862 mtx_unlock(&lun->lun_lock);
7864 /* send msg to other side */
7865 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7866 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7867 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7868 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7869 persis_io.pr.pr_info.res_type = type;
7870 memcpy(persis_io.pr.pr_info.sa_res_key,
7871 param->serv_act_res_key,
7872 sizeof(param->serv_act_res_key));
7873 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7874 sizeof(persis_io.pr), M_WAITOK);
7876 /* not all registrants */
7877 mtx_unlock(&lun->lun_lock);
7878 free(ctsio->kern_data_ptr, M_CTL);
7879 ctl_set_invalid_field(ctsio,
7885 ctl_done((union ctl_io *)ctsio);
7888 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7889 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7892 if (res_key == sa_res_key) {
7895 * The spec implies this is not good but doesn't
7896 * say what to do. There are two choices either
7897 * generate a res conflict or check condition
7898 * with illegal field in parameter data. Since
7899 * that is what is done when the sa_res_key is
7900 * zero I'll take that approach since this has
7901 * to do with the sa_res_key.
7903 mtx_unlock(&lun->lun_lock);
7904 free(ctsio->kern_data_ptr, M_CTL);
7905 ctl_set_invalid_field(ctsio,
7911 ctl_done((union ctl_io *)ctsio);
7915 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7916 if (ctl_get_prkey(lun, i) != sa_res_key)
7920 ctl_clr_prkey(lun, i);
7921 lun->pr_key_count--;
7922 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7925 mtx_unlock(&lun->lun_lock);
7926 free(ctsio->kern_data_ptr, M_CTL);
7927 ctl_set_reservation_conflict(ctsio);
7928 ctl_done((union ctl_io *)ctsio);
7929 return (CTL_RETVAL_COMPLETE);
7931 lun->pr_generation++;
7932 mtx_unlock(&lun->lun_lock);
7934 /* send msg to other side */
7935 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7936 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7937 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7938 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7939 persis_io.pr.pr_info.res_type = type;
7940 memcpy(persis_io.pr.pr_info.sa_res_key,
7941 param->serv_act_res_key,
7942 sizeof(param->serv_act_res_key));
7943 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7944 sizeof(persis_io.pr), M_WAITOK);
7946 /* Reserved but not all registrants */
7947 /* sa_res_key is res holder */
7948 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7949 /* validate scope and type */
7950 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7952 mtx_unlock(&lun->lun_lock);
7953 ctl_set_invalid_field(/*ctsio*/ ctsio,
7959 ctl_done((union ctl_io *)ctsio);
7963 if (type>8 || type==2 || type==4 || type==0) {
7964 mtx_unlock(&lun->lun_lock);
7965 ctl_set_invalid_field(/*ctsio*/ ctsio,
7971 ctl_done((union ctl_io *)ctsio);
7977 * if sa_res_key != res_key remove all
7978 * registrants w/sa_res_key and generate UA
7979 * for these registrants(Registrations
7980 * Preempted) if it wasn't an exclusive
7981 * reservation generate UA(Reservations
7982 * Preempted) for all other registered nexuses
7983 * if the type has changed. Establish the new
7984 * reservation and holder. If res_key and
7985 * sa_res_key are the same do the above
7986 * except don't unregister the res holder.
7989 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7990 if (i == residx || ctl_get_prkey(lun, i) == 0)
7993 if (sa_res_key == ctl_get_prkey(lun, i)) {
7994 ctl_clr_prkey(lun, i);
7995 lun->pr_key_count--;
7996 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7997 } else if (type != lun->pr_res_type &&
7998 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7999 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8000 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8003 lun->pr_res_type = type;
8004 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8005 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8006 lun->pr_res_idx = residx;
8008 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8009 lun->pr_generation++;
8010 mtx_unlock(&lun->lun_lock);
8012 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8013 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8014 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8015 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8016 persis_io.pr.pr_info.res_type = type;
8017 memcpy(persis_io.pr.pr_info.sa_res_key,
8018 param->serv_act_res_key,
8019 sizeof(param->serv_act_res_key));
8020 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8021 sizeof(persis_io.pr), M_WAITOK);
8024 * sa_res_key is not the res holder just
8025 * remove registrants
8029 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8030 if (sa_res_key != ctl_get_prkey(lun, i))
8034 ctl_clr_prkey(lun, i);
8035 lun->pr_key_count--;
8036 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8040 mtx_unlock(&lun->lun_lock);
8041 free(ctsio->kern_data_ptr, M_CTL);
8042 ctl_set_reservation_conflict(ctsio);
8043 ctl_done((union ctl_io *)ctsio);
8046 lun->pr_generation++;
8047 mtx_unlock(&lun->lun_lock);
8049 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8050 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8051 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8052 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8053 persis_io.pr.pr_info.res_type = type;
8054 memcpy(persis_io.pr.pr_info.sa_res_key,
8055 param->serv_act_res_key,
8056 sizeof(param->serv_act_res_key));
8057 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8058 sizeof(persis_io.pr), M_WAITOK);
8065 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8067 uint64_t sa_res_key;
8070 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8072 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8073 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8074 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8075 if (sa_res_key == 0) {
8077 * Unregister everybody else and build UA for
8080 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8081 if (i == msg->pr.pr_info.residx ||
8082 ctl_get_prkey(lun, i) == 0)
8085 ctl_clr_prkey(lun, i);
8086 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8089 lun->pr_key_count = 1;
8090 lun->pr_res_type = msg->pr.pr_info.res_type;
8091 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8092 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8093 lun->pr_res_idx = msg->pr.pr_info.residx;
8095 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8096 if (sa_res_key == ctl_get_prkey(lun, i))
8099 ctl_clr_prkey(lun, i);
8100 lun->pr_key_count--;
8101 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8105 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8106 if (i == msg->pr.pr_info.residx ||
8107 ctl_get_prkey(lun, i) == 0)
8110 if (sa_res_key == ctl_get_prkey(lun, i)) {
8111 ctl_clr_prkey(lun, i);
8112 lun->pr_key_count--;
8113 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8114 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8115 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8116 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8117 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8120 lun->pr_res_type = msg->pr.pr_info.res_type;
8121 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8122 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8123 lun->pr_res_idx = msg->pr.pr_info.residx;
8125 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8127 lun->pr_generation++;
8133 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8136 u_int32_t param_len;
8137 struct scsi_per_res_out *cdb;
8138 struct ctl_lun *lun;
8139 struct scsi_per_res_out_parms* param;
8140 struct ctl_softc *softc;
8142 uint64_t res_key, sa_res_key, key;
8144 union ctl_ha_msg persis_io;
8147 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8149 retval = CTL_RETVAL_COMPLETE;
8151 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8152 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8153 softc = lun->ctl_softc;
8156 * We only support whole-LUN scope. The scope & type are ignored for
8157 * register, register and ignore existing key and clear.
8158 * We sometimes ignore scope and type on preempts too!!
8159 * Verify reservation type here as well.
8161 type = cdb->scope_type & SPR_TYPE_MASK;
8162 if ((cdb->action == SPRO_RESERVE)
8163 || (cdb->action == SPRO_RELEASE)) {
8164 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8165 ctl_set_invalid_field(/*ctsio*/ ctsio,
8171 ctl_done((union ctl_io *)ctsio);
8172 return (CTL_RETVAL_COMPLETE);
8175 if (type>8 || type==2 || type==4 || type==0) {
8176 ctl_set_invalid_field(/*ctsio*/ ctsio,
8182 ctl_done((union ctl_io *)ctsio);
8183 return (CTL_RETVAL_COMPLETE);
8187 param_len = scsi_4btoul(cdb->length);
8189 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8190 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8191 ctsio->kern_data_len = param_len;
8192 ctsio->kern_total_len = param_len;
8193 ctsio->kern_data_resid = 0;
8194 ctsio->kern_rel_offset = 0;
8195 ctsio->kern_sg_entries = 0;
8196 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8197 ctsio->be_move_done = ctl_config_move_done;
8198 ctl_datamove((union ctl_io *)ctsio);
8200 return (CTL_RETVAL_COMPLETE);
8203 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8205 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8206 res_key = scsi_8btou64(param->res_key.key);
8207 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8210 * Validate the reservation key here except for SPRO_REG_IGNO
8211 * This must be done for all other service actions
8213 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8214 mtx_lock(&lun->lun_lock);
8215 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8216 if (res_key != key) {
8218 * The current key passed in doesn't match
8219 * the one the initiator previously
8222 mtx_unlock(&lun->lun_lock);
8223 free(ctsio->kern_data_ptr, M_CTL);
8224 ctl_set_reservation_conflict(ctsio);
8225 ctl_done((union ctl_io *)ctsio);
8226 return (CTL_RETVAL_COMPLETE);
8228 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8230 * We are not registered
8232 mtx_unlock(&lun->lun_lock);
8233 free(ctsio->kern_data_ptr, M_CTL);
8234 ctl_set_reservation_conflict(ctsio);
8235 ctl_done((union ctl_io *)ctsio);
8236 return (CTL_RETVAL_COMPLETE);
8237 } else if (res_key != 0) {
8239 * We are not registered and trying to register but
8240 * the register key isn't zero.
8242 mtx_unlock(&lun->lun_lock);
8243 free(ctsio->kern_data_ptr, M_CTL);
8244 ctl_set_reservation_conflict(ctsio);
8245 ctl_done((union ctl_io *)ctsio);
8246 return (CTL_RETVAL_COMPLETE);
8248 mtx_unlock(&lun->lun_lock);
8251 switch (cdb->action & SPRO_ACTION_MASK) {
8253 case SPRO_REG_IGNO: {
8256 printf("Registration received\n");
8260 * We don't support any of these options, as we report in
8261 * the read capabilities request (see
8262 * ctl_persistent_reserve_in(), above).
8264 if ((param->flags & SPR_SPEC_I_PT)
8265 || (param->flags & SPR_ALL_TG_PT)
8266 || (param->flags & SPR_APTPL)) {
8269 if (param->flags & SPR_APTPL)
8271 else if (param->flags & SPR_ALL_TG_PT)
8273 else /* SPR_SPEC_I_PT */
8276 free(ctsio->kern_data_ptr, M_CTL);
8277 ctl_set_invalid_field(ctsio,
8283 ctl_done((union ctl_io *)ctsio);
8284 return (CTL_RETVAL_COMPLETE);
8287 mtx_lock(&lun->lun_lock);
8290 * The initiator wants to clear the
8293 if (sa_res_key == 0) {
8295 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8296 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8297 && ctl_get_prkey(lun, residx) == 0)) {
8298 mtx_unlock(&lun->lun_lock);
8302 ctl_clr_prkey(lun, residx);
8303 lun->pr_key_count--;
8305 if (residx == lun->pr_res_idx) {
8306 lun->flags &= ~CTL_LUN_PR_RESERVED;
8307 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8309 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8310 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8311 lun->pr_key_count) {
8313 * If the reservation is a registrants
8314 * only type we need to generate a UA
8315 * for other registered inits. The
8316 * sense code should be RESERVATIONS
8320 for (i = softc->init_min; i < softc->init_max; i++){
8321 if (ctl_get_prkey(lun, i) == 0)
8324 CTL_UA_RES_RELEASE);
8327 lun->pr_res_type = 0;
8328 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8329 if (lun->pr_key_count==0) {
8330 lun->flags &= ~CTL_LUN_PR_RESERVED;
8331 lun->pr_res_type = 0;
8332 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8335 lun->pr_generation++;
8336 mtx_unlock(&lun->lun_lock);
8338 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8339 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8340 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8341 persis_io.pr.pr_info.residx = residx;
8342 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8343 sizeof(persis_io.pr), M_WAITOK);
8344 } else /* sa_res_key != 0 */ {
8347 * If we aren't registered currently then increment
8348 * the key count and set the registered flag.
8350 ctl_alloc_prkey(lun, residx);
8351 if (ctl_get_prkey(lun, residx) == 0)
8352 lun->pr_key_count++;
8353 ctl_set_prkey(lun, residx, sa_res_key);
8354 lun->pr_generation++;
8355 mtx_unlock(&lun->lun_lock);
8357 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8358 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8359 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8360 persis_io.pr.pr_info.residx = residx;
8361 memcpy(persis_io.pr.pr_info.sa_res_key,
8362 param->serv_act_res_key,
8363 sizeof(param->serv_act_res_key));
8364 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8365 sizeof(persis_io.pr), M_WAITOK);
8372 printf("Reserve executed type %d\n", type);
8374 mtx_lock(&lun->lun_lock);
8375 if (lun->flags & CTL_LUN_PR_RESERVED) {
8377 * if this isn't the reservation holder and it's
8378 * not a "all registrants" type or if the type is
8379 * different then we have a conflict
8381 if ((lun->pr_res_idx != residx
8382 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8383 || lun->pr_res_type != type) {
8384 mtx_unlock(&lun->lun_lock);
8385 free(ctsio->kern_data_ptr, M_CTL);
8386 ctl_set_reservation_conflict(ctsio);
8387 ctl_done((union ctl_io *)ctsio);
8388 return (CTL_RETVAL_COMPLETE);
8390 mtx_unlock(&lun->lun_lock);
8391 } else /* create a reservation */ {
8393 * If it's not an "all registrants" type record
8394 * reservation holder
8396 if (type != SPR_TYPE_WR_EX_AR
8397 && type != SPR_TYPE_EX_AC_AR)
8398 lun->pr_res_idx = residx; /* Res holder */
8400 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8402 lun->flags |= CTL_LUN_PR_RESERVED;
8403 lun->pr_res_type = type;
8405 mtx_unlock(&lun->lun_lock);
8407 /* send msg to other side */
8408 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8409 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8410 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8411 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8412 persis_io.pr.pr_info.res_type = type;
8413 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8414 sizeof(persis_io.pr), M_WAITOK);
8419 mtx_lock(&lun->lun_lock);
8420 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8421 /* No reservation exists return good status */
8422 mtx_unlock(&lun->lun_lock);
8426 * Is this nexus a reservation holder?
8428 if (lun->pr_res_idx != residx
8429 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8431 * not a res holder return good status but
8434 mtx_unlock(&lun->lun_lock);
8438 if (lun->pr_res_type != type) {
8439 mtx_unlock(&lun->lun_lock);
8440 free(ctsio->kern_data_ptr, M_CTL);
8441 ctl_set_illegal_pr_release(ctsio);
8442 ctl_done((union ctl_io *)ctsio);
8443 return (CTL_RETVAL_COMPLETE);
8446 /* okay to release */
8447 lun->flags &= ~CTL_LUN_PR_RESERVED;
8448 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8449 lun->pr_res_type = 0;
8452 * If this isn't an exclusive access reservation and NUAR
8453 * is not set, generate UA for all other registrants.
8455 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8456 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8457 for (i = softc->init_min; i < softc->init_max; i++) {
8458 if (i == residx || ctl_get_prkey(lun, i) == 0)
8460 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8463 mtx_unlock(&lun->lun_lock);
8465 /* Send msg to other side */
8466 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8467 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8468 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8469 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8470 sizeof(persis_io.pr), M_WAITOK);
8474 /* send msg to other side */
8476 mtx_lock(&lun->lun_lock);
8477 lun->flags &= ~CTL_LUN_PR_RESERVED;
8478 lun->pr_res_type = 0;
8479 lun->pr_key_count = 0;
8480 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8482 ctl_clr_prkey(lun, residx);
8483 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8484 if (ctl_get_prkey(lun, i) != 0) {
8485 ctl_clr_prkey(lun, i);
8486 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8488 lun->pr_generation++;
8489 mtx_unlock(&lun->lun_lock);
8491 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8492 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8493 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8494 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8495 sizeof(persis_io.pr), M_WAITOK);
8499 case SPRO_PRE_ABO: {
8502 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8503 residx, ctsio, cdb, param);
8505 return (CTL_RETVAL_COMPLETE);
8509 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8513 free(ctsio->kern_data_ptr, M_CTL);
8514 ctl_set_success(ctsio);
8515 ctl_done((union ctl_io *)ctsio);
8521 * This routine is for handling a message from the other SC pertaining to
8522 * persistent reserve out. All the error checking will have been done
8523 * so only perorming the action need be done here to keep the two
8527 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8529 struct ctl_softc *softc = control_softc;
8530 struct ctl_lun *lun;
8532 uint32_t residx, targ_lun;
8534 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8535 mtx_lock(&softc->ctl_lock);
8536 if (targ_lun >= CTL_MAX_LUNS ||
8537 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8538 mtx_unlock(&softc->ctl_lock);
8541 mtx_lock(&lun->lun_lock);
8542 mtx_unlock(&softc->ctl_lock);
8543 if (lun->flags & CTL_LUN_DISABLED) {
8544 mtx_unlock(&lun->lun_lock);
8547 residx = ctl_get_initindex(&msg->hdr.nexus);
8548 switch(msg->pr.pr_info.action) {
8549 case CTL_PR_REG_KEY:
8550 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8551 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8552 lun->pr_key_count++;
8553 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8554 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8555 lun->pr_generation++;
8558 case CTL_PR_UNREG_KEY:
8559 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8560 lun->pr_key_count--;
8562 /* XXX Need to see if the reservation has been released */
8563 /* if so do we need to generate UA? */
8564 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8565 lun->flags &= ~CTL_LUN_PR_RESERVED;
8566 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8568 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8569 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8570 lun->pr_key_count) {
8572 * If the reservation is a registrants
8573 * only type we need to generate a UA
8574 * for other registered inits. The
8575 * sense code should be RESERVATIONS
8579 for (i = softc->init_min; i < softc->init_max; i++) {
8580 if (ctl_get_prkey(lun, i) == 0)
8583 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8586 lun->pr_res_type = 0;
8587 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8588 if (lun->pr_key_count==0) {
8589 lun->flags &= ~CTL_LUN_PR_RESERVED;
8590 lun->pr_res_type = 0;
8591 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8594 lun->pr_generation++;
8597 case CTL_PR_RESERVE:
8598 lun->flags |= CTL_LUN_PR_RESERVED;
8599 lun->pr_res_type = msg->pr.pr_info.res_type;
8600 lun->pr_res_idx = msg->pr.pr_info.residx;
8604 case CTL_PR_RELEASE:
8606 * If this isn't an exclusive access reservation and NUAR
8607 * is not set, generate UA for all other registrants.
8609 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8610 lun->pr_res_type != SPR_TYPE_WR_EX &&
8611 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8612 for (i = softc->init_min; i < softc->init_max; i++)
8613 if (i == residx || ctl_get_prkey(lun, i) == 0)
8615 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8618 lun->flags &= ~CTL_LUN_PR_RESERVED;
8619 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8620 lun->pr_res_type = 0;
8623 case CTL_PR_PREEMPT:
8624 ctl_pro_preempt_other(lun, msg);
8627 lun->flags &= ~CTL_LUN_PR_RESERVED;
8628 lun->pr_res_type = 0;
8629 lun->pr_key_count = 0;
8630 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8632 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8633 if (ctl_get_prkey(lun, i) == 0)
8635 ctl_clr_prkey(lun, i);
8636 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8638 lun->pr_generation++;
8642 mtx_unlock(&lun->lun_lock);
8646 ctl_read_write(struct ctl_scsiio *ctsio)
8648 struct ctl_lun *lun;
8649 struct ctl_lba_len_flags *lbalen;
8651 uint32_t num_blocks;
8655 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8657 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8660 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8661 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8662 switch (ctsio->cdb[0]) {
8665 struct scsi_rw_6 *cdb;
8667 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8669 lba = scsi_3btoul(cdb->addr);
8670 /* only 5 bits are valid in the most significant address byte */
8672 num_blocks = cdb->length;
8674 * This is correct according to SBC-2.
8676 if (num_blocks == 0)
8682 struct scsi_rw_10 *cdb;
8684 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8685 if (cdb->byte2 & SRW10_FUA)
8686 flags |= CTL_LLF_FUA;
8687 if (cdb->byte2 & SRW10_DPO)
8688 flags |= CTL_LLF_DPO;
8689 lba = scsi_4btoul(cdb->addr);
8690 num_blocks = scsi_2btoul(cdb->length);
8693 case WRITE_VERIFY_10: {
8694 struct scsi_write_verify_10 *cdb;
8696 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8697 flags |= CTL_LLF_FUA;
8698 if (cdb->byte2 & SWV_DPO)
8699 flags |= CTL_LLF_DPO;
8700 lba = scsi_4btoul(cdb->addr);
8701 num_blocks = scsi_2btoul(cdb->length);
8706 struct scsi_rw_12 *cdb;
8708 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8709 if (cdb->byte2 & SRW12_FUA)
8710 flags |= CTL_LLF_FUA;
8711 if (cdb->byte2 & SRW12_DPO)
8712 flags |= CTL_LLF_DPO;
8713 lba = scsi_4btoul(cdb->addr);
8714 num_blocks = scsi_4btoul(cdb->length);
8717 case WRITE_VERIFY_12: {
8718 struct scsi_write_verify_12 *cdb;
8720 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8721 flags |= CTL_LLF_FUA;
8722 if (cdb->byte2 & SWV_DPO)
8723 flags |= CTL_LLF_DPO;
8724 lba = scsi_4btoul(cdb->addr);
8725 num_blocks = scsi_4btoul(cdb->length);
8730 struct scsi_rw_16 *cdb;
8732 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8733 if (cdb->byte2 & SRW12_FUA)
8734 flags |= CTL_LLF_FUA;
8735 if (cdb->byte2 & SRW12_DPO)
8736 flags |= CTL_LLF_DPO;
8737 lba = scsi_8btou64(cdb->addr);
8738 num_blocks = scsi_4btoul(cdb->length);
8741 case WRITE_ATOMIC_16: {
8742 struct scsi_write_atomic_16 *cdb;
8744 if (lun->be_lun->atomicblock == 0) {
8745 ctl_set_invalid_opcode(ctsio);
8746 ctl_done((union ctl_io *)ctsio);
8747 return (CTL_RETVAL_COMPLETE);
8750 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8751 if (cdb->byte2 & SRW12_FUA)
8752 flags |= CTL_LLF_FUA;
8753 if (cdb->byte2 & SRW12_DPO)
8754 flags |= CTL_LLF_DPO;
8755 lba = scsi_8btou64(cdb->addr);
8756 num_blocks = scsi_2btoul(cdb->length);
8757 if (num_blocks > lun->be_lun->atomicblock) {
8758 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8759 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8761 ctl_done((union ctl_io *)ctsio);
8762 return (CTL_RETVAL_COMPLETE);
8766 case WRITE_VERIFY_16: {
8767 struct scsi_write_verify_16 *cdb;
8769 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8770 flags |= CTL_LLF_FUA;
8771 if (cdb->byte2 & SWV_DPO)
8772 flags |= CTL_LLF_DPO;
8773 lba = scsi_8btou64(cdb->addr);
8774 num_blocks = scsi_4btoul(cdb->length);
8779 * We got a command we don't support. This shouldn't
8780 * happen, commands should be filtered out above us.
8782 ctl_set_invalid_opcode(ctsio);
8783 ctl_done((union ctl_io *)ctsio);
8785 return (CTL_RETVAL_COMPLETE);
8786 break; /* NOTREACHED */
8790 * The first check is to make sure we're in bounds, the second
8791 * check is to catch wrap-around problems. If the lba + num blocks
8792 * is less than the lba, then we've wrapped around and the block
8793 * range is invalid anyway.
8795 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8796 || ((lba + num_blocks) < lba)) {
8797 ctl_set_lba_out_of_range(ctsio,
8798 MAX(lba, lun->be_lun->maxlba + 1));
8799 ctl_done((union ctl_io *)ctsio);
8800 return (CTL_RETVAL_COMPLETE);
8804 * According to SBC-3, a transfer length of 0 is not an error.
8805 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8806 * translates to 256 blocks for those commands.
8808 if (num_blocks == 0) {
8809 ctl_set_success(ctsio);
8810 ctl_done((union ctl_io *)ctsio);
8811 return (CTL_RETVAL_COMPLETE);
8814 /* Set FUA and/or DPO if caches are disabled. */
8816 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8817 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8819 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8820 flags |= CTL_LLF_FUA;
8823 lbalen = (struct ctl_lba_len_flags *)
8824 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8826 lbalen->len = num_blocks;
8827 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8829 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8830 ctsio->kern_rel_offset = 0;
8832 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8834 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8839 ctl_cnw_cont(union ctl_io *io)
8841 struct ctl_scsiio *ctsio;
8842 struct ctl_lun *lun;
8843 struct ctl_lba_len_flags *lbalen;
8846 ctsio = &io->scsiio;
8847 ctsio->io_hdr.status = CTL_STATUS_NONE;
8848 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8849 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8850 lbalen = (struct ctl_lba_len_flags *)
8851 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8852 lbalen->flags &= ~CTL_LLF_COMPARE;
8853 lbalen->flags |= CTL_LLF_WRITE;
8855 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8856 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8861 ctl_cnw(struct ctl_scsiio *ctsio)
8863 struct ctl_lun *lun;
8864 struct ctl_lba_len_flags *lbalen;
8866 uint32_t num_blocks;
8869 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8871 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8874 switch (ctsio->cdb[0]) {
8875 case COMPARE_AND_WRITE: {
8876 struct scsi_compare_and_write *cdb;
8878 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8879 if (cdb->byte2 & SRW10_FUA)
8880 flags |= CTL_LLF_FUA;
8881 if (cdb->byte2 & SRW10_DPO)
8882 flags |= CTL_LLF_DPO;
8883 lba = scsi_8btou64(cdb->addr);
8884 num_blocks = cdb->length;
8889 * We got a command we don't support. This shouldn't
8890 * happen, commands should be filtered out above us.
8892 ctl_set_invalid_opcode(ctsio);
8893 ctl_done((union ctl_io *)ctsio);
8895 return (CTL_RETVAL_COMPLETE);
8896 break; /* NOTREACHED */
8900 * The first check is to make sure we're in bounds, the second
8901 * check is to catch wrap-around problems. If the lba + num blocks
8902 * is less than the lba, then we've wrapped around and the block
8903 * range is invalid anyway.
8905 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8906 || ((lba + num_blocks) < lba)) {
8907 ctl_set_lba_out_of_range(ctsio,
8908 MAX(lba, lun->be_lun->maxlba + 1));
8909 ctl_done((union ctl_io *)ctsio);
8910 return (CTL_RETVAL_COMPLETE);
8914 * According to SBC-3, a transfer length of 0 is not an error.
8916 if (num_blocks == 0) {
8917 ctl_set_success(ctsio);
8918 ctl_done((union ctl_io *)ctsio);
8919 return (CTL_RETVAL_COMPLETE);
8922 /* Set FUA if write cache is disabled. */
8923 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8924 flags |= CTL_LLF_FUA;
8926 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8927 ctsio->kern_rel_offset = 0;
8930 * Set the IO_CONT flag, so that if this I/O gets passed to
8931 * ctl_data_submit_done(), it'll get passed back to
8932 * ctl_ctl_cnw_cont() for further processing.
8934 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8935 ctsio->io_cont = ctl_cnw_cont;
8937 lbalen = (struct ctl_lba_len_flags *)
8938 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8940 lbalen->len = num_blocks;
8941 lbalen->flags = CTL_LLF_COMPARE | flags;
8943 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8944 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8949 ctl_verify(struct ctl_scsiio *ctsio)
8951 struct ctl_lun *lun;
8952 struct ctl_lba_len_flags *lbalen;
8954 uint32_t num_blocks;
8958 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8960 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8963 flags = CTL_LLF_FUA;
8964 switch (ctsio->cdb[0]) {
8966 struct scsi_verify_10 *cdb;
8968 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8969 if (cdb->byte2 & SVFY_BYTCHK)
8971 if (cdb->byte2 & SVFY_DPO)
8972 flags |= CTL_LLF_DPO;
8973 lba = scsi_4btoul(cdb->addr);
8974 num_blocks = scsi_2btoul(cdb->length);
8978 struct scsi_verify_12 *cdb;
8980 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8981 if (cdb->byte2 & SVFY_BYTCHK)
8983 if (cdb->byte2 & SVFY_DPO)
8984 flags |= CTL_LLF_DPO;
8985 lba = scsi_4btoul(cdb->addr);
8986 num_blocks = scsi_4btoul(cdb->length);
8990 struct scsi_rw_16 *cdb;
8992 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8993 if (cdb->byte2 & SVFY_BYTCHK)
8995 if (cdb->byte2 & SVFY_DPO)
8996 flags |= CTL_LLF_DPO;
8997 lba = scsi_8btou64(cdb->addr);
8998 num_blocks = scsi_4btoul(cdb->length);
9003 * We got a command we don't support. This shouldn't
9004 * happen, commands should be filtered out above us.
9006 ctl_set_invalid_opcode(ctsio);
9007 ctl_done((union ctl_io *)ctsio);
9008 return (CTL_RETVAL_COMPLETE);
9012 * The first check is to make sure we're in bounds, the second
9013 * check is to catch wrap-around problems. If the lba + num blocks
9014 * is less than the lba, then we've wrapped around and the block
9015 * range is invalid anyway.
9017 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9018 || ((lba + num_blocks) < lba)) {
9019 ctl_set_lba_out_of_range(ctsio,
9020 MAX(lba, lun->be_lun->maxlba + 1));
9021 ctl_done((union ctl_io *)ctsio);
9022 return (CTL_RETVAL_COMPLETE);
9026 * According to SBC-3, a transfer length of 0 is not an error.
9028 if (num_blocks == 0) {
9029 ctl_set_success(ctsio);
9030 ctl_done((union ctl_io *)ctsio);
9031 return (CTL_RETVAL_COMPLETE);
9034 lbalen = (struct ctl_lba_len_flags *)
9035 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9037 lbalen->len = num_blocks;
9039 lbalen->flags = CTL_LLF_COMPARE | flags;
9040 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9042 lbalen->flags = CTL_LLF_VERIFY | flags;
9043 ctsio->kern_total_len = 0;
9045 ctsio->kern_rel_offset = 0;
9047 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9048 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9053 ctl_report_luns(struct ctl_scsiio *ctsio)
9055 struct ctl_softc *softc;
9056 struct scsi_report_luns *cdb;
9057 struct scsi_report_luns_data *lun_data;
9058 struct ctl_lun *lun, *request_lun;
9059 struct ctl_port *port;
9060 int num_filled, num_luns, num_port_luns, retval;
9061 uint32_t alloc_len, lun_datalen;
9062 uint32_t initidx, targ_lun_id, lun_id;
9064 retval = CTL_RETVAL_COMPLETE;
9065 cdb = (struct scsi_report_luns *)ctsio->cdb;
9066 port = ctl_io_port(&ctsio->io_hdr);
9067 softc = port->ctl_softc;
9069 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9072 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS;
9073 mtx_lock(&softc->ctl_lock);
9074 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9075 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9078 mtx_unlock(&softc->ctl_lock);
9080 switch (cdb->select_report) {
9081 case RPL_REPORT_DEFAULT:
9082 case RPL_REPORT_ALL:
9083 case RPL_REPORT_NONSUBSID:
9085 case RPL_REPORT_WELLKNOWN:
9086 case RPL_REPORT_ADMIN:
9087 case RPL_REPORT_CONGLOM:
9091 ctl_set_invalid_field(ctsio,
9097 ctl_done((union ctl_io *)ctsio);
9099 break; /* NOTREACHED */
9102 alloc_len = scsi_4btoul(cdb->length);
9104 * The initiator has to allocate at least 16 bytes for this request,
9105 * so he can at least get the header and the first LUN. Otherwise
9106 * we reject the request (per SPC-3 rev 14, section 6.21).
9108 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9109 sizeof(struct scsi_report_luns_lundata))) {
9110 ctl_set_invalid_field(ctsio,
9116 ctl_done((union ctl_io *)ctsio);
9120 request_lun = (struct ctl_lun *)
9121 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9123 lun_datalen = sizeof(*lun_data) +
9124 (num_luns * sizeof(struct scsi_report_luns_lundata));
9126 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9127 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9128 ctsio->kern_sg_entries = 0;
9130 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9132 mtx_lock(&softc->ctl_lock);
9133 for (targ_lun_id = 0, num_filled = 0;
9134 targ_lun_id < num_port_luns && num_filled < num_luns;
9136 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9137 if (lun_id == UINT32_MAX)
9139 lun = softc->ctl_luns[lun_id];
9143 be64enc(lun_data->luns[num_filled++].lundata,
9144 ctl_encode_lun(targ_lun_id));
9147 * According to SPC-3, rev 14 section 6.21:
9149 * "The execution of a REPORT LUNS command to any valid and
9150 * installed logical unit shall clear the REPORTED LUNS DATA
9151 * HAS CHANGED unit attention condition for all logical
9152 * units of that target with respect to the requesting
9153 * initiator. A valid and installed logical unit is one
9154 * having a PERIPHERAL QUALIFIER of 000b in the standard
9155 * INQUIRY data (see 6.4.2)."
9157 * If request_lun is NULL, the LUN this report luns command
9158 * was issued to is either disabled or doesn't exist. In that
9159 * case, we shouldn't clear any pending lun change unit
9162 if (request_lun != NULL) {
9163 mtx_lock(&lun->lun_lock);
9164 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9165 mtx_unlock(&lun->lun_lock);
9168 mtx_unlock(&softc->ctl_lock);
9171 * It's quite possible that we've returned fewer LUNs than we allocated
9172 * space for. Trim it.
9174 lun_datalen = sizeof(*lun_data) +
9175 (num_filled * sizeof(struct scsi_report_luns_lundata));
9177 if (lun_datalen < alloc_len) {
9178 ctsio->residual = alloc_len - lun_datalen;
9179 ctsio->kern_data_len = lun_datalen;
9180 ctsio->kern_total_len = lun_datalen;
9182 ctsio->residual = 0;
9183 ctsio->kern_data_len = alloc_len;
9184 ctsio->kern_total_len = alloc_len;
9186 ctsio->kern_data_resid = 0;
9187 ctsio->kern_rel_offset = 0;
9188 ctsio->kern_sg_entries = 0;
9191 * We set this to the actual data length, regardless of how much
9192 * space we actually have to return results. If the user looks at
9193 * this value, he'll know whether or not he allocated enough space
9194 * and reissue the command if necessary. We don't support well
9195 * known logical units, so if the user asks for that, return none.
9197 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9200 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9203 ctl_set_success(ctsio);
9204 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9205 ctsio->be_move_done = ctl_config_move_done;
9206 ctl_datamove((union ctl_io *)ctsio);
9211 ctl_request_sense(struct ctl_scsiio *ctsio)
9213 struct scsi_request_sense *cdb;
9214 struct scsi_sense_data *sense_ptr;
9215 struct ctl_softc *softc;
9216 struct ctl_lun *lun;
9219 u_int sense_len = SSD_FULL_SIZE;
9220 scsi_sense_data_type sense_format;
9221 ctl_ua_type ua_type;
9222 uint8_t asc = 0, ascq = 0;
9224 cdb = (struct scsi_request_sense *)ctsio->cdb;
9226 softc = control_softc;
9227 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9229 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9232 * Determine which sense format the user wants.
9234 if (cdb->byte2 & SRS_DESC)
9235 sense_format = SSD_TYPE_DESC;
9237 sense_format = SSD_TYPE_FIXED;
9239 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9240 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9241 ctsio->kern_sg_entries = 0;
9244 * struct scsi_sense_data, which is currently set to 256 bytes, is
9245 * larger than the largest allowed value for the length field in the
9246 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9248 ctsio->residual = 0;
9249 ctsio->kern_data_len = cdb->length;
9250 ctsio->kern_total_len = cdb->length;
9252 ctsio->kern_data_resid = 0;
9253 ctsio->kern_rel_offset = 0;
9254 ctsio->kern_sg_entries = 0;
9257 * If we don't have a LUN, we don't have any pending sense.
9260 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9261 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9262 /* "Logical unit not supported" */
9263 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9264 /*current_error*/ 1,
9265 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9273 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9275 * Check for pending sense, and then for pending unit attentions.
9276 * Pending sense gets returned first, then pending unit attentions.
9278 mtx_lock(&lun->lun_lock);
9280 if (ctl_is_set(lun->have_ca, initidx)) {
9281 scsi_sense_data_type stored_format;
9284 * Check to see which sense format was used for the stored
9287 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9290 * If the user requested a different sense format than the
9291 * one we stored, then we need to convert it to the other
9292 * format. If we're going from descriptor to fixed format
9293 * sense data, we may lose things in translation, depending
9294 * on what options were used.
9296 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9297 * for some reason we'll just copy it out as-is.
9299 if ((stored_format == SSD_TYPE_FIXED)
9300 && (sense_format == SSD_TYPE_DESC))
9301 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9302 &lun->pending_sense[initidx],
9303 (struct scsi_sense_data_desc *)sense_ptr);
9304 else if ((stored_format == SSD_TYPE_DESC)
9305 && (sense_format == SSD_TYPE_FIXED))
9306 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9307 &lun->pending_sense[initidx],
9308 (struct scsi_sense_data_fixed *)sense_ptr);
9310 memcpy(sense_ptr, &lun->pending_sense[initidx],
9311 MIN(sizeof(*sense_ptr),
9312 sizeof(lun->pending_sense[initidx])));
9314 ctl_clear_mask(lun->have_ca, initidx);
9318 if (have_error == 0) {
9319 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9321 if (ua_type != CTL_UA_NONE)
9324 if (have_error == 0) {
9326 * Report informational exception if have one and allowed.
9328 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9330 ascq = lun->ie_ascq;
9332 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9333 /*current_error*/ 1,
9334 /*sense_key*/ SSD_KEY_NO_SENSE,
9339 mtx_unlock(&lun->lun_lock);
9343 * We report the SCSI status as OK, since the status of the command
9344 * itself is OK. We're reporting sense as parameter data.
9346 ctl_set_success(ctsio);
9347 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9348 ctsio->be_move_done = ctl_config_move_done;
9349 ctl_datamove((union ctl_io *)ctsio);
9350 return (CTL_RETVAL_COMPLETE);
9354 ctl_tur(struct ctl_scsiio *ctsio)
9357 CTL_DEBUG_PRINT(("ctl_tur\n"));
9359 ctl_set_success(ctsio);
9360 ctl_done((union ctl_io *)ctsio);
9362 return (CTL_RETVAL_COMPLETE);
9366 * SCSI VPD page 0x00, the Supported VPD Pages page.
9369 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9371 struct scsi_vpd_supported_pages *pages;
9373 struct ctl_lun *lun;
9376 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9378 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9379 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9380 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9381 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9382 ctsio->kern_sg_entries = 0;
9384 if (sup_page_size < alloc_len) {
9385 ctsio->residual = alloc_len - sup_page_size;
9386 ctsio->kern_data_len = sup_page_size;
9387 ctsio->kern_total_len = sup_page_size;
9389 ctsio->residual = 0;
9390 ctsio->kern_data_len = alloc_len;
9391 ctsio->kern_total_len = alloc_len;
9393 ctsio->kern_data_resid = 0;
9394 ctsio->kern_rel_offset = 0;
9395 ctsio->kern_sg_entries = 0;
9398 * The control device is always connected. The disk device, on the
9399 * other hand, may not be online all the time. Need to change this
9400 * to figure out whether the disk device is actually online or not.
9403 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9404 lun->be_lun->lun_type;
9406 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9409 /* Supported VPD pages */
9410 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9412 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9413 /* Device Identification */
9414 pages->page_list[p++] = SVPD_DEVICE_ID;
9415 /* Extended INQUIRY Data */
9416 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9417 /* Mode Page Policy */
9418 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9420 pages->page_list[p++] = SVPD_SCSI_PORTS;
9421 /* Third-party Copy */
9422 pages->page_list[p++] = SVPD_SCSI_TPC;
9423 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9425 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9426 /* Block Device Characteristics */
9427 pages->page_list[p++] = SVPD_BDC;
9428 /* Logical Block Provisioning */
9429 pages->page_list[p++] = SVPD_LBP;
9433 ctl_set_success(ctsio);
9434 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9435 ctsio->be_move_done = ctl_config_move_done;
9436 ctl_datamove((union ctl_io *)ctsio);
9437 return (CTL_RETVAL_COMPLETE);
9441 * SCSI VPD page 0x80, the Unit Serial Number page.
9444 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9446 struct scsi_vpd_unit_serial_number *sn_ptr;
9447 struct ctl_lun *lun;
9450 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9452 data_len = 4 + CTL_SN_LEN;
9453 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9454 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9455 if (data_len < alloc_len) {
9456 ctsio->residual = alloc_len - data_len;
9457 ctsio->kern_data_len = data_len;
9458 ctsio->kern_total_len = data_len;
9460 ctsio->residual = 0;
9461 ctsio->kern_data_len = alloc_len;
9462 ctsio->kern_total_len = alloc_len;
9464 ctsio->kern_data_resid = 0;
9465 ctsio->kern_rel_offset = 0;
9466 ctsio->kern_sg_entries = 0;
9469 * The control device is always connected. The disk device, on the
9470 * other hand, may not be online all the time. Need to change this
9471 * to figure out whether the disk device is actually online or not.
9474 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9475 lun->be_lun->lun_type;
9477 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9479 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9480 sn_ptr->length = CTL_SN_LEN;
9482 * If we don't have a LUN, we just leave the serial number as
9486 strncpy((char *)sn_ptr->serial_num,
9487 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9489 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9491 ctl_set_success(ctsio);
9492 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9493 ctsio->be_move_done = ctl_config_move_done;
9494 ctl_datamove((union ctl_io *)ctsio);
9495 return (CTL_RETVAL_COMPLETE);
9500 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9503 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9505 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9506 struct ctl_lun *lun;
9509 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9511 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9512 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9513 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9514 ctsio->kern_sg_entries = 0;
9516 if (data_len < alloc_len) {
9517 ctsio->residual = alloc_len - data_len;
9518 ctsio->kern_data_len = data_len;
9519 ctsio->kern_total_len = data_len;
9521 ctsio->residual = 0;
9522 ctsio->kern_data_len = alloc_len;
9523 ctsio->kern_total_len = alloc_len;
9525 ctsio->kern_data_resid = 0;
9526 ctsio->kern_rel_offset = 0;
9527 ctsio->kern_sg_entries = 0;
9530 * The control device is always connected. The disk device, on the
9531 * other hand, may not be online all the time.
9534 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9535 lun->be_lun->lun_type;
9537 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9538 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9539 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9541 * We support head of queue, ordered and simple tags.
9543 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9545 * Volatile cache supported.
9547 eid_ptr->flags3 = SVPD_EID_V_SUP;
9550 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9551 * attention for a particular IT nexus on all LUNs once we report
9552 * it to that nexus once. This bit is required as of SPC-4.
9554 eid_ptr->flags4 = SVPD_EID_LUICLR;
9557 * XXX KDM in order to correctly answer this, we would need
9558 * information from the SIM to determine how much sense data it
9559 * can send. So this would really be a path inquiry field, most
9560 * likely. This can be set to a maximum of 252 according to SPC-4,
9561 * but the hardware may or may not be able to support that much.
9562 * 0 just means that the maximum sense data length is not reported.
9564 eid_ptr->max_sense_length = 0;
9566 ctl_set_success(ctsio);
9567 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9568 ctsio->be_move_done = ctl_config_move_done;
9569 ctl_datamove((union ctl_io *)ctsio);
9570 return (CTL_RETVAL_COMPLETE);
9574 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9576 struct scsi_vpd_mode_page_policy *mpp_ptr;
9577 struct ctl_lun *lun;
9580 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9582 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9583 sizeof(struct scsi_vpd_mode_page_policy_descr);
9585 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9586 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9587 ctsio->kern_sg_entries = 0;
9589 if (data_len < alloc_len) {
9590 ctsio->residual = alloc_len - data_len;
9591 ctsio->kern_data_len = data_len;
9592 ctsio->kern_total_len = data_len;
9594 ctsio->residual = 0;
9595 ctsio->kern_data_len = alloc_len;
9596 ctsio->kern_total_len = alloc_len;
9598 ctsio->kern_data_resid = 0;
9599 ctsio->kern_rel_offset = 0;
9600 ctsio->kern_sg_entries = 0;
9603 * The control device is always connected. The disk device, on the
9604 * other hand, may not be online all the time.
9607 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9608 lun->be_lun->lun_type;
9610 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9611 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9612 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9613 mpp_ptr->descr[0].page_code = 0x3f;
9614 mpp_ptr->descr[0].subpage_code = 0xff;
9615 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9617 ctl_set_success(ctsio);
9618 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9619 ctsio->be_move_done = ctl_config_move_done;
9620 ctl_datamove((union ctl_io *)ctsio);
9621 return (CTL_RETVAL_COMPLETE);
9625 * SCSI VPD page 0x83, the Device Identification page.
9628 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9630 struct scsi_vpd_device_id *devid_ptr;
9631 struct scsi_vpd_id_descriptor *desc;
9632 struct ctl_softc *softc;
9633 struct ctl_lun *lun;
9634 struct ctl_port *port;
9638 softc = control_softc;
9640 port = ctl_io_port(&ctsio->io_hdr);
9641 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9643 data_len = sizeof(struct scsi_vpd_device_id) +
9644 sizeof(struct scsi_vpd_id_descriptor) +
9645 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9646 sizeof(struct scsi_vpd_id_descriptor) +
9647 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9648 if (lun && lun->lun_devid)
9649 data_len += lun->lun_devid->len;
9650 if (port && port->port_devid)
9651 data_len += port->port_devid->len;
9652 if (port && port->target_devid)
9653 data_len += port->target_devid->len;
9655 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9656 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9657 ctsio->kern_sg_entries = 0;
9659 if (data_len < alloc_len) {
9660 ctsio->residual = alloc_len - data_len;
9661 ctsio->kern_data_len = data_len;
9662 ctsio->kern_total_len = data_len;
9664 ctsio->residual = 0;
9665 ctsio->kern_data_len = alloc_len;
9666 ctsio->kern_total_len = alloc_len;
9668 ctsio->kern_data_resid = 0;
9669 ctsio->kern_rel_offset = 0;
9670 ctsio->kern_sg_entries = 0;
9673 * The control device is always connected. The disk device, on the
9674 * other hand, may not be online all the time.
9677 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9678 lun->be_lun->lun_type;
9680 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9681 devid_ptr->page_code = SVPD_DEVICE_ID;
9682 scsi_ulto2b(data_len - 4, devid_ptr->length);
9684 if (port && port->port_type == CTL_PORT_FC)
9685 proto = SCSI_PROTO_FC << 4;
9686 else if (port && port->port_type == CTL_PORT_ISCSI)
9687 proto = SCSI_PROTO_ISCSI << 4;
9689 proto = SCSI_PROTO_SPI << 4;
9690 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9693 * We're using a LUN association here. i.e., this device ID is a
9694 * per-LUN identifier.
9696 if (lun && lun->lun_devid) {
9697 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9698 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9699 lun->lun_devid->len);
9703 * This is for the WWPN which is a port association.
9705 if (port && port->port_devid) {
9706 memcpy(desc, port->port_devid->data, port->port_devid->len);
9707 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9708 port->port_devid->len);
9712 * This is for the Relative Target Port(type 4h) identifier
9714 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9715 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9716 SVPD_ID_TYPE_RELTARG;
9718 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9719 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9720 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9723 * This is for the Target Port Group(type 5h) identifier
9725 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9726 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9727 SVPD_ID_TYPE_TPORTGRP;
9729 if (softc->is_single ||
9730 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9733 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9734 scsi_ulto2b(g, &desc->identifier[2]);
9735 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9736 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9739 * This is for the Target identifier
9741 if (port && port->target_devid) {
9742 memcpy(desc, port->target_devid->data, port->target_devid->len);
9745 ctl_set_success(ctsio);
9746 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9747 ctsio->be_move_done = ctl_config_move_done;
9748 ctl_datamove((union ctl_io *)ctsio);
9749 return (CTL_RETVAL_COMPLETE);
9753 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9755 struct ctl_softc *softc = control_softc;
9756 struct scsi_vpd_scsi_ports *sp;
9757 struct scsi_vpd_port_designation *pd;
9758 struct scsi_vpd_port_designation_cont *pdc;
9759 struct ctl_lun *lun;
9760 struct ctl_port *port;
9761 int data_len, num_target_ports, iid_len, id_len;
9763 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9765 num_target_ports = 0;
9768 mtx_lock(&softc->ctl_lock);
9769 STAILQ_FOREACH(port, &softc->port_list, links) {
9770 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9773 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9776 if (port->init_devid)
9777 iid_len += port->init_devid->len;
9778 if (port->port_devid)
9779 id_len += port->port_devid->len;
9781 mtx_unlock(&softc->ctl_lock);
9783 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9784 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9785 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9786 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9787 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9788 ctsio->kern_sg_entries = 0;
9790 if (data_len < alloc_len) {
9791 ctsio->residual = alloc_len - data_len;
9792 ctsio->kern_data_len = data_len;
9793 ctsio->kern_total_len = data_len;
9795 ctsio->residual = 0;
9796 ctsio->kern_data_len = alloc_len;
9797 ctsio->kern_total_len = alloc_len;
9799 ctsio->kern_data_resid = 0;
9800 ctsio->kern_rel_offset = 0;
9801 ctsio->kern_sg_entries = 0;
9804 * The control device is always connected. The disk device, on the
9805 * other hand, may not be online all the time. Need to change this
9806 * to figure out whether the disk device is actually online or not.
9809 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9810 lun->be_lun->lun_type;
9812 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9814 sp->page_code = SVPD_SCSI_PORTS;
9815 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9817 pd = &sp->design[0];
9819 mtx_lock(&softc->ctl_lock);
9820 STAILQ_FOREACH(port, &softc->port_list, links) {
9821 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9824 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9826 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9827 if (port->init_devid) {
9828 iid_len = port->init_devid->len;
9829 memcpy(pd->initiator_transportid,
9830 port->init_devid->data, port->init_devid->len);
9833 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9834 pdc = (struct scsi_vpd_port_designation_cont *)
9835 (&pd->initiator_transportid[iid_len]);
9836 if (port->port_devid) {
9837 id_len = port->port_devid->len;
9838 memcpy(pdc->target_port_descriptors,
9839 port->port_devid->data, port->port_devid->len);
9842 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9843 pd = (struct scsi_vpd_port_designation *)
9844 ((uint8_t *)pdc->target_port_descriptors + id_len);
9846 mtx_unlock(&softc->ctl_lock);
9848 ctl_set_success(ctsio);
9849 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9850 ctsio->be_move_done = ctl_config_move_done;
9851 ctl_datamove((union ctl_io *)ctsio);
9852 return (CTL_RETVAL_COMPLETE);
9856 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9858 struct scsi_vpd_block_limits *bl_ptr;
9859 struct ctl_lun *lun;
9862 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9864 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9865 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9866 ctsio->kern_sg_entries = 0;
9868 if (sizeof(*bl_ptr) < alloc_len) {
9869 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9870 ctsio->kern_data_len = sizeof(*bl_ptr);
9871 ctsio->kern_total_len = sizeof(*bl_ptr);
9873 ctsio->residual = 0;
9874 ctsio->kern_data_len = alloc_len;
9875 ctsio->kern_total_len = alloc_len;
9877 ctsio->kern_data_resid = 0;
9878 ctsio->kern_rel_offset = 0;
9879 ctsio->kern_sg_entries = 0;
9882 * The control device is always connected. The disk device, on the
9883 * other hand, may not be online all the time. Need to change this
9884 * to figure out whether the disk device is actually online or not.
9887 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9888 lun->be_lun->lun_type;
9890 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9892 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9893 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9894 bl_ptr->max_cmp_write_len = 0xff;
9895 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9897 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9898 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9900 ctl_get_opt_number(&lun->be_lun->options,
9901 "unmap_max_lba", &ival);
9902 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9904 ctl_get_opt_number(&lun->be_lun->options,
9905 "unmap_max_descr", &ival);
9906 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9907 if (lun->be_lun->ublockexp != 0) {
9908 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9909 bl_ptr->opt_unmap_grain);
9910 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9911 bl_ptr->unmap_grain_align);
9914 scsi_ulto4b(lun->be_lun->atomicblock,
9915 bl_ptr->max_atomic_transfer_length);
9916 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9917 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9918 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9919 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9921 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9922 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9925 ctl_set_success(ctsio);
9926 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9927 ctsio->be_move_done = ctl_config_move_done;
9928 ctl_datamove((union ctl_io *)ctsio);
9929 return (CTL_RETVAL_COMPLETE);
9933 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9935 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9936 struct ctl_lun *lun;
9940 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9942 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9943 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9944 ctsio->kern_sg_entries = 0;
9946 if (sizeof(*bdc_ptr) < alloc_len) {
9947 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9948 ctsio->kern_data_len = sizeof(*bdc_ptr);
9949 ctsio->kern_total_len = sizeof(*bdc_ptr);
9951 ctsio->residual = 0;
9952 ctsio->kern_data_len = alloc_len;
9953 ctsio->kern_total_len = alloc_len;
9955 ctsio->kern_data_resid = 0;
9956 ctsio->kern_rel_offset = 0;
9957 ctsio->kern_sg_entries = 0;
9960 * The control device is always connected. The disk device, on the
9961 * other hand, may not be online all the time. Need to change this
9962 * to figure out whether the disk device is actually online or not.
9965 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9966 lun->be_lun->lun_type;
9968 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9969 bdc_ptr->page_code = SVPD_BDC;
9970 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9972 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9973 i = strtol(value, NULL, 0);
9975 i = CTL_DEFAULT_ROTATION_RATE;
9976 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9978 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9979 i = strtol(value, NULL, 0);
9982 bdc_ptr->wab_wac_ff = (i & 0x0f);
9983 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9985 ctl_set_success(ctsio);
9986 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9987 ctsio->be_move_done = ctl_config_move_done;
9988 ctl_datamove((union ctl_io *)ctsio);
9989 return (CTL_RETVAL_COMPLETE);
9993 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9995 struct scsi_vpd_logical_block_prov *lbp_ptr;
9996 struct ctl_lun *lun;
9999 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10001 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10002 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10003 ctsio->kern_sg_entries = 0;
10005 if (sizeof(*lbp_ptr) < alloc_len) {
10006 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10007 ctsio->kern_data_len = sizeof(*lbp_ptr);
10008 ctsio->kern_total_len = sizeof(*lbp_ptr);
10010 ctsio->residual = 0;
10011 ctsio->kern_data_len = alloc_len;
10012 ctsio->kern_total_len = alloc_len;
10014 ctsio->kern_data_resid = 0;
10015 ctsio->kern_rel_offset = 0;
10016 ctsio->kern_sg_entries = 0;
10019 * The control device is always connected. The disk device, on the
10020 * other hand, may not be online all the time. Need to change this
10021 * to figure out whether the disk device is actually online or not.
10024 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10025 lun->be_lun->lun_type;
10027 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10029 lbp_ptr->page_code = SVPD_LBP;
10030 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10031 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10032 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10033 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10034 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10035 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
10036 if (value != NULL) {
10037 if (strcmp(value, "resource") == 0)
10038 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10039 else if (strcmp(value, "thin") == 0)
10040 lbp_ptr->prov_type = SVPD_LBP_THIN;
10042 lbp_ptr->prov_type = SVPD_LBP_THIN;
10045 ctl_set_success(ctsio);
10046 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10047 ctsio->be_move_done = ctl_config_move_done;
10048 ctl_datamove((union ctl_io *)ctsio);
10049 return (CTL_RETVAL_COMPLETE);
10053 * INQUIRY with the EVPD bit set.
10056 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10058 struct ctl_lun *lun;
10059 struct scsi_inquiry *cdb;
10060 int alloc_len, retval;
10062 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10063 cdb = (struct scsi_inquiry *)ctsio->cdb;
10064 alloc_len = scsi_2btoul(cdb->length);
10066 switch (cdb->page_code) {
10067 case SVPD_SUPPORTED_PAGES:
10068 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10070 case SVPD_UNIT_SERIAL_NUMBER:
10071 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10073 case SVPD_DEVICE_ID:
10074 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10076 case SVPD_EXTENDED_INQUIRY_DATA:
10077 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10079 case SVPD_MODE_PAGE_POLICY:
10080 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10082 case SVPD_SCSI_PORTS:
10083 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10085 case SVPD_SCSI_TPC:
10086 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10088 case SVPD_BLOCK_LIMITS:
10089 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10091 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10094 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10096 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10099 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10101 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10105 ctl_set_invalid_field(ctsio,
10111 ctl_done((union ctl_io *)ctsio);
10112 retval = CTL_RETVAL_COMPLETE;
10120 * Standard INQUIRY data.
10123 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10125 struct scsi_inquiry_data *inq_ptr;
10126 struct scsi_inquiry *cdb;
10127 struct ctl_softc *softc = control_softc;
10128 struct ctl_port *port;
10129 struct ctl_lun *lun;
10131 uint32_t alloc_len, data_len;
10132 ctl_port_type port_type;
10134 port = ctl_io_port(&ctsio->io_hdr);
10135 port_type = port->port_type;
10136 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10137 port_type = CTL_PORT_SCSI;
10139 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10140 cdb = (struct scsi_inquiry *)ctsio->cdb;
10141 alloc_len = scsi_2btoul(cdb->length);
10144 * We malloc the full inquiry data size here and fill it
10145 * in. If the user only asks for less, we'll give him
10148 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10149 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10150 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10151 ctsio->kern_sg_entries = 0;
10152 ctsio->kern_data_resid = 0;
10153 ctsio->kern_rel_offset = 0;
10155 if (data_len < alloc_len) {
10156 ctsio->residual = alloc_len - data_len;
10157 ctsio->kern_data_len = data_len;
10158 ctsio->kern_total_len = data_len;
10160 ctsio->residual = 0;
10161 ctsio->kern_data_len = alloc_len;
10162 ctsio->kern_total_len = alloc_len;
10166 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10167 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10168 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10169 lun->be_lun->lun_type;
10171 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10172 lun->be_lun->lun_type;
10174 if (lun->flags & CTL_LUN_REMOVABLE)
10175 inq_ptr->dev_qual2 |= SID_RMB;
10177 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10179 /* RMB in byte 2 is 0 */
10180 inq_ptr->version = SCSI_REV_SPC5;
10183 * According to SAM-3, even if a device only supports a single
10184 * level of LUN addressing, it should still set the HISUP bit:
10186 * 4.9.1 Logical unit numbers overview
10188 * All logical unit number formats described in this standard are
10189 * hierarchical in structure even when only a single level in that
10190 * hierarchy is used. The HISUP bit shall be set to one in the
10191 * standard INQUIRY data (see SPC-2) when any logical unit number
10192 * format described in this standard is used. Non-hierarchical
10193 * formats are outside the scope of this standard.
10195 * Therefore we set the HiSup bit here.
10197 * The response format is 2, per SPC-3.
10199 inq_ptr->response_format = SID_HiSup | 2;
10201 inq_ptr->additional_length = data_len -
10202 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10203 CTL_DEBUG_PRINT(("additional_length = %d\n",
10204 inq_ptr->additional_length));
10206 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10207 if (port_type == CTL_PORT_SCSI)
10208 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10209 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10210 inq_ptr->flags = SID_CmdQue;
10211 if (port_type == CTL_PORT_SCSI)
10212 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10215 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10216 * We have 8 bytes for the vendor name, and 16 bytes for the device
10217 * name and 4 bytes for the revision.
10219 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10220 "vendor")) == NULL) {
10221 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10223 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10224 strncpy(inq_ptr->vendor, val,
10225 min(sizeof(inq_ptr->vendor), strlen(val)));
10228 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10229 sizeof(inq_ptr->product));
10230 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10231 switch (lun->be_lun->lun_type) {
10233 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10234 sizeof(inq_ptr->product));
10237 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10238 sizeof(inq_ptr->product));
10241 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10242 sizeof(inq_ptr->product));
10245 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10246 sizeof(inq_ptr->product));
10250 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10251 strncpy(inq_ptr->product, val,
10252 min(sizeof(inq_ptr->product), strlen(val)));
10256 * XXX make this a macro somewhere so it automatically gets
10257 * incremented when we make changes.
10259 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10260 "revision")) == NULL) {
10261 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10263 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10264 strncpy(inq_ptr->revision, val,
10265 min(sizeof(inq_ptr->revision), strlen(val)));
10269 * For parallel SCSI, we support double transition and single
10270 * transition clocking. We also support QAS (Quick Arbitration
10271 * and Selection) and Information Unit transfers on both the
10272 * control and array devices.
10274 if (port_type == CTL_PORT_SCSI)
10275 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10278 /* SAM-6 (no version claimed) */
10279 scsi_ulto2b(0x00C0, inq_ptr->version1);
10280 /* SPC-5 (no version claimed) */
10281 scsi_ulto2b(0x05C0, inq_ptr->version2);
10282 if (port_type == CTL_PORT_FC) {
10283 /* FCP-2 ANSI INCITS.350:2003 */
10284 scsi_ulto2b(0x0917, inq_ptr->version3);
10285 } else if (port_type == CTL_PORT_SCSI) {
10286 /* SPI-4 ANSI INCITS.362:200x */
10287 scsi_ulto2b(0x0B56, inq_ptr->version3);
10288 } else if (port_type == CTL_PORT_ISCSI) {
10289 /* iSCSI (no version claimed) */
10290 scsi_ulto2b(0x0960, inq_ptr->version3);
10291 } else if (port_type == CTL_PORT_SAS) {
10292 /* SAS (no version claimed) */
10293 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10297 /* SBC-4 (no version claimed) */
10298 scsi_ulto2b(0x0600, inq_ptr->version4);
10300 switch (lun->be_lun->lun_type) {
10302 /* SBC-4 (no version claimed) */
10303 scsi_ulto2b(0x0600, inq_ptr->version4);
10308 /* MMC-6 (no version claimed) */
10309 scsi_ulto2b(0x04E0, inq_ptr->version4);
10316 ctl_set_success(ctsio);
10317 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10318 ctsio->be_move_done = ctl_config_move_done;
10319 ctl_datamove((union ctl_io *)ctsio);
10320 return (CTL_RETVAL_COMPLETE);
10324 ctl_inquiry(struct ctl_scsiio *ctsio)
10326 struct scsi_inquiry *cdb;
10329 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10331 cdb = (struct scsi_inquiry *)ctsio->cdb;
10332 if (cdb->byte2 & SI_EVPD)
10333 retval = ctl_inquiry_evpd(ctsio);
10334 else if (cdb->page_code == 0)
10335 retval = ctl_inquiry_std(ctsio);
10337 ctl_set_invalid_field(ctsio,
10343 ctl_done((union ctl_io *)ctsio);
10344 return (CTL_RETVAL_COMPLETE);
10351 ctl_get_config(struct ctl_scsiio *ctsio)
10353 struct scsi_get_config_header *hdr;
10354 struct scsi_get_config_feature *feature;
10355 struct scsi_get_config *cdb;
10356 struct ctl_lun *lun;
10357 uint32_t alloc_len, data_len;
10360 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10361 cdb = (struct scsi_get_config *)ctsio->cdb;
10362 rt = (cdb->rt & SGC_RT_MASK);
10363 starting = scsi_2btoul(cdb->starting_feature);
10364 alloc_len = scsi_2btoul(cdb->length);
10366 data_len = sizeof(struct scsi_get_config_header) +
10367 sizeof(struct scsi_get_config_feature) + 8 +
10368 sizeof(struct scsi_get_config_feature) + 8 +
10369 sizeof(struct scsi_get_config_feature) + 4 +
10370 sizeof(struct scsi_get_config_feature) + 4 +
10371 sizeof(struct scsi_get_config_feature) + 8 +
10372 sizeof(struct scsi_get_config_feature) +
10373 sizeof(struct scsi_get_config_feature) + 4 +
10374 sizeof(struct scsi_get_config_feature) + 4 +
10375 sizeof(struct scsi_get_config_feature) + 4 +
10376 sizeof(struct scsi_get_config_feature) + 4 +
10377 sizeof(struct scsi_get_config_feature) + 4 +
10378 sizeof(struct scsi_get_config_feature) + 4;
10379 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10380 ctsio->kern_sg_entries = 0;
10381 ctsio->kern_data_resid = 0;
10382 ctsio->kern_rel_offset = 0;
10384 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10385 if (lun->flags & CTL_LUN_NO_MEDIA)
10386 scsi_ulto2b(0x0000, hdr->current_profile);
10388 scsi_ulto2b(0x0010, hdr->current_profile);
10389 feature = (struct scsi_get_config_feature *)(hdr + 1);
10391 if (starting > 0x003b)
10393 if (starting > 0x003a)
10395 if (starting > 0x002b)
10397 if (starting > 0x002a)
10399 if (starting > 0x001f)
10401 if (starting > 0x001e)
10403 if (starting > 0x001d)
10405 if (starting > 0x0010)
10407 if (starting > 0x0003)
10409 if (starting > 0x0002)
10411 if (starting > 0x0001)
10413 if (starting > 0x0000)
10417 scsi_ulto2b(0x0000, feature->feature_code);
10418 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10419 feature->add_length = 8;
10420 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10421 feature->feature_data[2] = 0x00;
10422 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10423 feature->feature_data[6] = 0x01;
10424 feature = (struct scsi_get_config_feature *)
10425 &feature->feature_data[feature->add_length];
10428 scsi_ulto2b(0x0001, feature->feature_code);
10429 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10430 feature->add_length = 8;
10431 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10432 feature->feature_data[4] = 0x03;
10433 feature = (struct scsi_get_config_feature *)
10434 &feature->feature_data[feature->add_length];
10437 scsi_ulto2b(0x0002, feature->feature_code);
10438 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10439 feature->add_length = 4;
10440 feature->feature_data[0] = 0x02;
10441 feature = (struct scsi_get_config_feature *)
10442 &feature->feature_data[feature->add_length];
10444 f3: /* Removable Medium */
10445 scsi_ulto2b(0x0003, feature->feature_code);
10446 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10447 feature->add_length = 4;
10448 feature->feature_data[0] = 0x39;
10449 feature = (struct scsi_get_config_feature *)
10450 &feature->feature_data[feature->add_length];
10452 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10455 f10: /* Random Read */
10456 scsi_ulto2b(0x0010, feature->feature_code);
10457 feature->flags = 0x00;
10458 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10459 feature->flags |= SGC_F_CURRENT;
10460 feature->add_length = 8;
10461 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10462 scsi_ulto2b(1, &feature->feature_data[4]);
10463 feature->feature_data[6] = 0x00;
10464 feature = (struct scsi_get_config_feature *)
10465 &feature->feature_data[feature->add_length];
10467 f1d: /* Multi-Read */
10468 scsi_ulto2b(0x001D, feature->feature_code);
10469 feature->flags = 0x00;
10470 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10471 feature->flags |= SGC_F_CURRENT;
10472 feature->add_length = 0;
10473 feature = (struct scsi_get_config_feature *)
10474 &feature->feature_data[feature->add_length];
10477 scsi_ulto2b(0x001E, feature->feature_code);
10478 feature->flags = 0x00;
10479 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10480 feature->flags |= SGC_F_CURRENT;
10481 feature->add_length = 4;
10482 feature->feature_data[0] = 0x00;
10483 feature = (struct scsi_get_config_feature *)
10484 &feature->feature_data[feature->add_length];
10486 f1f: /* DVD Read */
10487 scsi_ulto2b(0x001F, feature->feature_code);
10488 feature->flags = 0x08;
10489 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10490 feature->flags |= SGC_F_CURRENT;
10491 feature->add_length = 4;
10492 feature->feature_data[0] = 0x01;
10493 feature->feature_data[2] = 0x03;
10494 feature = (struct scsi_get_config_feature *)
10495 &feature->feature_data[feature->add_length];
10498 scsi_ulto2b(0x002A, feature->feature_code);
10499 feature->flags = 0x04;
10500 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10501 feature->flags |= SGC_F_CURRENT;
10502 feature->add_length = 4;
10503 feature->feature_data[0] = 0x00;
10504 feature->feature_data[1] = 0x00;
10505 feature = (struct scsi_get_config_feature *)
10506 &feature->feature_data[feature->add_length];
10509 scsi_ulto2b(0x002B, feature->feature_code);
10510 feature->flags = 0x00;
10511 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10512 feature->flags |= SGC_F_CURRENT;
10513 feature->add_length = 4;
10514 feature->feature_data[0] = 0x00;
10515 feature = (struct scsi_get_config_feature *)
10516 &feature->feature_data[feature->add_length];
10518 f3a: /* DVD+RW Dual Layer */
10519 scsi_ulto2b(0x003A, feature->feature_code);
10520 feature->flags = 0x00;
10521 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10522 feature->flags |= SGC_F_CURRENT;
10523 feature->add_length = 4;
10524 feature->feature_data[0] = 0x00;
10525 feature->feature_data[1] = 0x00;
10526 feature = (struct scsi_get_config_feature *)
10527 &feature->feature_data[feature->add_length];
10529 f3b: /* DVD+R Dual Layer */
10530 scsi_ulto2b(0x003B, feature->feature_code);
10531 feature->flags = 0x00;
10532 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10533 feature->flags |= SGC_F_CURRENT;
10534 feature->add_length = 4;
10535 feature->feature_data[0] = 0x00;
10536 feature = (struct scsi_get_config_feature *)
10537 &feature->feature_data[feature->add_length];
10540 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10541 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10542 feature = (struct scsi_get_config_feature *)(hdr + 1);
10543 if (scsi_2btoul(feature->feature_code) == starting)
10544 feature = (struct scsi_get_config_feature *)
10545 &feature->feature_data[feature->add_length];
10546 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10548 scsi_ulto4b(data_len - 4, hdr->data_length);
10549 if (data_len < alloc_len) {
10550 ctsio->residual = alloc_len - data_len;
10551 ctsio->kern_data_len = data_len;
10552 ctsio->kern_total_len = data_len;
10554 ctsio->residual = 0;
10555 ctsio->kern_data_len = alloc_len;
10556 ctsio->kern_total_len = alloc_len;
10559 ctl_set_success(ctsio);
10560 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10561 ctsio->be_move_done = ctl_config_move_done;
10562 ctl_datamove((union ctl_io *)ctsio);
10563 return (CTL_RETVAL_COMPLETE);
10567 ctl_get_event_status(struct ctl_scsiio *ctsio)
10569 struct scsi_get_event_status_header *hdr;
10570 struct scsi_get_event_status *cdb;
10571 struct ctl_lun *lun;
10572 uint32_t alloc_len, data_len;
10575 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10576 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10577 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10578 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10579 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10580 ctl_done((union ctl_io *)ctsio);
10581 return (CTL_RETVAL_COMPLETE);
10583 notif_class = cdb->notif_class;
10584 alloc_len = scsi_2btoul(cdb->length);
10586 data_len = sizeof(struct scsi_get_event_status_header);
10587 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10588 ctsio->kern_sg_entries = 0;
10589 ctsio->kern_data_resid = 0;
10590 ctsio->kern_rel_offset = 0;
10592 if (data_len < alloc_len) {
10593 ctsio->residual = alloc_len - data_len;
10594 ctsio->kern_data_len = data_len;
10595 ctsio->kern_total_len = data_len;
10597 ctsio->residual = 0;
10598 ctsio->kern_data_len = alloc_len;
10599 ctsio->kern_total_len = alloc_len;
10602 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10603 scsi_ulto2b(0, hdr->descr_length);
10604 hdr->nea_class = SGESN_NEA;
10605 hdr->supported_class = 0;
10607 ctl_set_success(ctsio);
10608 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10609 ctsio->be_move_done = ctl_config_move_done;
10610 ctl_datamove((union ctl_io *)ctsio);
10611 return (CTL_RETVAL_COMPLETE);
10615 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10617 struct scsi_mechanism_status_header *hdr;
10618 struct scsi_mechanism_status *cdb;
10619 struct ctl_lun *lun;
10620 uint32_t alloc_len, data_len;
10622 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10623 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10624 alloc_len = scsi_2btoul(cdb->length);
10626 data_len = sizeof(struct scsi_mechanism_status_header);
10627 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10628 ctsio->kern_sg_entries = 0;
10629 ctsio->kern_data_resid = 0;
10630 ctsio->kern_rel_offset = 0;
10632 if (data_len < alloc_len) {
10633 ctsio->residual = alloc_len - data_len;
10634 ctsio->kern_data_len = data_len;
10635 ctsio->kern_total_len = data_len;
10637 ctsio->residual = 0;
10638 ctsio->kern_data_len = alloc_len;
10639 ctsio->kern_total_len = alloc_len;
10642 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10643 hdr->state1 = 0x00;
10644 hdr->state2 = 0xe0;
10645 scsi_ulto3b(0, hdr->lba);
10646 hdr->slots_num = 0;
10647 scsi_ulto2b(0, hdr->slots_length);
10649 ctl_set_success(ctsio);
10650 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10651 ctsio->be_move_done = ctl_config_move_done;
10652 ctl_datamove((union ctl_io *)ctsio);
10653 return (CTL_RETVAL_COMPLETE);
10657 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10662 buf[1] = bin2bcd((lba / 75) / 60);
10663 buf[2] = bin2bcd((lba / 75) % 60);
10664 buf[3] = bin2bcd(lba % 75);
10668 ctl_read_toc(struct ctl_scsiio *ctsio)
10670 struct scsi_read_toc_hdr *hdr;
10671 struct scsi_read_toc_type01_descr *descr;
10672 struct scsi_read_toc *cdb;
10673 struct ctl_lun *lun;
10674 uint32_t alloc_len, data_len;
10677 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10678 cdb = (struct scsi_read_toc *)ctsio->cdb;
10679 msf = (cdb->byte2 & CD_MSF) != 0;
10680 format = cdb->format;
10681 alloc_len = scsi_2btoul(cdb->data_len);
10683 data_len = sizeof(struct scsi_read_toc_hdr);
10685 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10687 data_len += sizeof(struct scsi_read_toc_type01_descr);
10688 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10689 ctsio->kern_sg_entries = 0;
10690 ctsio->kern_data_resid = 0;
10691 ctsio->kern_rel_offset = 0;
10693 if (data_len < alloc_len) {
10694 ctsio->residual = alloc_len - data_len;
10695 ctsio->kern_data_len = data_len;
10696 ctsio->kern_total_len = data_len;
10698 ctsio->residual = 0;
10699 ctsio->kern_data_len = alloc_len;
10700 ctsio->kern_total_len = alloc_len;
10703 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10705 scsi_ulto2b(0x12, hdr->data_length);
10708 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10709 descr->addr_ctl = 0x14;
10710 descr->track_number = 1;
10712 ctl_ultomsf(0, descr->track_start);
10714 scsi_ulto4b(0, descr->track_start);
10716 descr->addr_ctl = 0x14;
10717 descr->track_number = 0xaa;
10719 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10721 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10723 scsi_ulto2b(0x0a, hdr->data_length);
10726 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10727 descr->addr_ctl = 0x14;
10728 descr->track_number = 1;
10730 ctl_ultomsf(0, descr->track_start);
10732 scsi_ulto4b(0, descr->track_start);
10735 ctl_set_success(ctsio);
10736 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10737 ctsio->be_move_done = ctl_config_move_done;
10738 ctl_datamove((union ctl_io *)ctsio);
10739 return (CTL_RETVAL_COMPLETE);
10743 * For known CDB types, parse the LBA and length.
10746 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10748 if (io->io_hdr.io_type != CTL_IO_SCSI)
10751 switch (io->scsiio.cdb[0]) {
10752 case COMPARE_AND_WRITE: {
10753 struct scsi_compare_and_write *cdb;
10755 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10757 *lba = scsi_8btou64(cdb->addr);
10758 *len = cdb->length;
10763 struct scsi_rw_6 *cdb;
10765 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10767 *lba = scsi_3btoul(cdb->addr);
10768 /* only 5 bits are valid in the most significant address byte */
10770 *len = cdb->length;
10775 struct scsi_rw_10 *cdb;
10777 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10779 *lba = scsi_4btoul(cdb->addr);
10780 *len = scsi_2btoul(cdb->length);
10783 case WRITE_VERIFY_10: {
10784 struct scsi_write_verify_10 *cdb;
10786 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10788 *lba = scsi_4btoul(cdb->addr);
10789 *len = scsi_2btoul(cdb->length);
10794 struct scsi_rw_12 *cdb;
10796 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10798 *lba = scsi_4btoul(cdb->addr);
10799 *len = scsi_4btoul(cdb->length);
10802 case WRITE_VERIFY_12: {
10803 struct scsi_write_verify_12 *cdb;
10805 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10807 *lba = scsi_4btoul(cdb->addr);
10808 *len = scsi_4btoul(cdb->length);
10813 struct scsi_rw_16 *cdb;
10815 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10817 *lba = scsi_8btou64(cdb->addr);
10818 *len = scsi_4btoul(cdb->length);
10821 case WRITE_ATOMIC_16: {
10822 struct scsi_write_atomic_16 *cdb;
10824 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10826 *lba = scsi_8btou64(cdb->addr);
10827 *len = scsi_2btoul(cdb->length);
10830 case WRITE_VERIFY_16: {
10831 struct scsi_write_verify_16 *cdb;
10833 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10835 *lba = scsi_8btou64(cdb->addr);
10836 *len = scsi_4btoul(cdb->length);
10839 case WRITE_SAME_10: {
10840 struct scsi_write_same_10 *cdb;
10842 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10844 *lba = scsi_4btoul(cdb->addr);
10845 *len = scsi_2btoul(cdb->length);
10848 case WRITE_SAME_16: {
10849 struct scsi_write_same_16 *cdb;
10851 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10853 *lba = scsi_8btou64(cdb->addr);
10854 *len = scsi_4btoul(cdb->length);
10858 struct scsi_verify_10 *cdb;
10860 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10862 *lba = scsi_4btoul(cdb->addr);
10863 *len = scsi_2btoul(cdb->length);
10867 struct scsi_verify_12 *cdb;
10869 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10871 *lba = scsi_4btoul(cdb->addr);
10872 *len = scsi_4btoul(cdb->length);
10876 struct scsi_verify_16 *cdb;
10878 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10880 *lba = scsi_8btou64(cdb->addr);
10881 *len = scsi_4btoul(cdb->length);
10889 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10890 struct scsi_get_lba_status *cdb;
10892 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10893 *lba = scsi_8btou64(cdb->addr);
10899 break; /* NOTREACHED */
10906 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10909 uint64_t endlba1, endlba2;
10911 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10912 endlba2 = lba2 + len2 - 1;
10914 if ((endlba1 < lba2) || (endlba2 < lba1))
10915 return (CTL_ACTION_PASS);
10917 return (CTL_ACTION_BLOCK);
10921 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10923 struct ctl_ptr_len_flags *ptrlen;
10924 struct scsi_unmap_desc *buf, *end, *range;
10928 /* If not UNMAP -- go other way. */
10929 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10930 io->scsiio.cdb[0] != UNMAP)
10931 return (CTL_ACTION_ERROR);
10933 /* If UNMAP without data -- block and wait for data. */
10934 ptrlen = (struct ctl_ptr_len_flags *)
10935 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10936 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10937 ptrlen->ptr == NULL)
10938 return (CTL_ACTION_BLOCK);
10940 /* UNMAP with data -- check for collision. */
10941 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10942 end = buf + ptrlen->len / sizeof(*buf);
10943 for (range = buf; range < end; range++) {
10944 lba = scsi_8btou64(range->lba);
10945 len = scsi_4btoul(range->length);
10946 if ((lba < lba2 + len2) && (lba + len > lba2))
10947 return (CTL_ACTION_BLOCK);
10949 return (CTL_ACTION_PASS);
10953 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10955 uint64_t lba1, lba2;
10956 uint64_t len1, len2;
10959 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10960 return (CTL_ACTION_ERROR);
10962 retval = ctl_extent_check_unmap(io1, lba2, len2);
10963 if (retval != CTL_ACTION_ERROR)
10966 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10967 return (CTL_ACTION_ERROR);
10969 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10971 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10975 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10977 uint64_t lba1, lba2;
10978 uint64_t len1, len2;
10980 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10981 return (CTL_ACTION_PASS);
10982 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10983 return (CTL_ACTION_ERROR);
10984 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10985 return (CTL_ACTION_ERROR);
10987 if (lba1 + len1 == lba2)
10988 return (CTL_ACTION_BLOCK);
10989 return (CTL_ACTION_PASS);
10993 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10994 union ctl_io *ooa_io)
10996 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10997 const ctl_serialize_action *serialize_row;
11000 * The initiator attempted multiple untagged commands at the same
11001 * time. Can't do that.
11003 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11004 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11005 && ((pending_io->io_hdr.nexus.targ_port ==
11006 ooa_io->io_hdr.nexus.targ_port)
11007 && (pending_io->io_hdr.nexus.initid ==
11008 ooa_io->io_hdr.nexus.initid))
11009 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11010 CTL_FLAG_STATUS_SENT)) == 0))
11011 return (CTL_ACTION_OVERLAP);
11014 * The initiator attempted to send multiple tagged commands with
11015 * the same ID. (It's fine if different initiators have the same
11018 * Even if all of those conditions are true, we don't kill the I/O
11019 * if the command ahead of us has been aborted. We won't end up
11020 * sending it to the FETD, and it's perfectly legal to resend a
11021 * command with the same tag number as long as the previous
11022 * instance of this tag number has been aborted somehow.
11024 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11025 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11026 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11027 && ((pending_io->io_hdr.nexus.targ_port ==
11028 ooa_io->io_hdr.nexus.targ_port)
11029 && (pending_io->io_hdr.nexus.initid ==
11030 ooa_io->io_hdr.nexus.initid))
11031 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11032 CTL_FLAG_STATUS_SENT)) == 0))
11033 return (CTL_ACTION_OVERLAP_TAG);
11036 * If we get a head of queue tag, SAM-3 says that we should
11037 * immediately execute it.
11039 * What happens if this command would normally block for some other
11040 * reason? e.g. a request sense with a head of queue tag
11041 * immediately after a write. Normally that would block, but this
11042 * will result in its getting executed immediately...
11044 * We currently return "pass" instead of "skip", so we'll end up
11045 * going through the rest of the queue to check for overlapped tags.
11047 * XXX KDM check for other types of blockage first??
11049 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11050 return (CTL_ACTION_PASS);
11053 * Ordered tags have to block until all items ahead of them
11054 * have completed. If we get called with an ordered tag, we always
11055 * block, if something else is ahead of us in the queue.
11057 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11058 return (CTL_ACTION_BLOCK);
11061 * Simple tags get blocked until all head of queue and ordered tags
11062 * ahead of them have completed. I'm lumping untagged commands in
11063 * with simple tags here. XXX KDM is that the right thing to do?
11065 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11066 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11067 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11068 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11069 return (CTL_ACTION_BLOCK);
11071 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11072 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11073 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11074 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11075 pending_io->scsiio.cdb[1], pending_io));
11076 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11077 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11078 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11079 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11080 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11081 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11082 ooa_io->scsiio.cdb[1], ooa_io));
11084 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11086 switch (serialize_row[pending_entry->seridx]) {
11087 case CTL_SER_BLOCK:
11088 return (CTL_ACTION_BLOCK);
11089 case CTL_SER_EXTENT:
11090 return (ctl_extent_check(ooa_io, pending_io,
11091 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11092 case CTL_SER_EXTENTOPT:
11093 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11094 SCP_QUEUE_ALG_UNRESTRICTED)
11095 return (ctl_extent_check(ooa_io, pending_io,
11097 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11098 return (CTL_ACTION_PASS);
11099 case CTL_SER_EXTENTSEQ:
11100 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11101 return (ctl_extent_check_seq(ooa_io, pending_io));
11102 return (CTL_ACTION_PASS);
11104 return (CTL_ACTION_PASS);
11105 case CTL_SER_BLOCKOPT:
11106 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11107 SCP_QUEUE_ALG_UNRESTRICTED)
11108 return (CTL_ACTION_BLOCK);
11109 return (CTL_ACTION_PASS);
11111 return (CTL_ACTION_SKIP);
11113 panic("%s: Invalid serialization value %d for %d => %d",
11114 __func__, serialize_row[pending_entry->seridx],
11115 pending_entry->seridx, ooa_entry->seridx);
11118 return (CTL_ACTION_ERROR);
11122 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11124 * - pending_io is generally either incoming, or on the blocked queue
11125 * - starting I/O is the I/O we want to start the check with.
11128 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11129 union ctl_io *starting_io)
11131 union ctl_io *ooa_io;
11134 mtx_assert(&lun->lun_lock, MA_OWNED);
11137 * Run back along the OOA queue, starting with the current
11138 * blocked I/O and going through every I/O before it on the
11139 * queue. If starting_io is NULL, we'll just end up returning
11142 for (ooa_io = starting_io; ooa_io != NULL;
11143 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11147 * This routine just checks to see whether
11148 * cur_blocked is blocked by ooa_io, which is ahead
11149 * of it in the queue. It doesn't queue/dequeue
11152 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11154 case CTL_ACTION_BLOCK:
11155 case CTL_ACTION_OVERLAP:
11156 case CTL_ACTION_OVERLAP_TAG:
11157 case CTL_ACTION_SKIP:
11158 case CTL_ACTION_ERROR:
11160 break; /* NOTREACHED */
11161 case CTL_ACTION_PASS:
11164 panic("%s: Invalid action %d\n", __func__, action);
11168 return (CTL_ACTION_PASS);
11173 * - An I/O has just completed, and has been removed from the per-LUN OOA
11174 * queue, so some items on the blocked queue may now be unblocked.
11177 ctl_check_blocked(struct ctl_lun *lun)
11179 struct ctl_softc *softc = lun->ctl_softc;
11180 union ctl_io *cur_blocked, *next_blocked;
11182 mtx_assert(&lun->lun_lock, MA_OWNED);
11185 * Run forward from the head of the blocked queue, checking each
11186 * entry against the I/Os prior to it on the OOA queue to see if
11187 * there is still any blockage.
11189 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11190 * with our removing a variable on it while it is traversing the
11193 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11194 cur_blocked != NULL; cur_blocked = next_blocked) {
11195 union ctl_io *prev_ooa;
11198 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11201 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11202 ctl_ooaq, ooa_links);
11205 * If cur_blocked happens to be the first item in the OOA
11206 * queue now, prev_ooa will be NULL, and the action
11207 * returned will just be CTL_ACTION_PASS.
11209 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11212 case CTL_ACTION_BLOCK:
11213 /* Nothing to do here, still blocked */
11215 case CTL_ACTION_OVERLAP:
11216 case CTL_ACTION_OVERLAP_TAG:
11218 * This shouldn't happen! In theory we've already
11219 * checked this command for overlap...
11222 case CTL_ACTION_PASS:
11223 case CTL_ACTION_SKIP: {
11224 const struct ctl_cmd_entry *entry;
11227 * The skip case shouldn't happen, this transaction
11228 * should have never made it onto the blocked queue.
11231 * This I/O is no longer blocked, we can remove it
11232 * from the blocked queue. Since this is a TAILQ
11233 * (doubly linked list), we can do O(1) removals
11234 * from any place on the list.
11236 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11238 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11240 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11241 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11243 * Need to send IO back to original side to
11246 union ctl_ha_msg msg_info;
11248 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11249 msg_info.hdr.original_sc =
11250 cur_blocked->io_hdr.original_sc;
11251 msg_info.hdr.serializing_sc = cur_blocked;
11252 msg_info.hdr.msg_type = CTL_MSG_R2R;
11253 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11254 sizeof(msg_info.hdr), M_NOWAIT);
11257 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11260 * Check this I/O for LUN state changes that may
11261 * have happened while this command was blocked.
11262 * The LUN state may have been changed by a command
11263 * ahead of us in the queue, so we need to re-check
11264 * for any states that can be caused by SCSI
11267 if (ctl_scsiio_lun_check(lun, entry,
11268 &cur_blocked->scsiio) == 0) {
11269 cur_blocked->io_hdr.flags |=
11270 CTL_FLAG_IS_WAS_ON_RTR;
11271 ctl_enqueue_rtr(cur_blocked);
11273 ctl_done(cur_blocked);
11278 * This probably shouldn't happen -- we shouldn't
11279 * get CTL_ACTION_ERROR, or anything else.
11285 return (CTL_RETVAL_COMPLETE);
11289 * This routine (with one exception) checks LUN flags that can be set by
11290 * commands ahead of us in the OOA queue. These flags have to be checked
11291 * when a command initially comes in, and when we pull a command off the
11292 * blocked queue and are preparing to execute it. The reason we have to
11293 * check these flags for commands on the blocked queue is that the LUN
11294 * state may have been changed by a command ahead of us while we're on the
11297 * Ordering is somewhat important with these checks, so please pay
11298 * careful attention to the placement of any new checks.
11301 ctl_scsiio_lun_check(struct ctl_lun *lun,
11302 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11304 struct ctl_softc *softc = lun->ctl_softc;
11310 mtx_assert(&lun->lun_lock, MA_OWNED);
11313 * If this shelf is a secondary shelf controller, we may have to
11314 * reject some commands disallowed by HA mode and link state.
11316 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11317 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11318 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11319 ctl_set_lun_unavail(ctsio);
11323 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11324 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11325 ctl_set_lun_transit(ctsio);
11329 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11330 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11331 ctl_set_lun_standby(ctsio);
11336 /* The rest of checks are only done on executing side */
11337 if (softc->ha_mode == CTL_HA_MODE_XFER)
11341 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11343 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11344 ctl_set_hw_write_protected(ctsio);
11348 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11349 ctl_set_sense(ctsio, /*current_error*/ 1,
11350 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11351 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11358 * Check for a reservation conflict. If this command isn't allowed
11359 * even on reserved LUNs, and if this initiator isn't the one who
11360 * reserved us, reject the command with a reservation conflict.
11362 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11363 if ((lun->flags & CTL_LUN_RESERVED)
11364 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11365 if (lun->res_idx != residx) {
11366 ctl_set_reservation_conflict(ctsio);
11372 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11373 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11374 /* No reservation or command is allowed. */;
11375 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11376 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11377 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11378 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11379 /* The command is allowed for Write Exclusive resv. */;
11382 * if we aren't registered or it's a res holder type
11383 * reservation and this isn't the res holder then set a
11386 if (ctl_get_prkey(lun, residx) == 0 ||
11387 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11388 ctl_set_reservation_conflict(ctsio);
11394 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11395 if (lun->flags & CTL_LUN_EJECTED)
11396 ctl_set_lun_ejected(ctsio);
11397 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11398 if (lun->flags & CTL_LUN_REMOVABLE)
11399 ctl_set_lun_no_media(ctsio);
11401 ctl_set_lun_int_reqd(ctsio);
11402 } else if (lun->flags & CTL_LUN_STOPPED)
11403 ctl_set_lun_stopped(ctsio);
11415 ctl_failover_io(union ctl_io *io, int have_lock)
11417 ctl_set_busy(&io->scsiio);
11422 ctl_failover_lun(union ctl_io *rio)
11424 struct ctl_softc *softc = control_softc;
11425 struct ctl_lun *lun;
11426 struct ctl_io_hdr *io, *next_io;
11429 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11430 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11432 /* Find and lock the LUN. */
11433 mtx_lock(&softc->ctl_lock);
11434 if (targ_lun > CTL_MAX_LUNS ||
11435 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11436 mtx_unlock(&softc->ctl_lock);
11439 mtx_lock(&lun->lun_lock);
11440 mtx_unlock(&softc->ctl_lock);
11441 if (lun->flags & CTL_LUN_DISABLED) {
11442 mtx_unlock(&lun->lun_lock);
11446 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11447 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11448 /* We are master */
11449 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11450 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11451 io->flags |= CTL_FLAG_ABORT;
11452 io->flags |= CTL_FLAG_FAILOVER;
11453 } else { /* This can be only due to DATAMOVE */
11454 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11455 io->flags &= ~CTL_FLAG_DMA_INPROG;
11456 io->flags |= CTL_FLAG_IO_ACTIVE;
11457 io->port_status = 31340;
11458 ctl_enqueue_isc((union ctl_io *)io);
11462 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11463 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11464 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11465 io->flags |= CTL_FLAG_FAILOVER;
11467 ctl_set_busy(&((union ctl_io *)io)->
11469 ctl_done((union ctl_io *)io);
11473 } else { /* SERIALIZE modes */
11474 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11476 /* We are master */
11477 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11478 TAILQ_REMOVE(&lun->blocked_queue, io,
11480 io->flags &= ~CTL_FLAG_BLOCKED;
11481 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11482 ctl_free_io((union ctl_io *)io);
11485 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11486 /* We are master */
11487 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11488 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11489 ctl_free_io((union ctl_io *)io);
11492 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11493 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11494 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11495 ctl_set_busy(&((union ctl_io *)io)->
11497 ctl_done((union ctl_io *)io);
11501 ctl_check_blocked(lun);
11503 mtx_unlock(&lun->lun_lock);
11507 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11509 struct ctl_lun *lun;
11510 const struct ctl_cmd_entry *entry;
11511 uint32_t initidx, targ_lun;
11515 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11516 if (targ_lun < CTL_MAX_LUNS)
11517 lun = softc->ctl_luns[targ_lun];
11520 * If the LUN is invalid, pretend that it doesn't exist.
11521 * It will go away as soon as all pending I/O has been
11524 mtx_lock(&lun->lun_lock);
11525 if (lun->flags & CTL_LUN_DISABLED) {
11526 mtx_unlock(&lun->lun_lock);
11530 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11532 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11536 * Every I/O goes into the OOA queue for a particular LUN,
11537 * and stays there until completion.
11540 if (TAILQ_EMPTY(&lun->ooa_queue))
11541 lun->idle_time += getsbinuptime() - lun->last_busy;
11543 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11546 /* Get command entry and return error if it is unsuppotyed. */
11547 entry = ctl_validate_command(ctsio);
11548 if (entry == NULL) {
11550 mtx_unlock(&lun->lun_lock);
11554 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11555 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11558 * Check to see whether we can send this command to LUNs that don't
11559 * exist. This should pretty much only be the case for inquiry
11560 * and request sense. Further checks, below, really require having
11561 * a LUN, so we can't really check the command anymore. Just put
11562 * it on the rtr queue.
11565 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11566 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11567 ctl_enqueue_rtr((union ctl_io *)ctsio);
11571 ctl_set_unsupported_lun(ctsio);
11572 ctl_done((union ctl_io *)ctsio);
11573 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11577 * Make sure we support this particular command on this LUN.
11578 * e.g., we don't support writes to the control LUN.
11580 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11581 mtx_unlock(&lun->lun_lock);
11582 ctl_set_invalid_opcode(ctsio);
11583 ctl_done((union ctl_io *)ctsio);
11588 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11592 * If we've got a request sense, it'll clear the contingent
11593 * allegiance condition. Otherwise, if we have a CA condition for
11594 * this initiator, clear it, because it sent down a command other
11595 * than request sense.
11597 if ((ctsio->cdb[0] != REQUEST_SENSE)
11598 && (ctl_is_set(lun->have_ca, initidx)))
11599 ctl_clear_mask(lun->have_ca, initidx);
11603 * If the command has this flag set, it handles its own unit
11604 * attention reporting, we shouldn't do anything. Otherwise we
11605 * check for any pending unit attentions, and send them back to the
11606 * initiator. We only do this when a command initially comes in,
11607 * not when we pull it off the blocked queue.
11609 * According to SAM-3, section 5.3.2, the order that things get
11610 * presented back to the host is basically unit attentions caused
11611 * by some sort of reset event, busy status, reservation conflicts
11612 * or task set full, and finally any other status.
11614 * One issue here is that some of the unit attentions we report
11615 * don't fall into the "reset" category (e.g. "reported luns data
11616 * has changed"). So reporting it here, before the reservation
11617 * check, may be technically wrong. I guess the only thing to do
11618 * would be to check for and report the reset events here, and then
11619 * check for the other unit attention types after we check for a
11620 * reservation conflict.
11622 * XXX KDM need to fix this
11624 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11625 ctl_ua_type ua_type;
11626 u_int sense_len = 0;
11628 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11629 &sense_len, SSD_TYPE_NONE);
11630 if (ua_type != CTL_UA_NONE) {
11631 mtx_unlock(&lun->lun_lock);
11632 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11633 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11634 ctsio->sense_len = sense_len;
11635 ctl_done((union ctl_io *)ctsio);
11641 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11642 mtx_unlock(&lun->lun_lock);
11643 ctl_done((union ctl_io *)ctsio);
11648 * XXX CHD this is where we want to send IO to other side if
11649 * this LUN is secondary on this SC. We will need to make a copy
11650 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11651 * the copy we send as FROM_OTHER.
11652 * We also need to stuff the address of the original IO so we can
11653 * find it easily. Something similar will need be done on the other
11654 * side so when we are done we can find the copy.
11656 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11657 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11658 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11659 union ctl_ha_msg msg_info;
11662 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11663 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11664 mtx_unlock(&lun->lun_lock);
11666 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11667 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11668 msg_info.hdr.serializing_sc = NULL;
11669 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11670 msg_info.scsi.tag_num = ctsio->tag_num;
11671 msg_info.scsi.tag_type = ctsio->tag_type;
11672 msg_info.scsi.cdb_len = ctsio->cdb_len;
11673 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11675 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11676 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11677 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11678 ctl_set_busy(ctsio);
11679 ctl_done((union ctl_io *)ctsio);
11685 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11686 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11687 ctl_ooaq, ooa_links))) {
11688 case CTL_ACTION_BLOCK:
11689 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11690 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11692 mtx_unlock(&lun->lun_lock);
11694 case CTL_ACTION_PASS:
11695 case CTL_ACTION_SKIP:
11696 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11697 mtx_unlock(&lun->lun_lock);
11698 ctl_enqueue_rtr((union ctl_io *)ctsio);
11700 case CTL_ACTION_OVERLAP:
11701 mtx_unlock(&lun->lun_lock);
11702 ctl_set_overlapped_cmd(ctsio);
11703 ctl_done((union ctl_io *)ctsio);
11705 case CTL_ACTION_OVERLAP_TAG:
11706 mtx_unlock(&lun->lun_lock);
11707 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11708 ctl_done((union ctl_io *)ctsio);
11710 case CTL_ACTION_ERROR:
11712 mtx_unlock(&lun->lun_lock);
11713 ctl_set_internal_failure(ctsio,
11715 /*retry_count*/ 0);
11716 ctl_done((union ctl_io *)ctsio);
11722 const struct ctl_cmd_entry *
11723 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11725 const struct ctl_cmd_entry *entry;
11726 int service_action;
11728 entry = &ctl_cmd_table[ctsio->cdb[0]];
11730 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11731 if (entry->flags & CTL_CMD_FLAG_SA5) {
11732 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11733 entry = &((const struct ctl_cmd_entry *)
11734 entry->execute)[service_action];
11739 const struct ctl_cmd_entry *
11740 ctl_validate_command(struct ctl_scsiio *ctsio)
11742 const struct ctl_cmd_entry *entry;
11746 entry = ctl_get_cmd_entry(ctsio, &sa);
11747 if (entry->execute == NULL) {
11749 ctl_set_invalid_field(ctsio,
11756 ctl_set_invalid_opcode(ctsio);
11757 ctl_done((union ctl_io *)ctsio);
11760 KASSERT(entry->length > 0,
11761 ("Not defined length for command 0x%02x/0x%02x",
11762 ctsio->cdb[0], ctsio->cdb[1]));
11763 for (i = 1; i < entry->length; i++) {
11764 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11767 ctl_set_invalid_field(ctsio,
11772 /*bit*/ fls(diff) - 1);
11773 ctl_done((union ctl_io *)ctsio);
11780 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11783 switch (lun_type) {
11785 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11789 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11793 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11803 ctl_scsiio(struct ctl_scsiio *ctsio)
11806 const struct ctl_cmd_entry *entry;
11808 retval = CTL_RETVAL_COMPLETE;
11810 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11812 entry = ctl_get_cmd_entry(ctsio, NULL);
11815 * If this I/O has been aborted, just send it straight to
11816 * ctl_done() without executing it.
11818 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11819 ctl_done((union ctl_io *)ctsio);
11824 * All the checks should have been handled by ctl_scsiio_precheck().
11825 * We should be clear now to just execute the I/O.
11827 retval = entry->execute(ctsio);
11834 * Since we only implement one target right now, a bus reset simply resets
11835 * our single target.
11838 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11840 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11844 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11845 ctl_ua_type ua_type)
11847 struct ctl_port *port;
11848 struct ctl_lun *lun;
11851 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11852 union ctl_ha_msg msg_info;
11854 msg_info.hdr.nexus = io->io_hdr.nexus;
11855 if (ua_type==CTL_UA_TARG_RESET)
11856 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11858 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11859 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11860 msg_info.hdr.original_sc = NULL;
11861 msg_info.hdr.serializing_sc = NULL;
11862 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11863 sizeof(msg_info.task), M_WAITOK);
11867 mtx_lock(&softc->ctl_lock);
11868 port = ctl_io_port(&io->io_hdr);
11869 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11870 if (port != NULL &&
11871 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11873 retval += ctl_do_lun_reset(lun, io, ua_type);
11875 mtx_unlock(&softc->ctl_lock);
11876 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11881 * The LUN should always be set. The I/O is optional, and is used to
11882 * distinguish between I/Os sent by this initiator, and by other
11883 * initiators. We set unit attention for initiators other than this one.
11884 * SAM-3 is vague on this point. It does say that a unit attention should
11885 * be established for other initiators when a LUN is reset (see section
11886 * 5.7.3), but it doesn't specifically say that the unit attention should
11887 * be established for this particular initiator when a LUN is reset. Here
11888 * is the relevant text, from SAM-3 rev 8:
11890 * 5.7.2 When a SCSI initiator port aborts its own tasks
11892 * When a SCSI initiator port causes its own task(s) to be aborted, no
11893 * notification that the task(s) have been aborted shall be returned to
11894 * the SCSI initiator port other than the completion response for the
11895 * command or task management function action that caused the task(s) to
11896 * be aborted and notification(s) associated with related effects of the
11897 * action (e.g., a reset unit attention condition).
11899 * XXX KDM for now, we're setting unit attention for all initiators.
11902 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11910 mtx_lock(&lun->lun_lock);
11912 * Run through the OOA queue and abort each I/O.
11914 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11915 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11916 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11920 * This version sets unit attention for every
11923 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11924 ctl_est_ua_all(lun, initidx, ua_type);
11926 ctl_est_ua_all(lun, -1, ua_type);
11930 * A reset (any kind, really) clears reservations established with
11931 * RESERVE/RELEASE. It does not clear reservations established
11932 * with PERSISTENT RESERVE OUT, but we don't support that at the
11933 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11934 * reservations made with the RESERVE/RELEASE commands, because
11935 * those commands are obsolete in SPC-3.
11937 lun->flags &= ~CTL_LUN_RESERVED;
11940 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11941 ctl_clear_mask(lun->have_ca, i);
11943 lun->prevent_count = 0;
11944 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11945 ctl_clear_mask(lun->prevent, i);
11946 mtx_unlock(&lun->lun_lock);
11952 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11954 struct ctl_lun *lun;
11958 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11959 mtx_lock(&softc->ctl_lock);
11960 if (targ_lun >= CTL_MAX_LUNS ||
11961 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11962 mtx_unlock(&softc->ctl_lock);
11963 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11966 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11967 mtx_unlock(&softc->ctl_lock);
11968 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11970 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11971 union ctl_ha_msg msg_info;
11973 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11974 msg_info.hdr.nexus = io->io_hdr.nexus;
11975 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11976 msg_info.hdr.original_sc = NULL;
11977 msg_info.hdr.serializing_sc = NULL;
11978 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11979 sizeof(msg_info.task), M_WAITOK);
11985 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11990 mtx_assert(&lun->lun_lock, MA_OWNED);
11993 * Run through the OOA queue and attempt to find the given I/O.
11994 * The target port, initiator ID, tag type and tag number have to
11995 * match the values that we got from the initiator. If we have an
11996 * untagged command to abort, simply abort the first untagged command
11997 * we come to. We only allow one untagged command at a time of course.
11999 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12000 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12002 if ((targ_port == UINT32_MAX ||
12003 targ_port == xio->io_hdr.nexus.targ_port) &&
12004 (init_id == UINT32_MAX ||
12005 init_id == xio->io_hdr.nexus.initid)) {
12006 if (targ_port != xio->io_hdr.nexus.targ_port ||
12007 init_id != xio->io_hdr.nexus.initid)
12008 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12009 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12010 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12011 union ctl_ha_msg msg_info;
12013 msg_info.hdr.nexus = xio->io_hdr.nexus;
12014 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12015 msg_info.task.tag_num = xio->scsiio.tag_num;
12016 msg_info.task.tag_type = xio->scsiio.tag_type;
12017 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12018 msg_info.hdr.original_sc = NULL;
12019 msg_info.hdr.serializing_sc = NULL;
12020 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12021 sizeof(msg_info.task), M_NOWAIT);
12028 ctl_abort_task_set(union ctl_io *io)
12030 struct ctl_softc *softc = control_softc;
12031 struct ctl_lun *lun;
12037 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12038 mtx_lock(&softc->ctl_lock);
12039 if (targ_lun >= CTL_MAX_LUNS ||
12040 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12041 mtx_unlock(&softc->ctl_lock);
12042 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12046 mtx_lock(&lun->lun_lock);
12047 mtx_unlock(&softc->ctl_lock);
12048 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12049 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12050 io->io_hdr.nexus.initid,
12051 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12052 } else { /* CTL_TASK_CLEAR_TASK_SET */
12053 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12054 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12056 mtx_unlock(&lun->lun_lock);
12057 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12062 ctl_i_t_nexus_reset(union ctl_io *io)
12064 struct ctl_softc *softc = control_softc;
12065 struct ctl_lun *lun;
12068 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12069 union ctl_ha_msg msg_info;
12071 msg_info.hdr.nexus = io->io_hdr.nexus;
12072 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12073 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12074 msg_info.hdr.original_sc = NULL;
12075 msg_info.hdr.serializing_sc = NULL;
12076 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12077 sizeof(msg_info.task), M_WAITOK);
12080 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12081 mtx_lock(&softc->ctl_lock);
12082 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12083 mtx_lock(&lun->lun_lock);
12084 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12085 io->io_hdr.nexus.initid, 1);
12087 ctl_clear_mask(lun->have_ca, initidx);
12089 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12090 lun->flags &= ~CTL_LUN_RESERVED;
12091 if (ctl_is_set(lun->prevent, initidx)) {
12092 ctl_clear_mask(lun->prevent, initidx);
12093 lun->prevent_count--;
12095 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12096 mtx_unlock(&lun->lun_lock);
12098 mtx_unlock(&softc->ctl_lock);
12099 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12104 ctl_abort_task(union ctl_io *io)
12107 struct ctl_lun *lun;
12108 struct ctl_softc *softc;
12111 char printbuf[128];
12116 softc = control_softc;
12122 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12123 mtx_lock(&softc->ctl_lock);
12124 if (targ_lun >= CTL_MAX_LUNS ||
12125 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12126 mtx_unlock(&softc->ctl_lock);
12127 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12132 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12133 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12136 mtx_lock(&lun->lun_lock);
12137 mtx_unlock(&softc->ctl_lock);
12139 * Run through the OOA queue and attempt to find the given I/O.
12140 * The target port, initiator ID, tag type and tag number have to
12141 * match the values that we got from the initiator. If we have an
12142 * untagged command to abort, simply abort the first untagged command
12143 * we come to. We only allow one untagged command at a time of course.
12145 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12146 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12148 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12150 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12151 lun->lun, xio->scsiio.tag_num,
12152 xio->scsiio.tag_type,
12153 (xio->io_hdr.blocked_links.tqe_prev
12154 == NULL) ? "" : " BLOCKED",
12155 (xio->io_hdr.flags &
12156 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12157 (xio->io_hdr.flags &
12158 CTL_FLAG_ABORT) ? " ABORT" : "",
12159 (xio->io_hdr.flags &
12160 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12161 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12163 printf("%s\n", sbuf_data(&sb));
12166 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12167 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12168 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12172 * If the abort says that the task is untagged, the
12173 * task in the queue must be untagged. Otherwise,
12174 * we just check to see whether the tag numbers
12175 * match. This is because the QLogic firmware
12176 * doesn't pass back the tag type in an abort
12180 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12181 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12182 || (xio->scsiio.tag_num == io->taskio.tag_num))
12185 * XXX KDM we've got problems with FC, because it
12186 * doesn't send down a tag type with aborts. So we
12187 * can only really go by the tag number...
12188 * This may cause problems with parallel SCSI.
12189 * Need to figure that out!!
12191 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12192 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12194 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12195 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12196 union ctl_ha_msg msg_info;
12198 msg_info.hdr.nexus = io->io_hdr.nexus;
12199 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12200 msg_info.task.tag_num = io->taskio.tag_num;
12201 msg_info.task.tag_type = io->taskio.tag_type;
12202 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12203 msg_info.hdr.original_sc = NULL;
12204 msg_info.hdr.serializing_sc = NULL;
12206 printf("Sent Abort to other side\n");
12208 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12209 sizeof(msg_info.task), M_NOWAIT);
12212 printf("ctl_abort_task: found I/O to abort\n");
12216 mtx_unlock(&lun->lun_lock);
12220 * This isn't really an error. It's entirely possible for
12221 * the abort and command completion to cross on the wire.
12222 * This is more of an informative/diagnostic error.
12225 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12226 "%u:%u:%u tag %d type %d\n",
12227 io->io_hdr.nexus.initid,
12228 io->io_hdr.nexus.targ_port,
12229 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12230 io->taskio.tag_type);
12233 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12238 ctl_query_task(union ctl_io *io, int task_set)
12241 struct ctl_lun *lun;
12242 struct ctl_softc *softc;
12246 softc = control_softc;
12247 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12248 mtx_lock(&softc->ctl_lock);
12249 if (targ_lun >= CTL_MAX_LUNS ||
12250 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12251 mtx_unlock(&softc->ctl_lock);
12252 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12255 mtx_lock(&lun->lun_lock);
12256 mtx_unlock(&softc->ctl_lock);
12257 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12258 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12260 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12261 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12262 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12265 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12270 mtx_unlock(&lun->lun_lock);
12272 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12274 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12279 ctl_query_async_event(union ctl_io *io)
12281 struct ctl_lun *lun;
12282 struct ctl_softc *softc;
12284 uint32_t targ_lun, initidx;
12286 softc = control_softc;
12287 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12288 mtx_lock(&softc->ctl_lock);
12289 if (targ_lun >= CTL_MAX_LUNS ||
12290 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12291 mtx_unlock(&softc->ctl_lock);
12292 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12295 mtx_lock(&lun->lun_lock);
12296 mtx_unlock(&softc->ctl_lock);
12297 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12298 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12299 mtx_unlock(&lun->lun_lock);
12300 if (ua != CTL_UA_NONE)
12301 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12303 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12308 ctl_run_task(union ctl_io *io)
12310 struct ctl_softc *softc = control_softc;
12313 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12314 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12315 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12316 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12317 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12318 switch (io->taskio.task_action) {
12319 case CTL_TASK_ABORT_TASK:
12320 retval = ctl_abort_task(io);
12322 case CTL_TASK_ABORT_TASK_SET:
12323 case CTL_TASK_CLEAR_TASK_SET:
12324 retval = ctl_abort_task_set(io);
12326 case CTL_TASK_CLEAR_ACA:
12328 case CTL_TASK_I_T_NEXUS_RESET:
12329 retval = ctl_i_t_nexus_reset(io);
12331 case CTL_TASK_LUN_RESET:
12332 retval = ctl_lun_reset(softc, io);
12334 case CTL_TASK_TARGET_RESET:
12335 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12337 case CTL_TASK_BUS_RESET:
12338 retval = ctl_bus_reset(softc, io);
12340 case CTL_TASK_PORT_LOGIN:
12342 case CTL_TASK_PORT_LOGOUT:
12344 case CTL_TASK_QUERY_TASK:
12345 retval = ctl_query_task(io, 0);
12347 case CTL_TASK_QUERY_TASK_SET:
12348 retval = ctl_query_task(io, 1);
12350 case CTL_TASK_QUERY_ASYNC_EVENT:
12351 retval = ctl_query_async_event(io);
12354 printf("%s: got unknown task management event %d\n",
12355 __func__, io->taskio.task_action);
12359 io->io_hdr.status = CTL_SUCCESS;
12361 io->io_hdr.status = CTL_ERROR;
12366 * For HA operation. Handle commands that come in from the other
12370 ctl_handle_isc(union ctl_io *io)
12372 struct ctl_softc *softc = control_softc;
12373 struct ctl_lun *lun;
12374 const struct ctl_cmd_entry *entry;
12377 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12378 switch (io->io_hdr.msg_type) {
12379 case CTL_MSG_SERIALIZE:
12380 ctl_serialize_other_sc_cmd(&io->scsiio);
12382 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12383 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12384 if (targ_lun >= CTL_MAX_LUNS ||
12385 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12389 mtx_lock(&lun->lun_lock);
12390 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12391 mtx_unlock(&lun->lun_lock);
12395 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12396 mtx_unlock(&lun->lun_lock);
12397 ctl_enqueue_rtr(io);
12399 case CTL_MSG_FINISH_IO:
12400 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12404 if (targ_lun >= CTL_MAX_LUNS ||
12405 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12409 mtx_lock(&lun->lun_lock);
12410 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12411 ctl_check_blocked(lun);
12412 mtx_unlock(&lun->lun_lock);
12415 case CTL_MSG_PERS_ACTION:
12416 ctl_hndl_per_res_out_on_other_sc(
12417 (union ctl_ha_msg *)&io->presio.pr_msg);
12420 case CTL_MSG_BAD_JUJU:
12423 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12424 ctl_datamove_remote(io);
12426 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12427 io->scsiio.be_move_done(io);
12429 case CTL_MSG_FAILOVER:
12430 ctl_failover_lun(io);
12434 printf("%s: Invalid message type %d\n",
12435 __func__, io->io_hdr.msg_type);
12444 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12445 * there is no match.
12447 static ctl_lun_error_pattern
12448 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12450 const struct ctl_cmd_entry *entry;
12451 ctl_lun_error_pattern filtered_pattern, pattern;
12453 pattern = desc->error_pattern;
12456 * XXX KDM we need more data passed into this function to match a
12457 * custom pattern, and we actually need to implement custom pattern
12460 if (pattern & CTL_LUN_PAT_CMD)
12461 return (CTL_LUN_PAT_CMD);
12463 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12464 return (CTL_LUN_PAT_ANY);
12466 entry = ctl_get_cmd_entry(ctsio, NULL);
12468 filtered_pattern = entry->pattern & pattern;
12471 * If the user requested specific flags in the pattern (e.g.
12472 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12475 * If the user did not specify any flags, it doesn't matter whether
12476 * or not the command supports the flags.
12478 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12479 (pattern & ~CTL_LUN_PAT_MASK))
12480 return (CTL_LUN_PAT_NONE);
12483 * If the user asked for a range check, see if the requested LBA
12484 * range overlaps with this command's LBA range.
12486 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12492 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12494 return (CTL_LUN_PAT_NONE);
12496 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12497 desc->lba_range.len, FALSE);
12499 * A "pass" means that the LBA ranges don't overlap, so
12500 * this doesn't match the user's range criteria.
12502 if (action == CTL_ACTION_PASS)
12503 return (CTL_LUN_PAT_NONE);
12506 return (filtered_pattern);
12510 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12512 struct ctl_error_desc *desc, *desc2;
12514 mtx_assert(&lun->lun_lock, MA_OWNED);
12516 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12517 ctl_lun_error_pattern pattern;
12519 * Check to see whether this particular command matches
12520 * the pattern in the descriptor.
12522 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12523 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12526 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12527 case CTL_LUN_INJ_ABORTED:
12528 ctl_set_aborted(&io->scsiio);
12530 case CTL_LUN_INJ_MEDIUM_ERR:
12531 ctl_set_medium_error(&io->scsiio,
12532 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12533 CTL_FLAG_DATA_OUT);
12535 case CTL_LUN_INJ_UA:
12536 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12538 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12540 case CTL_LUN_INJ_CUSTOM:
12542 * We're assuming the user knows what he is doing.
12543 * Just copy the sense information without doing
12546 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12547 MIN(sizeof(desc->custom_sense),
12548 sizeof(io->scsiio.sense_data)));
12549 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12550 io->scsiio.sense_len = SSD_FULL_SIZE;
12551 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12553 case CTL_LUN_INJ_NONE:
12556 * If this is an error injection type we don't know
12557 * about, clear the continuous flag (if it is set)
12558 * so it will get deleted below.
12560 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12564 * By default, each error injection action is a one-shot
12566 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12569 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12575 #ifdef CTL_IO_DELAY
12577 ctl_datamove_timer_wakeup(void *arg)
12581 io = (union ctl_io *)arg;
12585 #endif /* CTL_IO_DELAY */
12588 ctl_datamove(union ctl_io *io)
12590 struct ctl_lun *lun;
12591 void (*fe_datamove)(union ctl_io *io);
12593 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12595 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12597 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12599 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12604 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12605 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12607 sbuf_cat(&sb, path_str);
12608 switch (io->io_hdr.io_type) {
12610 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12611 sbuf_printf(&sb, "\n");
12612 sbuf_cat(&sb, path_str);
12613 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12614 io->scsiio.tag_num, io->scsiio.tag_type);
12617 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12618 "Tag Type: %d\n", io->taskio.task_action,
12619 io->taskio.tag_num, io->taskio.tag_type);
12622 panic("%s: Invalid CTL I/O type %d\n",
12623 __func__, io->io_hdr.io_type);
12625 sbuf_cat(&sb, path_str);
12626 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12627 (intmax_t)time_uptime - io->io_hdr.start_time);
12629 printf("%s", sbuf_data(&sb));
12631 #endif /* CTL_TIME_IO */
12633 #ifdef CTL_IO_DELAY
12634 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12635 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12638 && (lun->delay_info.datamove_delay > 0)) {
12640 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12641 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12642 callout_reset(&io->io_hdr.delay_callout,
12643 lun->delay_info.datamove_delay * hz,
12644 ctl_datamove_timer_wakeup, io);
12645 if (lun->delay_info.datamove_type ==
12646 CTL_DELAY_TYPE_ONESHOT)
12647 lun->delay_info.datamove_delay = 0;
12654 * This command has been aborted. Set the port status, so we fail
12657 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12658 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12659 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12660 io->io_hdr.nexus.targ_port,
12661 io->io_hdr.nexus.targ_lun);
12662 io->io_hdr.port_status = 31337;
12664 * Note that the backend, in this case, will get the
12665 * callback in its context. In other cases it may get
12666 * called in the frontend's interrupt thread context.
12668 io->scsiio.be_move_done(io);
12672 /* Don't confuse frontend with zero length data move. */
12673 if (io->scsiio.kern_data_len == 0) {
12674 io->scsiio.be_move_done(io);
12678 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12683 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12685 union ctl_ha_msg msg;
12687 struct bintime cur_bt;
12690 memset(&msg, 0, sizeof(msg));
12691 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12692 msg.hdr.original_sc = io;
12693 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12694 msg.hdr.nexus = io->io_hdr.nexus;
12695 msg.hdr.status = io->io_hdr.status;
12696 msg.scsi.tag_num = io->scsiio.tag_num;
12697 msg.scsi.tag_type = io->scsiio.tag_type;
12698 msg.scsi.scsi_status = io->scsiio.scsi_status;
12699 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12700 io->scsiio.sense_len);
12701 msg.scsi.sense_len = io->scsiio.sense_len;
12702 msg.scsi.sense_residual = io->scsiio.sense_residual;
12703 msg.scsi.fetd_status = io->io_hdr.port_status;
12704 msg.scsi.residual = io->scsiio.residual;
12705 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12706 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12707 ctl_failover_io(io, /*have_lock*/ have_lock);
12710 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12711 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12712 msg.scsi.sense_len, M_WAITOK);
12715 getbinuptime(&cur_bt);
12716 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12717 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12719 io->io_hdr.num_dmas++;
12723 * The DMA to the remote side is done, now we need to tell the other side
12724 * we're done so it can continue with its data movement.
12727 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12734 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12735 printf("%s: ISC DMA write failed with error %d", __func__,
12737 ctl_set_internal_failure(&io->scsiio,
12739 /*retry_count*/ rq->ret);
12742 ctl_dt_req_free(rq);
12744 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12745 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12746 free(io->io_hdr.remote_sglist, M_CTL);
12747 io->io_hdr.remote_sglist = NULL;
12748 io->io_hdr.local_sglist = NULL;
12751 * The data is in local and remote memory, so now we need to send
12752 * status (good or back) back to the other side.
12754 ctl_send_datamove_done(io, /*have_lock*/ 0);
12758 * We've moved the data from the host/controller into local memory. Now we
12759 * need to push it over to the remote controller's memory.
12762 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12766 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12767 ctl_datamove_remote_write_cb);
12772 ctl_datamove_remote_write(union ctl_io *io)
12775 void (*fe_datamove)(union ctl_io *io);
12778 * - Get the data from the host/HBA into local memory.
12779 * - DMA memory from the local controller to the remote controller.
12780 * - Send status back to the remote controller.
12783 retval = ctl_datamove_remote_sgl_setup(io);
12787 /* Switch the pointer over so the FETD knows what to do */
12788 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12791 * Use a custom move done callback, since we need to send completion
12792 * back to the other controller, not to the backend on this side.
12794 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12796 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12801 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12810 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12811 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12812 free(io->io_hdr.remote_sglist, M_CTL);
12813 io->io_hdr.remote_sglist = NULL;
12814 io->io_hdr.local_sglist = NULL;
12817 scsi_path_string(io, path_str, sizeof(path_str));
12818 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12819 sbuf_cat(&sb, path_str);
12820 scsi_command_string(&io->scsiio, NULL, &sb);
12821 sbuf_printf(&sb, "\n");
12822 sbuf_cat(&sb, path_str);
12823 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12824 io->scsiio.tag_num, io->scsiio.tag_type);
12825 sbuf_cat(&sb, path_str);
12826 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12827 io->io_hdr.flags, io->io_hdr.status);
12829 printk("%s", sbuf_data(&sb));
12834 * The read is done, now we need to send status (good or bad) back
12835 * to the other side.
12837 ctl_send_datamove_done(io, /*have_lock*/ 0);
12843 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12846 void (*fe_datamove)(union ctl_io *io);
12850 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12851 printf("%s: ISC DMA read failed with error %d\n", __func__,
12853 ctl_set_internal_failure(&io->scsiio,
12855 /*retry_count*/ rq->ret);
12858 ctl_dt_req_free(rq);
12860 /* Switch the pointer over so the FETD knows what to do */
12861 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12864 * Use a custom move done callback, since we need to send completion
12865 * back to the other controller, not to the backend on this side.
12867 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12869 /* XXX KDM add checks like the ones in ctl_datamove? */
12871 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12876 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12878 struct ctl_sg_entry *local_sglist;
12879 uint32_t len_to_go;
12884 local_sglist = io->io_hdr.local_sglist;
12885 len_to_go = io->scsiio.kern_data_len;
12888 * The difficult thing here is that the size of the various
12889 * S/G segments may be different than the size from the
12890 * remote controller. That'll make it harder when DMAing
12891 * the data back to the other side.
12893 for (i = 0; len_to_go > 0; i++) {
12894 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12895 local_sglist[i].addr =
12896 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12898 len_to_go -= local_sglist[i].len;
12901 * Reset the number of S/G entries accordingly. The original
12902 * number of S/G entries is available in rem_sg_entries.
12904 io->scsiio.kern_sg_entries = i;
12907 printf("%s: kern_sg_entries = %d\n", __func__,
12908 io->scsiio.kern_sg_entries);
12909 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12910 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12911 local_sglist[i].addr, local_sglist[i].len);
12918 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12919 ctl_ha_dt_cb callback)
12921 struct ctl_ha_dt_req *rq;
12922 struct ctl_sg_entry *remote_sglist, *local_sglist;
12923 uint32_t local_used, remote_used, total_used;
12926 rq = ctl_dt_req_alloc();
12929 * If we failed to allocate the request, and if the DMA didn't fail
12930 * anyway, set busy status. This is just a resource allocation
12934 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12935 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12936 ctl_set_busy(&io->scsiio);
12938 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12939 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12942 ctl_dt_req_free(rq);
12945 * The data move failed. We need to return status back
12946 * to the other controller. No point in trying to DMA
12947 * data to the remote controller.
12950 ctl_send_datamove_done(io, /*have_lock*/ 0);
12955 local_sglist = io->io_hdr.local_sglist;
12956 remote_sglist = io->io_hdr.remote_sglist;
12962 * Pull/push the data over the wire from/to the other controller.
12963 * This takes into account the possibility that the local and
12964 * remote sglists may not be identical in terms of the size of
12965 * the elements and the number of elements.
12967 * One fundamental assumption here is that the length allocated for
12968 * both the local and remote sglists is identical. Otherwise, we've
12969 * essentially got a coding error of some sort.
12971 isc_ret = CTL_HA_STATUS_SUCCESS;
12972 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12976 rq->command = command;
12980 * Both pointers should be aligned. But it is possible
12981 * that the allocation length is not. They should both
12982 * also have enough slack left over at the end, though,
12983 * to round up to the next 8 byte boundary.
12985 cur_len = MIN(local_sglist[i].len - local_used,
12986 remote_sglist[j].len - remote_used);
12987 rq->size = cur_len;
12989 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12990 tmp_ptr += local_used;
12993 /* Use physical addresses when talking to ISC hardware */
12994 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12995 /* XXX KDM use busdma */
12996 rq->local = vtophys(tmp_ptr);
12998 rq->local = tmp_ptr;
13000 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
13001 ("HA does not support BUS_ADDR"));
13002 rq->local = tmp_ptr;
13005 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13006 tmp_ptr += remote_used;
13007 rq->remote = tmp_ptr;
13009 rq->callback = NULL;
13011 local_used += cur_len;
13012 if (local_used >= local_sglist[i].len) {
13017 remote_used += cur_len;
13018 if (remote_used >= remote_sglist[j].len) {
13022 total_used += cur_len;
13024 if (total_used >= io->scsiio.kern_data_len)
13025 rq->callback = callback;
13028 printf("%s: %s: local %p remote %p size %d\n", __func__,
13029 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13030 rq->local, rq->remote, rq->size);
13033 isc_ret = ctl_dt_single(rq);
13034 if (isc_ret > CTL_HA_STATUS_SUCCESS)
13037 if (isc_ret != CTL_HA_STATUS_WAIT) {
13046 ctl_datamove_remote_read(union ctl_io *io)
13052 * This will send an error to the other controller in the case of a
13055 retval = ctl_datamove_remote_sgl_setup(io);
13059 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13060 ctl_datamove_remote_read_cb);
13063 * Make sure we free memory if there was an error.. The
13064 * ctl_datamove_remote_xfer() function will send the
13065 * datamove done message, or call the callback with an
13066 * error if there is a problem.
13068 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13069 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13070 free(io->io_hdr.remote_sglist, M_CTL);
13071 io->io_hdr.remote_sglist = NULL;
13072 io->io_hdr.local_sglist = NULL;
13077 * Process a datamove request from the other controller. This is used for
13078 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13079 * first. Once that is complete, the data gets DMAed into the remote
13080 * controller's memory. For reads, we DMA from the remote controller's
13081 * memory into our memory first, and then move it out to the FETD.
13084 ctl_datamove_remote(union ctl_io *io)
13087 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
13089 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13090 ctl_failover_io(io, /*have_lock*/ 0);
13095 * Note that we look for an aborted I/O here, but don't do some of
13096 * the other checks that ctl_datamove() normally does.
13097 * We don't need to run the datamove delay code, since that should
13098 * have been done if need be on the other controller.
13100 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13101 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13102 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13103 io->io_hdr.nexus.targ_port,
13104 io->io_hdr.nexus.targ_lun);
13105 io->io_hdr.port_status = 31338;
13106 ctl_send_datamove_done(io, /*have_lock*/ 0);
13110 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13111 ctl_datamove_remote_write(io);
13112 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13113 ctl_datamove_remote_read(io);
13115 io->io_hdr.port_status = 31339;
13116 ctl_send_datamove_done(io, /*have_lock*/ 0);
13121 ctl_process_done(union ctl_io *io)
13123 struct ctl_lun *lun;
13124 struct ctl_softc *softc = control_softc;
13125 void (*fe_done)(union ctl_io *io);
13126 union ctl_ha_msg msg;
13127 uint32_t targ_port = io->io_hdr.nexus.targ_port;
13129 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13130 fe_done = softc->ctl_ports[targ_port]->fe_done;
13133 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13138 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13139 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13141 sbuf_cat(&sb, path_str);
13142 switch (io->io_hdr.io_type) {
13144 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13145 sbuf_printf(&sb, "\n");
13146 sbuf_cat(&sb, path_str);
13147 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13148 io->scsiio.tag_num, io->scsiio.tag_type);
13151 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13152 "Tag Type: %d\n", io->taskio.task_action,
13153 io->taskio.tag_num, io->taskio.tag_type);
13156 panic("%s: Invalid CTL I/O type %d\n",
13157 __func__, io->io_hdr.io_type);
13159 sbuf_cat(&sb, path_str);
13160 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13161 (intmax_t)time_uptime - io->io_hdr.start_time);
13163 printf("%s", sbuf_data(&sb));
13165 #endif /* CTL_TIME_IO */
13167 switch (io->io_hdr.io_type) {
13171 if (ctl_debug & CTL_DEBUG_INFO)
13172 ctl_io_error_print(io, NULL);
13176 panic("%s: Invalid CTL I/O type %d\n",
13177 __func__, io->io_hdr.io_type);
13180 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13182 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13183 io->io_hdr.nexus.targ_mapped_lun));
13187 mtx_lock(&lun->lun_lock);
13190 * Check to see if we have any informational exception and status
13191 * of this command can be modified to report it in form of either
13192 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13194 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13195 io->io_hdr.status == CTL_SUCCESS &&
13196 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13197 uint8_t mrie = lun->MODE_IE.mrie;
13198 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13199 (lun->MODE_VER.byte3 & SMS_VER_PER));
13200 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13201 mrie == SIEP_MRIE_REC_UNCOND ||
13202 mrie == SIEP_MRIE_NO_SENSE) &&
13203 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13204 CTL_CMD_FLAG_NO_SENSE) == 0) {
13205 ctl_set_sense(&io->scsiio,
13206 /*current_error*/ 1,
13207 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13208 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13209 /*asc*/ lun->ie_asc,
13210 /*ascq*/ lun->ie_ascq,
13212 lun->ie_reported = 1;
13214 } else if (lun->ie_reported < 0)
13215 lun->ie_reported = 0;
13218 * Check to see if we have any errors to inject here. We only
13219 * inject errors for commands that don't already have errors set.
13221 if (!STAILQ_EMPTY(&lun->error_list) &&
13222 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13223 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13224 ctl_inject_error(lun, io);
13227 * XXX KDM how do we treat commands that aren't completed
13230 * XXX KDM should we also track I/O latency?
13232 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13233 io->io_hdr.io_type == CTL_IO_SCSI) {
13235 struct bintime cur_bt;
13239 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13241 type = CTL_STATS_READ;
13242 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13244 type = CTL_STATS_WRITE;
13246 type = CTL_STATS_NO_IO;
13248 lun->stats.ports[targ_port].bytes[type] +=
13249 io->scsiio.kern_total_len;
13250 lun->stats.ports[targ_port].operations[type]++;
13252 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13253 &io->io_hdr.dma_bt);
13254 getbinuptime(&cur_bt);
13255 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13256 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13258 lun->stats.ports[targ_port].num_dmas[type] +=
13259 io->io_hdr.num_dmas;
13263 * Remove this from the OOA queue.
13265 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13267 if (TAILQ_EMPTY(&lun->ooa_queue))
13268 lun->last_busy = getsbinuptime();
13272 * Run through the blocked queue on this LUN and see if anything
13273 * has become unblocked, now that this transaction is done.
13275 ctl_check_blocked(lun);
13278 * If the LUN has been invalidated, free it if there is nothing
13279 * left on its OOA queue.
13281 if ((lun->flags & CTL_LUN_INVALID)
13282 && TAILQ_EMPTY(&lun->ooa_queue)) {
13283 mtx_unlock(&lun->lun_lock);
13284 mtx_lock(&softc->ctl_lock);
13286 mtx_unlock(&softc->ctl_lock);
13288 mtx_unlock(&lun->lun_lock);
13293 * If this command has been aborted, make sure we set the status
13294 * properly. The FETD is responsible for freeing the I/O and doing
13295 * whatever it needs to do to clean up its state.
13297 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13298 ctl_set_task_aborted(&io->scsiio);
13301 * If enabled, print command error status.
13303 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13304 (ctl_debug & CTL_DEBUG_INFO) != 0)
13305 ctl_io_error_print(io, NULL);
13308 * Tell the FETD or the other shelf controller we're done with this
13309 * command. Note that only SCSI commands get to this point. Task
13310 * management commands are completed above.
13312 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13313 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13314 memset(&msg, 0, sizeof(msg));
13315 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13316 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13317 msg.hdr.nexus = io->io_hdr.nexus;
13318 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13319 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13328 * Front end should call this if it doesn't do autosense. When the request
13329 * sense comes back in from the initiator, we'll dequeue this and send it.
13332 ctl_queue_sense(union ctl_io *io)
13334 struct ctl_lun *lun;
13335 struct ctl_port *port;
13336 struct ctl_softc *softc;
13337 uint32_t initidx, targ_lun;
13339 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13341 softc = control_softc;
13342 port = ctl_io_port(&ctsio->io_hdr);
13343 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13346 * LUN lookup will likely move to the ctl_work_thread() once we
13347 * have our new queueing infrastructure (that doesn't put things on
13348 * a per-LUN queue initially). That is so that we can handle
13349 * things like an INQUIRY to a LUN that we don't have enabled. We
13350 * can't deal with that right now.
13351 * If we don't have a LUN for this, just toss the sense information.
13353 mtx_lock(&softc->ctl_lock);
13354 if (targ_lun >= CTL_MAX_LUNS ||
13355 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13356 mtx_unlock(&softc->ctl_lock);
13359 mtx_lock(&lun->lun_lock);
13360 mtx_unlock(&softc->ctl_lock);
13363 * Already have CA set for this LUN...toss the sense information.
13365 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13366 if (ctl_is_set(lun->have_ca, initidx)) {
13367 mtx_unlock(&lun->lun_lock);
13371 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13372 MIN(sizeof(lun->pending_sense[initidx]),
13373 sizeof(io->scsiio.sense_data)));
13374 ctl_set_mask(lun->have_ca, initidx);
13375 mtx_unlock(&lun->lun_lock);
13379 return (CTL_RETVAL_COMPLETE);
13384 * Primary command inlet from frontend ports. All SCSI and task I/O
13385 * requests must go through this function.
13388 ctl_queue(union ctl_io *io)
13390 struct ctl_port *port;
13392 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13395 io->io_hdr.start_time = time_uptime;
13396 getbinuptime(&io->io_hdr.start_bt);
13397 #endif /* CTL_TIME_IO */
13399 /* Map FE-specific LUN ID into global one. */
13400 port = ctl_io_port(&io->io_hdr);
13401 io->io_hdr.nexus.targ_mapped_lun =
13402 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13404 switch (io->io_hdr.io_type) {
13407 if (ctl_debug & CTL_DEBUG_CDB)
13409 ctl_enqueue_incoming(io);
13412 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13416 return (CTL_RETVAL_COMPLETE);
13419 #ifdef CTL_IO_DELAY
13421 ctl_done_timer_wakeup(void *arg)
13425 io = (union ctl_io *)arg;
13428 #endif /* CTL_IO_DELAY */
13431 ctl_serseq_done(union ctl_io *io)
13433 struct ctl_lun *lun;
13435 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13436 if (lun->be_lun == NULL ||
13437 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13439 mtx_lock(&lun->lun_lock);
13440 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13441 ctl_check_blocked(lun);
13442 mtx_unlock(&lun->lun_lock);
13446 ctl_done(union ctl_io *io)
13450 * Enable this to catch duplicate completion issues.
13453 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13454 printf("%s: type %d msg %d cdb %x iptl: "
13455 "%u:%u:%u tag 0x%04x "
13456 "flag %#x status %x\n",
13458 io->io_hdr.io_type,
13459 io->io_hdr.msg_type,
13461 io->io_hdr.nexus.initid,
13462 io->io_hdr.nexus.targ_port,
13463 io->io_hdr.nexus.targ_lun,
13464 (io->io_hdr.io_type ==
13466 io->taskio.tag_num :
13467 io->scsiio.tag_num,
13469 io->io_hdr.status);
13471 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13475 * This is an internal copy of an I/O, and should not go through
13476 * the normal done processing logic.
13478 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13481 #ifdef CTL_IO_DELAY
13482 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13483 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13485 struct ctl_lun *lun;
13487 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13490 && (lun->delay_info.done_delay > 0)) {
13492 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13493 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13494 callout_reset(&io->io_hdr.delay_callout,
13495 lun->delay_info.done_delay * hz,
13496 ctl_done_timer_wakeup, io);
13497 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13498 lun->delay_info.done_delay = 0;
13502 #endif /* CTL_IO_DELAY */
13504 ctl_enqueue_done(io);
13508 ctl_work_thread(void *arg)
13510 struct ctl_thread *thr = (struct ctl_thread *)arg;
13511 struct ctl_softc *softc = thr->ctl_softc;
13515 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13519 * We handle the queues in this order:
13521 * - done queue (to free up resources, unblock other commands)
13525 * If those queues are empty, we break out of the loop and
13528 mtx_lock(&thr->queue_lock);
13529 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13531 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13532 mtx_unlock(&thr->queue_lock);
13533 ctl_handle_isc(io);
13536 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13538 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13539 /* clear any blocked commands, call fe_done */
13540 mtx_unlock(&thr->queue_lock);
13541 ctl_process_done(io);
13544 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13546 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13547 mtx_unlock(&thr->queue_lock);
13548 if (io->io_hdr.io_type == CTL_IO_TASK)
13551 ctl_scsiio_precheck(softc, &io->scsiio);
13554 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13556 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13557 mtx_unlock(&thr->queue_lock);
13558 retval = ctl_scsiio(&io->scsiio);
13559 if (retval != CTL_RETVAL_COMPLETE)
13560 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13564 /* Sleep until we have something to do. */
13565 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13570 ctl_lun_thread(void *arg)
13572 struct ctl_softc *softc = (struct ctl_softc *)arg;
13573 struct ctl_be_lun *be_lun;
13575 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13578 mtx_lock(&softc->ctl_lock);
13579 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13580 if (be_lun != NULL) {
13581 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13582 mtx_unlock(&softc->ctl_lock);
13583 ctl_create_lun(be_lun);
13587 /* Sleep until we have something to do. */
13588 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13589 PDROP | PRIBIO, "-", 0);
13594 ctl_thresh_thread(void *arg)
13596 struct ctl_softc *softc = (struct ctl_softc *)arg;
13597 struct ctl_lun *lun;
13598 struct ctl_logical_block_provisioning_page *page;
13600 union ctl_ha_msg msg;
13601 uint64_t thres, val;
13604 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13607 mtx_lock(&softc->ctl_lock);
13608 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13609 if ((lun->flags & CTL_LUN_DISABLED) ||
13610 (lun->flags & CTL_LUN_NO_MEDIA) ||
13611 lun->backend->lun_attr == NULL)
13613 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13614 softc->ha_mode == CTL_HA_MODE_XFER)
13616 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13619 page = &lun->MODE_LBP;
13620 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13621 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13623 thres = scsi_4btoul(page->descr[i].count);
13624 thres <<= CTL_LBP_EXPONENT;
13625 switch (page->descr[i].resource) {
13627 attr = "blocksavail";
13630 attr = "blocksused";
13633 attr = "poolblocksavail";
13636 attr = "poolblocksused";
13641 mtx_unlock(&softc->ctl_lock); // XXX
13642 val = lun->backend->lun_attr(
13643 lun->be_lun->be_lun, attr);
13644 mtx_lock(&softc->ctl_lock);
13645 if (val == UINT64_MAX)
13647 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13648 == SLBPPD_ARMING_INC)
13649 e = (val >= thres);
13651 e = (val <= thres);
13655 mtx_lock(&lun->lun_lock);
13657 scsi_u64to8b((uint8_t *)&page->descr[i] -
13658 (uint8_t *)page, lun->ua_tpt_info);
13659 if (lun->lasttpt == 0 ||
13660 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13661 lun->lasttpt = time_uptime;
13662 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13668 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13671 mtx_unlock(&lun->lun_lock);
13673 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13674 /* Send msg to other side. */
13675 bzero(&msg.ua, sizeof(msg.ua));
13676 msg.hdr.msg_type = CTL_MSG_UA;
13677 msg.hdr.nexus.initid = -1;
13678 msg.hdr.nexus.targ_port = -1;
13679 msg.hdr.nexus.targ_lun = lun->lun;
13680 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13682 msg.ua.ua_set = (set > 0);
13683 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13684 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13685 mtx_unlock(&softc->ctl_lock); // XXX
13686 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13687 sizeof(msg.ua), M_WAITOK);
13688 mtx_lock(&softc->ctl_lock);
13691 mtx_unlock(&softc->ctl_lock);
13692 pause("-", CTL_LBP_PERIOD * hz);
13697 ctl_enqueue_incoming(union ctl_io *io)
13699 struct ctl_softc *softc = control_softc;
13700 struct ctl_thread *thr;
13703 idx = (io->io_hdr.nexus.targ_port * 127 +
13704 io->io_hdr.nexus.initid) % worker_threads;
13705 thr = &softc->threads[idx];
13706 mtx_lock(&thr->queue_lock);
13707 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13708 mtx_unlock(&thr->queue_lock);
13713 ctl_enqueue_rtr(union ctl_io *io)
13715 struct ctl_softc *softc = control_softc;
13716 struct ctl_thread *thr;
13718 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13719 mtx_lock(&thr->queue_lock);
13720 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13721 mtx_unlock(&thr->queue_lock);
13726 ctl_enqueue_done(union ctl_io *io)
13728 struct ctl_softc *softc = control_softc;
13729 struct ctl_thread *thr;
13731 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13732 mtx_lock(&thr->queue_lock);
13733 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13734 mtx_unlock(&thr->queue_lock);
13739 ctl_enqueue_isc(union ctl_io *io)
13741 struct ctl_softc *softc = control_softc;
13742 struct ctl_thread *thr;
13744 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13745 mtx_lock(&thr->queue_lock);
13746 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13747 mtx_unlock(&thr->queue_lock);