2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2015 Alexander Motin <mav@FreeBSD.org>
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
38 * CAM Target Layer, a SCSI device emulation subsystem.
40 * Author: Ken Merry <ken@FreeBSD.org>
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
55 #include <sys/fcntl.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
66 #include <sys/endian.h>
67 #include <sys/sysctl.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_cd.h>
73 #include <cam/scsi/scsi_da.h>
74 #include <cam/ctl/ctl_io.h>
75 #include <cam/ctl/ctl.h>
76 #include <cam/ctl/ctl_frontend.h>
77 #include <cam/ctl/ctl_util.h>
78 #include <cam/ctl/ctl_backend.h>
79 #include <cam/ctl/ctl_ioctl.h>
80 #include <cam/ctl/ctl_ha.h>
81 #include <cam/ctl/ctl_private.h>
82 #include <cam/ctl/ctl_debug.h>
83 #include <cam/ctl/ctl_scsi_all.h>
84 #include <cam/ctl/ctl_error.h>
86 struct ctl_softc *control_softc = NULL;
89 * Template mode pages.
93 * Note that these are default values only. The actual values will be
94 * filled in when the user does a mode sense.
96 const static struct copan_debugconf_subpage debugconf_page_default = {
97 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
98 DBGCNF_SUBPAGE_CODE, /* subpage */
99 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
100 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
101 DBGCNF_VERSION, /* page_version */
102 {CTL_TIME_IO_DEFAULT_SECS>>8,
103 CTL_TIME_IO_DEFAULT_SECS>>0}, /* ctl_time_io_secs */
106 const static struct copan_debugconf_subpage debugconf_page_changeable = {
107 DBGCNF_PAGE_CODE | SMPH_SPF, /* page_code */
108 DBGCNF_SUBPAGE_CODE, /* subpage */
109 {(sizeof(struct copan_debugconf_subpage) - 4) >> 8,
110 (sizeof(struct copan_debugconf_subpage) - 4) >> 0}, /* page_length */
111 0, /* page_version */
112 {0xff,0xff}, /* ctl_time_io_secs */
115 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
116 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
117 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
118 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
119 /*read_retry_count*/0,
120 /*correction_span*/0,
121 /*head_offset_count*/0,
122 /*data_strobe_offset_cnt*/0,
123 /*byte8*/SMS_RWER_LBPERE,
124 /*write_retry_count*/0,
126 /*recovery_time_limit*/{0, 0},
129 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
130 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
131 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
133 /*read_retry_count*/0,
134 /*correction_span*/0,
135 /*head_offset_count*/0,
136 /*data_strobe_offset_cnt*/0,
138 /*write_retry_count*/0,
140 /*recovery_time_limit*/{0, 0},
143 const static struct scsi_format_page format_page_default = {
144 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
145 /*page_length*/sizeof(struct scsi_format_page) - 2,
146 /*tracks_per_zone*/ {0, 0},
147 /*alt_sectors_per_zone*/ {0, 0},
148 /*alt_tracks_per_zone*/ {0, 0},
149 /*alt_tracks_per_lun*/ {0, 0},
150 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
151 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
152 /*bytes_per_sector*/ {0, 0},
153 /*interleave*/ {0, 0},
154 /*track_skew*/ {0, 0},
155 /*cylinder_skew*/ {0, 0},
157 /*reserved*/ {0, 0, 0}
160 const static struct scsi_format_page format_page_changeable = {
161 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
162 /*page_length*/sizeof(struct scsi_format_page) - 2,
163 /*tracks_per_zone*/ {0, 0},
164 /*alt_sectors_per_zone*/ {0, 0},
165 /*alt_tracks_per_zone*/ {0, 0},
166 /*alt_tracks_per_lun*/ {0, 0},
167 /*sectors_per_track*/ {0, 0},
168 /*bytes_per_sector*/ {0, 0},
169 /*interleave*/ {0, 0},
170 /*track_skew*/ {0, 0},
171 /*cylinder_skew*/ {0, 0},
173 /*reserved*/ {0, 0, 0}
176 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
177 /*page_code*/SMS_RIGID_DISK_PAGE,
178 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
179 /*cylinders*/ {0, 0, 0},
180 /*heads*/ CTL_DEFAULT_HEADS,
181 /*start_write_precomp*/ {0, 0, 0},
182 /*start_reduced_current*/ {0, 0, 0},
183 /*step_rate*/ {0, 0},
184 /*landing_zone_cylinder*/ {0, 0, 0},
185 /*rpl*/ SRDP_RPL_DISABLED,
186 /*rotational_offset*/ 0,
188 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
189 CTL_DEFAULT_ROTATION_RATE & 0xff},
193 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
194 /*page_code*/SMS_RIGID_DISK_PAGE,
195 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
196 /*cylinders*/ {0, 0, 0},
198 /*start_write_precomp*/ {0, 0, 0},
199 /*start_reduced_current*/ {0, 0, 0},
200 /*step_rate*/ {0, 0},
201 /*landing_zone_cylinder*/ {0, 0, 0},
203 /*rotational_offset*/ 0,
205 /*rotation_rate*/ {0, 0},
209 const static struct scsi_caching_page caching_page_default = {
210 /*page_code*/SMS_CACHING_PAGE,
211 /*page_length*/sizeof(struct scsi_caching_page) - 2,
212 /*flags1*/ SCP_DISC | SCP_WCE,
214 /*disable_pf_transfer_len*/ {0xff, 0xff},
215 /*min_prefetch*/ {0, 0},
216 /*max_prefetch*/ {0xff, 0xff},
217 /*max_pf_ceiling*/ {0xff, 0xff},
219 /*cache_segments*/ 0,
220 /*cache_seg_size*/ {0, 0},
222 /*non_cache_seg_size*/ {0, 0, 0}
225 const static struct scsi_caching_page caching_page_changeable = {
226 /*page_code*/SMS_CACHING_PAGE,
227 /*page_length*/sizeof(struct scsi_caching_page) - 2,
228 /*flags1*/ SCP_WCE | SCP_RCD,
230 /*disable_pf_transfer_len*/ {0, 0},
231 /*min_prefetch*/ {0, 0},
232 /*max_prefetch*/ {0, 0},
233 /*max_pf_ceiling*/ {0, 0},
235 /*cache_segments*/ 0,
236 /*cache_seg_size*/ {0, 0},
238 /*non_cache_seg_size*/ {0, 0, 0}
241 const static struct scsi_control_page control_page_default = {
242 /*page_code*/SMS_CONTROL_MODE_PAGE,
243 /*page_length*/sizeof(struct scsi_control_page) - 2,
245 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
248 /*aen_holdoff_period*/{0, 0},
249 /*busy_timeout_period*/{0, 0},
250 /*extended_selftest_completion_time*/{0, 0}
253 const static struct scsi_control_page control_page_changeable = {
254 /*page_code*/SMS_CONTROL_MODE_PAGE,
255 /*page_length*/sizeof(struct scsi_control_page) - 2,
257 /*queue_flags*/SCP_QUEUE_ALG_MASK,
258 /*eca_and_aen*/SCP_SWP,
260 /*aen_holdoff_period*/{0, 0},
261 /*busy_timeout_period*/{0, 0},
262 /*extended_selftest_completion_time*/{0, 0}
265 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
267 const static struct scsi_control_ext_page control_ext_page_default = {
268 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
269 /*subpage_code*/0x01,
270 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
276 const static struct scsi_control_ext_page control_ext_page_changeable = {
277 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
278 /*subpage_code*/0x01,
279 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
285 const static struct scsi_info_exceptions_page ie_page_default = {
286 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
287 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
288 /*info_flags*/SIEP_FLAGS_DEXCPT,
290 /*interval_timer*/{0, 0, 0, 0},
291 /*report_count*/{0, 0, 0, 0}
294 const static struct scsi_info_exceptions_page ie_page_changeable = {
295 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
296 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
299 /*interval_timer*/{0, 0, 0, 0},
300 /*report_count*/{0, 0, 0, 0}
303 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
305 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
307 /*subpage_code*/0x02,
308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
315 /*count*/{0, 0, 0, 0}},
319 /*count*/{0, 0, 0, 0}},
323 /*count*/{0, 0, 0, 0}},
327 /*count*/{0, 0, 0, 0}}
331 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
333 /*subpage_code*/0x02,
334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
341 /*count*/{0, 0, 0, 0}},
345 /*count*/{0, 0, 0, 0}},
349 /*count*/{0, 0, 0, 0}},
353 /*count*/{0, 0, 0, 0}}
357 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
358 /*page_code*/SMS_CDDVD_CAPS_PAGE,
359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
367 /*nvol_levels*/{0, 0},
368 /*buffer_size*/{8, 0},
373 /*copy_management*/0,
375 /*rotation_control*/0,
376 /*cur_write_speed*/0,
377 /*num_speed_descr*/0,
380 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
381 /*page_code*/SMS_CDDVD_CAPS_PAGE,
382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
390 /*nvol_levels*/{0, 0},
391 /*buffer_size*/{0, 0},
396 /*copy_management*/0,
398 /*rotation_control*/0,
399 /*cur_write_speed*/0,
400 /*num_speed_descr*/0,
403 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
404 static int worker_threads = -1;
405 TUNABLE_INT("kern.cam.ctl.worker_threads", &worker_threads);
406 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
407 &worker_threads, 1, "Number of worker threads");
408 static int ctl_debug = CTL_DEBUG_NONE;
409 TUNABLE_INT("kern.cam.ctl.debug", &ctl_debug);
410 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
411 &ctl_debug, 0, "Enabled debug flags");
414 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
415 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
416 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
417 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
419 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
421 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
423 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
424 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
425 static int ctl_init(void);
426 void ctl_shutdown(void);
427 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
428 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
429 static int ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
430 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
431 struct ctl_ooa *ooa_hdr,
432 struct ctl_ooa_entry *kern_entries);
433 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
435 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
436 struct ctl_be_lun *be_lun);
437 static int ctl_free_lun(struct ctl_lun *lun);
438 static void ctl_create_lun(struct ctl_be_lun *be_lun);
439 static struct ctl_port * ctl_io_port(struct ctl_io_hdr *io_hdr);
441 static int ctl_do_mode_select(union ctl_io *io);
442 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
443 uint64_t res_key, uint64_t sa_res_key,
444 uint8_t type, uint32_t residx,
445 struct ctl_scsiio *ctsio,
446 struct scsi_per_res_out *cdb,
447 struct scsi_per_res_out_parms* param);
448 static void ctl_pro_preempt_other(struct ctl_lun *lun,
449 union ctl_ha_msg *msg);
450 static void ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg);
451 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
452 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
453 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
454 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
455 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
456 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
458 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
460 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
461 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
462 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
463 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
464 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
465 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
467 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
468 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
469 union ctl_io *pending_io, union ctl_io *ooa_io);
470 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
471 union ctl_io *starting_io);
472 static int ctl_check_blocked(struct ctl_lun *lun);
473 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
474 const struct ctl_cmd_entry *entry,
475 struct ctl_scsiio *ctsio);
476 static void ctl_failover_lun(union ctl_io *io);
477 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
478 struct ctl_scsiio *ctsio);
479 static int ctl_scsiio(struct ctl_scsiio *ctsio);
481 static int ctl_bus_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
482 static int ctl_target_reset(struct ctl_softc *ctl_softc, union ctl_io *io,
483 ctl_ua_type ua_type);
484 static int ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io,
485 ctl_ua_type ua_type);
486 static int ctl_lun_reset(struct ctl_softc *ctl_softc, union ctl_io *io);
487 static int ctl_abort_task(union ctl_io *io);
488 static int ctl_abort_task_set(union ctl_io *io);
489 static int ctl_query_task(union ctl_io *io, int task_set);
490 static int ctl_i_t_nexus_reset(union ctl_io *io);
491 static int ctl_query_async_event(union ctl_io *io);
492 static void ctl_run_task(union ctl_io *io);
494 static void ctl_datamove_timer_wakeup(void *arg);
495 static void ctl_done_timer_wakeup(void *arg);
496 #endif /* CTL_IO_DELAY */
498 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
499 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
500 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
501 static void ctl_datamove_remote_write(union ctl_io *io);
502 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
503 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
504 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
505 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
506 ctl_ha_dt_cb callback);
507 static void ctl_datamove_remote_read(union ctl_io *io);
508 static void ctl_datamove_remote(union ctl_io *io);
509 static void ctl_process_done(union ctl_io *io);
510 static void ctl_lun_thread(void *arg);
511 static void ctl_thresh_thread(void *arg);
512 static void ctl_work_thread(void *arg);
513 static void ctl_enqueue_incoming(union ctl_io *io);
514 static void ctl_enqueue_rtr(union ctl_io *io);
515 static void ctl_enqueue_done(union ctl_io *io);
516 static void ctl_enqueue_isc(union ctl_io *io);
517 static const struct ctl_cmd_entry *
518 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
519 static const struct ctl_cmd_entry *
520 ctl_validate_command(struct ctl_scsiio *ctsio);
521 static int ctl_cmd_applicable(uint8_t lun_type,
522 const struct ctl_cmd_entry *entry);
524 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
525 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
526 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
527 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
530 * Load the serialization table. This isn't very pretty, but is probably
531 * the easiest way to do it.
533 #include "ctl_ser_table.c"
536 * We only need to define open, close and ioctl routines for this driver.
538 static struct cdevsw ctl_cdevsw = {
539 .d_version = D_VERSION,
542 .d_close = ctl_close,
543 .d_ioctl = ctl_ioctl,
548 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
550 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
552 static moduledata_t ctl_moduledata = {
554 ctl_module_event_handler,
558 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
559 MODULE_VERSION(ctl, 1);
561 static struct ctl_frontend ha_frontend =
567 ctl_ha_datamove(union ctl_io *io)
570 struct ctl_sg_entry *sgl;
571 union ctl_ha_msg msg;
572 uint32_t sg_entries_sent;
573 int do_sg_copy, i, j;
575 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
576 memset(&msg.dt, 0, sizeof(msg.dt));
577 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
578 msg.hdr.original_sc = io->io_hdr.original_sc;
579 msg.hdr.serializing_sc = io;
580 msg.hdr.nexus = io->io_hdr.nexus;
581 msg.hdr.status = io->io_hdr.status;
582 msg.dt.flags = io->io_hdr.flags;
585 * We convert everything into a S/G list here. We can't
586 * pass by reference, only by value between controllers.
587 * So we can't pass a pointer to the S/G list, only as many
588 * S/G entries as we can fit in here. If it's possible for
589 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
590 * then we need to break this up into multiple transfers.
592 if (io->scsiio.kern_sg_entries == 0) {
593 msg.dt.kern_sg_entries = 1;
595 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
596 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
598 /* XXX KDM use busdma here! */
599 msg.dt.sg_list[0].addr =
600 (void *)vtophys(io->scsiio.kern_data_ptr);
603 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
604 ("HA does not support BUS_ADDR"));
605 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
607 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
610 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
614 msg.dt.kern_data_len = io->scsiio.kern_data_len;
615 msg.dt.kern_total_len = io->scsiio.kern_total_len;
616 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
617 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
618 msg.dt.sg_sequence = 0;
621 * Loop until we've sent all of the S/G entries. On the
622 * other end, we'll recompose these S/G entries into one
623 * contiguous list before processing.
625 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
626 msg.dt.sg_sequence++) {
627 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
628 sizeof(msg.dt.sg_list[0])),
629 msg.dt.kern_sg_entries - sg_entries_sent);
630 if (do_sg_copy != 0) {
631 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
632 for (i = sg_entries_sent, j = 0;
633 i < msg.dt.cur_sg_entries; i++, j++) {
635 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
636 msg.dt.sg_list[j].addr = sgl[i].addr;
638 /* XXX KDM use busdma here! */
639 msg.dt.sg_list[j].addr =
640 (void *)vtophys(sgl[i].addr);
643 KASSERT((io->io_hdr.flags &
644 CTL_FLAG_BUS_ADDR) == 0,
645 ("HA does not support BUS_ADDR"));
646 msg.dt.sg_list[j].addr = sgl[i].addr;
648 msg.dt.sg_list[j].len = sgl[i].len;
652 sg_entries_sent += msg.dt.cur_sg_entries;
653 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
654 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
655 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
656 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
657 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
658 io->io_hdr.port_status = 31341;
659 io->scsiio.be_move_done(io);
662 msg.dt.sent_sg_entries = sg_entries_sent;
666 * Officially handover the request from us to peer.
667 * If failover has just happened, then we must return error.
668 * If failover happen just after, then it is not our problem.
671 mtx_lock(&lun->lun_lock);
672 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
674 mtx_unlock(&lun->lun_lock);
675 io->io_hdr.port_status = 31342;
676 io->scsiio.be_move_done(io);
679 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
680 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
682 mtx_unlock(&lun->lun_lock);
686 ctl_ha_done(union ctl_io *io)
688 union ctl_ha_msg msg;
690 if (io->io_hdr.io_type == CTL_IO_SCSI) {
691 memset(&msg, 0, sizeof(msg));
692 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
693 msg.hdr.original_sc = io->io_hdr.original_sc;
694 msg.hdr.nexus = io->io_hdr.nexus;
695 msg.hdr.status = io->io_hdr.status;
696 msg.scsi.scsi_status = io->scsiio.scsi_status;
697 msg.scsi.tag_num = io->scsiio.tag_num;
698 msg.scsi.tag_type = io->scsiio.tag_type;
699 msg.scsi.sense_len = io->scsiio.sense_len;
700 msg.scsi.sense_residual = io->scsiio.sense_residual;
701 msg.scsi.residual = io->scsiio.residual;
702 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
703 io->scsiio.sense_len);
704 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
705 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
706 msg.scsi.sense_len, M_WAITOK);
712 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
713 union ctl_ha_msg *msg_info)
715 struct ctl_scsiio *ctsio;
717 if (msg_info->hdr.original_sc == NULL) {
718 printf("%s: original_sc == NULL!\n", __func__);
719 /* XXX KDM now what? */
723 ctsio = &msg_info->hdr.original_sc->scsiio;
724 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
725 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
726 ctsio->io_hdr.status = msg_info->hdr.status;
727 ctsio->scsi_status = msg_info->scsi.scsi_status;
728 ctsio->sense_len = msg_info->scsi.sense_len;
729 ctsio->sense_residual = msg_info->scsi.sense_residual;
730 ctsio->residual = msg_info->scsi.residual;
731 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
732 msg_info->scsi.sense_len);
733 ctl_enqueue_isc((union ctl_io *)ctsio);
737 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
738 union ctl_ha_msg *msg_info)
740 struct ctl_scsiio *ctsio;
742 if (msg_info->hdr.serializing_sc == NULL) {
743 printf("%s: serializing_sc == NULL!\n", __func__);
744 /* XXX KDM now what? */
748 ctsio = &msg_info->hdr.serializing_sc->scsiio;
749 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
750 ctl_enqueue_isc((union ctl_io *)ctsio);
754 ctl_isc_announce_lun(struct ctl_lun *lun)
756 struct ctl_softc *softc = lun->ctl_softc;
757 union ctl_ha_msg *msg;
758 struct ctl_ha_msg_lun_pr_key pr_key;
761 if (softc->ha_link != CTL_HA_LINK_ONLINE)
763 mtx_lock(&lun->lun_lock);
764 i = sizeof(msg->lun);
766 i += lun->lun_devid->len;
767 i += sizeof(pr_key) * lun->pr_key_count;
769 mtx_unlock(&lun->lun_lock);
770 msg = malloc(i, M_CTL, M_WAITOK);
771 mtx_lock(&lun->lun_lock);
772 k = sizeof(msg->lun);
774 k += lun->lun_devid->len;
775 k += sizeof(pr_key) * lun->pr_key_count;
781 bzero(&msg->lun, sizeof(msg->lun));
782 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
783 msg->hdr.nexus.targ_lun = lun->lun;
784 msg->hdr.nexus.targ_mapped_lun = lun->lun;
785 msg->lun.flags = lun->flags;
786 msg->lun.pr_generation = lun->pr_generation;
787 msg->lun.pr_res_idx = lun->pr_res_idx;
788 msg->lun.pr_res_type = lun->pr_res_type;
789 msg->lun.pr_key_count = lun->pr_key_count;
791 if (lun->lun_devid) {
792 msg->lun.lun_devid_len = lun->lun_devid->len;
793 memcpy(&msg->lun.data[i], lun->lun_devid->data,
794 msg->lun.lun_devid_len);
795 i += msg->lun.lun_devid_len;
797 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
798 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
801 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
804 mtx_unlock(&lun->lun_lock);
805 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
809 if (lun->flags & CTL_LUN_PRIMARY_SC) {
810 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
811 ctl_isc_announce_mode(lun, -1,
812 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
813 lun->mode_pages.index[i].subpage);
819 ctl_isc_announce_port(struct ctl_port *port)
821 struct ctl_softc *softc = port->ctl_softc;
822 union ctl_ha_msg *msg;
825 if (port->targ_port < softc->port_min ||
826 port->targ_port >= softc->port_max ||
827 softc->ha_link != CTL_HA_LINK_ONLINE)
829 i = sizeof(msg->port) + strlen(port->port_name) + 1;
831 i += sizeof(uint32_t) * CTL_MAX_LUNS;
832 if (port->port_devid)
833 i += port->port_devid->len;
834 if (port->target_devid)
835 i += port->target_devid->len;
836 if (port->init_devid)
837 i += port->init_devid->len;
838 msg = malloc(i, M_CTL, M_WAITOK);
839 bzero(&msg->port, sizeof(msg->port));
840 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
841 msg->hdr.nexus.targ_port = port->targ_port;
842 msg->port.port_type = port->port_type;
843 msg->port.physical_port = port->physical_port;
844 msg->port.virtual_port = port->virtual_port;
845 msg->port.status = port->status;
847 msg->port.name_len = sprintf(&msg->port.data[i],
848 "%d:%s", softc->ha_id, port->port_name) + 1;
849 i += msg->port.name_len;
851 msg->port.lun_map_len = sizeof(uint32_t) * CTL_MAX_LUNS;
852 memcpy(&msg->port.data[i], port->lun_map,
853 msg->port.lun_map_len);
854 i += msg->port.lun_map_len;
856 if (port->port_devid) {
857 msg->port.port_devid_len = port->port_devid->len;
858 memcpy(&msg->port.data[i], port->port_devid->data,
859 msg->port.port_devid_len);
860 i += msg->port.port_devid_len;
862 if (port->target_devid) {
863 msg->port.target_devid_len = port->target_devid->len;
864 memcpy(&msg->port.data[i], port->target_devid->data,
865 msg->port.target_devid_len);
866 i += msg->port.target_devid_len;
868 if (port->init_devid) {
869 msg->port.init_devid_len = port->init_devid->len;
870 memcpy(&msg->port.data[i], port->init_devid->data,
871 msg->port.init_devid_len);
872 i += msg->port.init_devid_len;
874 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
880 ctl_isc_announce_iid(struct ctl_port *port, int iid)
882 struct ctl_softc *softc = port->ctl_softc;
883 union ctl_ha_msg *msg;
886 if (port->targ_port < softc->port_min ||
887 port->targ_port >= softc->port_max ||
888 softc->ha_link != CTL_HA_LINK_ONLINE)
890 mtx_lock(&softc->ctl_lock);
891 i = sizeof(msg->iid);
893 if (port->wwpn_iid[iid].name)
894 l = strlen(port->wwpn_iid[iid].name) + 1;
896 msg = malloc(i, M_CTL, M_NOWAIT);
898 mtx_unlock(&softc->ctl_lock);
901 bzero(&msg->iid, sizeof(msg->iid));
902 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
903 msg->hdr.nexus.targ_port = port->targ_port;
904 msg->hdr.nexus.initid = iid;
905 msg->iid.in_use = port->wwpn_iid[iid].in_use;
906 msg->iid.name_len = l;
907 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
908 if (port->wwpn_iid[iid].name)
909 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
910 mtx_unlock(&softc->ctl_lock);
911 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
916 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
917 uint8_t page, uint8_t subpage)
919 struct ctl_softc *softc = lun->ctl_softc;
920 union ctl_ha_msg msg;
923 if (softc->ha_link != CTL_HA_LINK_ONLINE)
925 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
926 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
927 page && lun->mode_pages.index[i].subpage == subpage)
930 if (i == CTL_NUM_MODE_PAGES)
933 /* Don't try to replicate pages not present on this device. */
934 if (lun->mode_pages.index[i].page_data == NULL)
937 bzero(&msg.mode, sizeof(msg.mode));
938 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
939 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
940 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
941 msg.hdr.nexus.targ_lun = lun->lun;
942 msg.hdr.nexus.targ_mapped_lun = lun->lun;
943 msg.mode.page_code = page;
944 msg.mode.subpage = subpage;
945 msg.mode.page_len = lun->mode_pages.index[i].page_len;
946 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
948 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
953 ctl_isc_ha_link_up(struct ctl_softc *softc)
955 struct ctl_port *port;
957 union ctl_ha_msg msg;
960 /* Announce this node parameters to peer for validation. */
961 msg.login.msg_type = CTL_MSG_LOGIN;
962 msg.login.version = CTL_HA_VERSION;
963 msg.login.ha_mode = softc->ha_mode;
964 msg.login.ha_id = softc->ha_id;
965 msg.login.max_luns = CTL_MAX_LUNS;
966 msg.login.max_ports = CTL_MAX_PORTS;
967 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
968 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
971 STAILQ_FOREACH(port, &softc->port_list, links) {
972 ctl_isc_announce_port(port);
973 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
974 if (port->wwpn_iid[i].in_use)
975 ctl_isc_announce_iid(port, i);
978 STAILQ_FOREACH(lun, &softc->lun_list, links)
979 ctl_isc_announce_lun(lun);
983 ctl_isc_ha_link_down(struct ctl_softc *softc)
985 struct ctl_port *port;
990 mtx_lock(&softc->ctl_lock);
991 STAILQ_FOREACH(lun, &softc->lun_list, links) {
992 mtx_lock(&lun->lun_lock);
993 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
994 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
995 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
997 mtx_unlock(&lun->lun_lock);
999 mtx_unlock(&softc->ctl_lock);
1000 io = ctl_alloc_io(softc->othersc_pool);
1001 mtx_lock(&softc->ctl_lock);
1003 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1004 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1005 ctl_enqueue_isc(io);
1008 STAILQ_FOREACH(port, &softc->port_list, links) {
1009 if (port->targ_port >= softc->port_min &&
1010 port->targ_port < softc->port_max)
1012 port->status &= ~CTL_PORT_STATUS_ONLINE;
1013 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1014 port->wwpn_iid[i].in_use = 0;
1015 free(port->wwpn_iid[i].name, M_CTL);
1016 port->wwpn_iid[i].name = NULL;
1019 mtx_unlock(&softc->ctl_lock);
1023 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1025 struct ctl_lun *lun;
1026 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1028 mtx_lock(&softc->ctl_lock);
1029 if (msg->hdr.nexus.targ_lun < CTL_MAX_LUNS &&
1030 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) != NULL) {
1031 mtx_lock(&lun->lun_lock);
1032 mtx_unlock(&softc->ctl_lock);
1033 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES &&
1035 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1036 if (msg->ua.ua_all) {
1038 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1040 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1043 ctl_est_ua(lun, iid, msg->ua.ua_type);
1045 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1047 mtx_unlock(&lun->lun_lock);
1049 mtx_unlock(&softc->ctl_lock);
1053 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1055 struct ctl_lun *lun;
1056 struct ctl_ha_msg_lun_pr_key pr_key;
1058 ctl_lun_flags oflags;
1061 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1062 mtx_lock(&softc->ctl_lock);
1063 if ((targ_lun >= CTL_MAX_LUNS) ||
1064 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1065 mtx_unlock(&softc->ctl_lock);
1068 mtx_lock(&lun->lun_lock);
1069 mtx_unlock(&softc->ctl_lock);
1070 if (lun->flags & CTL_LUN_DISABLED) {
1071 mtx_unlock(&lun->lun_lock);
1074 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1075 if (msg->lun.lun_devid_len != i || (i > 0 &&
1076 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1077 mtx_unlock(&lun->lun_lock);
1078 printf("%s: Received conflicting HA LUN %d\n",
1079 __func__, msg->hdr.nexus.targ_lun);
1082 /* Record whether peer is primary. */
1083 oflags = lun->flags;
1084 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1085 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1086 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1088 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1089 if (oflags != lun->flags)
1090 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1092 /* If peer is primary and we are not -- use data */
1093 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1094 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1095 lun->pr_generation = msg->lun.pr_generation;
1096 lun->pr_res_idx = msg->lun.pr_res_idx;
1097 lun->pr_res_type = msg->lun.pr_res_type;
1098 lun->pr_key_count = msg->lun.pr_key_count;
1099 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1100 ctl_clr_prkey(lun, k);
1101 for (k = 0; k < msg->lun.pr_key_count; k++) {
1102 memcpy(&pr_key, &msg->lun.data[i],
1104 ctl_alloc_prkey(lun, pr_key.pr_iid);
1105 ctl_set_prkey(lun, pr_key.pr_iid,
1107 i += sizeof(pr_key);
1111 mtx_unlock(&lun->lun_lock);
1112 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1113 __func__, msg->hdr.nexus.targ_lun,
1114 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1115 "primary" : "secondary"));
1117 /* If we are primary but peer doesn't know -- notify */
1118 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1119 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1120 ctl_isc_announce_lun(lun);
1125 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1127 struct ctl_port *port;
1128 struct ctl_lun *lun;
1131 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1133 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1134 msg->hdr.nexus.targ_port));
1136 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1137 port->frontend = &ha_frontend;
1138 port->targ_port = msg->hdr.nexus.targ_port;
1139 port->fe_datamove = ctl_ha_datamove;
1140 port->fe_done = ctl_ha_done;
1141 } else if (port->frontend == &ha_frontend) {
1142 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1143 msg->hdr.nexus.targ_port));
1146 printf("%s: Received conflicting HA port %d\n",
1147 __func__, msg->hdr.nexus.targ_port);
1150 port->port_type = msg->port.port_type;
1151 port->physical_port = msg->port.physical_port;
1152 port->virtual_port = msg->port.virtual_port;
1153 port->status = msg->port.status;
1155 free(port->port_name, M_CTL);
1156 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1158 i += msg->port.name_len;
1159 if (msg->port.lun_map_len != 0) {
1160 if (port->lun_map == NULL)
1161 port->lun_map = malloc(sizeof(uint32_t) * CTL_MAX_LUNS,
1163 memcpy(port->lun_map, &msg->port.data[i],
1164 sizeof(uint32_t) * CTL_MAX_LUNS);
1165 i += msg->port.lun_map_len;
1167 free(port->lun_map, M_CTL);
1168 port->lun_map = NULL;
1170 if (msg->port.port_devid_len != 0) {
1171 if (port->port_devid == NULL ||
1172 port->port_devid->len != msg->port.port_devid_len) {
1173 free(port->port_devid, M_CTL);
1174 port->port_devid = malloc(sizeof(struct ctl_devid) +
1175 msg->port.port_devid_len, M_CTL, M_WAITOK);
1177 memcpy(port->port_devid->data, &msg->port.data[i],
1178 msg->port.port_devid_len);
1179 port->port_devid->len = msg->port.port_devid_len;
1180 i += msg->port.port_devid_len;
1182 free(port->port_devid, M_CTL);
1183 port->port_devid = NULL;
1185 if (msg->port.target_devid_len != 0) {
1186 if (port->target_devid == NULL ||
1187 port->target_devid->len != msg->port.target_devid_len) {
1188 free(port->target_devid, M_CTL);
1189 port->target_devid = malloc(sizeof(struct ctl_devid) +
1190 msg->port.target_devid_len, M_CTL, M_WAITOK);
1192 memcpy(port->target_devid->data, &msg->port.data[i],
1193 msg->port.target_devid_len);
1194 port->target_devid->len = msg->port.target_devid_len;
1195 i += msg->port.target_devid_len;
1197 free(port->target_devid, M_CTL);
1198 port->target_devid = NULL;
1200 if (msg->port.init_devid_len != 0) {
1201 if (port->init_devid == NULL ||
1202 port->init_devid->len != msg->port.init_devid_len) {
1203 free(port->init_devid, M_CTL);
1204 port->init_devid = malloc(sizeof(struct ctl_devid) +
1205 msg->port.init_devid_len, M_CTL, M_WAITOK);
1207 memcpy(port->init_devid->data, &msg->port.data[i],
1208 msg->port.init_devid_len);
1209 port->init_devid->len = msg->port.init_devid_len;
1210 i += msg->port.init_devid_len;
1212 free(port->init_devid, M_CTL);
1213 port->init_devid = NULL;
1216 if (ctl_port_register(port) != 0) {
1217 printf("%s: ctl_port_register() failed with error\n",
1221 mtx_lock(&softc->ctl_lock);
1222 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1223 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
1225 mtx_lock(&lun->lun_lock);
1226 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1227 mtx_unlock(&lun->lun_lock);
1229 mtx_unlock(&softc->ctl_lock);
1233 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1235 struct ctl_port *port;
1238 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1240 printf("%s: Received IID for unknown port %d\n",
1241 __func__, msg->hdr.nexus.targ_port);
1244 iid = msg->hdr.nexus.initid;
1245 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1246 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1247 free(port->wwpn_iid[iid].name, M_CTL);
1248 if (msg->iid.name_len) {
1249 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1250 msg->iid.name_len, M_CTL);
1252 port->wwpn_iid[iid].name = NULL;
1256 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1259 if (msg->login.version != CTL_HA_VERSION) {
1260 printf("CTL HA peers have different versions %d != %d\n",
1261 msg->login.version, CTL_HA_VERSION);
1262 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1265 if (msg->login.ha_mode != softc->ha_mode) {
1266 printf("CTL HA peers have different ha_mode %d != %d\n",
1267 msg->login.ha_mode, softc->ha_mode);
1268 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1271 if (msg->login.ha_id == softc->ha_id) {
1272 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1273 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1276 if (msg->login.max_luns != CTL_MAX_LUNS ||
1277 msg->login.max_ports != CTL_MAX_PORTS ||
1278 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1279 printf("CTL HA peers have different limits\n");
1280 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1286 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1288 struct ctl_lun *lun;
1290 uint32_t initidx, targ_lun;
1292 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1293 mtx_lock(&softc->ctl_lock);
1294 if ((targ_lun >= CTL_MAX_LUNS) ||
1295 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
1296 mtx_unlock(&softc->ctl_lock);
1299 mtx_lock(&lun->lun_lock);
1300 mtx_unlock(&softc->ctl_lock);
1301 if (lun->flags & CTL_LUN_DISABLED) {
1302 mtx_unlock(&lun->lun_lock);
1305 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1306 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1307 msg->mode.page_code &&
1308 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1311 if (i == CTL_NUM_MODE_PAGES) {
1312 mtx_unlock(&lun->lun_lock);
1315 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1316 lun->mode_pages.index[i].page_len);
1317 initidx = ctl_get_initindex(&msg->hdr.nexus);
1319 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1320 mtx_unlock(&lun->lun_lock);
1324 * ISC (Inter Shelf Communication) event handler. Events from the HA
1325 * subsystem come in here.
1328 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1330 struct ctl_softc *softc = control_softc;
1332 struct ctl_prio *presio;
1333 ctl_ha_status isc_status;
1335 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1336 if (event == CTL_HA_EVT_MSG_RECV) {
1337 union ctl_ha_msg *msg, msgbuf;
1339 if (param > sizeof(msgbuf))
1340 msg = malloc(param, M_CTL, M_WAITOK);
1343 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1345 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1346 printf("%s: Error receiving message: %d\n",
1347 __func__, isc_status);
1353 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1354 switch (msg->hdr.msg_type) {
1355 case CTL_MSG_SERIALIZE:
1356 io = ctl_alloc_io(softc->othersc_pool);
1358 // populate ctsio from msg
1359 io->io_hdr.io_type = CTL_IO_SCSI;
1360 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1361 io->io_hdr.original_sc = msg->hdr.original_sc;
1362 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1365 * If we're in serialization-only mode, we don't
1366 * want to go through full done processing. Thus
1369 * XXX KDM add another flag that is more specific.
1371 if (softc->ha_mode != CTL_HA_MODE_XFER)
1372 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1373 io->io_hdr.nexus = msg->hdr.nexus;
1375 printf("port %u, iid %u, lun %u\n",
1376 io->io_hdr.nexus.targ_port,
1377 io->io_hdr.nexus.initid,
1378 io->io_hdr.nexus.targ_lun);
1380 io->scsiio.tag_num = msg->scsi.tag_num;
1381 io->scsiio.tag_type = msg->scsi.tag_type;
1383 io->io_hdr.start_time = time_uptime;
1384 getbinuptime(&io->io_hdr.start_bt);
1385 #endif /* CTL_TIME_IO */
1386 io->scsiio.cdb_len = msg->scsi.cdb_len;
1387 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1389 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1390 const struct ctl_cmd_entry *entry;
1392 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1393 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1395 entry->flags & CTL_FLAG_DATA_MASK;
1397 ctl_enqueue_isc(io);
1400 /* Performed on the Originating SC, XFER mode only */
1401 case CTL_MSG_DATAMOVE: {
1402 struct ctl_sg_entry *sgl;
1405 io = msg->hdr.original_sc;
1407 printf("%s: original_sc == NULL!\n", __func__);
1408 /* XXX KDM do something here */
1411 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1412 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1414 * Keep track of this, we need to send it back over
1415 * when the datamove is complete.
1417 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1418 if (msg->hdr.status == CTL_SUCCESS)
1419 io->io_hdr.status = msg->hdr.status;
1421 if (msg->dt.sg_sequence == 0) {
1423 getbinuptime(&io->io_hdr.dma_start_bt);
1425 i = msg->dt.kern_sg_entries +
1426 msg->dt.kern_data_len /
1427 CTL_HA_DATAMOVE_SEGMENT + 1;
1428 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1430 io->io_hdr.remote_sglist = sgl;
1431 io->io_hdr.local_sglist =
1432 &sgl[msg->dt.kern_sg_entries];
1434 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1436 io->scsiio.kern_sg_entries =
1437 msg->dt.kern_sg_entries;
1438 io->scsiio.rem_sg_entries =
1439 msg->dt.kern_sg_entries;
1440 io->scsiio.kern_data_len =
1441 msg->dt.kern_data_len;
1442 io->scsiio.kern_total_len =
1443 msg->dt.kern_total_len;
1444 io->scsiio.kern_data_resid =
1445 msg->dt.kern_data_resid;
1446 io->scsiio.kern_rel_offset =
1447 msg->dt.kern_rel_offset;
1448 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1449 io->io_hdr.flags |= msg->dt.flags &
1452 sgl = (struct ctl_sg_entry *)
1453 io->scsiio.kern_data_ptr;
1455 for (i = msg->dt.sent_sg_entries, j = 0;
1456 i < (msg->dt.sent_sg_entries +
1457 msg->dt.cur_sg_entries); i++, j++) {
1458 sgl[i].addr = msg->dt.sg_list[j].addr;
1459 sgl[i].len = msg->dt.sg_list[j].len;
1462 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1463 __func__, sgl[i].addr, sgl[i].len, j, i);
1468 * If this is the last piece of the I/O, we've got
1469 * the full S/G list. Queue processing in the thread.
1470 * Otherwise wait for the next piece.
1472 if (msg->dt.sg_last != 0)
1473 ctl_enqueue_isc(io);
1476 /* Performed on the Serializing (primary) SC, XFER mode only */
1477 case CTL_MSG_DATAMOVE_DONE: {
1478 if (msg->hdr.serializing_sc == NULL) {
1479 printf("%s: serializing_sc == NULL!\n",
1481 /* XXX KDM now what? */
1485 * We grab the sense information here in case
1486 * there was a failure, so we can return status
1487 * back to the initiator.
1489 io = msg->hdr.serializing_sc;
1490 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1491 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1492 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1493 io->io_hdr.port_status = msg->scsi.fetd_status;
1494 io->scsiio.residual = msg->scsi.residual;
1495 if (msg->hdr.status != CTL_STATUS_NONE) {
1496 io->io_hdr.status = msg->hdr.status;
1497 io->scsiio.scsi_status = msg->scsi.scsi_status;
1498 io->scsiio.sense_len = msg->scsi.sense_len;
1499 io->scsiio.sense_residual =msg->scsi.sense_residual;
1500 memcpy(&io->scsiio.sense_data,
1501 &msg->scsi.sense_data,
1502 msg->scsi.sense_len);
1503 if (msg->hdr.status == CTL_SUCCESS)
1504 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1506 ctl_enqueue_isc(io);
1510 /* Preformed on Originating SC, SER_ONLY mode */
1512 io = msg->hdr.original_sc;
1514 printf("%s: original_sc == NULL!\n",
1518 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1519 io->io_hdr.msg_type = CTL_MSG_R2R;
1520 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1521 ctl_enqueue_isc(io);
1525 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1527 * Performed on the Originating (i.e. secondary) SC in XFER
1530 case CTL_MSG_FINISH_IO:
1531 if (softc->ha_mode == CTL_HA_MODE_XFER)
1532 ctl_isc_handler_finish_xfer(softc, msg);
1534 ctl_isc_handler_finish_ser_only(softc, msg);
1537 /* Preformed on Originating SC */
1538 case CTL_MSG_BAD_JUJU:
1539 io = msg->hdr.original_sc;
1541 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1545 ctl_copy_sense_data(msg, io);
1547 * IO should have already been cleaned up on other
1548 * SC so clear this flag so we won't send a message
1549 * back to finish the IO there.
1551 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1552 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1554 /* io = msg->hdr.serializing_sc; */
1555 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1556 ctl_enqueue_isc(io);
1559 /* Handle resets sent from the other side */
1560 case CTL_MSG_MANAGE_TASKS: {
1561 struct ctl_taskio *taskio;
1562 taskio = (struct ctl_taskio *)ctl_alloc_io(
1563 softc->othersc_pool);
1564 ctl_zero_io((union ctl_io *)taskio);
1565 taskio->io_hdr.io_type = CTL_IO_TASK;
1566 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1567 taskio->io_hdr.nexus = msg->hdr.nexus;
1568 taskio->task_action = msg->task.task_action;
1569 taskio->tag_num = msg->task.tag_num;
1570 taskio->tag_type = msg->task.tag_type;
1572 taskio->io_hdr.start_time = time_uptime;
1573 getbinuptime(&taskio->io_hdr.start_bt);
1574 #endif /* CTL_TIME_IO */
1575 ctl_run_task((union ctl_io *)taskio);
1578 /* Persistent Reserve action which needs attention */
1579 case CTL_MSG_PERS_ACTION:
1580 presio = (struct ctl_prio *)ctl_alloc_io(
1581 softc->othersc_pool);
1582 ctl_zero_io((union ctl_io *)presio);
1583 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1584 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1585 presio->io_hdr.nexus = msg->hdr.nexus;
1586 presio->pr_msg = msg->pr;
1587 ctl_enqueue_isc((union ctl_io *)presio);
1590 ctl_isc_ua(softc, msg, param);
1592 case CTL_MSG_PORT_SYNC:
1593 ctl_isc_port_sync(softc, msg, param);
1595 case CTL_MSG_LUN_SYNC:
1596 ctl_isc_lun_sync(softc, msg, param);
1598 case CTL_MSG_IID_SYNC:
1599 ctl_isc_iid_sync(softc, msg, param);
1602 ctl_isc_login(softc, msg, param);
1604 case CTL_MSG_MODE_SYNC:
1605 ctl_isc_mode_sync(softc, msg, param);
1608 printf("Received HA message of unknown type %d\n",
1610 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1615 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1616 printf("CTL: HA link status changed from %d to %d\n",
1617 softc->ha_link, param);
1618 if (param == softc->ha_link)
1620 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1621 softc->ha_link = param;
1622 ctl_isc_ha_link_down(softc);
1624 softc->ha_link = param;
1625 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1626 ctl_isc_ha_link_up(softc);
1630 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1636 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1639 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1640 src->scsi.sense_len);
1641 dest->scsiio.scsi_status = src->scsi.scsi_status;
1642 dest->scsiio.sense_len = src->scsi.sense_len;
1643 dest->io_hdr.status = src->hdr.status;
1647 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1650 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1651 src->scsiio.sense_len);
1652 dest->scsi.scsi_status = src->scsiio.scsi_status;
1653 dest->scsi.sense_len = src->scsiio.sense_len;
1654 dest->hdr.status = src->io_hdr.status;
1658 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1660 struct ctl_softc *softc = lun->ctl_softc;
1663 if (initidx < softc->init_min || initidx >= softc->init_max)
1665 mtx_assert(&lun->lun_lock, MA_OWNED);
1666 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1669 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1673 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1677 mtx_assert(&lun->lun_lock, MA_OWNED);
1678 if (lun->pending_ua[port] == NULL)
1680 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1681 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1683 lun->pending_ua[port][i] |= ua;
1688 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1690 struct ctl_softc *softc = lun->ctl_softc;
1693 mtx_assert(&lun->lun_lock, MA_OWNED);
1694 for (i = softc->port_min; i < softc->port_max; i++)
1695 ctl_est_ua_port(lun, i, except, ua);
1699 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1701 struct ctl_softc *softc = lun->ctl_softc;
1704 if (initidx < softc->init_min || initidx >= softc->init_max)
1706 mtx_assert(&lun->lun_lock, MA_OWNED);
1707 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1710 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1714 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1716 struct ctl_softc *softc = lun->ctl_softc;
1719 mtx_assert(&lun->lun_lock, MA_OWNED);
1720 for (i = softc->port_min; i < softc->port_max; i++) {
1721 if (lun->pending_ua[i] == NULL)
1723 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1724 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1726 lun->pending_ua[i][j] &= ~ua;
1732 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1733 ctl_ua_type ua_type)
1735 struct ctl_lun *lun;
1737 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1738 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1739 mtx_lock(&lun->lun_lock);
1740 ctl_clr_ua(lun, initidx, ua_type);
1741 mtx_unlock(&lun->lun_lock);
1746 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1748 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1749 struct ctl_lun *lun;
1750 struct ctl_lun_req ireq;
1753 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1754 error = sysctl_handle_int(oidp, &value, 0, req);
1755 if ((error != 0) || (req->newptr == NULL))
1758 mtx_lock(&softc->ctl_lock);
1760 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1762 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1763 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1764 mtx_unlock(&softc->ctl_lock);
1765 bzero(&ireq, sizeof(ireq));
1766 ireq.reqtype = CTL_LUNREQ_MODIFY;
1767 ireq.reqdata.modify.lun_id = lun->lun;
1768 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1770 if (ireq.status != CTL_LUN_OK) {
1771 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1772 __func__, ireq.status, ireq.error_str);
1774 mtx_lock(&softc->ctl_lock);
1776 mtx_unlock(&softc->ctl_lock);
1783 struct ctl_softc *softc;
1787 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1790 softc->dev = make_dev(&ctl_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600,
1792 softc->dev->si_drv1 = softc;
1794 sysctl_ctx_init(&softc->sysctl_ctx);
1795 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1796 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1797 CTLFLAG_RD, 0, "CAM Target Layer");
1799 if (softc->sysctl_tree == NULL) {
1800 printf("%s: unable to allocate sysctl tree\n", __func__);
1801 destroy_dev(softc->dev);
1802 free(control_softc, M_DEVBUF);
1803 control_softc = NULL;
1807 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1808 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1809 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1812 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1813 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1814 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1817 * In Copan's HA scheme, the "master" and "slave" roles are
1818 * figured out through the slot the controller is in. Although it
1819 * is an active/active system, someone has to be in charge.
1821 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1822 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1823 "HA head ID (0 - no HA)");
1824 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1825 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1826 softc->is_single = 1;
1827 softc->port_cnt = CTL_MAX_PORTS;
1828 softc->port_min = 0;
1830 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1831 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1833 softc->port_max = softc->port_min + softc->port_cnt;
1834 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1835 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1837 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1838 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1839 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1841 STAILQ_INIT(&softc->lun_list);
1842 STAILQ_INIT(&softc->pending_lun_queue);
1843 STAILQ_INIT(&softc->fe_list);
1844 STAILQ_INIT(&softc->port_list);
1845 STAILQ_INIT(&softc->be_list);
1846 ctl_tpc_init(softc);
1848 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1851 printf("ctl: can't allocate %d entry other SC pool, "
1852 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1855 softc->othersc_pool = other_pool;
1857 if (worker_threads <= 0)
1858 worker_threads = max(1, mp_ncpus / 4);
1859 if (worker_threads > CTL_MAX_THREADS)
1860 worker_threads = CTL_MAX_THREADS;
1862 for (i = 0; i < worker_threads; i++) {
1863 struct ctl_thread *thr = &softc->threads[i];
1865 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1866 thr->ctl_softc = softc;
1867 STAILQ_INIT(&thr->incoming_queue);
1868 STAILQ_INIT(&thr->rtr_queue);
1869 STAILQ_INIT(&thr->done_queue);
1870 STAILQ_INIT(&thr->isc_queue);
1872 error = kproc_kthread_add(ctl_work_thread, thr,
1873 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1875 printf("error creating CTL work thread!\n");
1876 ctl_pool_free(other_pool);
1880 error = kproc_kthread_add(ctl_lun_thread, softc,
1881 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1883 printf("error creating CTL lun thread!\n");
1884 ctl_pool_free(other_pool);
1887 error = kproc_kthread_add(ctl_thresh_thread, softc,
1888 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1890 printf("error creating CTL threshold thread!\n");
1891 ctl_pool_free(other_pool);
1895 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1896 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1897 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1899 if (softc->is_single == 0) {
1900 ctl_frontend_register(&ha_frontend);
1901 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1902 printf("ctl_init: ctl_ha_msg_init failed.\n");
1903 softc->is_single = 1;
1905 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1906 != CTL_HA_STATUS_SUCCESS) {
1907 printf("ctl_init: ctl_ha_msg_register failed.\n");
1908 softc->is_single = 1;
1917 struct ctl_softc *softc = control_softc;
1918 struct ctl_lun *lun, *next_lun;
1920 if (softc->is_single == 0) {
1921 ctl_ha_msg_shutdown(softc);
1922 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1923 != CTL_HA_STATUS_SUCCESS)
1924 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1925 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1926 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1927 ctl_frontend_deregister(&ha_frontend);
1930 mtx_lock(&softc->ctl_lock);
1932 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun)
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.
2786 STAILQ_FOREACH(lun, &softc->lun_list, 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);
3544 ctl_decode_lun(uint64_t encoded)
3547 uint32_t result = 0xffffffff;
3549 be64enc(lun, encoded);
3550 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3551 case RPL_LUNDATA_ATYP_PERIPH:
3552 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3553 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3556 case RPL_LUNDATA_ATYP_FLAT:
3557 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3558 lun[6] == 0 && lun[7] == 0)
3559 result = ((lun[0] & 0x3f) << 8) + lun[1];
3561 case RPL_LUNDATA_ATYP_EXTLUN:
3562 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3564 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3569 result = (lun[1] << 16) + (lun[2] << 8) +
3573 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3574 result = (lun[2] << 24) +
3575 (lun[3] << 16) + (lun[4] << 8) +
3580 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3581 result = 0xffffffff;
3590 ctl_encode_lun(uint32_t decoded)
3592 uint64_t l = decoded;
3595 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3597 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3599 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3601 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3604 static struct ctl_port *
3605 ctl_io_port(struct ctl_io_hdr *io_hdr)
3608 return (control_softc->ctl_ports[io_hdr->nexus.targ_port]);
3612 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3616 for (i = first; i < last; i++) {
3617 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3624 ctl_set_mask(uint32_t *mask, uint32_t bit)
3626 uint32_t chunk, piece;
3629 piece = bit % (sizeof(uint32_t) * 8);
3631 if ((mask[chunk] & (1 << piece)) != 0)
3634 mask[chunk] |= (1 << piece);
3640 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3642 uint32_t chunk, piece;
3645 piece = bit % (sizeof(uint32_t) * 8);
3647 if ((mask[chunk] & (1 << piece)) == 0)
3650 mask[chunk] &= ~(1 << piece);
3656 ctl_is_set(uint32_t *mask, uint32_t bit)
3658 uint32_t chunk, piece;
3661 piece = bit % (sizeof(uint32_t) * 8);
3663 if ((mask[chunk] & (1 << piece)) == 0)
3670 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3674 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3677 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3681 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3685 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3688 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3692 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3697 i = residx/CTL_MAX_INIT_PER_PORT;
3698 if (lun->pr_keys[i] != NULL)
3700 mtx_unlock(&lun->lun_lock);
3701 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3703 mtx_lock(&lun->lun_lock);
3704 if (lun->pr_keys[i] == NULL)
3705 lun->pr_keys[i] = p;
3711 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3715 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3716 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3717 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3721 * ctl_softc, pool_name, total_ctl_io are passed in.
3722 * npool is passed out.
3725 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3726 uint32_t total_ctl_io, void **npool)
3729 struct ctl_io_pool *pool;
3731 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3736 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3737 pool->ctl_softc = ctl_softc;
3738 pool->zone = uma_zsecond_create(pool->name, NULL,
3739 NULL, NULL, NULL, ctl_softc->io_zone);
3740 /* uma_prealloc(pool->zone, total_ctl_io); */
3744 *npool = ctl_softc->io_zone;
3750 ctl_pool_free(struct ctl_io_pool *pool)
3757 uma_zdestroy(pool->zone);
3763 ctl_alloc_io(void *pool_ref)
3767 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3769 io = uma_zalloc(pool->zone, M_WAITOK);
3771 io = uma_zalloc((uma_zone_t)pool_ref, M_WAITOK);
3774 io->io_hdr.pool = pool_ref;
3779 ctl_alloc_io_nowait(void *pool_ref)
3783 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3785 io = uma_zalloc(pool->zone, M_NOWAIT);
3787 io = uma_zalloc((uma_zone_t)pool_ref, M_NOWAIT);
3790 io->io_hdr.pool = pool_ref;
3795 ctl_free_io(union ctl_io *io)
3798 struct ctl_io_pool *pool;
3805 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3806 uma_zfree(pool->zone, io);
3808 uma_zfree((uma_zone_t)io->io_hdr.pool, io);
3813 ctl_zero_io(union ctl_io *io)
3821 * May need to preserve linked list pointers at some point too.
3823 pool_ref = io->io_hdr.pool;
3824 memset(io, 0, sizeof(*io));
3825 io->io_hdr.pool = pool_ref;
3829 ctl_expand_number(const char *buf, uint64_t *num)
3835 number = strtoq(buf, &endptr, 0);
3837 switch (tolower((unsigned char)*endptr)) {
3857 case '\0': /* No unit. */
3861 /* Unrecognized unit. */
3865 if ((number << shift) >> shift != number) {
3869 *num = number << shift;
3875 * This routine could be used in the future to load default and/or saved
3876 * mode page parameters for a particuar lun.
3879 ctl_init_page_index(struct ctl_lun *lun)
3882 struct ctl_page_index *page_index;
3886 memcpy(&lun->mode_pages.index, page_index_template,
3887 sizeof(page_index_template));
3889 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3891 page_index = &lun->mode_pages.index[i];
3892 if (lun->be_lun->lun_type == T_DIRECT &&
3893 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3895 if (lun->be_lun->lun_type == T_PROCESSOR &&
3896 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3898 if (lun->be_lun->lun_type == T_CDROM &&
3899 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3902 page_code = page_index->page_code & SMPH_PC_MASK;
3903 switch (page_code) {
3904 case SMS_RW_ERROR_RECOVERY_PAGE: {
3905 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3906 ("subpage %#x for page %#x is incorrect!",
3907 page_index->subpage, page_code));
3908 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3909 &rw_er_page_default,
3910 sizeof(rw_er_page_default));
3911 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3912 &rw_er_page_changeable,
3913 sizeof(rw_er_page_changeable));
3914 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3915 &rw_er_page_default,
3916 sizeof(rw_er_page_default));
3917 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3918 &rw_er_page_default,
3919 sizeof(rw_er_page_default));
3920 page_index->page_data =
3921 (uint8_t *)lun->mode_pages.rw_er_page;
3924 case SMS_FORMAT_DEVICE_PAGE: {
3925 struct scsi_format_page *format_page;
3927 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3928 ("subpage %#x for page %#x is incorrect!",
3929 page_index->subpage, page_code));
3932 * Sectors per track are set above. Bytes per
3933 * sector need to be set here on a per-LUN basis.
3935 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
3936 &format_page_default,
3937 sizeof(format_page_default));
3938 memcpy(&lun->mode_pages.format_page[
3939 CTL_PAGE_CHANGEABLE], &format_page_changeable,
3940 sizeof(format_page_changeable));
3941 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
3942 &format_page_default,
3943 sizeof(format_page_default));
3944 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
3945 &format_page_default,
3946 sizeof(format_page_default));
3948 format_page = &lun->mode_pages.format_page[
3950 scsi_ulto2b(lun->be_lun->blocksize,
3951 format_page->bytes_per_sector);
3953 format_page = &lun->mode_pages.format_page[
3955 scsi_ulto2b(lun->be_lun->blocksize,
3956 format_page->bytes_per_sector);
3958 format_page = &lun->mode_pages.format_page[
3960 scsi_ulto2b(lun->be_lun->blocksize,
3961 format_page->bytes_per_sector);
3963 page_index->page_data =
3964 (uint8_t *)lun->mode_pages.format_page;
3967 case SMS_RIGID_DISK_PAGE: {
3968 struct scsi_rigid_disk_page *rigid_disk_page;
3969 uint32_t sectors_per_cylinder;
3973 #endif /* !__XSCALE__ */
3975 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3976 ("subpage %#x for page %#x is incorrect!",
3977 page_index->subpage, page_code));
3980 * Rotation rate and sectors per track are set
3981 * above. We calculate the cylinders here based on
3982 * capacity. Due to the number of heads and
3983 * sectors per track we're using, smaller arrays
3984 * may turn out to have 0 cylinders. Linux and
3985 * FreeBSD don't pay attention to these mode pages
3986 * to figure out capacity, but Solaris does. It
3987 * seems to deal with 0 cylinders just fine, and
3988 * works out a fake geometry based on the capacity.
3990 memcpy(&lun->mode_pages.rigid_disk_page[
3991 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
3992 sizeof(rigid_disk_page_default));
3993 memcpy(&lun->mode_pages.rigid_disk_page[
3994 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
3995 sizeof(rigid_disk_page_changeable));
3997 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4001 * The divide method here will be more accurate,
4002 * probably, but results in floating point being
4003 * used in the kernel on i386 (__udivdi3()). On the
4004 * XScale, though, __udivdi3() is implemented in
4007 * The shift method for cylinder calculation is
4008 * accurate if sectors_per_cylinder is a power of
4009 * 2. Otherwise it might be slightly off -- you
4010 * might have a bit of a truncation problem.
4013 cylinders = (lun->be_lun->maxlba + 1) /
4014 sectors_per_cylinder;
4016 for (shift = 31; shift > 0; shift--) {
4017 if (sectors_per_cylinder & (1 << shift))
4020 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4024 * We've basically got 3 bytes, or 24 bits for the
4025 * cylinder size in the mode page. If we're over,
4026 * just round down to 2^24.
4028 if (cylinders > 0xffffff)
4029 cylinders = 0xffffff;
4031 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4033 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4035 if ((value = ctl_get_opt(&lun->be_lun->options,
4037 scsi_ulto2b(strtol(value, NULL, 0),
4038 rigid_disk_page->rotation_rate);
4041 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4042 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4043 sizeof(rigid_disk_page_default));
4044 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4045 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4046 sizeof(rigid_disk_page_default));
4048 page_index->page_data =
4049 (uint8_t *)lun->mode_pages.rigid_disk_page;
4052 case SMS_CACHING_PAGE: {
4053 struct scsi_caching_page *caching_page;
4055 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4056 ("subpage %#x for page %#x is incorrect!",
4057 page_index->subpage, page_code));
4058 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4059 &caching_page_default,
4060 sizeof(caching_page_default));
4061 memcpy(&lun->mode_pages.caching_page[
4062 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4063 sizeof(caching_page_changeable));
4064 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4065 &caching_page_default,
4066 sizeof(caching_page_default));
4067 caching_page = &lun->mode_pages.caching_page[
4069 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4070 if (value != NULL && strcmp(value, "off") == 0)
4071 caching_page->flags1 &= ~SCP_WCE;
4072 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4073 if (value != NULL && strcmp(value, "off") == 0)
4074 caching_page->flags1 |= SCP_RCD;
4075 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4076 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4077 sizeof(caching_page_default));
4078 page_index->page_data =
4079 (uint8_t *)lun->mode_pages.caching_page;
4082 case SMS_CONTROL_MODE_PAGE: {
4083 switch (page_index->subpage) {
4084 case SMS_SUBPAGE_PAGE_0: {
4085 struct scsi_control_page *control_page;
4087 memcpy(&lun->mode_pages.control_page[
4089 &control_page_default,
4090 sizeof(control_page_default));
4091 memcpy(&lun->mode_pages.control_page[
4092 CTL_PAGE_CHANGEABLE],
4093 &control_page_changeable,
4094 sizeof(control_page_changeable));
4095 memcpy(&lun->mode_pages.control_page[
4097 &control_page_default,
4098 sizeof(control_page_default));
4099 control_page = &lun->mode_pages.control_page[
4101 value = ctl_get_opt(&lun->be_lun->options,
4103 if (value != NULL &&
4104 strcmp(value, "unrestricted") == 0) {
4105 control_page->queue_flags &=
4106 ~SCP_QUEUE_ALG_MASK;
4107 control_page->queue_flags |=
4108 SCP_QUEUE_ALG_UNRESTRICTED;
4110 memcpy(&lun->mode_pages.control_page[
4112 &lun->mode_pages.control_page[
4114 sizeof(control_page_default));
4115 page_index->page_data =
4116 (uint8_t *)lun->mode_pages.control_page;
4120 memcpy(&lun->mode_pages.control_ext_page[
4122 &control_ext_page_default,
4123 sizeof(control_ext_page_default));
4124 memcpy(&lun->mode_pages.control_ext_page[
4125 CTL_PAGE_CHANGEABLE],
4126 &control_ext_page_changeable,
4127 sizeof(control_ext_page_changeable));
4128 memcpy(&lun->mode_pages.control_ext_page[
4130 &control_ext_page_default,
4131 sizeof(control_ext_page_default));
4132 memcpy(&lun->mode_pages.control_ext_page[
4134 &lun->mode_pages.control_ext_page[
4136 sizeof(control_ext_page_default));
4137 page_index->page_data =
4138 (uint8_t *)lun->mode_pages.control_ext_page;
4141 panic("subpage %#x for page %#x is incorrect!",
4142 page_index->subpage, page_code);
4146 case SMS_INFO_EXCEPTIONS_PAGE: {
4147 switch (page_index->subpage) {
4148 case SMS_SUBPAGE_PAGE_0:
4149 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4151 sizeof(ie_page_default));
4152 memcpy(&lun->mode_pages.ie_page[
4153 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4154 sizeof(ie_page_changeable));
4155 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4157 sizeof(ie_page_default));
4158 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4160 sizeof(ie_page_default));
4161 page_index->page_data =
4162 (uint8_t *)lun->mode_pages.ie_page;
4165 struct ctl_logical_block_provisioning_page *page;
4167 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4169 sizeof(lbp_page_default));
4170 memcpy(&lun->mode_pages.lbp_page[
4171 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4172 sizeof(lbp_page_changeable));
4173 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4175 sizeof(lbp_page_default));
4176 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4177 value = ctl_get_opt(&lun->be_lun->options,
4179 if (value != NULL &&
4180 ctl_expand_number(value, &ival) == 0) {
4181 page->descr[0].flags |= SLBPPD_ENABLED |
4183 if (lun->be_lun->blocksize)
4184 ival /= lun->be_lun->blocksize;
4187 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4188 page->descr[0].count);
4190 value = ctl_get_opt(&lun->be_lun->options,
4192 if (value != NULL &&
4193 ctl_expand_number(value, &ival) == 0) {
4194 page->descr[1].flags |= SLBPPD_ENABLED |
4196 if (lun->be_lun->blocksize)
4197 ival /= lun->be_lun->blocksize;
4200 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4201 page->descr[1].count);
4203 value = ctl_get_opt(&lun->be_lun->options,
4204 "pool-avail-threshold");
4205 if (value != NULL &&
4206 ctl_expand_number(value, &ival) == 0) {
4207 page->descr[2].flags |= SLBPPD_ENABLED |
4209 if (lun->be_lun->blocksize)
4210 ival /= lun->be_lun->blocksize;
4213 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4214 page->descr[2].count);
4216 value = ctl_get_opt(&lun->be_lun->options,
4217 "pool-used-threshold");
4218 if (value != NULL &&
4219 ctl_expand_number(value, &ival) == 0) {
4220 page->descr[3].flags |= SLBPPD_ENABLED |
4222 if (lun->be_lun->blocksize)
4223 ival /= lun->be_lun->blocksize;
4226 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4227 page->descr[3].count);
4229 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4230 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4231 sizeof(lbp_page_default));
4232 page_index->page_data =
4233 (uint8_t *)lun->mode_pages.lbp_page;
4237 panic("subpage %#x for page %#x is incorrect!",
4238 page_index->subpage, page_code);
4242 case SMS_CDDVD_CAPS_PAGE:{
4243 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4244 ("subpage %#x for page %#x is incorrect!",
4245 page_index->subpage, page_code));
4246 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4247 &cddvd_page_default,
4248 sizeof(cddvd_page_default));
4249 memcpy(&lun->mode_pages.cddvd_page[
4250 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4251 sizeof(cddvd_page_changeable));
4252 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4253 &cddvd_page_default,
4254 sizeof(cddvd_page_default));
4255 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4256 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4257 sizeof(cddvd_page_default));
4258 page_index->page_data =
4259 (uint8_t *)lun->mode_pages.cddvd_page;
4262 case SMS_VENDOR_SPECIFIC_PAGE:{
4263 switch (page_index->subpage) {
4264 case DBGCNF_SUBPAGE_CODE: {
4265 memcpy(&lun->mode_pages.debugconf_subpage[
4267 &debugconf_page_default,
4268 sizeof(debugconf_page_default));
4269 memcpy(&lun->mode_pages.debugconf_subpage[
4270 CTL_PAGE_CHANGEABLE],
4271 &debugconf_page_changeable,
4272 sizeof(debugconf_page_changeable));
4273 memcpy(&lun->mode_pages.debugconf_subpage[
4275 &debugconf_page_default,
4276 sizeof(debugconf_page_default));
4277 memcpy(&lun->mode_pages.debugconf_subpage[
4279 &debugconf_page_default,
4280 sizeof(debugconf_page_default));
4281 page_index->page_data =
4282 (uint8_t *)lun->mode_pages.debugconf_subpage;
4286 panic("subpage %#x for page %#x is incorrect!",
4287 page_index->subpage, page_code);
4292 panic("invalid page code value %#x", page_code);
4296 return (CTL_RETVAL_COMPLETE);
4300 ctl_init_log_page_index(struct ctl_lun *lun)
4302 struct ctl_page_index *page_index;
4305 memcpy(&lun->log_pages.index, log_page_index_template,
4306 sizeof(log_page_index_template));
4309 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4311 page_index = &lun->log_pages.index[i];
4312 if (lun->be_lun->lun_type == T_DIRECT &&
4313 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4315 if (lun->be_lun->lun_type == T_PROCESSOR &&
4316 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4318 if (lun->be_lun->lun_type == T_CDROM &&
4319 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4322 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4323 lun->backend->lun_attr == NULL)
4326 if (page_index->page_code != prev) {
4327 lun->log_pages.pages_page[j] = page_index->page_code;
4328 prev = page_index->page_code;
4331 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4332 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4335 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4336 lun->log_pages.index[0].page_len = j;
4337 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4338 lun->log_pages.index[1].page_len = k * 2;
4339 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4340 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4341 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4342 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4344 return (CTL_RETVAL_COMPLETE);
4348 hex2bin(const char *str, uint8_t *buf, int buf_size)
4353 memset(buf, 0, buf_size);
4354 while (isspace(str[0]))
4356 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4359 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4363 else if (isalpha(c))
4364 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4370 buf[i / 2] |= (c << 4);
4374 return ((i + 1) / 2);
4381 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4382 * wants us to allocate the LUN and he can block.
4383 * - ctl_softc is always set
4384 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4386 * Returns 0 for success, non-zero (errno) for failure.
4389 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4390 struct ctl_be_lun *const be_lun)
4392 struct ctl_lun *nlun, *lun;
4393 struct scsi_vpd_id_descriptor *desc;
4394 struct scsi_vpd_id_t10 *t10id;
4395 const char *eui, *naa, *scsiname, *vendor, *value;
4396 int lun_number, i, lun_malloced;
4397 int devidlen, idlen1, idlen2 = 0, len;
4403 * We currently only support Direct Access or Processor LUN types.
4405 switch (be_lun->lun_type) {
4413 be_lun->lun_config_status(be_lun->be_lun,
4414 CTL_LUN_CONFIG_FAILURE);
4417 if (ctl_lun == NULL) {
4418 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4425 memset(lun, 0, sizeof(*lun));
4427 lun->flags = CTL_LUN_MALLOCED;
4429 /* Generate LUN ID. */
4430 devidlen = max(CTL_DEVID_MIN_LEN,
4431 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4432 idlen1 = sizeof(*t10id) + devidlen;
4433 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4434 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4435 if (scsiname != NULL) {
4436 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4437 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4439 eui = ctl_get_opt(&be_lun->options, "eui");
4441 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4443 naa = ctl_get_opt(&be_lun->options, "naa");
4445 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4447 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4448 M_CTL, M_WAITOK | M_ZERO);
4449 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4450 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4451 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4452 desc->length = idlen1;
4453 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4454 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4455 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4456 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4458 strncpy(t10id->vendor, vendor,
4459 min(sizeof(t10id->vendor), strlen(vendor)));
4461 strncpy((char *)t10id->vendor_spec_id,
4462 (char *)be_lun->device_id, devidlen);
4463 if (scsiname != NULL) {
4464 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4466 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4467 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4468 SVPD_ID_TYPE_SCSI_NAME;
4469 desc->length = idlen2;
4470 strlcpy(desc->identifier, scsiname, idlen2);
4473 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4475 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4476 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4478 desc->length = hex2bin(eui, desc->identifier, 16);
4479 desc->length = desc->length > 12 ? 16 :
4480 (desc->length > 8 ? 12 : 8);
4481 len -= 16 - desc->length;
4484 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4486 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4487 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4489 desc->length = hex2bin(naa, desc->identifier, 16);
4490 desc->length = desc->length > 8 ? 16 : 8;
4491 len -= 16 - desc->length;
4493 lun->lun_devid->len = len;
4495 mtx_lock(&ctl_softc->ctl_lock);
4497 * See if the caller requested a particular LUN number. If so, see
4498 * if it is available. Otherwise, allocate the first available LUN.
4500 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4501 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4502 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4503 mtx_unlock(&ctl_softc->ctl_lock);
4504 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4505 printf("ctl: requested LUN ID %d is higher "
4506 "than CTL_MAX_LUNS - 1 (%d)\n",
4507 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4510 * XXX KDM return an error, or just assign
4511 * another LUN ID in this case??
4513 printf("ctl: requested LUN ID %d is already "
4514 "in use\n", be_lun->req_lun_id);
4516 if (lun->flags & CTL_LUN_MALLOCED)
4518 be_lun->lun_config_status(be_lun->be_lun,
4519 CTL_LUN_CONFIG_FAILURE);
4522 lun_number = be_lun->req_lun_id;
4524 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4525 if (lun_number == -1) {
4526 mtx_unlock(&ctl_softc->ctl_lock);
4527 printf("ctl: can't allocate LUN, out of LUNs\n");
4528 if (lun->flags & CTL_LUN_MALLOCED)
4530 be_lun->lun_config_status(be_lun->be_lun,
4531 CTL_LUN_CONFIG_FAILURE);
4535 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4537 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4538 lun->lun = lun_number;
4539 lun->be_lun = be_lun;
4541 * The processor LUN is always enabled. Disk LUNs come on line
4542 * disabled, and must be enabled by the backend.
4544 lun->flags |= CTL_LUN_DISABLED;
4545 lun->backend = be_lun->be;
4546 be_lun->ctl_lun = lun;
4547 be_lun->lun_id = lun_number;
4548 atomic_add_int(&be_lun->be->num_luns, 1);
4549 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4550 lun->flags |= CTL_LUN_EJECTED;
4551 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4552 lun->flags |= CTL_LUN_NO_MEDIA;
4553 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4554 lun->flags |= CTL_LUN_STOPPED;
4556 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4557 lun->flags |= CTL_LUN_PRIMARY_SC;
4559 value = ctl_get_opt(&be_lun->options, "removable");
4560 if (value != NULL) {
4561 if (strcmp(value, "on") == 0)
4562 lun->flags |= CTL_LUN_REMOVABLE;
4563 } else if (be_lun->lun_type == T_CDROM)
4564 lun->flags |= CTL_LUN_REMOVABLE;
4566 lun->ctl_softc = ctl_softc;
4568 lun->last_busy = getsbinuptime();
4570 TAILQ_INIT(&lun->ooa_queue);
4571 TAILQ_INIT(&lun->blocked_queue);
4572 STAILQ_INIT(&lun->error_list);
4573 ctl_tpc_lun_init(lun);
4576 * Initialize the mode and log page index.
4578 ctl_init_page_index(lun);
4579 ctl_init_log_page_index(lun);
4582 * Now, before we insert this lun on the lun list, set the lun
4583 * inventory changed UA for all other luns.
4585 STAILQ_FOREACH(nlun, &ctl_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);
4591 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4593 ctl_softc->ctl_luns[lun_number] = lun;
4595 ctl_softc->num_luns++;
4597 /* Setup statistics gathering */
4598 lun->stats.device_type = be_lun->lun_type;
4599 lun->stats.lun_number = lun_number;
4600 lun->stats.blocksize = be_lun->blocksize;
4601 if (be_lun->blocksize == 0)
4602 lun->stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4603 for (i = 0;i < CTL_MAX_PORTS;i++)
4604 lun->stats.ports[i].targ_port = i;
4606 mtx_unlock(&ctl_softc->ctl_lock);
4608 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4615 * - LUN has already been marked invalid and any pending I/O has been taken
4619 ctl_free_lun(struct ctl_lun *lun)
4621 struct ctl_softc *softc;
4622 struct ctl_lun *nlun;
4625 softc = lun->ctl_softc;
4627 mtx_assert(&softc->ctl_lock, MA_OWNED);
4629 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4631 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4633 softc->ctl_luns[lun->lun] = NULL;
4635 if (!TAILQ_EMPTY(&lun->ooa_queue))
4636 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4641 * Tell the backend to free resources, if this LUN has a backend.
4643 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4644 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4646 ctl_tpc_lun_shutdown(lun);
4647 mtx_destroy(&lun->lun_lock);
4648 free(lun->lun_devid, M_CTL);
4649 for (i = 0; i < CTL_MAX_PORTS; i++)
4650 free(lun->pending_ua[i], M_CTL);
4651 for (i = 0; i < CTL_MAX_PORTS; i++)
4652 free(lun->pr_keys[i], M_CTL);
4653 free(lun->write_buffer, M_CTL);
4654 if (lun->flags & CTL_LUN_MALLOCED)
4657 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4658 mtx_lock(&nlun->lun_lock);
4659 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4660 mtx_unlock(&nlun->lun_lock);
4667 ctl_create_lun(struct ctl_be_lun *be_lun)
4671 * ctl_alloc_lun() should handle all potential failure cases.
4673 ctl_alloc_lun(control_softc, NULL, be_lun);
4677 ctl_add_lun(struct ctl_be_lun *be_lun)
4679 struct ctl_softc *softc = control_softc;
4681 mtx_lock(&softc->ctl_lock);
4682 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4683 mtx_unlock(&softc->ctl_lock);
4684 wakeup(&softc->pending_lun_queue);
4690 ctl_enable_lun(struct ctl_be_lun *be_lun)
4692 struct ctl_softc *softc;
4693 struct ctl_port *port, *nport;
4694 struct ctl_lun *lun;
4697 lun = (struct ctl_lun *)be_lun->ctl_lun;
4698 softc = lun->ctl_softc;
4700 mtx_lock(&softc->ctl_lock);
4701 mtx_lock(&lun->lun_lock);
4702 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4704 * eh? Why did we get called if the LUN is already
4707 mtx_unlock(&lun->lun_lock);
4708 mtx_unlock(&softc->ctl_lock);
4711 lun->flags &= ~CTL_LUN_DISABLED;
4712 mtx_unlock(&lun->lun_lock);
4714 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4715 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4716 port->lun_map != NULL || port->lun_enable == NULL)
4720 * Drop the lock while we call the FETD's enable routine.
4721 * This can lead to a callback into CTL (at least in the
4722 * case of the internal initiator frontend.
4724 mtx_unlock(&softc->ctl_lock);
4725 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4726 mtx_lock(&softc->ctl_lock);
4728 printf("%s: FETD %s port %d returned error "
4729 "%d for lun_enable on lun %jd\n",
4730 __func__, port->port_name, port->targ_port,
4731 retval, (intmax_t)lun->lun);
4735 mtx_unlock(&softc->ctl_lock);
4736 ctl_isc_announce_lun(lun);
4742 ctl_disable_lun(struct ctl_be_lun *be_lun)
4744 struct ctl_softc *softc;
4745 struct ctl_port *port;
4746 struct ctl_lun *lun;
4749 lun = (struct ctl_lun *)be_lun->ctl_lun;
4750 softc = lun->ctl_softc;
4752 mtx_lock(&softc->ctl_lock);
4753 mtx_lock(&lun->lun_lock);
4754 if (lun->flags & CTL_LUN_DISABLED) {
4755 mtx_unlock(&lun->lun_lock);
4756 mtx_unlock(&softc->ctl_lock);
4759 lun->flags |= CTL_LUN_DISABLED;
4760 mtx_unlock(&lun->lun_lock);
4762 STAILQ_FOREACH(port, &softc->port_list, links) {
4763 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4764 port->lun_map != NULL || port->lun_disable == NULL)
4768 * Drop the lock before we call the frontend's disable
4769 * routine, to avoid lock order reversals.
4771 * XXX KDM what happens if the frontend list changes while
4772 * we're traversing it? It's unlikely, but should be handled.
4774 mtx_unlock(&softc->ctl_lock);
4775 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4776 mtx_lock(&softc->ctl_lock);
4778 printf("%s: FETD %s port %d returned error "
4779 "%d for lun_disable on lun %jd\n",
4780 __func__, port->port_name, port->targ_port,
4781 retval, (intmax_t)lun->lun);
4785 mtx_unlock(&softc->ctl_lock);
4786 ctl_isc_announce_lun(lun);
4792 ctl_start_lun(struct ctl_be_lun *be_lun)
4794 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4796 mtx_lock(&lun->lun_lock);
4797 lun->flags &= ~CTL_LUN_STOPPED;
4798 mtx_unlock(&lun->lun_lock);
4803 ctl_stop_lun(struct ctl_be_lun *be_lun)
4805 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4807 mtx_lock(&lun->lun_lock);
4808 lun->flags |= CTL_LUN_STOPPED;
4809 mtx_unlock(&lun->lun_lock);
4814 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4816 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4818 mtx_lock(&lun->lun_lock);
4819 lun->flags |= CTL_LUN_NO_MEDIA;
4820 mtx_unlock(&lun->lun_lock);
4825 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4827 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4828 union ctl_ha_msg msg;
4830 mtx_lock(&lun->lun_lock);
4831 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4832 if (lun->flags & CTL_LUN_REMOVABLE)
4833 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4834 mtx_unlock(&lun->lun_lock);
4835 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4836 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4837 bzero(&msg.ua, sizeof(msg.ua));
4838 msg.hdr.msg_type = CTL_MSG_UA;
4839 msg.hdr.nexus.initid = -1;
4840 msg.hdr.nexus.targ_port = -1;
4841 msg.hdr.nexus.targ_lun = lun->lun;
4842 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4845 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4846 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4853 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4855 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4857 mtx_lock(&lun->lun_lock);
4858 lun->flags |= CTL_LUN_EJECTED;
4859 mtx_unlock(&lun->lun_lock);
4864 ctl_lun_primary(struct ctl_be_lun *be_lun)
4866 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4868 mtx_lock(&lun->lun_lock);
4869 lun->flags |= CTL_LUN_PRIMARY_SC;
4870 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4871 mtx_unlock(&lun->lun_lock);
4872 ctl_isc_announce_lun(lun);
4877 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4879 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4881 mtx_lock(&lun->lun_lock);
4882 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4883 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4884 mtx_unlock(&lun->lun_lock);
4885 ctl_isc_announce_lun(lun);
4890 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4892 struct ctl_softc *softc;
4893 struct ctl_lun *lun;
4895 lun = (struct ctl_lun *)be_lun->ctl_lun;
4896 softc = lun->ctl_softc;
4898 mtx_lock(&lun->lun_lock);
4901 * The LUN needs to be disabled before it can be marked invalid.
4903 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4904 mtx_unlock(&lun->lun_lock);
4908 * Mark the LUN invalid.
4910 lun->flags |= CTL_LUN_INVALID;
4913 * If there is nothing in the OOA queue, go ahead and free the LUN.
4914 * If we have something in the OOA queue, we'll free it when the
4915 * last I/O completes.
4917 if (TAILQ_EMPTY(&lun->ooa_queue)) {
4918 mtx_unlock(&lun->lun_lock);
4919 mtx_lock(&softc->ctl_lock);
4921 mtx_unlock(&softc->ctl_lock);
4923 mtx_unlock(&lun->lun_lock);
4929 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
4931 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4932 union ctl_ha_msg msg;
4934 mtx_lock(&lun->lun_lock);
4935 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
4936 mtx_unlock(&lun->lun_lock);
4937 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4938 /* Send msg to other side. */
4939 bzero(&msg.ua, sizeof(msg.ua));
4940 msg.hdr.msg_type = CTL_MSG_UA;
4941 msg.hdr.nexus.initid = -1;
4942 msg.hdr.nexus.targ_port = -1;
4943 msg.hdr.nexus.targ_lun = lun->lun;
4944 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4947 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
4948 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4954 * Backend "memory move is complete" callback for requests that never
4955 * make it down to say RAIDCore's configuration code.
4958 ctl_config_move_done(union ctl_io *io)
4962 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
4963 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
4964 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
4966 if ((io->io_hdr.port_status != 0) &&
4967 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
4968 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
4970 * For hardware error sense keys, the sense key
4971 * specific value is defined to be a retry count,
4972 * but we use it to pass back an internal FETD
4973 * error code. XXX KDM Hopefully the FETD is only
4974 * using 16 bits for an error code, since that's
4975 * all the space we have in the sks field.
4977 ctl_set_internal_failure(&io->scsiio,
4980 io->io_hdr.port_status);
4983 if (ctl_debug & CTL_DEBUG_CDB_DATA)
4985 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
4986 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
4987 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
4988 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
4990 * XXX KDM just assuming a single pointer here, and not a
4991 * S/G list. If we start using S/G lists for config data,
4992 * we'll need to know how to clean them up here as well.
4994 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
4995 free(io->scsiio.kern_data_ptr, M_CTL);
4997 retval = CTL_RETVAL_COMPLETE;
5000 * XXX KDM now we need to continue data movement. Some
5002 * - call ctl_scsiio() again? We don't do this for data
5003 * writes, because for those at least we know ahead of
5004 * time where the write will go and how long it is. For
5005 * config writes, though, that information is largely
5006 * contained within the write itself, thus we need to
5007 * parse out the data again.
5009 * - Call some other function once the data is in?
5013 * XXX KDM call ctl_scsiio() again for now, and check flag
5014 * bits to see whether we're allocated or not.
5016 retval = ctl_scsiio(&io->scsiio);
5022 * This gets called by a backend driver when it is done with a
5023 * data_submit method.
5026 ctl_data_submit_done(union ctl_io *io)
5029 * If the IO_CONT flag is set, we need to call the supplied
5030 * function to continue processing the I/O, instead of completing
5033 * If there is an error, though, we don't want to keep processing.
5034 * Instead, just send status back to the initiator.
5036 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5037 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5038 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5039 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5040 io->scsiio.io_cont(io);
5047 * This gets called by a backend driver when it is done with a
5048 * configuration write.
5051 ctl_config_write_done(union ctl_io *io)
5056 * If the IO_CONT flag is set, we need to call the supplied
5057 * function to continue processing the I/O, instead of completing
5060 * If there is an error, though, we don't want to keep processing.
5061 * Instead, just send status back to the initiator.
5063 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5064 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5065 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5066 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5067 io->scsiio.io_cont(io);
5071 * Since a configuration write can be done for commands that actually
5072 * have data allocated, like write buffer, and commands that have
5073 * no data, like start/stop unit, we need to check here.
5075 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5076 buf = io->scsiio.kern_data_ptr;
5085 ctl_config_read_done(union ctl_io *io)
5090 * If there is some error -- we are done, skip data transfer.
5092 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5093 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5094 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5095 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5096 buf = io->scsiio.kern_data_ptr;
5106 * If the IO_CONT flag is set, we need to call the supplied
5107 * function to continue processing the I/O, instead of completing
5110 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5111 io->scsiio.io_cont(io);
5119 * SCSI release command.
5122 ctl_scsi_release(struct ctl_scsiio *ctsio)
5124 struct ctl_lun *lun;
5127 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5129 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5130 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5133 * XXX KDM right now, we only support LUN reservation. We don't
5134 * support 3rd party reservations, or extent reservations, which
5135 * might actually need the parameter list. If we've gotten this
5136 * far, we've got a LUN reservation. Anything else got kicked out
5137 * above. So, according to SPC, ignore the length.
5140 mtx_lock(&lun->lun_lock);
5143 * According to SPC, it is not an error for an intiator to attempt
5144 * to release a reservation on a LUN that isn't reserved, or that
5145 * is reserved by another initiator. The reservation can only be
5146 * released, though, by the initiator who made it or by one of
5147 * several reset type events.
5149 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5150 lun->flags &= ~CTL_LUN_RESERVED;
5152 mtx_unlock(&lun->lun_lock);
5154 ctl_set_success(ctsio);
5155 ctl_done((union ctl_io *)ctsio);
5156 return (CTL_RETVAL_COMPLETE);
5160 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5162 struct ctl_lun *lun;
5165 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5167 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5168 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5171 * XXX KDM right now, we only support LUN reservation. We don't
5172 * support 3rd party reservations, or extent reservations, which
5173 * might actually need the parameter list. If we've gotten this
5174 * far, we've got a LUN reservation. Anything else got kicked out
5175 * above. So, according to SPC, ignore the length.
5178 mtx_lock(&lun->lun_lock);
5179 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5180 ctl_set_reservation_conflict(ctsio);
5184 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5185 if (lun->flags & CTL_LUN_PR_RESERVED) {
5186 ctl_set_success(ctsio);
5190 lun->flags |= CTL_LUN_RESERVED;
5191 lun->res_idx = residx;
5192 ctl_set_success(ctsio);
5195 mtx_unlock(&lun->lun_lock);
5196 ctl_done((union ctl_io *)ctsio);
5197 return (CTL_RETVAL_COMPLETE);
5201 ctl_start_stop(struct ctl_scsiio *ctsio)
5203 struct scsi_start_stop_unit *cdb;
5204 struct ctl_lun *lun;
5207 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5209 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5210 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5212 if ((cdb->how & SSS_PC_MASK) == 0) {
5213 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5214 (cdb->how & SSS_START) == 0) {
5217 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5218 if (ctl_get_prkey(lun, residx) == 0 ||
5219 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5221 ctl_set_reservation_conflict(ctsio);
5222 ctl_done((union ctl_io *)ctsio);
5223 return (CTL_RETVAL_COMPLETE);
5227 if ((cdb->how & SSS_LOEJ) &&
5228 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5229 ctl_set_invalid_field(ctsio,
5235 ctl_done((union ctl_io *)ctsio);
5236 return (CTL_RETVAL_COMPLETE);
5239 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5240 lun->prevent_count > 0) {
5241 /* "Medium removal prevented" */
5242 ctl_set_sense(ctsio, /*current_error*/ 1,
5243 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5244 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5245 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5246 ctl_done((union ctl_io *)ctsio);
5247 return (CTL_RETVAL_COMPLETE);
5251 retval = lun->backend->config_write((union ctl_io *)ctsio);
5256 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5258 struct ctl_lun *lun;
5259 struct scsi_prevent *cdb;
5263 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5265 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5266 cdb = (struct scsi_prevent *)ctsio->cdb;
5268 if ((lun->flags & CTL_LUN_REMOVABLE) == 0) {
5269 ctl_set_invalid_opcode(ctsio);
5270 ctl_done((union ctl_io *)ctsio);
5271 return (CTL_RETVAL_COMPLETE);
5274 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5275 mtx_lock(&lun->lun_lock);
5276 if ((cdb->how & PR_PREVENT) &&
5277 ctl_is_set(lun->prevent, initidx) == 0) {
5278 ctl_set_mask(lun->prevent, initidx);
5279 lun->prevent_count++;
5280 } else if ((cdb->how & PR_PREVENT) == 0 &&
5281 ctl_is_set(lun->prevent, initidx)) {
5282 ctl_clear_mask(lun->prevent, initidx);
5283 lun->prevent_count--;
5285 mtx_unlock(&lun->lun_lock);
5286 retval = lun->backend->config_write((union ctl_io *)ctsio);
5291 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5292 * we don't really do anything with the LBA and length fields if the user
5293 * passes them in. Instead we'll just flush out the cache for the entire
5297 ctl_sync_cache(struct ctl_scsiio *ctsio)
5299 struct ctl_lun *lun;
5300 struct ctl_softc *softc;
5301 struct ctl_lba_len_flags *lbalen;
5302 uint64_t starting_lba;
5303 uint32_t block_count;
5307 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5309 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5310 softc = lun->ctl_softc;
5313 switch (ctsio->cdb[0]) {
5314 case SYNCHRONIZE_CACHE: {
5315 struct scsi_sync_cache *cdb;
5316 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5318 starting_lba = scsi_4btoul(cdb->begin_lba);
5319 block_count = scsi_2btoul(cdb->lb_count);
5323 case SYNCHRONIZE_CACHE_16: {
5324 struct scsi_sync_cache_16 *cdb;
5325 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5327 starting_lba = scsi_8btou64(cdb->begin_lba);
5328 block_count = scsi_4btoul(cdb->lb_count);
5333 ctl_set_invalid_opcode(ctsio);
5334 ctl_done((union ctl_io *)ctsio);
5336 break; /* NOTREACHED */
5340 * We check the LBA and length, but don't do anything with them.
5341 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5342 * get flushed. This check will just help satisfy anyone who wants
5343 * to see an error for an out of range LBA.
5345 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5346 ctl_set_lba_out_of_range(ctsio);
5347 ctl_done((union ctl_io *)ctsio);
5351 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5352 lbalen->lba = starting_lba;
5353 lbalen->len = block_count;
5354 lbalen->flags = byte2;
5355 retval = lun->backend->config_write((union ctl_io *)ctsio);
5362 ctl_format(struct ctl_scsiio *ctsio)
5364 struct scsi_format *cdb;
5365 struct ctl_lun *lun;
5366 int length, defect_list_len;
5368 CTL_DEBUG_PRINT(("ctl_format\n"));
5370 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5372 cdb = (struct scsi_format *)ctsio->cdb;
5375 if (cdb->byte2 & SF_FMTDATA) {
5376 if (cdb->byte2 & SF_LONGLIST)
5377 length = sizeof(struct scsi_format_header_long);
5379 length = sizeof(struct scsi_format_header_short);
5382 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5384 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5385 ctsio->kern_data_len = length;
5386 ctsio->kern_total_len = length;
5387 ctsio->kern_data_resid = 0;
5388 ctsio->kern_rel_offset = 0;
5389 ctsio->kern_sg_entries = 0;
5390 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5391 ctsio->be_move_done = ctl_config_move_done;
5392 ctl_datamove((union ctl_io *)ctsio);
5394 return (CTL_RETVAL_COMPLETE);
5397 defect_list_len = 0;
5399 if (cdb->byte2 & SF_FMTDATA) {
5400 if (cdb->byte2 & SF_LONGLIST) {
5401 struct scsi_format_header_long *header;
5403 header = (struct scsi_format_header_long *)
5404 ctsio->kern_data_ptr;
5406 defect_list_len = scsi_4btoul(header->defect_list_len);
5407 if (defect_list_len != 0) {
5408 ctl_set_invalid_field(ctsio,
5417 struct scsi_format_header_short *header;
5419 header = (struct scsi_format_header_short *)
5420 ctsio->kern_data_ptr;
5422 defect_list_len = scsi_2btoul(header->defect_list_len);
5423 if (defect_list_len != 0) {
5424 ctl_set_invalid_field(ctsio,
5435 ctl_set_success(ctsio);
5438 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5439 free(ctsio->kern_data_ptr, M_CTL);
5440 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5443 ctl_done((union ctl_io *)ctsio);
5444 return (CTL_RETVAL_COMPLETE);
5448 ctl_read_buffer(struct ctl_scsiio *ctsio)
5450 struct ctl_lun *lun;
5451 uint64_t buffer_offset;
5454 static uint8_t descr[4];
5455 static uint8_t echo_descr[4] = { 0 };
5457 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5458 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5459 switch (ctsio->cdb[0]) {
5461 struct scsi_read_buffer *cdb;
5463 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5464 buffer_offset = scsi_3btoul(cdb->offset);
5465 len = scsi_3btoul(cdb->length);
5469 case READ_BUFFER_16: {
5470 struct scsi_read_buffer_16 *cdb;
5472 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5473 buffer_offset = scsi_8btou64(cdb->offset);
5474 len = scsi_4btoul(cdb->length);
5478 default: /* This shouldn't happen. */
5479 ctl_set_invalid_opcode(ctsio);
5480 ctl_done((union ctl_io *)ctsio);
5481 return (CTL_RETVAL_COMPLETE);
5484 if ((byte2 & RWB_MODE) != RWB_MODE_DATA &&
5485 (byte2 & RWB_MODE) != RWB_MODE_ECHO_DESCR &&
5486 (byte2 & RWB_MODE) != RWB_MODE_DESCR) {
5487 ctl_set_invalid_field(ctsio,
5493 ctl_done((union ctl_io *)ctsio);
5494 return (CTL_RETVAL_COMPLETE);
5497 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5498 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5499 ctl_set_invalid_field(ctsio,
5505 ctl_done((union ctl_io *)ctsio);
5506 return (CTL_RETVAL_COMPLETE);
5509 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5511 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5512 ctsio->kern_data_ptr = descr;
5513 len = min(len, sizeof(descr));
5514 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5515 ctsio->kern_data_ptr = echo_descr;
5516 len = min(len, sizeof(echo_descr));
5518 if (lun->write_buffer == NULL) {
5519 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5522 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5524 ctsio->kern_data_len = len;
5525 ctsio->kern_total_len = len;
5526 ctsio->kern_data_resid = 0;
5527 ctsio->kern_rel_offset = 0;
5528 ctsio->kern_sg_entries = 0;
5529 ctl_set_success(ctsio);
5530 ctsio->be_move_done = ctl_config_move_done;
5531 ctl_datamove((union ctl_io *)ctsio);
5532 return (CTL_RETVAL_COMPLETE);
5536 ctl_write_buffer(struct ctl_scsiio *ctsio)
5538 struct scsi_write_buffer *cdb;
5539 struct ctl_lun *lun;
5540 int buffer_offset, len;
5542 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5544 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5545 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5547 if ((cdb->byte2 & RWB_MODE) != RWB_MODE_DATA) {
5548 ctl_set_invalid_field(ctsio,
5554 ctl_done((union ctl_io *)ctsio);
5555 return (CTL_RETVAL_COMPLETE);
5558 len = scsi_3btoul(cdb->length);
5559 buffer_offset = scsi_3btoul(cdb->offset);
5561 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5562 ctl_set_invalid_field(ctsio,
5568 ctl_done((union ctl_io *)ctsio);
5569 return (CTL_RETVAL_COMPLETE);
5573 * If we've got a kernel request that hasn't been malloced yet,
5574 * malloc it and tell the caller the data buffer is here.
5576 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5577 if (lun->write_buffer == NULL) {
5578 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5581 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5582 ctsio->kern_data_len = len;
5583 ctsio->kern_total_len = len;
5584 ctsio->kern_data_resid = 0;
5585 ctsio->kern_rel_offset = 0;
5586 ctsio->kern_sg_entries = 0;
5587 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5588 ctsio->be_move_done = ctl_config_move_done;
5589 ctl_datamove((union ctl_io *)ctsio);
5591 return (CTL_RETVAL_COMPLETE);
5594 ctl_set_success(ctsio);
5595 ctl_done((union ctl_io *)ctsio);
5596 return (CTL_RETVAL_COMPLETE);
5600 ctl_write_same(struct ctl_scsiio *ctsio)
5602 struct ctl_lun *lun;
5603 struct ctl_lba_len_flags *lbalen;
5605 uint32_t num_blocks;
5609 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5611 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5613 switch (ctsio->cdb[0]) {
5614 case WRITE_SAME_10: {
5615 struct scsi_write_same_10 *cdb;
5617 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5619 lba = scsi_4btoul(cdb->addr);
5620 num_blocks = scsi_2btoul(cdb->length);
5624 case WRITE_SAME_16: {
5625 struct scsi_write_same_16 *cdb;
5627 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5629 lba = scsi_8btou64(cdb->addr);
5630 num_blocks = scsi_4btoul(cdb->length);
5636 * We got a command we don't support. This shouldn't
5637 * happen, commands should be filtered out above us.
5639 ctl_set_invalid_opcode(ctsio);
5640 ctl_done((union ctl_io *)ctsio);
5642 return (CTL_RETVAL_COMPLETE);
5643 break; /* NOTREACHED */
5646 /* ANCHOR flag can be used only together with UNMAP */
5647 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5648 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5649 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5650 ctl_done((union ctl_io *)ctsio);
5651 return (CTL_RETVAL_COMPLETE);
5655 * The first check is to make sure we're in bounds, the second
5656 * check is to catch wrap-around problems. If the lba + num blocks
5657 * is less than the lba, then we've wrapped around and the block
5658 * range is invalid anyway.
5660 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5661 || ((lba + num_blocks) < lba)) {
5662 ctl_set_lba_out_of_range(ctsio);
5663 ctl_done((union ctl_io *)ctsio);
5664 return (CTL_RETVAL_COMPLETE);
5667 /* Zero number of blocks means "to the last logical block" */
5668 if (num_blocks == 0) {
5669 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5670 ctl_set_invalid_field(ctsio,
5676 ctl_done((union ctl_io *)ctsio);
5677 return (CTL_RETVAL_COMPLETE);
5679 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5682 len = lun->be_lun->blocksize;
5685 * If we've got a kernel request that hasn't been malloced yet,
5686 * malloc it and tell the caller the data buffer is here.
5688 if ((byte2 & SWS_NDOB) == 0 &&
5689 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5690 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5691 ctsio->kern_data_len = len;
5692 ctsio->kern_total_len = len;
5693 ctsio->kern_data_resid = 0;
5694 ctsio->kern_rel_offset = 0;
5695 ctsio->kern_sg_entries = 0;
5696 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5697 ctsio->be_move_done = ctl_config_move_done;
5698 ctl_datamove((union ctl_io *)ctsio);
5700 return (CTL_RETVAL_COMPLETE);
5703 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5705 lbalen->len = num_blocks;
5706 lbalen->flags = byte2;
5707 retval = lun->backend->config_write((union ctl_io *)ctsio);
5713 ctl_unmap(struct ctl_scsiio *ctsio)
5715 struct ctl_lun *lun;
5716 struct scsi_unmap *cdb;
5717 struct ctl_ptr_len_flags *ptrlen;
5718 struct scsi_unmap_header *hdr;
5719 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5721 uint32_t num_blocks;
5725 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5727 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5728 cdb = (struct scsi_unmap *)ctsio->cdb;
5730 len = scsi_2btoul(cdb->length);
5734 * If we've got a kernel request that hasn't been malloced yet,
5735 * malloc it and tell the caller the data buffer is here.
5737 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5738 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);;
5739 ctsio->kern_data_len = len;
5740 ctsio->kern_total_len = len;
5741 ctsio->kern_data_resid = 0;
5742 ctsio->kern_rel_offset = 0;
5743 ctsio->kern_sg_entries = 0;
5744 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5745 ctsio->be_move_done = ctl_config_move_done;
5746 ctl_datamove((union ctl_io *)ctsio);
5748 return (CTL_RETVAL_COMPLETE);
5751 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5752 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5753 if (len < sizeof (*hdr) ||
5754 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5755 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5756 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5757 ctl_set_invalid_field(ctsio,
5765 len = scsi_2btoul(hdr->desc_length);
5766 buf = (struct scsi_unmap_desc *)(hdr + 1);
5767 end = buf + len / sizeof(*buf);
5770 for (range = buf; range < end; range++) {
5771 lba = scsi_8btou64(range->lba);
5772 num_blocks = scsi_4btoul(range->length);
5773 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5774 || ((lba + num_blocks) < lba)) {
5775 ctl_set_lba_out_of_range(ctsio);
5776 ctl_done((union ctl_io *)ctsio);
5777 return (CTL_RETVAL_COMPLETE);
5779 if (num_blocks != 0)
5784 * Block backend can not handle zero last range.
5785 * Filter it out and return if there is nothing left.
5787 len = (uint8_t *)endnz - (uint8_t *)buf;
5789 ctl_set_success(ctsio);
5793 mtx_lock(&lun->lun_lock);
5794 ptrlen = (struct ctl_ptr_len_flags *)
5795 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5796 ptrlen->ptr = (void *)buf;
5798 ptrlen->flags = byte2;
5799 ctl_check_blocked(lun);
5800 mtx_unlock(&lun->lun_lock);
5802 retval = lun->backend->config_write((union ctl_io *)ctsio);
5806 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5807 free(ctsio->kern_data_ptr, M_CTL);
5808 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5810 ctl_done((union ctl_io *)ctsio);
5811 return (CTL_RETVAL_COMPLETE);
5815 * Note that this function currently doesn't actually do anything inside
5816 * CTL to enforce things if the DQue bit is turned on.
5818 * Also note that this function can't be used in the default case, because
5819 * the DQue bit isn't set in the changeable mask for the control mode page
5820 * anyway. This is just here as an example for how to implement a page
5821 * handler, and a placeholder in case we want to allow the user to turn
5822 * tagged queueing on and off.
5824 * The D_SENSE bit handling is functional, however, and will turn
5825 * descriptor sense on and off for a given LUN.
5828 ctl_control_page_handler(struct ctl_scsiio *ctsio,
5829 struct ctl_page_index *page_index, uint8_t *page_ptr)
5831 struct scsi_control_page *current_cp, *saved_cp, *user_cp;
5832 struct ctl_lun *lun;
5836 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5837 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5840 user_cp = (struct scsi_control_page *)page_ptr;
5841 current_cp = (struct scsi_control_page *)
5842 (page_index->page_data + (page_index->page_len *
5844 saved_cp = (struct scsi_control_page *)
5845 (page_index->page_data + (page_index->page_len *
5848 mtx_lock(&lun->lun_lock);
5849 if (((current_cp->rlec & SCP_DSENSE) == 0)
5850 && ((user_cp->rlec & SCP_DSENSE) != 0)) {
5852 * Descriptor sense is currently turned off and the user
5853 * wants to turn it on.
5855 current_cp->rlec |= SCP_DSENSE;
5856 saved_cp->rlec |= SCP_DSENSE;
5857 lun->flags |= CTL_LUN_SENSE_DESC;
5859 } else if (((current_cp->rlec & SCP_DSENSE) != 0)
5860 && ((user_cp->rlec & SCP_DSENSE) == 0)) {
5862 * Descriptor sense is currently turned on, and the user
5863 * wants to turn it off.
5865 current_cp->rlec &= ~SCP_DSENSE;
5866 saved_cp->rlec &= ~SCP_DSENSE;
5867 lun->flags &= ~CTL_LUN_SENSE_DESC;
5870 if ((current_cp->queue_flags & SCP_QUEUE_ALG_MASK) !=
5871 (user_cp->queue_flags & SCP_QUEUE_ALG_MASK)) {
5872 current_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5873 current_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5874 saved_cp->queue_flags &= ~SCP_QUEUE_ALG_MASK;
5875 saved_cp->queue_flags |= user_cp->queue_flags & SCP_QUEUE_ALG_MASK;
5878 if ((current_cp->eca_and_aen & SCP_SWP) !=
5879 (user_cp->eca_and_aen & SCP_SWP)) {
5880 current_cp->eca_and_aen &= ~SCP_SWP;
5881 current_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5882 saved_cp->eca_and_aen &= ~SCP_SWP;
5883 saved_cp->eca_and_aen |= user_cp->eca_and_aen & SCP_SWP;
5887 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5888 mtx_unlock(&lun->lun_lock);
5890 ctl_isc_announce_mode(lun,
5891 ctl_get_initindex(&ctsio->io_hdr.nexus),
5892 page_index->page_code, page_index->subpage);
5898 ctl_caching_sp_handler(struct ctl_scsiio *ctsio,
5899 struct ctl_page_index *page_index, uint8_t *page_ptr)
5901 struct scsi_caching_page *current_cp, *saved_cp, *user_cp;
5902 struct ctl_lun *lun;
5906 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
5907 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5910 user_cp = (struct scsi_caching_page *)page_ptr;
5911 current_cp = (struct scsi_caching_page *)
5912 (page_index->page_data + (page_index->page_len *
5914 saved_cp = (struct scsi_caching_page *)
5915 (page_index->page_data + (page_index->page_len *
5918 mtx_lock(&lun->lun_lock);
5919 if ((current_cp->flags1 & (SCP_WCE | SCP_RCD)) !=
5920 (user_cp->flags1 & (SCP_WCE | SCP_RCD))) {
5921 current_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5922 current_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5923 saved_cp->flags1 &= ~(SCP_WCE | SCP_RCD);
5924 saved_cp->flags1 |= user_cp->flags1 & (SCP_WCE | SCP_RCD);
5928 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5929 mtx_unlock(&lun->lun_lock);
5931 ctl_isc_announce_mode(lun,
5932 ctl_get_initindex(&ctsio->io_hdr.nexus),
5933 page_index->page_code, page_index->subpage);
5939 ctl_debugconf_sp_select_handler(struct ctl_scsiio *ctsio,
5940 struct ctl_page_index *page_index,
5946 c = ((struct copan_debugconf_subpage *)page_ptr)->ctl_time_io_secs;
5951 CTL_DEBUG_PRINT(("set ctl_time_io_secs to %d\n", ctl_time_io_secs));
5952 printf("set ctl_time_io_secs to %d\n", ctl_time_io_secs);
5953 printf("page data:");
5955 printf(" %.2x",page_ptr[i]);
5961 ctl_debugconf_sp_sense_handler(struct ctl_scsiio *ctsio,
5962 struct ctl_page_index *page_index,
5965 struct copan_debugconf_subpage *page;
5967 page = (struct copan_debugconf_subpage *)page_index->page_data +
5968 (page_index->page_len * pc);
5971 case SMS_PAGE_CTRL_CHANGEABLE >> 6:
5972 case SMS_PAGE_CTRL_DEFAULT >> 6:
5973 case SMS_PAGE_CTRL_SAVED >> 6:
5975 * We don't update the changable or default bits for this page.
5978 case SMS_PAGE_CTRL_CURRENT >> 6:
5979 page->ctl_time_io_secs[0] = ctl_time_io_secs >> 8;
5980 page->ctl_time_io_secs[1] = ctl_time_io_secs >> 0;
5990 ctl_do_mode_select(union ctl_io *io)
5992 struct scsi_mode_page_header *page_header;
5993 struct ctl_page_index *page_index;
5994 struct ctl_scsiio *ctsio;
5995 int page_len, page_len_offset, page_len_size;
5996 union ctl_modepage_info *modepage_info;
5997 struct ctl_lun *lun;
5998 int *len_left, *len_used;
6001 ctsio = &io->scsiio;
6004 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6006 modepage_info = (union ctl_modepage_info *)
6007 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6008 len_left = &modepage_info->header.len_left;
6009 len_used = &modepage_info->header.len_used;
6013 page_header = (struct scsi_mode_page_header *)
6014 (ctsio->kern_data_ptr + *len_used);
6016 if (*len_left == 0) {
6017 free(ctsio->kern_data_ptr, M_CTL);
6018 ctl_set_success(ctsio);
6019 ctl_done((union ctl_io *)ctsio);
6020 return (CTL_RETVAL_COMPLETE);
6021 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6023 free(ctsio->kern_data_ptr, M_CTL);
6024 ctl_set_param_len_error(ctsio);
6025 ctl_done((union ctl_io *)ctsio);
6026 return (CTL_RETVAL_COMPLETE);
6028 } else if ((page_header->page_code & SMPH_SPF)
6029 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6031 free(ctsio->kern_data_ptr, M_CTL);
6032 ctl_set_param_len_error(ctsio);
6033 ctl_done((union ctl_io *)ctsio);
6034 return (CTL_RETVAL_COMPLETE);
6039 * XXX KDM should we do something with the block descriptor?
6041 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6042 page_index = &lun->mode_pages.index[i];
6043 if (lun->be_lun->lun_type == T_DIRECT &&
6044 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6046 if (lun->be_lun->lun_type == T_PROCESSOR &&
6047 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6049 if (lun->be_lun->lun_type == T_CDROM &&
6050 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6053 if ((page_index->page_code & SMPH_PC_MASK) !=
6054 (page_header->page_code & SMPH_PC_MASK))
6058 * If neither page has a subpage code, then we've got a
6061 if (((page_index->page_code & SMPH_SPF) == 0)
6062 && ((page_header->page_code & SMPH_SPF) == 0)) {
6063 page_len = page_header->page_length;
6068 * If both pages have subpages, then the subpage numbers
6071 if ((page_index->page_code & SMPH_SPF)
6072 && (page_header->page_code & SMPH_SPF)) {
6073 struct scsi_mode_page_header_sp *sph;
6075 sph = (struct scsi_mode_page_header_sp *)page_header;
6076 if (page_index->subpage == sph->subpage) {
6077 page_len = scsi_2btoul(sph->page_length);
6084 * If we couldn't find the page, or if we don't have a mode select
6085 * handler for it, send back an error to the user.
6087 if ((i >= CTL_NUM_MODE_PAGES)
6088 || (page_index->select_handler == NULL)) {
6089 ctl_set_invalid_field(ctsio,
6092 /*field*/ *len_used,
6095 free(ctsio->kern_data_ptr, M_CTL);
6096 ctl_done((union ctl_io *)ctsio);
6097 return (CTL_RETVAL_COMPLETE);
6100 if (page_index->page_code & SMPH_SPF) {
6101 page_len_offset = 2;
6105 page_len_offset = 1;
6109 * If the length the initiator gives us isn't the one we specify in
6110 * the mode page header, or if they didn't specify enough data in
6111 * the CDB to avoid truncating this page, kick out the request.
6113 if ((page_len != (page_index->page_len - page_len_offset -
6115 || (*len_left < page_index->page_len)) {
6118 ctl_set_invalid_field(ctsio,
6121 /*field*/ *len_used + page_len_offset,
6124 free(ctsio->kern_data_ptr, M_CTL);
6125 ctl_done((union ctl_io *)ctsio);
6126 return (CTL_RETVAL_COMPLETE);
6130 * Run through the mode page, checking to make sure that the bits
6131 * the user changed are actually legal for him to change.
6133 for (i = 0; i < page_index->page_len; i++) {
6134 uint8_t *user_byte, *change_mask, *current_byte;
6138 user_byte = (uint8_t *)page_header + i;
6139 change_mask = page_index->page_data +
6140 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6141 current_byte = page_index->page_data +
6142 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6145 * Check to see whether the user set any bits in this byte
6146 * that he is not allowed to set.
6148 if ((*user_byte & ~(*change_mask)) ==
6149 (*current_byte & ~(*change_mask)))
6153 * Go through bit by bit to determine which one is illegal.
6156 for (j = 7; j >= 0; j--) {
6157 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6158 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6163 ctl_set_invalid_field(ctsio,
6166 /*field*/ *len_used + i,
6169 free(ctsio->kern_data_ptr, M_CTL);
6170 ctl_done((union ctl_io *)ctsio);
6171 return (CTL_RETVAL_COMPLETE);
6175 * Decrement these before we call the page handler, since we may
6176 * end up getting called back one way or another before the handler
6177 * returns to this context.
6179 *len_left -= page_index->page_len;
6180 *len_used += page_index->page_len;
6182 retval = page_index->select_handler(ctsio, page_index,
6183 (uint8_t *)page_header);
6186 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6187 * wait until this queued command completes to finish processing
6188 * the mode page. If it returns anything other than
6189 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6190 * already set the sense information, freed the data pointer, and
6191 * completed the io for us.
6193 if (retval != CTL_RETVAL_COMPLETE)
6194 goto bailout_no_done;
6197 * If the initiator sent us more than one page, parse the next one.
6202 ctl_set_success(ctsio);
6203 free(ctsio->kern_data_ptr, M_CTL);
6204 ctl_done((union ctl_io *)ctsio);
6208 return (CTL_RETVAL_COMPLETE);
6213 ctl_mode_select(struct ctl_scsiio *ctsio)
6215 int param_len, pf, sp;
6216 int header_size, bd_len;
6217 union ctl_modepage_info *modepage_info;
6219 switch (ctsio->cdb[0]) {
6220 case MODE_SELECT_6: {
6221 struct scsi_mode_select_6 *cdb;
6223 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6225 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6226 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6227 param_len = cdb->length;
6228 header_size = sizeof(struct scsi_mode_header_6);
6231 case MODE_SELECT_10: {
6232 struct scsi_mode_select_10 *cdb;
6234 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6236 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6237 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6238 param_len = scsi_2btoul(cdb->length);
6239 header_size = sizeof(struct scsi_mode_header_10);
6243 ctl_set_invalid_opcode(ctsio);
6244 ctl_done((union ctl_io *)ctsio);
6245 return (CTL_RETVAL_COMPLETE);
6250 * "A parameter list length of zero indicates that the Data-Out Buffer
6251 * shall be empty. This condition shall not be considered as an error."
6253 if (param_len == 0) {
6254 ctl_set_success(ctsio);
6255 ctl_done((union ctl_io *)ctsio);
6256 return (CTL_RETVAL_COMPLETE);
6260 * Since we'll hit this the first time through, prior to
6261 * allocation, we don't need to free a data buffer here.
6263 if (param_len < header_size) {
6264 ctl_set_param_len_error(ctsio);
6265 ctl_done((union ctl_io *)ctsio);
6266 return (CTL_RETVAL_COMPLETE);
6270 * Allocate the data buffer and grab the user's data. In theory,
6271 * we shouldn't have to sanity check the parameter list length here
6272 * because the maximum size is 64K. We should be able to malloc
6273 * that much without too many problems.
6275 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6276 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6277 ctsio->kern_data_len = param_len;
6278 ctsio->kern_total_len = param_len;
6279 ctsio->kern_data_resid = 0;
6280 ctsio->kern_rel_offset = 0;
6281 ctsio->kern_sg_entries = 0;
6282 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6283 ctsio->be_move_done = ctl_config_move_done;
6284 ctl_datamove((union ctl_io *)ctsio);
6286 return (CTL_RETVAL_COMPLETE);
6289 switch (ctsio->cdb[0]) {
6290 case MODE_SELECT_6: {
6291 struct scsi_mode_header_6 *mh6;
6293 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6294 bd_len = mh6->blk_desc_len;
6297 case MODE_SELECT_10: {
6298 struct scsi_mode_header_10 *mh10;
6300 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6301 bd_len = scsi_2btoul(mh10->blk_desc_len);
6305 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6308 if (param_len < (header_size + bd_len)) {
6309 free(ctsio->kern_data_ptr, M_CTL);
6310 ctl_set_param_len_error(ctsio);
6311 ctl_done((union ctl_io *)ctsio);
6312 return (CTL_RETVAL_COMPLETE);
6316 * Set the IO_CONT flag, so that if this I/O gets passed to
6317 * ctl_config_write_done(), it'll get passed back to
6318 * ctl_do_mode_select() for further processing, or completion if
6321 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6322 ctsio->io_cont = ctl_do_mode_select;
6324 modepage_info = (union ctl_modepage_info *)
6325 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6326 memset(modepage_info, 0, sizeof(*modepage_info));
6327 modepage_info->header.len_left = param_len - header_size - bd_len;
6328 modepage_info->header.len_used = header_size + bd_len;
6330 return (ctl_do_mode_select((union ctl_io *)ctsio));
6334 ctl_mode_sense(struct ctl_scsiio *ctsio)
6336 struct ctl_lun *lun;
6337 int pc, page_code, dbd, llba, subpage;
6338 int alloc_len, page_len, header_len, total_len;
6339 struct scsi_mode_block_descr *block_desc;
6340 struct ctl_page_index *page_index;
6346 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6348 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6349 switch (ctsio->cdb[0]) {
6350 case MODE_SENSE_6: {
6351 struct scsi_mode_sense_6 *cdb;
6353 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6355 header_len = sizeof(struct scsi_mode_hdr_6);
6356 if (cdb->byte2 & SMS_DBD)
6359 header_len += sizeof(struct scsi_mode_block_descr);
6361 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6362 page_code = cdb->page & SMS_PAGE_CODE;
6363 subpage = cdb->subpage;
6364 alloc_len = cdb->length;
6367 case MODE_SENSE_10: {
6368 struct scsi_mode_sense_10 *cdb;
6370 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6372 header_len = sizeof(struct scsi_mode_hdr_10);
6374 if (cdb->byte2 & SMS_DBD)
6377 header_len += sizeof(struct scsi_mode_block_descr);
6378 if (cdb->byte2 & SMS10_LLBAA)
6380 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6381 page_code = cdb->page & SMS_PAGE_CODE;
6382 subpage = cdb->subpage;
6383 alloc_len = scsi_2btoul(cdb->length);
6387 ctl_set_invalid_opcode(ctsio);
6388 ctl_done((union ctl_io *)ctsio);
6389 return (CTL_RETVAL_COMPLETE);
6390 break; /* NOTREACHED */
6394 * We have to make a first pass through to calculate the size of
6395 * the pages that match the user's query. Then we allocate enough
6396 * memory to hold it, and actually copy the data into the buffer.
6398 switch (page_code) {
6399 case SMS_ALL_PAGES_PAGE: {
6405 * At the moment, values other than 0 and 0xff here are
6406 * reserved according to SPC-3.
6408 if ((subpage != SMS_SUBPAGE_PAGE_0)
6409 && (subpage != SMS_SUBPAGE_ALL)) {
6410 ctl_set_invalid_field(ctsio,
6416 ctl_done((union ctl_io *)ctsio);
6417 return (CTL_RETVAL_COMPLETE);
6420 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6421 page_index = &lun->mode_pages.index[i];
6423 /* Make sure the page is supported for this dev type */
6424 if (lun->be_lun->lun_type == T_DIRECT &&
6425 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6427 if (lun->be_lun->lun_type == T_PROCESSOR &&
6428 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6430 if (lun->be_lun->lun_type == T_CDROM &&
6431 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6435 * We don't use this subpage if the user didn't
6436 * request all subpages.
6438 if ((page_index->subpage != 0)
6439 && (subpage == SMS_SUBPAGE_PAGE_0))
6443 printf("found page %#x len %d\n",
6444 page_index->page_code & SMPH_PC_MASK,
6445 page_index->page_len);
6447 page_len += page_index->page_len;
6456 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6457 page_index = &lun->mode_pages.index[i];
6459 /* Make sure the page is supported for this dev type */
6460 if (lun->be_lun->lun_type == T_DIRECT &&
6461 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6463 if (lun->be_lun->lun_type == T_PROCESSOR &&
6464 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6466 if (lun->be_lun->lun_type == T_CDROM &&
6467 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6470 /* Look for the right page code */
6471 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6474 /* Look for the right subpage or the subpage wildcard*/
6475 if ((page_index->subpage != subpage)
6476 && (subpage != SMS_SUBPAGE_ALL))
6480 printf("found page %#x len %d\n",
6481 page_index->page_code & SMPH_PC_MASK,
6482 page_index->page_len);
6485 page_len += page_index->page_len;
6488 if (page_len == 0) {
6489 ctl_set_invalid_field(ctsio,
6495 ctl_done((union ctl_io *)ctsio);
6496 return (CTL_RETVAL_COMPLETE);
6502 total_len = header_len + page_len;
6504 printf("header_len = %d, page_len = %d, total_len = %d\n",
6505 header_len, page_len, total_len);
6508 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6509 ctsio->kern_sg_entries = 0;
6510 ctsio->kern_data_resid = 0;
6511 ctsio->kern_rel_offset = 0;
6512 if (total_len < alloc_len) {
6513 ctsio->residual = alloc_len - total_len;
6514 ctsio->kern_data_len = total_len;
6515 ctsio->kern_total_len = total_len;
6517 ctsio->residual = 0;
6518 ctsio->kern_data_len = alloc_len;
6519 ctsio->kern_total_len = alloc_len;
6522 switch (ctsio->cdb[0]) {
6523 case MODE_SENSE_6: {
6524 struct scsi_mode_hdr_6 *header;
6526 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6528 header->datalen = MIN(total_len - 1, 254);
6529 if (lun->be_lun->lun_type == T_DIRECT) {
6530 header->dev_specific = 0x10; /* DPOFUA */
6531 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6532 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6533 .eca_and_aen & SCP_SWP) != 0)
6534 header->dev_specific |= 0x80; /* WP */
6537 header->block_descr_len = 0;
6539 header->block_descr_len =
6540 sizeof(struct scsi_mode_block_descr);
6541 block_desc = (struct scsi_mode_block_descr *)&header[1];
6544 case MODE_SENSE_10: {
6545 struct scsi_mode_hdr_10 *header;
6548 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6550 datalen = MIN(total_len - 2, 65533);
6551 scsi_ulto2b(datalen, header->datalen);
6552 if (lun->be_lun->lun_type == T_DIRECT) {
6553 header->dev_specific = 0x10; /* DPOFUA */
6554 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6555 (lun->mode_pages.control_page[CTL_PAGE_CURRENT]
6556 .eca_and_aen & SCP_SWP) != 0)
6557 header->dev_specific |= 0x80; /* WP */
6560 scsi_ulto2b(0, header->block_descr_len);
6562 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6563 header->block_descr_len);
6564 block_desc = (struct scsi_mode_block_descr *)&header[1];
6568 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6572 * If we've got a disk, use its blocksize in the block
6573 * descriptor. Otherwise, just set it to 0.
6576 if (lun->be_lun->lun_type == T_DIRECT)
6577 scsi_ulto3b(lun->be_lun->blocksize,
6578 block_desc->block_len);
6580 scsi_ulto3b(0, block_desc->block_len);
6583 switch (page_code) {
6584 case SMS_ALL_PAGES_PAGE: {
6587 data_used = header_len;
6588 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6589 struct ctl_page_index *page_index;
6591 page_index = &lun->mode_pages.index[i];
6592 if (lun->be_lun->lun_type == T_DIRECT &&
6593 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6595 if (lun->be_lun->lun_type == T_PROCESSOR &&
6596 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6598 if (lun->be_lun->lun_type == T_CDROM &&
6599 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6603 * We don't use this subpage if the user didn't
6604 * request all subpages. We already checked (above)
6605 * to make sure the user only specified a subpage
6606 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6608 if ((page_index->subpage != 0)
6609 && (subpage == SMS_SUBPAGE_PAGE_0))
6613 * Call the handler, if it exists, to update the
6614 * page to the latest values.
6616 if (page_index->sense_handler != NULL)
6617 page_index->sense_handler(ctsio, page_index,pc);
6619 memcpy(ctsio->kern_data_ptr + data_used,
6620 page_index->page_data +
6621 (page_index->page_len * pc),
6622 page_index->page_len);
6623 data_used += page_index->page_len;
6630 data_used = header_len;
6632 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6633 struct ctl_page_index *page_index;
6635 page_index = &lun->mode_pages.index[i];
6637 /* Look for the right page code */
6638 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6641 /* Look for the right subpage or the subpage wildcard*/
6642 if ((page_index->subpage != subpage)
6643 && (subpage != SMS_SUBPAGE_ALL))
6646 /* Make sure the page is supported for this dev type */
6647 if (lun->be_lun->lun_type == T_DIRECT &&
6648 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6650 if (lun->be_lun->lun_type == T_PROCESSOR &&
6651 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6653 if (lun->be_lun->lun_type == T_CDROM &&
6654 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6658 * Call the handler, if it exists, to update the
6659 * page to the latest values.
6661 if (page_index->sense_handler != NULL)
6662 page_index->sense_handler(ctsio, page_index,pc);
6664 memcpy(ctsio->kern_data_ptr + data_used,
6665 page_index->page_data +
6666 (page_index->page_len * pc),
6667 page_index->page_len);
6668 data_used += page_index->page_len;
6674 ctl_set_success(ctsio);
6675 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6676 ctsio->be_move_done = ctl_config_move_done;
6677 ctl_datamove((union ctl_io *)ctsio);
6678 return (CTL_RETVAL_COMPLETE);
6682 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6683 struct ctl_page_index *page_index,
6686 struct ctl_lun *lun;
6687 struct scsi_log_param_header *phdr;
6691 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6692 data = page_index->page_data;
6694 if (lun->backend->lun_attr != NULL &&
6695 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6697 phdr = (struct scsi_log_param_header *)data;
6698 scsi_ulto2b(0x0001, phdr->param_code);
6699 phdr->param_control = SLP_LBIN | SLP_LP;
6700 phdr->param_len = 8;
6701 data = (uint8_t *)(phdr + 1);
6702 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6703 data[4] = 0x02; /* per-pool */
6704 data += phdr->param_len;
6707 if (lun->backend->lun_attr != NULL &&
6708 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6710 phdr = (struct scsi_log_param_header *)data;
6711 scsi_ulto2b(0x0002, phdr->param_code);
6712 phdr->param_control = SLP_LBIN | SLP_LP;
6713 phdr->param_len = 8;
6714 data = (uint8_t *)(phdr + 1);
6715 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6716 data[4] = 0x01; /* per-LUN */
6717 data += phdr->param_len;
6720 if (lun->backend->lun_attr != NULL &&
6721 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6723 phdr = (struct scsi_log_param_header *)data;
6724 scsi_ulto2b(0x00f1, phdr->param_code);
6725 phdr->param_control = SLP_LBIN | SLP_LP;
6726 phdr->param_len = 8;
6727 data = (uint8_t *)(phdr + 1);
6728 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6729 data[4] = 0x02; /* per-pool */
6730 data += phdr->param_len;
6733 if (lun->backend->lun_attr != NULL &&
6734 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6736 phdr = (struct scsi_log_param_header *)data;
6737 scsi_ulto2b(0x00f2, phdr->param_code);
6738 phdr->param_control = SLP_LBIN | SLP_LP;
6739 phdr->param_len = 8;
6740 data = (uint8_t *)(phdr + 1);
6741 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6742 data[4] = 0x02; /* per-pool */
6743 data += phdr->param_len;
6746 page_index->page_len = data - page_index->page_data;
6751 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6752 struct ctl_page_index *page_index,
6755 struct ctl_lun *lun;
6756 struct stat_page *data;
6757 uint64_t rn, wn, rb, wb;
6758 struct bintime rt, wt;
6761 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6762 data = (struct stat_page *)page_index->page_data;
6764 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6765 data->sap.hdr.param_control = SLP_LBIN;
6766 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6767 sizeof(struct scsi_log_param_header);
6768 rn = wn = rb = wb = 0;
6771 for (i = 0; i < CTL_MAX_PORTS; i++) {
6772 rn += lun->stats.ports[i].operations[CTL_STATS_READ];
6773 wn += lun->stats.ports[i].operations[CTL_STATS_WRITE];
6774 rb += lun->stats.ports[i].bytes[CTL_STATS_READ];
6775 wb += lun->stats.ports[i].bytes[CTL_STATS_WRITE];
6776 bintime_add(&rt, &lun->stats.ports[i].time[CTL_STATS_READ]);
6777 bintime_add(&wt, &lun->stats.ports[i].time[CTL_STATS_WRITE]);
6779 scsi_u64to8b(rn, data->sap.read_num);
6780 scsi_u64to8b(wn, data->sap.write_num);
6781 if (lun->stats.blocksize > 0) {
6782 scsi_u64to8b(wb / lun->stats.blocksize,
6783 data->sap.recvieved_lba);
6784 scsi_u64to8b(rb / lun->stats.blocksize,
6785 data->sap.transmitted_lba);
6787 scsi_u64to8b((uint64_t)rt.sec * 1000 + rt.frac / (UINT64_MAX / 1000),
6788 data->sap.read_int);
6789 scsi_u64to8b((uint64_t)wt.sec * 1000 + wt.frac / (UINT64_MAX / 1000),
6790 data->sap.write_int);
6791 scsi_u64to8b(0, data->sap.weighted_num);
6792 scsi_u64to8b(0, data->sap.weighted_int);
6793 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6794 data->it.hdr.param_control = SLP_LBIN;
6795 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6796 sizeof(struct scsi_log_param_header);
6798 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6800 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6801 data->it.hdr.param_control = SLP_LBIN;
6802 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6803 sizeof(struct scsi_log_param_header);
6804 scsi_ulto4b(3, data->ti.exponent);
6805 scsi_ulto4b(1, data->ti.integer);
6807 page_index->page_len = sizeof(*data);
6812 ctl_log_sense(struct ctl_scsiio *ctsio)
6814 struct ctl_lun *lun;
6815 int i, pc, page_code, subpage;
6816 int alloc_len, total_len;
6817 struct ctl_page_index *page_index;
6818 struct scsi_log_sense *cdb;
6819 struct scsi_log_header *header;
6821 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6823 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6824 cdb = (struct scsi_log_sense *)ctsio->cdb;
6825 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6826 page_code = cdb->page & SLS_PAGE_CODE;
6827 subpage = cdb->subpage;
6828 alloc_len = scsi_2btoul(cdb->length);
6831 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6832 page_index = &lun->log_pages.index[i];
6834 /* Look for the right page code */
6835 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6838 /* Look for the right subpage or the subpage wildcard*/
6839 if (page_index->subpage != subpage)
6844 if (i >= CTL_NUM_LOG_PAGES) {
6845 ctl_set_invalid_field(ctsio,
6851 ctl_done((union ctl_io *)ctsio);
6852 return (CTL_RETVAL_COMPLETE);
6855 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6857 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6858 ctsio->kern_sg_entries = 0;
6859 ctsio->kern_data_resid = 0;
6860 ctsio->kern_rel_offset = 0;
6861 if (total_len < alloc_len) {
6862 ctsio->residual = alloc_len - total_len;
6863 ctsio->kern_data_len = total_len;
6864 ctsio->kern_total_len = total_len;
6866 ctsio->residual = 0;
6867 ctsio->kern_data_len = alloc_len;
6868 ctsio->kern_total_len = alloc_len;
6871 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6872 header->page = page_index->page_code;
6873 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6874 header->page |= SL_DS;
6875 if (page_index->subpage) {
6876 header->page |= SL_SPF;
6877 header->subpage = page_index->subpage;
6879 scsi_ulto2b(page_index->page_len, header->datalen);
6882 * Call the handler, if it exists, to update the
6883 * page to the latest values.
6885 if (page_index->sense_handler != NULL)
6886 page_index->sense_handler(ctsio, page_index, pc);
6888 memcpy(header + 1, page_index->page_data, page_index->page_len);
6890 ctl_set_success(ctsio);
6891 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6892 ctsio->be_move_done = ctl_config_move_done;
6893 ctl_datamove((union ctl_io *)ctsio);
6894 return (CTL_RETVAL_COMPLETE);
6898 ctl_read_capacity(struct ctl_scsiio *ctsio)
6900 struct scsi_read_capacity *cdb;
6901 struct scsi_read_capacity_data *data;
6902 struct ctl_lun *lun;
6905 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6907 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6909 lba = scsi_4btoul(cdb->addr);
6910 if (((cdb->pmi & SRC_PMI) == 0)
6912 ctl_set_invalid_field(/*ctsio*/ ctsio,
6918 ctl_done((union ctl_io *)ctsio);
6919 return (CTL_RETVAL_COMPLETE);
6922 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6924 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6925 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6926 ctsio->residual = 0;
6927 ctsio->kern_data_len = sizeof(*data);
6928 ctsio->kern_total_len = sizeof(*data);
6929 ctsio->kern_data_resid = 0;
6930 ctsio->kern_rel_offset = 0;
6931 ctsio->kern_sg_entries = 0;
6934 * If the maximum LBA is greater than 0xfffffffe, the user must
6935 * issue a SERVICE ACTION IN (16) command, with the read capacity
6936 * serivce action set.
6938 if (lun->be_lun->maxlba > 0xfffffffe)
6939 scsi_ulto4b(0xffffffff, data->addr);
6941 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6944 * XXX KDM this may not be 512 bytes...
6946 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6948 ctl_set_success(ctsio);
6949 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6950 ctsio->be_move_done = ctl_config_move_done;
6951 ctl_datamove((union ctl_io *)ctsio);
6952 return (CTL_RETVAL_COMPLETE);
6956 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6958 struct scsi_read_capacity_16 *cdb;
6959 struct scsi_read_capacity_data_long *data;
6960 struct ctl_lun *lun;
6964 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6966 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6968 alloc_len = scsi_4btoul(cdb->alloc_len);
6969 lba = scsi_8btou64(cdb->addr);
6971 if ((cdb->reladr & SRC16_PMI)
6973 ctl_set_invalid_field(/*ctsio*/ ctsio,
6979 ctl_done((union ctl_io *)ctsio);
6980 return (CTL_RETVAL_COMPLETE);
6983 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
6985 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6986 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6988 if (sizeof(*data) < alloc_len) {
6989 ctsio->residual = alloc_len - sizeof(*data);
6990 ctsio->kern_data_len = sizeof(*data);
6991 ctsio->kern_total_len = sizeof(*data);
6993 ctsio->residual = 0;
6994 ctsio->kern_data_len = alloc_len;
6995 ctsio->kern_total_len = alloc_len;
6997 ctsio->kern_data_resid = 0;
6998 ctsio->kern_rel_offset = 0;
6999 ctsio->kern_sg_entries = 0;
7001 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7002 /* XXX KDM this may not be 512 bytes... */
7003 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7004 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7005 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7006 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7007 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7009 ctl_set_success(ctsio);
7010 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7011 ctsio->be_move_done = ctl_config_move_done;
7012 ctl_datamove((union ctl_io *)ctsio);
7013 return (CTL_RETVAL_COMPLETE);
7017 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7019 struct scsi_get_lba_status *cdb;
7020 struct scsi_get_lba_status_data *data;
7021 struct ctl_lun *lun;
7022 struct ctl_lba_len_flags *lbalen;
7024 uint32_t alloc_len, total_len;
7027 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7029 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7030 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7031 lba = scsi_8btou64(cdb->addr);
7032 alloc_len = scsi_4btoul(cdb->alloc_len);
7034 if (lba > lun->be_lun->maxlba) {
7035 ctl_set_lba_out_of_range(ctsio);
7036 ctl_done((union ctl_io *)ctsio);
7037 return (CTL_RETVAL_COMPLETE);
7040 total_len = sizeof(*data) + sizeof(data->descr[0]);
7041 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7042 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7044 if (total_len < alloc_len) {
7045 ctsio->residual = alloc_len - total_len;
7046 ctsio->kern_data_len = total_len;
7047 ctsio->kern_total_len = total_len;
7049 ctsio->residual = 0;
7050 ctsio->kern_data_len = alloc_len;
7051 ctsio->kern_total_len = alloc_len;
7053 ctsio->kern_data_resid = 0;
7054 ctsio->kern_rel_offset = 0;
7055 ctsio->kern_sg_entries = 0;
7057 /* Fill dummy data in case backend can't tell anything. */
7058 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7059 scsi_u64to8b(lba, data->descr[0].addr);
7060 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7061 data->descr[0].length);
7062 data->descr[0].status = 0; /* Mapped or unknown. */
7064 ctl_set_success(ctsio);
7065 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7066 ctsio->be_move_done = ctl_config_move_done;
7068 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7070 lbalen->len = total_len;
7072 retval = lun->backend->config_read((union ctl_io *)ctsio);
7073 return (CTL_RETVAL_COMPLETE);
7077 ctl_read_defect(struct ctl_scsiio *ctsio)
7079 struct scsi_read_defect_data_10 *ccb10;
7080 struct scsi_read_defect_data_12 *ccb12;
7081 struct scsi_read_defect_data_hdr_10 *data10;
7082 struct scsi_read_defect_data_hdr_12 *data12;
7083 uint32_t alloc_len, data_len;
7086 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7088 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7089 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7090 format = ccb10->format;
7091 alloc_len = scsi_2btoul(ccb10->alloc_length);
7092 data_len = sizeof(*data10);
7094 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7095 format = ccb12->format;
7096 alloc_len = scsi_4btoul(ccb12->alloc_length);
7097 data_len = sizeof(*data12);
7099 if (alloc_len == 0) {
7100 ctl_set_success(ctsio);
7101 ctl_done((union ctl_io *)ctsio);
7102 return (CTL_RETVAL_COMPLETE);
7105 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7106 if (data_len < alloc_len) {
7107 ctsio->residual = alloc_len - data_len;
7108 ctsio->kern_data_len = data_len;
7109 ctsio->kern_total_len = data_len;
7111 ctsio->residual = 0;
7112 ctsio->kern_data_len = alloc_len;
7113 ctsio->kern_total_len = alloc_len;
7115 ctsio->kern_data_resid = 0;
7116 ctsio->kern_rel_offset = 0;
7117 ctsio->kern_sg_entries = 0;
7119 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7120 data10 = (struct scsi_read_defect_data_hdr_10 *)
7121 ctsio->kern_data_ptr;
7122 data10->format = format;
7123 scsi_ulto2b(0, data10->length);
7125 data12 = (struct scsi_read_defect_data_hdr_12 *)
7126 ctsio->kern_data_ptr;
7127 data12->format = format;
7128 scsi_ulto2b(0, data12->generation);
7129 scsi_ulto4b(0, data12->length);
7132 ctl_set_success(ctsio);
7133 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7134 ctsio->be_move_done = ctl_config_move_done;
7135 ctl_datamove((union ctl_io *)ctsio);
7136 return (CTL_RETVAL_COMPLETE);
7140 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7142 struct scsi_maintenance_in *cdb;
7144 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7145 int num_ha_groups, num_target_ports, shared_group;
7146 struct ctl_lun *lun;
7147 struct ctl_softc *softc;
7148 struct ctl_port *port;
7149 struct scsi_target_group_data *rtg_ptr;
7150 struct scsi_target_group_data_extended *rtg_ext_ptr;
7151 struct scsi_target_port_group_descriptor *tpg_desc;
7153 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7155 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7156 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7157 softc = lun->ctl_softc;
7159 retval = CTL_RETVAL_COMPLETE;
7161 switch (cdb->byte2 & STG_PDF_MASK) {
7162 case STG_PDF_LENGTH:
7165 case STG_PDF_EXTENDED:
7169 ctl_set_invalid_field(/*ctsio*/ ctsio,
7175 ctl_done((union ctl_io *)ctsio);
7179 num_target_ports = 0;
7180 shared_group = (softc->is_single != 0);
7181 mtx_lock(&softc->ctl_lock);
7182 STAILQ_FOREACH(port, &softc->port_list, links) {
7183 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7185 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7188 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7191 mtx_unlock(&softc->ctl_lock);
7192 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7195 total_len = sizeof(struct scsi_target_group_data_extended);
7197 total_len = sizeof(struct scsi_target_group_data);
7198 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7199 (shared_group + num_ha_groups) +
7200 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7202 alloc_len = scsi_4btoul(cdb->length);
7204 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7206 ctsio->kern_sg_entries = 0;
7208 if (total_len < alloc_len) {
7209 ctsio->residual = alloc_len - total_len;
7210 ctsio->kern_data_len = total_len;
7211 ctsio->kern_total_len = total_len;
7213 ctsio->residual = 0;
7214 ctsio->kern_data_len = alloc_len;
7215 ctsio->kern_total_len = alloc_len;
7217 ctsio->kern_data_resid = 0;
7218 ctsio->kern_rel_offset = 0;
7221 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7222 ctsio->kern_data_ptr;
7223 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7224 rtg_ext_ptr->format_type = 0x10;
7225 rtg_ext_ptr->implicit_transition_time = 0;
7226 tpg_desc = &rtg_ext_ptr->groups[0];
7228 rtg_ptr = (struct scsi_target_group_data *)
7229 ctsio->kern_data_ptr;
7230 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7231 tpg_desc = &rtg_ptr->groups[0];
7234 mtx_lock(&softc->ctl_lock);
7235 pg = softc->port_min / softc->port_cnt;
7236 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7237 /* Some shelf is known to be primary. */
7238 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7239 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7240 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7241 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7242 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7243 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7245 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7246 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7247 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7250 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7253 /* No known primary shelf. */
7254 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7255 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7256 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7257 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7258 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7259 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7261 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7265 tpg_desc->pref_state = ts;
7266 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7267 TPG_U_SUP | TPG_T_SUP;
7268 scsi_ulto2b(1, tpg_desc->target_port_group);
7269 tpg_desc->status = TPG_IMPLICIT;
7271 STAILQ_FOREACH(port, &softc->port_list, links) {
7272 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7274 if (!softc->is_single &&
7275 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7277 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7279 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7280 relative_target_port_identifier);
7283 tpg_desc->target_port_count = pc;
7284 tpg_desc = (struct scsi_target_port_group_descriptor *)
7285 &tpg_desc->descriptors[pc];
7287 for (g = 0; g < num_ha_groups; g++) {
7288 tpg_desc->pref_state = (g == pg) ? ts : os;
7289 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7290 TPG_U_SUP | TPG_T_SUP;
7291 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7292 tpg_desc->status = TPG_IMPLICIT;
7294 STAILQ_FOREACH(port, &softc->port_list, links) {
7295 if (port->targ_port < g * softc->port_cnt ||
7296 port->targ_port >= (g + 1) * softc->port_cnt)
7298 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7300 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7302 if (ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
7304 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7305 relative_target_port_identifier);
7308 tpg_desc->target_port_count = pc;
7309 tpg_desc = (struct scsi_target_port_group_descriptor *)
7310 &tpg_desc->descriptors[pc];
7312 mtx_unlock(&softc->ctl_lock);
7314 ctl_set_success(ctsio);
7315 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7316 ctsio->be_move_done = ctl_config_move_done;
7317 ctl_datamove((union ctl_io *)ctsio);
7322 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7324 struct ctl_lun *lun;
7325 struct scsi_report_supported_opcodes *cdb;
7326 const struct ctl_cmd_entry *entry, *sentry;
7327 struct scsi_report_supported_opcodes_all *all;
7328 struct scsi_report_supported_opcodes_descr *descr;
7329 struct scsi_report_supported_opcodes_one *one;
7331 int alloc_len, total_len;
7332 int opcode, service_action, i, j, num;
7334 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7336 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7337 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7339 retval = CTL_RETVAL_COMPLETE;
7341 opcode = cdb->requested_opcode;
7342 service_action = scsi_2btoul(cdb->requested_service_action);
7343 switch (cdb->options & RSO_OPTIONS_MASK) {
7344 case RSO_OPTIONS_ALL:
7346 for (i = 0; i < 256; i++) {
7347 entry = &ctl_cmd_table[i];
7348 if (entry->flags & CTL_CMD_FLAG_SA5) {
7349 for (j = 0; j < 32; j++) {
7350 sentry = &((const struct ctl_cmd_entry *)
7352 if (ctl_cmd_applicable(
7353 lun->be_lun->lun_type, sentry))
7357 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7362 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7363 num * sizeof(struct scsi_report_supported_opcodes_descr);
7365 case RSO_OPTIONS_OC:
7366 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7367 ctl_set_invalid_field(/*ctsio*/ ctsio,
7373 ctl_done((union ctl_io *)ctsio);
7374 return (CTL_RETVAL_COMPLETE);
7376 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7378 case RSO_OPTIONS_OC_SA:
7379 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7380 service_action >= 32) {
7381 ctl_set_invalid_field(/*ctsio*/ ctsio,
7387 ctl_done((union ctl_io *)ctsio);
7388 return (CTL_RETVAL_COMPLETE);
7390 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7393 ctl_set_invalid_field(/*ctsio*/ ctsio,
7399 ctl_done((union ctl_io *)ctsio);
7400 return (CTL_RETVAL_COMPLETE);
7403 alloc_len = scsi_4btoul(cdb->length);
7405 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7407 ctsio->kern_sg_entries = 0;
7409 if (total_len < alloc_len) {
7410 ctsio->residual = alloc_len - total_len;
7411 ctsio->kern_data_len = total_len;
7412 ctsio->kern_total_len = total_len;
7414 ctsio->residual = 0;
7415 ctsio->kern_data_len = alloc_len;
7416 ctsio->kern_total_len = alloc_len;
7418 ctsio->kern_data_resid = 0;
7419 ctsio->kern_rel_offset = 0;
7421 switch (cdb->options & RSO_OPTIONS_MASK) {
7422 case RSO_OPTIONS_ALL:
7423 all = (struct scsi_report_supported_opcodes_all *)
7424 ctsio->kern_data_ptr;
7426 for (i = 0; i < 256; i++) {
7427 entry = &ctl_cmd_table[i];
7428 if (entry->flags & CTL_CMD_FLAG_SA5) {
7429 for (j = 0; j < 32; j++) {
7430 sentry = &((const struct ctl_cmd_entry *)
7432 if (!ctl_cmd_applicable(
7433 lun->be_lun->lun_type, sentry))
7435 descr = &all->descr[num++];
7437 scsi_ulto2b(j, descr->service_action);
7438 descr->flags = RSO_SERVACTV;
7439 scsi_ulto2b(sentry->length,
7443 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7446 descr = &all->descr[num++];
7448 scsi_ulto2b(0, descr->service_action);
7450 scsi_ulto2b(entry->length, descr->cdb_length);
7454 num * sizeof(struct scsi_report_supported_opcodes_descr),
7457 case RSO_OPTIONS_OC:
7458 one = (struct scsi_report_supported_opcodes_one *)
7459 ctsio->kern_data_ptr;
7460 entry = &ctl_cmd_table[opcode];
7462 case RSO_OPTIONS_OC_SA:
7463 one = (struct scsi_report_supported_opcodes_one *)
7464 ctsio->kern_data_ptr;
7465 entry = &ctl_cmd_table[opcode];
7466 entry = &((const struct ctl_cmd_entry *)
7467 entry->execute)[service_action];
7469 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7471 scsi_ulto2b(entry->length, one->cdb_length);
7472 one->cdb_usage[0] = opcode;
7473 memcpy(&one->cdb_usage[1], entry->usage,
7480 ctl_set_success(ctsio);
7481 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7482 ctsio->be_move_done = ctl_config_move_done;
7483 ctl_datamove((union ctl_io *)ctsio);
7488 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7490 struct scsi_report_supported_tmf *cdb;
7491 struct scsi_report_supported_tmf_data *data;
7493 int alloc_len, total_len;
7495 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7497 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7499 retval = CTL_RETVAL_COMPLETE;
7501 total_len = sizeof(struct scsi_report_supported_tmf_data);
7502 alloc_len = scsi_4btoul(cdb->length);
7504 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7506 ctsio->kern_sg_entries = 0;
7508 if (total_len < alloc_len) {
7509 ctsio->residual = alloc_len - total_len;
7510 ctsio->kern_data_len = total_len;
7511 ctsio->kern_total_len = total_len;
7513 ctsio->residual = 0;
7514 ctsio->kern_data_len = alloc_len;
7515 ctsio->kern_total_len = alloc_len;
7517 ctsio->kern_data_resid = 0;
7518 ctsio->kern_rel_offset = 0;
7520 data = (struct scsi_report_supported_tmf_data *)ctsio->kern_data_ptr;
7521 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7523 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7525 ctl_set_success(ctsio);
7526 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7527 ctsio->be_move_done = ctl_config_move_done;
7528 ctl_datamove((union ctl_io *)ctsio);
7533 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7535 struct scsi_report_timestamp *cdb;
7536 struct scsi_report_timestamp_data *data;
7540 int alloc_len, total_len;
7542 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7544 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7546 retval = CTL_RETVAL_COMPLETE;
7548 total_len = sizeof(struct scsi_report_timestamp_data);
7549 alloc_len = scsi_4btoul(cdb->length);
7551 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7553 ctsio->kern_sg_entries = 0;
7555 if (total_len < alloc_len) {
7556 ctsio->residual = alloc_len - total_len;
7557 ctsio->kern_data_len = total_len;
7558 ctsio->kern_total_len = total_len;
7560 ctsio->residual = 0;
7561 ctsio->kern_data_len = alloc_len;
7562 ctsio->kern_total_len = alloc_len;
7564 ctsio->kern_data_resid = 0;
7565 ctsio->kern_rel_offset = 0;
7567 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7568 scsi_ulto2b(sizeof(*data) - 2, data->length);
7569 data->origin = RTS_ORIG_OUTSIDE;
7571 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7572 scsi_ulto4b(timestamp >> 16, data->timestamp);
7573 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7575 ctl_set_success(ctsio);
7576 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7577 ctsio->be_move_done = ctl_config_move_done;
7578 ctl_datamove((union ctl_io *)ctsio);
7583 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7585 struct scsi_per_res_in *cdb;
7586 int alloc_len, total_len = 0;
7587 /* struct scsi_per_res_in_rsrv in_data; */
7588 struct ctl_lun *lun;
7589 struct ctl_softc *softc;
7592 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7594 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7596 alloc_len = scsi_2btoul(cdb->length);
7598 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
7599 softc = lun->ctl_softc;
7602 mtx_lock(&lun->lun_lock);
7603 switch (cdb->action) {
7604 case SPRI_RK: /* read keys */
7605 total_len = sizeof(struct scsi_per_res_in_keys) +
7607 sizeof(struct scsi_per_res_key);
7609 case SPRI_RR: /* read reservation */
7610 if (lun->flags & CTL_LUN_PR_RESERVED)
7611 total_len = sizeof(struct scsi_per_res_in_rsrv);
7613 total_len = sizeof(struct scsi_per_res_in_header);
7615 case SPRI_RC: /* report capabilities */
7616 total_len = sizeof(struct scsi_per_res_cap);
7618 case SPRI_RS: /* read full status */
7619 total_len = sizeof(struct scsi_per_res_in_header) +
7620 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7624 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7626 mtx_unlock(&lun->lun_lock);
7628 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7630 if (total_len < alloc_len) {
7631 ctsio->residual = alloc_len - total_len;
7632 ctsio->kern_data_len = total_len;
7633 ctsio->kern_total_len = total_len;
7635 ctsio->residual = 0;
7636 ctsio->kern_data_len = alloc_len;
7637 ctsio->kern_total_len = alloc_len;
7640 ctsio->kern_data_resid = 0;
7641 ctsio->kern_rel_offset = 0;
7642 ctsio->kern_sg_entries = 0;
7644 mtx_lock(&lun->lun_lock);
7645 switch (cdb->action) {
7646 case SPRI_RK: { // read keys
7647 struct scsi_per_res_in_keys *res_keys;
7650 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7653 * We had to drop the lock to allocate our buffer, which
7654 * leaves time for someone to come in with another
7655 * persistent reservation. (That is unlikely, though,
7656 * since this should be the only persistent reservation
7657 * command active right now.)
7659 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7660 (lun->pr_key_count *
7661 sizeof(struct scsi_per_res_key)))){
7662 mtx_unlock(&lun->lun_lock);
7663 free(ctsio->kern_data_ptr, M_CTL);
7664 printf("%s: reservation length changed, retrying\n",
7669 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7671 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7672 lun->pr_key_count, res_keys->header.length);
7674 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7675 if ((key = ctl_get_prkey(lun, i)) == 0)
7679 * We used lun->pr_key_count to calculate the
7680 * size to allocate. If it turns out the number of
7681 * initiators with the registered flag set is
7682 * larger than that (i.e. they haven't been kept in
7683 * sync), we've got a problem.
7685 if (key_count >= lun->pr_key_count) {
7689 scsi_u64to8b(key, res_keys->keys[key_count].key);
7694 case SPRI_RR: { // read reservation
7695 struct scsi_per_res_in_rsrv *res;
7696 int tmp_len, header_only;
7698 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7700 scsi_ulto4b(lun->pr_generation, res->header.generation);
7702 if (lun->flags & CTL_LUN_PR_RESERVED)
7704 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7705 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7706 res->header.length);
7709 tmp_len = sizeof(struct scsi_per_res_in_header);
7710 scsi_ulto4b(0, res->header.length);
7715 * We had to drop the lock to allocate our buffer, which
7716 * leaves time for someone to come in with another
7717 * persistent reservation. (That is unlikely, though,
7718 * since this should be the only persistent reservation
7719 * command active right now.)
7721 if (tmp_len != total_len) {
7722 mtx_unlock(&lun->lun_lock);
7723 free(ctsio->kern_data_ptr, M_CTL);
7724 printf("%s: reservation status changed, retrying\n",
7730 * No reservation held, so we're done.
7732 if (header_only != 0)
7736 * If the registration is an All Registrants type, the key
7737 * is 0, since it doesn't really matter.
7739 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7740 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7741 res->data.reservation);
7743 res->data.scopetype = lun->pr_res_type;
7746 case SPRI_RC: //report capabilities
7748 struct scsi_per_res_cap *res_cap;
7751 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7752 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7753 res_cap->flags1 = SPRI_CRH;
7754 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7755 type_mask = SPRI_TM_WR_EX_AR |
7761 scsi_ulto2b(type_mask, res_cap->type_mask);
7764 case SPRI_RS: { // read full status
7765 struct scsi_per_res_in_full *res_status;
7766 struct scsi_per_res_in_full_desc *res_desc;
7767 struct ctl_port *port;
7770 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7773 * We had to drop the lock to allocate our buffer, which
7774 * leaves time for someone to come in with another
7775 * persistent reservation. (That is unlikely, though,
7776 * since this should be the only persistent reservation
7777 * command active right now.)
7779 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7780 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7781 lun->pr_key_count)){
7782 mtx_unlock(&lun->lun_lock);
7783 free(ctsio->kern_data_ptr, M_CTL);
7784 printf("%s: reservation length changed, retrying\n",
7789 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7791 res_desc = &res_status->desc[0];
7792 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7793 if ((key = ctl_get_prkey(lun, i)) == 0)
7796 scsi_u64to8b(key, res_desc->res_key.key);
7797 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7798 (lun->pr_res_idx == i ||
7799 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7800 res_desc->flags = SPRI_FULL_R_HOLDER;
7801 res_desc->scopetype = lun->pr_res_type;
7803 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7804 res_desc->rel_trgt_port_id);
7806 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7808 len = ctl_create_iid(port,
7809 i % CTL_MAX_INIT_PER_PORT,
7810 res_desc->transport_id);
7811 scsi_ulto4b(len, res_desc->additional_length);
7812 res_desc = (struct scsi_per_res_in_full_desc *)
7813 &res_desc->transport_id[len];
7815 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7816 res_status->header.length);
7820 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7822 mtx_unlock(&lun->lun_lock);
7824 ctl_set_success(ctsio);
7825 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7826 ctsio->be_move_done = ctl_config_move_done;
7827 ctl_datamove((union ctl_io *)ctsio);
7828 return (CTL_RETVAL_COMPLETE);
7832 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7836 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7837 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7838 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7839 struct scsi_per_res_out_parms* param)
7841 union ctl_ha_msg persis_io;
7844 mtx_lock(&lun->lun_lock);
7845 if (sa_res_key == 0) {
7846 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7847 /* validate scope and type */
7848 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7850 mtx_unlock(&lun->lun_lock);
7851 ctl_set_invalid_field(/*ctsio*/ ctsio,
7857 ctl_done((union ctl_io *)ctsio);
7861 if (type>8 || type==2 || type==4 || type==0) {
7862 mtx_unlock(&lun->lun_lock);
7863 ctl_set_invalid_field(/*ctsio*/ ctsio,
7869 ctl_done((union ctl_io *)ctsio);
7874 * Unregister everybody else and build UA for
7877 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7878 if (i == residx || ctl_get_prkey(lun, i) == 0)
7881 ctl_clr_prkey(lun, i);
7882 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7884 lun->pr_key_count = 1;
7885 lun->pr_res_type = type;
7886 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7887 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7888 lun->pr_res_idx = residx;
7889 lun->pr_generation++;
7890 mtx_unlock(&lun->lun_lock);
7892 /* send msg to other side */
7893 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7894 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7895 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7896 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7897 persis_io.pr.pr_info.res_type = type;
7898 memcpy(persis_io.pr.pr_info.sa_res_key,
7899 param->serv_act_res_key,
7900 sizeof(param->serv_act_res_key));
7901 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7902 sizeof(persis_io.pr), M_WAITOK);
7904 /* not all registrants */
7905 mtx_unlock(&lun->lun_lock);
7906 free(ctsio->kern_data_ptr, M_CTL);
7907 ctl_set_invalid_field(ctsio,
7913 ctl_done((union ctl_io *)ctsio);
7916 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7917 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7920 if (res_key == sa_res_key) {
7923 * The spec implies this is not good but doesn't
7924 * say what to do. There are two choices either
7925 * generate a res conflict or check condition
7926 * with illegal field in parameter data. Since
7927 * that is what is done when the sa_res_key is
7928 * zero I'll take that approach since this has
7929 * to do with the sa_res_key.
7931 mtx_unlock(&lun->lun_lock);
7932 free(ctsio->kern_data_ptr, M_CTL);
7933 ctl_set_invalid_field(ctsio,
7939 ctl_done((union ctl_io *)ctsio);
7943 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7944 if (ctl_get_prkey(lun, i) != sa_res_key)
7948 ctl_clr_prkey(lun, i);
7949 lun->pr_key_count--;
7950 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7953 mtx_unlock(&lun->lun_lock);
7954 free(ctsio->kern_data_ptr, M_CTL);
7955 ctl_set_reservation_conflict(ctsio);
7956 ctl_done((union ctl_io *)ctsio);
7957 return (CTL_RETVAL_COMPLETE);
7959 lun->pr_generation++;
7960 mtx_unlock(&lun->lun_lock);
7962 /* send msg to other side */
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);
7974 /* Reserved but not all registrants */
7975 /* sa_res_key is res holder */
7976 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7977 /* validate scope and type */
7978 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7980 mtx_unlock(&lun->lun_lock);
7981 ctl_set_invalid_field(/*ctsio*/ ctsio,
7987 ctl_done((union ctl_io *)ctsio);
7991 if (type>8 || type==2 || type==4 || type==0) {
7992 mtx_unlock(&lun->lun_lock);
7993 ctl_set_invalid_field(/*ctsio*/ ctsio,
7999 ctl_done((union ctl_io *)ctsio);
8005 * if sa_res_key != res_key remove all
8006 * registrants w/sa_res_key and generate UA
8007 * for these registrants(Registrations
8008 * Preempted) if it wasn't an exclusive
8009 * reservation generate UA(Reservations
8010 * Preempted) for all other registered nexuses
8011 * if the type has changed. Establish the new
8012 * reservation and holder. If res_key and
8013 * sa_res_key are the same do the above
8014 * except don't unregister the res holder.
8017 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8018 if (i == residx || ctl_get_prkey(lun, i) == 0)
8021 if (sa_res_key == ctl_get_prkey(lun, i)) {
8022 ctl_clr_prkey(lun, i);
8023 lun->pr_key_count--;
8024 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8025 } else if (type != lun->pr_res_type &&
8026 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8027 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8028 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8031 lun->pr_res_type = type;
8032 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8033 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8034 lun->pr_res_idx = residx;
8036 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8037 lun->pr_generation++;
8038 mtx_unlock(&lun->lun_lock);
8040 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8041 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8042 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8043 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8044 persis_io.pr.pr_info.res_type = type;
8045 memcpy(persis_io.pr.pr_info.sa_res_key,
8046 param->serv_act_res_key,
8047 sizeof(param->serv_act_res_key));
8048 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8049 sizeof(persis_io.pr), M_WAITOK);
8052 * sa_res_key is not the res holder just
8053 * remove registrants
8057 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8058 if (sa_res_key != ctl_get_prkey(lun, i))
8062 ctl_clr_prkey(lun, i);
8063 lun->pr_key_count--;
8064 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8068 mtx_unlock(&lun->lun_lock);
8069 free(ctsio->kern_data_ptr, M_CTL);
8070 ctl_set_reservation_conflict(ctsio);
8071 ctl_done((union ctl_io *)ctsio);
8074 lun->pr_generation++;
8075 mtx_unlock(&lun->lun_lock);
8077 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8078 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8079 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8080 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8081 persis_io.pr.pr_info.res_type = type;
8082 memcpy(persis_io.pr.pr_info.sa_res_key,
8083 param->serv_act_res_key,
8084 sizeof(param->serv_act_res_key));
8085 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8086 sizeof(persis_io.pr), M_WAITOK);
8093 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8095 uint64_t sa_res_key;
8098 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8100 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8101 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8102 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8103 if (sa_res_key == 0) {
8105 * Unregister everybody else and build UA for
8108 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8109 if (i == msg->pr.pr_info.residx ||
8110 ctl_get_prkey(lun, i) == 0)
8113 ctl_clr_prkey(lun, i);
8114 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8117 lun->pr_key_count = 1;
8118 lun->pr_res_type = msg->pr.pr_info.res_type;
8119 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8120 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8121 lun->pr_res_idx = msg->pr.pr_info.residx;
8123 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8124 if (sa_res_key == ctl_get_prkey(lun, i))
8127 ctl_clr_prkey(lun, i);
8128 lun->pr_key_count--;
8129 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8133 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8134 if (i == msg->pr.pr_info.residx ||
8135 ctl_get_prkey(lun, i) == 0)
8138 if (sa_res_key == ctl_get_prkey(lun, i)) {
8139 ctl_clr_prkey(lun, i);
8140 lun->pr_key_count--;
8141 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8142 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8143 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8144 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8145 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8148 lun->pr_res_type = msg->pr.pr_info.res_type;
8149 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8150 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8151 lun->pr_res_idx = msg->pr.pr_info.residx;
8153 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8155 lun->pr_generation++;
8161 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8164 u_int32_t param_len;
8165 struct scsi_per_res_out *cdb;
8166 struct ctl_lun *lun;
8167 struct scsi_per_res_out_parms* param;
8168 struct ctl_softc *softc;
8170 uint64_t res_key, sa_res_key, key;
8172 union ctl_ha_msg persis_io;
8175 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8177 retval = CTL_RETVAL_COMPLETE;
8179 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8180 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8181 softc = lun->ctl_softc;
8184 * We only support whole-LUN scope. The scope & type are ignored for
8185 * register, register and ignore existing key and clear.
8186 * We sometimes ignore scope and type on preempts too!!
8187 * Verify reservation type here as well.
8189 type = cdb->scope_type & SPR_TYPE_MASK;
8190 if ((cdb->action == SPRO_RESERVE)
8191 || (cdb->action == SPRO_RELEASE)) {
8192 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8193 ctl_set_invalid_field(/*ctsio*/ ctsio,
8199 ctl_done((union ctl_io *)ctsio);
8200 return (CTL_RETVAL_COMPLETE);
8203 if (type>8 || type==2 || type==4 || type==0) {
8204 ctl_set_invalid_field(/*ctsio*/ ctsio,
8210 ctl_done((union ctl_io *)ctsio);
8211 return (CTL_RETVAL_COMPLETE);
8215 param_len = scsi_4btoul(cdb->length);
8217 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8218 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8219 ctsio->kern_data_len = param_len;
8220 ctsio->kern_total_len = param_len;
8221 ctsio->kern_data_resid = 0;
8222 ctsio->kern_rel_offset = 0;
8223 ctsio->kern_sg_entries = 0;
8224 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8225 ctsio->be_move_done = ctl_config_move_done;
8226 ctl_datamove((union ctl_io *)ctsio);
8228 return (CTL_RETVAL_COMPLETE);
8231 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8233 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8234 res_key = scsi_8btou64(param->res_key.key);
8235 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8238 * Validate the reservation key here except for SPRO_REG_IGNO
8239 * This must be done for all other service actions
8241 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8242 mtx_lock(&lun->lun_lock);
8243 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8244 if (res_key != key) {
8246 * The current key passed in doesn't match
8247 * the one the initiator previously
8250 mtx_unlock(&lun->lun_lock);
8251 free(ctsio->kern_data_ptr, M_CTL);
8252 ctl_set_reservation_conflict(ctsio);
8253 ctl_done((union ctl_io *)ctsio);
8254 return (CTL_RETVAL_COMPLETE);
8256 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8258 * We are not registered
8260 mtx_unlock(&lun->lun_lock);
8261 free(ctsio->kern_data_ptr, M_CTL);
8262 ctl_set_reservation_conflict(ctsio);
8263 ctl_done((union ctl_io *)ctsio);
8264 return (CTL_RETVAL_COMPLETE);
8265 } else if (res_key != 0) {
8267 * We are not registered and trying to register but
8268 * the register key isn't zero.
8270 mtx_unlock(&lun->lun_lock);
8271 free(ctsio->kern_data_ptr, M_CTL);
8272 ctl_set_reservation_conflict(ctsio);
8273 ctl_done((union ctl_io *)ctsio);
8274 return (CTL_RETVAL_COMPLETE);
8276 mtx_unlock(&lun->lun_lock);
8279 switch (cdb->action & SPRO_ACTION_MASK) {
8281 case SPRO_REG_IGNO: {
8284 printf("Registration received\n");
8288 * We don't support any of these options, as we report in
8289 * the read capabilities request (see
8290 * ctl_persistent_reserve_in(), above).
8292 if ((param->flags & SPR_SPEC_I_PT)
8293 || (param->flags & SPR_ALL_TG_PT)
8294 || (param->flags & SPR_APTPL)) {
8297 if (param->flags & SPR_APTPL)
8299 else if (param->flags & SPR_ALL_TG_PT)
8301 else /* SPR_SPEC_I_PT */
8304 free(ctsio->kern_data_ptr, M_CTL);
8305 ctl_set_invalid_field(ctsio,
8311 ctl_done((union ctl_io *)ctsio);
8312 return (CTL_RETVAL_COMPLETE);
8315 mtx_lock(&lun->lun_lock);
8318 * The initiator wants to clear the
8321 if (sa_res_key == 0) {
8323 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8324 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8325 && ctl_get_prkey(lun, residx) == 0)) {
8326 mtx_unlock(&lun->lun_lock);
8330 ctl_clr_prkey(lun, residx);
8331 lun->pr_key_count--;
8333 if (residx == lun->pr_res_idx) {
8334 lun->flags &= ~CTL_LUN_PR_RESERVED;
8335 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8337 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8338 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8339 lun->pr_key_count) {
8341 * If the reservation is a registrants
8342 * only type we need to generate a UA
8343 * for other registered inits. The
8344 * sense code should be RESERVATIONS
8348 for (i = softc->init_min; i < softc->init_max; i++){
8349 if (ctl_get_prkey(lun, i) == 0)
8352 CTL_UA_RES_RELEASE);
8355 lun->pr_res_type = 0;
8356 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8357 if (lun->pr_key_count==0) {
8358 lun->flags &= ~CTL_LUN_PR_RESERVED;
8359 lun->pr_res_type = 0;
8360 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8363 lun->pr_generation++;
8364 mtx_unlock(&lun->lun_lock);
8366 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8367 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8368 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8369 persis_io.pr.pr_info.residx = residx;
8370 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8371 sizeof(persis_io.pr), M_WAITOK);
8372 } else /* sa_res_key != 0 */ {
8375 * If we aren't registered currently then increment
8376 * the key count and set the registered flag.
8378 ctl_alloc_prkey(lun, residx);
8379 if (ctl_get_prkey(lun, residx) == 0)
8380 lun->pr_key_count++;
8381 ctl_set_prkey(lun, residx, sa_res_key);
8382 lun->pr_generation++;
8383 mtx_unlock(&lun->lun_lock);
8385 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8386 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8387 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8388 persis_io.pr.pr_info.residx = residx;
8389 memcpy(persis_io.pr.pr_info.sa_res_key,
8390 param->serv_act_res_key,
8391 sizeof(param->serv_act_res_key));
8392 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8393 sizeof(persis_io.pr), M_WAITOK);
8400 printf("Reserve executed type %d\n", type);
8402 mtx_lock(&lun->lun_lock);
8403 if (lun->flags & CTL_LUN_PR_RESERVED) {
8405 * if this isn't the reservation holder and it's
8406 * not a "all registrants" type or if the type is
8407 * different then we have a conflict
8409 if ((lun->pr_res_idx != residx
8410 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8411 || lun->pr_res_type != type) {
8412 mtx_unlock(&lun->lun_lock);
8413 free(ctsio->kern_data_ptr, M_CTL);
8414 ctl_set_reservation_conflict(ctsio);
8415 ctl_done((union ctl_io *)ctsio);
8416 return (CTL_RETVAL_COMPLETE);
8418 mtx_unlock(&lun->lun_lock);
8419 } else /* create a reservation */ {
8421 * If it's not an "all registrants" type record
8422 * reservation holder
8424 if (type != SPR_TYPE_WR_EX_AR
8425 && type != SPR_TYPE_EX_AC_AR)
8426 lun->pr_res_idx = residx; /* Res holder */
8428 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8430 lun->flags |= CTL_LUN_PR_RESERVED;
8431 lun->pr_res_type = type;
8433 mtx_unlock(&lun->lun_lock);
8435 /* send msg to other side */
8436 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8437 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8438 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8439 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8440 persis_io.pr.pr_info.res_type = type;
8441 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8442 sizeof(persis_io.pr), M_WAITOK);
8447 mtx_lock(&lun->lun_lock);
8448 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8449 /* No reservation exists return good status */
8450 mtx_unlock(&lun->lun_lock);
8454 * Is this nexus a reservation holder?
8456 if (lun->pr_res_idx != residx
8457 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8459 * not a res holder return good status but
8462 mtx_unlock(&lun->lun_lock);
8466 if (lun->pr_res_type != type) {
8467 mtx_unlock(&lun->lun_lock);
8468 free(ctsio->kern_data_ptr, M_CTL);
8469 ctl_set_illegal_pr_release(ctsio);
8470 ctl_done((union ctl_io *)ctsio);
8471 return (CTL_RETVAL_COMPLETE);
8474 /* okay to release */
8475 lun->flags &= ~CTL_LUN_PR_RESERVED;
8476 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8477 lun->pr_res_type = 0;
8480 * if this isn't an exclusive access
8481 * res generate UA for all other
8484 if (type != SPR_TYPE_EX_AC
8485 && type != SPR_TYPE_WR_EX) {
8486 for (i = softc->init_min; i < softc->init_max; i++) {
8487 if (i == residx || ctl_get_prkey(lun, i) == 0)
8489 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8492 mtx_unlock(&lun->lun_lock);
8494 /* Send msg to other side */
8495 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8496 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8497 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8498 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8499 sizeof(persis_io.pr), M_WAITOK);
8503 /* send msg to other side */
8505 mtx_lock(&lun->lun_lock);
8506 lun->flags &= ~CTL_LUN_PR_RESERVED;
8507 lun->pr_res_type = 0;
8508 lun->pr_key_count = 0;
8509 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8511 ctl_clr_prkey(lun, residx);
8512 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8513 if (ctl_get_prkey(lun, i) != 0) {
8514 ctl_clr_prkey(lun, i);
8515 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8517 lun->pr_generation++;
8518 mtx_unlock(&lun->lun_lock);
8520 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8521 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8522 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8523 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8524 sizeof(persis_io.pr), M_WAITOK);
8528 case SPRO_PRE_ABO: {
8531 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8532 residx, ctsio, cdb, param);
8534 return (CTL_RETVAL_COMPLETE);
8538 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8542 free(ctsio->kern_data_ptr, M_CTL);
8543 ctl_set_success(ctsio);
8544 ctl_done((union ctl_io *)ctsio);
8550 * This routine is for handling a message from the other SC pertaining to
8551 * persistent reserve out. All the error checking will have been done
8552 * so only perorming the action need be done here to keep the two
8556 ctl_hndl_per_res_out_on_other_sc(union ctl_ha_msg *msg)
8558 struct ctl_softc *softc = control_softc;
8559 struct ctl_lun *lun;
8561 uint32_t residx, targ_lun;
8563 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8564 mtx_lock(&softc->ctl_lock);
8565 if ((targ_lun >= CTL_MAX_LUNS) ||
8566 ((lun = softc->ctl_luns[targ_lun]) == NULL)) {
8567 mtx_unlock(&softc->ctl_lock);
8570 mtx_lock(&lun->lun_lock);
8571 mtx_unlock(&softc->ctl_lock);
8572 if (lun->flags & CTL_LUN_DISABLED) {
8573 mtx_unlock(&lun->lun_lock);
8576 residx = ctl_get_initindex(&msg->hdr.nexus);
8577 switch(msg->pr.pr_info.action) {
8578 case CTL_PR_REG_KEY:
8579 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8580 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8581 lun->pr_key_count++;
8582 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8583 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8584 lun->pr_generation++;
8587 case CTL_PR_UNREG_KEY:
8588 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8589 lun->pr_key_count--;
8591 /* XXX Need to see if the reservation has been released */
8592 /* if so do we need to generate UA? */
8593 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8594 lun->flags &= ~CTL_LUN_PR_RESERVED;
8595 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8597 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8598 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8599 lun->pr_key_count) {
8601 * If the reservation is a registrants
8602 * only type we need to generate a UA
8603 * for other registered inits. The
8604 * sense code should be RESERVATIONS
8608 for (i = softc->init_min; i < softc->init_max; i++) {
8609 if (ctl_get_prkey(lun, i) == 0)
8612 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8615 lun->pr_res_type = 0;
8616 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8617 if (lun->pr_key_count==0) {
8618 lun->flags &= ~CTL_LUN_PR_RESERVED;
8619 lun->pr_res_type = 0;
8620 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8623 lun->pr_generation++;
8626 case CTL_PR_RESERVE:
8627 lun->flags |= CTL_LUN_PR_RESERVED;
8628 lun->pr_res_type = msg->pr.pr_info.res_type;
8629 lun->pr_res_idx = msg->pr.pr_info.residx;
8633 case CTL_PR_RELEASE:
8635 * if this isn't an exclusive access res generate UA for all
8636 * other registrants.
8638 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8639 lun->pr_res_type != SPR_TYPE_WR_EX) {
8640 for (i = softc->init_min; i < softc->init_max; i++)
8641 if (i == residx || ctl_get_prkey(lun, i) == 0)
8643 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8646 lun->flags &= ~CTL_LUN_PR_RESERVED;
8647 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8648 lun->pr_res_type = 0;
8651 case CTL_PR_PREEMPT:
8652 ctl_pro_preempt_other(lun, msg);
8655 lun->flags &= ~CTL_LUN_PR_RESERVED;
8656 lun->pr_res_type = 0;
8657 lun->pr_key_count = 0;
8658 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8660 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8661 if (ctl_get_prkey(lun, i) == 0)
8663 ctl_clr_prkey(lun, i);
8664 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8666 lun->pr_generation++;
8670 mtx_unlock(&lun->lun_lock);
8674 ctl_read_write(struct ctl_scsiio *ctsio)
8676 struct ctl_lun *lun;
8677 struct ctl_lba_len_flags *lbalen;
8679 uint32_t num_blocks;
8683 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8685 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8688 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8689 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8690 switch (ctsio->cdb[0]) {
8693 struct scsi_rw_6 *cdb;
8695 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8697 lba = scsi_3btoul(cdb->addr);
8698 /* only 5 bits are valid in the most significant address byte */
8700 num_blocks = cdb->length;
8702 * This is correct according to SBC-2.
8704 if (num_blocks == 0)
8710 struct scsi_rw_10 *cdb;
8712 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8713 if (cdb->byte2 & SRW10_FUA)
8714 flags |= CTL_LLF_FUA;
8715 if (cdb->byte2 & SRW10_DPO)
8716 flags |= CTL_LLF_DPO;
8717 lba = scsi_4btoul(cdb->addr);
8718 num_blocks = scsi_2btoul(cdb->length);
8721 case WRITE_VERIFY_10: {
8722 struct scsi_write_verify_10 *cdb;
8724 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8725 flags |= CTL_LLF_FUA;
8726 if (cdb->byte2 & SWV_DPO)
8727 flags |= CTL_LLF_DPO;
8728 lba = scsi_4btoul(cdb->addr);
8729 num_blocks = scsi_2btoul(cdb->length);
8734 struct scsi_rw_12 *cdb;
8736 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8737 if (cdb->byte2 & SRW12_FUA)
8738 flags |= CTL_LLF_FUA;
8739 if (cdb->byte2 & SRW12_DPO)
8740 flags |= CTL_LLF_DPO;
8741 lba = scsi_4btoul(cdb->addr);
8742 num_blocks = scsi_4btoul(cdb->length);
8745 case WRITE_VERIFY_12: {
8746 struct scsi_write_verify_12 *cdb;
8748 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8749 flags |= CTL_LLF_FUA;
8750 if (cdb->byte2 & SWV_DPO)
8751 flags |= CTL_LLF_DPO;
8752 lba = scsi_4btoul(cdb->addr);
8753 num_blocks = scsi_4btoul(cdb->length);
8758 struct scsi_rw_16 *cdb;
8760 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8761 if (cdb->byte2 & SRW12_FUA)
8762 flags |= CTL_LLF_FUA;
8763 if (cdb->byte2 & SRW12_DPO)
8764 flags |= CTL_LLF_DPO;
8765 lba = scsi_8btou64(cdb->addr);
8766 num_blocks = scsi_4btoul(cdb->length);
8769 case WRITE_ATOMIC_16: {
8770 struct scsi_write_atomic_16 *cdb;
8772 if (lun->be_lun->atomicblock == 0) {
8773 ctl_set_invalid_opcode(ctsio);
8774 ctl_done((union ctl_io *)ctsio);
8775 return (CTL_RETVAL_COMPLETE);
8778 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8779 if (cdb->byte2 & SRW12_FUA)
8780 flags |= CTL_LLF_FUA;
8781 if (cdb->byte2 & SRW12_DPO)
8782 flags |= CTL_LLF_DPO;
8783 lba = scsi_8btou64(cdb->addr);
8784 num_blocks = scsi_2btoul(cdb->length);
8785 if (num_blocks > lun->be_lun->atomicblock) {
8786 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8787 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8789 ctl_done((union ctl_io *)ctsio);
8790 return (CTL_RETVAL_COMPLETE);
8794 case WRITE_VERIFY_16: {
8795 struct scsi_write_verify_16 *cdb;
8797 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8798 flags |= CTL_LLF_FUA;
8799 if (cdb->byte2 & SWV_DPO)
8800 flags |= CTL_LLF_DPO;
8801 lba = scsi_8btou64(cdb->addr);
8802 num_blocks = scsi_4btoul(cdb->length);
8807 * We got a command we don't support. This shouldn't
8808 * happen, commands should be filtered out above us.
8810 ctl_set_invalid_opcode(ctsio);
8811 ctl_done((union ctl_io *)ctsio);
8813 return (CTL_RETVAL_COMPLETE);
8814 break; /* NOTREACHED */
8818 * The first check is to make sure we're in bounds, the second
8819 * check is to catch wrap-around problems. If the lba + num blocks
8820 * is less than the lba, then we've wrapped around and the block
8821 * range is invalid anyway.
8823 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8824 || ((lba + num_blocks) < lba)) {
8825 ctl_set_lba_out_of_range(ctsio);
8826 ctl_done((union ctl_io *)ctsio);
8827 return (CTL_RETVAL_COMPLETE);
8831 * According to SBC-3, a transfer length of 0 is not an error.
8832 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8833 * translates to 256 blocks for those commands.
8835 if (num_blocks == 0) {
8836 ctl_set_success(ctsio);
8837 ctl_done((union ctl_io *)ctsio);
8838 return (CTL_RETVAL_COMPLETE);
8841 /* Set FUA and/or DPO if caches are disabled. */
8843 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8845 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8847 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8849 flags |= CTL_LLF_FUA;
8852 lbalen = (struct ctl_lba_len_flags *)
8853 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8855 lbalen->len = num_blocks;
8856 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8858 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8859 ctsio->kern_rel_offset = 0;
8861 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8863 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8868 ctl_cnw_cont(union ctl_io *io)
8870 struct ctl_scsiio *ctsio;
8871 struct ctl_lun *lun;
8872 struct ctl_lba_len_flags *lbalen;
8875 ctsio = &io->scsiio;
8876 ctsio->io_hdr.status = CTL_STATUS_NONE;
8877 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8878 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8879 lbalen = (struct ctl_lba_len_flags *)
8880 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8881 lbalen->flags &= ~CTL_LLF_COMPARE;
8882 lbalen->flags |= CTL_LLF_WRITE;
8884 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8885 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8890 ctl_cnw(struct ctl_scsiio *ctsio)
8892 struct ctl_lun *lun;
8893 struct ctl_lba_len_flags *lbalen;
8895 uint32_t num_blocks;
8898 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8900 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8903 switch (ctsio->cdb[0]) {
8904 case COMPARE_AND_WRITE: {
8905 struct scsi_compare_and_write *cdb;
8907 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8908 if (cdb->byte2 & SRW10_FUA)
8909 flags |= CTL_LLF_FUA;
8910 if (cdb->byte2 & SRW10_DPO)
8911 flags |= CTL_LLF_DPO;
8912 lba = scsi_8btou64(cdb->addr);
8913 num_blocks = cdb->length;
8918 * We got a command we don't support. This shouldn't
8919 * happen, commands should be filtered out above us.
8921 ctl_set_invalid_opcode(ctsio);
8922 ctl_done((union ctl_io *)ctsio);
8924 return (CTL_RETVAL_COMPLETE);
8925 break; /* NOTREACHED */
8929 * The first check is to make sure we're in bounds, the second
8930 * check is to catch wrap-around problems. If the lba + num blocks
8931 * is less than the lba, then we've wrapped around and the block
8932 * range is invalid anyway.
8934 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8935 || ((lba + num_blocks) < lba)) {
8936 ctl_set_lba_out_of_range(ctsio);
8937 ctl_done((union ctl_io *)ctsio);
8938 return (CTL_RETVAL_COMPLETE);
8942 * According to SBC-3, a transfer length of 0 is not an error.
8944 if (num_blocks == 0) {
8945 ctl_set_success(ctsio);
8946 ctl_done((union ctl_io *)ctsio);
8947 return (CTL_RETVAL_COMPLETE);
8950 /* Set FUA if write cache is disabled. */
8951 if ((lun->mode_pages.caching_page[CTL_PAGE_CURRENT].flags1 &
8953 flags |= CTL_LLF_FUA;
8955 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8956 ctsio->kern_rel_offset = 0;
8959 * Set the IO_CONT flag, so that if this I/O gets passed to
8960 * ctl_data_submit_done(), it'll get passed back to
8961 * ctl_ctl_cnw_cont() for further processing.
8963 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8964 ctsio->io_cont = ctl_cnw_cont;
8966 lbalen = (struct ctl_lba_len_flags *)
8967 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8969 lbalen->len = num_blocks;
8970 lbalen->flags = CTL_LLF_COMPARE | flags;
8972 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8973 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8978 ctl_verify(struct ctl_scsiio *ctsio)
8980 struct ctl_lun *lun;
8981 struct ctl_lba_len_flags *lbalen;
8983 uint32_t num_blocks;
8987 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
8989 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8992 flags = CTL_LLF_FUA;
8993 switch (ctsio->cdb[0]) {
8995 struct scsi_verify_10 *cdb;
8997 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8998 if (cdb->byte2 & SVFY_BYTCHK)
9000 if (cdb->byte2 & SVFY_DPO)
9001 flags |= CTL_LLF_DPO;
9002 lba = scsi_4btoul(cdb->addr);
9003 num_blocks = scsi_2btoul(cdb->length);
9007 struct scsi_verify_12 *cdb;
9009 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9010 if (cdb->byte2 & SVFY_BYTCHK)
9012 if (cdb->byte2 & SVFY_DPO)
9013 flags |= CTL_LLF_DPO;
9014 lba = scsi_4btoul(cdb->addr);
9015 num_blocks = scsi_4btoul(cdb->length);
9019 struct scsi_rw_16 *cdb;
9021 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9022 if (cdb->byte2 & SVFY_BYTCHK)
9024 if (cdb->byte2 & SVFY_DPO)
9025 flags |= CTL_LLF_DPO;
9026 lba = scsi_8btou64(cdb->addr);
9027 num_blocks = scsi_4btoul(cdb->length);
9032 * We got a command we don't support. This shouldn't
9033 * happen, commands should be filtered out above us.
9035 ctl_set_invalid_opcode(ctsio);
9036 ctl_done((union ctl_io *)ctsio);
9037 return (CTL_RETVAL_COMPLETE);
9041 * The first check is to make sure we're in bounds, the second
9042 * check is to catch wrap-around problems. If the lba + num blocks
9043 * is less than the lba, then we've wrapped around and the block
9044 * range is invalid anyway.
9046 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9047 || ((lba + num_blocks) < lba)) {
9048 ctl_set_lba_out_of_range(ctsio);
9049 ctl_done((union ctl_io *)ctsio);
9050 return (CTL_RETVAL_COMPLETE);
9054 * According to SBC-3, a transfer length of 0 is not an error.
9056 if (num_blocks == 0) {
9057 ctl_set_success(ctsio);
9058 ctl_done((union ctl_io *)ctsio);
9059 return (CTL_RETVAL_COMPLETE);
9062 lbalen = (struct ctl_lba_len_flags *)
9063 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9065 lbalen->len = num_blocks;
9067 lbalen->flags = CTL_LLF_COMPARE | flags;
9068 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9070 lbalen->flags = CTL_LLF_VERIFY | flags;
9071 ctsio->kern_total_len = 0;
9073 ctsio->kern_rel_offset = 0;
9075 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9076 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9081 ctl_report_luns(struct ctl_scsiio *ctsio)
9083 struct ctl_softc *softc;
9084 struct scsi_report_luns *cdb;
9085 struct scsi_report_luns_data *lun_data;
9086 struct ctl_lun *lun, *request_lun;
9087 struct ctl_port *port;
9088 int num_luns, retval;
9089 uint32_t alloc_len, lun_datalen;
9091 uint32_t initidx, targ_lun_id, lun_id;
9093 retval = CTL_RETVAL_COMPLETE;
9094 cdb = (struct scsi_report_luns *)ctsio->cdb;
9095 port = ctl_io_port(&ctsio->io_hdr);
9096 softc = port->ctl_softc;
9098 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9100 mtx_lock(&softc->ctl_lock);
9102 for (targ_lun_id = 0; targ_lun_id < CTL_MAX_LUNS; targ_lun_id++) {
9103 if (ctl_lun_map_from_port(port, targ_lun_id) < CTL_MAX_LUNS)
9106 mtx_unlock(&softc->ctl_lock);
9108 switch (cdb->select_report) {
9109 case RPL_REPORT_DEFAULT:
9110 case RPL_REPORT_ALL:
9111 case RPL_REPORT_NONSUBSID:
9113 case RPL_REPORT_WELLKNOWN:
9114 case RPL_REPORT_ADMIN:
9115 case RPL_REPORT_CONGLOM:
9119 ctl_set_invalid_field(ctsio,
9125 ctl_done((union ctl_io *)ctsio);
9127 break; /* NOTREACHED */
9130 alloc_len = scsi_4btoul(cdb->length);
9132 * The initiator has to allocate at least 16 bytes for this request,
9133 * so he can at least get the header and the first LUN. Otherwise
9134 * we reject the request (per SPC-3 rev 14, section 6.21).
9136 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9137 sizeof(struct scsi_report_luns_lundata))) {
9138 ctl_set_invalid_field(ctsio,
9144 ctl_done((union ctl_io *)ctsio);
9148 request_lun = (struct ctl_lun *)
9149 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9151 lun_datalen = sizeof(*lun_data) +
9152 (num_luns * sizeof(struct scsi_report_luns_lundata));
9154 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9155 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9156 ctsio->kern_sg_entries = 0;
9158 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9160 mtx_lock(&softc->ctl_lock);
9161 for (targ_lun_id = 0, num_filled = 0; targ_lun_id < CTL_MAX_LUNS && num_filled < num_luns; targ_lun_id++) {
9162 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9163 if (lun_id >= CTL_MAX_LUNS)
9165 lun = softc->ctl_luns[lun_id];
9169 be64enc(lun_data->luns[num_filled++].lundata,
9170 ctl_encode_lun(targ_lun_id));
9173 * According to SPC-3, rev 14 section 6.21:
9175 * "The execution of a REPORT LUNS command to any valid and
9176 * installed logical unit shall clear the REPORTED LUNS DATA
9177 * HAS CHANGED unit attention condition for all logical
9178 * units of that target with respect to the requesting
9179 * initiator. A valid and installed logical unit is one
9180 * having a PERIPHERAL QUALIFIER of 000b in the standard
9181 * INQUIRY data (see 6.4.2)."
9183 * If request_lun is NULL, the LUN this report luns command
9184 * was issued to is either disabled or doesn't exist. In that
9185 * case, we shouldn't clear any pending lun change unit
9188 if (request_lun != NULL) {
9189 mtx_lock(&lun->lun_lock);
9190 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9191 mtx_unlock(&lun->lun_lock);
9194 mtx_unlock(&softc->ctl_lock);
9197 * It's quite possible that we've returned fewer LUNs than we allocated
9198 * space for. Trim it.
9200 lun_datalen = sizeof(*lun_data) +
9201 (num_filled * sizeof(struct scsi_report_luns_lundata));
9203 if (lun_datalen < alloc_len) {
9204 ctsio->residual = alloc_len - lun_datalen;
9205 ctsio->kern_data_len = lun_datalen;
9206 ctsio->kern_total_len = lun_datalen;
9208 ctsio->residual = 0;
9209 ctsio->kern_data_len = alloc_len;
9210 ctsio->kern_total_len = alloc_len;
9212 ctsio->kern_data_resid = 0;
9213 ctsio->kern_rel_offset = 0;
9214 ctsio->kern_sg_entries = 0;
9217 * We set this to the actual data length, regardless of how much
9218 * space we actually have to return results. If the user looks at
9219 * this value, he'll know whether or not he allocated enough space
9220 * and reissue the command if necessary. We don't support well
9221 * known logical units, so if the user asks for that, return none.
9223 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9226 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9229 ctl_set_success(ctsio);
9230 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9231 ctsio->be_move_done = ctl_config_move_done;
9232 ctl_datamove((union ctl_io *)ctsio);
9237 ctl_request_sense(struct ctl_scsiio *ctsio)
9239 struct scsi_request_sense *cdb;
9240 struct scsi_sense_data *sense_ptr;
9241 struct ctl_softc *ctl_softc;
9242 struct ctl_lun *lun;
9245 scsi_sense_data_type sense_format;
9246 ctl_ua_type ua_type;
9248 cdb = (struct scsi_request_sense *)ctsio->cdb;
9250 ctl_softc = control_softc;
9251 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9253 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9256 * Determine which sense format the user wants.
9258 if (cdb->byte2 & SRS_DESC)
9259 sense_format = SSD_TYPE_DESC;
9261 sense_format = SSD_TYPE_FIXED;
9263 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9264 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9265 ctsio->kern_sg_entries = 0;
9268 * struct scsi_sense_data, which is currently set to 256 bytes, is
9269 * larger than the largest allowed value for the length field in the
9270 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9272 ctsio->residual = 0;
9273 ctsio->kern_data_len = cdb->length;
9274 ctsio->kern_total_len = cdb->length;
9276 ctsio->kern_data_resid = 0;
9277 ctsio->kern_rel_offset = 0;
9278 ctsio->kern_sg_entries = 0;
9281 * If we don't have a LUN, we don't have any pending sense.
9287 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9289 * Check for pending sense, and then for pending unit attentions.
9290 * Pending sense gets returned first, then pending unit attentions.
9292 mtx_lock(&lun->lun_lock);
9294 if (ctl_is_set(lun->have_ca, initidx)) {
9295 scsi_sense_data_type stored_format;
9298 * Check to see which sense format was used for the stored
9301 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9304 * If the user requested a different sense format than the
9305 * one we stored, then we need to convert it to the other
9306 * format. If we're going from descriptor to fixed format
9307 * sense data, we may lose things in translation, depending
9308 * on what options were used.
9310 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9311 * for some reason we'll just copy it out as-is.
9313 if ((stored_format == SSD_TYPE_FIXED)
9314 && (sense_format == SSD_TYPE_DESC))
9315 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9316 &lun->pending_sense[initidx],
9317 (struct scsi_sense_data_desc *)sense_ptr);
9318 else if ((stored_format == SSD_TYPE_DESC)
9319 && (sense_format == SSD_TYPE_FIXED))
9320 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9321 &lun->pending_sense[initidx],
9322 (struct scsi_sense_data_fixed *)sense_ptr);
9324 memcpy(sense_ptr, &lun->pending_sense[initidx],
9325 MIN(sizeof(*sense_ptr),
9326 sizeof(lun->pending_sense[initidx])));
9328 ctl_clear_mask(lun->have_ca, initidx);
9333 ua_type = ctl_build_ua(lun, initidx, sense_ptr, sense_format);
9334 if (ua_type != CTL_UA_NONE)
9336 if (ua_type == CTL_UA_LUN_CHANGE) {
9337 mtx_unlock(&lun->lun_lock);
9338 mtx_lock(&ctl_softc->ctl_lock);
9339 ctl_clr_ua_allluns(ctl_softc, initidx, ua_type);
9340 mtx_unlock(&ctl_softc->ctl_lock);
9341 mtx_lock(&lun->lun_lock);
9345 mtx_unlock(&lun->lun_lock);
9348 * We already have a pending error, return it.
9350 if (have_error != 0) {
9352 * We report the SCSI status as OK, since the status of the
9353 * request sense command itself is OK.
9354 * We report 0 for the sense length, because we aren't doing
9355 * autosense in this case. We're reporting sense as
9358 ctl_set_success(ctsio);
9359 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9360 ctsio->be_move_done = ctl_config_move_done;
9361 ctl_datamove((union ctl_io *)ctsio);
9362 return (CTL_RETVAL_COMPLETE);
9368 * No sense information to report, so we report that everything is
9371 ctl_set_sense_data(sense_ptr,
9374 /*current_error*/ 1,
9375 /*sense_key*/ SSD_KEY_NO_SENSE,
9381 * We report 0 for the sense length, because we aren't doing
9382 * autosense in this case. We're reporting sense as parameter data.
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 ctl_tur(struct ctl_scsiio *ctsio)
9395 CTL_DEBUG_PRINT(("ctl_tur\n"));
9397 ctl_set_success(ctsio);
9398 ctl_done((union ctl_io *)ctsio);
9400 return (CTL_RETVAL_COMPLETE);
9404 * SCSI VPD page 0x00, the Supported VPD Pages page.
9407 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9409 struct scsi_vpd_supported_pages *pages;
9411 struct ctl_lun *lun;
9414 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9416 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9417 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9418 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9419 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9420 ctsio->kern_sg_entries = 0;
9422 if (sup_page_size < alloc_len) {
9423 ctsio->residual = alloc_len - sup_page_size;
9424 ctsio->kern_data_len = sup_page_size;
9425 ctsio->kern_total_len = sup_page_size;
9427 ctsio->residual = 0;
9428 ctsio->kern_data_len = alloc_len;
9429 ctsio->kern_total_len = alloc_len;
9431 ctsio->kern_data_resid = 0;
9432 ctsio->kern_rel_offset = 0;
9433 ctsio->kern_sg_entries = 0;
9436 * The control device is always connected. The disk device, on the
9437 * other hand, may not be online all the time. Need to change this
9438 * to figure out whether the disk device is actually online or not.
9441 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9442 lun->be_lun->lun_type;
9444 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9447 /* Supported VPD pages */
9448 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9450 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9451 /* Device Identification */
9452 pages->page_list[p++] = SVPD_DEVICE_ID;
9453 /* Extended INQUIRY Data */
9454 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9455 /* Mode Page Policy */
9456 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9458 pages->page_list[p++] = SVPD_SCSI_PORTS;
9459 /* Third-party Copy */
9460 pages->page_list[p++] = SVPD_SCSI_TPC;
9461 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9463 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9464 /* Block Device Characteristics */
9465 pages->page_list[p++] = SVPD_BDC;
9466 /* Logical Block Provisioning */
9467 pages->page_list[p++] = SVPD_LBP;
9471 ctl_set_success(ctsio);
9472 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9473 ctsio->be_move_done = ctl_config_move_done;
9474 ctl_datamove((union ctl_io *)ctsio);
9475 return (CTL_RETVAL_COMPLETE);
9479 * SCSI VPD page 0x80, the Unit Serial Number page.
9482 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9484 struct scsi_vpd_unit_serial_number *sn_ptr;
9485 struct ctl_lun *lun;
9488 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9490 data_len = 4 + CTL_SN_LEN;
9491 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9492 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9493 if (data_len < alloc_len) {
9494 ctsio->residual = alloc_len - data_len;
9495 ctsio->kern_data_len = data_len;
9496 ctsio->kern_total_len = data_len;
9498 ctsio->residual = 0;
9499 ctsio->kern_data_len = alloc_len;
9500 ctsio->kern_total_len = alloc_len;
9502 ctsio->kern_data_resid = 0;
9503 ctsio->kern_rel_offset = 0;
9504 ctsio->kern_sg_entries = 0;
9507 * The control device is always connected. The disk device, on the
9508 * other hand, may not be online all the time. Need to change this
9509 * to figure out whether the disk device is actually online or not.
9512 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9513 lun->be_lun->lun_type;
9515 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9517 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9518 sn_ptr->length = CTL_SN_LEN;
9520 * If we don't have a LUN, we just leave the serial number as
9524 strncpy((char *)sn_ptr->serial_num,
9525 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9527 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9529 ctl_set_success(ctsio);
9530 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9531 ctsio->be_move_done = ctl_config_move_done;
9532 ctl_datamove((union ctl_io *)ctsio);
9533 return (CTL_RETVAL_COMPLETE);
9538 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9541 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9543 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9544 struct ctl_lun *lun;
9547 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9549 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9550 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9551 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9552 ctsio->kern_sg_entries = 0;
9554 if (data_len < alloc_len) {
9555 ctsio->residual = alloc_len - data_len;
9556 ctsio->kern_data_len = data_len;
9557 ctsio->kern_total_len = data_len;
9559 ctsio->residual = 0;
9560 ctsio->kern_data_len = alloc_len;
9561 ctsio->kern_total_len = alloc_len;
9563 ctsio->kern_data_resid = 0;
9564 ctsio->kern_rel_offset = 0;
9565 ctsio->kern_sg_entries = 0;
9568 * The control device is always connected. The disk device, on the
9569 * other hand, may not be online all the time.
9572 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9573 lun->be_lun->lun_type;
9575 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9576 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9577 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9579 * We support head of queue, ordered and simple tags.
9581 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9583 * Volatile cache supported.
9585 eid_ptr->flags3 = SVPD_EID_V_SUP;
9588 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9589 * attention for a particular IT nexus on all LUNs once we report
9590 * it to that nexus once. This bit is required as of SPC-4.
9592 eid_ptr->flags4 = SVPD_EID_LUICLT;
9595 * XXX KDM in order to correctly answer this, we would need
9596 * information from the SIM to determine how much sense data it
9597 * can send. So this would really be a path inquiry field, most
9598 * likely. This can be set to a maximum of 252 according to SPC-4,
9599 * but the hardware may or may not be able to support that much.
9600 * 0 just means that the maximum sense data length is not reported.
9602 eid_ptr->max_sense_length = 0;
9604 ctl_set_success(ctsio);
9605 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9606 ctsio->be_move_done = ctl_config_move_done;
9607 ctl_datamove((union ctl_io *)ctsio);
9608 return (CTL_RETVAL_COMPLETE);
9612 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9614 struct scsi_vpd_mode_page_policy *mpp_ptr;
9615 struct ctl_lun *lun;
9618 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9620 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9621 sizeof(struct scsi_vpd_mode_page_policy_descr);
9623 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9624 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9625 ctsio->kern_sg_entries = 0;
9627 if (data_len < alloc_len) {
9628 ctsio->residual = alloc_len - data_len;
9629 ctsio->kern_data_len = data_len;
9630 ctsio->kern_total_len = data_len;
9632 ctsio->residual = 0;
9633 ctsio->kern_data_len = alloc_len;
9634 ctsio->kern_total_len = alloc_len;
9636 ctsio->kern_data_resid = 0;
9637 ctsio->kern_rel_offset = 0;
9638 ctsio->kern_sg_entries = 0;
9641 * The control device is always connected. The disk device, on the
9642 * other hand, may not be online all the time.
9645 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9646 lun->be_lun->lun_type;
9648 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9649 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9650 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9651 mpp_ptr->descr[0].page_code = 0x3f;
9652 mpp_ptr->descr[0].subpage_code = 0xff;
9653 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9655 ctl_set_success(ctsio);
9656 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9657 ctsio->be_move_done = ctl_config_move_done;
9658 ctl_datamove((union ctl_io *)ctsio);
9659 return (CTL_RETVAL_COMPLETE);
9663 * SCSI VPD page 0x83, the Device Identification page.
9666 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9668 struct scsi_vpd_device_id *devid_ptr;
9669 struct scsi_vpd_id_descriptor *desc;
9670 struct ctl_softc *softc;
9671 struct ctl_lun *lun;
9672 struct ctl_port *port;
9676 softc = control_softc;
9678 port = ctl_io_port(&ctsio->io_hdr);
9679 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9681 data_len = sizeof(struct scsi_vpd_device_id) +
9682 sizeof(struct scsi_vpd_id_descriptor) +
9683 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9684 sizeof(struct scsi_vpd_id_descriptor) +
9685 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9686 if (lun && lun->lun_devid)
9687 data_len += lun->lun_devid->len;
9688 if (port && port->port_devid)
9689 data_len += port->port_devid->len;
9690 if (port && port->target_devid)
9691 data_len += port->target_devid->len;
9693 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9694 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9695 ctsio->kern_sg_entries = 0;
9697 if (data_len < alloc_len) {
9698 ctsio->residual = alloc_len - data_len;
9699 ctsio->kern_data_len = data_len;
9700 ctsio->kern_total_len = data_len;
9702 ctsio->residual = 0;
9703 ctsio->kern_data_len = alloc_len;
9704 ctsio->kern_total_len = alloc_len;
9706 ctsio->kern_data_resid = 0;
9707 ctsio->kern_rel_offset = 0;
9708 ctsio->kern_sg_entries = 0;
9711 * The control device is always connected. The disk device, on the
9712 * other hand, may not be online all the time.
9715 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9716 lun->be_lun->lun_type;
9718 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9719 devid_ptr->page_code = SVPD_DEVICE_ID;
9720 scsi_ulto2b(data_len - 4, devid_ptr->length);
9722 if (port && port->port_type == CTL_PORT_FC)
9723 proto = SCSI_PROTO_FC << 4;
9724 else if (port && port->port_type == CTL_PORT_ISCSI)
9725 proto = SCSI_PROTO_ISCSI << 4;
9727 proto = SCSI_PROTO_SPI << 4;
9728 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9731 * We're using a LUN association here. i.e., this device ID is a
9732 * per-LUN identifier.
9734 if (lun && lun->lun_devid) {
9735 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9736 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9737 lun->lun_devid->len);
9741 * This is for the WWPN which is a port association.
9743 if (port && port->port_devid) {
9744 memcpy(desc, port->port_devid->data, port->port_devid->len);
9745 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9746 port->port_devid->len);
9750 * This is for the Relative Target Port(type 4h) identifier
9752 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9753 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9754 SVPD_ID_TYPE_RELTARG;
9756 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9757 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9758 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9761 * This is for the Target Port Group(type 5h) identifier
9763 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9764 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9765 SVPD_ID_TYPE_TPORTGRP;
9767 if (softc->is_single ||
9768 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9771 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9772 scsi_ulto2b(g, &desc->identifier[2]);
9773 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9774 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9777 * This is for the Target identifier
9779 if (port && port->target_devid) {
9780 memcpy(desc, port->target_devid->data, port->target_devid->len);
9783 ctl_set_success(ctsio);
9784 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9785 ctsio->be_move_done = ctl_config_move_done;
9786 ctl_datamove((union ctl_io *)ctsio);
9787 return (CTL_RETVAL_COMPLETE);
9791 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9793 struct ctl_softc *softc = control_softc;
9794 struct scsi_vpd_scsi_ports *sp;
9795 struct scsi_vpd_port_designation *pd;
9796 struct scsi_vpd_port_designation_cont *pdc;
9797 struct ctl_lun *lun;
9798 struct ctl_port *port;
9799 int data_len, num_target_ports, iid_len, id_len;
9801 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9803 num_target_ports = 0;
9806 mtx_lock(&softc->ctl_lock);
9807 STAILQ_FOREACH(port, &softc->port_list, links) {
9808 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9811 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9814 if (port->init_devid)
9815 iid_len += port->init_devid->len;
9816 if (port->port_devid)
9817 id_len += port->port_devid->len;
9819 mtx_unlock(&softc->ctl_lock);
9821 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9822 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9823 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9824 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9825 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9826 ctsio->kern_sg_entries = 0;
9828 if (data_len < alloc_len) {
9829 ctsio->residual = alloc_len - data_len;
9830 ctsio->kern_data_len = data_len;
9831 ctsio->kern_total_len = data_len;
9833 ctsio->residual = 0;
9834 ctsio->kern_data_len = alloc_len;
9835 ctsio->kern_total_len = alloc_len;
9837 ctsio->kern_data_resid = 0;
9838 ctsio->kern_rel_offset = 0;
9839 ctsio->kern_sg_entries = 0;
9842 * The control device is always connected. The disk device, on the
9843 * other hand, may not be online all the time. Need to change this
9844 * to figure out whether the disk device is actually online or not.
9847 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9848 lun->be_lun->lun_type;
9850 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9852 sp->page_code = SVPD_SCSI_PORTS;
9853 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9855 pd = &sp->design[0];
9857 mtx_lock(&softc->ctl_lock);
9858 STAILQ_FOREACH(port, &softc->port_list, links) {
9859 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9862 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
9864 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9865 if (port->init_devid) {
9866 iid_len = port->init_devid->len;
9867 memcpy(pd->initiator_transportid,
9868 port->init_devid->data, port->init_devid->len);
9871 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9872 pdc = (struct scsi_vpd_port_designation_cont *)
9873 (&pd->initiator_transportid[iid_len]);
9874 if (port->port_devid) {
9875 id_len = port->port_devid->len;
9876 memcpy(pdc->target_port_descriptors,
9877 port->port_devid->data, port->port_devid->len);
9880 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9881 pd = (struct scsi_vpd_port_designation *)
9882 ((uint8_t *)pdc->target_port_descriptors + id_len);
9884 mtx_unlock(&softc->ctl_lock);
9886 ctl_set_success(ctsio);
9887 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9888 ctsio->be_move_done = ctl_config_move_done;
9889 ctl_datamove((union ctl_io *)ctsio);
9890 return (CTL_RETVAL_COMPLETE);
9894 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9896 struct scsi_vpd_block_limits *bl_ptr;
9897 struct ctl_lun *lun;
9899 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9901 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9902 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9903 ctsio->kern_sg_entries = 0;
9905 if (sizeof(*bl_ptr) < alloc_len) {
9906 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9907 ctsio->kern_data_len = sizeof(*bl_ptr);
9908 ctsio->kern_total_len = sizeof(*bl_ptr);
9910 ctsio->residual = 0;
9911 ctsio->kern_data_len = alloc_len;
9912 ctsio->kern_total_len = alloc_len;
9914 ctsio->kern_data_resid = 0;
9915 ctsio->kern_rel_offset = 0;
9916 ctsio->kern_sg_entries = 0;
9919 * The control device is always connected. The disk device, on the
9920 * other hand, may not be online all the time. Need to change this
9921 * to figure out whether the disk device is actually online or not.
9924 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9925 lun->be_lun->lun_type;
9927 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9929 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9930 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9931 bl_ptr->max_cmp_write_len = 0xff;
9932 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9934 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9935 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9936 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_lba_cnt);
9937 scsi_ulto4b(0xffffffff, bl_ptr->max_unmap_blk_cnt);
9938 if (lun->be_lun->ublockexp != 0) {
9939 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9940 bl_ptr->opt_unmap_grain);
9941 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9942 bl_ptr->unmap_grain_align);
9945 scsi_ulto4b(lun->be_lun->atomicblock,
9946 bl_ptr->max_atomic_transfer_length);
9947 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9948 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9949 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9950 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9952 scsi_u64to8b(UINT64_MAX, bl_ptr->max_write_same_length);
9954 ctl_set_success(ctsio);
9955 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9956 ctsio->be_move_done = ctl_config_move_done;
9957 ctl_datamove((union ctl_io *)ctsio);
9958 return (CTL_RETVAL_COMPLETE);
9962 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9964 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9965 struct ctl_lun *lun;
9969 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
9971 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9972 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9973 ctsio->kern_sg_entries = 0;
9975 if (sizeof(*bdc_ptr) < alloc_len) {
9976 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9977 ctsio->kern_data_len = sizeof(*bdc_ptr);
9978 ctsio->kern_total_len = sizeof(*bdc_ptr);
9980 ctsio->residual = 0;
9981 ctsio->kern_data_len = alloc_len;
9982 ctsio->kern_total_len = alloc_len;
9984 ctsio->kern_data_resid = 0;
9985 ctsio->kern_rel_offset = 0;
9986 ctsio->kern_sg_entries = 0;
9989 * The control device is always connected. The disk device, on the
9990 * other hand, may not be online all the time. Need to change this
9991 * to figure out whether the disk device is actually online or not.
9994 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9995 lun->be_lun->lun_type;
9997 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9998 bdc_ptr->page_code = SVPD_BDC;
9999 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
10001 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
10002 i = strtol(value, NULL, 0);
10004 i = CTL_DEFAULT_ROTATION_RATE;
10005 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
10007 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
10008 i = strtol(value, NULL, 0);
10011 bdc_ptr->wab_wac_ff = (i & 0x0f);
10012 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
10014 ctl_set_success(ctsio);
10015 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10016 ctsio->be_move_done = ctl_config_move_done;
10017 ctl_datamove((union ctl_io *)ctsio);
10018 return (CTL_RETVAL_COMPLETE);
10022 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10024 struct scsi_vpd_logical_block_prov *lbp_ptr;
10025 struct ctl_lun *lun;
10027 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10029 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10030 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10031 ctsio->kern_sg_entries = 0;
10033 if (sizeof(*lbp_ptr) < alloc_len) {
10034 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10035 ctsio->kern_data_len = sizeof(*lbp_ptr);
10036 ctsio->kern_total_len = sizeof(*lbp_ptr);
10038 ctsio->residual = 0;
10039 ctsio->kern_data_len = alloc_len;
10040 ctsio->kern_total_len = alloc_len;
10042 ctsio->kern_data_resid = 0;
10043 ctsio->kern_rel_offset = 0;
10044 ctsio->kern_sg_entries = 0;
10047 * The control device is always connected. The disk device, on the
10048 * other hand, may not be online all the time. Need to change this
10049 * to figure out whether the disk device is actually online or not.
10052 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10053 lun->be_lun->lun_type;
10055 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10057 lbp_ptr->page_code = SVPD_LBP;
10058 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10059 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10060 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10061 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10062 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10063 lbp_ptr->prov_type = SVPD_LBP_THIN;
10066 ctl_set_success(ctsio);
10067 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10068 ctsio->be_move_done = ctl_config_move_done;
10069 ctl_datamove((union ctl_io *)ctsio);
10070 return (CTL_RETVAL_COMPLETE);
10074 * INQUIRY with the EVPD bit set.
10077 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10079 struct ctl_lun *lun;
10080 struct scsi_inquiry *cdb;
10081 int alloc_len, retval;
10083 lun = (struct ctl_lun *)ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10084 cdb = (struct scsi_inquiry *)ctsio->cdb;
10085 alloc_len = scsi_2btoul(cdb->length);
10087 switch (cdb->page_code) {
10088 case SVPD_SUPPORTED_PAGES:
10089 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10091 case SVPD_UNIT_SERIAL_NUMBER:
10092 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10094 case SVPD_DEVICE_ID:
10095 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10097 case SVPD_EXTENDED_INQUIRY_DATA:
10098 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10100 case SVPD_MODE_PAGE_POLICY:
10101 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10103 case SVPD_SCSI_PORTS:
10104 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10106 case SVPD_SCSI_TPC:
10107 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10109 case SVPD_BLOCK_LIMITS:
10110 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10112 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10115 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10117 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10120 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10122 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10126 ctl_set_invalid_field(ctsio,
10132 ctl_done((union ctl_io *)ctsio);
10133 retval = CTL_RETVAL_COMPLETE;
10141 * Standard INQUIRY data.
10144 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10146 struct scsi_inquiry_data *inq_ptr;
10147 struct scsi_inquiry *cdb;
10148 struct ctl_softc *softc = control_softc;
10149 struct ctl_port *port;
10150 struct ctl_lun *lun;
10152 uint32_t alloc_len, data_len;
10153 ctl_port_type port_type;
10155 port = ctl_io_port(&ctsio->io_hdr);
10156 port_type = port->port_type;
10157 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10158 port_type = CTL_PORT_SCSI;
10160 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10161 cdb = (struct scsi_inquiry *)ctsio->cdb;
10162 alloc_len = scsi_2btoul(cdb->length);
10165 * We malloc the full inquiry data size here and fill it
10166 * in. If the user only asks for less, we'll give him
10169 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10170 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10171 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10172 ctsio->kern_sg_entries = 0;
10173 ctsio->kern_data_resid = 0;
10174 ctsio->kern_rel_offset = 0;
10176 if (data_len < alloc_len) {
10177 ctsio->residual = alloc_len - data_len;
10178 ctsio->kern_data_len = data_len;
10179 ctsio->kern_total_len = data_len;
10181 ctsio->residual = 0;
10182 ctsio->kern_data_len = alloc_len;
10183 ctsio->kern_total_len = alloc_len;
10187 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10188 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10189 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10190 lun->be_lun->lun_type;
10192 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10193 lun->be_lun->lun_type;
10195 if (lun->flags & CTL_LUN_REMOVABLE)
10196 inq_ptr->dev_qual2 |= SID_RMB;
10198 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10200 /* RMB in byte 2 is 0 */
10201 inq_ptr->version = SCSI_REV_SPC4;
10204 * According to SAM-3, even if a device only supports a single
10205 * level of LUN addressing, it should still set the HISUP bit:
10207 * 4.9.1 Logical unit numbers overview
10209 * All logical unit number formats described in this standard are
10210 * hierarchical in structure even when only a single level in that
10211 * hierarchy is used. The HISUP bit shall be set to one in the
10212 * standard INQUIRY data (see SPC-2) when any logical unit number
10213 * format described in this standard is used. Non-hierarchical
10214 * formats are outside the scope of this standard.
10216 * Therefore we set the HiSup bit here.
10218 * The reponse format is 2, per SPC-3.
10220 inq_ptr->response_format = SID_HiSup | 2;
10222 inq_ptr->additional_length = data_len -
10223 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10224 CTL_DEBUG_PRINT(("additional_length = %d\n",
10225 inq_ptr->additional_length));
10227 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10228 if (port_type == CTL_PORT_SCSI)
10229 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10230 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10231 inq_ptr->flags = SID_CmdQue;
10232 if (port_type == CTL_PORT_SCSI)
10233 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10236 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10237 * We have 8 bytes for the vendor name, and 16 bytes for the device
10238 * name and 4 bytes for the revision.
10240 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10241 "vendor")) == NULL) {
10242 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10244 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10245 strncpy(inq_ptr->vendor, val,
10246 min(sizeof(inq_ptr->vendor), strlen(val)));
10249 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10250 sizeof(inq_ptr->product));
10251 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10252 switch (lun->be_lun->lun_type) {
10254 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10255 sizeof(inq_ptr->product));
10258 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10259 sizeof(inq_ptr->product));
10262 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10263 sizeof(inq_ptr->product));
10266 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10267 sizeof(inq_ptr->product));
10271 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10272 strncpy(inq_ptr->product, val,
10273 min(sizeof(inq_ptr->product), strlen(val)));
10277 * XXX make this a macro somewhere so it automatically gets
10278 * incremented when we make changes.
10280 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10281 "revision")) == NULL) {
10282 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10284 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10285 strncpy(inq_ptr->revision, val,
10286 min(sizeof(inq_ptr->revision), strlen(val)));
10290 * For parallel SCSI, we support double transition and single
10291 * transition clocking. We also support QAS (Quick Arbitration
10292 * and Selection) and Information Unit transfers on both the
10293 * control and array devices.
10295 if (port_type == CTL_PORT_SCSI)
10296 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10299 /* SAM-5 (no version claimed) */
10300 scsi_ulto2b(0x00A0, inq_ptr->version1);
10301 /* SPC-4 (no version claimed) */
10302 scsi_ulto2b(0x0460, inq_ptr->version2);
10303 if (port_type == CTL_PORT_FC) {
10304 /* FCP-2 ANSI INCITS.350:2003 */
10305 scsi_ulto2b(0x0917, inq_ptr->version3);
10306 } else if (port_type == CTL_PORT_SCSI) {
10307 /* SPI-4 ANSI INCITS.362:200x */
10308 scsi_ulto2b(0x0B56, inq_ptr->version3);
10309 } else if (port_type == CTL_PORT_ISCSI) {
10310 /* iSCSI (no version claimed) */
10311 scsi_ulto2b(0x0960, inq_ptr->version3);
10312 } else if (port_type == CTL_PORT_SAS) {
10313 /* SAS (no version claimed) */
10314 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10318 /* SBC-4 (no version claimed) */
10319 scsi_ulto2b(0x0600, inq_ptr->version4);
10321 switch (lun->be_lun->lun_type) {
10323 /* SBC-4 (no version claimed) */
10324 scsi_ulto2b(0x0600, inq_ptr->version4);
10329 /* MMC-6 (no version claimed) */
10330 scsi_ulto2b(0x04E0, inq_ptr->version4);
10337 ctl_set_success(ctsio);
10338 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10339 ctsio->be_move_done = ctl_config_move_done;
10340 ctl_datamove((union ctl_io *)ctsio);
10341 return (CTL_RETVAL_COMPLETE);
10345 ctl_inquiry(struct ctl_scsiio *ctsio)
10347 struct scsi_inquiry *cdb;
10350 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10352 cdb = (struct scsi_inquiry *)ctsio->cdb;
10353 if (cdb->byte2 & SI_EVPD)
10354 retval = ctl_inquiry_evpd(ctsio);
10355 else if (cdb->page_code == 0)
10356 retval = ctl_inquiry_std(ctsio);
10358 ctl_set_invalid_field(ctsio,
10364 ctl_done((union ctl_io *)ctsio);
10365 return (CTL_RETVAL_COMPLETE);
10372 ctl_get_config(struct ctl_scsiio *ctsio)
10374 struct scsi_get_config_header *hdr;
10375 struct scsi_get_config_feature *feature;
10376 struct scsi_get_config *cdb;
10377 struct ctl_lun *lun;
10378 uint32_t alloc_len, data_len;
10381 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10382 cdb = (struct scsi_get_config *)ctsio->cdb;
10383 rt = (cdb->rt & SGC_RT_MASK);
10384 starting = scsi_2btoul(cdb->starting_feature);
10385 alloc_len = scsi_2btoul(cdb->length);
10387 data_len = sizeof(struct scsi_get_config_header) +
10388 sizeof(struct scsi_get_config_feature) + 8 +
10389 sizeof(struct scsi_get_config_feature) + 8 +
10390 sizeof(struct scsi_get_config_feature) + 4 +
10391 sizeof(struct scsi_get_config_feature) + 4 +
10392 sizeof(struct scsi_get_config_feature) + 8 +
10393 sizeof(struct scsi_get_config_feature) +
10394 sizeof(struct scsi_get_config_feature) + 4 +
10395 sizeof(struct scsi_get_config_feature) + 4 +
10396 sizeof(struct scsi_get_config_feature) + 4 +
10397 sizeof(struct scsi_get_config_feature) + 4 +
10398 sizeof(struct scsi_get_config_feature) + 4 +
10399 sizeof(struct scsi_get_config_feature) + 4;
10400 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10401 ctsio->kern_sg_entries = 0;
10402 ctsio->kern_data_resid = 0;
10403 ctsio->kern_rel_offset = 0;
10405 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10406 if (lun->flags & CTL_LUN_NO_MEDIA)
10407 scsi_ulto2b(0x0000, hdr->current_profile);
10409 scsi_ulto2b(0x0010, hdr->current_profile);
10410 feature = (struct scsi_get_config_feature *)(hdr + 1);
10412 if (starting > 0x003b)
10414 if (starting > 0x003a)
10416 if (starting > 0x002b)
10418 if (starting > 0x002a)
10420 if (starting > 0x001f)
10422 if (starting > 0x001e)
10424 if (starting > 0x001d)
10426 if (starting > 0x0010)
10428 if (starting > 0x0003)
10430 if (starting > 0x0002)
10432 if (starting > 0x0001)
10434 if (starting > 0x0000)
10438 scsi_ulto2b(0x0000, feature->feature_code);
10439 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10440 feature->add_length = 8;
10441 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10442 feature->feature_data[2] = 0x00;
10443 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10444 feature->feature_data[6] = 0x01;
10445 feature = (struct scsi_get_config_feature *)
10446 &feature->feature_data[feature->add_length];
10449 scsi_ulto2b(0x0001, feature->feature_code);
10450 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10451 feature->add_length = 8;
10452 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10453 feature->feature_data[4] = 0x03;
10454 feature = (struct scsi_get_config_feature *)
10455 &feature->feature_data[feature->add_length];
10458 scsi_ulto2b(0x0002, feature->feature_code);
10459 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10460 feature->add_length = 4;
10461 feature->feature_data[0] = 0x02;
10462 feature = (struct scsi_get_config_feature *)
10463 &feature->feature_data[feature->add_length];
10465 f3: /* Removable Medium */
10466 scsi_ulto2b(0x0003, feature->feature_code);
10467 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10468 feature->add_length = 4;
10469 feature->feature_data[0] = 0x39;
10470 feature = (struct scsi_get_config_feature *)
10471 &feature->feature_data[feature->add_length];
10473 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10476 f10: /* Random Read */
10477 scsi_ulto2b(0x0010, feature->feature_code);
10478 feature->flags = 0x00;
10479 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10480 feature->flags |= SGC_F_CURRENT;
10481 feature->add_length = 8;
10482 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10483 scsi_ulto2b(1, &feature->feature_data[4]);
10484 feature->feature_data[6] = 0x00;
10485 feature = (struct scsi_get_config_feature *)
10486 &feature->feature_data[feature->add_length];
10488 f1d: /* Multi-Read */
10489 scsi_ulto2b(0x001D, feature->feature_code);
10490 feature->flags = 0x00;
10491 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10492 feature->flags |= SGC_F_CURRENT;
10493 feature->add_length = 0;
10494 feature = (struct scsi_get_config_feature *)
10495 &feature->feature_data[feature->add_length];
10498 scsi_ulto2b(0x001E, feature->feature_code);
10499 feature->flags = 0x00;
10500 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10501 feature->flags |= SGC_F_CURRENT;
10502 feature->add_length = 4;
10503 feature->feature_data[0] = 0x00;
10504 feature = (struct scsi_get_config_feature *)
10505 &feature->feature_data[feature->add_length];
10507 f1f: /* DVD Read */
10508 scsi_ulto2b(0x001F, feature->feature_code);
10509 feature->flags = 0x08;
10510 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10511 feature->flags |= SGC_F_CURRENT;
10512 feature->add_length = 4;
10513 feature->feature_data[0] = 0x01;
10514 feature->feature_data[2] = 0x03;
10515 feature = (struct scsi_get_config_feature *)
10516 &feature->feature_data[feature->add_length];
10519 scsi_ulto2b(0x002A, feature->feature_code);
10520 feature->flags = 0x04;
10521 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10522 feature->flags |= SGC_F_CURRENT;
10523 feature->add_length = 4;
10524 feature->feature_data[0] = 0x00;
10525 feature->feature_data[1] = 0x00;
10526 feature = (struct scsi_get_config_feature *)
10527 &feature->feature_data[feature->add_length];
10530 scsi_ulto2b(0x002B, feature->feature_code);
10531 feature->flags = 0x00;
10532 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10533 feature->flags |= SGC_F_CURRENT;
10534 feature->add_length = 4;
10535 feature->feature_data[0] = 0x00;
10536 feature = (struct scsi_get_config_feature *)
10537 &feature->feature_data[feature->add_length];
10539 f3a: /* DVD+RW Dual Layer */
10540 scsi_ulto2b(0x003A, feature->feature_code);
10541 feature->flags = 0x00;
10542 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10543 feature->flags |= SGC_F_CURRENT;
10544 feature->add_length = 4;
10545 feature->feature_data[0] = 0x00;
10546 feature->feature_data[1] = 0x00;
10547 feature = (struct scsi_get_config_feature *)
10548 &feature->feature_data[feature->add_length];
10550 f3b: /* DVD+R Dual Layer */
10551 scsi_ulto2b(0x003B, feature->feature_code);
10552 feature->flags = 0x00;
10553 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10554 feature->flags |= SGC_F_CURRENT;
10555 feature->add_length = 4;
10556 feature->feature_data[0] = 0x00;
10557 feature = (struct scsi_get_config_feature *)
10558 &feature->feature_data[feature->add_length];
10561 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10562 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10563 feature = (struct scsi_get_config_feature *)(hdr + 1);
10564 if (scsi_2btoul(feature->feature_code) == starting)
10565 feature = (struct scsi_get_config_feature *)
10566 &feature->feature_data[feature->add_length];
10567 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10569 scsi_ulto4b(data_len - 4, hdr->data_length);
10570 if (data_len < alloc_len) {
10571 ctsio->residual = alloc_len - data_len;
10572 ctsio->kern_data_len = data_len;
10573 ctsio->kern_total_len = data_len;
10575 ctsio->residual = 0;
10576 ctsio->kern_data_len = alloc_len;
10577 ctsio->kern_total_len = alloc_len;
10580 ctl_set_success(ctsio);
10581 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10582 ctsio->be_move_done = ctl_config_move_done;
10583 ctl_datamove((union ctl_io *)ctsio);
10584 return (CTL_RETVAL_COMPLETE);
10588 ctl_get_event_status(struct ctl_scsiio *ctsio)
10590 struct scsi_get_event_status_header *hdr;
10591 struct scsi_get_event_status *cdb;
10592 struct ctl_lun *lun;
10593 uint32_t alloc_len, data_len;
10596 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10597 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10598 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10599 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10600 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10601 ctl_done((union ctl_io *)ctsio);
10602 return (CTL_RETVAL_COMPLETE);
10604 notif_class = cdb->notif_class;
10605 alloc_len = scsi_2btoul(cdb->length);
10607 data_len = sizeof(struct scsi_get_event_status_header);
10608 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10609 ctsio->kern_sg_entries = 0;
10610 ctsio->kern_data_resid = 0;
10611 ctsio->kern_rel_offset = 0;
10613 if (data_len < alloc_len) {
10614 ctsio->residual = alloc_len - data_len;
10615 ctsio->kern_data_len = data_len;
10616 ctsio->kern_total_len = data_len;
10618 ctsio->residual = 0;
10619 ctsio->kern_data_len = alloc_len;
10620 ctsio->kern_total_len = alloc_len;
10623 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10624 scsi_ulto2b(0, hdr->descr_length);
10625 hdr->nea_class = SGESN_NEA;
10626 hdr->supported_class = 0;
10628 ctl_set_success(ctsio);
10629 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10630 ctsio->be_move_done = ctl_config_move_done;
10631 ctl_datamove((union ctl_io *)ctsio);
10632 return (CTL_RETVAL_COMPLETE);
10636 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10638 struct scsi_mechanism_status_header *hdr;
10639 struct scsi_mechanism_status *cdb;
10640 struct ctl_lun *lun;
10641 uint32_t alloc_len, data_len;
10643 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10644 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10645 alloc_len = scsi_2btoul(cdb->length);
10647 data_len = sizeof(struct scsi_mechanism_status_header);
10648 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10649 ctsio->kern_sg_entries = 0;
10650 ctsio->kern_data_resid = 0;
10651 ctsio->kern_rel_offset = 0;
10653 if (data_len < alloc_len) {
10654 ctsio->residual = alloc_len - data_len;
10655 ctsio->kern_data_len = data_len;
10656 ctsio->kern_total_len = data_len;
10658 ctsio->residual = 0;
10659 ctsio->kern_data_len = alloc_len;
10660 ctsio->kern_total_len = alloc_len;
10663 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10664 hdr->state1 = 0x00;
10665 hdr->state2 = 0xe0;
10666 scsi_ulto3b(0, hdr->lba);
10667 hdr->slots_num = 0;
10668 scsi_ulto2b(0, hdr->slots_length);
10670 ctl_set_success(ctsio);
10671 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10672 ctsio->be_move_done = ctl_config_move_done;
10673 ctl_datamove((union ctl_io *)ctsio);
10674 return (CTL_RETVAL_COMPLETE);
10678 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10683 buf[1] = bin2bcd((lba / 75) / 60);
10684 buf[2] = bin2bcd((lba / 75) % 60);
10685 buf[3] = bin2bcd(lba % 75);
10689 ctl_read_toc(struct ctl_scsiio *ctsio)
10691 struct scsi_read_toc_hdr *hdr;
10692 struct scsi_read_toc_type01_descr *descr;
10693 struct scsi_read_toc *cdb;
10694 struct ctl_lun *lun;
10695 uint32_t alloc_len, data_len;
10698 lun = ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
10699 cdb = (struct scsi_read_toc *)ctsio->cdb;
10700 msf = (cdb->byte2 & CD_MSF) != 0;
10701 format = cdb->format;
10702 alloc_len = scsi_2btoul(cdb->data_len);
10704 data_len = sizeof(struct scsi_read_toc_hdr);
10706 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10708 data_len += sizeof(struct scsi_read_toc_type01_descr);
10709 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10710 ctsio->kern_sg_entries = 0;
10711 ctsio->kern_data_resid = 0;
10712 ctsio->kern_rel_offset = 0;
10714 if (data_len < alloc_len) {
10715 ctsio->residual = alloc_len - data_len;
10716 ctsio->kern_data_len = data_len;
10717 ctsio->kern_total_len = data_len;
10719 ctsio->residual = 0;
10720 ctsio->kern_data_len = alloc_len;
10721 ctsio->kern_total_len = alloc_len;
10724 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10726 scsi_ulto2b(0x12, hdr->data_length);
10729 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10730 descr->addr_ctl = 0x14;
10731 descr->track_number = 1;
10733 ctl_ultomsf(0, descr->track_start);
10735 scsi_ulto4b(0, descr->track_start);
10737 descr->addr_ctl = 0x14;
10738 descr->track_number = 0xaa;
10740 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10742 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10744 scsi_ulto2b(0x0a, hdr->data_length);
10747 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10748 descr->addr_ctl = 0x14;
10749 descr->track_number = 1;
10751 ctl_ultomsf(0, descr->track_start);
10753 scsi_ulto4b(0, descr->track_start);
10756 ctl_set_success(ctsio);
10757 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10758 ctsio->be_move_done = ctl_config_move_done;
10759 ctl_datamove((union ctl_io *)ctsio);
10760 return (CTL_RETVAL_COMPLETE);
10764 * For known CDB types, parse the LBA and length.
10767 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10769 if (io->io_hdr.io_type != CTL_IO_SCSI)
10772 switch (io->scsiio.cdb[0]) {
10773 case COMPARE_AND_WRITE: {
10774 struct scsi_compare_and_write *cdb;
10776 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10778 *lba = scsi_8btou64(cdb->addr);
10779 *len = cdb->length;
10784 struct scsi_rw_6 *cdb;
10786 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10788 *lba = scsi_3btoul(cdb->addr);
10789 /* only 5 bits are valid in the most significant address byte */
10791 *len = cdb->length;
10796 struct scsi_rw_10 *cdb;
10798 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10800 *lba = scsi_4btoul(cdb->addr);
10801 *len = scsi_2btoul(cdb->length);
10804 case WRITE_VERIFY_10: {
10805 struct scsi_write_verify_10 *cdb;
10807 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10809 *lba = scsi_4btoul(cdb->addr);
10810 *len = scsi_2btoul(cdb->length);
10815 struct scsi_rw_12 *cdb;
10817 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10819 *lba = scsi_4btoul(cdb->addr);
10820 *len = scsi_4btoul(cdb->length);
10823 case WRITE_VERIFY_12: {
10824 struct scsi_write_verify_12 *cdb;
10826 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10828 *lba = scsi_4btoul(cdb->addr);
10829 *len = scsi_4btoul(cdb->length);
10834 struct scsi_rw_16 *cdb;
10836 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10838 *lba = scsi_8btou64(cdb->addr);
10839 *len = scsi_4btoul(cdb->length);
10842 case WRITE_ATOMIC_16: {
10843 struct scsi_write_atomic_16 *cdb;
10845 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10847 *lba = scsi_8btou64(cdb->addr);
10848 *len = scsi_2btoul(cdb->length);
10851 case WRITE_VERIFY_16: {
10852 struct scsi_write_verify_16 *cdb;
10854 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10856 *lba = scsi_8btou64(cdb->addr);
10857 *len = scsi_4btoul(cdb->length);
10860 case WRITE_SAME_10: {
10861 struct scsi_write_same_10 *cdb;
10863 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10865 *lba = scsi_4btoul(cdb->addr);
10866 *len = scsi_2btoul(cdb->length);
10869 case WRITE_SAME_16: {
10870 struct scsi_write_same_16 *cdb;
10872 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10874 *lba = scsi_8btou64(cdb->addr);
10875 *len = scsi_4btoul(cdb->length);
10879 struct scsi_verify_10 *cdb;
10881 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10883 *lba = scsi_4btoul(cdb->addr);
10884 *len = scsi_2btoul(cdb->length);
10888 struct scsi_verify_12 *cdb;
10890 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10892 *lba = scsi_4btoul(cdb->addr);
10893 *len = scsi_4btoul(cdb->length);
10897 struct scsi_verify_16 *cdb;
10899 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10901 *lba = scsi_8btou64(cdb->addr);
10902 *len = scsi_4btoul(cdb->length);
10910 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10911 struct scsi_get_lba_status *cdb;
10913 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10914 *lba = scsi_8btou64(cdb->addr);
10920 break; /* NOTREACHED */
10927 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10930 uint64_t endlba1, endlba2;
10932 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10933 endlba2 = lba2 + len2 - 1;
10935 if ((endlba1 < lba2) || (endlba2 < lba1))
10936 return (CTL_ACTION_PASS);
10938 return (CTL_ACTION_BLOCK);
10942 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10944 struct ctl_ptr_len_flags *ptrlen;
10945 struct scsi_unmap_desc *buf, *end, *range;
10949 /* If not UNMAP -- go other way. */
10950 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10951 io->scsiio.cdb[0] != UNMAP)
10952 return (CTL_ACTION_ERROR);
10954 /* If UNMAP without data -- block and wait for data. */
10955 ptrlen = (struct ctl_ptr_len_flags *)
10956 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10957 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10958 ptrlen->ptr == NULL)
10959 return (CTL_ACTION_BLOCK);
10961 /* UNMAP with data -- check for collision. */
10962 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10963 end = buf + ptrlen->len / sizeof(*buf);
10964 for (range = buf; range < end; range++) {
10965 lba = scsi_8btou64(range->lba);
10966 len = scsi_4btoul(range->length);
10967 if ((lba < lba2 + len2) && (lba + len > lba2))
10968 return (CTL_ACTION_BLOCK);
10970 return (CTL_ACTION_PASS);
10974 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10976 uint64_t lba1, lba2;
10977 uint64_t len1, len2;
10980 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10981 return (CTL_ACTION_ERROR);
10983 retval = ctl_extent_check_unmap(io1, lba2, len2);
10984 if (retval != CTL_ACTION_ERROR)
10987 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10988 return (CTL_ACTION_ERROR);
10990 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10992 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10996 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10998 uint64_t lba1, lba2;
10999 uint64_t len1, len2;
11001 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
11002 return (CTL_ACTION_PASS);
11003 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
11004 return (CTL_ACTION_ERROR);
11005 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
11006 return (CTL_ACTION_ERROR);
11008 if (lba1 + len1 == lba2)
11009 return (CTL_ACTION_BLOCK);
11010 return (CTL_ACTION_PASS);
11014 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
11015 union ctl_io *ooa_io)
11017 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
11018 const ctl_serialize_action *serialize_row;
11021 * The initiator attempted multiple untagged commands at the same
11022 * time. Can't do that.
11024 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11025 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11026 && ((pending_io->io_hdr.nexus.targ_port ==
11027 ooa_io->io_hdr.nexus.targ_port)
11028 && (pending_io->io_hdr.nexus.initid ==
11029 ooa_io->io_hdr.nexus.initid))
11030 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11031 CTL_FLAG_STATUS_SENT)) == 0))
11032 return (CTL_ACTION_OVERLAP);
11035 * The initiator attempted to send multiple tagged commands with
11036 * the same ID. (It's fine if different initiators have the same
11039 * Even if all of those conditions are true, we don't kill the I/O
11040 * if the command ahead of us has been aborted. We won't end up
11041 * sending it to the FETD, and it's perfectly legal to resend a
11042 * command with the same tag number as long as the previous
11043 * instance of this tag number has been aborted somehow.
11045 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11046 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11047 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11048 && ((pending_io->io_hdr.nexus.targ_port ==
11049 ooa_io->io_hdr.nexus.targ_port)
11050 && (pending_io->io_hdr.nexus.initid ==
11051 ooa_io->io_hdr.nexus.initid))
11052 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11053 CTL_FLAG_STATUS_SENT)) == 0))
11054 return (CTL_ACTION_OVERLAP_TAG);
11057 * If we get a head of queue tag, SAM-3 says that we should
11058 * immediately execute it.
11060 * What happens if this command would normally block for some other
11061 * reason? e.g. a request sense with a head of queue tag
11062 * immediately after a write. Normally that would block, but this
11063 * will result in its getting executed immediately...
11065 * We currently return "pass" instead of "skip", so we'll end up
11066 * going through the rest of the queue to check for overlapped tags.
11068 * XXX KDM check for other types of blockage first??
11070 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11071 return (CTL_ACTION_PASS);
11074 * Ordered tags have to block until all items ahead of them
11075 * have completed. If we get called with an ordered tag, we always
11076 * block, if something else is ahead of us in the queue.
11078 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11079 return (CTL_ACTION_BLOCK);
11082 * Simple tags get blocked until all head of queue and ordered tags
11083 * ahead of them have completed. I'm lumping untagged commands in
11084 * with simple tags here. XXX KDM is that the right thing to do?
11086 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11087 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11088 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11089 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11090 return (CTL_ACTION_BLOCK);
11092 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11093 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11094 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11095 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11096 pending_io->scsiio.cdb[1], pending_io));
11097 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11098 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11099 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11100 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11101 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11102 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11103 ooa_io->scsiio.cdb[1], ooa_io));
11105 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11107 switch (serialize_row[pending_entry->seridx]) {
11108 case CTL_SER_BLOCK:
11109 return (CTL_ACTION_BLOCK);
11110 case CTL_SER_EXTENT:
11111 return (ctl_extent_check(ooa_io, pending_io,
11112 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11113 case CTL_SER_EXTENTOPT:
11114 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11115 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11116 return (ctl_extent_check(ooa_io, pending_io,
11118 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11119 return (CTL_ACTION_PASS);
11120 case CTL_SER_EXTENTSEQ:
11121 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11122 return (ctl_extent_check_seq(ooa_io, pending_io));
11123 return (CTL_ACTION_PASS);
11125 return (CTL_ACTION_PASS);
11126 case CTL_SER_BLOCKOPT:
11127 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT].queue_flags
11128 & SCP_QUEUE_ALG_MASK) != SCP_QUEUE_ALG_UNRESTRICTED)
11129 return (CTL_ACTION_BLOCK);
11130 return (CTL_ACTION_PASS);
11132 return (CTL_ACTION_SKIP);
11134 panic("%s: Invalid serialization value %d for %d => %d",
11135 __func__, serialize_row[pending_entry->seridx],
11136 pending_entry->seridx, ooa_entry->seridx);
11139 return (CTL_ACTION_ERROR);
11143 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11145 * - pending_io is generally either incoming, or on the blocked queue
11146 * - starting I/O is the I/O we want to start the check with.
11149 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11150 union ctl_io *starting_io)
11152 union ctl_io *ooa_io;
11155 mtx_assert(&lun->lun_lock, MA_OWNED);
11158 * Run back along the OOA queue, starting with the current
11159 * blocked I/O and going through every I/O before it on the
11160 * queue. If starting_io is NULL, we'll just end up returning
11163 for (ooa_io = starting_io; ooa_io != NULL;
11164 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11168 * This routine just checks to see whether
11169 * cur_blocked is blocked by ooa_io, which is ahead
11170 * of it in the queue. It doesn't queue/dequeue
11173 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11175 case CTL_ACTION_BLOCK:
11176 case CTL_ACTION_OVERLAP:
11177 case CTL_ACTION_OVERLAP_TAG:
11178 case CTL_ACTION_SKIP:
11179 case CTL_ACTION_ERROR:
11181 break; /* NOTREACHED */
11182 case CTL_ACTION_PASS:
11185 panic("%s: Invalid action %d\n", __func__, action);
11189 return (CTL_ACTION_PASS);
11194 * - An I/O has just completed, and has been removed from the per-LUN OOA
11195 * queue, so some items on the blocked queue may now be unblocked.
11198 ctl_check_blocked(struct ctl_lun *lun)
11200 struct ctl_softc *softc = lun->ctl_softc;
11201 union ctl_io *cur_blocked, *next_blocked;
11203 mtx_assert(&lun->lun_lock, MA_OWNED);
11206 * Run forward from the head of the blocked queue, checking each
11207 * entry against the I/Os prior to it on the OOA queue to see if
11208 * there is still any blockage.
11210 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11211 * with our removing a variable on it while it is traversing the
11214 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11215 cur_blocked != NULL; cur_blocked = next_blocked) {
11216 union ctl_io *prev_ooa;
11219 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11222 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11223 ctl_ooaq, ooa_links);
11226 * If cur_blocked happens to be the first item in the OOA
11227 * queue now, prev_ooa will be NULL, and the action
11228 * returned will just be CTL_ACTION_PASS.
11230 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11233 case CTL_ACTION_BLOCK:
11234 /* Nothing to do here, still blocked */
11236 case CTL_ACTION_OVERLAP:
11237 case CTL_ACTION_OVERLAP_TAG:
11239 * This shouldn't happen! In theory we've already
11240 * checked this command for overlap...
11243 case CTL_ACTION_PASS:
11244 case CTL_ACTION_SKIP: {
11245 const struct ctl_cmd_entry *entry;
11248 * The skip case shouldn't happen, this transaction
11249 * should have never made it onto the blocked queue.
11252 * This I/O is no longer blocked, we can remove it
11253 * from the blocked queue. Since this is a TAILQ
11254 * (doubly linked list), we can do O(1) removals
11255 * from any place on the list.
11257 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11259 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11261 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11262 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11264 * Need to send IO back to original side to
11267 union ctl_ha_msg msg_info;
11269 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11270 msg_info.hdr.original_sc =
11271 cur_blocked->io_hdr.original_sc;
11272 msg_info.hdr.serializing_sc = cur_blocked;
11273 msg_info.hdr.msg_type = CTL_MSG_R2R;
11274 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11275 sizeof(msg_info.hdr), M_NOWAIT);
11278 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11281 * Check this I/O for LUN state changes that may
11282 * have happened while this command was blocked.
11283 * The LUN state may have been changed by a command
11284 * ahead of us in the queue, so we need to re-check
11285 * for any states that can be caused by SCSI
11288 if (ctl_scsiio_lun_check(lun, entry,
11289 &cur_blocked->scsiio) == 0) {
11290 cur_blocked->io_hdr.flags |=
11291 CTL_FLAG_IS_WAS_ON_RTR;
11292 ctl_enqueue_rtr(cur_blocked);
11294 ctl_done(cur_blocked);
11299 * This probably shouldn't happen -- we shouldn't
11300 * get CTL_ACTION_ERROR, or anything else.
11306 return (CTL_RETVAL_COMPLETE);
11310 * This routine (with one exception) checks LUN flags that can be set by
11311 * commands ahead of us in the OOA queue. These flags have to be checked
11312 * when a command initially comes in, and when we pull a command off the
11313 * blocked queue and are preparing to execute it. The reason we have to
11314 * check these flags for commands on the blocked queue is that the LUN
11315 * state may have been changed by a command ahead of us while we're on the
11318 * Ordering is somewhat important with these checks, so please pay
11319 * careful attention to the placement of any new checks.
11322 ctl_scsiio_lun_check(struct ctl_lun *lun,
11323 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11325 struct ctl_softc *softc = lun->ctl_softc;
11331 mtx_assert(&lun->lun_lock, MA_OWNED);
11334 * If this shelf is a secondary shelf controller, we may have to
11335 * reject some commands disallowed by HA mode and link state.
11337 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11338 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11339 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11340 ctl_set_lun_unavail(ctsio);
11344 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11345 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11346 ctl_set_lun_transit(ctsio);
11350 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11351 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11352 ctl_set_lun_standby(ctsio);
11357 /* The rest of checks are only done on executing side */
11358 if (softc->ha_mode == CTL_HA_MODE_XFER)
11362 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11364 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11365 ctl_set_hw_write_protected(ctsio);
11369 if ((lun->mode_pages.control_page[CTL_PAGE_CURRENT]
11370 .eca_and_aen & SCP_SWP) != 0) {
11371 ctl_set_sense(ctsio, /*current_error*/ 1,
11372 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11373 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11380 * Check for a reservation conflict. If this command isn't allowed
11381 * even on reserved LUNs, and if this initiator isn't the one who
11382 * reserved us, reject the command with a reservation conflict.
11384 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11385 if ((lun->flags & CTL_LUN_RESERVED)
11386 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11387 if (lun->res_idx != residx) {
11388 ctl_set_reservation_conflict(ctsio);
11394 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11395 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11396 /* No reservation or command is allowed. */;
11397 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11398 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11399 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11400 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11401 /* The command is allowed for Write Exclusive resv. */;
11404 * if we aren't registered or it's a res holder type
11405 * reservation and this isn't the res holder then set a
11408 if (ctl_get_prkey(lun, residx) == 0 ||
11409 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11410 ctl_set_reservation_conflict(ctsio);
11416 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11417 if (lun->flags & CTL_LUN_EJECTED)
11418 ctl_set_lun_ejected(ctsio);
11419 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11420 if (lun->flags & CTL_LUN_REMOVABLE)
11421 ctl_set_lun_no_media(ctsio);
11423 ctl_set_lun_int_reqd(ctsio);
11424 } else if (lun->flags & CTL_LUN_STOPPED)
11425 ctl_set_lun_stopped(ctsio);
11437 ctl_failover_io(union ctl_io *io, int have_lock)
11439 ctl_set_busy(&io->scsiio);
11444 ctl_failover_lun(union ctl_io *rio)
11446 struct ctl_softc *softc = control_softc;
11447 struct ctl_lun *lun;
11448 struct ctl_io_hdr *io, *next_io;
11451 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11452 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11454 /* Find and lock the LUN. */
11455 mtx_lock(&softc->ctl_lock);
11456 if ((targ_lun < CTL_MAX_LUNS) &&
11457 ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11458 mtx_lock(&lun->lun_lock);
11459 mtx_unlock(&softc->ctl_lock);
11460 if (lun->flags & CTL_LUN_DISABLED) {
11461 mtx_unlock(&lun->lun_lock);
11465 mtx_unlock(&softc->ctl_lock);
11469 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11470 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11471 /* We are master */
11472 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11473 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11474 io->flags |= CTL_FLAG_ABORT;
11475 io->flags |= CTL_FLAG_FAILOVER;
11476 } else { /* This can be only due to DATAMOVE */
11477 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11478 io->flags &= ~CTL_FLAG_DMA_INPROG;
11479 io->flags |= CTL_FLAG_IO_ACTIVE;
11480 io->port_status = 31340;
11481 ctl_enqueue_isc((union ctl_io *)io);
11485 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11486 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11487 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11488 io->flags |= CTL_FLAG_FAILOVER;
11490 ctl_set_busy(&((union ctl_io *)io)->
11492 ctl_done((union ctl_io *)io);
11496 } else { /* SERIALIZE modes */
11497 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11499 /* We are master */
11500 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11501 TAILQ_REMOVE(&lun->blocked_queue, io,
11503 io->flags &= ~CTL_FLAG_BLOCKED;
11504 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11505 ctl_free_io((union ctl_io *)io);
11508 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11509 /* We are master */
11510 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11511 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11512 ctl_free_io((union ctl_io *)io);
11515 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11516 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11517 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11518 ctl_set_busy(&((union ctl_io *)io)->
11520 ctl_done((union ctl_io *)io);
11524 ctl_check_blocked(lun);
11526 mtx_unlock(&lun->lun_lock);
11530 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11532 struct ctl_lun *lun;
11533 const struct ctl_cmd_entry *entry;
11534 uint32_t initidx, targ_lun;
11541 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11542 if ((targ_lun < CTL_MAX_LUNS)
11543 && ((lun = softc->ctl_luns[targ_lun]) != NULL)) {
11545 * If the LUN is invalid, pretend that it doesn't exist.
11546 * It will go away as soon as all pending I/O has been
11549 mtx_lock(&lun->lun_lock);
11550 if (lun->flags & CTL_LUN_DISABLED) {
11551 mtx_unlock(&lun->lun_lock);
11553 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11554 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11556 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = lun;
11557 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr =
11561 * Every I/O goes into the OOA queue for a
11562 * particular LUN, and stays there until completion.
11565 if (TAILQ_EMPTY(&lun->ooa_queue)) {
11566 lun->idle_time += getsbinuptime() -
11570 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr,
11574 ctsio->io_hdr.ctl_private[CTL_PRIV_LUN].ptr = NULL;
11575 ctsio->io_hdr.ctl_private[CTL_PRIV_BACKEND_LUN].ptr = NULL;
11578 /* Get command entry and return error if it is unsuppotyed. */
11579 entry = ctl_validate_command(ctsio);
11580 if (entry == NULL) {
11582 mtx_unlock(&lun->lun_lock);
11586 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11587 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11590 * Check to see whether we can send this command to LUNs that don't
11591 * exist. This should pretty much only be the case for inquiry
11592 * and request sense. Further checks, below, really require having
11593 * a LUN, so we can't really check the command anymore. Just put
11594 * it on the rtr queue.
11597 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11598 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11599 ctl_enqueue_rtr((union ctl_io *)ctsio);
11603 ctl_set_unsupported_lun(ctsio);
11604 ctl_done((union ctl_io *)ctsio);
11605 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11609 * Make sure we support this particular command on this LUN.
11610 * e.g., we don't support writes to the control LUN.
11612 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11613 mtx_unlock(&lun->lun_lock);
11614 ctl_set_invalid_opcode(ctsio);
11615 ctl_done((union ctl_io *)ctsio);
11620 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11624 * If we've got a request sense, it'll clear the contingent
11625 * allegiance condition. Otherwise, if we have a CA condition for
11626 * this initiator, clear it, because it sent down a command other
11627 * than request sense.
11629 if ((ctsio->cdb[0] != REQUEST_SENSE)
11630 && (ctl_is_set(lun->have_ca, initidx)))
11631 ctl_clear_mask(lun->have_ca, initidx);
11635 * If the command has this flag set, it handles its own unit
11636 * attention reporting, we shouldn't do anything. Otherwise we
11637 * check for any pending unit attentions, and send them back to the
11638 * initiator. We only do this when a command initially comes in,
11639 * not when we pull it off the blocked queue.
11641 * According to SAM-3, section 5.3.2, the order that things get
11642 * presented back to the host is basically unit attentions caused
11643 * by some sort of reset event, busy status, reservation conflicts
11644 * or task set full, and finally any other status.
11646 * One issue here is that some of the unit attentions we report
11647 * don't fall into the "reset" category (e.g. "reported luns data
11648 * has changed"). So reporting it here, before the reservation
11649 * check, may be technically wrong. I guess the only thing to do
11650 * would be to check for and report the reset events here, and then
11651 * check for the other unit attention types after we check for a
11652 * reservation conflict.
11654 * XXX KDM need to fix this
11656 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11657 ctl_ua_type ua_type;
11659 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11661 if (ua_type != CTL_UA_NONE) {
11662 mtx_unlock(&lun->lun_lock);
11663 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11664 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11665 ctsio->sense_len = SSD_FULL_SIZE;
11666 ctl_done((union ctl_io *)ctsio);
11672 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11673 mtx_unlock(&lun->lun_lock);
11674 ctl_done((union ctl_io *)ctsio);
11679 * XXX CHD this is where we want to send IO to other side if
11680 * this LUN is secondary on this SC. We will need to make a copy
11681 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11682 * the copy we send as FROM_OTHER.
11683 * We also need to stuff the address of the original IO so we can
11684 * find it easily. Something similar will need be done on the other
11685 * side so when we are done we can find the copy.
11687 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11688 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11689 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11690 union ctl_ha_msg msg_info;
11693 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11694 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11695 mtx_unlock(&lun->lun_lock);
11697 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11698 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11699 msg_info.hdr.serializing_sc = NULL;
11700 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11701 msg_info.scsi.tag_num = ctsio->tag_num;
11702 msg_info.scsi.tag_type = ctsio->tag_type;
11703 msg_info.scsi.cdb_len = ctsio->cdb_len;
11704 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11706 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11707 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11708 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11709 ctl_set_busy(ctsio);
11710 ctl_done((union ctl_io *)ctsio);
11716 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11717 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11718 ctl_ooaq, ooa_links))) {
11719 case CTL_ACTION_BLOCK:
11720 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11721 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11723 mtx_unlock(&lun->lun_lock);
11725 case CTL_ACTION_PASS:
11726 case CTL_ACTION_SKIP:
11727 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11728 mtx_unlock(&lun->lun_lock);
11729 ctl_enqueue_rtr((union ctl_io *)ctsio);
11731 case CTL_ACTION_OVERLAP:
11732 mtx_unlock(&lun->lun_lock);
11733 ctl_set_overlapped_cmd(ctsio);
11734 ctl_done((union ctl_io *)ctsio);
11736 case CTL_ACTION_OVERLAP_TAG:
11737 mtx_unlock(&lun->lun_lock);
11738 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11739 ctl_done((union ctl_io *)ctsio);
11741 case CTL_ACTION_ERROR:
11743 mtx_unlock(&lun->lun_lock);
11744 ctl_set_internal_failure(ctsio,
11746 /*retry_count*/ 0);
11747 ctl_done((union ctl_io *)ctsio);
11753 const struct ctl_cmd_entry *
11754 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11756 const struct ctl_cmd_entry *entry;
11757 int service_action;
11759 entry = &ctl_cmd_table[ctsio->cdb[0]];
11761 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11762 if (entry->flags & CTL_CMD_FLAG_SA5) {
11763 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11764 entry = &((const struct ctl_cmd_entry *)
11765 entry->execute)[service_action];
11770 const struct ctl_cmd_entry *
11771 ctl_validate_command(struct ctl_scsiio *ctsio)
11773 const struct ctl_cmd_entry *entry;
11777 entry = ctl_get_cmd_entry(ctsio, &sa);
11778 if (entry->execute == NULL) {
11780 ctl_set_invalid_field(ctsio,
11787 ctl_set_invalid_opcode(ctsio);
11788 ctl_done((union ctl_io *)ctsio);
11791 KASSERT(entry->length > 0,
11792 ("Not defined length for command 0x%02x/0x%02x",
11793 ctsio->cdb[0], ctsio->cdb[1]));
11794 for (i = 1; i < entry->length; i++) {
11795 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11798 ctl_set_invalid_field(ctsio,
11803 /*bit*/ fls(diff) - 1);
11804 ctl_done((union ctl_io *)ctsio);
11811 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11814 switch (lun_type) {
11816 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11820 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11824 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11834 ctl_scsiio(struct ctl_scsiio *ctsio)
11837 const struct ctl_cmd_entry *entry;
11839 retval = CTL_RETVAL_COMPLETE;
11841 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11843 entry = ctl_get_cmd_entry(ctsio, NULL);
11846 * If this I/O has been aborted, just send it straight to
11847 * ctl_done() without executing it.
11849 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11850 ctl_done((union ctl_io *)ctsio);
11855 * All the checks should have been handled by ctl_scsiio_precheck().
11856 * We should be clear now to just execute the I/O.
11858 retval = entry->execute(ctsio);
11865 * Since we only implement one target right now, a bus reset simply resets
11866 * our single target.
11869 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11871 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11875 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11876 ctl_ua_type ua_type)
11878 struct ctl_port *port;
11879 struct ctl_lun *lun;
11882 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11883 union ctl_ha_msg msg_info;
11885 msg_info.hdr.nexus = io->io_hdr.nexus;
11886 if (ua_type==CTL_UA_TARG_RESET)
11887 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11889 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11890 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11891 msg_info.hdr.original_sc = NULL;
11892 msg_info.hdr.serializing_sc = NULL;
11893 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11894 sizeof(msg_info.task), M_WAITOK);
11898 mtx_lock(&softc->ctl_lock);
11899 port = ctl_io_port(&io->io_hdr);
11900 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11901 if (port != NULL &&
11902 ctl_lun_map_to_port(port, lun->lun) >= CTL_MAX_LUNS)
11904 retval += ctl_do_lun_reset(lun, io, ua_type);
11906 mtx_unlock(&softc->ctl_lock);
11907 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11912 * The LUN should always be set. The I/O is optional, and is used to
11913 * distinguish between I/Os sent by this initiator, and by other
11914 * initiators. We set unit attention for initiators other than this one.
11915 * SAM-3 is vague on this point. It does say that a unit attention should
11916 * be established for other initiators when a LUN is reset (see section
11917 * 5.7.3), but it doesn't specifically say that the unit attention should
11918 * be established for this particular initiator when a LUN is reset. Here
11919 * is the relevant text, from SAM-3 rev 8:
11921 * 5.7.2 When a SCSI initiator port aborts its own tasks
11923 * When a SCSI initiator port causes its own task(s) to be aborted, no
11924 * notification that the task(s) have been aborted shall be returned to
11925 * the SCSI initiator port other than the completion response for the
11926 * command or task management function action that caused the task(s) to
11927 * be aborted and notification(s) associated with related effects of the
11928 * action (e.g., a reset unit attention condition).
11930 * XXX KDM for now, we're setting unit attention for all initiators.
11933 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11941 mtx_lock(&lun->lun_lock);
11943 * Run through the OOA queue and abort each I/O.
11945 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11946 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11947 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11951 * This version sets unit attention for every
11954 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11955 ctl_est_ua_all(lun, initidx, ua_type);
11957 ctl_est_ua_all(lun, -1, ua_type);
11961 * A reset (any kind, really) clears reservations established with
11962 * RESERVE/RELEASE. It does not clear reservations established
11963 * with PERSISTENT RESERVE OUT, but we don't support that at the
11964 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11965 * reservations made with the RESERVE/RELEASE commands, because
11966 * those commands are obsolete in SPC-3.
11968 lun->flags &= ~CTL_LUN_RESERVED;
11971 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11972 ctl_clear_mask(lun->have_ca, i);
11974 lun->prevent_count = 0;
11975 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11976 ctl_clear_mask(lun->prevent, i);
11977 mtx_unlock(&lun->lun_lock);
11983 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11985 struct ctl_lun *lun;
11989 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11990 mtx_lock(&softc->ctl_lock);
11991 if ((targ_lun >= CTL_MAX_LUNS) ||
11992 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11993 mtx_unlock(&softc->ctl_lock);
11994 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11997 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11998 mtx_unlock(&softc->ctl_lock);
11999 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12001 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
12002 union ctl_ha_msg msg_info;
12004 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12005 msg_info.hdr.nexus = io->io_hdr.nexus;
12006 msg_info.task.task_action = CTL_TASK_LUN_RESET;
12007 msg_info.hdr.original_sc = NULL;
12008 msg_info.hdr.serializing_sc = NULL;
12009 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12010 sizeof(msg_info.task), M_WAITOK);
12016 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
12021 mtx_assert(&lun->lun_lock, MA_OWNED);
12024 * Run through the OOA queue and attempt to find the given I/O.
12025 * The target port, initiator ID, tag type and tag number have to
12026 * match the values that we got from the initiator. If we have an
12027 * untagged command to abort, simply abort the first untagged command
12028 * we come to. We only allow one untagged command at a time of course.
12030 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12031 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12033 if ((targ_port == UINT32_MAX ||
12034 targ_port == xio->io_hdr.nexus.targ_port) &&
12035 (init_id == UINT32_MAX ||
12036 init_id == xio->io_hdr.nexus.initid)) {
12037 if (targ_port != xio->io_hdr.nexus.targ_port ||
12038 init_id != xio->io_hdr.nexus.initid)
12039 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12040 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12041 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12042 union ctl_ha_msg msg_info;
12044 msg_info.hdr.nexus = xio->io_hdr.nexus;
12045 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12046 msg_info.task.tag_num = xio->scsiio.tag_num;
12047 msg_info.task.tag_type = xio->scsiio.tag_type;
12048 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12049 msg_info.hdr.original_sc = NULL;
12050 msg_info.hdr.serializing_sc = NULL;
12051 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12052 sizeof(msg_info.task), M_NOWAIT);
12059 ctl_abort_task_set(union ctl_io *io)
12061 struct ctl_softc *softc = control_softc;
12062 struct ctl_lun *lun;
12068 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12069 mtx_lock(&softc->ctl_lock);
12070 if ((targ_lun >= CTL_MAX_LUNS) ||
12071 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12072 mtx_unlock(&softc->ctl_lock);
12073 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12077 mtx_lock(&lun->lun_lock);
12078 mtx_unlock(&softc->ctl_lock);
12079 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12080 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12081 io->io_hdr.nexus.initid,
12082 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12083 } else { /* CTL_TASK_CLEAR_TASK_SET */
12084 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12085 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12087 mtx_unlock(&lun->lun_lock);
12088 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12093 ctl_i_t_nexus_reset(union ctl_io *io)
12095 struct ctl_softc *softc = control_softc;
12096 struct ctl_lun *lun;
12099 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12100 union ctl_ha_msg msg_info;
12102 msg_info.hdr.nexus = io->io_hdr.nexus;
12103 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12104 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12105 msg_info.hdr.original_sc = NULL;
12106 msg_info.hdr.serializing_sc = NULL;
12107 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12108 sizeof(msg_info.task), M_WAITOK);
12111 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12112 mtx_lock(&softc->ctl_lock);
12113 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12114 mtx_lock(&lun->lun_lock);
12115 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12116 io->io_hdr.nexus.initid, 1);
12118 ctl_clear_mask(lun->have_ca, initidx);
12120 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12121 lun->flags &= ~CTL_LUN_RESERVED;
12122 if (ctl_is_set(lun->prevent, initidx)) {
12123 ctl_clear_mask(lun->prevent, initidx);
12124 lun->prevent_count--;
12126 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12127 mtx_unlock(&lun->lun_lock);
12129 mtx_unlock(&softc->ctl_lock);
12130 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12135 ctl_abort_task(union ctl_io *io)
12138 struct ctl_lun *lun;
12139 struct ctl_softc *softc;
12142 char printbuf[128];
12147 softc = control_softc;
12153 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12154 mtx_lock(&softc->ctl_lock);
12155 if ((targ_lun >= CTL_MAX_LUNS) ||
12156 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12157 mtx_unlock(&softc->ctl_lock);
12158 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12163 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12164 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12167 mtx_lock(&lun->lun_lock);
12168 mtx_unlock(&softc->ctl_lock);
12170 * Run through the OOA queue and attempt to find the given I/O.
12171 * The target port, initiator ID, tag type and tag number have to
12172 * match the values that we got from the initiator. If we have an
12173 * untagged command to abort, simply abort the first untagged command
12174 * we come to. We only allow one untagged command at a time of course.
12176 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12177 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12179 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12181 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12182 lun->lun, xio->scsiio.tag_num,
12183 xio->scsiio.tag_type,
12184 (xio->io_hdr.blocked_links.tqe_prev
12185 == NULL) ? "" : " BLOCKED",
12186 (xio->io_hdr.flags &
12187 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12188 (xio->io_hdr.flags &
12189 CTL_FLAG_ABORT) ? " ABORT" : "",
12190 (xio->io_hdr.flags &
12191 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12192 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12194 printf("%s\n", sbuf_data(&sb));
12197 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12198 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12199 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12203 * If the abort says that the task is untagged, the
12204 * task in the queue must be untagged. Otherwise,
12205 * we just check to see whether the tag numbers
12206 * match. This is because the QLogic firmware
12207 * doesn't pass back the tag type in an abort
12211 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12212 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12213 || (xio->scsiio.tag_num == io->taskio.tag_num))
12216 * XXX KDM we've got problems with FC, because it
12217 * doesn't send down a tag type with aborts. So we
12218 * can only really go by the tag number...
12219 * This may cause problems with parallel SCSI.
12220 * Need to figure that out!!
12222 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12223 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12225 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12226 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12227 union ctl_ha_msg msg_info;
12229 msg_info.hdr.nexus = io->io_hdr.nexus;
12230 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12231 msg_info.task.tag_num = io->taskio.tag_num;
12232 msg_info.task.tag_type = io->taskio.tag_type;
12233 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12234 msg_info.hdr.original_sc = NULL;
12235 msg_info.hdr.serializing_sc = NULL;
12237 printf("Sent Abort to other side\n");
12239 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12240 sizeof(msg_info.task), M_NOWAIT);
12243 printf("ctl_abort_task: found I/O to abort\n");
12247 mtx_unlock(&lun->lun_lock);
12251 * This isn't really an error. It's entirely possible for
12252 * the abort and command completion to cross on the wire.
12253 * This is more of an informative/diagnostic error.
12256 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12257 "%u:%u:%u tag %d type %d\n",
12258 io->io_hdr.nexus.initid,
12259 io->io_hdr.nexus.targ_port,
12260 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12261 io->taskio.tag_type);
12264 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12269 ctl_query_task(union ctl_io *io, int task_set)
12272 struct ctl_lun *lun;
12273 struct ctl_softc *softc;
12277 softc = control_softc;
12278 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12279 mtx_lock(&softc->ctl_lock);
12280 if ((targ_lun >= CTL_MAX_LUNS) ||
12281 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12282 mtx_unlock(&softc->ctl_lock);
12283 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12286 mtx_lock(&lun->lun_lock);
12287 mtx_unlock(&softc->ctl_lock);
12288 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12289 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12291 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12292 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12293 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12296 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12301 mtx_unlock(&lun->lun_lock);
12303 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12305 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12310 ctl_query_async_event(union ctl_io *io)
12312 struct ctl_lun *lun;
12313 struct ctl_softc *softc;
12315 uint32_t targ_lun, initidx;
12317 softc = control_softc;
12318 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12319 mtx_lock(&softc->ctl_lock);
12320 if ((targ_lun >= CTL_MAX_LUNS) ||
12321 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12322 mtx_unlock(&softc->ctl_lock);
12323 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12326 mtx_lock(&lun->lun_lock);
12327 mtx_unlock(&softc->ctl_lock);
12328 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12329 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12330 mtx_unlock(&lun->lun_lock);
12331 if (ua != CTL_UA_NONE)
12332 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12334 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12339 ctl_run_task(union ctl_io *io)
12341 struct ctl_softc *softc = control_softc;
12344 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12345 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12346 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12347 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12348 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12349 switch (io->taskio.task_action) {
12350 case CTL_TASK_ABORT_TASK:
12351 retval = ctl_abort_task(io);
12353 case CTL_TASK_ABORT_TASK_SET:
12354 case CTL_TASK_CLEAR_TASK_SET:
12355 retval = ctl_abort_task_set(io);
12357 case CTL_TASK_CLEAR_ACA:
12359 case CTL_TASK_I_T_NEXUS_RESET:
12360 retval = ctl_i_t_nexus_reset(io);
12362 case CTL_TASK_LUN_RESET:
12363 retval = ctl_lun_reset(softc, io);
12365 case CTL_TASK_TARGET_RESET:
12366 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12368 case CTL_TASK_BUS_RESET:
12369 retval = ctl_bus_reset(softc, io);
12371 case CTL_TASK_PORT_LOGIN:
12373 case CTL_TASK_PORT_LOGOUT:
12375 case CTL_TASK_QUERY_TASK:
12376 retval = ctl_query_task(io, 0);
12378 case CTL_TASK_QUERY_TASK_SET:
12379 retval = ctl_query_task(io, 1);
12381 case CTL_TASK_QUERY_ASYNC_EVENT:
12382 retval = ctl_query_async_event(io);
12385 printf("%s: got unknown task management event %d\n",
12386 __func__, io->taskio.task_action);
12390 io->io_hdr.status = CTL_SUCCESS;
12392 io->io_hdr.status = CTL_ERROR;
12397 * For HA operation. Handle commands that come in from the other
12401 ctl_handle_isc(union ctl_io *io)
12404 struct ctl_lun *lun;
12405 struct ctl_softc *softc = control_softc;
12408 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12409 lun = softc->ctl_luns[targ_lun];
12411 switch (io->io_hdr.msg_type) {
12412 case CTL_MSG_SERIALIZE:
12413 free_io = ctl_serialize_other_sc_cmd(&io->scsiio);
12415 case CTL_MSG_R2R: {
12416 const struct ctl_cmd_entry *entry;
12419 * This is only used in SER_ONLY mode.
12422 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12423 mtx_lock(&lun->lun_lock);
12424 if (ctl_scsiio_lun_check(lun,
12425 entry, (struct ctl_scsiio *)io) != 0) {
12426 mtx_unlock(&lun->lun_lock);
12430 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12431 mtx_unlock(&lun->lun_lock);
12432 ctl_enqueue_rtr(io);
12435 case CTL_MSG_FINISH_IO:
12436 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12441 mtx_lock(&lun->lun_lock);
12442 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr,
12444 ctl_check_blocked(lun);
12445 mtx_unlock(&lun->lun_lock);
12448 case CTL_MSG_PERS_ACTION:
12449 ctl_hndl_per_res_out_on_other_sc(
12450 (union ctl_ha_msg *)&io->presio.pr_msg);
12453 case CTL_MSG_BAD_JUJU:
12457 case CTL_MSG_DATAMOVE:
12458 /* Only used in XFER mode */
12460 ctl_datamove_remote(io);
12462 case CTL_MSG_DATAMOVE_DONE:
12463 /* Only used in XFER mode */
12465 io->scsiio.be_move_done(io);
12467 case CTL_MSG_FAILOVER:
12468 ctl_failover_lun(io);
12473 printf("%s: Invalid message type %d\n",
12474 __func__, io->io_hdr.msg_type);
12484 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12485 * there is no match.
12487 static ctl_lun_error_pattern
12488 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12490 const struct ctl_cmd_entry *entry;
12491 ctl_lun_error_pattern filtered_pattern, pattern;
12493 pattern = desc->error_pattern;
12496 * XXX KDM we need more data passed into this function to match a
12497 * custom pattern, and we actually need to implement custom pattern
12500 if (pattern & CTL_LUN_PAT_CMD)
12501 return (CTL_LUN_PAT_CMD);
12503 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12504 return (CTL_LUN_PAT_ANY);
12506 entry = ctl_get_cmd_entry(ctsio, NULL);
12508 filtered_pattern = entry->pattern & pattern;
12511 * If the user requested specific flags in the pattern (e.g.
12512 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12515 * If the user did not specify any flags, it doesn't matter whether
12516 * or not the command supports the flags.
12518 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12519 (pattern & ~CTL_LUN_PAT_MASK))
12520 return (CTL_LUN_PAT_NONE);
12523 * If the user asked for a range check, see if the requested LBA
12524 * range overlaps with this command's LBA range.
12526 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12532 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12534 return (CTL_LUN_PAT_NONE);
12536 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12537 desc->lba_range.len, FALSE);
12539 * A "pass" means that the LBA ranges don't overlap, so
12540 * this doesn't match the user's range criteria.
12542 if (action == CTL_ACTION_PASS)
12543 return (CTL_LUN_PAT_NONE);
12546 return (filtered_pattern);
12550 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12552 struct ctl_error_desc *desc, *desc2;
12554 mtx_assert(&lun->lun_lock, MA_OWNED);
12556 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12557 ctl_lun_error_pattern pattern;
12559 * Check to see whether this particular command matches
12560 * the pattern in the descriptor.
12562 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12563 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12566 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12567 case CTL_LUN_INJ_ABORTED:
12568 ctl_set_aborted(&io->scsiio);
12570 case CTL_LUN_INJ_MEDIUM_ERR:
12571 ctl_set_medium_error(&io->scsiio,
12572 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12573 CTL_FLAG_DATA_OUT);
12575 case CTL_LUN_INJ_UA:
12576 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12578 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12580 case CTL_LUN_INJ_CUSTOM:
12582 * We're assuming the user knows what he is doing.
12583 * Just copy the sense information without doing
12586 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12587 MIN(sizeof(desc->custom_sense),
12588 sizeof(io->scsiio.sense_data)));
12589 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12590 io->scsiio.sense_len = SSD_FULL_SIZE;
12591 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12593 case CTL_LUN_INJ_NONE:
12596 * If this is an error injection type we don't know
12597 * about, clear the continuous flag (if it is set)
12598 * so it will get deleted below.
12600 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12604 * By default, each error injection action is a one-shot
12606 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12609 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12615 #ifdef CTL_IO_DELAY
12617 ctl_datamove_timer_wakeup(void *arg)
12621 io = (union ctl_io *)arg;
12625 #endif /* CTL_IO_DELAY */
12628 ctl_datamove(union ctl_io *io)
12630 struct ctl_lun *lun;
12631 void (*fe_datamove)(union ctl_io *io);
12633 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
12635 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12637 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
12639 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12644 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12645 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12647 sbuf_cat(&sb, path_str);
12648 switch (io->io_hdr.io_type) {
12650 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12651 sbuf_printf(&sb, "\n");
12652 sbuf_cat(&sb, path_str);
12653 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12654 io->scsiio.tag_num, io->scsiio.tag_type);
12657 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12658 "Tag Type: %d\n", io->taskio.task_action,
12659 io->taskio.tag_num, io->taskio.tag_type);
12662 panic("%s: Invalid CTL I/O type %d\n",
12663 __func__, io->io_hdr.io_type);
12665 sbuf_cat(&sb, path_str);
12666 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12667 (intmax_t)time_uptime - io->io_hdr.start_time);
12669 printf("%s", sbuf_data(&sb));
12671 #endif /* CTL_TIME_IO */
12673 #ifdef CTL_IO_DELAY
12674 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12675 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12678 && (lun->delay_info.datamove_delay > 0)) {
12680 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12681 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12682 callout_reset(&io->io_hdr.delay_callout,
12683 lun->delay_info.datamove_delay * hz,
12684 ctl_datamove_timer_wakeup, io);
12685 if (lun->delay_info.datamove_type ==
12686 CTL_DELAY_TYPE_ONESHOT)
12687 lun->delay_info.datamove_delay = 0;
12694 * This command has been aborted. Set the port status, so we fail
12697 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12698 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12699 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12700 io->io_hdr.nexus.targ_port,
12701 io->io_hdr.nexus.targ_lun);
12702 io->io_hdr.port_status = 31337;
12704 * Note that the backend, in this case, will get the
12705 * callback in its context. In other cases it may get
12706 * called in the frontend's interrupt thread context.
12708 io->scsiio.be_move_done(io);
12712 /* Don't confuse frontend with zero length data move. */
12713 if (io->scsiio.kern_data_len == 0) {
12714 io->scsiio.be_move_done(io);
12718 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12723 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12725 union ctl_ha_msg msg;
12727 struct bintime cur_bt;
12730 memset(&msg, 0, sizeof(msg));
12731 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12732 msg.hdr.original_sc = io;
12733 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12734 msg.hdr.nexus = io->io_hdr.nexus;
12735 msg.hdr.status = io->io_hdr.status;
12736 msg.scsi.tag_num = io->scsiio.tag_num;
12737 msg.scsi.tag_type = io->scsiio.tag_type;
12738 msg.scsi.scsi_status = io->scsiio.scsi_status;
12739 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12740 io->scsiio.sense_len);
12741 msg.scsi.sense_len = io->scsiio.sense_len;
12742 msg.scsi.sense_residual = io->scsiio.sense_residual;
12743 msg.scsi.fetd_status = io->io_hdr.port_status;
12744 msg.scsi.residual = io->scsiio.residual;
12745 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12746 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12747 ctl_failover_io(io, /*have_lock*/ have_lock);
12750 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12751 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12752 msg.scsi.sense_len, M_WAITOK);
12755 getbinuptime(&cur_bt);
12756 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12757 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12759 io->io_hdr.num_dmas++;
12763 * The DMA to the remote side is done, now we need to tell the other side
12764 * we're done so it can continue with its data movement.
12767 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12774 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12775 printf("%s: ISC DMA write failed with error %d", __func__,
12777 ctl_set_internal_failure(&io->scsiio,
12779 /*retry_count*/ rq->ret);
12782 ctl_dt_req_free(rq);
12784 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12785 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12786 free(io->io_hdr.remote_sglist, M_CTL);
12787 io->io_hdr.remote_sglist = NULL;
12788 io->io_hdr.local_sglist = NULL;
12791 * The data is in local and remote memory, so now we need to send
12792 * status (good or back) back to the other side.
12794 ctl_send_datamove_done(io, /*have_lock*/ 0);
12798 * We've moved the data from the host/controller into local memory. Now we
12799 * need to push it over to the remote controller's memory.
12802 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12806 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12807 ctl_datamove_remote_write_cb);
12812 ctl_datamove_remote_write(union ctl_io *io)
12815 void (*fe_datamove)(union ctl_io *io);
12818 * - Get the data from the host/HBA into local memory.
12819 * - DMA memory from the local controller to the remote controller.
12820 * - Send status back to the remote controller.
12823 retval = ctl_datamove_remote_sgl_setup(io);
12827 /* Switch the pointer over so the FETD knows what to do */
12828 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12831 * Use a custom move done callback, since we need to send completion
12832 * back to the other controller, not to the backend on this side.
12834 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12836 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12841 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12850 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12851 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12852 free(io->io_hdr.remote_sglist, M_CTL);
12853 io->io_hdr.remote_sglist = NULL;
12854 io->io_hdr.local_sglist = NULL;
12857 scsi_path_string(io, path_str, sizeof(path_str));
12858 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12859 sbuf_cat(&sb, path_str);
12860 scsi_command_string(&io->scsiio, NULL, &sb);
12861 sbuf_printf(&sb, "\n");
12862 sbuf_cat(&sb, path_str);
12863 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12864 io->scsiio.tag_num, io->scsiio.tag_type);
12865 sbuf_cat(&sb, path_str);
12866 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12867 io->io_hdr.flags, io->io_hdr.status);
12869 printk("%s", sbuf_data(&sb));
12874 * The read is done, now we need to send status (good or bad) back
12875 * to the other side.
12877 ctl_send_datamove_done(io, /*have_lock*/ 0);
12883 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12886 void (*fe_datamove)(union ctl_io *io);
12890 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12891 printf("%s: ISC DMA read failed with error %d\n", __func__,
12893 ctl_set_internal_failure(&io->scsiio,
12895 /*retry_count*/ rq->ret);
12898 ctl_dt_req_free(rq);
12900 /* Switch the pointer over so the FETD knows what to do */
12901 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12904 * Use a custom move done callback, since we need to send completion
12905 * back to the other controller, not to the backend on this side.
12907 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12909 /* XXX KDM add checks like the ones in ctl_datamove? */
12911 fe_datamove = ctl_io_port(&io->io_hdr)->fe_datamove;
12916 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12918 struct ctl_sg_entry *local_sglist;
12919 uint32_t len_to_go;
12924 local_sglist = io->io_hdr.local_sglist;
12925 len_to_go = io->scsiio.kern_data_len;
12928 * The difficult thing here is that the size of the various
12929 * S/G segments may be different than the size from the
12930 * remote controller. That'll make it harder when DMAing
12931 * the data back to the other side.
12933 for (i = 0; len_to_go > 0; i++) {
12934 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12935 local_sglist[i].addr =
12936 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12938 len_to_go -= local_sglist[i].len;
12941 * Reset the number of S/G entries accordingly. The original
12942 * number of S/G entries is available in rem_sg_entries.
12944 io->scsiio.kern_sg_entries = i;
12947 printf("%s: kern_sg_entries = %d\n", __func__,
12948 io->scsiio.kern_sg_entries);
12949 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12950 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12951 local_sglist[i].addr, local_sglist[i].len);
12958 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12959 ctl_ha_dt_cb callback)
12961 struct ctl_ha_dt_req *rq;
12962 struct ctl_sg_entry *remote_sglist, *local_sglist;
12963 uint32_t local_used, remote_used, total_used;
12966 rq = ctl_dt_req_alloc();
12969 * If we failed to allocate the request, and if the DMA didn't fail
12970 * anyway, set busy status. This is just a resource allocation
12974 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12975 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12976 ctl_set_busy(&io->scsiio);
12978 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12979 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12982 ctl_dt_req_free(rq);
12985 * The data move failed. We need to return status back
12986 * to the other controller. No point in trying to DMA
12987 * data to the remote controller.
12990 ctl_send_datamove_done(io, /*have_lock*/ 0);
12995 local_sglist = io->io_hdr.local_sglist;
12996 remote_sglist = io->io_hdr.remote_sglist;
13002 * Pull/push the data over the wire from/to the other controller.
13003 * This takes into account the possibility that the local and
13004 * remote sglists may not be identical in terms of the size of
13005 * the elements and the number of elements.
13007 * One fundamental assumption here is that the length allocated for
13008 * both the local and remote sglists is identical. Otherwise, we've
13009 * essentially got a coding error of some sort.
13011 isc_ret = CTL_HA_STATUS_SUCCESS;
13012 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
13016 rq->command = command;
13020 * Both pointers should be aligned. But it is possible
13021 * that the allocation length is not. They should both
13022 * also have enough slack left over at the end, though,
13023 * to round up to the next 8 byte boundary.
13025 cur_len = MIN(local_sglist[i].len - local_used,
13026 remote_sglist[j].len - remote_used);
13027 rq->size = cur_len;
13029 tmp_ptr = (uint8_t *)local_sglist[i].addr;
13030 tmp_ptr += local_used;
13033 /* Use physical addresses when talking to ISC hardware */
13034 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
13035 /* XXX KDM use busdma */
13036 rq->local = vtophys(tmp_ptr);
13038 rq->local = tmp_ptr;
13040 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
13041 ("HA does not support BUS_ADDR"));
13042 rq->local = tmp_ptr;
13045 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
13046 tmp_ptr += remote_used;
13047 rq->remote = tmp_ptr;
13049 rq->callback = NULL;
13051 local_used += cur_len;
13052 if (local_used >= local_sglist[i].len) {
13057 remote_used += cur_len;
13058 if (remote_used >= remote_sglist[j].len) {
13062 total_used += cur_len;
13064 if (total_used >= io->scsiio.kern_data_len)
13065 rq->callback = callback;
13068 printf("%s: %s: local %p remote %p size %d\n", __func__,
13069 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13070 rq->local, rq->remote, rq->size);
13073 isc_ret = ctl_dt_single(rq);
13074 if (isc_ret > CTL_HA_STATUS_SUCCESS)
13077 if (isc_ret != CTL_HA_STATUS_WAIT) {
13086 ctl_datamove_remote_read(union ctl_io *io)
13092 * This will send an error to the other controller in the case of a
13095 retval = ctl_datamove_remote_sgl_setup(io);
13099 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13100 ctl_datamove_remote_read_cb);
13103 * Make sure we free memory if there was an error.. The
13104 * ctl_datamove_remote_xfer() function will send the
13105 * datamove done message, or call the callback with an
13106 * error if there is a problem.
13108 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13109 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13110 free(io->io_hdr.remote_sglist, M_CTL);
13111 io->io_hdr.remote_sglist = NULL;
13112 io->io_hdr.local_sglist = NULL;
13117 * Process a datamove request from the other controller. This is used for
13118 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13119 * first. Once that is complete, the data gets DMAed into the remote
13120 * controller's memory. For reads, we DMA from the remote controller's
13121 * memory into our memory first, and then move it out to the FETD.
13124 ctl_datamove_remote(union ctl_io *io)
13127 mtx_assert(&control_softc->ctl_lock, MA_NOTOWNED);
13129 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13130 ctl_failover_io(io, /*have_lock*/ 0);
13135 * Note that we look for an aborted I/O here, but don't do some of
13136 * the other checks that ctl_datamove() normally does.
13137 * We don't need to run the datamove delay code, since that should
13138 * have been done if need be on the other controller.
13140 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13141 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13142 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13143 io->io_hdr.nexus.targ_port,
13144 io->io_hdr.nexus.targ_lun);
13145 io->io_hdr.port_status = 31338;
13146 ctl_send_datamove_done(io, /*have_lock*/ 0);
13150 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13151 ctl_datamove_remote_write(io);
13152 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13153 ctl_datamove_remote_read(io);
13155 io->io_hdr.port_status = 31339;
13156 ctl_send_datamove_done(io, /*have_lock*/ 0);
13161 ctl_process_done(union ctl_io *io)
13163 struct ctl_lun *lun;
13164 struct ctl_softc *softc = control_softc;
13165 void (*fe_done)(union ctl_io *io);
13166 union ctl_ha_msg msg;
13167 uint32_t targ_port = io->io_hdr.nexus.targ_port;
13169 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13170 fe_done = softc->ctl_ports[targ_port]->fe_done;
13173 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13178 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13179 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13181 sbuf_cat(&sb, path_str);
13182 switch (io->io_hdr.io_type) {
13184 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13185 sbuf_printf(&sb, "\n");
13186 sbuf_cat(&sb, path_str);
13187 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13188 io->scsiio.tag_num, io->scsiio.tag_type);
13191 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13192 "Tag Type: %d\n", io->taskio.task_action,
13193 io->taskio.tag_num, io->taskio.tag_type);
13196 panic("%s: Invalid CTL I/O type %d\n",
13197 __func__, io->io_hdr.io_type);
13199 sbuf_cat(&sb, path_str);
13200 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13201 (intmax_t)time_uptime - io->io_hdr.start_time);
13203 printf("%s", sbuf_data(&sb));
13205 #endif /* CTL_TIME_IO */
13207 switch (io->io_hdr.io_type) {
13211 if (ctl_debug & CTL_DEBUG_INFO)
13212 ctl_io_error_print(io, NULL);
13216 panic("%s: Invalid CTL I/O type %d\n",
13217 __func__, io->io_hdr.io_type);
13220 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13222 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13223 io->io_hdr.nexus.targ_mapped_lun));
13227 mtx_lock(&lun->lun_lock);
13230 * Check to see if we have any errors to inject here. We only
13231 * inject errors for commands that don't already have errors set.
13233 if (!STAILQ_EMPTY(&lun->error_list) &&
13234 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13235 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13236 ctl_inject_error(lun, io);
13239 * XXX KDM how do we treat commands that aren't completed
13242 * XXX KDM should we also track I/O latency?
13244 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13245 io->io_hdr.io_type == CTL_IO_SCSI) {
13247 struct bintime cur_bt;
13251 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13253 type = CTL_STATS_READ;
13254 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13256 type = CTL_STATS_WRITE;
13258 type = CTL_STATS_NO_IO;
13260 lun->stats.ports[targ_port].bytes[type] +=
13261 io->scsiio.kern_total_len;
13262 lun->stats.ports[targ_port].operations[type]++;
13264 bintime_add(&lun->stats.ports[targ_port].dma_time[type],
13265 &io->io_hdr.dma_bt);
13266 getbinuptime(&cur_bt);
13267 bintime_sub(&cur_bt, &io->io_hdr.start_bt);
13268 bintime_add(&lun->stats.ports[targ_port].time[type], &cur_bt);
13270 lun->stats.ports[targ_port].num_dmas[type] +=
13271 io->io_hdr.num_dmas;
13275 * Remove this from the OOA queue.
13277 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13279 if (TAILQ_EMPTY(&lun->ooa_queue))
13280 lun->last_busy = getsbinuptime();
13284 * Run through the blocked queue on this LUN and see if anything
13285 * has become unblocked, now that this transaction is done.
13287 ctl_check_blocked(lun);
13290 * If the LUN has been invalidated, free it if there is nothing
13291 * left on its OOA queue.
13293 if ((lun->flags & CTL_LUN_INVALID)
13294 && TAILQ_EMPTY(&lun->ooa_queue)) {
13295 mtx_unlock(&lun->lun_lock);
13296 mtx_lock(&softc->ctl_lock);
13298 mtx_unlock(&softc->ctl_lock);
13300 mtx_unlock(&lun->lun_lock);
13305 * If this command has been aborted, make sure we set the status
13306 * properly. The FETD is responsible for freeing the I/O and doing
13307 * whatever it needs to do to clean up its state.
13309 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13310 ctl_set_task_aborted(&io->scsiio);
13313 * If enabled, print command error status.
13315 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13316 (ctl_debug & CTL_DEBUG_INFO) != 0)
13317 ctl_io_error_print(io, NULL);
13320 * Tell the FETD or the other shelf controller we're done with this
13321 * command. Note that only SCSI commands get to this point. Task
13322 * management commands are completed above.
13324 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13325 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13326 memset(&msg, 0, sizeof(msg));
13327 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13328 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13329 msg.hdr.nexus = io->io_hdr.nexus;
13330 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13331 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13340 * Front end should call this if it doesn't do autosense. When the request
13341 * sense comes back in from the initiator, we'll dequeue this and send it.
13344 ctl_queue_sense(union ctl_io *io)
13346 struct ctl_lun *lun;
13347 struct ctl_port *port;
13348 struct ctl_softc *softc;
13349 uint32_t initidx, targ_lun;
13351 softc = control_softc;
13353 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13356 * LUN lookup will likely move to the ctl_work_thread() once we
13357 * have our new queueing infrastructure (that doesn't put things on
13358 * a per-LUN queue initially). That is so that we can handle
13359 * things like an INQUIRY to a LUN that we don't have enabled. We
13360 * can't deal with that right now.
13362 mtx_lock(&softc->ctl_lock);
13365 * If we don't have a LUN for this, just toss the sense
13368 port = ctl_io_port(&ctsio->io_hdr);
13369 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13370 if ((targ_lun < CTL_MAX_LUNS)
13371 && (softc->ctl_luns[targ_lun] != NULL))
13372 lun = softc->ctl_luns[targ_lun];
13376 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13378 mtx_lock(&lun->lun_lock);
13380 * Already have CA set for this LUN...toss the sense information.
13382 if (ctl_is_set(lun->have_ca, initidx)) {
13383 mtx_unlock(&lun->lun_lock);
13387 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13388 MIN(sizeof(lun->pending_sense[initidx]),
13389 sizeof(io->scsiio.sense_data)));
13390 ctl_set_mask(lun->have_ca, initidx);
13391 mtx_unlock(&lun->lun_lock);
13394 mtx_unlock(&softc->ctl_lock);
13398 return (CTL_RETVAL_COMPLETE);
13403 * Primary command inlet from frontend ports. All SCSI and task I/O
13404 * requests must go through this function.
13407 ctl_queue(union ctl_io *io)
13409 struct ctl_port *port;
13411 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13414 io->io_hdr.start_time = time_uptime;
13415 getbinuptime(&io->io_hdr.start_bt);
13416 #endif /* CTL_TIME_IO */
13418 /* Map FE-specific LUN ID into global one. */
13419 port = ctl_io_port(&io->io_hdr);
13420 io->io_hdr.nexus.targ_mapped_lun =
13421 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13423 switch (io->io_hdr.io_type) {
13426 if (ctl_debug & CTL_DEBUG_CDB)
13428 ctl_enqueue_incoming(io);
13431 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13435 return (CTL_RETVAL_COMPLETE);
13438 #ifdef CTL_IO_DELAY
13440 ctl_done_timer_wakeup(void *arg)
13444 io = (union ctl_io *)arg;
13447 #endif /* CTL_IO_DELAY */
13450 ctl_serseq_done(union ctl_io *io)
13452 struct ctl_lun *lun;
13454 lun = (struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13455 if (lun->be_lun == NULL ||
13456 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13458 mtx_lock(&lun->lun_lock);
13459 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13460 ctl_check_blocked(lun);
13461 mtx_unlock(&lun->lun_lock);
13465 ctl_done(union ctl_io *io)
13469 * Enable this to catch duplicate completion issues.
13472 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13473 printf("%s: type %d msg %d cdb %x iptl: "
13474 "%u:%u:%u tag 0x%04x "
13475 "flag %#x status %x\n",
13477 io->io_hdr.io_type,
13478 io->io_hdr.msg_type,
13480 io->io_hdr.nexus.initid,
13481 io->io_hdr.nexus.targ_port,
13482 io->io_hdr.nexus.targ_lun,
13483 (io->io_hdr.io_type ==
13485 io->taskio.tag_num :
13486 io->scsiio.tag_num,
13488 io->io_hdr.status);
13490 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13494 * This is an internal copy of an I/O, and should not go through
13495 * the normal done processing logic.
13497 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13500 #ifdef CTL_IO_DELAY
13501 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13502 struct ctl_lun *lun;
13504 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13506 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13508 struct ctl_lun *lun;
13510 lun =(struct ctl_lun *)io->io_hdr.ctl_private[CTL_PRIV_LUN].ptr;
13513 && (lun->delay_info.done_delay > 0)) {
13515 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13516 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13517 callout_reset(&io->io_hdr.delay_callout,
13518 lun->delay_info.done_delay * hz,
13519 ctl_done_timer_wakeup, io);
13520 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13521 lun->delay_info.done_delay = 0;
13525 #endif /* CTL_IO_DELAY */
13527 ctl_enqueue_done(io);
13531 ctl_work_thread(void *arg)
13533 struct ctl_thread *thr = (struct ctl_thread *)arg;
13534 struct ctl_softc *softc = thr->ctl_softc;
13538 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13542 * We handle the queues in this order:
13544 * - done queue (to free up resources, unblock other commands)
13548 * If those queues are empty, we break out of the loop and
13551 mtx_lock(&thr->queue_lock);
13552 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13554 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13555 mtx_unlock(&thr->queue_lock);
13556 ctl_handle_isc(io);
13559 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13561 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13562 /* clear any blocked commands, call fe_done */
13563 mtx_unlock(&thr->queue_lock);
13564 ctl_process_done(io);
13567 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13569 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13570 mtx_unlock(&thr->queue_lock);
13571 if (io->io_hdr.io_type == CTL_IO_TASK)
13574 ctl_scsiio_precheck(softc, &io->scsiio);
13577 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13579 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13580 mtx_unlock(&thr->queue_lock);
13581 retval = ctl_scsiio(&io->scsiio);
13582 if (retval != CTL_RETVAL_COMPLETE)
13583 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13587 /* Sleep until we have something to do. */
13588 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13593 ctl_lun_thread(void *arg)
13595 struct ctl_softc *softc = (struct ctl_softc *)arg;
13596 struct ctl_be_lun *be_lun;
13598 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13601 mtx_lock(&softc->ctl_lock);
13602 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13603 if (be_lun != NULL) {
13604 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13605 mtx_unlock(&softc->ctl_lock);
13606 ctl_create_lun(be_lun);
13610 /* Sleep until we have something to do. */
13611 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13612 PDROP | PRIBIO, "-", 0);
13617 ctl_thresh_thread(void *arg)
13619 struct ctl_softc *softc = (struct ctl_softc *)arg;
13620 struct ctl_lun *lun;
13621 struct scsi_da_rw_recovery_page *rwpage;
13622 struct ctl_logical_block_provisioning_page *page;
13624 union ctl_ha_msg msg;
13625 uint64_t thres, val;
13628 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13631 mtx_lock(&softc->ctl_lock);
13632 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13633 if ((lun->flags & CTL_LUN_DISABLED) ||
13634 (lun->flags & CTL_LUN_NO_MEDIA) ||
13635 lun->backend->lun_attr == NULL)
13637 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13638 softc->ha_mode == CTL_HA_MODE_XFER)
13640 rwpage = &lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT];
13641 if ((rwpage->byte8 & SMS_RWER_LBPERE) == 0)
13644 page = &lun->mode_pages.lbp_page[CTL_PAGE_CURRENT];
13645 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13646 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13648 thres = scsi_4btoul(page->descr[i].count);
13649 thres <<= CTL_LBP_EXPONENT;
13650 switch (page->descr[i].resource) {
13652 attr = "blocksavail";
13655 attr = "blocksused";
13658 attr = "poolblocksavail";
13661 attr = "poolblocksused";
13666 mtx_unlock(&softc->ctl_lock); // XXX
13667 val = lun->backend->lun_attr(
13668 lun->be_lun->be_lun, attr);
13669 mtx_lock(&softc->ctl_lock);
13670 if (val == UINT64_MAX)
13672 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13673 == SLBPPD_ARMING_INC)
13674 e = (val >= thres);
13676 e = (val <= thres);
13680 mtx_lock(&lun->lun_lock);
13682 scsi_u64to8b((uint8_t *)&page->descr[i] -
13683 (uint8_t *)page, lun->ua_tpt_info);
13684 if (lun->lasttpt == 0 ||
13685 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13686 lun->lasttpt = time_uptime;
13687 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13693 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13696 mtx_unlock(&lun->lun_lock);
13698 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13699 /* Send msg to other side. */
13700 bzero(&msg.ua, sizeof(msg.ua));
13701 msg.hdr.msg_type = CTL_MSG_UA;
13702 msg.hdr.nexus.initid = -1;
13703 msg.hdr.nexus.targ_port = -1;
13704 msg.hdr.nexus.targ_lun = lun->lun;
13705 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13707 msg.ua.ua_set = (set > 0);
13708 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13709 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13710 mtx_unlock(&softc->ctl_lock); // XXX
13711 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13712 sizeof(msg.ua), M_WAITOK);
13713 mtx_lock(&softc->ctl_lock);
13716 mtx_unlock(&softc->ctl_lock);
13717 pause("-", CTL_LBP_PERIOD * hz);
13722 ctl_enqueue_incoming(union ctl_io *io)
13724 struct ctl_softc *softc = control_softc;
13725 struct ctl_thread *thr;
13728 idx = (io->io_hdr.nexus.targ_port * 127 +
13729 io->io_hdr.nexus.initid) % worker_threads;
13730 thr = &softc->threads[idx];
13731 mtx_lock(&thr->queue_lock);
13732 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13733 mtx_unlock(&thr->queue_lock);
13738 ctl_enqueue_rtr(union ctl_io *io)
13740 struct ctl_softc *softc = control_softc;
13741 struct ctl_thread *thr;
13743 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13744 mtx_lock(&thr->queue_lock);
13745 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13746 mtx_unlock(&thr->queue_lock);
13751 ctl_enqueue_done(union ctl_io *io)
13753 struct ctl_softc *softc = control_softc;
13754 struct ctl_thread *thr;
13756 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13757 mtx_lock(&thr->queue_lock);
13758 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13759 mtx_unlock(&thr->queue_lock);
13764 ctl_enqueue_isc(union ctl_io *io)
13766 struct ctl_softc *softc = control_softc;
13767 struct ctl_thread *thr;
13769 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13770 mtx_lock(&thr->queue_lock);
13771 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13772 mtx_unlock(&thr->queue_lock);