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");
414 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
415 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
416 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
417 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
419 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
421 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
423 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
424 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
425 static int ctl_init(void);
426 void ctl_shutdown(void);
427 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
428 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
429 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
430 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
431 struct ctl_ooa *ooa_hdr,
432 struct ctl_ooa_entry *kern_entries);
433 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
435 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
436 struct ctl_be_lun *be_lun);
437 static int ctl_free_lun(struct ctl_lun *lun);
438 static void ctl_create_lun(struct ctl_be_lun *be_lun);
439 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
441 static int ctl_do_mode_select(union ctl_io *io);
442 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
443 uint64_t res_key, uint64_t sa_res_key,
444 uint8_t type, uint32_t residx,
445 struct ctl_scsiio *ctsio,
446 struct scsi_per_res_out *cdb,
447 struct scsi_per_res_out_parms* param);
448 static void ctl_pro_preempt_other(struct ctl_lun *lun,
449 union ctl_ha_msg *msg);
450 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
451 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
452 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
453 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
454 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
455 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
456 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
458 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
460 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
461 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
462 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
463 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
464 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
465 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
467 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
468 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
469 union ctl_io *pending_io, union ctl_io *ooa_io);
470 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
471 union ctl_io *starting_io);
472 static int ctl_check_blocked(struct ctl_lun *lun);
473 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
474 const struct ctl_cmd_entry *entry,
475 struct ctl_scsiio *ctsio);
476 static void ctl_failover_lun(union ctl_io *io);
477 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
478 struct ctl_scsiio *ctsio);
479 static int ctl_scsiio(struct ctl_scsiio *ctsio);
481 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
482 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
483 ctl_ua_type ua_type);
484 static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
485 ctl_ua_type ua_type);
486 static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
487 static int ctl_abort_task(union ctl_io *io);
488 static int ctl_abort_task_set(union ctl_io *io);
489 static int ctl_query_task(union ctl_io *io, int task_set);
490 static int ctl_i_t_nexus_reset(union ctl_io *io);
491 static int ctl_query_async_event(union ctl_io *io);
492 static void ctl_run_task(union ctl_io *io);
494 static void ctl_datamove_timer_wakeup(void *arg);
495 static void ctl_done_timer_wakeup(void *arg);
496 #endif /* CTL_IO_DELAY */
498 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
499 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
500 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
501 static void ctl_datamove_remote_write(union ctl_io *io);
502 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
503 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
504 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
505 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
506 ctl_ha_dt_cb callback);
507 static void ctl_datamove_remote_read(union ctl_io *io);
508 static void ctl_datamove_remote(union ctl_io *io);
509 static void ctl_process_done(union ctl_io *io);
510 static void ctl_lun_thread(void *arg);
511 static void ctl_thresh_thread(void *arg);
512 static void ctl_work_thread(void *arg);
513 static void ctl_enqueue_incoming(union ctl_io *io);
514 static void ctl_enqueue_rtr(union ctl_io *io);
515 static void ctl_enqueue_done(union ctl_io *io);
516 static void ctl_enqueue_isc(union ctl_io *io);
517 static const struct ctl_cmd_entry *
518 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
519 static const struct ctl_cmd_entry *
520 ctl_validate_command(struct ctl_scsiio *ctsio);
521 static int ctl_cmd_applicable(uint8_t lun_type,
522 const struct ctl_cmd_entry *entry);
524 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
525 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
526 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
527 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
530 * Load the serialization table. This isn't very pretty, but is probably
531 * the easiest way to do it.
533 #include "ctl_ser_table.c"
536 * We only need to define open, close and ioctl routines for this driver.
538 static struct cdevsw ctl_cdevsw = {
539 .d_version = D_VERSION,
542 .d_close = ctl_close,
543 .d_ioctl = ctl_ioctl,
548 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
550 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
552 static moduledata_t ctl_moduledata = {
554 ctl_module_event_handler,
558 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
559 MODULE_VERSION(ctl, 1);
561 static struct ctl_frontend ha_frontend =
567 ctl_ha_datamove(union ctl_io *io)
570 struct ctl_sg_entry *sgl;
571 union ctl_ha_msg msg;
572 uint32_t sg_entries_sent;
573 int do_sg_copy, i, j;
575 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
576 memset(&msg.dt, 0, sizeof(msg.dt));
577 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
578 msg.hdr.original_sc = io->io_hdr.original_sc;
579 msg.hdr.serializing_sc = io;
580 msg.hdr.nexus = io->io_hdr.nexus;
581 msg.hdr.status = io->io_hdr.status;
582 msg.dt.flags = io->io_hdr.flags;
585 * We convert everything into a S/G list here. We can't
586 * pass by reference, only by value between controllers.
587 * So we can't pass a pointer to the S/G list, only as many
588 * S/G entries as we can fit in here. If it's possible for
589 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
590 * then we need to break this up into multiple transfers.
592 if (io->scsiio.kern_sg_entries == 0) {
593 msg.dt.kern_sg_entries = 1;
595 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
596 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
598 /* XXX KDM use busdma here! */
599 msg.dt.sg_list[0].addr =
600 (void *)vtophys(io->scsiio.kern_data_ptr);
603 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
604 ("HA does not support BUS_ADDR"));
605 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
607 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
610 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
614 msg.dt.kern_data_len = io->scsiio.kern_data_len;
615 msg.dt.kern_total_len = io->scsiio.kern_total_len;
616 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
617 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
618 msg.dt.sg_sequence = 0;
621 * Loop until we've sent all of the S/G entries. On the
622 * other end, we'll recompose these S/G entries into one
623 * contiguous list before processing.
625 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
626 msg.dt.sg_sequence++) {
627 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
628 sizeof(msg.dt.sg_list[0])),
629 msg.dt.kern_sg_entries - sg_entries_sent);
630 if (do_sg_copy != 0) {
631 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
632 for (i = sg_entries_sent, j = 0;
633 i < msg.dt.cur_sg_entries; i++, j++) {
635 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
636 msg.dt.sg_list[j].addr = sgl[i].addr;
638 /* XXX KDM use busdma here! */
639 msg.dt.sg_list[j].addr =
640 (void *)vtophys(sgl[i].addr);
643 KASSERT((io->io_hdr.flags &
644 CTL_FLAG_BUS_ADDR) == 0,
645 ("HA does not support BUS_ADDR"));
646 msg.dt.sg_list[j].addr = sgl[i].addr;
648 msg.dt.sg_list[j].len = sgl[i].len;
652 sg_entries_sent += msg.dt.cur_sg_entries;
653 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
654 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
655 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
656 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
657 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
658 io->io_hdr.port_status = 31341;
659 io->scsiio.be_move_done(io);
662 msg.dt.sent_sg_entries = sg_entries_sent;
666 * Officially handover the request from us to peer.
667 * If failover has just happened, then we must return error.
668 * If failover happen just after, then it is not our problem.
671 mtx_lock(&lun->lun_lock);
672 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
674 mtx_unlock(&lun->lun_lock);
675 io->io_hdr.port_status = 31342;
676 io->scsiio.be_move_done(io);
679 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
680 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
682 mtx_unlock(&lun->lun_lock);
686 ctl_ha_done(union ctl_io *io)
688 union ctl_ha_msg msg;
690 if (io->io_hdr.io_type == CTL_IO_SCSI) {
691 memset(&msg, 0, sizeof(msg));
692 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
693 msg.hdr.original_sc = io->io_hdr.original_sc;
694 msg.hdr.nexus = io->io_hdr.nexus;
695 msg.hdr.status = io->io_hdr.status;
696 msg.scsi.scsi_status = io->scsiio.scsi_status;
697 msg.scsi.tag_num = io->scsiio.tag_num;
698 msg.scsi.tag_type = io->scsiio.tag_type;
699 msg.scsi.sense_len = io->scsiio.sense_len;
700 msg.scsi.sense_residual = io->scsiio.sense_residual;
701 msg.scsi.residual = io->scsiio.residual;
702 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
703 io->scsiio.sense_len);
704 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
705 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
706 msg.scsi.sense_len, M_WAITOK);
712 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
713 union ctl_ha_msg *msg_info)
715 struct ctl_scsiio *ctsio;
717 if (msg_info->hdr.original_sc == NULL) {
718 printf("%s: original_sc == NULL!\n", __func__);
719 /* XXX KDM now what? */
723 ctsio = &msg_info->hdr.original_sc->scsiio;
724 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
725 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
726 ctsio->io_hdr.status = msg_info->hdr.status;
727 ctsio->scsi_status = msg_info->scsi.scsi_status;
728 ctsio->sense_len = msg_info->scsi.sense_len;
729 ctsio->sense_residual = msg_info->scsi.sense_residual;
730 ctsio->residual = msg_info->scsi.residual;
731 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
732 msg_info->scsi.sense_len);
733 ctl_enqueue_isc((union ctl_io *)ctsio);
737 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
738 union ctl_ha_msg *msg_info)
740 struct ctl_scsiio *ctsio;
742 if (msg_info->hdr.serializing_sc == NULL) {
743 printf("%s: serializing_sc == NULL!\n", __func__);
744 /* XXX KDM now what? */
748 ctsio = &msg_info->hdr.serializing_sc->scsiio;
749 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
750 ctl_enqueue_isc((union ctl_io *)ctsio);
754 ctl_isc_announce_lun(struct ctl_lun *lun)
756 struct ctl_softc *softc = lun->ctl_softc;
757 union ctl_ha_msg *msg;
758 struct ctl_ha_msg_lun_pr_key pr_key;
761 if (softc->ha_link != CTL_HA_LINK_ONLINE)
763 mtx_lock(&lun->lun_lock);
764 i = sizeof(msg->lun);
766 i += lun->lun_devid->len;
767 i += sizeof(pr_key) * lun->pr_key_count;
769 mtx_unlock(&lun->lun_lock);
770 msg = malloc(i, M_CTL, M_WAITOK);
771 mtx_lock(&lun->lun_lock);
772 k = sizeof(msg->lun);
774 k += lun->lun_devid->len;
775 k += sizeof(pr_key) * lun->pr_key_count;
781 bzero(&msg->lun, sizeof(msg->lun));
782 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
783 msg->hdr.nexus.targ_lun = lun->lun;
784 msg->hdr.nexus.targ_mapped_lun = lun->lun;
785 msg->lun.flags = lun->flags;
786 msg->lun.pr_generation = lun->pr_generation;
787 msg->lun.pr_res_idx = lun->pr_res_idx;
788 msg->lun.pr_res_type = lun->pr_res_type;
789 msg->lun.pr_key_count = lun->pr_key_count;
791 if (lun->lun_devid) {
792 msg->lun.lun_devid_len = lun->lun_devid->len;
793 memcpy(&msg->lun.data[i], lun->lun_devid->data,
794 msg->lun.lun_devid_len);
795 i += msg->lun.lun_devid_len;
797 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
798 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
801 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
804 mtx_unlock(&lun->lun_lock);
805 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
809 if (lun->flags & CTL_LUN_PRIMARY_SC) {
810 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
811 ctl_isc_announce_mode(lun, -1,
812 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
813 lun->mode_pages.index[i].subpage);
819 ctl_isc_announce_port(struct ctl_port *port)
821 struct ctl_softc *softc = port->ctl_softc;
822 union ctl_ha_msg *msg;
825 if (port->targ_port < softc->port_min ||
826 port->targ_port >= softc->port_max ||
827 softc->ha_link != CTL_HA_LINK_ONLINE)
829 i = sizeof(msg->port) + strlen(port->port_name) + 1;
831 i += sizeof(uint32_t) * CTL_MAX_LUNS;
832 if (port->port_devid)
833 i += port->port_devid->len;
834 if (port->target_devid)
835 i += port->target_devid->len;
836 if (port->init_devid)
837 i += port->init_devid->len;
838 msg = malloc(i, M_CTL, M_WAITOK);
839 bzero(&msg->port, sizeof(msg->port));
840 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
841 msg->hdr.nexus.targ_port = port->targ_port;
842 msg->port.port_type = port->port_type;
843 msg->port.physical_port = port->physical_port;
844 msg->port.virtual_port = port->virtual_port;
845 msg->port.status = port->status;
847 msg->port.name_len = sprintf(&msg->port.data[i],
848 "%d:%s", softc->ha_id, port->port_name) + 1;
849 i += msg->port.name_len;
851 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS;
852 memcpy(&msg->port.data[i], port->lun_map,
853 msg->port.lun_map_len);
854 i += msg->port.lun_map_len;
856 if (port->port_devid) {
857 msg->port.port_devid_len = port->port_devid->len;
858 memcpy(&msg->port.data[i], port->port_devid->data,
859 msg->port.port_devid_len);
860 i += msg->port.port_devid_len;
862 if (port->target_devid) {
863 msg->port.target_devid_len = port->target_devid->len;
864 memcpy(&msg->port.data[i], port->target_devid->data,
865 msg->port.target_devid_len);
866 i += msg->port.target_devid_len;
868 if (port->init_devid) {
869 msg->port.init_devid_len = port->init_devid->len;
870 memcpy(&msg->port.data[i], port->init_devid->data,
871 msg->port.init_devid_len);
872 i += msg->port.init_devid_len;
874 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
880 ctl_isc_announce_iid(struct ctl_port *port, int iid)
882 struct ctl_softc *softc = port->ctl_softc;
883 union ctl_ha_msg *msg;
886 if (port->targ_port < softc->port_min ||
887 port->targ_port >= softc->port_max ||
888 softc->ha_link != CTL_HA_LINK_ONLINE)
890 mtx_lock(&softc->ctl_lock);
891 i = sizeof(msg->iid);
893 if (port->wwpn_iid[iid].name)
894 l = strlen(port->wwpn_iid[iid].name) + 1;
896 msg = malloc(i, M_CTL, M_NOWAIT);
898 mtx_unlock(&softc->ctl_lock);
901 bzero(&msg->iid, sizeof(msg->iid));
902 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
903 msg->hdr.nexus.targ_port = port->targ_port;
904 msg->hdr.nexus.initid = iid;
905 msg->iid.in_use = port->wwpn_iid[iid].in_use;
906 msg->iid.name_len = l;
907 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
908 if (port->wwpn_iid[iid].name)
909 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
910 mtx_unlock(&softc->ctl_lock);
911 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
916 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
917 uint8_t page, uint8_t subpage)
919 struct ctl_softc *softc = lun->ctl_softc;
920 union ctl_ha_msg msg;
923 if (softc->ha_link != CTL_HA_LINK_ONLINE)
925 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
926 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
927 page && lun->mode_pages.index[i].subpage == subpage)
930 if (i == CTL_NUM_MODE_PAGES)
933 /* Don't try to replicate pages not present on this device. */
934 if (lun->mode_pages.index[i].page_data == NULL)
937 bzero(&msg.mode, sizeof(msg.mode));
938 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
939 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
940 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
941 msg.hdr.nexus.targ_lun = lun->lun;
942 msg.hdr.nexus.targ_mapped_lun = lun->lun;
943 msg.mode.page_code = page;
944 msg.mode.subpage = subpage;
945 msg.mode.page_len = lun->mode_pages.index[i].page_len;
946 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
948 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
953 ctl_isc_ha_link_up(struct ctl_softc *softc)
955 struct ctl_port *port;
957 union ctl_ha_msg msg;
960 /* Announce this node parameters to peer for validation. */
961 msg.login.msg_type = CTL_MSG_LOGIN;
962 msg.login.version = CTL_HA_VERSION;
963 msg.login.ha_mode = softc->ha_mode;
964 msg.login.ha_id = softc->ha_id;
965 msg.login.max_luns = CTL_MAX_LUNS;
966 msg.login.max_ports = CTL_MAX_PORTS;
967 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
968 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
971 STAILQ_FOREACH(port, &softc->port_list, links) {
972 ctl_isc_announce_port(port);
973 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
974 if (port->wwpn_iid[i].in_use)
975 ctl_isc_announce_iid(port, i);
978 STAILQ_FOREACH(lun, &softc->lun_list, links)
979 ctl_isc_announce_lun(lun);
983 ctl_isc_ha_link_down(struct ctl_softc *softc)
985 struct ctl_port *port;
990 mtx_lock(&softc->ctl_lock);
991 STAILQ_FOREACH(lun, &softc->lun_list, links) {
992 mtx_lock(&lun->lun_lock);
993 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
994 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
995 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
997 mtx_unlock(&lun->lun_lock);
999 mtx_unlock(&softc->ctl_lock);
1000 io = ctl_alloc_io(softc->othersc_pool);
1001 mtx_lock(&softc->ctl_lock);
1003 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1004 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1005 ctl_enqueue_isc(io);
1008 STAILQ_FOREACH(port, &softc->port_list, links) {
1009 if (port->targ_port >= softc->port_min &&
1010 port->targ_port < softc->port_max)
1012 port->status &= ~CTL_PORT_STATUS_ONLINE;
1013 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1014 port->wwpn_iid[i].in_use = 0;
1015 free(port->wwpn_iid[i].name, M_CTL);
1016 port->wwpn_iid[i].name = NULL;
1019 mtx_unlock(&softc->ctl_lock);
1023 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1025 struct ctl_lun *lun;
1026 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1028 mtx_lock(&softc->ctl_lock);
1029 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
1030 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
1031 mtx_lock(&lun->lun_lock);
1032 mtx_unlock(&softc->ctl_lock);
1033 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES &&
1035 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1036 if (msg->ua.ua_all) {
1038 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1040 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1043 ctl_est_ua(lun, iid, msg->ua.ua_type);
1045 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1047 mtx_unlock(&lun->lun_lock);
1049 mtx_unlock(&softc->ctl_lock);
1053 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1055 struct ctl_lun *lun;
1056 struct ctl_ha_msg_lun_pr_key pr_key;
1058 ctl_lun_flags oflags;
1061 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1062 mtx_lock(&softc->ctl_lock);
1063 if ((targ_lun >= CTL_MAX_LUNS) ||
1064 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1065 mtx_unlock(&softc->ctl_lock);
1068 mtx_lock(&lun->lun_lock);
1069 mtx_unlock(&softc->ctl_lock);
1070 if (lun->flags & CTL_LUN_DISABLED) {
1071 mtx_unlock(&lun->lun_lock);
1074 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1075 if (msg->lun.lun_devid_len != i || (i > 0 &&
1076 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1077 mtx_unlock(&lun->lun_lock);
1078 printf("%s: Received conflicting HA LUN %d\n",
1079 __func__, msg->hdr.nexus.targ_lun);
1082 /* Record whether peer is primary. */
1083 oflags = lun->flags;
1084 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1085 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1086 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1088 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1089 if (oflags != lun->flags)
1090 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1092 /* If peer is primary and we are not -- use data */
1093 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1094 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1095 lun->pr_generation = msg->lun.pr_generation;
1096 lun->pr_res_idx = msg->lun.pr_res_idx;
1097 lun->pr_res_type = msg->lun.pr_res_type;
1098 lun->pr_key_count = msg->lun.pr_key_count;
1099 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1100 ctl_clr_prkey(lun, k);
1101 for (k = 0; k < msg->lun.pr_key_count; k++) {
1102 memcpy(&pr_key, &msg->lun.data[i],
1104 ctl_alloc_prkey(lun, pr_key.pr_iid);
1105 ctl_set_prkey(lun, pr_key.pr_iid,
1107 i += sizeof(pr_key);
1111 mtx_unlock(&lun->lun_lock);
1112 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1113 __func__, msg->hdr.nexus.targ_lun,
1114 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1115 "primary" : "secondary"));
1117 /* If we are primary but peer doesn't know -- notify */
1118 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1119 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1120 ctl_isc_announce_lun(lun);
1125 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1127 struct ctl_port *port;
1128 struct ctl_lun *lun;
1131 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1133 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1134 msg->hdr.nexus.targ_port));
1136 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1137 port->frontend = &ha_frontend;
1138 port->targ_port = msg->hdr.nexus.targ_port;
1139 port->fe_datamove = ctl_ha_datamove;
1140 port->fe_done = ctl_ha_done;
1141 } else if (port->frontend == &ha_frontend) {
1142 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1143 msg->hdr.nexus.targ_port));
1146 printf("%s: Received conflicting HA port %d\n",
1147 __func__, msg->hdr.nexus.targ_port);
1150 port->port_type = msg->port.port_type;
1151 port->physical_port = msg->port.physical_port;
1152 port->virtual_port = msg->port.virtual_port;
1153 port->status = msg->port.status;
1155 free(port->port_name, M_CTL);
1156 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1158 i += msg->port.name_len;
1159 if (msg->port.lun_map_len != 0) {
1160 if (port->lun_map == NULL)
1161 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
1163 memcpy(port->lun_map, &msg->port.data[i],
1164 sizeof(uint32_t) * CTL_MAX_LUNS);
1165 i += msg->port.lun_map_len;
1167 free(port->lun_map, M_CTL);
1168 port->lun_map = NULL;
1170 if (msg->port.port_devid_len != 0) {
1171 if (port->port_devid == NULL ||
1172 port->port_devid->len != msg->port.port_devid_len) {
1173 free(port->port_devid, M_CTL);
1174 port->port_devid = malloc(sizeof(struct ctl_devid) +
1175 msg->port.port_devid_len, M_CTL, M_WAITOK);
1177 memcpy(port->port_devid->data, &msg->port.data[i],
1178 msg->port.port_devid_len);
1179 port->port_devid->len = msg->port.port_devid_len;
1180 i += msg->port.port_devid_len;
1182 free(port->port_devid, M_CTL);
1183 port->port_devid = NULL;
1185 if (msg->port.target_devid_len != 0) {
1186 if (port->target_devid == NULL ||
1187 port->target_devid->len != msg->port.target_devid_len) {
1188 free(port->target_devid, M_CTL);
1189 port->target_devid = malloc(sizeof(struct ctl_devid) +
1190 msg->port.target_devid_len, M_CTL, M_WAITOK);
1192 memcpy(port->target_devid->data, &msg->port.data[i],
1193 msg->port.target_devid_len);
1194 port->target_devid->len = msg->port.target_devid_len;
1195 i += msg->port.target_devid_len;
1197 free(port->target_devid, M_CTL);
1198 port->target_devid = NULL;
1200 if (msg->port.init_devid_len != 0) {
1201 if (port->init_devid == NULL ||
1202 port->init_devid->len != msg->port.init_devid_len) {
1203 free(port->init_devid, M_CTL);
1204 port->init_devid = malloc(sizeof(struct ctl_devid) +
1205 msg->port.init_devid_len, M_CTL, M_WAITOK);
1207 memcpy(port->init_devid->data, &msg->port.data[i],
1208 msg->port.init_devid_len);
1209 port->init_devid->len = msg->port.init_devid_len;
1210 i += msg->port.init_devid_len;
1212 free(port->init_devid, M_CTL);
1213 port->init_devid = NULL;
1216 if (ctl_port_register(port) != 0) {
1217 printf("%s: ctl_port_register() failed with error\n",
1221 mtx_lock(&softc->ctl_lock);
1222 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1223 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
1225 mtx_lock(&lun->lun_lock);
1226 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1227 mtx_unlock(&lun->lun_lock);
1229 mtx_unlock(&softc->ctl_lock);
1233 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1235 struct ctl_port *port;
1238 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1240 printf("%s: Received IID for unknown port %d\n",
1241 __func__, msg->hdr.nexus.targ_port);
1244 iid = msg->hdr.nexus.initid;
1245 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1246 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1247 free(port->wwpn_iid[iid].name, M_CTL);
1248 if (msg->iid.name_len) {
1249 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1250 msg->iid.name_len, M_CTL);
1252 port->wwpn_iid[iid].name = NULL;
1256 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1259 if (msg->login.version != CTL_HA_VERSION) {
1260 printf("CTL HA peers have different versions %d != %d\n",
1261 msg->login.version, CTL_HA_VERSION);
1262 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1265 if (msg->login.ha_mode != softc->ha_mode) {
1266 printf("CTL HA peers have different ha_mode %d != %d\n",
1267 msg->login.ha_mode, softc->ha_mode);
1268 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1271 if (msg->login.ha_id == softc->ha_id) {
1272 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1273 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1276 if (msg->login.max_luns != CTL_MAX_LUNS ||
1277 msg->login.max_ports != CTL_MAX_PORTS ||
1278 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1279 printf("CTL HA peers have different limits\n");
1280 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1286 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1288 struct ctl_lun *lun;
1290 uint32_t initidx, targ_lun;
1292 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1293 mtx_lock(&softc->ctl_lock);
1294 if ((targ_lun >= CTL_MAX_LUNS) ||
1295 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1296 mtx_unlock(&softc->ctl_lock);
1299 mtx_lock(&lun->lun_lock);
1300 mtx_unlock(&softc->ctl_lock);
1301 if (lun->flags & CTL_LUN_DISABLED) {
1302 mtx_unlock(&lun->lun_lock);
1305 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1306 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1307 msg->mode.page_code &&
1308 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1311 if (i == CTL_NUM_MODE_PAGES) {
1312 mtx_unlock(&lun->lun_lock);
1315 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1316 lun->mode_pages.index[i].page_len);
1317 initidx = ctl_get_initindex(&msg->hdr.nexus);
1319 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1320 mtx_unlock(&lun->lun_lock);
1324 * ISC (Inter Shelf Communication) event handler. Events from the HA
1325 * subsystem come in here.
1328 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1330 struct ctl_softc *softc = control_softc;
1332 struct ctl_prio *presio;
1333 ctl_ha_status isc_status;
1335 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1336 if (event == CTL_HA_EVT_MSG_RECV) {
1337 union ctl_ha_msg *msg, msgbuf;
1339 if (param > sizeof(msgbuf))
1340 msg = malloc(param, M_CTL, M_WAITOK);
1343 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1345 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1346 printf("%s: Error receiving message: %d\n",
1347 __func__, isc_status);
1353 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1354 switch (msg->hdr.msg_type) {
1355 case CTL_MSG_SERIALIZE:
1356 io = ctl_alloc_io(softc->othersc_pool);
1358 // populate ctsio from msg
1359 io->io_hdr.io_type = CTL_IO_SCSI;
1360 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1361 io->io_hdr.original_sc = msg->hdr.original_sc;
1362 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1365 * If we're in serialization-only mode, we don't
1366 * want to go through full done processing. Thus
1369 * XXX KDM add another flag that is more specific.
1371 if (softc->ha_mode != CTL_HA_MODE_XFER)
1372 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1373 io->io_hdr.nexus = msg->hdr.nexus;
1375 printf("port %u, iid %u, lun %u\n",
1376 io->io_hdr.nexus.targ_port,
1377 io->io_hdr.nexus.initid,
1378 io->io_hdr.nexus.targ_lun);
1380 io->scsiio.tag_num = msg->scsi.tag_num;
1381 io->scsiio.tag_type = msg->scsi.tag_type;
1383 io->io_hdr.start_time = time_uptime;
1384 getbinuptime(&io->io_hdr.start_bt);
1385 #endif /* CTL_TIME_IO */
1386 io->scsiio.cdb_len = msg->scsi.cdb_len;
1387 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1389 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1390 const struct ctl_cmd_entry *entry;
1392 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1393 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1395 entry->flags & CTL_FLAG_DATA_MASK;
1397 ctl_enqueue_isc(io);
1400 /* Performed on the Originating SC, XFER mode only */
1401 case CTL_MSG_DATAMOVE: {
1402 struct ctl_sg_entry *sgl;
1405 io = msg->hdr.original_sc;
1407 printf("%s: original_sc == NULL!\n", __func__);
1408 /* XXX KDM do something here */
1411 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1412 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1414 * Keep track of this, we need to send it back over
1415 * when the datamove is complete.
1417 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1418 if (msg->hdr.status == CTL_SUCCESS)
1419 io->io_hdr.status = msg->hdr.status;
1421 if (msg->dt.sg_sequence == 0) {
1423 getbinuptime(&io->io_hdr.dma_start_bt);
1425 i = msg->dt.kern_sg_entries +
1426 msg->dt.kern_data_len /
1427 CTL_HA_DATAMOVE_SEGMENT + 1;
1428 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1430 io->io_hdr.remote_sglist = sgl;
1431 io->io_hdr.local_sglist =
1432 &sgl[msg->dt.kern_sg_entries];
1434 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1436 io->scsiio.kern_sg_entries =
1437 msg->dt.kern_sg_entries;
1438 io->scsiio.rem_sg_entries =
1439 msg->dt.kern_sg_entries;
1440 io->scsiio.kern_data_len =
1441 msg->dt.kern_data_len;
1442 io->scsiio.kern_total_len =
1443 msg->dt.kern_total_len;
1444 io->scsiio.kern_data_resid =
1445 msg->dt.kern_data_resid;
1446 io->scsiio.kern_rel_offset =
1447 msg->dt.kern_rel_offset;
1448 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1449 io->io_hdr.flags |= msg->dt.flags &
1452 sgl = (struct ctl_sg_entry *)
1453 io->scsiio.kern_data_ptr;
1455 for (i = msg->dt.sent_sg_entries, j = 0;
1456 i < (msg->dt.sent_sg_entries +
1457 msg->dt.cur_sg_entries); i++, j++) {
1458 sgl[i].addr = msg->dt.sg_list[j].addr;
1459 sgl[i].len = msg->dt.sg_list[j].len;
1462 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1463 __func__, sgl[i].addr, sgl[i].len, j, i);
1468 * If this is the last piece of the I/O, we've got
1469 * the full S/G list. Queue processing in the thread.
1470 * Otherwise wait for the next piece.
1472 if (msg->dt.sg_last != 0)
1473 ctl_enqueue_isc(io);
1476 /* Performed on the Serializing (primary) SC, XFER mode only */
1477 case CTL_MSG_DATAMOVE_DONE: {
1478 if (msg->hdr.serializing_sc == NULL) {
1479 printf("%s: serializing_sc == NULL!\n",
1481 /* XXX KDM now what? */
1485 * We grab the sense information here in case
1486 * there was a failure, so we can return status
1487 * back to the initiator.
1489 io = msg->hdr.serializing_sc;
1490 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1491 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1492 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1493 io->io_hdr.port_status = msg->scsi.fetd_status;
1494 io->scsiio.residual = msg->scsi.residual;
1495 if (msg->hdr.status != CTL_STATUS_NONE) {
1496 io->io_hdr.status = msg->hdr.status;
1497 io->scsiio.scsi_status = msg->scsi.scsi_status;
1498 io->scsiio.sense_len = msg->scsi.sense_len;
1499 io->scsiio.sense_residual =msg->scsi.sense_residual;
1500 memcpy(&io->scsiio.sense_data,
1501 &msg->scsi.sense_data,
1502 msg->scsi.sense_len);
1503 if (msg->hdr.status == CTL_SUCCESS)
1504 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1506 ctl_enqueue_isc(io);
1510 /* Preformed on Originating SC, SER_ONLY mode */
1512 io = msg->hdr.original_sc;
1514 printf("%s: original_sc == NULL!\n",
1518 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1519 io->io_hdr.msg_type = CTL_MSG_R2R;
1520 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1521 ctl_enqueue_isc(io);
1525 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1527 * Performed on the Originating (i.e. secondary) SC in XFER
1530 case CTL_MSG_FINISH_IO:
1531 if (softc->ha_mode == CTL_HA_MODE_XFER)
1532 ctl_isc_handler_finish_xfer(softc, msg);
1534 ctl_isc_handler_finish_ser_only(softc, msg);
1537 /* Preformed on Originating SC */
1538 case CTL_MSG_BAD_JUJU:
1539 io = msg->hdr.original_sc;
1541 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1545 ctl_copy_sense_data(msg, io);
1547 * IO should have already been cleaned up on other
1548 * SC so clear this flag so we won't send a message
1549 * back to finish the IO there.
1551 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1552 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1554 /* io = msg->hdr.serializing_sc; */
1555 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1556 ctl_enqueue_isc(io);
1559 /* Handle resets sent from the other side */
1560 case CTL_MSG_MANAGE_TASKS: {
1561 struct ctl_taskio *taskio;
1562 taskio = (struct ctl_taskio *)ctl_alloc_io(
1563 softc->othersc_pool);
1564 ctl_zero_io((union ctl_io *)taskio);
1565 taskio->io_hdr.io_type = CTL_IO_TASK;
1566 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1567 taskio->io_hdr.nexus = msg->hdr.nexus;
1568 taskio->task_action = msg->task.task_action;
1569 taskio->tag_num = msg->task.tag_num;
1570 taskio->tag_type = msg->task.tag_type;
1572 taskio->io_hdr.start_time = time_uptime;
1573 getbinuptime(&taskio->io_hdr.start_bt);
1574 #endif /* CTL_TIME_IO */
1575 ctl_run_task((union ctl_io *)taskio);
1578 /* Persistent Reserve action which needs attention */
1579 case CTL_MSG_PERS_ACTION:
1580 presio = (struct ctl_prio *)ctl_alloc_io(
1581 softc->othersc_pool);
1582 ctl_zero_io((union ctl_io *)presio);
1583 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1584 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1585 presio->io_hdr.nexus = msg->hdr.nexus;
1586 presio->pr_msg = msg->pr;
1587 ctl_enqueue_isc((union ctl_io *)presio);
1590 ctl_isc_ua(softc, msg, param);
1592 case CTL_MSG_PORT_SYNC:
1593 ctl_isc_port_sync(softc, msg, param);
1595 case CTL_MSG_LUN_SYNC:
1596 ctl_isc_lun_sync(softc, msg, param);
1598 case CTL_MSG_IID_SYNC:
1599 ctl_isc_iid_sync(softc, msg, param);
1602 ctl_isc_login(softc, msg, param);
1604 case CTL_MSG_MODE_SYNC:
1605 ctl_isc_mode_sync(softc, msg, param);
1608 printf("Received HA message of unknown type %d\n",
1610 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1615 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1616 printf("CTL: HA link status changed from %d to %d\n",
1617 softc->ha_link, param);
1618 if (param == softc->ha_link)
1620 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1621 softc->ha_link = param;
1622 ctl_isc_ha_link_down(softc);
1624 softc->ha_link = param;
1625 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1626 ctl_isc_ha_link_up(softc);
1630 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1636 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1639 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1640 src->scsi.sense_len);
1641 dest->scsiio.scsi_status = src->scsi.scsi_status;
1642 dest->scsiio.sense_len = src->scsi.sense_len;
1643 dest->io_hdr.status = src->hdr.status;
1647 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1650 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1651 src->scsiio.sense_len);
1652 dest->scsi.scsi_status = src->scsiio.scsi_status;
1653 dest->scsi.sense_len = src->scsiio.sense_len;
1654 dest->hdr.status = src->io_hdr.status;
1658 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1660 struct ctl_softc *softc = lun->ctl_softc;
1663 if (initidx < softc->init_min || initidx >= softc->init_max)
1665 mtx_assert(&lun->lun_lock, MA_OWNED);
1666 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1669 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1673 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1677 mtx_assert(&lun->lun_lock, MA_OWNED);
1678 if (lun->pending_ua[port] == NULL)
1680 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1681 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1683 lun->pending_ua[port][i] |= ua;
1688 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1690 struct ctl_softc *softc = lun->ctl_softc;
1693 mtx_assert(&lun->lun_lock, MA_OWNED);
1694 for (i = softc->port_min; i < softc->port_max; i++)
1695 ctl_est_ua_port(lun, i, except, ua);
1699 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1701 struct ctl_softc *softc = lun->ctl_softc;
1704 if (initidx < softc->init_min || initidx >= softc->init_max)
1706 mtx_assert(&lun->lun_lock, MA_OWNED);
1707 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1710 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1714 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1716 struct ctl_softc *softc = lun->ctl_softc;
1719 mtx_assert(&lun->lun_lock, MA_OWNED);
1720 for (i = softc->port_min; i < softc->port_max; i++) {
1721 if (lun->pending_ua[i] == NULL)
1723 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1724 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1726 lun->pending_ua[i][j] &= ~ua;
1732 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1733 ctl_ua_type ua_type)
1735 struct ctl_lun *lun;
1737 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1738 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1739 mtx_lock(&lun->lun_lock);
1740 ctl_clr_ua(lun, initidx, ua_type);
1741 mtx_unlock(&lun->lun_lock);
1746 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1748 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1749 struct ctl_lun *lun;
1750 struct ctl_lun_req ireq;
1753 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1754 error = sysctl_handle_int(oidp, &value, 0, req);
1755 if ((error != 0) || (req->newptr == NULL))
1758 mtx_lock(&softc->ctl_lock);
1760 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1762 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1763 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1764 mtx_unlock(&softc->ctl_lock);
1765 bzero(&ireq, sizeof(ireq));
1766 ireq.reqtype = CTL_LUNREQ_MODIFY;
1767 ireq.reqdata.modify.lun_id = lun->lun;
1768 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1770 if (ireq.status != CTL_LUN_OK) {
1771 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1772 __func__, ireq.status, ireq.error_str);
1774 mtx_lock(&softc->ctl_lock);
1776 mtx_unlock(&softc->ctl_lock);
1783 struct make_dev_args args;
1784 struct ctl_softc *softc;
1788 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1791 make_dev_args_init(&args);
1792 args.mda_devsw = &ctl_cdevsw;
1793 args.mda_uid = UID_ROOT;
1794 args.mda_gid = GID_OPERATOR;
1795 args.mda_mode = 0600;
1796 args.mda_si_drv1 = softc;
1797 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1799 free(control_softc, M_DEVBUF);
1803 sysctl_ctx_init(&softc->sysctl_ctx);
1804 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1805 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1806 CTLFLAG_RD, 0, "CAM Target Layer");
1808 if (softc->sysctl_tree == NULL) {
1809 printf("%s: unable to allocate sysctl tree\n", __func__);
1810 destroy_dev(softc->dev);
1811 free(control_softc, M_DEVBUF);
1812 control_softc = NULL;
1816 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1817 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1818 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1821 TUNABLE_INT_FETCH("kern.cam.ctl.ha_mode", (int *)&softc->ha_mode);
1822 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1823 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1824 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1827 * In Copan's HA scheme, the "master" and "slave" roles are
1828 * figured out through the slot the controller is in. Although it
1829 * is an active/active system, someone has to be in charge.
1831 TUNABLE_INT_FETCH("kern.cam.ctl.ha_id", &softc->ha_id);
1832 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1833 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1834 "HA head ID (0 - no HA)");
1835 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1836 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1837 softc->is_single = 1;
1838 softc->port_cnt = CTL_MAX_PORTS;
1839 softc->port_min = 0;
1841 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1842 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1844 softc->port_max = softc->port_min + softc->port_cnt;
1845 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1846 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1848 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1849 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1850 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1852 STAILQ_INIT(&softc->lun_list);
1853 STAILQ_INIT(&softc->pending_lun_queue);
1854 STAILQ_INIT(&softc->fe_list);
1855 STAILQ_INIT(&softc->port_list);
1856 STAILQ_INIT(&softc->be_list);
1857 ctl_tpc_init(softc);
1859 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1862 printf("ctl: can't allocate %d entry other SC pool, "
1863 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1866 softc->othersc_pool = other_pool;
1868 if (worker_threads <= 0)
1869 worker_threads = max(1, mp_ncpus / 4);
1870 if (worker_threads > CTL_MAX_THREADS)
1871 worker_threads = CTL_MAX_THREADS;
1873 for (i = 0; i < worker_threads; i++) {
1874 struct ctl_thread *thr = &softc->threads[i];
1876 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1877 thr->ctl_softc = softc;
1878 STAILQ_INIT(&thr->incoming_queue);
1879 STAILQ_INIT(&thr->rtr_queue);
1880 STAILQ_INIT(&thr->done_queue);
1881 STAILQ_INIT(&thr->isc_queue);
1883 error = kproc_kthread_add(ctl_work_thread, thr,
1884 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1886 printf("error creating CTL work thread!\n");
1887 ctl_pool_free(other_pool);
1891 error = kproc_kthread_add(ctl_lun_thread, softc,
1892 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1894 printf("error creating CTL lun thread!\n");
1895 ctl_pool_free(other_pool);
1898 error = kproc_kthread_add(ctl_thresh_thread, softc,
1899 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1901 printf("error creating CTL threshold thread!\n");
1902 ctl_pool_free(other_pool);
1906 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1907 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1908 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1910 if (softc->is_single == 0) {
1911 ctl_frontend_register(&ha_frontend);
1912 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1913 printf("ctl_init: ctl_ha_msg_init failed.\n");
1914 softc->is_single = 1;
1916 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1917 != CTL_HA_STATUS_SUCCESS) {
1918 printf("ctl_init: ctl_ha_msg_register failed.\n");
1919 softc->is_single = 1;
1928 struct ctl_softc *softc = control_softc;
1929 struct ctl_lun *lun, *next_lun;
1931 if (softc->is_single == 0) {
1932 ctl_ha_msg_shutdown(softc);
1933 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1934 != CTL_HA_STATUS_SUCCESS)
1935 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1936 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1937 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1938 ctl_frontend_deregister(&ha_frontend);
1941 mtx_lock(&softc->ctl_lock);
1943 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun)
1946 mtx_unlock(&softc->ctl_lock);
1949 ctl_shutdown_thread(softc->work_thread);
1950 mtx_destroy(&softc->queue_lock);
1953 ctl_tpc_shutdown(softc);
1954 uma_zdestroy(softc->io_zone);
1955 mtx_destroy(&softc->ctl_lock);
1957 destroy_dev(softc->dev);
1959 sysctl_ctx_free(&softc->sysctl_ctx);
1961 free(control_softc, M_DEVBUF);
1962 control_softc = NULL;
1966 ctl_module_event_handler(module_t mod, int what, void *arg)
1971 return (ctl_init());
1975 return (EOPNOTSUPP);
1980 * XXX KDM should we do some access checks here? Bump a reference count to
1981 * prevent a CTL module from being unloaded while someone has it open?
1984 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1990 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1996 * Remove an initiator by port number and initiator ID.
1997 * Returns 0 for success, -1 for failure.
2000 ctl_remove_initiator(struct ctl_port *port, int iid)
2002 struct ctl_softc *softc = port->ctl_softc;
2004 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2006 if (iid > CTL_MAX_INIT_PER_PORT) {
2007 printf("%s: initiator ID %u > maximun %u!\n",
2008 __func__, iid, CTL_MAX_INIT_PER_PORT);
2012 mtx_lock(&softc->ctl_lock);
2013 port->wwpn_iid[iid].in_use--;
2014 port->wwpn_iid[iid].last_use = time_uptime;
2015 mtx_unlock(&softc->ctl_lock);
2016 ctl_isc_announce_iid(port, iid);
2022 * Add an initiator to the initiator map.
2023 * Returns iid for success, < 0 for failure.
2026 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2028 struct ctl_softc *softc = port->ctl_softc;
2032 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2034 if (iid >= CTL_MAX_INIT_PER_PORT) {
2035 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2036 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2041 mtx_lock(&softc->ctl_lock);
2043 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2044 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2045 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2049 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2050 strcmp(name, port->wwpn_iid[i].name) == 0) {
2058 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2059 if (port->wwpn_iid[i].in_use == 0 &&
2060 port->wwpn_iid[i].wwpn == 0 &&
2061 port->wwpn_iid[i].name == NULL) {
2070 best_time = INT32_MAX;
2071 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2072 if (port->wwpn_iid[i].in_use == 0) {
2073 if (port->wwpn_iid[i].last_use < best_time) {
2075 best_time = port->wwpn_iid[i].last_use;
2083 mtx_unlock(&softc->ctl_lock);
2088 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2090 * This is not an error yet.
2092 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2094 printf("%s: port %d iid %u WWPN %#jx arrived"
2095 " again\n", __func__, port->targ_port,
2096 iid, (uintmax_t)wwpn);
2100 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2101 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2103 printf("%s: port %d iid %u name '%s' arrived"
2104 " again\n", __func__, port->targ_port,
2111 * This is an error, but what do we do about it? The
2112 * driver is telling us we have a new WWPN for this
2113 * initiator ID, so we pretty much need to use it.
2115 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2116 " but WWPN %#jx '%s' is still at that address\n",
2117 __func__, port->targ_port, iid, wwpn, name,
2118 (uintmax_t)port->wwpn_iid[iid].wwpn,
2119 port->wwpn_iid[iid].name);
2122 * XXX KDM clear have_ca and ua_pending on each LUN for
2127 free(port->wwpn_iid[iid].name, M_CTL);
2128 port->wwpn_iid[iid].name = name;
2129 port->wwpn_iid[iid].wwpn = wwpn;
2130 port->wwpn_iid[iid].in_use++;
2131 mtx_unlock(&softc->ctl_lock);
2132 ctl_isc_announce_iid(port, iid);
2138 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2142 switch (port->port_type) {
2145 struct scsi_transportid_fcp *id =
2146 (struct scsi_transportid_fcp *)buf;
2147 if (port->wwpn_iid[iid].wwpn == 0)
2149 memset(id, 0, sizeof(*id));
2150 id->format_protocol = SCSI_PROTO_FC;
2151 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2152 return (sizeof(*id));
2154 case CTL_PORT_ISCSI:
2156 struct scsi_transportid_iscsi_port *id =
2157 (struct scsi_transportid_iscsi_port *)buf;
2158 if (port->wwpn_iid[iid].name == NULL)
2161 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2163 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2164 len = roundup2(min(len, 252), 4);
2165 scsi_ulto2b(len, id->additional_length);
2166 return (sizeof(*id) + len);
2170 struct scsi_transportid_sas *id =
2171 (struct scsi_transportid_sas *)buf;
2172 if (port->wwpn_iid[iid].wwpn == 0)
2174 memset(id, 0, sizeof(*id));
2175 id->format_protocol = SCSI_PROTO_SAS;
2176 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2177 return (sizeof(*id));
2181 struct scsi_transportid_spi *id =
2182 (struct scsi_transportid_spi *)buf;
2183 memset(id, 0, sizeof(*id));
2184 id->format_protocol = SCSI_PROTO_SPI;
2185 scsi_ulto2b(iid, id->scsi_addr);
2186 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2187 return (sizeof(*id));
2193 * Serialize a command that went down the "wrong" side, and so was sent to
2194 * this controller for execution. The logic is a little different than the
2195 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2196 * sent back to the other side, but in the success case, we execute the
2197 * command on this side (XFER mode) or tell the other side to execute it
2201 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2203 struct ctl_softc *softc = control_softc;
2204 union ctl_ha_msg msg_info;
2205 struct ctl_port *port;
2206 struct ctl_lun *lun;
2207 const struct ctl_cmd_entry *entry;
2211 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2212 mtx_lock(&softc->ctl_lock);
2214 /* Make sure that we know about this port. */
2215 port = ctl_io_port(&ctsio->io_hdr);
2216 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2217 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2222 /* Make sure that we know about this LUN. */
2223 if ((targ_lun < CTL_MAX_LUNS) &&
2224 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
2225 mtx_lock(&lun->lun_lock);
2226 mtx_unlock(&softc->ctl_lock);
2228 * If the LUN is invalid, pretend that it doesn't exist.
2229 * It will go away as soon as all pending I/O has been
2232 if (lun->flags & CTL_LUN_DISABLED) {
2233 mtx_unlock(&lun->lun_lock);
2237 mtx_unlock(&softc->ctl_lock);
2242 * The other node would not send this request to us unless
2243 * received announce that we are primary node for this LUN.
2244 * If this LUN does not exist now, it is probably result of
2245 * a race, so respond to initiator in the most opaque way.
2247 ctl_set_busy(ctsio);
2251 entry = ctl_get_cmd_entry(ctsio, NULL);
2252 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2253 mtx_unlock(&lun->lun_lock);
2257 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
2258 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
2261 * Every I/O goes into the OOA queue for a
2262 * particular LUN, and stays there until completion.
2265 if (TAILQ_EMPTY(&lun->ooa_queue))
2266 lun->idle_time += getsbinuptime() - lun->last_busy;
2268 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2270 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2271 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2273 case CTL_ACTION_BLOCK:
2274 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2275 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2277 mtx_unlock(&lun->lun_lock);
2279 case CTL_ACTION_PASS:
2280 case CTL_ACTION_SKIP:
2281 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2282 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2283 ctl_enqueue_rtr((union ctl_io *)ctsio);
2284 mtx_unlock(&lun->lun_lock);
2286 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2287 mtx_unlock(&lun->lun_lock);
2289 /* send msg back to other side */
2290 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2291 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2292 msg_info.hdr.msg_type = CTL_MSG_R2R;
2293 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2294 sizeof(msg_info.hdr), M_WAITOK);
2297 case CTL_ACTION_OVERLAP:
2298 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2299 mtx_unlock(&lun->lun_lock);
2300 ctl_set_overlapped_cmd(ctsio);
2302 case CTL_ACTION_OVERLAP_TAG:
2303 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2304 mtx_unlock(&lun->lun_lock);
2305 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2307 case CTL_ACTION_ERROR:
2309 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2310 mtx_unlock(&lun->lun_lock);
2312 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2315 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2316 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2317 msg_info.hdr.serializing_sc = NULL;
2318 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2319 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2320 sizeof(msg_info.scsi), M_WAITOK);
2328 * Returns 0 for success, errno for failure.
2331 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2332 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2336 mtx_lock(&lun->lun_lock);
2337 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2338 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2340 struct ctl_ooa_entry *entry;
2343 * If we've got more than we can fit, just count the
2344 * remaining entries.
2346 if (*cur_fill_num >= ooa_hdr->alloc_num)
2349 entry = &kern_entries[*cur_fill_num];
2351 entry->tag_num = io->scsiio.tag_num;
2352 entry->lun_num = lun->lun;
2354 entry->start_bt = io->io_hdr.start_bt;
2356 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2357 entry->cdb_len = io->scsiio.cdb_len;
2358 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2359 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2361 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2362 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2364 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2365 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2367 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2368 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2370 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2371 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2373 mtx_unlock(&lun->lun_lock);
2377 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2378 size_t error_str_len)
2382 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2384 if (copyin(user_addr, kptr, len) != 0) {
2385 snprintf(error_str, error_str_len, "Error copying %d bytes "
2386 "from user address %p to kernel address %p", len,
2396 ctl_free_args(int num_args, struct ctl_be_arg *args)
2403 for (i = 0; i < num_args; i++) {
2404 free(args[i].kname, M_CTL);
2405 free(args[i].kvalue, M_CTL);
2411 static struct ctl_be_arg *
2412 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2413 char *error_str, size_t error_str_len)
2415 struct ctl_be_arg *args;
2418 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2419 error_str, error_str_len);
2424 for (i = 0; i < num_args; i++) {
2425 args[i].kname = NULL;
2426 args[i].kvalue = NULL;
2429 for (i = 0; i < num_args; i++) {
2432 args[i].kname = ctl_copyin_alloc(args[i].name,
2433 args[i].namelen, error_str, error_str_len);
2434 if (args[i].kname == NULL)
2437 if (args[i].kname[args[i].namelen - 1] != '\0') {
2438 snprintf(error_str, error_str_len, "Argument %d "
2439 "name is not NUL-terminated", i);
2443 if (args[i].flags & CTL_BEARG_RD) {
2444 tmpptr = ctl_copyin_alloc(args[i].value,
2445 args[i].vallen, error_str, error_str_len);
2448 if ((args[i].flags & CTL_BEARG_ASCII)
2449 && (tmpptr[args[i].vallen - 1] != '\0')) {
2450 snprintf(error_str, error_str_len, "Argument "
2451 "%d value is not NUL-terminated", i);
2452 free(tmpptr, M_CTL);
2455 args[i].kvalue = tmpptr;
2457 args[i].kvalue = malloc(args[i].vallen,
2458 M_CTL, M_WAITOK | M_ZERO);
2465 ctl_free_args(num_args, args);
2471 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2475 for (i = 0; i < num_args; i++) {
2476 if (args[i].flags & CTL_BEARG_WR)
2477 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2482 * Escape characters that are illegal or not recommended in XML.
2485 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2487 char *end = str + size;
2492 for (; *str && str < end; str++) {
2495 retval = sbuf_printf(sb, "&");
2498 retval = sbuf_printf(sb, ">");
2501 retval = sbuf_printf(sb, "<");
2504 retval = sbuf_putc(sb, *str);
2517 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2519 struct scsi_vpd_id_descriptor *desc;
2522 if (id == NULL || id->len < 4)
2524 desc = (struct scsi_vpd_id_descriptor *)id->data;
2525 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2526 case SVPD_ID_TYPE_T10:
2527 sbuf_printf(sb, "t10.");
2529 case SVPD_ID_TYPE_EUI64:
2530 sbuf_printf(sb, "eui.");
2532 case SVPD_ID_TYPE_NAA:
2533 sbuf_printf(sb, "naa.");
2535 case SVPD_ID_TYPE_SCSI_NAME:
2538 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2539 case SVPD_ID_CODESET_BINARY:
2540 for (i = 0; i < desc->length; i++)
2541 sbuf_printf(sb, "%02x", desc->identifier[i]);
2543 case SVPD_ID_CODESET_ASCII:
2544 sbuf_printf(sb, "%.*s", (int)desc->length,
2545 (char *)desc->identifier);
2547 case SVPD_ID_CODESET_UTF8:
2548 sbuf_printf(sb, "%s", (char *)desc->identifier);
2554 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2557 struct ctl_softc *softc = dev->si_drv1;
2558 struct ctl_lun *lun;
2565 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2567 case CTL_ENABLE_PORT:
2568 case CTL_DISABLE_PORT:
2569 case CTL_SET_PORT_WWNS: {
2570 struct ctl_port *port;
2571 struct ctl_port_entry *entry;
2573 entry = (struct ctl_port_entry *)addr;
2575 mtx_lock(&softc->ctl_lock);
2576 STAILQ_FOREACH(port, &softc->port_list, links) {
2579 if (port->targ_port < softc->port_min ||
2580 port->targ_port >= softc->port_max)
2585 if ((entry->port_type == CTL_PORT_NONE)
2586 && (entry->targ_port == port->targ_port)) {
2588 * If the user only wants to enable or
2589 * disable or set WWNs on a specific port,
2590 * do the operation and we're done.
2594 } else if (entry->port_type & port->port_type) {
2596 * Compare the user's type mask with the
2597 * particular frontend type to see if we
2604 * Make sure the user isn't trying to set
2605 * WWNs on multiple ports at the same time.
2607 if (cmd == CTL_SET_PORT_WWNS) {
2608 printf("%s: Can't set WWNs on "
2609 "multiple ports\n", __func__);
2618 * XXX KDM we have to drop the lock here, because
2619 * the online/offline operations can potentially
2620 * block. We need to reference count the frontends
2621 * so they can't go away,
2623 if (cmd == CTL_ENABLE_PORT) {
2624 mtx_unlock(&softc->ctl_lock);
2625 ctl_port_online(port);
2626 mtx_lock(&softc->ctl_lock);
2627 } else if (cmd == CTL_DISABLE_PORT) {
2628 mtx_unlock(&softc->ctl_lock);
2629 ctl_port_offline(port);
2630 mtx_lock(&softc->ctl_lock);
2631 } else if (cmd == CTL_SET_PORT_WWNS) {
2632 ctl_port_set_wwns(port,
2633 (entry->flags & CTL_PORT_WWNN_VALID) ?
2635 (entry->flags & CTL_PORT_WWPN_VALID) ?
2636 1 : 0, entry->wwpn);
2641 mtx_unlock(&softc->ctl_lock);
2645 struct ctl_ooa *ooa_hdr;
2646 struct ctl_ooa_entry *entries;
2647 uint32_t cur_fill_num;
2649 ooa_hdr = (struct ctl_ooa *)addr;
2651 if ((ooa_hdr->alloc_len == 0)
2652 || (ooa_hdr->alloc_num == 0)) {
2653 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2654 "must be non-zero\n", __func__,
2655 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2660 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2661 sizeof(struct ctl_ooa_entry))) {
2662 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2663 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2664 __func__, ooa_hdr->alloc_len,
2665 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2670 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2671 if (entries == NULL) {
2672 printf("%s: could not allocate %d bytes for OOA "
2673 "dump\n", __func__, ooa_hdr->alloc_len);
2678 mtx_lock(&softc->ctl_lock);
2679 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2680 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2681 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2682 mtx_unlock(&softc->ctl_lock);
2683 free(entries, M_CTL);
2684 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2685 __func__, (uintmax_t)ooa_hdr->lun_num);
2692 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2693 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2694 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2698 lun = softc->ctl_luns[ooa_hdr->lun_num];
2699 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2702 mtx_unlock(&softc->ctl_lock);
2704 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2705 ooa_hdr->fill_len = ooa_hdr->fill_num *
2706 sizeof(struct ctl_ooa_entry);
2707 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2709 printf("%s: error copying out %d bytes for OOA dump\n",
2710 __func__, ooa_hdr->fill_len);
2713 getbinuptime(&ooa_hdr->cur_bt);
2715 if (cur_fill_num > ooa_hdr->alloc_num) {
2716 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2717 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2719 ooa_hdr->dropped_num = 0;
2720 ooa_hdr->status = CTL_OOA_OK;
2723 free(entries, M_CTL);
2726 case CTL_DELAY_IO: {
2727 struct ctl_io_delay_info *delay_info;
2729 delay_info = (struct ctl_io_delay_info *)addr;
2732 mtx_lock(&softc->ctl_lock);
2734 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2735 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2736 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2738 lun = softc->ctl_luns[delay_info->lun_id];
2739 mtx_lock(&lun->lun_lock);
2741 delay_info->status = CTL_DELAY_STATUS_OK;
2743 switch (delay_info->delay_type) {
2744 case CTL_DELAY_TYPE_CONT:
2746 case CTL_DELAY_TYPE_ONESHOT:
2749 delay_info->status =
2750 CTL_DELAY_STATUS_INVALID_TYPE;
2754 switch (delay_info->delay_loc) {
2755 case CTL_DELAY_LOC_DATAMOVE:
2756 lun->delay_info.datamove_type =
2757 delay_info->delay_type;
2758 lun->delay_info.datamove_delay =
2759 delay_info->delay_secs;
2761 case CTL_DELAY_LOC_DONE:
2762 lun->delay_info.done_type =
2763 delay_info->delay_type;
2764 lun->delay_info.done_delay =
2765 delay_info->delay_secs;
2768 delay_info->status =
2769 CTL_DELAY_STATUS_INVALID_LOC;
2772 mtx_unlock(&lun->lun_lock);
2775 mtx_unlock(&softc->ctl_lock);
2777 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2778 #endif /* CTL_IO_DELAY */
2781 case CTL_GETSTATS: {
2782 struct ctl_stats *stats;
2785 stats = (struct ctl_stats *)addr;
2787 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2789 stats->status = CTL_SS_NEED_MORE_SPACE;
2790 stats->num_luns = softc->num_luns;
2794 * XXX KDM no locking here. If the LUN list changes,
2795 * things can blow up.
2798 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2799 retval = copyout(&lun->stats, &stats->lun_stats[i++],
2800 sizeof(lun->stats));
2804 stats->num_luns = softc->num_luns;
2805 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2807 stats->status = CTL_SS_OK;
2809 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2811 stats->flags = CTL_STATS_FLAG_NONE;
2813 getnanouptime(&stats->timestamp);
2816 case CTL_ERROR_INJECT: {
2817 struct ctl_error_desc *err_desc, *new_err_desc;
2819 err_desc = (struct ctl_error_desc *)addr;
2821 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2823 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2825 mtx_lock(&softc->ctl_lock);
2826 lun = softc->ctl_luns[err_desc->lun_id];
2828 mtx_unlock(&softc->ctl_lock);
2829 free(new_err_desc, M_CTL);
2830 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2831 __func__, (uintmax_t)err_desc->lun_id);
2835 mtx_lock(&lun->lun_lock);
2836 mtx_unlock(&softc->ctl_lock);
2839 * We could do some checking here to verify the validity
2840 * of the request, but given the complexity of error
2841 * injection requests, the checking logic would be fairly
2844 * For now, if the request is invalid, it just won't get
2845 * executed and might get deleted.
2847 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2850 * XXX KDM check to make sure the serial number is unique,
2851 * in case we somehow manage to wrap. That shouldn't
2852 * happen for a very long time, but it's the right thing to
2855 new_err_desc->serial = lun->error_serial;
2856 err_desc->serial = lun->error_serial;
2857 lun->error_serial++;
2859 mtx_unlock(&lun->lun_lock);
2862 case CTL_ERROR_INJECT_DELETE: {
2863 struct ctl_error_desc *delete_desc, *desc, *desc2;
2866 delete_desc = (struct ctl_error_desc *)addr;
2869 mtx_lock(&softc->ctl_lock);
2870 lun = softc->ctl_luns[delete_desc->lun_id];
2872 mtx_unlock(&softc->ctl_lock);
2873 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2874 __func__, (uintmax_t)delete_desc->lun_id);
2878 mtx_lock(&lun->lun_lock);
2879 mtx_unlock(&softc->ctl_lock);
2880 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2881 if (desc->serial != delete_desc->serial)
2884 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2889 mtx_unlock(&lun->lun_lock);
2890 if (delete_done == 0) {
2891 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2892 "error serial %ju on LUN %u\n", __func__,
2893 delete_desc->serial, delete_desc->lun_id);
2899 case CTL_DUMP_STRUCTS: {
2901 struct ctl_port *port;
2902 struct ctl_frontend *fe;
2904 mtx_lock(&softc->ctl_lock);
2905 printf("CTL Persistent Reservation information start:\n");
2906 for (i = 0; i < CTL_MAX_LUNS; i++) {
2907 lun = softc->ctl_luns[i];
2910 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2913 for (j = 0; j < CTL_MAX_PORTS; j++) {
2914 if (lun->pr_keys[j] == NULL)
2916 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2917 if (lun->pr_keys[j][k] == 0)
2919 printf(" LUN %d port %d iid %d key "
2921 (uintmax_t)lun->pr_keys[j][k]);
2925 printf("CTL Persistent Reservation information end\n");
2926 printf("CTL Ports:\n");
2927 STAILQ_FOREACH(port, &softc->port_list, links) {
2928 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2929 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2930 port->frontend->name, port->port_type,
2931 port->physical_port, port->virtual_port,
2932 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2933 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2934 if (port->wwpn_iid[j].in_use == 0 &&
2935 port->wwpn_iid[j].wwpn == 0 &&
2936 port->wwpn_iid[j].name == NULL)
2939 printf(" iid %u use %d WWPN %#jx '%s'\n",
2940 j, port->wwpn_iid[j].in_use,
2941 (uintmax_t)port->wwpn_iid[j].wwpn,
2942 port->wwpn_iid[j].name);
2945 printf("CTL Port information end\n");
2946 mtx_unlock(&softc->ctl_lock);
2948 * XXX KDM calling this without a lock. We'd likely want
2949 * to drop the lock before calling the frontend's dump
2952 printf("CTL Frontends:\n");
2953 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2954 printf(" Frontend '%s'\n", fe->name);
2955 if (fe->fe_dump != NULL)
2958 printf("CTL Frontend information end\n");
2962 struct ctl_lun_req *lun_req;
2963 struct ctl_backend_driver *backend;
2965 lun_req = (struct ctl_lun_req *)addr;
2967 backend = ctl_backend_find(lun_req->backend);
2968 if (backend == NULL) {
2969 lun_req->status = CTL_LUN_ERROR;
2970 snprintf(lun_req->error_str,
2971 sizeof(lun_req->error_str),
2972 "Backend \"%s\" not found.",
2976 if (lun_req->num_be_args > 0) {
2977 lun_req->kern_be_args = ctl_copyin_args(
2978 lun_req->num_be_args,
2981 sizeof(lun_req->error_str));
2982 if (lun_req->kern_be_args == NULL) {
2983 lun_req->status = CTL_LUN_ERROR;
2988 retval = backend->ioctl(dev, cmd, addr, flag, td);
2990 if (lun_req->num_be_args > 0) {
2991 ctl_copyout_args(lun_req->num_be_args,
2992 lun_req->kern_be_args);
2993 ctl_free_args(lun_req->num_be_args,
2994 lun_req->kern_be_args);
2998 case CTL_LUN_LIST: {
3000 struct ctl_lun_list *list;
3001 struct ctl_option *opt;
3003 list = (struct ctl_lun_list *)addr;
3006 * Allocate a fixed length sbuf here, based on the length
3007 * of the user's buffer. We could allocate an auto-extending
3008 * buffer, and then tell the user how much larger our
3009 * amount of data is than his buffer, but that presents
3012 * 1. The sbuf(9) routines use a blocking malloc, and so
3013 * we can't hold a lock while calling them with an
3014 * auto-extending buffer.
3016 * 2. There is not currently a LUN reference counting
3017 * mechanism, outside of outstanding transactions on
3018 * the LUN's OOA queue. So a LUN could go away on us
3019 * while we're getting the LUN number, backend-specific
3020 * information, etc. Thus, given the way things
3021 * currently work, we need to hold the CTL lock while
3022 * grabbing LUN information.
3024 * So, from the user's standpoint, the best thing to do is
3025 * allocate what he thinks is a reasonable buffer length,
3026 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3027 * double the buffer length and try again. (And repeat
3028 * that until he succeeds.)
3030 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3032 list->status = CTL_LUN_LIST_ERROR;
3033 snprintf(list->error_str, sizeof(list->error_str),
3034 "Unable to allocate %d bytes for LUN list",
3039 sbuf_printf(sb, "<ctllunlist>\n");
3041 mtx_lock(&softc->ctl_lock);
3042 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3043 mtx_lock(&lun->lun_lock);
3044 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3045 (uintmax_t)lun->lun);
3048 * Bail out as soon as we see that we've overfilled
3054 retval = sbuf_printf(sb, "\t<backend_type>%s"
3055 "</backend_type>\n",
3056 (lun->backend == NULL) ? "none" :
3057 lun->backend->name);
3062 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3063 lun->be_lun->lun_type);
3068 if (lun->backend == NULL) {
3069 retval = sbuf_printf(sb, "</lun>\n");
3075 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3076 (lun->be_lun->maxlba > 0) ?
3077 lun->be_lun->maxlba + 1 : 0);
3082 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3083 lun->be_lun->blocksize);
3088 retval = sbuf_printf(sb, "\t<serial_number>");
3093 retval = ctl_sbuf_printf_esc(sb,
3094 lun->be_lun->serial_num,
3095 sizeof(lun->be_lun->serial_num));
3100 retval = sbuf_printf(sb, "</serial_number>\n");
3105 retval = sbuf_printf(sb, "\t<device_id>");
3110 retval = ctl_sbuf_printf_esc(sb,
3111 lun->be_lun->device_id,
3112 sizeof(lun->be_lun->device_id));
3117 retval = sbuf_printf(sb, "</device_id>\n");
3122 if (lun->backend->lun_info != NULL) {
3123 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3127 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3128 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3129 opt->name, opt->value, opt->name);
3134 retval = sbuf_printf(sb, "</lun>\n");
3138 mtx_unlock(&lun->lun_lock);
3141 mtx_unlock(&lun->lun_lock);
3142 mtx_unlock(&softc->ctl_lock);
3145 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3148 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3149 snprintf(list->error_str, sizeof(list->error_str),
3150 "Out of space, %d bytes is too small",
3157 retval = copyout(sbuf_data(sb), list->lun_xml,
3160 list->fill_len = sbuf_len(sb) + 1;
3161 list->status = CTL_LUN_LIST_OK;
3166 struct ctl_iscsi *ci;
3167 struct ctl_frontend *fe;
3169 ci = (struct ctl_iscsi *)addr;
3171 fe = ctl_frontend_find("iscsi");
3173 ci->status = CTL_ISCSI_ERROR;
3174 snprintf(ci->error_str, sizeof(ci->error_str),
3175 "Frontend \"iscsi\" not found.");
3179 retval = fe->ioctl(dev, cmd, addr, flag, td);
3182 case CTL_PORT_REQ: {
3183 struct ctl_req *req;
3184 struct ctl_frontend *fe;
3186 req = (struct ctl_req *)addr;
3188 fe = ctl_frontend_find(req->driver);
3190 req->status = CTL_LUN_ERROR;
3191 snprintf(req->error_str, sizeof(req->error_str),
3192 "Frontend \"%s\" not found.", req->driver);
3195 if (req->num_args > 0) {
3196 req->kern_args = ctl_copyin_args(req->num_args,
3197 req->args, req->error_str, sizeof(req->error_str));
3198 if (req->kern_args == NULL) {
3199 req->status = CTL_LUN_ERROR;
3205 retval = fe->ioctl(dev, cmd, addr, flag, td);
3209 if (req->num_args > 0) {
3210 ctl_copyout_args(req->num_args, req->kern_args);
3211 ctl_free_args(req->num_args, req->kern_args);
3215 case CTL_PORT_LIST: {
3217 struct ctl_port *port;
3218 struct ctl_lun_list *list;
3219 struct ctl_option *opt;
3223 list = (struct ctl_lun_list *)addr;
3225 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3227 list->status = CTL_LUN_LIST_ERROR;
3228 snprintf(list->error_str, sizeof(list->error_str),
3229 "Unable to allocate %d bytes for LUN list",
3234 sbuf_printf(sb, "<ctlportlist>\n");
3236 mtx_lock(&softc->ctl_lock);
3237 STAILQ_FOREACH(port, &softc->port_list, links) {
3238 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3239 (uintmax_t)port->targ_port);
3242 * Bail out as soon as we see that we've overfilled
3248 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3249 "</frontend_type>\n", port->frontend->name);
3253 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3258 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3259 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3263 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3268 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3269 port->physical_port);
3273 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3274 port->virtual_port);
3278 if (port->target_devid != NULL) {
3279 sbuf_printf(sb, "\t<target>");
3280 ctl_id_sbuf(port->target_devid, sb);
3281 sbuf_printf(sb, "</target>\n");
3284 if (port->port_devid != NULL) {
3285 sbuf_printf(sb, "\t<port>");
3286 ctl_id_sbuf(port->port_devid, sb);
3287 sbuf_printf(sb, "</port>\n");
3290 if (port->port_info != NULL) {
3291 retval = port->port_info(port->onoff_arg, sb);
3295 STAILQ_FOREACH(opt, &port->options, links) {
3296 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3297 opt->name, opt->value, opt->name);
3302 if (port->lun_map != NULL) {
3303 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3304 for (j = 0; j < CTL_MAX_LUNS; j++) {
3305 plun = ctl_lun_map_from_port(port, j);
3306 if (plun >= CTL_MAX_LUNS)
3309 "\t<lun id=\"%u\">%u</lun>\n",
3314 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3315 if (port->wwpn_iid[j].in_use == 0 ||
3316 (port->wwpn_iid[j].wwpn == 0 &&
3317 port->wwpn_iid[j].name == NULL))
3320 if (port->wwpn_iid[j].name != NULL)
3321 retval = sbuf_printf(sb,
3322 "\t<initiator id=\"%u\">%s</initiator>\n",
3323 j, port->wwpn_iid[j].name);
3325 retval = sbuf_printf(sb,
3326 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3327 j, port->wwpn_iid[j].wwpn);
3334 retval = sbuf_printf(sb, "</targ_port>\n");
3338 mtx_unlock(&softc->ctl_lock);
3341 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3344 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3345 snprintf(list->error_str, sizeof(list->error_str),
3346 "Out of space, %d bytes is too small",
3353 retval = copyout(sbuf_data(sb), list->lun_xml,
3356 list->fill_len = sbuf_len(sb) + 1;
3357 list->status = CTL_LUN_LIST_OK;
3362 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3363 struct ctl_port *port;
3365 mtx_lock(&softc->ctl_lock);
3366 if (lm->port < softc->port_min ||
3367 lm->port >= softc->port_max ||
3368 (port = softc->ctl_ports[lm->port]) == NULL) {
3369 mtx_unlock(&softc->ctl_lock);
3372 if (port->status & CTL_PORT_STATUS_ONLINE) {
3373 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3374 if (ctl_lun_map_to_port(port, lun->lun) >=
3377 mtx_lock(&lun->lun_lock);
3378 ctl_est_ua_port(lun, lm->port, -1,
3380 mtx_unlock(&lun->lun_lock);
3383 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3384 if (lm->plun < CTL_MAX_LUNS) {
3385 if (lm->lun == UINT32_MAX)
3386 retval = ctl_lun_map_unset(port, lm->plun);
3387 else if (lm->lun < CTL_MAX_LUNS &&
3388 softc->ctl_luns[lm->lun] != NULL)
3389 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3392 } else if (lm->plun == UINT32_MAX) {
3393 if (lm->lun == UINT32_MAX)
3394 retval = ctl_lun_map_deinit(port);
3396 retval = ctl_lun_map_init(port);
3399 if (port->status & CTL_PORT_STATUS_ONLINE)
3400 ctl_isc_announce_port(port);
3404 /* XXX KDM should we fix this? */
3406 struct ctl_backend_driver *backend;
3413 * We encode the backend type as the ioctl type for backend
3414 * ioctls. So parse it out here, and then search for a
3415 * backend of this type.
3417 type = _IOC_TYPE(cmd);
3419 STAILQ_FOREACH(backend, &softc->be_list, links) {
3420 if (backend->type == type) {
3426 printf("ctl: unknown ioctl command %#lx or backend "
3431 retval = backend->ioctl(dev, cmd, addr, flag, td);
3441 ctl_get_initindex(struct ctl_nexus *nexus)
3443 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3447 ctl_lun_map_init(struct ctl_port *port)
3449 struct ctl_softc *softc = port->ctl_softc;
3450 struct ctl_lun *lun;
3453 if (port->lun_map == NULL)
3454 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3456 if (port->lun_map == NULL)
3458 for (i = 0; i < CTL_MAX_LUNS; i++)
3459 port->lun_map[i] = UINT32_MAX;
3460 if (port->status & CTL_PORT_STATUS_ONLINE) {
3461 if (port->lun_disable != NULL) {
3462 STAILQ_FOREACH(lun, &softc->lun_list, links)
3463 port->lun_disable(port->targ_lun_arg, lun->lun);
3465 ctl_isc_announce_port(port);
3471 ctl_lun_map_deinit(struct ctl_port *port)
3473 struct ctl_softc *softc = port->ctl_softc;
3474 struct ctl_lun *lun;
3476 if (port->lun_map == NULL)
3478 free(port->lun_map, M_CTL);
3479 port->lun_map = NULL;
3480 if (port->status & CTL_PORT_STATUS_ONLINE) {
3481 if (port->lun_enable != NULL) {
3482 STAILQ_FOREACH(lun, &softc->lun_list, links)
3483 port->lun_enable(port->targ_lun_arg, lun->lun);
3485 ctl_isc_announce_port(port);
3491 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3496 if (port->lun_map == NULL) {
3497 status = ctl_lun_map_init(port);
3501 old = port->lun_map[plun];
3502 port->lun_map[plun] = glun;
3503 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3504 if (port->lun_enable != NULL)
3505 port->lun_enable(port->targ_lun_arg, plun);
3506 ctl_isc_announce_port(port);
3512 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3516 if (port->lun_map == NULL)
3518 old = port->lun_map[plun];
3519 port->lun_map[plun] = UINT32_MAX;
3520 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3521 if (port->lun_disable != NULL)
3522 port->lun_disable(port->targ_lun_arg, plun);
3523 ctl_isc_announce_port(port);
3529 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3533 return (UINT32_MAX);
3534 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3536 return (port->lun_map[lun_id]);
3540 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3545 return (UINT32_MAX);
3546 if (port->lun_map == NULL)
3548 for (i = 0; i < CTL_MAX_LUNS; i++) {
3549 if (port->lun_map[i] == lun_id)
3552 return (UINT32_MAX);
3556 ctl_decode_lun(uint64_t encoded)
3559 uint32_t result = 0xffffffff;
3561 be64enc(lun, encoded);
3562 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3563 case RPL_LUNDATA_ATYP_PERIPH:
3564 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3565 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3568 case RPL_LUNDATA_ATYP_FLAT:
3569 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3570 lun[6] == 0 && lun[7] == 0)
3571 result = ((lun[0] & 0x3f) << 8) + lun[1];
3573 case RPL_LUNDATA_ATYP_EXTLUN:
3574 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3576 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3581 result = (lun[1] << 16) + (lun[2] << 8) +
3585 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3586 result = (lun[2] << 24) +
3587 (lun[3] << 16) + (lun[4] << 8) +
3592 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3593 result = 0xffffffff;
3602 ctl_encode_lun(uint32_t decoded)
3604 uint64_t l = decoded;
3607 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3609 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3611 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3613 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3616 static struct ctl_port *
3617 ctl_io_port(struct ctl_io_hdr *io_hdr)
3620 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3624 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3628 for (i = first; i < last; i++) {
3629 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3636 ctl_set_mask(uint32_t *mask, uint32_t bit)
3638 uint32_t chunk, piece;
3641 piece = bit % (sizeof(uint32_t) * 8);
3643 if ((mask[chunk] & (1 << piece)) != 0)
3646 mask[chunk] |= (1 << piece);
3652 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3654 uint32_t chunk, piece;
3657 piece = bit % (sizeof(uint32_t) * 8);
3659 if ((mask[chunk] & (1 << piece)) == 0)
3662 mask[chunk] &= ~(1 << piece);
3668 ctl_is_set(uint32_t *mask, uint32_t bit)
3670 uint32_t chunk, piece;
3673 piece = bit % (sizeof(uint32_t) * 8);
3675 if ((mask[chunk] & (1 << piece)) == 0)
3682 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3686 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3689 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3693 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3697 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3700 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3704 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3709 i = residx/CTL_MAX_INIT_PER_PORT;
3710 if (lun->pr_keys[i] != NULL)
3712 mtx_unlock(&lun->lun_lock);
3713 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3715 mtx_lock(&lun->lun_lock);
3716 if (lun->pr_keys[i] == NULL)
3717 lun->pr_keys[i] = p;
3723 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3727 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3728 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3729 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3733 * ctl_softc, pool_name, total_ctl_io are passed in.
3734 * npool is passed out.
3737 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3738 uint32_t total_ctl_io, void **npool)
3741 struct ctl_io_pool *pool;
3743 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3748 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3749 pool->ctl_softc = ctl_softc;
3750 pool->zone = uma_zsecond_create(pool->name, NULL,
3751 NULL, NULL, NULL, ctl_softc->io_zone);
3752 /* uma_prealloc(pool->zone, total_ctl_io); */
3756 *npool = ctl_softc->io_zone;
3762 ctl_pool_free(struct ctl_io_pool *pool)
3769 uma_zdestroy(pool->zone);
3775 ctl_alloc_io(void *pool_ref)
3779 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3781 io = uma_zalloc(pool->zone, M_WAITOK);
3783 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3786 io->io_hdr.pool = pool_ref;
3791 ctl_alloc_io_nowait(void *pool_ref)
3795 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3797 io = uma_zalloc(pool->zone, M_NOWAIT);
3799 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3802 io->io_hdr.pool = pool_ref;
3807 ctl_free_io(union ctl_io *io)
3810 struct ctl_io_pool *pool;
3817 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3818 uma_zfree(pool->zone, io);
3820 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3825 ctl_zero_io(union ctl_io *io)
3833 * May need to preserve linked list pointers at some point too.
3835 pool_ref = io->io_hdr.pool;
3836 memset(io, 0, sizeof(*io));
3837 io->io_hdr.pool = pool_ref;
3841 ctl_expand_number(const char *buf, uint64_t *num)
3847 number = strtoq(buf, &endptr, 0);
3849 switch (tolower((unsigned char)*endptr)) {
3869 case '\0': /* No unit. */
3873 /* Unrecognized unit. */
3877 if ((number << shift) >> shift != number) {
3881 *num = number << shift;
3887 * This routine could be used in the future to load default and/or saved
3888 * mode page parameters for a particuar lun.
3891 ctl_init_page_index(struct ctl_lun *lun)
3894 struct ctl_page_index *page_index;
3898 memcpy(&lun->mode_pages.index, page_index_template,
3899 sizeof(page_index_template));
3901 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3903 page_index = &lun->mode_pages.index[i];
3904 if (lun->be_lun->lun_type == T_DIRECT &&
3905 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3907 if (lun->be_lun->lun_type == T_PROCESSOR &&
3908 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3910 if (lun->be_lun->lun_type == T_CDROM &&
3911 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3914 page_code = page_index->page_code & SMPH_PC_MASK;
3915 switch (page_code) {
3916 case SMS_RW_ERROR_RECOVERY_PAGE: {
3917 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3918 ("subpage %#x for page %#x is incorrect!",
3919 page_index->subpage, page_code));
3920 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3921 &rw_er_page_default,
3922 sizeof(rw_er_page_default));
3923 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3924 &rw_er_page_changeable,
3925 sizeof(rw_er_page_changeable));
3926 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3927 &rw_er_page_default,
3928 sizeof(rw_er_page_default));
3929 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3930 &rw_er_page_default,
3931 sizeof(rw_er_page_default));
3932 page_index->page_data =
3933 (uint8_t *)lun->mode_pages.rw_er_page;
3936 case SMS_FORMAT_DEVICE_PAGE: {
3937 struct scsi_format_page *format_page;
3939 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3940 ("subpage %#x for page %#x is incorrect!",
3941 page_index->subpage, page_code));
3944 * Sectors per track are set above. Bytes per
3945 * sector need to be set here on a per-LUN basis.
3947 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3948 &format_page_default,
3949 sizeof(format_page_default));
3950 memcpy(&lun->mode_pages.format_page[
3951 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3952 sizeof(format_page_changeable));
3953 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3954 &format_page_default,
3955 sizeof(format_page_default));
3956 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3957 &format_page_default,
3958 sizeof(format_page_default));
3960 format_page = &lun->mode_pages.format_page[
3962 scsi_ulto2b(lun->be_lun->blocksize,
3963 format_page->bytes_per_sector);
3965 format_page = &lun->mode_pages.format_page[
3967 scsi_ulto2b(lun->be_lun->blocksize,
3968 format_page->bytes_per_sector);
3970 format_page = &lun->mode_pages.format_page[
3972 scsi_ulto2b(lun->be_lun->blocksize,
3973 format_page->bytes_per_sector);
3975 page_index->page_data =
3976 (uint8_t *)lun->mode_pages.format_page;
3979 case SMS_RIGID_DISK_PAGE: {
3980 struct scsi_rigid_disk_page *rigid_disk_page;
3981 uint32_t sectors_per_cylinder;
3985 #endif /* !__XSCALE__ */
3987 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3988 ("subpage %#x for page %#x is incorrect!",
3989 page_index->subpage, page_code));
3992 * Rotation rate and sectors per track are set
3993 * above. We calculate the cylinders here based on
3994 * capacity. Due to the number of heads and
3995 * sectors per track we're using, smaller arrays
3996 * may turn out to have 0 cylinders. Linux and
3997 * FreeBSD don't pay attention to these mode pages
3998 * to figure out capacity, but Solaris does. It
3999 * seems to deal with 0 cylinders just fine, and
4000 * works out a fake geometry based on the capacity.
4002 memcpy(&lun->mode_pages.rigid_disk_page[
4003 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4004 sizeof(rigid_disk_page_default));
4005 memcpy(&lun->mode_pages.rigid_disk_page[
4006 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4007 sizeof(rigid_disk_page_changeable));
4009 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4013 * The divide method here will be more accurate,
4014 * probably, but results in floating point being
4015 * used in the kernel on i386 (__udivdi3()). On the
4016 * XScale, though, __udivdi3() is implemented in
4019 * The shift method for cylinder calculation is
4020 * accurate if sectors_per_cylinder is a power of
4021 * 2. Otherwise it might be slightly off -- you
4022 * might have a bit of a truncation problem.
4025 cylinders = (lun->be_lun->maxlba + 1) /
4026 sectors_per_cylinder;
4028 for (shift = 31; shift > 0; shift--) {
4029 if (sectors_per_cylinder & (1 << shift))
4032 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4036 * We've basically got 3 bytes, or 24 bits for the
4037 * cylinder size in the mode page. If we're over,
4038 * just round down to 2^24.
4040 if (cylinders > 0xffffff)
4041 cylinders = 0xffffff;
4043 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4045 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4047 if ((value = ctl_get_opt(&lun->be_lun->options,
4049 scsi_ulto2b(strtol(value, NULL, 0),
4050 rigid_disk_page->rotation_rate);
4053 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4054 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4055 sizeof(rigid_disk_page_default));
4056 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4057 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4058 sizeof(rigid_disk_page_default));
4060 page_index->page_data =
4061 (uint8_t *)lun->mode_pages.rigid_disk_page;
4064 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4065 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4066 ("subpage %#x for page %#x is incorrect!",
4067 page_index->subpage, page_code));
4068 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4069 &verify_er_page_default,
4070 sizeof(verify_er_page_default));
4071 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4072 &verify_er_page_changeable,
4073 sizeof(verify_er_page_changeable));
4074 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4075 &verify_er_page_default,
4076 sizeof(verify_er_page_default));
4077 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4078 &verify_er_page_default,
4079 sizeof(verify_er_page_default));
4080 page_index->page_data =
4081 (uint8_t *)lun->mode_pages.verify_er_page;
4084 case SMS_CACHING_PAGE: {
4085 struct scsi_caching_page *caching_page;
4087 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4088 ("subpage %#x for page %#x is incorrect!",
4089 page_index->subpage, page_code));
4090 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4091 &caching_page_default,
4092 sizeof(caching_page_default));
4093 memcpy(&lun->mode_pages.caching_page[
4094 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4095 sizeof(caching_page_changeable));
4096 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4097 &caching_page_default,
4098 sizeof(caching_page_default));
4099 caching_page = &lun->mode_pages.caching_page[
4101 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4102 if (value != NULL && strcmp(value, "off") == 0)
4103 caching_page->flags1 &= ~SCP_WCE;
4104 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4105 if (value != NULL && strcmp(value, "off") == 0)
4106 caching_page->flags1 |= SCP_RCD;
4107 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4108 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4109 sizeof(caching_page_default));
4110 page_index->page_data =
4111 (uint8_t *)lun->mode_pages.caching_page;
4114 case SMS_CONTROL_MODE_PAGE: {
4115 switch (page_index->subpage) {
4116 case SMS_SUBPAGE_PAGE_0: {
4117 struct scsi_control_page *control_page;
4119 memcpy(&lun->mode_pages.control_page[
4121 &control_page_default,
4122 sizeof(control_page_default));
4123 memcpy(&lun->mode_pages.control_page[
4124 CTL_PAGE_CHANGEABLE],
4125 &control_page_changeable,
4126 sizeof(control_page_changeable));
4127 memcpy(&lun->mode_pages.control_page[
4129 &control_page_default,
4130 sizeof(control_page_default));
4131 control_page = &lun->mode_pages.control_page[
4133 value = ctl_get_opt(&lun->be_lun->options,
4135 if (value != NULL &&
4136 strcmp(value, "unrestricted") == 0) {
4137 control_page->queue_flags &=
4138 ~SCP_QUEUE_ALG_MASK;
4139 control_page->queue_flags |=
4140 SCP_QUEUE_ALG_UNRESTRICTED;
4142 memcpy(&lun->mode_pages.control_page[
4144 &lun->mode_pages.control_page[
4146 sizeof(control_page_default));
4147 page_index->page_data =
4148 (uint8_t *)lun->mode_pages.control_page;
4152 memcpy(&lun->mode_pages.control_ext_page[
4154 &control_ext_page_default,
4155 sizeof(control_ext_page_default));
4156 memcpy(&lun->mode_pages.control_ext_page[
4157 CTL_PAGE_CHANGEABLE],
4158 &control_ext_page_changeable,
4159 sizeof(control_ext_page_changeable));
4160 memcpy(&lun->mode_pages.control_ext_page[
4162 &control_ext_page_default,
4163 sizeof(control_ext_page_default));
4164 memcpy(&lun->mode_pages.control_ext_page[
4166 &lun->mode_pages.control_ext_page[
4168 sizeof(control_ext_page_default));
4169 page_index->page_data =
4170 (uint8_t *)lun->mode_pages.control_ext_page;
4173 panic("subpage %#x for page %#x is incorrect!",
4174 page_index->subpage, page_code);
4178 case SMS_INFO_EXCEPTIONS_PAGE: {
4179 switch (page_index->subpage) {
4180 case SMS_SUBPAGE_PAGE_0:
4181 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4183 sizeof(ie_page_default));
4184 memcpy(&lun->mode_pages.ie_page[
4185 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4186 sizeof(ie_page_changeable));
4187 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4189 sizeof(ie_page_default));
4190 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4192 sizeof(ie_page_default));
4193 page_index->page_data =
4194 (uint8_t *)lun->mode_pages.ie_page;
4197 struct ctl_logical_block_provisioning_page *page;
4199 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4201 sizeof(lbp_page_default));
4202 memcpy(&lun->mode_pages.lbp_page[
4203 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4204 sizeof(lbp_page_changeable));
4205 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4207 sizeof(lbp_page_default));
4208 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4209 value = ctl_get_opt(&lun->be_lun->options,
4211 if (value != NULL &&
4212 ctl_expand_number(value, &ival) == 0) {
4213 page->descr[0].flags |= SLBPPD_ENABLED |
4215 if (lun->be_lun->blocksize)
4216 ival /= lun->be_lun->blocksize;
4219 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4220 page->descr[0].count);
4222 value = ctl_get_opt(&lun->be_lun->options,
4224 if (value != NULL &&
4225 ctl_expand_number(value, &ival) == 0) {
4226 page->descr[1].flags |= SLBPPD_ENABLED |
4228 if (lun->be_lun->blocksize)
4229 ival /= lun->be_lun->blocksize;
4232 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4233 page->descr[1].count);
4235 value = ctl_get_opt(&lun->be_lun->options,
4236 "pool-avail-threshold");
4237 if (value != NULL &&
4238 ctl_expand_number(value, &ival) == 0) {
4239 page->descr[2].flags |= SLBPPD_ENABLED |
4241 if (lun->be_lun->blocksize)
4242 ival /= lun->be_lun->blocksize;
4245 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4246 page->descr[2].count);
4248 value = ctl_get_opt(&lun->be_lun->options,
4249 "pool-used-threshold");
4250 if (value != NULL &&
4251 ctl_expand_number(value, &ival) == 0) {
4252 page->descr[3].flags |= SLBPPD_ENABLED |
4254 if (lun->be_lun->blocksize)
4255 ival /= lun->be_lun->blocksize;
4258 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4259 page->descr[3].count);
4261 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4262 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4263 sizeof(lbp_page_default));
4264 page_index->page_data =
4265 (uint8_t *)lun->mode_pages.lbp_page;
4269 panic("subpage %#x for page %#x is incorrect!",
4270 page_index->subpage, page_code);
4274 case SMS_CDDVD_CAPS_PAGE:{
4275 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4276 ("subpage %#x for page %#x is incorrect!",
4277 page_index->subpage, page_code));
4278 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4279 &cddvd_page_default,
4280 sizeof(cddvd_page_default));
4281 memcpy(&lun->mode_pages.cddvd_page[
4282 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4283 sizeof(cddvd_page_changeable));
4284 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4285 &cddvd_page_default,
4286 sizeof(cddvd_page_default));
4287 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4288 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4289 sizeof(cddvd_page_default));
4290 page_index->page_data =
4291 (uint8_t *)lun->mode_pages.cddvd_page;
4295 panic("invalid page code value %#x", page_code);
4299 return (CTL_RETVAL_COMPLETE);
4303 ctl_init_log_page_index(struct ctl_lun *lun)
4305 struct ctl_page_index *page_index;
4308 memcpy(&lun->log_pages.index, log_page_index_template,
4309 sizeof(log_page_index_template));
4312 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4314 page_index = &lun->log_pages.index[i];
4315 if (lun->be_lun->lun_type == T_DIRECT &&
4316 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4318 if (lun->be_lun->lun_type == T_PROCESSOR &&
4319 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4321 if (lun->be_lun->lun_type == T_CDROM &&
4322 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4325 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4326 lun->backend->lun_attr == NULL)
4329 if (page_index->page_code != prev) {
4330 lun->log_pages.pages_page[j] = page_index->page_code;
4331 prev = page_index->page_code;
4334 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4335 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4338 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4339 lun->log_pages.index[0].page_len = j;
4340 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4341 lun->log_pages.index[1].page_len = k * 2;
4342 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4343 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4344 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4345 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4346 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4347 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4349 return (CTL_RETVAL_COMPLETE);
4353 hex2bin(const char *str, uint8_t *buf, int buf_size)
4358 memset(buf, 0, buf_size);
4359 while (isspace(str[0]))
4361 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4364 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4365 while (str[i] == '-') /* Skip dashes in UUIDs. */
4370 else if (isalpha(c))
4371 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4377 buf[i / 2] |= (c << 4);
4381 return ((i + 1) / 2);
4388 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4389 * wants us to allocate the LUN and he can block.
4390 * - ctl_softc is always set
4391 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4393 * Returns 0 for success, non-zero (errno) for failure.
4396 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4397 struct ctl_be_lun *const be_lun)
4399 struct ctl_lun *nlun, *lun;
4400 struct scsi_vpd_id_descriptor *desc;
4401 struct scsi_vpd_id_t10 *t10id;
4402 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4403 int lun_number, i, lun_malloced;
4404 int devidlen, idlen1, idlen2 = 0, len;
4410 * We currently only support Direct Access or Processor LUN types.
4412 switch (be_lun->lun_type) {
4420 be_lun->lun_config_status(be_lun->be_lun,
4421 CTL_LUN_CONFIG_FAILURE);
4424 if (ctl_lun == NULL) {
4425 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4432 memset(lun, 0, sizeof(*lun));
4434 lun->flags = CTL_LUN_MALLOCED;
4436 /* Generate LUN ID. */
4437 devidlen = max(CTL_DEVID_MIN_LEN,
4438 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4439 idlen1 = sizeof(*t10id) + devidlen;
4440 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4441 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4442 if (scsiname != NULL) {
4443 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4444 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4446 eui = ctl_get_opt(&be_lun->options, "eui");
4448 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4450 naa = ctl_get_opt(&be_lun->options, "naa");
4452 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4454 uuid = ctl_get_opt(&be_lun->options, "uuid");
4456 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4458 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4459 M_CTL, M_WAITOK | M_ZERO);
4460 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4461 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4462 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4463 desc->length = idlen1;
4464 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4465 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4466 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4467 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4469 strncpy(t10id->vendor, vendor,
4470 min(sizeof(t10id->vendor), strlen(vendor)));
4472 strncpy((char *)t10id->vendor_spec_id,
4473 (char *)be_lun->device_id, devidlen);
4474 if (scsiname != NULL) {
4475 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4477 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4478 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4479 SVPD_ID_TYPE_SCSI_NAME;
4480 desc->length = idlen2;
4481 strlcpy(desc->identifier, scsiname, idlen2);
4484 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4486 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4487 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4489 desc->length = hex2bin(eui, desc->identifier, 16);
4490 desc->length = desc->length > 12 ? 16 :
4491 (desc->length > 8 ? 12 : 8);
4492 len -= 16 - desc->length;
4495 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4497 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4498 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4500 desc->length = hex2bin(naa, desc->identifier, 16);
4501 desc->length = desc->length > 8 ? 16 : 8;
4502 len -= 16 - desc->length;
4505 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4507 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4508 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4510 desc->identifier[0] = 0x10;
4511 hex2bin(uuid, &desc->identifier[2], 16);
4514 lun->lun_devid->len = len;
4516 mtx_lock(&ctl_softc->ctl_lock);
4518 * See if the caller requested a particular LUN number. If so, see
4519 * if it is available. Otherwise, allocate the first available LUN.
4521 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4522 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4523 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4524 mtx_unlock(&ctl_softc->ctl_lock);
4525 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4526 printf("ctl: requested LUN ID %d is higher "
4527 "than CTL_MAX_LUNS - 1 (%d)\n",
4528 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4531 * XXX KDM return an error, or just assign
4532 * another LUN ID in this case??
4534 printf("ctl: requested LUN ID %d is already "
4535 "in use\n", be_lun->req_lun_id);
4537 if (lun->flags & CTL_LUN_MALLOCED)
4539 be_lun->lun_config_status(be_lun->be_lun,
4540 CTL_LUN_CONFIG_FAILURE);
4543 lun_number = be_lun->req_lun_id;
4545 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4546 if (lun_number == -1) {
4547 mtx_unlock(&ctl_softc->ctl_lock);
4548 printf("ctl: can't allocate LUN, out of LUNs\n");
4549 if (lun->flags & CTL_LUN_MALLOCED)
4551 be_lun->lun_config_status(be_lun->be_lun,
4552 CTL_LUN_CONFIG_FAILURE);
4556 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4558 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4559 lun->lun = lun_number;
4560 lun->be_lun = be_lun;
4562 * The processor LUN is always enabled. Disk LUNs come on line
4563 * disabled, and must be enabled by the backend.
4565 lun->flags |= CTL_LUN_DISABLED;
4566 lun->backend = be_lun->be;
4567 be_lun->ctl_lun = lun;
4568 be_lun->lun_id = lun_number;
4569 atomic_add_int(&be_lun->be->num_luns, 1);
4570 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4571 lun->flags |= CTL_LUN_EJECTED;
4572 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4573 lun->flags |= CTL_LUN_NO_MEDIA;
4574 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4575 lun->flags |= CTL_LUN_STOPPED;
4577 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4578 lun->flags |= CTL_LUN_PRIMARY_SC;
4580 value = ctl_get_opt(&be_lun->options, "removable");
4581 if (value != NULL) {
4582 if (strcmp(value, "on") == 0)
4583 lun->flags |= CTL_LUN_REMOVABLE;
4584 } else if (be_lun->lun_type == T_CDROM)
4585 lun->flags |= CTL_LUN_REMOVABLE;
4587 lun->ctl_softc = ctl_softc;
4589 lun->last_busy = getsbinuptime();
4591 TAILQ_INIT(&lun->ooa_queue);
4592 TAILQ_INIT(&lun->blocked_queue);
4593 STAILQ_INIT(&lun->error_list);
4594 lun->ie_reported = 1;
4595 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4596 ctl_tpc_lun_init(lun);
4599 * Initialize the mode and log page index.
4601 ctl_init_page_index(lun);
4602 ctl_init_log_page_index(lun);
4605 * Now, before we insert this lun on the lun list, set the lun
4606 * inventory changed UA for all other luns.
4608 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4609 mtx_lock(&nlun->lun_lock);
4610 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4611 mtx_unlock(&nlun->lun_lock);
4614 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4616 ctl_softc->ctl_luns[lun_number] = lun;
4618 ctl_softc->num_luns++;
4620 /* Setup statistics gathering */
4621 lun->stats.device_type = be_lun->lun_type;
4622 lun->stats.lun_number = lun_number;
4623 lun->stats.blocksize = be_lun->blocksize;
4624 if (be_lun->blocksize == 0)
4625 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4626 for (i = 0;i < CTL_MAX_PORTS;i++)
4627 lun->stats.ports[i].targ_port = i;
4629 mtx_unlock(&ctl_softc->ctl_lock);
4631 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4638 * - LUN has already been marked invalid and any pending I/O has been taken
4642 ctl_free_lun(struct ctl_lun *lun)
4644 struct ctl_softc *softc;
4645 struct ctl_lun *nlun;
4648 softc = lun->ctl_softc;
4650 mtx_assert(&softc->ctl_lock, MA_OWNED);
4652 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4654 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4656 softc->ctl_luns[lun->lun] = NULL;
4658 if (!TAILQ_EMPTY(&lun->ooa_queue))
4659 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4664 * Tell the backend to free resources, if this LUN has a backend.
4666 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4667 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4669 lun->ie_reportcnt = UINT32_MAX;
4670 callout_drain(&lun->ie_callout);
4672 ctl_tpc_lun_shutdown(lun);
4673 mtx_destroy(&lun->lun_lock);
4674 free(lun->lun_devid, M_CTL);
4675 for (i = 0; i < CTL_MAX_PORTS; i++)
4676 free(lun->pending_ua[i], M_CTL);
4677 for (i = 0; i < CTL_MAX_PORTS; i++)
4678 free(lun->pr_keys[i], M_CTL);
4679 free(lun->write_buffer, M_CTL);
4680 if (lun->flags & CTL_LUN_MALLOCED)
4683 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4684 mtx_lock(&nlun->lun_lock);
4685 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4686 mtx_unlock(&nlun->lun_lock);
4693 ctl_create_lun(struct ctl_be_lun *be_lun)
4697 * ctl_alloc_lun() should handle all potential failure cases.
4699 ctl_alloc_lun(control_softc, NULL, be_lun);
4703 ctl_add_lun(struct ctl_be_lun *be_lun)
4705 struct ctl_softc *softc = control_softc;
4707 mtx_lock(&softc->ctl_lock);
4708 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4709 mtx_unlock(&softc->ctl_lock);
4710 wakeup(&softc->pending_lun_queue);
4716 ctl_enable_lun(struct ctl_be_lun *be_lun)
4718 struct ctl_softc *softc;
4719 struct ctl_port *port, *nport;
4720 struct ctl_lun *lun;
4723 lun = (struct ctl_lun *)be_lun->ctl_lun;
4724 softc = lun->ctl_softc;
4726 mtx_lock(&softc->ctl_lock);
4727 mtx_lock(&lun->lun_lock);
4728 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4730 * eh? Why did we get called if the LUN is already
4733 mtx_unlock(&lun->lun_lock);
4734 mtx_unlock(&softc->ctl_lock);
4737 lun->flags &= ~CTL_LUN_DISABLED;
4738 mtx_unlock(&lun->lun_lock);
4740 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4741 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4742 port->lun_map != NULL || port->lun_enable == NULL)
4746 * Drop the lock while we call the FETD's enable routine.
4747 * This can lead to a callback into CTL (at least in the
4748 * case of the internal initiator frontend.
4750 mtx_unlock(&softc->ctl_lock);
4751 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4752 mtx_lock(&softc->ctl_lock);
4754 printf("%s: FETD %s port %d returned error "
4755 "%d for lun_enable on lun %jd\n",
4756 __func__, port->port_name, port->targ_port,
4757 retval, (intmax_t)lun->lun);
4761 mtx_unlock(&softc->ctl_lock);
4762 ctl_isc_announce_lun(lun);
4768 ctl_disable_lun(struct ctl_be_lun *be_lun)
4770 struct ctl_softc *softc;
4771 struct ctl_port *port;
4772 struct ctl_lun *lun;
4775 lun = (struct ctl_lun *)be_lun->ctl_lun;
4776 softc = lun->ctl_softc;
4778 mtx_lock(&softc->ctl_lock);
4779 mtx_lock(&lun->lun_lock);
4780 if (lun->flags & CTL_LUN_DISABLED) {
4781 mtx_unlock(&lun->lun_lock);
4782 mtx_unlock(&softc->ctl_lock);
4785 lun->flags |= CTL_LUN_DISABLED;
4786 mtx_unlock(&lun->lun_lock);
4788 STAILQ_FOREACH(port, &softc->port_list, links) {
4789 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4790 port->lun_map != NULL || port->lun_disable == NULL)
4794 * Drop the lock before we call the frontend's disable
4795 * routine, to avoid lock order reversals.
4797 * XXX KDM what happens if the frontend list changes while
4798 * we're traversing it? It's unlikely, but should be handled.
4800 mtx_unlock(&softc->ctl_lock);
4801 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4802 mtx_lock(&softc->ctl_lock);
4804 printf("%s: FETD %s port %d returned error "
4805 "%d for lun_disable on lun %jd\n",
4806 __func__, port->port_name, port->targ_port,
4807 retval, (intmax_t)lun->lun);
4811 mtx_unlock(&softc->ctl_lock);
4812 ctl_isc_announce_lun(lun);
4818 ctl_start_lun(struct ctl_be_lun *be_lun)
4820 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4822 mtx_lock(&lun->lun_lock);
4823 lun->flags &= ~CTL_LUN_STOPPED;
4824 mtx_unlock(&lun->lun_lock);
4829 ctl_stop_lun(struct ctl_be_lun *be_lun)
4831 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4833 mtx_lock(&lun->lun_lock);
4834 lun->flags |= CTL_LUN_STOPPED;
4835 mtx_unlock(&lun->lun_lock);
4840 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4842 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4844 mtx_lock(&lun->lun_lock);
4845 lun->flags |= CTL_LUN_NO_MEDIA;
4846 mtx_unlock(&lun->lun_lock);
4851 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4853 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4854 union ctl_ha_msg msg;
4856 mtx_lock(&lun->lun_lock);
4857 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4858 if (lun->flags & CTL_LUN_REMOVABLE)
4859 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4860 mtx_unlock(&lun->lun_lock);
4861 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4862 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4863 bzero(&msg.ua, sizeof(msg.ua));
4864 msg.hdr.msg_type = CTL_MSG_UA;
4865 msg.hdr.nexus.initid = -1;
4866 msg.hdr.nexus.targ_port = -1;
4867 msg.hdr.nexus.targ_lun = lun->lun;
4868 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4871 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4872 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4879 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4881 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4883 mtx_lock(&lun->lun_lock);
4884 lun->flags |= CTL_LUN_EJECTED;
4885 mtx_unlock(&lun->lun_lock);
4890 ctl_lun_primary(struct ctl_be_lun *be_lun)
4892 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4894 mtx_lock(&lun->lun_lock);
4895 lun->flags |= CTL_LUN_PRIMARY_SC;
4896 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4897 mtx_unlock(&lun->lun_lock);
4898 ctl_isc_announce_lun(lun);
4903 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4905 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4907 mtx_lock(&lun->lun_lock);
4908 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4909 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4910 mtx_unlock(&lun->lun_lock);
4911 ctl_isc_announce_lun(lun);
4916 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4918 struct ctl_softc *softc;
4919 struct ctl_lun *lun;
4921 lun = (struct ctl_lun *)be_lun->ctl_lun;
4922 softc = lun->ctl_softc;
4924 mtx_lock(&lun->lun_lock);
4927 * The LUN needs to be disabled before it can be marked invalid.
4929 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4930 mtx_unlock(&lun->lun_lock);
4934 * Mark the LUN invalid.
4936 lun->flags |= CTL_LUN_INVALID;
4939 * If there is nothing in the OOA queue, go ahead and free the LUN.
4940 * If we have something in the OOA queue, we'll free it when the
4941 * last I/O completes.
4943 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4944 mtx_unlock(&lun->lun_lock);
4945 mtx_lock(&softc->ctl_lock);
4947 mtx_unlock(&softc->ctl_lock);
4949 mtx_unlock(&lun->lun_lock);
4955 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4957 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4958 union ctl_ha_msg msg;
4960 mtx_lock(&lun->lun_lock);
4961 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
4962 mtx_unlock(&lun->lun_lock);
4963 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4964 /* Send msg to other side. */
4965 bzero(&msg.ua, sizeof(msg.ua));
4966 msg.hdr.msg_type = CTL_MSG_UA;
4967 msg.hdr.nexus.initid = -1;
4968 msg.hdr.nexus.targ_port = -1;
4969 msg.hdr.nexus.targ_lun = lun->lun;
4970 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4973 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
4974 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4980 * Backend "memory move is complete" callback for requests that never
4981 * make it down to say RAIDCore's configuration code.
4984 ctl_config_move_done(union ctl_io *io)
4988 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4989 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4990 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4992 if ((io->io_hdr.port_status != 0) &&
4993 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4994 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4996 * For hardware error sense keys, the sense key
4997 * specific value is defined to be a retry count,
4998 * but we use it to pass back an internal FETD
4999 * error code. XXX KDM Hopefully the FETD is only
5000 * using 16 bits for an error code, since that's
5001 * all the space we have in the sks field.
5003 ctl_set_internal_failure(&io->scsiio,
5006 io->io_hdr.port_status);
5009 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5011 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5012 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5013 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5014 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5016 * XXX KDM just assuming a single pointer here, and not a
5017 * S/G list. If we start using S/G lists for config data,
5018 * we'll need to know how to clean them up here as well.
5020 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5021 free(io->scsiio.kern_data_ptr, M_CTL);
5023 retval = CTL_RETVAL_COMPLETE;
5026 * XXX KDM now we need to continue data movement. Some
5028 * - call ctl_scsiio() again? We don't do this for data
5029 * writes, because for those at least we know ahead of
5030 * time where the write will go and how long it is. For
5031 * config writes, though, that information is largely
5032 * contained within the write itself, thus we need to
5033 * parse out the data again.
5035 * - Call some other function once the data is in?
5039 * XXX KDM call ctl_scsiio() again for now, and check flag
5040 * bits to see whether we're allocated or not.
5042 retval = ctl_scsiio(&io->scsiio);
5048 * This gets called by a backend driver when it is done with a
5049 * data_submit method.
5052 ctl_data_submit_done(union ctl_io *io)
5055 * If the IO_CONT flag is set, we need to call the supplied
5056 * function to continue processing the I/O, instead of completing
5059 * If there is an error, though, we don't want to keep processing.
5060 * Instead, just send status back to the initiator.
5062 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5063 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5064 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5065 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5066 io->scsiio.io_cont(io);
5073 * This gets called by a backend driver when it is done with a
5074 * configuration write.
5077 ctl_config_write_done(union ctl_io *io)
5082 * If the IO_CONT flag is set, we need to call the supplied
5083 * function to continue processing the I/O, instead of completing
5086 * If there is an error, though, we don't want to keep processing.
5087 * Instead, just send status back to the initiator.
5089 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5090 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5091 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5092 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5093 io->scsiio.io_cont(io);
5097 * Since a configuration write can be done for commands that actually
5098 * have data allocated, like write buffer, and commands that have
5099 * no data, like start/stop unit, we need to check here.
5101 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5102 buf = io->scsiio.kern_data_ptr;
5111 ctl_config_read_done(union ctl_io *io)
5116 * If there is some error -- we are done, skip data transfer.
5118 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5119 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5120 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5121 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5122 buf = io->scsiio.kern_data_ptr;
5132 * If the IO_CONT flag is set, we need to call the supplied
5133 * function to continue processing the I/O, instead of completing
5136 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5137 io->scsiio.io_cont(io);
5145 * SCSI release command.
5148 ctl_scsi_release(struct ctl_scsiio *ctsio)
5150 struct ctl_lun *lun;
5153 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5155 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5156 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5159 * XXX KDM right now, we only support LUN reservation. We don't
5160 * support 3rd party reservations, or extent reservations, which
5161 * might actually need the parameter list. If we've gotten this
5162 * far, we've got a LUN reservation. Anything else got kicked out
5163 * above. So, according to SPC, ignore the length.
5166 mtx_lock(&lun->lun_lock);
5169 * According to SPC, it is not an error for an intiator to attempt
5170 * to release a reservation on a LUN that isn't reserved, or that
5171 * is reserved by another initiator. The reservation can only be
5172 * released, though, by the initiator who made it or by one of
5173 * several reset type events.
5175 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5176 lun->flags &= ~CTL_LUN_RESERVED;
5178 mtx_unlock(&lun->lun_lock);
5180 ctl_set_success(ctsio);
5181 ctl_done((union ctl_io *)ctsio);
5182 return (CTL_RETVAL_COMPLETE);
5186 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5188 struct ctl_lun *lun;
5191 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5193 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5194 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5197 * XXX KDM right now, we only support LUN reservation. We don't
5198 * support 3rd party reservations, or extent reservations, which
5199 * might actually need the parameter list. If we've gotten this
5200 * far, we've got a LUN reservation. Anything else got kicked out
5201 * above. So, according to SPC, ignore the length.
5204 mtx_lock(&lun->lun_lock);
5205 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5206 ctl_set_reservation_conflict(ctsio);
5210 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5211 if (lun->flags & CTL_LUN_PR_RESERVED) {
5212 ctl_set_success(ctsio);
5216 lun->flags |= CTL_LUN_RESERVED;
5217 lun->res_idx = residx;
5218 ctl_set_success(ctsio);
5221 mtx_unlock(&lun->lun_lock);
5222 ctl_done((union ctl_io *)ctsio);
5223 return (CTL_RETVAL_COMPLETE);
5227 ctl_start_stop(struct ctl_scsiio *ctsio)
5229 struct scsi_start_stop_unit *cdb;
5230 struct ctl_lun *lun;
5233 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5235 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5236 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5238 if ((cdb->how & SSS_PC_MASK) == 0) {
5239 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5240 (cdb->how & SSS_START) == 0) {
5243 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5244 if (ctl_get_prkey(lun, residx) == 0 ||
5245 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5247 ctl_set_reservation_conflict(ctsio);
5248 ctl_done((union ctl_io *)ctsio);
5249 return (CTL_RETVAL_COMPLETE);
5253 if ((cdb->how & SSS_LOEJ) &&
5254 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5255 ctl_set_invalid_field(ctsio,
5261 ctl_done((union ctl_io *)ctsio);
5262 return (CTL_RETVAL_COMPLETE);
5265 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5266 lun->prevent_count > 0) {
5267 /* "Medium removal prevented" */
5268 ctl_set_sense(ctsio, /*current_error*/ 1,
5269 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5270 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5271 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5272 ctl_done((union ctl_io *)ctsio);
5273 return (CTL_RETVAL_COMPLETE);
5277 retval = lun->backend->config_write((union ctl_io *)ctsio);
5282 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5284 struct ctl_lun *lun;
5285 struct scsi_prevent *cdb;
5289 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5291 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5292 cdb = (struct scsi_prevent *)ctsio->cdb;
5294 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) {
5295 ctl_set_invalid_opcode(ctsio);
5296 ctl_done((union ctl_io *)ctsio);
5297 return (CTL_RETVAL_COMPLETE);
5300 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5301 mtx_lock(&lun->lun_lock);
5302 if ((cdb->how & PR_PREVENT) &&
5303 ctl_is_set(lun->prevent, initidx) == 0) {
5304 ctl_set_mask(lun->prevent, initidx);
5305 lun->prevent_count++;
5306 } else if ((cdb->how & PR_PREVENT) == 0 &&
5307 ctl_is_set(lun->prevent, initidx)) {
5308 ctl_clear_mask(lun->prevent, initidx);
5309 lun->prevent_count--;
5311 mtx_unlock(&lun->lun_lock);
5312 retval = lun->backend->config_write((union ctl_io *)ctsio);
5317 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5318 * we don't really do anything with the LBA and length fields if the user
5319 * passes them in. Instead we'll just flush out the cache for the entire
5323 ctl_sync_cache(struct ctl_scsiio *ctsio)
5325 struct ctl_lun *lun;
5326 struct ctl_softc *softc;
5327 struct ctl_lba_len_flags *lbalen;
5328 uint64_t starting_lba;
5329 uint32_t block_count;
5333 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5335 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5336 softc = lun->ctl_softc;
5339 switch (ctsio->cdb[0]) {
5340 case SYNCHRONIZE_CACHE: {
5341 struct scsi_sync_cache *cdb;
5342 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5344 starting_lba = scsi_4btoul(cdb->begin_lba);
5345 block_count = scsi_2btoul(cdb->lb_count);
5349 case SYNCHRONIZE_CACHE_16: {
5350 struct scsi_sync_cache_16 *cdb;
5351 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5353 starting_lba = scsi_8btou64(cdb->begin_lba);
5354 block_count = scsi_4btoul(cdb->lb_count);
5359 ctl_set_invalid_opcode(ctsio);
5360 ctl_done((union ctl_io *)ctsio);
5362 break; /* NOTREACHED */
5366 * We check the LBA and length, but don't do anything with them.
5367 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5368 * get flushed. This check will just help satisfy anyone who wants
5369 * to see an error for an out of range LBA.
5371 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5372 ctl_set_lba_out_of_range(ctsio,
5373 MAX(starting_lba, lun->be_lun->maxlba + 1));
5374 ctl_done((union ctl_io *)ctsio);
5378 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5379 lbalen->lba = starting_lba;
5380 lbalen->len = block_count;
5381 lbalen->flags = byte2;
5382 retval = lun->backend->config_write((union ctl_io *)ctsio);
5389 ctl_format(struct ctl_scsiio *ctsio)
5391 struct scsi_format *cdb;
5392 struct ctl_lun *lun;
5393 int length, defect_list_len;
5395 CTL_DEBUG_PRINT(("ctl_format\n"));
5397 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5399 cdb = (struct scsi_format *)ctsio->cdb;
5402 if (cdb->byte2 & SF_FMTDATA) {
5403 if (cdb->byte2 & SF_LONGLIST)
5404 length = sizeof(struct scsi_format_header_long);
5406 length = sizeof(struct scsi_format_header_short);
5409 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5411 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5412 ctsio->kern_data_len = length;
5413 ctsio->kern_total_len = length;
5414 ctsio->kern_data_resid = 0;
5415 ctsio->kern_rel_offset = 0;
5416 ctsio->kern_sg_entries = 0;
5417 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5418 ctsio->be_move_done = ctl_config_move_done;
5419 ctl_datamove((union ctl_io *)ctsio);
5421 return (CTL_RETVAL_COMPLETE);
5424 defect_list_len = 0;
5426 if (cdb->byte2 & SF_FMTDATA) {
5427 if (cdb->byte2 & SF_LONGLIST) {
5428 struct scsi_format_header_long *header;
5430 header = (struct scsi_format_header_long *)
5431 ctsio->kern_data_ptr;
5433 defect_list_len = scsi_4btoul(header->defect_list_len);
5434 if (defect_list_len != 0) {
5435 ctl_set_invalid_field(ctsio,
5444 struct scsi_format_header_short *header;
5446 header = (struct scsi_format_header_short *)
5447 ctsio->kern_data_ptr;
5449 defect_list_len = scsi_2btoul(header->defect_list_len);
5450 if (defect_list_len != 0) {
5451 ctl_set_invalid_field(ctsio,
5462 ctl_set_success(ctsio);
5465 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5466 free(ctsio->kern_data_ptr, M_CTL);
5467 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5470 ctl_done((union ctl_io *)ctsio);
5471 return (CTL_RETVAL_COMPLETE);
5475 ctl_read_buffer(struct ctl_scsiio *ctsio)
5477 struct ctl_lun *lun;
5478 uint64_t buffer_offset;
5481 static uint8_t descr[4];
5482 static uint8_t echo_descr[4] = { 0 };
5484 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5485 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5486 switch (ctsio->cdb[0]) {
5488 struct scsi_read_buffer *cdb;
5490 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5491 buffer_offset = scsi_3btoul(cdb->offset);
5492 len = scsi_3btoul(cdb->length);
5496 case READ_BUFFER_16: {
5497 struct scsi_read_buffer_16 *cdb;
5499 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5500 buffer_offset = scsi_8btou64(cdb->offset);
5501 len = scsi_4btoul(cdb->length);
5505 default: /* This shouldn't happen. */
5506 ctl_set_invalid_opcode(ctsio);
5507 ctl_done((union ctl_io *)ctsio);
5508 return (CTL_RETVAL_COMPLETE);
5511 if ((byte2 & RWB_MODE) != RWB_MODE_DATA &&
5512 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5513 (byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5514 ctl_set_invalid_field(ctsio,
5520 ctl_done((union ctl_io *)ctsio);
5521 return (CTL_RETVAL_COMPLETE);
5524 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5525 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5526 ctl_set_invalid_field(ctsio,
5532 ctl_done((union ctl_io *)ctsio);
5533 return (CTL_RETVAL_COMPLETE);
5536 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5538 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5539 ctsio->kern_data_ptr = descr;
5540 len = min(len, sizeof(descr));
5541 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5542 ctsio->kern_data_ptr = echo_descr;
5543 len = min(len, sizeof(echo_descr));
5545 if (lun->write_buffer == NULL) {
5546 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5549 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5551 ctsio->kern_data_len = len;
5552 ctsio->kern_total_len = len;
5553 ctsio->kern_data_resid = 0;
5554 ctsio->kern_rel_offset = 0;
5555 ctsio->kern_sg_entries = 0;
5556 ctl_set_success(ctsio);
5557 ctsio->be_move_done = ctl_config_move_done;
5558 ctl_datamove((union ctl_io *)ctsio);
5559 return (CTL_RETVAL_COMPLETE);
5563 ctl_write_buffer(struct ctl_scsiio *ctsio)
5565 struct scsi_write_buffer *cdb;
5566 struct ctl_lun *lun;
5567 int buffer_offset, len;
5569 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5571 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5572 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5574 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5575 ctl_set_invalid_field(ctsio,
5581 ctl_done((union ctl_io *)ctsio);
5582 return (CTL_RETVAL_COMPLETE);
5585 len = scsi_3btoul(cdb->length);
5586 buffer_offset = scsi_3btoul(cdb->offset);
5588 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5589 ctl_set_invalid_field(ctsio,
5595 ctl_done((union ctl_io *)ctsio);
5596 return (CTL_RETVAL_COMPLETE);
5600 * If we've got a kernel request that hasn't been malloced yet,
5601 * malloc it and tell the caller the data buffer is here.
5603 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5604 if (lun->write_buffer == NULL) {
5605 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5608 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5609 ctsio->kern_data_len = len;
5610 ctsio->kern_total_len = len;
5611 ctsio->kern_data_resid = 0;
5612 ctsio->kern_rel_offset = 0;
5613 ctsio->kern_sg_entries = 0;
5614 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5615 ctsio->be_move_done = ctl_config_move_done;
5616 ctl_datamove((union ctl_io *)ctsio);
5618 return (CTL_RETVAL_COMPLETE);
5621 ctl_set_success(ctsio);
5622 ctl_done((union ctl_io *)ctsio);
5623 return (CTL_RETVAL_COMPLETE);
5627 ctl_write_same(struct ctl_scsiio *ctsio)
5629 struct ctl_lun *lun;
5630 struct ctl_lba_len_flags *lbalen;
5632 uint32_t num_blocks;
5636 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5638 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5640 switch (ctsio->cdb[0]) {
5641 case WRITE_SAME_10: {
5642 struct scsi_write_same_10 *cdb;
5644 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5646 lba = scsi_4btoul(cdb->addr);
5647 num_blocks = scsi_2btoul(cdb->length);
5651 case WRITE_SAME_16: {
5652 struct scsi_write_same_16 *cdb;
5654 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5656 lba = scsi_8btou64(cdb->addr);
5657 num_blocks = scsi_4btoul(cdb->length);
5663 * We got a command we don't support. This shouldn't
5664 * happen, commands should be filtered out above us.
5666 ctl_set_invalid_opcode(ctsio);
5667 ctl_done((union ctl_io *)ctsio);
5669 return (CTL_RETVAL_COMPLETE);
5670 break; /* NOTREACHED */
5673 /* ANCHOR flag can be used only together with UNMAP */
5674 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5675 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5676 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5677 ctl_done((union ctl_io *)ctsio);
5678 return (CTL_RETVAL_COMPLETE);
5682 * The first check is to make sure we're in bounds, the second
5683 * check is to catch wrap-around problems. If the lba + num blocks
5684 * is less than the lba, then we've wrapped around and the block
5685 * range is invalid anyway.
5687 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5688 || ((lba + num_blocks) < lba)) {
5689 ctl_set_lba_out_of_range(ctsio,
5690 MAX(lba, lun->be_lun->maxlba + 1));
5691 ctl_done((union ctl_io *)ctsio);
5692 return (CTL_RETVAL_COMPLETE);
5695 /* Zero number of blocks means "to the last logical block" */
5696 if (num_blocks == 0) {
5697 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5698 ctl_set_invalid_field(ctsio,
5704 ctl_done((union ctl_io *)ctsio);
5705 return (CTL_RETVAL_COMPLETE);
5707 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5710 len = lun->be_lun->blocksize;
5713 * If we've got a kernel request that hasn't been malloced yet,
5714 * malloc it and tell the caller the data buffer is here.
5716 if ((byte2 & SWS_NDOB) == 0 &&
5717 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5718 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5719 ctsio->kern_data_len = len;
5720 ctsio->kern_total_len = len;
5721 ctsio->kern_data_resid = 0;
5722 ctsio->kern_rel_offset = 0;
5723 ctsio->kern_sg_entries = 0;
5724 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5725 ctsio->be_move_done = ctl_config_move_done;
5726 ctl_datamove((union ctl_io *)ctsio);
5728 return (CTL_RETVAL_COMPLETE);
5731 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5733 lbalen->len = num_blocks;
5734 lbalen->flags = byte2;
5735 retval = lun->backend->config_write((union ctl_io *)ctsio);
5741 ctl_unmap(struct ctl_scsiio *ctsio)
5743 struct ctl_lun *lun;
5744 struct scsi_unmap *cdb;
5745 struct ctl_ptr_len_flags *ptrlen;
5746 struct scsi_unmap_header *hdr;
5747 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5749 uint32_t num_blocks;
5753 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5755 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5756 cdb = (struct scsi_unmap *)ctsio->cdb;
5758 len = scsi_2btoul(cdb->length);
5762 * If we've got a kernel request that hasn't been malloced yet,
5763 * malloc it and tell the caller the data buffer is here.
5765 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5766 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5767 ctsio->kern_data_len = len;
5768 ctsio->kern_total_len = len;
5769 ctsio->kern_data_resid = 0;
5770 ctsio->kern_rel_offset = 0;
5771 ctsio->kern_sg_entries = 0;
5772 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5773 ctsio->be_move_done = ctl_config_move_done;
5774 ctl_datamove((union ctl_io *)ctsio);
5776 return (CTL_RETVAL_COMPLETE);
5779 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5780 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5781 if (len < sizeof (*hdr) ||
5782 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5783 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5784 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5785 ctl_set_invalid_field(ctsio,
5793 len = scsi_2btoul(hdr->desc_length);
5794 buf = (struct scsi_unmap_desc *)(hdr + 1);
5795 end = buf + len / sizeof(*buf);
5798 for (range = buf; range < end; range++) {
5799 lba = scsi_8btou64(range->lba);
5800 num_blocks = scsi_4btoul(range->length);
5801 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5802 || ((lba + num_blocks) < lba)) {
5803 ctl_set_lba_out_of_range(ctsio,
5804 MAX(lba, lun->be_lun->maxlba + 1));
5805 ctl_done((union ctl_io *)ctsio);
5806 return (CTL_RETVAL_COMPLETE);
5808 if (num_blocks != 0)
5813 * Block backend can not handle zero last range.
5814 * Filter it out and return if there is nothing left.
5816 len = (uint8_t *)endnz - (uint8_t *)buf;
5818 ctl_set_success(ctsio);
5822 mtx_lock(&lun->lun_lock);
5823 ptrlen = (struct ctl_ptr_len_flags *)
5824 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5825 ptrlen->ptr = (void *)buf;
5827 ptrlen->flags = byte2;
5828 ctl_check_blocked(lun);
5829 mtx_unlock(&lun->lun_lock);
5831 retval = lun->backend->config_write((union ctl_io *)ctsio);
5835 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5836 free(ctsio->kern_data_ptr, M_CTL);
5837 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5839 ctl_done((union ctl_io *)ctsio);
5840 return (CTL_RETVAL_COMPLETE);
5844 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5845 struct ctl_page_index *page_index, uint8_t *page_ptr)
5847 struct ctl_lun *lun;
5848 uint8_t *current_cp, *saved_cp;
5852 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5853 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5856 current_cp = (page_index->page_data + (page_index->page_len *
5858 saved_cp = (page_index->page_data + (page_index->page_len *
5861 mtx_lock(&lun->lun_lock);
5862 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5863 memcpy(current_cp, page_ptr, page_index->page_len);
5864 memcpy(saved_cp, page_ptr, page_index->page_len);
5868 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5869 mtx_unlock(&lun->lun_lock);
5871 ctl_isc_announce_mode(lun,
5872 ctl_get_initindex(&ctsio->io_hdr.nexus),
5873 page_index->page_code, page_index->subpage);
5875 return (CTL_RETVAL_COMPLETE);
5879 ctl_ie_timer(void *arg)
5881 struct ctl_lun *lun = arg;
5884 if (lun->ie_asc == 0)
5887 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5888 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5890 lun->ie_reported = 0;
5892 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5893 lun->ie_reportcnt++;
5894 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5895 if (t == 0 || t == UINT32_MAX)
5896 t = 3000; /* 5 min */
5897 callout_schedule(&lun->ie_callout, t * hz / 10);
5902 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5903 struct ctl_page_index *page_index, uint8_t *page_ptr)
5905 struct scsi_info_exceptions_page *pg;
5906 struct ctl_lun *lun;
5909 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5911 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5912 pg = (struct scsi_info_exceptions_page *)page_ptr;
5913 mtx_lock(&lun->lun_lock);
5914 if (pg->info_flags & SIEP_FLAGS_TEST) {
5916 lun->ie_ascq = 0xff;
5917 if (pg->mrie == SIEP_MRIE_UA) {
5918 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5919 lun->ie_reported = 1;
5921 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5922 lun->ie_reported = -1;
5924 lun->ie_reportcnt = 1;
5925 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5926 lun->ie_reportcnt++;
5927 t = scsi_4btoul(pg->interval_timer);
5928 if (t == 0 || t == UINT32_MAX)
5929 t = 3000; /* 5 min */
5930 callout_reset(&lun->ie_callout, t * hz / 10,
5936 lun->ie_reported = 1;
5937 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5938 lun->ie_reportcnt = UINT32_MAX;
5939 callout_stop(&lun->ie_callout);
5941 mtx_unlock(&lun->lun_lock);
5942 return (CTL_RETVAL_COMPLETE);
5946 ctl_do_mode_select(union ctl_io *io)
5948 struct scsi_mode_page_header *page_header;
5949 struct ctl_page_index *page_index;
5950 struct ctl_scsiio *ctsio;
5951 int page_len, page_len_offset, page_len_size;
5952 union ctl_modepage_info *modepage_info;
5953 struct ctl_lun *lun;
5954 int *len_left, *len_used;
5957 ctsio = &io->scsiio;
5960 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5962 modepage_info = (union ctl_modepage_info *)
5963 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5964 len_left = &modepage_info->header.len_left;
5965 len_used = &modepage_info->header.len_used;
5969 page_header = (struct scsi_mode_page_header *)
5970 (ctsio->kern_data_ptr + *len_used);
5972 if (*len_left == 0) {
5973 free(ctsio->kern_data_ptr, M_CTL);
5974 ctl_set_success(ctsio);
5975 ctl_done((union ctl_io *)ctsio);
5976 return (CTL_RETVAL_COMPLETE);
5977 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5979 free(ctsio->kern_data_ptr, M_CTL);
5980 ctl_set_param_len_error(ctsio);
5981 ctl_done((union ctl_io *)ctsio);
5982 return (CTL_RETVAL_COMPLETE);
5984 } else if ((page_header->page_code & SMPH_SPF)
5985 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5987 free(ctsio->kern_data_ptr, M_CTL);
5988 ctl_set_param_len_error(ctsio);
5989 ctl_done((union ctl_io *)ctsio);
5990 return (CTL_RETVAL_COMPLETE);
5995 * XXX KDM should we do something with the block descriptor?
5997 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5998 page_index = &lun->mode_pages.index[i];
5999 if (lun->be_lun->lun_type == T_DIRECT &&
6000 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6002 if (lun->be_lun->lun_type == T_PROCESSOR &&
6003 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6005 if (lun->be_lun->lun_type == T_CDROM &&
6006 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6009 if ((page_index->page_code & SMPH_PC_MASK) !=
6010 (page_header->page_code & SMPH_PC_MASK))
6014 * If neither page has a subpage code, then we've got a
6017 if (((page_index->page_code & SMPH_SPF) == 0)
6018 && ((page_header->page_code & SMPH_SPF) == 0)) {
6019 page_len = page_header->page_length;
6024 * If both pages have subpages, then the subpage numbers
6027 if ((page_index->page_code & SMPH_SPF)
6028 && (page_header->page_code & SMPH_SPF)) {
6029 struct scsi_mode_page_header_sp *sph;
6031 sph = (struct scsi_mode_page_header_sp *)page_header;
6032 if (page_index->subpage == sph->subpage) {
6033 page_len = scsi_2btoul(sph->page_length);
6040 * If we couldn't find the page, or if we don't have a mode select
6041 * handler for it, send back an error to the user.
6043 if ((i >= CTL_NUM_MODE_PAGES)
6044 || (page_index->select_handler == NULL)) {
6045 ctl_set_invalid_field(ctsio,
6048 /*field*/ *len_used,
6051 free(ctsio->kern_data_ptr, M_CTL);
6052 ctl_done((union ctl_io *)ctsio);
6053 return (CTL_RETVAL_COMPLETE);
6056 if (page_index->page_code & SMPH_SPF) {
6057 page_len_offset = 2;
6061 page_len_offset = 1;
6065 * If the length the initiator gives us isn't the one we specify in
6066 * the mode page header, or if they didn't specify enough data in
6067 * the CDB to avoid truncating this page, kick out the request.
6069 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6070 ctl_set_invalid_field(ctsio,
6073 /*field*/ *len_used + page_len_offset,
6076 free(ctsio->kern_data_ptr, M_CTL);
6077 ctl_done((union ctl_io *)ctsio);
6078 return (CTL_RETVAL_COMPLETE);
6080 if (*len_left < page_index->page_len) {
6081 free(ctsio->kern_data_ptr, M_CTL);
6082 ctl_set_param_len_error(ctsio);
6083 ctl_done((union ctl_io *)ctsio);
6084 return (CTL_RETVAL_COMPLETE);
6088 * Run through the mode page, checking to make sure that the bits
6089 * the user changed are actually legal for him to change.
6091 for (i = 0; i < page_index->page_len; i++) {
6092 uint8_t *user_byte, *change_mask, *current_byte;
6096 user_byte = (uint8_t *)page_header + i;
6097 change_mask = page_index->page_data +
6098 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6099 current_byte = page_index->page_data +
6100 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6103 * Check to see whether the user set any bits in this byte
6104 * that he is not allowed to set.
6106 if ((*user_byte & ~(*change_mask)) ==
6107 (*current_byte & ~(*change_mask)))
6111 * Go through bit by bit to determine which one is illegal.
6114 for (j = 7; j >= 0; j--) {
6115 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6116 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6121 ctl_set_invalid_field(ctsio,
6124 /*field*/ *len_used + i,
6127 free(ctsio->kern_data_ptr, M_CTL);
6128 ctl_done((union ctl_io *)ctsio);
6129 return (CTL_RETVAL_COMPLETE);
6133 * Decrement these before we call the page handler, since we may
6134 * end up getting called back one way or another before the handler
6135 * returns to this context.
6137 *len_left -= page_index->page_len;
6138 *len_used += page_index->page_len;
6140 retval = page_index->select_handler(ctsio, page_index,
6141 (uint8_t *)page_header);
6144 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6145 * wait until this queued command completes to finish processing
6146 * the mode page. If it returns anything other than
6147 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6148 * already set the sense information, freed the data pointer, and
6149 * completed the io for us.
6151 if (retval != CTL_RETVAL_COMPLETE)
6152 goto bailout_no_done;
6155 * If the initiator sent us more than one page, parse the next one.
6160 ctl_set_success(ctsio);
6161 free(ctsio->kern_data_ptr, M_CTL);
6162 ctl_done((union ctl_io *)ctsio);
6166 return (CTL_RETVAL_COMPLETE);
6171 ctl_mode_select(struct ctl_scsiio *ctsio)
6173 int param_len, pf, sp;
6174 int header_size, bd_len;
6175 union ctl_modepage_info *modepage_info;
6177 switch (ctsio->cdb[0]) {
6178 case MODE_SELECT_6: {
6179 struct scsi_mode_select_6 *cdb;
6181 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6183 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6184 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6185 param_len = cdb->length;
6186 header_size = sizeof(struct scsi_mode_header_6);
6189 case MODE_SELECT_10: {
6190 struct scsi_mode_select_10 *cdb;
6192 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6194 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6195 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6196 param_len = scsi_2btoul(cdb->length);
6197 header_size = sizeof(struct scsi_mode_header_10);
6201 ctl_set_invalid_opcode(ctsio);
6202 ctl_done((union ctl_io *)ctsio);
6203 return (CTL_RETVAL_COMPLETE);
6208 * "A parameter list length of zero indicates that the Data-Out Buffer
6209 * shall be empty. This condition shall not be considered as an error."
6211 if (param_len == 0) {
6212 ctl_set_success(ctsio);
6213 ctl_done((union ctl_io *)ctsio);
6214 return (CTL_RETVAL_COMPLETE);
6218 * Since we'll hit this the first time through, prior to
6219 * allocation, we don't need to free a data buffer here.
6221 if (param_len < header_size) {
6222 ctl_set_param_len_error(ctsio);
6223 ctl_done((union ctl_io *)ctsio);
6224 return (CTL_RETVAL_COMPLETE);
6228 * Allocate the data buffer and grab the user's data. In theory,
6229 * we shouldn't have to sanity check the parameter list length here
6230 * because the maximum size is 64K. We should be able to malloc
6231 * that much without too many problems.
6233 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6234 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6235 ctsio->kern_data_len = param_len;
6236 ctsio->kern_total_len = param_len;
6237 ctsio->kern_data_resid = 0;
6238 ctsio->kern_rel_offset = 0;
6239 ctsio->kern_sg_entries = 0;
6240 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6241 ctsio->be_move_done = ctl_config_move_done;
6242 ctl_datamove((union ctl_io *)ctsio);
6244 return (CTL_RETVAL_COMPLETE);
6247 switch (ctsio->cdb[0]) {
6248 case MODE_SELECT_6: {
6249 struct scsi_mode_header_6 *mh6;
6251 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6252 bd_len = mh6->blk_desc_len;
6255 case MODE_SELECT_10: {
6256 struct scsi_mode_header_10 *mh10;
6258 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6259 bd_len = scsi_2btoul(mh10->blk_desc_len);
6263 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6266 if (param_len < (header_size + bd_len)) {
6267 free(ctsio->kern_data_ptr, M_CTL);
6268 ctl_set_param_len_error(ctsio);
6269 ctl_done((union ctl_io *)ctsio);
6270 return (CTL_RETVAL_COMPLETE);
6274 * Set the IO_CONT flag, so that if this I/O gets passed to
6275 * ctl_config_write_done(), it'll get passed back to
6276 * ctl_do_mode_select() for further processing, or completion if
6279 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6280 ctsio->io_cont = ctl_do_mode_select;
6282 modepage_info = (union ctl_modepage_info *)
6283 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6284 memset(modepage_info, 0, sizeof(*modepage_info));
6285 modepage_info->header.len_left = param_len - header_size - bd_len;
6286 modepage_info->header.len_used = header_size + bd_len;
6288 return (ctl_do_mode_select((union ctl_io *)ctsio));
6292 ctl_mode_sense(struct ctl_scsiio *ctsio)
6294 struct ctl_lun *lun;
6295 int pc, page_code, dbd, llba, subpage;
6296 int alloc_len, page_len, header_len, total_len;
6297 struct scsi_mode_block_descr *block_desc;
6298 struct ctl_page_index *page_index;
6304 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6306 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6307 switch (ctsio->cdb[0]) {
6308 case MODE_SENSE_6: {
6309 struct scsi_mode_sense_6 *cdb;
6311 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6313 header_len = sizeof(struct scsi_mode_hdr_6);
6314 if (cdb->byte2 & SMS_DBD)
6317 header_len += sizeof(struct scsi_mode_block_descr);
6319 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6320 page_code = cdb->page & SMS_PAGE_CODE;
6321 subpage = cdb->subpage;
6322 alloc_len = cdb->length;
6325 case MODE_SENSE_10: {
6326 struct scsi_mode_sense_10 *cdb;
6328 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6330 header_len = sizeof(struct scsi_mode_hdr_10);
6332 if (cdb->byte2 & SMS_DBD)
6335 header_len += sizeof(struct scsi_mode_block_descr);
6336 if (cdb->byte2 & SMS10_LLBAA)
6338 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6339 page_code = cdb->page & SMS_PAGE_CODE;
6340 subpage = cdb->subpage;
6341 alloc_len = scsi_2btoul(cdb->length);
6345 ctl_set_invalid_opcode(ctsio);
6346 ctl_done((union ctl_io *)ctsio);
6347 return (CTL_RETVAL_COMPLETE);
6348 break; /* NOTREACHED */
6352 * We have to make a first pass through to calculate the size of
6353 * the pages that match the user's query. Then we allocate enough
6354 * memory to hold it, and actually copy the data into the buffer.
6356 switch (page_code) {
6357 case SMS_ALL_PAGES_PAGE: {
6363 * At the moment, values other than 0 and 0xff here are
6364 * reserved according to SPC-3.
6366 if ((subpage != SMS_SUBPAGE_PAGE_0)
6367 && (subpage != SMS_SUBPAGE_ALL)) {
6368 ctl_set_invalid_field(ctsio,
6374 ctl_done((union ctl_io *)ctsio);
6375 return (CTL_RETVAL_COMPLETE);
6378 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6379 page_index = &lun->mode_pages.index[i];
6381 /* Make sure the page is supported for this dev type */
6382 if (lun->be_lun->lun_type == T_DIRECT &&
6383 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6385 if (lun->be_lun->lun_type == T_PROCESSOR &&
6386 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6388 if (lun->be_lun->lun_type == T_CDROM &&
6389 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6393 * We don't use this subpage if the user didn't
6394 * request all subpages.
6396 if ((page_index->subpage != 0)
6397 && (subpage == SMS_SUBPAGE_PAGE_0))
6401 printf("found page %#x len %d\n",
6402 page_index->page_code & SMPH_PC_MASK,
6403 page_index->page_len);
6405 page_len += page_index->page_len;
6414 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6415 page_index = &lun->mode_pages.index[i];
6417 /* Make sure the page is supported for this dev type */
6418 if (lun->be_lun->lun_type == T_DIRECT &&
6419 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6421 if (lun->be_lun->lun_type == T_PROCESSOR &&
6422 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6424 if (lun->be_lun->lun_type == T_CDROM &&
6425 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6428 /* Look for the right page code */
6429 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6432 /* Look for the right subpage or the subpage wildcard*/
6433 if ((page_index->subpage != subpage)
6434 && (subpage != SMS_SUBPAGE_ALL))
6438 printf("found page %#x len %d\n",
6439 page_index->page_code & SMPH_PC_MASK,
6440 page_index->page_len);
6443 page_len += page_index->page_len;
6446 if (page_len == 0) {
6447 ctl_set_invalid_field(ctsio,
6453 ctl_done((union ctl_io *)ctsio);
6454 return (CTL_RETVAL_COMPLETE);
6460 total_len = header_len + page_len;
6462 printf("header_len = %d, page_len = %d, total_len = %d\n",
6463 header_len, page_len, total_len);
6466 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6467 ctsio->kern_sg_entries = 0;
6468 ctsio->kern_data_resid = 0;
6469 ctsio->kern_rel_offset = 0;
6470 if (total_len < alloc_len) {
6471 ctsio->residual = alloc_len - total_len;
6472 ctsio->kern_data_len = total_len;
6473 ctsio->kern_total_len = total_len;
6475 ctsio->residual = 0;
6476 ctsio->kern_data_len = alloc_len;
6477 ctsio->kern_total_len = alloc_len;
6480 switch (ctsio->cdb[0]) {
6481 case MODE_SENSE_6: {
6482 struct scsi_mode_hdr_6 *header;
6484 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6486 header->datalen = MIN(total_len - 1, 254);
6487 if (lun->be_lun->lun_type == T_DIRECT) {
6488 header->dev_specific = 0x10; /* DPOFUA */
6489 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6490 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6491 header->dev_specific |= 0x80; /* WP */
6494 header->block_descr_len = 0;
6496 header->block_descr_len =
6497 sizeof(struct scsi_mode_block_descr);
6498 block_desc = (struct scsi_mode_block_descr *)&header[1];
6501 case MODE_SENSE_10: {
6502 struct scsi_mode_hdr_10 *header;
6505 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6507 datalen = MIN(total_len - 2, 65533);
6508 scsi_ulto2b(datalen, header->datalen);
6509 if (lun->be_lun->lun_type == T_DIRECT) {
6510 header->dev_specific = 0x10; /* DPOFUA */
6511 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6512 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6513 header->dev_specific |= 0x80; /* WP */
6516 scsi_ulto2b(0, header->block_descr_len);
6518 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6519 header->block_descr_len);
6520 block_desc = (struct scsi_mode_block_descr *)&header[1];
6524 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6528 * If we've got a disk, use its blocksize in the block
6529 * descriptor. Otherwise, just set it to 0.
6532 if (lun->be_lun->lun_type == T_DIRECT)
6533 scsi_ulto3b(lun->be_lun->blocksize,
6534 block_desc->block_len);
6536 scsi_ulto3b(0, block_desc->block_len);
6539 switch (page_code) {
6540 case SMS_ALL_PAGES_PAGE: {
6543 data_used = header_len;
6544 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6545 struct ctl_page_index *page_index;
6547 page_index = &lun->mode_pages.index[i];
6548 if (lun->be_lun->lun_type == T_DIRECT &&
6549 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6551 if (lun->be_lun->lun_type == T_PROCESSOR &&
6552 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6554 if (lun->be_lun->lun_type == T_CDROM &&
6555 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6559 * We don't use this subpage if the user didn't
6560 * request all subpages. We already checked (above)
6561 * to make sure the user only specified a subpage
6562 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6564 if ((page_index->subpage != 0)
6565 && (subpage == SMS_SUBPAGE_PAGE_0))
6569 * Call the handler, if it exists, to update the
6570 * page to the latest values.
6572 if (page_index->sense_handler != NULL)
6573 page_index->sense_handler(ctsio, page_index,pc);
6575 memcpy(ctsio->kern_data_ptr + data_used,
6576 page_index->page_data +
6577 (page_index->page_len * pc),
6578 page_index->page_len);
6579 data_used += page_index->page_len;
6586 data_used = header_len;
6588 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6589 struct ctl_page_index *page_index;
6591 page_index = &lun->mode_pages.index[i];
6593 /* Look for the right page code */
6594 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6597 /* Look for the right subpage or the subpage wildcard*/
6598 if ((page_index->subpage != subpage)
6599 && (subpage != SMS_SUBPAGE_ALL))
6602 /* Make sure the page is supported for this dev type */
6603 if (lun->be_lun->lun_type == T_DIRECT &&
6604 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6606 if (lun->be_lun->lun_type == T_PROCESSOR &&
6607 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6609 if (lun->be_lun->lun_type == T_CDROM &&
6610 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6614 * Call the handler, if it exists, to update the
6615 * page to the latest values.
6617 if (page_index->sense_handler != NULL)
6618 page_index->sense_handler(ctsio, page_index,pc);
6620 memcpy(ctsio->kern_data_ptr + data_used,
6621 page_index->page_data +
6622 (page_index->page_len * pc),
6623 page_index->page_len);
6624 data_used += page_index->page_len;
6630 ctl_set_success(ctsio);
6631 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6632 ctsio->be_move_done = ctl_config_move_done;
6633 ctl_datamove((union ctl_io *)ctsio);
6634 return (CTL_RETVAL_COMPLETE);
6638 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6639 struct ctl_page_index *page_index,
6642 struct ctl_lun *lun;
6643 struct scsi_log_param_header *phdr;
6647 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6648 data = page_index->page_data;
6650 if (lun->backend->lun_attr != NULL &&
6651 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6653 phdr = (struct scsi_log_param_header *)data;
6654 scsi_ulto2b(0x0001, phdr->param_code);
6655 phdr->param_control = SLP_LBIN | SLP_LP;
6656 phdr->param_len = 8;
6657 data = (uint8_t *)(phdr + 1);
6658 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6659 data[4] = 0x02; /* per-pool */
6660 data += phdr->param_len;
6663 if (lun->backend->lun_attr != NULL &&
6664 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6666 phdr = (struct scsi_log_param_header *)data;
6667 scsi_ulto2b(0x0002, phdr->param_code);
6668 phdr->param_control = SLP_LBIN | SLP_LP;
6669 phdr->param_len = 8;
6670 data = (uint8_t *)(phdr + 1);
6671 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6672 data[4] = 0x01; /* per-LUN */
6673 data += phdr->param_len;
6676 if (lun->backend->lun_attr != NULL &&
6677 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6679 phdr = (struct scsi_log_param_header *)data;
6680 scsi_ulto2b(0x00f1, phdr->param_code);
6681 phdr->param_control = SLP_LBIN | SLP_LP;
6682 phdr->param_len = 8;
6683 data = (uint8_t *)(phdr + 1);
6684 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6685 data[4] = 0x02; /* per-pool */
6686 data += phdr->param_len;
6689 if (lun->backend->lun_attr != NULL &&
6690 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6692 phdr = (struct scsi_log_param_header *)data;
6693 scsi_ulto2b(0x00f2, phdr->param_code);
6694 phdr->param_control = SLP_LBIN | SLP_LP;
6695 phdr->param_len = 8;
6696 data = (uint8_t *)(phdr + 1);
6697 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6698 data[4] = 0x02; /* per-pool */
6699 data += phdr->param_len;
6702 page_index->page_len = data - page_index->page_data;
6707 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6708 struct ctl_page_index *page_index,
6711 struct ctl_lun *lun;
6712 struct stat_page *data;
6713 uint64_t rn, wn, rb, wb;
6714 struct bintime rt, wt;
6717 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6718 data = (struct stat_page *)page_index->page_data;
6720 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6721 data->sap.hdr.param_control = SLP_LBIN;
6722 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6723 sizeof(struct scsi_log_param_header);
6724 rn = wn = rb = wb = 0;
6727 for (i = 0; i < CTL_MAX_PORTS; i++) {
6728 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6729 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6730 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6731 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6732 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6733 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6735 scsi_u64to8b(rn, data->sap.read_num);
6736 scsi_u64to8b(wn, data->sap.write_num);
6737 if (lun->stats.blocksize > 0) {
6738 scsi_u64to8b(wb / lun->stats.blocksize,
6739 data->sap.recvieved_lba);
6740 scsi_u64to8b(rb / lun->stats.blocksize,
6741 data->sap.transmitted_lba);
6743 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6744 data->sap.read_int);
6745 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6746 data->sap.write_int);
6747 scsi_u64to8b(0, data->sap.weighted_num);
6748 scsi_u64to8b(0, data->sap.weighted_int);
6749 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6750 data->it.hdr.param_control = SLP_LBIN;
6751 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6752 sizeof(struct scsi_log_param_header);
6754 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6756 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6757 data->it.hdr.param_control = SLP_LBIN;
6758 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6759 sizeof(struct scsi_log_param_header);
6760 scsi_ulto4b(3, data->ti.exponent);
6761 scsi_ulto4b(1, data->ti.integer);
6766 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6767 struct ctl_page_index *page_index,
6770 struct ctl_lun *lun;
6771 struct scsi_log_informational_exceptions *data;
6773 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6774 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6776 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6777 data->hdr.param_control = SLP_LBIN;
6778 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6779 sizeof(struct scsi_log_param_header);
6780 data->ie_asc = lun->ie_asc;
6781 data->ie_ascq = lun->ie_ascq;
6782 data->temperature = 0xff;
6787 ctl_log_sense(struct ctl_scsiio *ctsio)
6789 struct ctl_lun *lun;
6790 int i, pc, page_code, subpage;
6791 int alloc_len, total_len;
6792 struct ctl_page_index *page_index;
6793 struct scsi_log_sense *cdb;
6794 struct scsi_log_header *header;
6796 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6798 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6799 cdb = (struct scsi_log_sense *)ctsio->cdb;
6800 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6801 page_code = cdb->page & SLS_PAGE_CODE;
6802 subpage = cdb->subpage;
6803 alloc_len = scsi_2btoul(cdb->length);
6806 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6807 page_index = &lun->log_pages.index[i];
6809 /* Look for the right page code */
6810 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6813 /* Look for the right subpage or the subpage wildcard*/
6814 if (page_index->subpage != subpage)
6819 if (i >= CTL_NUM_LOG_PAGES) {
6820 ctl_set_invalid_field(ctsio,
6826 ctl_done((union ctl_io *)ctsio);
6827 return (CTL_RETVAL_COMPLETE);
6830 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6832 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6833 ctsio->kern_sg_entries = 0;
6834 ctsio->kern_data_resid = 0;
6835 ctsio->kern_rel_offset = 0;
6836 if (total_len < alloc_len) {
6837 ctsio->residual = alloc_len - total_len;
6838 ctsio->kern_data_len = total_len;
6839 ctsio->kern_total_len = total_len;
6841 ctsio->residual = 0;
6842 ctsio->kern_data_len = alloc_len;
6843 ctsio->kern_total_len = alloc_len;
6846 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6847 header->page = page_index->page_code;
6848 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6849 header->page |= SL_DS;
6850 if (page_index->subpage) {
6851 header->page |= SL_SPF;
6852 header->subpage = page_index->subpage;
6854 scsi_ulto2b(page_index->page_len, header->datalen);
6857 * Call the handler, if it exists, to update the
6858 * page to the latest values.
6860 if (page_index->sense_handler != NULL)
6861 page_index->sense_handler(ctsio, page_index, pc);
6863 memcpy(header + 1, page_index->page_data, page_index->page_len);
6865 ctl_set_success(ctsio);
6866 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6867 ctsio->be_move_done = ctl_config_move_done;
6868 ctl_datamove((union ctl_io *)ctsio);
6869 return (CTL_RETVAL_COMPLETE);
6873 ctl_read_capacity(struct ctl_scsiio *ctsio)
6875 struct scsi_read_capacity *cdb;
6876 struct scsi_read_capacity_data *data;
6877 struct ctl_lun *lun;
6880 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6882 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6884 lba = scsi_4btoul(cdb->addr);
6885 if (((cdb->pmi & SRC_PMI) == 0)
6887 ctl_set_invalid_field(/*ctsio*/ ctsio,
6893 ctl_done((union ctl_io *)ctsio);
6894 return (CTL_RETVAL_COMPLETE);
6897 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6899 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6900 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6901 ctsio->residual = 0;
6902 ctsio->kern_data_len = sizeof(*data);
6903 ctsio->kern_total_len = sizeof(*data);
6904 ctsio->kern_data_resid = 0;
6905 ctsio->kern_rel_offset = 0;
6906 ctsio->kern_sg_entries = 0;
6909 * If the maximum LBA is greater than 0xfffffffe, the user must
6910 * issue a SERVICE ACTION IN (16) command, with the read capacity
6911 * serivce action set.
6913 if (lun->be_lun->maxlba > 0xfffffffe)
6914 scsi_ulto4b(0xffffffff, data->addr);
6916 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6919 * XXX KDM this may not be 512 bytes...
6921 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6923 ctl_set_success(ctsio);
6924 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6925 ctsio->be_move_done = ctl_config_move_done;
6926 ctl_datamove((union ctl_io *)ctsio);
6927 return (CTL_RETVAL_COMPLETE);
6931 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6933 struct scsi_read_capacity_16 *cdb;
6934 struct scsi_read_capacity_data_long *data;
6935 struct ctl_lun *lun;
6939 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6941 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6943 alloc_len = scsi_4btoul(cdb->alloc_len);
6944 lba = scsi_8btou64(cdb->addr);
6946 if ((cdb->reladr & SRC16_PMI)
6948 ctl_set_invalid_field(/*ctsio*/ ctsio,
6954 ctl_done((union ctl_io *)ctsio);
6955 return (CTL_RETVAL_COMPLETE);
6958 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6960 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6961 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6963 if (sizeof(*data) < alloc_len) {
6964 ctsio->residual = alloc_len - sizeof(*data);
6965 ctsio->kern_data_len = sizeof(*data);
6966 ctsio->kern_total_len = sizeof(*data);
6968 ctsio->residual = 0;
6969 ctsio->kern_data_len = alloc_len;
6970 ctsio->kern_total_len = alloc_len;
6972 ctsio->kern_data_resid = 0;
6973 ctsio->kern_rel_offset = 0;
6974 ctsio->kern_sg_entries = 0;
6976 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6977 /* XXX KDM this may not be 512 bytes... */
6978 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6979 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6980 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6981 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6982 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6984 ctl_set_success(ctsio);
6985 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6986 ctsio->be_move_done = ctl_config_move_done;
6987 ctl_datamove((union ctl_io *)ctsio);
6988 return (CTL_RETVAL_COMPLETE);
6992 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6994 struct scsi_get_lba_status *cdb;
6995 struct scsi_get_lba_status_data *data;
6996 struct ctl_lun *lun;
6997 struct ctl_lba_len_flags *lbalen;
6999 uint32_t alloc_len, total_len;
7002 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7004 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7005 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7006 lba = scsi_8btou64(cdb->addr);
7007 alloc_len = scsi_4btoul(cdb->alloc_len);
7009 if (lba > lun->be_lun->maxlba) {
7010 ctl_set_lba_out_of_range(ctsio, lba);
7011 ctl_done((union ctl_io *)ctsio);
7012 return (CTL_RETVAL_COMPLETE);
7015 total_len = sizeof(*data) + sizeof(data->descr[0]);
7016 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7017 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7019 if (total_len < alloc_len) {
7020 ctsio->residual = alloc_len - total_len;
7021 ctsio->kern_data_len = total_len;
7022 ctsio->kern_total_len = total_len;
7024 ctsio->residual = 0;
7025 ctsio->kern_data_len = alloc_len;
7026 ctsio->kern_total_len = alloc_len;
7028 ctsio->kern_data_resid = 0;
7029 ctsio->kern_rel_offset = 0;
7030 ctsio->kern_sg_entries = 0;
7032 /* Fill dummy data in case backend can't tell anything. */
7033 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7034 scsi_u64to8b(lba, data->descr[0].addr);
7035 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7036 data->descr[0].length);
7037 data->descr[0].status = 0; /* Mapped or unknown. */
7039 ctl_set_success(ctsio);
7040 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7041 ctsio->be_move_done = ctl_config_move_done;
7043 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7045 lbalen->len = total_len;
7047 retval = lun->backend->config_read((union ctl_io *)ctsio);
7048 return (CTL_RETVAL_COMPLETE);
7052 ctl_read_defect(struct ctl_scsiio *ctsio)
7054 struct scsi_read_defect_data_10 *ccb10;
7055 struct scsi_read_defect_data_12 *ccb12;
7056 struct scsi_read_defect_data_hdr_10 *data10;
7057 struct scsi_read_defect_data_hdr_12 *data12;
7058 uint32_t alloc_len, data_len;
7061 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7063 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7064 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7065 format = ccb10->format;
7066 alloc_len = scsi_2btoul(ccb10->alloc_length);
7067 data_len = sizeof(*data10);
7069 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7070 format = ccb12->format;
7071 alloc_len = scsi_4btoul(ccb12->alloc_length);
7072 data_len = sizeof(*data12);
7074 if (alloc_len == 0) {
7075 ctl_set_success(ctsio);
7076 ctl_done((union ctl_io *)ctsio);
7077 return (CTL_RETVAL_COMPLETE);
7080 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7081 if (data_len < alloc_len) {
7082 ctsio->residual = alloc_len - data_len;
7083 ctsio->kern_data_len = data_len;
7084 ctsio->kern_total_len = data_len;
7086 ctsio->residual = 0;
7087 ctsio->kern_data_len = alloc_len;
7088 ctsio->kern_total_len = alloc_len;
7090 ctsio->kern_data_resid = 0;
7091 ctsio->kern_rel_offset = 0;
7092 ctsio->kern_sg_entries = 0;
7094 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7095 data10 = (struct scsi_read_defect_data_hdr_10 *)
7096 ctsio->kern_data_ptr;
7097 data10->format = format;
7098 scsi_ulto2b(0, data10->length);
7100 data12 = (struct scsi_read_defect_data_hdr_12 *)
7101 ctsio->kern_data_ptr;
7102 data12->format = format;
7103 scsi_ulto2b(0, data12->generation);
7104 scsi_ulto4b(0, data12->length);
7107 ctl_set_success(ctsio);
7108 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7109 ctsio->be_move_done = ctl_config_move_done;
7110 ctl_datamove((union ctl_io *)ctsio);
7111 return (CTL_RETVAL_COMPLETE);
7115 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7117 struct scsi_maintenance_in *cdb;
7119 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7120 int num_ha_groups, num_target_ports, shared_group;
7121 struct ctl_lun *lun;
7122 struct ctl_softc *softc;
7123 struct ctl_port *port;
7124 struct scsi_target_group_data *rtg_ptr;
7125 struct scsi_target_group_data_extended *rtg_ext_ptr;
7126 struct scsi_target_port_group_descriptor *tpg_desc;
7128 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7130 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7131 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7132 softc = lun->ctl_softc;
7134 retval = CTL_RETVAL_COMPLETE;
7136 switch (cdb->byte2 & STG_PDF_MASK) {
7137 case STG_PDF_LENGTH:
7140 case STG_PDF_EXTENDED:
7144 ctl_set_invalid_field(/*ctsio*/ ctsio,
7150 ctl_done((union ctl_io *)ctsio);
7154 num_target_ports = 0;
7155 shared_group = (softc->is_single != 0);
7156 mtx_lock(&softc->ctl_lock);
7157 STAILQ_FOREACH(port, &softc->port_list, links) {
7158 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7160 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7163 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7166 mtx_unlock(&softc->ctl_lock);
7167 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7170 total_len = sizeof(struct scsi_target_group_data_extended);
7172 total_len = sizeof(struct scsi_target_group_data);
7173 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7174 (shared_group + num_ha_groups) +
7175 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7177 alloc_len = scsi_4btoul(cdb->length);
7179 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7181 ctsio->kern_sg_entries = 0;
7183 if (total_len < alloc_len) {
7184 ctsio->residual = alloc_len - total_len;
7185 ctsio->kern_data_len = total_len;
7186 ctsio->kern_total_len = total_len;
7188 ctsio->residual = 0;
7189 ctsio->kern_data_len = alloc_len;
7190 ctsio->kern_total_len = alloc_len;
7192 ctsio->kern_data_resid = 0;
7193 ctsio->kern_rel_offset = 0;
7196 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7197 ctsio->kern_data_ptr;
7198 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7199 rtg_ext_ptr->format_type = 0x10;
7200 rtg_ext_ptr->implicit_transition_time = 0;
7201 tpg_desc = &rtg_ext_ptr->groups[0];
7203 rtg_ptr = (struct scsi_target_group_data *)
7204 ctsio->kern_data_ptr;
7205 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7206 tpg_desc = &rtg_ptr->groups[0];
7209 mtx_lock(&softc->ctl_lock);
7210 pg = softc->port_min / softc->port_cnt;
7211 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7212 /* Some shelf is known to be primary. */
7213 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7214 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7215 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7216 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7217 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7218 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7220 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7221 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7222 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7225 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7228 /* No known primary shelf. */
7229 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7230 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7231 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7232 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7233 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7234 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7236 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7240 tpg_desc->pref_state = ts;
7241 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7242 TPG_U_SUP | TPG_T_SUP;
7243 scsi_ulto2b(1, tpg_desc->target_port_group);
7244 tpg_desc->status = TPG_IMPLICIT;
7246 STAILQ_FOREACH(port, &softc->port_list, links) {
7247 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7249 if (!softc->is_single &&
7250 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7252 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7254 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7255 relative_target_port_identifier);
7258 tpg_desc->target_port_count = pc;
7259 tpg_desc = (struct scsi_target_port_group_descriptor *)
7260 &tpg_desc->descriptors[pc];
7262 for (g = 0; g < num_ha_groups; g++) {
7263 tpg_desc->pref_state = (g == pg) ? ts : os;
7264 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7265 TPG_U_SUP | TPG_T_SUP;
7266 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7267 tpg_desc->status = TPG_IMPLICIT;
7269 STAILQ_FOREACH(port, &softc->port_list, links) {
7270 if (port->targ_port < g * softc->port_cnt ||
7271 port->targ_port >= (g + 1) * softc->port_cnt)
7273 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7275 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7277 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7279 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7280 relative_target_port_identifier);
7283 tpg_desc->target_port_count = pc;
7284 tpg_desc = (struct scsi_target_port_group_descriptor *)
7285 &tpg_desc->descriptors[pc];
7287 mtx_unlock(&softc->ctl_lock);
7289 ctl_set_success(ctsio);
7290 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7291 ctsio->be_move_done = ctl_config_move_done;
7292 ctl_datamove((union ctl_io *)ctsio);
7297 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7299 struct ctl_lun *lun;
7300 struct scsi_report_supported_opcodes *cdb;
7301 const struct ctl_cmd_entry *entry, *sentry;
7302 struct scsi_report_supported_opcodes_all *all;
7303 struct scsi_report_supported_opcodes_descr *descr;
7304 struct scsi_report_supported_opcodes_one *one;
7306 int alloc_len, total_len;
7307 int opcode, service_action, i, j, num;
7309 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7311 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7312 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7314 retval = CTL_RETVAL_COMPLETE;
7316 opcode = cdb->requested_opcode;
7317 service_action = scsi_2btoul(cdb->requested_service_action);
7318 switch (cdb->options & RSO_OPTIONS_MASK) {
7319 case RSO_OPTIONS_ALL:
7321 for (i = 0; i < 256; i++) {
7322 entry = &ctl_cmd_table[i];
7323 if (entry->flags & CTL_CMD_FLAG_SA5) {
7324 for (j = 0; j < 32; j++) {
7325 sentry = &((const struct ctl_cmd_entry *)
7327 if (ctl_cmd_applicable(
7328 lun->be_lun->lun_type, sentry))
7332 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7337 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7338 num * sizeof(struct scsi_report_supported_opcodes_descr);
7340 case RSO_OPTIONS_OC:
7341 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7342 ctl_set_invalid_field(/*ctsio*/ ctsio,
7348 ctl_done((union ctl_io *)ctsio);
7349 return (CTL_RETVAL_COMPLETE);
7351 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7353 case RSO_OPTIONS_OC_SA:
7354 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7355 service_action >= 32) {
7356 ctl_set_invalid_field(/*ctsio*/ ctsio,
7362 ctl_done((union ctl_io *)ctsio);
7363 return (CTL_RETVAL_COMPLETE);
7365 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7368 ctl_set_invalid_field(/*ctsio*/ ctsio,
7374 ctl_done((union ctl_io *)ctsio);
7375 return (CTL_RETVAL_COMPLETE);
7378 alloc_len = scsi_4btoul(cdb->length);
7380 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7382 ctsio->kern_sg_entries = 0;
7384 if (total_len < alloc_len) {
7385 ctsio->residual = alloc_len - total_len;
7386 ctsio->kern_data_len = total_len;
7387 ctsio->kern_total_len = total_len;
7389 ctsio->residual = 0;
7390 ctsio->kern_data_len = alloc_len;
7391 ctsio->kern_total_len = alloc_len;
7393 ctsio->kern_data_resid = 0;
7394 ctsio->kern_rel_offset = 0;
7396 switch (cdb->options & RSO_OPTIONS_MASK) {
7397 case RSO_OPTIONS_ALL:
7398 all = (struct scsi_report_supported_opcodes_all *)
7399 ctsio->kern_data_ptr;
7401 for (i = 0; i < 256; i++) {
7402 entry = &ctl_cmd_table[i];
7403 if (entry->flags & CTL_CMD_FLAG_SA5) {
7404 for (j = 0; j < 32; j++) {
7405 sentry = &((const struct ctl_cmd_entry *)
7407 if (!ctl_cmd_applicable(
7408 lun->be_lun->lun_type, sentry))
7410 descr = &all->descr[num++];
7412 scsi_ulto2b(j, descr->service_action);
7413 descr->flags = RSO_SERVACTV;
7414 scsi_ulto2b(sentry->length,
7418 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7421 descr = &all->descr[num++];
7423 scsi_ulto2b(0, descr->service_action);
7425 scsi_ulto2b(entry->length, descr->cdb_length);
7429 num * sizeof(struct scsi_report_supported_opcodes_descr),
7432 case RSO_OPTIONS_OC:
7433 one = (struct scsi_report_supported_opcodes_one *)
7434 ctsio->kern_data_ptr;
7435 entry = &ctl_cmd_table[opcode];
7437 case RSO_OPTIONS_OC_SA:
7438 one = (struct scsi_report_supported_opcodes_one *)
7439 ctsio->kern_data_ptr;
7440 entry = &ctl_cmd_table[opcode];
7441 entry = &((const struct ctl_cmd_entry *)
7442 entry->execute)[service_action];
7444 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7446 scsi_ulto2b(entry->length, one->cdb_length);
7447 one->cdb_usage[0] = opcode;
7448 memcpy(&one->cdb_usage[1], entry->usage,
7455 ctl_set_success(ctsio);
7456 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7457 ctsio->be_move_done = ctl_config_move_done;
7458 ctl_datamove((union ctl_io *)ctsio);
7463 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7465 struct scsi_report_supported_tmf *cdb;
7466 struct scsi_report_supported_tmf_data *data;
7468 int alloc_len, total_len;
7470 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7472 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7474 retval = CTL_RETVAL_COMPLETE;
7476 total_len = sizeof(struct scsi_report_supported_tmf_data);
7477 alloc_len = scsi_4btoul(cdb->length);
7479 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7481 ctsio->kern_sg_entries = 0;
7483 if (total_len < alloc_len) {
7484 ctsio->residual = alloc_len - total_len;
7485 ctsio->kern_data_len = total_len;
7486 ctsio->kern_total_len = total_len;
7488 ctsio->residual = 0;
7489 ctsio->kern_data_len = alloc_len;
7490 ctsio->kern_total_len = alloc_len;
7492 ctsio->kern_data_resid = 0;
7493 ctsio->kern_rel_offset = 0;
7495 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7496 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7498 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7500 ctl_set_success(ctsio);
7501 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7502 ctsio->be_move_done = ctl_config_move_done;
7503 ctl_datamove((union ctl_io *)ctsio);
7508 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7510 struct scsi_report_timestamp *cdb;
7511 struct scsi_report_timestamp_data *data;
7515 int alloc_len, total_len;
7517 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7519 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7521 retval = CTL_RETVAL_COMPLETE;
7523 total_len = sizeof(struct scsi_report_timestamp_data);
7524 alloc_len = scsi_4btoul(cdb->length);
7526 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7528 ctsio->kern_sg_entries = 0;
7530 if (total_len < alloc_len) {
7531 ctsio->residual = alloc_len - total_len;
7532 ctsio->kern_data_len = total_len;
7533 ctsio->kern_total_len = total_len;
7535 ctsio->residual = 0;
7536 ctsio->kern_data_len = alloc_len;
7537 ctsio->kern_total_len = alloc_len;
7539 ctsio->kern_data_resid = 0;
7540 ctsio->kern_rel_offset = 0;
7542 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7543 scsi_ulto2b(sizeof(*data) - 2, data->length);
7544 data->origin = RTS_ORIG_OUTSIDE;
7546 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7547 scsi_ulto4b(timestamp >> 16, data->timestamp);
7548 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7550 ctl_set_success(ctsio);
7551 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7552 ctsio->be_move_done = ctl_config_move_done;
7553 ctl_datamove((union ctl_io *)ctsio);
7558 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7560 struct scsi_per_res_in *cdb;
7561 int alloc_len, total_len = 0;
7562 /* struct scsi_per_res_in_rsrv in_data; */
7563 struct ctl_lun *lun;
7564 struct ctl_softc *softc;
7567 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7569 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7571 alloc_len = scsi_2btoul(cdb->length);
7573 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7574 softc = lun->ctl_softc;
7577 mtx_lock(&lun->lun_lock);
7578 switch (cdb->action) {
7579 case SPRI_RK: /* read keys */
7580 total_len = sizeof(struct scsi_per_res_in_keys) +
7582 sizeof(struct scsi_per_res_key);
7584 case SPRI_RR: /* read reservation */
7585 if (lun->flags & CTL_LUN_PR_RESERVED)
7586 total_len = sizeof(struct scsi_per_res_in_rsrv);
7588 total_len = sizeof(struct scsi_per_res_in_header);
7590 case SPRI_RC: /* report capabilities */
7591 total_len = sizeof(struct scsi_per_res_cap);
7593 case SPRI_RS: /* read full status */
7594 total_len = sizeof(struct scsi_per_res_in_header) +
7595 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7599 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7601 mtx_unlock(&lun->lun_lock);
7603 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7605 if (total_len < alloc_len) {
7606 ctsio->residual = alloc_len - total_len;
7607 ctsio->kern_data_len = total_len;
7608 ctsio->kern_total_len = total_len;
7610 ctsio->residual = 0;
7611 ctsio->kern_data_len = alloc_len;
7612 ctsio->kern_total_len = alloc_len;
7615 ctsio->kern_data_resid = 0;
7616 ctsio->kern_rel_offset = 0;
7617 ctsio->kern_sg_entries = 0;
7619 mtx_lock(&lun->lun_lock);
7620 switch (cdb->action) {
7621 case SPRI_RK: { // read keys
7622 struct scsi_per_res_in_keys *res_keys;
7625 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7628 * We had to drop the lock to allocate our buffer, which
7629 * leaves time for someone to come in with another
7630 * persistent reservation. (That is unlikely, though,
7631 * since this should be the only persistent reservation
7632 * command active right now.)
7634 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7635 (lun->pr_key_count *
7636 sizeof(struct scsi_per_res_key)))){
7637 mtx_unlock(&lun->lun_lock);
7638 free(ctsio->kern_data_ptr, M_CTL);
7639 printf("%s: reservation length changed, retrying\n",
7644 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7646 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7647 lun->pr_key_count, res_keys->header.length);
7649 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7650 if ((key = ctl_get_prkey(lun, i)) == 0)
7654 * We used lun->pr_key_count to calculate the
7655 * size to allocate. If it turns out the number of
7656 * initiators with the registered flag set is
7657 * larger than that (i.e. they haven't been kept in
7658 * sync), we've got a problem.
7660 if (key_count >= lun->pr_key_count) {
7664 scsi_u64to8b(key, res_keys->keys[key_count].key);
7669 case SPRI_RR: { // read reservation
7670 struct scsi_per_res_in_rsrv *res;
7671 int tmp_len, header_only;
7673 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7675 scsi_ulto4b(lun->pr_generation, res->header.generation);
7677 if (lun->flags & CTL_LUN_PR_RESERVED)
7679 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7680 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7681 res->header.length);
7684 tmp_len = sizeof(struct scsi_per_res_in_header);
7685 scsi_ulto4b(0, res->header.length);
7690 * We had to drop the lock to allocate our buffer, which
7691 * leaves time for someone to come in with another
7692 * persistent reservation. (That is unlikely, though,
7693 * since this should be the only persistent reservation
7694 * command active right now.)
7696 if (tmp_len != total_len) {
7697 mtx_unlock(&lun->lun_lock);
7698 free(ctsio->kern_data_ptr, M_CTL);
7699 printf("%s: reservation status changed, retrying\n",
7705 * No reservation held, so we're done.
7707 if (header_only != 0)
7711 * If the registration is an All Registrants type, the key
7712 * is 0, since it doesn't really matter.
7714 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7715 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7716 res->data.reservation);
7718 res->data.scopetype = lun->pr_res_type;
7721 case SPRI_RC: //report capabilities
7723 struct scsi_per_res_cap *res_cap;
7726 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7727 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7728 res_cap->flags1 = SPRI_CRH;
7729 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7730 type_mask = SPRI_TM_WR_EX_AR |
7736 scsi_ulto2b(type_mask, res_cap->type_mask);
7739 case SPRI_RS: { // read full status
7740 struct scsi_per_res_in_full *res_status;
7741 struct scsi_per_res_in_full_desc *res_desc;
7742 struct ctl_port *port;
7745 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7748 * We had to drop the lock to allocate our buffer, which
7749 * leaves time for someone to come in with another
7750 * persistent reservation. (That is unlikely, though,
7751 * since this should be the only persistent reservation
7752 * command active right now.)
7754 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7755 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7756 lun->pr_key_count)){
7757 mtx_unlock(&lun->lun_lock);
7758 free(ctsio->kern_data_ptr, M_CTL);
7759 printf("%s: reservation length changed, retrying\n",
7764 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7766 res_desc = &res_status->desc[0];
7767 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7768 if ((key = ctl_get_prkey(lun, i)) == 0)
7771 scsi_u64to8b(key, res_desc->res_key.key);
7772 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7773 (lun->pr_res_idx == i ||
7774 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7775 res_desc->flags = SPRI_FULL_R_HOLDER;
7776 res_desc->scopetype = lun->pr_res_type;
7778 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7779 res_desc->rel_trgt_port_id);
7781 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7783 len = ctl_create_iid(port,
7784 i % CTL_MAX_INIT_PER_PORT,
7785 res_desc->transport_id);
7786 scsi_ulto4b(len, res_desc->additional_length);
7787 res_desc = (struct scsi_per_res_in_full_desc *)
7788 &res_desc->transport_id[len];
7790 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7791 res_status->header.length);
7795 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7797 mtx_unlock(&lun->lun_lock);
7799 ctl_set_success(ctsio);
7800 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7801 ctsio->be_move_done = ctl_config_move_done;
7802 ctl_datamove((union ctl_io *)ctsio);
7803 return (CTL_RETVAL_COMPLETE);
7807 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7811 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7812 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7813 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7814 struct scsi_per_res_out_parms* param)
7816 union ctl_ha_msg persis_io;
7819 mtx_lock(&lun->lun_lock);
7820 if (sa_res_key == 0) {
7821 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7822 /* validate scope and type */
7823 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7825 mtx_unlock(&lun->lun_lock);
7826 ctl_set_invalid_field(/*ctsio*/ ctsio,
7832 ctl_done((union ctl_io *)ctsio);
7836 if (type>8 || type==2 || type==4 || type==0) {
7837 mtx_unlock(&lun->lun_lock);
7838 ctl_set_invalid_field(/*ctsio*/ ctsio,
7844 ctl_done((union ctl_io *)ctsio);
7849 * Unregister everybody else and build UA for
7852 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7853 if (i == residx || ctl_get_prkey(lun, i) == 0)
7856 ctl_clr_prkey(lun, i);
7857 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7859 lun->pr_key_count = 1;
7860 lun->pr_res_type = type;
7861 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7862 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7863 lun->pr_res_idx = residx;
7864 lun->pr_generation++;
7865 mtx_unlock(&lun->lun_lock);
7867 /* send msg to other side */
7868 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7869 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7870 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7871 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7872 persis_io.pr.pr_info.res_type = type;
7873 memcpy(persis_io.pr.pr_info.sa_res_key,
7874 param->serv_act_res_key,
7875 sizeof(param->serv_act_res_key));
7876 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7877 sizeof(persis_io.pr), M_WAITOK);
7879 /* not all registrants */
7880 mtx_unlock(&lun->lun_lock);
7881 free(ctsio->kern_data_ptr, M_CTL);
7882 ctl_set_invalid_field(ctsio,
7888 ctl_done((union ctl_io *)ctsio);
7891 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7892 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7895 if (res_key == sa_res_key) {
7898 * The spec implies this is not good but doesn't
7899 * say what to do. There are two choices either
7900 * generate a res conflict or check condition
7901 * with illegal field in parameter data. Since
7902 * that is what is done when the sa_res_key is
7903 * zero I'll take that approach since this has
7904 * to do with the sa_res_key.
7906 mtx_unlock(&lun->lun_lock);
7907 free(ctsio->kern_data_ptr, M_CTL);
7908 ctl_set_invalid_field(ctsio,
7914 ctl_done((union ctl_io *)ctsio);
7918 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7919 if (ctl_get_prkey(lun, i) != sa_res_key)
7923 ctl_clr_prkey(lun, i);
7924 lun->pr_key_count--;
7925 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7928 mtx_unlock(&lun->lun_lock);
7929 free(ctsio->kern_data_ptr, M_CTL);
7930 ctl_set_reservation_conflict(ctsio);
7931 ctl_done((union ctl_io *)ctsio);
7932 return (CTL_RETVAL_COMPLETE);
7934 lun->pr_generation++;
7935 mtx_unlock(&lun->lun_lock);
7937 /* send msg to other side */
7938 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7939 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7940 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7941 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7942 persis_io.pr.pr_info.res_type = type;
7943 memcpy(persis_io.pr.pr_info.sa_res_key,
7944 param->serv_act_res_key,
7945 sizeof(param->serv_act_res_key));
7946 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7947 sizeof(persis_io.pr), M_WAITOK);
7949 /* Reserved but not all registrants */
7950 /* sa_res_key is res holder */
7951 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7952 /* validate scope and type */
7953 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7955 mtx_unlock(&lun->lun_lock);
7956 ctl_set_invalid_field(/*ctsio*/ ctsio,
7962 ctl_done((union ctl_io *)ctsio);
7966 if (type>8 || type==2 || type==4 || type==0) {
7967 mtx_unlock(&lun->lun_lock);
7968 ctl_set_invalid_field(/*ctsio*/ ctsio,
7974 ctl_done((union ctl_io *)ctsio);
7980 * if sa_res_key != res_key remove all
7981 * registrants w/sa_res_key and generate UA
7982 * for these registrants(Registrations
7983 * Preempted) if it wasn't an exclusive
7984 * reservation generate UA(Reservations
7985 * Preempted) for all other registered nexuses
7986 * if the type has changed. Establish the new
7987 * reservation and holder. If res_key and
7988 * sa_res_key are the same do the above
7989 * except don't unregister the res holder.
7992 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7993 if (i == residx || ctl_get_prkey(lun, i) == 0)
7996 if (sa_res_key == ctl_get_prkey(lun, i)) {
7997 ctl_clr_prkey(lun, i);
7998 lun->pr_key_count--;
7999 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8000 } else if (type != lun->pr_res_type &&
8001 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8002 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8003 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8006 lun->pr_res_type = type;
8007 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8008 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8009 lun->pr_res_idx = residx;
8011 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8012 lun->pr_generation++;
8013 mtx_unlock(&lun->lun_lock);
8015 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8016 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8017 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8018 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8019 persis_io.pr.pr_info.res_type = type;
8020 memcpy(persis_io.pr.pr_info.sa_res_key,
8021 param->serv_act_res_key,
8022 sizeof(param->serv_act_res_key));
8023 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8024 sizeof(persis_io.pr), M_WAITOK);
8027 * sa_res_key is not the res holder just
8028 * remove registrants
8032 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8033 if (sa_res_key != ctl_get_prkey(lun, i))
8037 ctl_clr_prkey(lun, i);
8038 lun->pr_key_count--;
8039 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8043 mtx_unlock(&lun->lun_lock);
8044 free(ctsio->kern_data_ptr, M_CTL);
8045 ctl_set_reservation_conflict(ctsio);
8046 ctl_done((union ctl_io *)ctsio);
8049 lun->pr_generation++;
8050 mtx_unlock(&lun->lun_lock);
8052 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8053 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8054 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8055 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8056 persis_io.pr.pr_info.res_type = type;
8057 memcpy(persis_io.pr.pr_info.sa_res_key,
8058 param->serv_act_res_key,
8059 sizeof(param->serv_act_res_key));
8060 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8061 sizeof(persis_io.pr), M_WAITOK);
8068 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8070 uint64_t sa_res_key;
8073 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8075 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8076 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8077 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8078 if (sa_res_key == 0) {
8080 * Unregister everybody else and build UA for
8083 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8084 if (i == msg->pr.pr_info.residx ||
8085 ctl_get_prkey(lun, i) == 0)
8088 ctl_clr_prkey(lun, i);
8089 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8092 lun->pr_key_count = 1;
8093 lun->pr_res_type = msg->pr.pr_info.res_type;
8094 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8095 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8096 lun->pr_res_idx = msg->pr.pr_info.residx;
8098 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8099 if (sa_res_key == ctl_get_prkey(lun, i))
8102 ctl_clr_prkey(lun, i);
8103 lun->pr_key_count--;
8104 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8108 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8109 if (i == msg->pr.pr_info.residx ||
8110 ctl_get_prkey(lun, i) == 0)
8113 if (sa_res_key == ctl_get_prkey(lun, i)) {
8114 ctl_clr_prkey(lun, i);
8115 lun->pr_key_count--;
8116 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8117 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8118 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8119 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8120 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8123 lun->pr_res_type = msg->pr.pr_info.res_type;
8124 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8125 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8126 lun->pr_res_idx = msg->pr.pr_info.residx;
8128 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8130 lun->pr_generation++;
8136 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8139 u_int32_t param_len;
8140 struct scsi_per_res_out *cdb;
8141 struct ctl_lun *lun;
8142 struct scsi_per_res_out_parms* param;
8143 struct ctl_softc *softc;
8145 uint64_t res_key, sa_res_key, key;
8147 union ctl_ha_msg persis_io;
8150 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8152 retval = CTL_RETVAL_COMPLETE;
8154 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8155 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8156 softc = lun->ctl_softc;
8159 * We only support whole-LUN scope. The scope & type are ignored for
8160 * register, register and ignore existing key and clear.
8161 * We sometimes ignore scope and type on preempts too!!
8162 * Verify reservation type here as well.
8164 type = cdb->scope_type & SPR_TYPE_MASK;
8165 if ((cdb->action == SPRO_RESERVE)
8166 || (cdb->action == SPRO_RELEASE)) {
8167 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8168 ctl_set_invalid_field(/*ctsio*/ ctsio,
8174 ctl_done((union ctl_io *)ctsio);
8175 return (CTL_RETVAL_COMPLETE);
8178 if (type>8 || type==2 || type==4 || type==0) {
8179 ctl_set_invalid_field(/*ctsio*/ ctsio,
8185 ctl_done((union ctl_io *)ctsio);
8186 return (CTL_RETVAL_COMPLETE);
8190 param_len = scsi_4btoul(cdb->length);
8192 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8193 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8194 ctsio->kern_data_len = param_len;
8195 ctsio->kern_total_len = param_len;
8196 ctsio->kern_data_resid = 0;
8197 ctsio->kern_rel_offset = 0;
8198 ctsio->kern_sg_entries = 0;
8199 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8200 ctsio->be_move_done = ctl_config_move_done;
8201 ctl_datamove((union ctl_io *)ctsio);
8203 return (CTL_RETVAL_COMPLETE);
8206 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8208 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8209 res_key = scsi_8btou64(param->res_key.key);
8210 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8213 * Validate the reservation key here except for SPRO_REG_IGNO
8214 * This must be done for all other service actions
8216 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8217 mtx_lock(&lun->lun_lock);
8218 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8219 if (res_key != key) {
8221 * The current key passed in doesn't match
8222 * the one the initiator previously
8225 mtx_unlock(&lun->lun_lock);
8226 free(ctsio->kern_data_ptr, M_CTL);
8227 ctl_set_reservation_conflict(ctsio);
8228 ctl_done((union ctl_io *)ctsio);
8229 return (CTL_RETVAL_COMPLETE);
8231 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8233 * We are not registered
8235 mtx_unlock(&lun->lun_lock);
8236 free(ctsio->kern_data_ptr, M_CTL);
8237 ctl_set_reservation_conflict(ctsio);
8238 ctl_done((union ctl_io *)ctsio);
8239 return (CTL_RETVAL_COMPLETE);
8240 } else if (res_key != 0) {
8242 * We are not registered and trying to register but
8243 * the register key isn't zero.
8245 mtx_unlock(&lun->lun_lock);
8246 free(ctsio->kern_data_ptr, M_CTL);
8247 ctl_set_reservation_conflict(ctsio);
8248 ctl_done((union ctl_io *)ctsio);
8249 return (CTL_RETVAL_COMPLETE);
8251 mtx_unlock(&lun->lun_lock);
8254 switch (cdb->action & SPRO_ACTION_MASK) {
8256 case SPRO_REG_IGNO: {
8259 printf("Registration received\n");
8263 * We don't support any of these options, as we report in
8264 * the read capabilities request (see
8265 * ctl_persistent_reserve_in(), above).
8267 if ((param->flags & SPR_SPEC_I_PT)
8268 || (param->flags & SPR_ALL_TG_PT)
8269 || (param->flags & SPR_APTPL)) {
8272 if (param->flags & SPR_APTPL)
8274 else if (param->flags & SPR_ALL_TG_PT)
8276 else /* SPR_SPEC_I_PT */
8279 free(ctsio->kern_data_ptr, M_CTL);
8280 ctl_set_invalid_field(ctsio,
8286 ctl_done((union ctl_io *)ctsio);
8287 return (CTL_RETVAL_COMPLETE);
8290 mtx_lock(&lun->lun_lock);
8293 * The initiator wants to clear the
8296 if (sa_res_key == 0) {
8298 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8299 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8300 && ctl_get_prkey(lun, residx) == 0)) {
8301 mtx_unlock(&lun->lun_lock);
8305 ctl_clr_prkey(lun, residx);
8306 lun->pr_key_count--;
8308 if (residx == lun->pr_res_idx) {
8309 lun->flags &= ~CTL_LUN_PR_RESERVED;
8310 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8312 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8313 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8314 lun->pr_key_count) {
8316 * If the reservation is a registrants
8317 * only type we need to generate a UA
8318 * for other registered inits. The
8319 * sense code should be RESERVATIONS
8323 for (i = softc->init_min; i < softc->init_max; i++){
8324 if (ctl_get_prkey(lun, i) == 0)
8327 CTL_UA_RES_RELEASE);
8330 lun->pr_res_type = 0;
8331 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8332 if (lun->pr_key_count==0) {
8333 lun->flags &= ~CTL_LUN_PR_RESERVED;
8334 lun->pr_res_type = 0;
8335 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8338 lun->pr_generation++;
8339 mtx_unlock(&lun->lun_lock);
8341 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8342 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8343 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8344 persis_io.pr.pr_info.residx = residx;
8345 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8346 sizeof(persis_io.pr), M_WAITOK);
8347 } else /* sa_res_key != 0 */ {
8350 * If we aren't registered currently then increment
8351 * the key count and set the registered flag.
8353 ctl_alloc_prkey(lun, residx);
8354 if (ctl_get_prkey(lun, residx) == 0)
8355 lun->pr_key_count++;
8356 ctl_set_prkey(lun, residx, sa_res_key);
8357 lun->pr_generation++;
8358 mtx_unlock(&lun->lun_lock);
8360 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8361 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8362 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8363 persis_io.pr.pr_info.residx = residx;
8364 memcpy(persis_io.pr.pr_info.sa_res_key,
8365 param->serv_act_res_key,
8366 sizeof(param->serv_act_res_key));
8367 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8368 sizeof(persis_io.pr), M_WAITOK);
8375 printf("Reserve executed type %d\n", type);
8377 mtx_lock(&lun->lun_lock);
8378 if (lun->flags & CTL_LUN_PR_RESERVED) {
8380 * if this isn't the reservation holder and it's
8381 * not a "all registrants" type or if the type is
8382 * different then we have a conflict
8384 if ((lun->pr_res_idx != residx
8385 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8386 || lun->pr_res_type != type) {
8387 mtx_unlock(&lun->lun_lock);
8388 free(ctsio->kern_data_ptr, M_CTL);
8389 ctl_set_reservation_conflict(ctsio);
8390 ctl_done((union ctl_io *)ctsio);
8391 return (CTL_RETVAL_COMPLETE);
8393 mtx_unlock(&lun->lun_lock);
8394 } else /* create a reservation */ {
8396 * If it's not an "all registrants" type record
8397 * reservation holder
8399 if (type != SPR_TYPE_WR_EX_AR
8400 && type != SPR_TYPE_EX_AC_AR)
8401 lun->pr_res_idx = residx; /* Res holder */
8403 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8405 lun->flags |= CTL_LUN_PR_RESERVED;
8406 lun->pr_res_type = type;
8408 mtx_unlock(&lun->lun_lock);
8410 /* send msg to other side */
8411 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8412 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8413 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8414 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8415 persis_io.pr.pr_info.res_type = type;
8416 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8417 sizeof(persis_io.pr), M_WAITOK);
8422 mtx_lock(&lun->lun_lock);
8423 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8424 /* No reservation exists return good status */
8425 mtx_unlock(&lun->lun_lock);
8429 * Is this nexus a reservation holder?
8431 if (lun->pr_res_idx != residx
8432 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8434 * not a res holder return good status but
8437 mtx_unlock(&lun->lun_lock);
8441 if (lun->pr_res_type != type) {
8442 mtx_unlock(&lun->lun_lock);
8443 free(ctsio->kern_data_ptr, M_CTL);
8444 ctl_set_illegal_pr_release(ctsio);
8445 ctl_done((union ctl_io *)ctsio);
8446 return (CTL_RETVAL_COMPLETE);
8449 /* okay to release */
8450 lun->flags &= ~CTL_LUN_PR_RESERVED;
8451 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8452 lun->pr_res_type = 0;
8455 * If this isn't an exclusive access reservation and NUAR
8456 * is not set, generate UA for all other registrants.
8458 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8459 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8460 for (i = softc->init_min; i < softc->init_max; i++) {
8461 if (i == residx || ctl_get_prkey(lun, i) == 0)
8463 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8466 mtx_unlock(&lun->lun_lock);
8468 /* Send msg to other side */
8469 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8470 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8471 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8472 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8473 sizeof(persis_io.pr), M_WAITOK);
8477 /* send msg to other side */
8479 mtx_lock(&lun->lun_lock);
8480 lun->flags &= ~CTL_LUN_PR_RESERVED;
8481 lun->pr_res_type = 0;
8482 lun->pr_key_count = 0;
8483 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8485 ctl_clr_prkey(lun, residx);
8486 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8487 if (ctl_get_prkey(lun, i) != 0) {
8488 ctl_clr_prkey(lun, i);
8489 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8491 lun->pr_generation++;
8492 mtx_unlock(&lun->lun_lock);
8494 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8495 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8496 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8497 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8498 sizeof(persis_io.pr), M_WAITOK);
8502 case SPRO_PRE_ABO: {
8505 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8506 residx, ctsio, cdb, param);
8508 return (CTL_RETVAL_COMPLETE);
8512 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8516 free(ctsio->kern_data_ptr, M_CTL);
8517 ctl_set_success(ctsio);
8518 ctl_done((union ctl_io *)ctsio);
8524 * This routine is for handling a message from the other SC pertaining to
8525 * persistent reserve out. All the error checking will have been done
8526 * so only perorming the action need be done here to keep the two
8530 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8532 struct ctl_softc *softc = control_softc;
8533 struct ctl_lun *lun;
8535 uint32_t residx, targ_lun;
8537 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8538 mtx_lock(&softc->ctl_lock);
8539 if ((targ_lun >= CTL_MAX_LUNS) ||
8540 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8541 mtx_unlock(&softc->ctl_lock);
8544 mtx_lock(&lun->lun_lock);
8545 mtx_unlock(&softc->ctl_lock);
8546 if (lun->flags & CTL_LUN_DISABLED) {
8547 mtx_unlock(&lun->lun_lock);
8550 residx = ctl_get_initindex(&msg->hdr.nexus);
8551 switch(msg->pr.pr_info.action) {
8552 case CTL_PR_REG_KEY:
8553 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8554 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8555 lun->pr_key_count++;
8556 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8557 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8558 lun->pr_generation++;
8561 case CTL_PR_UNREG_KEY:
8562 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8563 lun->pr_key_count--;
8565 /* XXX Need to see if the reservation has been released */
8566 /* if so do we need to generate UA? */
8567 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8568 lun->flags &= ~CTL_LUN_PR_RESERVED;
8569 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8571 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8572 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8573 lun->pr_key_count) {
8575 * If the reservation is a registrants
8576 * only type we need to generate a UA
8577 * for other registered inits. The
8578 * sense code should be RESERVATIONS
8582 for (i = softc->init_min; i < softc->init_max; i++) {
8583 if (ctl_get_prkey(lun, i) == 0)
8586 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8589 lun->pr_res_type = 0;
8590 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8591 if (lun->pr_key_count==0) {
8592 lun->flags &= ~CTL_LUN_PR_RESERVED;
8593 lun->pr_res_type = 0;
8594 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8597 lun->pr_generation++;
8600 case CTL_PR_RESERVE:
8601 lun->flags |= CTL_LUN_PR_RESERVED;
8602 lun->pr_res_type = msg->pr.pr_info.res_type;
8603 lun->pr_res_idx = msg->pr.pr_info.residx;
8607 case CTL_PR_RELEASE:
8609 * If this isn't an exclusive access reservation and NUAR
8610 * is not set, generate UA for all other registrants.
8612 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8613 lun->pr_res_type != SPR_TYPE_WR_EX &&
8614 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8615 for (i = softc->init_min; i < softc->init_max; i++)
8616 if (i == residx || ctl_get_prkey(lun, i) == 0)
8618 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8621 lun->flags &= ~CTL_LUN_PR_RESERVED;
8622 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8623 lun->pr_res_type = 0;
8626 case CTL_PR_PREEMPT:
8627 ctl_pro_preempt_other(lun, msg);
8630 lun->flags &= ~CTL_LUN_PR_RESERVED;
8631 lun->pr_res_type = 0;
8632 lun->pr_key_count = 0;
8633 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8635 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8636 if (ctl_get_prkey(lun, i) == 0)
8638 ctl_clr_prkey(lun, i);
8639 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8641 lun->pr_generation++;
8645 mtx_unlock(&lun->lun_lock);
8649 ctl_read_write(struct ctl_scsiio *ctsio)
8651 struct ctl_lun *lun;
8652 struct ctl_lba_len_flags *lbalen;
8654 uint32_t num_blocks;
8658 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8660 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8663 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8664 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8665 switch (ctsio->cdb[0]) {
8668 struct scsi_rw_6 *cdb;
8670 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8672 lba = scsi_3btoul(cdb->addr);
8673 /* only 5 bits are valid in the most significant address byte */
8675 num_blocks = cdb->length;
8677 * This is correct according to SBC-2.
8679 if (num_blocks == 0)
8685 struct scsi_rw_10 *cdb;
8687 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8688 if (cdb->byte2 & SRW10_FUA)
8689 flags |= CTL_LLF_FUA;
8690 if (cdb->byte2 & SRW10_DPO)
8691 flags |= CTL_LLF_DPO;
8692 lba = scsi_4btoul(cdb->addr);
8693 num_blocks = scsi_2btoul(cdb->length);
8696 case WRITE_VERIFY_10: {
8697 struct scsi_write_verify_10 *cdb;
8699 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8700 flags |= CTL_LLF_FUA;
8701 if (cdb->byte2 & SWV_DPO)
8702 flags |= CTL_LLF_DPO;
8703 lba = scsi_4btoul(cdb->addr);
8704 num_blocks = scsi_2btoul(cdb->length);
8709 struct scsi_rw_12 *cdb;
8711 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8712 if (cdb->byte2 & SRW12_FUA)
8713 flags |= CTL_LLF_FUA;
8714 if (cdb->byte2 & SRW12_DPO)
8715 flags |= CTL_LLF_DPO;
8716 lba = scsi_4btoul(cdb->addr);
8717 num_blocks = scsi_4btoul(cdb->length);
8720 case WRITE_VERIFY_12: {
8721 struct scsi_write_verify_12 *cdb;
8723 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8724 flags |= CTL_LLF_FUA;
8725 if (cdb->byte2 & SWV_DPO)
8726 flags |= CTL_LLF_DPO;
8727 lba = scsi_4btoul(cdb->addr);
8728 num_blocks = scsi_4btoul(cdb->length);
8733 struct scsi_rw_16 *cdb;
8735 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8736 if (cdb->byte2 & SRW12_FUA)
8737 flags |= CTL_LLF_FUA;
8738 if (cdb->byte2 & SRW12_DPO)
8739 flags |= CTL_LLF_DPO;
8740 lba = scsi_8btou64(cdb->addr);
8741 num_blocks = scsi_4btoul(cdb->length);
8744 case WRITE_ATOMIC_16: {
8745 struct scsi_write_atomic_16 *cdb;
8747 if (lun->be_lun->atomicblock == 0) {
8748 ctl_set_invalid_opcode(ctsio);
8749 ctl_done((union ctl_io *)ctsio);
8750 return (CTL_RETVAL_COMPLETE);
8753 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8754 if (cdb->byte2 & SRW12_FUA)
8755 flags |= CTL_LLF_FUA;
8756 if (cdb->byte2 & SRW12_DPO)
8757 flags |= CTL_LLF_DPO;
8758 lba = scsi_8btou64(cdb->addr);
8759 num_blocks = scsi_2btoul(cdb->length);
8760 if (num_blocks > lun->be_lun->atomicblock) {
8761 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8762 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8764 ctl_done((union ctl_io *)ctsio);
8765 return (CTL_RETVAL_COMPLETE);
8769 case WRITE_VERIFY_16: {
8770 struct scsi_write_verify_16 *cdb;
8772 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8773 flags |= CTL_LLF_FUA;
8774 if (cdb->byte2 & SWV_DPO)
8775 flags |= CTL_LLF_DPO;
8776 lba = scsi_8btou64(cdb->addr);
8777 num_blocks = scsi_4btoul(cdb->length);
8782 * We got a command we don't support. This shouldn't
8783 * happen, commands should be filtered out above us.
8785 ctl_set_invalid_opcode(ctsio);
8786 ctl_done((union ctl_io *)ctsio);
8788 return (CTL_RETVAL_COMPLETE);
8789 break; /* NOTREACHED */
8793 * The first check is to make sure we're in bounds, the second
8794 * check is to catch wrap-around problems. If the lba + num blocks
8795 * is less than the lba, then we've wrapped around and the block
8796 * range is invalid anyway.
8798 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8799 || ((lba + num_blocks) < lba)) {
8800 ctl_set_lba_out_of_range(ctsio,
8801 MAX(lba, lun->be_lun->maxlba + 1));
8802 ctl_done((union ctl_io *)ctsio);
8803 return (CTL_RETVAL_COMPLETE);
8807 * According to SBC-3, a transfer length of 0 is not an error.
8808 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8809 * translates to 256 blocks for those commands.
8811 if (num_blocks == 0) {
8812 ctl_set_success(ctsio);
8813 ctl_done((union ctl_io *)ctsio);
8814 return (CTL_RETVAL_COMPLETE);
8817 /* Set FUA and/or DPO if caches are disabled. */
8819 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8820 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8822 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8823 flags |= CTL_LLF_FUA;
8826 lbalen = (struct ctl_lba_len_flags *)
8827 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8829 lbalen->len = num_blocks;
8830 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8832 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8833 ctsio->kern_rel_offset = 0;
8835 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8837 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8842 ctl_cnw_cont(union ctl_io *io)
8844 struct ctl_scsiio *ctsio;
8845 struct ctl_lun *lun;
8846 struct ctl_lba_len_flags *lbalen;
8849 ctsio = &io->scsiio;
8850 ctsio->io_hdr.status = CTL_STATUS_NONE;
8851 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8852 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8853 lbalen = (struct ctl_lba_len_flags *)
8854 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8855 lbalen->flags &= ~CTL_LLF_COMPARE;
8856 lbalen->flags |= CTL_LLF_WRITE;
8858 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8859 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8864 ctl_cnw(struct ctl_scsiio *ctsio)
8866 struct ctl_lun *lun;
8867 struct ctl_lba_len_flags *lbalen;
8869 uint32_t num_blocks;
8872 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8874 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8877 switch (ctsio->cdb[0]) {
8878 case COMPARE_AND_WRITE: {
8879 struct scsi_compare_and_write *cdb;
8881 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8882 if (cdb->byte2 & SRW10_FUA)
8883 flags |= CTL_LLF_FUA;
8884 if (cdb->byte2 & SRW10_DPO)
8885 flags |= CTL_LLF_DPO;
8886 lba = scsi_8btou64(cdb->addr);
8887 num_blocks = cdb->length;
8892 * We got a command we don't support. This shouldn't
8893 * happen, commands should be filtered out above us.
8895 ctl_set_invalid_opcode(ctsio);
8896 ctl_done((union ctl_io *)ctsio);
8898 return (CTL_RETVAL_COMPLETE);
8899 break; /* NOTREACHED */
8903 * The first check is to make sure we're in bounds, the second
8904 * check is to catch wrap-around problems. If the lba + num blocks
8905 * is less than the lba, then we've wrapped around and the block
8906 * range is invalid anyway.
8908 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8909 || ((lba + num_blocks) < lba)) {
8910 ctl_set_lba_out_of_range(ctsio,
8911 MAX(lba, lun->be_lun->maxlba + 1));
8912 ctl_done((union ctl_io *)ctsio);
8913 return (CTL_RETVAL_COMPLETE);
8917 * According to SBC-3, a transfer length of 0 is not an error.
8919 if (num_blocks == 0) {
8920 ctl_set_success(ctsio);
8921 ctl_done((union ctl_io *)ctsio);
8922 return (CTL_RETVAL_COMPLETE);
8925 /* Set FUA if write cache is disabled. */
8926 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8927 flags |= CTL_LLF_FUA;
8929 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8930 ctsio->kern_rel_offset = 0;
8933 * Set the IO_CONT flag, so that if this I/O gets passed to
8934 * ctl_data_submit_done(), it'll get passed back to
8935 * ctl_ctl_cnw_cont() for further processing.
8937 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8938 ctsio->io_cont = ctl_cnw_cont;
8940 lbalen = (struct ctl_lba_len_flags *)
8941 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8943 lbalen->len = num_blocks;
8944 lbalen->flags = CTL_LLF_COMPARE | flags;
8946 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8947 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8952 ctl_verify(struct ctl_scsiio *ctsio)
8954 struct ctl_lun *lun;
8955 struct ctl_lba_len_flags *lbalen;
8957 uint32_t num_blocks;
8961 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8963 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8966 flags = CTL_LLF_FUA;
8967 switch (ctsio->cdb[0]) {
8969 struct scsi_verify_10 *cdb;
8971 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8972 if (cdb->byte2 & SVFY_BYTCHK)
8974 if (cdb->byte2 & SVFY_DPO)
8975 flags |= CTL_LLF_DPO;
8976 lba = scsi_4btoul(cdb->addr);
8977 num_blocks = scsi_2btoul(cdb->length);
8981 struct scsi_verify_12 *cdb;
8983 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8984 if (cdb->byte2 & SVFY_BYTCHK)
8986 if (cdb->byte2 & SVFY_DPO)
8987 flags |= CTL_LLF_DPO;
8988 lba = scsi_4btoul(cdb->addr);
8989 num_blocks = scsi_4btoul(cdb->length);
8993 struct scsi_rw_16 *cdb;
8995 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8996 if (cdb->byte2 & SVFY_BYTCHK)
8998 if (cdb->byte2 & SVFY_DPO)
8999 flags |= CTL_LLF_DPO;
9000 lba = scsi_8btou64(cdb->addr);
9001 num_blocks = scsi_4btoul(cdb->length);
9006 * We got a command we don't support. This shouldn't
9007 * happen, commands should be filtered out above us.
9009 ctl_set_invalid_opcode(ctsio);
9010 ctl_done((union ctl_io *)ctsio);
9011 return (CTL_RETVAL_COMPLETE);
9015 * The first check is to make sure we're in bounds, the second
9016 * check is to catch wrap-around problems. If the lba + num blocks
9017 * is less than the lba, then we've wrapped around and the block
9018 * range is invalid anyway.
9020 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9021 || ((lba + num_blocks) < lba)) {
9022 ctl_set_lba_out_of_range(ctsio,
9023 MAX(lba, lun->be_lun->maxlba + 1));
9024 ctl_done((union ctl_io *)ctsio);
9025 return (CTL_RETVAL_COMPLETE);
9029 * According to SBC-3, a transfer length of 0 is not an error.
9031 if (num_blocks == 0) {
9032 ctl_set_success(ctsio);
9033 ctl_done((union ctl_io *)ctsio);
9034 return (CTL_RETVAL_COMPLETE);
9037 lbalen = (struct ctl_lba_len_flags *)
9038 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9040 lbalen->len = num_blocks;
9042 lbalen->flags = CTL_LLF_COMPARE | flags;
9043 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9045 lbalen->flags = CTL_LLF_VERIFY | flags;
9046 ctsio->kern_total_len = 0;
9048 ctsio->kern_rel_offset = 0;
9050 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9051 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9056 ctl_report_luns(struct ctl_scsiio *ctsio)
9058 struct ctl_softc *softc;
9059 struct scsi_report_luns *cdb;
9060 struct scsi_report_luns_data *lun_data;
9061 struct ctl_lun *lun, *request_lun;
9062 struct ctl_port *port;
9063 int num_luns, retval;
9064 uint32_t alloc_len, lun_datalen;
9066 uint32_t initidx, targ_lun_id, lun_id;
9068 retval = CTL_RETVAL_COMPLETE;
9069 cdb = (struct scsi_report_luns *)ctsio->cdb;
9070 port = ctl_io_port(&ctsio->io_hdr);
9071 softc = port->ctl_softc;
9073 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9075 mtx_lock(&softc->ctl_lock);
9077 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
9078 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
9081 mtx_unlock(&softc->ctl_lock);
9083 switch (cdb->select_report) {
9084 case RPL_REPORT_DEFAULT:
9085 case RPL_REPORT_ALL:
9086 case RPL_REPORT_NONSUBSID:
9088 case RPL_REPORT_WELLKNOWN:
9089 case RPL_REPORT_ADMIN:
9090 case RPL_REPORT_CONGLOM:
9094 ctl_set_invalid_field(ctsio,
9100 ctl_done((union ctl_io *)ctsio);
9102 break; /* NOTREACHED */
9105 alloc_len = scsi_4btoul(cdb->length);
9107 * The initiator has to allocate at least 16 bytes for this request,
9108 * so he can at least get the header and the first LUN. Otherwise
9109 * we reject the request (per SPC-3 rev 14, section 6.21).
9111 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9112 sizeof(struct scsi_report_luns_lundata))) {
9113 ctl_set_invalid_field(ctsio,
9119 ctl_done((union ctl_io *)ctsio);
9123 request_lun = (struct ctl_lun *)
9124 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9126 lun_datalen = sizeof(*lun_data) +
9127 (num_luns * sizeof(struct scsi_report_luns_lundata));
9129 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9130 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9131 ctsio->kern_sg_entries = 0;
9133 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9135 mtx_lock(&softc->ctl_lock);
9136 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9137 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9138 if (lun_id >= CTL_MAX_LUNS)
9140 lun = softc->ctl_luns[lun_id];
9144 be64enc(lun_data->luns[num_filled++].lundata,
9145 ctl_encode_lun(targ_lun_id));
9148 * According to SPC-3, rev 14 section 6.21:
9150 * "The execution of a REPORT LUNS command to any valid and
9151 * installed logical unit shall clear the REPORTED LUNS DATA
9152 * HAS CHANGED unit attention condition for all logical
9153 * units of that target with respect to the requesting
9154 * initiator. A valid and installed logical unit is one
9155 * having a PERIPHERAL QUALIFIER of 000b in the standard
9156 * INQUIRY data (see 6.4.2)."
9158 * If request_lun is NULL, the LUN this report luns command
9159 * was issued to is either disabled or doesn't exist. In that
9160 * case, we shouldn't clear any pending lun change unit
9163 if (request_lun != NULL) {
9164 mtx_lock(&lun->lun_lock);
9165 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9166 mtx_unlock(&lun->lun_lock);
9169 mtx_unlock(&softc->ctl_lock);
9172 * It's quite possible that we've returned fewer LUNs than we allocated
9173 * space for. Trim it.
9175 lun_datalen = sizeof(*lun_data) +
9176 (num_filled * sizeof(struct scsi_report_luns_lundata));
9178 if (lun_datalen < alloc_len) {
9179 ctsio->residual = alloc_len - lun_datalen;
9180 ctsio->kern_data_len = lun_datalen;
9181 ctsio->kern_total_len = lun_datalen;
9183 ctsio->residual = 0;
9184 ctsio->kern_data_len = alloc_len;
9185 ctsio->kern_total_len = alloc_len;
9187 ctsio->kern_data_resid = 0;
9188 ctsio->kern_rel_offset = 0;
9189 ctsio->kern_sg_entries = 0;
9192 * We set this to the actual data length, regardless of how much
9193 * space we actually have to return results. If the user looks at
9194 * this value, he'll know whether or not he allocated enough space
9195 * and reissue the command if necessary. We don't support well
9196 * known logical units, so if the user asks for that, return none.
9198 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9201 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9204 ctl_set_success(ctsio);
9205 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9206 ctsio->be_move_done = ctl_config_move_done;
9207 ctl_datamove((union ctl_io *)ctsio);
9212 ctl_request_sense(struct ctl_scsiio *ctsio)
9214 struct scsi_request_sense *cdb;
9215 struct scsi_sense_data *sense_ptr;
9216 struct ctl_softc *softc;
9217 struct ctl_lun *lun;
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, 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_format);
9320 if (ua_type != CTL_UA_NONE)
9323 if (have_error == 0) {
9325 * Report informational exception if have one and allowed.
9327 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9329 ascq = lun->ie_ascq;
9331 ctl_set_sense_data(sense_ptr, lun, sense_format,
9332 /*current_error*/ 1,
9333 /*sense_key*/ SSD_KEY_NO_SENSE,
9338 mtx_unlock(&lun->lun_lock);
9342 * We report the SCSI status as OK, since the status of the command
9343 * itself is OK. We're reporting sense as parameter data.
9345 ctl_set_success(ctsio);
9346 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9347 ctsio->be_move_done = ctl_config_move_done;
9348 ctl_datamove((union ctl_io *)ctsio);
9349 return (CTL_RETVAL_COMPLETE);
9353 ctl_tur(struct ctl_scsiio *ctsio)
9356 CTL_DEBUG_PRINT(("ctl_tur\n"));
9358 ctl_set_success(ctsio);
9359 ctl_done((union ctl_io *)ctsio);
9361 return (CTL_RETVAL_COMPLETE);
9365 * SCSI VPD page 0x00, the Supported VPD Pages page.
9368 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9370 struct scsi_vpd_supported_pages *pages;
9372 struct ctl_lun *lun;
9375 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9377 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9378 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9379 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9380 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9381 ctsio->kern_sg_entries = 0;
9383 if (sup_page_size < alloc_len) {
9384 ctsio->residual = alloc_len - sup_page_size;
9385 ctsio->kern_data_len = sup_page_size;
9386 ctsio->kern_total_len = sup_page_size;
9388 ctsio->residual = 0;
9389 ctsio->kern_data_len = alloc_len;
9390 ctsio->kern_total_len = alloc_len;
9392 ctsio->kern_data_resid = 0;
9393 ctsio->kern_rel_offset = 0;
9394 ctsio->kern_sg_entries = 0;
9397 * The control device is always connected. The disk device, on the
9398 * other hand, may not be online all the time. Need to change this
9399 * to figure out whether the disk device is actually online or not.
9402 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9403 lun->be_lun->lun_type;
9405 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9408 /* Supported VPD pages */
9409 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9411 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9412 /* Device Identification */
9413 pages->page_list[p++] = SVPD_DEVICE_ID;
9414 /* Extended INQUIRY Data */
9415 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9416 /* Mode Page Policy */
9417 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9419 pages->page_list[p++] = SVPD_SCSI_PORTS;
9420 /* Third-party Copy */
9421 pages->page_list[p++] = SVPD_SCSI_TPC;
9422 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9424 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9425 /* Block Device Characteristics */
9426 pages->page_list[p++] = SVPD_BDC;
9427 /* Logical Block Provisioning */
9428 pages->page_list[p++] = SVPD_LBP;
9432 ctl_set_success(ctsio);
9433 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9434 ctsio->be_move_done = ctl_config_move_done;
9435 ctl_datamove((union ctl_io *)ctsio);
9436 return (CTL_RETVAL_COMPLETE);
9440 * SCSI VPD page 0x80, the Unit Serial Number page.
9443 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9445 struct scsi_vpd_unit_serial_number *sn_ptr;
9446 struct ctl_lun *lun;
9449 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9451 data_len = 4 + CTL_SN_LEN;
9452 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9453 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9454 if (data_len < alloc_len) {
9455 ctsio->residual = alloc_len - data_len;
9456 ctsio->kern_data_len = data_len;
9457 ctsio->kern_total_len = data_len;
9459 ctsio->residual = 0;
9460 ctsio->kern_data_len = alloc_len;
9461 ctsio->kern_total_len = alloc_len;
9463 ctsio->kern_data_resid = 0;
9464 ctsio->kern_rel_offset = 0;
9465 ctsio->kern_sg_entries = 0;
9468 * The control device is always connected. The disk device, on the
9469 * other hand, may not be online all the time. Need to change this
9470 * to figure out whether the disk device is actually online or not.
9473 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9474 lun->be_lun->lun_type;
9476 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9478 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9479 sn_ptr->length = CTL_SN_LEN;
9481 * If we don't have a LUN, we just leave the serial number as
9485 strncpy((char *)sn_ptr->serial_num,
9486 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9488 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9490 ctl_set_success(ctsio);
9491 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9492 ctsio->be_move_done = ctl_config_move_done;
9493 ctl_datamove((union ctl_io *)ctsio);
9494 return (CTL_RETVAL_COMPLETE);
9499 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9502 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9504 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9505 struct ctl_lun *lun;
9508 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9510 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9511 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9512 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9513 ctsio->kern_sg_entries = 0;
9515 if (data_len < alloc_len) {
9516 ctsio->residual = alloc_len - data_len;
9517 ctsio->kern_data_len = data_len;
9518 ctsio->kern_total_len = data_len;
9520 ctsio->residual = 0;
9521 ctsio->kern_data_len = alloc_len;
9522 ctsio->kern_total_len = alloc_len;
9524 ctsio->kern_data_resid = 0;
9525 ctsio->kern_rel_offset = 0;
9526 ctsio->kern_sg_entries = 0;
9529 * The control device is always connected. The disk device, on the
9530 * other hand, may not be online all the time.
9533 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9534 lun->be_lun->lun_type;
9536 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9537 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9538 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9540 * We support head of queue, ordered and simple tags.
9542 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9544 * Volatile cache supported.
9546 eid_ptr->flags3 = SVPD_EID_V_SUP;
9549 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9550 * attention for a particular IT nexus on all LUNs once we report
9551 * it to that nexus once. This bit is required as of SPC-4.
9553 eid_ptr->flags4 = SVPD_EID_LUICLR;
9556 * XXX KDM in order to correctly answer this, we would need
9557 * information from the SIM to determine how much sense data it
9558 * can send. So this would really be a path inquiry field, most
9559 * likely. This can be set to a maximum of 252 according to SPC-4,
9560 * but the hardware may or may not be able to support that much.
9561 * 0 just means that the maximum sense data length is not reported.
9563 eid_ptr->max_sense_length = 0;
9565 ctl_set_success(ctsio);
9566 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9567 ctsio->be_move_done = ctl_config_move_done;
9568 ctl_datamove((union ctl_io *)ctsio);
9569 return (CTL_RETVAL_COMPLETE);
9573 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9575 struct scsi_vpd_mode_page_policy *mpp_ptr;
9576 struct ctl_lun *lun;
9579 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9581 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9582 sizeof(struct scsi_vpd_mode_page_policy_descr);
9584 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9585 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9586 ctsio->kern_sg_entries = 0;
9588 if (data_len < alloc_len) {
9589 ctsio->residual = alloc_len - data_len;
9590 ctsio->kern_data_len = data_len;
9591 ctsio->kern_total_len = data_len;
9593 ctsio->residual = 0;
9594 ctsio->kern_data_len = alloc_len;
9595 ctsio->kern_total_len = alloc_len;
9597 ctsio->kern_data_resid = 0;
9598 ctsio->kern_rel_offset = 0;
9599 ctsio->kern_sg_entries = 0;
9602 * The control device is always connected. The disk device, on the
9603 * other hand, may not be online all the time.
9606 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9607 lun->be_lun->lun_type;
9609 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9610 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9611 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9612 mpp_ptr->descr[0].page_code = 0x3f;
9613 mpp_ptr->descr[0].subpage_code = 0xff;
9614 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9616 ctl_set_success(ctsio);
9617 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9618 ctsio->be_move_done = ctl_config_move_done;
9619 ctl_datamove((union ctl_io *)ctsio);
9620 return (CTL_RETVAL_COMPLETE);
9624 * SCSI VPD page 0x83, the Device Identification page.
9627 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9629 struct scsi_vpd_device_id *devid_ptr;
9630 struct scsi_vpd_id_descriptor *desc;
9631 struct ctl_softc *softc;
9632 struct ctl_lun *lun;
9633 struct ctl_port *port;
9637 softc = control_softc;
9639 port = ctl_io_port(&ctsio->io_hdr);
9640 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9642 data_len = sizeof(struct scsi_vpd_device_id) +
9643 sizeof(struct scsi_vpd_id_descriptor) +
9644 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9645 sizeof(struct scsi_vpd_id_descriptor) +
9646 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9647 if (lun && lun->lun_devid)
9648 data_len += lun->lun_devid->len;
9649 if (port && port->port_devid)
9650 data_len += port->port_devid->len;
9651 if (port && port->target_devid)
9652 data_len += port->target_devid->len;
9654 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9655 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9656 ctsio->kern_sg_entries = 0;
9658 if (data_len < alloc_len) {
9659 ctsio->residual = alloc_len - data_len;
9660 ctsio->kern_data_len = data_len;
9661 ctsio->kern_total_len = data_len;
9663 ctsio->residual = 0;
9664 ctsio->kern_data_len = alloc_len;
9665 ctsio->kern_total_len = alloc_len;
9667 ctsio->kern_data_resid = 0;
9668 ctsio->kern_rel_offset = 0;
9669 ctsio->kern_sg_entries = 0;
9672 * The control device is always connected. The disk device, on the
9673 * other hand, may not be online all the time.
9676 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9677 lun->be_lun->lun_type;
9679 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9680 devid_ptr->page_code = SVPD_DEVICE_ID;
9681 scsi_ulto2b(data_len - 4, devid_ptr->length);
9683 if (port && port->port_type == CTL_PORT_FC)
9684 proto = SCSI_PROTO_FC << 4;
9685 else if (port && port->port_type == CTL_PORT_ISCSI)
9686 proto = SCSI_PROTO_ISCSI << 4;
9688 proto = SCSI_PROTO_SPI << 4;
9689 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9692 * We're using a LUN association here. i.e., this device ID is a
9693 * per-LUN identifier.
9695 if (lun && lun->lun_devid) {
9696 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9697 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9698 lun->lun_devid->len);
9702 * This is for the WWPN which is a port association.
9704 if (port && port->port_devid) {
9705 memcpy(desc, port->port_devid->data, port->port_devid->len);
9706 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9707 port->port_devid->len);
9711 * This is for the Relative Target Port(type 4h) identifier
9713 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9714 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9715 SVPD_ID_TYPE_RELTARG;
9717 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9718 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9719 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9722 * This is for the Target Port Group(type 5h) identifier
9724 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9725 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9726 SVPD_ID_TYPE_TPORTGRP;
9728 if (softc->is_single ||
9729 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9732 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9733 scsi_ulto2b(g, &desc->identifier[2]);
9734 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9735 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9738 * This is for the Target identifier
9740 if (port && port->target_devid) {
9741 memcpy(desc, port->target_devid->data, port->target_devid->len);
9744 ctl_set_success(ctsio);
9745 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9746 ctsio->be_move_done = ctl_config_move_done;
9747 ctl_datamove((union ctl_io *)ctsio);
9748 return (CTL_RETVAL_COMPLETE);
9752 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9754 struct ctl_softc *softc = control_softc;
9755 struct scsi_vpd_scsi_ports *sp;
9756 struct scsi_vpd_port_designation *pd;
9757 struct scsi_vpd_port_designation_cont *pdc;
9758 struct ctl_lun *lun;
9759 struct ctl_port *port;
9760 int data_len, num_target_ports, iid_len, id_len;
9762 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9764 num_target_ports = 0;
9767 mtx_lock(&softc->ctl_lock);
9768 STAILQ_FOREACH(port, &softc->port_list, links) {
9769 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9772 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9775 if (port->init_devid)
9776 iid_len += port->init_devid->len;
9777 if (port->port_devid)
9778 id_len += port->port_devid->len;
9780 mtx_unlock(&softc->ctl_lock);
9782 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9783 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9784 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9785 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9786 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9787 ctsio->kern_sg_entries = 0;
9789 if (data_len < alloc_len) {
9790 ctsio->residual = alloc_len - data_len;
9791 ctsio->kern_data_len = data_len;
9792 ctsio->kern_total_len = data_len;
9794 ctsio->residual = 0;
9795 ctsio->kern_data_len = alloc_len;
9796 ctsio->kern_total_len = alloc_len;
9798 ctsio->kern_data_resid = 0;
9799 ctsio->kern_rel_offset = 0;
9800 ctsio->kern_sg_entries = 0;
9803 * The control device is always connected. The disk device, on the
9804 * other hand, may not be online all the time. Need to change this
9805 * to figure out whether the disk device is actually online or not.
9808 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9809 lun->be_lun->lun_type;
9811 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9813 sp->page_code = SVPD_SCSI_PORTS;
9814 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9816 pd = &sp->design[0];
9818 mtx_lock(&softc->ctl_lock);
9819 STAILQ_FOREACH(port, &softc->port_list, links) {
9820 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9823 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9825 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9826 if (port->init_devid) {
9827 iid_len = port->init_devid->len;
9828 memcpy(pd->initiator_transportid,
9829 port->init_devid->data, port->init_devid->len);
9832 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9833 pdc = (struct scsi_vpd_port_designation_cont *)
9834 (&pd->initiator_transportid[iid_len]);
9835 if (port->port_devid) {
9836 id_len = port->port_devid->len;
9837 memcpy(pdc->target_port_descriptors,
9838 port->port_devid->data, port->port_devid->len);
9841 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9842 pd = (struct scsi_vpd_port_designation *)
9843 ((uint8_t *)pdc->target_port_descriptors + id_len);
9845 mtx_unlock(&softc->ctl_lock);
9847 ctl_set_success(ctsio);
9848 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9849 ctsio->be_move_done = ctl_config_move_done;
9850 ctl_datamove((union ctl_io *)ctsio);
9851 return (CTL_RETVAL_COMPLETE);
9855 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9857 struct scsi_vpd_block_limits *bl_ptr;
9858 struct ctl_lun *lun;
9861 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9863 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9864 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9865 ctsio->kern_sg_entries = 0;
9867 if (sizeof(*bl_ptr) < alloc_len) {
9868 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9869 ctsio->kern_data_len = sizeof(*bl_ptr);
9870 ctsio->kern_total_len = sizeof(*bl_ptr);
9872 ctsio->residual = 0;
9873 ctsio->kern_data_len = alloc_len;
9874 ctsio->kern_total_len = alloc_len;
9876 ctsio->kern_data_resid = 0;
9877 ctsio->kern_rel_offset = 0;
9878 ctsio->kern_sg_entries = 0;
9881 * The control device is always connected. The disk device, on the
9882 * other hand, may not be online all the time. Need to change this
9883 * to figure out whether the disk device is actually online or not.
9886 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9887 lun->be_lun->lun_type;
9889 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9891 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9892 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9893 bl_ptr->max_cmp_write_len = 0xff;
9894 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9896 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9897 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9899 ctl_get_opt_number(&lun->be_lun->options,
9900 "unmap_max_lba", &ival);
9901 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9903 ctl_get_opt_number(&lun->be_lun->options,
9904 "unmap_max_descr", &ival);
9905 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9906 if (lun->be_lun->ublockexp != 0) {
9907 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9908 bl_ptr->opt_unmap_grain);
9909 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9910 bl_ptr->unmap_grain_align);
9913 scsi_ulto4b(lun->be_lun->atomicblock,
9914 bl_ptr->max_atomic_transfer_length);
9915 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9916 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9917 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9918 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9920 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9921 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9924 ctl_set_success(ctsio);
9925 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9926 ctsio->be_move_done = ctl_config_move_done;
9927 ctl_datamove((union ctl_io *)ctsio);
9928 return (CTL_RETVAL_COMPLETE);
9932 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9934 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9935 struct ctl_lun *lun;
9939 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9941 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9942 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9943 ctsio->kern_sg_entries = 0;
9945 if (sizeof(*bdc_ptr) < alloc_len) {
9946 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9947 ctsio->kern_data_len = sizeof(*bdc_ptr);
9948 ctsio->kern_total_len = sizeof(*bdc_ptr);
9950 ctsio->residual = 0;
9951 ctsio->kern_data_len = alloc_len;
9952 ctsio->kern_total_len = alloc_len;
9954 ctsio->kern_data_resid = 0;
9955 ctsio->kern_rel_offset = 0;
9956 ctsio->kern_sg_entries = 0;
9959 * The control device is always connected. The disk device, on the
9960 * other hand, may not be online all the time. Need to change this
9961 * to figure out whether the disk device is actually online or not.
9964 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9965 lun->be_lun->lun_type;
9967 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9968 bdc_ptr->page_code = SVPD_BDC;
9969 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9971 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9972 i = strtol(value, NULL, 0);
9974 i = CTL_DEFAULT_ROTATION_RATE;
9975 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9977 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9978 i = strtol(value, NULL, 0);
9981 bdc_ptr->wab_wac_ff = (i & 0x0f);
9982 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9984 ctl_set_success(ctsio);
9985 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9986 ctsio->be_move_done = ctl_config_move_done;
9987 ctl_datamove((union ctl_io *)ctsio);
9988 return (CTL_RETVAL_COMPLETE);
9992 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9994 struct scsi_vpd_logical_block_prov *lbp_ptr;
9995 struct ctl_lun *lun;
9998 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10000 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10001 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10002 ctsio->kern_sg_entries = 0;
10004 if (sizeof(*lbp_ptr) < alloc_len) {
10005 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10006 ctsio->kern_data_len = sizeof(*lbp_ptr);
10007 ctsio->kern_total_len = sizeof(*lbp_ptr);
10009 ctsio->residual = 0;
10010 ctsio->kern_data_len = alloc_len;
10011 ctsio->kern_total_len = alloc_len;
10013 ctsio->kern_data_resid = 0;
10014 ctsio->kern_rel_offset = 0;
10015 ctsio->kern_sg_entries = 0;
10018 * The control device is always connected. The disk device, on the
10019 * other hand, may not be online all the time. Need to change this
10020 * to figure out whether the disk device is actually online or not.
10023 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10024 lun->be_lun->lun_type;
10026 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10028 lbp_ptr->page_code = SVPD_LBP;
10029 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10030 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10031 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10032 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10033 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10034 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
10035 if (value != NULL) {
10036 if (strcmp(value, "resource") == 0)
10037 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10038 else if (strcmp(value, "thin") == 0)
10039 lbp_ptr->prov_type = SVPD_LBP_THIN;
10041 lbp_ptr->prov_type = SVPD_LBP_THIN;
10044 ctl_set_success(ctsio);
10045 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10046 ctsio->be_move_done = ctl_config_move_done;
10047 ctl_datamove((union ctl_io *)ctsio);
10048 return (CTL_RETVAL_COMPLETE);
10052 * INQUIRY with the EVPD bit set.
10055 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10057 struct ctl_lun *lun;
10058 struct scsi_inquiry *cdb;
10059 int alloc_len, retval;
10061 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10062 cdb = (struct scsi_inquiry *)ctsio->cdb;
10063 alloc_len = scsi_2btoul(cdb->length);
10065 switch (cdb->page_code) {
10066 case SVPD_SUPPORTED_PAGES:
10067 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10069 case SVPD_UNIT_SERIAL_NUMBER:
10070 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10072 case SVPD_DEVICE_ID:
10073 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10075 case SVPD_EXTENDED_INQUIRY_DATA:
10076 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10078 case SVPD_MODE_PAGE_POLICY:
10079 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10081 case SVPD_SCSI_PORTS:
10082 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10084 case SVPD_SCSI_TPC:
10085 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10087 case SVPD_BLOCK_LIMITS:
10088 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10090 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10093 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10095 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10098 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10100 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10104 ctl_set_invalid_field(ctsio,
10110 ctl_done((union ctl_io *)ctsio);
10111 retval = CTL_RETVAL_COMPLETE;
10119 * Standard INQUIRY data.
10122 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10124 struct scsi_inquiry_data *inq_ptr;
10125 struct scsi_inquiry *cdb;
10126 struct ctl_softc *softc = control_softc;
10127 struct ctl_port *port;
10128 struct ctl_lun *lun;
10130 uint32_t alloc_len, data_len;
10131 ctl_port_type port_type;
10133 port = ctl_io_port(&ctsio->io_hdr);
10134 port_type = port->port_type;
10135 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10136 port_type = CTL_PORT_SCSI;
10138 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10139 cdb = (struct scsi_inquiry *)ctsio->cdb;
10140 alloc_len = scsi_2btoul(cdb->length);
10143 * We malloc the full inquiry data size here and fill it
10144 * in. If the user only asks for less, we'll give him
10147 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10148 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10149 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10150 ctsio->kern_sg_entries = 0;
10151 ctsio->kern_data_resid = 0;
10152 ctsio->kern_rel_offset = 0;
10154 if (data_len < alloc_len) {
10155 ctsio->residual = alloc_len - data_len;
10156 ctsio->kern_data_len = data_len;
10157 ctsio->kern_total_len = data_len;
10159 ctsio->residual = 0;
10160 ctsio->kern_data_len = alloc_len;
10161 ctsio->kern_total_len = alloc_len;
10165 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10166 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10167 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10168 lun->be_lun->lun_type;
10170 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10171 lun->be_lun->lun_type;
10173 if (lun->flags & CTL_LUN_REMOVABLE)
10174 inq_ptr->dev_qual2 |= SID_RMB;
10176 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10178 /* RMB in byte 2 is 0 */
10179 inq_ptr->version = SCSI_REV_SPC5;
10182 * According to SAM-3, even if a device only supports a single
10183 * level of LUN addressing, it should still set the HISUP bit:
10185 * 4.9.1 Logical unit numbers overview
10187 * All logical unit number formats described in this standard are
10188 * hierarchical in structure even when only a single level in that
10189 * hierarchy is used. The HISUP bit shall be set to one in the
10190 * standard INQUIRY data (see SPC-2) when any logical unit number
10191 * format described in this standard is used. Non-hierarchical
10192 * formats are outside the scope of this standard.
10194 * Therefore we set the HiSup bit here.
10196 * The response format is 2, per SPC-3.
10198 inq_ptr->response_format = SID_HiSup | 2;
10200 inq_ptr->additional_length = data_len -
10201 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10202 CTL_DEBUG_PRINT(("additional_length = %d\n",
10203 inq_ptr->additional_length));
10205 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10206 if (port_type == CTL_PORT_SCSI)
10207 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10208 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10209 inq_ptr->flags = SID_CmdQue;
10210 if (port_type == CTL_PORT_SCSI)
10211 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10214 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10215 * We have 8 bytes for the vendor name, and 16 bytes for the device
10216 * name and 4 bytes for the revision.
10218 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10219 "vendor")) == NULL) {
10220 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10222 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10223 strncpy(inq_ptr->vendor, val,
10224 min(sizeof(inq_ptr->vendor), strlen(val)));
10227 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10228 sizeof(inq_ptr->product));
10229 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10230 switch (lun->be_lun->lun_type) {
10232 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10233 sizeof(inq_ptr->product));
10236 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10237 sizeof(inq_ptr->product));
10240 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10241 sizeof(inq_ptr->product));
10244 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10245 sizeof(inq_ptr->product));
10249 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10250 strncpy(inq_ptr->product, val,
10251 min(sizeof(inq_ptr->product), strlen(val)));
10255 * XXX make this a macro somewhere so it automatically gets
10256 * incremented when we make changes.
10258 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10259 "revision")) == NULL) {
10260 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10262 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10263 strncpy(inq_ptr->revision, val,
10264 min(sizeof(inq_ptr->revision), strlen(val)));
10268 * For parallel SCSI, we support double transition and single
10269 * transition clocking. We also support QAS (Quick Arbitration
10270 * and Selection) and Information Unit transfers on both the
10271 * control and array devices.
10273 if (port_type == CTL_PORT_SCSI)
10274 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10277 /* SAM-6 (no version claimed) */
10278 scsi_ulto2b(0x00C0, inq_ptr->version1);
10279 /* SPC-5 (no version claimed) */
10280 scsi_ulto2b(0x05C0, inq_ptr->version2);
10281 if (port_type == CTL_PORT_FC) {
10282 /* FCP-2 ANSI INCITS.350:2003 */
10283 scsi_ulto2b(0x0917, inq_ptr->version3);
10284 } else if (port_type == CTL_PORT_SCSI) {
10285 /* SPI-4 ANSI INCITS.362:200x */
10286 scsi_ulto2b(0x0B56, inq_ptr->version3);
10287 } else if (port_type == CTL_PORT_ISCSI) {
10288 /* iSCSI (no version claimed) */
10289 scsi_ulto2b(0x0960, inq_ptr->version3);
10290 } else if (port_type == CTL_PORT_SAS) {
10291 /* SAS (no version claimed) */
10292 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10296 /* SBC-4 (no version claimed) */
10297 scsi_ulto2b(0x0600, inq_ptr->version4);
10299 switch (lun->be_lun->lun_type) {
10301 /* SBC-4 (no version claimed) */
10302 scsi_ulto2b(0x0600, inq_ptr->version4);
10307 /* MMC-6 (no version claimed) */
10308 scsi_ulto2b(0x04E0, inq_ptr->version4);
10315 ctl_set_success(ctsio);
10316 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10317 ctsio->be_move_done = ctl_config_move_done;
10318 ctl_datamove((union ctl_io *)ctsio);
10319 return (CTL_RETVAL_COMPLETE);
10323 ctl_inquiry(struct ctl_scsiio *ctsio)
10325 struct scsi_inquiry *cdb;
10328 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10330 cdb = (struct scsi_inquiry *)ctsio->cdb;
10331 if (cdb->byte2 & SI_EVPD)
10332 retval = ctl_inquiry_evpd(ctsio);
10333 else if (cdb->page_code == 0)
10334 retval = ctl_inquiry_std(ctsio);
10336 ctl_set_invalid_field(ctsio,
10342 ctl_done((union ctl_io *)ctsio);
10343 return (CTL_RETVAL_COMPLETE);
10350 ctl_get_config(struct ctl_scsiio *ctsio)
10352 struct scsi_get_config_header *hdr;
10353 struct scsi_get_config_feature *feature;
10354 struct scsi_get_config *cdb;
10355 struct ctl_lun *lun;
10356 uint32_t alloc_len, data_len;
10359 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10360 cdb = (struct scsi_get_config *)ctsio->cdb;
10361 rt = (cdb->rt & SGC_RT_MASK);
10362 starting = scsi_2btoul(cdb->starting_feature);
10363 alloc_len = scsi_2btoul(cdb->length);
10365 data_len = sizeof(struct scsi_get_config_header) +
10366 sizeof(struct scsi_get_config_feature) + 8 +
10367 sizeof(struct scsi_get_config_feature) + 8 +
10368 sizeof(struct scsi_get_config_feature) + 4 +
10369 sizeof(struct scsi_get_config_feature) + 4 +
10370 sizeof(struct scsi_get_config_feature) + 8 +
10371 sizeof(struct scsi_get_config_feature) +
10372 sizeof(struct scsi_get_config_feature) + 4 +
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 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10379 ctsio->kern_sg_entries = 0;
10380 ctsio->kern_data_resid = 0;
10381 ctsio->kern_rel_offset = 0;
10383 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10384 if (lun->flags & CTL_LUN_NO_MEDIA)
10385 scsi_ulto2b(0x0000, hdr->current_profile);
10387 scsi_ulto2b(0x0010, hdr->current_profile);
10388 feature = (struct scsi_get_config_feature *)(hdr + 1);
10390 if (starting > 0x003b)
10392 if (starting > 0x003a)
10394 if (starting > 0x002b)
10396 if (starting > 0x002a)
10398 if (starting > 0x001f)
10400 if (starting > 0x001e)
10402 if (starting > 0x001d)
10404 if (starting > 0x0010)
10406 if (starting > 0x0003)
10408 if (starting > 0x0002)
10410 if (starting > 0x0001)
10412 if (starting > 0x0000)
10416 scsi_ulto2b(0x0000, feature->feature_code);
10417 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10418 feature->add_length = 8;
10419 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10420 feature->feature_data[2] = 0x00;
10421 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10422 feature->feature_data[6] = 0x01;
10423 feature = (struct scsi_get_config_feature *)
10424 &feature->feature_data[feature->add_length];
10427 scsi_ulto2b(0x0001, feature->feature_code);
10428 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10429 feature->add_length = 8;
10430 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10431 feature->feature_data[4] = 0x03;
10432 feature = (struct scsi_get_config_feature *)
10433 &feature->feature_data[feature->add_length];
10436 scsi_ulto2b(0x0002, feature->feature_code);
10437 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10438 feature->add_length = 4;
10439 feature->feature_data[0] = 0x02;
10440 feature = (struct scsi_get_config_feature *)
10441 &feature->feature_data[feature->add_length];
10443 f3: /* Removable Medium */
10444 scsi_ulto2b(0x0003, feature->feature_code);
10445 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10446 feature->add_length = 4;
10447 feature->feature_data[0] = 0x39;
10448 feature = (struct scsi_get_config_feature *)
10449 &feature->feature_data[feature->add_length];
10451 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10454 f10: /* Random Read */
10455 scsi_ulto2b(0x0010, feature->feature_code);
10456 feature->flags = 0x00;
10457 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10458 feature->flags |= SGC_F_CURRENT;
10459 feature->add_length = 8;
10460 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10461 scsi_ulto2b(1, &feature->feature_data[4]);
10462 feature->feature_data[6] = 0x00;
10463 feature = (struct scsi_get_config_feature *)
10464 &feature->feature_data[feature->add_length];
10466 f1d: /* Multi-Read */
10467 scsi_ulto2b(0x001D, feature->feature_code);
10468 feature->flags = 0x00;
10469 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10470 feature->flags |= SGC_F_CURRENT;
10471 feature->add_length = 0;
10472 feature = (struct scsi_get_config_feature *)
10473 &feature->feature_data[feature->add_length];
10476 scsi_ulto2b(0x001E, feature->feature_code);
10477 feature->flags = 0x00;
10478 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10479 feature->flags |= SGC_F_CURRENT;
10480 feature->add_length = 4;
10481 feature->feature_data[0] = 0x00;
10482 feature = (struct scsi_get_config_feature *)
10483 &feature->feature_data[feature->add_length];
10485 f1f: /* DVD Read */
10486 scsi_ulto2b(0x001F, feature->feature_code);
10487 feature->flags = 0x08;
10488 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10489 feature->flags |= SGC_F_CURRENT;
10490 feature->add_length = 4;
10491 feature->feature_data[0] = 0x01;
10492 feature->feature_data[2] = 0x03;
10493 feature = (struct scsi_get_config_feature *)
10494 &feature->feature_data[feature->add_length];
10497 scsi_ulto2b(0x002A, feature->feature_code);
10498 feature->flags = 0x04;
10499 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10500 feature->flags |= SGC_F_CURRENT;
10501 feature->add_length = 4;
10502 feature->feature_data[0] = 0x00;
10503 feature->feature_data[1] = 0x00;
10504 feature = (struct scsi_get_config_feature *)
10505 &feature->feature_data[feature->add_length];
10508 scsi_ulto2b(0x002B, feature->feature_code);
10509 feature->flags = 0x00;
10510 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10511 feature->flags |= SGC_F_CURRENT;
10512 feature->add_length = 4;
10513 feature->feature_data[0] = 0x00;
10514 feature = (struct scsi_get_config_feature *)
10515 &feature->feature_data[feature->add_length];
10517 f3a: /* DVD+RW Dual Layer */
10518 scsi_ulto2b(0x003A, feature->feature_code);
10519 feature->flags = 0x00;
10520 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10521 feature->flags |= SGC_F_CURRENT;
10522 feature->add_length = 4;
10523 feature->feature_data[0] = 0x00;
10524 feature->feature_data[1] = 0x00;
10525 feature = (struct scsi_get_config_feature *)
10526 &feature->feature_data[feature->add_length];
10528 f3b: /* DVD+R Dual Layer */
10529 scsi_ulto2b(0x003B, feature->feature_code);
10530 feature->flags = 0x00;
10531 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10532 feature->flags |= SGC_F_CURRENT;
10533 feature->add_length = 4;
10534 feature->feature_data[0] = 0x00;
10535 feature = (struct scsi_get_config_feature *)
10536 &feature->feature_data[feature->add_length];
10539 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10540 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10541 feature = (struct scsi_get_config_feature *)(hdr + 1);
10542 if (scsi_2btoul(feature->feature_code) == starting)
10543 feature = (struct scsi_get_config_feature *)
10544 &feature->feature_data[feature->add_length];
10545 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10547 scsi_ulto4b(data_len - 4, hdr->data_length);
10548 if (data_len < alloc_len) {
10549 ctsio->residual = alloc_len - data_len;
10550 ctsio->kern_data_len = data_len;
10551 ctsio->kern_total_len = data_len;
10553 ctsio->residual = 0;
10554 ctsio->kern_data_len = alloc_len;
10555 ctsio->kern_total_len = alloc_len;
10558 ctl_set_success(ctsio);
10559 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10560 ctsio->be_move_done = ctl_config_move_done;
10561 ctl_datamove((union ctl_io *)ctsio);
10562 return (CTL_RETVAL_COMPLETE);
10566 ctl_get_event_status(struct ctl_scsiio *ctsio)
10568 struct scsi_get_event_status_header *hdr;
10569 struct scsi_get_event_status *cdb;
10570 struct ctl_lun *lun;
10571 uint32_t alloc_len, data_len;
10574 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10575 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10576 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10577 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10578 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10579 ctl_done((union ctl_io *)ctsio);
10580 return (CTL_RETVAL_COMPLETE);
10582 notif_class = cdb->notif_class;
10583 alloc_len = scsi_2btoul(cdb->length);
10585 data_len = sizeof(struct scsi_get_event_status_header);
10586 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10587 ctsio->kern_sg_entries = 0;
10588 ctsio->kern_data_resid = 0;
10589 ctsio->kern_rel_offset = 0;
10591 if (data_len < alloc_len) {
10592 ctsio->residual = alloc_len - data_len;
10593 ctsio->kern_data_len = data_len;
10594 ctsio->kern_total_len = data_len;
10596 ctsio->residual = 0;
10597 ctsio->kern_data_len = alloc_len;
10598 ctsio->kern_total_len = alloc_len;
10601 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10602 scsi_ulto2b(0, hdr->descr_length);
10603 hdr->nea_class = SGESN_NEA;
10604 hdr->supported_class = 0;
10606 ctl_set_success(ctsio);
10607 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10608 ctsio->be_move_done = ctl_config_move_done;
10609 ctl_datamove((union ctl_io *)ctsio);
10610 return (CTL_RETVAL_COMPLETE);
10614 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10616 struct scsi_mechanism_status_header *hdr;
10617 struct scsi_mechanism_status *cdb;
10618 struct ctl_lun *lun;
10619 uint32_t alloc_len, data_len;
10621 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10622 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10623 alloc_len = scsi_2btoul(cdb->length);
10625 data_len = sizeof(struct scsi_mechanism_status_header);
10626 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10627 ctsio->kern_sg_entries = 0;
10628 ctsio->kern_data_resid = 0;
10629 ctsio->kern_rel_offset = 0;
10631 if (data_len < alloc_len) {
10632 ctsio->residual = alloc_len - data_len;
10633 ctsio->kern_data_len = data_len;
10634 ctsio->kern_total_len = data_len;
10636 ctsio->residual = 0;
10637 ctsio->kern_data_len = alloc_len;
10638 ctsio->kern_total_len = alloc_len;
10641 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10642 hdr->state1 = 0x00;
10643 hdr->state2 = 0xe0;
10644 scsi_ulto3b(0, hdr->lba);
10645 hdr->slots_num = 0;
10646 scsi_ulto2b(0, hdr->slots_length);
10648 ctl_set_success(ctsio);
10649 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10650 ctsio->be_move_done = ctl_config_move_done;
10651 ctl_datamove((union ctl_io *)ctsio);
10652 return (CTL_RETVAL_COMPLETE);
10656 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10661 buf[1] = bin2bcd((lba / 75) / 60);
10662 buf[2] = bin2bcd((lba / 75) % 60);
10663 buf[3] = bin2bcd(lba % 75);
10667 ctl_read_toc(struct ctl_scsiio *ctsio)
10669 struct scsi_read_toc_hdr *hdr;
10670 struct scsi_read_toc_type01_descr *descr;
10671 struct scsi_read_toc *cdb;
10672 struct ctl_lun *lun;
10673 uint32_t alloc_len, data_len;
10676 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10677 cdb = (struct scsi_read_toc *)ctsio->cdb;
10678 msf = (cdb->byte2 & CD_MSF) != 0;
10679 format = cdb->format;
10680 alloc_len = scsi_2btoul(cdb->data_len);
10682 data_len = sizeof(struct scsi_read_toc_hdr);
10684 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10686 data_len += sizeof(struct scsi_read_toc_type01_descr);
10687 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10688 ctsio->kern_sg_entries = 0;
10689 ctsio->kern_data_resid = 0;
10690 ctsio->kern_rel_offset = 0;
10692 if (data_len < alloc_len) {
10693 ctsio->residual = alloc_len - data_len;
10694 ctsio->kern_data_len = data_len;
10695 ctsio->kern_total_len = data_len;
10697 ctsio->residual = 0;
10698 ctsio->kern_data_len = alloc_len;
10699 ctsio->kern_total_len = alloc_len;
10702 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10704 scsi_ulto2b(0x12, hdr->data_length);
10707 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10708 descr->addr_ctl = 0x14;
10709 descr->track_number = 1;
10711 ctl_ultomsf(0, descr->track_start);
10713 scsi_ulto4b(0, descr->track_start);
10715 descr->addr_ctl = 0x14;
10716 descr->track_number = 0xaa;
10718 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10720 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10722 scsi_ulto2b(0x0a, hdr->data_length);
10725 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10726 descr->addr_ctl = 0x14;
10727 descr->track_number = 1;
10729 ctl_ultomsf(0, descr->track_start);
10731 scsi_ulto4b(0, descr->track_start);
10734 ctl_set_success(ctsio);
10735 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10736 ctsio->be_move_done = ctl_config_move_done;
10737 ctl_datamove((union ctl_io *)ctsio);
10738 return (CTL_RETVAL_COMPLETE);
10742 * For known CDB types, parse the LBA and length.
10745 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10747 if (io->io_hdr.io_type != CTL_IO_SCSI)
10750 switch (io->scsiio.cdb[0]) {
10751 case COMPARE_AND_WRITE: {
10752 struct scsi_compare_and_write *cdb;
10754 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10756 *lba = scsi_8btou64(cdb->addr);
10757 *len = cdb->length;
10762 struct scsi_rw_6 *cdb;
10764 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10766 *lba = scsi_3btoul(cdb->addr);
10767 /* only 5 bits are valid in the most significant address byte */
10769 *len = cdb->length;
10774 struct scsi_rw_10 *cdb;
10776 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10778 *lba = scsi_4btoul(cdb->addr);
10779 *len = scsi_2btoul(cdb->length);
10782 case WRITE_VERIFY_10: {
10783 struct scsi_write_verify_10 *cdb;
10785 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10787 *lba = scsi_4btoul(cdb->addr);
10788 *len = scsi_2btoul(cdb->length);
10793 struct scsi_rw_12 *cdb;
10795 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10797 *lba = scsi_4btoul(cdb->addr);
10798 *len = scsi_4btoul(cdb->length);
10801 case WRITE_VERIFY_12: {
10802 struct scsi_write_verify_12 *cdb;
10804 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10806 *lba = scsi_4btoul(cdb->addr);
10807 *len = scsi_4btoul(cdb->length);
10812 struct scsi_rw_16 *cdb;
10814 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10816 *lba = scsi_8btou64(cdb->addr);
10817 *len = scsi_4btoul(cdb->length);
10820 case WRITE_ATOMIC_16: {
10821 struct scsi_write_atomic_16 *cdb;
10823 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10825 *lba = scsi_8btou64(cdb->addr);
10826 *len = scsi_2btoul(cdb->length);
10829 case WRITE_VERIFY_16: {
10830 struct scsi_write_verify_16 *cdb;
10832 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10834 *lba = scsi_8btou64(cdb->addr);
10835 *len = scsi_4btoul(cdb->length);
10838 case WRITE_SAME_10: {
10839 struct scsi_write_same_10 *cdb;
10841 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10843 *lba = scsi_4btoul(cdb->addr);
10844 *len = scsi_2btoul(cdb->length);
10847 case WRITE_SAME_16: {
10848 struct scsi_write_same_16 *cdb;
10850 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10852 *lba = scsi_8btou64(cdb->addr);
10853 *len = scsi_4btoul(cdb->length);
10857 struct scsi_verify_10 *cdb;
10859 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10861 *lba = scsi_4btoul(cdb->addr);
10862 *len = scsi_2btoul(cdb->length);
10866 struct scsi_verify_12 *cdb;
10868 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10870 *lba = scsi_4btoul(cdb->addr);
10871 *len = scsi_4btoul(cdb->length);
10875 struct scsi_verify_16 *cdb;
10877 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10879 *lba = scsi_8btou64(cdb->addr);
10880 *len = scsi_4btoul(cdb->length);
10888 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10889 struct scsi_get_lba_status *cdb;
10891 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10892 *lba = scsi_8btou64(cdb->addr);
10898 break; /* NOTREACHED */
10905 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10908 uint64_t endlba1, endlba2;
10910 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10911 endlba2 = lba2 + len2 - 1;
10913 if ((endlba1 < lba2) || (endlba2 < lba1))
10914 return (CTL_ACTION_PASS);
10916 return (CTL_ACTION_BLOCK);
10920 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10922 struct ctl_ptr_len_flags *ptrlen;
10923 struct scsi_unmap_desc *buf, *end, *range;
10927 /* If not UNMAP -- go other way. */
10928 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10929 io->scsiio.cdb[0] != UNMAP)
10930 return (CTL_ACTION_ERROR);
10932 /* If UNMAP without data -- block and wait for data. */
10933 ptrlen = (struct ctl_ptr_len_flags *)
10934 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10935 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10936 ptrlen->ptr == NULL)
10937 return (CTL_ACTION_BLOCK);
10939 /* UNMAP with data -- check for collision. */
10940 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10941 end = buf + ptrlen->len / sizeof(*buf);
10942 for (range = buf; range < end; range++) {
10943 lba = scsi_8btou64(range->lba);
10944 len = scsi_4btoul(range->length);
10945 if ((lba < lba2 + len2) && (lba + len > lba2))
10946 return (CTL_ACTION_BLOCK);
10948 return (CTL_ACTION_PASS);
10952 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10954 uint64_t lba1, lba2;
10955 uint64_t len1, len2;
10958 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10959 return (CTL_ACTION_ERROR);
10961 retval = ctl_extent_check_unmap(io1, lba2, len2);
10962 if (retval != CTL_ACTION_ERROR)
10965 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10966 return (CTL_ACTION_ERROR);
10968 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10970 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10974 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10976 uint64_t lba1, lba2;
10977 uint64_t len1, len2;
10979 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10980 return (CTL_ACTION_PASS);
10981 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10982 return (CTL_ACTION_ERROR);
10983 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10984 return (CTL_ACTION_ERROR);
10986 if (lba1 + len1 == lba2)
10987 return (CTL_ACTION_BLOCK);
10988 return (CTL_ACTION_PASS);
10992 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10993 union ctl_io *ooa_io)
10995 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10996 const ctl_serialize_action *serialize_row;
10999 * The initiator attempted multiple untagged commands at the same
11000 * time. Can't do that.
11002 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11003 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11004 && ((pending_io->io_hdr.nexus.targ_port ==
11005 ooa_io->io_hdr.nexus.targ_port)
11006 && (pending_io->io_hdr.nexus.initid ==
11007 ooa_io->io_hdr.nexus.initid))
11008 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11009 CTL_FLAG_STATUS_SENT)) == 0))
11010 return (CTL_ACTION_OVERLAP);
11013 * The initiator attempted to send multiple tagged commands with
11014 * the same ID. (It's fine if different initiators have the same
11017 * Even if all of those conditions are true, we don't kill the I/O
11018 * if the command ahead of us has been aborted. We won't end up
11019 * sending it to the FETD, and it's perfectly legal to resend a
11020 * command with the same tag number as long as the previous
11021 * instance of this tag number has been aborted somehow.
11023 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11024 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11025 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11026 && ((pending_io->io_hdr.nexus.targ_port ==
11027 ooa_io->io_hdr.nexus.targ_port)
11028 && (pending_io->io_hdr.nexus.initid ==
11029 ooa_io->io_hdr.nexus.initid))
11030 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11031 CTL_FLAG_STATUS_SENT)) == 0))
11032 return (CTL_ACTION_OVERLAP_TAG);
11035 * If we get a head of queue tag, SAM-3 says that we should
11036 * immediately execute it.
11038 * What happens if this command would normally block for some other
11039 * reason? e.g. a request sense with a head of queue tag
11040 * immediately after a write. Normally that would block, but this
11041 * will result in its getting executed immediately...
11043 * We currently return "pass" instead of "skip", so we'll end up
11044 * going through the rest of the queue to check for overlapped tags.
11046 * XXX KDM check for other types of blockage first??
11048 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11049 return (CTL_ACTION_PASS);
11052 * Ordered tags have to block until all items ahead of them
11053 * have completed. If we get called with an ordered tag, we always
11054 * block, if something else is ahead of us in the queue.
11056 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11057 return (CTL_ACTION_BLOCK);
11060 * Simple tags get blocked until all head of queue and ordered tags
11061 * ahead of them have completed. I'm lumping untagged commands in
11062 * with simple tags here. XXX KDM is that the right thing to do?
11064 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11065 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11066 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11067 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11068 return (CTL_ACTION_BLOCK);
11070 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11071 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11072 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11073 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11074 pending_io->scsiio.cdb[1], pending_io));
11075 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11076 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11077 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11078 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11079 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11080 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11081 ooa_io->scsiio.cdb[1], ooa_io));
11083 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11085 switch (serialize_row[pending_entry->seridx]) {
11086 case CTL_SER_BLOCK:
11087 return (CTL_ACTION_BLOCK);
11088 case CTL_SER_EXTENT:
11089 return (ctl_extent_check(ooa_io, pending_io,
11090 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11091 case CTL_SER_EXTENTOPT:
11092 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11093 SCP_QUEUE_ALG_UNRESTRICTED)
11094 return (ctl_extent_check(ooa_io, pending_io,
11096 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11097 return (CTL_ACTION_PASS);
11098 case CTL_SER_EXTENTSEQ:
11099 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11100 return (ctl_extent_check_seq(ooa_io, pending_io));
11101 return (CTL_ACTION_PASS);
11103 return (CTL_ACTION_PASS);
11104 case CTL_SER_BLOCKOPT:
11105 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11106 SCP_QUEUE_ALG_UNRESTRICTED)
11107 return (CTL_ACTION_BLOCK);
11108 return (CTL_ACTION_PASS);
11110 return (CTL_ACTION_SKIP);
11112 panic("%s: Invalid serialization value %d for %d => %d",
11113 __func__, serialize_row[pending_entry->seridx],
11114 pending_entry->seridx, ooa_entry->seridx);
11117 return (CTL_ACTION_ERROR);
11121 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11123 * - pending_io is generally either incoming, or on the blocked queue
11124 * - starting I/O is the I/O we want to start the check with.
11127 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11128 union ctl_io *starting_io)
11130 union ctl_io *ooa_io;
11133 mtx_assert(&lun->lun_lock, MA_OWNED);
11136 * Run back along the OOA queue, starting with the current
11137 * blocked I/O and going through every I/O before it on the
11138 * queue. If starting_io is NULL, we'll just end up returning
11141 for (ooa_io = starting_io; ooa_io != NULL;
11142 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11146 * This routine just checks to see whether
11147 * cur_blocked is blocked by ooa_io, which is ahead
11148 * of it in the queue. It doesn't queue/dequeue
11151 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11153 case CTL_ACTION_BLOCK:
11154 case CTL_ACTION_OVERLAP:
11155 case CTL_ACTION_OVERLAP_TAG:
11156 case CTL_ACTION_SKIP:
11157 case CTL_ACTION_ERROR:
11159 break; /* NOTREACHED */
11160 case CTL_ACTION_PASS:
11163 panic("%s: Invalid action %d\n", __func__, action);
11167 return (CTL_ACTION_PASS);
11172 * - An I/O has just completed, and has been removed from the per-LUN OOA
11173 * queue, so some items on the blocked queue may now be unblocked.
11176 ctl_check_blocked(struct ctl_lun *lun)
11178 struct ctl_softc *softc = lun->ctl_softc;
11179 union ctl_io *cur_blocked, *next_blocked;
11181 mtx_assert(&lun->lun_lock, MA_OWNED);
11184 * Run forward from the head of the blocked queue, checking each
11185 * entry against the I/Os prior to it on the OOA queue to see if
11186 * there is still any blockage.
11188 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11189 * with our removing a variable on it while it is traversing the
11192 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11193 cur_blocked != NULL; cur_blocked = next_blocked) {
11194 union ctl_io *prev_ooa;
11197 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11200 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11201 ctl_ooaq, ooa_links);
11204 * If cur_blocked happens to be the first item in the OOA
11205 * queue now, prev_ooa will be NULL, and the action
11206 * returned will just be CTL_ACTION_PASS.
11208 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11211 case CTL_ACTION_BLOCK:
11212 /* Nothing to do here, still blocked */
11214 case CTL_ACTION_OVERLAP:
11215 case CTL_ACTION_OVERLAP_TAG:
11217 * This shouldn't happen! In theory we've already
11218 * checked this command for overlap...
11221 case CTL_ACTION_PASS:
11222 case CTL_ACTION_SKIP: {
11223 const struct ctl_cmd_entry *entry;
11226 * The skip case shouldn't happen, this transaction
11227 * should have never made it onto the blocked queue.
11230 * This I/O is no longer blocked, we can remove it
11231 * from the blocked queue. Since this is a TAILQ
11232 * (doubly linked list), we can do O(1) removals
11233 * from any place on the list.
11235 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11237 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11239 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11240 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11242 * Need to send IO back to original side to
11245 union ctl_ha_msg msg_info;
11247 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11248 msg_info.hdr.original_sc =
11249 cur_blocked->io_hdr.original_sc;
11250 msg_info.hdr.serializing_sc = cur_blocked;
11251 msg_info.hdr.msg_type = CTL_MSG_R2R;
11252 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11253 sizeof(msg_info.hdr), M_NOWAIT);
11256 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11259 * Check this I/O for LUN state changes that may
11260 * have happened while this command was blocked.
11261 * The LUN state may have been changed by a command
11262 * ahead of us in the queue, so we need to re-check
11263 * for any states that can be caused by SCSI
11266 if (ctl_scsiio_lun_check(lun, entry,
11267 &cur_blocked->scsiio) == 0) {
11268 cur_blocked->io_hdr.flags |=
11269 CTL_FLAG_IS_WAS_ON_RTR;
11270 ctl_enqueue_rtr(cur_blocked);
11272 ctl_done(cur_blocked);
11277 * This probably shouldn't happen -- we shouldn't
11278 * get CTL_ACTION_ERROR, or anything else.
11284 return (CTL_RETVAL_COMPLETE);
11288 * This routine (with one exception) checks LUN flags that can be set by
11289 * commands ahead of us in the OOA queue. These flags have to be checked
11290 * when a command initially comes in, and when we pull a command off the
11291 * blocked queue and are preparing to execute it. The reason we have to
11292 * check these flags for commands on the blocked queue is that the LUN
11293 * state may have been changed by a command ahead of us while we're on the
11296 * Ordering is somewhat important with these checks, so please pay
11297 * careful attention to the placement of any new checks.
11300 ctl_scsiio_lun_check(struct ctl_lun *lun,
11301 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11303 struct ctl_softc *softc = lun->ctl_softc;
11309 mtx_assert(&lun->lun_lock, MA_OWNED);
11312 * If this shelf is a secondary shelf controller, we may have to
11313 * reject some commands disallowed by HA mode and link state.
11315 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11316 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11317 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11318 ctl_set_lun_unavail(ctsio);
11322 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11323 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11324 ctl_set_lun_transit(ctsio);
11328 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11329 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11330 ctl_set_lun_standby(ctsio);
11335 /* The rest of checks are only done on executing side */
11336 if (softc->ha_mode == CTL_HA_MODE_XFER)
11340 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11342 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11343 ctl_set_hw_write_protected(ctsio);
11347 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11348 ctl_set_sense(ctsio, /*current_error*/ 1,
11349 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11350 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11357 * Check for a reservation conflict. If this command isn't allowed
11358 * even on reserved LUNs, and if this initiator isn't the one who
11359 * reserved us, reject the command with a reservation conflict.
11361 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11362 if ((lun->flags & CTL_LUN_RESERVED)
11363 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11364 if (lun->res_idx != residx) {
11365 ctl_set_reservation_conflict(ctsio);
11371 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11372 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11373 /* No reservation or command is allowed. */;
11374 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11375 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11376 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11377 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11378 /* The command is allowed for Write Exclusive resv. */;
11381 * if we aren't registered or it's a res holder type
11382 * reservation and this isn't the res holder then set a
11385 if (ctl_get_prkey(lun, residx) == 0 ||
11386 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11387 ctl_set_reservation_conflict(ctsio);
11393 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11394 if (lun->flags & CTL_LUN_EJECTED)
11395 ctl_set_lun_ejected(ctsio);
11396 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11397 if (lun->flags & CTL_LUN_REMOVABLE)
11398 ctl_set_lun_no_media(ctsio);
11400 ctl_set_lun_int_reqd(ctsio);
11401 } else if (lun->flags & CTL_LUN_STOPPED)
11402 ctl_set_lun_stopped(ctsio);
11414 ctl_failover_io(union ctl_io *io, int have_lock)
11416 ctl_set_busy(&io->scsiio);
11421 ctl_failover_lun(union ctl_io *rio)
11423 struct ctl_softc *softc = control_softc;
11424 struct ctl_lun *lun;
11425 struct ctl_io_hdr *io, *next_io;
11428 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11429 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11431 /* Find and lock the LUN. */
11432 mtx_lock(&softc->ctl_lock);
11433 if ((targ_lun < CTL_MAX_LUNS) &&
11434 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11435 mtx_lock(&lun->lun_lock);
11436 mtx_unlock(&softc->ctl_lock);
11437 if (lun->flags & CTL_LUN_DISABLED) {
11438 mtx_unlock(&lun->lun_lock);
11442 mtx_unlock(&softc->ctl_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;
11518 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11519 if ((targ_lun < CTL_MAX_LUNS)
11520 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11522 * If the LUN is invalid, pretend that it doesn't exist.
11523 * It will go away as soon as all pending I/O has been
11526 mtx_lock(&lun->lun_lock);
11527 if (lun->flags & CTL_LUN_DISABLED) {
11528 mtx_unlock(&lun->lun_lock);
11530 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11531 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11533 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11534 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11538 * Every I/O goes into the OOA queue for a
11539 * particular LUN, and stays there until completion.
11542 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11543 lun->idle_time += getsbinuptime() -
11547 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11551 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11552 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11555 /* Get command entry and return error if it is unsuppotyed. */
11556 entry = ctl_validate_command(ctsio);
11557 if (entry == NULL) {
11559 mtx_unlock(&lun->lun_lock);
11563 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11564 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11567 * Check to see whether we can send this command to LUNs that don't
11568 * exist. This should pretty much only be the case for inquiry
11569 * and request sense. Further checks, below, really require having
11570 * a LUN, so we can't really check the command anymore. Just put
11571 * it on the rtr queue.
11574 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11575 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11576 ctl_enqueue_rtr((union ctl_io *)ctsio);
11580 ctl_set_unsupported_lun(ctsio);
11581 ctl_done((union ctl_io *)ctsio);
11582 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11586 * Make sure we support this particular command on this LUN.
11587 * e.g., we don't support writes to the control LUN.
11589 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11590 mtx_unlock(&lun->lun_lock);
11591 ctl_set_invalid_opcode(ctsio);
11592 ctl_done((union ctl_io *)ctsio);
11597 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11601 * If we've got a request sense, it'll clear the contingent
11602 * allegiance condition. Otherwise, if we have a CA condition for
11603 * this initiator, clear it, because it sent down a command other
11604 * than request sense.
11606 if ((ctsio->cdb[0] != REQUEST_SENSE)
11607 && (ctl_is_set(lun->have_ca, initidx)))
11608 ctl_clear_mask(lun->have_ca, initidx);
11612 * If the command has this flag set, it handles its own unit
11613 * attention reporting, we shouldn't do anything. Otherwise we
11614 * check for any pending unit attentions, and send them back to the
11615 * initiator. We only do this when a command initially comes in,
11616 * not when we pull it off the blocked queue.
11618 * According to SAM-3, section 5.3.2, the order that things get
11619 * presented back to the host is basically unit attentions caused
11620 * by some sort of reset event, busy status, reservation conflicts
11621 * or task set full, and finally any other status.
11623 * One issue here is that some of the unit attentions we report
11624 * don't fall into the "reset" category (e.g. "reported luns data
11625 * has changed"). So reporting it here, before the reservation
11626 * check, may be technically wrong. I guess the only thing to do
11627 * would be to check for and report the reset events here, and then
11628 * check for the other unit attention types after we check for a
11629 * reservation conflict.
11631 * XXX KDM need to fix this
11633 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11634 ctl_ua_type ua_type;
11636 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11638 if (ua_type != CTL_UA_NONE) {
11639 mtx_unlock(&lun->lun_lock);
11640 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11641 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11642 ctsio->sense_len = SSD_FULL_SIZE;
11643 ctl_done((union ctl_io *)ctsio);
11649 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11650 mtx_unlock(&lun->lun_lock);
11651 ctl_done((union ctl_io *)ctsio);
11656 * XXX CHD this is where we want to send IO to other side if
11657 * this LUN is secondary on this SC. We will need to make a copy
11658 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11659 * the copy we send as FROM_OTHER.
11660 * We also need to stuff the address of the original IO so we can
11661 * find it easily. Something similar will need be done on the other
11662 * side so when we are done we can find the copy.
11664 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11665 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11666 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11667 union ctl_ha_msg msg_info;
11670 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11671 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11672 mtx_unlock(&lun->lun_lock);
11674 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11675 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11676 msg_info.hdr.serializing_sc = NULL;
11677 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11678 msg_info.scsi.tag_num = ctsio->tag_num;
11679 msg_info.scsi.tag_type = ctsio->tag_type;
11680 msg_info.scsi.cdb_len = ctsio->cdb_len;
11681 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11683 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11684 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11685 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11686 ctl_set_busy(ctsio);
11687 ctl_done((union ctl_io *)ctsio);
11693 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11694 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11695 ctl_ooaq, ooa_links))) {
11696 case CTL_ACTION_BLOCK:
11697 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11698 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11700 mtx_unlock(&lun->lun_lock);
11702 case CTL_ACTION_PASS:
11703 case CTL_ACTION_SKIP:
11704 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11705 mtx_unlock(&lun->lun_lock);
11706 ctl_enqueue_rtr((union ctl_io *)ctsio);
11708 case CTL_ACTION_OVERLAP:
11709 mtx_unlock(&lun->lun_lock);
11710 ctl_set_overlapped_cmd(ctsio);
11711 ctl_done((union ctl_io *)ctsio);
11713 case CTL_ACTION_OVERLAP_TAG:
11714 mtx_unlock(&lun->lun_lock);
11715 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11716 ctl_done((union ctl_io *)ctsio);
11718 case CTL_ACTION_ERROR:
11720 mtx_unlock(&lun->lun_lock);
11721 ctl_set_internal_failure(ctsio,
11723 /*retry_count*/ 0);
11724 ctl_done((union ctl_io *)ctsio);
11730 const struct ctl_cmd_entry *
11731 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11733 const struct ctl_cmd_entry *entry;
11734 int service_action;
11736 entry = &ctl_cmd_table[ctsio->cdb[0]];
11738 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11739 if (entry->flags & CTL_CMD_FLAG_SA5) {
11740 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11741 entry = &((const struct ctl_cmd_entry *)
11742 entry->execute)[service_action];
11747 const struct ctl_cmd_entry *
11748 ctl_validate_command(struct ctl_scsiio *ctsio)
11750 const struct ctl_cmd_entry *entry;
11754 entry = ctl_get_cmd_entry(ctsio, &sa);
11755 if (entry->execute == NULL) {
11757 ctl_set_invalid_field(ctsio,
11764 ctl_set_invalid_opcode(ctsio);
11765 ctl_done((union ctl_io *)ctsio);
11768 KASSERT(entry->length > 0,
11769 ("Not defined length for command 0x%02x/0x%02x",
11770 ctsio->cdb[0], ctsio->cdb[1]));
11771 for (i = 1; i < entry->length; i++) {
11772 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11775 ctl_set_invalid_field(ctsio,
11780 /*bit*/ fls(diff) - 1);
11781 ctl_done((union ctl_io *)ctsio);
11788 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11791 switch (lun_type) {
11793 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11797 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11801 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11811 ctl_scsiio(struct ctl_scsiio *ctsio)
11814 const struct ctl_cmd_entry *entry;
11816 retval = CTL_RETVAL_COMPLETE;
11818 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11820 entry = ctl_get_cmd_entry(ctsio, NULL);
11823 * If this I/O has been aborted, just send it straight to
11824 * ctl_done() without executing it.
11826 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11827 ctl_done((union ctl_io *)ctsio);
11832 * All the checks should have been handled by ctl_scsiio_precheck().
11833 * We should be clear now to just execute the I/O.
11835 retval = entry->execute(ctsio);
11842 * Since we only implement one target right now, a bus reset simply resets
11843 * our single target.
11846 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11848 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11852 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11853 ctl_ua_type ua_type)
11855 struct ctl_port *port;
11856 struct ctl_lun *lun;
11859 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11860 union ctl_ha_msg msg_info;
11862 msg_info.hdr.nexus = io->io_hdr.nexus;
11863 if (ua_type==CTL_UA_TARG_RESET)
11864 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11866 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11867 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11868 msg_info.hdr.original_sc = NULL;
11869 msg_info.hdr.serializing_sc = NULL;
11870 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11871 sizeof(msg_info.task), M_WAITOK);
11875 mtx_lock(&softc->ctl_lock);
11876 port = ctl_io_port(&io->io_hdr);
11877 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11878 if (port != NULL &&
11879 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11881 retval += ctl_do_lun_reset(lun, io, ua_type);
11883 mtx_unlock(&softc->ctl_lock);
11884 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11889 * The LUN should always be set. The I/O is optional, and is used to
11890 * distinguish between I/Os sent by this initiator, and by other
11891 * initiators. We set unit attention for initiators other than this one.
11892 * SAM-3 is vague on this point. It does say that a unit attention should
11893 * be established for other initiators when a LUN is reset (see section
11894 * 5.7.3), but it doesn't specifically say that the unit attention should
11895 * be established for this particular initiator when a LUN is reset. Here
11896 * is the relevant text, from SAM-3 rev 8:
11898 * 5.7.2 When a SCSI initiator port aborts its own tasks
11900 * When a SCSI initiator port causes its own task(s) to be aborted, no
11901 * notification that the task(s) have been aborted shall be returned to
11902 * the SCSI initiator port other than the completion response for the
11903 * command or task management function action that caused the task(s) to
11904 * be aborted and notification(s) associated with related effects of the
11905 * action (e.g., a reset unit attention condition).
11907 * XXX KDM for now, we're setting unit attention for all initiators.
11910 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11918 mtx_lock(&lun->lun_lock);
11920 * Run through the OOA queue and abort each I/O.
11922 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11923 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11924 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11928 * This version sets unit attention for every
11931 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11932 ctl_est_ua_all(lun, initidx, ua_type);
11934 ctl_est_ua_all(lun, -1, ua_type);
11938 * A reset (any kind, really) clears reservations established with
11939 * RESERVE/RELEASE. It does not clear reservations established
11940 * with PERSISTENT RESERVE OUT, but we don't support that at the
11941 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11942 * reservations made with the RESERVE/RELEASE commands, because
11943 * those commands are obsolete in SPC-3.
11945 lun->flags &= ~CTL_LUN_RESERVED;
11948 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11949 ctl_clear_mask(lun->have_ca, i);
11951 lun->prevent_count = 0;
11952 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11953 ctl_clear_mask(lun->prevent, i);
11954 mtx_unlock(&lun->lun_lock);
11960 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11962 struct ctl_lun *lun;
11966 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11967 mtx_lock(&softc->ctl_lock);
11968 if ((targ_lun >= CTL_MAX_LUNS) ||
11969 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11970 mtx_unlock(&softc->ctl_lock);
11971 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11974 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11975 mtx_unlock(&softc->ctl_lock);
11976 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11978 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11979 union ctl_ha_msg msg_info;
11981 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11982 msg_info.hdr.nexus = io->io_hdr.nexus;
11983 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11984 msg_info.hdr.original_sc = NULL;
11985 msg_info.hdr.serializing_sc = NULL;
11986 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11987 sizeof(msg_info.task), M_WAITOK);
11993 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11998 mtx_assert(&lun->lun_lock, MA_OWNED);
12001 * Run through the OOA queue and attempt to find the given I/O.
12002 * The target port, initiator ID, tag type and tag number have to
12003 * match the values that we got from the initiator. If we have an
12004 * untagged command to abort, simply abort the first untagged command
12005 * we come to. We only allow one untagged command at a time of course.
12007 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12008 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12010 if ((targ_port == UINT32_MAX ||
12011 targ_port == xio->io_hdr.nexus.targ_port) &&
12012 (init_id == UINT32_MAX ||
12013 init_id == xio->io_hdr.nexus.initid)) {
12014 if (targ_port != xio->io_hdr.nexus.targ_port ||
12015 init_id != xio->io_hdr.nexus.initid)
12016 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12017 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12018 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12019 union ctl_ha_msg msg_info;
12021 msg_info.hdr.nexus = xio->io_hdr.nexus;
12022 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12023 msg_info.task.tag_num = xio->scsiio.tag_num;
12024 msg_info.task.tag_type = xio->scsiio.tag_type;
12025 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12026 msg_info.hdr.original_sc = NULL;
12027 msg_info.hdr.serializing_sc = NULL;
12028 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12029 sizeof(msg_info.task), M_NOWAIT);
12036 ctl_abort_task_set(union ctl_io *io)
12038 struct ctl_softc *softc = control_softc;
12039 struct ctl_lun *lun;
12045 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12046 mtx_lock(&softc->ctl_lock);
12047 if ((targ_lun >= CTL_MAX_LUNS) ||
12048 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12049 mtx_unlock(&softc->ctl_lock);
12050 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12054 mtx_lock(&lun->lun_lock);
12055 mtx_unlock(&softc->ctl_lock);
12056 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12057 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12058 io->io_hdr.nexus.initid,
12059 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12060 } else { /* CTL_TASK_CLEAR_TASK_SET */
12061 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12062 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12064 mtx_unlock(&lun->lun_lock);
12065 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12070 ctl_i_t_nexus_reset(union ctl_io *io)
12072 struct ctl_softc *softc = control_softc;
12073 struct ctl_lun *lun;
12076 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12077 union ctl_ha_msg msg_info;
12079 msg_info.hdr.nexus = io->io_hdr.nexus;
12080 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12081 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12082 msg_info.hdr.original_sc = NULL;
12083 msg_info.hdr.serializing_sc = NULL;
12084 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12085 sizeof(msg_info.task), M_WAITOK);
12088 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12089 mtx_lock(&softc->ctl_lock);
12090 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12091 mtx_lock(&lun->lun_lock);
12092 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12093 io->io_hdr.nexus.initid, 1);
12095 ctl_clear_mask(lun->have_ca, initidx);
12097 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12098 lun->flags &= ~CTL_LUN_RESERVED;
12099 if (ctl_is_set(lun->prevent, initidx)) {
12100 ctl_clear_mask(lun->prevent, initidx);
12101 lun->prevent_count--;
12103 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12104 mtx_unlock(&lun->lun_lock);
12106 mtx_unlock(&softc->ctl_lock);
12107 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12112 ctl_abort_task(union ctl_io *io)
12115 struct ctl_lun *lun;
12116 struct ctl_softc *softc;
12119 char printbuf[128];
12124 softc = control_softc;
12130 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12131 mtx_lock(&softc->ctl_lock);
12132 if ((targ_lun >= CTL_MAX_LUNS) ||
12133 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12134 mtx_unlock(&softc->ctl_lock);
12135 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12140 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12141 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12144 mtx_lock(&lun->lun_lock);
12145 mtx_unlock(&softc->ctl_lock);
12147 * Run through the OOA queue and attempt to find the given I/O.
12148 * The target port, initiator ID, tag type and tag number have to
12149 * match the values that we got from the initiator. If we have an
12150 * untagged command to abort, simply abort the first untagged command
12151 * we come to. We only allow one untagged command at a time of course.
12153 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12154 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12156 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12158 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12159 lun->lun, xio->scsiio.tag_num,
12160 xio->scsiio.tag_type,
12161 (xio->io_hdr.blocked_links.tqe_prev
12162 == NULL) ? "" : " BLOCKED",
12163 (xio->io_hdr.flags &
12164 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12165 (xio->io_hdr.flags &
12166 CTL_FLAG_ABORT) ? " ABORT" : "",
12167 (xio->io_hdr.flags &
12168 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12169 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12171 printf("%s\n", sbuf_data(&sb));
12174 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12175 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12176 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12180 * If the abort says that the task is untagged, the
12181 * task in the queue must be untagged. Otherwise,
12182 * we just check to see whether the tag numbers
12183 * match. This is because the QLogic firmware
12184 * doesn't pass back the tag type in an abort
12188 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12189 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12190 || (xio->scsiio.tag_num == io->taskio.tag_num))
12193 * XXX KDM we've got problems with FC, because it
12194 * doesn't send down a tag type with aborts. So we
12195 * can only really go by the tag number...
12196 * This may cause problems with parallel SCSI.
12197 * Need to figure that out!!
12199 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12200 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12202 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12203 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12204 union ctl_ha_msg msg_info;
12206 msg_info.hdr.nexus = io->io_hdr.nexus;
12207 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12208 msg_info.task.tag_num = io->taskio.tag_num;
12209 msg_info.task.tag_type = io->taskio.tag_type;
12210 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12211 msg_info.hdr.original_sc = NULL;
12212 msg_info.hdr.serializing_sc = NULL;
12214 printf("Sent Abort to other side\n");
12216 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12217 sizeof(msg_info.task), M_NOWAIT);
12220 printf("ctl_abort_task: found I/O to abort\n");
12224 mtx_unlock(&lun->lun_lock);
12228 * This isn't really an error. It's entirely possible for
12229 * the abort and command completion to cross on the wire.
12230 * This is more of an informative/diagnostic error.
12233 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12234 "%u:%u:%u tag %d type %d\n",
12235 io->io_hdr.nexus.initid,
12236 io->io_hdr.nexus.targ_port,
12237 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12238 io->taskio.tag_type);
12241 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12246 ctl_query_task(union ctl_io *io, int task_set)
12249 struct ctl_lun *lun;
12250 struct ctl_softc *softc;
12254 softc = control_softc;
12255 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12256 mtx_lock(&softc->ctl_lock);
12257 if ((targ_lun >= CTL_MAX_LUNS) ||
12258 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12259 mtx_unlock(&softc->ctl_lock);
12260 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12263 mtx_lock(&lun->lun_lock);
12264 mtx_unlock(&softc->ctl_lock);
12265 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12266 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12268 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12269 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12270 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12273 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12278 mtx_unlock(&lun->lun_lock);
12280 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12282 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12287 ctl_query_async_event(union ctl_io *io)
12289 struct ctl_lun *lun;
12290 struct ctl_softc *softc;
12292 uint32_t targ_lun, initidx;
12294 softc = control_softc;
12295 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12296 mtx_lock(&softc->ctl_lock);
12297 if ((targ_lun >= CTL_MAX_LUNS) ||
12298 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12299 mtx_unlock(&softc->ctl_lock);
12300 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12303 mtx_lock(&lun->lun_lock);
12304 mtx_unlock(&softc->ctl_lock);
12305 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12306 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12307 mtx_unlock(&lun->lun_lock);
12308 if (ua != CTL_UA_NONE)
12309 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12311 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12316 ctl_run_task(union ctl_io *io)
12318 struct ctl_softc *softc = control_softc;
12321 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12322 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12323 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12324 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12325 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12326 switch (io->taskio.task_action) {
12327 case CTL_TASK_ABORT_TASK:
12328 retval = ctl_abort_task(io);
12330 case CTL_TASK_ABORT_TASK_SET:
12331 case CTL_TASK_CLEAR_TASK_SET:
12332 retval = ctl_abort_task_set(io);
12334 case CTL_TASK_CLEAR_ACA:
12336 case CTL_TASK_I_T_NEXUS_RESET:
12337 retval = ctl_i_t_nexus_reset(io);
12339 case CTL_TASK_LUN_RESET:
12340 retval = ctl_lun_reset(softc, io);
12342 case CTL_TASK_TARGET_RESET:
12343 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12345 case CTL_TASK_BUS_RESET:
12346 retval = ctl_bus_reset(softc, io);
12348 case CTL_TASK_PORT_LOGIN:
12350 case CTL_TASK_PORT_LOGOUT:
12352 case CTL_TASK_QUERY_TASK:
12353 retval = ctl_query_task(io, 0);
12355 case CTL_TASK_QUERY_TASK_SET:
12356 retval = ctl_query_task(io, 1);
12358 case CTL_TASK_QUERY_ASYNC_EVENT:
12359 retval = ctl_query_async_event(io);
12362 printf("%s: got unknown task management event %d\n",
12363 __func__, io->taskio.task_action);
12367 io->io_hdr.status = CTL_SUCCESS;
12369 io->io_hdr.status = CTL_ERROR;
12374 * For HA operation. Handle commands that come in from the other
12378 ctl_handle_isc(union ctl_io *io)
12381 struct ctl_lun *lun;
12382 struct ctl_softc *softc = control_softc;
12385 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12386 lun = softc->ctl_luns[targ_lun];
12388 switch (io->io_hdr.msg_type) {
12389 case CTL_MSG_SERIALIZE:
12390 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12392 case CTL_MSG_R2R: {
12393 const struct ctl_cmd_entry *entry;
12396 * This is only used in SER_ONLY mode.
12399 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12400 mtx_lock(&lun->lun_lock);
12401 if (ctl_scsiio_lun_check(lun,
12402 entry, (struct ctl_scsiio *)io) != 0) {
12403 mtx_unlock(&lun->lun_lock);
12407 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12408 mtx_unlock(&lun->lun_lock);
12409 ctl_enqueue_rtr(io);
12412 case CTL_MSG_FINISH_IO:
12413 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12418 mtx_lock(&lun->lun_lock);
12419 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12421 ctl_check_blocked(lun);
12422 mtx_unlock(&lun->lun_lock);
12425 case CTL_MSG_PERS_ACTION:
12426 ctl_hndl_per_res_out_on_other_sc(
12427 (union ctl_ha_msg *)&io->presio.pr_msg);
12430 case CTL_MSG_BAD_JUJU:
12434 case CTL_MSG_DATAMOVE:
12435 /* Only used in XFER mode */
12437 ctl_datamove_remote(io);
12439 case CTL_MSG_DATAMOVE_DONE:
12440 /* Only used in XFER mode */
12442 io->scsiio.be_move_done(io);
12444 case CTL_MSG_FAILOVER:
12445 ctl_failover_lun(io);
12450 printf("%s: Invalid message type %d\n",
12451 __func__, io->io_hdr.msg_type);
12461 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12462 * there is no match.
12464 static ctl_lun_error_pattern
12465 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12467 const struct ctl_cmd_entry *entry;
12468 ctl_lun_error_pattern filtered_pattern, pattern;
12470 pattern = desc->error_pattern;
12473 * XXX KDM we need more data passed into this function to match a
12474 * custom pattern, and we actually need to implement custom pattern
12477 if (pattern & CTL_LUN_PAT_CMD)
12478 return (CTL_LUN_PAT_CMD);
12480 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12481 return (CTL_LUN_PAT_ANY);
12483 entry = ctl_get_cmd_entry(ctsio, NULL);
12485 filtered_pattern = entry->pattern & pattern;
12488 * If the user requested specific flags in the pattern (e.g.
12489 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12492 * If the user did not specify any flags, it doesn't matter whether
12493 * or not the command supports the flags.
12495 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12496 (pattern & ~CTL_LUN_PAT_MASK))
12497 return (CTL_LUN_PAT_NONE);
12500 * If the user asked for a range check, see if the requested LBA
12501 * range overlaps with this command's LBA range.
12503 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12509 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12511 return (CTL_LUN_PAT_NONE);
12513 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12514 desc->lba_range.len, FALSE);
12516 * A "pass" means that the LBA ranges don't overlap, so
12517 * this doesn't match the user's range criteria.
12519 if (action == CTL_ACTION_PASS)
12520 return (CTL_LUN_PAT_NONE);
12523 return (filtered_pattern);
12527 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12529 struct ctl_error_desc *desc, *desc2;
12531 mtx_assert(&lun->lun_lock, MA_OWNED);
12533 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12534 ctl_lun_error_pattern pattern;
12536 * Check to see whether this particular command matches
12537 * the pattern in the descriptor.
12539 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12540 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12543 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12544 case CTL_LUN_INJ_ABORTED:
12545 ctl_set_aborted(&io->scsiio);
12547 case CTL_LUN_INJ_MEDIUM_ERR:
12548 ctl_set_medium_error(&io->scsiio,
12549 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12550 CTL_FLAG_DATA_OUT);
12552 case CTL_LUN_INJ_UA:
12553 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12555 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12557 case CTL_LUN_INJ_CUSTOM:
12559 * We're assuming the user knows what he is doing.
12560 * Just copy the sense information without doing
12563 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12564 MIN(sizeof(desc->custom_sense),
12565 sizeof(io->scsiio.sense_data)));
12566 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12567 io->scsiio.sense_len = SSD_FULL_SIZE;
12568 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12570 case CTL_LUN_INJ_NONE:
12573 * If this is an error injection type we don't know
12574 * about, clear the continuous flag (if it is set)
12575 * so it will get deleted below.
12577 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12581 * By default, each error injection action is a one-shot
12583 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12586 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12592 #ifdef CTL_IO_DELAY
12594 ctl_datamove_timer_wakeup(void *arg)
12598 io = (union ctl_io *)arg;
12602 #endif /* CTL_IO_DELAY */
12605 ctl_datamove(union ctl_io *io)
12607 struct ctl_lun *lun;
12608 void (*fe_datamove)(union ctl_io *io);
12610 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12612 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12614 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12616 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12621 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12622 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12624 sbuf_cat(&sb, path_str);
12625 switch (io->io_hdr.io_type) {
12627 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12628 sbuf_printf(&sb, "\n");
12629 sbuf_cat(&sb, path_str);
12630 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12631 io->scsiio.tag_num, io->scsiio.tag_type);
12634 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12635 "Tag Type: %d\n", io->taskio.task_action,
12636 io->taskio.tag_num, io->taskio.tag_type);
12639 panic("%s: Invalid CTL I/O type %d\n",
12640 __func__, io->io_hdr.io_type);
12642 sbuf_cat(&sb, path_str);
12643 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12644 (intmax_t)time_uptime - io->io_hdr.start_time);
12646 printf("%s", sbuf_data(&sb));
12648 #endif /* CTL_TIME_IO */
12650 #ifdef CTL_IO_DELAY
12651 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12652 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12655 && (lun->delay_info.datamove_delay > 0)) {
12657 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12658 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12659 callout_reset(&io->io_hdr.delay_callout,
12660 lun->delay_info.datamove_delay * hz,
12661 ctl_datamove_timer_wakeup, io);
12662 if (lun->delay_info.datamove_type ==
12663 CTL_DELAY_TYPE_ONESHOT)
12664 lun->delay_info.datamove_delay = 0;
12671 * This command has been aborted. Set the port status, so we fail
12674 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12675 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12676 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12677 io->io_hdr.nexus.targ_port,
12678 io->io_hdr.nexus.targ_lun);
12679 io->io_hdr.port_status = 31337;
12681 * Note that the backend, in this case, will get the
12682 * callback in its context. In other cases it may get
12683 * called in the frontend's interrupt thread context.
12685 io->scsiio.be_move_done(io);
12689 /* Don't confuse frontend with zero length data move. */
12690 if (io->scsiio.kern_data_len == 0) {
12691 io->scsiio.be_move_done(io);
12695 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12700 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12702 union ctl_ha_msg msg;
12704 struct bintime cur_bt;
12707 memset(&msg, 0, sizeof(msg));
12708 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12709 msg.hdr.original_sc = io;
12710 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12711 msg.hdr.nexus = io->io_hdr.nexus;
12712 msg.hdr.status = io->io_hdr.status;
12713 msg.scsi.tag_num = io->scsiio.tag_num;
12714 msg.scsi.tag_type = io->scsiio.tag_type;
12715 msg.scsi.scsi_status = io->scsiio.scsi_status;
12716 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12717 io->scsiio.sense_len);
12718 msg.scsi.sense_len = io->scsiio.sense_len;
12719 msg.scsi.sense_residual = io->scsiio.sense_residual;
12720 msg.scsi.fetd_status = io->io_hdr.port_status;
12721 msg.scsi.residual = io->scsiio.residual;
12722 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12723 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12724 ctl_failover_io(io, /*have_lock*/ have_lock);
12727 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12728 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12729 msg.scsi.sense_len, M_WAITOK);
12732 getbinuptime(&cur_bt);
12733 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12734 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12736 io->io_hdr.num_dmas++;
12740 * The DMA to the remote side is done, now we need to tell the other side
12741 * we're done so it can continue with its data movement.
12744 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12751 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12752 printf("%s: ISC DMA write failed with error %d", __func__,
12754 ctl_set_internal_failure(&io->scsiio,
12756 /*retry_count*/ rq->ret);
12759 ctl_dt_req_free(rq);
12761 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12762 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12763 free(io->io_hdr.remote_sglist, M_CTL);
12764 io->io_hdr.remote_sglist = NULL;
12765 io->io_hdr.local_sglist = NULL;
12768 * The data is in local and remote memory, so now we need to send
12769 * status (good or back) back to the other side.
12771 ctl_send_datamove_done(io, /*have_lock*/ 0);
12775 * We've moved the data from the host/controller into local memory. Now we
12776 * need to push it over to the remote controller's memory.
12779 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12783 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12784 ctl_datamove_remote_write_cb);
12789 ctl_datamove_remote_write(union ctl_io *io)
12792 void (*fe_datamove)(union ctl_io *io);
12795 * - Get the data from the host/HBA into local memory.
12796 * - DMA memory from the local controller to the remote controller.
12797 * - Send status back to the remote controller.
12800 retval = ctl_datamove_remote_sgl_setup(io);
12804 /* Switch the pointer over so the FETD knows what to do */
12805 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12808 * Use a custom move done callback, since we need to send completion
12809 * back to the other controller, not to the backend on this side.
12811 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12813 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12818 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12827 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12828 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12829 free(io->io_hdr.remote_sglist, M_CTL);
12830 io->io_hdr.remote_sglist = NULL;
12831 io->io_hdr.local_sglist = NULL;
12834 scsi_path_string(io, path_str, sizeof(path_str));
12835 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12836 sbuf_cat(&sb, path_str);
12837 scsi_command_string(&io->scsiio, NULL, &sb);
12838 sbuf_printf(&sb, "\n");
12839 sbuf_cat(&sb, path_str);
12840 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12841 io->scsiio.tag_num, io->scsiio.tag_type);
12842 sbuf_cat(&sb, path_str);
12843 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12844 io->io_hdr.flags, io->io_hdr.status);
12846 printk("%s", sbuf_data(&sb));
12851 * The read is done, now we need to send status (good or bad) back
12852 * to the other side.
12854 ctl_send_datamove_done(io, /*have_lock*/ 0);
12860 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12863 void (*fe_datamove)(union ctl_io *io);
12867 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12868 printf("%s: ISC DMA read failed with error %d\n", __func__,
12870 ctl_set_internal_failure(&io->scsiio,
12872 /*retry_count*/ rq->ret);
12875 ctl_dt_req_free(rq);
12877 /* Switch the pointer over so the FETD knows what to do */
12878 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12881 * Use a custom move done callback, since we need to send completion
12882 * back to the other controller, not to the backend on this side.
12884 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12886 /* XXX KDM add checks like the ones in ctl_datamove? */
12888 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12893 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12895 struct ctl_sg_entry *local_sglist;
12896 uint32_t len_to_go;
12901 local_sglist = io->io_hdr.local_sglist;
12902 len_to_go = io->scsiio.kern_data_len;
12905 * The difficult thing here is that the size of the various
12906 * S/G segments may be different than the size from the
12907 * remote controller. That'll make it harder when DMAing
12908 * the data back to the other side.
12910 for (i = 0; len_to_go > 0; i++) {
12911 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12912 local_sglist[i].addr =
12913 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12915 len_to_go -= local_sglist[i].len;
12918 * Reset the number of S/G entries accordingly. The original
12919 * number of S/G entries is available in rem_sg_entries.
12921 io->scsiio.kern_sg_entries = i;
12924 printf("%s: kern_sg_entries = %d\n", __func__,
12925 io->scsiio.kern_sg_entries);
12926 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12927 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12928 local_sglist[i].addr, local_sglist[i].len);
12935 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12936 ctl_ha_dt_cb callback)
12938 struct ctl_ha_dt_req *rq;
12939 struct ctl_sg_entry *remote_sglist, *local_sglist;
12940 uint32_t local_used, remote_used, total_used;
12943 rq = ctl_dt_req_alloc();
12946 * If we failed to allocate the request, and if the DMA didn't fail
12947 * anyway, set busy status. This is just a resource allocation
12951 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12952 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12953 ctl_set_busy(&io->scsiio);
12955 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12956 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12959 ctl_dt_req_free(rq);
12962 * The data move failed. We need to return status back
12963 * to the other controller. No point in trying to DMA
12964 * data to the remote controller.
12967 ctl_send_datamove_done(io, /*have_lock*/ 0);
12972 local_sglist = io->io_hdr.local_sglist;
12973 remote_sglist = io->io_hdr.remote_sglist;
12979 * Pull/push the data over the wire from/to the other controller.
12980 * This takes into account the possibility that the local and
12981 * remote sglists may not be identical in terms of the size of
12982 * the elements and the number of elements.
12984 * One fundamental assumption here is that the length allocated for
12985 * both the local and remote sglists is identical. Otherwise, we've
12986 * essentially got a coding error of some sort.
12988 isc_ret = CTL_HA_STATUS_SUCCESS;
12989 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12993 rq->command = command;
12997 * Both pointers should be aligned. But it is possible
12998 * that the allocation length is not. They should both
12999 * also have enough slack left over at the end, though,
13000 * to round up to the next 8 byte boundary.
13002 cur_len = MIN(local_sglist[i].len - local_used,
13003 remote_sglist[j].len - remote_used);
13004 rq->size = cur_len;
13006 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13007 tmp_ptr += local_used;
13010 /* Use physical addresses when talking to ISC hardware */
13011 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13012 /* XXX KDM use busdma */
13013 rq->local = vtophys(tmp_ptr);
13015 rq->local = tmp_ptr;
13017 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
13018 ("HA does not support BUS_ADDR"));
13019 rq->local = tmp_ptr;
13022 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13023 tmp_ptr += remote_used;
13024 rq->remote = tmp_ptr;
13026 rq->callback = NULL;
13028 local_used += cur_len;
13029 if (local_used >= local_sglist[i].len) {
13034 remote_used += cur_len;
13035 if (remote_used >= remote_sglist[j].len) {
13039 total_used += cur_len;
13041 if (total_used >= io->scsiio.kern_data_len)
13042 rq->callback = callback;
13045 printf("%s: %s: local %p remote %p size %d\n", __func__,
13046 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13047 rq->local, rq->remote, rq->size);
13050 isc_ret = ctl_dt_single(rq);
13051 if (isc_ret > CTL_HA_STATUS_SUCCESS)
13054 if (isc_ret != CTL_HA_STATUS_WAIT) {
13063 ctl_datamove_remote_read(union ctl_io *io)
13069 * This will send an error to the other controller in the case of a
13072 retval = ctl_datamove_remote_sgl_setup(io);
13076 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13077 ctl_datamove_remote_read_cb);
13080 * Make sure we free memory if there was an error.. The
13081 * ctl_datamove_remote_xfer() function will send the
13082 * datamove done message, or call the callback with an
13083 * error if there is a problem.
13085 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13086 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13087 free(io->io_hdr.remote_sglist, M_CTL);
13088 io->io_hdr.remote_sglist = NULL;
13089 io->io_hdr.local_sglist = NULL;
13094 * Process a datamove request from the other controller. This is used for
13095 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13096 * first. Once that is complete, the data gets DMAed into the remote
13097 * controller's memory. For reads, we DMA from the remote controller's
13098 * memory into our memory first, and then move it out to the FETD.
13101 ctl_datamove_remote(union ctl_io *io)
13104 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
13106 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13107 ctl_failover_io(io, /*have_lock*/ 0);
13112 * Note that we look for an aborted I/O here, but don't do some of
13113 * the other checks that ctl_datamove() normally does.
13114 * We don't need to run the datamove delay code, since that should
13115 * have been done if need be on the other controller.
13117 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13118 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13119 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13120 io->io_hdr.nexus.targ_port,
13121 io->io_hdr.nexus.targ_lun);
13122 io->io_hdr.port_status = 31338;
13123 ctl_send_datamove_done(io, /*have_lock*/ 0);
13127 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13128 ctl_datamove_remote_write(io);
13129 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13130 ctl_datamove_remote_read(io);
13132 io->io_hdr.port_status = 31339;
13133 ctl_send_datamove_done(io, /*have_lock*/ 0);
13138 ctl_process_done(union ctl_io *io)
13140 struct ctl_lun *lun;
13141 struct ctl_softc *softc = control_softc;
13142 void (*fe_done)(union ctl_io *io);
13143 union ctl_ha_msg msg;
13144 uint32_t targ_port = io->io_hdr.nexus.targ_port;
13146 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13147 fe_done = softc->ctl_ports[targ_port]->fe_done;
13150 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13155 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13156 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13158 sbuf_cat(&sb, path_str);
13159 switch (io->io_hdr.io_type) {
13161 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13162 sbuf_printf(&sb, "\n");
13163 sbuf_cat(&sb, path_str);
13164 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13165 io->scsiio.tag_num, io->scsiio.tag_type);
13168 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13169 "Tag Type: %d\n", io->taskio.task_action,
13170 io->taskio.tag_num, io->taskio.tag_type);
13173 panic("%s: Invalid CTL I/O type %d\n",
13174 __func__, io->io_hdr.io_type);
13176 sbuf_cat(&sb, path_str);
13177 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13178 (intmax_t)time_uptime - io->io_hdr.start_time);
13180 printf("%s", sbuf_data(&sb));
13182 #endif /* CTL_TIME_IO */
13184 switch (io->io_hdr.io_type) {
13188 if (ctl_debug & CTL_DEBUG_INFO)
13189 ctl_io_error_print(io, NULL);
13193 panic("%s: Invalid CTL I/O type %d\n",
13194 __func__, io->io_hdr.io_type);
13197 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13199 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13200 io->io_hdr.nexus.targ_mapped_lun));
13204 mtx_lock(&lun->lun_lock);
13207 * Check to see if we have any informational exception and status
13208 * of this command can be modified to report it in form of either
13209 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13211 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13212 io->io_hdr.status == CTL_SUCCESS &&
13213 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13214 uint8_t mrie = lun->MODE_IE.mrie;
13215 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13216 (lun->MODE_VER.byte3 & SMS_VER_PER));
13217 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13218 mrie == SIEP_MRIE_REC_UNCOND ||
13219 mrie == SIEP_MRIE_NO_SENSE) &&
13220 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13221 CTL_CMD_FLAG_NO_SENSE) == 0) {
13222 ctl_set_sense(&io->scsiio,
13223 /*current_error*/ 1,
13224 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13225 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13226 /*asc*/ lun->ie_asc,
13227 /*ascq*/ lun->ie_ascq,
13229 lun->ie_reported = 1;
13231 } else if (lun->ie_reported < 0)
13232 lun->ie_reported = 0;
13235 * Check to see if we have any errors to inject here. We only
13236 * inject errors for commands that don't already have errors set.
13238 if (!STAILQ_EMPTY(&lun->error_list) &&
13239 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13240 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13241 ctl_inject_error(lun, io);
13244 * XXX KDM how do we treat commands that aren't completed
13247 * XXX KDM should we also track I/O latency?
13249 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13250 io->io_hdr.io_type == CTL_IO_SCSI) {
13252 struct bintime cur_bt;
13256 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13258 type = CTL_STATS_READ;
13259 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13261 type = CTL_STATS_WRITE;
13263 type = CTL_STATS_NO_IO;
13265 lun->stats.ports[targ_port].bytes[type] +=
13266 io->scsiio.kern_total_len;
13267 lun->stats.ports[targ_port].operations[type]++;
13269 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13270 &io->io_hdr.dma_bt);
13271 getbinuptime(&cur_bt);
13272 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13273 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13275 lun->stats.ports[targ_port].num_dmas[type] +=
13276 io->io_hdr.num_dmas;
13280 * Remove this from the OOA queue.
13282 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13284 if (TAILQ_EMPTY(&lun->ooa_queue))
13285 lun->last_busy = getsbinuptime();
13289 * Run through the blocked queue on this LUN and see if anything
13290 * has become unblocked, now that this transaction is done.
13292 ctl_check_blocked(lun);
13295 * If the LUN has been invalidated, free it if there is nothing
13296 * left on its OOA queue.
13298 if ((lun->flags & CTL_LUN_INVALID)
13299 && TAILQ_EMPTY(&lun->ooa_queue)) {
13300 mtx_unlock(&lun->lun_lock);
13301 mtx_lock(&softc->ctl_lock);
13303 mtx_unlock(&softc->ctl_lock);
13305 mtx_unlock(&lun->lun_lock);
13310 * If this command has been aborted, make sure we set the status
13311 * properly. The FETD is responsible for freeing the I/O and doing
13312 * whatever it needs to do to clean up its state.
13314 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13315 ctl_set_task_aborted(&io->scsiio);
13318 * If enabled, print command error status.
13320 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13321 (ctl_debug & CTL_DEBUG_INFO) != 0)
13322 ctl_io_error_print(io, NULL);
13325 * Tell the FETD or the other shelf controller we're done with this
13326 * command. Note that only SCSI commands get to this point. Task
13327 * management commands are completed above.
13329 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13330 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13331 memset(&msg, 0, sizeof(msg));
13332 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13333 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13334 msg.hdr.nexus = io->io_hdr.nexus;
13335 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13336 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13345 * Front end should call this if it doesn't do autosense. When the request
13346 * sense comes back in from the initiator, we'll dequeue this and send it.
13349 ctl_queue_sense(union ctl_io *io)
13351 struct ctl_lun *lun;
13352 struct ctl_port *port;
13353 struct ctl_softc *softc;
13354 uint32_t initidx, targ_lun;
13356 softc = control_softc;
13358 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13361 * LUN lookup will likely move to the ctl_work_thread() once we
13362 * have our new queueing infrastructure (that doesn't put things on
13363 * a per-LUN queue initially). That is so that we can handle
13364 * things like an INQUIRY to a LUN that we don't have enabled. We
13365 * can't deal with that right now.
13367 mtx_lock(&softc->ctl_lock);
13370 * If we don't have a LUN for this, just toss the sense
13373 port = ctl_io_port(&ctsio->io_hdr);
13374 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13375 if ((targ_lun < CTL_MAX_LUNS)
13376 && (softc->ctl_luns[targ_lun] != NULL))
13377 lun = softc->ctl_luns[targ_lun];
13381 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13383 mtx_lock(&lun->lun_lock);
13385 * Already have CA set for this LUN...toss the sense information.
13387 if (ctl_is_set(lun->have_ca, initidx)) {
13388 mtx_unlock(&lun->lun_lock);
13392 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13393 MIN(sizeof(lun->pending_sense[initidx]),
13394 sizeof(io->scsiio.sense_data)));
13395 ctl_set_mask(lun->have_ca, initidx);
13396 mtx_unlock(&lun->lun_lock);
13399 mtx_unlock(&softc->ctl_lock);
13403 return (CTL_RETVAL_COMPLETE);
13408 * Primary command inlet from frontend ports. All SCSI and task I/O
13409 * requests must go through this function.
13412 ctl_queue(union ctl_io *io)
13414 struct ctl_port *port;
13416 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13419 io->io_hdr.start_time = time_uptime;
13420 getbinuptime(&io->io_hdr.start_bt);
13421 #endif /* CTL_TIME_IO */
13423 /* Map FE-specific LUN ID into global one. */
13424 port = ctl_io_port(&io->io_hdr);
13425 io->io_hdr.nexus.targ_mapped_lun =
13426 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13428 switch (io->io_hdr.io_type) {
13431 if (ctl_debug & CTL_DEBUG_CDB)
13433 ctl_enqueue_incoming(io);
13436 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13440 return (CTL_RETVAL_COMPLETE);
13443 #ifdef CTL_IO_DELAY
13445 ctl_done_timer_wakeup(void *arg)
13449 io = (union ctl_io *)arg;
13452 #endif /* CTL_IO_DELAY */
13455 ctl_serseq_done(union ctl_io *io)
13457 struct ctl_lun *lun;
13459 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13460 if (lun->be_lun == NULL ||
13461 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13463 mtx_lock(&lun->lun_lock);
13464 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13465 ctl_check_blocked(lun);
13466 mtx_unlock(&lun->lun_lock);
13470 ctl_done(union ctl_io *io)
13474 * Enable this to catch duplicate completion issues.
13477 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13478 printf("%s: type %d msg %d cdb %x iptl: "
13479 "%u:%u:%u tag 0x%04x "
13480 "flag %#x status %x\n",
13482 io->io_hdr.io_type,
13483 io->io_hdr.msg_type,
13485 io->io_hdr.nexus.initid,
13486 io->io_hdr.nexus.targ_port,
13487 io->io_hdr.nexus.targ_lun,
13488 (io->io_hdr.io_type ==
13490 io->taskio.tag_num :
13491 io->scsiio.tag_num,
13493 io->io_hdr.status);
13495 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13499 * This is an internal copy of an I/O, and should not go through
13500 * the normal done processing logic.
13502 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13505 #ifdef CTL_IO_DELAY
13506 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13507 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13509 struct ctl_lun *lun;
13511 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13514 && (lun->delay_info.done_delay > 0)) {
13516 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13517 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13518 callout_reset(&io->io_hdr.delay_callout,
13519 lun->delay_info.done_delay * hz,
13520 ctl_done_timer_wakeup, io);
13521 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13522 lun->delay_info.done_delay = 0;
13526 #endif /* CTL_IO_DELAY */
13528 ctl_enqueue_done(io);
13532 ctl_work_thread(void *arg)
13534 struct ctl_thread *thr = (struct ctl_thread *)arg;
13535 struct ctl_softc *softc = thr->ctl_softc;
13539 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13543 * We handle the queues in this order:
13545 * - done queue (to free up resources, unblock other commands)
13549 * If those queues are empty, we break out of the loop and
13552 mtx_lock(&thr->queue_lock);
13553 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13555 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13556 mtx_unlock(&thr->queue_lock);
13557 ctl_handle_isc(io);
13560 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13562 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13563 /* clear any blocked commands, call fe_done */
13564 mtx_unlock(&thr->queue_lock);
13565 ctl_process_done(io);
13568 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13570 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13571 mtx_unlock(&thr->queue_lock);
13572 if (io->io_hdr.io_type == CTL_IO_TASK)
13575 ctl_scsiio_precheck(softc, &io->scsiio);
13578 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13580 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13581 mtx_unlock(&thr->queue_lock);
13582 retval = ctl_scsiio(&io->scsiio);
13583 if (retval != CTL_RETVAL_COMPLETE)
13584 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13588 /* Sleep until we have something to do. */
13589 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13594 ctl_lun_thread(void *arg)
13596 struct ctl_softc *softc = (struct ctl_softc *)arg;
13597 struct ctl_be_lun *be_lun;
13599 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13602 mtx_lock(&softc->ctl_lock);
13603 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13604 if (be_lun != NULL) {
13605 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13606 mtx_unlock(&softc->ctl_lock);
13607 ctl_create_lun(be_lun);
13611 /* Sleep until we have something to do. */
13612 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13613 PDROP | PRIBIO, "-", 0);
13618 ctl_thresh_thread(void *arg)
13620 struct ctl_softc *softc = (struct ctl_softc *)arg;
13621 struct ctl_lun *lun;
13622 struct ctl_logical_block_provisioning_page *page;
13624 union ctl_ha_msg msg;
13625 uint64_t thres, val;
13628 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13631 mtx_lock(&softc->ctl_lock);
13632 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13633 if ((lun->flags & CTL_LUN_DISABLED) ||
13634 (lun->flags & CTL_LUN_NO_MEDIA) ||
13635 lun->backend->lun_attr == NULL)
13637 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13638 softc->ha_mode == CTL_HA_MODE_XFER)
13640 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13643 page = &lun->MODE_LBP;
13644 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13645 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13647 thres = scsi_4btoul(page->descr[i].count);
13648 thres <<= CTL_LBP_EXPONENT;
13649 switch (page->descr[i].resource) {
13651 attr = "blocksavail";
13654 attr = "blocksused";
13657 attr = "poolblocksavail";
13660 attr = "poolblocksused";
13665 mtx_unlock(&softc->ctl_lock); // XXX
13666 val = lun->backend->lun_attr(
13667 lun->be_lun->be_lun, attr);
13668 mtx_lock(&softc->ctl_lock);
13669 if (val == UINT64_MAX)
13671 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13672 == SLBPPD_ARMING_INC)
13673 e = (val >= thres);
13675 e = (val <= thres);
13679 mtx_lock(&lun->lun_lock);
13681 scsi_u64to8b((uint8_t *)&page->descr[i] -
13682 (uint8_t *)page, lun->ua_tpt_info);
13683 if (lun->lasttpt == 0 ||
13684 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13685 lun->lasttpt = time_uptime;
13686 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13692 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13695 mtx_unlock(&lun->lun_lock);
13697 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13698 /* Send msg to other side. */
13699 bzero(&msg.ua, sizeof(msg.ua));
13700 msg.hdr.msg_type = CTL_MSG_UA;
13701 msg.hdr.nexus.initid = -1;
13702 msg.hdr.nexus.targ_port = -1;
13703 msg.hdr.nexus.targ_lun = lun->lun;
13704 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13706 msg.ua.ua_set = (set > 0);
13707 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13708 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13709 mtx_unlock(&softc->ctl_lock); // XXX
13710 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13711 sizeof(msg.ua), M_WAITOK);
13712 mtx_lock(&softc->ctl_lock);
13715 mtx_unlock(&softc->ctl_lock);
13716 pause("-", CTL_LBP_PERIOD * hz);
13721 ctl_enqueue_incoming(union ctl_io *io)
13723 struct ctl_softc *softc = control_softc;
13724 struct ctl_thread *thr;
13727 idx = (io->io_hdr.nexus.targ_port * 127 +
13728 io->io_hdr.nexus.initid) % worker_threads;
13729 thr = &softc->threads[idx];
13730 mtx_lock(&thr->queue_lock);
13731 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13732 mtx_unlock(&thr->queue_lock);
13737 ctl_enqueue_rtr(union ctl_io *io)
13739 struct ctl_softc *softc = control_softc;
13740 struct ctl_thread *thr;
13742 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13743 mtx_lock(&thr->queue_lock);
13744 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13745 mtx_unlock(&thr->queue_lock);
13750 ctl_enqueue_done(union ctl_io *io)
13752 struct ctl_softc *softc = control_softc;
13753 struct ctl_thread *thr;
13755 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13756 mtx_lock(&thr->queue_lock);
13757 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13758 mtx_unlock(&thr->queue_lock);
13763 ctl_enqueue_isc(union ctl_io *io)
13765 struct ctl_softc *softc = control_softc;
13766 struct ctl_thread *thr;
13768 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13769 mtx_lock(&thr->queue_lock);
13770 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13771 mtx_unlock(&thr->queue_lock);