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 copan_debugconf_subpage debugconf_page_default = {
97 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
98 DBGCNF_SUBPAGE_CODE, /* subpage */
99 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
100 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
101 DBGCNF_VERSION, /* page_version */
102 {CTL_TIME_IO_DEFAULT_SECS>>8,
103 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
106 const static struct copan_debugconf_subpage debugconf_page_changeable = {
107 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
108 DBGCNF_SUBPAGE_CODE, /* subpage */
109 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
110 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
111 0, /* page_version */
112 {0xff,0xff}, /* ctl_time_io_secs */
115 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
116 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
117 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
118 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
119 /*read_retry_count*/0,
120 /*correction_span*/0,
121 /*head_offset_count*/0,
122 /*data_strobe_offset_cnt*/0,
123 /*byte8*/SMS_RWER_LBPERE,
124 /*write_retry_count*/0,
126 /*recovery_time_limit*/{0, 0},
129 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
130 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
131 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
133 /*read_retry_count*/0,
134 /*correction_span*/0,
135 /*head_offset_count*/0,
136 /*data_strobe_offset_cnt*/0,
138 /*write_retry_count*/0,
140 /*recovery_time_limit*/{0, 0},
143 const static struct scsi_format_page format_page_default = {
144 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
145 /*page_length*/sizeof(struct scsi_format_page) - 2,
146 /*tracks_per_zone*/ {0, 0},
147 /*alt_sectors_per_zone*/ {0, 0},
148 /*alt_tracks_per_zone*/ {0, 0},
149 /*alt_tracks_per_lun*/ {0, 0},
150 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
151 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
152 /*bytes_per_sector*/ {0, 0},
153 /*interleave*/ {0, 0},
154 /*track_skew*/ {0, 0},
155 /*cylinder_skew*/ {0, 0},
157 /*reserved*/ {0, 0, 0}
160 const static struct scsi_format_page format_page_changeable = {
161 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
162 /*page_length*/sizeof(struct scsi_format_page) - 2,
163 /*tracks_per_zone*/ {0, 0},
164 /*alt_sectors_per_zone*/ {0, 0},
165 /*alt_tracks_per_zone*/ {0, 0},
166 /*alt_tracks_per_lun*/ {0, 0},
167 /*sectors_per_track*/ {0, 0},
168 /*bytes_per_sector*/ {0, 0},
169 /*interleave*/ {0, 0},
170 /*track_skew*/ {0, 0},
171 /*cylinder_skew*/ {0, 0},
173 /*reserved*/ {0, 0, 0}
176 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
177 /*page_code*/SMS_RIGID_DISK_PAGE,
178 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
179 /*cylinders*/ {0, 0, 0},
180 /*heads*/ CTL_DEFAULT_HEADS,
181 /*start_write_precomp*/ {0, 0, 0},
182 /*start_reduced_current*/ {0, 0, 0},
183 /*step_rate*/ {0, 0},
184 /*landing_zone_cylinder*/ {0, 0, 0},
185 /*rpl*/ SRDP_RPL_DISABLED,
186 /*rotational_offset*/ 0,
188 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
189 CTL_DEFAULT_ROTATION_RATE & 0xff},
193 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
194 /*page_code*/SMS_RIGID_DISK_PAGE,
195 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
196 /*cylinders*/ {0, 0, 0},
198 /*start_write_precomp*/ {0, 0, 0},
199 /*start_reduced_current*/ {0, 0, 0},
200 /*step_rate*/ {0, 0},
201 /*landing_zone_cylinder*/ {0, 0, 0},
203 /*rotational_offset*/ 0,
205 /*rotation_rate*/ {0, 0},
209 const static struct scsi_caching_page caching_page_default = {
210 /*page_code*/SMS_CACHING_PAGE,
211 /*page_length*/sizeof(struct scsi_caching_page) - 2,
212 /*flags1*/ SCP_DISC | SCP_WCE,
214 /*disable_pf_transfer_len*/ {0xff, 0xff},
215 /*min_prefetch*/ {0, 0},
216 /*max_prefetch*/ {0xff, 0xff},
217 /*max_pf_ceiling*/ {0xff, 0xff},
219 /*cache_segments*/ 0,
220 /*cache_seg_size*/ {0, 0},
222 /*non_cache_seg_size*/ {0, 0, 0}
225 const static struct scsi_caching_page caching_page_changeable = {
226 /*page_code*/SMS_CACHING_PAGE,
227 /*page_length*/sizeof(struct scsi_caching_page) - 2,
228 /*flags1*/ SCP_WCE | SCP_RCD,
230 /*disable_pf_transfer_len*/ {0, 0},
231 /*min_prefetch*/ {0, 0},
232 /*max_prefetch*/ {0, 0},
233 /*max_pf_ceiling*/ {0, 0},
235 /*cache_segments*/ 0,
236 /*cache_seg_size*/ {0, 0},
238 /*non_cache_seg_size*/ {0, 0, 0}
241 const static struct scsi_control_page control_page_default = {
242 /*page_code*/SMS_CONTROL_MODE_PAGE,
243 /*page_length*/sizeof(struct scsi_control_page) - 2,
245 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
248 /*aen_holdoff_period*/{0, 0},
249 /*busy_timeout_period*/{0, 0},
250 /*extended_selftest_completion_time*/{0, 0}
253 const static struct scsi_control_page control_page_changeable = {
254 /*page_code*/SMS_CONTROL_MODE_PAGE,
255 /*page_length*/sizeof(struct scsi_control_page) - 2,
257 /*queue_flags*/SCP_QUEUE_ALG_MASK,
258 /*eca_and_aen*/SCP_SWP,
260 /*aen_holdoff_period*/{0, 0},
261 /*busy_timeout_period*/{0, 0},
262 /*extended_selftest_completion_time*/{0, 0}
265 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
267 const static struct scsi_control_ext_page control_ext_page_default = {
268 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
269 /*subpage_code*/0x01,
270 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
276 const static struct scsi_control_ext_page control_ext_page_changeable = {
277 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
278 /*subpage_code*/0x01,
279 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
285 const static struct scsi_info_exceptions_page ie_page_default = {
286 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
287 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
288 /*info_flags*/SIEP_FLAGS_DEXCPT,
290 /*interval_timer*/{0, 0, 0, 0},
291 /*report_count*/{0, 0, 0, 0}
294 const static struct scsi_info_exceptions_page ie_page_changeable = {
295 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
296 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
299 /*interval_timer*/{0, 0, 0, 0},
300 /*report_count*/{0, 0, 0, 0}
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},
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 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1822 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1823 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1826 * In Copan's HA scheme, the "master" and "slave" roles are
1827 * figured out through the slot the controller is in. Although it
1828 * is an active/active system, someone has to be in charge.
1830 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1831 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1832 "HA head ID (0 - no HA)");
1833 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1834 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1835 softc->is_single = 1;
1836 softc->port_cnt = CTL_MAX_PORTS;
1837 softc->port_min = 0;
1839 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1840 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1842 softc->port_max = softc->port_min + softc->port_cnt;
1843 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1844 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1846 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1847 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1848 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1850 STAILQ_INIT(&softc->lun_list);
1851 STAILQ_INIT(&softc->pending_lun_queue);
1852 STAILQ_INIT(&softc->fe_list);
1853 STAILQ_INIT(&softc->port_list);
1854 STAILQ_INIT(&softc->be_list);
1855 ctl_tpc_init(softc);
1857 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1860 printf("ctl: can't allocate %d entry other SC pool, "
1861 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1864 softc->othersc_pool = other_pool;
1866 if (worker_threads <= 0)
1867 worker_threads = max(1, mp_ncpus / 4);
1868 if (worker_threads > CTL_MAX_THREADS)
1869 worker_threads = CTL_MAX_THREADS;
1871 for (i = 0; i < worker_threads; i++) {
1872 struct ctl_thread *thr = &softc->threads[i];
1874 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1875 thr->ctl_softc = softc;
1876 STAILQ_INIT(&thr->incoming_queue);
1877 STAILQ_INIT(&thr->rtr_queue);
1878 STAILQ_INIT(&thr->done_queue);
1879 STAILQ_INIT(&thr->isc_queue);
1881 error = kproc_kthread_add(ctl_work_thread, thr,
1882 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1884 printf("error creating CTL work thread!\n");
1885 ctl_pool_free(other_pool);
1889 error = kproc_kthread_add(ctl_lun_thread, softc,
1890 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1892 printf("error creating CTL lun thread!\n");
1893 ctl_pool_free(other_pool);
1896 error = kproc_kthread_add(ctl_thresh_thread, softc,
1897 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1899 printf("error creating CTL threshold thread!\n");
1900 ctl_pool_free(other_pool);
1904 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1905 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1906 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1908 if (softc->is_single == 0) {
1909 ctl_frontend_register(&ha_frontend);
1910 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1911 printf("ctl_init: ctl_ha_msg_init failed.\n");
1912 softc->is_single = 1;
1914 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1915 != CTL_HA_STATUS_SUCCESS) {
1916 printf("ctl_init: ctl_ha_msg_register failed.\n");
1917 softc->is_single = 1;
1926 struct ctl_softc *softc = control_softc;
1927 struct ctl_lun *lun, *next_lun;
1929 if (softc->is_single == 0) {
1930 ctl_ha_msg_shutdown(softc);
1931 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1932 != CTL_HA_STATUS_SUCCESS)
1933 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1934 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1935 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1936 ctl_frontend_deregister(&ha_frontend);
1939 mtx_lock(&softc->ctl_lock);
1941 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun)
1944 mtx_unlock(&softc->ctl_lock);
1947 ctl_shutdown_thread(softc->work_thread);
1948 mtx_destroy(&softc->queue_lock);
1951 ctl_tpc_shutdown(softc);
1952 uma_zdestroy(softc->io_zone);
1953 mtx_destroy(&softc->ctl_lock);
1955 destroy_dev(softc->dev);
1957 sysctl_ctx_free(&softc->sysctl_ctx);
1959 free(control_softc, M_DEVBUF);
1960 control_softc = NULL;
1964 ctl_module_event_handler(module_t mod, int what, void *arg)
1969 return (ctl_init());
1973 return (EOPNOTSUPP);
1978 * XXX KDM should we do some access checks here? Bump a reference count to
1979 * prevent a CTL module from being unloaded while someone has it open?
1982 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1988 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1994 * Remove an initiator by port number and initiator ID.
1995 * Returns 0 for success, -1 for failure.
1998 ctl_remove_initiator(struct ctl_port *port, int iid)
2000 struct ctl_softc *softc = port->ctl_softc;
2002 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2004 if (iid > CTL_MAX_INIT_PER_PORT) {
2005 printf("%s: initiator ID %u > maximun %u!\n",
2006 __func__, iid, CTL_MAX_INIT_PER_PORT);
2010 mtx_lock(&softc->ctl_lock);
2011 port->wwpn_iid[iid].in_use--;
2012 port->wwpn_iid[iid].last_use = time_uptime;
2013 mtx_unlock(&softc->ctl_lock);
2014 ctl_isc_announce_iid(port, iid);
2020 * Add an initiator to the initiator map.
2021 * Returns iid for success, < 0 for failure.
2024 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2026 struct ctl_softc *softc = port->ctl_softc;
2030 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2032 if (iid >= CTL_MAX_INIT_PER_PORT) {
2033 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2034 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2039 mtx_lock(&softc->ctl_lock);
2041 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2042 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2043 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2047 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2048 strcmp(name, port->wwpn_iid[i].name) == 0) {
2056 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2057 if (port->wwpn_iid[i].in_use == 0 &&
2058 port->wwpn_iid[i].wwpn == 0 &&
2059 port->wwpn_iid[i].name == NULL) {
2068 best_time = INT32_MAX;
2069 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2070 if (port->wwpn_iid[i].in_use == 0) {
2071 if (port->wwpn_iid[i].last_use < best_time) {
2073 best_time = port->wwpn_iid[i].last_use;
2081 mtx_unlock(&softc->ctl_lock);
2086 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2088 * This is not an error yet.
2090 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2092 printf("%s: port %d iid %u WWPN %#jx arrived"
2093 " again\n", __func__, port->targ_port,
2094 iid, (uintmax_t)wwpn);
2098 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2099 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2101 printf("%s: port %d iid %u name '%s' arrived"
2102 " again\n", __func__, port->targ_port,
2109 * This is an error, but what do we do about it? The
2110 * driver is telling us we have a new WWPN for this
2111 * initiator ID, so we pretty much need to use it.
2113 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2114 " but WWPN %#jx '%s' is still at that address\n",
2115 __func__, port->targ_port, iid, wwpn, name,
2116 (uintmax_t)port->wwpn_iid[iid].wwpn,
2117 port->wwpn_iid[iid].name);
2120 * XXX KDM clear have_ca and ua_pending on each LUN for
2125 free(port->wwpn_iid[iid].name, M_CTL);
2126 port->wwpn_iid[iid].name = name;
2127 port->wwpn_iid[iid].wwpn = wwpn;
2128 port->wwpn_iid[iid].in_use++;
2129 mtx_unlock(&softc->ctl_lock);
2130 ctl_isc_announce_iid(port, iid);
2136 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2140 switch (port->port_type) {
2143 struct scsi_transportid_fcp *id =
2144 (struct scsi_transportid_fcp *)buf;
2145 if (port->wwpn_iid[iid].wwpn == 0)
2147 memset(id, 0, sizeof(*id));
2148 id->format_protocol = SCSI_PROTO_FC;
2149 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2150 return (sizeof(*id));
2152 case CTL_PORT_ISCSI:
2154 struct scsi_transportid_iscsi_port *id =
2155 (struct scsi_transportid_iscsi_port *)buf;
2156 if (port->wwpn_iid[iid].name == NULL)
2159 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2161 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2162 len = roundup2(min(len, 252), 4);
2163 scsi_ulto2b(len, id->additional_length);
2164 return (sizeof(*id) + len);
2168 struct scsi_transportid_sas *id =
2169 (struct scsi_transportid_sas *)buf;
2170 if (port->wwpn_iid[iid].wwpn == 0)
2172 memset(id, 0, sizeof(*id));
2173 id->format_protocol = SCSI_PROTO_SAS;
2174 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2175 return (sizeof(*id));
2179 struct scsi_transportid_spi *id =
2180 (struct scsi_transportid_spi *)buf;
2181 memset(id, 0, sizeof(*id));
2182 id->format_protocol = SCSI_PROTO_SPI;
2183 scsi_ulto2b(iid, id->scsi_addr);
2184 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2185 return (sizeof(*id));
2191 * Serialize a command that went down the "wrong" side, and so was sent to
2192 * this controller for execution. The logic is a little different than the
2193 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2194 * sent back to the other side, but in the success case, we execute the
2195 * command on this side (XFER mode) or tell the other side to execute it
2199 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2201 struct ctl_softc *softc = control_softc;
2202 union ctl_ha_msg msg_info;
2203 struct ctl_port *port;
2204 struct ctl_lun *lun;
2205 const struct ctl_cmd_entry *entry;
2209 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2210 mtx_lock(&softc->ctl_lock);
2212 /* Make sure that we know about this port. */
2213 port = ctl_io_port(&ctsio->io_hdr);
2214 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2215 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2220 /* Make sure that we know about this LUN. */
2221 if ((targ_lun < CTL_MAX_LUNS) &&
2222 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
2223 mtx_lock(&lun->lun_lock);
2224 mtx_unlock(&softc->ctl_lock);
2226 * If the LUN is invalid, pretend that it doesn't exist.
2227 * It will go away as soon as all pending I/O has been
2230 if (lun->flags & CTL_LUN_DISABLED) {
2231 mtx_unlock(&lun->lun_lock);
2235 mtx_unlock(&softc->ctl_lock);
2240 * The other node would not send this request to us unless
2241 * received announce that we are primary node for this LUN.
2242 * If this LUN does not exist now, it is probably result of
2243 * a race, so respond to initiator in the most opaque way.
2245 ctl_set_busy(ctsio);
2249 entry = ctl_get_cmd_entry(ctsio, NULL);
2250 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2251 mtx_unlock(&lun->lun_lock);
2255 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
2256 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
2259 * Every I/O goes into the OOA queue for a
2260 * particular LUN, and stays there until completion.
2263 if (TAILQ_EMPTY(&lun->ooa_queue))
2264 lun->idle_time += getsbinuptime() - lun->last_busy;
2266 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2268 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2269 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2271 case CTL_ACTION_BLOCK:
2272 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2273 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2275 mtx_unlock(&lun->lun_lock);
2277 case CTL_ACTION_PASS:
2278 case CTL_ACTION_SKIP:
2279 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2280 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2281 ctl_enqueue_rtr((union ctl_io *)ctsio);
2282 mtx_unlock(&lun->lun_lock);
2284 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2285 mtx_unlock(&lun->lun_lock);
2287 /* send msg back to other side */
2288 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2289 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2290 msg_info.hdr.msg_type = CTL_MSG_R2R;
2291 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2292 sizeof(msg_info.hdr), M_WAITOK);
2295 case CTL_ACTION_OVERLAP:
2296 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2297 mtx_unlock(&lun->lun_lock);
2298 ctl_set_overlapped_cmd(ctsio);
2300 case CTL_ACTION_OVERLAP_TAG:
2301 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2302 mtx_unlock(&lun->lun_lock);
2303 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2305 case CTL_ACTION_ERROR:
2307 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2308 mtx_unlock(&lun->lun_lock);
2310 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2313 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2314 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2315 msg_info.hdr.serializing_sc = NULL;
2316 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2317 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2318 sizeof(msg_info.scsi), M_WAITOK);
2326 * Returns 0 for success, errno for failure.
2329 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2330 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2334 mtx_lock(&lun->lun_lock);
2335 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2336 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2338 struct ctl_ooa_entry *entry;
2341 * If we've got more than we can fit, just count the
2342 * remaining entries.
2344 if (*cur_fill_num >= ooa_hdr->alloc_num)
2347 entry = &kern_entries[*cur_fill_num];
2349 entry->tag_num = io->scsiio.tag_num;
2350 entry->lun_num = lun->lun;
2352 entry->start_bt = io->io_hdr.start_bt;
2354 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2355 entry->cdb_len = io->scsiio.cdb_len;
2356 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2357 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2359 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2360 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2362 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2363 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2365 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2366 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2368 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2369 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2371 mtx_unlock(&lun->lun_lock);
2375 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2376 size_t error_str_len)
2380 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2382 if (copyin(user_addr, kptr, len) != 0) {
2383 snprintf(error_str, error_str_len, "Error copying %d bytes "
2384 "from user address %p to kernel address %p", len,
2394 ctl_free_args(int num_args, struct ctl_be_arg *args)
2401 for (i = 0; i < num_args; i++) {
2402 free(args[i].kname, M_CTL);
2403 free(args[i].kvalue, M_CTL);
2409 static struct ctl_be_arg *
2410 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2411 char *error_str, size_t error_str_len)
2413 struct ctl_be_arg *args;
2416 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2417 error_str, error_str_len);
2422 for (i = 0; i < num_args; i++) {
2423 args[i].kname = NULL;
2424 args[i].kvalue = NULL;
2427 for (i = 0; i < num_args; i++) {
2430 args[i].kname = ctl_copyin_alloc(args[i].name,
2431 args[i].namelen, error_str, error_str_len);
2432 if (args[i].kname == NULL)
2435 if (args[i].kname[args[i].namelen - 1] != '\0') {
2436 snprintf(error_str, error_str_len, "Argument %d "
2437 "name is not NUL-terminated", i);
2441 if (args[i].flags & CTL_BEARG_RD) {
2442 tmpptr = ctl_copyin_alloc(args[i].value,
2443 args[i].vallen, error_str, error_str_len);
2446 if ((args[i].flags & CTL_BEARG_ASCII)
2447 && (tmpptr[args[i].vallen - 1] != '\0')) {
2448 snprintf(error_str, error_str_len, "Argument "
2449 "%d value is not NUL-terminated", i);
2450 free(tmpptr, M_CTL);
2453 args[i].kvalue = tmpptr;
2455 args[i].kvalue = malloc(args[i].vallen,
2456 M_CTL, M_WAITOK | M_ZERO);
2463 ctl_free_args(num_args, args);
2469 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2473 for (i = 0; i < num_args; i++) {
2474 if (args[i].flags & CTL_BEARG_WR)
2475 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2480 * Escape characters that are illegal or not recommended in XML.
2483 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2485 char *end = str + size;
2490 for (; *str && str < end; str++) {
2493 retval = sbuf_printf(sb, "&");
2496 retval = sbuf_printf(sb, ">");
2499 retval = sbuf_printf(sb, "<");
2502 retval = sbuf_putc(sb, *str);
2515 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2517 struct scsi_vpd_id_descriptor *desc;
2520 if (id == NULL || id->len < 4)
2522 desc = (struct scsi_vpd_id_descriptor *)id->data;
2523 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2524 case SVPD_ID_TYPE_T10:
2525 sbuf_printf(sb, "t10.");
2527 case SVPD_ID_TYPE_EUI64:
2528 sbuf_printf(sb, "eui.");
2530 case SVPD_ID_TYPE_NAA:
2531 sbuf_printf(sb, "naa.");
2533 case SVPD_ID_TYPE_SCSI_NAME:
2536 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2537 case SVPD_ID_CODESET_BINARY:
2538 for (i = 0; i < desc->length; i++)
2539 sbuf_printf(sb, "%02x", desc->identifier[i]);
2541 case SVPD_ID_CODESET_ASCII:
2542 sbuf_printf(sb, "%.*s", (int)desc->length,
2543 (char *)desc->identifier);
2545 case SVPD_ID_CODESET_UTF8:
2546 sbuf_printf(sb, "%s", (char *)desc->identifier);
2552 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2555 struct ctl_softc *softc = dev->si_drv1;
2556 struct ctl_lun *lun;
2563 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2565 case CTL_ENABLE_PORT:
2566 case CTL_DISABLE_PORT:
2567 case CTL_SET_PORT_WWNS: {
2568 struct ctl_port *port;
2569 struct ctl_port_entry *entry;
2571 entry = (struct ctl_port_entry *)addr;
2573 mtx_lock(&softc->ctl_lock);
2574 STAILQ_FOREACH(port, &softc->port_list, links) {
2577 if (port->targ_port < softc->port_min ||
2578 port->targ_port >= softc->port_max)
2583 if ((entry->port_type == CTL_PORT_NONE)
2584 && (entry->targ_port == port->targ_port)) {
2586 * If the user only wants to enable or
2587 * disable or set WWNs on a specific port,
2588 * do the operation and we're done.
2592 } else if (entry->port_type & port->port_type) {
2594 * Compare the user's type mask with the
2595 * particular frontend type to see if we
2602 * Make sure the user isn't trying to set
2603 * WWNs on multiple ports at the same time.
2605 if (cmd == CTL_SET_PORT_WWNS) {
2606 printf("%s: Can't set WWNs on "
2607 "multiple ports\n", __func__);
2616 * XXX KDM we have to drop the lock here, because
2617 * the online/offline operations can potentially
2618 * block. We need to reference count the frontends
2619 * so they can't go away,
2621 if (cmd == CTL_ENABLE_PORT) {
2622 mtx_unlock(&softc->ctl_lock);
2623 ctl_port_online(port);
2624 mtx_lock(&softc->ctl_lock);
2625 } else if (cmd == CTL_DISABLE_PORT) {
2626 mtx_unlock(&softc->ctl_lock);
2627 ctl_port_offline(port);
2628 mtx_lock(&softc->ctl_lock);
2629 } else if (cmd == CTL_SET_PORT_WWNS) {
2630 ctl_port_set_wwns(port,
2631 (entry->flags & CTL_PORT_WWNN_VALID) ?
2633 (entry->flags & CTL_PORT_WWPN_VALID) ?
2634 1 : 0, entry->wwpn);
2639 mtx_unlock(&softc->ctl_lock);
2643 struct ctl_ooa *ooa_hdr;
2644 struct ctl_ooa_entry *entries;
2645 uint32_t cur_fill_num;
2647 ooa_hdr = (struct ctl_ooa *)addr;
2649 if ((ooa_hdr->alloc_len == 0)
2650 || (ooa_hdr->alloc_num == 0)) {
2651 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2652 "must be non-zero\n", __func__,
2653 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2658 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2659 sizeof(struct ctl_ooa_entry))) {
2660 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2661 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2662 __func__, ooa_hdr->alloc_len,
2663 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2668 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2669 if (entries == NULL) {
2670 printf("%s: could not allocate %d bytes for OOA "
2671 "dump\n", __func__, ooa_hdr->alloc_len);
2676 mtx_lock(&softc->ctl_lock);
2677 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2678 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2679 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2680 mtx_unlock(&softc->ctl_lock);
2681 free(entries, M_CTL);
2682 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2683 __func__, (uintmax_t)ooa_hdr->lun_num);
2690 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2691 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2692 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2696 lun = softc->ctl_luns[ooa_hdr->lun_num];
2697 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2700 mtx_unlock(&softc->ctl_lock);
2702 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2703 ooa_hdr->fill_len = ooa_hdr->fill_num *
2704 sizeof(struct ctl_ooa_entry);
2705 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2707 printf("%s: error copying out %d bytes for OOA dump\n",
2708 __func__, ooa_hdr->fill_len);
2711 getbinuptime(&ooa_hdr->cur_bt);
2713 if (cur_fill_num > ooa_hdr->alloc_num) {
2714 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2715 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2717 ooa_hdr->dropped_num = 0;
2718 ooa_hdr->status = CTL_OOA_OK;
2721 free(entries, M_CTL);
2724 case CTL_DELAY_IO: {
2725 struct ctl_io_delay_info *delay_info;
2727 delay_info = (struct ctl_io_delay_info *)addr;
2730 mtx_lock(&softc->ctl_lock);
2732 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2733 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2734 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2736 lun = softc->ctl_luns[delay_info->lun_id];
2737 mtx_lock(&lun->lun_lock);
2739 delay_info->status = CTL_DELAY_STATUS_OK;
2741 switch (delay_info->delay_type) {
2742 case CTL_DELAY_TYPE_CONT:
2744 case CTL_DELAY_TYPE_ONESHOT:
2747 delay_info->status =
2748 CTL_DELAY_STATUS_INVALID_TYPE;
2752 switch (delay_info->delay_loc) {
2753 case CTL_DELAY_LOC_DATAMOVE:
2754 lun->delay_info.datamove_type =
2755 delay_info->delay_type;
2756 lun->delay_info.datamove_delay =
2757 delay_info->delay_secs;
2759 case CTL_DELAY_LOC_DONE:
2760 lun->delay_info.done_type =
2761 delay_info->delay_type;
2762 lun->delay_info.done_delay =
2763 delay_info->delay_secs;
2766 delay_info->status =
2767 CTL_DELAY_STATUS_INVALID_LOC;
2770 mtx_unlock(&lun->lun_lock);
2773 mtx_unlock(&softc->ctl_lock);
2775 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2776 #endif /* CTL_IO_DELAY */
2779 case CTL_GETSTATS: {
2780 struct ctl_stats *stats;
2783 stats = (struct ctl_stats *)addr;
2785 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2787 stats->status = CTL_SS_NEED_MORE_SPACE;
2788 stats->num_luns = softc->num_luns;
2792 * XXX KDM no locking here. If the LUN list changes,
2793 * things can blow up.
2796 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2797 retval = copyout(&lun->stats, &stats->lun_stats[i++],
2798 sizeof(lun->stats));
2802 stats->num_luns = softc->num_luns;
2803 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2805 stats->status = CTL_SS_OK;
2807 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2809 stats->flags = CTL_STATS_FLAG_NONE;
2811 getnanouptime(&stats->timestamp);
2814 case CTL_ERROR_INJECT: {
2815 struct ctl_error_desc *err_desc, *new_err_desc;
2817 err_desc = (struct ctl_error_desc *)addr;
2819 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2821 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2823 mtx_lock(&softc->ctl_lock);
2824 lun = softc->ctl_luns[err_desc->lun_id];
2826 mtx_unlock(&softc->ctl_lock);
2827 free(new_err_desc, M_CTL);
2828 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2829 __func__, (uintmax_t)err_desc->lun_id);
2833 mtx_lock(&lun->lun_lock);
2834 mtx_unlock(&softc->ctl_lock);
2837 * We could do some checking here to verify the validity
2838 * of the request, but given the complexity of error
2839 * injection requests, the checking logic would be fairly
2842 * For now, if the request is invalid, it just won't get
2843 * executed and might get deleted.
2845 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2848 * XXX KDM check to make sure the serial number is unique,
2849 * in case we somehow manage to wrap. That shouldn't
2850 * happen for a very long time, but it's the right thing to
2853 new_err_desc->serial = lun->error_serial;
2854 err_desc->serial = lun->error_serial;
2855 lun->error_serial++;
2857 mtx_unlock(&lun->lun_lock);
2860 case CTL_ERROR_INJECT_DELETE: {
2861 struct ctl_error_desc *delete_desc, *desc, *desc2;
2864 delete_desc = (struct ctl_error_desc *)addr;
2867 mtx_lock(&softc->ctl_lock);
2868 lun = softc->ctl_luns[delete_desc->lun_id];
2870 mtx_unlock(&softc->ctl_lock);
2871 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2872 __func__, (uintmax_t)delete_desc->lun_id);
2876 mtx_lock(&lun->lun_lock);
2877 mtx_unlock(&softc->ctl_lock);
2878 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2879 if (desc->serial != delete_desc->serial)
2882 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2887 mtx_unlock(&lun->lun_lock);
2888 if (delete_done == 0) {
2889 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2890 "error serial %ju on LUN %u\n", __func__,
2891 delete_desc->serial, delete_desc->lun_id);
2897 case CTL_DUMP_STRUCTS: {
2899 struct ctl_port *port;
2900 struct ctl_frontend *fe;
2902 mtx_lock(&softc->ctl_lock);
2903 printf("CTL Persistent Reservation information start:\n");
2904 for (i = 0; i < CTL_MAX_LUNS; i++) {
2905 lun = softc->ctl_luns[i];
2908 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2911 for (j = 0; j < CTL_MAX_PORTS; j++) {
2912 if (lun->pr_keys[j] == NULL)
2914 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2915 if (lun->pr_keys[j][k] == 0)
2917 printf(" LUN %d port %d iid %d key "
2919 (uintmax_t)lun->pr_keys[j][k]);
2923 printf("CTL Persistent Reservation information end\n");
2924 printf("CTL Ports:\n");
2925 STAILQ_FOREACH(port, &softc->port_list, links) {
2926 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2927 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2928 port->frontend->name, port->port_type,
2929 port->physical_port, port->virtual_port,
2930 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2931 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2932 if (port->wwpn_iid[j].in_use == 0 &&
2933 port->wwpn_iid[j].wwpn == 0 &&
2934 port->wwpn_iid[j].name == NULL)
2937 printf(" iid %u use %d WWPN %#jx '%s'\n",
2938 j, port->wwpn_iid[j].in_use,
2939 (uintmax_t)port->wwpn_iid[j].wwpn,
2940 port->wwpn_iid[j].name);
2943 printf("CTL Port information end\n");
2944 mtx_unlock(&softc->ctl_lock);
2946 * XXX KDM calling this without a lock. We'd likely want
2947 * to drop the lock before calling the frontend's dump
2950 printf("CTL Frontends:\n");
2951 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2952 printf(" Frontend '%s'\n", fe->name);
2953 if (fe->fe_dump != NULL)
2956 printf("CTL Frontend information end\n");
2960 struct ctl_lun_req *lun_req;
2961 struct ctl_backend_driver *backend;
2963 lun_req = (struct ctl_lun_req *)addr;
2965 backend = ctl_backend_find(lun_req->backend);
2966 if (backend == NULL) {
2967 lun_req->status = CTL_LUN_ERROR;
2968 snprintf(lun_req->error_str,
2969 sizeof(lun_req->error_str),
2970 "Backend \"%s\" not found.",
2974 if (lun_req->num_be_args > 0) {
2975 lun_req->kern_be_args = ctl_copyin_args(
2976 lun_req->num_be_args,
2979 sizeof(lun_req->error_str));
2980 if (lun_req->kern_be_args == NULL) {
2981 lun_req->status = CTL_LUN_ERROR;
2986 retval = backend->ioctl(dev, cmd, addr, flag, td);
2988 if (lun_req->num_be_args > 0) {
2989 ctl_copyout_args(lun_req->num_be_args,
2990 lun_req->kern_be_args);
2991 ctl_free_args(lun_req->num_be_args,
2992 lun_req->kern_be_args);
2996 case CTL_LUN_LIST: {
2998 struct ctl_lun_list *list;
2999 struct ctl_option *opt;
3001 list = (struct ctl_lun_list *)addr;
3004 * Allocate a fixed length sbuf here, based on the length
3005 * of the user's buffer. We could allocate an auto-extending
3006 * buffer, and then tell the user how much larger our
3007 * amount of data is than his buffer, but that presents
3010 * 1. The sbuf(9) routines use a blocking malloc, and so
3011 * we can't hold a lock while calling them with an
3012 * auto-extending buffer.
3014 * 2. There is not currently a LUN reference counting
3015 * mechanism, outside of outstanding transactions on
3016 * the LUN's OOA queue. So a LUN could go away on us
3017 * while we're getting the LUN number, backend-specific
3018 * information, etc. Thus, given the way things
3019 * currently work, we need to hold the CTL lock while
3020 * grabbing LUN information.
3022 * So, from the user's standpoint, the best thing to do is
3023 * allocate what he thinks is a reasonable buffer length,
3024 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3025 * double the buffer length and try again. (And repeat
3026 * that until he succeeds.)
3028 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3030 list->status = CTL_LUN_LIST_ERROR;
3031 snprintf(list->error_str, sizeof(list->error_str),
3032 "Unable to allocate %d bytes for LUN list",
3037 sbuf_printf(sb, "<ctllunlist>\n");
3039 mtx_lock(&softc->ctl_lock);
3040 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3041 mtx_lock(&lun->lun_lock);
3042 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3043 (uintmax_t)lun->lun);
3046 * Bail out as soon as we see that we've overfilled
3052 retval = sbuf_printf(sb, "\t<backend_type>%s"
3053 "</backend_type>\n",
3054 (lun->backend == NULL) ? "none" :
3055 lun->backend->name);
3060 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3061 lun->be_lun->lun_type);
3066 if (lun->backend == NULL) {
3067 retval = sbuf_printf(sb, "</lun>\n");
3073 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3074 (lun->be_lun->maxlba > 0) ?
3075 lun->be_lun->maxlba + 1 : 0);
3080 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3081 lun->be_lun->blocksize);
3086 retval = sbuf_printf(sb, "\t<serial_number>");
3091 retval = ctl_sbuf_printf_esc(sb,
3092 lun->be_lun->serial_num,
3093 sizeof(lun->be_lun->serial_num));
3098 retval = sbuf_printf(sb, "</serial_number>\n");
3103 retval = sbuf_printf(sb, "\t<device_id>");
3108 retval = ctl_sbuf_printf_esc(sb,
3109 lun->be_lun->device_id,
3110 sizeof(lun->be_lun->device_id));
3115 retval = sbuf_printf(sb, "</device_id>\n");
3120 if (lun->backend->lun_info != NULL) {
3121 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3125 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3126 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3127 opt->name, opt->value, opt->name);
3132 retval = sbuf_printf(sb, "</lun>\n");
3136 mtx_unlock(&lun->lun_lock);
3139 mtx_unlock(&lun->lun_lock);
3140 mtx_unlock(&softc->ctl_lock);
3143 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3146 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3147 snprintf(list->error_str, sizeof(list->error_str),
3148 "Out of space, %d bytes is too small",
3155 retval = copyout(sbuf_data(sb), list->lun_xml,
3158 list->fill_len = sbuf_len(sb) + 1;
3159 list->status = CTL_LUN_LIST_OK;
3164 struct ctl_iscsi *ci;
3165 struct ctl_frontend *fe;
3167 ci = (struct ctl_iscsi *)addr;
3169 fe = ctl_frontend_find("iscsi");
3171 ci->status = CTL_ISCSI_ERROR;
3172 snprintf(ci->error_str, sizeof(ci->error_str),
3173 "Frontend \"iscsi\" not found.");
3177 retval = fe->ioctl(dev, cmd, addr, flag, td);
3180 case CTL_PORT_REQ: {
3181 struct ctl_req *req;
3182 struct ctl_frontend *fe;
3184 req = (struct ctl_req *)addr;
3186 fe = ctl_frontend_find(req->driver);
3188 req->status = CTL_LUN_ERROR;
3189 snprintf(req->error_str, sizeof(req->error_str),
3190 "Frontend \"%s\" not found.", req->driver);
3193 if (req->num_args > 0) {
3194 req->kern_args = ctl_copyin_args(req->num_args,
3195 req->args, req->error_str, sizeof(req->error_str));
3196 if (req->kern_args == NULL) {
3197 req->status = CTL_LUN_ERROR;
3203 retval = fe->ioctl(dev, cmd, addr, flag, td);
3207 if (req->num_args > 0) {
3208 ctl_copyout_args(req->num_args, req->kern_args);
3209 ctl_free_args(req->num_args, req->kern_args);
3213 case CTL_PORT_LIST: {
3215 struct ctl_port *port;
3216 struct ctl_lun_list *list;
3217 struct ctl_option *opt;
3221 list = (struct ctl_lun_list *)addr;
3223 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3225 list->status = CTL_LUN_LIST_ERROR;
3226 snprintf(list->error_str, sizeof(list->error_str),
3227 "Unable to allocate %d bytes for LUN list",
3232 sbuf_printf(sb, "<ctlportlist>\n");
3234 mtx_lock(&softc->ctl_lock);
3235 STAILQ_FOREACH(port, &softc->port_list, links) {
3236 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3237 (uintmax_t)port->targ_port);
3240 * Bail out as soon as we see that we've overfilled
3246 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3247 "</frontend_type>\n", port->frontend->name);
3251 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3256 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3257 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3261 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3266 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3267 port->physical_port);
3271 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3272 port->virtual_port);
3276 if (port->target_devid != NULL) {
3277 sbuf_printf(sb, "\t<target>");
3278 ctl_id_sbuf(port->target_devid, sb);
3279 sbuf_printf(sb, "</target>\n");
3282 if (port->port_devid != NULL) {
3283 sbuf_printf(sb, "\t<port>");
3284 ctl_id_sbuf(port->port_devid, sb);
3285 sbuf_printf(sb, "</port>\n");
3288 if (port->port_info != NULL) {
3289 retval = port->port_info(port->onoff_arg, sb);
3293 STAILQ_FOREACH(opt, &port->options, links) {
3294 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3295 opt->name, opt->value, opt->name);
3300 if (port->lun_map != NULL) {
3301 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3302 for (j = 0; j < CTL_MAX_LUNS; j++) {
3303 plun = ctl_lun_map_from_port(port, j);
3304 if (plun >= CTL_MAX_LUNS)
3307 "\t<lun id=\"%u\">%u</lun>\n",
3312 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3313 if (port->wwpn_iid[j].in_use == 0 ||
3314 (port->wwpn_iid[j].wwpn == 0 &&
3315 port->wwpn_iid[j].name == NULL))
3318 if (port->wwpn_iid[j].name != NULL)
3319 retval = sbuf_printf(sb,
3320 "\t<initiator id=\"%u\">%s</initiator>\n",
3321 j, port->wwpn_iid[j].name);
3323 retval = sbuf_printf(sb,
3324 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3325 j, port->wwpn_iid[j].wwpn);
3332 retval = sbuf_printf(sb, "</targ_port>\n");
3336 mtx_unlock(&softc->ctl_lock);
3339 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3342 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3343 snprintf(list->error_str, sizeof(list->error_str),
3344 "Out of space, %d bytes is too small",
3351 retval = copyout(sbuf_data(sb), list->lun_xml,
3354 list->fill_len = sbuf_len(sb) + 1;
3355 list->status = CTL_LUN_LIST_OK;
3360 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3361 struct ctl_port *port;
3363 mtx_lock(&softc->ctl_lock);
3364 if (lm->port < softc->port_min ||
3365 lm->port >= softc->port_max ||
3366 (port = softc->ctl_ports[lm->port]) == NULL) {
3367 mtx_unlock(&softc->ctl_lock);
3370 if (port->status & CTL_PORT_STATUS_ONLINE) {
3371 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3372 if (ctl_lun_map_to_port(port, lun->lun) >=
3375 mtx_lock(&lun->lun_lock);
3376 ctl_est_ua_port(lun, lm->port, -1,
3378 mtx_unlock(&lun->lun_lock);
3381 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3382 if (lm->plun < CTL_MAX_LUNS) {
3383 if (lm->lun == UINT32_MAX)
3384 retval = ctl_lun_map_unset(port, lm->plun);
3385 else if (lm->lun < CTL_MAX_LUNS &&
3386 softc->ctl_luns[lm->lun] != NULL)
3387 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3390 } else if (lm->plun == UINT32_MAX) {
3391 if (lm->lun == UINT32_MAX)
3392 retval = ctl_lun_map_deinit(port);
3394 retval = ctl_lun_map_init(port);
3397 if (port->status & CTL_PORT_STATUS_ONLINE)
3398 ctl_isc_announce_port(port);
3402 /* XXX KDM should we fix this? */
3404 struct ctl_backend_driver *backend;
3411 * We encode the backend type as the ioctl type for backend
3412 * ioctls. So parse it out here, and then search for a
3413 * backend of this type.
3415 type = _IOC_TYPE(cmd);
3417 STAILQ_FOREACH(backend, &softc->be_list, links) {
3418 if (backend->type == type) {
3424 printf("ctl: unknown ioctl command %#lx or backend "
3429 retval = backend->ioctl(dev, cmd, addr, flag, td);
3439 ctl_get_initindex(struct ctl_nexus *nexus)
3441 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3445 ctl_lun_map_init(struct ctl_port *port)
3447 struct ctl_softc *softc = port->ctl_softc;
3448 struct ctl_lun *lun;
3451 if (port->lun_map == NULL)
3452 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3454 if (port->lun_map == NULL)
3456 for (i = 0; i < CTL_MAX_LUNS; i++)
3457 port->lun_map[i] = UINT32_MAX;
3458 if (port->status & CTL_PORT_STATUS_ONLINE) {
3459 if (port->lun_disable != NULL) {
3460 STAILQ_FOREACH(lun, &softc->lun_list, links)
3461 port->lun_disable(port->targ_lun_arg, lun->lun);
3463 ctl_isc_announce_port(port);
3469 ctl_lun_map_deinit(struct ctl_port *port)
3471 struct ctl_softc *softc = port->ctl_softc;
3472 struct ctl_lun *lun;
3474 if (port->lun_map == NULL)
3476 free(port->lun_map, M_CTL);
3477 port->lun_map = NULL;
3478 if (port->status & CTL_PORT_STATUS_ONLINE) {
3479 if (port->lun_enable != NULL) {
3480 STAILQ_FOREACH(lun, &softc->lun_list, links)
3481 port->lun_enable(port->targ_lun_arg, lun->lun);
3483 ctl_isc_announce_port(port);
3489 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3494 if (port->lun_map == NULL) {
3495 status = ctl_lun_map_init(port);
3499 old = port->lun_map[plun];
3500 port->lun_map[plun] = glun;
3501 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3502 if (port->lun_enable != NULL)
3503 port->lun_enable(port->targ_lun_arg, plun);
3504 ctl_isc_announce_port(port);
3510 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3514 if (port->lun_map == NULL)
3516 old = port->lun_map[plun];
3517 port->lun_map[plun] = UINT32_MAX;
3518 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3519 if (port->lun_disable != NULL)
3520 port->lun_disable(port->targ_lun_arg, plun);
3521 ctl_isc_announce_port(port);
3527 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3531 return (UINT32_MAX);
3532 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3534 return (port->lun_map[lun_id]);
3538 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3543 return (UINT32_MAX);
3544 if (port->lun_map == NULL)
3546 for (i = 0; i < CTL_MAX_LUNS; i++) {
3547 if (port->lun_map[i] == lun_id)
3550 return (UINT32_MAX);
3554 ctl_decode_lun(uint64_t encoded)
3557 uint32_t result = 0xffffffff;
3559 be64enc(lun, encoded);
3560 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3561 case RPL_LUNDATA_ATYP_PERIPH:
3562 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3563 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3566 case RPL_LUNDATA_ATYP_FLAT:
3567 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3568 lun[6] == 0 && lun[7] == 0)
3569 result = ((lun[0] & 0x3f) << 8) + lun[1];
3571 case RPL_LUNDATA_ATYP_EXTLUN:
3572 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3574 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3579 result = (lun[1] << 16) + (lun[2] << 8) +
3583 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3584 result = (lun[2] << 24) +
3585 (lun[3] << 16) + (lun[4] << 8) +
3590 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3591 result = 0xffffffff;
3600 ctl_encode_lun(uint32_t decoded)
3602 uint64_t l = decoded;
3605 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3607 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3609 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3611 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3614 static struct ctl_port *
3615 ctl_io_port(struct ctl_io_hdr *io_hdr)
3618 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3622 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3626 for (i = first; i < last; i++) {
3627 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3634 ctl_set_mask(uint32_t *mask, uint32_t bit)
3636 uint32_t chunk, piece;
3639 piece = bit % (sizeof(uint32_t) * 8);
3641 if ((mask[chunk] & (1 << piece)) != 0)
3644 mask[chunk] |= (1 << piece);
3650 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3652 uint32_t chunk, piece;
3655 piece = bit % (sizeof(uint32_t) * 8);
3657 if ((mask[chunk] & (1 << piece)) == 0)
3660 mask[chunk] &= ~(1 << piece);
3666 ctl_is_set(uint32_t *mask, uint32_t bit)
3668 uint32_t chunk, piece;
3671 piece = bit % (sizeof(uint32_t) * 8);
3673 if ((mask[chunk] & (1 << piece)) == 0)
3680 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3684 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3687 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3691 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3695 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3698 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3702 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3707 i = residx/CTL_MAX_INIT_PER_PORT;
3708 if (lun->pr_keys[i] != NULL)
3710 mtx_unlock(&lun->lun_lock);
3711 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3713 mtx_lock(&lun->lun_lock);
3714 if (lun->pr_keys[i] == NULL)
3715 lun->pr_keys[i] = p;
3721 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3725 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3726 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3727 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3731 * ctl_softc, pool_name, total_ctl_io are passed in.
3732 * npool is passed out.
3735 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3736 uint32_t total_ctl_io, void **npool)
3739 struct ctl_io_pool *pool;
3741 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3746 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3747 pool->ctl_softc = ctl_softc;
3748 pool->zone = uma_zsecond_create(pool->name, NULL,
3749 NULL, NULL, NULL, ctl_softc->io_zone);
3750 /* uma_prealloc(pool->zone, total_ctl_io); */
3754 *npool = ctl_softc->io_zone;
3760 ctl_pool_free(struct ctl_io_pool *pool)
3767 uma_zdestroy(pool->zone);
3773 ctl_alloc_io(void *pool_ref)
3777 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3779 io = uma_zalloc(pool->zone, M_WAITOK);
3781 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3784 io->io_hdr.pool = pool_ref;
3789 ctl_alloc_io_nowait(void *pool_ref)
3793 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3795 io = uma_zalloc(pool->zone, M_NOWAIT);
3797 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3800 io->io_hdr.pool = pool_ref;
3805 ctl_free_io(union ctl_io *io)
3808 struct ctl_io_pool *pool;
3815 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3816 uma_zfree(pool->zone, io);
3818 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3823 ctl_zero_io(union ctl_io *io)
3831 * May need to preserve linked list pointers at some point too.
3833 pool_ref = io->io_hdr.pool;
3834 memset(io, 0, sizeof(*io));
3835 io->io_hdr.pool = pool_ref;
3839 ctl_expand_number(const char *buf, uint64_t *num)
3845 number = strtoq(buf, &endptr, 0);
3847 switch (tolower((unsigned char)*endptr)) {
3867 case '\0': /* No unit. */
3871 /* Unrecognized unit. */
3875 if ((number << shift) >> shift != number) {
3879 *num = number << shift;
3885 * This routine could be used in the future to load default and/or saved
3886 * mode page parameters for a particuar lun.
3889 ctl_init_page_index(struct ctl_lun *lun)
3892 struct ctl_page_index *page_index;
3896 memcpy(&lun->mode_pages.index, page_index_template,
3897 sizeof(page_index_template));
3899 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3901 page_index = &lun->mode_pages.index[i];
3902 if (lun->be_lun->lun_type == T_DIRECT &&
3903 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3905 if (lun->be_lun->lun_type == T_PROCESSOR &&
3906 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3908 if (lun->be_lun->lun_type == T_CDROM &&
3909 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3912 page_code = page_index->page_code & SMPH_PC_MASK;
3913 switch (page_code) {
3914 case SMS_RW_ERROR_RECOVERY_PAGE: {
3915 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3916 ("subpage %#x for page %#x is incorrect!",
3917 page_index->subpage, page_code));
3918 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3919 &rw_er_page_default,
3920 sizeof(rw_er_page_default));
3921 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3922 &rw_er_page_changeable,
3923 sizeof(rw_er_page_changeable));
3924 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3925 &rw_er_page_default,
3926 sizeof(rw_er_page_default));
3927 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3928 &rw_er_page_default,
3929 sizeof(rw_er_page_default));
3930 page_index->page_data =
3931 (uint8_t *)lun->mode_pages.rw_er_page;
3934 case SMS_FORMAT_DEVICE_PAGE: {
3935 struct scsi_format_page *format_page;
3937 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3938 ("subpage %#x for page %#x is incorrect!",
3939 page_index->subpage, page_code));
3942 * Sectors per track are set above. Bytes per
3943 * sector need to be set here on a per-LUN basis.
3945 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3946 &format_page_default,
3947 sizeof(format_page_default));
3948 memcpy(&lun->mode_pages.format_page[
3949 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3950 sizeof(format_page_changeable));
3951 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3952 &format_page_default,
3953 sizeof(format_page_default));
3954 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3955 &format_page_default,
3956 sizeof(format_page_default));
3958 format_page = &lun->mode_pages.format_page[
3960 scsi_ulto2b(lun->be_lun->blocksize,
3961 format_page->bytes_per_sector);
3963 format_page = &lun->mode_pages.format_page[
3965 scsi_ulto2b(lun->be_lun->blocksize,
3966 format_page->bytes_per_sector);
3968 format_page = &lun->mode_pages.format_page[
3970 scsi_ulto2b(lun->be_lun->blocksize,
3971 format_page->bytes_per_sector);
3973 page_index->page_data =
3974 (uint8_t *)lun->mode_pages.format_page;
3977 case SMS_RIGID_DISK_PAGE: {
3978 struct scsi_rigid_disk_page *rigid_disk_page;
3979 uint32_t sectors_per_cylinder;
3983 #endif /* !__XSCALE__ */
3985 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3986 ("subpage %#x for page %#x is incorrect!",
3987 page_index->subpage, page_code));
3990 * Rotation rate and sectors per track are set
3991 * above. We calculate the cylinders here based on
3992 * capacity. Due to the number of heads and
3993 * sectors per track we're using, smaller arrays
3994 * may turn out to have 0 cylinders. Linux and
3995 * FreeBSD don't pay attention to these mode pages
3996 * to figure out capacity, but Solaris does. It
3997 * seems to deal with 0 cylinders just fine, and
3998 * works out a fake geometry based on the capacity.
4000 memcpy(&lun->mode_pages.rigid_disk_page[
4001 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4002 sizeof(rigid_disk_page_default));
4003 memcpy(&lun->mode_pages.rigid_disk_page[
4004 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4005 sizeof(rigid_disk_page_changeable));
4007 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4011 * The divide method here will be more accurate,
4012 * probably, but results in floating point being
4013 * used in the kernel on i386 (__udivdi3()). On the
4014 * XScale, though, __udivdi3() is implemented in
4017 * The shift method for cylinder calculation is
4018 * accurate if sectors_per_cylinder is a power of
4019 * 2. Otherwise it might be slightly off -- you
4020 * might have a bit of a truncation problem.
4023 cylinders = (lun->be_lun->maxlba + 1) /
4024 sectors_per_cylinder;
4026 for (shift = 31; shift > 0; shift--) {
4027 if (sectors_per_cylinder & (1 << shift))
4030 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4034 * We've basically got 3 bytes, or 24 bits for the
4035 * cylinder size in the mode page. If we're over,
4036 * just round down to 2^24.
4038 if (cylinders > 0xffffff)
4039 cylinders = 0xffffff;
4041 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4043 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4045 if ((value = ctl_get_opt(&lun->be_lun->options,
4047 scsi_ulto2b(strtol(value, NULL, 0),
4048 rigid_disk_page->rotation_rate);
4051 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4052 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4053 sizeof(rigid_disk_page_default));
4054 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4055 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4056 sizeof(rigid_disk_page_default));
4058 page_index->page_data =
4059 (uint8_t *)lun->mode_pages.rigid_disk_page;
4062 case SMS_CACHING_PAGE: {
4063 struct scsi_caching_page *caching_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.caching_page[CTL_PAGE_DEFAULT],
4069 &caching_page_default,
4070 sizeof(caching_page_default));
4071 memcpy(&lun->mode_pages.caching_page[
4072 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4073 sizeof(caching_page_changeable));
4074 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4075 &caching_page_default,
4076 sizeof(caching_page_default));
4077 caching_page = &lun->mode_pages.caching_page[
4079 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4080 if (value != NULL && strcmp(value, "off") == 0)
4081 caching_page->flags1 &= ~SCP_WCE;
4082 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4083 if (value != NULL && strcmp(value, "off") == 0)
4084 caching_page->flags1 |= SCP_RCD;
4085 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4086 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4087 sizeof(caching_page_default));
4088 page_index->page_data =
4089 (uint8_t *)lun->mode_pages.caching_page;
4092 case SMS_CONTROL_MODE_PAGE: {
4093 switch (page_index->subpage) {
4094 case SMS_SUBPAGE_PAGE_0: {
4095 struct scsi_control_page *control_page;
4097 memcpy(&lun->mode_pages.control_page[
4099 &control_page_default,
4100 sizeof(control_page_default));
4101 memcpy(&lun->mode_pages.control_page[
4102 CTL_PAGE_CHANGEABLE],
4103 &control_page_changeable,
4104 sizeof(control_page_changeable));
4105 memcpy(&lun->mode_pages.control_page[
4107 &control_page_default,
4108 sizeof(control_page_default));
4109 control_page = &lun->mode_pages.control_page[
4111 value = ctl_get_opt(&lun->be_lun->options,
4113 if (value != NULL &&
4114 strcmp(value, "unrestricted") == 0) {
4115 control_page->queue_flags &=
4116 ~SCP_QUEUE_ALG_MASK;
4117 control_page->queue_flags |=
4118 SCP_QUEUE_ALG_UNRESTRICTED;
4120 memcpy(&lun->mode_pages.control_page[
4122 &lun->mode_pages.control_page[
4124 sizeof(control_page_default));
4125 page_index->page_data =
4126 (uint8_t *)lun->mode_pages.control_page;
4130 memcpy(&lun->mode_pages.control_ext_page[
4132 &control_ext_page_default,
4133 sizeof(control_ext_page_default));
4134 memcpy(&lun->mode_pages.control_ext_page[
4135 CTL_PAGE_CHANGEABLE],
4136 &control_ext_page_changeable,
4137 sizeof(control_ext_page_changeable));
4138 memcpy(&lun->mode_pages.control_ext_page[
4140 &control_ext_page_default,
4141 sizeof(control_ext_page_default));
4142 memcpy(&lun->mode_pages.control_ext_page[
4144 &lun->mode_pages.control_ext_page[
4146 sizeof(control_ext_page_default));
4147 page_index->page_data =
4148 (uint8_t *)lun->mode_pages.control_ext_page;
4151 panic("subpage %#x for page %#x is incorrect!",
4152 page_index->subpage, page_code);
4156 case SMS_INFO_EXCEPTIONS_PAGE: {
4157 switch (page_index->subpage) {
4158 case SMS_SUBPAGE_PAGE_0:
4159 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4161 sizeof(ie_page_default));
4162 memcpy(&lun->mode_pages.ie_page[
4163 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4164 sizeof(ie_page_changeable));
4165 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4167 sizeof(ie_page_default));
4168 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4170 sizeof(ie_page_default));
4171 page_index->page_data =
4172 (uint8_t *)lun->mode_pages.ie_page;
4175 struct ctl_logical_block_provisioning_page *page;
4177 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4179 sizeof(lbp_page_default));
4180 memcpy(&lun->mode_pages.lbp_page[
4181 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4182 sizeof(lbp_page_changeable));
4183 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4185 sizeof(lbp_page_default));
4186 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4187 value = ctl_get_opt(&lun->be_lun->options,
4189 if (value != NULL &&
4190 ctl_expand_number(value, &ival) == 0) {
4191 page->descr[0].flags |= SLBPPD_ENABLED |
4193 if (lun->be_lun->blocksize)
4194 ival /= lun->be_lun->blocksize;
4197 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4198 page->descr[0].count);
4200 value = ctl_get_opt(&lun->be_lun->options,
4202 if (value != NULL &&
4203 ctl_expand_number(value, &ival) == 0) {
4204 page->descr[1].flags |= SLBPPD_ENABLED |
4206 if (lun->be_lun->blocksize)
4207 ival /= lun->be_lun->blocksize;
4210 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4211 page->descr[1].count);
4213 value = ctl_get_opt(&lun->be_lun->options,
4214 "pool-avail-threshold");
4215 if (value != NULL &&
4216 ctl_expand_number(value, &ival) == 0) {
4217 page->descr[2].flags |= SLBPPD_ENABLED |
4219 if (lun->be_lun->blocksize)
4220 ival /= lun->be_lun->blocksize;
4223 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4224 page->descr[2].count);
4226 value = ctl_get_opt(&lun->be_lun->options,
4227 "pool-used-threshold");
4228 if (value != NULL &&
4229 ctl_expand_number(value, &ival) == 0) {
4230 page->descr[3].flags |= SLBPPD_ENABLED |
4232 if (lun->be_lun->blocksize)
4233 ival /= lun->be_lun->blocksize;
4236 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4237 page->descr[3].count);
4239 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4240 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4241 sizeof(lbp_page_default));
4242 page_index->page_data =
4243 (uint8_t *)lun->mode_pages.lbp_page;
4247 panic("subpage %#x for page %#x is incorrect!",
4248 page_index->subpage, page_code);
4252 case SMS_CDDVD_CAPS_PAGE:{
4253 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4254 ("subpage %#x for page %#x is incorrect!",
4255 page_index->subpage, page_code));
4256 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4257 &cddvd_page_default,
4258 sizeof(cddvd_page_default));
4259 memcpy(&lun->mode_pages.cddvd_page[
4260 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4261 sizeof(cddvd_page_changeable));
4262 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4263 &cddvd_page_default,
4264 sizeof(cddvd_page_default));
4265 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4266 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4267 sizeof(cddvd_page_default));
4268 page_index->page_data =
4269 (uint8_t *)lun->mode_pages.cddvd_page;
4272 case SMS_VENDOR_SPECIFIC_PAGE:{
4273 switch (page_index->subpage) {
4274 case DBGCNF_SUBPAGE_CODE: {
4275 memcpy(&lun->mode_pages.debugconf_subpage[
4277 &debugconf_page_default,
4278 sizeof(debugconf_page_default));
4279 memcpy(&lun->mode_pages.debugconf_subpage[
4280 CTL_PAGE_CHANGEABLE],
4281 &debugconf_page_changeable,
4282 sizeof(debugconf_page_changeable));
4283 memcpy(&lun->mode_pages.debugconf_subpage[
4285 &debugconf_page_default,
4286 sizeof(debugconf_page_default));
4287 memcpy(&lun->mode_pages.debugconf_subpage[
4289 &debugconf_page_default,
4290 sizeof(debugconf_page_default));
4291 page_index->page_data =
4292 (uint8_t *)lun->mode_pages.debugconf_subpage;
4296 panic("subpage %#x for page %#x is incorrect!",
4297 page_index->subpage, page_code);
4302 panic("invalid page code value %#x", page_code);
4306 return (CTL_RETVAL_COMPLETE);
4310 ctl_init_log_page_index(struct ctl_lun *lun)
4312 struct ctl_page_index *page_index;
4315 memcpy(&lun->log_pages.index, log_page_index_template,
4316 sizeof(log_page_index_template));
4319 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4321 page_index = &lun->log_pages.index[i];
4322 if (lun->be_lun->lun_type == T_DIRECT &&
4323 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4325 if (lun->be_lun->lun_type == T_PROCESSOR &&
4326 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4328 if (lun->be_lun->lun_type == T_CDROM &&
4329 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4332 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4333 lun->backend->lun_attr == NULL)
4336 if (page_index->page_code != prev) {
4337 lun->log_pages.pages_page[j] = page_index->page_code;
4338 prev = page_index->page_code;
4341 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4342 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4345 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4346 lun->log_pages.index[0].page_len = j;
4347 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4348 lun->log_pages.index[1].page_len = k * 2;
4349 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4350 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4351 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4352 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4354 return (CTL_RETVAL_COMPLETE);
4358 hex2bin(const char *str, uint8_t *buf, int buf_size)
4363 memset(buf, 0, buf_size);
4364 while (isspace(str[0]))
4366 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4369 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4373 else if (isalpha(c))
4374 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4380 buf[i / 2] |= (c << 4);
4384 return ((i + 1) / 2);
4391 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4392 * wants us to allocate the LUN and he can block.
4393 * - ctl_softc is always set
4394 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4396 * Returns 0 for success, non-zero (errno) for failure.
4399 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4400 struct ctl_be_lun *const be_lun)
4402 struct ctl_lun *nlun, *lun;
4403 struct scsi_vpd_id_descriptor *desc;
4404 struct scsi_vpd_id_t10 *t10id;
4405 const char *eui, *naa, *scsiname, *vendor, *value;
4406 int lun_number, i, lun_malloced;
4407 int devidlen, idlen1, idlen2 = 0, len;
4413 * We currently only support Direct Access or Processor LUN types.
4415 switch (be_lun->lun_type) {
4423 be_lun->lun_config_status(be_lun->be_lun,
4424 CTL_LUN_CONFIG_FAILURE);
4427 if (ctl_lun == NULL) {
4428 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4435 memset(lun, 0, sizeof(*lun));
4437 lun->flags = CTL_LUN_MALLOCED;
4439 /* Generate LUN ID. */
4440 devidlen = max(CTL_DEVID_MIN_LEN,
4441 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4442 idlen1 = sizeof(*t10id) + devidlen;
4443 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4444 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4445 if (scsiname != NULL) {
4446 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4447 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4449 eui = ctl_get_opt(&be_lun->options, "eui");
4451 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4453 naa = ctl_get_opt(&be_lun->options, "naa");
4455 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4457 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4458 M_CTL, M_WAITOK | M_ZERO);
4459 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4460 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4461 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4462 desc->length = idlen1;
4463 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4464 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4465 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4466 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4468 strncpy(t10id->vendor, vendor,
4469 min(sizeof(t10id->vendor), strlen(vendor)));
4471 strncpy((char *)t10id->vendor_spec_id,
4472 (char *)be_lun->device_id, devidlen);
4473 if (scsiname != NULL) {
4474 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4476 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4477 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4478 SVPD_ID_TYPE_SCSI_NAME;
4479 desc->length = idlen2;
4480 strlcpy(desc->identifier, scsiname, idlen2);
4483 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4485 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4486 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4488 desc->length = hex2bin(eui, desc->identifier, 16);
4489 desc->length = desc->length > 12 ? 16 :
4490 (desc->length > 8 ? 12 : 8);
4491 len -= 16 - desc->length;
4494 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4496 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4497 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4499 desc->length = hex2bin(naa, desc->identifier, 16);
4500 desc->length = desc->length > 8 ? 16 : 8;
4501 len -= 16 - desc->length;
4503 lun->lun_devid->len = len;
4505 mtx_lock(&ctl_softc->ctl_lock);
4507 * See if the caller requested a particular LUN number. If so, see
4508 * if it is available. Otherwise, allocate the first available LUN.
4510 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4511 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4512 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4513 mtx_unlock(&ctl_softc->ctl_lock);
4514 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4515 printf("ctl: requested LUN ID %d is higher "
4516 "than CTL_MAX_LUNS - 1 (%d)\n",
4517 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4520 * XXX KDM return an error, or just assign
4521 * another LUN ID in this case??
4523 printf("ctl: requested LUN ID %d is already "
4524 "in use\n", be_lun->req_lun_id);
4526 if (lun->flags & CTL_LUN_MALLOCED)
4528 be_lun->lun_config_status(be_lun->be_lun,
4529 CTL_LUN_CONFIG_FAILURE);
4532 lun_number = be_lun->req_lun_id;
4534 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4535 if (lun_number == -1) {
4536 mtx_unlock(&ctl_softc->ctl_lock);
4537 printf("ctl: can't allocate LUN, out of LUNs\n");
4538 if (lun->flags & CTL_LUN_MALLOCED)
4540 be_lun->lun_config_status(be_lun->be_lun,
4541 CTL_LUN_CONFIG_FAILURE);
4545 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4547 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4548 lun->lun = lun_number;
4549 lun->be_lun = be_lun;
4551 * The processor LUN is always enabled. Disk LUNs come on line
4552 * disabled, and must be enabled by the backend.
4554 lun->flags |= CTL_LUN_DISABLED;
4555 lun->backend = be_lun->be;
4556 be_lun->ctl_lun = lun;
4557 be_lun->lun_id = lun_number;
4558 atomic_add_int(&be_lun->be->num_luns, 1);
4559 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4560 lun->flags |= CTL_LUN_EJECTED;
4561 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4562 lun->flags |= CTL_LUN_NO_MEDIA;
4563 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4564 lun->flags |= CTL_LUN_STOPPED;
4566 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4567 lun->flags |= CTL_LUN_PRIMARY_SC;
4569 value = ctl_get_opt(&be_lun->options, "removable");
4570 if (value != NULL) {
4571 if (strcmp(value, "on") == 0)
4572 lun->flags |= CTL_LUN_REMOVABLE;
4573 } else if (be_lun->lun_type == T_CDROM)
4574 lun->flags |= CTL_LUN_REMOVABLE;
4576 lun->ctl_softc = ctl_softc;
4578 lun->last_busy = getsbinuptime();
4580 TAILQ_INIT(&lun->ooa_queue);
4581 TAILQ_INIT(&lun->blocked_queue);
4582 STAILQ_INIT(&lun->error_list);
4583 ctl_tpc_lun_init(lun);
4586 * Initialize the mode and log page index.
4588 ctl_init_page_index(lun);
4589 ctl_init_log_page_index(lun);
4592 * Now, before we insert this lun on the lun list, set the lun
4593 * inventory changed UA for all other luns.
4595 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4596 mtx_lock(&nlun->lun_lock);
4597 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4598 mtx_unlock(&nlun->lun_lock);
4601 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4603 ctl_softc->ctl_luns[lun_number] = lun;
4605 ctl_softc->num_luns++;
4607 /* Setup statistics gathering */
4608 lun->stats.device_type = be_lun->lun_type;
4609 lun->stats.lun_number = lun_number;
4610 lun->stats.blocksize = be_lun->blocksize;
4611 if (be_lun->blocksize == 0)
4612 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4613 for (i = 0;i < CTL_MAX_PORTS;i++)
4614 lun->stats.ports[i].targ_port = i;
4616 mtx_unlock(&ctl_softc->ctl_lock);
4618 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4625 * - LUN has already been marked invalid and any pending I/O has been taken
4629 ctl_free_lun(struct ctl_lun *lun)
4631 struct ctl_softc *softc;
4632 struct ctl_lun *nlun;
4635 softc = lun->ctl_softc;
4637 mtx_assert(&softc->ctl_lock, MA_OWNED);
4639 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4641 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4643 softc->ctl_luns[lun->lun] = NULL;
4645 if (!TAILQ_EMPTY(&lun->ooa_queue))
4646 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4651 * Tell the backend to free resources, if this LUN has a backend.
4653 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4654 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4656 ctl_tpc_lun_shutdown(lun);
4657 mtx_destroy(&lun->lun_lock);
4658 free(lun->lun_devid, M_CTL);
4659 for (i = 0; i < CTL_MAX_PORTS; i++)
4660 free(lun->pending_ua[i], M_CTL);
4661 for (i = 0; i < CTL_MAX_PORTS; i++)
4662 free(lun->pr_keys[i], M_CTL);
4663 free(lun->write_buffer, M_CTL);
4664 if (lun->flags & CTL_LUN_MALLOCED)
4667 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4668 mtx_lock(&nlun->lun_lock);
4669 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4670 mtx_unlock(&nlun->lun_lock);
4677 ctl_create_lun(struct ctl_be_lun *be_lun)
4681 * ctl_alloc_lun() should handle all potential failure cases.
4683 ctl_alloc_lun(control_softc, NULL, be_lun);
4687 ctl_add_lun(struct ctl_be_lun *be_lun)
4689 struct ctl_softc *softc = control_softc;
4691 mtx_lock(&softc->ctl_lock);
4692 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4693 mtx_unlock(&softc->ctl_lock);
4694 wakeup(&softc->pending_lun_queue);
4700 ctl_enable_lun(struct ctl_be_lun *be_lun)
4702 struct ctl_softc *softc;
4703 struct ctl_port *port, *nport;
4704 struct ctl_lun *lun;
4707 lun = (struct ctl_lun *)be_lun->ctl_lun;
4708 softc = lun->ctl_softc;
4710 mtx_lock(&softc->ctl_lock);
4711 mtx_lock(&lun->lun_lock);
4712 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4714 * eh? Why did we get called if the LUN is already
4717 mtx_unlock(&lun->lun_lock);
4718 mtx_unlock(&softc->ctl_lock);
4721 lun->flags &= ~CTL_LUN_DISABLED;
4722 mtx_unlock(&lun->lun_lock);
4724 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4725 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4726 port->lun_map != NULL || port->lun_enable == NULL)
4730 * Drop the lock while we call the FETD's enable routine.
4731 * This can lead to a callback into CTL (at least in the
4732 * case of the internal initiator frontend.
4734 mtx_unlock(&softc->ctl_lock);
4735 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4736 mtx_lock(&softc->ctl_lock);
4738 printf("%s: FETD %s port %d returned error "
4739 "%d for lun_enable on lun %jd\n",
4740 __func__, port->port_name, port->targ_port,
4741 retval, (intmax_t)lun->lun);
4745 mtx_unlock(&softc->ctl_lock);
4746 ctl_isc_announce_lun(lun);
4752 ctl_disable_lun(struct ctl_be_lun *be_lun)
4754 struct ctl_softc *softc;
4755 struct ctl_port *port;
4756 struct ctl_lun *lun;
4759 lun = (struct ctl_lun *)be_lun->ctl_lun;
4760 softc = lun->ctl_softc;
4762 mtx_lock(&softc->ctl_lock);
4763 mtx_lock(&lun->lun_lock);
4764 if (lun->flags & CTL_LUN_DISABLED) {
4765 mtx_unlock(&lun->lun_lock);
4766 mtx_unlock(&softc->ctl_lock);
4769 lun->flags |= CTL_LUN_DISABLED;
4770 mtx_unlock(&lun->lun_lock);
4772 STAILQ_FOREACH(port, &softc->port_list, links) {
4773 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4774 port->lun_map != NULL || port->lun_disable == NULL)
4778 * Drop the lock before we call the frontend's disable
4779 * routine, to avoid lock order reversals.
4781 * XXX KDM what happens if the frontend list changes while
4782 * we're traversing it? It's unlikely, but should be handled.
4784 mtx_unlock(&softc->ctl_lock);
4785 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4786 mtx_lock(&softc->ctl_lock);
4788 printf("%s: FETD %s port %d returned error "
4789 "%d for lun_disable on lun %jd\n",
4790 __func__, port->port_name, port->targ_port,
4791 retval, (intmax_t)lun->lun);
4795 mtx_unlock(&softc->ctl_lock);
4796 ctl_isc_announce_lun(lun);
4802 ctl_start_lun(struct ctl_be_lun *be_lun)
4804 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4806 mtx_lock(&lun->lun_lock);
4807 lun->flags &= ~CTL_LUN_STOPPED;
4808 mtx_unlock(&lun->lun_lock);
4813 ctl_stop_lun(struct ctl_be_lun *be_lun)
4815 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4817 mtx_lock(&lun->lun_lock);
4818 lun->flags |= CTL_LUN_STOPPED;
4819 mtx_unlock(&lun->lun_lock);
4824 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4826 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4828 mtx_lock(&lun->lun_lock);
4829 lun->flags |= CTL_LUN_NO_MEDIA;
4830 mtx_unlock(&lun->lun_lock);
4835 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4837 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4838 union ctl_ha_msg msg;
4840 mtx_lock(&lun->lun_lock);
4841 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4842 if (lun->flags & CTL_LUN_REMOVABLE)
4843 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4844 mtx_unlock(&lun->lun_lock);
4845 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4846 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4847 bzero(&msg.ua, sizeof(msg.ua));
4848 msg.hdr.msg_type = CTL_MSG_UA;
4849 msg.hdr.nexus.initid = -1;
4850 msg.hdr.nexus.targ_port = -1;
4851 msg.hdr.nexus.targ_lun = lun->lun;
4852 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4855 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4856 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4863 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4865 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4867 mtx_lock(&lun->lun_lock);
4868 lun->flags |= CTL_LUN_EJECTED;
4869 mtx_unlock(&lun->lun_lock);
4874 ctl_lun_primary(struct ctl_be_lun *be_lun)
4876 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4878 mtx_lock(&lun->lun_lock);
4879 lun->flags |= CTL_LUN_PRIMARY_SC;
4880 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4881 mtx_unlock(&lun->lun_lock);
4882 ctl_isc_announce_lun(lun);
4887 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4889 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4891 mtx_lock(&lun->lun_lock);
4892 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4893 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4894 mtx_unlock(&lun->lun_lock);
4895 ctl_isc_announce_lun(lun);
4900 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4902 struct ctl_softc *softc;
4903 struct ctl_lun *lun;
4905 lun = (struct ctl_lun *)be_lun->ctl_lun;
4906 softc = lun->ctl_softc;
4908 mtx_lock(&lun->lun_lock);
4911 * The LUN needs to be disabled before it can be marked invalid.
4913 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4914 mtx_unlock(&lun->lun_lock);
4918 * Mark the LUN invalid.
4920 lun->flags |= CTL_LUN_INVALID;
4923 * If there is nothing in the OOA queue, go ahead and free the LUN.
4924 * If we have something in the OOA queue, we'll free it when the
4925 * last I/O completes.
4927 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4928 mtx_unlock(&lun->lun_lock);
4929 mtx_lock(&softc->ctl_lock);
4931 mtx_unlock(&softc->ctl_lock);
4933 mtx_unlock(&lun->lun_lock);
4939 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4941 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4942 union ctl_ha_msg msg;
4944 mtx_lock(&lun->lun_lock);
4945 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
4946 mtx_unlock(&lun->lun_lock);
4947 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4948 /* Send msg to other side. */
4949 bzero(&msg.ua, sizeof(msg.ua));
4950 msg.hdr.msg_type = CTL_MSG_UA;
4951 msg.hdr.nexus.initid = -1;
4952 msg.hdr.nexus.targ_port = -1;
4953 msg.hdr.nexus.targ_lun = lun->lun;
4954 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4957 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
4958 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4964 * Backend "memory move is complete" callback for requests that never
4965 * make it down to say RAIDCore's configuration code.
4968 ctl_config_move_done(union ctl_io *io)
4972 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4973 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4974 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4976 if ((io->io_hdr.port_status != 0) &&
4977 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4978 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4980 * For hardware error sense keys, the sense key
4981 * specific value is defined to be a retry count,
4982 * but we use it to pass back an internal FETD
4983 * error code. XXX KDM Hopefully the FETD is only
4984 * using 16 bits for an error code, since that's
4985 * all the space we have in the sks field.
4987 ctl_set_internal_failure(&io->scsiio,
4990 io->io_hdr.port_status);
4993 if (ctl_debug & CTL_DEBUG_CDB_DATA)
4995 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4996 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4997 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4998 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5000 * XXX KDM just assuming a single pointer here, and not a
5001 * S/G list. If we start using S/G lists for config data,
5002 * we'll need to know how to clean them up here as well.
5004 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5005 free(io->scsiio.kern_data_ptr, M_CTL);
5007 retval = CTL_RETVAL_COMPLETE;
5010 * XXX KDM now we need to continue data movement. Some
5012 * - call ctl_scsiio() again? We don't do this for data
5013 * writes, because for those at least we know ahead of
5014 * time where the write will go and how long it is. For
5015 * config writes, though, that information is largely
5016 * contained within the write itself, thus we need to
5017 * parse out the data again.
5019 * - Call some other function once the data is in?
5023 * XXX KDM call ctl_scsiio() again for now, and check flag
5024 * bits to see whether we're allocated or not.
5026 retval = ctl_scsiio(&io->scsiio);
5032 * This gets called by a backend driver when it is done with a
5033 * data_submit method.
5036 ctl_data_submit_done(union ctl_io *io)
5039 * If the IO_CONT flag is set, we need to call the supplied
5040 * function to continue processing the I/O, instead of completing
5043 * If there is an error, though, we don't want to keep processing.
5044 * Instead, just send status back to the initiator.
5046 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5047 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5048 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5049 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5050 io->scsiio.io_cont(io);
5057 * This gets called by a backend driver when it is done with a
5058 * configuration write.
5061 ctl_config_write_done(union ctl_io *io)
5066 * If the IO_CONT flag is set, we need to call the supplied
5067 * function to continue processing the I/O, instead of completing
5070 * If there is an error, though, we don't want to keep processing.
5071 * Instead, just send status back to the initiator.
5073 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5074 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5075 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5076 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5077 io->scsiio.io_cont(io);
5081 * Since a configuration write can be done for commands that actually
5082 * have data allocated, like write buffer, and commands that have
5083 * no data, like start/stop unit, we need to check here.
5085 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5086 buf = io->scsiio.kern_data_ptr;
5095 ctl_config_read_done(union ctl_io *io)
5100 * If there is some error -- we are done, skip data transfer.
5102 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5103 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5104 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5105 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5106 buf = io->scsiio.kern_data_ptr;
5116 * If the IO_CONT flag is set, we need to call the supplied
5117 * function to continue processing the I/O, instead of completing
5120 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5121 io->scsiio.io_cont(io);
5129 * SCSI release command.
5132 ctl_scsi_release(struct ctl_scsiio *ctsio)
5134 struct ctl_lun *lun;
5137 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5139 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5140 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5143 * XXX KDM right now, we only support LUN reservation. We don't
5144 * support 3rd party reservations, or extent reservations, which
5145 * might actually need the parameter list. If we've gotten this
5146 * far, we've got a LUN reservation. Anything else got kicked out
5147 * above. So, according to SPC, ignore the length.
5150 mtx_lock(&lun->lun_lock);
5153 * According to SPC, it is not an error for an intiator to attempt
5154 * to release a reservation on a LUN that isn't reserved, or that
5155 * is reserved by another initiator. The reservation can only be
5156 * released, though, by the initiator who made it or by one of
5157 * several reset type events.
5159 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5160 lun->flags &= ~CTL_LUN_RESERVED;
5162 mtx_unlock(&lun->lun_lock);
5164 ctl_set_success(ctsio);
5165 ctl_done((union ctl_io *)ctsio);
5166 return (CTL_RETVAL_COMPLETE);
5170 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5172 struct ctl_lun *lun;
5175 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5177 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5178 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5181 * XXX KDM right now, we only support LUN reservation. We don't
5182 * support 3rd party reservations, or extent reservations, which
5183 * might actually need the parameter list. If we've gotten this
5184 * far, we've got a LUN reservation. Anything else got kicked out
5185 * above. So, according to SPC, ignore the length.
5188 mtx_lock(&lun->lun_lock);
5189 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5190 ctl_set_reservation_conflict(ctsio);
5194 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5195 if (lun->flags & CTL_LUN_PR_RESERVED) {
5196 ctl_set_success(ctsio);
5200 lun->flags |= CTL_LUN_RESERVED;
5201 lun->res_idx = residx;
5202 ctl_set_success(ctsio);
5205 mtx_unlock(&lun->lun_lock);
5206 ctl_done((union ctl_io *)ctsio);
5207 return (CTL_RETVAL_COMPLETE);
5211 ctl_start_stop(struct ctl_scsiio *ctsio)
5213 struct scsi_start_stop_unit *cdb;
5214 struct ctl_lun *lun;
5217 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5219 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5220 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5222 if ((cdb->how & SSS_PC_MASK) == 0) {
5223 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5224 (cdb->how & SSS_START) == 0) {
5227 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5228 if (ctl_get_prkey(lun, residx) == 0 ||
5229 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5231 ctl_set_reservation_conflict(ctsio);
5232 ctl_done((union ctl_io *)ctsio);
5233 return (CTL_RETVAL_COMPLETE);
5237 if ((cdb->how & SSS_LOEJ) &&
5238 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5239 ctl_set_invalid_field(ctsio,
5245 ctl_done((union ctl_io *)ctsio);
5246 return (CTL_RETVAL_COMPLETE);
5249 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5250 lun->prevent_count > 0) {
5251 /* "Medium removal prevented" */
5252 ctl_set_sense(ctsio, /*current_error*/ 1,
5253 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5254 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5255 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5256 ctl_done((union ctl_io *)ctsio);
5257 return (CTL_RETVAL_COMPLETE);
5261 retval = lun->backend->config_write((union ctl_io *)ctsio);
5266 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5268 struct ctl_lun *lun;
5269 struct scsi_prevent *cdb;
5273 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5275 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5276 cdb = (struct scsi_prevent *)ctsio->cdb;
5278 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) {
5279 ctl_set_invalid_opcode(ctsio);
5280 ctl_done((union ctl_io *)ctsio);
5281 return (CTL_RETVAL_COMPLETE);
5284 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5285 mtx_lock(&lun->lun_lock);
5286 if ((cdb->how & PR_PREVENT) &&
5287 ctl_is_set(lun->prevent, initidx) == 0) {
5288 ctl_set_mask(lun->prevent, initidx);
5289 lun->prevent_count++;
5290 } else if ((cdb->how & PR_PREVENT) == 0 &&
5291 ctl_is_set(lun->prevent, initidx)) {
5292 ctl_clear_mask(lun->prevent, initidx);
5293 lun->prevent_count--;
5295 mtx_unlock(&lun->lun_lock);
5296 retval = lun->backend->config_write((union ctl_io *)ctsio);
5301 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5302 * we don't really do anything with the LBA and length fields if the user
5303 * passes them in. Instead we'll just flush out the cache for the entire
5307 ctl_sync_cache(struct ctl_scsiio *ctsio)
5309 struct ctl_lun *lun;
5310 struct ctl_softc *softc;
5311 struct ctl_lba_len_flags *lbalen;
5312 uint64_t starting_lba;
5313 uint32_t block_count;
5317 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5319 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5320 softc = lun->ctl_softc;
5323 switch (ctsio->cdb[0]) {
5324 case SYNCHRONIZE_CACHE: {
5325 struct scsi_sync_cache *cdb;
5326 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5328 starting_lba = scsi_4btoul(cdb->begin_lba);
5329 block_count = scsi_2btoul(cdb->lb_count);
5333 case SYNCHRONIZE_CACHE_16: {
5334 struct scsi_sync_cache_16 *cdb;
5335 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5337 starting_lba = scsi_8btou64(cdb->begin_lba);
5338 block_count = scsi_4btoul(cdb->lb_count);
5343 ctl_set_invalid_opcode(ctsio);
5344 ctl_done((union ctl_io *)ctsio);
5346 break; /* NOTREACHED */
5350 * We check the LBA and length, but don't do anything with them.
5351 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5352 * get flushed. This check will just help satisfy anyone who wants
5353 * to see an error for an out of range LBA.
5355 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5356 ctl_set_lba_out_of_range(ctsio);
5357 ctl_done((union ctl_io *)ctsio);
5361 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5362 lbalen->lba = starting_lba;
5363 lbalen->len = block_count;
5364 lbalen->flags = byte2;
5365 retval = lun->backend->config_write((union ctl_io *)ctsio);
5372 ctl_format(struct ctl_scsiio *ctsio)
5374 struct scsi_format *cdb;
5375 struct ctl_lun *lun;
5376 int length, defect_list_len;
5378 CTL_DEBUG_PRINT(("ctl_format\n"));
5380 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5382 cdb = (struct scsi_format *)ctsio->cdb;
5385 if (cdb->byte2 & SF_FMTDATA) {
5386 if (cdb->byte2 & SF_LONGLIST)
5387 length = sizeof(struct scsi_format_header_long);
5389 length = sizeof(struct scsi_format_header_short);
5392 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5394 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5395 ctsio->kern_data_len = length;
5396 ctsio->kern_total_len = length;
5397 ctsio->kern_data_resid = 0;
5398 ctsio->kern_rel_offset = 0;
5399 ctsio->kern_sg_entries = 0;
5400 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5401 ctsio->be_move_done = ctl_config_move_done;
5402 ctl_datamove((union ctl_io *)ctsio);
5404 return (CTL_RETVAL_COMPLETE);
5407 defect_list_len = 0;
5409 if (cdb->byte2 & SF_FMTDATA) {
5410 if (cdb->byte2 & SF_LONGLIST) {
5411 struct scsi_format_header_long *header;
5413 header = (struct scsi_format_header_long *)
5414 ctsio->kern_data_ptr;
5416 defect_list_len = scsi_4btoul(header->defect_list_len);
5417 if (defect_list_len != 0) {
5418 ctl_set_invalid_field(ctsio,
5427 struct scsi_format_header_short *header;
5429 header = (struct scsi_format_header_short *)
5430 ctsio->kern_data_ptr;
5432 defect_list_len = scsi_2btoul(header->defect_list_len);
5433 if (defect_list_len != 0) {
5434 ctl_set_invalid_field(ctsio,
5445 ctl_set_success(ctsio);
5448 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5449 free(ctsio->kern_data_ptr, M_CTL);
5450 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5453 ctl_done((union ctl_io *)ctsio);
5454 return (CTL_RETVAL_COMPLETE);
5458 ctl_read_buffer(struct ctl_scsiio *ctsio)
5460 struct ctl_lun *lun;
5461 uint64_t buffer_offset;
5464 static uint8_t descr[4];
5465 static uint8_t echo_descr[4] = { 0 };
5467 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5468 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5469 switch (ctsio->cdb[0]) {
5471 struct scsi_read_buffer *cdb;
5473 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5474 buffer_offset = scsi_3btoul(cdb->offset);
5475 len = scsi_3btoul(cdb->length);
5479 case READ_BUFFER_16: {
5480 struct scsi_read_buffer_16 *cdb;
5482 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5483 buffer_offset = scsi_8btou64(cdb->offset);
5484 len = scsi_4btoul(cdb->length);
5488 default: /* This shouldn't happen. */
5489 ctl_set_invalid_opcode(ctsio);
5490 ctl_done((union ctl_io *)ctsio);
5491 return (CTL_RETVAL_COMPLETE);
5494 if ((byte2 & RWB_MODE) != RWB_MODE_DATA &&
5495 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5496 (byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5497 ctl_set_invalid_field(ctsio,
5503 ctl_done((union ctl_io *)ctsio);
5504 return (CTL_RETVAL_COMPLETE);
5507 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5508 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5509 ctl_set_invalid_field(ctsio,
5515 ctl_done((union ctl_io *)ctsio);
5516 return (CTL_RETVAL_COMPLETE);
5519 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5521 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5522 ctsio->kern_data_ptr = descr;
5523 len = min(len, sizeof(descr));
5524 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5525 ctsio->kern_data_ptr = echo_descr;
5526 len = min(len, sizeof(echo_descr));
5528 if (lun->write_buffer == NULL) {
5529 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5532 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5534 ctsio->kern_data_len = len;
5535 ctsio->kern_total_len = len;
5536 ctsio->kern_data_resid = 0;
5537 ctsio->kern_rel_offset = 0;
5538 ctsio->kern_sg_entries = 0;
5539 ctl_set_success(ctsio);
5540 ctsio->be_move_done = ctl_config_move_done;
5541 ctl_datamove((union ctl_io *)ctsio);
5542 return (CTL_RETVAL_COMPLETE);
5546 ctl_write_buffer(struct ctl_scsiio *ctsio)
5548 struct scsi_write_buffer *cdb;
5549 struct ctl_lun *lun;
5550 int buffer_offset, len;
5552 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5554 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5555 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5557 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5558 ctl_set_invalid_field(ctsio,
5564 ctl_done((union ctl_io *)ctsio);
5565 return (CTL_RETVAL_COMPLETE);
5568 len = scsi_3btoul(cdb->length);
5569 buffer_offset = scsi_3btoul(cdb->offset);
5571 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5572 ctl_set_invalid_field(ctsio,
5578 ctl_done((union ctl_io *)ctsio);
5579 return (CTL_RETVAL_COMPLETE);
5583 * If we've got a kernel request that hasn't been malloced yet,
5584 * malloc it and tell the caller the data buffer is here.
5586 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5587 if (lun->write_buffer == NULL) {
5588 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5591 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5592 ctsio->kern_data_len = len;
5593 ctsio->kern_total_len = len;
5594 ctsio->kern_data_resid = 0;
5595 ctsio->kern_rel_offset = 0;
5596 ctsio->kern_sg_entries = 0;
5597 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5598 ctsio->be_move_done = ctl_config_move_done;
5599 ctl_datamove((union ctl_io *)ctsio);
5601 return (CTL_RETVAL_COMPLETE);
5604 ctl_set_success(ctsio);
5605 ctl_done((union ctl_io *)ctsio);
5606 return (CTL_RETVAL_COMPLETE);
5610 ctl_write_same(struct ctl_scsiio *ctsio)
5612 struct ctl_lun *lun;
5613 struct ctl_lba_len_flags *lbalen;
5615 uint32_t num_blocks;
5619 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5621 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5623 switch (ctsio->cdb[0]) {
5624 case WRITE_SAME_10: {
5625 struct scsi_write_same_10 *cdb;
5627 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5629 lba = scsi_4btoul(cdb->addr);
5630 num_blocks = scsi_2btoul(cdb->length);
5634 case WRITE_SAME_16: {
5635 struct scsi_write_same_16 *cdb;
5637 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5639 lba = scsi_8btou64(cdb->addr);
5640 num_blocks = scsi_4btoul(cdb->length);
5646 * We got a command we don't support. This shouldn't
5647 * happen, commands should be filtered out above us.
5649 ctl_set_invalid_opcode(ctsio);
5650 ctl_done((union ctl_io *)ctsio);
5652 return (CTL_RETVAL_COMPLETE);
5653 break; /* NOTREACHED */
5656 /* ANCHOR flag can be used only together with UNMAP */
5657 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5658 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5659 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5660 ctl_done((union ctl_io *)ctsio);
5661 return (CTL_RETVAL_COMPLETE);
5665 * The first check is to make sure we're in bounds, the second
5666 * check is to catch wrap-around problems. If the lba + num blocks
5667 * is less than the lba, then we've wrapped around and the block
5668 * range is invalid anyway.
5670 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5671 || ((lba + num_blocks) < lba)) {
5672 ctl_set_lba_out_of_range(ctsio);
5673 ctl_done((union ctl_io *)ctsio);
5674 return (CTL_RETVAL_COMPLETE);
5677 /* Zero number of blocks means "to the last logical block" */
5678 if (num_blocks == 0) {
5679 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5680 ctl_set_invalid_field(ctsio,
5686 ctl_done((union ctl_io *)ctsio);
5687 return (CTL_RETVAL_COMPLETE);
5689 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5692 len = lun->be_lun->blocksize;
5695 * If we've got a kernel request that hasn't been malloced yet,
5696 * malloc it and tell the caller the data buffer is here.
5698 if ((byte2 & SWS_NDOB) == 0 &&
5699 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5700 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5701 ctsio->kern_data_len = len;
5702 ctsio->kern_total_len = len;
5703 ctsio->kern_data_resid = 0;
5704 ctsio->kern_rel_offset = 0;
5705 ctsio->kern_sg_entries = 0;
5706 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5707 ctsio->be_move_done = ctl_config_move_done;
5708 ctl_datamove((union ctl_io *)ctsio);
5710 return (CTL_RETVAL_COMPLETE);
5713 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5715 lbalen->len = num_blocks;
5716 lbalen->flags = byte2;
5717 retval = lun->backend->config_write((union ctl_io *)ctsio);
5723 ctl_unmap(struct ctl_scsiio *ctsio)
5725 struct ctl_lun *lun;
5726 struct scsi_unmap *cdb;
5727 struct ctl_ptr_len_flags *ptrlen;
5728 struct scsi_unmap_header *hdr;
5729 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5731 uint32_t num_blocks;
5735 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5737 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5738 cdb = (struct scsi_unmap *)ctsio->cdb;
5740 len = scsi_2btoul(cdb->length);
5744 * If we've got a kernel request that hasn't been malloced yet,
5745 * malloc it and tell the caller the data buffer is here.
5747 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5748 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5749 ctsio->kern_data_len = len;
5750 ctsio->kern_total_len = len;
5751 ctsio->kern_data_resid = 0;
5752 ctsio->kern_rel_offset = 0;
5753 ctsio->kern_sg_entries = 0;
5754 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5755 ctsio->be_move_done = ctl_config_move_done;
5756 ctl_datamove((union ctl_io *)ctsio);
5758 return (CTL_RETVAL_COMPLETE);
5761 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5762 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5763 if (len < sizeof (*hdr) ||
5764 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5765 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5766 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5767 ctl_set_invalid_field(ctsio,
5775 len = scsi_2btoul(hdr->desc_length);
5776 buf = (struct scsi_unmap_desc *)(hdr + 1);
5777 end = buf + len / sizeof(*buf);
5780 for (range = buf; range < end; range++) {
5781 lba = scsi_8btou64(range->lba);
5782 num_blocks = scsi_4btoul(range->length);
5783 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5784 || ((lba + num_blocks) < lba)) {
5785 ctl_set_lba_out_of_range(ctsio);
5786 ctl_done((union ctl_io *)ctsio);
5787 return (CTL_RETVAL_COMPLETE);
5789 if (num_blocks != 0)
5794 * Block backend can not handle zero last range.
5795 * Filter it out and return if there is nothing left.
5797 len = (uint8_t *)endnz - (uint8_t *)buf;
5799 ctl_set_success(ctsio);
5803 mtx_lock(&lun->lun_lock);
5804 ptrlen = (struct ctl_ptr_len_flags *)
5805 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5806 ptrlen->ptr = (void *)buf;
5808 ptrlen->flags = byte2;
5809 ctl_check_blocked(lun);
5810 mtx_unlock(&lun->lun_lock);
5812 retval = lun->backend->config_write((union ctl_io *)ctsio);
5816 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5817 free(ctsio->kern_data_ptr, M_CTL);
5818 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5820 ctl_done((union ctl_io *)ctsio);
5821 return (CTL_RETVAL_COMPLETE);
5825 * Note that this function currently doesn't actually do anything inside
5826 * CTL to enforce things if the DQue bit is turned on.
5828 * Also note that this function can't be used in the default case, because
5829 * the DQue bit isn't set in the changeable mask for the control mode page
5830 * anyway. This is just here as an example for how to implement a page
5831 * handler, and a placeholder in case we want to allow the user to turn
5832 * tagged queueing on and off.
5834 * The D_SENSE bit handling is functional, however, and will turn
5835 * descriptor sense on and off for a given LUN.
5838 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5839 struct ctl_page_index *page_index, uint8_t *page_ptr)
5841 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5842 struct ctl_lun *lun;
5846 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5847 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5850 user_cp = (struct scsi_control_page *)page_ptr;
5851 current_cp = (struct scsi_control_page *)
5852 (page_index->page_data + (page_index->page_len *
5854 saved_cp = (struct scsi_control_page *)
5855 (page_index->page_data + (page_index->page_len *
5858 mtx_lock(&lun->lun_lock);
5859 if (((current_cp->rlec & SCP_DSENSE) == 0)
5860 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5862 * Descriptor sense is currently turned off and the user
5863 * wants to turn it on.
5865 current_cp->rlec |= SCP_DSENSE;
5866 saved_cp->rlec |= SCP_DSENSE;
5867 lun->flags |= CTL_LUN_SENSE_DESC;
5869 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5870 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5872 * Descriptor sense is currently turned on, and the user
5873 * wants to turn it off.
5875 current_cp->rlec &= ~SCP_DSENSE;
5876 saved_cp->rlec &= ~SCP_DSENSE;
5877 lun->flags &= ~CTL_LUN_SENSE_DESC;
5880 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
5881 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
5882 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5883 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5884 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5885 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5888 if ((current_cp->eca_and_aen & SCP_SWP) !=
5889 (user_cp->eca_and_aen & SCP_SWP)) {
5890 current_cp->eca_and_aen &= ~SCP_SWP;
5891 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5892 saved_cp->eca_and_aen &= ~SCP_SWP;
5893 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5897 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5898 mtx_unlock(&lun->lun_lock);
5900 ctl_isc_announce_mode(lun,
5901 ctl_get_initindex(&ctsio->io_hdr.nexus),
5902 page_index->page_code, page_index->subpage);
5908 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
5909 struct ctl_page_index *page_index, uint8_t *page_ptr)
5911 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
5912 struct ctl_lun *lun;
5916 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5917 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5920 user_cp = (struct scsi_caching_page *)page_ptr;
5921 current_cp = (struct scsi_caching_page *)
5922 (page_index->page_data + (page_index->page_len *
5924 saved_cp = (struct scsi_caching_page *)
5925 (page_index->page_data + (page_index->page_len *
5928 mtx_lock(&lun->lun_lock);
5929 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
5930 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
5931 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5932 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5933 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5934 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5938 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5939 mtx_unlock(&lun->lun_lock);
5941 ctl_isc_announce_mode(lun,
5942 ctl_get_initindex(&ctsio->io_hdr.nexus),
5943 page_index->page_code, page_index->subpage);
5949 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5950 struct ctl_page_index *page_index,
5956 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
5961 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
5962 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
5963 printf("page data:");
5965 printf(" %.2x",page_ptr[i]);
5971 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
5972 struct ctl_page_index *page_index,
5975 struct copan_debugconf_subpage *page;
5977 page = (struct copan_debugconf_subpage *)page_index->page_data +
5978 (page_index->page_len * pc);
5981 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5982 case SMS_PAGE_CTRL_DEFAULT >> 6:
5983 case SMS_PAGE_CTRL_SAVED >> 6:
5985 * We don't update the changable or default bits for this page.
5988 case SMS_PAGE_CTRL_CURRENT >> 6:
5989 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
5990 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
6000 ctl_do_mode_select(union ctl_io *io)
6002 struct scsi_mode_page_header *page_header;
6003 struct ctl_page_index *page_index;
6004 struct ctl_scsiio *ctsio;
6005 int page_len, page_len_offset, page_len_size;
6006 union ctl_modepage_info *modepage_info;
6007 struct ctl_lun *lun;
6008 int *len_left, *len_used;
6011 ctsio = &io->scsiio;
6014 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6016 modepage_info = (union ctl_modepage_info *)
6017 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6018 len_left = &modepage_info->header.len_left;
6019 len_used = &modepage_info->header.len_used;
6023 page_header = (struct scsi_mode_page_header *)
6024 (ctsio->kern_data_ptr + *len_used);
6026 if (*len_left == 0) {
6027 free(ctsio->kern_data_ptr, M_CTL);
6028 ctl_set_success(ctsio);
6029 ctl_done((union ctl_io *)ctsio);
6030 return (CTL_RETVAL_COMPLETE);
6031 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6033 free(ctsio->kern_data_ptr, M_CTL);
6034 ctl_set_param_len_error(ctsio);
6035 ctl_done((union ctl_io *)ctsio);
6036 return (CTL_RETVAL_COMPLETE);
6038 } else if ((page_header->page_code & SMPH_SPF)
6039 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6041 free(ctsio->kern_data_ptr, M_CTL);
6042 ctl_set_param_len_error(ctsio);
6043 ctl_done((union ctl_io *)ctsio);
6044 return (CTL_RETVAL_COMPLETE);
6049 * XXX KDM should we do something with the block descriptor?
6051 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6052 page_index = &lun->mode_pages.index[i];
6053 if (lun->be_lun->lun_type == T_DIRECT &&
6054 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6056 if (lun->be_lun->lun_type == T_PROCESSOR &&
6057 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6059 if (lun->be_lun->lun_type == T_CDROM &&
6060 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6063 if ((page_index->page_code & SMPH_PC_MASK) !=
6064 (page_header->page_code & SMPH_PC_MASK))
6068 * If neither page has a subpage code, then we've got a
6071 if (((page_index->page_code & SMPH_SPF) == 0)
6072 && ((page_header->page_code & SMPH_SPF) == 0)) {
6073 page_len = page_header->page_length;
6078 * If both pages have subpages, then the subpage numbers
6081 if ((page_index->page_code & SMPH_SPF)
6082 && (page_header->page_code & SMPH_SPF)) {
6083 struct scsi_mode_page_header_sp *sph;
6085 sph = (struct scsi_mode_page_header_sp *)page_header;
6086 if (page_index->subpage == sph->subpage) {
6087 page_len = scsi_2btoul(sph->page_length);
6094 * If we couldn't find the page, or if we don't have a mode select
6095 * handler for it, send back an error to the user.
6097 if ((i >= CTL_NUM_MODE_PAGES)
6098 || (page_index->select_handler == NULL)) {
6099 ctl_set_invalid_field(ctsio,
6102 /*field*/ *len_used,
6105 free(ctsio->kern_data_ptr, M_CTL);
6106 ctl_done((union ctl_io *)ctsio);
6107 return (CTL_RETVAL_COMPLETE);
6110 if (page_index->page_code & SMPH_SPF) {
6111 page_len_offset = 2;
6115 page_len_offset = 1;
6119 * If the length the initiator gives us isn't the one we specify in
6120 * the mode page header, or if they didn't specify enough data in
6121 * the CDB to avoid truncating this page, kick out the request.
6123 if ((page_len != (page_index->page_len - page_len_offset -
6125 || (*len_left < page_index->page_len)) {
6128 ctl_set_invalid_field(ctsio,
6131 /*field*/ *len_used + page_len_offset,
6134 free(ctsio->kern_data_ptr, M_CTL);
6135 ctl_done((union ctl_io *)ctsio);
6136 return (CTL_RETVAL_COMPLETE);
6140 * Run through the mode page, checking to make sure that the bits
6141 * the user changed are actually legal for him to change.
6143 for (i = 0; i < page_index->page_len; i++) {
6144 uint8_t *user_byte, *change_mask, *current_byte;
6148 user_byte = (uint8_t *)page_header + i;
6149 change_mask = page_index->page_data +
6150 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6151 current_byte = page_index->page_data +
6152 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6155 * Check to see whether the user set any bits in this byte
6156 * that he is not allowed to set.
6158 if ((*user_byte & ~(*change_mask)) ==
6159 (*current_byte & ~(*change_mask)))
6163 * Go through bit by bit to determine which one is illegal.
6166 for (j = 7; j >= 0; j--) {
6167 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6168 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6173 ctl_set_invalid_field(ctsio,
6176 /*field*/ *len_used + i,
6179 free(ctsio->kern_data_ptr, M_CTL);
6180 ctl_done((union ctl_io *)ctsio);
6181 return (CTL_RETVAL_COMPLETE);
6185 * Decrement these before we call the page handler, since we may
6186 * end up getting called back one way or another before the handler
6187 * returns to this context.
6189 *len_left -= page_index->page_len;
6190 *len_used += page_index->page_len;
6192 retval = page_index->select_handler(ctsio, page_index,
6193 (uint8_t *)page_header);
6196 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6197 * wait until this queued command completes to finish processing
6198 * the mode page. If it returns anything other than
6199 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6200 * already set the sense information, freed the data pointer, and
6201 * completed the io for us.
6203 if (retval != CTL_RETVAL_COMPLETE)
6204 goto bailout_no_done;
6207 * If the initiator sent us more than one page, parse the next one.
6212 ctl_set_success(ctsio);
6213 free(ctsio->kern_data_ptr, M_CTL);
6214 ctl_done((union ctl_io *)ctsio);
6218 return (CTL_RETVAL_COMPLETE);
6223 ctl_mode_select(struct ctl_scsiio *ctsio)
6225 int param_len, pf, sp;
6226 int header_size, bd_len;
6227 union ctl_modepage_info *modepage_info;
6229 switch (ctsio->cdb[0]) {
6230 case MODE_SELECT_6: {
6231 struct scsi_mode_select_6 *cdb;
6233 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6235 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6236 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6237 param_len = cdb->length;
6238 header_size = sizeof(struct scsi_mode_header_6);
6241 case MODE_SELECT_10: {
6242 struct scsi_mode_select_10 *cdb;
6244 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6246 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6247 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6248 param_len = scsi_2btoul(cdb->length);
6249 header_size = sizeof(struct scsi_mode_header_10);
6253 ctl_set_invalid_opcode(ctsio);
6254 ctl_done((union ctl_io *)ctsio);
6255 return (CTL_RETVAL_COMPLETE);
6260 * "A parameter list length of zero indicates that the Data-Out Buffer
6261 * shall be empty. This condition shall not be considered as an error."
6263 if (param_len == 0) {
6264 ctl_set_success(ctsio);
6265 ctl_done((union ctl_io *)ctsio);
6266 return (CTL_RETVAL_COMPLETE);
6270 * Since we'll hit this the first time through, prior to
6271 * allocation, we don't need to free a data buffer here.
6273 if (param_len < header_size) {
6274 ctl_set_param_len_error(ctsio);
6275 ctl_done((union ctl_io *)ctsio);
6276 return (CTL_RETVAL_COMPLETE);
6280 * Allocate the data buffer and grab the user's data. In theory,
6281 * we shouldn't have to sanity check the parameter list length here
6282 * because the maximum size is 64K. We should be able to malloc
6283 * that much without too many problems.
6285 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6286 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6287 ctsio->kern_data_len = param_len;
6288 ctsio->kern_total_len = param_len;
6289 ctsio->kern_data_resid = 0;
6290 ctsio->kern_rel_offset = 0;
6291 ctsio->kern_sg_entries = 0;
6292 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6293 ctsio->be_move_done = ctl_config_move_done;
6294 ctl_datamove((union ctl_io *)ctsio);
6296 return (CTL_RETVAL_COMPLETE);
6299 switch (ctsio->cdb[0]) {
6300 case MODE_SELECT_6: {
6301 struct scsi_mode_header_6 *mh6;
6303 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6304 bd_len = mh6->blk_desc_len;
6307 case MODE_SELECT_10: {
6308 struct scsi_mode_header_10 *mh10;
6310 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6311 bd_len = scsi_2btoul(mh10->blk_desc_len);
6315 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6318 if (param_len < (header_size + bd_len)) {
6319 free(ctsio->kern_data_ptr, M_CTL);
6320 ctl_set_param_len_error(ctsio);
6321 ctl_done((union ctl_io *)ctsio);
6322 return (CTL_RETVAL_COMPLETE);
6326 * Set the IO_CONT flag, so that if this I/O gets passed to
6327 * ctl_config_write_done(), it'll get passed back to
6328 * ctl_do_mode_select() for further processing, or completion if
6331 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6332 ctsio->io_cont = ctl_do_mode_select;
6334 modepage_info = (union ctl_modepage_info *)
6335 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6336 memset(modepage_info, 0, sizeof(*modepage_info));
6337 modepage_info->header.len_left = param_len - header_size - bd_len;
6338 modepage_info->header.len_used = header_size + bd_len;
6340 return (ctl_do_mode_select((union ctl_io *)ctsio));
6344 ctl_mode_sense(struct ctl_scsiio *ctsio)
6346 struct ctl_lun *lun;
6347 int pc, page_code, dbd, llba, subpage;
6348 int alloc_len, page_len, header_len, total_len;
6349 struct scsi_mode_block_descr *block_desc;
6350 struct ctl_page_index *page_index;
6356 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6358 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6359 switch (ctsio->cdb[0]) {
6360 case MODE_SENSE_6: {
6361 struct scsi_mode_sense_6 *cdb;
6363 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6365 header_len = sizeof(struct scsi_mode_hdr_6);
6366 if (cdb->byte2 & SMS_DBD)
6369 header_len += sizeof(struct scsi_mode_block_descr);
6371 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6372 page_code = cdb->page & SMS_PAGE_CODE;
6373 subpage = cdb->subpage;
6374 alloc_len = cdb->length;
6377 case MODE_SENSE_10: {
6378 struct scsi_mode_sense_10 *cdb;
6380 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6382 header_len = sizeof(struct scsi_mode_hdr_10);
6384 if (cdb->byte2 & SMS_DBD)
6387 header_len += sizeof(struct scsi_mode_block_descr);
6388 if (cdb->byte2 & SMS10_LLBAA)
6390 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6391 page_code = cdb->page & SMS_PAGE_CODE;
6392 subpage = cdb->subpage;
6393 alloc_len = scsi_2btoul(cdb->length);
6397 ctl_set_invalid_opcode(ctsio);
6398 ctl_done((union ctl_io *)ctsio);
6399 return (CTL_RETVAL_COMPLETE);
6400 break; /* NOTREACHED */
6404 * We have to make a first pass through to calculate the size of
6405 * the pages that match the user's query. Then we allocate enough
6406 * memory to hold it, and actually copy the data into the buffer.
6408 switch (page_code) {
6409 case SMS_ALL_PAGES_PAGE: {
6415 * At the moment, values other than 0 and 0xff here are
6416 * reserved according to SPC-3.
6418 if ((subpage != SMS_SUBPAGE_PAGE_0)
6419 && (subpage != SMS_SUBPAGE_ALL)) {
6420 ctl_set_invalid_field(ctsio,
6426 ctl_done((union ctl_io *)ctsio);
6427 return (CTL_RETVAL_COMPLETE);
6430 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6431 page_index = &lun->mode_pages.index[i];
6433 /* Make sure the page is supported for this dev type */
6434 if (lun->be_lun->lun_type == T_DIRECT &&
6435 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6437 if (lun->be_lun->lun_type == T_PROCESSOR &&
6438 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6440 if (lun->be_lun->lun_type == T_CDROM &&
6441 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6445 * We don't use this subpage if the user didn't
6446 * request all subpages.
6448 if ((page_index->subpage != 0)
6449 && (subpage == SMS_SUBPAGE_PAGE_0))
6453 printf("found page %#x len %d\n",
6454 page_index->page_code & SMPH_PC_MASK,
6455 page_index->page_len);
6457 page_len += page_index->page_len;
6466 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6467 page_index = &lun->mode_pages.index[i];
6469 /* Make sure the page is supported for this dev type */
6470 if (lun->be_lun->lun_type == T_DIRECT &&
6471 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6473 if (lun->be_lun->lun_type == T_PROCESSOR &&
6474 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6476 if (lun->be_lun->lun_type == T_CDROM &&
6477 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6480 /* Look for the right page code */
6481 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6484 /* Look for the right subpage or the subpage wildcard*/
6485 if ((page_index->subpage != subpage)
6486 && (subpage != SMS_SUBPAGE_ALL))
6490 printf("found page %#x len %d\n",
6491 page_index->page_code & SMPH_PC_MASK,
6492 page_index->page_len);
6495 page_len += page_index->page_len;
6498 if (page_len == 0) {
6499 ctl_set_invalid_field(ctsio,
6505 ctl_done((union ctl_io *)ctsio);
6506 return (CTL_RETVAL_COMPLETE);
6512 total_len = header_len + page_len;
6514 printf("header_len = %d, page_len = %d, total_len = %d\n",
6515 header_len, page_len, total_len);
6518 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6519 ctsio->kern_sg_entries = 0;
6520 ctsio->kern_data_resid = 0;
6521 ctsio->kern_rel_offset = 0;
6522 if (total_len < alloc_len) {
6523 ctsio->residual = alloc_len - total_len;
6524 ctsio->kern_data_len = total_len;
6525 ctsio->kern_total_len = total_len;
6527 ctsio->residual = 0;
6528 ctsio->kern_data_len = alloc_len;
6529 ctsio->kern_total_len = alloc_len;
6532 switch (ctsio->cdb[0]) {
6533 case MODE_SENSE_6: {
6534 struct scsi_mode_hdr_6 *header;
6536 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6538 header->datalen = MIN(total_len - 1, 254);
6539 if (lun->be_lun->lun_type == T_DIRECT) {
6540 header->dev_specific = 0x10; /* DPOFUA */
6541 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6542 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6543 .eca_and_aen & SCP_SWP) != 0)
6544 header->dev_specific |= 0x80; /* WP */
6547 header->block_descr_len = 0;
6549 header->block_descr_len =
6550 sizeof(struct scsi_mode_block_descr);
6551 block_desc = (struct scsi_mode_block_descr *)&header[1];
6554 case MODE_SENSE_10: {
6555 struct scsi_mode_hdr_10 *header;
6558 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6560 datalen = MIN(total_len - 2, 65533);
6561 scsi_ulto2b(datalen, header->datalen);
6562 if (lun->be_lun->lun_type == T_DIRECT) {
6563 header->dev_specific = 0x10; /* DPOFUA */
6564 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6565 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6566 .eca_and_aen & SCP_SWP) != 0)
6567 header->dev_specific |= 0x80; /* WP */
6570 scsi_ulto2b(0, header->block_descr_len);
6572 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6573 header->block_descr_len);
6574 block_desc = (struct scsi_mode_block_descr *)&header[1];
6578 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6582 * If we've got a disk, use its blocksize in the block
6583 * descriptor. Otherwise, just set it to 0.
6586 if (lun->be_lun->lun_type == T_DIRECT)
6587 scsi_ulto3b(lun->be_lun->blocksize,
6588 block_desc->block_len);
6590 scsi_ulto3b(0, block_desc->block_len);
6593 switch (page_code) {
6594 case SMS_ALL_PAGES_PAGE: {
6597 data_used = header_len;
6598 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6599 struct ctl_page_index *page_index;
6601 page_index = &lun->mode_pages.index[i];
6602 if (lun->be_lun->lun_type == T_DIRECT &&
6603 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6605 if (lun->be_lun->lun_type == T_PROCESSOR &&
6606 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6608 if (lun->be_lun->lun_type == T_CDROM &&
6609 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6613 * We don't use this subpage if the user didn't
6614 * request all subpages. We already checked (above)
6615 * to make sure the user only specified a subpage
6616 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6618 if ((page_index->subpage != 0)
6619 && (subpage == SMS_SUBPAGE_PAGE_0))
6623 * Call the handler, if it exists, to update the
6624 * page to the latest values.
6626 if (page_index->sense_handler != NULL)
6627 page_index->sense_handler(ctsio, page_index,pc);
6629 memcpy(ctsio->kern_data_ptr + data_used,
6630 page_index->page_data +
6631 (page_index->page_len * pc),
6632 page_index->page_len);
6633 data_used += page_index->page_len;
6640 data_used = header_len;
6642 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6643 struct ctl_page_index *page_index;
6645 page_index = &lun->mode_pages.index[i];
6647 /* Look for the right page code */
6648 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6651 /* Look for the right subpage or the subpage wildcard*/
6652 if ((page_index->subpage != subpage)
6653 && (subpage != SMS_SUBPAGE_ALL))
6656 /* Make sure the page is supported for this dev type */
6657 if (lun->be_lun->lun_type == T_DIRECT &&
6658 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6660 if (lun->be_lun->lun_type == T_PROCESSOR &&
6661 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6663 if (lun->be_lun->lun_type == T_CDROM &&
6664 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6668 * Call the handler, if it exists, to update the
6669 * page to the latest values.
6671 if (page_index->sense_handler != NULL)
6672 page_index->sense_handler(ctsio, page_index,pc);
6674 memcpy(ctsio->kern_data_ptr + data_used,
6675 page_index->page_data +
6676 (page_index->page_len * pc),
6677 page_index->page_len);
6678 data_used += page_index->page_len;
6684 ctl_set_success(ctsio);
6685 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6686 ctsio->be_move_done = ctl_config_move_done;
6687 ctl_datamove((union ctl_io *)ctsio);
6688 return (CTL_RETVAL_COMPLETE);
6692 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6693 struct ctl_page_index *page_index,
6696 struct ctl_lun *lun;
6697 struct scsi_log_param_header *phdr;
6701 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6702 data = page_index->page_data;
6704 if (lun->backend->lun_attr != NULL &&
6705 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6707 phdr = (struct scsi_log_param_header *)data;
6708 scsi_ulto2b(0x0001, phdr->param_code);
6709 phdr->param_control = SLP_LBIN | SLP_LP;
6710 phdr->param_len = 8;
6711 data = (uint8_t *)(phdr + 1);
6712 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6713 data[4] = 0x02; /* per-pool */
6714 data += phdr->param_len;
6717 if (lun->backend->lun_attr != NULL &&
6718 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6720 phdr = (struct scsi_log_param_header *)data;
6721 scsi_ulto2b(0x0002, phdr->param_code);
6722 phdr->param_control = SLP_LBIN | SLP_LP;
6723 phdr->param_len = 8;
6724 data = (uint8_t *)(phdr + 1);
6725 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6726 data[4] = 0x01; /* per-LUN */
6727 data += phdr->param_len;
6730 if (lun->backend->lun_attr != NULL &&
6731 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6733 phdr = (struct scsi_log_param_header *)data;
6734 scsi_ulto2b(0x00f1, phdr->param_code);
6735 phdr->param_control = SLP_LBIN | SLP_LP;
6736 phdr->param_len = 8;
6737 data = (uint8_t *)(phdr + 1);
6738 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6739 data[4] = 0x02; /* per-pool */
6740 data += phdr->param_len;
6743 if (lun->backend->lun_attr != NULL &&
6744 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6746 phdr = (struct scsi_log_param_header *)data;
6747 scsi_ulto2b(0x00f2, phdr->param_code);
6748 phdr->param_control = SLP_LBIN | SLP_LP;
6749 phdr->param_len = 8;
6750 data = (uint8_t *)(phdr + 1);
6751 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6752 data[4] = 0x02; /* per-pool */
6753 data += phdr->param_len;
6756 page_index->page_len = data - page_index->page_data;
6761 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6762 struct ctl_page_index *page_index,
6765 struct ctl_lun *lun;
6766 struct stat_page *data;
6767 uint64_t rn, wn, rb, wb;
6768 struct bintime rt, wt;
6771 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6772 data = (struct stat_page *)page_index->page_data;
6774 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6775 data->sap.hdr.param_control = SLP_LBIN;
6776 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6777 sizeof(struct scsi_log_param_header);
6778 rn = wn = rb = wb = 0;
6781 for (i = 0; i < CTL_MAX_PORTS; i++) {
6782 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6783 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6784 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6785 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6786 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6787 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6789 scsi_u64to8b(rn, data->sap.read_num);
6790 scsi_u64to8b(wn, data->sap.write_num);
6791 if (lun->stats.blocksize > 0) {
6792 scsi_u64to8b(wb / lun->stats.blocksize,
6793 data->sap.recvieved_lba);
6794 scsi_u64to8b(rb / lun->stats.blocksize,
6795 data->sap.transmitted_lba);
6797 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6798 data->sap.read_int);
6799 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6800 data->sap.write_int);
6801 scsi_u64to8b(0, data->sap.weighted_num);
6802 scsi_u64to8b(0, data->sap.weighted_int);
6803 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6804 data->it.hdr.param_control = SLP_LBIN;
6805 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6806 sizeof(struct scsi_log_param_header);
6808 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6810 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6811 data->it.hdr.param_control = SLP_LBIN;
6812 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6813 sizeof(struct scsi_log_param_header);
6814 scsi_ulto4b(3, data->ti.exponent);
6815 scsi_ulto4b(1, data->ti.integer);
6817 page_index->page_len = sizeof(*data);
6822 ctl_log_sense(struct ctl_scsiio *ctsio)
6824 struct ctl_lun *lun;
6825 int i, pc, page_code, subpage;
6826 int alloc_len, total_len;
6827 struct ctl_page_index *page_index;
6828 struct scsi_log_sense *cdb;
6829 struct scsi_log_header *header;
6831 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6833 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6834 cdb = (struct scsi_log_sense *)ctsio->cdb;
6835 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6836 page_code = cdb->page & SLS_PAGE_CODE;
6837 subpage = cdb->subpage;
6838 alloc_len = scsi_2btoul(cdb->length);
6841 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6842 page_index = &lun->log_pages.index[i];
6844 /* Look for the right page code */
6845 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6848 /* Look for the right subpage or the subpage wildcard*/
6849 if (page_index->subpage != subpage)
6854 if (i >= CTL_NUM_LOG_PAGES) {
6855 ctl_set_invalid_field(ctsio,
6861 ctl_done((union ctl_io *)ctsio);
6862 return (CTL_RETVAL_COMPLETE);
6865 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6867 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6868 ctsio->kern_sg_entries = 0;
6869 ctsio->kern_data_resid = 0;
6870 ctsio->kern_rel_offset = 0;
6871 if (total_len < alloc_len) {
6872 ctsio->residual = alloc_len - total_len;
6873 ctsio->kern_data_len = total_len;
6874 ctsio->kern_total_len = total_len;
6876 ctsio->residual = 0;
6877 ctsio->kern_data_len = alloc_len;
6878 ctsio->kern_total_len = alloc_len;
6881 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6882 header->page = page_index->page_code;
6883 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6884 header->page |= SL_DS;
6885 if (page_index->subpage) {
6886 header->page |= SL_SPF;
6887 header->subpage = page_index->subpage;
6889 scsi_ulto2b(page_index->page_len, header->datalen);
6892 * Call the handler, if it exists, to update the
6893 * page to the latest values.
6895 if (page_index->sense_handler != NULL)
6896 page_index->sense_handler(ctsio, page_index, pc);
6898 memcpy(header + 1, page_index->page_data, page_index->page_len);
6900 ctl_set_success(ctsio);
6901 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6902 ctsio->be_move_done = ctl_config_move_done;
6903 ctl_datamove((union ctl_io *)ctsio);
6904 return (CTL_RETVAL_COMPLETE);
6908 ctl_read_capacity(struct ctl_scsiio *ctsio)
6910 struct scsi_read_capacity *cdb;
6911 struct scsi_read_capacity_data *data;
6912 struct ctl_lun *lun;
6915 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6917 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6919 lba = scsi_4btoul(cdb->addr);
6920 if (((cdb->pmi & SRC_PMI) == 0)
6922 ctl_set_invalid_field(/*ctsio*/ ctsio,
6928 ctl_done((union ctl_io *)ctsio);
6929 return (CTL_RETVAL_COMPLETE);
6932 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6934 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6935 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6936 ctsio->residual = 0;
6937 ctsio->kern_data_len = sizeof(*data);
6938 ctsio->kern_total_len = sizeof(*data);
6939 ctsio->kern_data_resid = 0;
6940 ctsio->kern_rel_offset = 0;
6941 ctsio->kern_sg_entries = 0;
6944 * If the maximum LBA is greater than 0xfffffffe, the user must
6945 * issue a SERVICE ACTION IN (16) command, with the read capacity
6946 * serivce action set.
6948 if (lun->be_lun->maxlba > 0xfffffffe)
6949 scsi_ulto4b(0xffffffff, data->addr);
6951 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6954 * XXX KDM this may not be 512 bytes...
6956 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6958 ctl_set_success(ctsio);
6959 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6960 ctsio->be_move_done = ctl_config_move_done;
6961 ctl_datamove((union ctl_io *)ctsio);
6962 return (CTL_RETVAL_COMPLETE);
6966 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6968 struct scsi_read_capacity_16 *cdb;
6969 struct scsi_read_capacity_data_long *data;
6970 struct ctl_lun *lun;
6974 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6976 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6978 alloc_len = scsi_4btoul(cdb->alloc_len);
6979 lba = scsi_8btou64(cdb->addr);
6981 if ((cdb->reladr & SRC16_PMI)
6983 ctl_set_invalid_field(/*ctsio*/ ctsio,
6989 ctl_done((union ctl_io *)ctsio);
6990 return (CTL_RETVAL_COMPLETE);
6993 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6995 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6996 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6998 if (sizeof(*data) < alloc_len) {
6999 ctsio->residual = alloc_len - sizeof(*data);
7000 ctsio->kern_data_len = sizeof(*data);
7001 ctsio->kern_total_len = sizeof(*data);
7003 ctsio->residual = 0;
7004 ctsio->kern_data_len = alloc_len;
7005 ctsio->kern_total_len = alloc_len;
7007 ctsio->kern_data_resid = 0;
7008 ctsio->kern_rel_offset = 0;
7009 ctsio->kern_sg_entries = 0;
7011 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7012 /* XXX KDM this may not be 512 bytes... */
7013 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7014 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7015 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7016 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7017 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7019 ctl_set_success(ctsio);
7020 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7021 ctsio->be_move_done = ctl_config_move_done;
7022 ctl_datamove((union ctl_io *)ctsio);
7023 return (CTL_RETVAL_COMPLETE);
7027 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7029 struct scsi_get_lba_status *cdb;
7030 struct scsi_get_lba_status_data *data;
7031 struct ctl_lun *lun;
7032 struct ctl_lba_len_flags *lbalen;
7034 uint32_t alloc_len, total_len;
7037 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7039 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7040 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7041 lba = scsi_8btou64(cdb->addr);
7042 alloc_len = scsi_4btoul(cdb->alloc_len);
7044 if (lba > lun->be_lun->maxlba) {
7045 ctl_set_lba_out_of_range(ctsio);
7046 ctl_done((union ctl_io *)ctsio);
7047 return (CTL_RETVAL_COMPLETE);
7050 total_len = sizeof(*data) + sizeof(data->descr[0]);
7051 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7052 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7054 if (total_len < alloc_len) {
7055 ctsio->residual = alloc_len - total_len;
7056 ctsio->kern_data_len = total_len;
7057 ctsio->kern_total_len = total_len;
7059 ctsio->residual = 0;
7060 ctsio->kern_data_len = alloc_len;
7061 ctsio->kern_total_len = alloc_len;
7063 ctsio->kern_data_resid = 0;
7064 ctsio->kern_rel_offset = 0;
7065 ctsio->kern_sg_entries = 0;
7067 /* Fill dummy data in case backend can't tell anything. */
7068 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7069 scsi_u64to8b(lba, data->descr[0].addr);
7070 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7071 data->descr[0].length);
7072 data->descr[0].status = 0; /* Mapped or unknown. */
7074 ctl_set_success(ctsio);
7075 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7076 ctsio->be_move_done = ctl_config_move_done;
7078 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7080 lbalen->len = total_len;
7082 retval = lun->backend->config_read((union ctl_io *)ctsio);
7083 return (CTL_RETVAL_COMPLETE);
7087 ctl_read_defect(struct ctl_scsiio *ctsio)
7089 struct scsi_read_defect_data_10 *ccb10;
7090 struct scsi_read_defect_data_12 *ccb12;
7091 struct scsi_read_defect_data_hdr_10 *data10;
7092 struct scsi_read_defect_data_hdr_12 *data12;
7093 uint32_t alloc_len, data_len;
7096 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7098 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7099 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7100 format = ccb10->format;
7101 alloc_len = scsi_2btoul(ccb10->alloc_length);
7102 data_len = sizeof(*data10);
7104 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7105 format = ccb12->format;
7106 alloc_len = scsi_4btoul(ccb12->alloc_length);
7107 data_len = sizeof(*data12);
7109 if (alloc_len == 0) {
7110 ctl_set_success(ctsio);
7111 ctl_done((union ctl_io *)ctsio);
7112 return (CTL_RETVAL_COMPLETE);
7115 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7116 if (data_len < alloc_len) {
7117 ctsio->residual = alloc_len - data_len;
7118 ctsio->kern_data_len = data_len;
7119 ctsio->kern_total_len = data_len;
7121 ctsio->residual = 0;
7122 ctsio->kern_data_len = alloc_len;
7123 ctsio->kern_total_len = alloc_len;
7125 ctsio->kern_data_resid = 0;
7126 ctsio->kern_rel_offset = 0;
7127 ctsio->kern_sg_entries = 0;
7129 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7130 data10 = (struct scsi_read_defect_data_hdr_10 *)
7131 ctsio->kern_data_ptr;
7132 data10->format = format;
7133 scsi_ulto2b(0, data10->length);
7135 data12 = (struct scsi_read_defect_data_hdr_12 *)
7136 ctsio->kern_data_ptr;
7137 data12->format = format;
7138 scsi_ulto2b(0, data12->generation);
7139 scsi_ulto4b(0, data12->length);
7142 ctl_set_success(ctsio);
7143 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7144 ctsio->be_move_done = ctl_config_move_done;
7145 ctl_datamove((union ctl_io *)ctsio);
7146 return (CTL_RETVAL_COMPLETE);
7150 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7152 struct scsi_maintenance_in *cdb;
7154 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7155 int num_ha_groups, num_target_ports, shared_group;
7156 struct ctl_lun *lun;
7157 struct ctl_softc *softc;
7158 struct ctl_port *port;
7159 struct scsi_target_group_data *rtg_ptr;
7160 struct scsi_target_group_data_extended *rtg_ext_ptr;
7161 struct scsi_target_port_group_descriptor *tpg_desc;
7163 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7165 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7166 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7167 softc = lun->ctl_softc;
7169 retval = CTL_RETVAL_COMPLETE;
7171 switch (cdb->byte2 & STG_PDF_MASK) {
7172 case STG_PDF_LENGTH:
7175 case STG_PDF_EXTENDED:
7179 ctl_set_invalid_field(/*ctsio*/ ctsio,
7185 ctl_done((union ctl_io *)ctsio);
7189 num_target_ports = 0;
7190 shared_group = (softc->is_single != 0);
7191 mtx_lock(&softc->ctl_lock);
7192 STAILQ_FOREACH(port, &softc->port_list, links) {
7193 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7195 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7198 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7201 mtx_unlock(&softc->ctl_lock);
7202 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7205 total_len = sizeof(struct scsi_target_group_data_extended);
7207 total_len = sizeof(struct scsi_target_group_data);
7208 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7209 (shared_group + num_ha_groups) +
7210 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7212 alloc_len = scsi_4btoul(cdb->length);
7214 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7216 ctsio->kern_sg_entries = 0;
7218 if (total_len < alloc_len) {
7219 ctsio->residual = alloc_len - total_len;
7220 ctsio->kern_data_len = total_len;
7221 ctsio->kern_total_len = total_len;
7223 ctsio->residual = 0;
7224 ctsio->kern_data_len = alloc_len;
7225 ctsio->kern_total_len = alloc_len;
7227 ctsio->kern_data_resid = 0;
7228 ctsio->kern_rel_offset = 0;
7231 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7232 ctsio->kern_data_ptr;
7233 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7234 rtg_ext_ptr->format_type = 0x10;
7235 rtg_ext_ptr->implicit_transition_time = 0;
7236 tpg_desc = &rtg_ext_ptr->groups[0];
7238 rtg_ptr = (struct scsi_target_group_data *)
7239 ctsio->kern_data_ptr;
7240 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7241 tpg_desc = &rtg_ptr->groups[0];
7244 mtx_lock(&softc->ctl_lock);
7245 pg = softc->port_min / softc->port_cnt;
7246 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7247 /* Some shelf is known to be primary. */
7248 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7249 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7250 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7251 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7252 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7253 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7255 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7256 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7257 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7260 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7263 /* No known primary shelf. */
7264 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7265 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7266 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7267 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7268 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7269 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7271 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7275 tpg_desc->pref_state = ts;
7276 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7277 TPG_U_SUP | TPG_T_SUP;
7278 scsi_ulto2b(1, tpg_desc->target_port_group);
7279 tpg_desc->status = TPG_IMPLICIT;
7281 STAILQ_FOREACH(port, &softc->port_list, links) {
7282 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7284 if (!softc->is_single &&
7285 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7287 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7289 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7290 relative_target_port_identifier);
7293 tpg_desc->target_port_count = pc;
7294 tpg_desc = (struct scsi_target_port_group_descriptor *)
7295 &tpg_desc->descriptors[pc];
7297 for (g = 0; g < num_ha_groups; g++) {
7298 tpg_desc->pref_state = (g == pg) ? ts : os;
7299 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7300 TPG_U_SUP | TPG_T_SUP;
7301 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7302 tpg_desc->status = TPG_IMPLICIT;
7304 STAILQ_FOREACH(port, &softc->port_list, links) {
7305 if (port->targ_port < g * softc->port_cnt ||
7306 port->targ_port >= (g + 1) * softc->port_cnt)
7308 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7310 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7312 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7314 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7315 relative_target_port_identifier);
7318 tpg_desc->target_port_count = pc;
7319 tpg_desc = (struct scsi_target_port_group_descriptor *)
7320 &tpg_desc->descriptors[pc];
7322 mtx_unlock(&softc->ctl_lock);
7324 ctl_set_success(ctsio);
7325 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7326 ctsio->be_move_done = ctl_config_move_done;
7327 ctl_datamove((union ctl_io *)ctsio);
7332 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7334 struct ctl_lun *lun;
7335 struct scsi_report_supported_opcodes *cdb;
7336 const struct ctl_cmd_entry *entry, *sentry;
7337 struct scsi_report_supported_opcodes_all *all;
7338 struct scsi_report_supported_opcodes_descr *descr;
7339 struct scsi_report_supported_opcodes_one *one;
7341 int alloc_len, total_len;
7342 int opcode, service_action, i, j, num;
7344 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7346 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7347 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7349 retval = CTL_RETVAL_COMPLETE;
7351 opcode = cdb->requested_opcode;
7352 service_action = scsi_2btoul(cdb->requested_service_action);
7353 switch (cdb->options & RSO_OPTIONS_MASK) {
7354 case RSO_OPTIONS_ALL:
7356 for (i = 0; i < 256; i++) {
7357 entry = &ctl_cmd_table[i];
7358 if (entry->flags & CTL_CMD_FLAG_SA5) {
7359 for (j = 0; j < 32; j++) {
7360 sentry = &((const struct ctl_cmd_entry *)
7362 if (ctl_cmd_applicable(
7363 lun->be_lun->lun_type, sentry))
7367 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7372 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7373 num * sizeof(struct scsi_report_supported_opcodes_descr);
7375 case RSO_OPTIONS_OC:
7376 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7377 ctl_set_invalid_field(/*ctsio*/ ctsio,
7383 ctl_done((union ctl_io *)ctsio);
7384 return (CTL_RETVAL_COMPLETE);
7386 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7388 case RSO_OPTIONS_OC_SA:
7389 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7390 service_action >= 32) {
7391 ctl_set_invalid_field(/*ctsio*/ ctsio,
7397 ctl_done((union ctl_io *)ctsio);
7398 return (CTL_RETVAL_COMPLETE);
7400 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7403 ctl_set_invalid_field(/*ctsio*/ ctsio,
7409 ctl_done((union ctl_io *)ctsio);
7410 return (CTL_RETVAL_COMPLETE);
7413 alloc_len = scsi_4btoul(cdb->length);
7415 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7417 ctsio->kern_sg_entries = 0;
7419 if (total_len < alloc_len) {
7420 ctsio->residual = alloc_len - total_len;
7421 ctsio->kern_data_len = total_len;
7422 ctsio->kern_total_len = total_len;
7424 ctsio->residual = 0;
7425 ctsio->kern_data_len = alloc_len;
7426 ctsio->kern_total_len = alloc_len;
7428 ctsio->kern_data_resid = 0;
7429 ctsio->kern_rel_offset = 0;
7431 switch (cdb->options & RSO_OPTIONS_MASK) {
7432 case RSO_OPTIONS_ALL:
7433 all = (struct scsi_report_supported_opcodes_all *)
7434 ctsio->kern_data_ptr;
7436 for (i = 0; i < 256; i++) {
7437 entry = &ctl_cmd_table[i];
7438 if (entry->flags & CTL_CMD_FLAG_SA5) {
7439 for (j = 0; j < 32; j++) {
7440 sentry = &((const struct ctl_cmd_entry *)
7442 if (!ctl_cmd_applicable(
7443 lun->be_lun->lun_type, sentry))
7445 descr = &all->descr[num++];
7447 scsi_ulto2b(j, descr->service_action);
7448 descr->flags = RSO_SERVACTV;
7449 scsi_ulto2b(sentry->length,
7453 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7456 descr = &all->descr[num++];
7458 scsi_ulto2b(0, descr->service_action);
7460 scsi_ulto2b(entry->length, descr->cdb_length);
7464 num * sizeof(struct scsi_report_supported_opcodes_descr),
7467 case RSO_OPTIONS_OC:
7468 one = (struct scsi_report_supported_opcodes_one *)
7469 ctsio->kern_data_ptr;
7470 entry = &ctl_cmd_table[opcode];
7472 case RSO_OPTIONS_OC_SA:
7473 one = (struct scsi_report_supported_opcodes_one *)
7474 ctsio->kern_data_ptr;
7475 entry = &ctl_cmd_table[opcode];
7476 entry = &((const struct ctl_cmd_entry *)
7477 entry->execute)[service_action];
7479 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7481 scsi_ulto2b(entry->length, one->cdb_length);
7482 one->cdb_usage[0] = opcode;
7483 memcpy(&one->cdb_usage[1], entry->usage,
7490 ctl_set_success(ctsio);
7491 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7492 ctsio->be_move_done = ctl_config_move_done;
7493 ctl_datamove((union ctl_io *)ctsio);
7498 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7500 struct scsi_report_supported_tmf *cdb;
7501 struct scsi_report_supported_tmf_data *data;
7503 int alloc_len, total_len;
7505 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7507 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7509 retval = CTL_RETVAL_COMPLETE;
7511 total_len = sizeof(struct scsi_report_supported_tmf_data);
7512 alloc_len = scsi_4btoul(cdb->length);
7514 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7516 ctsio->kern_sg_entries = 0;
7518 if (total_len < alloc_len) {
7519 ctsio->residual = alloc_len - total_len;
7520 ctsio->kern_data_len = total_len;
7521 ctsio->kern_total_len = total_len;
7523 ctsio->residual = 0;
7524 ctsio->kern_data_len = alloc_len;
7525 ctsio->kern_total_len = alloc_len;
7527 ctsio->kern_data_resid = 0;
7528 ctsio->kern_rel_offset = 0;
7530 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7531 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7533 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7535 ctl_set_success(ctsio);
7536 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7537 ctsio->be_move_done = ctl_config_move_done;
7538 ctl_datamove((union ctl_io *)ctsio);
7543 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7545 struct scsi_report_timestamp *cdb;
7546 struct scsi_report_timestamp_data *data;
7550 int alloc_len, total_len;
7552 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7554 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7556 retval = CTL_RETVAL_COMPLETE;
7558 total_len = sizeof(struct scsi_report_timestamp_data);
7559 alloc_len = scsi_4btoul(cdb->length);
7561 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7563 ctsio->kern_sg_entries = 0;
7565 if (total_len < alloc_len) {
7566 ctsio->residual = alloc_len - total_len;
7567 ctsio->kern_data_len = total_len;
7568 ctsio->kern_total_len = total_len;
7570 ctsio->residual = 0;
7571 ctsio->kern_data_len = alloc_len;
7572 ctsio->kern_total_len = alloc_len;
7574 ctsio->kern_data_resid = 0;
7575 ctsio->kern_rel_offset = 0;
7577 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7578 scsi_ulto2b(sizeof(*data) - 2, data->length);
7579 data->origin = RTS_ORIG_OUTSIDE;
7581 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7582 scsi_ulto4b(timestamp >> 16, data->timestamp);
7583 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7585 ctl_set_success(ctsio);
7586 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7587 ctsio->be_move_done = ctl_config_move_done;
7588 ctl_datamove((union ctl_io *)ctsio);
7593 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7595 struct scsi_per_res_in *cdb;
7596 int alloc_len, total_len = 0;
7597 /* struct scsi_per_res_in_rsrv in_data; */
7598 struct ctl_lun *lun;
7599 struct ctl_softc *softc;
7602 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7604 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7606 alloc_len = scsi_2btoul(cdb->length);
7608 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7609 softc = lun->ctl_softc;
7612 mtx_lock(&lun->lun_lock);
7613 switch (cdb->action) {
7614 case SPRI_RK: /* read keys */
7615 total_len = sizeof(struct scsi_per_res_in_keys) +
7617 sizeof(struct scsi_per_res_key);
7619 case SPRI_RR: /* read reservation */
7620 if (lun->flags & CTL_LUN_PR_RESERVED)
7621 total_len = sizeof(struct scsi_per_res_in_rsrv);
7623 total_len = sizeof(struct scsi_per_res_in_header);
7625 case SPRI_RC: /* report capabilities */
7626 total_len = sizeof(struct scsi_per_res_cap);
7628 case SPRI_RS: /* read full status */
7629 total_len = sizeof(struct scsi_per_res_in_header) +
7630 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7634 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7636 mtx_unlock(&lun->lun_lock);
7638 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7640 if (total_len < alloc_len) {
7641 ctsio->residual = alloc_len - total_len;
7642 ctsio->kern_data_len = total_len;
7643 ctsio->kern_total_len = total_len;
7645 ctsio->residual = 0;
7646 ctsio->kern_data_len = alloc_len;
7647 ctsio->kern_total_len = alloc_len;
7650 ctsio->kern_data_resid = 0;
7651 ctsio->kern_rel_offset = 0;
7652 ctsio->kern_sg_entries = 0;
7654 mtx_lock(&lun->lun_lock);
7655 switch (cdb->action) {
7656 case SPRI_RK: { // read keys
7657 struct scsi_per_res_in_keys *res_keys;
7660 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7663 * We had to drop the lock to allocate our buffer, which
7664 * leaves time for someone to come in with another
7665 * persistent reservation. (That is unlikely, though,
7666 * since this should be the only persistent reservation
7667 * command active right now.)
7669 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7670 (lun->pr_key_count *
7671 sizeof(struct scsi_per_res_key)))){
7672 mtx_unlock(&lun->lun_lock);
7673 free(ctsio->kern_data_ptr, M_CTL);
7674 printf("%s: reservation length changed, retrying\n",
7679 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7681 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7682 lun->pr_key_count, res_keys->header.length);
7684 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7685 if ((key = ctl_get_prkey(lun, i)) == 0)
7689 * We used lun->pr_key_count to calculate the
7690 * size to allocate. If it turns out the number of
7691 * initiators with the registered flag set is
7692 * larger than that (i.e. they haven't been kept in
7693 * sync), we've got a problem.
7695 if (key_count >= lun->pr_key_count) {
7699 scsi_u64to8b(key, res_keys->keys[key_count].key);
7704 case SPRI_RR: { // read reservation
7705 struct scsi_per_res_in_rsrv *res;
7706 int tmp_len, header_only;
7708 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7710 scsi_ulto4b(lun->pr_generation, res->header.generation);
7712 if (lun->flags & CTL_LUN_PR_RESERVED)
7714 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7715 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7716 res->header.length);
7719 tmp_len = sizeof(struct scsi_per_res_in_header);
7720 scsi_ulto4b(0, res->header.length);
7725 * We had to drop the lock to allocate our buffer, which
7726 * leaves time for someone to come in with another
7727 * persistent reservation. (That is unlikely, though,
7728 * since this should be the only persistent reservation
7729 * command active right now.)
7731 if (tmp_len != total_len) {
7732 mtx_unlock(&lun->lun_lock);
7733 free(ctsio->kern_data_ptr, M_CTL);
7734 printf("%s: reservation status changed, retrying\n",
7740 * No reservation held, so we're done.
7742 if (header_only != 0)
7746 * If the registration is an All Registrants type, the key
7747 * is 0, since it doesn't really matter.
7749 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7750 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7751 res->data.reservation);
7753 res->data.scopetype = lun->pr_res_type;
7756 case SPRI_RC: //report capabilities
7758 struct scsi_per_res_cap *res_cap;
7761 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7762 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7763 res_cap->flags1 = SPRI_CRH;
7764 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7765 type_mask = SPRI_TM_WR_EX_AR |
7771 scsi_ulto2b(type_mask, res_cap->type_mask);
7774 case SPRI_RS: { // read full status
7775 struct scsi_per_res_in_full *res_status;
7776 struct scsi_per_res_in_full_desc *res_desc;
7777 struct ctl_port *port;
7780 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7783 * We had to drop the lock to allocate our buffer, which
7784 * leaves time for someone to come in with another
7785 * persistent reservation. (That is unlikely, though,
7786 * since this should be the only persistent reservation
7787 * command active right now.)
7789 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7790 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7791 lun->pr_key_count)){
7792 mtx_unlock(&lun->lun_lock);
7793 free(ctsio->kern_data_ptr, M_CTL);
7794 printf("%s: reservation length changed, retrying\n",
7799 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7801 res_desc = &res_status->desc[0];
7802 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7803 if ((key = ctl_get_prkey(lun, i)) == 0)
7806 scsi_u64to8b(key, res_desc->res_key.key);
7807 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7808 (lun->pr_res_idx == i ||
7809 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7810 res_desc->flags = SPRI_FULL_R_HOLDER;
7811 res_desc->scopetype = lun->pr_res_type;
7813 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7814 res_desc->rel_trgt_port_id);
7816 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7818 len = ctl_create_iid(port,
7819 i % CTL_MAX_INIT_PER_PORT,
7820 res_desc->transport_id);
7821 scsi_ulto4b(len, res_desc->additional_length);
7822 res_desc = (struct scsi_per_res_in_full_desc *)
7823 &res_desc->transport_id[len];
7825 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7826 res_status->header.length);
7830 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7832 mtx_unlock(&lun->lun_lock);
7834 ctl_set_success(ctsio);
7835 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7836 ctsio->be_move_done = ctl_config_move_done;
7837 ctl_datamove((union ctl_io *)ctsio);
7838 return (CTL_RETVAL_COMPLETE);
7842 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7846 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7847 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7848 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7849 struct scsi_per_res_out_parms* param)
7851 union ctl_ha_msg persis_io;
7854 mtx_lock(&lun->lun_lock);
7855 if (sa_res_key == 0) {
7856 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7857 /* validate scope and type */
7858 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7860 mtx_unlock(&lun->lun_lock);
7861 ctl_set_invalid_field(/*ctsio*/ ctsio,
7867 ctl_done((union ctl_io *)ctsio);
7871 if (type>8 || type==2 || type==4 || type==0) {
7872 mtx_unlock(&lun->lun_lock);
7873 ctl_set_invalid_field(/*ctsio*/ ctsio,
7879 ctl_done((union ctl_io *)ctsio);
7884 * Unregister everybody else and build UA for
7887 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7888 if (i == residx || ctl_get_prkey(lun, i) == 0)
7891 ctl_clr_prkey(lun, i);
7892 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7894 lun->pr_key_count = 1;
7895 lun->pr_res_type = type;
7896 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7897 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7898 lun->pr_res_idx = residx;
7899 lun->pr_generation++;
7900 mtx_unlock(&lun->lun_lock);
7902 /* send msg to other side */
7903 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7904 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7905 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7906 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7907 persis_io.pr.pr_info.res_type = type;
7908 memcpy(persis_io.pr.pr_info.sa_res_key,
7909 param->serv_act_res_key,
7910 sizeof(param->serv_act_res_key));
7911 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7912 sizeof(persis_io.pr), M_WAITOK);
7914 /* not all registrants */
7915 mtx_unlock(&lun->lun_lock);
7916 free(ctsio->kern_data_ptr, M_CTL);
7917 ctl_set_invalid_field(ctsio,
7923 ctl_done((union ctl_io *)ctsio);
7926 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7927 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7930 if (res_key == sa_res_key) {
7933 * The spec implies this is not good but doesn't
7934 * say what to do. There are two choices either
7935 * generate a res conflict or check condition
7936 * with illegal field in parameter data. Since
7937 * that is what is done when the sa_res_key is
7938 * zero I'll take that approach since this has
7939 * to do with the sa_res_key.
7941 mtx_unlock(&lun->lun_lock);
7942 free(ctsio->kern_data_ptr, M_CTL);
7943 ctl_set_invalid_field(ctsio,
7949 ctl_done((union ctl_io *)ctsio);
7953 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7954 if (ctl_get_prkey(lun, i) != sa_res_key)
7958 ctl_clr_prkey(lun, i);
7959 lun->pr_key_count--;
7960 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7963 mtx_unlock(&lun->lun_lock);
7964 free(ctsio->kern_data_ptr, M_CTL);
7965 ctl_set_reservation_conflict(ctsio);
7966 ctl_done((union ctl_io *)ctsio);
7967 return (CTL_RETVAL_COMPLETE);
7969 lun->pr_generation++;
7970 mtx_unlock(&lun->lun_lock);
7972 /* send msg to other side */
7973 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7974 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7975 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7976 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7977 persis_io.pr.pr_info.res_type = type;
7978 memcpy(persis_io.pr.pr_info.sa_res_key,
7979 param->serv_act_res_key,
7980 sizeof(param->serv_act_res_key));
7981 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7982 sizeof(persis_io.pr), M_WAITOK);
7984 /* Reserved but not all registrants */
7985 /* sa_res_key is res holder */
7986 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7987 /* validate scope and type */
7988 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7990 mtx_unlock(&lun->lun_lock);
7991 ctl_set_invalid_field(/*ctsio*/ ctsio,
7997 ctl_done((union ctl_io *)ctsio);
8001 if (type>8 || type==2 || type==4 || type==0) {
8002 mtx_unlock(&lun->lun_lock);
8003 ctl_set_invalid_field(/*ctsio*/ ctsio,
8009 ctl_done((union ctl_io *)ctsio);
8015 * if sa_res_key != res_key remove all
8016 * registrants w/sa_res_key and generate UA
8017 * for these registrants(Registrations
8018 * Preempted) if it wasn't an exclusive
8019 * reservation generate UA(Reservations
8020 * Preempted) for all other registered nexuses
8021 * if the type has changed. Establish the new
8022 * reservation and holder. If res_key and
8023 * sa_res_key are the same do the above
8024 * except don't unregister the res holder.
8027 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8028 if (i == residx || ctl_get_prkey(lun, i) == 0)
8031 if (sa_res_key == ctl_get_prkey(lun, i)) {
8032 ctl_clr_prkey(lun, i);
8033 lun->pr_key_count--;
8034 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8035 } else if (type != lun->pr_res_type &&
8036 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8037 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8038 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8041 lun->pr_res_type = type;
8042 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8043 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8044 lun->pr_res_idx = residx;
8046 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8047 lun->pr_generation++;
8048 mtx_unlock(&lun->lun_lock);
8050 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8051 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8052 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8053 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8054 persis_io.pr.pr_info.res_type = type;
8055 memcpy(persis_io.pr.pr_info.sa_res_key,
8056 param->serv_act_res_key,
8057 sizeof(param->serv_act_res_key));
8058 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8059 sizeof(persis_io.pr), M_WAITOK);
8062 * sa_res_key is not the res holder just
8063 * remove registrants
8067 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8068 if (sa_res_key != ctl_get_prkey(lun, i))
8072 ctl_clr_prkey(lun, i);
8073 lun->pr_key_count--;
8074 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8078 mtx_unlock(&lun->lun_lock);
8079 free(ctsio->kern_data_ptr, M_CTL);
8080 ctl_set_reservation_conflict(ctsio);
8081 ctl_done((union ctl_io *)ctsio);
8084 lun->pr_generation++;
8085 mtx_unlock(&lun->lun_lock);
8087 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8088 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8089 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8090 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8091 persis_io.pr.pr_info.res_type = type;
8092 memcpy(persis_io.pr.pr_info.sa_res_key,
8093 param->serv_act_res_key,
8094 sizeof(param->serv_act_res_key));
8095 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8096 sizeof(persis_io.pr), M_WAITOK);
8103 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8105 uint64_t sa_res_key;
8108 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8110 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8111 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8112 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8113 if (sa_res_key == 0) {
8115 * Unregister everybody else and build UA for
8118 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8119 if (i == msg->pr.pr_info.residx ||
8120 ctl_get_prkey(lun, i) == 0)
8123 ctl_clr_prkey(lun, i);
8124 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8127 lun->pr_key_count = 1;
8128 lun->pr_res_type = msg->pr.pr_info.res_type;
8129 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8130 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8131 lun->pr_res_idx = msg->pr.pr_info.residx;
8133 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8134 if (sa_res_key == ctl_get_prkey(lun, i))
8137 ctl_clr_prkey(lun, i);
8138 lun->pr_key_count--;
8139 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8143 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8144 if (i == msg->pr.pr_info.residx ||
8145 ctl_get_prkey(lun, i) == 0)
8148 if (sa_res_key == ctl_get_prkey(lun, i)) {
8149 ctl_clr_prkey(lun, i);
8150 lun->pr_key_count--;
8151 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8152 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8153 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8154 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8155 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8158 lun->pr_res_type = msg->pr.pr_info.res_type;
8159 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8160 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8161 lun->pr_res_idx = msg->pr.pr_info.residx;
8163 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8165 lun->pr_generation++;
8171 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8174 u_int32_t param_len;
8175 struct scsi_per_res_out *cdb;
8176 struct ctl_lun *lun;
8177 struct scsi_per_res_out_parms* param;
8178 struct ctl_softc *softc;
8180 uint64_t res_key, sa_res_key, key;
8182 union ctl_ha_msg persis_io;
8185 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8187 retval = CTL_RETVAL_COMPLETE;
8189 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8190 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8191 softc = lun->ctl_softc;
8194 * We only support whole-LUN scope. The scope & type are ignored for
8195 * register, register and ignore existing key and clear.
8196 * We sometimes ignore scope and type on preempts too!!
8197 * Verify reservation type here as well.
8199 type = cdb->scope_type & SPR_TYPE_MASK;
8200 if ((cdb->action == SPRO_RESERVE)
8201 || (cdb->action == SPRO_RELEASE)) {
8202 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8203 ctl_set_invalid_field(/*ctsio*/ ctsio,
8209 ctl_done((union ctl_io *)ctsio);
8210 return (CTL_RETVAL_COMPLETE);
8213 if (type>8 || type==2 || type==4 || type==0) {
8214 ctl_set_invalid_field(/*ctsio*/ ctsio,
8220 ctl_done((union ctl_io *)ctsio);
8221 return (CTL_RETVAL_COMPLETE);
8225 param_len = scsi_4btoul(cdb->length);
8227 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8228 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8229 ctsio->kern_data_len = param_len;
8230 ctsio->kern_total_len = param_len;
8231 ctsio->kern_data_resid = 0;
8232 ctsio->kern_rel_offset = 0;
8233 ctsio->kern_sg_entries = 0;
8234 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8235 ctsio->be_move_done = ctl_config_move_done;
8236 ctl_datamove((union ctl_io *)ctsio);
8238 return (CTL_RETVAL_COMPLETE);
8241 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8243 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8244 res_key = scsi_8btou64(param->res_key.key);
8245 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8248 * Validate the reservation key here except for SPRO_REG_IGNO
8249 * This must be done for all other service actions
8251 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8252 mtx_lock(&lun->lun_lock);
8253 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8254 if (res_key != key) {
8256 * The current key passed in doesn't match
8257 * the one the initiator previously
8260 mtx_unlock(&lun->lun_lock);
8261 free(ctsio->kern_data_ptr, M_CTL);
8262 ctl_set_reservation_conflict(ctsio);
8263 ctl_done((union ctl_io *)ctsio);
8264 return (CTL_RETVAL_COMPLETE);
8266 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8268 * We are not registered
8270 mtx_unlock(&lun->lun_lock);
8271 free(ctsio->kern_data_ptr, M_CTL);
8272 ctl_set_reservation_conflict(ctsio);
8273 ctl_done((union ctl_io *)ctsio);
8274 return (CTL_RETVAL_COMPLETE);
8275 } else if (res_key != 0) {
8277 * We are not registered and trying to register but
8278 * the register key isn't zero.
8280 mtx_unlock(&lun->lun_lock);
8281 free(ctsio->kern_data_ptr, M_CTL);
8282 ctl_set_reservation_conflict(ctsio);
8283 ctl_done((union ctl_io *)ctsio);
8284 return (CTL_RETVAL_COMPLETE);
8286 mtx_unlock(&lun->lun_lock);
8289 switch (cdb->action & SPRO_ACTION_MASK) {
8291 case SPRO_REG_IGNO: {
8294 printf("Registration received\n");
8298 * We don't support any of these options, as we report in
8299 * the read capabilities request (see
8300 * ctl_persistent_reserve_in(), above).
8302 if ((param->flags & SPR_SPEC_I_PT)
8303 || (param->flags & SPR_ALL_TG_PT)
8304 || (param->flags & SPR_APTPL)) {
8307 if (param->flags & SPR_APTPL)
8309 else if (param->flags & SPR_ALL_TG_PT)
8311 else /* SPR_SPEC_I_PT */
8314 free(ctsio->kern_data_ptr, M_CTL);
8315 ctl_set_invalid_field(ctsio,
8321 ctl_done((union ctl_io *)ctsio);
8322 return (CTL_RETVAL_COMPLETE);
8325 mtx_lock(&lun->lun_lock);
8328 * The initiator wants to clear the
8331 if (sa_res_key == 0) {
8333 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8334 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8335 && ctl_get_prkey(lun, residx) == 0)) {
8336 mtx_unlock(&lun->lun_lock);
8340 ctl_clr_prkey(lun, residx);
8341 lun->pr_key_count--;
8343 if (residx == lun->pr_res_idx) {
8344 lun->flags &= ~CTL_LUN_PR_RESERVED;
8345 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8347 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8348 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8349 lun->pr_key_count) {
8351 * If the reservation is a registrants
8352 * only type we need to generate a UA
8353 * for other registered inits. The
8354 * sense code should be RESERVATIONS
8358 for (i = softc->init_min; i < softc->init_max; i++){
8359 if (ctl_get_prkey(lun, i) == 0)
8362 CTL_UA_RES_RELEASE);
8365 lun->pr_res_type = 0;
8366 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8367 if (lun->pr_key_count==0) {
8368 lun->flags &= ~CTL_LUN_PR_RESERVED;
8369 lun->pr_res_type = 0;
8370 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8373 lun->pr_generation++;
8374 mtx_unlock(&lun->lun_lock);
8376 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8377 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8378 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8379 persis_io.pr.pr_info.residx = residx;
8380 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8381 sizeof(persis_io.pr), M_WAITOK);
8382 } else /* sa_res_key != 0 */ {
8385 * If we aren't registered currently then increment
8386 * the key count and set the registered flag.
8388 ctl_alloc_prkey(lun, residx);
8389 if (ctl_get_prkey(lun, residx) == 0)
8390 lun->pr_key_count++;
8391 ctl_set_prkey(lun, residx, sa_res_key);
8392 lun->pr_generation++;
8393 mtx_unlock(&lun->lun_lock);
8395 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8396 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8397 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8398 persis_io.pr.pr_info.residx = residx;
8399 memcpy(persis_io.pr.pr_info.sa_res_key,
8400 param->serv_act_res_key,
8401 sizeof(param->serv_act_res_key));
8402 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8403 sizeof(persis_io.pr), M_WAITOK);
8410 printf("Reserve executed type %d\n", type);
8412 mtx_lock(&lun->lun_lock);
8413 if (lun->flags & CTL_LUN_PR_RESERVED) {
8415 * if this isn't the reservation holder and it's
8416 * not a "all registrants" type or if the type is
8417 * different then we have a conflict
8419 if ((lun->pr_res_idx != residx
8420 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8421 || lun->pr_res_type != type) {
8422 mtx_unlock(&lun->lun_lock);
8423 free(ctsio->kern_data_ptr, M_CTL);
8424 ctl_set_reservation_conflict(ctsio);
8425 ctl_done((union ctl_io *)ctsio);
8426 return (CTL_RETVAL_COMPLETE);
8428 mtx_unlock(&lun->lun_lock);
8429 } else /* create a reservation */ {
8431 * If it's not an "all registrants" type record
8432 * reservation holder
8434 if (type != SPR_TYPE_WR_EX_AR
8435 && type != SPR_TYPE_EX_AC_AR)
8436 lun->pr_res_idx = residx; /* Res holder */
8438 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8440 lun->flags |= CTL_LUN_PR_RESERVED;
8441 lun->pr_res_type = type;
8443 mtx_unlock(&lun->lun_lock);
8445 /* send msg to other side */
8446 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8447 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8448 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8449 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8450 persis_io.pr.pr_info.res_type = type;
8451 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8452 sizeof(persis_io.pr), M_WAITOK);
8457 mtx_lock(&lun->lun_lock);
8458 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8459 /* No reservation exists return good status */
8460 mtx_unlock(&lun->lun_lock);
8464 * Is this nexus a reservation holder?
8466 if (lun->pr_res_idx != residx
8467 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8469 * not a res holder return good status but
8472 mtx_unlock(&lun->lun_lock);
8476 if (lun->pr_res_type != type) {
8477 mtx_unlock(&lun->lun_lock);
8478 free(ctsio->kern_data_ptr, M_CTL);
8479 ctl_set_illegal_pr_release(ctsio);
8480 ctl_done((union ctl_io *)ctsio);
8481 return (CTL_RETVAL_COMPLETE);
8484 /* okay to release */
8485 lun->flags &= ~CTL_LUN_PR_RESERVED;
8486 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8487 lun->pr_res_type = 0;
8490 * if this isn't an exclusive access
8491 * res generate UA for all other
8494 if (type != SPR_TYPE_EX_AC
8495 && type != SPR_TYPE_WR_EX) {
8496 for (i = softc->init_min; i < softc->init_max; i++) {
8497 if (i == residx || ctl_get_prkey(lun, i) == 0)
8499 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8502 mtx_unlock(&lun->lun_lock);
8504 /* Send msg to other side */
8505 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8506 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8507 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8508 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8509 sizeof(persis_io.pr), M_WAITOK);
8513 /* send msg to other side */
8515 mtx_lock(&lun->lun_lock);
8516 lun->flags &= ~CTL_LUN_PR_RESERVED;
8517 lun->pr_res_type = 0;
8518 lun->pr_key_count = 0;
8519 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8521 ctl_clr_prkey(lun, residx);
8522 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8523 if (ctl_get_prkey(lun, i) != 0) {
8524 ctl_clr_prkey(lun, i);
8525 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8527 lun->pr_generation++;
8528 mtx_unlock(&lun->lun_lock);
8530 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8531 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8532 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8533 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8534 sizeof(persis_io.pr), M_WAITOK);
8538 case SPRO_PRE_ABO: {
8541 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8542 residx, ctsio, cdb, param);
8544 return (CTL_RETVAL_COMPLETE);
8548 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8552 free(ctsio->kern_data_ptr, M_CTL);
8553 ctl_set_success(ctsio);
8554 ctl_done((union ctl_io *)ctsio);
8560 * This routine is for handling a message from the other SC pertaining to
8561 * persistent reserve out. All the error checking will have been done
8562 * so only perorming the action need be done here to keep the two
8566 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8568 struct ctl_softc *softc = control_softc;
8569 struct ctl_lun *lun;
8571 uint32_t residx, targ_lun;
8573 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8574 mtx_lock(&softc->ctl_lock);
8575 if ((targ_lun >= CTL_MAX_LUNS) ||
8576 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8577 mtx_unlock(&softc->ctl_lock);
8580 mtx_lock(&lun->lun_lock);
8581 mtx_unlock(&softc->ctl_lock);
8582 if (lun->flags & CTL_LUN_DISABLED) {
8583 mtx_unlock(&lun->lun_lock);
8586 residx = ctl_get_initindex(&msg->hdr.nexus);
8587 switch(msg->pr.pr_info.action) {
8588 case CTL_PR_REG_KEY:
8589 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8590 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8591 lun->pr_key_count++;
8592 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8593 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8594 lun->pr_generation++;
8597 case CTL_PR_UNREG_KEY:
8598 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8599 lun->pr_key_count--;
8601 /* XXX Need to see if the reservation has been released */
8602 /* if so do we need to generate UA? */
8603 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8604 lun->flags &= ~CTL_LUN_PR_RESERVED;
8605 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8607 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8608 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8609 lun->pr_key_count) {
8611 * If the reservation is a registrants
8612 * only type we need to generate a UA
8613 * for other registered inits. The
8614 * sense code should be RESERVATIONS
8618 for (i = softc->init_min; i < softc->init_max; i++) {
8619 if (ctl_get_prkey(lun, i) == 0)
8622 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8625 lun->pr_res_type = 0;
8626 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8627 if (lun->pr_key_count==0) {
8628 lun->flags &= ~CTL_LUN_PR_RESERVED;
8629 lun->pr_res_type = 0;
8630 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8633 lun->pr_generation++;
8636 case CTL_PR_RESERVE:
8637 lun->flags |= CTL_LUN_PR_RESERVED;
8638 lun->pr_res_type = msg->pr.pr_info.res_type;
8639 lun->pr_res_idx = msg->pr.pr_info.residx;
8643 case CTL_PR_RELEASE:
8645 * if this isn't an exclusive access res generate UA for all
8646 * other registrants.
8648 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8649 lun->pr_res_type != SPR_TYPE_WR_EX) {
8650 for (i = softc->init_min; i < softc->init_max; i++)
8651 if (i == residx || ctl_get_prkey(lun, i) == 0)
8653 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8656 lun->flags &= ~CTL_LUN_PR_RESERVED;
8657 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8658 lun->pr_res_type = 0;
8661 case CTL_PR_PREEMPT:
8662 ctl_pro_preempt_other(lun, msg);
8665 lun->flags &= ~CTL_LUN_PR_RESERVED;
8666 lun->pr_res_type = 0;
8667 lun->pr_key_count = 0;
8668 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8670 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8671 if (ctl_get_prkey(lun, i) == 0)
8673 ctl_clr_prkey(lun, i);
8674 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8676 lun->pr_generation++;
8680 mtx_unlock(&lun->lun_lock);
8684 ctl_read_write(struct ctl_scsiio *ctsio)
8686 struct ctl_lun *lun;
8687 struct ctl_lba_len_flags *lbalen;
8689 uint32_t num_blocks;
8693 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8695 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8698 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8699 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8700 switch (ctsio->cdb[0]) {
8703 struct scsi_rw_6 *cdb;
8705 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8707 lba = scsi_3btoul(cdb->addr);
8708 /* only 5 bits are valid in the most significant address byte */
8710 num_blocks = cdb->length;
8712 * This is correct according to SBC-2.
8714 if (num_blocks == 0)
8720 struct scsi_rw_10 *cdb;
8722 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8723 if (cdb->byte2 & SRW10_FUA)
8724 flags |= CTL_LLF_FUA;
8725 if (cdb->byte2 & SRW10_DPO)
8726 flags |= CTL_LLF_DPO;
8727 lba = scsi_4btoul(cdb->addr);
8728 num_blocks = scsi_2btoul(cdb->length);
8731 case WRITE_VERIFY_10: {
8732 struct scsi_write_verify_10 *cdb;
8734 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8735 flags |= CTL_LLF_FUA;
8736 if (cdb->byte2 & SWV_DPO)
8737 flags |= CTL_LLF_DPO;
8738 lba = scsi_4btoul(cdb->addr);
8739 num_blocks = scsi_2btoul(cdb->length);
8744 struct scsi_rw_12 *cdb;
8746 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8747 if (cdb->byte2 & SRW12_FUA)
8748 flags |= CTL_LLF_FUA;
8749 if (cdb->byte2 & SRW12_DPO)
8750 flags |= CTL_LLF_DPO;
8751 lba = scsi_4btoul(cdb->addr);
8752 num_blocks = scsi_4btoul(cdb->length);
8755 case WRITE_VERIFY_12: {
8756 struct scsi_write_verify_12 *cdb;
8758 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8759 flags |= CTL_LLF_FUA;
8760 if (cdb->byte2 & SWV_DPO)
8761 flags |= CTL_LLF_DPO;
8762 lba = scsi_4btoul(cdb->addr);
8763 num_blocks = scsi_4btoul(cdb->length);
8768 struct scsi_rw_16 *cdb;
8770 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8771 if (cdb->byte2 & SRW12_FUA)
8772 flags |= CTL_LLF_FUA;
8773 if (cdb->byte2 & SRW12_DPO)
8774 flags |= CTL_LLF_DPO;
8775 lba = scsi_8btou64(cdb->addr);
8776 num_blocks = scsi_4btoul(cdb->length);
8779 case WRITE_ATOMIC_16: {
8780 struct scsi_write_atomic_16 *cdb;
8782 if (lun->be_lun->atomicblock == 0) {
8783 ctl_set_invalid_opcode(ctsio);
8784 ctl_done((union ctl_io *)ctsio);
8785 return (CTL_RETVAL_COMPLETE);
8788 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8789 if (cdb->byte2 & SRW12_FUA)
8790 flags |= CTL_LLF_FUA;
8791 if (cdb->byte2 & SRW12_DPO)
8792 flags |= CTL_LLF_DPO;
8793 lba = scsi_8btou64(cdb->addr);
8794 num_blocks = scsi_2btoul(cdb->length);
8795 if (num_blocks > lun->be_lun->atomicblock) {
8796 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8797 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8799 ctl_done((union ctl_io *)ctsio);
8800 return (CTL_RETVAL_COMPLETE);
8804 case WRITE_VERIFY_16: {
8805 struct scsi_write_verify_16 *cdb;
8807 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8808 flags |= CTL_LLF_FUA;
8809 if (cdb->byte2 & SWV_DPO)
8810 flags |= CTL_LLF_DPO;
8811 lba = scsi_8btou64(cdb->addr);
8812 num_blocks = scsi_4btoul(cdb->length);
8817 * We got a command we don't support. This shouldn't
8818 * happen, commands should be filtered out above us.
8820 ctl_set_invalid_opcode(ctsio);
8821 ctl_done((union ctl_io *)ctsio);
8823 return (CTL_RETVAL_COMPLETE);
8824 break; /* NOTREACHED */
8828 * The first check is to make sure we're in bounds, the second
8829 * check is to catch wrap-around problems. If the lba + num blocks
8830 * is less than the lba, then we've wrapped around and the block
8831 * range is invalid anyway.
8833 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8834 || ((lba + num_blocks) < lba)) {
8835 ctl_set_lba_out_of_range(ctsio);
8836 ctl_done((union ctl_io *)ctsio);
8837 return (CTL_RETVAL_COMPLETE);
8841 * According to SBC-3, a transfer length of 0 is not an error.
8842 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8843 * translates to 256 blocks for those commands.
8845 if (num_blocks == 0) {
8846 ctl_set_success(ctsio);
8847 ctl_done((union ctl_io *)ctsio);
8848 return (CTL_RETVAL_COMPLETE);
8851 /* Set FUA and/or DPO if caches are disabled. */
8853 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8855 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8857 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8859 flags |= CTL_LLF_FUA;
8862 lbalen = (struct ctl_lba_len_flags *)
8863 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8865 lbalen->len = num_blocks;
8866 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8868 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8869 ctsio->kern_rel_offset = 0;
8871 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8873 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8878 ctl_cnw_cont(union ctl_io *io)
8880 struct ctl_scsiio *ctsio;
8881 struct ctl_lun *lun;
8882 struct ctl_lba_len_flags *lbalen;
8885 ctsio = &io->scsiio;
8886 ctsio->io_hdr.status = CTL_STATUS_NONE;
8887 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8888 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8889 lbalen = (struct ctl_lba_len_flags *)
8890 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8891 lbalen->flags &= ~CTL_LLF_COMPARE;
8892 lbalen->flags |= CTL_LLF_WRITE;
8894 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8895 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8900 ctl_cnw(struct ctl_scsiio *ctsio)
8902 struct ctl_lun *lun;
8903 struct ctl_lba_len_flags *lbalen;
8905 uint32_t num_blocks;
8908 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8910 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8913 switch (ctsio->cdb[0]) {
8914 case COMPARE_AND_WRITE: {
8915 struct scsi_compare_and_write *cdb;
8917 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8918 if (cdb->byte2 & SRW10_FUA)
8919 flags |= CTL_LLF_FUA;
8920 if (cdb->byte2 & SRW10_DPO)
8921 flags |= CTL_LLF_DPO;
8922 lba = scsi_8btou64(cdb->addr);
8923 num_blocks = cdb->length;
8928 * We got a command we don't support. This shouldn't
8929 * happen, commands should be filtered out above us.
8931 ctl_set_invalid_opcode(ctsio);
8932 ctl_done((union ctl_io *)ctsio);
8934 return (CTL_RETVAL_COMPLETE);
8935 break; /* NOTREACHED */
8939 * The first check is to make sure we're in bounds, the second
8940 * check is to catch wrap-around problems. If the lba + num blocks
8941 * is less than the lba, then we've wrapped around and the block
8942 * range is invalid anyway.
8944 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8945 || ((lba + num_blocks) < lba)) {
8946 ctl_set_lba_out_of_range(ctsio);
8947 ctl_done((union ctl_io *)ctsio);
8948 return (CTL_RETVAL_COMPLETE);
8952 * According to SBC-3, a transfer length of 0 is not an error.
8954 if (num_blocks == 0) {
8955 ctl_set_success(ctsio);
8956 ctl_done((union ctl_io *)ctsio);
8957 return (CTL_RETVAL_COMPLETE);
8960 /* Set FUA if write cache is disabled. */
8961 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8963 flags |= CTL_LLF_FUA;
8965 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8966 ctsio->kern_rel_offset = 0;
8969 * Set the IO_CONT flag, so that if this I/O gets passed to
8970 * ctl_data_submit_done(), it'll get passed back to
8971 * ctl_ctl_cnw_cont() for further processing.
8973 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8974 ctsio->io_cont = ctl_cnw_cont;
8976 lbalen = (struct ctl_lba_len_flags *)
8977 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8979 lbalen->len = num_blocks;
8980 lbalen->flags = CTL_LLF_COMPARE | flags;
8982 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8983 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8988 ctl_verify(struct ctl_scsiio *ctsio)
8990 struct ctl_lun *lun;
8991 struct ctl_lba_len_flags *lbalen;
8993 uint32_t num_blocks;
8997 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8999 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
9002 flags = CTL_LLF_FUA;
9003 switch (ctsio->cdb[0]) {
9005 struct scsi_verify_10 *cdb;
9007 cdb = (struct scsi_verify_10 *)ctsio->cdb;
9008 if (cdb->byte2 & SVFY_BYTCHK)
9010 if (cdb->byte2 & SVFY_DPO)
9011 flags |= CTL_LLF_DPO;
9012 lba = scsi_4btoul(cdb->addr);
9013 num_blocks = scsi_2btoul(cdb->length);
9017 struct scsi_verify_12 *cdb;
9019 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9020 if (cdb->byte2 & SVFY_BYTCHK)
9022 if (cdb->byte2 & SVFY_DPO)
9023 flags |= CTL_LLF_DPO;
9024 lba = scsi_4btoul(cdb->addr);
9025 num_blocks = scsi_4btoul(cdb->length);
9029 struct scsi_rw_16 *cdb;
9031 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9032 if (cdb->byte2 & SVFY_BYTCHK)
9034 if (cdb->byte2 & SVFY_DPO)
9035 flags |= CTL_LLF_DPO;
9036 lba = scsi_8btou64(cdb->addr);
9037 num_blocks = scsi_4btoul(cdb->length);
9042 * We got a command we don't support. This shouldn't
9043 * happen, commands should be filtered out above us.
9045 ctl_set_invalid_opcode(ctsio);
9046 ctl_done((union ctl_io *)ctsio);
9047 return (CTL_RETVAL_COMPLETE);
9051 * The first check is to make sure we're in bounds, the second
9052 * check is to catch wrap-around problems. If the lba + num blocks
9053 * is less than the lba, then we've wrapped around and the block
9054 * range is invalid anyway.
9056 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9057 || ((lba + num_blocks) < lba)) {
9058 ctl_set_lba_out_of_range(ctsio);
9059 ctl_done((union ctl_io *)ctsio);
9060 return (CTL_RETVAL_COMPLETE);
9064 * According to SBC-3, a transfer length of 0 is not an error.
9066 if (num_blocks == 0) {
9067 ctl_set_success(ctsio);
9068 ctl_done((union ctl_io *)ctsio);
9069 return (CTL_RETVAL_COMPLETE);
9072 lbalen = (struct ctl_lba_len_flags *)
9073 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9075 lbalen->len = num_blocks;
9077 lbalen->flags = CTL_LLF_COMPARE | flags;
9078 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9080 lbalen->flags = CTL_LLF_VERIFY | flags;
9081 ctsio->kern_total_len = 0;
9083 ctsio->kern_rel_offset = 0;
9085 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9086 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9091 ctl_report_luns(struct ctl_scsiio *ctsio)
9093 struct ctl_softc *softc;
9094 struct scsi_report_luns *cdb;
9095 struct scsi_report_luns_data *lun_data;
9096 struct ctl_lun *lun, *request_lun;
9097 struct ctl_port *port;
9098 int num_luns, retval;
9099 uint32_t alloc_len, lun_datalen;
9101 uint32_t initidx, targ_lun_id, lun_id;
9103 retval = CTL_RETVAL_COMPLETE;
9104 cdb = (struct scsi_report_luns *)ctsio->cdb;
9105 port = ctl_io_port(&ctsio->io_hdr);
9106 softc = port->ctl_softc;
9108 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9110 mtx_lock(&softc->ctl_lock);
9112 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
9113 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
9116 mtx_unlock(&softc->ctl_lock);
9118 switch (cdb->select_report) {
9119 case RPL_REPORT_DEFAULT:
9120 case RPL_REPORT_ALL:
9121 case RPL_REPORT_NONSUBSID:
9123 case RPL_REPORT_WELLKNOWN:
9124 case RPL_REPORT_ADMIN:
9125 case RPL_REPORT_CONGLOM:
9129 ctl_set_invalid_field(ctsio,
9135 ctl_done((union ctl_io *)ctsio);
9137 break; /* NOTREACHED */
9140 alloc_len = scsi_4btoul(cdb->length);
9142 * The initiator has to allocate at least 16 bytes for this request,
9143 * so he can at least get the header and the first LUN. Otherwise
9144 * we reject the request (per SPC-3 rev 14, section 6.21).
9146 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9147 sizeof(struct scsi_report_luns_lundata))) {
9148 ctl_set_invalid_field(ctsio,
9154 ctl_done((union ctl_io *)ctsio);
9158 request_lun = (struct ctl_lun *)
9159 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9161 lun_datalen = sizeof(*lun_data) +
9162 (num_luns * sizeof(struct scsi_report_luns_lundata));
9164 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9165 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9166 ctsio->kern_sg_entries = 0;
9168 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9170 mtx_lock(&softc->ctl_lock);
9171 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9172 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9173 if (lun_id >= CTL_MAX_LUNS)
9175 lun = softc->ctl_luns[lun_id];
9179 be64enc(lun_data->luns[num_filled++].lundata,
9180 ctl_encode_lun(targ_lun_id));
9183 * According to SPC-3, rev 14 section 6.21:
9185 * "The execution of a REPORT LUNS command to any valid and
9186 * installed logical unit shall clear the REPORTED LUNS DATA
9187 * HAS CHANGED unit attention condition for all logical
9188 * units of that target with respect to the requesting
9189 * initiator. A valid and installed logical unit is one
9190 * having a PERIPHERAL QUALIFIER of 000b in the standard
9191 * INQUIRY data (see 6.4.2)."
9193 * If request_lun is NULL, the LUN this report luns command
9194 * was issued to is either disabled or doesn't exist. In that
9195 * case, we shouldn't clear any pending lun change unit
9198 if (request_lun != NULL) {
9199 mtx_lock(&lun->lun_lock);
9200 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9201 mtx_unlock(&lun->lun_lock);
9204 mtx_unlock(&softc->ctl_lock);
9207 * It's quite possible that we've returned fewer LUNs than we allocated
9208 * space for. Trim it.
9210 lun_datalen = sizeof(*lun_data) +
9211 (num_filled * sizeof(struct scsi_report_luns_lundata));
9213 if (lun_datalen < alloc_len) {
9214 ctsio->residual = alloc_len - lun_datalen;
9215 ctsio->kern_data_len = lun_datalen;
9216 ctsio->kern_total_len = lun_datalen;
9218 ctsio->residual = 0;
9219 ctsio->kern_data_len = alloc_len;
9220 ctsio->kern_total_len = alloc_len;
9222 ctsio->kern_data_resid = 0;
9223 ctsio->kern_rel_offset = 0;
9224 ctsio->kern_sg_entries = 0;
9227 * We set this to the actual data length, regardless of how much
9228 * space we actually have to return results. If the user looks at
9229 * this value, he'll know whether or not he allocated enough space
9230 * and reissue the command if necessary. We don't support well
9231 * known logical units, so if the user asks for that, return none.
9233 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9236 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9239 ctl_set_success(ctsio);
9240 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9241 ctsio->be_move_done = ctl_config_move_done;
9242 ctl_datamove((union ctl_io *)ctsio);
9247 ctl_request_sense(struct ctl_scsiio *ctsio)
9249 struct scsi_request_sense *cdb;
9250 struct scsi_sense_data *sense_ptr;
9251 struct ctl_softc *ctl_softc;
9252 struct ctl_lun *lun;
9255 scsi_sense_data_type sense_format;
9256 ctl_ua_type ua_type;
9258 cdb = (struct scsi_request_sense *)ctsio->cdb;
9260 ctl_softc = control_softc;
9261 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9263 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9266 * Determine which sense format the user wants.
9268 if (cdb->byte2 & SRS_DESC)
9269 sense_format = SSD_TYPE_DESC;
9271 sense_format = SSD_TYPE_FIXED;
9273 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9274 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9275 ctsio->kern_sg_entries = 0;
9278 * struct scsi_sense_data, which is currently set to 256 bytes, is
9279 * larger than the largest allowed value for the length field in the
9280 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9282 ctsio->residual = 0;
9283 ctsio->kern_data_len = cdb->length;
9284 ctsio->kern_total_len = cdb->length;
9286 ctsio->kern_data_resid = 0;
9287 ctsio->kern_rel_offset = 0;
9288 ctsio->kern_sg_entries = 0;
9291 * If we don't have a LUN, we don't have any pending sense.
9297 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9299 * Check for pending sense, and then for pending unit attentions.
9300 * Pending sense gets returned first, then pending unit attentions.
9302 mtx_lock(&lun->lun_lock);
9304 if (ctl_is_set(lun->have_ca, initidx)) {
9305 scsi_sense_data_type stored_format;
9308 * Check to see which sense format was used for the stored
9311 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9314 * If the user requested a different sense format than the
9315 * one we stored, then we need to convert it to the other
9316 * format. If we're going from descriptor to fixed format
9317 * sense data, we may lose things in translation, depending
9318 * on what options were used.
9320 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9321 * for some reason we'll just copy it out as-is.
9323 if ((stored_format == SSD_TYPE_FIXED)
9324 && (sense_format == SSD_TYPE_DESC))
9325 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9326 &lun->pending_sense[initidx],
9327 (struct scsi_sense_data_desc *)sense_ptr);
9328 else if ((stored_format == SSD_TYPE_DESC)
9329 && (sense_format == SSD_TYPE_FIXED))
9330 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9331 &lun->pending_sense[initidx],
9332 (struct scsi_sense_data_fixed *)sense_ptr);
9334 memcpy(sense_ptr, &lun->pending_sense[initidx],
9335 MIN(sizeof(*sense_ptr),
9336 sizeof(lun->pending_sense[initidx])));
9338 ctl_clear_mask(lun->have_ca, initidx);
9343 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9344 if (ua_type != CTL_UA_NONE)
9346 if (ua_type == CTL_UA_LUN_CHANGE) {
9347 mtx_unlock(&lun->lun_lock);
9348 mtx_lock(&ctl_softc->ctl_lock);
9349 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9350 mtx_unlock(&ctl_softc->ctl_lock);
9351 mtx_lock(&lun->lun_lock);
9355 mtx_unlock(&lun->lun_lock);
9358 * We already have a pending error, return it.
9360 if (have_error != 0) {
9362 * We report the SCSI status as OK, since the status of the
9363 * request sense command itself is OK.
9364 * We report 0 for the sense length, because we aren't doing
9365 * autosense in this case. We're reporting sense as
9368 ctl_set_success(ctsio);
9369 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9370 ctsio->be_move_done = ctl_config_move_done;
9371 ctl_datamove((union ctl_io *)ctsio);
9372 return (CTL_RETVAL_COMPLETE);
9378 * No sense information to report, so we report that everything is
9381 ctl_set_sense_data(sense_ptr,
9384 /*current_error*/ 1,
9385 /*sense_key*/ SSD_KEY_NO_SENSE,
9391 * We report 0 for the sense length, because we aren't doing
9392 * autosense in this case. We're reporting sense as parameter data.
9394 ctl_set_success(ctsio);
9395 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9396 ctsio->be_move_done = ctl_config_move_done;
9397 ctl_datamove((union ctl_io *)ctsio);
9398 return (CTL_RETVAL_COMPLETE);
9402 ctl_tur(struct ctl_scsiio *ctsio)
9405 CTL_DEBUG_PRINT(("ctl_tur\n"));
9407 ctl_set_success(ctsio);
9408 ctl_done((union ctl_io *)ctsio);
9410 return (CTL_RETVAL_COMPLETE);
9414 * SCSI VPD page 0x00, the Supported VPD Pages page.
9417 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9419 struct scsi_vpd_supported_pages *pages;
9421 struct ctl_lun *lun;
9424 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9426 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9427 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9428 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9429 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9430 ctsio->kern_sg_entries = 0;
9432 if (sup_page_size < alloc_len) {
9433 ctsio->residual = alloc_len - sup_page_size;
9434 ctsio->kern_data_len = sup_page_size;
9435 ctsio->kern_total_len = sup_page_size;
9437 ctsio->residual = 0;
9438 ctsio->kern_data_len = alloc_len;
9439 ctsio->kern_total_len = alloc_len;
9441 ctsio->kern_data_resid = 0;
9442 ctsio->kern_rel_offset = 0;
9443 ctsio->kern_sg_entries = 0;
9446 * The control device is always connected. The disk device, on the
9447 * other hand, may not be online all the time. Need to change this
9448 * to figure out whether the disk device is actually online or not.
9451 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9452 lun->be_lun->lun_type;
9454 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9457 /* Supported VPD pages */
9458 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9460 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9461 /* Device Identification */
9462 pages->page_list[p++] = SVPD_DEVICE_ID;
9463 /* Extended INQUIRY Data */
9464 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9465 /* Mode Page Policy */
9466 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9468 pages->page_list[p++] = SVPD_SCSI_PORTS;
9469 /* Third-party Copy */
9470 pages->page_list[p++] = SVPD_SCSI_TPC;
9471 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9473 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9474 /* Block Device Characteristics */
9475 pages->page_list[p++] = SVPD_BDC;
9476 /* Logical Block Provisioning */
9477 pages->page_list[p++] = SVPD_LBP;
9481 ctl_set_success(ctsio);
9482 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9483 ctsio->be_move_done = ctl_config_move_done;
9484 ctl_datamove((union ctl_io *)ctsio);
9485 return (CTL_RETVAL_COMPLETE);
9489 * SCSI VPD page 0x80, the Unit Serial Number page.
9492 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9494 struct scsi_vpd_unit_serial_number *sn_ptr;
9495 struct ctl_lun *lun;
9498 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9500 data_len = 4 + CTL_SN_LEN;
9501 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9502 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9503 if (data_len < alloc_len) {
9504 ctsio->residual = alloc_len - data_len;
9505 ctsio->kern_data_len = data_len;
9506 ctsio->kern_total_len = data_len;
9508 ctsio->residual = 0;
9509 ctsio->kern_data_len = alloc_len;
9510 ctsio->kern_total_len = alloc_len;
9512 ctsio->kern_data_resid = 0;
9513 ctsio->kern_rel_offset = 0;
9514 ctsio->kern_sg_entries = 0;
9517 * The control device is always connected. The disk device, on the
9518 * other hand, may not be online all the time. Need to change this
9519 * to figure out whether the disk device is actually online or not.
9522 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9523 lun->be_lun->lun_type;
9525 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9527 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9528 sn_ptr->length = CTL_SN_LEN;
9530 * If we don't have a LUN, we just leave the serial number as
9534 strncpy((char *)sn_ptr->serial_num,
9535 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9537 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9539 ctl_set_success(ctsio);
9540 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9541 ctsio->be_move_done = ctl_config_move_done;
9542 ctl_datamove((union ctl_io *)ctsio);
9543 return (CTL_RETVAL_COMPLETE);
9548 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9551 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9553 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9554 struct ctl_lun *lun;
9557 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9559 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9560 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9561 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9562 ctsio->kern_sg_entries = 0;
9564 if (data_len < alloc_len) {
9565 ctsio->residual = alloc_len - data_len;
9566 ctsio->kern_data_len = data_len;
9567 ctsio->kern_total_len = data_len;
9569 ctsio->residual = 0;
9570 ctsio->kern_data_len = alloc_len;
9571 ctsio->kern_total_len = alloc_len;
9573 ctsio->kern_data_resid = 0;
9574 ctsio->kern_rel_offset = 0;
9575 ctsio->kern_sg_entries = 0;
9578 * The control device is always connected. The disk device, on the
9579 * other hand, may not be online all the time.
9582 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9583 lun->be_lun->lun_type;
9585 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9586 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9587 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9589 * We support head of queue, ordered and simple tags.
9591 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9593 * Volatile cache supported.
9595 eid_ptr->flags3 = SVPD_EID_V_SUP;
9598 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9599 * attention for a particular IT nexus on all LUNs once we report
9600 * it to that nexus once. This bit is required as of SPC-4.
9602 eid_ptr->flags4 = SVPD_EID_LUICLT;
9605 * XXX KDM in order to correctly answer this, we would need
9606 * information from the SIM to determine how much sense data it
9607 * can send. So this would really be a path inquiry field, most
9608 * likely. This can be set to a maximum of 252 according to SPC-4,
9609 * but the hardware may or may not be able to support that much.
9610 * 0 just means that the maximum sense data length is not reported.
9612 eid_ptr->max_sense_length = 0;
9614 ctl_set_success(ctsio);
9615 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9616 ctsio->be_move_done = ctl_config_move_done;
9617 ctl_datamove((union ctl_io *)ctsio);
9618 return (CTL_RETVAL_COMPLETE);
9622 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9624 struct scsi_vpd_mode_page_policy *mpp_ptr;
9625 struct ctl_lun *lun;
9628 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9630 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9631 sizeof(struct scsi_vpd_mode_page_policy_descr);
9633 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9634 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9635 ctsio->kern_sg_entries = 0;
9637 if (data_len < alloc_len) {
9638 ctsio->residual = alloc_len - data_len;
9639 ctsio->kern_data_len = data_len;
9640 ctsio->kern_total_len = data_len;
9642 ctsio->residual = 0;
9643 ctsio->kern_data_len = alloc_len;
9644 ctsio->kern_total_len = alloc_len;
9646 ctsio->kern_data_resid = 0;
9647 ctsio->kern_rel_offset = 0;
9648 ctsio->kern_sg_entries = 0;
9651 * The control device is always connected. The disk device, on the
9652 * other hand, may not be online all the time.
9655 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9656 lun->be_lun->lun_type;
9658 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9659 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9660 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9661 mpp_ptr->descr[0].page_code = 0x3f;
9662 mpp_ptr->descr[0].subpage_code = 0xff;
9663 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9665 ctl_set_success(ctsio);
9666 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9667 ctsio->be_move_done = ctl_config_move_done;
9668 ctl_datamove((union ctl_io *)ctsio);
9669 return (CTL_RETVAL_COMPLETE);
9673 * SCSI VPD page 0x83, the Device Identification page.
9676 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9678 struct scsi_vpd_device_id *devid_ptr;
9679 struct scsi_vpd_id_descriptor *desc;
9680 struct ctl_softc *softc;
9681 struct ctl_lun *lun;
9682 struct ctl_port *port;
9686 softc = control_softc;
9688 port = ctl_io_port(&ctsio->io_hdr);
9689 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9691 data_len = sizeof(struct scsi_vpd_device_id) +
9692 sizeof(struct scsi_vpd_id_descriptor) +
9693 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9694 sizeof(struct scsi_vpd_id_descriptor) +
9695 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9696 if (lun && lun->lun_devid)
9697 data_len += lun->lun_devid->len;
9698 if (port && port->port_devid)
9699 data_len += port->port_devid->len;
9700 if (port && port->target_devid)
9701 data_len += port->target_devid->len;
9703 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9704 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9705 ctsio->kern_sg_entries = 0;
9707 if (data_len < alloc_len) {
9708 ctsio->residual = alloc_len - data_len;
9709 ctsio->kern_data_len = data_len;
9710 ctsio->kern_total_len = data_len;
9712 ctsio->residual = 0;
9713 ctsio->kern_data_len = alloc_len;
9714 ctsio->kern_total_len = alloc_len;
9716 ctsio->kern_data_resid = 0;
9717 ctsio->kern_rel_offset = 0;
9718 ctsio->kern_sg_entries = 0;
9721 * The control device is always connected. The disk device, on the
9722 * other hand, may not be online all the time.
9725 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9726 lun->be_lun->lun_type;
9728 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9729 devid_ptr->page_code = SVPD_DEVICE_ID;
9730 scsi_ulto2b(data_len - 4, devid_ptr->length);
9732 if (port && port->port_type == CTL_PORT_FC)
9733 proto = SCSI_PROTO_FC << 4;
9734 else if (port && port->port_type == CTL_PORT_ISCSI)
9735 proto = SCSI_PROTO_ISCSI << 4;
9737 proto = SCSI_PROTO_SPI << 4;
9738 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9741 * We're using a LUN association here. i.e., this device ID is a
9742 * per-LUN identifier.
9744 if (lun && lun->lun_devid) {
9745 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9746 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9747 lun->lun_devid->len);
9751 * This is for the WWPN which is a port association.
9753 if (port && port->port_devid) {
9754 memcpy(desc, port->port_devid->data, port->port_devid->len);
9755 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9756 port->port_devid->len);
9760 * This is for the Relative Target Port(type 4h) identifier
9762 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9763 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9764 SVPD_ID_TYPE_RELTARG;
9766 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9767 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9768 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9771 * This is for the Target Port Group(type 5h) identifier
9773 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9774 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9775 SVPD_ID_TYPE_TPORTGRP;
9777 if (softc->is_single ||
9778 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9781 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9782 scsi_ulto2b(g, &desc->identifier[2]);
9783 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9784 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9787 * This is for the Target identifier
9789 if (port && port->target_devid) {
9790 memcpy(desc, port->target_devid->data, port->target_devid->len);
9793 ctl_set_success(ctsio);
9794 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9795 ctsio->be_move_done = ctl_config_move_done;
9796 ctl_datamove((union ctl_io *)ctsio);
9797 return (CTL_RETVAL_COMPLETE);
9801 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9803 struct ctl_softc *softc = control_softc;
9804 struct scsi_vpd_scsi_ports *sp;
9805 struct scsi_vpd_port_designation *pd;
9806 struct scsi_vpd_port_designation_cont *pdc;
9807 struct ctl_lun *lun;
9808 struct ctl_port *port;
9809 int data_len, num_target_ports, iid_len, id_len;
9811 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9813 num_target_ports = 0;
9816 mtx_lock(&softc->ctl_lock);
9817 STAILQ_FOREACH(port, &softc->port_list, links) {
9818 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9821 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9824 if (port->init_devid)
9825 iid_len += port->init_devid->len;
9826 if (port->port_devid)
9827 id_len += port->port_devid->len;
9829 mtx_unlock(&softc->ctl_lock);
9831 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9832 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9833 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9834 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9835 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9836 ctsio->kern_sg_entries = 0;
9838 if (data_len < alloc_len) {
9839 ctsio->residual = alloc_len - data_len;
9840 ctsio->kern_data_len = data_len;
9841 ctsio->kern_total_len = data_len;
9843 ctsio->residual = 0;
9844 ctsio->kern_data_len = alloc_len;
9845 ctsio->kern_total_len = alloc_len;
9847 ctsio->kern_data_resid = 0;
9848 ctsio->kern_rel_offset = 0;
9849 ctsio->kern_sg_entries = 0;
9852 * The control device is always connected. The disk device, on the
9853 * other hand, may not be online all the time. Need to change this
9854 * to figure out whether the disk device is actually online or not.
9857 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9858 lun->be_lun->lun_type;
9860 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9862 sp->page_code = SVPD_SCSI_PORTS;
9863 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9865 pd = &sp->design[0];
9867 mtx_lock(&softc->ctl_lock);
9868 STAILQ_FOREACH(port, &softc->port_list, links) {
9869 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9872 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9874 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9875 if (port->init_devid) {
9876 iid_len = port->init_devid->len;
9877 memcpy(pd->initiator_transportid,
9878 port->init_devid->data, port->init_devid->len);
9881 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9882 pdc = (struct scsi_vpd_port_designation_cont *)
9883 (&pd->initiator_transportid[iid_len]);
9884 if (port->port_devid) {
9885 id_len = port->port_devid->len;
9886 memcpy(pdc->target_port_descriptors,
9887 port->port_devid->data, port->port_devid->len);
9890 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9891 pd = (struct scsi_vpd_port_designation *)
9892 ((uint8_t *)pdc->target_port_descriptors + id_len);
9894 mtx_unlock(&softc->ctl_lock);
9896 ctl_set_success(ctsio);
9897 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9898 ctsio->be_move_done = ctl_config_move_done;
9899 ctl_datamove((union ctl_io *)ctsio);
9900 return (CTL_RETVAL_COMPLETE);
9904 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9906 struct scsi_vpd_block_limits *bl_ptr;
9907 struct ctl_lun *lun;
9909 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9911 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9912 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9913 ctsio->kern_sg_entries = 0;
9915 if (sizeof(*bl_ptr) < alloc_len) {
9916 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9917 ctsio->kern_data_len = sizeof(*bl_ptr);
9918 ctsio->kern_total_len = sizeof(*bl_ptr);
9920 ctsio->residual = 0;
9921 ctsio->kern_data_len = alloc_len;
9922 ctsio->kern_total_len = alloc_len;
9924 ctsio->kern_data_resid = 0;
9925 ctsio->kern_rel_offset = 0;
9926 ctsio->kern_sg_entries = 0;
9929 * The control device is always connected. The disk device, on the
9930 * other hand, may not be online all the time. Need to change this
9931 * to figure out whether the disk device is actually online or not.
9934 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9935 lun->be_lun->lun_type;
9937 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9939 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9940 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9941 bl_ptr->max_cmp_write_len = 0xff;
9942 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9944 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9945 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9946 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9947 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9948 if (lun->be_lun->ublockexp != 0) {
9949 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9950 bl_ptr->opt_unmap_grain);
9951 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9952 bl_ptr->unmap_grain_align);
9955 scsi_ulto4b(lun->be_lun->atomicblock,
9956 bl_ptr->max_atomic_transfer_length);
9957 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9958 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9959 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9960 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9962 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9964 ctl_set_success(ctsio);
9965 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9966 ctsio->be_move_done = ctl_config_move_done;
9967 ctl_datamove((union ctl_io *)ctsio);
9968 return (CTL_RETVAL_COMPLETE);
9972 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9974 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9975 struct ctl_lun *lun;
9979 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9981 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9982 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9983 ctsio->kern_sg_entries = 0;
9985 if (sizeof(*bdc_ptr) < alloc_len) {
9986 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9987 ctsio->kern_data_len = sizeof(*bdc_ptr);
9988 ctsio->kern_total_len = sizeof(*bdc_ptr);
9990 ctsio->residual = 0;
9991 ctsio->kern_data_len = alloc_len;
9992 ctsio->kern_total_len = alloc_len;
9994 ctsio->kern_data_resid = 0;
9995 ctsio->kern_rel_offset = 0;
9996 ctsio->kern_sg_entries = 0;
9999 * The control device is always connected. The disk device, on the
10000 * other hand, may not be online all the time. Need to change this
10001 * to figure out whether the disk device is actually online or not.
10004 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10005 lun->be_lun->lun_type;
10007 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10008 bdc_ptr->page_code = SVPD_BDC;
10009 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10011 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10012 i = strtol(value, NULL, 0);
10014 i = CTL_DEFAULT_ROTATION_RATE;
10015 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10017 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10018 i = strtol(value, NULL, 0);
10021 bdc_ptr->wab_wac_ff = (i & 0x0f);
10022 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10024 ctl_set_success(ctsio);
10025 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10026 ctsio->be_move_done = ctl_config_move_done;
10027 ctl_datamove((union ctl_io *)ctsio);
10028 return (CTL_RETVAL_COMPLETE);
10032 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10034 struct scsi_vpd_logical_block_prov *lbp_ptr;
10035 struct ctl_lun *lun;
10037 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10039 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10040 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10041 ctsio->kern_sg_entries = 0;
10043 if (sizeof(*lbp_ptr) < alloc_len) {
10044 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10045 ctsio->kern_data_len = sizeof(*lbp_ptr);
10046 ctsio->kern_total_len = sizeof(*lbp_ptr);
10048 ctsio->residual = 0;
10049 ctsio->kern_data_len = alloc_len;
10050 ctsio->kern_total_len = alloc_len;
10052 ctsio->kern_data_resid = 0;
10053 ctsio->kern_rel_offset = 0;
10054 ctsio->kern_sg_entries = 0;
10057 * The control device is always connected. The disk device, on the
10058 * other hand, may not be online all the time. Need to change this
10059 * to figure out whether the disk device is actually online or not.
10062 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10063 lun->be_lun->lun_type;
10065 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10067 lbp_ptr->page_code = SVPD_LBP;
10068 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10069 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10070 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10071 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10072 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10073 lbp_ptr->prov_type = SVPD_LBP_THIN;
10076 ctl_set_success(ctsio);
10077 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10078 ctsio->be_move_done = ctl_config_move_done;
10079 ctl_datamove((union ctl_io *)ctsio);
10080 return (CTL_RETVAL_COMPLETE);
10084 * INQUIRY with the EVPD bit set.
10087 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10089 struct ctl_lun *lun;
10090 struct scsi_inquiry *cdb;
10091 int alloc_len, retval;
10093 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10094 cdb = (struct scsi_inquiry *)ctsio->cdb;
10095 alloc_len = scsi_2btoul(cdb->length);
10097 switch (cdb->page_code) {
10098 case SVPD_SUPPORTED_PAGES:
10099 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10101 case SVPD_UNIT_SERIAL_NUMBER:
10102 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10104 case SVPD_DEVICE_ID:
10105 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10107 case SVPD_EXTENDED_INQUIRY_DATA:
10108 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10110 case SVPD_MODE_PAGE_POLICY:
10111 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10113 case SVPD_SCSI_PORTS:
10114 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10116 case SVPD_SCSI_TPC:
10117 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10119 case SVPD_BLOCK_LIMITS:
10120 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10122 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10125 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10127 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10130 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10132 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10136 ctl_set_invalid_field(ctsio,
10142 ctl_done((union ctl_io *)ctsio);
10143 retval = CTL_RETVAL_COMPLETE;
10151 * Standard INQUIRY data.
10154 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10156 struct scsi_inquiry_data *inq_ptr;
10157 struct scsi_inquiry *cdb;
10158 struct ctl_softc *softc = control_softc;
10159 struct ctl_port *port;
10160 struct ctl_lun *lun;
10162 uint32_t alloc_len, data_len;
10163 ctl_port_type port_type;
10165 port = ctl_io_port(&ctsio->io_hdr);
10166 port_type = port->port_type;
10167 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10168 port_type = CTL_PORT_SCSI;
10170 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10171 cdb = (struct scsi_inquiry *)ctsio->cdb;
10172 alloc_len = scsi_2btoul(cdb->length);
10175 * We malloc the full inquiry data size here and fill it
10176 * in. If the user only asks for less, we'll give him
10179 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10180 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10181 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10182 ctsio->kern_sg_entries = 0;
10183 ctsio->kern_data_resid = 0;
10184 ctsio->kern_rel_offset = 0;
10186 if (data_len < alloc_len) {
10187 ctsio->residual = alloc_len - data_len;
10188 ctsio->kern_data_len = data_len;
10189 ctsio->kern_total_len = data_len;
10191 ctsio->residual = 0;
10192 ctsio->kern_data_len = alloc_len;
10193 ctsio->kern_total_len = alloc_len;
10197 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10198 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10199 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10200 lun->be_lun->lun_type;
10202 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10203 lun->be_lun->lun_type;
10205 if (lun->flags & CTL_LUN_REMOVABLE)
10206 inq_ptr->dev_qual2 |= SID_RMB;
10208 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10210 /* RMB in byte 2 is 0 */
10211 inq_ptr->version = SCSI_REV_SPC4;
10214 * According to SAM-3, even if a device only supports a single
10215 * level of LUN addressing, it should still set the HISUP bit:
10217 * 4.9.1 Logical unit numbers overview
10219 * All logical unit number formats described in this standard are
10220 * hierarchical in structure even when only a single level in that
10221 * hierarchy is used. The HISUP bit shall be set to one in the
10222 * standard INQUIRY data (see SPC-2) when any logical unit number
10223 * format described in this standard is used. Non-hierarchical
10224 * formats are outside the scope of this standard.
10226 * Therefore we set the HiSup bit here.
10228 * The reponse format is 2, per SPC-3.
10230 inq_ptr->response_format = SID_HiSup | 2;
10232 inq_ptr->additional_length = data_len -
10233 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10234 CTL_DEBUG_PRINT(("additional_length = %d\n",
10235 inq_ptr->additional_length));
10237 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10238 if (port_type == CTL_PORT_SCSI)
10239 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10240 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10241 inq_ptr->flags = SID_CmdQue;
10242 if (port_type == CTL_PORT_SCSI)
10243 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10246 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10247 * We have 8 bytes for the vendor name, and 16 bytes for the device
10248 * name and 4 bytes for the revision.
10250 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10251 "vendor")) == NULL) {
10252 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10254 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10255 strncpy(inq_ptr->vendor, val,
10256 min(sizeof(inq_ptr->vendor), strlen(val)));
10259 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10260 sizeof(inq_ptr->product));
10261 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10262 switch (lun->be_lun->lun_type) {
10264 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10265 sizeof(inq_ptr->product));
10268 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10269 sizeof(inq_ptr->product));
10272 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10273 sizeof(inq_ptr->product));
10276 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10277 sizeof(inq_ptr->product));
10281 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10282 strncpy(inq_ptr->product, val,
10283 min(sizeof(inq_ptr->product), strlen(val)));
10287 * XXX make this a macro somewhere so it automatically gets
10288 * incremented when we make changes.
10290 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10291 "revision")) == NULL) {
10292 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10294 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10295 strncpy(inq_ptr->revision, val,
10296 min(sizeof(inq_ptr->revision), strlen(val)));
10300 * For parallel SCSI, we support double transition and single
10301 * transition clocking. We also support QAS (Quick Arbitration
10302 * and Selection) and Information Unit transfers on both the
10303 * control and array devices.
10305 if (port_type == CTL_PORT_SCSI)
10306 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10309 /* SAM-5 (no version claimed) */
10310 scsi_ulto2b(0x00A0, inq_ptr->version1);
10311 /* SPC-4 (no version claimed) */
10312 scsi_ulto2b(0x0460, inq_ptr->version2);
10313 if (port_type == CTL_PORT_FC) {
10314 /* FCP-2 ANSI INCITS.350:2003 */
10315 scsi_ulto2b(0x0917, inq_ptr->version3);
10316 } else if (port_type == CTL_PORT_SCSI) {
10317 /* SPI-4 ANSI INCITS.362:200x */
10318 scsi_ulto2b(0x0B56, inq_ptr->version3);
10319 } else if (port_type == CTL_PORT_ISCSI) {
10320 /* iSCSI (no version claimed) */
10321 scsi_ulto2b(0x0960, inq_ptr->version3);
10322 } else if (port_type == CTL_PORT_SAS) {
10323 /* SAS (no version claimed) */
10324 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10328 /* SBC-4 (no version claimed) */
10329 scsi_ulto2b(0x0600, inq_ptr->version4);
10331 switch (lun->be_lun->lun_type) {
10333 /* SBC-4 (no version claimed) */
10334 scsi_ulto2b(0x0600, inq_ptr->version4);
10339 /* MMC-6 (no version claimed) */
10340 scsi_ulto2b(0x04E0, inq_ptr->version4);
10347 ctl_set_success(ctsio);
10348 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10349 ctsio->be_move_done = ctl_config_move_done;
10350 ctl_datamove((union ctl_io *)ctsio);
10351 return (CTL_RETVAL_COMPLETE);
10355 ctl_inquiry(struct ctl_scsiio *ctsio)
10357 struct scsi_inquiry *cdb;
10360 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10362 cdb = (struct scsi_inquiry *)ctsio->cdb;
10363 if (cdb->byte2 & SI_EVPD)
10364 retval = ctl_inquiry_evpd(ctsio);
10365 else if (cdb->page_code == 0)
10366 retval = ctl_inquiry_std(ctsio);
10368 ctl_set_invalid_field(ctsio,
10374 ctl_done((union ctl_io *)ctsio);
10375 return (CTL_RETVAL_COMPLETE);
10382 ctl_get_config(struct ctl_scsiio *ctsio)
10384 struct scsi_get_config_header *hdr;
10385 struct scsi_get_config_feature *feature;
10386 struct scsi_get_config *cdb;
10387 struct ctl_lun *lun;
10388 uint32_t alloc_len, data_len;
10391 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10392 cdb = (struct scsi_get_config *)ctsio->cdb;
10393 rt = (cdb->rt & SGC_RT_MASK);
10394 starting = scsi_2btoul(cdb->starting_feature);
10395 alloc_len = scsi_2btoul(cdb->length);
10397 data_len = sizeof(struct scsi_get_config_header) +
10398 sizeof(struct scsi_get_config_feature) + 8 +
10399 sizeof(struct scsi_get_config_feature) + 8 +
10400 sizeof(struct scsi_get_config_feature) + 4 +
10401 sizeof(struct scsi_get_config_feature) + 4 +
10402 sizeof(struct scsi_get_config_feature) + 8 +
10403 sizeof(struct scsi_get_config_feature) +
10404 sizeof(struct scsi_get_config_feature) + 4 +
10405 sizeof(struct scsi_get_config_feature) + 4 +
10406 sizeof(struct scsi_get_config_feature) + 4 +
10407 sizeof(struct scsi_get_config_feature) + 4 +
10408 sizeof(struct scsi_get_config_feature) + 4 +
10409 sizeof(struct scsi_get_config_feature) + 4;
10410 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10411 ctsio->kern_sg_entries = 0;
10412 ctsio->kern_data_resid = 0;
10413 ctsio->kern_rel_offset = 0;
10415 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10416 if (lun->flags & CTL_LUN_NO_MEDIA)
10417 scsi_ulto2b(0x0000, hdr->current_profile);
10419 scsi_ulto2b(0x0010, hdr->current_profile);
10420 feature = (struct scsi_get_config_feature *)(hdr + 1);
10422 if (starting > 0x003b)
10424 if (starting > 0x003a)
10426 if (starting > 0x002b)
10428 if (starting > 0x002a)
10430 if (starting > 0x001f)
10432 if (starting > 0x001e)
10434 if (starting > 0x001d)
10436 if (starting > 0x0010)
10438 if (starting > 0x0003)
10440 if (starting > 0x0002)
10442 if (starting > 0x0001)
10444 if (starting > 0x0000)
10448 scsi_ulto2b(0x0000, feature->feature_code);
10449 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10450 feature->add_length = 8;
10451 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10452 feature->feature_data[2] = 0x00;
10453 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10454 feature->feature_data[6] = 0x01;
10455 feature = (struct scsi_get_config_feature *)
10456 &feature->feature_data[feature->add_length];
10459 scsi_ulto2b(0x0001, feature->feature_code);
10460 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10461 feature->add_length = 8;
10462 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10463 feature->feature_data[4] = 0x03;
10464 feature = (struct scsi_get_config_feature *)
10465 &feature->feature_data[feature->add_length];
10468 scsi_ulto2b(0x0002, feature->feature_code);
10469 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10470 feature->add_length = 4;
10471 feature->feature_data[0] = 0x02;
10472 feature = (struct scsi_get_config_feature *)
10473 &feature->feature_data[feature->add_length];
10475 f3: /* Removable Medium */
10476 scsi_ulto2b(0x0003, feature->feature_code);
10477 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10478 feature->add_length = 4;
10479 feature->feature_data[0] = 0x39;
10480 feature = (struct scsi_get_config_feature *)
10481 &feature->feature_data[feature->add_length];
10483 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10486 f10: /* Random Read */
10487 scsi_ulto2b(0x0010, feature->feature_code);
10488 feature->flags = 0x00;
10489 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10490 feature->flags |= SGC_F_CURRENT;
10491 feature->add_length = 8;
10492 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10493 scsi_ulto2b(1, &feature->feature_data[4]);
10494 feature->feature_data[6] = 0x00;
10495 feature = (struct scsi_get_config_feature *)
10496 &feature->feature_data[feature->add_length];
10498 f1d: /* Multi-Read */
10499 scsi_ulto2b(0x001D, feature->feature_code);
10500 feature->flags = 0x00;
10501 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10502 feature->flags |= SGC_F_CURRENT;
10503 feature->add_length = 0;
10504 feature = (struct scsi_get_config_feature *)
10505 &feature->feature_data[feature->add_length];
10508 scsi_ulto2b(0x001E, 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 f1f: /* DVD Read */
10518 scsi_ulto2b(0x001F, feature->feature_code);
10519 feature->flags = 0x08;
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] = 0x01;
10524 feature->feature_data[2] = 0x03;
10525 feature = (struct scsi_get_config_feature *)
10526 &feature->feature_data[feature->add_length];
10529 scsi_ulto2b(0x002A, feature->feature_code);
10530 feature->flags = 0x04;
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->feature_data[1] = 0x00;
10536 feature = (struct scsi_get_config_feature *)
10537 &feature->feature_data[feature->add_length];
10540 scsi_ulto2b(0x002B, feature->feature_code);
10541 feature->flags = 0x00;
10542 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10543 feature->flags |= SGC_F_CURRENT;
10544 feature->add_length = 4;
10545 feature->feature_data[0] = 0x00;
10546 feature = (struct scsi_get_config_feature *)
10547 &feature->feature_data[feature->add_length];
10549 f3a: /* DVD+RW Dual Layer */
10550 scsi_ulto2b(0x003A, feature->feature_code);
10551 feature->flags = 0x00;
10552 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10553 feature->flags |= SGC_F_CURRENT;
10554 feature->add_length = 4;
10555 feature->feature_data[0] = 0x00;
10556 feature->feature_data[1] = 0x00;
10557 feature = (struct scsi_get_config_feature *)
10558 &feature->feature_data[feature->add_length];
10560 f3b: /* DVD+R Dual Layer */
10561 scsi_ulto2b(0x003B, feature->feature_code);
10562 feature->flags = 0x00;
10563 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10564 feature->flags |= SGC_F_CURRENT;
10565 feature->add_length = 4;
10566 feature->feature_data[0] = 0x00;
10567 feature = (struct scsi_get_config_feature *)
10568 &feature->feature_data[feature->add_length];
10571 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10572 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10573 feature = (struct scsi_get_config_feature *)(hdr + 1);
10574 if (scsi_2btoul(feature->feature_code) == starting)
10575 feature = (struct scsi_get_config_feature *)
10576 &feature->feature_data[feature->add_length];
10577 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10579 scsi_ulto4b(data_len - 4, hdr->data_length);
10580 if (data_len < alloc_len) {
10581 ctsio->residual = alloc_len - data_len;
10582 ctsio->kern_data_len = data_len;
10583 ctsio->kern_total_len = data_len;
10585 ctsio->residual = 0;
10586 ctsio->kern_data_len = alloc_len;
10587 ctsio->kern_total_len = alloc_len;
10590 ctl_set_success(ctsio);
10591 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10592 ctsio->be_move_done = ctl_config_move_done;
10593 ctl_datamove((union ctl_io *)ctsio);
10594 return (CTL_RETVAL_COMPLETE);
10598 ctl_get_event_status(struct ctl_scsiio *ctsio)
10600 struct scsi_get_event_status_header *hdr;
10601 struct scsi_get_event_status *cdb;
10602 struct ctl_lun *lun;
10603 uint32_t alloc_len, data_len;
10606 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10607 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10608 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10609 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10610 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10611 ctl_done((union ctl_io *)ctsio);
10612 return (CTL_RETVAL_COMPLETE);
10614 notif_class = cdb->notif_class;
10615 alloc_len = scsi_2btoul(cdb->length);
10617 data_len = sizeof(struct scsi_get_event_status_header);
10618 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10619 ctsio->kern_sg_entries = 0;
10620 ctsio->kern_data_resid = 0;
10621 ctsio->kern_rel_offset = 0;
10623 if (data_len < alloc_len) {
10624 ctsio->residual = alloc_len - data_len;
10625 ctsio->kern_data_len = data_len;
10626 ctsio->kern_total_len = data_len;
10628 ctsio->residual = 0;
10629 ctsio->kern_data_len = alloc_len;
10630 ctsio->kern_total_len = alloc_len;
10633 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10634 scsi_ulto2b(0, hdr->descr_length);
10635 hdr->nea_class = SGESN_NEA;
10636 hdr->supported_class = 0;
10638 ctl_set_success(ctsio);
10639 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10640 ctsio->be_move_done = ctl_config_move_done;
10641 ctl_datamove((union ctl_io *)ctsio);
10642 return (CTL_RETVAL_COMPLETE);
10646 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10648 struct scsi_mechanism_status_header *hdr;
10649 struct scsi_mechanism_status *cdb;
10650 struct ctl_lun *lun;
10651 uint32_t alloc_len, data_len;
10653 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10654 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10655 alloc_len = scsi_2btoul(cdb->length);
10657 data_len = sizeof(struct scsi_mechanism_status_header);
10658 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10659 ctsio->kern_sg_entries = 0;
10660 ctsio->kern_data_resid = 0;
10661 ctsio->kern_rel_offset = 0;
10663 if (data_len < alloc_len) {
10664 ctsio->residual = alloc_len - data_len;
10665 ctsio->kern_data_len = data_len;
10666 ctsio->kern_total_len = data_len;
10668 ctsio->residual = 0;
10669 ctsio->kern_data_len = alloc_len;
10670 ctsio->kern_total_len = alloc_len;
10673 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10674 hdr->state1 = 0x00;
10675 hdr->state2 = 0xe0;
10676 scsi_ulto3b(0, hdr->lba);
10677 hdr->slots_num = 0;
10678 scsi_ulto2b(0, hdr->slots_length);
10680 ctl_set_success(ctsio);
10681 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10682 ctsio->be_move_done = ctl_config_move_done;
10683 ctl_datamove((union ctl_io *)ctsio);
10684 return (CTL_RETVAL_COMPLETE);
10688 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10693 buf[1] = bin2bcd((lba / 75) / 60);
10694 buf[2] = bin2bcd((lba / 75) % 60);
10695 buf[3] = bin2bcd(lba % 75);
10699 ctl_read_toc(struct ctl_scsiio *ctsio)
10701 struct scsi_read_toc_hdr *hdr;
10702 struct scsi_read_toc_type01_descr *descr;
10703 struct scsi_read_toc *cdb;
10704 struct ctl_lun *lun;
10705 uint32_t alloc_len, data_len;
10708 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10709 cdb = (struct scsi_read_toc *)ctsio->cdb;
10710 msf = (cdb->byte2 & CD_MSF) != 0;
10711 format = cdb->format;
10712 alloc_len = scsi_2btoul(cdb->data_len);
10714 data_len = sizeof(struct scsi_read_toc_hdr);
10716 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10718 data_len += sizeof(struct scsi_read_toc_type01_descr);
10719 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10720 ctsio->kern_sg_entries = 0;
10721 ctsio->kern_data_resid = 0;
10722 ctsio->kern_rel_offset = 0;
10724 if (data_len < alloc_len) {
10725 ctsio->residual = alloc_len - data_len;
10726 ctsio->kern_data_len = data_len;
10727 ctsio->kern_total_len = data_len;
10729 ctsio->residual = 0;
10730 ctsio->kern_data_len = alloc_len;
10731 ctsio->kern_total_len = alloc_len;
10734 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10736 scsi_ulto2b(0x12, hdr->data_length);
10739 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10740 descr->addr_ctl = 0x14;
10741 descr->track_number = 1;
10743 ctl_ultomsf(0, descr->track_start);
10745 scsi_ulto4b(0, descr->track_start);
10747 descr->addr_ctl = 0x14;
10748 descr->track_number = 0xaa;
10750 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10752 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10754 scsi_ulto2b(0x0a, hdr->data_length);
10757 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10758 descr->addr_ctl = 0x14;
10759 descr->track_number = 1;
10761 ctl_ultomsf(0, descr->track_start);
10763 scsi_ulto4b(0, descr->track_start);
10766 ctl_set_success(ctsio);
10767 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10768 ctsio->be_move_done = ctl_config_move_done;
10769 ctl_datamove((union ctl_io *)ctsio);
10770 return (CTL_RETVAL_COMPLETE);
10774 * For known CDB types, parse the LBA and length.
10777 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10779 if (io->io_hdr.io_type != CTL_IO_SCSI)
10782 switch (io->scsiio.cdb[0]) {
10783 case COMPARE_AND_WRITE: {
10784 struct scsi_compare_and_write *cdb;
10786 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10788 *lba = scsi_8btou64(cdb->addr);
10789 *len = cdb->length;
10794 struct scsi_rw_6 *cdb;
10796 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10798 *lba = scsi_3btoul(cdb->addr);
10799 /* only 5 bits are valid in the most significant address byte */
10801 *len = cdb->length;
10806 struct scsi_rw_10 *cdb;
10808 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10810 *lba = scsi_4btoul(cdb->addr);
10811 *len = scsi_2btoul(cdb->length);
10814 case WRITE_VERIFY_10: {
10815 struct scsi_write_verify_10 *cdb;
10817 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10819 *lba = scsi_4btoul(cdb->addr);
10820 *len = scsi_2btoul(cdb->length);
10825 struct scsi_rw_12 *cdb;
10827 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10829 *lba = scsi_4btoul(cdb->addr);
10830 *len = scsi_4btoul(cdb->length);
10833 case WRITE_VERIFY_12: {
10834 struct scsi_write_verify_12 *cdb;
10836 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10838 *lba = scsi_4btoul(cdb->addr);
10839 *len = scsi_4btoul(cdb->length);
10844 struct scsi_rw_16 *cdb;
10846 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10848 *lba = scsi_8btou64(cdb->addr);
10849 *len = scsi_4btoul(cdb->length);
10852 case WRITE_ATOMIC_16: {
10853 struct scsi_write_atomic_16 *cdb;
10855 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10857 *lba = scsi_8btou64(cdb->addr);
10858 *len = scsi_2btoul(cdb->length);
10861 case WRITE_VERIFY_16: {
10862 struct scsi_write_verify_16 *cdb;
10864 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10866 *lba = scsi_8btou64(cdb->addr);
10867 *len = scsi_4btoul(cdb->length);
10870 case WRITE_SAME_10: {
10871 struct scsi_write_same_10 *cdb;
10873 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10875 *lba = scsi_4btoul(cdb->addr);
10876 *len = scsi_2btoul(cdb->length);
10879 case WRITE_SAME_16: {
10880 struct scsi_write_same_16 *cdb;
10882 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10884 *lba = scsi_8btou64(cdb->addr);
10885 *len = scsi_4btoul(cdb->length);
10889 struct scsi_verify_10 *cdb;
10891 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10893 *lba = scsi_4btoul(cdb->addr);
10894 *len = scsi_2btoul(cdb->length);
10898 struct scsi_verify_12 *cdb;
10900 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10902 *lba = scsi_4btoul(cdb->addr);
10903 *len = scsi_4btoul(cdb->length);
10907 struct scsi_verify_16 *cdb;
10909 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10911 *lba = scsi_8btou64(cdb->addr);
10912 *len = scsi_4btoul(cdb->length);
10920 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10921 struct scsi_get_lba_status *cdb;
10923 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10924 *lba = scsi_8btou64(cdb->addr);
10930 break; /* NOTREACHED */
10937 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10940 uint64_t endlba1, endlba2;
10942 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10943 endlba2 = lba2 + len2 - 1;
10945 if ((endlba1 < lba2) || (endlba2 < lba1))
10946 return (CTL_ACTION_PASS);
10948 return (CTL_ACTION_BLOCK);
10952 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10954 struct ctl_ptr_len_flags *ptrlen;
10955 struct scsi_unmap_desc *buf, *end, *range;
10959 /* If not UNMAP -- go other way. */
10960 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10961 io->scsiio.cdb[0] != UNMAP)
10962 return (CTL_ACTION_ERROR);
10964 /* If UNMAP without data -- block and wait for data. */
10965 ptrlen = (struct ctl_ptr_len_flags *)
10966 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10967 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10968 ptrlen->ptr == NULL)
10969 return (CTL_ACTION_BLOCK);
10971 /* UNMAP with data -- check for collision. */
10972 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10973 end = buf + ptrlen->len / sizeof(*buf);
10974 for (range = buf; range < end; range++) {
10975 lba = scsi_8btou64(range->lba);
10976 len = scsi_4btoul(range->length);
10977 if ((lba < lba2 + len2) && (lba + len > lba2))
10978 return (CTL_ACTION_BLOCK);
10980 return (CTL_ACTION_PASS);
10984 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10986 uint64_t lba1, lba2;
10987 uint64_t len1, len2;
10990 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10991 return (CTL_ACTION_ERROR);
10993 retval = ctl_extent_check_unmap(io1, lba2, len2);
10994 if (retval != CTL_ACTION_ERROR)
10997 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10998 return (CTL_ACTION_ERROR);
11000 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
11002 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
11006 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
11008 uint64_t lba1, lba2;
11009 uint64_t len1, len2;
11011 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
11012 return (CTL_ACTION_PASS);
11013 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
11014 return (CTL_ACTION_ERROR);
11015 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
11016 return (CTL_ACTION_ERROR);
11018 if (lba1 + len1 == lba2)
11019 return (CTL_ACTION_BLOCK);
11020 return (CTL_ACTION_PASS);
11024 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
11025 union ctl_io *ooa_io)
11027 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
11028 const ctl_serialize_action *serialize_row;
11031 * The initiator attempted multiple untagged commands at the same
11032 * time. Can't do that.
11034 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11035 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11036 && ((pending_io->io_hdr.nexus.targ_port ==
11037 ooa_io->io_hdr.nexus.targ_port)
11038 && (pending_io->io_hdr.nexus.initid ==
11039 ooa_io->io_hdr.nexus.initid))
11040 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11041 CTL_FLAG_STATUS_SENT)) == 0))
11042 return (CTL_ACTION_OVERLAP);
11045 * The initiator attempted to send multiple tagged commands with
11046 * the same ID. (It's fine if different initiators have the same
11049 * Even if all of those conditions are true, we don't kill the I/O
11050 * if the command ahead of us has been aborted. We won't end up
11051 * sending it to the FETD, and it's perfectly legal to resend a
11052 * command with the same tag number as long as the previous
11053 * instance of this tag number has been aborted somehow.
11055 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11056 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11057 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11058 && ((pending_io->io_hdr.nexus.targ_port ==
11059 ooa_io->io_hdr.nexus.targ_port)
11060 && (pending_io->io_hdr.nexus.initid ==
11061 ooa_io->io_hdr.nexus.initid))
11062 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11063 CTL_FLAG_STATUS_SENT)) == 0))
11064 return (CTL_ACTION_OVERLAP_TAG);
11067 * If we get a head of queue tag, SAM-3 says that we should
11068 * immediately execute it.
11070 * What happens if this command would normally block for some other
11071 * reason? e.g. a request sense with a head of queue tag
11072 * immediately after a write. Normally that would block, but this
11073 * will result in its getting executed immediately...
11075 * We currently return "pass" instead of "skip", so we'll end up
11076 * going through the rest of the queue to check for overlapped tags.
11078 * XXX KDM check for other types of blockage first??
11080 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11081 return (CTL_ACTION_PASS);
11084 * Ordered tags have to block until all items ahead of them
11085 * have completed. If we get called with an ordered tag, we always
11086 * block, if something else is ahead of us in the queue.
11088 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11089 return (CTL_ACTION_BLOCK);
11092 * Simple tags get blocked until all head of queue and ordered tags
11093 * ahead of them have completed. I'm lumping untagged commands in
11094 * with simple tags here. XXX KDM is that the right thing to do?
11096 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11097 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11098 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11099 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11100 return (CTL_ACTION_BLOCK);
11102 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11103 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11104 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11105 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11106 pending_io->scsiio.cdb[1], pending_io));
11107 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11108 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11109 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11110 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11111 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11112 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11113 ooa_io->scsiio.cdb[1], ooa_io));
11115 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11117 switch (serialize_row[pending_entry->seridx]) {
11118 case CTL_SER_BLOCK:
11119 return (CTL_ACTION_BLOCK);
11120 case CTL_SER_EXTENT:
11121 return (ctl_extent_check(ooa_io, pending_io,
11122 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11123 case CTL_SER_EXTENTOPT:
11124 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11125 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11126 return (ctl_extent_check(ooa_io, pending_io,
11128 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11129 return (CTL_ACTION_PASS);
11130 case CTL_SER_EXTENTSEQ:
11131 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11132 return (ctl_extent_check_seq(ooa_io, pending_io));
11133 return (CTL_ACTION_PASS);
11135 return (CTL_ACTION_PASS);
11136 case CTL_SER_BLOCKOPT:
11137 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11138 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11139 return (CTL_ACTION_BLOCK);
11140 return (CTL_ACTION_PASS);
11142 return (CTL_ACTION_SKIP);
11144 panic("%s: Invalid serialization value %d for %d => %d",
11145 __func__, serialize_row[pending_entry->seridx],
11146 pending_entry->seridx, ooa_entry->seridx);
11149 return (CTL_ACTION_ERROR);
11153 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11155 * - pending_io is generally either incoming, or on the blocked queue
11156 * - starting I/O is the I/O we want to start the check with.
11159 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11160 union ctl_io *starting_io)
11162 union ctl_io *ooa_io;
11165 mtx_assert(&lun->lun_lock, MA_OWNED);
11168 * Run back along the OOA queue, starting with the current
11169 * blocked I/O and going through every I/O before it on the
11170 * queue. If starting_io is NULL, we'll just end up returning
11173 for (ooa_io = starting_io; ooa_io != NULL;
11174 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11178 * This routine just checks to see whether
11179 * cur_blocked is blocked by ooa_io, which is ahead
11180 * of it in the queue. It doesn't queue/dequeue
11183 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11185 case CTL_ACTION_BLOCK:
11186 case CTL_ACTION_OVERLAP:
11187 case CTL_ACTION_OVERLAP_TAG:
11188 case CTL_ACTION_SKIP:
11189 case CTL_ACTION_ERROR:
11191 break; /* NOTREACHED */
11192 case CTL_ACTION_PASS:
11195 panic("%s: Invalid action %d\n", __func__, action);
11199 return (CTL_ACTION_PASS);
11204 * - An I/O has just completed, and has been removed from the per-LUN OOA
11205 * queue, so some items on the blocked queue may now be unblocked.
11208 ctl_check_blocked(struct ctl_lun *lun)
11210 struct ctl_softc *softc = lun->ctl_softc;
11211 union ctl_io *cur_blocked, *next_blocked;
11213 mtx_assert(&lun->lun_lock, MA_OWNED);
11216 * Run forward from the head of the blocked queue, checking each
11217 * entry against the I/Os prior to it on the OOA queue to see if
11218 * there is still any blockage.
11220 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11221 * with our removing a variable on it while it is traversing the
11224 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11225 cur_blocked != NULL; cur_blocked = next_blocked) {
11226 union ctl_io *prev_ooa;
11229 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11232 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11233 ctl_ooaq, ooa_links);
11236 * If cur_blocked happens to be the first item in the OOA
11237 * queue now, prev_ooa will be NULL, and the action
11238 * returned will just be CTL_ACTION_PASS.
11240 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11243 case CTL_ACTION_BLOCK:
11244 /* Nothing to do here, still blocked */
11246 case CTL_ACTION_OVERLAP:
11247 case CTL_ACTION_OVERLAP_TAG:
11249 * This shouldn't happen! In theory we've already
11250 * checked this command for overlap...
11253 case CTL_ACTION_PASS:
11254 case CTL_ACTION_SKIP: {
11255 const struct ctl_cmd_entry *entry;
11258 * The skip case shouldn't happen, this transaction
11259 * should have never made it onto the blocked queue.
11262 * This I/O is no longer blocked, we can remove it
11263 * from the blocked queue. Since this is a TAILQ
11264 * (doubly linked list), we can do O(1) removals
11265 * from any place on the list.
11267 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11269 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11271 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11272 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11274 * Need to send IO back to original side to
11277 union ctl_ha_msg msg_info;
11279 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11280 msg_info.hdr.original_sc =
11281 cur_blocked->io_hdr.original_sc;
11282 msg_info.hdr.serializing_sc = cur_blocked;
11283 msg_info.hdr.msg_type = CTL_MSG_R2R;
11284 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11285 sizeof(msg_info.hdr), M_NOWAIT);
11288 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11291 * Check this I/O for LUN state changes that may
11292 * have happened while this command was blocked.
11293 * The LUN state may have been changed by a command
11294 * ahead of us in the queue, so we need to re-check
11295 * for any states that can be caused by SCSI
11298 if (ctl_scsiio_lun_check(lun, entry,
11299 &cur_blocked->scsiio) == 0) {
11300 cur_blocked->io_hdr.flags |=
11301 CTL_FLAG_IS_WAS_ON_RTR;
11302 ctl_enqueue_rtr(cur_blocked);
11304 ctl_done(cur_blocked);
11309 * This probably shouldn't happen -- we shouldn't
11310 * get CTL_ACTION_ERROR, or anything else.
11316 return (CTL_RETVAL_COMPLETE);
11320 * This routine (with one exception) checks LUN flags that can be set by
11321 * commands ahead of us in the OOA queue. These flags have to be checked
11322 * when a command initially comes in, and when we pull a command off the
11323 * blocked queue and are preparing to execute it. The reason we have to
11324 * check these flags for commands on the blocked queue is that the LUN
11325 * state may have been changed by a command ahead of us while we're on the
11328 * Ordering is somewhat important with these checks, so please pay
11329 * careful attention to the placement of any new checks.
11332 ctl_scsiio_lun_check(struct ctl_lun *lun,
11333 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11335 struct ctl_softc *softc = lun->ctl_softc;
11341 mtx_assert(&lun->lun_lock, MA_OWNED);
11344 * If this shelf is a secondary shelf controller, we may have to
11345 * reject some commands disallowed by HA mode and link state.
11347 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11348 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11349 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11350 ctl_set_lun_unavail(ctsio);
11354 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11355 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11356 ctl_set_lun_transit(ctsio);
11360 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11361 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11362 ctl_set_lun_standby(ctsio);
11367 /* The rest of checks are only done on executing side */
11368 if (softc->ha_mode == CTL_HA_MODE_XFER)
11372 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11374 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11375 ctl_set_hw_write_protected(ctsio);
11379 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11380 .eca_and_aen & SCP_SWP) != 0) {
11381 ctl_set_sense(ctsio, /*current_error*/ 1,
11382 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11383 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11390 * Check for a reservation conflict. If this command isn't allowed
11391 * even on reserved LUNs, and if this initiator isn't the one who
11392 * reserved us, reject the command with a reservation conflict.
11394 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11395 if ((lun->flags & CTL_LUN_RESERVED)
11396 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11397 if (lun->res_idx != residx) {
11398 ctl_set_reservation_conflict(ctsio);
11404 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11405 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11406 /* No reservation or command is allowed. */;
11407 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11408 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11409 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11410 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11411 /* The command is allowed for Write Exclusive resv. */;
11414 * if we aren't registered or it's a res holder type
11415 * reservation and this isn't the res holder then set a
11418 if (ctl_get_prkey(lun, residx) == 0 ||
11419 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11420 ctl_set_reservation_conflict(ctsio);
11426 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11427 if (lun->flags & CTL_LUN_EJECTED)
11428 ctl_set_lun_ejected(ctsio);
11429 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11430 if (lun->flags & CTL_LUN_REMOVABLE)
11431 ctl_set_lun_no_media(ctsio);
11433 ctl_set_lun_int_reqd(ctsio);
11434 } else if (lun->flags & CTL_LUN_STOPPED)
11435 ctl_set_lun_stopped(ctsio);
11447 ctl_failover_io(union ctl_io *io, int have_lock)
11449 ctl_set_busy(&io->scsiio);
11454 ctl_failover_lun(union ctl_io *rio)
11456 struct ctl_softc *softc = control_softc;
11457 struct ctl_lun *lun;
11458 struct ctl_io_hdr *io, *next_io;
11461 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11462 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11464 /* Find and lock the LUN. */
11465 mtx_lock(&softc->ctl_lock);
11466 if ((targ_lun < CTL_MAX_LUNS) &&
11467 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11468 mtx_lock(&lun->lun_lock);
11469 mtx_unlock(&softc->ctl_lock);
11470 if (lun->flags & CTL_LUN_DISABLED) {
11471 mtx_unlock(&lun->lun_lock);
11475 mtx_unlock(&softc->ctl_lock);
11479 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11480 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11481 /* We are master */
11482 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11483 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11484 io->flags |= CTL_FLAG_ABORT;
11485 io->flags |= CTL_FLAG_FAILOVER;
11486 } else { /* This can be only due to DATAMOVE */
11487 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11488 io->flags &= ~CTL_FLAG_DMA_INPROG;
11489 io->flags |= CTL_FLAG_IO_ACTIVE;
11490 io->port_status = 31340;
11491 ctl_enqueue_isc((union ctl_io *)io);
11495 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11496 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11497 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11498 io->flags |= CTL_FLAG_FAILOVER;
11500 ctl_set_busy(&((union ctl_io *)io)->
11502 ctl_done((union ctl_io *)io);
11506 } else { /* SERIALIZE modes */
11507 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11509 /* We are master */
11510 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11511 TAILQ_REMOVE(&lun->blocked_queue, io,
11513 io->flags &= ~CTL_FLAG_BLOCKED;
11514 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11515 ctl_free_io((union ctl_io *)io);
11518 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11519 /* We are master */
11520 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11521 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11522 ctl_free_io((union ctl_io *)io);
11525 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11526 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11527 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11528 ctl_set_busy(&((union ctl_io *)io)->
11530 ctl_done((union ctl_io *)io);
11534 ctl_check_blocked(lun);
11536 mtx_unlock(&lun->lun_lock);
11540 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11542 struct ctl_lun *lun;
11543 const struct ctl_cmd_entry *entry;
11544 uint32_t initidx, targ_lun;
11551 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11552 if ((targ_lun < CTL_MAX_LUNS)
11553 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11555 * If the LUN is invalid, pretend that it doesn't exist.
11556 * It will go away as soon as all pending I/O has been
11559 mtx_lock(&lun->lun_lock);
11560 if (lun->flags & CTL_LUN_DISABLED) {
11561 mtx_unlock(&lun->lun_lock);
11563 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11564 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11566 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11567 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11571 * Every I/O goes into the OOA queue for a
11572 * particular LUN, and stays there until completion.
11575 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11576 lun->idle_time += getsbinuptime() -
11580 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11584 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11585 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11588 /* Get command entry and return error if it is unsuppotyed. */
11589 entry = ctl_validate_command(ctsio);
11590 if (entry == NULL) {
11592 mtx_unlock(&lun->lun_lock);
11596 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11597 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11600 * Check to see whether we can send this command to LUNs that don't
11601 * exist. This should pretty much only be the case for inquiry
11602 * and request sense. Further checks, below, really require having
11603 * a LUN, so we can't really check the command anymore. Just put
11604 * it on the rtr queue.
11607 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11608 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11609 ctl_enqueue_rtr((union ctl_io *)ctsio);
11613 ctl_set_unsupported_lun(ctsio);
11614 ctl_done((union ctl_io *)ctsio);
11615 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11619 * Make sure we support this particular command on this LUN.
11620 * e.g., we don't support writes to the control LUN.
11622 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11623 mtx_unlock(&lun->lun_lock);
11624 ctl_set_invalid_opcode(ctsio);
11625 ctl_done((union ctl_io *)ctsio);
11630 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11634 * If we've got a request sense, it'll clear the contingent
11635 * allegiance condition. Otherwise, if we have a CA condition for
11636 * this initiator, clear it, because it sent down a command other
11637 * than request sense.
11639 if ((ctsio->cdb[0] != REQUEST_SENSE)
11640 && (ctl_is_set(lun->have_ca, initidx)))
11641 ctl_clear_mask(lun->have_ca, initidx);
11645 * If the command has this flag set, it handles its own unit
11646 * attention reporting, we shouldn't do anything. Otherwise we
11647 * check for any pending unit attentions, and send them back to the
11648 * initiator. We only do this when a command initially comes in,
11649 * not when we pull it off the blocked queue.
11651 * According to SAM-3, section 5.3.2, the order that things get
11652 * presented back to the host is basically unit attentions caused
11653 * by some sort of reset event, busy status, reservation conflicts
11654 * or task set full, and finally any other status.
11656 * One issue here is that some of the unit attentions we report
11657 * don't fall into the "reset" category (e.g. "reported luns data
11658 * has changed"). So reporting it here, before the reservation
11659 * check, may be technically wrong. I guess the only thing to do
11660 * would be to check for and report the reset events here, and then
11661 * check for the other unit attention types after we check for a
11662 * reservation conflict.
11664 * XXX KDM need to fix this
11666 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11667 ctl_ua_type ua_type;
11669 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11671 if (ua_type != CTL_UA_NONE) {
11672 mtx_unlock(&lun->lun_lock);
11673 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11674 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11675 ctsio->sense_len = SSD_FULL_SIZE;
11676 ctl_done((union ctl_io *)ctsio);
11682 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11683 mtx_unlock(&lun->lun_lock);
11684 ctl_done((union ctl_io *)ctsio);
11689 * XXX CHD this is where we want to send IO to other side if
11690 * this LUN is secondary on this SC. We will need to make a copy
11691 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11692 * the copy we send as FROM_OTHER.
11693 * We also need to stuff the address of the original IO so we can
11694 * find it easily. Something similar will need be done on the other
11695 * side so when we are done we can find the copy.
11697 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11698 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11699 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11700 union ctl_ha_msg msg_info;
11703 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11704 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11705 mtx_unlock(&lun->lun_lock);
11707 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11708 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11709 msg_info.hdr.serializing_sc = NULL;
11710 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11711 msg_info.scsi.tag_num = ctsio->tag_num;
11712 msg_info.scsi.tag_type = ctsio->tag_type;
11713 msg_info.scsi.cdb_len = ctsio->cdb_len;
11714 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11716 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11717 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11718 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11719 ctl_set_busy(ctsio);
11720 ctl_done((union ctl_io *)ctsio);
11726 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11727 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11728 ctl_ooaq, ooa_links))) {
11729 case CTL_ACTION_BLOCK:
11730 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11731 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11733 mtx_unlock(&lun->lun_lock);
11735 case CTL_ACTION_PASS:
11736 case CTL_ACTION_SKIP:
11737 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11738 mtx_unlock(&lun->lun_lock);
11739 ctl_enqueue_rtr((union ctl_io *)ctsio);
11741 case CTL_ACTION_OVERLAP:
11742 mtx_unlock(&lun->lun_lock);
11743 ctl_set_overlapped_cmd(ctsio);
11744 ctl_done((union ctl_io *)ctsio);
11746 case CTL_ACTION_OVERLAP_TAG:
11747 mtx_unlock(&lun->lun_lock);
11748 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11749 ctl_done((union ctl_io *)ctsio);
11751 case CTL_ACTION_ERROR:
11753 mtx_unlock(&lun->lun_lock);
11754 ctl_set_internal_failure(ctsio,
11756 /*retry_count*/ 0);
11757 ctl_done((union ctl_io *)ctsio);
11763 const struct ctl_cmd_entry *
11764 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11766 const struct ctl_cmd_entry *entry;
11767 int service_action;
11769 entry = &ctl_cmd_table[ctsio->cdb[0]];
11771 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11772 if (entry->flags & CTL_CMD_FLAG_SA5) {
11773 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11774 entry = &((const struct ctl_cmd_entry *)
11775 entry->execute)[service_action];
11780 const struct ctl_cmd_entry *
11781 ctl_validate_command(struct ctl_scsiio *ctsio)
11783 const struct ctl_cmd_entry *entry;
11787 entry = ctl_get_cmd_entry(ctsio, &sa);
11788 if (entry->execute == NULL) {
11790 ctl_set_invalid_field(ctsio,
11797 ctl_set_invalid_opcode(ctsio);
11798 ctl_done((union ctl_io *)ctsio);
11801 KASSERT(entry->length > 0,
11802 ("Not defined length for command 0x%02x/0x%02x",
11803 ctsio->cdb[0], ctsio->cdb[1]));
11804 for (i = 1; i < entry->length; i++) {
11805 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11808 ctl_set_invalid_field(ctsio,
11813 /*bit*/ fls(diff) - 1);
11814 ctl_done((union ctl_io *)ctsio);
11821 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11824 switch (lun_type) {
11826 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11830 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11834 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11844 ctl_scsiio(struct ctl_scsiio *ctsio)
11847 const struct ctl_cmd_entry *entry;
11849 retval = CTL_RETVAL_COMPLETE;
11851 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11853 entry = ctl_get_cmd_entry(ctsio, NULL);
11856 * If this I/O has been aborted, just send it straight to
11857 * ctl_done() without executing it.
11859 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11860 ctl_done((union ctl_io *)ctsio);
11865 * All the checks should have been handled by ctl_scsiio_precheck().
11866 * We should be clear now to just execute the I/O.
11868 retval = entry->execute(ctsio);
11875 * Since we only implement one target right now, a bus reset simply resets
11876 * our single target.
11879 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11881 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11885 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11886 ctl_ua_type ua_type)
11888 struct ctl_port *port;
11889 struct ctl_lun *lun;
11892 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11893 union ctl_ha_msg msg_info;
11895 msg_info.hdr.nexus = io->io_hdr.nexus;
11896 if (ua_type==CTL_UA_TARG_RESET)
11897 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11899 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11900 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11901 msg_info.hdr.original_sc = NULL;
11902 msg_info.hdr.serializing_sc = NULL;
11903 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11904 sizeof(msg_info.task), M_WAITOK);
11908 mtx_lock(&softc->ctl_lock);
11909 port = ctl_io_port(&io->io_hdr);
11910 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11911 if (port != NULL &&
11912 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11914 retval += ctl_do_lun_reset(lun, io, ua_type);
11916 mtx_unlock(&softc->ctl_lock);
11917 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11922 * The LUN should always be set. The I/O is optional, and is used to
11923 * distinguish between I/Os sent by this initiator, and by other
11924 * initiators. We set unit attention for initiators other than this one.
11925 * SAM-3 is vague on this point. It does say that a unit attention should
11926 * be established for other initiators when a LUN is reset (see section
11927 * 5.7.3), but it doesn't specifically say that the unit attention should
11928 * be established for this particular initiator when a LUN is reset. Here
11929 * is the relevant text, from SAM-3 rev 8:
11931 * 5.7.2 When a SCSI initiator port aborts its own tasks
11933 * When a SCSI initiator port causes its own task(s) to be aborted, no
11934 * notification that the task(s) have been aborted shall be returned to
11935 * the SCSI initiator port other than the completion response for the
11936 * command or task management function action that caused the task(s) to
11937 * be aborted and notification(s) associated with related effects of the
11938 * action (e.g., a reset unit attention condition).
11940 * XXX KDM for now, we're setting unit attention for all initiators.
11943 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11951 mtx_lock(&lun->lun_lock);
11953 * Run through the OOA queue and abort each I/O.
11955 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11956 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11957 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11961 * This version sets unit attention for every
11964 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11965 ctl_est_ua_all(lun, initidx, ua_type);
11967 ctl_est_ua_all(lun, -1, ua_type);
11971 * A reset (any kind, really) clears reservations established with
11972 * RESERVE/RELEASE. It does not clear reservations established
11973 * with PERSISTENT RESERVE OUT, but we don't support that at the
11974 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11975 * reservations made with the RESERVE/RELEASE commands, because
11976 * those commands are obsolete in SPC-3.
11978 lun->flags &= ~CTL_LUN_RESERVED;
11981 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11982 ctl_clear_mask(lun->have_ca, i);
11984 lun->prevent_count = 0;
11985 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11986 ctl_clear_mask(lun->prevent, i);
11987 mtx_unlock(&lun->lun_lock);
11993 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11995 struct ctl_lun *lun;
11999 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12000 mtx_lock(&softc->ctl_lock);
12001 if ((targ_lun >= CTL_MAX_LUNS) ||
12002 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12003 mtx_unlock(&softc->ctl_lock);
12004 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12007 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
12008 mtx_unlock(&softc->ctl_lock);
12009 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12011 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
12012 union ctl_ha_msg msg_info;
12014 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12015 msg_info.hdr.nexus = io->io_hdr.nexus;
12016 msg_info.task.task_action = CTL_TASK_LUN_RESET;
12017 msg_info.hdr.original_sc = NULL;
12018 msg_info.hdr.serializing_sc = NULL;
12019 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12020 sizeof(msg_info.task), M_WAITOK);
12026 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12031 mtx_assert(&lun->lun_lock, MA_OWNED);
12034 * Run through the OOA queue and attempt to find the given I/O.
12035 * The target port, initiator ID, tag type and tag number have to
12036 * match the values that we got from the initiator. If we have an
12037 * untagged command to abort, simply abort the first untagged command
12038 * we come to. We only allow one untagged command at a time of course.
12040 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12041 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12043 if ((targ_port == UINT32_MAX ||
12044 targ_port == xio->io_hdr.nexus.targ_port) &&
12045 (init_id == UINT32_MAX ||
12046 init_id == xio->io_hdr.nexus.initid)) {
12047 if (targ_port != xio->io_hdr.nexus.targ_port ||
12048 init_id != xio->io_hdr.nexus.initid)
12049 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12050 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12051 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12052 union ctl_ha_msg msg_info;
12054 msg_info.hdr.nexus = xio->io_hdr.nexus;
12055 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12056 msg_info.task.tag_num = xio->scsiio.tag_num;
12057 msg_info.task.tag_type = xio->scsiio.tag_type;
12058 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12059 msg_info.hdr.original_sc = NULL;
12060 msg_info.hdr.serializing_sc = NULL;
12061 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12062 sizeof(msg_info.task), M_NOWAIT);
12069 ctl_abort_task_set(union ctl_io *io)
12071 struct ctl_softc *softc = control_softc;
12072 struct ctl_lun *lun;
12078 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12079 mtx_lock(&softc->ctl_lock);
12080 if ((targ_lun >= CTL_MAX_LUNS) ||
12081 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12082 mtx_unlock(&softc->ctl_lock);
12083 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12087 mtx_lock(&lun->lun_lock);
12088 mtx_unlock(&softc->ctl_lock);
12089 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12090 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12091 io->io_hdr.nexus.initid,
12092 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12093 } else { /* CTL_TASK_CLEAR_TASK_SET */
12094 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12095 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12097 mtx_unlock(&lun->lun_lock);
12098 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12103 ctl_i_t_nexus_reset(union ctl_io *io)
12105 struct ctl_softc *softc = control_softc;
12106 struct ctl_lun *lun;
12109 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12110 union ctl_ha_msg msg_info;
12112 msg_info.hdr.nexus = io->io_hdr.nexus;
12113 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12114 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12115 msg_info.hdr.original_sc = NULL;
12116 msg_info.hdr.serializing_sc = NULL;
12117 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12118 sizeof(msg_info.task), M_WAITOK);
12121 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12122 mtx_lock(&softc->ctl_lock);
12123 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12124 mtx_lock(&lun->lun_lock);
12125 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12126 io->io_hdr.nexus.initid, 1);
12128 ctl_clear_mask(lun->have_ca, initidx);
12130 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12131 lun->flags &= ~CTL_LUN_RESERVED;
12132 if (ctl_is_set(lun->prevent, initidx)) {
12133 ctl_clear_mask(lun->prevent, initidx);
12134 lun->prevent_count--;
12136 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12137 mtx_unlock(&lun->lun_lock);
12139 mtx_unlock(&softc->ctl_lock);
12140 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12145 ctl_abort_task(union ctl_io *io)
12148 struct ctl_lun *lun;
12149 struct ctl_softc *softc;
12152 char printbuf[128];
12157 softc = control_softc;
12163 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12164 mtx_lock(&softc->ctl_lock);
12165 if ((targ_lun >= CTL_MAX_LUNS) ||
12166 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12167 mtx_unlock(&softc->ctl_lock);
12168 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12173 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12174 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12177 mtx_lock(&lun->lun_lock);
12178 mtx_unlock(&softc->ctl_lock);
12180 * Run through the OOA queue and attempt to find the given I/O.
12181 * The target port, initiator ID, tag type and tag number have to
12182 * match the values that we got from the initiator. If we have an
12183 * untagged command to abort, simply abort the first untagged command
12184 * we come to. We only allow one untagged command at a time of course.
12186 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12187 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12189 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12191 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12192 lun->lun, xio->scsiio.tag_num,
12193 xio->scsiio.tag_type,
12194 (xio->io_hdr.blocked_links.tqe_prev
12195 == NULL) ? "" : " BLOCKED",
12196 (xio->io_hdr.flags &
12197 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12198 (xio->io_hdr.flags &
12199 CTL_FLAG_ABORT) ? " ABORT" : "",
12200 (xio->io_hdr.flags &
12201 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12202 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12204 printf("%s\n", sbuf_data(&sb));
12207 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12208 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12209 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12213 * If the abort says that the task is untagged, the
12214 * task in the queue must be untagged. Otherwise,
12215 * we just check to see whether the tag numbers
12216 * match. This is because the QLogic firmware
12217 * doesn't pass back the tag type in an abort
12221 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12222 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12223 || (xio->scsiio.tag_num == io->taskio.tag_num))
12226 * XXX KDM we've got problems with FC, because it
12227 * doesn't send down a tag type with aborts. So we
12228 * can only really go by the tag number...
12229 * This may cause problems with parallel SCSI.
12230 * Need to figure that out!!
12232 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12233 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12235 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12236 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12237 union ctl_ha_msg msg_info;
12239 msg_info.hdr.nexus = io->io_hdr.nexus;
12240 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12241 msg_info.task.tag_num = io->taskio.tag_num;
12242 msg_info.task.tag_type = io->taskio.tag_type;
12243 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12244 msg_info.hdr.original_sc = NULL;
12245 msg_info.hdr.serializing_sc = NULL;
12247 printf("Sent Abort to other side\n");
12249 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12250 sizeof(msg_info.task), M_NOWAIT);
12253 printf("ctl_abort_task: found I/O to abort\n");
12257 mtx_unlock(&lun->lun_lock);
12261 * This isn't really an error. It's entirely possible for
12262 * the abort and command completion to cross on the wire.
12263 * This is more of an informative/diagnostic error.
12266 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12267 "%u:%u:%u tag %d type %d\n",
12268 io->io_hdr.nexus.initid,
12269 io->io_hdr.nexus.targ_port,
12270 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12271 io->taskio.tag_type);
12274 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12279 ctl_query_task(union ctl_io *io, int task_set)
12282 struct ctl_lun *lun;
12283 struct ctl_softc *softc;
12287 softc = control_softc;
12288 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12289 mtx_lock(&softc->ctl_lock);
12290 if ((targ_lun >= CTL_MAX_LUNS) ||
12291 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12292 mtx_unlock(&softc->ctl_lock);
12293 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12296 mtx_lock(&lun->lun_lock);
12297 mtx_unlock(&softc->ctl_lock);
12298 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12299 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12301 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12302 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12303 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12306 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12311 mtx_unlock(&lun->lun_lock);
12313 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12315 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12320 ctl_query_async_event(union ctl_io *io)
12322 struct ctl_lun *lun;
12323 struct ctl_softc *softc;
12325 uint32_t targ_lun, initidx;
12327 softc = control_softc;
12328 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12329 mtx_lock(&softc->ctl_lock);
12330 if ((targ_lun >= CTL_MAX_LUNS) ||
12331 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12332 mtx_unlock(&softc->ctl_lock);
12333 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12336 mtx_lock(&lun->lun_lock);
12337 mtx_unlock(&softc->ctl_lock);
12338 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12339 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12340 mtx_unlock(&lun->lun_lock);
12341 if (ua != CTL_UA_NONE)
12342 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12344 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12349 ctl_run_task(union ctl_io *io)
12351 struct ctl_softc *softc = control_softc;
12354 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12355 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12356 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12357 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12358 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12359 switch (io->taskio.task_action) {
12360 case CTL_TASK_ABORT_TASK:
12361 retval = ctl_abort_task(io);
12363 case CTL_TASK_ABORT_TASK_SET:
12364 case CTL_TASK_CLEAR_TASK_SET:
12365 retval = ctl_abort_task_set(io);
12367 case CTL_TASK_CLEAR_ACA:
12369 case CTL_TASK_I_T_NEXUS_RESET:
12370 retval = ctl_i_t_nexus_reset(io);
12372 case CTL_TASK_LUN_RESET:
12373 retval = ctl_lun_reset(softc, io);
12375 case CTL_TASK_TARGET_RESET:
12376 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12378 case CTL_TASK_BUS_RESET:
12379 retval = ctl_bus_reset(softc, io);
12381 case CTL_TASK_PORT_LOGIN:
12383 case CTL_TASK_PORT_LOGOUT:
12385 case CTL_TASK_QUERY_TASK:
12386 retval = ctl_query_task(io, 0);
12388 case CTL_TASK_QUERY_TASK_SET:
12389 retval = ctl_query_task(io, 1);
12391 case CTL_TASK_QUERY_ASYNC_EVENT:
12392 retval = ctl_query_async_event(io);
12395 printf("%s: got unknown task management event %d\n",
12396 __func__, io->taskio.task_action);
12400 io->io_hdr.status = CTL_SUCCESS;
12402 io->io_hdr.status = CTL_ERROR;
12407 * For HA operation. Handle commands that come in from the other
12411 ctl_handle_isc(union ctl_io *io)
12414 struct ctl_lun *lun;
12415 struct ctl_softc *softc = control_softc;
12418 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12419 lun = softc->ctl_luns[targ_lun];
12421 switch (io->io_hdr.msg_type) {
12422 case CTL_MSG_SERIALIZE:
12423 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12425 case CTL_MSG_R2R: {
12426 const struct ctl_cmd_entry *entry;
12429 * This is only used in SER_ONLY mode.
12432 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12433 mtx_lock(&lun->lun_lock);
12434 if (ctl_scsiio_lun_check(lun,
12435 entry, (struct ctl_scsiio *)io) != 0) {
12436 mtx_unlock(&lun->lun_lock);
12440 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12441 mtx_unlock(&lun->lun_lock);
12442 ctl_enqueue_rtr(io);
12445 case CTL_MSG_FINISH_IO:
12446 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12451 mtx_lock(&lun->lun_lock);
12452 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12454 ctl_check_blocked(lun);
12455 mtx_unlock(&lun->lun_lock);
12458 case CTL_MSG_PERS_ACTION:
12459 ctl_hndl_per_res_out_on_other_sc(
12460 (union ctl_ha_msg *)&io->presio.pr_msg);
12463 case CTL_MSG_BAD_JUJU:
12467 case CTL_MSG_DATAMOVE:
12468 /* Only used in XFER mode */
12470 ctl_datamove_remote(io);
12472 case CTL_MSG_DATAMOVE_DONE:
12473 /* Only used in XFER mode */
12475 io->scsiio.be_move_done(io);
12477 case CTL_MSG_FAILOVER:
12478 ctl_failover_lun(io);
12483 printf("%s: Invalid message type %d\n",
12484 __func__, io->io_hdr.msg_type);
12494 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12495 * there is no match.
12497 static ctl_lun_error_pattern
12498 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12500 const struct ctl_cmd_entry *entry;
12501 ctl_lun_error_pattern filtered_pattern, pattern;
12503 pattern = desc->error_pattern;
12506 * XXX KDM we need more data passed into this function to match a
12507 * custom pattern, and we actually need to implement custom pattern
12510 if (pattern & CTL_LUN_PAT_CMD)
12511 return (CTL_LUN_PAT_CMD);
12513 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12514 return (CTL_LUN_PAT_ANY);
12516 entry = ctl_get_cmd_entry(ctsio, NULL);
12518 filtered_pattern = entry->pattern & pattern;
12521 * If the user requested specific flags in the pattern (e.g.
12522 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12525 * If the user did not specify any flags, it doesn't matter whether
12526 * or not the command supports the flags.
12528 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12529 (pattern & ~CTL_LUN_PAT_MASK))
12530 return (CTL_LUN_PAT_NONE);
12533 * If the user asked for a range check, see if the requested LBA
12534 * range overlaps with this command's LBA range.
12536 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12542 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12544 return (CTL_LUN_PAT_NONE);
12546 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12547 desc->lba_range.len, FALSE);
12549 * A "pass" means that the LBA ranges don't overlap, so
12550 * this doesn't match the user's range criteria.
12552 if (action == CTL_ACTION_PASS)
12553 return (CTL_LUN_PAT_NONE);
12556 return (filtered_pattern);
12560 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12562 struct ctl_error_desc *desc, *desc2;
12564 mtx_assert(&lun->lun_lock, MA_OWNED);
12566 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12567 ctl_lun_error_pattern pattern;
12569 * Check to see whether this particular command matches
12570 * the pattern in the descriptor.
12572 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12573 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12576 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12577 case CTL_LUN_INJ_ABORTED:
12578 ctl_set_aborted(&io->scsiio);
12580 case CTL_LUN_INJ_MEDIUM_ERR:
12581 ctl_set_medium_error(&io->scsiio,
12582 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12583 CTL_FLAG_DATA_OUT);
12585 case CTL_LUN_INJ_UA:
12586 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12588 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12590 case CTL_LUN_INJ_CUSTOM:
12592 * We're assuming the user knows what he is doing.
12593 * Just copy the sense information without doing
12596 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12597 MIN(sizeof(desc->custom_sense),
12598 sizeof(io->scsiio.sense_data)));
12599 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12600 io->scsiio.sense_len = SSD_FULL_SIZE;
12601 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12603 case CTL_LUN_INJ_NONE:
12606 * If this is an error injection type we don't know
12607 * about, clear the continuous flag (if it is set)
12608 * so it will get deleted below.
12610 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12614 * By default, each error injection action is a one-shot
12616 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12619 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12625 #ifdef CTL_IO_DELAY
12627 ctl_datamove_timer_wakeup(void *arg)
12631 io = (union ctl_io *)arg;
12635 #endif /* CTL_IO_DELAY */
12638 ctl_datamove(union ctl_io *io)
12640 struct ctl_lun *lun;
12641 void (*fe_datamove)(union ctl_io *io);
12643 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12645 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12647 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12649 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12654 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12655 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12657 sbuf_cat(&sb, path_str);
12658 switch (io->io_hdr.io_type) {
12660 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12661 sbuf_printf(&sb, "\n");
12662 sbuf_cat(&sb, path_str);
12663 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12664 io->scsiio.tag_num, io->scsiio.tag_type);
12667 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12668 "Tag Type: %d\n", io->taskio.task_action,
12669 io->taskio.tag_num, io->taskio.tag_type);
12672 panic("%s: Invalid CTL I/O type %d\n",
12673 __func__, io->io_hdr.io_type);
12675 sbuf_cat(&sb, path_str);
12676 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12677 (intmax_t)time_uptime - io->io_hdr.start_time);
12679 printf("%s", sbuf_data(&sb));
12681 #endif /* CTL_TIME_IO */
12683 #ifdef CTL_IO_DELAY
12684 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12685 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12688 && (lun->delay_info.datamove_delay > 0)) {
12690 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12691 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12692 callout_reset(&io->io_hdr.delay_callout,
12693 lun->delay_info.datamove_delay * hz,
12694 ctl_datamove_timer_wakeup, io);
12695 if (lun->delay_info.datamove_type ==
12696 CTL_DELAY_TYPE_ONESHOT)
12697 lun->delay_info.datamove_delay = 0;
12704 * This command has been aborted. Set the port status, so we fail
12707 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12708 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12709 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12710 io->io_hdr.nexus.targ_port,
12711 io->io_hdr.nexus.targ_lun);
12712 io->io_hdr.port_status = 31337;
12714 * Note that the backend, in this case, will get the
12715 * callback in its context. In other cases it may get
12716 * called in the frontend's interrupt thread context.
12718 io->scsiio.be_move_done(io);
12722 /* Don't confuse frontend with zero length data move. */
12723 if (io->scsiio.kern_data_len == 0) {
12724 io->scsiio.be_move_done(io);
12728 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12733 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12735 union ctl_ha_msg msg;
12737 struct bintime cur_bt;
12740 memset(&msg, 0, sizeof(msg));
12741 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12742 msg.hdr.original_sc = io;
12743 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12744 msg.hdr.nexus = io->io_hdr.nexus;
12745 msg.hdr.status = io->io_hdr.status;
12746 msg.scsi.tag_num = io->scsiio.tag_num;
12747 msg.scsi.tag_type = io->scsiio.tag_type;
12748 msg.scsi.scsi_status = io->scsiio.scsi_status;
12749 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12750 io->scsiio.sense_len);
12751 msg.scsi.sense_len = io->scsiio.sense_len;
12752 msg.scsi.sense_residual = io->scsiio.sense_residual;
12753 msg.scsi.fetd_status = io->io_hdr.port_status;
12754 msg.scsi.residual = io->scsiio.residual;
12755 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12756 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12757 ctl_failover_io(io, /*have_lock*/ have_lock);
12760 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12761 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12762 msg.scsi.sense_len, M_WAITOK);
12765 getbinuptime(&cur_bt);
12766 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12767 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12769 io->io_hdr.num_dmas++;
12773 * The DMA to the remote side is done, now we need to tell the other side
12774 * we're done so it can continue with its data movement.
12777 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12784 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12785 printf("%s: ISC DMA write failed with error %d", __func__,
12787 ctl_set_internal_failure(&io->scsiio,
12789 /*retry_count*/ rq->ret);
12792 ctl_dt_req_free(rq);
12794 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12795 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12796 free(io->io_hdr.remote_sglist, M_CTL);
12797 io->io_hdr.remote_sglist = NULL;
12798 io->io_hdr.local_sglist = NULL;
12801 * The data is in local and remote memory, so now we need to send
12802 * status (good or back) back to the other side.
12804 ctl_send_datamove_done(io, /*have_lock*/ 0);
12808 * We've moved the data from the host/controller into local memory. Now we
12809 * need to push it over to the remote controller's memory.
12812 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12816 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12817 ctl_datamove_remote_write_cb);
12822 ctl_datamove_remote_write(union ctl_io *io)
12825 void (*fe_datamove)(union ctl_io *io);
12828 * - Get the data from the host/HBA into local memory.
12829 * - DMA memory from the local controller to the remote controller.
12830 * - Send status back to the remote controller.
12833 retval = ctl_datamove_remote_sgl_setup(io);
12837 /* Switch the pointer over so the FETD knows what to do */
12838 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12841 * Use a custom move done callback, since we need to send completion
12842 * back to the other controller, not to the backend on this side.
12844 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12846 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12851 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12860 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12861 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12862 free(io->io_hdr.remote_sglist, M_CTL);
12863 io->io_hdr.remote_sglist = NULL;
12864 io->io_hdr.local_sglist = NULL;
12867 scsi_path_string(io, path_str, sizeof(path_str));
12868 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12869 sbuf_cat(&sb, path_str);
12870 scsi_command_string(&io->scsiio, NULL, &sb);
12871 sbuf_printf(&sb, "\n");
12872 sbuf_cat(&sb, path_str);
12873 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12874 io->scsiio.tag_num, io->scsiio.tag_type);
12875 sbuf_cat(&sb, path_str);
12876 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12877 io->io_hdr.flags, io->io_hdr.status);
12879 printk("%s", sbuf_data(&sb));
12884 * The read is done, now we need to send status (good or bad) back
12885 * to the other side.
12887 ctl_send_datamove_done(io, /*have_lock*/ 0);
12893 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12896 void (*fe_datamove)(union ctl_io *io);
12900 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12901 printf("%s: ISC DMA read failed with error %d\n", __func__,
12903 ctl_set_internal_failure(&io->scsiio,
12905 /*retry_count*/ rq->ret);
12908 ctl_dt_req_free(rq);
12910 /* Switch the pointer over so the FETD knows what to do */
12911 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12914 * Use a custom move done callback, since we need to send completion
12915 * back to the other controller, not to the backend on this side.
12917 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12919 /* XXX KDM add checks like the ones in ctl_datamove? */
12921 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12926 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12928 struct ctl_sg_entry *local_sglist;
12929 uint32_t len_to_go;
12934 local_sglist = io->io_hdr.local_sglist;
12935 len_to_go = io->scsiio.kern_data_len;
12938 * The difficult thing here is that the size of the various
12939 * S/G segments may be different than the size from the
12940 * remote controller. That'll make it harder when DMAing
12941 * the data back to the other side.
12943 for (i = 0; len_to_go > 0; i++) {
12944 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12945 local_sglist[i].addr =
12946 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12948 len_to_go -= local_sglist[i].len;
12951 * Reset the number of S/G entries accordingly. The original
12952 * number of S/G entries is available in rem_sg_entries.
12954 io->scsiio.kern_sg_entries = i;
12957 printf("%s: kern_sg_entries = %d\n", __func__,
12958 io->scsiio.kern_sg_entries);
12959 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12960 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12961 local_sglist[i].addr, local_sglist[i].len);
12968 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12969 ctl_ha_dt_cb callback)
12971 struct ctl_ha_dt_req *rq;
12972 struct ctl_sg_entry *remote_sglist, *local_sglist;
12973 uint32_t local_used, remote_used, total_used;
12976 rq = ctl_dt_req_alloc();
12979 * If we failed to allocate the request, and if the DMA didn't fail
12980 * anyway, set busy status. This is just a resource allocation
12984 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12985 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12986 ctl_set_busy(&io->scsiio);
12988 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12989 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12992 ctl_dt_req_free(rq);
12995 * The data move failed. We need to return status back
12996 * to the other controller. No point in trying to DMA
12997 * data to the remote controller.
13000 ctl_send_datamove_done(io, /*have_lock*/ 0);
13005 local_sglist = io->io_hdr.local_sglist;
13006 remote_sglist = io->io_hdr.remote_sglist;
13012 * Pull/push the data over the wire from/to the other controller.
13013 * This takes into account the possibility that the local and
13014 * remote sglists may not be identical in terms of the size of
13015 * the elements and the number of elements.
13017 * One fundamental assumption here is that the length allocated for
13018 * both the local and remote sglists is identical. Otherwise, we've
13019 * essentially got a coding error of some sort.
13021 isc_ret = CTL_HA_STATUS_SUCCESS;
13022 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13026 rq->command = command;
13030 * Both pointers should be aligned. But it is possible
13031 * that the allocation length is not. They should both
13032 * also have enough slack left over at the end, though,
13033 * to round up to the next 8 byte boundary.
13035 cur_len = MIN(local_sglist[i].len - local_used,
13036 remote_sglist[j].len - remote_used);
13037 rq->size = cur_len;
13039 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13040 tmp_ptr += local_used;
13043 /* Use physical addresses when talking to ISC hardware */
13044 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13045 /* XXX KDM use busdma */
13046 rq->local = vtophys(tmp_ptr);
13048 rq->local = tmp_ptr;
13050 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
13051 ("HA does not support BUS_ADDR"));
13052 rq->local = tmp_ptr;
13055 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13056 tmp_ptr += remote_used;
13057 rq->remote = tmp_ptr;
13059 rq->callback = NULL;
13061 local_used += cur_len;
13062 if (local_used >= local_sglist[i].len) {
13067 remote_used += cur_len;
13068 if (remote_used >= remote_sglist[j].len) {
13072 total_used += cur_len;
13074 if (total_used >= io->scsiio.kern_data_len)
13075 rq->callback = callback;
13078 printf("%s: %s: local %p remote %p size %d\n", __func__,
13079 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13080 rq->local, rq->remote, rq->size);
13083 isc_ret = ctl_dt_single(rq);
13084 if (isc_ret > CTL_HA_STATUS_SUCCESS)
13087 if (isc_ret != CTL_HA_STATUS_WAIT) {
13096 ctl_datamove_remote_read(union ctl_io *io)
13102 * This will send an error to the other controller in the case of a
13105 retval = ctl_datamove_remote_sgl_setup(io);
13109 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13110 ctl_datamove_remote_read_cb);
13113 * Make sure we free memory if there was an error.. The
13114 * ctl_datamove_remote_xfer() function will send the
13115 * datamove done message, or call the callback with an
13116 * error if there is a problem.
13118 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13119 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13120 free(io->io_hdr.remote_sglist, M_CTL);
13121 io->io_hdr.remote_sglist = NULL;
13122 io->io_hdr.local_sglist = NULL;
13127 * Process a datamove request from the other controller. This is used for
13128 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13129 * first. Once that is complete, the data gets DMAed into the remote
13130 * controller's memory. For reads, we DMA from the remote controller's
13131 * memory into our memory first, and then move it out to the FETD.
13134 ctl_datamove_remote(union ctl_io *io)
13137 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
13139 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13140 ctl_failover_io(io, /*have_lock*/ 0);
13145 * Note that we look for an aborted I/O here, but don't do some of
13146 * the other checks that ctl_datamove() normally does.
13147 * We don't need to run the datamove delay code, since that should
13148 * have been done if need be on the other controller.
13150 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13151 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13152 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13153 io->io_hdr.nexus.targ_port,
13154 io->io_hdr.nexus.targ_lun);
13155 io->io_hdr.port_status = 31338;
13156 ctl_send_datamove_done(io, /*have_lock*/ 0);
13160 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13161 ctl_datamove_remote_write(io);
13162 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13163 ctl_datamove_remote_read(io);
13165 io->io_hdr.port_status = 31339;
13166 ctl_send_datamove_done(io, /*have_lock*/ 0);
13171 ctl_process_done(union ctl_io *io)
13173 struct ctl_lun *lun;
13174 struct ctl_softc *softc = control_softc;
13175 void (*fe_done)(union ctl_io *io);
13176 union ctl_ha_msg msg;
13177 uint32_t targ_port = io->io_hdr.nexus.targ_port;
13179 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13180 fe_done = softc->ctl_ports[targ_port]->fe_done;
13183 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13188 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13189 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13191 sbuf_cat(&sb, path_str);
13192 switch (io->io_hdr.io_type) {
13194 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13195 sbuf_printf(&sb, "\n");
13196 sbuf_cat(&sb, path_str);
13197 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13198 io->scsiio.tag_num, io->scsiio.tag_type);
13201 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13202 "Tag Type: %d\n", io->taskio.task_action,
13203 io->taskio.tag_num, io->taskio.tag_type);
13206 panic("%s: Invalid CTL I/O type %d\n",
13207 __func__, io->io_hdr.io_type);
13209 sbuf_cat(&sb, path_str);
13210 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13211 (intmax_t)time_uptime - io->io_hdr.start_time);
13213 printf("%s", sbuf_data(&sb));
13215 #endif /* CTL_TIME_IO */
13217 switch (io->io_hdr.io_type) {
13221 if (ctl_debug & CTL_DEBUG_INFO)
13222 ctl_io_error_print(io, NULL);
13226 panic("%s: Invalid CTL I/O type %d\n",
13227 __func__, io->io_hdr.io_type);
13230 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13232 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13233 io->io_hdr.nexus.targ_mapped_lun));
13237 mtx_lock(&lun->lun_lock);
13240 * Check to see if we have any errors to inject here. We only
13241 * inject errors for commands that don't already have errors set.
13243 if (!STAILQ_EMPTY(&lun->error_list) &&
13244 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13245 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13246 ctl_inject_error(lun, io);
13249 * XXX KDM how do we treat commands that aren't completed
13252 * XXX KDM should we also track I/O latency?
13254 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13255 io->io_hdr.io_type == CTL_IO_SCSI) {
13257 struct bintime cur_bt;
13261 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13263 type = CTL_STATS_READ;
13264 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13266 type = CTL_STATS_WRITE;
13268 type = CTL_STATS_NO_IO;
13270 lun->stats.ports[targ_port].bytes[type] +=
13271 io->scsiio.kern_total_len;
13272 lun->stats.ports[targ_port].operations[type]++;
13274 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13275 &io->io_hdr.dma_bt);
13276 getbinuptime(&cur_bt);
13277 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13278 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13280 lun->stats.ports[targ_port].num_dmas[type] +=
13281 io->io_hdr.num_dmas;
13285 * Remove this from the OOA queue.
13287 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13289 if (TAILQ_EMPTY(&lun->ooa_queue))
13290 lun->last_busy = getsbinuptime();
13294 * Run through the blocked queue on this LUN and see if anything
13295 * has become unblocked, now that this transaction is done.
13297 ctl_check_blocked(lun);
13300 * If the LUN has been invalidated, free it if there is nothing
13301 * left on its OOA queue.
13303 if ((lun->flags & CTL_LUN_INVALID)
13304 && TAILQ_EMPTY(&lun->ooa_queue)) {
13305 mtx_unlock(&lun->lun_lock);
13306 mtx_lock(&softc->ctl_lock);
13308 mtx_unlock(&softc->ctl_lock);
13310 mtx_unlock(&lun->lun_lock);
13315 * If this command has been aborted, make sure we set the status
13316 * properly. The FETD is responsible for freeing the I/O and doing
13317 * whatever it needs to do to clean up its state.
13319 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13320 ctl_set_task_aborted(&io->scsiio);
13323 * If enabled, print command error status.
13325 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13326 (ctl_debug & CTL_DEBUG_INFO) != 0)
13327 ctl_io_error_print(io, NULL);
13330 * Tell the FETD or the other shelf controller we're done with this
13331 * command. Note that only SCSI commands get to this point. Task
13332 * management commands are completed above.
13334 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13335 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13336 memset(&msg, 0, sizeof(msg));
13337 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13338 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13339 msg.hdr.nexus = io->io_hdr.nexus;
13340 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13341 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13350 * Front end should call this if it doesn't do autosense. When the request
13351 * sense comes back in from the initiator, we'll dequeue this and send it.
13354 ctl_queue_sense(union ctl_io *io)
13356 struct ctl_lun *lun;
13357 struct ctl_port *port;
13358 struct ctl_softc *softc;
13359 uint32_t initidx, targ_lun;
13361 softc = control_softc;
13363 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13366 * LUN lookup will likely move to the ctl_work_thread() once we
13367 * have our new queueing infrastructure (that doesn't put things on
13368 * a per-LUN queue initially). That is so that we can handle
13369 * things like an INQUIRY to a LUN that we don't have enabled. We
13370 * can't deal with that right now.
13372 mtx_lock(&softc->ctl_lock);
13375 * If we don't have a LUN for this, just toss the sense
13378 port = ctl_io_port(&ctsio->io_hdr);
13379 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13380 if ((targ_lun < CTL_MAX_LUNS)
13381 && (softc->ctl_luns[targ_lun] != NULL))
13382 lun = softc->ctl_luns[targ_lun];
13386 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13388 mtx_lock(&lun->lun_lock);
13390 * Already have CA set for this LUN...toss the sense information.
13392 if (ctl_is_set(lun->have_ca, initidx)) {
13393 mtx_unlock(&lun->lun_lock);
13397 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13398 MIN(sizeof(lun->pending_sense[initidx]),
13399 sizeof(io->scsiio.sense_data)));
13400 ctl_set_mask(lun->have_ca, initidx);
13401 mtx_unlock(&lun->lun_lock);
13404 mtx_unlock(&softc->ctl_lock);
13408 return (CTL_RETVAL_COMPLETE);
13413 * Primary command inlet from frontend ports. All SCSI and task I/O
13414 * requests must go through this function.
13417 ctl_queue(union ctl_io *io)
13419 struct ctl_port *port;
13421 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13424 io->io_hdr.start_time = time_uptime;
13425 getbinuptime(&io->io_hdr.start_bt);
13426 #endif /* CTL_TIME_IO */
13428 /* Map FE-specific LUN ID into global one. */
13429 port = ctl_io_port(&io->io_hdr);
13430 io->io_hdr.nexus.targ_mapped_lun =
13431 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13433 switch (io->io_hdr.io_type) {
13436 if (ctl_debug & CTL_DEBUG_CDB)
13438 ctl_enqueue_incoming(io);
13441 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13445 return (CTL_RETVAL_COMPLETE);
13448 #ifdef CTL_IO_DELAY
13450 ctl_done_timer_wakeup(void *arg)
13454 io = (union ctl_io *)arg;
13457 #endif /* CTL_IO_DELAY */
13460 ctl_serseq_done(union ctl_io *io)
13462 struct ctl_lun *lun;
13464 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13465 if (lun->be_lun == NULL ||
13466 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13468 mtx_lock(&lun->lun_lock);
13469 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13470 ctl_check_blocked(lun);
13471 mtx_unlock(&lun->lun_lock);
13475 ctl_done(union ctl_io *io)
13479 * Enable this to catch duplicate completion issues.
13482 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13483 printf("%s: type %d msg %d cdb %x iptl: "
13484 "%u:%u:%u tag 0x%04x "
13485 "flag %#x status %x\n",
13487 io->io_hdr.io_type,
13488 io->io_hdr.msg_type,
13490 io->io_hdr.nexus.initid,
13491 io->io_hdr.nexus.targ_port,
13492 io->io_hdr.nexus.targ_lun,
13493 (io->io_hdr.io_type ==
13495 io->taskio.tag_num :
13496 io->scsiio.tag_num,
13498 io->io_hdr.status);
13500 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13504 * This is an internal copy of an I/O, and should not go through
13505 * the normal done processing logic.
13507 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13510 #ifdef CTL_IO_DELAY
13511 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13512 struct ctl_lun *lun;
13514 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13516 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13518 struct ctl_lun *lun;
13520 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13523 && (lun->delay_info.done_delay > 0)) {
13525 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13526 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13527 callout_reset(&io->io_hdr.delay_callout,
13528 lun->delay_info.done_delay * hz,
13529 ctl_done_timer_wakeup, io);
13530 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13531 lun->delay_info.done_delay = 0;
13535 #endif /* CTL_IO_DELAY */
13537 ctl_enqueue_done(io);
13541 ctl_work_thread(void *arg)
13543 struct ctl_thread *thr = (struct ctl_thread *)arg;
13544 struct ctl_softc *softc = thr->ctl_softc;
13548 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13552 * We handle the queues in this order:
13554 * - done queue (to free up resources, unblock other commands)
13558 * If those queues are empty, we break out of the loop and
13561 mtx_lock(&thr->queue_lock);
13562 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13564 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13565 mtx_unlock(&thr->queue_lock);
13566 ctl_handle_isc(io);
13569 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13571 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13572 /* clear any blocked commands, call fe_done */
13573 mtx_unlock(&thr->queue_lock);
13574 ctl_process_done(io);
13577 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13579 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13580 mtx_unlock(&thr->queue_lock);
13581 if (io->io_hdr.io_type == CTL_IO_TASK)
13584 ctl_scsiio_precheck(softc, &io->scsiio);
13587 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13589 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13590 mtx_unlock(&thr->queue_lock);
13591 retval = ctl_scsiio(&io->scsiio);
13592 if (retval != CTL_RETVAL_COMPLETE)
13593 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13597 /* Sleep until we have something to do. */
13598 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13603 ctl_lun_thread(void *arg)
13605 struct ctl_softc *softc = (struct ctl_softc *)arg;
13606 struct ctl_be_lun *be_lun;
13608 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13611 mtx_lock(&softc->ctl_lock);
13612 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13613 if (be_lun != NULL) {
13614 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13615 mtx_unlock(&softc->ctl_lock);
13616 ctl_create_lun(be_lun);
13620 /* Sleep until we have something to do. */
13621 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13622 PDROP | PRIBIO, "-", 0);
13627 ctl_thresh_thread(void *arg)
13629 struct ctl_softc *softc = (struct ctl_softc *)arg;
13630 struct ctl_lun *lun;
13631 struct scsi_da_rw_recovery_page *rwpage;
13632 struct ctl_logical_block_provisioning_page *page;
13634 union ctl_ha_msg msg;
13635 uint64_t thres, val;
13638 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13641 mtx_lock(&softc->ctl_lock);
13642 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13643 if ((lun->flags & CTL_LUN_DISABLED) ||
13644 (lun->flags & CTL_LUN_NO_MEDIA) ||
13645 lun->backend->lun_attr == NULL)
13647 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13648 softc->ha_mode == CTL_HA_MODE_XFER)
13650 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13651 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13654 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13655 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13656 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13658 thres = scsi_4btoul(page->descr[i].count);
13659 thres <<= CTL_LBP_EXPONENT;
13660 switch (page->descr[i].resource) {
13662 attr = "blocksavail";
13665 attr = "blocksused";
13668 attr = "poolblocksavail";
13671 attr = "poolblocksused";
13676 mtx_unlock(&softc->ctl_lock); // XXX
13677 val = lun->backend->lun_attr(
13678 lun->be_lun->be_lun, attr);
13679 mtx_lock(&softc->ctl_lock);
13680 if (val == UINT64_MAX)
13682 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13683 == SLBPPD_ARMING_INC)
13684 e = (val >= thres);
13686 e = (val <= thres);
13690 mtx_lock(&lun->lun_lock);
13692 scsi_u64to8b((uint8_t *)&page->descr[i] -
13693 (uint8_t *)page, lun->ua_tpt_info);
13694 if (lun->lasttpt == 0 ||
13695 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13696 lun->lasttpt = time_uptime;
13697 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13703 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13706 mtx_unlock(&lun->lun_lock);
13708 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13709 /* Send msg to other side. */
13710 bzero(&msg.ua, sizeof(msg.ua));
13711 msg.hdr.msg_type = CTL_MSG_UA;
13712 msg.hdr.nexus.initid = -1;
13713 msg.hdr.nexus.targ_port = -1;
13714 msg.hdr.nexus.targ_lun = lun->lun;
13715 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13717 msg.ua.ua_set = (set > 0);
13718 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13719 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13720 mtx_unlock(&softc->ctl_lock); // XXX
13721 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13722 sizeof(msg.ua), M_WAITOK);
13723 mtx_lock(&softc->ctl_lock);
13726 mtx_unlock(&softc->ctl_lock);
13727 pause("-", CTL_LBP_PERIOD * hz);
13732 ctl_enqueue_incoming(union ctl_io *io)
13734 struct ctl_softc *softc = control_softc;
13735 struct ctl_thread *thr;
13738 idx = (io->io_hdr.nexus.targ_port * 127 +
13739 io->io_hdr.nexus.initid) % worker_threads;
13740 thr = &softc->threads[idx];
13741 mtx_lock(&thr->queue_lock);
13742 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13743 mtx_unlock(&thr->queue_lock);
13748 ctl_enqueue_rtr(union ctl_io *io)
13750 struct ctl_softc *softc = control_softc;
13751 struct ctl_thread *thr;
13753 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13754 mtx_lock(&thr->queue_lock);
13755 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13756 mtx_unlock(&thr->queue_lock);
13761 ctl_enqueue_done(union ctl_io *io)
13763 struct ctl_softc *softc = control_softc;
13764 struct ctl_thread *thr;
13766 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13767 mtx_lock(&thr->queue_lock);
13768 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13769 mtx_unlock(&thr->queue_lock);
13774 ctl_enqueue_isc(union ctl_io *io)
13776 struct ctl_softc *softc = control_softc;
13777 struct ctl_thread *thr;
13779 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13780 mtx_lock(&thr->queue_lock);
13781 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13782 mtx_unlock(&thr->queue_lock);