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->PRGeneration;
787 msg->lun.pr_res_idx = lun->pr_res_idx;
788 msg->lun.pr_res_type = lun->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)
932 bzero(&msg.mode, sizeof(msg.mode));
933 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
934 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
935 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
936 msg.hdr.nexus.targ_lun = lun->lun;
937 msg.hdr.nexus.targ_mapped_lun = lun->lun;
938 msg.mode.page_code = page;
939 msg.mode.subpage = subpage;
940 msg.mode.page_len = lun->mode_pages.index[i].page_len;
941 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
943 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
948 ctl_isc_ha_link_up(struct ctl_softc *softc)
950 struct ctl_port *port;
952 union ctl_ha_msg msg;
955 /* Announce this node parameters to peer for validation. */
956 msg.login.msg_type = CTL_MSG_LOGIN;
957 msg.login.version = CTL_HA_VERSION;
958 msg.login.ha_mode = softc->ha_mode;
959 msg.login.ha_id = softc->ha_id;
960 msg.login.max_luns = CTL_MAX_LUNS;
961 msg.login.max_ports = CTL_MAX_PORTS;
962 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
963 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
966 STAILQ_FOREACH(port, &softc->port_list, links) {
967 ctl_isc_announce_port(port);
968 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
969 if (port->wwpn_iid[i].in_use)
970 ctl_isc_announce_iid(port, i);
973 STAILQ_FOREACH(lun, &softc->lun_list, links)
974 ctl_isc_announce_lun(lun);
978 ctl_isc_ha_link_down(struct ctl_softc *softc)
980 struct ctl_port *port;
985 mtx_lock(&softc->ctl_lock);
986 STAILQ_FOREACH(lun, &softc->lun_list, links) {
987 mtx_lock(&lun->lun_lock);
988 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
989 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
990 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
992 mtx_unlock(&lun->lun_lock);
994 mtx_unlock(&softc->ctl_lock);
995 io = ctl_alloc_io(softc->othersc_pool);
996 mtx_lock(&softc->ctl_lock);
998 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
999 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1000 ctl_enqueue_isc(io);
1003 STAILQ_FOREACH(port, &softc->port_list, links) {
1004 if (port->targ_port >= softc->port_min &&
1005 port->targ_port < softc->port_max)
1007 port->status &= ~CTL_PORT_STATUS_ONLINE;
1008 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1009 port->wwpn_iid[i].in_use = 0;
1010 free(port->wwpn_iid[i].name, M_CTL);
1011 port->wwpn_iid[i].name = NULL;
1014 mtx_unlock(&softc->ctl_lock);
1018 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1020 struct ctl_lun *lun;
1021 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1023 mtx_lock(&softc->ctl_lock);
1024 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
1025 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
1026 mtx_lock(&lun->lun_lock);
1027 mtx_unlock(&softc->ctl_lock);
1028 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES &&
1030 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1031 if (msg->ua.ua_all) {
1033 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1035 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1038 ctl_est_ua(lun, iid, msg->ua.ua_type);
1040 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1042 mtx_unlock(&lun->lun_lock);
1044 mtx_unlock(&softc->ctl_lock);
1048 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1050 struct ctl_lun *lun;
1051 struct ctl_ha_msg_lun_pr_key pr_key;
1053 ctl_lun_flags oflags;
1056 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1057 mtx_lock(&softc->ctl_lock);
1058 if ((targ_lun >= CTL_MAX_LUNS) ||
1059 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1060 mtx_unlock(&softc->ctl_lock);
1063 mtx_lock(&lun->lun_lock);
1064 mtx_unlock(&softc->ctl_lock);
1065 if (lun->flags & CTL_LUN_DISABLED) {
1066 mtx_unlock(&lun->lun_lock);
1069 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1070 if (msg->lun.lun_devid_len != i || (i > 0 &&
1071 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1072 mtx_unlock(&lun->lun_lock);
1073 printf("%s: Received conflicting HA LUN %d\n",
1074 __func__, msg->hdr.nexus.targ_lun);
1077 /* Record whether peer is primary. */
1078 oflags = lun->flags;
1079 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1080 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1081 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1083 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1084 if (oflags != lun->flags)
1085 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1087 /* If peer is primary and we are not -- use data */
1088 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1089 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1090 lun->PRGeneration = msg->lun.pr_generation;
1091 lun->pr_res_idx = msg->lun.pr_res_idx;
1092 lun->res_type = msg->lun.pr_res_type;
1093 lun->pr_key_count = msg->lun.pr_key_count;
1094 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1095 ctl_clr_prkey(lun, k);
1096 for (k = 0; k < msg->lun.pr_key_count; k++) {
1097 memcpy(&pr_key, &msg->lun.data[i],
1099 ctl_alloc_prkey(lun, pr_key.pr_iid);
1100 ctl_set_prkey(lun, pr_key.pr_iid,
1102 i += sizeof(pr_key);
1106 mtx_unlock(&lun->lun_lock);
1107 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1108 __func__, msg->hdr.nexus.targ_lun,
1109 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1110 "primary" : "secondary"));
1112 /* If we are primary but peer doesn't know -- notify */
1113 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1114 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1115 ctl_isc_announce_lun(lun);
1120 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1122 struct ctl_port *port;
1123 struct ctl_lun *lun;
1126 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1128 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1129 msg->hdr.nexus.targ_port));
1131 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1132 port->frontend = &ha_frontend;
1133 port->targ_port = msg->hdr.nexus.targ_port;
1134 port->fe_datamove = ctl_ha_datamove;
1135 port->fe_done = ctl_ha_done;
1136 } else if (port->frontend == &ha_frontend) {
1137 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1138 msg->hdr.nexus.targ_port));
1141 printf("%s: Received conflicting HA port %d\n",
1142 __func__, msg->hdr.nexus.targ_port);
1145 port->port_type = msg->port.port_type;
1146 port->physical_port = msg->port.physical_port;
1147 port->virtual_port = msg->port.virtual_port;
1148 port->status = msg->port.status;
1150 free(port->port_name, M_CTL);
1151 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1153 i += msg->port.name_len;
1154 if (msg->port.lun_map_len != 0) {
1155 if (port->lun_map == NULL)
1156 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
1158 memcpy(port->lun_map, &msg->port.data[i],
1159 sizeof(uint32_t) * CTL_MAX_LUNS);
1160 i += msg->port.lun_map_len;
1162 free(port->lun_map, M_CTL);
1163 port->lun_map = NULL;
1165 if (msg->port.port_devid_len != 0) {
1166 if (port->port_devid == NULL ||
1167 port->port_devid->len != msg->port.port_devid_len) {
1168 free(port->port_devid, M_CTL);
1169 port->port_devid = malloc(sizeof(struct ctl_devid) +
1170 msg->port.port_devid_len, M_CTL, M_WAITOK);
1172 memcpy(port->port_devid->data, &msg->port.data[i],
1173 msg->port.port_devid_len);
1174 port->port_devid->len = msg->port.port_devid_len;
1175 i += msg->port.port_devid_len;
1177 free(port->port_devid, M_CTL);
1178 port->port_devid = NULL;
1180 if (msg->port.target_devid_len != 0) {
1181 if (port->target_devid == NULL ||
1182 port->target_devid->len != msg->port.target_devid_len) {
1183 free(port->target_devid, M_CTL);
1184 port->target_devid = malloc(sizeof(struct ctl_devid) +
1185 msg->port.target_devid_len, M_CTL, M_WAITOK);
1187 memcpy(port->target_devid->data, &msg->port.data[i],
1188 msg->port.target_devid_len);
1189 port->target_devid->len = msg->port.target_devid_len;
1190 i += msg->port.target_devid_len;
1192 free(port->target_devid, M_CTL);
1193 port->target_devid = NULL;
1195 if (msg->port.init_devid_len != 0) {
1196 if (port->init_devid == NULL ||
1197 port->init_devid->len != msg->port.init_devid_len) {
1198 free(port->init_devid, M_CTL);
1199 port->init_devid = malloc(sizeof(struct ctl_devid) +
1200 msg->port.init_devid_len, M_CTL, M_WAITOK);
1202 memcpy(port->init_devid->data, &msg->port.data[i],
1203 msg->port.init_devid_len);
1204 port->init_devid->len = msg->port.init_devid_len;
1205 i += msg->port.init_devid_len;
1207 free(port->init_devid, M_CTL);
1208 port->init_devid = NULL;
1211 if (ctl_port_register(port) != 0) {
1212 printf("%s: ctl_port_register() failed with error\n",
1216 mtx_lock(&softc->ctl_lock);
1217 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1218 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
1220 mtx_lock(&lun->lun_lock);
1221 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1222 mtx_unlock(&lun->lun_lock);
1224 mtx_unlock(&softc->ctl_lock);
1228 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1230 struct ctl_port *port;
1233 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1235 printf("%s: Received IID for unknown port %d\n",
1236 __func__, msg->hdr.nexus.targ_port);
1239 iid = msg->hdr.nexus.initid;
1240 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1241 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1242 free(port->wwpn_iid[iid].name, M_CTL);
1243 if (msg->iid.name_len) {
1244 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1245 msg->iid.name_len, M_CTL);
1247 port->wwpn_iid[iid].name = NULL;
1251 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1254 if (msg->login.version != CTL_HA_VERSION) {
1255 printf("CTL HA peers have different versions %d != %d\n",
1256 msg->login.version, CTL_HA_VERSION);
1257 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1260 if (msg->login.ha_mode != softc->ha_mode) {
1261 printf("CTL HA peers have different ha_mode %d != %d\n",
1262 msg->login.ha_mode, softc->ha_mode);
1263 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1266 if (msg->login.ha_id == softc->ha_id) {
1267 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1268 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1271 if (msg->login.max_luns != CTL_MAX_LUNS ||
1272 msg->login.max_ports != CTL_MAX_PORTS ||
1273 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1274 printf("CTL HA peers have different limits\n");
1275 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1281 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1283 struct ctl_lun *lun;
1285 uint32_t initidx, targ_lun;
1287 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1288 mtx_lock(&softc->ctl_lock);
1289 if ((targ_lun >= CTL_MAX_LUNS) ||
1290 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1291 mtx_unlock(&softc->ctl_lock);
1294 mtx_lock(&lun->lun_lock);
1295 mtx_unlock(&softc->ctl_lock);
1296 if (lun->flags & CTL_LUN_DISABLED) {
1297 mtx_unlock(&lun->lun_lock);
1300 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1301 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1302 msg->mode.page_code &&
1303 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1306 if (i == CTL_NUM_MODE_PAGES) {
1307 mtx_unlock(&lun->lun_lock);
1310 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1311 lun->mode_pages.index[i].page_len);
1312 initidx = ctl_get_initindex(&msg->hdr.nexus);
1314 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1315 mtx_unlock(&lun->lun_lock);
1319 * ISC (Inter Shelf Communication) event handler. Events from the HA
1320 * subsystem come in here.
1323 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1325 struct ctl_softc *softc = control_softc;
1327 struct ctl_prio *presio;
1328 ctl_ha_status isc_status;
1330 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1331 if (event == CTL_HA_EVT_MSG_RECV) {
1332 union ctl_ha_msg *msg, msgbuf;
1334 if (param > sizeof(msgbuf))
1335 msg = malloc(param, M_CTL, M_WAITOK);
1338 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1340 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1341 printf("%s: Error receiving message: %d\n",
1342 __func__, isc_status);
1348 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1349 switch (msg->hdr.msg_type) {
1350 case CTL_MSG_SERIALIZE:
1351 io = ctl_alloc_io(softc->othersc_pool);
1353 // populate ctsio from msg
1354 io->io_hdr.io_type = CTL_IO_SCSI;
1355 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1356 io->io_hdr.original_sc = msg->hdr.original_sc;
1357 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1360 * If we're in serialization-only mode, we don't
1361 * want to go through full done processing. Thus
1364 * XXX KDM add another flag that is more specific.
1366 if (softc->ha_mode != CTL_HA_MODE_XFER)
1367 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1368 io->io_hdr.nexus = msg->hdr.nexus;
1370 printf("port %u, iid %u, lun %u\n",
1371 io->io_hdr.nexus.targ_port,
1372 io->io_hdr.nexus.initid,
1373 io->io_hdr.nexus.targ_lun);
1375 io->scsiio.tag_num = msg->scsi.tag_num;
1376 io->scsiio.tag_type = msg->scsi.tag_type;
1378 io->io_hdr.start_time = time_uptime;
1379 getbinuptime(&io->io_hdr.start_bt);
1380 #endif /* CTL_TIME_IO */
1381 io->scsiio.cdb_len = msg->scsi.cdb_len;
1382 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1384 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1385 const struct ctl_cmd_entry *entry;
1387 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1388 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1390 entry->flags & CTL_FLAG_DATA_MASK;
1392 ctl_enqueue_isc(io);
1395 /* Performed on the Originating SC, XFER mode only */
1396 case CTL_MSG_DATAMOVE: {
1397 struct ctl_sg_entry *sgl;
1400 io = msg->hdr.original_sc;
1402 printf("%s: original_sc == NULL!\n", __func__);
1403 /* XXX KDM do something here */
1406 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1407 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1409 * Keep track of this, we need to send it back over
1410 * when the datamove is complete.
1412 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1413 if (msg->hdr.status == CTL_SUCCESS)
1414 io->io_hdr.status = msg->hdr.status;
1416 if (msg->dt.sg_sequence == 0) {
1418 getbinuptime(&io->io_hdr.dma_start_bt);
1420 i = msg->dt.kern_sg_entries +
1421 msg->dt.kern_data_len /
1422 CTL_HA_DATAMOVE_SEGMENT + 1;
1423 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1425 io->io_hdr.remote_sglist = sgl;
1426 io->io_hdr.local_sglist =
1427 &sgl[msg->dt.kern_sg_entries];
1429 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1431 io->scsiio.kern_sg_entries =
1432 msg->dt.kern_sg_entries;
1433 io->scsiio.rem_sg_entries =
1434 msg->dt.kern_sg_entries;
1435 io->scsiio.kern_data_len =
1436 msg->dt.kern_data_len;
1437 io->scsiio.kern_total_len =
1438 msg->dt.kern_total_len;
1439 io->scsiio.kern_data_resid =
1440 msg->dt.kern_data_resid;
1441 io->scsiio.kern_rel_offset =
1442 msg->dt.kern_rel_offset;
1443 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1444 io->io_hdr.flags |= msg->dt.flags &
1447 sgl = (struct ctl_sg_entry *)
1448 io->scsiio.kern_data_ptr;
1450 for (i = msg->dt.sent_sg_entries, j = 0;
1451 i < (msg->dt.sent_sg_entries +
1452 msg->dt.cur_sg_entries); i++, j++) {
1453 sgl[i].addr = msg->dt.sg_list[j].addr;
1454 sgl[i].len = msg->dt.sg_list[j].len;
1457 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1458 __func__, sgl[i].addr, sgl[i].len, j, i);
1463 * If this is the last piece of the I/O, we've got
1464 * the full S/G list. Queue processing in the thread.
1465 * Otherwise wait for the next piece.
1467 if (msg->dt.sg_last != 0)
1468 ctl_enqueue_isc(io);
1471 /* Performed on the Serializing (primary) SC, XFER mode only */
1472 case CTL_MSG_DATAMOVE_DONE: {
1473 if (msg->hdr.serializing_sc == NULL) {
1474 printf("%s: serializing_sc == NULL!\n",
1476 /* XXX KDM now what? */
1480 * We grab the sense information here in case
1481 * there was a failure, so we can return status
1482 * back to the initiator.
1484 io = msg->hdr.serializing_sc;
1485 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1486 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1487 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1488 io->io_hdr.port_status = msg->scsi.fetd_status;
1489 io->scsiio.residual = msg->scsi.residual;
1490 if (msg->hdr.status != CTL_STATUS_NONE) {
1491 io->io_hdr.status = msg->hdr.status;
1492 io->scsiio.scsi_status = msg->scsi.scsi_status;
1493 io->scsiio.sense_len = msg->scsi.sense_len;
1494 io->scsiio.sense_residual =msg->scsi.sense_residual;
1495 memcpy(&io->scsiio.sense_data,
1496 &msg->scsi.sense_data,
1497 msg->scsi.sense_len);
1498 if (msg->hdr.status == CTL_SUCCESS)
1499 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1501 ctl_enqueue_isc(io);
1505 /* Preformed on Originating SC, SER_ONLY mode */
1507 io = msg->hdr.original_sc;
1509 printf("%s: original_sc == NULL!\n",
1513 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1514 io->io_hdr.msg_type = CTL_MSG_R2R;
1515 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1516 ctl_enqueue_isc(io);
1520 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1522 * Performed on the Originating (i.e. secondary) SC in XFER
1525 case CTL_MSG_FINISH_IO:
1526 if (softc->ha_mode == CTL_HA_MODE_XFER)
1527 ctl_isc_handler_finish_xfer(softc, msg);
1529 ctl_isc_handler_finish_ser_only(softc, msg);
1532 /* Preformed on Originating SC */
1533 case CTL_MSG_BAD_JUJU:
1534 io = msg->hdr.original_sc;
1536 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1540 ctl_copy_sense_data(msg, io);
1542 * IO should have already been cleaned up on other
1543 * SC so clear this flag so we won't send a message
1544 * back to finish the IO there.
1546 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1547 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1549 /* io = msg->hdr.serializing_sc; */
1550 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1551 ctl_enqueue_isc(io);
1554 /* Handle resets sent from the other side */
1555 case CTL_MSG_MANAGE_TASKS: {
1556 struct ctl_taskio *taskio;
1557 taskio = (struct ctl_taskio *)ctl_alloc_io(
1558 softc->othersc_pool);
1559 ctl_zero_io((union ctl_io *)taskio);
1560 taskio->io_hdr.io_type = CTL_IO_TASK;
1561 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1562 taskio->io_hdr.nexus = msg->hdr.nexus;
1563 taskio->task_action = msg->task.task_action;
1564 taskio->tag_num = msg->task.tag_num;
1565 taskio->tag_type = msg->task.tag_type;
1567 taskio->io_hdr.start_time = time_uptime;
1568 getbinuptime(&taskio->io_hdr.start_bt);
1569 #endif /* CTL_TIME_IO */
1570 ctl_run_task((union ctl_io *)taskio);
1573 /* Persistent Reserve action which needs attention */
1574 case CTL_MSG_PERS_ACTION:
1575 presio = (struct ctl_prio *)ctl_alloc_io(
1576 softc->othersc_pool);
1577 ctl_zero_io((union ctl_io *)presio);
1578 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1579 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1580 presio->io_hdr.nexus = msg->hdr.nexus;
1581 presio->pr_msg = msg->pr;
1582 ctl_enqueue_isc((union ctl_io *)presio);
1585 ctl_isc_ua(softc, msg, param);
1587 case CTL_MSG_PORT_SYNC:
1588 ctl_isc_port_sync(softc, msg, param);
1590 case CTL_MSG_LUN_SYNC:
1591 ctl_isc_lun_sync(softc, msg, param);
1593 case CTL_MSG_IID_SYNC:
1594 ctl_isc_iid_sync(softc, msg, param);
1597 ctl_isc_login(softc, msg, param);
1599 case CTL_MSG_MODE_SYNC:
1600 ctl_isc_mode_sync(softc, msg, param);
1603 printf("Received HA message of unknown type %d\n",
1605 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1610 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1611 printf("CTL: HA link status changed from %d to %d\n",
1612 softc->ha_link, param);
1613 if (param == softc->ha_link)
1615 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1616 softc->ha_link = param;
1617 ctl_isc_ha_link_down(softc);
1619 softc->ha_link = param;
1620 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1621 ctl_isc_ha_link_up(softc);
1625 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1631 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1634 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1635 src->scsi.sense_len);
1636 dest->scsiio.scsi_status = src->scsi.scsi_status;
1637 dest->scsiio.sense_len = src->scsi.sense_len;
1638 dest->io_hdr.status = src->hdr.status;
1642 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1645 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1646 src->scsiio.sense_len);
1647 dest->scsi.scsi_status = src->scsiio.scsi_status;
1648 dest->scsi.sense_len = src->scsiio.sense_len;
1649 dest->hdr.status = src->io_hdr.status;
1653 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1655 struct ctl_softc *softc = lun->ctl_softc;
1658 if (initidx < softc->init_min || initidx >= softc->init_max)
1660 mtx_assert(&lun->lun_lock, MA_OWNED);
1661 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1664 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1668 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1672 mtx_assert(&lun->lun_lock, MA_OWNED);
1673 if (lun->pending_ua[port] == NULL)
1675 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1676 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1678 lun->pending_ua[port][i] |= ua;
1683 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1685 struct ctl_softc *softc = lun->ctl_softc;
1688 mtx_assert(&lun->lun_lock, MA_OWNED);
1689 for (i = softc->port_min; i < softc->port_max; i++)
1690 ctl_est_ua_port(lun, i, except, ua);
1694 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1696 struct ctl_softc *softc = lun->ctl_softc;
1699 if (initidx < softc->init_min || initidx >= softc->init_max)
1701 mtx_assert(&lun->lun_lock, MA_OWNED);
1702 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1705 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1709 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1711 struct ctl_softc *softc = lun->ctl_softc;
1714 mtx_assert(&lun->lun_lock, MA_OWNED);
1715 for (i = softc->port_min; i < softc->port_max; i++) {
1716 if (lun->pending_ua[i] == NULL)
1718 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1719 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1721 lun->pending_ua[i][j] &= ~ua;
1727 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1728 ctl_ua_type ua_type)
1730 struct ctl_lun *lun;
1732 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1733 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1734 mtx_lock(&lun->lun_lock);
1735 ctl_clr_ua(lun, initidx, ua_type);
1736 mtx_unlock(&lun->lun_lock);
1741 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1743 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1744 struct ctl_lun *lun;
1745 struct ctl_lun_req ireq;
1748 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1749 error = sysctl_handle_int(oidp, &value, 0, req);
1750 if ((error != 0) || (req->newptr == NULL))
1753 mtx_lock(&softc->ctl_lock);
1755 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1757 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1758 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1759 mtx_unlock(&softc->ctl_lock);
1760 bzero(&ireq, sizeof(ireq));
1761 ireq.reqtype = CTL_LUNREQ_MODIFY;
1762 ireq.reqdata.modify.lun_id = lun->lun;
1763 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1765 if (ireq.status != CTL_LUN_OK) {
1766 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1767 __func__, ireq.status, ireq.error_str);
1769 mtx_lock(&softc->ctl_lock);
1771 mtx_unlock(&softc->ctl_lock);
1778 struct ctl_softc *softc;
1782 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1785 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1787 softc->dev->si_drv1 = softc;
1789 sysctl_ctx_init(&softc->sysctl_ctx);
1790 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1791 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1792 CTLFLAG_RD, 0, "CAM Target Layer");
1794 if (softc->sysctl_tree == NULL) {
1795 printf("%s: unable to allocate sysctl tree\n", __func__);
1796 destroy_dev(softc->dev);
1797 free(control_softc, M_DEVBUF);
1798 control_softc = NULL;
1802 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1803 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1804 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1807 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1808 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1809 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1812 * In Copan's HA scheme, the "master" and "slave" roles are
1813 * figured out through the slot the controller is in. Although it
1814 * is an active/active system, someone has to be in charge.
1816 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1817 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1818 "HA head ID (0 - no HA)");
1819 if (softc->ha_id == 0 || softc->ha_id > NUM_TARGET_PORT_GROUPS) {
1820 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1821 softc->is_single = 1;
1822 softc->port_cnt = CTL_MAX_PORTS;
1823 softc->port_min = 0;
1825 softc->port_cnt = CTL_MAX_PORTS / NUM_TARGET_PORT_GROUPS;
1826 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1828 softc->port_max = softc->port_min + softc->port_cnt;
1829 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1830 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1832 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1833 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1834 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1836 STAILQ_INIT(&softc->lun_list);
1837 STAILQ_INIT(&softc->pending_lun_queue);
1838 STAILQ_INIT(&softc->fe_list);
1839 STAILQ_INIT(&softc->port_list);
1840 STAILQ_INIT(&softc->be_list);
1841 ctl_tpc_init(softc);
1843 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1846 printf("ctl: can't allocate %d entry other SC pool, "
1847 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1850 softc->othersc_pool = other_pool;
1852 if (worker_threads <= 0)
1853 worker_threads = max(1, mp_ncpus / 4);
1854 if (worker_threads > CTL_MAX_THREADS)
1855 worker_threads = CTL_MAX_THREADS;
1857 for (i = 0; i < worker_threads; i++) {
1858 struct ctl_thread *thr = &softc->threads[i];
1860 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1861 thr->ctl_softc = softc;
1862 STAILQ_INIT(&thr->incoming_queue);
1863 STAILQ_INIT(&thr->rtr_queue);
1864 STAILQ_INIT(&thr->done_queue);
1865 STAILQ_INIT(&thr->isc_queue);
1867 error = kproc_kthread_add(ctl_work_thread, thr,
1868 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1870 printf("error creating CTL work thread!\n");
1871 ctl_pool_free(other_pool);
1875 error = kproc_kthread_add(ctl_lun_thread, softc,
1876 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1878 printf("error creating CTL lun thread!\n");
1879 ctl_pool_free(other_pool);
1882 error = kproc_kthread_add(ctl_thresh_thread, softc,
1883 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1885 printf("error creating CTL threshold thread!\n");
1886 ctl_pool_free(other_pool);
1890 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1891 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1892 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1894 if (softc->is_single == 0) {
1895 ctl_frontend_register(&ha_frontend);
1896 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1897 printf("ctl_init: ctl_ha_msg_init failed.\n");
1898 softc->is_single = 1;
1900 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1901 != CTL_HA_STATUS_SUCCESS) {
1902 printf("ctl_init: ctl_ha_msg_register failed.\n");
1903 softc->is_single = 1;
1912 struct ctl_softc *softc = control_softc;
1913 struct ctl_lun *lun, *next_lun;
1915 if (softc->is_single == 0) {
1916 ctl_ha_msg_shutdown(softc);
1917 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1918 != CTL_HA_STATUS_SUCCESS)
1919 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1920 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1921 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1922 ctl_frontend_deregister(&ha_frontend);
1925 mtx_lock(&softc->ctl_lock);
1930 for (lun = STAILQ_FIRST(&softc->lun_list); lun != NULL; lun = next_lun){
1931 next_lun = STAILQ_NEXT(lun, links);
1935 mtx_unlock(&softc->ctl_lock);
1938 ctl_shutdown_thread(softc->work_thread);
1939 mtx_destroy(&softc->queue_lock);
1942 ctl_tpc_shutdown(softc);
1943 uma_zdestroy(softc->io_zone);
1944 mtx_destroy(&softc->ctl_lock);
1946 destroy_dev(softc->dev);
1948 sysctl_ctx_free(&softc->sysctl_ctx);
1950 free(control_softc, M_DEVBUF);
1951 control_softc = NULL;
1955 ctl_module_event_handler(module_t mod, int what, void *arg)
1960 return (ctl_init());
1964 return (EOPNOTSUPP);
1969 * XXX KDM should we do some access checks here? Bump a reference count to
1970 * prevent a CTL module from being unloaded while someone has it open?
1973 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1979 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
1985 * Remove an initiator by port number and initiator ID.
1986 * Returns 0 for success, -1 for failure.
1989 ctl_remove_initiator(struct ctl_port *port, int iid)
1991 struct ctl_softc *softc = port->ctl_softc;
1993 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
1995 if (iid > CTL_MAX_INIT_PER_PORT) {
1996 printf("%s: initiator ID %u > maximun %u!\n",
1997 __func__, iid, CTL_MAX_INIT_PER_PORT);
2001 mtx_lock(&softc->ctl_lock);
2002 port->wwpn_iid[iid].in_use--;
2003 port->wwpn_iid[iid].last_use = time_uptime;
2004 mtx_unlock(&softc->ctl_lock);
2005 ctl_isc_announce_iid(port, iid);
2011 * Add an initiator to the initiator map.
2012 * Returns iid for success, < 0 for failure.
2015 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2017 struct ctl_softc *softc = port->ctl_softc;
2021 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2023 if (iid >= CTL_MAX_INIT_PER_PORT) {
2024 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2025 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2030 mtx_lock(&softc->ctl_lock);
2032 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2033 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2034 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2038 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2039 strcmp(name, port->wwpn_iid[i].name) == 0) {
2047 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2048 if (port->wwpn_iid[i].in_use == 0 &&
2049 port->wwpn_iid[i].wwpn == 0 &&
2050 port->wwpn_iid[i].name == NULL) {
2059 best_time = INT32_MAX;
2060 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2061 if (port->wwpn_iid[i].in_use == 0) {
2062 if (port->wwpn_iid[i].last_use < best_time) {
2064 best_time = port->wwpn_iid[i].last_use;
2072 mtx_unlock(&softc->ctl_lock);
2077 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2079 * This is not an error yet.
2081 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2083 printf("%s: port %d iid %u WWPN %#jx arrived"
2084 " again\n", __func__, port->targ_port,
2085 iid, (uintmax_t)wwpn);
2089 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2090 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2092 printf("%s: port %d iid %u name '%s' arrived"
2093 " again\n", __func__, port->targ_port,
2100 * This is an error, but what do we do about it? The
2101 * driver is telling us we have a new WWPN for this
2102 * initiator ID, so we pretty much need to use it.
2104 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2105 " but WWPN %#jx '%s' is still at that address\n",
2106 __func__, port->targ_port, iid, wwpn, name,
2107 (uintmax_t)port->wwpn_iid[iid].wwpn,
2108 port->wwpn_iid[iid].name);
2111 * XXX KDM clear have_ca and ua_pending on each LUN for
2116 free(port->wwpn_iid[iid].name, M_CTL);
2117 port->wwpn_iid[iid].name = name;
2118 port->wwpn_iid[iid].wwpn = wwpn;
2119 port->wwpn_iid[iid].in_use++;
2120 mtx_unlock(&softc->ctl_lock);
2121 ctl_isc_announce_iid(port, iid);
2127 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2131 switch (port->port_type) {
2134 struct scsi_transportid_fcp *id =
2135 (struct scsi_transportid_fcp *)buf;
2136 if (port->wwpn_iid[iid].wwpn == 0)
2138 memset(id, 0, sizeof(*id));
2139 id->format_protocol = SCSI_PROTO_FC;
2140 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2141 return (sizeof(*id));
2143 case CTL_PORT_ISCSI:
2145 struct scsi_transportid_iscsi_port *id =
2146 (struct scsi_transportid_iscsi_port *)buf;
2147 if (port->wwpn_iid[iid].name == NULL)
2150 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2152 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2153 len = roundup2(min(len, 252), 4);
2154 scsi_ulto2b(len, id->additional_length);
2155 return (sizeof(*id) + len);
2159 struct scsi_transportid_sas *id =
2160 (struct scsi_transportid_sas *)buf;
2161 if (port->wwpn_iid[iid].wwpn == 0)
2163 memset(id, 0, sizeof(*id));
2164 id->format_protocol = SCSI_PROTO_SAS;
2165 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2166 return (sizeof(*id));
2170 struct scsi_transportid_spi *id =
2171 (struct scsi_transportid_spi *)buf;
2172 memset(id, 0, sizeof(*id));
2173 id->format_protocol = SCSI_PROTO_SPI;
2174 scsi_ulto2b(iid, id->scsi_addr);
2175 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2176 return (sizeof(*id));
2182 * Serialize a command that went down the "wrong" side, and so was sent to
2183 * this controller for execution. The logic is a little different than the
2184 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2185 * sent back to the other side, but in the success case, we execute the
2186 * command on this side (XFER mode) or tell the other side to execute it
2190 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2192 struct ctl_softc *softc = control_softc;
2193 union ctl_ha_msg msg_info;
2194 struct ctl_port *port;
2195 struct ctl_lun *lun;
2196 const struct ctl_cmd_entry *entry;
2200 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2201 mtx_lock(&softc->ctl_lock);
2203 /* Make sure that we know about this port. */
2204 port = ctl_io_port(&ctsio->io_hdr);
2205 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2206 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2211 /* Make sure that we know about this LUN. */
2212 if ((targ_lun < CTL_MAX_LUNS) &&
2213 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
2214 mtx_lock(&lun->lun_lock);
2215 mtx_unlock(&softc->ctl_lock);
2217 * If the LUN is invalid, pretend that it doesn't exist.
2218 * It will go away as soon as all pending I/O has been
2221 if (lun->flags & CTL_LUN_DISABLED) {
2222 mtx_unlock(&lun->lun_lock);
2226 mtx_unlock(&softc->ctl_lock);
2231 * The other node would not send this request to us unless
2232 * received announce that we are primary node for this LUN.
2233 * If this LUN does not exist now, it is probably result of
2234 * a race, so respond to initiator in the most opaque way.
2236 ctl_set_busy(ctsio);
2240 entry = ctl_get_cmd_entry(ctsio, NULL);
2241 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2242 mtx_unlock(&lun->lun_lock);
2246 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
2247 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = lun->be_lun;
2250 * Every I/O goes into the OOA queue for a
2251 * particular LUN, and stays there until completion.
2254 if (TAILQ_EMPTY(&lun->ooa_queue))
2255 lun->idle_time += getsbinuptime() - lun->last_busy;
2257 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2259 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2260 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2262 case CTL_ACTION_BLOCK:
2263 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2264 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2266 mtx_unlock(&lun->lun_lock);
2268 case CTL_ACTION_PASS:
2269 case CTL_ACTION_SKIP:
2270 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2271 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2272 ctl_enqueue_rtr((union ctl_io *)ctsio);
2273 mtx_unlock(&lun->lun_lock);
2275 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2276 mtx_unlock(&lun->lun_lock);
2278 /* send msg back to other side */
2279 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2280 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2281 msg_info.hdr.msg_type = CTL_MSG_R2R;
2282 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2283 sizeof(msg_info.hdr), M_WAITOK);
2286 case CTL_ACTION_OVERLAP:
2287 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2288 mtx_unlock(&lun->lun_lock);
2289 ctl_set_overlapped_cmd(ctsio);
2291 case CTL_ACTION_OVERLAP_TAG:
2292 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2293 mtx_unlock(&lun->lun_lock);
2294 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2296 case CTL_ACTION_ERROR:
2298 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2299 mtx_unlock(&lun->lun_lock);
2301 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2304 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2305 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2306 msg_info.hdr.serializing_sc = NULL;
2307 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2308 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2309 sizeof(msg_info.scsi), M_WAITOK);
2317 * Returns 0 for success, errno for failure.
2320 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2321 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2325 mtx_lock(&lun->lun_lock);
2326 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2327 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2329 struct ctl_ooa_entry *entry;
2332 * If we've got more than we can fit, just count the
2333 * remaining entries.
2335 if (*cur_fill_num >= ooa_hdr->alloc_num)
2338 entry = &kern_entries[*cur_fill_num];
2340 entry->tag_num = io->scsiio.tag_num;
2341 entry->lun_num = lun->lun;
2343 entry->start_bt = io->io_hdr.start_bt;
2345 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2346 entry->cdb_len = io->scsiio.cdb_len;
2347 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2348 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2350 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2351 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2353 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2354 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2356 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2357 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2359 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2360 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2362 mtx_unlock(&lun->lun_lock);
2366 ctl_copyin_alloc(void *user_addr, int len, char *error_str,
2367 size_t error_str_len)
2371 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2373 if (copyin(user_addr, kptr, len) != 0) {
2374 snprintf(error_str, error_str_len, "Error copying %d bytes "
2375 "from user address %p to kernel address %p", len,
2385 ctl_free_args(int num_args, struct ctl_be_arg *args)
2392 for (i = 0; i < num_args; i++) {
2393 free(args[i].kname, M_CTL);
2394 free(args[i].kvalue, M_CTL);
2400 static struct ctl_be_arg *
2401 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2402 char *error_str, size_t error_str_len)
2404 struct ctl_be_arg *args;
2407 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2408 error_str, error_str_len);
2413 for (i = 0; i < num_args; i++) {
2414 args[i].kname = NULL;
2415 args[i].kvalue = NULL;
2418 for (i = 0; i < num_args; i++) {
2421 args[i].kname = ctl_copyin_alloc(args[i].name,
2422 args[i].namelen, error_str, error_str_len);
2423 if (args[i].kname == NULL)
2426 if (args[i].kname[args[i].namelen - 1] != '\0') {
2427 snprintf(error_str, error_str_len, "Argument %d "
2428 "name is not NUL-terminated", i);
2432 if (args[i].flags & CTL_BEARG_RD) {
2433 tmpptr = ctl_copyin_alloc(args[i].value,
2434 args[i].vallen, error_str, error_str_len);
2437 if ((args[i].flags & CTL_BEARG_ASCII)
2438 && (tmpptr[args[i].vallen - 1] != '\0')) {
2439 snprintf(error_str, error_str_len, "Argument "
2440 "%d value is not NUL-terminated", i);
2443 args[i].kvalue = tmpptr;
2445 args[i].kvalue = malloc(args[i].vallen,
2446 M_CTL, M_WAITOK | M_ZERO);
2453 ctl_free_args(num_args, args);
2459 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2463 for (i = 0; i < num_args; i++) {
2464 if (args[i].flags & CTL_BEARG_WR)
2465 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2470 * Escape characters that are illegal or not recommended in XML.
2473 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2475 char *end = str + size;
2480 for (; *str && str < end; str++) {
2483 retval = sbuf_printf(sb, "&");
2486 retval = sbuf_printf(sb, ">");
2489 retval = sbuf_printf(sb, "<");
2492 retval = sbuf_putc(sb, *str);
2505 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2507 struct scsi_vpd_id_descriptor *desc;
2510 if (id == NULL || id->len < 4)
2512 desc = (struct scsi_vpd_id_descriptor *)id->data;
2513 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2514 case SVPD_ID_TYPE_T10:
2515 sbuf_printf(sb, "t10.");
2517 case SVPD_ID_TYPE_EUI64:
2518 sbuf_printf(sb, "eui.");
2520 case SVPD_ID_TYPE_NAA:
2521 sbuf_printf(sb, "naa.");
2523 case SVPD_ID_TYPE_SCSI_NAME:
2526 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2527 case SVPD_ID_CODESET_BINARY:
2528 for (i = 0; i < desc->length; i++)
2529 sbuf_printf(sb, "%02x", desc->identifier[i]);
2531 case SVPD_ID_CODESET_ASCII:
2532 sbuf_printf(sb, "%.*s", (int)desc->length,
2533 (char *)desc->identifier);
2535 case SVPD_ID_CODESET_UTF8:
2536 sbuf_printf(sb, "%s", (char *)desc->identifier);
2542 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2545 struct ctl_softc *softc = dev->si_drv1;
2546 struct ctl_lun *lun;
2553 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2555 case CTL_ENABLE_PORT:
2556 case CTL_DISABLE_PORT:
2557 case CTL_SET_PORT_WWNS: {
2558 struct ctl_port *port;
2559 struct ctl_port_entry *entry;
2561 entry = (struct ctl_port_entry *)addr;
2563 mtx_lock(&softc->ctl_lock);
2564 STAILQ_FOREACH(port, &softc->port_list, links) {
2567 if (port->targ_port < softc->port_min ||
2568 port->targ_port >= softc->port_max)
2573 if ((entry->port_type == CTL_PORT_NONE)
2574 && (entry->targ_port == port->targ_port)) {
2576 * If the user only wants to enable or
2577 * disable or set WWNs on a specific port,
2578 * do the operation and we're done.
2582 } else if (entry->port_type & port->port_type) {
2584 * Compare the user's type mask with the
2585 * particular frontend type to see if we
2592 * Make sure the user isn't trying to set
2593 * WWNs on multiple ports at the same time.
2595 if (cmd == CTL_SET_PORT_WWNS) {
2596 printf("%s: Can't set WWNs on "
2597 "multiple ports\n", __func__);
2606 * XXX KDM we have to drop the lock here, because
2607 * the online/offline operations can potentially
2608 * block. We need to reference count the frontends
2609 * so they can't go away,
2611 if (cmd == CTL_ENABLE_PORT) {
2612 mtx_unlock(&softc->ctl_lock);
2613 ctl_port_online(port);
2614 mtx_lock(&softc->ctl_lock);
2615 } else if (cmd == CTL_DISABLE_PORT) {
2616 mtx_unlock(&softc->ctl_lock);
2617 ctl_port_offline(port);
2618 mtx_lock(&softc->ctl_lock);
2619 } else if (cmd == CTL_SET_PORT_WWNS) {
2620 ctl_port_set_wwns(port,
2621 (entry->flags & CTL_PORT_WWNN_VALID) ?
2623 (entry->flags & CTL_PORT_WWPN_VALID) ?
2624 1 : 0, entry->wwpn);
2629 mtx_unlock(&softc->ctl_lock);
2633 struct ctl_ooa *ooa_hdr;
2634 struct ctl_ooa_entry *entries;
2635 uint32_t cur_fill_num;
2637 ooa_hdr = (struct ctl_ooa *)addr;
2639 if ((ooa_hdr->alloc_len == 0)
2640 || (ooa_hdr->alloc_num == 0)) {
2641 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2642 "must be non-zero\n", __func__,
2643 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2648 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2649 sizeof(struct ctl_ooa_entry))) {
2650 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2651 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2652 __func__, ooa_hdr->alloc_len,
2653 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2658 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2659 if (entries == NULL) {
2660 printf("%s: could not allocate %d bytes for OOA "
2661 "dump\n", __func__, ooa_hdr->alloc_len);
2666 mtx_lock(&softc->ctl_lock);
2667 if (((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0)
2668 && ((ooa_hdr->lun_num >= CTL_MAX_LUNS)
2669 || (softc->ctl_luns[ooa_hdr->lun_num] == NULL))) {
2670 mtx_unlock(&softc->ctl_lock);
2671 free(entries, M_CTL);
2672 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2673 __func__, (uintmax_t)ooa_hdr->lun_num);
2680 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2681 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2682 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2686 lun = softc->ctl_luns[ooa_hdr->lun_num];
2687 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2690 mtx_unlock(&softc->ctl_lock);
2692 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2693 ooa_hdr->fill_len = ooa_hdr->fill_num *
2694 sizeof(struct ctl_ooa_entry);
2695 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2697 printf("%s: error copying out %d bytes for OOA dump\n",
2698 __func__, ooa_hdr->fill_len);
2701 getbinuptime(&ooa_hdr->cur_bt);
2703 if (cur_fill_num > ooa_hdr->alloc_num) {
2704 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2705 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2707 ooa_hdr->dropped_num = 0;
2708 ooa_hdr->status = CTL_OOA_OK;
2711 free(entries, M_CTL);
2714 case CTL_DELAY_IO: {
2715 struct ctl_io_delay_info *delay_info;
2717 delay_info = (struct ctl_io_delay_info *)addr;
2720 mtx_lock(&softc->ctl_lock);
2722 if ((delay_info->lun_id >= CTL_MAX_LUNS)
2723 || (softc->ctl_luns[delay_info->lun_id] == NULL)) {
2724 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2726 lun = softc->ctl_luns[delay_info->lun_id];
2727 mtx_lock(&lun->lun_lock);
2729 delay_info->status = CTL_DELAY_STATUS_OK;
2731 switch (delay_info->delay_type) {
2732 case CTL_DELAY_TYPE_CONT:
2734 case CTL_DELAY_TYPE_ONESHOT:
2737 delay_info->status =
2738 CTL_DELAY_STATUS_INVALID_TYPE;
2742 switch (delay_info->delay_loc) {
2743 case CTL_DELAY_LOC_DATAMOVE:
2744 lun->delay_info.datamove_type =
2745 delay_info->delay_type;
2746 lun->delay_info.datamove_delay =
2747 delay_info->delay_secs;
2749 case CTL_DELAY_LOC_DONE:
2750 lun->delay_info.done_type =
2751 delay_info->delay_type;
2752 lun->delay_info.done_delay =
2753 delay_info->delay_secs;
2756 delay_info->status =
2757 CTL_DELAY_STATUS_INVALID_LOC;
2760 mtx_unlock(&lun->lun_lock);
2763 mtx_unlock(&softc->ctl_lock);
2765 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2766 #endif /* CTL_IO_DELAY */
2769 case CTL_GETSTATS: {
2770 struct ctl_stats *stats;
2773 stats = (struct ctl_stats *)addr;
2775 if ((sizeof(struct ctl_lun_io_stats) * softc->num_luns) >
2777 stats->status = CTL_SS_NEED_MORE_SPACE;
2778 stats->num_luns = softc->num_luns;
2782 * XXX KDM no locking here. If the LUN list changes,
2783 * things can blow up.
2785 for (i = 0, lun = STAILQ_FIRST(&softc->lun_list); lun != NULL;
2786 i++, lun = STAILQ_NEXT(lun, links)) {
2787 retval = copyout(&lun->stats, &stats->lun_stats[i],
2788 sizeof(lun->stats));
2792 stats->num_luns = softc->num_luns;
2793 stats->fill_len = sizeof(struct ctl_lun_io_stats) *
2795 stats->status = CTL_SS_OK;
2797 stats->flags = CTL_STATS_FLAG_TIME_VALID;
2799 stats->flags = CTL_STATS_FLAG_NONE;
2801 getnanouptime(&stats->timestamp);
2804 case CTL_ERROR_INJECT: {
2805 struct ctl_error_desc *err_desc, *new_err_desc;
2807 err_desc = (struct ctl_error_desc *)addr;
2809 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2811 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2813 mtx_lock(&softc->ctl_lock);
2814 lun = softc->ctl_luns[err_desc->lun_id];
2816 mtx_unlock(&softc->ctl_lock);
2817 free(new_err_desc, M_CTL);
2818 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2819 __func__, (uintmax_t)err_desc->lun_id);
2823 mtx_lock(&lun->lun_lock);
2824 mtx_unlock(&softc->ctl_lock);
2827 * We could do some checking here to verify the validity
2828 * of the request, but given the complexity of error
2829 * injection requests, the checking logic would be fairly
2832 * For now, if the request is invalid, it just won't get
2833 * executed and might get deleted.
2835 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2838 * XXX KDM check to make sure the serial number is unique,
2839 * in case we somehow manage to wrap. That shouldn't
2840 * happen for a very long time, but it's the right thing to
2843 new_err_desc->serial = lun->error_serial;
2844 err_desc->serial = lun->error_serial;
2845 lun->error_serial++;
2847 mtx_unlock(&lun->lun_lock);
2850 case CTL_ERROR_INJECT_DELETE: {
2851 struct ctl_error_desc *delete_desc, *desc, *desc2;
2854 delete_desc = (struct ctl_error_desc *)addr;
2857 mtx_lock(&softc->ctl_lock);
2858 lun = softc->ctl_luns[delete_desc->lun_id];
2860 mtx_unlock(&softc->ctl_lock);
2861 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2862 __func__, (uintmax_t)delete_desc->lun_id);
2866 mtx_lock(&lun->lun_lock);
2867 mtx_unlock(&softc->ctl_lock);
2868 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2869 if (desc->serial != delete_desc->serial)
2872 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2877 mtx_unlock(&lun->lun_lock);
2878 if (delete_done == 0) {
2879 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2880 "error serial %ju on LUN %u\n", __func__,
2881 delete_desc->serial, delete_desc->lun_id);
2887 case CTL_DUMP_STRUCTS: {
2889 struct ctl_port *port;
2890 struct ctl_frontend *fe;
2892 mtx_lock(&softc->ctl_lock);
2893 printf("CTL Persistent Reservation information start:\n");
2894 for (i = 0; i < CTL_MAX_LUNS; i++) {
2895 lun = softc->ctl_luns[i];
2898 || ((lun->flags & CTL_LUN_DISABLED) != 0))
2901 for (j = 0; j < CTL_MAX_PORTS; j++) {
2902 if (lun->pr_keys[j] == NULL)
2904 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2905 if (lun->pr_keys[j][k] == 0)
2907 printf(" LUN %d port %d iid %d key "
2909 (uintmax_t)lun->pr_keys[j][k]);
2913 printf("CTL Persistent Reservation information end\n");
2914 printf("CTL Ports:\n");
2915 STAILQ_FOREACH(port, &softc->port_list, links) {
2916 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2917 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2918 port->frontend->name, port->port_type,
2919 port->physical_port, port->virtual_port,
2920 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2921 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2922 if (port->wwpn_iid[j].in_use == 0 &&
2923 port->wwpn_iid[j].wwpn == 0 &&
2924 port->wwpn_iid[j].name == NULL)
2927 printf(" iid %u use %d WWPN %#jx '%s'\n",
2928 j, port->wwpn_iid[j].in_use,
2929 (uintmax_t)port->wwpn_iid[j].wwpn,
2930 port->wwpn_iid[j].name);
2933 printf("CTL Port information end\n");
2934 mtx_unlock(&softc->ctl_lock);
2936 * XXX KDM calling this without a lock. We'd likely want
2937 * to drop the lock before calling the frontend's dump
2940 printf("CTL Frontends:\n");
2941 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2942 printf(" Frontend '%s'\n", fe->name);
2943 if (fe->fe_dump != NULL)
2946 printf("CTL Frontend information end\n");
2950 struct ctl_lun_req *lun_req;
2951 struct ctl_backend_driver *backend;
2953 lun_req = (struct ctl_lun_req *)addr;
2955 backend = ctl_backend_find(lun_req->backend);
2956 if (backend == NULL) {
2957 lun_req->status = CTL_LUN_ERROR;
2958 snprintf(lun_req->error_str,
2959 sizeof(lun_req->error_str),
2960 "Backend \"%s\" not found.",
2964 if (lun_req->num_be_args > 0) {
2965 lun_req->kern_be_args = ctl_copyin_args(
2966 lun_req->num_be_args,
2969 sizeof(lun_req->error_str));
2970 if (lun_req->kern_be_args == NULL) {
2971 lun_req->status = CTL_LUN_ERROR;
2976 retval = backend->ioctl(dev, cmd, addr, flag, td);
2978 if (lun_req->num_be_args > 0) {
2979 ctl_copyout_args(lun_req->num_be_args,
2980 lun_req->kern_be_args);
2981 ctl_free_args(lun_req->num_be_args,
2982 lun_req->kern_be_args);
2986 case CTL_LUN_LIST: {
2988 struct ctl_lun_list *list;
2989 struct ctl_option *opt;
2991 list = (struct ctl_lun_list *)addr;
2994 * Allocate a fixed length sbuf here, based on the length
2995 * of the user's buffer. We could allocate an auto-extending
2996 * buffer, and then tell the user how much larger our
2997 * amount of data is than his buffer, but that presents
3000 * 1. The sbuf(9) routines use a blocking malloc, and so
3001 * we can't hold a lock while calling them with an
3002 * auto-extending buffer.
3004 * 2. There is not currently a LUN reference counting
3005 * mechanism, outside of outstanding transactions on
3006 * the LUN's OOA queue. So a LUN could go away on us
3007 * while we're getting the LUN number, backend-specific
3008 * information, etc. Thus, given the way things
3009 * currently work, we need to hold the CTL lock while
3010 * grabbing LUN information.
3012 * So, from the user's standpoint, the best thing to do is
3013 * allocate what he thinks is a reasonable buffer length,
3014 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3015 * double the buffer length and try again. (And repeat
3016 * that until he succeeds.)
3018 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3020 list->status = CTL_LUN_LIST_ERROR;
3021 snprintf(list->error_str, sizeof(list->error_str),
3022 "Unable to allocate %d bytes for LUN list",
3027 sbuf_printf(sb, "<ctllunlist>\n");
3029 mtx_lock(&softc->ctl_lock);
3030 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3031 mtx_lock(&lun->lun_lock);
3032 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3033 (uintmax_t)lun->lun);
3036 * Bail out as soon as we see that we've overfilled
3042 retval = sbuf_printf(sb, "\t<backend_type>%s"
3043 "</backend_type>\n",
3044 (lun->backend == NULL) ? "none" :
3045 lun->backend->name);
3050 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3051 lun->be_lun->lun_type);
3056 if (lun->backend == NULL) {
3057 retval = sbuf_printf(sb, "</lun>\n");
3063 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3064 (lun->be_lun->maxlba > 0) ?
3065 lun->be_lun->maxlba + 1 : 0);
3070 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3071 lun->be_lun->blocksize);
3076 retval = sbuf_printf(sb, "\t<serial_number>");
3081 retval = ctl_sbuf_printf_esc(sb,
3082 lun->be_lun->serial_num,
3083 sizeof(lun->be_lun->serial_num));
3088 retval = sbuf_printf(sb, "</serial_number>\n");
3093 retval = sbuf_printf(sb, "\t<device_id>");
3098 retval = ctl_sbuf_printf_esc(sb,
3099 lun->be_lun->device_id,
3100 sizeof(lun->be_lun->device_id));
3105 retval = sbuf_printf(sb, "</device_id>\n");
3110 if (lun->backend->lun_info != NULL) {
3111 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3115 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3116 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3117 opt->name, opt->value, opt->name);
3122 retval = sbuf_printf(sb, "</lun>\n");
3126 mtx_unlock(&lun->lun_lock);
3129 mtx_unlock(&lun->lun_lock);
3130 mtx_unlock(&softc->ctl_lock);
3133 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3136 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3137 snprintf(list->error_str, sizeof(list->error_str),
3138 "Out of space, %d bytes is too small",
3145 retval = copyout(sbuf_data(sb), list->lun_xml,
3148 list->fill_len = sbuf_len(sb) + 1;
3149 list->status = CTL_LUN_LIST_OK;
3154 struct ctl_iscsi *ci;
3155 struct ctl_frontend *fe;
3157 ci = (struct ctl_iscsi *)addr;
3159 fe = ctl_frontend_find("iscsi");
3161 ci->status = CTL_ISCSI_ERROR;
3162 snprintf(ci->error_str, sizeof(ci->error_str),
3163 "Frontend \"iscsi\" not found.");
3167 retval = fe->ioctl(dev, cmd, addr, flag, td);
3170 case CTL_PORT_REQ: {
3171 struct ctl_req *req;
3172 struct ctl_frontend *fe;
3174 req = (struct ctl_req *)addr;
3176 fe = ctl_frontend_find(req->driver);
3178 req->status = CTL_LUN_ERROR;
3179 snprintf(req->error_str, sizeof(req->error_str),
3180 "Frontend \"%s\" not found.", req->driver);
3183 if (req->num_args > 0) {
3184 req->kern_args = ctl_copyin_args(req->num_args,
3185 req->args, req->error_str, sizeof(req->error_str));
3186 if (req->kern_args == NULL) {
3187 req->status = CTL_LUN_ERROR;
3193 retval = fe->ioctl(dev, cmd, addr, flag, td);
3197 if (req->num_args > 0) {
3198 ctl_copyout_args(req->num_args, req->kern_args);
3199 ctl_free_args(req->num_args, req->kern_args);
3203 case CTL_PORT_LIST: {
3205 struct ctl_port *port;
3206 struct ctl_lun_list *list;
3207 struct ctl_option *opt;
3211 list = (struct ctl_lun_list *)addr;
3213 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3215 list->status = CTL_LUN_LIST_ERROR;
3216 snprintf(list->error_str, sizeof(list->error_str),
3217 "Unable to allocate %d bytes for LUN list",
3222 sbuf_printf(sb, "<ctlportlist>\n");
3224 mtx_lock(&softc->ctl_lock);
3225 STAILQ_FOREACH(port, &softc->port_list, links) {
3226 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3227 (uintmax_t)port->targ_port);
3230 * Bail out as soon as we see that we've overfilled
3236 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3237 "</frontend_type>\n", port->frontend->name);
3241 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3246 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3247 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3251 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3256 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3257 port->physical_port);
3261 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3262 port->virtual_port);
3266 if (port->target_devid != NULL) {
3267 sbuf_printf(sb, "\t<target>");
3268 ctl_id_sbuf(port->target_devid, sb);
3269 sbuf_printf(sb, "</target>\n");
3272 if (port->port_devid != NULL) {
3273 sbuf_printf(sb, "\t<port>");
3274 ctl_id_sbuf(port->port_devid, sb);
3275 sbuf_printf(sb, "</port>\n");
3278 if (port->port_info != NULL) {
3279 retval = port->port_info(port->onoff_arg, sb);
3283 STAILQ_FOREACH(opt, &port->options, links) {
3284 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3285 opt->name, opt->value, opt->name);
3290 if (port->lun_map != NULL) {
3291 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3292 for (j = 0; j < CTL_MAX_LUNS; j++) {
3293 plun = ctl_lun_map_from_port(port, j);
3294 if (plun >= CTL_MAX_LUNS)
3297 "\t<lun id=\"%u\">%u</lun>\n",
3302 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3303 if (port->wwpn_iid[j].in_use == 0 ||
3304 (port->wwpn_iid[j].wwpn == 0 &&
3305 port->wwpn_iid[j].name == NULL))
3308 if (port->wwpn_iid[j].name != NULL)
3309 retval = sbuf_printf(sb,
3310 "\t<initiator id=\"%u\">%s</initiator>\n",
3311 j, port->wwpn_iid[j].name);
3313 retval = sbuf_printf(sb,
3314 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3315 j, port->wwpn_iid[j].wwpn);
3322 retval = sbuf_printf(sb, "</targ_port>\n");
3326 mtx_unlock(&softc->ctl_lock);
3329 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3332 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3333 snprintf(list->error_str, sizeof(list->error_str),
3334 "Out of space, %d bytes is too small",
3341 retval = copyout(sbuf_data(sb), list->lun_xml,
3344 list->fill_len = sbuf_len(sb) + 1;
3345 list->status = CTL_LUN_LIST_OK;
3350 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3351 struct ctl_port *port;
3353 mtx_lock(&softc->ctl_lock);
3354 if (lm->port < softc->port_min ||
3355 lm->port >= softc->port_max ||
3356 (port = softc->ctl_ports[lm->port]) == NULL) {
3357 mtx_unlock(&softc->ctl_lock);
3360 if (port->status & CTL_PORT_STATUS_ONLINE) {
3361 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3362 if (ctl_lun_map_to_port(port, lun->lun) >=
3365 mtx_lock(&lun->lun_lock);
3366 ctl_est_ua_port(lun, lm->port, -1,
3368 mtx_unlock(&lun->lun_lock);
3371 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3372 if (lm->plun < CTL_MAX_LUNS) {
3373 if (lm->lun == UINT32_MAX)
3374 retval = ctl_lun_map_unset(port, lm->plun);
3375 else if (lm->lun < CTL_MAX_LUNS &&
3376 softc->ctl_luns[lm->lun] != NULL)
3377 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3380 } else if (lm->plun == UINT32_MAX) {
3381 if (lm->lun == UINT32_MAX)
3382 retval = ctl_lun_map_deinit(port);
3384 retval = ctl_lun_map_init(port);
3387 if (port->status & CTL_PORT_STATUS_ONLINE)
3388 ctl_isc_announce_port(port);
3392 /* XXX KDM should we fix this? */
3394 struct ctl_backend_driver *backend;
3401 * We encode the backend type as the ioctl type for backend
3402 * ioctls. So parse it out here, and then search for a
3403 * backend of this type.
3405 type = _IOC_TYPE(cmd);
3407 STAILQ_FOREACH(backend, &softc->be_list, links) {
3408 if (backend->type == type) {
3414 printf("ctl: unknown ioctl command %#lx or backend "
3419 retval = backend->ioctl(dev, cmd, addr, flag, td);
3429 ctl_get_initindex(struct ctl_nexus *nexus)
3431 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3435 ctl_lun_map_init(struct ctl_port *port)
3437 struct ctl_softc *softc = port->ctl_softc;
3438 struct ctl_lun *lun;
3441 if (port->lun_map == NULL)
3442 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
3444 if (port->lun_map == NULL)
3446 for (i = 0; i < CTL_MAX_LUNS; i++)
3447 port->lun_map[i] = UINT32_MAX;
3448 if (port->status & CTL_PORT_STATUS_ONLINE) {
3449 if (port->lun_disable != NULL) {
3450 STAILQ_FOREACH(lun, &softc->lun_list, links)
3451 port->lun_disable(port->targ_lun_arg, lun->lun);
3453 ctl_isc_announce_port(port);
3459 ctl_lun_map_deinit(struct ctl_port *port)
3461 struct ctl_softc *softc = port->ctl_softc;
3462 struct ctl_lun *lun;
3464 if (port->lun_map == NULL)
3466 free(port->lun_map, M_CTL);
3467 port->lun_map = NULL;
3468 if (port->status & CTL_PORT_STATUS_ONLINE) {
3469 if (port->lun_enable != NULL) {
3470 STAILQ_FOREACH(lun, &softc->lun_list, links)
3471 port->lun_enable(port->targ_lun_arg, lun->lun);
3473 ctl_isc_announce_port(port);
3479 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3484 if (port->lun_map == NULL) {
3485 status = ctl_lun_map_init(port);
3489 old = port->lun_map[plun];
3490 port->lun_map[plun] = glun;
3491 if ((port->status & CTL_PORT_STATUS_ONLINE) && old >= CTL_MAX_LUNS) {
3492 if (port->lun_enable != NULL)
3493 port->lun_enable(port->targ_lun_arg, plun);
3494 ctl_isc_announce_port(port);
3500 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3504 if (port->lun_map == NULL)
3506 old = port->lun_map[plun];
3507 port->lun_map[plun] = UINT32_MAX;
3508 if ((port->status & CTL_PORT_STATUS_ONLINE) && old < CTL_MAX_LUNS) {
3509 if (port->lun_disable != NULL)
3510 port->lun_disable(port->targ_lun_arg, plun);
3511 ctl_isc_announce_port(port);
3517 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3521 return (UINT32_MAX);
3522 if (port->lun_map == NULL || lun_id >= CTL_MAX_LUNS)
3524 return (port->lun_map[lun_id]);
3528 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3533 return (UINT32_MAX);
3534 if (port->lun_map == NULL)
3536 for (i = 0; i < CTL_MAX_LUNS; i++) {
3537 if (port->lun_map[i] == lun_id)
3540 return (UINT32_MAX);
3543 static struct ctl_port *
3544 ctl_io_port(struct ctl_io_hdr *io_hdr)
3547 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3551 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3555 for (i = first; i < last; i++) {
3556 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3563 ctl_set_mask(uint32_t *mask, uint32_t bit)
3565 uint32_t chunk, piece;
3568 piece = bit % (sizeof(uint32_t) * 8);
3570 if ((mask[chunk] & (1 << piece)) != 0)
3573 mask[chunk] |= (1 << piece);
3579 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3581 uint32_t chunk, piece;
3584 piece = bit % (sizeof(uint32_t) * 8);
3586 if ((mask[chunk] & (1 << piece)) == 0)
3589 mask[chunk] &= ~(1 << piece);
3595 ctl_is_set(uint32_t *mask, uint32_t bit)
3597 uint32_t chunk, piece;
3600 piece = bit % (sizeof(uint32_t) * 8);
3602 if ((mask[chunk] & (1 << piece)) == 0)
3609 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3613 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3616 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3620 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3624 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3627 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3631 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3636 i = residx/CTL_MAX_INIT_PER_PORT;
3637 if (lun->pr_keys[i] != NULL)
3639 mtx_unlock(&lun->lun_lock);
3640 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3642 mtx_lock(&lun->lun_lock);
3643 if (lun->pr_keys[i] == NULL)
3644 lun->pr_keys[i] = p;
3650 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3654 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3655 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3656 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3660 * ctl_softc, pool_name, total_ctl_io are passed in.
3661 * npool is passed out.
3664 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3665 uint32_t total_ctl_io, void **npool)
3668 struct ctl_io_pool *pool;
3670 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3675 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3676 pool->ctl_softc = ctl_softc;
3677 pool->zone = uma_zsecond_create(pool->name, NULL,
3678 NULL, NULL, NULL, ctl_softc->io_zone);
3679 /* uma_prealloc(pool->zone, total_ctl_io); */
3683 *npool = ctl_softc->io_zone;
3689 ctl_pool_free(struct ctl_io_pool *pool)
3696 uma_zdestroy(pool->zone);
3702 ctl_alloc_io(void *pool_ref)
3706 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3708 io = uma_zalloc(pool->zone, M_WAITOK);
3710 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3713 io->io_hdr.pool = pool_ref;
3718 ctl_alloc_io_nowait(void *pool_ref)
3722 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3724 io = uma_zalloc(pool->zone, M_NOWAIT);
3726 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3729 io->io_hdr.pool = pool_ref;
3734 ctl_free_io(union ctl_io *io)
3737 struct ctl_io_pool *pool;
3744 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3745 uma_zfree(pool->zone, io);
3747 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3752 ctl_zero_io(union ctl_io *io)
3760 * May need to preserve linked list pointers at some point too.
3762 pool_ref = io->io_hdr.pool;
3763 memset(io, 0, sizeof(*io));
3764 io->io_hdr.pool = pool_ref;
3768 ctl_expand_number(const char *buf, uint64_t *num)
3774 number = strtoq(buf, &endptr, 0);
3776 switch (tolower((unsigned char)*endptr)) {
3796 case '\0': /* No unit. */
3800 /* Unrecognized unit. */
3804 if ((number << shift) >> shift != number) {
3808 *num = number << shift;
3814 * This routine could be used in the future to load default and/or saved
3815 * mode page parameters for a particuar lun.
3818 ctl_init_page_index(struct ctl_lun *lun)
3821 struct ctl_page_index *page_index;
3825 memcpy(&lun->mode_pages.index, page_index_template,
3826 sizeof(page_index_template));
3828 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3830 page_index = &lun->mode_pages.index[i];
3831 if (lun->be_lun->lun_type == T_DIRECT &&
3832 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3834 if (lun->be_lun->lun_type == T_PROCESSOR &&
3835 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3837 if (lun->be_lun->lun_type == T_CDROM &&
3838 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3841 switch (page_index->page_code & SMPH_PC_MASK) {
3842 case SMS_RW_ERROR_RECOVERY_PAGE: {
3843 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3844 panic("subpage is incorrect!");
3845 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3846 &rw_er_page_default,
3847 sizeof(rw_er_page_default));
3848 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3849 &rw_er_page_changeable,
3850 sizeof(rw_er_page_changeable));
3851 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3852 &rw_er_page_default,
3853 sizeof(rw_er_page_default));
3854 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3855 &rw_er_page_default,
3856 sizeof(rw_er_page_default));
3857 page_index->page_data =
3858 (uint8_t *)lun->mode_pages.rw_er_page;
3861 case SMS_FORMAT_DEVICE_PAGE: {
3862 struct scsi_format_page *format_page;
3864 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3865 panic("subpage is incorrect!");
3868 * Sectors per track are set above. Bytes per
3869 * sector need to be set here on a per-LUN basis.
3871 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3872 &format_page_default,
3873 sizeof(format_page_default));
3874 memcpy(&lun->mode_pages.format_page[
3875 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3876 sizeof(format_page_changeable));
3877 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3878 &format_page_default,
3879 sizeof(format_page_default));
3880 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3881 &format_page_default,
3882 sizeof(format_page_default));
3884 format_page = &lun->mode_pages.format_page[
3886 scsi_ulto2b(lun->be_lun->blocksize,
3887 format_page->bytes_per_sector);
3889 format_page = &lun->mode_pages.format_page[
3891 scsi_ulto2b(lun->be_lun->blocksize,
3892 format_page->bytes_per_sector);
3894 format_page = &lun->mode_pages.format_page[
3896 scsi_ulto2b(lun->be_lun->blocksize,
3897 format_page->bytes_per_sector);
3899 page_index->page_data =
3900 (uint8_t *)lun->mode_pages.format_page;
3903 case SMS_RIGID_DISK_PAGE: {
3904 struct scsi_rigid_disk_page *rigid_disk_page;
3905 uint32_t sectors_per_cylinder;
3909 #endif /* !__XSCALE__ */
3911 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3912 panic("invalid subpage value %d",
3913 page_index->subpage);
3916 * Rotation rate and sectors per track are set
3917 * above. We calculate the cylinders here based on
3918 * capacity. Due to the number of heads and
3919 * sectors per track we're using, smaller arrays
3920 * may turn out to have 0 cylinders. Linux and
3921 * FreeBSD don't pay attention to these mode pages
3922 * to figure out capacity, but Solaris does. It
3923 * seems to deal with 0 cylinders just fine, and
3924 * works out a fake geometry based on the capacity.
3926 memcpy(&lun->mode_pages.rigid_disk_page[
3927 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3928 sizeof(rigid_disk_page_default));
3929 memcpy(&lun->mode_pages.rigid_disk_page[
3930 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3931 sizeof(rigid_disk_page_changeable));
3933 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
3937 * The divide method here will be more accurate,
3938 * probably, but results in floating point being
3939 * used in the kernel on i386 (__udivdi3()). On the
3940 * XScale, though, __udivdi3() is implemented in
3943 * The shift method for cylinder calculation is
3944 * accurate if sectors_per_cylinder is a power of
3945 * 2. Otherwise it might be slightly off -- you
3946 * might have a bit of a truncation problem.
3949 cylinders = (lun->be_lun->maxlba + 1) /
3950 sectors_per_cylinder;
3952 for (shift = 31; shift > 0; shift--) {
3953 if (sectors_per_cylinder & (1 << shift))
3956 cylinders = (lun->be_lun->maxlba + 1) >> shift;
3960 * We've basically got 3 bytes, or 24 bits for the
3961 * cylinder size in the mode page. If we're over,
3962 * just round down to 2^24.
3964 if (cylinders > 0xffffff)
3965 cylinders = 0xffffff;
3967 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
3969 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
3971 if ((value = ctl_get_opt(&lun->be_lun->options,
3973 scsi_ulto2b(strtol(value, NULL, 0),
3974 rigid_disk_page->rotation_rate);
3977 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
3978 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3979 sizeof(rigid_disk_page_default));
3980 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
3981 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
3982 sizeof(rigid_disk_page_default));
3984 page_index->page_data =
3985 (uint8_t *)lun->mode_pages.rigid_disk_page;
3988 case SMS_CACHING_PAGE: {
3989 struct scsi_caching_page *caching_page;
3991 if (page_index->subpage != SMS_SUBPAGE_PAGE_0)
3992 panic("invalid subpage value %d",
3993 page_index->subpage);
3994 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
3995 &caching_page_default,
3996 sizeof(caching_page_default));
3997 memcpy(&lun->mode_pages.caching_page[
3998 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
3999 sizeof(caching_page_changeable));
4000 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4001 &caching_page_default,
4002 sizeof(caching_page_default));
4003 caching_page = &lun->mode_pages.caching_page[
4005 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4006 if (value != NULL && strcmp(value, "off") == 0)
4007 caching_page->flags1 &= ~SCP_WCE;
4008 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4009 if (value != NULL && strcmp(value, "off") == 0)
4010 caching_page->flags1 |= SCP_RCD;
4011 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4012 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4013 sizeof(caching_page_default));
4014 page_index->page_data =
4015 (uint8_t *)lun->mode_pages.caching_page;
4018 case SMS_CONTROL_MODE_PAGE: {
4019 switch (page_index->subpage) {
4020 case SMS_SUBPAGE_PAGE_0: {
4021 struct scsi_control_page *control_page;
4023 memcpy(&lun->mode_pages.control_page[
4025 &control_page_default,
4026 sizeof(control_page_default));
4027 memcpy(&lun->mode_pages.control_page[
4028 CTL_PAGE_CHANGEABLE],
4029 &control_page_changeable,
4030 sizeof(control_page_changeable));
4031 memcpy(&lun->mode_pages.control_page[
4033 &control_page_default,
4034 sizeof(control_page_default));
4035 control_page = &lun->mode_pages.control_page[
4037 value = ctl_get_opt(&lun->be_lun->options,
4039 if (value != NULL &&
4040 strcmp(value, "unrestricted") == 0) {
4041 control_page->queue_flags &=
4042 ~SCP_QUEUE_ALG_MASK;
4043 control_page->queue_flags |=
4044 SCP_QUEUE_ALG_UNRESTRICTED;
4046 memcpy(&lun->mode_pages.control_page[
4048 &lun->mode_pages.control_page[
4050 sizeof(control_page_default));
4051 page_index->page_data =
4052 (uint8_t *)lun->mode_pages.control_page;
4056 memcpy(&lun->mode_pages.control_ext_page[
4058 &control_ext_page_default,
4059 sizeof(control_ext_page_default));
4060 memcpy(&lun->mode_pages.control_ext_page[
4061 CTL_PAGE_CHANGEABLE],
4062 &control_ext_page_changeable,
4063 sizeof(control_ext_page_changeable));
4064 memcpy(&lun->mode_pages.control_ext_page[
4066 &control_ext_page_default,
4067 sizeof(control_ext_page_default));
4068 memcpy(&lun->mode_pages.control_ext_page[
4070 &lun->mode_pages.control_ext_page[
4072 sizeof(control_ext_page_default));
4073 page_index->page_data =
4074 (uint8_t *)lun->mode_pages.control_ext_page;
4079 case SMS_INFO_EXCEPTIONS_PAGE: {
4080 switch (page_index->subpage) {
4081 case SMS_SUBPAGE_PAGE_0:
4082 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4084 sizeof(ie_page_default));
4085 memcpy(&lun->mode_pages.ie_page[
4086 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4087 sizeof(ie_page_changeable));
4088 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4090 sizeof(ie_page_default));
4091 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4093 sizeof(ie_page_default));
4094 page_index->page_data =
4095 (uint8_t *)lun->mode_pages.ie_page;
4098 struct ctl_logical_block_provisioning_page *page;
4100 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4102 sizeof(lbp_page_default));
4103 memcpy(&lun->mode_pages.lbp_page[
4104 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4105 sizeof(lbp_page_changeable));
4106 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4108 sizeof(lbp_page_default));
4109 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4110 value = ctl_get_opt(&lun->be_lun->options,
4112 if (value != NULL &&
4113 ctl_expand_number(value, &ival) == 0) {
4114 page->descr[0].flags |= SLBPPD_ENABLED |
4116 if (lun->be_lun->blocksize)
4117 ival /= lun->be_lun->blocksize;
4120 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4121 page->descr[0].count);
4123 value = ctl_get_opt(&lun->be_lun->options,
4125 if (value != NULL &&
4126 ctl_expand_number(value, &ival) == 0) {
4127 page->descr[1].flags |= SLBPPD_ENABLED |
4129 if (lun->be_lun->blocksize)
4130 ival /= lun->be_lun->blocksize;
4133 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4134 page->descr[1].count);
4136 value = ctl_get_opt(&lun->be_lun->options,
4137 "pool-avail-threshold");
4138 if (value != NULL &&
4139 ctl_expand_number(value, &ival) == 0) {
4140 page->descr[2].flags |= SLBPPD_ENABLED |
4142 if (lun->be_lun->blocksize)
4143 ival /= lun->be_lun->blocksize;
4146 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4147 page->descr[2].count);
4149 value = ctl_get_opt(&lun->be_lun->options,
4150 "pool-used-threshold");
4151 if (value != NULL &&
4152 ctl_expand_number(value, &ival) == 0) {
4153 page->descr[3].flags |= SLBPPD_ENABLED |
4155 if (lun->be_lun->blocksize)
4156 ival /= lun->be_lun->blocksize;
4159 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4160 page->descr[3].count);
4162 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4163 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4164 sizeof(lbp_page_default));
4165 page_index->page_data =
4166 (uint8_t *)lun->mode_pages.lbp_page;
4170 case SMS_CDDVD_CAPS_PAGE:{
4171 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4172 &cddvd_page_default,
4173 sizeof(cddvd_page_default));
4174 memcpy(&lun->mode_pages.cddvd_page[
4175 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4176 sizeof(cddvd_page_changeable));
4177 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4178 &cddvd_page_default,
4179 sizeof(cddvd_page_default));
4180 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4181 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4182 sizeof(cddvd_page_default));
4183 page_index->page_data =
4184 (uint8_t *)lun->mode_pages.cddvd_page;
4187 case SMS_VENDOR_SPECIFIC_PAGE:{
4188 switch (page_index->subpage) {
4189 case DBGCNF_SUBPAGE_CODE: {
4190 memcpy(&lun->mode_pages.debugconf_subpage[
4192 &debugconf_page_default,
4193 sizeof(debugconf_page_default));
4194 memcpy(&lun->mode_pages.debugconf_subpage[
4195 CTL_PAGE_CHANGEABLE],
4196 &debugconf_page_changeable,
4197 sizeof(debugconf_page_changeable));
4198 memcpy(&lun->mode_pages.debugconf_subpage[
4200 &debugconf_page_default,
4201 sizeof(debugconf_page_default));
4202 memcpy(&lun->mode_pages.debugconf_subpage[
4204 &debugconf_page_default,
4205 sizeof(debugconf_page_default));
4206 page_index->page_data =
4207 (uint8_t *)lun->mode_pages.debugconf_subpage;
4211 panic("invalid subpage value %d",
4212 page_index->subpage);
4218 panic("invalid page value %d",
4219 page_index->page_code & SMPH_PC_MASK);
4224 return (CTL_RETVAL_COMPLETE);
4228 ctl_init_log_page_index(struct ctl_lun *lun)
4230 struct ctl_page_index *page_index;
4233 memcpy(&lun->log_pages.index, log_page_index_template,
4234 sizeof(log_page_index_template));
4237 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4239 page_index = &lun->log_pages.index[i];
4240 if (lun->be_lun->lun_type == T_DIRECT &&
4241 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4243 if (lun->be_lun->lun_type == T_PROCESSOR &&
4244 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4246 if (lun->be_lun->lun_type == T_CDROM &&
4247 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4250 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4251 lun->backend->lun_attr == NULL)
4254 if (page_index->page_code != prev) {
4255 lun->log_pages.pages_page[j] = page_index->page_code;
4256 prev = page_index->page_code;
4259 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4260 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4263 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4264 lun->log_pages.index[0].page_len = j;
4265 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4266 lun->log_pages.index[1].page_len = k * 2;
4267 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4268 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4269 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4270 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4272 return (CTL_RETVAL_COMPLETE);
4276 hex2bin(const char *str, uint8_t *buf, int buf_size)
4281 memset(buf, 0, buf_size);
4282 while (isspace(str[0]))
4284 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4287 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4291 else if (isalpha(c))
4292 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4298 buf[i / 2] |= (c << 4);
4302 return ((i + 1) / 2);
4309 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4310 * wants us to allocate the LUN and he can block.
4311 * - ctl_softc is always set
4312 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4314 * Returns 0 for success, non-zero (errno) for failure.
4317 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4318 struct ctl_be_lun *const be_lun)
4320 struct ctl_lun *nlun, *lun;
4321 struct scsi_vpd_id_descriptor *desc;
4322 struct scsi_vpd_id_t10 *t10id;
4323 const char *eui, *naa, *scsiname, *vendor, *value;
4324 int lun_number, i, lun_malloced;
4325 int devidlen, idlen1, idlen2 = 0, len;
4331 * We currently only support Direct Access or Processor LUN types.
4333 switch (be_lun->lun_type) {
4341 be_lun->lun_config_status(be_lun->be_lun,
4342 CTL_LUN_CONFIG_FAILURE);
4345 if (ctl_lun == NULL) {
4346 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4353 memset(lun, 0, sizeof(*lun));
4355 lun->flags = CTL_LUN_MALLOCED;
4357 /* Generate LUN ID. */
4358 devidlen = max(CTL_DEVID_MIN_LEN,
4359 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4360 idlen1 = sizeof(*t10id) + devidlen;
4361 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4362 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4363 if (scsiname != NULL) {
4364 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4365 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4367 eui = ctl_get_opt(&be_lun->options, "eui");
4369 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4371 naa = ctl_get_opt(&be_lun->options, "naa");
4373 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4375 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4376 M_CTL, M_WAITOK | M_ZERO);
4377 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4378 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4379 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4380 desc->length = idlen1;
4381 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4382 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4383 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4384 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4386 strncpy(t10id->vendor, vendor,
4387 min(sizeof(t10id->vendor), strlen(vendor)));
4389 strncpy((char *)t10id->vendor_spec_id,
4390 (char *)be_lun->device_id, devidlen);
4391 if (scsiname != NULL) {
4392 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4394 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4395 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4396 SVPD_ID_TYPE_SCSI_NAME;
4397 desc->length = idlen2;
4398 strlcpy(desc->identifier, scsiname, idlen2);
4401 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4403 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4404 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4406 desc->length = hex2bin(eui, desc->identifier, 16);
4407 desc->length = desc->length > 12 ? 16 :
4408 (desc->length > 8 ? 12 : 8);
4409 len -= 16 - desc->length;
4412 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4414 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4415 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4417 desc->length = hex2bin(naa, desc->identifier, 16);
4418 desc->length = desc->length > 8 ? 16 : 8;
4419 len -= 16 - desc->length;
4421 lun->lun_devid->len = len;
4423 mtx_lock(&ctl_softc->ctl_lock);
4425 * See if the caller requested a particular LUN number. If so, see
4426 * if it is available. Otherwise, allocate the first available LUN.
4428 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4429 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4430 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4431 mtx_unlock(&ctl_softc->ctl_lock);
4432 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4433 printf("ctl: requested LUN ID %d is higher "
4434 "than CTL_MAX_LUNS - 1 (%d)\n",
4435 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4438 * XXX KDM return an error, or just assign
4439 * another LUN ID in this case??
4441 printf("ctl: requested LUN ID %d is already "
4442 "in use\n", be_lun->req_lun_id);
4444 if (lun->flags & CTL_LUN_MALLOCED)
4446 be_lun->lun_config_status(be_lun->be_lun,
4447 CTL_LUN_CONFIG_FAILURE);
4450 lun_number = be_lun->req_lun_id;
4452 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4453 if (lun_number == -1) {
4454 mtx_unlock(&ctl_softc->ctl_lock);
4455 printf("ctl: can't allocate LUN, out of LUNs\n");
4456 if (lun->flags & CTL_LUN_MALLOCED)
4458 be_lun->lun_config_status(be_lun->be_lun,
4459 CTL_LUN_CONFIG_FAILURE);
4463 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4465 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4466 lun->lun = lun_number;
4467 lun->be_lun = be_lun;
4469 * The processor LUN is always enabled. Disk LUNs come on line
4470 * disabled, and must be enabled by the backend.
4472 lun->flags |= CTL_LUN_DISABLED;
4473 lun->backend = be_lun->be;
4474 be_lun->ctl_lun = lun;
4475 be_lun->lun_id = lun_number;
4476 atomic_add_int(&be_lun->be->num_luns, 1);
4477 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4478 lun->flags |= CTL_LUN_EJECTED;
4479 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4480 lun->flags |= CTL_LUN_NO_MEDIA;
4481 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4482 lun->flags |= CTL_LUN_STOPPED;
4484 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4485 lun->flags |= CTL_LUN_PRIMARY_SC;
4487 value = ctl_get_opt(&be_lun->options, "removable");
4488 if (value != NULL) {
4489 if (strcmp(value, "on") == 0)
4490 lun->flags |= CTL_LUN_REMOVABLE;
4491 } else if (be_lun->lun_type == T_CDROM)
4492 lun->flags |= CTL_LUN_REMOVABLE;
4494 lun->ctl_softc = ctl_softc;
4496 lun->last_busy = getsbinuptime();
4498 TAILQ_INIT(&lun->ooa_queue);
4499 TAILQ_INIT(&lun->blocked_queue);
4500 STAILQ_INIT(&lun->error_list);
4501 ctl_tpc_lun_init(lun);
4504 * Initialize the mode and log page index.
4506 ctl_init_page_index(lun);
4507 ctl_init_log_page_index(lun);
4510 * Now, before we insert this lun on the lun list, set the lun
4511 * inventory changed UA for all other luns.
4513 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4514 mtx_lock(&nlun->lun_lock);
4515 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4516 mtx_unlock(&nlun->lun_lock);
4519 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4521 ctl_softc->ctl_luns[lun_number] = lun;
4523 ctl_softc->num_luns++;
4525 /* Setup statistics gathering */
4526 lun->stats.device_type = be_lun->lun_type;
4527 lun->stats.lun_number = lun_number;
4528 lun->stats.blocksize = be_lun->blocksize;
4529 if (be_lun->blocksize == 0)
4530 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4531 for (i = 0;i < CTL_MAX_PORTS;i++)
4532 lun->stats.ports[i].targ_port = i;
4534 mtx_unlock(&ctl_softc->ctl_lock);
4536 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4543 * - LUN has already been marked invalid and any pending I/O has been taken
4547 ctl_free_lun(struct ctl_lun *lun)
4549 struct ctl_softc *softc;
4550 struct ctl_lun *nlun;
4553 softc = lun->ctl_softc;
4555 mtx_assert(&softc->ctl_lock, MA_OWNED);
4557 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4559 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4561 softc->ctl_luns[lun->lun] = NULL;
4563 if (!TAILQ_EMPTY(&lun->ooa_queue))
4564 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4569 * Tell the backend to free resources, if this LUN has a backend.
4571 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4572 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4574 ctl_tpc_lun_shutdown(lun);
4575 mtx_destroy(&lun->lun_lock);
4576 free(lun->lun_devid, M_CTL);
4577 for (i = 0; i < CTL_MAX_PORTS; i++)
4578 free(lun->pending_ua[i], M_CTL);
4579 for (i = 0; i < CTL_MAX_PORTS; i++)
4580 free(lun->pr_keys[i], M_CTL);
4581 free(lun->write_buffer, M_CTL);
4582 if (lun->flags & CTL_LUN_MALLOCED)
4585 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4586 mtx_lock(&nlun->lun_lock);
4587 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4588 mtx_unlock(&nlun->lun_lock);
4595 ctl_create_lun(struct ctl_be_lun *be_lun)
4599 * ctl_alloc_lun() should handle all potential failure cases.
4601 ctl_alloc_lun(control_softc, NULL, be_lun);
4605 ctl_add_lun(struct ctl_be_lun *be_lun)
4607 struct ctl_softc *softc = control_softc;
4609 mtx_lock(&softc->ctl_lock);
4610 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4611 mtx_unlock(&softc->ctl_lock);
4612 wakeup(&softc->pending_lun_queue);
4618 ctl_enable_lun(struct ctl_be_lun *be_lun)
4620 struct ctl_softc *softc;
4621 struct ctl_port *port, *nport;
4622 struct ctl_lun *lun;
4625 lun = (struct ctl_lun *)be_lun->ctl_lun;
4626 softc = lun->ctl_softc;
4628 mtx_lock(&softc->ctl_lock);
4629 mtx_lock(&lun->lun_lock);
4630 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4632 * eh? Why did we get called if the LUN is already
4635 mtx_unlock(&lun->lun_lock);
4636 mtx_unlock(&softc->ctl_lock);
4639 lun->flags &= ~CTL_LUN_DISABLED;
4640 mtx_unlock(&lun->lun_lock);
4642 for (port = STAILQ_FIRST(&softc->port_list); port != NULL; port = nport) {
4643 nport = STAILQ_NEXT(port, links);
4644 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4645 port->lun_map != NULL || port->lun_enable == NULL)
4649 * Drop the lock while we call the FETD's enable routine.
4650 * This can lead to a callback into CTL (at least in the
4651 * case of the internal initiator frontend.
4653 mtx_unlock(&softc->ctl_lock);
4654 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4655 mtx_lock(&softc->ctl_lock);
4657 printf("%s: FETD %s port %d returned error "
4658 "%d for lun_enable on lun %jd\n",
4659 __func__, port->port_name, port->targ_port,
4660 retval, (intmax_t)lun->lun);
4664 mtx_unlock(&softc->ctl_lock);
4665 ctl_isc_announce_lun(lun);
4671 ctl_disable_lun(struct ctl_be_lun *be_lun)
4673 struct ctl_softc *softc;
4674 struct ctl_port *port;
4675 struct ctl_lun *lun;
4678 lun = (struct ctl_lun *)be_lun->ctl_lun;
4679 softc = lun->ctl_softc;
4681 mtx_lock(&softc->ctl_lock);
4682 mtx_lock(&lun->lun_lock);
4683 if (lun->flags & CTL_LUN_DISABLED) {
4684 mtx_unlock(&lun->lun_lock);
4685 mtx_unlock(&softc->ctl_lock);
4688 lun->flags |= CTL_LUN_DISABLED;
4689 mtx_unlock(&lun->lun_lock);
4691 STAILQ_FOREACH(port, &softc->port_list, links) {
4692 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4693 port->lun_map != NULL || port->lun_disable == NULL)
4697 * Drop the lock before we call the frontend's disable
4698 * routine, to avoid lock order reversals.
4700 * XXX KDM what happens if the frontend list changes while
4701 * we're traversing it? It's unlikely, but should be handled.
4703 mtx_unlock(&softc->ctl_lock);
4704 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4705 mtx_lock(&softc->ctl_lock);
4707 printf("%s: FETD %s port %d returned error "
4708 "%d for lun_disable on lun %jd\n",
4709 __func__, port->port_name, port->targ_port,
4710 retval, (intmax_t)lun->lun);
4714 mtx_unlock(&softc->ctl_lock);
4715 ctl_isc_announce_lun(lun);
4721 ctl_start_lun(struct ctl_be_lun *be_lun)
4723 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4725 mtx_lock(&lun->lun_lock);
4726 lun->flags &= ~CTL_LUN_STOPPED;
4727 mtx_unlock(&lun->lun_lock);
4732 ctl_stop_lun(struct ctl_be_lun *be_lun)
4734 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4736 mtx_lock(&lun->lun_lock);
4737 lun->flags |= CTL_LUN_STOPPED;
4738 mtx_unlock(&lun->lun_lock);
4743 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4745 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4747 mtx_lock(&lun->lun_lock);
4748 lun->flags |= CTL_LUN_NO_MEDIA;
4749 mtx_unlock(&lun->lun_lock);
4754 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4756 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4757 union ctl_ha_msg msg;
4759 mtx_lock(&lun->lun_lock);
4760 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4761 if (lun->flags & CTL_LUN_REMOVABLE)
4762 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4763 mtx_unlock(&lun->lun_lock);
4764 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4765 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4766 bzero(&msg.ua, sizeof(msg.ua));
4767 msg.hdr.msg_type = CTL_MSG_UA;
4768 msg.hdr.nexus.initid = -1;
4769 msg.hdr.nexus.targ_port = -1;
4770 msg.hdr.nexus.targ_lun = lun->lun;
4771 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4774 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4775 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4782 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4784 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4786 mtx_lock(&lun->lun_lock);
4787 lun->flags |= CTL_LUN_EJECTED;
4788 mtx_unlock(&lun->lun_lock);
4793 ctl_lun_primary(struct ctl_be_lun *be_lun)
4795 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4797 mtx_lock(&lun->lun_lock);
4798 lun->flags |= CTL_LUN_PRIMARY_SC;
4799 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4800 mtx_unlock(&lun->lun_lock);
4801 ctl_isc_announce_lun(lun);
4806 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4808 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4810 mtx_lock(&lun->lun_lock);
4811 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4812 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4813 mtx_unlock(&lun->lun_lock);
4814 ctl_isc_announce_lun(lun);
4819 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4821 struct ctl_softc *softc;
4822 struct ctl_lun *lun;
4824 lun = (struct ctl_lun *)be_lun->ctl_lun;
4825 softc = lun->ctl_softc;
4827 mtx_lock(&lun->lun_lock);
4830 * The LUN needs to be disabled before it can be marked invalid.
4832 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4833 mtx_unlock(&lun->lun_lock);
4837 * Mark the LUN invalid.
4839 lun->flags |= CTL_LUN_INVALID;
4842 * If there is nothing in the OOA queue, go ahead and free the LUN.
4843 * If we have something in the OOA queue, we'll free it when the
4844 * last I/O completes.
4846 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4847 mtx_unlock(&lun->lun_lock);
4848 mtx_lock(&softc->ctl_lock);
4850 mtx_unlock(&softc->ctl_lock);
4852 mtx_unlock(&lun->lun_lock);
4858 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4860 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4861 union ctl_ha_msg msg;
4863 mtx_lock(&lun->lun_lock);
4864 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
4865 mtx_unlock(&lun->lun_lock);
4866 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4867 /* Send msg to other side. */
4868 bzero(&msg.ua, sizeof(msg.ua));
4869 msg.hdr.msg_type = CTL_MSG_UA;
4870 msg.hdr.nexus.initid = -1;
4871 msg.hdr.nexus.targ_port = -1;
4872 msg.hdr.nexus.targ_lun = lun->lun;
4873 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4876 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
4877 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4883 * Backend "memory move is complete" callback for requests that never
4884 * make it down to say RAIDCore's configuration code.
4887 ctl_config_move_done(union ctl_io *io)
4891 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4892 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4893 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4895 if ((io->io_hdr.port_status != 0) &&
4896 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4897 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4899 * For hardware error sense keys, the sense key
4900 * specific value is defined to be a retry count,
4901 * but we use it to pass back an internal FETD
4902 * error code. XXX KDM Hopefully the FETD is only
4903 * using 16 bits for an error code, since that's
4904 * all the space we have in the sks field.
4906 ctl_set_internal_failure(&io->scsiio,
4909 io->io_hdr.port_status);
4912 if (ctl_debug & CTL_DEBUG_CDB_DATA)
4914 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4915 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4916 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4917 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4919 * XXX KDM just assuming a single pointer here, and not a
4920 * S/G list. If we start using S/G lists for config data,
4921 * we'll need to know how to clean them up here as well.
4923 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4924 free(io->scsiio.kern_data_ptr, M_CTL);
4926 retval = CTL_RETVAL_COMPLETE;
4929 * XXX KDM now we need to continue data movement. Some
4931 * - call ctl_scsiio() again? We don't do this for data
4932 * writes, because for those at least we know ahead of
4933 * time where the write will go and how long it is. For
4934 * config writes, though, that information is largely
4935 * contained within the write itself, thus we need to
4936 * parse out the data again.
4938 * - Call some other function once the data is in?
4942 * XXX KDM call ctl_scsiio() again for now, and check flag
4943 * bits to see whether we're allocated or not.
4945 retval = ctl_scsiio(&io->scsiio);
4951 * This gets called by a backend driver when it is done with a
4952 * data_submit method.
4955 ctl_data_submit_done(union ctl_io *io)
4958 * If the IO_CONT flag is set, we need to call the supplied
4959 * function to continue processing the I/O, instead of completing
4962 * If there is an error, though, we don't want to keep processing.
4963 * Instead, just send status back to the initiator.
4965 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4966 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4967 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4968 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4969 io->scsiio.io_cont(io);
4976 * This gets called by a backend driver when it is done with a
4977 * configuration write.
4980 ctl_config_write_done(union ctl_io *io)
4985 * If the IO_CONT flag is set, we need to call the supplied
4986 * function to continue processing the I/O, instead of completing
4989 * If there is an error, though, we don't want to keep processing.
4990 * Instead, just send status back to the initiator.
4992 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
4993 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
4994 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4995 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4996 io->scsiio.io_cont(io);
5000 * Since a configuration write can be done for commands that actually
5001 * have data allocated, like write buffer, and commands that have
5002 * no data, like start/stop unit, we need to check here.
5004 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5005 buf = io->scsiio.kern_data_ptr;
5014 ctl_config_read_done(union ctl_io *io)
5019 * If there is some error -- we are done, skip data transfer.
5021 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5022 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5023 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5024 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5025 buf = io->scsiio.kern_data_ptr;
5035 * If the IO_CONT flag is set, we need to call the supplied
5036 * function to continue processing the I/O, instead of completing
5039 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5040 io->scsiio.io_cont(io);
5048 * SCSI release command.
5051 ctl_scsi_release(struct ctl_scsiio *ctsio)
5053 struct ctl_lun *lun;
5056 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5058 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5059 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5062 * XXX KDM right now, we only support LUN reservation. We don't
5063 * support 3rd party reservations, or extent reservations, which
5064 * might actually need the parameter list. If we've gotten this
5065 * far, we've got a LUN reservation. Anything else got kicked out
5066 * above. So, according to SPC, ignore the length.
5069 mtx_lock(&lun->lun_lock);
5072 * According to SPC, it is not an error for an intiator to attempt
5073 * to release a reservation on a LUN that isn't reserved, or that
5074 * is reserved by another initiator. The reservation can only be
5075 * released, though, by the initiator who made it or by one of
5076 * several reset type events.
5078 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5079 lun->flags &= ~CTL_LUN_RESERVED;
5081 mtx_unlock(&lun->lun_lock);
5083 ctl_set_success(ctsio);
5084 ctl_done((union ctl_io *)ctsio);
5085 return (CTL_RETVAL_COMPLETE);
5089 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5091 struct ctl_lun *lun;
5094 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5096 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5097 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5100 * XXX KDM right now, we only support LUN reservation. We don't
5101 * support 3rd party reservations, or extent reservations, which
5102 * might actually need the parameter list. If we've gotten this
5103 * far, we've got a LUN reservation. Anything else got kicked out
5104 * above. So, according to SPC, ignore the length.
5107 mtx_lock(&lun->lun_lock);
5108 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5109 ctl_set_reservation_conflict(ctsio);
5112 lun->flags |= CTL_LUN_RESERVED;
5113 lun->res_idx = residx;
5114 ctl_set_success(ctsio);
5117 mtx_unlock(&lun->lun_lock);
5118 ctl_done((union ctl_io *)ctsio);
5119 return (CTL_RETVAL_COMPLETE);
5123 ctl_start_stop(struct ctl_scsiio *ctsio)
5125 struct scsi_start_stop_unit *cdb;
5126 struct ctl_lun *lun;
5129 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5131 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5132 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5134 if ((cdb->how & SSS_PC_MASK) == 0) {
5135 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5136 (cdb->how & SSS_START) == 0) {
5139 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5140 if (ctl_get_prkey(lun, residx) == 0 ||
5141 (lun->pr_res_idx != residx && lun->res_type < 4)) {
5143 ctl_set_reservation_conflict(ctsio);
5144 ctl_done((union ctl_io *)ctsio);
5145 return (CTL_RETVAL_COMPLETE);
5149 if ((cdb->how & SSS_LOEJ) &&
5150 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5151 ctl_set_invalid_field(ctsio,
5157 ctl_done((union ctl_io *)ctsio);
5158 return (CTL_RETVAL_COMPLETE);
5161 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5162 lun->prevent_count > 0) {
5163 /* "Medium removal prevented" */
5164 ctl_set_sense(ctsio, /*current_error*/ 1,
5165 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5166 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5167 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5168 ctl_done((union ctl_io *)ctsio);
5169 return (CTL_RETVAL_COMPLETE);
5173 retval = lun->backend->config_write((union ctl_io *)ctsio);
5178 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5180 struct ctl_lun *lun;
5181 struct scsi_prevent *cdb;
5185 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5187 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5188 cdb = (struct scsi_prevent *)ctsio->cdb;
5190 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) {
5191 ctl_set_invalid_opcode(ctsio);
5192 ctl_done((union ctl_io *)ctsio);
5193 return (CTL_RETVAL_COMPLETE);
5196 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5197 mtx_lock(&lun->lun_lock);
5198 if ((cdb->how & PR_PREVENT) &&
5199 ctl_is_set(lun->prevent, initidx) == 0) {
5200 ctl_set_mask(lun->prevent, initidx);
5201 lun->prevent_count++;
5202 } else if ((cdb->how & PR_PREVENT) == 0 &&
5203 ctl_is_set(lun->prevent, initidx)) {
5204 ctl_clear_mask(lun->prevent, initidx);
5205 lun->prevent_count--;
5207 mtx_unlock(&lun->lun_lock);
5208 retval = lun->backend->config_write((union ctl_io *)ctsio);
5213 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5214 * we don't really do anything with the LBA and length fields if the user
5215 * passes them in. Instead we'll just flush out the cache for the entire
5219 ctl_sync_cache(struct ctl_scsiio *ctsio)
5221 struct ctl_lun *lun;
5222 struct ctl_softc *softc;
5223 struct ctl_lba_len_flags *lbalen;
5224 uint64_t starting_lba;
5225 uint32_t block_count;
5229 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5231 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5232 softc = lun->ctl_softc;
5235 switch (ctsio->cdb[0]) {
5236 case SYNCHRONIZE_CACHE: {
5237 struct scsi_sync_cache *cdb;
5238 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5240 starting_lba = scsi_4btoul(cdb->begin_lba);
5241 block_count = scsi_2btoul(cdb->lb_count);
5245 case SYNCHRONIZE_CACHE_16: {
5246 struct scsi_sync_cache_16 *cdb;
5247 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5249 starting_lba = scsi_8btou64(cdb->begin_lba);
5250 block_count = scsi_4btoul(cdb->lb_count);
5255 ctl_set_invalid_opcode(ctsio);
5256 ctl_done((union ctl_io *)ctsio);
5258 break; /* NOTREACHED */
5262 * We check the LBA and length, but don't do anything with them.
5263 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5264 * get flushed. This check will just help satisfy anyone who wants
5265 * to see an error for an out of range LBA.
5267 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5268 ctl_set_lba_out_of_range(ctsio);
5269 ctl_done((union ctl_io *)ctsio);
5273 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5274 lbalen->lba = starting_lba;
5275 lbalen->len = block_count;
5276 lbalen->flags = byte2;
5277 retval = lun->backend->config_write((union ctl_io *)ctsio);
5284 ctl_format(struct ctl_scsiio *ctsio)
5286 struct scsi_format *cdb;
5287 struct ctl_lun *lun;
5288 int length, defect_list_len;
5290 CTL_DEBUG_PRINT(("ctl_format\n"));
5292 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5294 cdb = (struct scsi_format *)ctsio->cdb;
5297 if (cdb->byte2 & SF_FMTDATA) {
5298 if (cdb->byte2 & SF_LONGLIST)
5299 length = sizeof(struct scsi_format_header_long);
5301 length = sizeof(struct scsi_format_header_short);
5304 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5306 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5307 ctsio->kern_data_len = length;
5308 ctsio->kern_total_len = length;
5309 ctsio->kern_data_resid = 0;
5310 ctsio->kern_rel_offset = 0;
5311 ctsio->kern_sg_entries = 0;
5312 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5313 ctsio->be_move_done = ctl_config_move_done;
5314 ctl_datamove((union ctl_io *)ctsio);
5316 return (CTL_RETVAL_COMPLETE);
5319 defect_list_len = 0;
5321 if (cdb->byte2 & SF_FMTDATA) {
5322 if (cdb->byte2 & SF_LONGLIST) {
5323 struct scsi_format_header_long *header;
5325 header = (struct scsi_format_header_long *)
5326 ctsio->kern_data_ptr;
5328 defect_list_len = scsi_4btoul(header->defect_list_len);
5329 if (defect_list_len != 0) {
5330 ctl_set_invalid_field(ctsio,
5339 struct scsi_format_header_short *header;
5341 header = (struct scsi_format_header_short *)
5342 ctsio->kern_data_ptr;
5344 defect_list_len = scsi_2btoul(header->defect_list_len);
5345 if (defect_list_len != 0) {
5346 ctl_set_invalid_field(ctsio,
5357 ctl_set_success(ctsio);
5360 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5361 free(ctsio->kern_data_ptr, M_CTL);
5362 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5365 ctl_done((union ctl_io *)ctsio);
5366 return (CTL_RETVAL_COMPLETE);
5370 ctl_read_buffer(struct ctl_scsiio *ctsio)
5372 struct ctl_lun *lun;
5373 uint64_t buffer_offset;
5376 static uint8_t descr[4];
5377 static uint8_t echo_descr[4] = { 0 };
5379 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5380 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5381 switch (ctsio->cdb[0]) {
5383 struct scsi_read_buffer *cdb;
5385 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5386 buffer_offset = scsi_3btoul(cdb->offset);
5387 len = scsi_3btoul(cdb->length);
5391 case READ_BUFFER_16: {
5392 struct scsi_read_buffer_16 *cdb;
5394 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5395 buffer_offset = scsi_8btou64(cdb->offset);
5396 len = scsi_4btoul(cdb->length);
5400 default: /* This shouldn't happen. */
5401 ctl_set_invalid_opcode(ctsio);
5402 ctl_done((union ctl_io *)ctsio);
5403 return (CTL_RETVAL_COMPLETE);
5406 if ((byte2 & RWB_MODE) != RWB_MODE_DATA &&
5407 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5408 (byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5409 ctl_set_invalid_field(ctsio,
5415 ctl_done((union ctl_io *)ctsio);
5416 return (CTL_RETVAL_COMPLETE);
5419 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5420 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5421 ctl_set_invalid_field(ctsio,
5427 ctl_done((union ctl_io *)ctsio);
5428 return (CTL_RETVAL_COMPLETE);
5431 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5433 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5434 ctsio->kern_data_ptr = descr;
5435 len = min(len, sizeof(descr));
5436 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5437 ctsio->kern_data_ptr = echo_descr;
5438 len = min(len, sizeof(echo_descr));
5440 if (lun->write_buffer == NULL) {
5441 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5444 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5446 ctsio->kern_data_len = len;
5447 ctsio->kern_total_len = len;
5448 ctsio->kern_data_resid = 0;
5449 ctsio->kern_rel_offset = 0;
5450 ctsio->kern_sg_entries = 0;
5451 ctl_set_success(ctsio);
5452 ctsio->be_move_done = ctl_config_move_done;
5453 ctl_datamove((union ctl_io *)ctsio);
5454 return (CTL_RETVAL_COMPLETE);
5458 ctl_write_buffer(struct ctl_scsiio *ctsio)
5460 struct scsi_write_buffer *cdb;
5461 struct ctl_lun *lun;
5462 int buffer_offset, len;
5464 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5466 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5467 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5469 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5470 ctl_set_invalid_field(ctsio,
5476 ctl_done((union ctl_io *)ctsio);
5477 return (CTL_RETVAL_COMPLETE);
5480 len = scsi_3btoul(cdb->length);
5481 buffer_offset = scsi_3btoul(cdb->offset);
5483 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5484 ctl_set_invalid_field(ctsio,
5490 ctl_done((union ctl_io *)ctsio);
5491 return (CTL_RETVAL_COMPLETE);
5495 * If we've got a kernel request that hasn't been malloced yet,
5496 * malloc it and tell the caller the data buffer is here.
5498 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5499 if (lun->write_buffer == NULL) {
5500 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5503 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5504 ctsio->kern_data_len = len;
5505 ctsio->kern_total_len = len;
5506 ctsio->kern_data_resid = 0;
5507 ctsio->kern_rel_offset = 0;
5508 ctsio->kern_sg_entries = 0;
5509 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5510 ctsio->be_move_done = ctl_config_move_done;
5511 ctl_datamove((union ctl_io *)ctsio);
5513 return (CTL_RETVAL_COMPLETE);
5516 ctl_set_success(ctsio);
5517 ctl_done((union ctl_io *)ctsio);
5518 return (CTL_RETVAL_COMPLETE);
5522 ctl_write_same(struct ctl_scsiio *ctsio)
5524 struct ctl_lun *lun;
5525 struct ctl_lba_len_flags *lbalen;
5527 uint32_t num_blocks;
5531 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5533 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5535 switch (ctsio->cdb[0]) {
5536 case WRITE_SAME_10: {
5537 struct scsi_write_same_10 *cdb;
5539 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5541 lba = scsi_4btoul(cdb->addr);
5542 num_blocks = scsi_2btoul(cdb->length);
5546 case WRITE_SAME_16: {
5547 struct scsi_write_same_16 *cdb;
5549 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5551 lba = scsi_8btou64(cdb->addr);
5552 num_blocks = scsi_4btoul(cdb->length);
5558 * We got a command we don't support. This shouldn't
5559 * happen, commands should be filtered out above us.
5561 ctl_set_invalid_opcode(ctsio);
5562 ctl_done((union ctl_io *)ctsio);
5564 return (CTL_RETVAL_COMPLETE);
5565 break; /* NOTREACHED */
5568 /* ANCHOR flag can be used only together with UNMAP */
5569 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5570 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5571 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5572 ctl_done((union ctl_io *)ctsio);
5573 return (CTL_RETVAL_COMPLETE);
5577 * The first check is to make sure we're in bounds, the second
5578 * check is to catch wrap-around problems. If the lba + num blocks
5579 * is less than the lba, then we've wrapped around and the block
5580 * range is invalid anyway.
5582 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5583 || ((lba + num_blocks) < lba)) {
5584 ctl_set_lba_out_of_range(ctsio);
5585 ctl_done((union ctl_io *)ctsio);
5586 return (CTL_RETVAL_COMPLETE);
5589 /* Zero number of blocks means "to the last logical block" */
5590 if (num_blocks == 0) {
5591 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5592 ctl_set_invalid_field(ctsio,
5598 ctl_done((union ctl_io *)ctsio);
5599 return (CTL_RETVAL_COMPLETE);
5601 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5604 len = lun->be_lun->blocksize;
5607 * If we've got a kernel request that hasn't been malloced yet,
5608 * malloc it and tell the caller the data buffer is here.
5610 if ((byte2 & SWS_NDOB) == 0 &&
5611 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5612 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5613 ctsio->kern_data_len = len;
5614 ctsio->kern_total_len = len;
5615 ctsio->kern_data_resid = 0;
5616 ctsio->kern_rel_offset = 0;
5617 ctsio->kern_sg_entries = 0;
5618 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5619 ctsio->be_move_done = ctl_config_move_done;
5620 ctl_datamove((union ctl_io *)ctsio);
5622 return (CTL_RETVAL_COMPLETE);
5625 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5627 lbalen->len = num_blocks;
5628 lbalen->flags = byte2;
5629 retval = lun->backend->config_write((union ctl_io *)ctsio);
5635 ctl_unmap(struct ctl_scsiio *ctsio)
5637 struct ctl_lun *lun;
5638 struct scsi_unmap *cdb;
5639 struct ctl_ptr_len_flags *ptrlen;
5640 struct scsi_unmap_header *hdr;
5641 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5643 uint32_t num_blocks;
5647 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5649 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5650 cdb = (struct scsi_unmap *)ctsio->cdb;
5652 len = scsi_2btoul(cdb->length);
5656 * If we've got a kernel request that hasn't been malloced yet,
5657 * malloc it and tell the caller the data buffer is here.
5659 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5660 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5661 ctsio->kern_data_len = len;
5662 ctsio->kern_total_len = len;
5663 ctsio->kern_data_resid = 0;
5664 ctsio->kern_rel_offset = 0;
5665 ctsio->kern_sg_entries = 0;
5666 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5667 ctsio->be_move_done = ctl_config_move_done;
5668 ctl_datamove((union ctl_io *)ctsio);
5670 return (CTL_RETVAL_COMPLETE);
5673 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5674 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5675 if (len < sizeof (*hdr) ||
5676 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5677 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5678 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5679 ctl_set_invalid_field(ctsio,
5687 len = scsi_2btoul(hdr->desc_length);
5688 buf = (struct scsi_unmap_desc *)(hdr + 1);
5689 end = buf + len / sizeof(*buf);
5692 for (range = buf; range < end; range++) {
5693 lba = scsi_8btou64(range->lba);
5694 num_blocks = scsi_4btoul(range->length);
5695 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5696 || ((lba + num_blocks) < lba)) {
5697 ctl_set_lba_out_of_range(ctsio);
5698 ctl_done((union ctl_io *)ctsio);
5699 return (CTL_RETVAL_COMPLETE);
5701 if (num_blocks != 0)
5706 * Block backend can not handle zero last range.
5707 * Filter it out and return if there is nothing left.
5709 len = (uint8_t *)endnz - (uint8_t *)buf;
5711 ctl_set_success(ctsio);
5715 mtx_lock(&lun->lun_lock);
5716 ptrlen = (struct ctl_ptr_len_flags *)
5717 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5718 ptrlen->ptr = (void *)buf;
5720 ptrlen->flags = byte2;
5721 ctl_check_blocked(lun);
5722 mtx_unlock(&lun->lun_lock);
5724 retval = lun->backend->config_write((union ctl_io *)ctsio);
5728 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5729 free(ctsio->kern_data_ptr, M_CTL);
5730 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5732 ctl_done((union ctl_io *)ctsio);
5733 return (CTL_RETVAL_COMPLETE);
5737 * Note that this function currently doesn't actually do anything inside
5738 * CTL to enforce things if the DQue bit is turned on.
5740 * Also note that this function can't be used in the default case, because
5741 * the DQue bit isn't set in the changeable mask for the control mode page
5742 * anyway. This is just here as an example for how to implement a page
5743 * handler, and a placeholder in case we want to allow the user to turn
5744 * tagged queueing on and off.
5746 * The D_SENSE bit handling is functional, however, and will turn
5747 * descriptor sense on and off for a given LUN.
5750 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5751 struct ctl_page_index *page_index, uint8_t *page_ptr)
5753 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5754 struct ctl_lun *lun;
5758 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5759 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5762 user_cp = (struct scsi_control_page *)page_ptr;
5763 current_cp = (struct scsi_control_page *)
5764 (page_index->page_data + (page_index->page_len *
5766 saved_cp = (struct scsi_control_page *)
5767 (page_index->page_data + (page_index->page_len *
5770 mtx_lock(&lun->lun_lock);
5771 if (((current_cp->rlec & SCP_DSENSE) == 0)
5772 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5774 * Descriptor sense is currently turned off and the user
5775 * wants to turn it on.
5777 current_cp->rlec |= SCP_DSENSE;
5778 saved_cp->rlec |= SCP_DSENSE;
5779 lun->flags |= CTL_LUN_SENSE_DESC;
5781 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5782 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5784 * Descriptor sense is currently turned on, and the user
5785 * wants to turn it off.
5787 current_cp->rlec &= ~SCP_DSENSE;
5788 saved_cp->rlec &= ~SCP_DSENSE;
5789 lun->flags &= ~CTL_LUN_SENSE_DESC;
5792 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
5793 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
5794 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5795 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5796 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5797 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5800 if ((current_cp->eca_and_aen & SCP_SWP) !=
5801 (user_cp->eca_and_aen & SCP_SWP)) {
5802 current_cp->eca_and_aen &= ~SCP_SWP;
5803 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5804 saved_cp->eca_and_aen &= ~SCP_SWP;
5805 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5809 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5810 mtx_unlock(&lun->lun_lock);
5812 ctl_isc_announce_mode(lun,
5813 ctl_get_initindex(&ctsio->io_hdr.nexus),
5814 page_index->page_code, page_index->subpage);
5820 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
5821 struct ctl_page_index *page_index, uint8_t *page_ptr)
5823 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
5824 struct ctl_lun *lun;
5828 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5829 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5832 user_cp = (struct scsi_caching_page *)page_ptr;
5833 current_cp = (struct scsi_caching_page *)
5834 (page_index->page_data + (page_index->page_len *
5836 saved_cp = (struct scsi_caching_page *)
5837 (page_index->page_data + (page_index->page_len *
5840 mtx_lock(&lun->lun_lock);
5841 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
5842 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
5843 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5844 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5845 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5846 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5850 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5851 mtx_unlock(&lun->lun_lock);
5853 ctl_isc_announce_mode(lun,
5854 ctl_get_initindex(&ctsio->io_hdr.nexus),
5855 page_index->page_code, page_index->subpage);
5861 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5862 struct ctl_page_index *page_index,
5868 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
5873 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
5874 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
5875 printf("page data:");
5877 printf(" %.2x",page_ptr[i]);
5883 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
5884 struct ctl_page_index *page_index,
5887 struct copan_debugconf_subpage *page;
5889 page = (struct copan_debugconf_subpage *)page_index->page_data +
5890 (page_index->page_len * pc);
5893 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5894 case SMS_PAGE_CTRL_DEFAULT >> 6:
5895 case SMS_PAGE_CTRL_SAVED >> 6:
5897 * We don't update the changable or default bits for this page.
5900 case SMS_PAGE_CTRL_CURRENT >> 6:
5901 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
5902 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
5912 ctl_do_mode_select(union ctl_io *io)
5914 struct scsi_mode_page_header *page_header;
5915 struct ctl_page_index *page_index;
5916 struct ctl_scsiio *ctsio;
5917 int page_len, page_len_offset, page_len_size;
5918 union ctl_modepage_info *modepage_info;
5919 struct ctl_lun *lun;
5920 int *len_left, *len_used;
5923 ctsio = &io->scsiio;
5926 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5928 modepage_info = (union ctl_modepage_info *)
5929 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5930 len_left = &modepage_info->header.len_left;
5931 len_used = &modepage_info->header.len_used;
5935 page_header = (struct scsi_mode_page_header *)
5936 (ctsio->kern_data_ptr + *len_used);
5938 if (*len_left == 0) {
5939 free(ctsio->kern_data_ptr, M_CTL);
5940 ctl_set_success(ctsio);
5941 ctl_done((union ctl_io *)ctsio);
5942 return (CTL_RETVAL_COMPLETE);
5943 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5945 free(ctsio->kern_data_ptr, M_CTL);
5946 ctl_set_param_len_error(ctsio);
5947 ctl_done((union ctl_io *)ctsio);
5948 return (CTL_RETVAL_COMPLETE);
5950 } else if ((page_header->page_code & SMPH_SPF)
5951 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
5953 free(ctsio->kern_data_ptr, M_CTL);
5954 ctl_set_param_len_error(ctsio);
5955 ctl_done((union ctl_io *)ctsio);
5956 return (CTL_RETVAL_COMPLETE);
5961 * XXX KDM should we do something with the block descriptor?
5963 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
5964 page_index = &lun->mode_pages.index[i];
5965 if (lun->be_lun->lun_type == T_DIRECT &&
5966 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
5968 if (lun->be_lun->lun_type == T_PROCESSOR &&
5969 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
5971 if (lun->be_lun->lun_type == T_CDROM &&
5972 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
5975 if ((page_index->page_code & SMPH_PC_MASK) !=
5976 (page_header->page_code & SMPH_PC_MASK))
5980 * If neither page has a subpage code, then we've got a
5983 if (((page_index->page_code & SMPH_SPF) == 0)
5984 && ((page_header->page_code & SMPH_SPF) == 0)) {
5985 page_len = page_header->page_length;
5990 * If both pages have subpages, then the subpage numbers
5993 if ((page_index->page_code & SMPH_SPF)
5994 && (page_header->page_code & SMPH_SPF)) {
5995 struct scsi_mode_page_header_sp *sph;
5997 sph = (struct scsi_mode_page_header_sp *)page_header;
5998 if (page_index->subpage == sph->subpage) {
5999 page_len = scsi_2btoul(sph->page_length);
6006 * If we couldn't find the page, or if we don't have a mode select
6007 * handler for it, send back an error to the user.
6009 if ((i >= CTL_NUM_MODE_PAGES)
6010 || (page_index->select_handler == NULL)) {
6011 ctl_set_invalid_field(ctsio,
6014 /*field*/ *len_used,
6017 free(ctsio->kern_data_ptr, M_CTL);
6018 ctl_done((union ctl_io *)ctsio);
6019 return (CTL_RETVAL_COMPLETE);
6022 if (page_index->page_code & SMPH_SPF) {
6023 page_len_offset = 2;
6027 page_len_offset = 1;
6031 * If the length the initiator gives us isn't the one we specify in
6032 * the mode page header, or if they didn't specify enough data in
6033 * the CDB to avoid truncating this page, kick out the request.
6035 if ((page_len != (page_index->page_len - page_len_offset -
6037 || (*len_left < page_index->page_len)) {
6040 ctl_set_invalid_field(ctsio,
6043 /*field*/ *len_used + page_len_offset,
6046 free(ctsio->kern_data_ptr, M_CTL);
6047 ctl_done((union ctl_io *)ctsio);
6048 return (CTL_RETVAL_COMPLETE);
6052 * Run through the mode page, checking to make sure that the bits
6053 * the user changed are actually legal for him to change.
6055 for (i = 0; i < page_index->page_len; i++) {
6056 uint8_t *user_byte, *change_mask, *current_byte;
6060 user_byte = (uint8_t *)page_header + i;
6061 change_mask = page_index->page_data +
6062 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6063 current_byte = page_index->page_data +
6064 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6067 * Check to see whether the user set any bits in this byte
6068 * that he is not allowed to set.
6070 if ((*user_byte & ~(*change_mask)) ==
6071 (*current_byte & ~(*change_mask)))
6075 * Go through bit by bit to determine which one is illegal.
6078 for (j = 7; j >= 0; j--) {
6079 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6080 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6085 ctl_set_invalid_field(ctsio,
6088 /*field*/ *len_used + i,
6091 free(ctsio->kern_data_ptr, M_CTL);
6092 ctl_done((union ctl_io *)ctsio);
6093 return (CTL_RETVAL_COMPLETE);
6097 * Decrement these before we call the page handler, since we may
6098 * end up getting called back one way or another before the handler
6099 * returns to this context.
6101 *len_left -= page_index->page_len;
6102 *len_used += page_index->page_len;
6104 retval = page_index->select_handler(ctsio, page_index,
6105 (uint8_t *)page_header);
6108 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6109 * wait until this queued command completes to finish processing
6110 * the mode page. If it returns anything other than
6111 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6112 * already set the sense information, freed the data pointer, and
6113 * completed the io for us.
6115 if (retval != CTL_RETVAL_COMPLETE)
6116 goto bailout_no_done;
6119 * If the initiator sent us more than one page, parse the next one.
6124 ctl_set_success(ctsio);
6125 free(ctsio->kern_data_ptr, M_CTL);
6126 ctl_done((union ctl_io *)ctsio);
6130 return (CTL_RETVAL_COMPLETE);
6135 ctl_mode_select(struct ctl_scsiio *ctsio)
6137 int param_len, pf, sp;
6138 int header_size, bd_len;
6139 union ctl_modepage_info *modepage_info;
6141 switch (ctsio->cdb[0]) {
6142 case MODE_SELECT_6: {
6143 struct scsi_mode_select_6 *cdb;
6145 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6147 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6148 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6149 param_len = cdb->length;
6150 header_size = sizeof(struct scsi_mode_header_6);
6153 case MODE_SELECT_10: {
6154 struct scsi_mode_select_10 *cdb;
6156 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6158 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6159 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6160 param_len = scsi_2btoul(cdb->length);
6161 header_size = sizeof(struct scsi_mode_header_10);
6165 ctl_set_invalid_opcode(ctsio);
6166 ctl_done((union ctl_io *)ctsio);
6167 return (CTL_RETVAL_COMPLETE);
6172 * "A parameter list length of zero indicates that the Data-Out Buffer
6173 * shall be empty. This condition shall not be considered as an error."
6175 if (param_len == 0) {
6176 ctl_set_success(ctsio);
6177 ctl_done((union ctl_io *)ctsio);
6178 return (CTL_RETVAL_COMPLETE);
6182 * Since we'll hit this the first time through, prior to
6183 * allocation, we don't need to free a data buffer here.
6185 if (param_len < header_size) {
6186 ctl_set_param_len_error(ctsio);
6187 ctl_done((union ctl_io *)ctsio);
6188 return (CTL_RETVAL_COMPLETE);
6192 * Allocate the data buffer and grab the user's data. In theory,
6193 * we shouldn't have to sanity check the parameter list length here
6194 * because the maximum size is 64K. We should be able to malloc
6195 * that much without too many problems.
6197 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6198 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6199 ctsio->kern_data_len = param_len;
6200 ctsio->kern_total_len = param_len;
6201 ctsio->kern_data_resid = 0;
6202 ctsio->kern_rel_offset = 0;
6203 ctsio->kern_sg_entries = 0;
6204 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6205 ctsio->be_move_done = ctl_config_move_done;
6206 ctl_datamove((union ctl_io *)ctsio);
6208 return (CTL_RETVAL_COMPLETE);
6211 switch (ctsio->cdb[0]) {
6212 case MODE_SELECT_6: {
6213 struct scsi_mode_header_6 *mh6;
6215 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6216 bd_len = mh6->blk_desc_len;
6219 case MODE_SELECT_10: {
6220 struct scsi_mode_header_10 *mh10;
6222 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6223 bd_len = scsi_2btoul(mh10->blk_desc_len);
6227 panic("Invalid CDB type %#x", ctsio->cdb[0]);
6231 if (param_len < (header_size + bd_len)) {
6232 free(ctsio->kern_data_ptr, M_CTL);
6233 ctl_set_param_len_error(ctsio);
6234 ctl_done((union ctl_io *)ctsio);
6235 return (CTL_RETVAL_COMPLETE);
6239 * Set the IO_CONT flag, so that if this I/O gets passed to
6240 * ctl_config_write_done(), it'll get passed back to
6241 * ctl_do_mode_select() for further processing, or completion if
6244 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6245 ctsio->io_cont = ctl_do_mode_select;
6247 modepage_info = (union ctl_modepage_info *)
6248 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6249 memset(modepage_info, 0, sizeof(*modepage_info));
6250 modepage_info->header.len_left = param_len - header_size - bd_len;
6251 modepage_info->header.len_used = header_size + bd_len;
6253 return (ctl_do_mode_select((union ctl_io *)ctsio));
6257 ctl_mode_sense(struct ctl_scsiio *ctsio)
6259 struct ctl_lun *lun;
6260 int pc, page_code, dbd, llba, subpage;
6261 int alloc_len, page_len, header_len, total_len;
6262 struct scsi_mode_block_descr *block_desc;
6263 struct ctl_page_index *page_index;
6269 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6271 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6272 switch (ctsio->cdb[0]) {
6273 case MODE_SENSE_6: {
6274 struct scsi_mode_sense_6 *cdb;
6276 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6278 header_len = sizeof(struct scsi_mode_hdr_6);
6279 if (cdb->byte2 & SMS_DBD)
6282 header_len += sizeof(struct scsi_mode_block_descr);
6284 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6285 page_code = cdb->page & SMS_PAGE_CODE;
6286 subpage = cdb->subpage;
6287 alloc_len = cdb->length;
6290 case MODE_SENSE_10: {
6291 struct scsi_mode_sense_10 *cdb;
6293 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6295 header_len = sizeof(struct scsi_mode_hdr_10);
6297 if (cdb->byte2 & SMS_DBD)
6300 header_len += sizeof(struct scsi_mode_block_descr);
6301 if (cdb->byte2 & SMS10_LLBAA)
6303 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6304 page_code = cdb->page & SMS_PAGE_CODE;
6305 subpage = cdb->subpage;
6306 alloc_len = scsi_2btoul(cdb->length);
6310 ctl_set_invalid_opcode(ctsio);
6311 ctl_done((union ctl_io *)ctsio);
6312 return (CTL_RETVAL_COMPLETE);
6313 break; /* NOTREACHED */
6317 * We have to make a first pass through to calculate the size of
6318 * the pages that match the user's query. Then we allocate enough
6319 * memory to hold it, and actually copy the data into the buffer.
6321 switch (page_code) {
6322 case SMS_ALL_PAGES_PAGE: {
6328 * At the moment, values other than 0 and 0xff here are
6329 * reserved according to SPC-3.
6331 if ((subpage != SMS_SUBPAGE_PAGE_0)
6332 && (subpage != SMS_SUBPAGE_ALL)) {
6333 ctl_set_invalid_field(ctsio,
6339 ctl_done((union ctl_io *)ctsio);
6340 return (CTL_RETVAL_COMPLETE);
6343 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6344 page_index = &lun->mode_pages.index[i];
6346 /* Make sure the page is supported for this dev type */
6347 if (lun->be_lun->lun_type == T_DIRECT &&
6348 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6350 if (lun->be_lun->lun_type == T_PROCESSOR &&
6351 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6353 if (lun->be_lun->lun_type == T_CDROM &&
6354 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6358 * We don't use this subpage if the user didn't
6359 * request all subpages.
6361 if ((page_index->subpage != 0)
6362 && (subpage == SMS_SUBPAGE_PAGE_0))
6366 printf("found page %#x len %d\n",
6367 page_index->page_code & SMPH_PC_MASK,
6368 page_index->page_len);
6370 page_len += page_index->page_len;
6379 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6380 page_index = &lun->mode_pages.index[i];
6382 /* Make sure the page is supported for this dev type */
6383 if (lun->be_lun->lun_type == T_DIRECT &&
6384 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6386 if (lun->be_lun->lun_type == T_PROCESSOR &&
6387 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6389 if (lun->be_lun->lun_type == T_CDROM &&
6390 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6393 /* Look for the right page code */
6394 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6397 /* Look for the right subpage or the subpage wildcard*/
6398 if ((page_index->subpage != subpage)
6399 && (subpage != SMS_SUBPAGE_ALL))
6403 printf("found page %#x len %d\n",
6404 page_index->page_code & SMPH_PC_MASK,
6405 page_index->page_len);
6408 page_len += page_index->page_len;
6411 if (page_len == 0) {
6412 ctl_set_invalid_field(ctsio,
6418 ctl_done((union ctl_io *)ctsio);
6419 return (CTL_RETVAL_COMPLETE);
6425 total_len = header_len + page_len;
6427 printf("header_len = %d, page_len = %d, total_len = %d\n",
6428 header_len, page_len, total_len);
6431 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6432 ctsio->kern_sg_entries = 0;
6433 ctsio->kern_data_resid = 0;
6434 ctsio->kern_rel_offset = 0;
6435 if (total_len < alloc_len) {
6436 ctsio->residual = alloc_len - total_len;
6437 ctsio->kern_data_len = total_len;
6438 ctsio->kern_total_len = total_len;
6440 ctsio->residual = 0;
6441 ctsio->kern_data_len = alloc_len;
6442 ctsio->kern_total_len = alloc_len;
6445 switch (ctsio->cdb[0]) {
6446 case MODE_SENSE_6: {
6447 struct scsi_mode_hdr_6 *header;
6449 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6451 header->datalen = MIN(total_len - 1, 254);
6452 if (lun->be_lun->lun_type == T_DIRECT) {
6453 header->dev_specific = 0x10; /* DPOFUA */
6454 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6455 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6456 .eca_and_aen & SCP_SWP) != 0)
6457 header->dev_specific |= 0x80; /* WP */
6460 header->block_descr_len = 0;
6462 header->block_descr_len =
6463 sizeof(struct scsi_mode_block_descr);
6464 block_desc = (struct scsi_mode_block_descr *)&header[1];
6467 case MODE_SENSE_10: {
6468 struct scsi_mode_hdr_10 *header;
6471 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6473 datalen = MIN(total_len - 2, 65533);
6474 scsi_ulto2b(datalen, header->datalen);
6475 if (lun->be_lun->lun_type == T_DIRECT) {
6476 header->dev_specific = 0x10; /* DPOFUA */
6477 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6478 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6479 .eca_and_aen & SCP_SWP) != 0)
6480 header->dev_specific |= 0x80; /* WP */
6483 scsi_ulto2b(0, header->block_descr_len);
6485 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6486 header->block_descr_len);
6487 block_desc = (struct scsi_mode_block_descr *)&header[1];
6491 panic("invalid CDB type %#x", ctsio->cdb[0]);
6492 break; /* NOTREACHED */
6496 * If we've got a disk, use its blocksize in the block
6497 * descriptor. Otherwise, just set it to 0.
6500 if (lun->be_lun->lun_type == T_DIRECT)
6501 scsi_ulto3b(lun->be_lun->blocksize,
6502 block_desc->block_len);
6504 scsi_ulto3b(0, block_desc->block_len);
6507 switch (page_code) {
6508 case SMS_ALL_PAGES_PAGE: {
6511 data_used = header_len;
6512 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6513 struct ctl_page_index *page_index;
6515 page_index = &lun->mode_pages.index[i];
6516 if (lun->be_lun->lun_type == T_DIRECT &&
6517 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6519 if (lun->be_lun->lun_type == T_PROCESSOR &&
6520 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6522 if (lun->be_lun->lun_type == T_CDROM &&
6523 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6527 * We don't use this subpage if the user didn't
6528 * request all subpages. We already checked (above)
6529 * to make sure the user only specified a subpage
6530 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6532 if ((page_index->subpage != 0)
6533 && (subpage == SMS_SUBPAGE_PAGE_0))
6537 * Call the handler, if it exists, to update the
6538 * page to the latest values.
6540 if (page_index->sense_handler != NULL)
6541 page_index->sense_handler(ctsio, page_index,pc);
6543 memcpy(ctsio->kern_data_ptr + data_used,
6544 page_index->page_data +
6545 (page_index->page_len * pc),
6546 page_index->page_len);
6547 data_used += page_index->page_len;
6554 data_used = header_len;
6556 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6557 struct ctl_page_index *page_index;
6559 page_index = &lun->mode_pages.index[i];
6561 /* Look for the right page code */
6562 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6565 /* Look for the right subpage or the subpage wildcard*/
6566 if ((page_index->subpage != subpage)
6567 && (subpage != SMS_SUBPAGE_ALL))
6570 /* Make sure the page is supported for this dev type */
6571 if (lun->be_lun->lun_type == T_DIRECT &&
6572 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6574 if (lun->be_lun->lun_type == T_PROCESSOR &&
6575 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6577 if (lun->be_lun->lun_type == T_CDROM &&
6578 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6582 * Call the handler, if it exists, to update the
6583 * page to the latest values.
6585 if (page_index->sense_handler != NULL)
6586 page_index->sense_handler(ctsio, page_index,pc);
6588 memcpy(ctsio->kern_data_ptr + data_used,
6589 page_index->page_data +
6590 (page_index->page_len * pc),
6591 page_index->page_len);
6592 data_used += page_index->page_len;
6598 ctl_set_success(ctsio);
6599 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6600 ctsio->be_move_done = ctl_config_move_done;
6601 ctl_datamove((union ctl_io *)ctsio);
6602 return (CTL_RETVAL_COMPLETE);
6606 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6607 struct ctl_page_index *page_index,
6610 struct ctl_lun *lun;
6611 struct scsi_log_param_header *phdr;
6615 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6616 data = page_index->page_data;
6618 if (lun->backend->lun_attr != NULL &&
6619 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6621 phdr = (struct scsi_log_param_header *)data;
6622 scsi_ulto2b(0x0001, phdr->param_code);
6623 phdr->param_control = SLP_LBIN | SLP_LP;
6624 phdr->param_len = 8;
6625 data = (uint8_t *)(phdr + 1);
6626 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6627 data[4] = 0x02; /* per-pool */
6628 data += phdr->param_len;
6631 if (lun->backend->lun_attr != NULL &&
6632 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6634 phdr = (struct scsi_log_param_header *)data;
6635 scsi_ulto2b(0x0002, phdr->param_code);
6636 phdr->param_control = SLP_LBIN | SLP_LP;
6637 phdr->param_len = 8;
6638 data = (uint8_t *)(phdr + 1);
6639 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6640 data[4] = 0x01; /* per-LUN */
6641 data += phdr->param_len;
6644 if (lun->backend->lun_attr != NULL &&
6645 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6647 phdr = (struct scsi_log_param_header *)data;
6648 scsi_ulto2b(0x00f1, phdr->param_code);
6649 phdr->param_control = SLP_LBIN | SLP_LP;
6650 phdr->param_len = 8;
6651 data = (uint8_t *)(phdr + 1);
6652 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6653 data[4] = 0x02; /* per-pool */
6654 data += phdr->param_len;
6657 if (lun->backend->lun_attr != NULL &&
6658 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6660 phdr = (struct scsi_log_param_header *)data;
6661 scsi_ulto2b(0x00f2, phdr->param_code);
6662 phdr->param_control = SLP_LBIN | SLP_LP;
6663 phdr->param_len = 8;
6664 data = (uint8_t *)(phdr + 1);
6665 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6666 data[4] = 0x02; /* per-pool */
6667 data += phdr->param_len;
6670 page_index->page_len = data - page_index->page_data;
6675 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6676 struct ctl_page_index *page_index,
6679 struct ctl_lun *lun;
6680 struct stat_page *data;
6681 uint64_t rn, wn, rb, wb;
6682 struct bintime rt, wt;
6685 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6686 data = (struct stat_page *)page_index->page_data;
6688 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6689 data->sap.hdr.param_control = SLP_LBIN;
6690 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6691 sizeof(struct scsi_log_param_header);
6692 rn = wn = rb = wb = 0;
6695 for (i = 0; i < CTL_MAX_PORTS; i++) {
6696 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6697 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6698 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6699 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6700 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6701 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6703 scsi_u64to8b(rn, data->sap.read_num);
6704 scsi_u64to8b(wn, data->sap.write_num);
6705 if (lun->stats.blocksize > 0) {
6706 scsi_u64to8b(wb / lun->stats.blocksize,
6707 data->sap.recvieved_lba);
6708 scsi_u64to8b(rb / lun->stats.blocksize,
6709 data->sap.transmitted_lba);
6711 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6712 data->sap.read_int);
6713 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6714 data->sap.write_int);
6715 scsi_u64to8b(0, data->sap.weighted_num);
6716 scsi_u64to8b(0, data->sap.weighted_int);
6717 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6718 data->it.hdr.param_control = SLP_LBIN;
6719 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6720 sizeof(struct scsi_log_param_header);
6722 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6724 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6725 data->it.hdr.param_control = SLP_LBIN;
6726 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6727 sizeof(struct scsi_log_param_header);
6728 scsi_ulto4b(3, data->ti.exponent);
6729 scsi_ulto4b(1, data->ti.integer);
6731 page_index->page_len = sizeof(*data);
6736 ctl_log_sense(struct ctl_scsiio *ctsio)
6738 struct ctl_lun *lun;
6739 int i, pc, page_code, subpage;
6740 int alloc_len, total_len;
6741 struct ctl_page_index *page_index;
6742 struct scsi_log_sense *cdb;
6743 struct scsi_log_header *header;
6745 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6747 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6748 cdb = (struct scsi_log_sense *)ctsio->cdb;
6749 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6750 page_code = cdb->page & SLS_PAGE_CODE;
6751 subpage = cdb->subpage;
6752 alloc_len = scsi_2btoul(cdb->length);
6755 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6756 page_index = &lun->log_pages.index[i];
6758 /* Look for the right page code */
6759 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6762 /* Look for the right subpage or the subpage wildcard*/
6763 if (page_index->subpage != subpage)
6768 if (i >= CTL_NUM_LOG_PAGES) {
6769 ctl_set_invalid_field(ctsio,
6775 ctl_done((union ctl_io *)ctsio);
6776 return (CTL_RETVAL_COMPLETE);
6779 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6781 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6782 ctsio->kern_sg_entries = 0;
6783 ctsio->kern_data_resid = 0;
6784 ctsio->kern_rel_offset = 0;
6785 if (total_len < alloc_len) {
6786 ctsio->residual = alloc_len - total_len;
6787 ctsio->kern_data_len = total_len;
6788 ctsio->kern_total_len = total_len;
6790 ctsio->residual = 0;
6791 ctsio->kern_data_len = alloc_len;
6792 ctsio->kern_total_len = alloc_len;
6795 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6796 header->page = page_index->page_code;
6797 if (page_index->subpage) {
6798 header->page |= SL_SPF;
6799 header->subpage = page_index->subpage;
6801 scsi_ulto2b(page_index->page_len, header->datalen);
6804 * Call the handler, if it exists, to update the
6805 * page to the latest values.
6807 if (page_index->sense_handler != NULL)
6808 page_index->sense_handler(ctsio, page_index, pc);
6810 memcpy(header + 1, page_index->page_data, page_index->page_len);
6812 ctl_set_success(ctsio);
6813 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6814 ctsio->be_move_done = ctl_config_move_done;
6815 ctl_datamove((union ctl_io *)ctsio);
6816 return (CTL_RETVAL_COMPLETE);
6820 ctl_read_capacity(struct ctl_scsiio *ctsio)
6822 struct scsi_read_capacity *cdb;
6823 struct scsi_read_capacity_data *data;
6824 struct ctl_lun *lun;
6827 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6829 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6831 lba = scsi_4btoul(cdb->addr);
6832 if (((cdb->pmi & SRC_PMI) == 0)
6834 ctl_set_invalid_field(/*ctsio*/ ctsio,
6840 ctl_done((union ctl_io *)ctsio);
6841 return (CTL_RETVAL_COMPLETE);
6844 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6846 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6847 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6848 ctsio->residual = 0;
6849 ctsio->kern_data_len = sizeof(*data);
6850 ctsio->kern_total_len = sizeof(*data);
6851 ctsio->kern_data_resid = 0;
6852 ctsio->kern_rel_offset = 0;
6853 ctsio->kern_sg_entries = 0;
6856 * If the maximum LBA is greater than 0xfffffffe, the user must
6857 * issue a SERVICE ACTION IN (16) command, with the read capacity
6858 * serivce action set.
6860 if (lun->be_lun->maxlba > 0xfffffffe)
6861 scsi_ulto4b(0xffffffff, data->addr);
6863 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6866 * XXX KDM this may not be 512 bytes...
6868 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6870 ctl_set_success(ctsio);
6871 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6872 ctsio->be_move_done = ctl_config_move_done;
6873 ctl_datamove((union ctl_io *)ctsio);
6874 return (CTL_RETVAL_COMPLETE);
6878 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6880 struct scsi_read_capacity_16 *cdb;
6881 struct scsi_read_capacity_data_long *data;
6882 struct ctl_lun *lun;
6886 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6888 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6890 alloc_len = scsi_4btoul(cdb->alloc_len);
6891 lba = scsi_8btou64(cdb->addr);
6893 if ((cdb->reladr & SRC16_PMI)
6895 ctl_set_invalid_field(/*ctsio*/ ctsio,
6901 ctl_done((union ctl_io *)ctsio);
6902 return (CTL_RETVAL_COMPLETE);
6905 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6907 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6908 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6910 if (sizeof(*data) < alloc_len) {
6911 ctsio->residual = alloc_len - sizeof(*data);
6912 ctsio->kern_data_len = sizeof(*data);
6913 ctsio->kern_total_len = sizeof(*data);
6915 ctsio->residual = 0;
6916 ctsio->kern_data_len = alloc_len;
6917 ctsio->kern_total_len = alloc_len;
6919 ctsio->kern_data_resid = 0;
6920 ctsio->kern_rel_offset = 0;
6921 ctsio->kern_sg_entries = 0;
6923 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6924 /* XXX KDM this may not be 512 bytes... */
6925 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6926 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6927 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6928 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6929 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6931 ctl_set_success(ctsio);
6932 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6933 ctsio->be_move_done = ctl_config_move_done;
6934 ctl_datamove((union ctl_io *)ctsio);
6935 return (CTL_RETVAL_COMPLETE);
6939 ctl_get_lba_status(struct ctl_scsiio *ctsio)
6941 struct scsi_get_lba_status *cdb;
6942 struct scsi_get_lba_status_data *data;
6943 struct ctl_lun *lun;
6944 struct ctl_lba_len_flags *lbalen;
6946 uint32_t alloc_len, total_len;
6949 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
6951 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6952 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
6953 lba = scsi_8btou64(cdb->addr);
6954 alloc_len = scsi_4btoul(cdb->alloc_len);
6956 if (lba > lun->be_lun->maxlba) {
6957 ctl_set_lba_out_of_range(ctsio);
6958 ctl_done((union ctl_io *)ctsio);
6959 return (CTL_RETVAL_COMPLETE);
6962 total_len = sizeof(*data) + sizeof(data->descr[0]);
6963 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6964 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
6966 if (total_len < alloc_len) {
6967 ctsio->residual = alloc_len - total_len;
6968 ctsio->kern_data_len = total_len;
6969 ctsio->kern_total_len = total_len;
6971 ctsio->residual = 0;
6972 ctsio->kern_data_len = alloc_len;
6973 ctsio->kern_total_len = alloc_len;
6975 ctsio->kern_data_resid = 0;
6976 ctsio->kern_rel_offset = 0;
6977 ctsio->kern_sg_entries = 0;
6979 /* Fill dummy data in case backend can't tell anything. */
6980 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
6981 scsi_u64to8b(lba, data->descr[0].addr);
6982 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
6983 data->descr[0].length);
6984 data->descr[0].status = 0; /* Mapped or unknown. */
6986 ctl_set_success(ctsio);
6987 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6988 ctsio->be_move_done = ctl_config_move_done;
6990 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
6992 lbalen->len = total_len;
6994 retval = lun->backend->config_read((union ctl_io *)ctsio);
6995 return (CTL_RETVAL_COMPLETE);
6999 ctl_read_defect(struct ctl_scsiio *ctsio)
7001 struct scsi_read_defect_data_10 *ccb10;
7002 struct scsi_read_defect_data_12 *ccb12;
7003 struct scsi_read_defect_data_hdr_10 *data10;
7004 struct scsi_read_defect_data_hdr_12 *data12;
7005 uint32_t alloc_len, data_len;
7008 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7010 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7011 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7012 format = ccb10->format;
7013 alloc_len = scsi_2btoul(ccb10->alloc_length);
7014 data_len = sizeof(*data10);
7016 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7017 format = ccb12->format;
7018 alloc_len = scsi_4btoul(ccb12->alloc_length);
7019 data_len = sizeof(*data12);
7021 if (alloc_len == 0) {
7022 ctl_set_success(ctsio);
7023 ctl_done((union ctl_io *)ctsio);
7024 return (CTL_RETVAL_COMPLETE);
7027 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7028 if (data_len < alloc_len) {
7029 ctsio->residual = alloc_len - data_len;
7030 ctsio->kern_data_len = data_len;
7031 ctsio->kern_total_len = data_len;
7033 ctsio->residual = 0;
7034 ctsio->kern_data_len = alloc_len;
7035 ctsio->kern_total_len = alloc_len;
7037 ctsio->kern_data_resid = 0;
7038 ctsio->kern_rel_offset = 0;
7039 ctsio->kern_sg_entries = 0;
7041 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7042 data10 = (struct scsi_read_defect_data_hdr_10 *)
7043 ctsio->kern_data_ptr;
7044 data10->format = format;
7045 scsi_ulto2b(0, data10->length);
7047 data12 = (struct scsi_read_defect_data_hdr_12 *)
7048 ctsio->kern_data_ptr;
7049 data12->format = format;
7050 scsi_ulto2b(0, data12->generation);
7051 scsi_ulto4b(0, data12->length);
7054 ctl_set_success(ctsio);
7055 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7056 ctsio->be_move_done = ctl_config_move_done;
7057 ctl_datamove((union ctl_io *)ctsio);
7058 return (CTL_RETVAL_COMPLETE);
7062 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7064 struct scsi_maintenance_in *cdb;
7066 int alloc_len, ext, total_len = 0, g, pc, pg, gs, os;
7067 int num_target_port_groups, num_target_ports;
7068 struct ctl_lun *lun;
7069 struct ctl_softc *softc;
7070 struct ctl_port *port;
7071 struct scsi_target_group_data *rtg_ptr;
7072 struct scsi_target_group_data_extended *rtg_ext_ptr;
7073 struct scsi_target_port_group_descriptor *tpg_desc;
7075 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7077 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7078 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7079 softc = lun->ctl_softc;
7081 retval = CTL_RETVAL_COMPLETE;
7083 switch (cdb->byte2 & STG_PDF_MASK) {
7084 case STG_PDF_LENGTH:
7087 case STG_PDF_EXTENDED:
7091 ctl_set_invalid_field(/*ctsio*/ ctsio,
7097 ctl_done((union ctl_io *)ctsio);
7101 if (softc->is_single)
7102 num_target_port_groups = 1;
7104 num_target_port_groups = NUM_TARGET_PORT_GROUPS;
7105 num_target_ports = 0;
7106 mtx_lock(&softc->ctl_lock);
7107 STAILQ_FOREACH(port, &softc->port_list, links) {
7108 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7110 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7114 mtx_unlock(&softc->ctl_lock);
7117 total_len = sizeof(struct scsi_target_group_data_extended);
7119 total_len = sizeof(struct scsi_target_group_data);
7120 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7121 num_target_port_groups +
7122 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7124 alloc_len = scsi_4btoul(cdb->length);
7126 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7128 ctsio->kern_sg_entries = 0;
7130 if (total_len < alloc_len) {
7131 ctsio->residual = alloc_len - total_len;
7132 ctsio->kern_data_len = total_len;
7133 ctsio->kern_total_len = total_len;
7135 ctsio->residual = 0;
7136 ctsio->kern_data_len = alloc_len;
7137 ctsio->kern_total_len = alloc_len;
7139 ctsio->kern_data_resid = 0;
7140 ctsio->kern_rel_offset = 0;
7143 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7144 ctsio->kern_data_ptr;
7145 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7146 rtg_ext_ptr->format_type = 0x10;
7147 rtg_ext_ptr->implicit_transition_time = 0;
7148 tpg_desc = &rtg_ext_ptr->groups[0];
7150 rtg_ptr = (struct scsi_target_group_data *)
7151 ctsio->kern_data_ptr;
7152 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7153 tpg_desc = &rtg_ptr->groups[0];
7156 mtx_lock(&softc->ctl_lock);
7157 pg = softc->port_min / softc->port_cnt;
7158 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7159 gs = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7160 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7161 gs = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7162 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7163 gs = TPG_ASYMMETRIC_ACCESS_STANDBY;
7165 gs = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7166 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7168 gs = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7170 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7171 for (g = 0; g < num_target_port_groups; g++) {
7172 tpg_desc->pref_state = (g == pg) ? gs : os;
7173 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7174 TPG_U_SUP | TPG_T_SUP;
7175 scsi_ulto2b(g + 1, tpg_desc->target_port_group);
7176 tpg_desc->status = TPG_IMPLICIT;
7178 STAILQ_FOREACH(port, &softc->port_list, links) {
7179 if (port->targ_port < g * softc->port_cnt ||
7180 port->targ_port >= (g + 1) * softc->port_cnt)
7182 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7184 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7186 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7187 relative_target_port_identifier);
7190 tpg_desc->target_port_count = pc;
7191 tpg_desc = (struct scsi_target_port_group_descriptor *)
7192 &tpg_desc->descriptors[pc];
7194 mtx_unlock(&softc->ctl_lock);
7196 ctl_set_success(ctsio);
7197 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7198 ctsio->be_move_done = ctl_config_move_done;
7199 ctl_datamove((union ctl_io *)ctsio);
7204 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7206 struct ctl_lun *lun;
7207 struct scsi_report_supported_opcodes *cdb;
7208 const struct ctl_cmd_entry *entry, *sentry;
7209 struct scsi_report_supported_opcodes_all *all;
7210 struct scsi_report_supported_opcodes_descr *descr;
7211 struct scsi_report_supported_opcodes_one *one;
7213 int alloc_len, total_len;
7214 int opcode, service_action, i, j, num;
7216 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7218 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7219 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7221 retval = CTL_RETVAL_COMPLETE;
7223 opcode = cdb->requested_opcode;
7224 service_action = scsi_2btoul(cdb->requested_service_action);
7225 switch (cdb->options & RSO_OPTIONS_MASK) {
7226 case RSO_OPTIONS_ALL:
7228 for (i = 0; i < 256; i++) {
7229 entry = &ctl_cmd_table[i];
7230 if (entry->flags & CTL_CMD_FLAG_SA5) {
7231 for (j = 0; j < 32; j++) {
7232 sentry = &((const struct ctl_cmd_entry *)
7234 if (ctl_cmd_applicable(
7235 lun->be_lun->lun_type, sentry))
7239 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7244 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7245 num * sizeof(struct scsi_report_supported_opcodes_descr);
7247 case RSO_OPTIONS_OC:
7248 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7249 ctl_set_invalid_field(/*ctsio*/ ctsio,
7255 ctl_done((union ctl_io *)ctsio);
7256 return (CTL_RETVAL_COMPLETE);
7258 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7260 case RSO_OPTIONS_OC_SA:
7261 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7262 service_action >= 32) {
7263 ctl_set_invalid_field(/*ctsio*/ ctsio,
7269 ctl_done((union ctl_io *)ctsio);
7270 return (CTL_RETVAL_COMPLETE);
7272 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7275 ctl_set_invalid_field(/*ctsio*/ ctsio,
7281 ctl_done((union ctl_io *)ctsio);
7282 return (CTL_RETVAL_COMPLETE);
7285 alloc_len = scsi_4btoul(cdb->length);
7287 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7289 ctsio->kern_sg_entries = 0;
7291 if (total_len < alloc_len) {
7292 ctsio->residual = alloc_len - total_len;
7293 ctsio->kern_data_len = total_len;
7294 ctsio->kern_total_len = total_len;
7296 ctsio->residual = 0;
7297 ctsio->kern_data_len = alloc_len;
7298 ctsio->kern_total_len = alloc_len;
7300 ctsio->kern_data_resid = 0;
7301 ctsio->kern_rel_offset = 0;
7303 switch (cdb->options & RSO_OPTIONS_MASK) {
7304 case RSO_OPTIONS_ALL:
7305 all = (struct scsi_report_supported_opcodes_all *)
7306 ctsio->kern_data_ptr;
7308 for (i = 0; i < 256; i++) {
7309 entry = &ctl_cmd_table[i];
7310 if (entry->flags & CTL_CMD_FLAG_SA5) {
7311 for (j = 0; j < 32; j++) {
7312 sentry = &((const struct ctl_cmd_entry *)
7314 if (!ctl_cmd_applicable(
7315 lun->be_lun->lun_type, sentry))
7317 descr = &all->descr[num++];
7319 scsi_ulto2b(j, descr->service_action);
7320 descr->flags = RSO_SERVACTV;
7321 scsi_ulto2b(sentry->length,
7325 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7328 descr = &all->descr[num++];
7330 scsi_ulto2b(0, descr->service_action);
7332 scsi_ulto2b(entry->length, descr->cdb_length);
7336 num * sizeof(struct scsi_report_supported_opcodes_descr),
7339 case RSO_OPTIONS_OC:
7340 one = (struct scsi_report_supported_opcodes_one *)
7341 ctsio->kern_data_ptr;
7342 entry = &ctl_cmd_table[opcode];
7344 case RSO_OPTIONS_OC_SA:
7345 one = (struct scsi_report_supported_opcodes_one *)
7346 ctsio->kern_data_ptr;
7347 entry = &ctl_cmd_table[opcode];
7348 entry = &((const struct ctl_cmd_entry *)
7349 entry->execute)[service_action];
7351 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7353 scsi_ulto2b(entry->length, one->cdb_length);
7354 one->cdb_usage[0] = opcode;
7355 memcpy(&one->cdb_usage[1], entry->usage,
7362 ctl_set_success(ctsio);
7363 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7364 ctsio->be_move_done = ctl_config_move_done;
7365 ctl_datamove((union ctl_io *)ctsio);
7370 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7372 struct scsi_report_supported_tmf *cdb;
7373 struct scsi_report_supported_tmf_data *data;
7375 int alloc_len, total_len;
7377 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7379 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7381 retval = CTL_RETVAL_COMPLETE;
7383 total_len = sizeof(struct scsi_report_supported_tmf_data);
7384 alloc_len = scsi_4btoul(cdb->length);
7386 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7388 ctsio->kern_sg_entries = 0;
7390 if (total_len < alloc_len) {
7391 ctsio->residual = alloc_len - total_len;
7392 ctsio->kern_data_len = total_len;
7393 ctsio->kern_total_len = total_len;
7395 ctsio->residual = 0;
7396 ctsio->kern_data_len = alloc_len;
7397 ctsio->kern_total_len = alloc_len;
7399 ctsio->kern_data_resid = 0;
7400 ctsio->kern_rel_offset = 0;
7402 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7403 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7405 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7407 ctl_set_success(ctsio);
7408 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7409 ctsio->be_move_done = ctl_config_move_done;
7410 ctl_datamove((union ctl_io *)ctsio);
7415 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7417 struct scsi_report_timestamp *cdb;
7418 struct scsi_report_timestamp_data *data;
7422 int alloc_len, total_len;
7424 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7426 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7428 retval = CTL_RETVAL_COMPLETE;
7430 total_len = sizeof(struct scsi_report_timestamp_data);
7431 alloc_len = scsi_4btoul(cdb->length);
7433 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7435 ctsio->kern_sg_entries = 0;
7437 if (total_len < alloc_len) {
7438 ctsio->residual = alloc_len - total_len;
7439 ctsio->kern_data_len = total_len;
7440 ctsio->kern_total_len = total_len;
7442 ctsio->residual = 0;
7443 ctsio->kern_data_len = alloc_len;
7444 ctsio->kern_total_len = alloc_len;
7446 ctsio->kern_data_resid = 0;
7447 ctsio->kern_rel_offset = 0;
7449 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7450 scsi_ulto2b(sizeof(*data) - 2, data->length);
7451 data->origin = RTS_ORIG_OUTSIDE;
7453 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7454 scsi_ulto4b(timestamp >> 16, data->timestamp);
7455 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7457 ctl_set_success(ctsio);
7458 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7459 ctsio->be_move_done = ctl_config_move_done;
7460 ctl_datamove((union ctl_io *)ctsio);
7465 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7467 struct scsi_per_res_in *cdb;
7468 int alloc_len, total_len = 0;
7469 /* struct scsi_per_res_in_rsrv in_data; */
7470 struct ctl_lun *lun;
7471 struct ctl_softc *softc;
7474 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7476 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7478 alloc_len = scsi_2btoul(cdb->length);
7480 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7481 softc = lun->ctl_softc;
7484 mtx_lock(&lun->lun_lock);
7485 switch (cdb->action) {
7486 case SPRI_RK: /* read keys */
7487 total_len = sizeof(struct scsi_per_res_in_keys) +
7489 sizeof(struct scsi_per_res_key);
7491 case SPRI_RR: /* read reservation */
7492 if (lun->flags & CTL_LUN_PR_RESERVED)
7493 total_len = sizeof(struct scsi_per_res_in_rsrv);
7495 total_len = sizeof(struct scsi_per_res_in_header);
7497 case SPRI_RC: /* report capabilities */
7498 total_len = sizeof(struct scsi_per_res_cap);
7500 case SPRI_RS: /* read full status */
7501 total_len = sizeof(struct scsi_per_res_in_header) +
7502 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7506 panic("Invalid PR type %x", cdb->action);
7508 mtx_unlock(&lun->lun_lock);
7510 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7512 if (total_len < alloc_len) {
7513 ctsio->residual = alloc_len - total_len;
7514 ctsio->kern_data_len = total_len;
7515 ctsio->kern_total_len = total_len;
7517 ctsio->residual = 0;
7518 ctsio->kern_data_len = alloc_len;
7519 ctsio->kern_total_len = alloc_len;
7522 ctsio->kern_data_resid = 0;
7523 ctsio->kern_rel_offset = 0;
7524 ctsio->kern_sg_entries = 0;
7526 mtx_lock(&lun->lun_lock);
7527 switch (cdb->action) {
7528 case SPRI_RK: { // read keys
7529 struct scsi_per_res_in_keys *res_keys;
7532 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7535 * We had to drop the lock to allocate our buffer, which
7536 * leaves time for someone to come in with another
7537 * persistent reservation. (That is unlikely, though,
7538 * since this should be the only persistent reservation
7539 * command active right now.)
7541 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7542 (lun->pr_key_count *
7543 sizeof(struct scsi_per_res_key)))){
7544 mtx_unlock(&lun->lun_lock);
7545 free(ctsio->kern_data_ptr, M_CTL);
7546 printf("%s: reservation length changed, retrying\n",
7551 scsi_ulto4b(lun->PRGeneration, res_keys->header.generation);
7553 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7554 lun->pr_key_count, res_keys->header.length);
7556 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7557 if ((key = ctl_get_prkey(lun, i)) == 0)
7561 * We used lun->pr_key_count to calculate the
7562 * size to allocate. If it turns out the number of
7563 * initiators with the registered flag set is
7564 * larger than that (i.e. they haven't been kept in
7565 * sync), we've got a problem.
7567 if (key_count >= lun->pr_key_count) {
7571 scsi_u64to8b(key, res_keys->keys[key_count].key);
7576 case SPRI_RR: { // read reservation
7577 struct scsi_per_res_in_rsrv *res;
7578 int tmp_len, header_only;
7580 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7582 scsi_ulto4b(lun->PRGeneration, res->header.generation);
7584 if (lun->flags & CTL_LUN_PR_RESERVED)
7586 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7587 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7588 res->header.length);
7591 tmp_len = sizeof(struct scsi_per_res_in_header);
7592 scsi_ulto4b(0, res->header.length);
7597 * We had to drop the lock to allocate our buffer, which
7598 * leaves time for someone to come in with another
7599 * persistent reservation. (That is unlikely, though,
7600 * since this should be the only persistent reservation
7601 * command active right now.)
7603 if (tmp_len != total_len) {
7604 mtx_unlock(&lun->lun_lock);
7605 free(ctsio->kern_data_ptr, M_CTL);
7606 printf("%s: reservation status changed, retrying\n",
7612 * No reservation held, so we're done.
7614 if (header_only != 0)
7618 * If the registration is an All Registrants type, the key
7619 * is 0, since it doesn't really matter.
7621 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7622 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7623 res->data.reservation);
7625 res->data.scopetype = lun->res_type;
7628 case SPRI_RC: //report capabilities
7630 struct scsi_per_res_cap *res_cap;
7633 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7634 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7635 res_cap->flags2 |= SPRI_TMV | SPRI_ALLOW_5;
7636 type_mask = SPRI_TM_WR_EX_AR |
7642 scsi_ulto2b(type_mask, res_cap->type_mask);
7645 case SPRI_RS: { // read full status
7646 struct scsi_per_res_in_full *res_status;
7647 struct scsi_per_res_in_full_desc *res_desc;
7648 struct ctl_port *port;
7651 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7654 * We had to drop the lock to allocate our buffer, which
7655 * leaves time for someone to come in with another
7656 * persistent reservation. (That is unlikely, though,
7657 * since this should be the only persistent reservation
7658 * command active right now.)
7660 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7661 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7662 lun->pr_key_count)){
7663 mtx_unlock(&lun->lun_lock);
7664 free(ctsio->kern_data_ptr, M_CTL);
7665 printf("%s: reservation length changed, retrying\n",
7670 scsi_ulto4b(lun->PRGeneration, res_status->header.generation);
7672 res_desc = &res_status->desc[0];
7673 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7674 if ((key = ctl_get_prkey(lun, i)) == 0)
7677 scsi_u64to8b(key, res_desc->res_key.key);
7678 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7679 (lun->pr_res_idx == i ||
7680 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7681 res_desc->flags = SPRI_FULL_R_HOLDER;
7682 res_desc->scopetype = lun->res_type;
7684 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7685 res_desc->rel_trgt_port_id);
7687 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7689 len = ctl_create_iid(port,
7690 i % CTL_MAX_INIT_PER_PORT,
7691 res_desc->transport_id);
7692 scsi_ulto4b(len, res_desc->additional_length);
7693 res_desc = (struct scsi_per_res_in_full_desc *)
7694 &res_desc->transport_id[len];
7696 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7697 res_status->header.length);
7702 * This is a bug, because we just checked for this above,
7703 * and should have returned an error.
7705 panic("Invalid PR type %x", cdb->action);
7706 break; /* NOTREACHED */
7708 mtx_unlock(&lun->lun_lock);
7710 ctl_set_success(ctsio);
7711 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7712 ctsio->be_move_done = ctl_config_move_done;
7713 ctl_datamove((union ctl_io *)ctsio);
7714 return (CTL_RETVAL_COMPLETE);
7718 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7722 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7723 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7724 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7725 struct scsi_per_res_out_parms* param)
7727 union ctl_ha_msg persis_io;
7730 mtx_lock(&lun->lun_lock);
7731 if (sa_res_key == 0) {
7732 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7733 /* validate scope and type */
7734 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7736 mtx_unlock(&lun->lun_lock);
7737 ctl_set_invalid_field(/*ctsio*/ ctsio,
7743 ctl_done((union ctl_io *)ctsio);
7747 if (type>8 || type==2 || type==4 || type==0) {
7748 mtx_unlock(&lun->lun_lock);
7749 ctl_set_invalid_field(/*ctsio*/ ctsio,
7755 ctl_done((union ctl_io *)ctsio);
7760 * Unregister everybody else and build UA for
7763 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7764 if (i == residx || ctl_get_prkey(lun, i) == 0)
7767 ctl_clr_prkey(lun, i);
7768 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7770 lun->pr_key_count = 1;
7771 lun->res_type = type;
7772 if (lun->res_type != SPR_TYPE_WR_EX_AR
7773 && lun->res_type != SPR_TYPE_EX_AC_AR)
7774 lun->pr_res_idx = residx;
7775 lun->PRGeneration++;
7776 mtx_unlock(&lun->lun_lock);
7778 /* send msg to other side */
7779 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7780 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7781 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7782 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7783 persis_io.pr.pr_info.res_type = type;
7784 memcpy(persis_io.pr.pr_info.sa_res_key,
7785 param->serv_act_res_key,
7786 sizeof(param->serv_act_res_key));
7787 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7788 sizeof(persis_io.pr), M_WAITOK);
7790 /* not all registrants */
7791 mtx_unlock(&lun->lun_lock);
7792 free(ctsio->kern_data_ptr, M_CTL);
7793 ctl_set_invalid_field(ctsio,
7799 ctl_done((union ctl_io *)ctsio);
7802 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7803 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7806 if (res_key == sa_res_key) {
7809 * The spec implies this is not good but doesn't
7810 * say what to do. There are two choices either
7811 * generate a res conflict or check condition
7812 * with illegal field in parameter data. Since
7813 * that is what is done when the sa_res_key is
7814 * zero I'll take that approach since this has
7815 * to do with the sa_res_key.
7817 mtx_unlock(&lun->lun_lock);
7818 free(ctsio->kern_data_ptr, M_CTL);
7819 ctl_set_invalid_field(ctsio,
7825 ctl_done((union ctl_io *)ctsio);
7829 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7830 if (ctl_get_prkey(lun, i) != sa_res_key)
7834 ctl_clr_prkey(lun, i);
7835 lun->pr_key_count--;
7836 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7839 mtx_unlock(&lun->lun_lock);
7840 free(ctsio->kern_data_ptr, M_CTL);
7841 ctl_set_reservation_conflict(ctsio);
7842 ctl_done((union ctl_io *)ctsio);
7843 return (CTL_RETVAL_COMPLETE);
7845 lun->PRGeneration++;
7846 mtx_unlock(&lun->lun_lock);
7848 /* send msg to other side */
7849 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7850 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7851 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7852 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7853 persis_io.pr.pr_info.res_type = type;
7854 memcpy(persis_io.pr.pr_info.sa_res_key,
7855 param->serv_act_res_key,
7856 sizeof(param->serv_act_res_key));
7857 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7858 sizeof(persis_io.pr), M_WAITOK);
7860 /* Reserved but not all registrants */
7861 /* sa_res_key is res holder */
7862 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7863 /* validate scope and type */
7864 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7866 mtx_unlock(&lun->lun_lock);
7867 ctl_set_invalid_field(/*ctsio*/ ctsio,
7873 ctl_done((union ctl_io *)ctsio);
7877 if (type>8 || type==2 || type==4 || type==0) {
7878 mtx_unlock(&lun->lun_lock);
7879 ctl_set_invalid_field(/*ctsio*/ ctsio,
7885 ctl_done((union ctl_io *)ctsio);
7891 * if sa_res_key != res_key remove all
7892 * registrants w/sa_res_key and generate UA
7893 * for these registrants(Registrations
7894 * Preempted) if it wasn't an exclusive
7895 * reservation generate UA(Reservations
7896 * Preempted) for all other registered nexuses
7897 * if the type has changed. Establish the new
7898 * reservation and holder. If res_key and
7899 * sa_res_key are the same do the above
7900 * except don't unregister the res holder.
7903 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7904 if (i == residx || ctl_get_prkey(lun, i) == 0)
7907 if (sa_res_key == ctl_get_prkey(lun, i)) {
7908 ctl_clr_prkey(lun, i);
7909 lun->pr_key_count--;
7910 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7911 } else if (type != lun->res_type
7912 && (lun->res_type == SPR_TYPE_WR_EX_RO
7913 || lun->res_type ==SPR_TYPE_EX_AC_RO)){
7914 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7917 lun->res_type = type;
7918 if (lun->res_type != SPR_TYPE_WR_EX_AR
7919 && lun->res_type != SPR_TYPE_EX_AC_AR)
7920 lun->pr_res_idx = residx;
7922 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7923 lun->PRGeneration++;
7924 mtx_unlock(&lun->lun_lock);
7926 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7927 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7928 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7929 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7930 persis_io.pr.pr_info.res_type = type;
7931 memcpy(persis_io.pr.pr_info.sa_res_key,
7932 param->serv_act_res_key,
7933 sizeof(param->serv_act_res_key));
7934 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7935 sizeof(persis_io.pr), M_WAITOK);
7938 * sa_res_key is not the res holder just
7939 * remove registrants
7943 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7944 if (sa_res_key != ctl_get_prkey(lun, i))
7948 ctl_clr_prkey(lun, i);
7949 lun->pr_key_count--;
7950 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7954 mtx_unlock(&lun->lun_lock);
7955 free(ctsio->kern_data_ptr, M_CTL);
7956 ctl_set_reservation_conflict(ctsio);
7957 ctl_done((union ctl_io *)ctsio);
7960 lun->PRGeneration++;
7961 mtx_unlock(&lun->lun_lock);
7963 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7964 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7965 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7966 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7967 persis_io.pr.pr_info.res_type = type;
7968 memcpy(persis_io.pr.pr_info.sa_res_key,
7969 param->serv_act_res_key,
7970 sizeof(param->serv_act_res_key));
7971 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7972 sizeof(persis_io.pr), M_WAITOK);
7979 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
7981 uint64_t sa_res_key;
7984 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
7986 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7987 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
7988 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
7989 if (sa_res_key == 0) {
7991 * Unregister everybody else and build UA for
7994 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7995 if (i == msg->pr.pr_info.residx ||
7996 ctl_get_prkey(lun, i) == 0)
7999 ctl_clr_prkey(lun, i);
8000 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8003 lun->pr_key_count = 1;
8004 lun->res_type = msg->pr.pr_info.res_type;
8005 if (lun->res_type != SPR_TYPE_WR_EX_AR
8006 && lun->res_type != SPR_TYPE_EX_AC_AR)
8007 lun->pr_res_idx = msg->pr.pr_info.residx;
8009 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8010 if (sa_res_key == ctl_get_prkey(lun, i))
8013 ctl_clr_prkey(lun, i);
8014 lun->pr_key_count--;
8015 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8019 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8020 if (i == msg->pr.pr_info.residx ||
8021 ctl_get_prkey(lun, i) == 0)
8024 if (sa_res_key == ctl_get_prkey(lun, i)) {
8025 ctl_clr_prkey(lun, i);
8026 lun->pr_key_count--;
8027 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8028 } else if (msg->pr.pr_info.res_type != lun->res_type
8029 && (lun->res_type == SPR_TYPE_WR_EX_RO
8030 || lun->res_type == SPR_TYPE_EX_AC_RO)) {
8031 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8034 lun->res_type = msg->pr.pr_info.res_type;
8035 if (lun->res_type != SPR_TYPE_WR_EX_AR
8036 && lun->res_type != SPR_TYPE_EX_AC_AR)
8037 lun->pr_res_idx = msg->pr.pr_info.residx;
8039 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8041 lun->PRGeneration++;
8047 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8050 u_int32_t param_len;
8051 struct scsi_per_res_out *cdb;
8052 struct ctl_lun *lun;
8053 struct scsi_per_res_out_parms* param;
8054 struct ctl_softc *softc;
8056 uint64_t res_key, sa_res_key, key;
8058 union ctl_ha_msg persis_io;
8061 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8063 retval = CTL_RETVAL_COMPLETE;
8065 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8066 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8067 softc = lun->ctl_softc;
8070 * We only support whole-LUN scope. The scope & type are ignored for
8071 * register, register and ignore existing key and clear.
8072 * We sometimes ignore scope and type on preempts too!!
8073 * Verify reservation type here as well.
8075 type = cdb->scope_type & SPR_TYPE_MASK;
8076 if ((cdb->action == SPRO_RESERVE)
8077 || (cdb->action == SPRO_RELEASE)) {
8078 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8079 ctl_set_invalid_field(/*ctsio*/ ctsio,
8085 ctl_done((union ctl_io *)ctsio);
8086 return (CTL_RETVAL_COMPLETE);
8089 if (type>8 || type==2 || type==4 || type==0) {
8090 ctl_set_invalid_field(/*ctsio*/ ctsio,
8096 ctl_done((union ctl_io *)ctsio);
8097 return (CTL_RETVAL_COMPLETE);
8101 param_len = scsi_4btoul(cdb->length);
8103 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8104 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8105 ctsio->kern_data_len = param_len;
8106 ctsio->kern_total_len = param_len;
8107 ctsio->kern_data_resid = 0;
8108 ctsio->kern_rel_offset = 0;
8109 ctsio->kern_sg_entries = 0;
8110 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8111 ctsio->be_move_done = ctl_config_move_done;
8112 ctl_datamove((union ctl_io *)ctsio);
8114 return (CTL_RETVAL_COMPLETE);
8117 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8119 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8120 res_key = scsi_8btou64(param->res_key.key);
8121 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8124 * Validate the reservation key here except for SPRO_REG_IGNO
8125 * This must be done for all other service actions
8127 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8128 mtx_lock(&lun->lun_lock);
8129 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8130 if (res_key != key) {
8132 * The current key passed in doesn't match
8133 * the one the initiator previously
8136 mtx_unlock(&lun->lun_lock);
8137 free(ctsio->kern_data_ptr, M_CTL);
8138 ctl_set_reservation_conflict(ctsio);
8139 ctl_done((union ctl_io *)ctsio);
8140 return (CTL_RETVAL_COMPLETE);
8142 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8144 * We are not registered
8146 mtx_unlock(&lun->lun_lock);
8147 free(ctsio->kern_data_ptr, M_CTL);
8148 ctl_set_reservation_conflict(ctsio);
8149 ctl_done((union ctl_io *)ctsio);
8150 return (CTL_RETVAL_COMPLETE);
8151 } else if (res_key != 0) {
8153 * We are not registered and trying to register but
8154 * the register key isn't zero.
8156 mtx_unlock(&lun->lun_lock);
8157 free(ctsio->kern_data_ptr, M_CTL);
8158 ctl_set_reservation_conflict(ctsio);
8159 ctl_done((union ctl_io *)ctsio);
8160 return (CTL_RETVAL_COMPLETE);
8162 mtx_unlock(&lun->lun_lock);
8165 switch (cdb->action & SPRO_ACTION_MASK) {
8167 case SPRO_REG_IGNO: {
8170 printf("Registration received\n");
8174 * We don't support any of these options, as we report in
8175 * the read capabilities request (see
8176 * ctl_persistent_reserve_in(), above).
8178 if ((param->flags & SPR_SPEC_I_PT)
8179 || (param->flags & SPR_ALL_TG_PT)
8180 || (param->flags & SPR_APTPL)) {
8183 if (param->flags & SPR_APTPL)
8185 else if (param->flags & SPR_ALL_TG_PT)
8187 else /* SPR_SPEC_I_PT */
8190 free(ctsio->kern_data_ptr, M_CTL);
8191 ctl_set_invalid_field(ctsio,
8197 ctl_done((union ctl_io *)ctsio);
8198 return (CTL_RETVAL_COMPLETE);
8201 mtx_lock(&lun->lun_lock);
8204 * The initiator wants to clear the
8207 if (sa_res_key == 0) {
8209 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8210 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8211 && ctl_get_prkey(lun, residx) == 0)) {
8212 mtx_unlock(&lun->lun_lock);
8216 ctl_clr_prkey(lun, residx);
8217 lun->pr_key_count--;
8219 if (residx == lun->pr_res_idx) {
8220 lun->flags &= ~CTL_LUN_PR_RESERVED;
8221 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8223 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8224 || lun->res_type == SPR_TYPE_EX_AC_RO)
8225 && lun->pr_key_count) {
8227 * If the reservation is a registrants
8228 * only type we need to generate a UA
8229 * for other registered inits. The
8230 * sense code should be RESERVATIONS
8234 for (i = softc->init_min; i < softc->init_max; i++){
8235 if (ctl_get_prkey(lun, i) == 0)
8238 CTL_UA_RES_RELEASE);
8242 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8243 if (lun->pr_key_count==0) {
8244 lun->flags &= ~CTL_LUN_PR_RESERVED;
8246 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8249 lun->PRGeneration++;
8250 mtx_unlock(&lun->lun_lock);
8252 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8253 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8254 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8255 persis_io.pr.pr_info.residx = residx;
8256 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8257 sizeof(persis_io.pr), M_WAITOK);
8258 } else /* sa_res_key != 0 */ {
8261 * If we aren't registered currently then increment
8262 * the key count and set the registered flag.
8264 ctl_alloc_prkey(lun, residx);
8265 if (ctl_get_prkey(lun, residx) == 0)
8266 lun->pr_key_count++;
8267 ctl_set_prkey(lun, residx, sa_res_key);
8268 lun->PRGeneration++;
8269 mtx_unlock(&lun->lun_lock);
8271 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8272 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8273 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8274 persis_io.pr.pr_info.residx = residx;
8275 memcpy(persis_io.pr.pr_info.sa_res_key,
8276 param->serv_act_res_key,
8277 sizeof(param->serv_act_res_key));
8278 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8279 sizeof(persis_io.pr), M_WAITOK);
8286 printf("Reserve executed type %d\n", type);
8288 mtx_lock(&lun->lun_lock);
8289 if (lun->flags & CTL_LUN_PR_RESERVED) {
8291 * if this isn't the reservation holder and it's
8292 * not a "all registrants" type or if the type is
8293 * different then we have a conflict
8295 if ((lun->pr_res_idx != residx
8296 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8297 || lun->res_type != type) {
8298 mtx_unlock(&lun->lun_lock);
8299 free(ctsio->kern_data_ptr, M_CTL);
8300 ctl_set_reservation_conflict(ctsio);
8301 ctl_done((union ctl_io *)ctsio);
8302 return (CTL_RETVAL_COMPLETE);
8304 mtx_unlock(&lun->lun_lock);
8305 } else /* create a reservation */ {
8307 * If it's not an "all registrants" type record
8308 * reservation holder
8310 if (type != SPR_TYPE_WR_EX_AR
8311 && type != SPR_TYPE_EX_AC_AR)
8312 lun->pr_res_idx = residx; /* Res holder */
8314 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8316 lun->flags |= CTL_LUN_PR_RESERVED;
8317 lun->res_type = type;
8319 mtx_unlock(&lun->lun_lock);
8321 /* send msg to other side */
8322 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8323 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8324 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8325 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8326 persis_io.pr.pr_info.res_type = type;
8327 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8328 sizeof(persis_io.pr), M_WAITOK);
8333 mtx_lock(&lun->lun_lock);
8334 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8335 /* No reservation exists return good status */
8336 mtx_unlock(&lun->lun_lock);
8340 * Is this nexus a reservation holder?
8342 if (lun->pr_res_idx != residx
8343 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8345 * not a res holder return good status but
8348 mtx_unlock(&lun->lun_lock);
8352 if (lun->res_type != type) {
8353 mtx_unlock(&lun->lun_lock);
8354 free(ctsio->kern_data_ptr, M_CTL);
8355 ctl_set_illegal_pr_release(ctsio);
8356 ctl_done((union ctl_io *)ctsio);
8357 return (CTL_RETVAL_COMPLETE);
8360 /* okay to release */
8361 lun->flags &= ~CTL_LUN_PR_RESERVED;
8362 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8366 * if this isn't an exclusive access
8367 * res generate UA for all other
8370 if (type != SPR_TYPE_EX_AC
8371 && type != SPR_TYPE_WR_EX) {
8372 for (i = softc->init_min; i < softc->init_max; i++) {
8373 if (i == residx || ctl_get_prkey(lun, i) == 0)
8375 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8378 mtx_unlock(&lun->lun_lock);
8380 /* Send msg to other side */
8381 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8382 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8383 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8384 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8385 sizeof(persis_io.pr), M_WAITOK);
8389 /* send msg to other side */
8391 mtx_lock(&lun->lun_lock);
8392 lun->flags &= ~CTL_LUN_PR_RESERVED;
8394 lun->pr_key_count = 0;
8395 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8397 ctl_clr_prkey(lun, residx);
8398 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8399 if (ctl_get_prkey(lun, i) != 0) {
8400 ctl_clr_prkey(lun, i);
8401 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8403 lun->PRGeneration++;
8404 mtx_unlock(&lun->lun_lock);
8406 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8407 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8408 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8409 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8410 sizeof(persis_io.pr), M_WAITOK);
8414 case SPRO_PRE_ABO: {
8417 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8418 residx, ctsio, cdb, param);
8420 return (CTL_RETVAL_COMPLETE);
8424 panic("Invalid PR type %x", cdb->action);
8428 free(ctsio->kern_data_ptr, M_CTL);
8429 ctl_set_success(ctsio);
8430 ctl_done((union ctl_io *)ctsio);
8436 * This routine is for handling a message from the other SC pertaining to
8437 * persistent reserve out. All the error checking will have been done
8438 * so only perorming the action need be done here to keep the two
8442 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8444 struct ctl_softc *softc = control_softc;
8445 struct ctl_lun *lun;
8447 uint32_t residx, targ_lun;
8449 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8450 mtx_lock(&softc->ctl_lock);
8451 if ((targ_lun >= CTL_MAX_LUNS) ||
8452 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8453 mtx_unlock(&softc->ctl_lock);
8456 mtx_lock(&lun->lun_lock);
8457 mtx_unlock(&softc->ctl_lock);
8458 if (lun->flags & CTL_LUN_DISABLED) {
8459 mtx_unlock(&lun->lun_lock);
8462 residx = ctl_get_initindex(&msg->hdr.nexus);
8463 switch(msg->pr.pr_info.action) {
8464 case CTL_PR_REG_KEY:
8465 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8466 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8467 lun->pr_key_count++;
8468 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8469 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8470 lun->PRGeneration++;
8473 case CTL_PR_UNREG_KEY:
8474 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8475 lun->pr_key_count--;
8477 /* XXX Need to see if the reservation has been released */
8478 /* if so do we need to generate UA? */
8479 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8480 lun->flags &= ~CTL_LUN_PR_RESERVED;
8481 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8483 if ((lun->res_type == SPR_TYPE_WR_EX_RO
8484 || lun->res_type == SPR_TYPE_EX_AC_RO)
8485 && lun->pr_key_count) {
8487 * If the reservation is a registrants
8488 * only type we need to generate a UA
8489 * for other registered inits. The
8490 * sense code should be RESERVATIONS
8494 for (i = softc->init_min; i < softc->init_max; i++) {
8495 if (ctl_get_prkey(lun, i) == 0)
8498 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8502 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8503 if (lun->pr_key_count==0) {
8504 lun->flags &= ~CTL_LUN_PR_RESERVED;
8506 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8509 lun->PRGeneration++;
8512 case CTL_PR_RESERVE:
8513 lun->flags |= CTL_LUN_PR_RESERVED;
8514 lun->res_type = msg->pr.pr_info.res_type;
8515 lun->pr_res_idx = msg->pr.pr_info.residx;
8519 case CTL_PR_RELEASE:
8521 * if this isn't an exclusive access res generate UA for all
8522 * other registrants.
8524 if (lun->res_type != SPR_TYPE_EX_AC
8525 && lun->res_type != SPR_TYPE_WR_EX) {
8526 for (i = softc->init_min; i < softc->init_max; i++)
8527 if (i == residx || ctl_get_prkey(lun, i) == 0)
8529 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8532 lun->flags &= ~CTL_LUN_PR_RESERVED;
8533 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8537 case CTL_PR_PREEMPT:
8538 ctl_pro_preempt_other(lun, msg);
8541 lun->flags &= ~CTL_LUN_PR_RESERVED;
8543 lun->pr_key_count = 0;
8544 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8546 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8547 if (ctl_get_prkey(lun, i) == 0)
8549 ctl_clr_prkey(lun, i);
8550 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8552 lun->PRGeneration++;
8556 mtx_unlock(&lun->lun_lock);
8560 ctl_read_write(struct ctl_scsiio *ctsio)
8562 struct ctl_lun *lun;
8563 struct ctl_lba_len_flags *lbalen;
8565 uint32_t num_blocks;
8569 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8571 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8574 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8575 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8576 switch (ctsio->cdb[0]) {
8579 struct scsi_rw_6 *cdb;
8581 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8583 lba = scsi_3btoul(cdb->addr);
8584 /* only 5 bits are valid in the most significant address byte */
8586 num_blocks = cdb->length;
8588 * This is correct according to SBC-2.
8590 if (num_blocks == 0)
8596 struct scsi_rw_10 *cdb;
8598 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8599 if (cdb->byte2 & SRW10_FUA)
8600 flags |= CTL_LLF_FUA;
8601 if (cdb->byte2 & SRW10_DPO)
8602 flags |= CTL_LLF_DPO;
8603 lba = scsi_4btoul(cdb->addr);
8604 num_blocks = scsi_2btoul(cdb->length);
8607 case WRITE_VERIFY_10: {
8608 struct scsi_write_verify_10 *cdb;
8610 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8611 flags |= CTL_LLF_FUA;
8612 if (cdb->byte2 & SWV_DPO)
8613 flags |= CTL_LLF_DPO;
8614 lba = scsi_4btoul(cdb->addr);
8615 num_blocks = scsi_2btoul(cdb->length);
8620 struct scsi_rw_12 *cdb;
8622 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8623 if (cdb->byte2 & SRW12_FUA)
8624 flags |= CTL_LLF_FUA;
8625 if (cdb->byte2 & SRW12_DPO)
8626 flags |= CTL_LLF_DPO;
8627 lba = scsi_4btoul(cdb->addr);
8628 num_blocks = scsi_4btoul(cdb->length);
8631 case WRITE_VERIFY_12: {
8632 struct scsi_write_verify_12 *cdb;
8634 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8635 flags |= CTL_LLF_FUA;
8636 if (cdb->byte2 & SWV_DPO)
8637 flags |= CTL_LLF_DPO;
8638 lba = scsi_4btoul(cdb->addr);
8639 num_blocks = scsi_4btoul(cdb->length);
8644 struct scsi_rw_16 *cdb;
8646 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8647 if (cdb->byte2 & SRW12_FUA)
8648 flags |= CTL_LLF_FUA;
8649 if (cdb->byte2 & SRW12_DPO)
8650 flags |= CTL_LLF_DPO;
8651 lba = scsi_8btou64(cdb->addr);
8652 num_blocks = scsi_4btoul(cdb->length);
8655 case WRITE_ATOMIC_16: {
8656 struct scsi_write_atomic_16 *cdb;
8658 if (lun->be_lun->atomicblock == 0) {
8659 ctl_set_invalid_opcode(ctsio);
8660 ctl_done((union ctl_io *)ctsio);
8661 return (CTL_RETVAL_COMPLETE);
8664 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8665 if (cdb->byte2 & SRW12_FUA)
8666 flags |= CTL_LLF_FUA;
8667 if (cdb->byte2 & SRW12_DPO)
8668 flags |= CTL_LLF_DPO;
8669 lba = scsi_8btou64(cdb->addr);
8670 num_blocks = scsi_2btoul(cdb->length);
8671 if (num_blocks > lun->be_lun->atomicblock) {
8672 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8673 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8675 ctl_done((union ctl_io *)ctsio);
8676 return (CTL_RETVAL_COMPLETE);
8680 case WRITE_VERIFY_16: {
8681 struct scsi_write_verify_16 *cdb;
8683 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8684 flags |= CTL_LLF_FUA;
8685 if (cdb->byte2 & SWV_DPO)
8686 flags |= CTL_LLF_DPO;
8687 lba = scsi_8btou64(cdb->addr);
8688 num_blocks = scsi_4btoul(cdb->length);
8693 * We got a command we don't support. This shouldn't
8694 * happen, commands should be filtered out above us.
8696 ctl_set_invalid_opcode(ctsio);
8697 ctl_done((union ctl_io *)ctsio);
8699 return (CTL_RETVAL_COMPLETE);
8700 break; /* NOTREACHED */
8704 * The first check is to make sure we're in bounds, the second
8705 * check is to catch wrap-around problems. If the lba + num blocks
8706 * is less than the lba, then we've wrapped around and the block
8707 * range is invalid anyway.
8709 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8710 || ((lba + num_blocks) < lba)) {
8711 ctl_set_lba_out_of_range(ctsio);
8712 ctl_done((union ctl_io *)ctsio);
8713 return (CTL_RETVAL_COMPLETE);
8717 * According to SBC-3, a transfer length of 0 is not an error.
8718 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8719 * translates to 256 blocks for those commands.
8721 if (num_blocks == 0) {
8722 ctl_set_success(ctsio);
8723 ctl_done((union ctl_io *)ctsio);
8724 return (CTL_RETVAL_COMPLETE);
8727 /* Set FUA and/or DPO if caches are disabled. */
8729 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8731 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8733 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8735 flags |= CTL_LLF_FUA;
8738 lbalen = (struct ctl_lba_len_flags *)
8739 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8741 lbalen->len = num_blocks;
8742 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8744 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8745 ctsio->kern_rel_offset = 0;
8747 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8749 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8754 ctl_cnw_cont(union ctl_io *io)
8756 struct ctl_scsiio *ctsio;
8757 struct ctl_lun *lun;
8758 struct ctl_lba_len_flags *lbalen;
8761 ctsio = &io->scsiio;
8762 ctsio->io_hdr.status = CTL_STATUS_NONE;
8763 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8764 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8765 lbalen = (struct ctl_lba_len_flags *)
8766 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8767 lbalen->flags &= ~CTL_LLF_COMPARE;
8768 lbalen->flags |= CTL_LLF_WRITE;
8770 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8771 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8776 ctl_cnw(struct ctl_scsiio *ctsio)
8778 struct ctl_lun *lun;
8779 struct ctl_lba_len_flags *lbalen;
8781 uint32_t num_blocks;
8784 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8786 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8789 switch (ctsio->cdb[0]) {
8790 case COMPARE_AND_WRITE: {
8791 struct scsi_compare_and_write *cdb;
8793 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8794 if (cdb->byte2 & SRW10_FUA)
8795 flags |= CTL_LLF_FUA;
8796 if (cdb->byte2 & SRW10_DPO)
8797 flags |= CTL_LLF_DPO;
8798 lba = scsi_8btou64(cdb->addr);
8799 num_blocks = cdb->length;
8804 * We got a command we don't support. This shouldn't
8805 * happen, commands should be filtered out above us.
8807 ctl_set_invalid_opcode(ctsio);
8808 ctl_done((union ctl_io *)ctsio);
8810 return (CTL_RETVAL_COMPLETE);
8811 break; /* NOTREACHED */
8815 * The first check is to make sure we're in bounds, the second
8816 * check is to catch wrap-around problems. If the lba + num blocks
8817 * is less than the lba, then we've wrapped around and the block
8818 * range is invalid anyway.
8820 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8821 || ((lba + num_blocks) < lba)) {
8822 ctl_set_lba_out_of_range(ctsio);
8823 ctl_done((union ctl_io *)ctsio);
8824 return (CTL_RETVAL_COMPLETE);
8828 * According to SBC-3, a transfer length of 0 is not an error.
8830 if (num_blocks == 0) {
8831 ctl_set_success(ctsio);
8832 ctl_done((union ctl_io *)ctsio);
8833 return (CTL_RETVAL_COMPLETE);
8836 /* Set FUA if write cache is disabled. */
8837 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8839 flags |= CTL_LLF_FUA;
8841 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8842 ctsio->kern_rel_offset = 0;
8845 * Set the IO_CONT flag, so that if this I/O gets passed to
8846 * ctl_data_submit_done(), it'll get passed back to
8847 * ctl_ctl_cnw_cont() for further processing.
8849 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8850 ctsio->io_cont = ctl_cnw_cont;
8852 lbalen = (struct ctl_lba_len_flags *)
8853 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8855 lbalen->len = num_blocks;
8856 lbalen->flags = CTL_LLF_COMPARE | flags;
8858 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8859 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8864 ctl_verify(struct ctl_scsiio *ctsio)
8866 struct ctl_lun *lun;
8867 struct ctl_lba_len_flags *lbalen;
8869 uint32_t num_blocks;
8873 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8875 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8878 flags = CTL_LLF_FUA;
8879 switch (ctsio->cdb[0]) {
8881 struct scsi_verify_10 *cdb;
8883 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8884 if (cdb->byte2 & SVFY_BYTCHK)
8886 if (cdb->byte2 & SVFY_DPO)
8887 flags |= CTL_LLF_DPO;
8888 lba = scsi_4btoul(cdb->addr);
8889 num_blocks = scsi_2btoul(cdb->length);
8893 struct scsi_verify_12 *cdb;
8895 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8896 if (cdb->byte2 & SVFY_BYTCHK)
8898 if (cdb->byte2 & SVFY_DPO)
8899 flags |= CTL_LLF_DPO;
8900 lba = scsi_4btoul(cdb->addr);
8901 num_blocks = scsi_4btoul(cdb->length);
8905 struct scsi_rw_16 *cdb;
8907 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8908 if (cdb->byte2 & SVFY_BYTCHK)
8910 if (cdb->byte2 & SVFY_DPO)
8911 flags |= CTL_LLF_DPO;
8912 lba = scsi_8btou64(cdb->addr);
8913 num_blocks = scsi_4btoul(cdb->length);
8918 * We got a command we don't support. This shouldn't
8919 * happen, commands should be filtered out above us.
8921 ctl_set_invalid_opcode(ctsio);
8922 ctl_done((union ctl_io *)ctsio);
8923 return (CTL_RETVAL_COMPLETE);
8927 * The first check is to make sure we're in bounds, the second
8928 * check is to catch wrap-around problems. If the lba + num blocks
8929 * is less than the lba, then we've wrapped around and the block
8930 * range is invalid anyway.
8932 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8933 || ((lba + num_blocks) < lba)) {
8934 ctl_set_lba_out_of_range(ctsio);
8935 ctl_done((union ctl_io *)ctsio);
8936 return (CTL_RETVAL_COMPLETE);
8940 * According to SBC-3, a transfer length of 0 is not an error.
8942 if (num_blocks == 0) {
8943 ctl_set_success(ctsio);
8944 ctl_done((union ctl_io *)ctsio);
8945 return (CTL_RETVAL_COMPLETE);
8948 lbalen = (struct ctl_lba_len_flags *)
8949 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8951 lbalen->len = num_blocks;
8953 lbalen->flags = CTL_LLF_COMPARE | flags;
8954 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8956 lbalen->flags = CTL_LLF_VERIFY | flags;
8957 ctsio->kern_total_len = 0;
8959 ctsio->kern_rel_offset = 0;
8961 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8962 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8967 ctl_report_luns(struct ctl_scsiio *ctsio)
8969 struct ctl_softc *softc;
8970 struct scsi_report_luns *cdb;
8971 struct scsi_report_luns_data *lun_data;
8972 struct ctl_lun *lun, *request_lun;
8973 struct ctl_port *port;
8974 int num_luns, retval;
8975 uint32_t alloc_len, lun_datalen;
8977 uint32_t initidx, targ_lun_id, lun_id;
8979 retval = CTL_RETVAL_COMPLETE;
8980 cdb = (struct scsi_report_luns *)ctsio->cdb;
8981 port = ctl_io_port(&ctsio->io_hdr);
8982 softc = port->ctl_softc;
8984 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
8986 mtx_lock(&softc->ctl_lock);
8988 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
8989 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
8992 mtx_unlock(&softc->ctl_lock);
8994 switch (cdb->select_report) {
8995 case RPL_REPORT_DEFAULT:
8996 case RPL_REPORT_ALL:
8997 case RPL_REPORT_NONSUBSID:
8999 case RPL_REPORT_WELLKNOWN:
9000 case RPL_REPORT_ADMIN:
9001 case RPL_REPORT_CONGLOM:
9005 ctl_set_invalid_field(ctsio,
9011 ctl_done((union ctl_io *)ctsio);
9013 break; /* NOTREACHED */
9016 alloc_len = scsi_4btoul(cdb->length);
9018 * The initiator has to allocate at least 16 bytes for this request,
9019 * so he can at least get the header and the first LUN. Otherwise
9020 * we reject the request (per SPC-3 rev 14, section 6.21).
9022 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9023 sizeof(struct scsi_report_luns_lundata))) {
9024 ctl_set_invalid_field(ctsio,
9030 ctl_done((union ctl_io *)ctsio);
9034 request_lun = (struct ctl_lun *)
9035 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9037 lun_datalen = sizeof(*lun_data) +
9038 (num_luns * sizeof(struct scsi_report_luns_lundata));
9040 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9041 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9042 ctsio->kern_sg_entries = 0;
9044 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9046 mtx_lock(&softc->ctl_lock);
9047 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9048 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9049 if (lun_id >= CTL_MAX_LUNS)
9051 lun = softc->ctl_luns[lun_id];
9055 if (targ_lun_id <= 0xff) {
9057 * Peripheral addressing method, bus number 0.
9059 lun_data->luns[num_filled].lundata[0] =
9060 RPL_LUNDATA_ATYP_PERIPH;
9061 lun_data->luns[num_filled].lundata[1] = targ_lun_id;
9063 } else if (targ_lun_id <= 0x3fff) {
9065 * Flat addressing method.
9067 lun_data->luns[num_filled].lundata[0] =
9068 RPL_LUNDATA_ATYP_FLAT | (targ_lun_id >> 8);
9069 lun_data->luns[num_filled].lundata[1] =
9070 (targ_lun_id & 0xff);
9072 } else if (targ_lun_id <= 0xffffff) {
9074 * Extended flat addressing method.
9076 lun_data->luns[num_filled].lundata[0] =
9077 RPL_LUNDATA_ATYP_EXTLUN | 0x12;
9078 scsi_ulto3b(targ_lun_id,
9079 &lun_data->luns[num_filled].lundata[1]);
9082 printf("ctl_report_luns: bogus LUN number %jd, "
9083 "skipping\n", (intmax_t)targ_lun_id);
9086 * According to SPC-3, rev 14 section 6.21:
9088 * "The execution of a REPORT LUNS command to any valid and
9089 * installed logical unit shall clear the REPORTED LUNS DATA
9090 * HAS CHANGED unit attention condition for all logical
9091 * units of that target with respect to the requesting
9092 * initiator. A valid and installed logical unit is one
9093 * having a PERIPHERAL QUALIFIER of 000b in the standard
9094 * INQUIRY data (see 6.4.2)."
9096 * If request_lun is NULL, the LUN this report luns command
9097 * was issued to is either disabled or doesn't exist. In that
9098 * case, we shouldn't clear any pending lun change unit
9101 if (request_lun != NULL) {
9102 mtx_lock(&lun->lun_lock);
9103 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9104 mtx_unlock(&lun->lun_lock);
9107 mtx_unlock(&softc->ctl_lock);
9110 * It's quite possible that we've returned fewer LUNs than we allocated
9111 * space for. Trim it.
9113 lun_datalen = sizeof(*lun_data) +
9114 (num_filled * sizeof(struct scsi_report_luns_lundata));
9116 if (lun_datalen < alloc_len) {
9117 ctsio->residual = alloc_len - lun_datalen;
9118 ctsio->kern_data_len = lun_datalen;
9119 ctsio->kern_total_len = lun_datalen;
9121 ctsio->residual = 0;
9122 ctsio->kern_data_len = alloc_len;
9123 ctsio->kern_total_len = alloc_len;
9125 ctsio->kern_data_resid = 0;
9126 ctsio->kern_rel_offset = 0;
9127 ctsio->kern_sg_entries = 0;
9130 * We set this to the actual data length, regardless of how much
9131 * space we actually have to return results. If the user looks at
9132 * this value, he'll know whether or not he allocated enough space
9133 * and reissue the command if necessary. We don't support well
9134 * known logical units, so if the user asks for that, return none.
9136 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9139 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9142 ctl_set_success(ctsio);
9143 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9144 ctsio->be_move_done = ctl_config_move_done;
9145 ctl_datamove((union ctl_io *)ctsio);
9150 ctl_request_sense(struct ctl_scsiio *ctsio)
9152 struct scsi_request_sense *cdb;
9153 struct scsi_sense_data *sense_ptr;
9154 struct ctl_softc *ctl_softc;
9155 struct ctl_lun *lun;
9158 scsi_sense_data_type sense_format;
9159 ctl_ua_type ua_type;
9161 cdb = (struct scsi_request_sense *)ctsio->cdb;
9163 ctl_softc = control_softc;
9164 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9166 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9169 * Determine which sense format the user wants.
9171 if (cdb->byte2 & SRS_DESC)
9172 sense_format = SSD_TYPE_DESC;
9174 sense_format = SSD_TYPE_FIXED;
9176 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9177 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9178 ctsio->kern_sg_entries = 0;
9181 * struct scsi_sense_data, which is currently set to 256 bytes, is
9182 * larger than the largest allowed value for the length field in the
9183 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9185 ctsio->residual = 0;
9186 ctsio->kern_data_len = cdb->length;
9187 ctsio->kern_total_len = cdb->length;
9189 ctsio->kern_data_resid = 0;
9190 ctsio->kern_rel_offset = 0;
9191 ctsio->kern_sg_entries = 0;
9194 * If we don't have a LUN, we don't have any pending sense.
9200 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9202 * Check for pending sense, and then for pending unit attentions.
9203 * Pending sense gets returned first, then pending unit attentions.
9205 mtx_lock(&lun->lun_lock);
9207 if (ctl_is_set(lun->have_ca, initidx)) {
9208 scsi_sense_data_type stored_format;
9211 * Check to see which sense format was used for the stored
9214 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9217 * If the user requested a different sense format than the
9218 * one we stored, then we need to convert it to the other
9219 * format. If we're going from descriptor to fixed format
9220 * sense data, we may lose things in translation, depending
9221 * on what options were used.
9223 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9224 * for some reason we'll just copy it out as-is.
9226 if ((stored_format == SSD_TYPE_FIXED)
9227 && (sense_format == SSD_TYPE_DESC))
9228 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9229 &lun->pending_sense[initidx],
9230 (struct scsi_sense_data_desc *)sense_ptr);
9231 else if ((stored_format == SSD_TYPE_DESC)
9232 && (sense_format == SSD_TYPE_FIXED))
9233 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9234 &lun->pending_sense[initidx],
9235 (struct scsi_sense_data_fixed *)sense_ptr);
9237 memcpy(sense_ptr, &lun->pending_sense[initidx],
9238 MIN(sizeof(*sense_ptr),
9239 sizeof(lun->pending_sense[initidx])));
9241 ctl_clear_mask(lun->have_ca, initidx);
9246 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9247 if (ua_type != CTL_UA_NONE)
9249 if (ua_type == CTL_UA_LUN_CHANGE) {
9250 mtx_unlock(&lun->lun_lock);
9251 mtx_lock(&ctl_softc->ctl_lock);
9252 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9253 mtx_unlock(&ctl_softc->ctl_lock);
9254 mtx_lock(&lun->lun_lock);
9258 mtx_unlock(&lun->lun_lock);
9261 * We already have a pending error, return it.
9263 if (have_error != 0) {
9265 * We report the SCSI status as OK, since the status of the
9266 * request sense command itself is OK.
9267 * We report 0 for the sense length, because we aren't doing
9268 * autosense in this case. We're reporting sense as
9271 ctl_set_success(ctsio);
9272 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9273 ctsio->be_move_done = ctl_config_move_done;
9274 ctl_datamove((union ctl_io *)ctsio);
9275 return (CTL_RETVAL_COMPLETE);
9281 * No sense information to report, so we report that everything is
9284 ctl_set_sense_data(sense_ptr,
9287 /*current_error*/ 1,
9288 /*sense_key*/ SSD_KEY_NO_SENSE,
9294 * We report 0 for the sense length, because we aren't doing
9295 * autosense in this case. We're reporting sense as parameter data.
9297 ctl_set_success(ctsio);
9298 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9299 ctsio->be_move_done = ctl_config_move_done;
9300 ctl_datamove((union ctl_io *)ctsio);
9301 return (CTL_RETVAL_COMPLETE);
9305 ctl_tur(struct ctl_scsiio *ctsio)
9308 CTL_DEBUG_PRINT(("ctl_tur\n"));
9310 ctl_set_success(ctsio);
9311 ctl_done((union ctl_io *)ctsio);
9313 return (CTL_RETVAL_COMPLETE);
9317 * SCSI VPD page 0x00, the Supported VPD Pages page.
9320 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9322 struct scsi_vpd_supported_pages *pages;
9324 struct ctl_lun *lun;
9327 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9329 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9330 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9331 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9332 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9333 ctsio->kern_sg_entries = 0;
9335 if (sup_page_size < alloc_len) {
9336 ctsio->residual = alloc_len - sup_page_size;
9337 ctsio->kern_data_len = sup_page_size;
9338 ctsio->kern_total_len = sup_page_size;
9340 ctsio->residual = 0;
9341 ctsio->kern_data_len = alloc_len;
9342 ctsio->kern_total_len = alloc_len;
9344 ctsio->kern_data_resid = 0;
9345 ctsio->kern_rel_offset = 0;
9346 ctsio->kern_sg_entries = 0;
9349 * The control device is always connected. The disk device, on the
9350 * other hand, may not be online all the time. Need to change this
9351 * to figure out whether the disk device is actually online or not.
9354 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9355 lun->be_lun->lun_type;
9357 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9360 /* Supported VPD pages */
9361 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9363 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9364 /* Device Identification */
9365 pages->page_list[p++] = SVPD_DEVICE_ID;
9366 /* Extended INQUIRY Data */
9367 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9368 /* Mode Page Policy */
9369 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9371 pages->page_list[p++] = SVPD_SCSI_PORTS;
9372 /* Third-party Copy */
9373 pages->page_list[p++] = SVPD_SCSI_TPC;
9374 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9376 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9377 /* Block Device Characteristics */
9378 pages->page_list[p++] = SVPD_BDC;
9379 /* Logical Block Provisioning */
9380 pages->page_list[p++] = SVPD_LBP;
9384 ctl_set_success(ctsio);
9385 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9386 ctsio->be_move_done = ctl_config_move_done;
9387 ctl_datamove((union ctl_io *)ctsio);
9388 return (CTL_RETVAL_COMPLETE);
9392 * SCSI VPD page 0x80, the Unit Serial Number page.
9395 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9397 struct scsi_vpd_unit_serial_number *sn_ptr;
9398 struct ctl_lun *lun;
9401 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9403 data_len = 4 + CTL_SN_LEN;
9404 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9405 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9406 if (data_len < alloc_len) {
9407 ctsio->residual = alloc_len - data_len;
9408 ctsio->kern_data_len = data_len;
9409 ctsio->kern_total_len = data_len;
9411 ctsio->residual = 0;
9412 ctsio->kern_data_len = alloc_len;
9413 ctsio->kern_total_len = alloc_len;
9415 ctsio->kern_data_resid = 0;
9416 ctsio->kern_rel_offset = 0;
9417 ctsio->kern_sg_entries = 0;
9420 * The control device is always connected. The disk device, on the
9421 * other hand, may not be online all the time. Need to change this
9422 * to figure out whether the disk device is actually online or not.
9425 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9426 lun->be_lun->lun_type;
9428 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9430 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9431 sn_ptr->length = CTL_SN_LEN;
9433 * If we don't have a LUN, we just leave the serial number as
9437 strncpy((char *)sn_ptr->serial_num,
9438 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9440 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9442 ctl_set_success(ctsio);
9443 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9444 ctsio->be_move_done = ctl_config_move_done;
9445 ctl_datamove((union ctl_io *)ctsio);
9446 return (CTL_RETVAL_COMPLETE);
9451 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9454 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9456 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9457 struct ctl_lun *lun;
9460 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9462 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9463 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9464 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9465 ctsio->kern_sg_entries = 0;
9467 if (data_len < alloc_len) {
9468 ctsio->residual = alloc_len - data_len;
9469 ctsio->kern_data_len = data_len;
9470 ctsio->kern_total_len = data_len;
9472 ctsio->residual = 0;
9473 ctsio->kern_data_len = alloc_len;
9474 ctsio->kern_total_len = alloc_len;
9476 ctsio->kern_data_resid = 0;
9477 ctsio->kern_rel_offset = 0;
9478 ctsio->kern_sg_entries = 0;
9481 * The control device is always connected. The disk device, on the
9482 * other hand, may not be online all the time.
9485 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9486 lun->be_lun->lun_type;
9488 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9489 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9490 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9492 * We support head of queue, ordered and simple tags.
9494 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9496 * Volatile cache supported.
9498 eid_ptr->flags3 = SVPD_EID_V_SUP;
9501 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9502 * attention for a particular IT nexus on all LUNs once we report
9503 * it to that nexus once. This bit is required as of SPC-4.
9505 eid_ptr->flags4 = SVPD_EID_LUICLT;
9508 * XXX KDM in order to correctly answer this, we would need
9509 * information from the SIM to determine how much sense data it
9510 * can send. So this would really be a path inquiry field, most
9511 * likely. This can be set to a maximum of 252 according to SPC-4,
9512 * but the hardware may or may not be able to support that much.
9513 * 0 just means that the maximum sense data length is not reported.
9515 eid_ptr->max_sense_length = 0;
9517 ctl_set_success(ctsio);
9518 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9519 ctsio->be_move_done = ctl_config_move_done;
9520 ctl_datamove((union ctl_io *)ctsio);
9521 return (CTL_RETVAL_COMPLETE);
9525 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9527 struct scsi_vpd_mode_page_policy *mpp_ptr;
9528 struct ctl_lun *lun;
9531 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9533 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9534 sizeof(struct scsi_vpd_mode_page_policy_descr);
9536 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9537 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9538 ctsio->kern_sg_entries = 0;
9540 if (data_len < alloc_len) {
9541 ctsio->residual = alloc_len - data_len;
9542 ctsio->kern_data_len = data_len;
9543 ctsio->kern_total_len = data_len;
9545 ctsio->residual = 0;
9546 ctsio->kern_data_len = alloc_len;
9547 ctsio->kern_total_len = alloc_len;
9549 ctsio->kern_data_resid = 0;
9550 ctsio->kern_rel_offset = 0;
9551 ctsio->kern_sg_entries = 0;
9554 * The control device is always connected. The disk device, on the
9555 * other hand, may not be online all the time.
9558 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9559 lun->be_lun->lun_type;
9561 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9562 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9563 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9564 mpp_ptr->descr[0].page_code = 0x3f;
9565 mpp_ptr->descr[0].subpage_code = 0xff;
9566 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9568 ctl_set_success(ctsio);
9569 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9570 ctsio->be_move_done = ctl_config_move_done;
9571 ctl_datamove((union ctl_io *)ctsio);
9572 return (CTL_RETVAL_COMPLETE);
9576 * SCSI VPD page 0x83, the Device Identification page.
9579 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9581 struct scsi_vpd_device_id *devid_ptr;
9582 struct scsi_vpd_id_descriptor *desc;
9583 struct ctl_softc *softc;
9584 struct ctl_lun *lun;
9585 struct ctl_port *port;
9589 softc = control_softc;
9591 port = ctl_io_port(&ctsio->io_hdr);
9592 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9594 data_len = sizeof(struct scsi_vpd_device_id) +
9595 sizeof(struct scsi_vpd_id_descriptor) +
9596 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9597 sizeof(struct scsi_vpd_id_descriptor) +
9598 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9599 if (lun && lun->lun_devid)
9600 data_len += lun->lun_devid->len;
9601 if (port && port->port_devid)
9602 data_len += port->port_devid->len;
9603 if (port && port->target_devid)
9604 data_len += port->target_devid->len;
9606 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9607 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9608 ctsio->kern_sg_entries = 0;
9610 if (data_len < alloc_len) {
9611 ctsio->residual = alloc_len - data_len;
9612 ctsio->kern_data_len = data_len;
9613 ctsio->kern_total_len = data_len;
9615 ctsio->residual = 0;
9616 ctsio->kern_data_len = alloc_len;
9617 ctsio->kern_total_len = alloc_len;
9619 ctsio->kern_data_resid = 0;
9620 ctsio->kern_rel_offset = 0;
9621 ctsio->kern_sg_entries = 0;
9624 * The control device is always connected. The disk device, on the
9625 * other hand, may not be online all the time.
9628 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9629 lun->be_lun->lun_type;
9631 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9632 devid_ptr->page_code = SVPD_DEVICE_ID;
9633 scsi_ulto2b(data_len - 4, devid_ptr->length);
9635 if (port && port->port_type == CTL_PORT_FC)
9636 proto = SCSI_PROTO_FC << 4;
9637 else if (port && port->port_type == CTL_PORT_ISCSI)
9638 proto = SCSI_PROTO_ISCSI << 4;
9640 proto = SCSI_PROTO_SPI << 4;
9641 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9644 * We're using a LUN association here. i.e., this device ID is a
9645 * per-LUN identifier.
9647 if (lun && lun->lun_devid) {
9648 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9649 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9650 lun->lun_devid->len);
9654 * This is for the WWPN which is a port association.
9656 if (port && port->port_devid) {
9657 memcpy(desc, port->port_devid->data, port->port_devid->len);
9658 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9659 port->port_devid->len);
9663 * This is for the Relative Target Port(type 4h) identifier
9665 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9666 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9667 SVPD_ID_TYPE_RELTARG;
9669 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9670 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9671 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9674 * This is for the Target Port Group(type 5h) identifier
9676 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9677 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9678 SVPD_ID_TYPE_TPORTGRP;
9680 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port / softc->port_cnt + 1,
9681 &desc->identifier[2]);
9682 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9683 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9686 * This is for the Target identifier
9688 if (port && port->target_devid) {
9689 memcpy(desc, port->target_devid->data, port->target_devid->len);
9692 ctl_set_success(ctsio);
9693 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9694 ctsio->be_move_done = ctl_config_move_done;
9695 ctl_datamove((union ctl_io *)ctsio);
9696 return (CTL_RETVAL_COMPLETE);
9700 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9702 struct ctl_softc *softc = control_softc;
9703 struct scsi_vpd_scsi_ports *sp;
9704 struct scsi_vpd_port_designation *pd;
9705 struct scsi_vpd_port_designation_cont *pdc;
9706 struct ctl_lun *lun;
9707 struct ctl_port *port;
9708 int data_len, num_target_ports, iid_len, id_len;
9710 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9712 num_target_ports = 0;
9715 mtx_lock(&softc->ctl_lock);
9716 STAILQ_FOREACH(port, &softc->port_list, links) {
9717 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9720 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9723 if (port->init_devid)
9724 iid_len += port->init_devid->len;
9725 if (port->port_devid)
9726 id_len += port->port_devid->len;
9728 mtx_unlock(&softc->ctl_lock);
9730 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9731 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9732 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9733 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9734 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9735 ctsio->kern_sg_entries = 0;
9737 if (data_len < alloc_len) {
9738 ctsio->residual = alloc_len - data_len;
9739 ctsio->kern_data_len = data_len;
9740 ctsio->kern_total_len = data_len;
9742 ctsio->residual = 0;
9743 ctsio->kern_data_len = alloc_len;
9744 ctsio->kern_total_len = alloc_len;
9746 ctsio->kern_data_resid = 0;
9747 ctsio->kern_rel_offset = 0;
9748 ctsio->kern_sg_entries = 0;
9751 * The control device is always connected. The disk device, on the
9752 * other hand, may not be online all the time. Need to change this
9753 * to figure out whether the disk device is actually online or not.
9756 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9757 lun->be_lun->lun_type;
9759 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9761 sp->page_code = SVPD_SCSI_PORTS;
9762 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9764 pd = &sp->design[0];
9766 mtx_lock(&softc->ctl_lock);
9767 STAILQ_FOREACH(port, &softc->port_list, links) {
9768 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9771 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9773 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9774 if (port->init_devid) {
9775 iid_len = port->init_devid->len;
9776 memcpy(pd->initiator_transportid,
9777 port->init_devid->data, port->init_devid->len);
9780 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9781 pdc = (struct scsi_vpd_port_designation_cont *)
9782 (&pd->initiator_transportid[iid_len]);
9783 if (port->port_devid) {
9784 id_len = port->port_devid->len;
9785 memcpy(pdc->target_port_descriptors,
9786 port->port_devid->data, port->port_devid->len);
9789 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9790 pd = (struct scsi_vpd_port_designation *)
9791 ((uint8_t *)pdc->target_port_descriptors + id_len);
9793 mtx_unlock(&softc->ctl_lock);
9795 ctl_set_success(ctsio);
9796 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9797 ctsio->be_move_done = ctl_config_move_done;
9798 ctl_datamove((union ctl_io *)ctsio);
9799 return (CTL_RETVAL_COMPLETE);
9803 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9805 struct scsi_vpd_block_limits *bl_ptr;
9806 struct ctl_lun *lun;
9808 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9810 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9811 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9812 ctsio->kern_sg_entries = 0;
9814 if (sizeof(*bl_ptr) < alloc_len) {
9815 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9816 ctsio->kern_data_len = sizeof(*bl_ptr);
9817 ctsio->kern_total_len = sizeof(*bl_ptr);
9819 ctsio->residual = 0;
9820 ctsio->kern_data_len = alloc_len;
9821 ctsio->kern_total_len = alloc_len;
9823 ctsio->kern_data_resid = 0;
9824 ctsio->kern_rel_offset = 0;
9825 ctsio->kern_sg_entries = 0;
9828 * The control device is always connected. The disk device, on the
9829 * other hand, may not be online all the time. Need to change this
9830 * to figure out whether the disk device is actually online or not.
9833 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9834 lun->be_lun->lun_type;
9836 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9838 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9839 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9840 bl_ptr->max_cmp_write_len = 0xff;
9841 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9843 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9844 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9845 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9846 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9847 if (lun->be_lun->ublockexp != 0) {
9848 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9849 bl_ptr->opt_unmap_grain);
9850 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9851 bl_ptr->unmap_grain_align);
9854 scsi_ulto4b(lun->be_lun->atomicblock,
9855 bl_ptr->max_atomic_transfer_length);
9856 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9857 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9858 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9859 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9861 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9863 ctl_set_success(ctsio);
9864 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9865 ctsio->be_move_done = ctl_config_move_done;
9866 ctl_datamove((union ctl_io *)ctsio);
9867 return (CTL_RETVAL_COMPLETE);
9871 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9873 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9874 struct ctl_lun *lun;
9878 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9880 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9881 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9882 ctsio->kern_sg_entries = 0;
9884 if (sizeof(*bdc_ptr) < alloc_len) {
9885 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9886 ctsio->kern_data_len = sizeof(*bdc_ptr);
9887 ctsio->kern_total_len = sizeof(*bdc_ptr);
9889 ctsio->residual = 0;
9890 ctsio->kern_data_len = alloc_len;
9891 ctsio->kern_total_len = alloc_len;
9893 ctsio->kern_data_resid = 0;
9894 ctsio->kern_rel_offset = 0;
9895 ctsio->kern_sg_entries = 0;
9898 * The control device is always connected. The disk device, on the
9899 * other hand, may not be online all the time. Need to change this
9900 * to figure out whether the disk device is actually online or not.
9903 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9904 lun->be_lun->lun_type;
9906 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9907 bdc_ptr->page_code = SVPD_BDC;
9908 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9910 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9911 i = strtol(value, NULL, 0);
9913 i = CTL_DEFAULT_ROTATION_RATE;
9914 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9916 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9917 i = strtol(value, NULL, 0);
9920 bdc_ptr->wab_wac_ff = (i & 0x0f);
9921 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9923 ctl_set_success(ctsio);
9924 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9925 ctsio->be_move_done = ctl_config_move_done;
9926 ctl_datamove((union ctl_io *)ctsio);
9927 return (CTL_RETVAL_COMPLETE);
9931 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9933 struct scsi_vpd_logical_block_prov *lbp_ptr;
9934 struct ctl_lun *lun;
9936 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9938 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9939 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9940 ctsio->kern_sg_entries = 0;
9942 if (sizeof(*lbp_ptr) < alloc_len) {
9943 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
9944 ctsio->kern_data_len = sizeof(*lbp_ptr);
9945 ctsio->kern_total_len = sizeof(*lbp_ptr);
9947 ctsio->residual = 0;
9948 ctsio->kern_data_len = alloc_len;
9949 ctsio->kern_total_len = alloc_len;
9951 ctsio->kern_data_resid = 0;
9952 ctsio->kern_rel_offset = 0;
9953 ctsio->kern_sg_entries = 0;
9956 * The control device is always connected. The disk device, on the
9957 * other hand, may not be online all the time. Need to change this
9958 * to figure out whether the disk device is actually online or not.
9961 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9962 lun->be_lun->lun_type;
9964 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9966 lbp_ptr->page_code = SVPD_LBP;
9967 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9968 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9969 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9970 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9971 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9972 lbp_ptr->prov_type = SVPD_LBP_THIN;
9975 ctl_set_success(ctsio);
9976 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9977 ctsio->be_move_done = ctl_config_move_done;
9978 ctl_datamove((union ctl_io *)ctsio);
9979 return (CTL_RETVAL_COMPLETE);
9983 * INQUIRY with the EVPD bit set.
9986 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9988 struct ctl_lun *lun;
9989 struct scsi_inquiry *cdb;
9990 int alloc_len, retval;
9992 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9993 cdb = (struct scsi_inquiry *)ctsio->cdb;
9994 alloc_len = scsi_2btoul(cdb->length);
9996 switch (cdb->page_code) {
9997 case SVPD_SUPPORTED_PAGES:
9998 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10000 case SVPD_UNIT_SERIAL_NUMBER:
10001 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10003 case SVPD_DEVICE_ID:
10004 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10006 case SVPD_EXTENDED_INQUIRY_DATA:
10007 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10009 case SVPD_MODE_PAGE_POLICY:
10010 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10012 case SVPD_SCSI_PORTS:
10013 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10015 case SVPD_SCSI_TPC:
10016 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10018 case SVPD_BLOCK_LIMITS:
10019 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10021 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10024 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10026 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10029 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10031 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10035 ctl_set_invalid_field(ctsio,
10041 ctl_done((union ctl_io *)ctsio);
10042 retval = CTL_RETVAL_COMPLETE;
10050 * Standard INQUIRY data.
10053 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10055 struct scsi_inquiry_data *inq_ptr;
10056 struct scsi_inquiry *cdb;
10057 struct ctl_softc *softc = control_softc;
10058 struct ctl_port *port;
10059 struct ctl_lun *lun;
10061 uint32_t alloc_len, data_len;
10062 ctl_port_type port_type;
10064 port = ctl_io_port(&ctsio->io_hdr);
10065 port_type = port->port_type;
10066 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10067 port_type = CTL_PORT_SCSI;
10069 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10070 cdb = (struct scsi_inquiry *)ctsio->cdb;
10071 alloc_len = scsi_2btoul(cdb->length);
10074 * We malloc the full inquiry data size here and fill it
10075 * in. If the user only asks for less, we'll give him
10078 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10079 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10080 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10081 ctsio->kern_sg_entries = 0;
10082 ctsio->kern_data_resid = 0;
10083 ctsio->kern_rel_offset = 0;
10085 if (data_len < alloc_len) {
10086 ctsio->residual = alloc_len - data_len;
10087 ctsio->kern_data_len = data_len;
10088 ctsio->kern_total_len = data_len;
10090 ctsio->residual = 0;
10091 ctsio->kern_data_len = alloc_len;
10092 ctsio->kern_total_len = alloc_len;
10096 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10097 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10098 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10099 lun->be_lun->lun_type;
10101 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10102 lun->be_lun->lun_type;
10104 if (lun->flags & CTL_LUN_REMOVABLE)
10105 inq_ptr->dev_qual2 |= SID_RMB;
10107 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10109 /* RMB in byte 2 is 0 */
10110 inq_ptr->version = SCSI_REV_SPC4;
10113 * According to SAM-3, even if a device only supports a single
10114 * level of LUN addressing, it should still set the HISUP bit:
10116 * 4.9.1 Logical unit numbers overview
10118 * All logical unit number formats described in this standard are
10119 * hierarchical in structure even when only a single level in that
10120 * hierarchy is used. The HISUP bit shall be set to one in the
10121 * standard INQUIRY data (see SPC-2) when any logical unit number
10122 * format described in this standard is used. Non-hierarchical
10123 * formats are outside the scope of this standard.
10125 * Therefore we set the HiSup bit here.
10127 * The reponse format is 2, per SPC-3.
10129 inq_ptr->response_format = SID_HiSup | 2;
10131 inq_ptr->additional_length = data_len -
10132 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10133 CTL_DEBUG_PRINT(("additional_length = %d\n",
10134 inq_ptr->additional_length));
10136 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10137 if (port_type == CTL_PORT_SCSI)
10138 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10139 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10140 inq_ptr->flags = SID_CmdQue;
10141 if (port_type == CTL_PORT_SCSI)
10142 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10145 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10146 * We have 8 bytes for the vendor name, and 16 bytes for the device
10147 * name and 4 bytes for the revision.
10149 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10150 "vendor")) == NULL) {
10151 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10153 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10154 strncpy(inq_ptr->vendor, val,
10155 min(sizeof(inq_ptr->vendor), strlen(val)));
10158 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10159 sizeof(inq_ptr->product));
10160 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10161 switch (lun->be_lun->lun_type) {
10163 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10164 sizeof(inq_ptr->product));
10167 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10168 sizeof(inq_ptr->product));
10171 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10172 sizeof(inq_ptr->product));
10175 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10176 sizeof(inq_ptr->product));
10180 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10181 strncpy(inq_ptr->product, val,
10182 min(sizeof(inq_ptr->product), strlen(val)));
10186 * XXX make this a macro somewhere so it automatically gets
10187 * incremented when we make changes.
10189 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10190 "revision")) == NULL) {
10191 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10193 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10194 strncpy(inq_ptr->revision, val,
10195 min(sizeof(inq_ptr->revision), strlen(val)));
10199 * For parallel SCSI, we support double transition and single
10200 * transition clocking. We also support QAS (Quick Arbitration
10201 * and Selection) and Information Unit transfers on both the
10202 * control and array devices.
10204 if (port_type == CTL_PORT_SCSI)
10205 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10208 /* SAM-5 (no version claimed) */
10209 scsi_ulto2b(0x00A0, inq_ptr->version1);
10210 /* SPC-4 (no version claimed) */
10211 scsi_ulto2b(0x0460, inq_ptr->version2);
10212 if (port_type == CTL_PORT_FC) {
10213 /* FCP-2 ANSI INCITS.350:2003 */
10214 scsi_ulto2b(0x0917, inq_ptr->version3);
10215 } else if (port_type == CTL_PORT_SCSI) {
10216 /* SPI-4 ANSI INCITS.362:200x */
10217 scsi_ulto2b(0x0B56, inq_ptr->version3);
10218 } else if (port_type == CTL_PORT_ISCSI) {
10219 /* iSCSI (no version claimed) */
10220 scsi_ulto2b(0x0960, inq_ptr->version3);
10221 } else if (port_type == CTL_PORT_SAS) {
10222 /* SAS (no version claimed) */
10223 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10227 /* SBC-4 (no version claimed) */
10228 scsi_ulto2b(0x0600, inq_ptr->version4);
10230 switch (lun->be_lun->lun_type) {
10232 /* SBC-4 (no version claimed) */
10233 scsi_ulto2b(0x0600, inq_ptr->version4);
10238 /* MMC-6 (no version claimed) */
10239 scsi_ulto2b(0x04E0, inq_ptr->version4);
10246 ctl_set_success(ctsio);
10247 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10248 ctsio->be_move_done = ctl_config_move_done;
10249 ctl_datamove((union ctl_io *)ctsio);
10250 return (CTL_RETVAL_COMPLETE);
10254 ctl_inquiry(struct ctl_scsiio *ctsio)
10256 struct scsi_inquiry *cdb;
10259 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10261 cdb = (struct scsi_inquiry *)ctsio->cdb;
10262 if (cdb->byte2 & SI_EVPD)
10263 retval = ctl_inquiry_evpd(ctsio);
10264 else if (cdb->page_code == 0)
10265 retval = ctl_inquiry_std(ctsio);
10267 ctl_set_invalid_field(ctsio,
10273 ctl_done((union ctl_io *)ctsio);
10274 return (CTL_RETVAL_COMPLETE);
10281 ctl_get_config(struct ctl_scsiio *ctsio)
10283 struct scsi_get_config_header *hdr;
10284 struct scsi_get_config_feature *feature;
10285 struct scsi_get_config *cdb;
10286 struct ctl_lun *lun;
10287 uint32_t alloc_len, data_len;
10290 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10291 cdb = (struct scsi_get_config *)ctsio->cdb;
10292 rt = (cdb->rt & SGC_RT_MASK);
10293 starting = scsi_2btoul(cdb->starting_feature);
10294 alloc_len = scsi_2btoul(cdb->length);
10296 data_len = sizeof(struct scsi_get_config_header) +
10297 sizeof(struct scsi_get_config_feature) + 8 +
10298 sizeof(struct scsi_get_config_feature) + 8 +
10299 sizeof(struct scsi_get_config_feature) + 4 +
10300 sizeof(struct scsi_get_config_feature) + 4 +
10301 sizeof(struct scsi_get_config_feature) + 8 +
10302 sizeof(struct scsi_get_config_feature) +
10303 sizeof(struct scsi_get_config_feature) + 4 +
10304 sizeof(struct scsi_get_config_feature) + 4 +
10305 sizeof(struct scsi_get_config_feature) + 4 +
10306 sizeof(struct scsi_get_config_feature) + 4 +
10307 sizeof(struct scsi_get_config_feature) + 4 +
10308 sizeof(struct scsi_get_config_feature) + 4;
10309 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10310 ctsio->kern_sg_entries = 0;
10311 ctsio->kern_data_resid = 0;
10312 ctsio->kern_rel_offset = 0;
10314 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10315 if (lun->flags & CTL_LUN_NO_MEDIA)
10316 scsi_ulto2b(0x0000, hdr->current_profile);
10318 scsi_ulto2b(0x0010, hdr->current_profile);
10319 feature = (struct scsi_get_config_feature *)(hdr + 1);
10321 if (starting > 0x003b)
10323 if (starting > 0x003a)
10325 if (starting > 0x002b)
10327 if (starting > 0x002a)
10329 if (starting > 0x001f)
10331 if (starting > 0x001e)
10333 if (starting > 0x001d)
10335 if (starting > 0x0010)
10337 if (starting > 0x0003)
10339 if (starting > 0x0002)
10341 if (starting > 0x0001)
10343 if (starting > 0x0000)
10347 scsi_ulto2b(0x0000, feature->feature_code);
10348 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10349 feature->add_length = 8;
10350 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10351 feature->feature_data[2] = 0x00;
10352 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10353 feature->feature_data[6] = 0x01;
10354 feature = (struct scsi_get_config_feature *)
10355 &feature->feature_data[feature->add_length];
10358 scsi_ulto2b(0x0001, feature->feature_code);
10359 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10360 feature->add_length = 8;
10361 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10362 feature->feature_data[4] = 0x03;
10363 feature = (struct scsi_get_config_feature *)
10364 &feature->feature_data[feature->add_length];
10367 scsi_ulto2b(0x0002, feature->feature_code);
10368 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10369 feature->add_length = 4;
10370 feature->feature_data[0] = 0x02;
10371 feature = (struct scsi_get_config_feature *)
10372 &feature->feature_data[feature->add_length];
10374 f3: /* Removable Medium */
10375 scsi_ulto2b(0x0003, feature->feature_code);
10376 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10377 feature->add_length = 4;
10378 feature->feature_data[0] = 0x39;
10379 feature = (struct scsi_get_config_feature *)
10380 &feature->feature_data[feature->add_length];
10382 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10385 f10: /* Random Read */
10386 scsi_ulto2b(0x0010, feature->feature_code);
10387 feature->flags = 0x00;
10388 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10389 feature->flags |= SGC_F_CURRENT;
10390 feature->add_length = 8;
10391 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10392 scsi_ulto2b(1, &feature->feature_data[4]);
10393 feature->feature_data[6] = 0x00;
10394 feature = (struct scsi_get_config_feature *)
10395 &feature->feature_data[feature->add_length];
10397 f1d: /* Multi-Read */
10398 scsi_ulto2b(0x001D, feature->feature_code);
10399 feature->flags = 0x00;
10400 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10401 feature->flags |= SGC_F_CURRENT;
10402 feature->add_length = 0;
10403 feature = (struct scsi_get_config_feature *)
10404 &feature->feature_data[feature->add_length];
10407 scsi_ulto2b(0x001E, feature->feature_code);
10408 feature->flags = 0x00;
10409 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10410 feature->flags |= SGC_F_CURRENT;
10411 feature->add_length = 4;
10412 feature->feature_data[0] = 0x00;
10413 feature = (struct scsi_get_config_feature *)
10414 &feature->feature_data[feature->add_length];
10416 f1f: /* DVD Read */
10417 scsi_ulto2b(0x001F, feature->feature_code);
10418 feature->flags = 0x08;
10419 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10420 feature->flags |= SGC_F_CURRENT;
10421 feature->add_length = 4;
10422 feature->feature_data[0] = 0x01;
10423 feature->feature_data[2] = 0x03;
10424 feature = (struct scsi_get_config_feature *)
10425 &feature->feature_data[feature->add_length];
10428 scsi_ulto2b(0x002A, feature->feature_code);
10429 feature->flags = 0x04;
10430 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10431 feature->flags |= SGC_F_CURRENT;
10432 feature->add_length = 4;
10433 feature->feature_data[0] = 0x00;
10434 feature->feature_data[1] = 0x00;
10435 feature = (struct scsi_get_config_feature *)
10436 &feature->feature_data[feature->add_length];
10439 scsi_ulto2b(0x002B, feature->feature_code);
10440 feature->flags = 0x00;
10441 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10442 feature->flags |= SGC_F_CURRENT;
10443 feature->add_length = 4;
10444 feature->feature_data[0] = 0x00;
10445 feature = (struct scsi_get_config_feature *)
10446 &feature->feature_data[feature->add_length];
10448 f3a: /* DVD+RW Dual Layer */
10449 scsi_ulto2b(0x003A, feature->feature_code);
10450 feature->flags = 0x00;
10451 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10452 feature->flags |= SGC_F_CURRENT;
10453 feature->add_length = 4;
10454 feature->feature_data[0] = 0x00;
10455 feature->feature_data[1] = 0x00;
10456 feature = (struct scsi_get_config_feature *)
10457 &feature->feature_data[feature->add_length];
10459 f3b: /* DVD+R Dual Layer */
10460 scsi_ulto2b(0x003B, feature->feature_code);
10461 feature->flags = 0x00;
10462 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10463 feature->flags |= SGC_F_CURRENT;
10464 feature->add_length = 4;
10465 feature->feature_data[0] = 0x00;
10466 feature = (struct scsi_get_config_feature *)
10467 &feature->feature_data[feature->add_length];
10470 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10471 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10472 feature = (struct scsi_get_config_feature *)(hdr + 1);
10473 if (scsi_2btoul(feature->feature_code) == starting)
10474 feature = (struct scsi_get_config_feature *)
10475 &feature->feature_data[feature->add_length];
10476 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10478 scsi_ulto4b(data_len - 4, hdr->data_length);
10479 if (data_len < alloc_len) {
10480 ctsio->residual = alloc_len - data_len;
10481 ctsio->kern_data_len = data_len;
10482 ctsio->kern_total_len = data_len;
10484 ctsio->residual = 0;
10485 ctsio->kern_data_len = alloc_len;
10486 ctsio->kern_total_len = alloc_len;
10489 ctl_set_success(ctsio);
10490 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10491 ctsio->be_move_done = ctl_config_move_done;
10492 ctl_datamove((union ctl_io *)ctsio);
10493 return (CTL_RETVAL_COMPLETE);
10497 ctl_get_event_status(struct ctl_scsiio *ctsio)
10499 struct scsi_get_event_status_header *hdr;
10500 struct scsi_get_event_status *cdb;
10501 struct ctl_lun *lun;
10502 uint32_t alloc_len, data_len;
10505 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10506 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10507 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10508 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10509 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10510 ctl_done((union ctl_io *)ctsio);
10511 return (CTL_RETVAL_COMPLETE);
10513 notif_class = cdb->notif_class;
10514 alloc_len = scsi_2btoul(cdb->length);
10516 data_len = sizeof(struct scsi_get_event_status_header);
10517 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10518 ctsio->kern_sg_entries = 0;
10519 ctsio->kern_data_resid = 0;
10520 ctsio->kern_rel_offset = 0;
10522 if (data_len < alloc_len) {
10523 ctsio->residual = alloc_len - data_len;
10524 ctsio->kern_data_len = data_len;
10525 ctsio->kern_total_len = data_len;
10527 ctsio->residual = 0;
10528 ctsio->kern_data_len = alloc_len;
10529 ctsio->kern_total_len = alloc_len;
10532 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10533 scsi_ulto2b(0, hdr->descr_length);
10534 hdr->nea_class = SGESN_NEA;
10535 hdr->supported_class = 0;
10537 ctl_set_success(ctsio);
10538 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10539 ctsio->be_move_done = ctl_config_move_done;
10540 ctl_datamove((union ctl_io *)ctsio);
10541 return (CTL_RETVAL_COMPLETE);
10545 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10547 struct scsi_mechanism_status_header *hdr;
10548 struct scsi_mechanism_status *cdb;
10549 struct ctl_lun *lun;
10550 uint32_t alloc_len, data_len;
10552 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10553 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10554 alloc_len = scsi_2btoul(cdb->length);
10556 data_len = sizeof(struct scsi_mechanism_status_header);
10557 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10558 ctsio->kern_sg_entries = 0;
10559 ctsio->kern_data_resid = 0;
10560 ctsio->kern_rel_offset = 0;
10562 if (data_len < alloc_len) {
10563 ctsio->residual = alloc_len - data_len;
10564 ctsio->kern_data_len = data_len;
10565 ctsio->kern_total_len = data_len;
10567 ctsio->residual = 0;
10568 ctsio->kern_data_len = alloc_len;
10569 ctsio->kern_total_len = alloc_len;
10572 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10573 hdr->state1 = 0x00;
10574 hdr->state2 = 0xe0;
10575 scsi_ulto3b(0, hdr->lba);
10576 hdr->slots_num = 0;
10577 scsi_ulto2b(0, hdr->slots_length);
10579 ctl_set_success(ctsio);
10580 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10581 ctsio->be_move_done = ctl_config_move_done;
10582 ctl_datamove((union ctl_io *)ctsio);
10583 return (CTL_RETVAL_COMPLETE);
10587 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10592 buf[1] = bin2bcd((lba / 75) / 60);
10593 buf[2] = bin2bcd((lba / 75) % 60);
10594 buf[3] = bin2bcd(lba % 75);
10598 ctl_read_toc(struct ctl_scsiio *ctsio)
10600 struct scsi_read_toc_hdr *hdr;
10601 struct scsi_read_toc_type01_descr *descr;
10602 struct scsi_read_toc *cdb;
10603 struct ctl_lun *lun;
10604 uint32_t alloc_len, data_len;
10607 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10608 cdb = (struct scsi_read_toc *)ctsio->cdb;
10609 msf = (cdb->byte2 & CD_MSF) != 0;
10610 format = cdb->format;
10611 alloc_len = scsi_2btoul(cdb->data_len);
10613 data_len = sizeof(struct scsi_read_toc_hdr);
10615 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10617 data_len += sizeof(struct scsi_read_toc_type01_descr);
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_read_toc_hdr *)ctsio->kern_data_ptr;
10635 scsi_ulto2b(0x12, hdr->data_length);
10638 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10639 descr->addr_ctl = 0x14;
10640 descr->track_number = 1;
10642 ctl_ultomsf(0, descr->track_start);
10644 scsi_ulto4b(0, descr->track_start);
10646 descr->addr_ctl = 0x14;
10647 descr->track_number = 0xaa;
10649 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10651 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10653 scsi_ulto2b(0x0a, hdr->data_length);
10656 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10657 descr->addr_ctl = 0x14;
10658 descr->track_number = 1;
10660 ctl_ultomsf(0, descr->track_start);
10662 scsi_ulto4b(0, descr->track_start);
10665 ctl_set_success(ctsio);
10666 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10667 ctsio->be_move_done = ctl_config_move_done;
10668 ctl_datamove((union ctl_io *)ctsio);
10669 return (CTL_RETVAL_COMPLETE);
10673 * For known CDB types, parse the LBA and length.
10676 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10678 if (io->io_hdr.io_type != CTL_IO_SCSI)
10681 switch (io->scsiio.cdb[0]) {
10682 case COMPARE_AND_WRITE: {
10683 struct scsi_compare_and_write *cdb;
10685 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10687 *lba = scsi_8btou64(cdb->addr);
10688 *len = cdb->length;
10693 struct scsi_rw_6 *cdb;
10695 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10697 *lba = scsi_3btoul(cdb->addr);
10698 /* only 5 bits are valid in the most significant address byte */
10700 *len = cdb->length;
10705 struct scsi_rw_10 *cdb;
10707 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10709 *lba = scsi_4btoul(cdb->addr);
10710 *len = scsi_2btoul(cdb->length);
10713 case WRITE_VERIFY_10: {
10714 struct scsi_write_verify_10 *cdb;
10716 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10718 *lba = scsi_4btoul(cdb->addr);
10719 *len = scsi_2btoul(cdb->length);
10724 struct scsi_rw_12 *cdb;
10726 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10728 *lba = scsi_4btoul(cdb->addr);
10729 *len = scsi_4btoul(cdb->length);
10732 case WRITE_VERIFY_12: {
10733 struct scsi_write_verify_12 *cdb;
10735 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10737 *lba = scsi_4btoul(cdb->addr);
10738 *len = scsi_4btoul(cdb->length);
10743 struct scsi_rw_16 *cdb;
10745 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10747 *lba = scsi_8btou64(cdb->addr);
10748 *len = scsi_4btoul(cdb->length);
10751 case WRITE_ATOMIC_16: {
10752 struct scsi_write_atomic_16 *cdb;
10754 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10756 *lba = scsi_8btou64(cdb->addr);
10757 *len = scsi_2btoul(cdb->length);
10760 case WRITE_VERIFY_16: {
10761 struct scsi_write_verify_16 *cdb;
10763 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10765 *lba = scsi_8btou64(cdb->addr);
10766 *len = scsi_4btoul(cdb->length);
10769 case WRITE_SAME_10: {
10770 struct scsi_write_same_10 *cdb;
10772 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10774 *lba = scsi_4btoul(cdb->addr);
10775 *len = scsi_2btoul(cdb->length);
10778 case WRITE_SAME_16: {
10779 struct scsi_write_same_16 *cdb;
10781 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10783 *lba = scsi_8btou64(cdb->addr);
10784 *len = scsi_4btoul(cdb->length);
10788 struct scsi_verify_10 *cdb;
10790 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10792 *lba = scsi_4btoul(cdb->addr);
10793 *len = scsi_2btoul(cdb->length);
10797 struct scsi_verify_12 *cdb;
10799 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10801 *lba = scsi_4btoul(cdb->addr);
10802 *len = scsi_4btoul(cdb->length);
10806 struct scsi_verify_16 *cdb;
10808 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10810 *lba = scsi_8btou64(cdb->addr);
10811 *len = scsi_4btoul(cdb->length);
10819 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10820 struct scsi_get_lba_status *cdb;
10822 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10823 *lba = scsi_8btou64(cdb->addr);
10829 break; /* NOTREACHED */
10836 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10839 uint64_t endlba1, endlba2;
10841 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10842 endlba2 = lba2 + len2 - 1;
10844 if ((endlba1 < lba2) || (endlba2 < lba1))
10845 return (CTL_ACTION_PASS);
10847 return (CTL_ACTION_BLOCK);
10851 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10853 struct ctl_ptr_len_flags *ptrlen;
10854 struct scsi_unmap_desc *buf, *end, *range;
10858 /* If not UNMAP -- go other way. */
10859 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10860 io->scsiio.cdb[0] != UNMAP)
10861 return (CTL_ACTION_ERROR);
10863 /* If UNMAP without data -- block and wait for data. */
10864 ptrlen = (struct ctl_ptr_len_flags *)
10865 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10866 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10867 ptrlen->ptr == NULL)
10868 return (CTL_ACTION_BLOCK);
10870 /* UNMAP with data -- check for collision. */
10871 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10872 end = buf + ptrlen->len / sizeof(*buf);
10873 for (range = buf; range < end; range++) {
10874 lba = scsi_8btou64(range->lba);
10875 len = scsi_4btoul(range->length);
10876 if ((lba < lba2 + len2) && (lba + len > lba2))
10877 return (CTL_ACTION_BLOCK);
10879 return (CTL_ACTION_PASS);
10883 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10885 uint64_t lba1, lba2;
10886 uint64_t len1, len2;
10889 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10890 return (CTL_ACTION_ERROR);
10892 retval = ctl_extent_check_unmap(io1, lba2, len2);
10893 if (retval != CTL_ACTION_ERROR)
10896 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10897 return (CTL_ACTION_ERROR);
10899 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10901 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10905 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10907 uint64_t lba1, lba2;
10908 uint64_t len1, len2;
10910 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10911 return (CTL_ACTION_PASS);
10912 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10913 return (CTL_ACTION_ERROR);
10914 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10915 return (CTL_ACTION_ERROR);
10917 if (lba1 + len1 == lba2)
10918 return (CTL_ACTION_BLOCK);
10919 return (CTL_ACTION_PASS);
10923 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10924 union ctl_io *ooa_io)
10926 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10927 const ctl_serialize_action *serialize_row;
10930 * The initiator attempted multiple untagged commands at the same
10931 * time. Can't do that.
10933 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10934 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10935 && ((pending_io->io_hdr.nexus.targ_port ==
10936 ooa_io->io_hdr.nexus.targ_port)
10937 && (pending_io->io_hdr.nexus.initid ==
10938 ooa_io->io_hdr.nexus.initid))
10939 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10940 CTL_FLAG_STATUS_SENT)) == 0))
10941 return (CTL_ACTION_OVERLAP);
10944 * The initiator attempted to send multiple tagged commands with
10945 * the same ID. (It's fine if different initiators have the same
10948 * Even if all of those conditions are true, we don't kill the I/O
10949 * if the command ahead of us has been aborted. We won't end up
10950 * sending it to the FETD, and it's perfectly legal to resend a
10951 * command with the same tag number as long as the previous
10952 * instance of this tag number has been aborted somehow.
10954 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10955 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10956 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10957 && ((pending_io->io_hdr.nexus.targ_port ==
10958 ooa_io->io_hdr.nexus.targ_port)
10959 && (pending_io->io_hdr.nexus.initid ==
10960 ooa_io->io_hdr.nexus.initid))
10961 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10962 CTL_FLAG_STATUS_SENT)) == 0))
10963 return (CTL_ACTION_OVERLAP_TAG);
10966 * If we get a head of queue tag, SAM-3 says that we should
10967 * immediately execute it.
10969 * What happens if this command would normally block for some other
10970 * reason? e.g. a request sense with a head of queue tag
10971 * immediately after a write. Normally that would block, but this
10972 * will result in its getting executed immediately...
10974 * We currently return "pass" instead of "skip", so we'll end up
10975 * going through the rest of the queue to check for overlapped tags.
10977 * XXX KDM check for other types of blockage first??
10979 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10980 return (CTL_ACTION_PASS);
10983 * Ordered tags have to block until all items ahead of them
10984 * have completed. If we get called with an ordered tag, we always
10985 * block, if something else is ahead of us in the queue.
10987 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10988 return (CTL_ACTION_BLOCK);
10991 * Simple tags get blocked until all head of queue and ordered tags
10992 * ahead of them have completed. I'm lumping untagged commands in
10993 * with simple tags here. XXX KDM is that the right thing to do?
10995 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10996 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10997 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10998 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10999 return (CTL_ACTION_BLOCK);
11001 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11002 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11004 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11006 switch (serialize_row[pending_entry->seridx]) {
11007 case CTL_SER_BLOCK:
11008 return (CTL_ACTION_BLOCK);
11009 case CTL_SER_EXTENT:
11010 return (ctl_extent_check(ooa_io, pending_io,
11011 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11012 case CTL_SER_EXTENTOPT:
11013 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11014 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11015 return (ctl_extent_check(ooa_io, pending_io,
11017 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11018 return (CTL_ACTION_PASS);
11019 case CTL_SER_EXTENTSEQ:
11020 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11021 return (ctl_extent_check_seq(ooa_io, pending_io));
11022 return (CTL_ACTION_PASS);
11024 return (CTL_ACTION_PASS);
11025 case CTL_SER_BLOCKOPT:
11026 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11027 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11028 return (CTL_ACTION_BLOCK);
11029 return (CTL_ACTION_PASS);
11031 return (CTL_ACTION_SKIP);
11033 panic("invalid serialization value %d",
11034 serialize_row[pending_entry->seridx]);
11037 return (CTL_ACTION_ERROR);
11041 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11043 * - pending_io is generally either incoming, or on the blocked queue
11044 * - starting I/O is the I/O we want to start the check with.
11047 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11048 union ctl_io *starting_io)
11050 union ctl_io *ooa_io;
11053 mtx_assert(&lun->lun_lock, MA_OWNED);
11056 * Run back along the OOA queue, starting with the current
11057 * blocked I/O and going through every I/O before it on the
11058 * queue. If starting_io is NULL, we'll just end up returning
11061 for (ooa_io = starting_io; ooa_io != NULL;
11062 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11066 * This routine just checks to see whether
11067 * cur_blocked is blocked by ooa_io, which is ahead
11068 * of it in the queue. It doesn't queue/dequeue
11071 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11073 case CTL_ACTION_BLOCK:
11074 case CTL_ACTION_OVERLAP:
11075 case CTL_ACTION_OVERLAP_TAG:
11076 case CTL_ACTION_SKIP:
11077 case CTL_ACTION_ERROR:
11079 break; /* NOTREACHED */
11080 case CTL_ACTION_PASS:
11083 panic("invalid action %d", action);
11084 break; /* NOTREACHED */
11088 return (CTL_ACTION_PASS);
11093 * - An I/O has just completed, and has been removed from the per-LUN OOA
11094 * queue, so some items on the blocked queue may now be unblocked.
11097 ctl_check_blocked(struct ctl_lun *lun)
11099 struct ctl_softc *softc = lun->ctl_softc;
11100 union ctl_io *cur_blocked, *next_blocked;
11102 mtx_assert(&lun->lun_lock, MA_OWNED);
11105 * Run forward from the head of the blocked queue, checking each
11106 * entry against the I/Os prior to it on the OOA queue to see if
11107 * there is still any blockage.
11109 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11110 * with our removing a variable on it while it is traversing the
11113 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11114 cur_blocked != NULL; cur_blocked = next_blocked) {
11115 union ctl_io *prev_ooa;
11118 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11121 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11122 ctl_ooaq, ooa_links);
11125 * If cur_blocked happens to be the first item in the OOA
11126 * queue now, prev_ooa will be NULL, and the action
11127 * returned will just be CTL_ACTION_PASS.
11129 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11132 case CTL_ACTION_BLOCK:
11133 /* Nothing to do here, still blocked */
11135 case CTL_ACTION_OVERLAP:
11136 case CTL_ACTION_OVERLAP_TAG:
11138 * This shouldn't happen! In theory we've already
11139 * checked this command for overlap...
11142 case CTL_ACTION_PASS:
11143 case CTL_ACTION_SKIP: {
11144 const struct ctl_cmd_entry *entry;
11147 * The skip case shouldn't happen, this transaction
11148 * should have never made it onto the blocked queue.
11151 * This I/O is no longer blocked, we can remove it
11152 * from the blocked queue. Since this is a TAILQ
11153 * (doubly linked list), we can do O(1) removals
11154 * from any place on the list.
11156 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11158 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11160 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11161 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11163 * Need to send IO back to original side to
11166 union ctl_ha_msg msg_info;
11168 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11169 msg_info.hdr.original_sc =
11170 cur_blocked->io_hdr.original_sc;
11171 msg_info.hdr.serializing_sc = cur_blocked;
11172 msg_info.hdr.msg_type = CTL_MSG_R2R;
11173 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11174 sizeof(msg_info.hdr), M_NOWAIT);
11177 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11180 * Check this I/O for LUN state changes that may
11181 * have happened while this command was blocked.
11182 * The LUN state may have been changed by a command
11183 * ahead of us in the queue, so we need to re-check
11184 * for any states that can be caused by SCSI
11187 if (ctl_scsiio_lun_check(lun, entry,
11188 &cur_blocked->scsiio) == 0) {
11189 cur_blocked->io_hdr.flags |=
11190 CTL_FLAG_IS_WAS_ON_RTR;
11191 ctl_enqueue_rtr(cur_blocked);
11193 ctl_done(cur_blocked);
11198 * This probably shouldn't happen -- we shouldn't
11199 * get CTL_ACTION_ERROR, or anything else.
11205 return (CTL_RETVAL_COMPLETE);
11209 * This routine (with one exception) checks LUN flags that can be set by
11210 * commands ahead of us in the OOA queue. These flags have to be checked
11211 * when a command initially comes in, and when we pull a command off the
11212 * blocked queue and are preparing to execute it. The reason we have to
11213 * check these flags for commands on the blocked queue is that the LUN
11214 * state may have been changed by a command ahead of us while we're on the
11217 * Ordering is somewhat important with these checks, so please pay
11218 * careful attention to the placement of any new checks.
11221 ctl_scsiio_lun_check(struct ctl_lun *lun,
11222 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11224 struct ctl_softc *softc = lun->ctl_softc;
11230 mtx_assert(&lun->lun_lock, MA_OWNED);
11233 * If this shelf is a secondary shelf controller, we may have to
11234 * reject some commands disallowed by HA mode and link state.
11236 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11237 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11238 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11239 ctl_set_lun_unavail(ctsio);
11243 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11244 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11245 ctl_set_lun_transit(ctsio);
11249 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11250 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11251 ctl_set_lun_standby(ctsio);
11256 /* The rest of checks are only done on executing side */
11257 if (softc->ha_mode == CTL_HA_MODE_XFER)
11261 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11263 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11264 ctl_set_hw_write_protected(ctsio);
11268 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11269 .eca_and_aen & SCP_SWP) != 0) {
11270 ctl_set_sense(ctsio, /*current_error*/ 1,
11271 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11272 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11279 * Check for a reservation conflict. If this command isn't allowed
11280 * even on reserved LUNs, and if this initiator isn't the one who
11281 * reserved us, reject the command with a reservation conflict.
11283 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11284 if ((lun->flags & CTL_LUN_RESERVED)
11285 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11286 if (lun->res_idx != residx) {
11287 ctl_set_reservation_conflict(ctsio);
11293 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11294 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11295 /* No reservation or command is allowed. */;
11296 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11297 (lun->res_type == SPR_TYPE_WR_EX ||
11298 lun->res_type == SPR_TYPE_WR_EX_RO ||
11299 lun->res_type == SPR_TYPE_WR_EX_AR)) {
11300 /* The command is allowed for Write Exclusive resv. */;
11303 * if we aren't registered or it's a res holder type
11304 * reservation and this isn't the res holder then set a
11307 if (ctl_get_prkey(lun, residx) == 0
11308 || (residx != lun->pr_res_idx && lun->res_type < 4)) {
11309 ctl_set_reservation_conflict(ctsio);
11315 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11316 if (lun->flags & CTL_LUN_EJECTED)
11317 ctl_set_lun_ejected(ctsio);
11318 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11319 if (lun->flags & CTL_LUN_REMOVABLE)
11320 ctl_set_lun_no_media(ctsio);
11322 ctl_set_lun_int_reqd(ctsio);
11323 } else if (lun->flags & CTL_LUN_STOPPED)
11324 ctl_set_lun_stopped(ctsio);
11336 ctl_failover_io(union ctl_io *io, int have_lock)
11338 ctl_set_busy(&io->scsiio);
11343 ctl_failover_lun(union ctl_io *rio)
11345 struct ctl_softc *softc = control_softc;
11346 struct ctl_lun *lun;
11347 struct ctl_io_hdr *io, *next_io;
11350 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11351 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11353 /* Find and lock the LUN. */
11354 mtx_lock(&softc->ctl_lock);
11355 if ((targ_lun < CTL_MAX_LUNS) &&
11356 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11357 mtx_lock(&lun->lun_lock);
11358 mtx_unlock(&softc->ctl_lock);
11359 if (lun->flags & CTL_LUN_DISABLED) {
11360 mtx_unlock(&lun->lun_lock);
11364 mtx_unlock(&softc->ctl_lock);
11368 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11369 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11370 /* We are master */
11371 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11372 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11373 io->flags |= CTL_FLAG_ABORT;
11374 io->flags |= CTL_FLAG_FAILOVER;
11375 } else { /* This can be only due to DATAMOVE */
11376 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11377 io->flags &= ~CTL_FLAG_DMA_INPROG;
11378 io->flags |= CTL_FLAG_IO_ACTIVE;
11379 io->port_status = 31340;
11380 ctl_enqueue_isc((union ctl_io *)io);
11384 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11385 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11386 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11387 io->flags |= CTL_FLAG_FAILOVER;
11389 ctl_set_busy(&((union ctl_io *)io)->
11391 ctl_done((union ctl_io *)io);
11395 } else { /* SERIALIZE modes */
11396 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11398 /* We are master */
11399 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11400 TAILQ_REMOVE(&lun->blocked_queue, io,
11402 io->flags &= ~CTL_FLAG_BLOCKED;
11403 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11404 ctl_free_io((union ctl_io *)io);
11407 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11408 /* We are master */
11409 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11410 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11411 ctl_free_io((union ctl_io *)io);
11414 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11415 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11416 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11417 ctl_set_busy(&((union ctl_io *)io)->
11419 ctl_done((union ctl_io *)io);
11423 ctl_check_blocked(lun);
11425 mtx_unlock(&lun->lun_lock);
11429 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11431 struct ctl_lun *lun;
11432 const struct ctl_cmd_entry *entry;
11433 uint32_t initidx, targ_lun;
11440 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11441 if ((targ_lun < CTL_MAX_LUNS)
11442 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11444 * If the LUN is invalid, pretend that it doesn't exist.
11445 * It will go away as soon as all pending I/O has been
11448 mtx_lock(&lun->lun_lock);
11449 if (lun->flags & CTL_LUN_DISABLED) {
11450 mtx_unlock(&lun->lun_lock);
11452 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11453 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11455 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11456 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11460 * Every I/O goes into the OOA queue for a
11461 * particular LUN, and stays there until completion.
11464 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11465 lun->idle_time += getsbinuptime() -
11469 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11473 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11474 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11477 /* Get command entry and return error if it is unsuppotyed. */
11478 entry = ctl_validate_command(ctsio);
11479 if (entry == NULL) {
11481 mtx_unlock(&lun->lun_lock);
11485 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11486 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11489 * Check to see whether we can send this command to LUNs that don't
11490 * exist. This should pretty much only be the case for inquiry
11491 * and request sense. Further checks, below, really require having
11492 * a LUN, so we can't really check the command anymore. Just put
11493 * it on the rtr queue.
11496 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11497 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11498 ctl_enqueue_rtr((union ctl_io *)ctsio);
11502 ctl_set_unsupported_lun(ctsio);
11503 ctl_done((union ctl_io *)ctsio);
11504 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11508 * Make sure we support this particular command on this LUN.
11509 * e.g., we don't support writes to the control LUN.
11511 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11512 mtx_unlock(&lun->lun_lock);
11513 ctl_set_invalid_opcode(ctsio);
11514 ctl_done((union ctl_io *)ctsio);
11519 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11523 * If we've got a request sense, it'll clear the contingent
11524 * allegiance condition. Otherwise, if we have a CA condition for
11525 * this initiator, clear it, because it sent down a command other
11526 * than request sense.
11528 if ((ctsio->cdb[0] != REQUEST_SENSE)
11529 && (ctl_is_set(lun->have_ca, initidx)))
11530 ctl_clear_mask(lun->have_ca, initidx);
11534 * If the command has this flag set, it handles its own unit
11535 * attention reporting, we shouldn't do anything. Otherwise we
11536 * check for any pending unit attentions, and send them back to the
11537 * initiator. We only do this when a command initially comes in,
11538 * not when we pull it off the blocked queue.
11540 * According to SAM-3, section 5.3.2, the order that things get
11541 * presented back to the host is basically unit attentions caused
11542 * by some sort of reset event, busy status, reservation conflicts
11543 * or task set full, and finally any other status.
11545 * One issue here is that some of the unit attentions we report
11546 * don't fall into the "reset" category (e.g. "reported luns data
11547 * has changed"). So reporting it here, before the reservation
11548 * check, may be technically wrong. I guess the only thing to do
11549 * would be to check for and report the reset events here, and then
11550 * check for the other unit attention types after we check for a
11551 * reservation conflict.
11553 * XXX KDM need to fix this
11555 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11556 ctl_ua_type ua_type;
11558 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11560 if (ua_type != CTL_UA_NONE) {
11561 mtx_unlock(&lun->lun_lock);
11562 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11563 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11564 ctsio->sense_len = SSD_FULL_SIZE;
11565 ctl_done((union ctl_io *)ctsio);
11571 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11572 mtx_unlock(&lun->lun_lock);
11573 ctl_done((union ctl_io *)ctsio);
11578 * XXX CHD this is where we want to send IO to other side if
11579 * this LUN is secondary on this SC. We will need to make a copy
11580 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11581 * the copy we send as FROM_OTHER.
11582 * We also need to stuff the address of the original IO so we can
11583 * find it easily. Something similar will need be done on the other
11584 * side so when we are done we can find the copy.
11586 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11587 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11588 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11589 union ctl_ha_msg msg_info;
11592 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11593 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11594 mtx_unlock(&lun->lun_lock);
11596 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11597 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11598 msg_info.hdr.serializing_sc = NULL;
11599 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11600 msg_info.scsi.tag_num = ctsio->tag_num;
11601 msg_info.scsi.tag_type = ctsio->tag_type;
11602 msg_info.scsi.cdb_len = ctsio->cdb_len;
11603 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11605 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11606 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11607 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11608 ctl_set_busy(ctsio);
11609 ctl_done((union ctl_io *)ctsio);
11615 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11616 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11617 ctl_ooaq, ooa_links))) {
11618 case CTL_ACTION_BLOCK:
11619 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11620 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11622 mtx_unlock(&lun->lun_lock);
11624 case CTL_ACTION_PASS:
11625 case CTL_ACTION_SKIP:
11626 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11627 mtx_unlock(&lun->lun_lock);
11628 ctl_enqueue_rtr((union ctl_io *)ctsio);
11630 case CTL_ACTION_OVERLAP:
11631 mtx_unlock(&lun->lun_lock);
11632 ctl_set_overlapped_cmd(ctsio);
11633 ctl_done((union ctl_io *)ctsio);
11635 case CTL_ACTION_OVERLAP_TAG:
11636 mtx_unlock(&lun->lun_lock);
11637 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11638 ctl_done((union ctl_io *)ctsio);
11640 case CTL_ACTION_ERROR:
11642 mtx_unlock(&lun->lun_lock);
11643 ctl_set_internal_failure(ctsio,
11645 /*retry_count*/ 0);
11646 ctl_done((union ctl_io *)ctsio);
11652 const struct ctl_cmd_entry *
11653 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11655 const struct ctl_cmd_entry *entry;
11656 int service_action;
11658 entry = &ctl_cmd_table[ctsio->cdb[0]];
11660 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11661 if (entry->flags & CTL_CMD_FLAG_SA5) {
11662 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11663 entry = &((const struct ctl_cmd_entry *)
11664 entry->execute)[service_action];
11669 const struct ctl_cmd_entry *
11670 ctl_validate_command(struct ctl_scsiio *ctsio)
11672 const struct ctl_cmd_entry *entry;
11676 entry = ctl_get_cmd_entry(ctsio, &sa);
11677 if (entry->execute == NULL) {
11679 ctl_set_invalid_field(ctsio,
11686 ctl_set_invalid_opcode(ctsio);
11687 ctl_done((union ctl_io *)ctsio);
11690 KASSERT(entry->length > 0,
11691 ("Not defined length for command 0x%02x/0x%02x",
11692 ctsio->cdb[0], ctsio->cdb[1]));
11693 for (i = 1; i < entry->length; i++) {
11694 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11697 ctl_set_invalid_field(ctsio,
11702 /*bit*/ fls(diff) - 1);
11703 ctl_done((union ctl_io *)ctsio);
11710 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11713 switch (lun_type) {
11715 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11719 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11723 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11733 ctl_scsiio(struct ctl_scsiio *ctsio)
11736 const struct ctl_cmd_entry *entry;
11738 retval = CTL_RETVAL_COMPLETE;
11740 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11742 entry = ctl_get_cmd_entry(ctsio, NULL);
11745 * If this I/O has been aborted, just send it straight to
11746 * ctl_done() without executing it.
11748 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11749 ctl_done((union ctl_io *)ctsio);
11754 * All the checks should have been handled by ctl_scsiio_precheck().
11755 * We should be clear now to just execute the I/O.
11757 retval = entry->execute(ctsio);
11764 * Since we only implement one target right now, a bus reset simply resets
11765 * our single target.
11768 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11770 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11774 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11775 ctl_ua_type ua_type)
11777 struct ctl_port *port;
11778 struct ctl_lun *lun;
11781 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11782 union ctl_ha_msg msg_info;
11784 msg_info.hdr.nexus = io->io_hdr.nexus;
11785 if (ua_type==CTL_UA_TARG_RESET)
11786 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11788 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11789 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11790 msg_info.hdr.original_sc = NULL;
11791 msg_info.hdr.serializing_sc = NULL;
11792 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11793 sizeof(msg_info.task), M_WAITOK);
11797 mtx_lock(&softc->ctl_lock);
11798 port = ctl_io_port(&io->io_hdr);
11799 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11800 if (port != NULL &&
11801 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11803 retval += ctl_do_lun_reset(lun, io, ua_type);
11805 mtx_unlock(&softc->ctl_lock);
11806 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11811 * The LUN should always be set. The I/O is optional, and is used to
11812 * distinguish between I/Os sent by this initiator, and by other
11813 * initiators. We set unit attention for initiators other than this one.
11814 * SAM-3 is vague on this point. It does say that a unit attention should
11815 * be established for other initiators when a LUN is reset (see section
11816 * 5.7.3), but it doesn't specifically say that the unit attention should
11817 * be established for this particular initiator when a LUN is reset. Here
11818 * is the relevant text, from SAM-3 rev 8:
11820 * 5.7.2 When a SCSI initiator port aborts its own tasks
11822 * When a SCSI initiator port causes its own task(s) to be aborted, no
11823 * notification that the task(s) have been aborted shall be returned to
11824 * the SCSI initiator port other than the completion response for the
11825 * command or task management function action that caused the task(s) to
11826 * be aborted and notification(s) associated with related effects of the
11827 * action (e.g., a reset unit attention condition).
11829 * XXX KDM for now, we're setting unit attention for all initiators.
11832 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11840 mtx_lock(&lun->lun_lock);
11842 * Run through the OOA queue and abort each I/O.
11844 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11845 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11846 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11850 * This version sets unit attention for every
11853 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11854 ctl_est_ua_all(lun, initidx, ua_type);
11856 ctl_est_ua_all(lun, -1, ua_type);
11860 * A reset (any kind, really) clears reservations established with
11861 * RESERVE/RELEASE. It does not clear reservations established
11862 * with PERSISTENT RESERVE OUT, but we don't support that at the
11863 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11864 * reservations made with the RESERVE/RELEASE commands, because
11865 * those commands are obsolete in SPC-3.
11867 lun->flags &= ~CTL_LUN_RESERVED;
11870 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11871 ctl_clear_mask(lun->have_ca, i);
11873 lun->prevent_count = 0;
11874 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11875 ctl_clear_mask(lun->prevent, i);
11876 mtx_unlock(&lun->lun_lock);
11882 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11884 struct ctl_lun *lun;
11888 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11889 mtx_lock(&softc->ctl_lock);
11890 if ((targ_lun >= CTL_MAX_LUNS) ||
11891 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11892 mtx_unlock(&softc->ctl_lock);
11893 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11896 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11897 mtx_unlock(&softc->ctl_lock);
11898 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11900 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11901 union ctl_ha_msg msg_info;
11903 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11904 msg_info.hdr.nexus = io->io_hdr.nexus;
11905 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11906 msg_info.hdr.original_sc = NULL;
11907 msg_info.hdr.serializing_sc = NULL;
11908 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11909 sizeof(msg_info.task), M_WAITOK);
11915 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11920 mtx_assert(&lun->lun_lock, MA_OWNED);
11923 * Run through the OOA queue and attempt to find the given I/O.
11924 * The target port, initiator ID, tag type and tag number have to
11925 * match the values that we got from the initiator. If we have an
11926 * untagged command to abort, simply abort the first untagged command
11927 * we come to. We only allow one untagged command at a time of course.
11929 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11930 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11932 if ((targ_port == UINT32_MAX ||
11933 targ_port == xio->io_hdr.nexus.targ_port) &&
11934 (init_id == UINT32_MAX ||
11935 init_id == xio->io_hdr.nexus.initid)) {
11936 if (targ_port != xio->io_hdr.nexus.targ_port ||
11937 init_id != xio->io_hdr.nexus.initid)
11938 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11939 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11940 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11941 union ctl_ha_msg msg_info;
11943 msg_info.hdr.nexus = xio->io_hdr.nexus;
11944 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11945 msg_info.task.tag_num = xio->scsiio.tag_num;
11946 msg_info.task.tag_type = xio->scsiio.tag_type;
11947 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11948 msg_info.hdr.original_sc = NULL;
11949 msg_info.hdr.serializing_sc = NULL;
11950 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11951 sizeof(msg_info.task), M_NOWAIT);
11958 ctl_abort_task_set(union ctl_io *io)
11960 struct ctl_softc *softc = control_softc;
11961 struct ctl_lun *lun;
11967 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11968 mtx_lock(&softc->ctl_lock);
11969 if ((targ_lun >= CTL_MAX_LUNS) ||
11970 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11971 mtx_unlock(&softc->ctl_lock);
11972 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11976 mtx_lock(&lun->lun_lock);
11977 mtx_unlock(&softc->ctl_lock);
11978 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11979 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11980 io->io_hdr.nexus.initid,
11981 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11982 } else { /* CTL_TASK_CLEAR_TASK_SET */
11983 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11984 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11986 mtx_unlock(&lun->lun_lock);
11987 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11992 ctl_i_t_nexus_reset(union ctl_io *io)
11994 struct ctl_softc *softc = control_softc;
11995 struct ctl_lun *lun;
11998 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11999 union ctl_ha_msg msg_info;
12001 msg_info.hdr.nexus = io->io_hdr.nexus;
12002 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12003 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12004 msg_info.hdr.original_sc = NULL;
12005 msg_info.hdr.serializing_sc = NULL;
12006 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12007 sizeof(msg_info.task), M_WAITOK);
12010 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12011 mtx_lock(&softc->ctl_lock);
12012 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12013 mtx_lock(&lun->lun_lock);
12014 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12015 io->io_hdr.nexus.initid, 1);
12017 ctl_clear_mask(lun->have_ca, initidx);
12019 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12020 lun->flags &= ~CTL_LUN_RESERVED;
12021 if (ctl_is_set(lun->prevent, initidx)) {
12022 ctl_clear_mask(lun->prevent, initidx);
12023 lun->prevent_count--;
12025 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12026 mtx_unlock(&lun->lun_lock);
12028 mtx_unlock(&softc->ctl_lock);
12029 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12034 ctl_abort_task(union ctl_io *io)
12037 struct ctl_lun *lun;
12038 struct ctl_softc *softc;
12041 char printbuf[128];
12046 softc = control_softc;
12052 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12053 mtx_lock(&softc->ctl_lock);
12054 if ((targ_lun >= CTL_MAX_LUNS) ||
12055 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12056 mtx_unlock(&softc->ctl_lock);
12057 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12062 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12063 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12066 mtx_lock(&lun->lun_lock);
12067 mtx_unlock(&softc->ctl_lock);
12069 * Run through the OOA queue and attempt to find the given I/O.
12070 * The target port, initiator ID, tag type and tag number have to
12071 * match the values that we got from the initiator. If we have an
12072 * untagged command to abort, simply abort the first untagged command
12073 * we come to. We only allow one untagged command at a time of course.
12075 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12076 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12078 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12080 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12081 lun->lun, xio->scsiio.tag_num,
12082 xio->scsiio.tag_type,
12083 (xio->io_hdr.blocked_links.tqe_prev
12084 == NULL) ? "" : " BLOCKED",
12085 (xio->io_hdr.flags &
12086 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12087 (xio->io_hdr.flags &
12088 CTL_FLAG_ABORT) ? " ABORT" : "",
12089 (xio->io_hdr.flags &
12090 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12091 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12093 printf("%s\n", sbuf_data(&sb));
12096 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12097 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12098 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12102 * If the abort says that the task is untagged, the
12103 * task in the queue must be untagged. Otherwise,
12104 * we just check to see whether the tag numbers
12105 * match. This is because the QLogic firmware
12106 * doesn't pass back the tag type in an abort
12110 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12111 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12112 || (xio->scsiio.tag_num == io->taskio.tag_num))
12115 * XXX KDM we've got problems with FC, because it
12116 * doesn't send down a tag type with aborts. So we
12117 * can only really go by the tag number...
12118 * This may cause problems with parallel SCSI.
12119 * Need to figure that out!!
12121 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12122 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12124 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12125 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12126 union ctl_ha_msg msg_info;
12128 msg_info.hdr.nexus = io->io_hdr.nexus;
12129 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12130 msg_info.task.tag_num = io->taskio.tag_num;
12131 msg_info.task.tag_type = io->taskio.tag_type;
12132 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12133 msg_info.hdr.original_sc = NULL;
12134 msg_info.hdr.serializing_sc = NULL;
12136 printf("Sent Abort to other side\n");
12138 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12139 sizeof(msg_info.task), M_NOWAIT);
12142 printf("ctl_abort_task: found I/O to abort\n");
12146 mtx_unlock(&lun->lun_lock);
12150 * This isn't really an error. It's entirely possible for
12151 * the abort and command completion to cross on the wire.
12152 * This is more of an informative/diagnostic error.
12155 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12156 "%u:%u:%u tag %d type %d\n",
12157 io->io_hdr.nexus.initid,
12158 io->io_hdr.nexus.targ_port,
12159 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12160 io->taskio.tag_type);
12163 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12168 ctl_query_task(union ctl_io *io, int task_set)
12171 struct ctl_lun *lun;
12172 struct ctl_softc *softc;
12176 softc = control_softc;
12177 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12178 mtx_lock(&softc->ctl_lock);
12179 if ((targ_lun >= CTL_MAX_LUNS) ||
12180 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12181 mtx_unlock(&softc->ctl_lock);
12182 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12185 mtx_lock(&lun->lun_lock);
12186 mtx_unlock(&softc->ctl_lock);
12187 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12188 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12190 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12191 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12192 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12195 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12200 mtx_unlock(&lun->lun_lock);
12202 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12204 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12209 ctl_query_async_event(union ctl_io *io)
12211 struct ctl_lun *lun;
12212 struct ctl_softc *softc;
12214 uint32_t targ_lun, initidx;
12216 softc = control_softc;
12217 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12218 mtx_lock(&softc->ctl_lock);
12219 if ((targ_lun >= CTL_MAX_LUNS) ||
12220 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12221 mtx_unlock(&softc->ctl_lock);
12222 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12225 mtx_lock(&lun->lun_lock);
12226 mtx_unlock(&softc->ctl_lock);
12227 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12228 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12229 mtx_unlock(&lun->lun_lock);
12230 if (ua != CTL_UA_NONE)
12231 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12233 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12238 ctl_run_task(union ctl_io *io)
12240 struct ctl_softc *softc = control_softc;
12243 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12244 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12245 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12246 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12247 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12248 switch (io->taskio.task_action) {
12249 case CTL_TASK_ABORT_TASK:
12250 retval = ctl_abort_task(io);
12252 case CTL_TASK_ABORT_TASK_SET:
12253 case CTL_TASK_CLEAR_TASK_SET:
12254 retval = ctl_abort_task_set(io);
12256 case CTL_TASK_CLEAR_ACA:
12258 case CTL_TASK_I_T_NEXUS_RESET:
12259 retval = ctl_i_t_nexus_reset(io);
12261 case CTL_TASK_LUN_RESET:
12262 retval = ctl_lun_reset(softc, io);
12264 case CTL_TASK_TARGET_RESET:
12265 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12267 case CTL_TASK_BUS_RESET:
12268 retval = ctl_bus_reset(softc, io);
12270 case CTL_TASK_PORT_LOGIN:
12272 case CTL_TASK_PORT_LOGOUT:
12274 case CTL_TASK_QUERY_TASK:
12275 retval = ctl_query_task(io, 0);
12277 case CTL_TASK_QUERY_TASK_SET:
12278 retval = ctl_query_task(io, 1);
12280 case CTL_TASK_QUERY_ASYNC_EVENT:
12281 retval = ctl_query_async_event(io);
12284 printf("%s: got unknown task management event %d\n",
12285 __func__, io->taskio.task_action);
12289 io->io_hdr.status = CTL_SUCCESS;
12291 io->io_hdr.status = CTL_ERROR;
12296 * For HA operation. Handle commands that come in from the other
12300 ctl_handle_isc(union ctl_io *io)
12303 struct ctl_lun *lun;
12304 struct ctl_softc *softc = control_softc;
12307 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12308 lun = softc->ctl_luns[targ_lun];
12310 switch (io->io_hdr.msg_type) {
12311 case CTL_MSG_SERIALIZE:
12312 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12314 case CTL_MSG_R2R: {
12315 const struct ctl_cmd_entry *entry;
12318 * This is only used in SER_ONLY mode.
12321 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12322 mtx_lock(&lun->lun_lock);
12323 if (ctl_scsiio_lun_check(lun,
12324 entry, (struct ctl_scsiio *)io) != 0) {
12325 mtx_unlock(&lun->lun_lock);
12329 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12330 mtx_unlock(&lun->lun_lock);
12331 ctl_enqueue_rtr(io);
12334 case CTL_MSG_FINISH_IO:
12335 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12340 mtx_lock(&lun->lun_lock);
12341 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12343 ctl_check_blocked(lun);
12344 mtx_unlock(&lun->lun_lock);
12347 case CTL_MSG_PERS_ACTION:
12348 ctl_hndl_per_res_out_on_other_sc(
12349 (union ctl_ha_msg *)&io->presio.pr_msg);
12352 case CTL_MSG_BAD_JUJU:
12356 case CTL_MSG_DATAMOVE:
12357 /* Only used in XFER mode */
12359 ctl_datamove_remote(io);
12361 case CTL_MSG_DATAMOVE_DONE:
12362 /* Only used in XFER mode */
12364 io->scsiio.be_move_done(io);
12366 case CTL_MSG_FAILOVER:
12367 ctl_failover_lun(io);
12372 printf("%s: Invalid message type %d\n",
12373 __func__, io->io_hdr.msg_type);
12383 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12384 * there is no match.
12386 static ctl_lun_error_pattern
12387 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12389 const struct ctl_cmd_entry *entry;
12390 ctl_lun_error_pattern filtered_pattern, pattern;
12392 pattern = desc->error_pattern;
12395 * XXX KDM we need more data passed into this function to match a
12396 * custom pattern, and we actually need to implement custom pattern
12399 if (pattern & CTL_LUN_PAT_CMD)
12400 return (CTL_LUN_PAT_CMD);
12402 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12403 return (CTL_LUN_PAT_ANY);
12405 entry = ctl_get_cmd_entry(ctsio, NULL);
12407 filtered_pattern = entry->pattern & pattern;
12410 * If the user requested specific flags in the pattern (e.g.
12411 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12414 * If the user did not specify any flags, it doesn't matter whether
12415 * or not the command supports the flags.
12417 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12418 (pattern & ~CTL_LUN_PAT_MASK))
12419 return (CTL_LUN_PAT_NONE);
12422 * If the user asked for a range check, see if the requested LBA
12423 * range overlaps with this command's LBA range.
12425 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12431 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12433 return (CTL_LUN_PAT_NONE);
12435 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12436 desc->lba_range.len, FALSE);
12438 * A "pass" means that the LBA ranges don't overlap, so
12439 * this doesn't match the user's range criteria.
12441 if (action == CTL_ACTION_PASS)
12442 return (CTL_LUN_PAT_NONE);
12445 return (filtered_pattern);
12449 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12451 struct ctl_error_desc *desc, *desc2;
12453 mtx_assert(&lun->lun_lock, MA_OWNED);
12455 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12456 ctl_lun_error_pattern pattern;
12458 * Check to see whether this particular command matches
12459 * the pattern in the descriptor.
12461 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12462 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12465 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12466 case CTL_LUN_INJ_ABORTED:
12467 ctl_set_aborted(&io->scsiio);
12469 case CTL_LUN_INJ_MEDIUM_ERR:
12470 ctl_set_medium_error(&io->scsiio,
12471 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12472 CTL_FLAG_DATA_OUT);
12474 case CTL_LUN_INJ_UA:
12475 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12477 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12479 case CTL_LUN_INJ_CUSTOM:
12481 * We're assuming the user knows what he is doing.
12482 * Just copy the sense information without doing
12485 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12486 MIN(sizeof(desc->custom_sense),
12487 sizeof(io->scsiio.sense_data)));
12488 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12489 io->scsiio.sense_len = SSD_FULL_SIZE;
12490 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12492 case CTL_LUN_INJ_NONE:
12495 * If this is an error injection type we don't know
12496 * about, clear the continuous flag (if it is set)
12497 * so it will get deleted below.
12499 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12503 * By default, each error injection action is a one-shot
12505 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12508 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12514 #ifdef CTL_IO_DELAY
12516 ctl_datamove_timer_wakeup(void *arg)
12520 io = (union ctl_io *)arg;
12524 #endif /* CTL_IO_DELAY */
12527 ctl_datamove(union ctl_io *io)
12529 struct ctl_lun *lun;
12530 void (*fe_datamove)(union ctl_io *io);
12532 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12534 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12536 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12538 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12543 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12544 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12546 sbuf_cat(&sb, path_str);
12547 switch (io->io_hdr.io_type) {
12549 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12550 sbuf_printf(&sb, "\n");
12551 sbuf_cat(&sb, path_str);
12552 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12553 io->scsiio.tag_num, io->scsiio.tag_type);
12556 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12557 "Tag Type: %d\n", io->taskio.task_action,
12558 io->taskio.tag_num, io->taskio.tag_type);
12561 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12562 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
12565 sbuf_cat(&sb, path_str);
12566 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12567 (intmax_t)time_uptime - io->io_hdr.start_time);
12569 printf("%s", sbuf_data(&sb));
12571 #endif /* CTL_TIME_IO */
12573 #ifdef CTL_IO_DELAY
12574 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12575 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12578 && (lun->delay_info.datamove_delay > 0)) {
12580 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12581 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12582 callout_reset(&io->io_hdr.delay_callout,
12583 lun->delay_info.datamove_delay * hz,
12584 ctl_datamove_timer_wakeup, io);
12585 if (lun->delay_info.datamove_type ==
12586 CTL_DELAY_TYPE_ONESHOT)
12587 lun->delay_info.datamove_delay = 0;
12594 * This command has been aborted. Set the port status, so we fail
12597 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12598 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12599 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12600 io->io_hdr.nexus.targ_port,
12601 io->io_hdr.nexus.targ_lun);
12602 io->io_hdr.port_status = 31337;
12604 * Note that the backend, in this case, will get the
12605 * callback in its context. In other cases it may get
12606 * called in the frontend's interrupt thread context.
12608 io->scsiio.be_move_done(io);
12612 /* Don't confuse frontend with zero length data move. */
12613 if (io->scsiio.kern_data_len == 0) {
12614 io->scsiio.be_move_done(io);
12618 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12623 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12625 union ctl_ha_msg msg;
12627 struct bintime cur_bt;
12630 memset(&msg, 0, sizeof(msg));
12631 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12632 msg.hdr.original_sc = io;
12633 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12634 msg.hdr.nexus = io->io_hdr.nexus;
12635 msg.hdr.status = io->io_hdr.status;
12636 msg.scsi.tag_num = io->scsiio.tag_num;
12637 msg.scsi.tag_type = io->scsiio.tag_type;
12638 msg.scsi.scsi_status = io->scsiio.scsi_status;
12639 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12640 io->scsiio.sense_len);
12641 msg.scsi.sense_len = io->scsiio.sense_len;
12642 msg.scsi.sense_residual = io->scsiio.sense_residual;
12643 msg.scsi.fetd_status = io->io_hdr.port_status;
12644 msg.scsi.residual = io->scsiio.residual;
12645 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12646 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12647 ctl_failover_io(io, /*have_lock*/ have_lock);
12650 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12651 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12652 msg.scsi.sense_len, M_WAITOK);
12655 getbinuptime(&cur_bt);
12656 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12657 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12659 io->io_hdr.num_dmas++;
12663 * The DMA to the remote side is done, now we need to tell the other side
12664 * we're done so it can continue with its data movement.
12667 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12674 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12675 printf("%s: ISC DMA write failed with error %d", __func__,
12677 ctl_set_internal_failure(&io->scsiio,
12679 /*retry_count*/ rq->ret);
12682 ctl_dt_req_free(rq);
12684 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12685 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12686 free(io->io_hdr.remote_sglist, M_CTL);
12687 io->io_hdr.remote_sglist = NULL;
12688 io->io_hdr.local_sglist = NULL;
12691 * The data is in local and remote memory, so now we need to send
12692 * status (good or back) back to the other side.
12694 ctl_send_datamove_done(io, /*have_lock*/ 0);
12698 * We've moved the data from the host/controller into local memory. Now we
12699 * need to push it over to the remote controller's memory.
12702 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12706 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12707 ctl_datamove_remote_write_cb);
12712 ctl_datamove_remote_write(union ctl_io *io)
12715 void (*fe_datamove)(union ctl_io *io);
12718 * - Get the data from the host/HBA into local memory.
12719 * - DMA memory from the local controller to the remote controller.
12720 * - Send status back to the remote controller.
12723 retval = ctl_datamove_remote_sgl_setup(io);
12727 /* Switch the pointer over so the FETD knows what to do */
12728 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12731 * Use a custom move done callback, since we need to send completion
12732 * back to the other controller, not to the backend on this side.
12734 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12736 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12741 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12750 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12751 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12752 free(io->io_hdr.remote_sglist, M_CTL);
12753 io->io_hdr.remote_sglist = NULL;
12754 io->io_hdr.local_sglist = NULL;
12757 scsi_path_string(io, path_str, sizeof(path_str));
12758 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12759 sbuf_cat(&sb, path_str);
12760 scsi_command_string(&io->scsiio, NULL, &sb);
12761 sbuf_printf(&sb, "\n");
12762 sbuf_cat(&sb, path_str);
12763 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12764 io->scsiio.tag_num, io->scsiio.tag_type);
12765 sbuf_cat(&sb, path_str);
12766 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12767 io->io_hdr.flags, io->io_hdr.status);
12769 printk("%s", sbuf_data(&sb));
12774 * The read is done, now we need to send status (good or bad) back
12775 * to the other side.
12777 ctl_send_datamove_done(io, /*have_lock*/ 0);
12783 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12786 void (*fe_datamove)(union ctl_io *io);
12790 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12791 printf("%s: ISC DMA read failed with error %d\n", __func__,
12793 ctl_set_internal_failure(&io->scsiio,
12795 /*retry_count*/ rq->ret);
12798 ctl_dt_req_free(rq);
12800 /* Switch the pointer over so the FETD knows what to do */
12801 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12804 * Use a custom move done callback, since we need to send completion
12805 * back to the other controller, not to the backend on this side.
12807 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12809 /* XXX KDM add checks like the ones in ctl_datamove? */
12811 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12816 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12818 struct ctl_sg_entry *local_sglist;
12819 uint32_t len_to_go;
12824 local_sglist = io->io_hdr.local_sglist;
12825 len_to_go = io->scsiio.kern_data_len;
12828 * The difficult thing here is that the size of the various
12829 * S/G segments may be different than the size from the
12830 * remote controller. That'll make it harder when DMAing
12831 * the data back to the other side.
12833 for (i = 0; len_to_go > 0; i++) {
12834 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12835 local_sglist[i].addr =
12836 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12838 len_to_go -= local_sglist[i].len;
12841 * Reset the number of S/G entries accordingly. The original
12842 * number of S/G entries is available in rem_sg_entries.
12844 io->scsiio.kern_sg_entries = i;
12847 printf("%s: kern_sg_entries = %d\n", __func__,
12848 io->scsiio.kern_sg_entries);
12849 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12850 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12851 local_sglist[i].addr, local_sglist[i].len);
12858 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12859 ctl_ha_dt_cb callback)
12861 struct ctl_ha_dt_req *rq;
12862 struct ctl_sg_entry *remote_sglist, *local_sglist;
12863 uint32_t local_used, remote_used, total_used;
12866 rq = ctl_dt_req_alloc();
12869 * If we failed to allocate the request, and if the DMA didn't fail
12870 * anyway, set busy status. This is just a resource allocation
12874 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12875 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12876 ctl_set_busy(&io->scsiio);
12878 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12879 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12882 ctl_dt_req_free(rq);
12885 * The data move failed. We need to return status back
12886 * to the other controller. No point in trying to DMA
12887 * data to the remote controller.
12890 ctl_send_datamove_done(io, /*have_lock*/ 0);
12895 local_sglist = io->io_hdr.local_sglist;
12896 remote_sglist = io->io_hdr.remote_sglist;
12902 * Pull/push the data over the wire from/to the other controller.
12903 * This takes into account the possibility that the local and
12904 * remote sglists may not be identical in terms of the size of
12905 * the elements and the number of elements.
12907 * One fundamental assumption here is that the length allocated for
12908 * both the local and remote sglists is identical. Otherwise, we've
12909 * essentially got a coding error of some sort.
12911 isc_ret = CTL_HA_STATUS_SUCCESS;
12912 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12916 rq->command = command;
12920 * Both pointers should be aligned. But it is possible
12921 * that the allocation length is not. They should both
12922 * also have enough slack left over at the end, though,
12923 * to round up to the next 8 byte boundary.
12925 cur_len = MIN(local_sglist[i].len - local_used,
12926 remote_sglist[j].len - remote_used);
12927 rq->size = cur_len;
12929 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12930 tmp_ptr += local_used;
12933 /* Use physical addresses when talking to ISC hardware */
12934 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12935 /* XXX KDM use busdma */
12936 rq->local = vtophys(tmp_ptr);
12938 rq->local = tmp_ptr;
12940 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12941 ("HA does not support BUS_ADDR"));
12942 rq->local = tmp_ptr;
12945 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12946 tmp_ptr += remote_used;
12947 rq->remote = tmp_ptr;
12949 rq->callback = NULL;
12951 local_used += cur_len;
12952 if (local_used >= local_sglist[i].len) {
12957 remote_used += cur_len;
12958 if (remote_used >= remote_sglist[j].len) {
12962 total_used += cur_len;
12964 if (total_used >= io->scsiio.kern_data_len)
12965 rq->callback = callback;
12968 printf("%s: %s: local %p remote %p size %d\n", __func__,
12969 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12970 rq->local, rq->remote, rq->size);
12973 isc_ret = ctl_dt_single(rq);
12974 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12977 if (isc_ret != CTL_HA_STATUS_WAIT) {
12986 ctl_datamove_remote_read(union ctl_io *io)
12992 * This will send an error to the other controller in the case of a
12995 retval = ctl_datamove_remote_sgl_setup(io);
12999 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13000 ctl_datamove_remote_read_cb);
13003 * Make sure we free memory if there was an error.. The
13004 * ctl_datamove_remote_xfer() function will send the
13005 * datamove done message, or call the callback with an
13006 * error if there is a problem.
13008 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13009 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13010 free(io->io_hdr.remote_sglist, M_CTL);
13011 io->io_hdr.remote_sglist = NULL;
13012 io->io_hdr.local_sglist = NULL;
13017 * Process a datamove request from the other controller. This is used for
13018 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13019 * first. Once that is complete, the data gets DMAed into the remote
13020 * controller's memory. For reads, we DMA from the remote controller's
13021 * memory into our memory first, and then move it out to the FETD.
13024 ctl_datamove_remote(union ctl_io *io)
13027 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
13029 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13030 ctl_failover_io(io, /*have_lock*/ 0);
13035 * Note that we look for an aborted I/O here, but don't do some of
13036 * the other checks that ctl_datamove() normally does.
13037 * We don't need to run the datamove delay code, since that should
13038 * have been done if need be on the other controller.
13040 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13041 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13042 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13043 io->io_hdr.nexus.targ_port,
13044 io->io_hdr.nexus.targ_lun);
13045 io->io_hdr.port_status = 31338;
13046 ctl_send_datamove_done(io, /*have_lock*/ 0);
13050 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13051 ctl_datamove_remote_write(io);
13052 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13053 ctl_datamove_remote_read(io);
13055 io->io_hdr.port_status = 31339;
13056 ctl_send_datamove_done(io, /*have_lock*/ 0);
13061 ctl_process_done(union ctl_io *io)
13063 struct ctl_lun *lun;
13064 struct ctl_softc *softc = control_softc;
13065 void (*fe_done)(union ctl_io *io);
13066 union ctl_ha_msg msg;
13067 uint32_t targ_port = io->io_hdr.nexus.targ_port;
13069 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13070 fe_done = softc->ctl_ports[targ_port]->fe_done;
13073 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13078 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13079 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13081 sbuf_cat(&sb, path_str);
13082 switch (io->io_hdr.io_type) {
13084 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13085 sbuf_printf(&sb, "\n");
13086 sbuf_cat(&sb, path_str);
13087 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13088 io->scsiio.tag_num, io->scsiio.tag_type);
13091 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13092 "Tag Type: %d\n", io->taskio.task_action,
13093 io->taskio.tag_num, io->taskio.tag_type);
13096 printf("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13097 panic("Invalid CTL I/O type %d\n", io->io_hdr.io_type);
13100 sbuf_cat(&sb, path_str);
13101 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13102 (intmax_t)time_uptime - io->io_hdr.start_time);
13104 printf("%s", sbuf_data(&sb));
13106 #endif /* CTL_TIME_IO */
13108 switch (io->io_hdr.io_type) {
13112 if (ctl_debug & CTL_DEBUG_INFO)
13113 ctl_io_error_print(io, NULL);
13117 panic("ctl_process_done: invalid io type %d\n",
13118 io->io_hdr.io_type);
13119 break; /* NOTREACHED */
13122 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13124 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13125 io->io_hdr.nexus.targ_mapped_lun));
13129 mtx_lock(&lun->lun_lock);
13132 * Check to see if we have any errors to inject here. We only
13133 * inject errors for commands that don't already have errors set.
13135 if ((STAILQ_FIRST(&lun->error_list) != NULL) &&
13136 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13137 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13138 ctl_inject_error(lun, io);
13141 * XXX KDM how do we treat commands that aren't completed
13144 * XXX KDM should we also track I/O latency?
13146 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13147 io->io_hdr.io_type == CTL_IO_SCSI) {
13149 struct bintime cur_bt;
13153 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13155 type = CTL_STATS_READ;
13156 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13158 type = CTL_STATS_WRITE;
13160 type = CTL_STATS_NO_IO;
13162 lun->stats.ports[targ_port].bytes[type] +=
13163 io->scsiio.kern_total_len;
13164 lun->stats.ports[targ_port].operations[type]++;
13166 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13167 &io->io_hdr.dma_bt);
13168 getbinuptime(&cur_bt);
13169 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13170 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13172 lun->stats.ports[targ_port].num_dmas[type] +=
13173 io->io_hdr.num_dmas;
13177 * Remove this from the OOA queue.
13179 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13181 if (TAILQ_EMPTY(&lun->ooa_queue))
13182 lun->last_busy = getsbinuptime();
13186 * Run through the blocked queue on this LUN and see if anything
13187 * has become unblocked, now that this transaction is done.
13189 ctl_check_blocked(lun);
13192 * If the LUN has been invalidated, free it if there is nothing
13193 * left on its OOA queue.
13195 if ((lun->flags & CTL_LUN_INVALID)
13196 && TAILQ_EMPTY(&lun->ooa_queue)) {
13197 mtx_unlock(&lun->lun_lock);
13198 mtx_lock(&softc->ctl_lock);
13200 mtx_unlock(&softc->ctl_lock);
13202 mtx_unlock(&lun->lun_lock);
13207 * If this command has been aborted, make sure we set the status
13208 * properly. The FETD is responsible for freeing the I/O and doing
13209 * whatever it needs to do to clean up its state.
13211 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13212 ctl_set_task_aborted(&io->scsiio);
13215 * If enabled, print command error status.
13217 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13218 (ctl_debug & CTL_DEBUG_INFO) != 0)
13219 ctl_io_error_print(io, NULL);
13222 * Tell the FETD or the other shelf controller we're done with this
13223 * command. Note that only SCSI commands get to this point. Task
13224 * management commands are completed above.
13226 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13227 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13228 memset(&msg, 0, sizeof(msg));
13229 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13230 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13231 msg.hdr.nexus = io->io_hdr.nexus;
13232 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13233 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13242 * Front end should call this if it doesn't do autosense. When the request
13243 * sense comes back in from the initiator, we'll dequeue this and send it.
13246 ctl_queue_sense(union ctl_io *io)
13248 struct ctl_lun *lun;
13249 struct ctl_port *port;
13250 struct ctl_softc *softc;
13251 uint32_t initidx, targ_lun;
13253 softc = control_softc;
13255 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13258 * LUN lookup will likely move to the ctl_work_thread() once we
13259 * have our new queueing infrastructure (that doesn't put things on
13260 * a per-LUN queue initially). That is so that we can handle
13261 * things like an INQUIRY to a LUN that we don't have enabled. We
13262 * can't deal with that right now.
13264 mtx_lock(&softc->ctl_lock);
13267 * If we don't have a LUN for this, just toss the sense
13270 port = ctl_io_port(&ctsio->io_hdr);
13271 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13272 if ((targ_lun < CTL_MAX_LUNS)
13273 && (softc->ctl_luns[targ_lun] != NULL))
13274 lun = softc->ctl_luns[targ_lun];
13278 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13280 mtx_lock(&lun->lun_lock);
13282 * Already have CA set for this LUN...toss the sense information.
13284 if (ctl_is_set(lun->have_ca, initidx)) {
13285 mtx_unlock(&lun->lun_lock);
13289 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13290 MIN(sizeof(lun->pending_sense[initidx]),
13291 sizeof(io->scsiio.sense_data)));
13292 ctl_set_mask(lun->have_ca, initidx);
13293 mtx_unlock(&lun->lun_lock);
13296 mtx_unlock(&softc->ctl_lock);
13300 return (CTL_RETVAL_COMPLETE);
13305 * Primary command inlet from frontend ports. All SCSI and task I/O
13306 * requests must go through this function.
13309 ctl_queue(union ctl_io *io)
13311 struct ctl_port *port;
13313 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13316 io->io_hdr.start_time = time_uptime;
13317 getbinuptime(&io->io_hdr.start_bt);
13318 #endif /* CTL_TIME_IO */
13320 /* Map FE-specific LUN ID into global one. */
13321 port = ctl_io_port(&io->io_hdr);
13322 io->io_hdr.nexus.targ_mapped_lun =
13323 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13325 switch (io->io_hdr.io_type) {
13328 if (ctl_debug & CTL_DEBUG_CDB)
13330 ctl_enqueue_incoming(io);
13333 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13337 return (CTL_RETVAL_COMPLETE);
13340 #ifdef CTL_IO_DELAY
13342 ctl_done_timer_wakeup(void *arg)
13346 io = (union ctl_io *)arg;
13349 #endif /* CTL_IO_DELAY */
13352 ctl_serseq_done(union ctl_io *io)
13354 struct ctl_lun *lun;
13356 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13357 if (lun->be_lun == NULL ||
13358 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13360 mtx_lock(&lun->lun_lock);
13361 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13362 ctl_check_blocked(lun);
13363 mtx_unlock(&lun->lun_lock);
13367 ctl_done(union ctl_io *io)
13371 * Enable this to catch duplicate completion issues.
13374 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13375 printf("%s: type %d msg %d cdb %x iptl: "
13376 "%u:%u:%u tag 0x%04x "
13377 "flag %#x status %x\n",
13379 io->io_hdr.io_type,
13380 io->io_hdr.msg_type,
13382 io->io_hdr.nexus.initid,
13383 io->io_hdr.nexus.targ_port,
13384 io->io_hdr.nexus.targ_lun,
13385 (io->io_hdr.io_type ==
13387 io->taskio.tag_num :
13388 io->scsiio.tag_num,
13390 io->io_hdr.status);
13392 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13396 * This is an internal copy of an I/O, and should not go through
13397 * the normal done processing logic.
13399 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13402 #ifdef CTL_IO_DELAY
13403 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13404 struct ctl_lun *lun;
13406 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13408 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13410 struct ctl_lun *lun;
13412 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13415 && (lun->delay_info.done_delay > 0)) {
13417 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13418 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13419 callout_reset(&io->io_hdr.delay_callout,
13420 lun->delay_info.done_delay * hz,
13421 ctl_done_timer_wakeup, io);
13422 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13423 lun->delay_info.done_delay = 0;
13427 #endif /* CTL_IO_DELAY */
13429 ctl_enqueue_done(io);
13433 ctl_work_thread(void *arg)
13435 struct ctl_thread *thr = (struct ctl_thread *)arg;
13436 struct ctl_softc *softc = thr->ctl_softc;
13440 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13444 * We handle the queues in this order:
13446 * - done queue (to free up resources, unblock other commands)
13450 * If those queues are empty, we break out of the loop and
13453 mtx_lock(&thr->queue_lock);
13454 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13456 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13457 mtx_unlock(&thr->queue_lock);
13458 ctl_handle_isc(io);
13461 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13463 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13464 /* clear any blocked commands, call fe_done */
13465 mtx_unlock(&thr->queue_lock);
13466 ctl_process_done(io);
13469 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13471 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13472 mtx_unlock(&thr->queue_lock);
13473 if (io->io_hdr.io_type == CTL_IO_TASK)
13476 ctl_scsiio_precheck(softc, &io->scsiio);
13479 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13481 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13482 mtx_unlock(&thr->queue_lock);
13483 retval = ctl_scsiio(&io->scsiio);
13484 if (retval != CTL_RETVAL_COMPLETE)
13485 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13489 /* Sleep until we have something to do. */
13490 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13495 ctl_lun_thread(void *arg)
13497 struct ctl_softc *softc = (struct ctl_softc *)arg;
13498 struct ctl_be_lun *be_lun;
13500 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13503 mtx_lock(&softc->ctl_lock);
13504 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13505 if (be_lun != NULL) {
13506 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13507 mtx_unlock(&softc->ctl_lock);
13508 ctl_create_lun(be_lun);
13512 /* Sleep until we have something to do. */
13513 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13514 PDROP | PRIBIO, "-", 0);
13519 ctl_thresh_thread(void *arg)
13521 struct ctl_softc *softc = (struct ctl_softc *)arg;
13522 struct ctl_lun *lun;
13523 struct scsi_da_rw_recovery_page *rwpage;
13524 struct ctl_logical_block_provisioning_page *page;
13526 union ctl_ha_msg msg;
13527 uint64_t thres, val;
13530 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13533 mtx_lock(&softc->ctl_lock);
13534 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13535 if ((lun->flags & CTL_LUN_DISABLED) ||
13536 (lun->flags & CTL_LUN_NO_MEDIA) ||
13537 lun->backend->lun_attr == NULL)
13539 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13540 softc->ha_mode == CTL_HA_MODE_XFER)
13542 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13543 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13546 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13547 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13548 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13550 thres = scsi_4btoul(page->descr[i].count);
13551 thres <<= CTL_LBP_EXPONENT;
13552 switch (page->descr[i].resource) {
13554 attr = "blocksavail";
13557 attr = "blocksused";
13560 attr = "poolblocksavail";
13563 attr = "poolblocksused";
13568 mtx_unlock(&softc->ctl_lock); // XXX
13569 val = lun->backend->lun_attr(
13570 lun->be_lun->be_lun, attr);
13571 mtx_lock(&softc->ctl_lock);
13572 if (val == UINT64_MAX)
13574 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13575 == SLBPPD_ARMING_INC)
13576 e = (val >= thres);
13578 e = (val <= thres);
13582 mtx_lock(&lun->lun_lock);
13584 scsi_u64to8b((uint8_t *)&page->descr[i] -
13585 (uint8_t *)page, lun->ua_tpt_info);
13586 if (lun->lasttpt == 0 ||
13587 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13588 lun->lasttpt = time_uptime;
13589 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13595 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13598 mtx_unlock(&lun->lun_lock);
13600 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13601 /* Send msg to other side. */
13602 bzero(&msg.ua, sizeof(msg.ua));
13603 msg.hdr.msg_type = CTL_MSG_UA;
13604 msg.hdr.nexus.initid = -1;
13605 msg.hdr.nexus.targ_port = -1;
13606 msg.hdr.nexus.targ_lun = lun->lun;
13607 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13609 msg.ua.ua_set = (set > 0);
13610 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13611 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13612 mtx_unlock(&softc->ctl_lock); // XXX
13613 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13614 sizeof(msg.ua), M_WAITOK);
13615 mtx_lock(&softc->ctl_lock);
13618 mtx_unlock(&softc->ctl_lock);
13619 pause("-", CTL_LBP_PERIOD * hz);
13624 ctl_enqueue_incoming(union ctl_io *io)
13626 struct ctl_softc *softc = control_softc;
13627 struct ctl_thread *thr;
13630 idx = (io->io_hdr.nexus.targ_port * 127 +
13631 io->io_hdr.nexus.initid) % worker_threads;
13632 thr = &softc->threads[idx];
13633 mtx_lock(&thr->queue_lock);
13634 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13635 mtx_unlock(&thr->queue_lock);
13640 ctl_enqueue_rtr(union ctl_io *io)
13642 struct ctl_softc *softc = control_softc;
13643 struct ctl_thread *thr;
13645 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13646 mtx_lock(&thr->queue_lock);
13647 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13648 mtx_unlock(&thr->queue_lock);
13653 ctl_enqueue_done(union ctl_io *io)
13655 struct ctl_softc *softc = control_softc;
13656 struct ctl_thread *thr;
13658 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13659 mtx_lock(&thr->queue_lock);
13660 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13661 mtx_unlock(&thr->queue_lock);
13666 ctl_enqueue_isc(union ctl_io *io)
13668 struct ctl_softc *softc = control_softc;
13669 struct ctl_thread *thr;
13671 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13672 mtx_lock(&thr->queue_lock);
13673 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13674 mtx_unlock(&thr->queue_lock);