2 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
7 * Portions of this software were developed by Edward Tomasz Napierala
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
17 * substantially similar to the "NO WARRANTY" disclaimer below
18 * ("Disclaimer") and any redistribution must be conditioned upon
19 * including a substantially similar Disclaimer requirement for further
20 * binary redistribution.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGES.
38 * CAM Target Layer, a SCSI device emulation subsystem.
40 * Author: Ken Merry <ken@FreeBSD.org>
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/ctype.h>
51 #include <sys/kernel.h>
52 #include <sys/types.h>
53 #include <sys/kthread.h>
55 #include <sys/fcntl.h>
57 #include <sys/module.h>
58 #include <sys/mutex.h>
59 #include <sys/condvar.h>
60 #include <sys/malloc.h>
62 #include <sys/ioccom.h>
63 #include <sys/queue.h>
66 #include <sys/endian.h>
67 #include <sys/sysctl.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_cd.h>
73 #include <cam/scsi/scsi_da.h>
74 #include <cam/ctl/ctl_io.h>
75 #include <cam/ctl/ctl.h>
76 #include <cam/ctl/ctl_frontend.h>
77 #include <cam/ctl/ctl_util.h>
78 #include <cam/ctl/ctl_backend.h>
79 #include <cam/ctl/ctl_ioctl.h>
80 #include <cam/ctl/ctl_ha.h>
81 #include <cam/ctl/ctl_private.h>
82 #include <cam/ctl/ctl_debug.h>
83 #include <cam/ctl/ctl_scsi_all.h>
84 #include <cam/ctl/ctl_error.h>
86 struct ctl_softc *control_softc = NULL;
89 * Template mode pages.
93 * Note that these are default values only. The actual values will be
94 * filled in when the user does a mode sense.
96 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
97 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
98 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
99 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
100 /*read_retry_count*/0,
101 /*correction_span*/0,
102 /*head_offset_count*/0,
103 /*data_strobe_offset_cnt*/0,
104 /*byte8*/SMS_RWER_LBPERE,
105 /*write_retry_count*/0,
107 /*recovery_time_limit*/{0, 0},
110 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
111 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
112 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
113 /*byte3*/SMS_RWER_PER,
114 /*read_retry_count*/0,
115 /*correction_span*/0,
116 /*head_offset_count*/0,
117 /*data_strobe_offset_cnt*/0,
118 /*byte8*/SMS_RWER_LBPERE,
119 /*write_retry_count*/0,
121 /*recovery_time_limit*/{0, 0},
124 const static struct scsi_format_page format_page_default = {
125 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
126 /*page_length*/sizeof(struct scsi_format_page) - 2,
127 /*tracks_per_zone*/ {0, 0},
128 /*alt_sectors_per_zone*/ {0, 0},
129 /*alt_tracks_per_zone*/ {0, 0},
130 /*alt_tracks_per_lun*/ {0, 0},
131 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
132 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
133 /*bytes_per_sector*/ {0, 0},
134 /*interleave*/ {0, 0},
135 /*track_skew*/ {0, 0},
136 /*cylinder_skew*/ {0, 0},
138 /*reserved*/ {0, 0, 0}
141 const static struct scsi_format_page format_page_changeable = {
142 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
143 /*page_length*/sizeof(struct scsi_format_page) - 2,
144 /*tracks_per_zone*/ {0, 0},
145 /*alt_sectors_per_zone*/ {0, 0},
146 /*alt_tracks_per_zone*/ {0, 0},
147 /*alt_tracks_per_lun*/ {0, 0},
148 /*sectors_per_track*/ {0, 0},
149 /*bytes_per_sector*/ {0, 0},
150 /*interleave*/ {0, 0},
151 /*track_skew*/ {0, 0},
152 /*cylinder_skew*/ {0, 0},
154 /*reserved*/ {0, 0, 0}
157 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
158 /*page_code*/SMS_RIGID_DISK_PAGE,
159 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
160 /*cylinders*/ {0, 0, 0},
161 /*heads*/ CTL_DEFAULT_HEADS,
162 /*start_write_precomp*/ {0, 0, 0},
163 /*start_reduced_current*/ {0, 0, 0},
164 /*step_rate*/ {0, 0},
165 /*landing_zone_cylinder*/ {0, 0, 0},
166 /*rpl*/ SRDP_RPL_DISABLED,
167 /*rotational_offset*/ 0,
169 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
170 CTL_DEFAULT_ROTATION_RATE & 0xff},
174 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
175 /*page_code*/SMS_RIGID_DISK_PAGE,
176 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
177 /*cylinders*/ {0, 0, 0},
179 /*start_write_precomp*/ {0, 0, 0},
180 /*start_reduced_current*/ {0, 0, 0},
181 /*step_rate*/ {0, 0},
182 /*landing_zone_cylinder*/ {0, 0, 0},
184 /*rotational_offset*/ 0,
186 /*rotation_rate*/ {0, 0},
190 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
191 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
192 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
194 /*read_retry_count*/0,
195 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
196 /*recovery_time_limit*/{0, 0},
199 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
200 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
201 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
202 /*byte3*/SMS_VER_PER,
203 /*read_retry_count*/0,
204 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
205 /*recovery_time_limit*/{0, 0},
208 const static struct scsi_caching_page caching_page_default = {
209 /*page_code*/SMS_CACHING_PAGE,
210 /*page_length*/sizeof(struct scsi_caching_page) - 2,
211 /*flags1*/ SCP_DISC | SCP_WCE,
213 /*disable_pf_transfer_len*/ {0xff, 0xff},
214 /*min_prefetch*/ {0, 0},
215 /*max_prefetch*/ {0xff, 0xff},
216 /*max_pf_ceiling*/ {0xff, 0xff},
218 /*cache_segments*/ 0,
219 /*cache_seg_size*/ {0, 0},
221 /*non_cache_seg_size*/ {0, 0, 0}
224 const static struct scsi_caching_page caching_page_changeable = {
225 /*page_code*/SMS_CACHING_PAGE,
226 /*page_length*/sizeof(struct scsi_caching_page) - 2,
227 /*flags1*/ SCP_WCE | SCP_RCD,
229 /*disable_pf_transfer_len*/ {0, 0},
230 /*min_prefetch*/ {0, 0},
231 /*max_prefetch*/ {0, 0},
232 /*max_pf_ceiling*/ {0, 0},
234 /*cache_segments*/ 0,
235 /*cache_seg_size*/ {0, 0},
237 /*non_cache_seg_size*/ {0, 0, 0}
240 const static struct scsi_control_page control_page_default = {
241 /*page_code*/SMS_CONTROL_MODE_PAGE,
242 /*page_length*/sizeof(struct scsi_control_page) - 2,
244 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
247 /*aen_holdoff_period*/{0, 0},
248 /*busy_timeout_period*/{0, 0},
249 /*extended_selftest_completion_time*/{0, 0}
252 const static struct scsi_control_page control_page_changeable = {
253 /*page_code*/SMS_CONTROL_MODE_PAGE,
254 /*page_length*/sizeof(struct scsi_control_page) - 2,
256 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
257 /*eca_and_aen*/SCP_SWP,
259 /*aen_holdoff_period*/{0, 0},
260 /*busy_timeout_period*/{0, 0},
261 /*extended_selftest_completion_time*/{0, 0}
264 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
266 const static struct scsi_control_ext_page control_ext_page_default = {
267 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
268 /*subpage_code*/0x01,
269 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
275 const static struct scsi_control_ext_page control_ext_page_changeable = {
276 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
277 /*subpage_code*/0x01,
278 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
284 const static struct scsi_info_exceptions_page ie_page_default = {
285 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
286 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
287 /*info_flags*/SIEP_FLAGS_EWASC,
288 /*mrie*/SIEP_MRIE_NO,
289 /*interval_timer*/{0, 0, 0, 0},
290 /*report_count*/{0, 0, 0, 1}
293 const static struct scsi_info_exceptions_page ie_page_changeable = {
294 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
295 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
296 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
299 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
300 /*report_count*/{0xff, 0xff, 0xff, 0xff}
303 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
305 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
306 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
307 /*subpage_code*/0x02,
308 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
310 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
315 /*count*/{0, 0, 0, 0}},
319 /*count*/{0, 0, 0, 0}},
323 /*count*/{0, 0, 0, 0}},
327 /*count*/{0, 0, 0, 0}}
331 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
332 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
333 /*subpage_code*/0x02,
334 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
335 /*flags*/SLBPP_SITUA,
336 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
341 /*count*/{0, 0, 0, 0}},
345 /*count*/{0, 0, 0, 0}},
349 /*count*/{0, 0, 0, 0}},
353 /*count*/{0, 0, 0, 0}}
357 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
358 /*page_code*/SMS_CDDVD_CAPS_PAGE,
359 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
367 /*nvol_levels*/{0, 0},
368 /*buffer_size*/{8, 0},
373 /*copy_management*/0,
375 /*rotation_control*/0,
376 /*cur_write_speed*/0,
377 /*num_speed_descr*/0,
380 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
381 /*page_code*/SMS_CDDVD_CAPS_PAGE,
382 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
390 /*nvol_levels*/{0, 0},
391 /*buffer_size*/{0, 0},
396 /*copy_management*/0,
398 /*rotation_control*/0,
399 /*cur_write_speed*/0,
400 /*num_speed_descr*/0,
403 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
404 static int worker_threads = -1;
405 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
406 &worker_threads, 1, "Number of worker threads");
407 static int ctl_debug = CTL_DEBUG_NONE;
408 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
409 &ctl_debug, 0, "Enabled debug flags");
410 static int ctl_lun_map_size = 1024;
411 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
412 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
415 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
416 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
417 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
418 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
420 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
422 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
424 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
425 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
426 static int ctl_init(void);
427 void ctl_shutdown(void);
428 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
429 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
430 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
431 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
432 struct ctl_ooa *ooa_hdr,
433 struct ctl_ooa_entry *kern_entries);
434 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
436 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
437 struct ctl_be_lun *be_lun);
438 static int ctl_free_lun(struct ctl_lun *lun);
439 static void ctl_create_lun(struct ctl_be_lun *be_lun);
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_io *io);
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)
569 struct ctl_lun *lun = CTL_LUN(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 memset(&msg.dt, 0, sizeof(msg.dt));
576 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
577 msg.hdr.original_sc = io->io_hdr.original_sc;
578 msg.hdr.serializing_sc = io;
579 msg.hdr.nexus = io->io_hdr.nexus;
580 msg.hdr.status = io->io_hdr.status;
581 msg.dt.flags = io->io_hdr.flags;
584 * We convert everything into a S/G list here. We can't
585 * pass by reference, only by value between controllers.
586 * So we can't pass a pointer to the S/G list, only as many
587 * S/G entries as we can fit in here. If it's possible for
588 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
589 * then we need to break this up into multiple transfers.
591 if (io->scsiio.kern_sg_entries == 0) {
592 msg.dt.kern_sg_entries = 1;
594 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
595 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
597 /* XXX KDM use busdma here! */
598 msg.dt.sg_list[0].addr =
599 (void *)vtophys(io->scsiio.kern_data_ptr);
602 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
603 ("HA does not support BUS_ADDR"));
604 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
606 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
609 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
613 msg.dt.kern_data_len = io->scsiio.kern_data_len;
614 msg.dt.kern_total_len = io->scsiio.kern_total_len;
615 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
616 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
617 msg.dt.sg_sequence = 0;
620 * Loop until we've sent all of the S/G entries. On the
621 * other end, we'll recompose these S/G entries into one
622 * contiguous list before processing.
624 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
625 msg.dt.sg_sequence++) {
626 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
627 sizeof(msg.dt.sg_list[0])),
628 msg.dt.kern_sg_entries - sg_entries_sent);
629 if (do_sg_copy != 0) {
630 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
631 for (i = sg_entries_sent, j = 0;
632 i < msg.dt.cur_sg_entries; i++, j++) {
634 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
635 msg.dt.sg_list[j].addr = sgl[i].addr;
637 /* XXX KDM use busdma here! */
638 msg.dt.sg_list[j].addr =
639 (void *)vtophys(sgl[i].addr);
642 KASSERT((io->io_hdr.flags &
643 CTL_FLAG_BUS_ADDR) == 0,
644 ("HA does not support BUS_ADDR"));
645 msg.dt.sg_list[j].addr = sgl[i].addr;
647 msg.dt.sg_list[j].len = sgl[i].len;
651 sg_entries_sent += msg.dt.cur_sg_entries;
652 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
653 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
654 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
655 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
656 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
657 io->io_hdr.port_status = 31341;
658 io->scsiio.be_move_done(io);
661 msg.dt.sent_sg_entries = sg_entries_sent;
665 * Officially handover the request from us to peer.
666 * If failover has just happened, then we must return error.
667 * If failover happen just after, then it is not our problem.
670 mtx_lock(&lun->lun_lock);
671 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
673 mtx_unlock(&lun->lun_lock);
674 io->io_hdr.port_status = 31342;
675 io->scsiio.be_move_done(io);
678 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
679 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
681 mtx_unlock(&lun->lun_lock);
685 ctl_ha_done(union ctl_io *io)
687 union ctl_ha_msg msg;
689 if (io->io_hdr.io_type == CTL_IO_SCSI) {
690 memset(&msg, 0, sizeof(msg));
691 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
692 msg.hdr.original_sc = io->io_hdr.original_sc;
693 msg.hdr.nexus = io->io_hdr.nexus;
694 msg.hdr.status = io->io_hdr.status;
695 msg.scsi.scsi_status = io->scsiio.scsi_status;
696 msg.scsi.tag_num = io->scsiio.tag_num;
697 msg.scsi.tag_type = io->scsiio.tag_type;
698 msg.scsi.sense_len = io->scsiio.sense_len;
699 msg.scsi.sense_residual = io->scsiio.sense_residual;
700 msg.scsi.residual = io->scsiio.residual;
701 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
702 io->scsiio.sense_len);
703 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
704 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
705 msg.scsi.sense_len, M_WAITOK);
711 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
712 union ctl_ha_msg *msg_info)
714 struct ctl_scsiio *ctsio;
716 if (msg_info->hdr.original_sc == NULL) {
717 printf("%s: original_sc == NULL!\n", __func__);
718 /* XXX KDM now what? */
722 ctsio = &msg_info->hdr.original_sc->scsiio;
723 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
724 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
725 ctsio->io_hdr.status = msg_info->hdr.status;
726 ctsio->scsi_status = msg_info->scsi.scsi_status;
727 ctsio->sense_len = msg_info->scsi.sense_len;
728 ctsio->sense_residual = msg_info->scsi.sense_residual;
729 ctsio->residual = msg_info->scsi.residual;
730 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
731 msg_info->scsi.sense_len);
732 ctl_enqueue_isc((union ctl_io *)ctsio);
736 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
737 union ctl_ha_msg *msg_info)
739 struct ctl_scsiio *ctsio;
741 if (msg_info->hdr.serializing_sc == NULL) {
742 printf("%s: serializing_sc == NULL!\n", __func__);
743 /* XXX KDM now what? */
747 ctsio = &msg_info->hdr.serializing_sc->scsiio;
748 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
749 ctl_enqueue_isc((union ctl_io *)ctsio);
753 ctl_isc_announce_lun(struct ctl_lun *lun)
755 struct ctl_softc *softc = lun->ctl_softc;
756 union ctl_ha_msg *msg;
757 struct ctl_ha_msg_lun_pr_key pr_key;
760 if (softc->ha_link != CTL_HA_LINK_ONLINE)
762 mtx_lock(&lun->lun_lock);
763 i = sizeof(msg->lun);
765 i += lun->lun_devid->len;
766 i += sizeof(pr_key) * lun->pr_key_count;
768 mtx_unlock(&lun->lun_lock);
769 msg = malloc(i, M_CTL, M_WAITOK);
770 mtx_lock(&lun->lun_lock);
771 k = sizeof(msg->lun);
773 k += lun->lun_devid->len;
774 k += sizeof(pr_key) * lun->pr_key_count;
780 bzero(&msg->lun, sizeof(msg->lun));
781 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
782 msg->hdr.nexus.targ_lun = lun->lun;
783 msg->hdr.nexus.targ_mapped_lun = lun->lun;
784 msg->lun.flags = lun->flags;
785 msg->lun.pr_generation = lun->pr_generation;
786 msg->lun.pr_res_idx = lun->pr_res_idx;
787 msg->lun.pr_res_type = lun->pr_res_type;
788 msg->lun.pr_key_count = lun->pr_key_count;
790 if (lun->lun_devid) {
791 msg->lun.lun_devid_len = lun->lun_devid->len;
792 memcpy(&msg->lun.data[i], lun->lun_devid->data,
793 msg->lun.lun_devid_len);
794 i += msg->lun.lun_devid_len;
796 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
797 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
800 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
803 mtx_unlock(&lun->lun_lock);
804 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
808 if (lun->flags & CTL_LUN_PRIMARY_SC) {
809 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
810 ctl_isc_announce_mode(lun, -1,
811 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
812 lun->mode_pages.index[i].subpage);
818 ctl_isc_announce_port(struct ctl_port *port)
820 struct ctl_softc *softc = port->ctl_softc;
821 union ctl_ha_msg *msg;
824 if (port->targ_port < softc->port_min ||
825 port->targ_port >= softc->port_max ||
826 softc->ha_link != CTL_HA_LINK_ONLINE)
828 i = sizeof(msg->port) + strlen(port->port_name) + 1;
830 i += port->lun_map_size * sizeof(uint32_t);
831 if (port->port_devid)
832 i += port->port_devid->len;
833 if (port->target_devid)
834 i += port->target_devid->len;
835 if (port->init_devid)
836 i += port->init_devid->len;
837 msg = malloc(i, M_CTL, M_WAITOK);
838 bzero(&msg->port, sizeof(msg->port));
839 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
840 msg->hdr.nexus.targ_port = port->targ_port;
841 msg->port.port_type = port->port_type;
842 msg->port.physical_port = port->physical_port;
843 msg->port.virtual_port = port->virtual_port;
844 msg->port.status = port->status;
846 msg->port.name_len = sprintf(&msg->port.data[i],
847 "%d:%s", softc->ha_id, port->port_name) + 1;
848 i += msg->port.name_len;
850 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
851 memcpy(&msg->port.data[i], port->lun_map,
852 msg->port.lun_map_len);
853 i += msg->port.lun_map_len;
855 if (port->port_devid) {
856 msg->port.port_devid_len = port->port_devid->len;
857 memcpy(&msg->port.data[i], port->port_devid->data,
858 msg->port.port_devid_len);
859 i += msg->port.port_devid_len;
861 if (port->target_devid) {
862 msg->port.target_devid_len = port->target_devid->len;
863 memcpy(&msg->port.data[i], port->target_devid->data,
864 msg->port.target_devid_len);
865 i += msg->port.target_devid_len;
867 if (port->init_devid) {
868 msg->port.init_devid_len = port->init_devid->len;
869 memcpy(&msg->port.data[i], port->init_devid->data,
870 msg->port.init_devid_len);
871 i += msg->port.init_devid_len;
873 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
879 ctl_isc_announce_iid(struct ctl_port *port, int iid)
881 struct ctl_softc *softc = port->ctl_softc;
882 union ctl_ha_msg *msg;
885 if (port->targ_port < softc->port_min ||
886 port->targ_port >= softc->port_max ||
887 softc->ha_link != CTL_HA_LINK_ONLINE)
889 mtx_lock(&softc->ctl_lock);
890 i = sizeof(msg->iid);
892 if (port->wwpn_iid[iid].name)
893 l = strlen(port->wwpn_iid[iid].name) + 1;
895 msg = malloc(i, M_CTL, M_NOWAIT);
897 mtx_unlock(&softc->ctl_lock);
900 bzero(&msg->iid, sizeof(msg->iid));
901 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
902 msg->hdr.nexus.targ_port = port->targ_port;
903 msg->hdr.nexus.initid = iid;
904 msg->iid.in_use = port->wwpn_iid[iid].in_use;
905 msg->iid.name_len = l;
906 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
907 if (port->wwpn_iid[iid].name)
908 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
909 mtx_unlock(&softc->ctl_lock);
910 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
915 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
916 uint8_t page, uint8_t subpage)
918 struct ctl_softc *softc = lun->ctl_softc;
919 union ctl_ha_msg msg;
922 if (softc->ha_link != CTL_HA_LINK_ONLINE)
924 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
925 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
926 page && lun->mode_pages.index[i].subpage == subpage)
929 if (i == CTL_NUM_MODE_PAGES)
932 /* Don't try to replicate pages not present on this device. */
933 if (lun->mode_pages.index[i].page_data == NULL)
936 bzero(&msg.mode, sizeof(msg.mode));
937 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
938 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
939 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
940 msg.hdr.nexus.targ_lun = lun->lun;
941 msg.hdr.nexus.targ_mapped_lun = lun->lun;
942 msg.mode.page_code = page;
943 msg.mode.subpage = subpage;
944 msg.mode.page_len = lun->mode_pages.index[i].page_len;
945 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
947 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
952 ctl_isc_ha_link_up(struct ctl_softc *softc)
954 struct ctl_port *port;
956 union ctl_ha_msg msg;
959 /* Announce this node parameters to peer for validation. */
960 msg.login.msg_type = CTL_MSG_LOGIN;
961 msg.login.version = CTL_HA_VERSION;
962 msg.login.ha_mode = softc->ha_mode;
963 msg.login.ha_id = softc->ha_id;
964 msg.login.max_luns = CTL_MAX_LUNS;
965 msg.login.max_ports = CTL_MAX_PORTS;
966 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
967 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
970 STAILQ_FOREACH(port, &softc->port_list, links) {
971 ctl_isc_announce_port(port);
972 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
973 if (port->wwpn_iid[i].in_use)
974 ctl_isc_announce_iid(port, i);
977 STAILQ_FOREACH(lun, &softc->lun_list, links)
978 ctl_isc_announce_lun(lun);
982 ctl_isc_ha_link_down(struct ctl_softc *softc)
984 struct ctl_port *port;
989 mtx_lock(&softc->ctl_lock);
990 STAILQ_FOREACH(lun, &softc->lun_list, links) {
991 mtx_lock(&lun->lun_lock);
992 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
993 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
994 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
996 mtx_unlock(&lun->lun_lock);
998 mtx_unlock(&softc->ctl_lock);
999 io = ctl_alloc_io(softc->othersc_pool);
1000 mtx_lock(&softc->ctl_lock);
1002 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1003 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1004 ctl_enqueue_isc(io);
1007 STAILQ_FOREACH(port, &softc->port_list, links) {
1008 if (port->targ_port >= softc->port_min &&
1009 port->targ_port < softc->port_max)
1011 port->status &= ~CTL_PORT_STATUS_ONLINE;
1012 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1013 port->wwpn_iid[i].in_use = 0;
1014 free(port->wwpn_iid[i].name, M_CTL);
1015 port->wwpn_iid[i].name = NULL;
1018 mtx_unlock(&softc->ctl_lock);
1022 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1024 struct ctl_lun *lun;
1025 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1027 mtx_lock(&softc->ctl_lock);
1028 if (msg->hdr.nexus.targ_mapped_lun >= CTL_MAX_LUNS ||
1029 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1030 mtx_unlock(&softc->ctl_lock);
1033 mtx_lock(&lun->lun_lock);
1034 mtx_unlock(&softc->ctl_lock);
1035 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1036 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1037 if (msg->ua.ua_all) {
1039 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1041 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1044 ctl_est_ua(lun, iid, msg->ua.ua_type);
1046 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1048 mtx_unlock(&lun->lun_lock);
1052 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1054 struct ctl_lun *lun;
1055 struct ctl_ha_msg_lun_pr_key pr_key;
1057 ctl_lun_flags oflags;
1060 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1061 mtx_lock(&softc->ctl_lock);
1062 if (targ_lun >= CTL_MAX_LUNS ||
1063 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1064 mtx_unlock(&softc->ctl_lock);
1067 mtx_lock(&lun->lun_lock);
1068 mtx_unlock(&softc->ctl_lock);
1069 if (lun->flags & CTL_LUN_DISABLED) {
1070 mtx_unlock(&lun->lun_lock);
1073 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1074 if (msg->lun.lun_devid_len != i || (i > 0 &&
1075 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1076 mtx_unlock(&lun->lun_lock);
1077 printf("%s: Received conflicting HA LUN %d\n",
1078 __func__, targ_lun);
1081 /* Record whether peer is primary. */
1082 oflags = lun->flags;
1083 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1084 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1085 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1087 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1088 if (oflags != lun->flags)
1089 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1091 /* If peer is primary and we are not -- use data */
1092 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1093 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1094 lun->pr_generation = msg->lun.pr_generation;
1095 lun->pr_res_idx = msg->lun.pr_res_idx;
1096 lun->pr_res_type = msg->lun.pr_res_type;
1097 lun->pr_key_count = msg->lun.pr_key_count;
1098 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1099 ctl_clr_prkey(lun, k);
1100 for (k = 0; k < msg->lun.pr_key_count; k++) {
1101 memcpy(&pr_key, &msg->lun.data[i],
1103 ctl_alloc_prkey(lun, pr_key.pr_iid);
1104 ctl_set_prkey(lun, pr_key.pr_iid,
1106 i += sizeof(pr_key);
1110 mtx_unlock(&lun->lun_lock);
1111 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1113 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1114 "primary" : "secondary"));
1116 /* If we are primary but peer doesn't know -- notify */
1117 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1118 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1119 ctl_isc_announce_lun(lun);
1124 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1126 struct ctl_port *port;
1127 struct ctl_lun *lun;
1130 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1132 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1133 msg->hdr.nexus.targ_port));
1135 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1136 port->frontend = &ha_frontend;
1137 port->targ_port = msg->hdr.nexus.targ_port;
1138 port->fe_datamove = ctl_ha_datamove;
1139 port->fe_done = ctl_ha_done;
1140 } else if (port->frontend == &ha_frontend) {
1141 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1142 msg->hdr.nexus.targ_port));
1145 printf("%s: Received conflicting HA port %d\n",
1146 __func__, msg->hdr.nexus.targ_port);
1149 port->port_type = msg->port.port_type;
1150 port->physical_port = msg->port.physical_port;
1151 port->virtual_port = msg->port.virtual_port;
1152 port->status = msg->port.status;
1154 free(port->port_name, M_CTL);
1155 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1157 i += msg->port.name_len;
1158 if (msg->port.lun_map_len != 0) {
1159 if (port->lun_map == NULL ||
1160 port->lun_map_size * sizeof(uint32_t) <
1161 msg->port.lun_map_len) {
1162 port->lun_map_size = 0;
1163 free(port->lun_map, M_CTL);
1164 port->lun_map = malloc(msg->port.lun_map_len,
1167 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1168 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1169 i += msg->port.lun_map_len;
1171 port->lun_map_size = 0;
1172 free(port->lun_map, M_CTL);
1173 port->lun_map = NULL;
1175 if (msg->port.port_devid_len != 0) {
1176 if (port->port_devid == NULL ||
1177 port->port_devid->len < msg->port.port_devid_len) {
1178 free(port->port_devid, M_CTL);
1179 port->port_devid = malloc(sizeof(struct ctl_devid) +
1180 msg->port.port_devid_len, M_CTL, M_WAITOK);
1182 memcpy(port->port_devid->data, &msg->port.data[i],
1183 msg->port.port_devid_len);
1184 port->port_devid->len = msg->port.port_devid_len;
1185 i += msg->port.port_devid_len;
1187 free(port->port_devid, M_CTL);
1188 port->port_devid = NULL;
1190 if (msg->port.target_devid_len != 0) {
1191 if (port->target_devid == NULL ||
1192 port->target_devid->len < msg->port.target_devid_len) {
1193 free(port->target_devid, M_CTL);
1194 port->target_devid = malloc(sizeof(struct ctl_devid) +
1195 msg->port.target_devid_len, M_CTL, M_WAITOK);
1197 memcpy(port->target_devid->data, &msg->port.data[i],
1198 msg->port.target_devid_len);
1199 port->target_devid->len = msg->port.target_devid_len;
1200 i += msg->port.target_devid_len;
1202 free(port->target_devid, M_CTL);
1203 port->target_devid = NULL;
1205 if (msg->port.init_devid_len != 0) {
1206 if (port->init_devid == NULL ||
1207 port->init_devid->len < msg->port.init_devid_len) {
1208 free(port->init_devid, M_CTL);
1209 port->init_devid = malloc(sizeof(struct ctl_devid) +
1210 msg->port.init_devid_len, M_CTL, M_WAITOK);
1212 memcpy(port->init_devid->data, &msg->port.data[i],
1213 msg->port.init_devid_len);
1214 port->init_devid->len = msg->port.init_devid_len;
1215 i += msg->port.init_devid_len;
1217 free(port->init_devid, M_CTL);
1218 port->init_devid = NULL;
1221 if (ctl_port_register(port) != 0) {
1222 printf("%s: ctl_port_register() failed with error\n",
1226 mtx_lock(&softc->ctl_lock);
1227 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1228 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1230 mtx_lock(&lun->lun_lock);
1231 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1232 mtx_unlock(&lun->lun_lock);
1234 mtx_unlock(&softc->ctl_lock);
1238 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1240 struct ctl_port *port;
1243 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1245 printf("%s: Received IID for unknown port %d\n",
1246 __func__, msg->hdr.nexus.targ_port);
1249 iid = msg->hdr.nexus.initid;
1250 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1251 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1252 free(port->wwpn_iid[iid].name, M_CTL);
1253 if (msg->iid.name_len) {
1254 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1255 msg->iid.name_len, M_CTL);
1257 port->wwpn_iid[iid].name = NULL;
1261 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1264 if (msg->login.version != CTL_HA_VERSION) {
1265 printf("CTL HA peers have different versions %d != %d\n",
1266 msg->login.version, CTL_HA_VERSION);
1267 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1270 if (msg->login.ha_mode != softc->ha_mode) {
1271 printf("CTL HA peers have different ha_mode %d != %d\n",
1272 msg->login.ha_mode, softc->ha_mode);
1273 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1276 if (msg->login.ha_id == softc->ha_id) {
1277 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1278 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1281 if (msg->login.max_luns != CTL_MAX_LUNS ||
1282 msg->login.max_ports != CTL_MAX_PORTS ||
1283 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1284 printf("CTL HA peers have different limits\n");
1285 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1291 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1293 struct ctl_lun *lun;
1295 uint32_t initidx, targ_lun;
1297 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1298 mtx_lock(&softc->ctl_lock);
1299 if (targ_lun >= CTL_MAX_LUNS ||
1300 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1301 mtx_unlock(&softc->ctl_lock);
1304 mtx_lock(&lun->lun_lock);
1305 mtx_unlock(&softc->ctl_lock);
1306 if (lun->flags & CTL_LUN_DISABLED) {
1307 mtx_unlock(&lun->lun_lock);
1310 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1311 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1312 msg->mode.page_code &&
1313 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1316 if (i == CTL_NUM_MODE_PAGES) {
1317 mtx_unlock(&lun->lun_lock);
1320 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1321 lun->mode_pages.index[i].page_len);
1322 initidx = ctl_get_initindex(&msg->hdr.nexus);
1324 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1325 mtx_unlock(&lun->lun_lock);
1329 * ISC (Inter Shelf Communication) event handler. Events from the HA
1330 * subsystem come in here.
1333 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1335 struct ctl_softc *softc = control_softc;
1337 struct ctl_prio *presio;
1338 ctl_ha_status isc_status;
1340 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1341 if (event == CTL_HA_EVT_MSG_RECV) {
1342 union ctl_ha_msg *msg, msgbuf;
1344 if (param > sizeof(msgbuf))
1345 msg = malloc(param, M_CTL, M_WAITOK);
1348 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1350 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1351 printf("%s: Error receiving message: %d\n",
1352 __func__, isc_status);
1358 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1359 switch (msg->hdr.msg_type) {
1360 case CTL_MSG_SERIALIZE:
1361 io = ctl_alloc_io(softc->othersc_pool);
1363 // populate ctsio from msg
1364 io->io_hdr.io_type = CTL_IO_SCSI;
1365 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1366 io->io_hdr.original_sc = msg->hdr.original_sc;
1367 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1370 * If we're in serialization-only mode, we don't
1371 * want to go through full done processing. Thus
1374 * XXX KDM add another flag that is more specific.
1376 if (softc->ha_mode != CTL_HA_MODE_XFER)
1377 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1378 io->io_hdr.nexus = msg->hdr.nexus;
1380 printf("port %u, iid %u, lun %u\n",
1381 io->io_hdr.nexus.targ_port,
1382 io->io_hdr.nexus.initid,
1383 io->io_hdr.nexus.targ_lun);
1385 io->scsiio.tag_num = msg->scsi.tag_num;
1386 io->scsiio.tag_type = msg->scsi.tag_type;
1388 io->io_hdr.start_time = time_uptime;
1389 getbinuptime(&io->io_hdr.start_bt);
1390 #endif /* CTL_TIME_IO */
1391 io->scsiio.cdb_len = msg->scsi.cdb_len;
1392 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1394 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1395 const struct ctl_cmd_entry *entry;
1397 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1398 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1400 entry->flags & CTL_FLAG_DATA_MASK;
1402 ctl_enqueue_isc(io);
1405 /* Performed on the Originating SC, XFER mode only */
1406 case CTL_MSG_DATAMOVE: {
1407 struct ctl_sg_entry *sgl;
1410 io = msg->hdr.original_sc;
1412 printf("%s: original_sc == NULL!\n", __func__);
1413 /* XXX KDM do something here */
1416 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1417 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1419 * Keep track of this, we need to send it back over
1420 * when the datamove is complete.
1422 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1423 if (msg->hdr.status == CTL_SUCCESS)
1424 io->io_hdr.status = msg->hdr.status;
1426 if (msg->dt.sg_sequence == 0) {
1428 getbinuptime(&io->io_hdr.dma_start_bt);
1430 i = msg->dt.kern_sg_entries +
1431 msg->dt.kern_data_len /
1432 CTL_HA_DATAMOVE_SEGMENT + 1;
1433 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1435 io->io_hdr.remote_sglist = sgl;
1436 io->io_hdr.local_sglist =
1437 &sgl[msg->dt.kern_sg_entries];
1439 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1441 io->scsiio.kern_sg_entries =
1442 msg->dt.kern_sg_entries;
1443 io->scsiio.rem_sg_entries =
1444 msg->dt.kern_sg_entries;
1445 io->scsiio.kern_data_len =
1446 msg->dt.kern_data_len;
1447 io->scsiio.kern_total_len =
1448 msg->dt.kern_total_len;
1449 io->scsiio.kern_data_resid =
1450 msg->dt.kern_data_resid;
1451 io->scsiio.kern_rel_offset =
1452 msg->dt.kern_rel_offset;
1453 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1454 io->io_hdr.flags |= msg->dt.flags &
1457 sgl = (struct ctl_sg_entry *)
1458 io->scsiio.kern_data_ptr;
1460 for (i = msg->dt.sent_sg_entries, j = 0;
1461 i < (msg->dt.sent_sg_entries +
1462 msg->dt.cur_sg_entries); i++, j++) {
1463 sgl[i].addr = msg->dt.sg_list[j].addr;
1464 sgl[i].len = msg->dt.sg_list[j].len;
1467 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1468 __func__, sgl[i].addr, sgl[i].len, j, i);
1473 * If this is the last piece of the I/O, we've got
1474 * the full S/G list. Queue processing in the thread.
1475 * Otherwise wait for the next piece.
1477 if (msg->dt.sg_last != 0)
1478 ctl_enqueue_isc(io);
1481 /* Performed on the Serializing (primary) SC, XFER mode only */
1482 case CTL_MSG_DATAMOVE_DONE: {
1483 if (msg->hdr.serializing_sc == NULL) {
1484 printf("%s: serializing_sc == NULL!\n",
1486 /* XXX KDM now what? */
1490 * We grab the sense information here in case
1491 * there was a failure, so we can return status
1492 * back to the initiator.
1494 io = msg->hdr.serializing_sc;
1495 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1496 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1497 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1498 io->io_hdr.port_status = msg->scsi.fetd_status;
1499 io->scsiio.residual = msg->scsi.residual;
1500 if (msg->hdr.status != CTL_STATUS_NONE) {
1501 io->io_hdr.status = msg->hdr.status;
1502 io->scsiio.scsi_status = msg->scsi.scsi_status;
1503 io->scsiio.sense_len = msg->scsi.sense_len;
1504 io->scsiio.sense_residual =msg->scsi.sense_residual;
1505 memcpy(&io->scsiio.sense_data,
1506 &msg->scsi.sense_data,
1507 msg->scsi.sense_len);
1508 if (msg->hdr.status == CTL_SUCCESS)
1509 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1511 ctl_enqueue_isc(io);
1515 /* Preformed on Originating SC, SER_ONLY mode */
1517 io = msg->hdr.original_sc;
1519 printf("%s: original_sc == NULL!\n",
1523 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1524 io->io_hdr.msg_type = CTL_MSG_R2R;
1525 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1526 ctl_enqueue_isc(io);
1530 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1532 * Performed on the Originating (i.e. secondary) SC in XFER
1535 case CTL_MSG_FINISH_IO:
1536 if (softc->ha_mode == CTL_HA_MODE_XFER)
1537 ctl_isc_handler_finish_xfer(softc, msg);
1539 ctl_isc_handler_finish_ser_only(softc, msg);
1542 /* Preformed on Originating SC */
1543 case CTL_MSG_BAD_JUJU:
1544 io = msg->hdr.original_sc;
1546 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1550 ctl_copy_sense_data(msg, io);
1552 * IO should have already been cleaned up on other
1553 * SC so clear this flag so we won't send a message
1554 * back to finish the IO there.
1556 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1557 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1559 /* io = msg->hdr.serializing_sc; */
1560 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1561 ctl_enqueue_isc(io);
1564 /* Handle resets sent from the other side */
1565 case CTL_MSG_MANAGE_TASKS: {
1566 struct ctl_taskio *taskio;
1567 taskio = (struct ctl_taskio *)ctl_alloc_io(
1568 softc->othersc_pool);
1569 ctl_zero_io((union ctl_io *)taskio);
1570 taskio->io_hdr.io_type = CTL_IO_TASK;
1571 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1572 taskio->io_hdr.nexus = msg->hdr.nexus;
1573 taskio->task_action = msg->task.task_action;
1574 taskio->tag_num = msg->task.tag_num;
1575 taskio->tag_type = msg->task.tag_type;
1577 taskio->io_hdr.start_time = time_uptime;
1578 getbinuptime(&taskio->io_hdr.start_bt);
1579 #endif /* CTL_TIME_IO */
1580 ctl_run_task((union ctl_io *)taskio);
1583 /* Persistent Reserve action which needs attention */
1584 case CTL_MSG_PERS_ACTION:
1585 presio = (struct ctl_prio *)ctl_alloc_io(
1586 softc->othersc_pool);
1587 ctl_zero_io((union ctl_io *)presio);
1588 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1589 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1590 presio->io_hdr.nexus = msg->hdr.nexus;
1591 presio->pr_msg = msg->pr;
1592 ctl_enqueue_isc((union ctl_io *)presio);
1595 ctl_isc_ua(softc, msg, param);
1597 case CTL_MSG_PORT_SYNC:
1598 ctl_isc_port_sync(softc, msg, param);
1600 case CTL_MSG_LUN_SYNC:
1601 ctl_isc_lun_sync(softc, msg, param);
1603 case CTL_MSG_IID_SYNC:
1604 ctl_isc_iid_sync(softc, msg, param);
1607 ctl_isc_login(softc, msg, param);
1609 case CTL_MSG_MODE_SYNC:
1610 ctl_isc_mode_sync(softc, msg, param);
1613 printf("Received HA message of unknown type %d\n",
1615 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1620 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1621 printf("CTL: HA link status changed from %d to %d\n",
1622 softc->ha_link, param);
1623 if (param == softc->ha_link)
1625 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1626 softc->ha_link = param;
1627 ctl_isc_ha_link_down(softc);
1629 softc->ha_link = param;
1630 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1631 ctl_isc_ha_link_up(softc);
1635 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1641 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1644 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1645 src->scsi.sense_len);
1646 dest->scsiio.scsi_status = src->scsi.scsi_status;
1647 dest->scsiio.sense_len = src->scsi.sense_len;
1648 dest->io_hdr.status = src->hdr.status;
1652 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1655 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1656 src->scsiio.sense_len);
1657 dest->scsi.scsi_status = src->scsiio.scsi_status;
1658 dest->scsi.sense_len = src->scsiio.sense_len;
1659 dest->hdr.status = src->io_hdr.status;
1663 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1665 struct ctl_softc *softc = lun->ctl_softc;
1668 if (initidx < softc->init_min || initidx >= softc->init_max)
1670 mtx_assert(&lun->lun_lock, MA_OWNED);
1671 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1674 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1678 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1682 mtx_assert(&lun->lun_lock, MA_OWNED);
1683 if (lun->pending_ua[port] == NULL)
1685 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1686 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1688 lun->pending_ua[port][i] |= ua;
1693 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1695 struct ctl_softc *softc = lun->ctl_softc;
1698 mtx_assert(&lun->lun_lock, MA_OWNED);
1699 for (i = softc->port_min; i < softc->port_max; i++)
1700 ctl_est_ua_port(lun, i, except, ua);
1704 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1706 struct ctl_softc *softc = lun->ctl_softc;
1709 if (initidx < softc->init_min || initidx >= softc->init_max)
1711 mtx_assert(&lun->lun_lock, MA_OWNED);
1712 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1715 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1719 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1721 struct ctl_softc *softc = lun->ctl_softc;
1724 mtx_assert(&lun->lun_lock, MA_OWNED);
1725 for (i = softc->port_min; i < softc->port_max; i++) {
1726 if (lun->pending_ua[i] == NULL)
1728 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1729 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1731 lun->pending_ua[i][j] &= ~ua;
1737 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1738 ctl_ua_type ua_type)
1740 struct ctl_lun *lun;
1742 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1743 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1744 mtx_lock(&lun->lun_lock);
1745 ctl_clr_ua(lun, initidx, ua_type);
1746 mtx_unlock(&lun->lun_lock);
1751 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1753 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1754 struct ctl_lun *lun;
1755 struct ctl_lun_req ireq;
1758 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1759 error = sysctl_handle_int(oidp, &value, 0, req);
1760 if ((error != 0) || (req->newptr == NULL))
1763 mtx_lock(&softc->ctl_lock);
1765 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1767 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1768 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1769 mtx_unlock(&softc->ctl_lock);
1770 bzero(&ireq, sizeof(ireq));
1771 ireq.reqtype = CTL_LUNREQ_MODIFY;
1772 ireq.reqdata.modify.lun_id = lun->lun;
1773 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1775 if (ireq.status != CTL_LUN_OK) {
1776 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1777 __func__, ireq.status, ireq.error_str);
1779 mtx_lock(&softc->ctl_lock);
1781 mtx_unlock(&softc->ctl_lock);
1788 struct make_dev_args args;
1789 struct ctl_softc *softc;
1793 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1796 make_dev_args_init(&args);
1797 args.mda_devsw = &ctl_cdevsw;
1798 args.mda_uid = UID_ROOT;
1799 args.mda_gid = GID_OPERATOR;
1800 args.mda_mode = 0600;
1801 args.mda_si_drv1 = softc;
1802 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1804 free(softc, M_DEVBUF);
1805 control_softc = NULL;
1809 sysctl_ctx_init(&softc->sysctl_ctx);
1810 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1811 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1812 CTLFLAG_RD, 0, "CAM Target Layer");
1814 if (softc->sysctl_tree == NULL) {
1815 printf("%s: unable to allocate sysctl tree\n", __func__);
1816 destroy_dev(softc->dev);
1817 free(softc, M_DEVBUF);
1818 control_softc = NULL;
1822 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1823 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1824 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1827 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1828 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1829 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1832 * In Copan's HA scheme, the "master" and "slave" roles are
1833 * figured out through the slot the controller is in. Although it
1834 * is an active/active system, someone has to be in charge.
1836 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1837 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1838 "HA head ID (0 - no HA)");
1839 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1840 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1841 softc->is_single = 1;
1842 softc->port_cnt = CTL_MAX_PORTS;
1843 softc->port_min = 0;
1845 softc->port_cnt = CTL_MAX_PORTS / NUM_HA_SHELVES;
1846 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1848 softc->port_max = softc->port_min + softc->port_cnt;
1849 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1850 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1852 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1853 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1854 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1856 STAILQ_INIT(&softc->lun_list);
1857 STAILQ_INIT(&softc->pending_lun_queue);
1858 STAILQ_INIT(&softc->fe_list);
1859 STAILQ_INIT(&softc->port_list);
1860 STAILQ_INIT(&softc->be_list);
1861 ctl_tpc_init(softc);
1863 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
1866 printf("ctl: can't allocate %d entry other SC pool, "
1867 "exiting\n", CTL_POOL_ENTRIES_OTHER_SC);
1870 softc->othersc_pool = other_pool;
1872 if (worker_threads <= 0)
1873 worker_threads = max(1, mp_ncpus / 4);
1874 if (worker_threads > CTL_MAX_THREADS)
1875 worker_threads = CTL_MAX_THREADS;
1877 for (i = 0; i < worker_threads; i++) {
1878 struct ctl_thread *thr = &softc->threads[i];
1880 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1881 thr->ctl_softc = softc;
1882 STAILQ_INIT(&thr->incoming_queue);
1883 STAILQ_INIT(&thr->rtr_queue);
1884 STAILQ_INIT(&thr->done_queue);
1885 STAILQ_INIT(&thr->isc_queue);
1887 error = kproc_kthread_add(ctl_work_thread, thr,
1888 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1890 printf("error creating CTL work thread!\n");
1891 ctl_pool_free(other_pool);
1895 error = kproc_kthread_add(ctl_lun_thread, softc,
1896 &softc->ctl_proc, NULL, 0, 0, "ctl", "lun");
1898 printf("error creating CTL lun thread!\n");
1899 ctl_pool_free(other_pool);
1902 error = kproc_kthread_add(ctl_thresh_thread, softc,
1903 &softc->ctl_proc, NULL, 0, 0, "ctl", "thresh");
1905 printf("error creating CTL threshold thread!\n");
1906 ctl_pool_free(other_pool);
1910 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1911 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1912 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1914 if (softc->is_single == 0) {
1915 ctl_frontend_register(&ha_frontend);
1916 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
1917 printf("ctl_init: ctl_ha_msg_init failed.\n");
1918 softc->is_single = 1;
1920 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
1921 != CTL_HA_STATUS_SUCCESS) {
1922 printf("ctl_init: ctl_ha_msg_register failed.\n");
1923 softc->is_single = 1;
1932 struct ctl_softc *softc = control_softc;
1933 struct ctl_lun *lun, *next_lun;
1935 if (softc->is_single == 0) {
1936 ctl_ha_msg_shutdown(softc);
1937 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL)
1938 != CTL_HA_STATUS_SUCCESS)
1939 printf("%s: ctl_ha_msg_deregister failed.\n", __func__);
1940 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
1941 printf("%s: ctl_ha_msg_destroy failed.\n", __func__);
1942 ctl_frontend_deregister(&ha_frontend);
1945 mtx_lock(&softc->ctl_lock);
1947 STAILQ_FOREACH_SAFE(lun, &softc->lun_list, links, next_lun)
1950 mtx_unlock(&softc->ctl_lock);
1953 ctl_shutdown_thread(softc->work_thread);
1954 mtx_destroy(&softc->queue_lock);
1957 ctl_tpc_shutdown(softc);
1958 uma_zdestroy(softc->io_zone);
1959 mtx_destroy(&softc->ctl_lock);
1961 destroy_dev(softc->dev);
1963 sysctl_ctx_free(&softc->sysctl_ctx);
1965 free(softc, M_DEVBUF);
1966 control_softc = NULL;
1970 ctl_module_event_handler(module_t mod, int what, void *arg)
1975 return (ctl_init());
1979 return (EOPNOTSUPP);
1984 * XXX KDM should we do some access checks here? Bump a reference count to
1985 * prevent a CTL module from being unloaded while someone has it open?
1988 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
1994 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2000 * Remove an initiator by port number and initiator ID.
2001 * Returns 0 for success, -1 for failure.
2004 ctl_remove_initiator(struct ctl_port *port, int iid)
2006 struct ctl_softc *softc = port->ctl_softc;
2008 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2010 if (iid > CTL_MAX_INIT_PER_PORT) {
2011 printf("%s: initiator ID %u > maximun %u!\n",
2012 __func__, iid, CTL_MAX_INIT_PER_PORT);
2016 mtx_lock(&softc->ctl_lock);
2017 port->wwpn_iid[iid].in_use--;
2018 port->wwpn_iid[iid].last_use = time_uptime;
2019 mtx_unlock(&softc->ctl_lock);
2020 ctl_isc_announce_iid(port, iid);
2026 * Add an initiator to the initiator map.
2027 * Returns iid for success, < 0 for failure.
2030 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2032 struct ctl_softc *softc = port->ctl_softc;
2036 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2038 if (iid >= CTL_MAX_INIT_PER_PORT) {
2039 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2040 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2045 mtx_lock(&softc->ctl_lock);
2047 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2048 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2049 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2053 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2054 strcmp(name, port->wwpn_iid[i].name) == 0) {
2062 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2063 if (port->wwpn_iid[i].in_use == 0 &&
2064 port->wwpn_iid[i].wwpn == 0 &&
2065 port->wwpn_iid[i].name == NULL) {
2074 best_time = INT32_MAX;
2075 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2076 if (port->wwpn_iid[i].in_use == 0) {
2077 if (port->wwpn_iid[i].last_use < best_time) {
2079 best_time = port->wwpn_iid[i].last_use;
2087 mtx_unlock(&softc->ctl_lock);
2092 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2094 * This is not an error yet.
2096 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2098 printf("%s: port %d iid %u WWPN %#jx arrived"
2099 " again\n", __func__, port->targ_port,
2100 iid, (uintmax_t)wwpn);
2104 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2105 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2107 printf("%s: port %d iid %u name '%s' arrived"
2108 " again\n", __func__, port->targ_port,
2115 * This is an error, but what do we do about it? The
2116 * driver is telling us we have a new WWPN for this
2117 * initiator ID, so we pretty much need to use it.
2119 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2120 " but WWPN %#jx '%s' is still at that address\n",
2121 __func__, port->targ_port, iid, wwpn, name,
2122 (uintmax_t)port->wwpn_iid[iid].wwpn,
2123 port->wwpn_iid[iid].name);
2126 * XXX KDM clear have_ca and ua_pending on each LUN for
2131 free(port->wwpn_iid[iid].name, M_CTL);
2132 port->wwpn_iid[iid].name = name;
2133 port->wwpn_iid[iid].wwpn = wwpn;
2134 port->wwpn_iid[iid].in_use++;
2135 mtx_unlock(&softc->ctl_lock);
2136 ctl_isc_announce_iid(port, iid);
2142 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2146 switch (port->port_type) {
2149 struct scsi_transportid_fcp *id =
2150 (struct scsi_transportid_fcp *)buf;
2151 if (port->wwpn_iid[iid].wwpn == 0)
2153 memset(id, 0, sizeof(*id));
2154 id->format_protocol = SCSI_PROTO_FC;
2155 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2156 return (sizeof(*id));
2158 case CTL_PORT_ISCSI:
2160 struct scsi_transportid_iscsi_port *id =
2161 (struct scsi_transportid_iscsi_port *)buf;
2162 if (port->wwpn_iid[iid].name == NULL)
2165 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2167 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2168 len = roundup2(min(len, 252), 4);
2169 scsi_ulto2b(len, id->additional_length);
2170 return (sizeof(*id) + len);
2174 struct scsi_transportid_sas *id =
2175 (struct scsi_transportid_sas *)buf;
2176 if (port->wwpn_iid[iid].wwpn == 0)
2178 memset(id, 0, sizeof(*id));
2179 id->format_protocol = SCSI_PROTO_SAS;
2180 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2181 return (sizeof(*id));
2185 struct scsi_transportid_spi *id =
2186 (struct scsi_transportid_spi *)buf;
2187 memset(id, 0, sizeof(*id));
2188 id->format_protocol = SCSI_PROTO_SPI;
2189 scsi_ulto2b(iid, id->scsi_addr);
2190 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2191 return (sizeof(*id));
2197 * Serialize a command that went down the "wrong" side, and so was sent to
2198 * this controller for execution. The logic is a little different than the
2199 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2200 * sent back to the other side, but in the success case, we execute the
2201 * command on this side (XFER mode) or tell the other side to execute it
2205 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2207 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2208 struct ctl_port *port = CTL_PORT(ctsio);
2209 union ctl_ha_msg msg_info;
2210 struct ctl_lun *lun;
2211 const struct ctl_cmd_entry *entry;
2214 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2216 /* Make sure that we know about this port. */
2217 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2218 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2223 /* Make sure that we know about this LUN. */
2224 mtx_lock(&softc->ctl_lock);
2225 if (targ_lun >= CTL_MAX_LUNS ||
2226 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2227 mtx_unlock(&softc->ctl_lock);
2230 * The other node would not send this request to us unless
2231 * received announce that we are primary node for this LUN.
2232 * If this LUN does not exist now, it is probably result of
2233 * a race, so respond to initiator in the most opaque way.
2235 ctl_set_busy(ctsio);
2238 mtx_lock(&lun->lun_lock);
2239 mtx_unlock(&softc->ctl_lock);
2242 * If the LUN is invalid, pretend that it doesn't exist.
2243 * It will go away as soon as all pending I/Os completed.
2245 if (lun->flags & CTL_LUN_DISABLED) {
2246 mtx_unlock(&lun->lun_lock);
2247 ctl_set_busy(ctsio);
2251 entry = ctl_get_cmd_entry(ctsio, NULL);
2252 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2253 mtx_unlock(&lun->lun_lock);
2257 CTL_LUN(ctsio) = lun;
2258 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2261 * Every I/O goes into the OOA queue for a
2262 * particular LUN, and stays there until completion.
2265 if (TAILQ_EMPTY(&lun->ooa_queue))
2266 lun->idle_time += getsbinuptime() - lun->last_busy;
2268 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2270 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2271 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2273 case CTL_ACTION_BLOCK:
2274 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2275 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2277 mtx_unlock(&lun->lun_lock);
2279 case CTL_ACTION_PASS:
2280 case CTL_ACTION_SKIP:
2281 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2282 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2283 ctl_enqueue_rtr((union ctl_io *)ctsio);
2284 mtx_unlock(&lun->lun_lock);
2286 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2287 mtx_unlock(&lun->lun_lock);
2289 /* send msg back to other side */
2290 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2291 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2292 msg_info.hdr.msg_type = CTL_MSG_R2R;
2293 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2294 sizeof(msg_info.hdr), M_WAITOK);
2297 case CTL_ACTION_OVERLAP:
2298 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2299 mtx_unlock(&lun->lun_lock);
2300 ctl_set_overlapped_cmd(ctsio);
2302 case CTL_ACTION_OVERLAP_TAG:
2303 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2304 mtx_unlock(&lun->lun_lock);
2305 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2307 case CTL_ACTION_ERROR:
2309 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2310 mtx_unlock(&lun->lun_lock);
2312 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2315 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2316 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2317 msg_info.hdr.serializing_sc = NULL;
2318 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2319 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2320 sizeof(msg_info.scsi), M_WAITOK);
2321 ctl_free_io((union ctl_io *)ctsio);
2327 * Returns 0 for success, errno for failure.
2330 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2331 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2335 mtx_lock(&lun->lun_lock);
2336 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2337 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2339 struct ctl_ooa_entry *entry;
2342 * If we've got more than we can fit, just count the
2343 * remaining entries.
2345 if (*cur_fill_num >= ooa_hdr->alloc_num)
2348 entry = &kern_entries[*cur_fill_num];
2350 entry->tag_num = io->scsiio.tag_num;
2351 entry->lun_num = lun->lun;
2353 entry->start_bt = io->io_hdr.start_bt;
2355 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2356 entry->cdb_len = io->scsiio.cdb_len;
2357 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2358 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2360 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2361 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2363 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2364 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2366 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2367 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2369 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2370 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2372 mtx_unlock(&lun->lun_lock);
2376 ctl_copyin_alloc(void *user_addr, unsigned int len, char *error_str,
2377 size_t error_str_len)
2381 kptr = malloc(len, M_CTL, M_WAITOK | M_ZERO);
2383 if (copyin(user_addr, kptr, len) != 0) {
2384 snprintf(error_str, error_str_len, "Error copying %d bytes "
2385 "from user address %p to kernel address %p", len,
2395 ctl_free_args(int num_args, struct ctl_be_arg *args)
2402 for (i = 0; i < num_args; i++) {
2403 free(args[i].kname, M_CTL);
2404 free(args[i].kvalue, M_CTL);
2410 static struct ctl_be_arg *
2411 ctl_copyin_args(int num_args, struct ctl_be_arg *uargs,
2412 char *error_str, size_t error_str_len)
2414 struct ctl_be_arg *args;
2417 args = ctl_copyin_alloc(uargs, num_args * sizeof(*args),
2418 error_str, error_str_len);
2423 for (i = 0; i < num_args; i++) {
2424 args[i].kname = NULL;
2425 args[i].kvalue = NULL;
2428 for (i = 0; i < num_args; i++) {
2431 if (args[i].namelen == 0) {
2432 snprintf(error_str, error_str_len, "Argument %d "
2433 "name length is zero", i);
2437 args[i].kname = ctl_copyin_alloc(args[i].name,
2438 args[i].namelen, error_str, error_str_len);
2439 if (args[i].kname == NULL)
2442 if (args[i].kname[args[i].namelen - 1] != '\0') {
2443 snprintf(error_str, error_str_len, "Argument %d "
2444 "name is not NUL-terminated", i);
2448 if (args[i].flags & CTL_BEARG_RD) {
2449 if (args[i].vallen == 0) {
2450 snprintf(error_str, error_str_len, "Argument %d "
2451 "value length is zero", i);
2455 tmpptr = ctl_copyin_alloc(args[i].value,
2456 args[i].vallen, error_str, error_str_len);
2460 if ((args[i].flags & CTL_BEARG_ASCII)
2461 && (tmpptr[args[i].vallen - 1] != '\0')) {
2462 snprintf(error_str, error_str_len, "Argument "
2463 "%d value is not NUL-terminated", i);
2464 free(tmpptr, M_CTL);
2467 args[i].kvalue = tmpptr;
2469 args[i].kvalue = malloc(args[i].vallen,
2470 M_CTL, M_WAITOK | M_ZERO);
2477 ctl_free_args(num_args, args);
2483 ctl_copyout_args(int num_args, struct ctl_be_arg *args)
2487 for (i = 0; i < num_args; i++) {
2488 if (args[i].flags & CTL_BEARG_WR)
2489 copyout(args[i].kvalue, args[i].value, args[i].vallen);
2494 * Escape characters that are illegal or not recommended in XML.
2497 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2499 char *end = str + size;
2504 for (; *str && str < end; str++) {
2507 retval = sbuf_printf(sb, "&");
2510 retval = sbuf_printf(sb, ">");
2513 retval = sbuf_printf(sb, "<");
2516 retval = sbuf_putc(sb, *str);
2529 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2531 struct scsi_vpd_id_descriptor *desc;
2534 if (id == NULL || id->len < 4)
2536 desc = (struct scsi_vpd_id_descriptor *)id->data;
2537 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2538 case SVPD_ID_TYPE_T10:
2539 sbuf_printf(sb, "t10.");
2541 case SVPD_ID_TYPE_EUI64:
2542 sbuf_printf(sb, "eui.");
2544 case SVPD_ID_TYPE_NAA:
2545 sbuf_printf(sb, "naa.");
2547 case SVPD_ID_TYPE_SCSI_NAME:
2550 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2551 case SVPD_ID_CODESET_BINARY:
2552 for (i = 0; i < desc->length; i++)
2553 sbuf_printf(sb, "%02x", desc->identifier[i]);
2555 case SVPD_ID_CODESET_ASCII:
2556 sbuf_printf(sb, "%.*s", (int)desc->length,
2557 (char *)desc->identifier);
2559 case SVPD_ID_CODESET_UTF8:
2560 sbuf_printf(sb, "%s", (char *)desc->identifier);
2566 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2569 struct ctl_softc *softc = dev->si_drv1;
2570 struct ctl_port *port;
2571 struct ctl_lun *lun;
2578 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2580 case CTL_ENABLE_PORT:
2581 case CTL_DISABLE_PORT:
2582 case CTL_SET_PORT_WWNS: {
2583 struct ctl_port *port;
2584 struct ctl_port_entry *entry;
2586 entry = (struct ctl_port_entry *)addr;
2588 mtx_lock(&softc->ctl_lock);
2589 STAILQ_FOREACH(port, &softc->port_list, links) {
2592 if (port->targ_port < softc->port_min ||
2593 port->targ_port >= softc->port_max)
2598 if ((entry->port_type == CTL_PORT_NONE)
2599 && (entry->targ_port == port->targ_port)) {
2601 * If the user only wants to enable or
2602 * disable or set WWNs on a specific port,
2603 * do the operation and we're done.
2607 } else if (entry->port_type & port->port_type) {
2609 * Compare the user's type mask with the
2610 * particular frontend type to see if we
2617 * Make sure the user isn't trying to set
2618 * WWNs on multiple ports at the same time.
2620 if (cmd == CTL_SET_PORT_WWNS) {
2621 printf("%s: Can't set WWNs on "
2622 "multiple ports\n", __func__);
2631 * XXX KDM we have to drop the lock here, because
2632 * the online/offline operations can potentially
2633 * block. We need to reference count the frontends
2634 * so they can't go away,
2636 if (cmd == CTL_ENABLE_PORT) {
2637 mtx_unlock(&softc->ctl_lock);
2638 ctl_port_online(port);
2639 mtx_lock(&softc->ctl_lock);
2640 } else if (cmd == CTL_DISABLE_PORT) {
2641 mtx_unlock(&softc->ctl_lock);
2642 ctl_port_offline(port);
2643 mtx_lock(&softc->ctl_lock);
2644 } else if (cmd == CTL_SET_PORT_WWNS) {
2645 ctl_port_set_wwns(port,
2646 (entry->flags & CTL_PORT_WWNN_VALID) ?
2648 (entry->flags & CTL_PORT_WWPN_VALID) ?
2649 1 : 0, entry->wwpn);
2654 mtx_unlock(&softc->ctl_lock);
2658 struct ctl_ooa *ooa_hdr;
2659 struct ctl_ooa_entry *entries;
2660 uint32_t cur_fill_num;
2662 ooa_hdr = (struct ctl_ooa *)addr;
2664 if ((ooa_hdr->alloc_len == 0)
2665 || (ooa_hdr->alloc_num == 0)) {
2666 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2667 "must be non-zero\n", __func__,
2668 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2673 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2674 sizeof(struct ctl_ooa_entry))) {
2675 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2676 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2677 __func__, ooa_hdr->alloc_len,
2678 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2683 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2684 if (entries == NULL) {
2685 printf("%s: could not allocate %d bytes for OOA "
2686 "dump\n", __func__, ooa_hdr->alloc_len);
2691 mtx_lock(&softc->ctl_lock);
2692 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2693 (ooa_hdr->lun_num >= CTL_MAX_LUNS ||
2694 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2695 mtx_unlock(&softc->ctl_lock);
2696 free(entries, M_CTL);
2697 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2698 __func__, (uintmax_t)ooa_hdr->lun_num);
2705 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2706 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2707 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2711 lun = softc->ctl_luns[ooa_hdr->lun_num];
2712 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2715 mtx_unlock(&softc->ctl_lock);
2717 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2718 ooa_hdr->fill_len = ooa_hdr->fill_num *
2719 sizeof(struct ctl_ooa_entry);
2720 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2722 printf("%s: error copying out %d bytes for OOA dump\n",
2723 __func__, ooa_hdr->fill_len);
2726 getbinuptime(&ooa_hdr->cur_bt);
2728 if (cur_fill_num > ooa_hdr->alloc_num) {
2729 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2730 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2732 ooa_hdr->dropped_num = 0;
2733 ooa_hdr->status = CTL_OOA_OK;
2736 free(entries, M_CTL);
2739 case CTL_DELAY_IO: {
2740 struct ctl_io_delay_info *delay_info;
2742 delay_info = (struct ctl_io_delay_info *)addr;
2745 mtx_lock(&softc->ctl_lock);
2746 if (delay_info->lun_id >= CTL_MAX_LUNS ||
2747 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2748 mtx_unlock(&softc->ctl_lock);
2749 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2752 mtx_lock(&lun->lun_lock);
2753 mtx_unlock(&softc->ctl_lock);
2754 delay_info->status = CTL_DELAY_STATUS_OK;
2755 switch (delay_info->delay_type) {
2756 case CTL_DELAY_TYPE_CONT:
2757 case CTL_DELAY_TYPE_ONESHOT:
2760 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2763 switch (delay_info->delay_loc) {
2764 case CTL_DELAY_LOC_DATAMOVE:
2765 lun->delay_info.datamove_type = delay_info->delay_type;
2766 lun->delay_info.datamove_delay = delay_info->delay_secs;
2768 case CTL_DELAY_LOC_DONE:
2769 lun->delay_info.done_type = delay_info->delay_type;
2770 lun->delay_info.done_delay = delay_info->delay_secs;
2773 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2776 mtx_unlock(&lun->lun_lock);
2778 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2779 #endif /* CTL_IO_DELAY */
2782 #ifdef CTL_LEGACY_STATS
2783 case CTL_GETSTATS: {
2784 struct ctl_stats *stats = (struct ctl_stats *)addr;
2788 * XXX KDM no locking here. If the LUN list changes,
2789 * things can blow up.
2792 stats->status = CTL_SS_OK;
2793 stats->fill_len = 0;
2794 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2795 if (stats->fill_len + sizeof(lun->legacy_stats) >
2797 stats->status = CTL_SS_NEED_MORE_SPACE;
2800 retval = copyout(&lun->legacy_stats, &stats->lun_stats[i++],
2801 sizeof(lun->legacy_stats));
2804 stats->fill_len += sizeof(lun->legacy_stats);
2806 stats->num_luns = softc->num_luns;
2807 stats->flags = CTL_STATS_FLAG_NONE;
2809 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
2811 getnanouptime(&stats->timestamp);
2814 #endif /* CTL_LEGACY_STATS */
2815 case CTL_ERROR_INJECT: {
2816 struct ctl_error_desc *err_desc, *new_err_desc;
2818 err_desc = (struct ctl_error_desc *)addr;
2820 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2822 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2824 mtx_lock(&softc->ctl_lock);
2825 if (err_desc->lun_id >= CTL_MAX_LUNS ||
2826 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2827 mtx_unlock(&softc->ctl_lock);
2828 free(new_err_desc, M_CTL);
2829 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2830 __func__, (uintmax_t)err_desc->lun_id);
2834 mtx_lock(&lun->lun_lock);
2835 mtx_unlock(&softc->ctl_lock);
2838 * We could do some checking here to verify the validity
2839 * of the request, but given the complexity of error
2840 * injection requests, the checking logic would be fairly
2843 * For now, if the request is invalid, it just won't get
2844 * executed and might get deleted.
2846 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2849 * XXX KDM check to make sure the serial number is unique,
2850 * in case we somehow manage to wrap. That shouldn't
2851 * happen for a very long time, but it's the right thing to
2854 new_err_desc->serial = lun->error_serial;
2855 err_desc->serial = lun->error_serial;
2856 lun->error_serial++;
2858 mtx_unlock(&lun->lun_lock);
2861 case CTL_ERROR_INJECT_DELETE: {
2862 struct ctl_error_desc *delete_desc, *desc, *desc2;
2865 delete_desc = (struct ctl_error_desc *)addr;
2868 mtx_lock(&softc->ctl_lock);
2869 if (delete_desc->lun_id >= CTL_MAX_LUNS ||
2870 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2871 mtx_unlock(&softc->ctl_lock);
2872 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2873 __func__, (uintmax_t)delete_desc->lun_id);
2877 mtx_lock(&lun->lun_lock);
2878 mtx_unlock(&softc->ctl_lock);
2879 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2880 if (desc->serial != delete_desc->serial)
2883 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2888 mtx_unlock(&lun->lun_lock);
2889 if (delete_done == 0) {
2890 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2891 "error serial %ju on LUN %u\n", __func__,
2892 delete_desc->serial, delete_desc->lun_id);
2898 case CTL_DUMP_STRUCTS: {
2900 struct ctl_port *port;
2901 struct ctl_frontend *fe;
2903 mtx_lock(&softc->ctl_lock);
2904 printf("CTL Persistent Reservation information start:\n");
2905 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2906 mtx_lock(&lun->lun_lock);
2907 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2908 mtx_unlock(&lun->lun_lock);
2912 for (j = 0; j < CTL_MAX_PORTS; j++) {
2913 if (lun->pr_keys[j] == NULL)
2915 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2916 if (lun->pr_keys[j][k] == 0)
2918 printf(" LUN %ju port %d iid %d key "
2919 "%#jx\n", lun->lun, j, k,
2920 (uintmax_t)lun->pr_keys[j][k]);
2923 mtx_unlock(&lun->lun_lock);
2925 printf("CTL Persistent Reservation information end\n");
2926 printf("CTL Ports:\n");
2927 STAILQ_FOREACH(port, &softc->port_list, links) {
2928 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2929 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2930 port->frontend->name, port->port_type,
2931 port->physical_port, port->virtual_port,
2932 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2933 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2934 if (port->wwpn_iid[j].in_use == 0 &&
2935 port->wwpn_iid[j].wwpn == 0 &&
2936 port->wwpn_iid[j].name == NULL)
2939 printf(" iid %u use %d WWPN %#jx '%s'\n",
2940 j, port->wwpn_iid[j].in_use,
2941 (uintmax_t)port->wwpn_iid[j].wwpn,
2942 port->wwpn_iid[j].name);
2945 printf("CTL Port information end\n");
2946 mtx_unlock(&softc->ctl_lock);
2948 * XXX KDM calling this without a lock. We'd likely want
2949 * to drop the lock before calling the frontend's dump
2952 printf("CTL Frontends:\n");
2953 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2954 printf(" Frontend '%s'\n", fe->name);
2955 if (fe->fe_dump != NULL)
2958 printf("CTL Frontend information end\n");
2962 struct ctl_lun_req *lun_req;
2963 struct ctl_backend_driver *backend;
2965 lun_req = (struct ctl_lun_req *)addr;
2967 backend = ctl_backend_find(lun_req->backend);
2968 if (backend == NULL) {
2969 lun_req->status = CTL_LUN_ERROR;
2970 snprintf(lun_req->error_str,
2971 sizeof(lun_req->error_str),
2972 "Backend \"%s\" not found.",
2976 if (lun_req->num_be_args > 0) {
2977 lun_req->kern_be_args = ctl_copyin_args(
2978 lun_req->num_be_args,
2981 sizeof(lun_req->error_str));
2982 if (lun_req->kern_be_args == NULL) {
2983 lun_req->status = CTL_LUN_ERROR;
2988 retval = backend->ioctl(dev, cmd, addr, flag, td);
2990 if (lun_req->num_be_args > 0) {
2991 ctl_copyout_args(lun_req->num_be_args,
2992 lun_req->kern_be_args);
2993 ctl_free_args(lun_req->num_be_args,
2994 lun_req->kern_be_args);
2998 case CTL_LUN_LIST: {
3000 struct ctl_lun_list *list;
3001 struct ctl_option *opt;
3003 list = (struct ctl_lun_list *)addr;
3006 * Allocate a fixed length sbuf here, based on the length
3007 * of the user's buffer. We could allocate an auto-extending
3008 * buffer, and then tell the user how much larger our
3009 * amount of data is than his buffer, but that presents
3012 * 1. The sbuf(9) routines use a blocking malloc, and so
3013 * we can't hold a lock while calling them with an
3014 * auto-extending buffer.
3016 * 2. There is not currently a LUN reference counting
3017 * mechanism, outside of outstanding transactions on
3018 * the LUN's OOA queue. So a LUN could go away on us
3019 * while we're getting the LUN number, backend-specific
3020 * information, etc. Thus, given the way things
3021 * currently work, we need to hold the CTL lock while
3022 * grabbing LUN information.
3024 * So, from the user's standpoint, the best thing to do is
3025 * allocate what he thinks is a reasonable buffer length,
3026 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3027 * double the buffer length and try again. (And repeat
3028 * that until he succeeds.)
3030 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3032 list->status = CTL_LUN_LIST_ERROR;
3033 snprintf(list->error_str, sizeof(list->error_str),
3034 "Unable to allocate %d bytes for LUN list",
3039 sbuf_printf(sb, "<ctllunlist>\n");
3041 mtx_lock(&softc->ctl_lock);
3042 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3043 mtx_lock(&lun->lun_lock);
3044 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3045 (uintmax_t)lun->lun);
3048 * Bail out as soon as we see that we've overfilled
3054 retval = sbuf_printf(sb, "\t<backend_type>%s"
3055 "</backend_type>\n",
3056 (lun->backend == NULL) ? "none" :
3057 lun->backend->name);
3062 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3063 lun->be_lun->lun_type);
3068 if (lun->backend == NULL) {
3069 retval = sbuf_printf(sb, "</lun>\n");
3075 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3076 (lun->be_lun->maxlba > 0) ?
3077 lun->be_lun->maxlba + 1 : 0);
3082 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3083 lun->be_lun->blocksize);
3088 retval = sbuf_printf(sb, "\t<serial_number>");
3093 retval = ctl_sbuf_printf_esc(sb,
3094 lun->be_lun->serial_num,
3095 sizeof(lun->be_lun->serial_num));
3100 retval = sbuf_printf(sb, "</serial_number>\n");
3105 retval = sbuf_printf(sb, "\t<device_id>");
3110 retval = ctl_sbuf_printf_esc(sb,
3111 lun->be_lun->device_id,
3112 sizeof(lun->be_lun->device_id));
3117 retval = sbuf_printf(sb, "</device_id>\n");
3122 if (lun->backend->lun_info != NULL) {
3123 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3127 STAILQ_FOREACH(opt, &lun->be_lun->options, links) {
3128 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3129 opt->name, opt->value, opt->name);
3134 retval = sbuf_printf(sb, "</lun>\n");
3138 mtx_unlock(&lun->lun_lock);
3141 mtx_unlock(&lun->lun_lock);
3142 mtx_unlock(&softc->ctl_lock);
3145 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3148 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3149 snprintf(list->error_str, sizeof(list->error_str),
3150 "Out of space, %d bytes is too small",
3157 retval = copyout(sbuf_data(sb), list->lun_xml,
3160 list->fill_len = sbuf_len(sb) + 1;
3161 list->status = CTL_LUN_LIST_OK;
3166 struct ctl_iscsi *ci;
3167 struct ctl_frontend *fe;
3169 ci = (struct ctl_iscsi *)addr;
3171 fe = ctl_frontend_find("iscsi");
3173 ci->status = CTL_ISCSI_ERROR;
3174 snprintf(ci->error_str, sizeof(ci->error_str),
3175 "Frontend \"iscsi\" not found.");
3179 retval = fe->ioctl(dev, cmd, addr, flag, td);
3182 case CTL_PORT_REQ: {
3183 struct ctl_req *req;
3184 struct ctl_frontend *fe;
3186 req = (struct ctl_req *)addr;
3188 fe = ctl_frontend_find(req->driver);
3190 req->status = CTL_LUN_ERROR;
3191 snprintf(req->error_str, sizeof(req->error_str),
3192 "Frontend \"%s\" not found.", req->driver);
3195 if (req->num_args > 0) {
3196 req->kern_args = ctl_copyin_args(req->num_args,
3197 req->args, req->error_str, sizeof(req->error_str));
3198 if (req->kern_args == NULL) {
3199 req->status = CTL_LUN_ERROR;
3205 retval = fe->ioctl(dev, cmd, addr, flag, td);
3209 if (req->num_args > 0) {
3210 ctl_copyout_args(req->num_args, req->kern_args);
3211 ctl_free_args(req->num_args, req->kern_args);
3215 case CTL_PORT_LIST: {
3217 struct ctl_port *port;
3218 struct ctl_lun_list *list;
3219 struct ctl_option *opt;
3223 list = (struct ctl_lun_list *)addr;
3225 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3227 list->status = CTL_LUN_LIST_ERROR;
3228 snprintf(list->error_str, sizeof(list->error_str),
3229 "Unable to allocate %d bytes for LUN list",
3234 sbuf_printf(sb, "<ctlportlist>\n");
3236 mtx_lock(&softc->ctl_lock);
3237 STAILQ_FOREACH(port, &softc->port_list, links) {
3238 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3239 (uintmax_t)port->targ_port);
3242 * Bail out as soon as we see that we've overfilled
3248 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3249 "</frontend_type>\n", port->frontend->name);
3253 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3258 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3259 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3263 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3268 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3269 port->physical_port);
3273 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3274 port->virtual_port);
3278 if (port->target_devid != NULL) {
3279 sbuf_printf(sb, "\t<target>");
3280 ctl_id_sbuf(port->target_devid, sb);
3281 sbuf_printf(sb, "</target>\n");
3284 if (port->port_devid != NULL) {
3285 sbuf_printf(sb, "\t<port>");
3286 ctl_id_sbuf(port->port_devid, sb);
3287 sbuf_printf(sb, "</port>\n");
3290 if (port->port_info != NULL) {
3291 retval = port->port_info(port->onoff_arg, sb);
3295 STAILQ_FOREACH(opt, &port->options, links) {
3296 retval = sbuf_printf(sb, "\t<%s>%s</%s>\n",
3297 opt->name, opt->value, opt->name);
3302 if (port->lun_map != NULL) {
3303 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3304 for (j = 0; j < port->lun_map_size; j++) {
3305 plun = ctl_lun_map_from_port(port, j);
3306 if (plun == UINT32_MAX)
3309 "\t<lun id=\"%u\">%u</lun>\n",
3314 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3315 if (port->wwpn_iid[j].in_use == 0 ||
3316 (port->wwpn_iid[j].wwpn == 0 &&
3317 port->wwpn_iid[j].name == NULL))
3320 if (port->wwpn_iid[j].name != NULL)
3321 retval = sbuf_printf(sb,
3322 "\t<initiator id=\"%u\">%s</initiator>\n",
3323 j, port->wwpn_iid[j].name);
3325 retval = sbuf_printf(sb,
3326 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3327 j, port->wwpn_iid[j].wwpn);
3334 retval = sbuf_printf(sb, "</targ_port>\n");
3338 mtx_unlock(&softc->ctl_lock);
3341 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3344 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3345 snprintf(list->error_str, sizeof(list->error_str),
3346 "Out of space, %d bytes is too small",
3353 retval = copyout(sbuf_data(sb), list->lun_xml,
3356 list->fill_len = sbuf_len(sb) + 1;
3357 list->status = CTL_LUN_LIST_OK;
3362 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3363 struct ctl_port *port;
3365 mtx_lock(&softc->ctl_lock);
3366 if (lm->port < softc->port_min ||
3367 lm->port >= softc->port_max ||
3368 (port = softc->ctl_ports[lm->port]) == NULL) {
3369 mtx_unlock(&softc->ctl_lock);
3372 if (port->status & CTL_PORT_STATUS_ONLINE) {
3373 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3374 if (ctl_lun_map_to_port(port, lun->lun) ==
3377 mtx_lock(&lun->lun_lock);
3378 ctl_est_ua_port(lun, lm->port, -1,
3380 mtx_unlock(&lun->lun_lock);
3383 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3384 if (lm->plun != UINT32_MAX) {
3385 if (lm->lun == UINT32_MAX)
3386 retval = ctl_lun_map_unset(port, lm->plun);
3387 else if (lm->lun < CTL_MAX_LUNS &&
3388 softc->ctl_luns[lm->lun] != NULL)
3389 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3393 if (lm->lun == UINT32_MAX)
3394 retval = ctl_lun_map_deinit(port);
3396 retval = ctl_lun_map_init(port);
3398 if (port->status & CTL_PORT_STATUS_ONLINE)
3399 ctl_isc_announce_port(port);
3402 case CTL_GET_LUN_STATS: {
3403 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3407 * XXX KDM no locking here. If the LUN list changes,
3408 * things can blow up.
3411 stats->status = CTL_SS_OK;
3412 stats->fill_len = 0;
3413 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3414 if (lun->lun < stats->first_item)
3416 if (stats->fill_len + sizeof(lun->stats) >
3418 stats->status = CTL_SS_NEED_MORE_SPACE;
3421 retval = copyout(&lun->stats, &stats->stats[i++],
3422 sizeof(lun->stats));
3425 stats->fill_len += sizeof(lun->stats);
3427 stats->num_items = softc->num_luns;
3428 stats->flags = CTL_STATS_FLAG_NONE;
3430 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3432 getnanouptime(&stats->timestamp);
3435 case CTL_GET_PORT_STATS: {
3436 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3440 * XXX KDM no locking here. If the LUN list changes,
3441 * things can blow up.
3444 stats->status = CTL_SS_OK;
3445 stats->fill_len = 0;
3446 STAILQ_FOREACH(port, &softc->port_list, links) {
3447 if (port->targ_port < stats->first_item)
3449 if (stats->fill_len + sizeof(port->stats) >
3451 stats->status = CTL_SS_NEED_MORE_SPACE;
3454 retval = copyout(&port->stats, &stats->stats[i++],
3455 sizeof(port->stats));
3458 stats->fill_len += sizeof(port->stats);
3460 stats->num_items = softc->num_ports;
3461 stats->flags = CTL_STATS_FLAG_NONE;
3463 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3465 getnanouptime(&stats->timestamp);
3469 /* XXX KDM should we fix this? */
3471 struct ctl_backend_driver *backend;
3478 * We encode the backend type as the ioctl type for backend
3479 * ioctls. So parse it out here, and then search for a
3480 * backend of this type.
3482 type = _IOC_TYPE(cmd);
3484 STAILQ_FOREACH(backend, &softc->be_list, links) {
3485 if (backend->type == type) {
3491 printf("ctl: unknown ioctl command %#lx or backend "
3496 retval = backend->ioctl(dev, cmd, addr, flag, td);
3506 ctl_get_initindex(struct ctl_nexus *nexus)
3508 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3512 ctl_lun_map_init(struct ctl_port *port)
3514 struct ctl_softc *softc = port->ctl_softc;
3515 struct ctl_lun *lun;
3516 int size = ctl_lun_map_size;
3519 if (port->lun_map == NULL || port->lun_map_size < size) {
3520 port->lun_map_size = 0;
3521 free(port->lun_map, M_CTL);
3522 port->lun_map = malloc(size * sizeof(uint32_t),
3525 if (port->lun_map == NULL)
3527 for (i = 0; i < size; i++)
3528 port->lun_map[i] = UINT32_MAX;
3529 port->lun_map_size = size;
3530 if (port->status & CTL_PORT_STATUS_ONLINE) {
3531 if (port->lun_disable != NULL) {
3532 STAILQ_FOREACH(lun, &softc->lun_list, links)
3533 port->lun_disable(port->targ_lun_arg, lun->lun);
3535 ctl_isc_announce_port(port);
3541 ctl_lun_map_deinit(struct ctl_port *port)
3543 struct ctl_softc *softc = port->ctl_softc;
3544 struct ctl_lun *lun;
3546 if (port->lun_map == NULL)
3548 port->lun_map_size = 0;
3549 free(port->lun_map, M_CTL);
3550 port->lun_map = NULL;
3551 if (port->status & CTL_PORT_STATUS_ONLINE) {
3552 if (port->lun_enable != NULL) {
3553 STAILQ_FOREACH(lun, &softc->lun_list, links)
3554 port->lun_enable(port->targ_lun_arg, lun->lun);
3556 ctl_isc_announce_port(port);
3562 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3567 if (port->lun_map == NULL) {
3568 status = ctl_lun_map_init(port);
3572 if (plun >= port->lun_map_size)
3574 old = port->lun_map[plun];
3575 port->lun_map[plun] = glun;
3576 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3577 if (port->lun_enable != NULL)
3578 port->lun_enable(port->targ_lun_arg, plun);
3579 ctl_isc_announce_port(port);
3585 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3589 if (port->lun_map == NULL || plun >= port->lun_map_size)
3591 old = port->lun_map[plun];
3592 port->lun_map[plun] = UINT32_MAX;
3593 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3594 if (port->lun_disable != NULL)
3595 port->lun_disable(port->targ_lun_arg, plun);
3596 ctl_isc_announce_port(port);
3602 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3606 return (UINT32_MAX);
3607 if (port->lun_map == NULL)
3609 if (lun_id > port->lun_map_size)
3610 return (UINT32_MAX);
3611 return (port->lun_map[lun_id]);
3615 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3620 return (UINT32_MAX);
3621 if (port->lun_map == NULL)
3623 for (i = 0; i < port->lun_map_size; i++) {
3624 if (port->lun_map[i] == lun_id)
3627 return (UINT32_MAX);
3631 ctl_decode_lun(uint64_t encoded)
3634 uint32_t result = 0xffffffff;
3636 be64enc(lun, encoded);
3637 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3638 case RPL_LUNDATA_ATYP_PERIPH:
3639 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3640 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3643 case RPL_LUNDATA_ATYP_FLAT:
3644 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3645 lun[6] == 0 && lun[7] == 0)
3646 result = ((lun[0] & 0x3f) << 8) + lun[1];
3648 case RPL_LUNDATA_ATYP_EXTLUN:
3649 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3651 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3656 result = (lun[1] << 16) + (lun[2] << 8) +
3660 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3661 result = (lun[2] << 24) +
3662 (lun[3] << 16) + (lun[4] << 8) +
3667 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3668 result = 0xffffffff;
3677 ctl_encode_lun(uint32_t decoded)
3679 uint64_t l = decoded;
3682 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3684 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3686 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3688 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3692 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3696 for (i = first; i < last; i++) {
3697 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3704 ctl_set_mask(uint32_t *mask, uint32_t bit)
3706 uint32_t chunk, piece;
3709 piece = bit % (sizeof(uint32_t) * 8);
3711 if ((mask[chunk] & (1 << piece)) != 0)
3714 mask[chunk] |= (1 << piece);
3720 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3722 uint32_t chunk, piece;
3725 piece = bit % (sizeof(uint32_t) * 8);
3727 if ((mask[chunk] & (1 << piece)) == 0)
3730 mask[chunk] &= ~(1 << piece);
3736 ctl_is_set(uint32_t *mask, uint32_t bit)
3738 uint32_t chunk, piece;
3741 piece = bit % (sizeof(uint32_t) * 8);
3743 if ((mask[chunk] & (1 << piece)) == 0)
3750 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3754 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3757 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3761 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3765 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3768 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3772 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3777 i = residx/CTL_MAX_INIT_PER_PORT;
3778 if (lun->pr_keys[i] != NULL)
3780 mtx_unlock(&lun->lun_lock);
3781 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3783 mtx_lock(&lun->lun_lock);
3784 if (lun->pr_keys[i] == NULL)
3785 lun->pr_keys[i] = p;
3791 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3795 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3796 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3797 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3801 * ctl_softc, pool_name, total_ctl_io are passed in.
3802 * npool is passed out.
3805 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3806 uint32_t total_ctl_io, void **npool)
3808 struct ctl_io_pool *pool;
3810 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3815 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3816 pool->ctl_softc = ctl_softc;
3818 pool->zone = uma_zsecond_create(pool->name, NULL,
3819 NULL, NULL, NULL, ctl_softc->io_zone);
3820 /* uma_prealloc(pool->zone, total_ctl_io); */
3822 pool->zone = ctl_softc->io_zone;
3830 ctl_pool_free(struct ctl_io_pool *pool)
3837 uma_zdestroy(pool->zone);
3843 ctl_alloc_io(void *pool_ref)
3845 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3848 io = uma_zalloc(pool->zone, M_WAITOK);
3850 io->io_hdr.pool = pool_ref;
3851 CTL_SOFTC(io) = pool->ctl_softc;
3857 ctl_alloc_io_nowait(void *pool_ref)
3859 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3862 io = uma_zalloc(pool->zone, M_NOWAIT);
3864 io->io_hdr.pool = pool_ref;
3865 CTL_SOFTC(io) = pool->ctl_softc;
3871 ctl_free_io(union ctl_io *io)
3873 struct ctl_io_pool *pool;
3878 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3879 uma_zfree(pool->zone, io);
3883 ctl_zero_io(union ctl_io *io)
3885 struct ctl_io_pool *pool;
3891 * May need to preserve linked list pointers at some point too.
3893 pool = io->io_hdr.pool;
3894 memset(io, 0, sizeof(*io));
3895 io->io_hdr.pool = pool;
3896 CTL_SOFTC(io) = pool->ctl_softc;
3900 ctl_expand_number(const char *buf, uint64_t *num)
3906 number = strtoq(buf, &endptr, 0);
3908 switch (tolower((unsigned char)*endptr)) {
3928 case '\0': /* No unit. */
3932 /* Unrecognized unit. */
3936 if ((number << shift) >> shift != number) {
3940 *num = number << shift;
3946 * This routine could be used in the future to load default and/or saved
3947 * mode page parameters for a particuar lun.
3950 ctl_init_page_index(struct ctl_lun *lun)
3953 struct ctl_page_index *page_index;
3957 memcpy(&lun->mode_pages.index, page_index_template,
3958 sizeof(page_index_template));
3960 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
3962 page_index = &lun->mode_pages.index[i];
3963 if (lun->be_lun->lun_type == T_DIRECT &&
3964 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
3966 if (lun->be_lun->lun_type == T_PROCESSOR &&
3967 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
3969 if (lun->be_lun->lun_type == T_CDROM &&
3970 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
3973 page_code = page_index->page_code & SMPH_PC_MASK;
3974 switch (page_code) {
3975 case SMS_RW_ERROR_RECOVERY_PAGE: {
3976 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3977 ("subpage %#x for page %#x is incorrect!",
3978 page_index->subpage, page_code));
3979 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
3980 &rw_er_page_default,
3981 sizeof(rw_er_page_default));
3982 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
3983 &rw_er_page_changeable,
3984 sizeof(rw_er_page_changeable));
3985 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
3986 &rw_er_page_default,
3987 sizeof(rw_er_page_default));
3988 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
3989 &rw_er_page_default,
3990 sizeof(rw_er_page_default));
3991 page_index->page_data =
3992 (uint8_t *)lun->mode_pages.rw_er_page;
3995 case SMS_FORMAT_DEVICE_PAGE: {
3996 struct scsi_format_page *format_page;
3998 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
3999 ("subpage %#x for page %#x is incorrect!",
4000 page_index->subpage, page_code));
4003 * Sectors per track are set above. Bytes per
4004 * sector need to be set here on a per-LUN basis.
4006 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4007 &format_page_default,
4008 sizeof(format_page_default));
4009 memcpy(&lun->mode_pages.format_page[
4010 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4011 sizeof(format_page_changeable));
4012 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4013 &format_page_default,
4014 sizeof(format_page_default));
4015 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4016 &format_page_default,
4017 sizeof(format_page_default));
4019 format_page = &lun->mode_pages.format_page[
4021 scsi_ulto2b(lun->be_lun->blocksize,
4022 format_page->bytes_per_sector);
4024 format_page = &lun->mode_pages.format_page[
4026 scsi_ulto2b(lun->be_lun->blocksize,
4027 format_page->bytes_per_sector);
4029 format_page = &lun->mode_pages.format_page[
4031 scsi_ulto2b(lun->be_lun->blocksize,
4032 format_page->bytes_per_sector);
4034 page_index->page_data =
4035 (uint8_t *)lun->mode_pages.format_page;
4038 case SMS_RIGID_DISK_PAGE: {
4039 struct scsi_rigid_disk_page *rigid_disk_page;
4040 uint32_t sectors_per_cylinder;
4044 #endif /* !__XSCALE__ */
4046 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4047 ("subpage %#x for page %#x is incorrect!",
4048 page_index->subpage, page_code));
4051 * Rotation rate and sectors per track are set
4052 * above. We calculate the cylinders here based on
4053 * capacity. Due to the number of heads and
4054 * sectors per track we're using, smaller arrays
4055 * may turn out to have 0 cylinders. Linux and
4056 * FreeBSD don't pay attention to these mode pages
4057 * to figure out capacity, but Solaris does. It
4058 * seems to deal with 0 cylinders just fine, and
4059 * works out a fake geometry based on the capacity.
4061 memcpy(&lun->mode_pages.rigid_disk_page[
4062 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4063 sizeof(rigid_disk_page_default));
4064 memcpy(&lun->mode_pages.rigid_disk_page[
4065 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4066 sizeof(rigid_disk_page_changeable));
4068 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4072 * The divide method here will be more accurate,
4073 * probably, but results in floating point being
4074 * used in the kernel on i386 (__udivdi3()). On the
4075 * XScale, though, __udivdi3() is implemented in
4078 * The shift method for cylinder calculation is
4079 * accurate if sectors_per_cylinder is a power of
4080 * 2. Otherwise it might be slightly off -- you
4081 * might have a bit of a truncation problem.
4084 cylinders = (lun->be_lun->maxlba + 1) /
4085 sectors_per_cylinder;
4087 for (shift = 31; shift > 0; shift--) {
4088 if (sectors_per_cylinder & (1 << shift))
4091 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4095 * We've basically got 3 bytes, or 24 bits for the
4096 * cylinder size in the mode page. If we're over,
4097 * just round down to 2^24.
4099 if (cylinders > 0xffffff)
4100 cylinders = 0xffffff;
4102 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4104 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4106 if ((value = ctl_get_opt(&lun->be_lun->options,
4108 scsi_ulto2b(strtol(value, NULL, 0),
4109 rigid_disk_page->rotation_rate);
4112 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4113 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4114 sizeof(rigid_disk_page_default));
4115 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4116 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4117 sizeof(rigid_disk_page_default));
4119 page_index->page_data =
4120 (uint8_t *)lun->mode_pages.rigid_disk_page;
4123 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4124 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4125 ("subpage %#x for page %#x is incorrect!",
4126 page_index->subpage, page_code));
4127 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4128 &verify_er_page_default,
4129 sizeof(verify_er_page_default));
4130 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4131 &verify_er_page_changeable,
4132 sizeof(verify_er_page_changeable));
4133 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4134 &verify_er_page_default,
4135 sizeof(verify_er_page_default));
4136 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4137 &verify_er_page_default,
4138 sizeof(verify_er_page_default));
4139 page_index->page_data =
4140 (uint8_t *)lun->mode_pages.verify_er_page;
4143 case SMS_CACHING_PAGE: {
4144 struct scsi_caching_page *caching_page;
4146 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4147 ("subpage %#x for page %#x is incorrect!",
4148 page_index->subpage, page_code));
4149 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4150 &caching_page_default,
4151 sizeof(caching_page_default));
4152 memcpy(&lun->mode_pages.caching_page[
4153 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4154 sizeof(caching_page_changeable));
4155 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4156 &caching_page_default,
4157 sizeof(caching_page_default));
4158 caching_page = &lun->mode_pages.caching_page[
4160 value = ctl_get_opt(&lun->be_lun->options, "writecache");
4161 if (value != NULL && strcmp(value, "off") == 0)
4162 caching_page->flags1 &= ~SCP_WCE;
4163 value = ctl_get_opt(&lun->be_lun->options, "readcache");
4164 if (value != NULL && strcmp(value, "off") == 0)
4165 caching_page->flags1 |= SCP_RCD;
4166 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4167 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4168 sizeof(caching_page_default));
4169 page_index->page_data =
4170 (uint8_t *)lun->mode_pages.caching_page;
4173 case SMS_CONTROL_MODE_PAGE: {
4174 switch (page_index->subpage) {
4175 case SMS_SUBPAGE_PAGE_0: {
4176 struct scsi_control_page *control_page;
4178 memcpy(&lun->mode_pages.control_page[
4180 &control_page_default,
4181 sizeof(control_page_default));
4182 memcpy(&lun->mode_pages.control_page[
4183 CTL_PAGE_CHANGEABLE],
4184 &control_page_changeable,
4185 sizeof(control_page_changeable));
4186 memcpy(&lun->mode_pages.control_page[
4188 &control_page_default,
4189 sizeof(control_page_default));
4190 control_page = &lun->mode_pages.control_page[
4192 value = ctl_get_opt(&lun->be_lun->options,
4194 if (value != NULL &&
4195 strcmp(value, "unrestricted") == 0) {
4196 control_page->queue_flags &=
4197 ~SCP_QUEUE_ALG_MASK;
4198 control_page->queue_flags |=
4199 SCP_QUEUE_ALG_UNRESTRICTED;
4201 memcpy(&lun->mode_pages.control_page[
4203 &lun->mode_pages.control_page[
4205 sizeof(control_page_default));
4206 page_index->page_data =
4207 (uint8_t *)lun->mode_pages.control_page;
4211 memcpy(&lun->mode_pages.control_ext_page[
4213 &control_ext_page_default,
4214 sizeof(control_ext_page_default));
4215 memcpy(&lun->mode_pages.control_ext_page[
4216 CTL_PAGE_CHANGEABLE],
4217 &control_ext_page_changeable,
4218 sizeof(control_ext_page_changeable));
4219 memcpy(&lun->mode_pages.control_ext_page[
4221 &control_ext_page_default,
4222 sizeof(control_ext_page_default));
4223 memcpy(&lun->mode_pages.control_ext_page[
4225 &lun->mode_pages.control_ext_page[
4227 sizeof(control_ext_page_default));
4228 page_index->page_data =
4229 (uint8_t *)lun->mode_pages.control_ext_page;
4232 panic("subpage %#x for page %#x is incorrect!",
4233 page_index->subpage, page_code);
4237 case SMS_INFO_EXCEPTIONS_PAGE: {
4238 switch (page_index->subpage) {
4239 case SMS_SUBPAGE_PAGE_0:
4240 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4242 sizeof(ie_page_default));
4243 memcpy(&lun->mode_pages.ie_page[
4244 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4245 sizeof(ie_page_changeable));
4246 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4248 sizeof(ie_page_default));
4249 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4251 sizeof(ie_page_default));
4252 page_index->page_data =
4253 (uint8_t *)lun->mode_pages.ie_page;
4256 struct ctl_logical_block_provisioning_page *page;
4258 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4260 sizeof(lbp_page_default));
4261 memcpy(&lun->mode_pages.lbp_page[
4262 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4263 sizeof(lbp_page_changeable));
4264 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4266 sizeof(lbp_page_default));
4267 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4268 value = ctl_get_opt(&lun->be_lun->options,
4270 if (value != NULL &&
4271 ctl_expand_number(value, &ival) == 0) {
4272 page->descr[0].flags |= SLBPPD_ENABLED |
4274 if (lun->be_lun->blocksize)
4275 ival /= lun->be_lun->blocksize;
4278 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4279 page->descr[0].count);
4281 value = ctl_get_opt(&lun->be_lun->options,
4283 if (value != NULL &&
4284 ctl_expand_number(value, &ival) == 0) {
4285 page->descr[1].flags |= SLBPPD_ENABLED |
4287 if (lun->be_lun->blocksize)
4288 ival /= lun->be_lun->blocksize;
4291 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4292 page->descr[1].count);
4294 value = ctl_get_opt(&lun->be_lun->options,
4295 "pool-avail-threshold");
4296 if (value != NULL &&
4297 ctl_expand_number(value, &ival) == 0) {
4298 page->descr[2].flags |= SLBPPD_ENABLED |
4300 if (lun->be_lun->blocksize)
4301 ival /= lun->be_lun->blocksize;
4304 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4305 page->descr[2].count);
4307 value = ctl_get_opt(&lun->be_lun->options,
4308 "pool-used-threshold");
4309 if (value != NULL &&
4310 ctl_expand_number(value, &ival) == 0) {
4311 page->descr[3].flags |= SLBPPD_ENABLED |
4313 if (lun->be_lun->blocksize)
4314 ival /= lun->be_lun->blocksize;
4317 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4318 page->descr[3].count);
4320 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4321 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4322 sizeof(lbp_page_default));
4323 page_index->page_data =
4324 (uint8_t *)lun->mode_pages.lbp_page;
4328 panic("subpage %#x for page %#x is incorrect!",
4329 page_index->subpage, page_code);
4333 case SMS_CDDVD_CAPS_PAGE:{
4334 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4335 ("subpage %#x for page %#x is incorrect!",
4336 page_index->subpage, page_code));
4337 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4338 &cddvd_page_default,
4339 sizeof(cddvd_page_default));
4340 memcpy(&lun->mode_pages.cddvd_page[
4341 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4342 sizeof(cddvd_page_changeable));
4343 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4344 &cddvd_page_default,
4345 sizeof(cddvd_page_default));
4346 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4347 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4348 sizeof(cddvd_page_default));
4349 page_index->page_data =
4350 (uint8_t *)lun->mode_pages.cddvd_page;
4354 panic("invalid page code value %#x", page_code);
4358 return (CTL_RETVAL_COMPLETE);
4362 ctl_init_log_page_index(struct ctl_lun *lun)
4364 struct ctl_page_index *page_index;
4367 memcpy(&lun->log_pages.index, log_page_index_template,
4368 sizeof(log_page_index_template));
4371 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4373 page_index = &lun->log_pages.index[i];
4374 if (lun->be_lun->lun_type == T_DIRECT &&
4375 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4377 if (lun->be_lun->lun_type == T_PROCESSOR &&
4378 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4380 if (lun->be_lun->lun_type == T_CDROM &&
4381 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4384 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4385 lun->backend->lun_attr == NULL)
4388 if (page_index->page_code != prev) {
4389 lun->log_pages.pages_page[j] = page_index->page_code;
4390 prev = page_index->page_code;
4393 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4394 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4397 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4398 lun->log_pages.index[0].page_len = j;
4399 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4400 lun->log_pages.index[1].page_len = k * 2;
4401 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4402 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4403 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4404 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4405 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4406 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4408 return (CTL_RETVAL_COMPLETE);
4412 hex2bin(const char *str, uint8_t *buf, int buf_size)
4417 memset(buf, 0, buf_size);
4418 while (isspace(str[0]))
4420 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4423 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4424 while (str[i] == '-') /* Skip dashes in UUIDs. */
4429 else if (isalpha(c))
4430 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4436 buf[i / 2] |= (c << 4);
4440 return ((i + 1) / 2);
4447 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4448 * wants us to allocate the LUN and he can block.
4449 * - ctl_softc is always set
4450 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4452 * Returns 0 for success, non-zero (errno) for failure.
4455 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4456 struct ctl_be_lun *const be_lun)
4458 struct ctl_lun *nlun, *lun;
4459 struct scsi_vpd_id_descriptor *desc;
4460 struct scsi_vpd_id_t10 *t10id;
4461 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4462 int lun_number, lun_malloced;
4463 int devidlen, idlen1, idlen2 = 0, len;
4469 * We currently only support Direct Access or Processor LUN types.
4471 switch (be_lun->lun_type) {
4479 be_lun->lun_config_status(be_lun->be_lun,
4480 CTL_LUN_CONFIG_FAILURE);
4483 if (ctl_lun == NULL) {
4484 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4491 memset(lun, 0, sizeof(*lun));
4493 lun->flags = CTL_LUN_MALLOCED;
4495 /* Generate LUN ID. */
4496 devidlen = max(CTL_DEVID_MIN_LEN,
4497 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4498 idlen1 = sizeof(*t10id) + devidlen;
4499 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4500 scsiname = ctl_get_opt(&be_lun->options, "scsiname");
4501 if (scsiname != NULL) {
4502 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4503 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4505 eui = ctl_get_opt(&be_lun->options, "eui");
4507 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4509 naa = ctl_get_opt(&be_lun->options, "naa");
4511 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4513 uuid = ctl_get_opt(&be_lun->options, "uuid");
4515 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4517 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4518 M_CTL, M_WAITOK | M_ZERO);
4519 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4520 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4521 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4522 desc->length = idlen1;
4523 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4524 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4525 if ((vendor = ctl_get_opt(&be_lun->options, "vendor")) == NULL) {
4526 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4528 strncpy(t10id->vendor, vendor,
4529 min(sizeof(t10id->vendor), strlen(vendor)));
4531 strncpy((char *)t10id->vendor_spec_id,
4532 (char *)be_lun->device_id, devidlen);
4533 if (scsiname != NULL) {
4534 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4536 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4537 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4538 SVPD_ID_TYPE_SCSI_NAME;
4539 desc->length = idlen2;
4540 strlcpy(desc->identifier, scsiname, idlen2);
4543 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4545 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4546 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4548 desc->length = hex2bin(eui, desc->identifier, 16);
4549 desc->length = desc->length > 12 ? 16 :
4550 (desc->length > 8 ? 12 : 8);
4551 len -= 16 - desc->length;
4554 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4556 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4557 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4559 desc->length = hex2bin(naa, desc->identifier, 16);
4560 desc->length = desc->length > 8 ? 16 : 8;
4561 len -= 16 - desc->length;
4564 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4566 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4567 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4569 desc->identifier[0] = 0x10;
4570 hex2bin(uuid, &desc->identifier[2], 16);
4573 lun->lun_devid->len = len;
4575 mtx_lock(&ctl_softc->ctl_lock);
4577 * See if the caller requested a particular LUN number. If so, see
4578 * if it is available. Otherwise, allocate the first available LUN.
4580 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4581 if ((be_lun->req_lun_id > (CTL_MAX_LUNS - 1))
4582 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4583 mtx_unlock(&ctl_softc->ctl_lock);
4584 if (be_lun->req_lun_id > (CTL_MAX_LUNS - 1)) {
4585 printf("ctl: requested LUN ID %d is higher "
4586 "than CTL_MAX_LUNS - 1 (%d)\n",
4587 be_lun->req_lun_id, CTL_MAX_LUNS - 1);
4590 * XXX KDM return an error, or just assign
4591 * another LUN ID in this case??
4593 printf("ctl: requested LUN ID %d is already "
4594 "in use\n", be_lun->req_lun_id);
4597 free(lun->lun_devid, M_CTL);
4598 if (lun->flags & CTL_LUN_MALLOCED)
4600 be_lun->lun_config_status(be_lun->be_lun,
4601 CTL_LUN_CONFIG_FAILURE);
4604 lun_number = be_lun->req_lun_id;
4606 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, CTL_MAX_LUNS);
4607 if (lun_number == -1) {
4608 mtx_unlock(&ctl_softc->ctl_lock);
4609 printf("ctl: can't allocate LUN, out of LUNs\n");
4613 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4614 mtx_unlock(&ctl_softc->ctl_lock);
4616 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4617 lun->lun = lun_number;
4618 lun->be_lun = be_lun;
4620 * The processor LUN is always enabled. Disk LUNs come on line
4621 * disabled, and must be enabled by the backend.
4623 lun->flags |= CTL_LUN_DISABLED;
4624 lun->backend = be_lun->be;
4625 be_lun->ctl_lun = lun;
4626 be_lun->lun_id = lun_number;
4627 atomic_add_int(&be_lun->be->num_luns, 1);
4628 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4629 lun->flags |= CTL_LUN_EJECTED;
4630 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4631 lun->flags |= CTL_LUN_NO_MEDIA;
4632 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4633 lun->flags |= CTL_LUN_STOPPED;
4635 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4636 lun->flags |= CTL_LUN_PRIMARY_SC;
4638 value = ctl_get_opt(&be_lun->options, "removable");
4639 if (value != NULL) {
4640 if (strcmp(value, "on") == 0)
4641 lun->flags |= CTL_LUN_REMOVABLE;
4642 } else if (be_lun->lun_type == T_CDROM)
4643 lun->flags |= CTL_LUN_REMOVABLE;
4645 lun->ctl_softc = ctl_softc;
4647 lun->last_busy = getsbinuptime();
4649 TAILQ_INIT(&lun->ooa_queue);
4650 TAILQ_INIT(&lun->blocked_queue);
4651 STAILQ_INIT(&lun->error_list);
4652 lun->ie_reported = 1;
4653 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4654 ctl_tpc_lun_init(lun);
4655 if (lun->flags & CTL_LUN_REMOVABLE) {
4656 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4661 * Initialize the mode and log page index.
4663 ctl_init_page_index(lun);
4664 ctl_init_log_page_index(lun);
4666 /* Setup statistics gathering */
4667 #ifdef CTL_LEGACY_STATS
4668 lun->legacy_stats.device_type = be_lun->lun_type;
4669 lun->legacy_stats.lun_number = lun_number;
4670 lun->legacy_stats.blocksize = be_lun->blocksize;
4671 if (be_lun->blocksize == 0)
4672 lun->legacy_stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4673 for (len = 0; len < CTL_MAX_PORTS; len++)
4674 lun->legacy_stats.ports[len].targ_port = len;
4675 #endif /* CTL_LEGACY_STATS */
4676 lun->stats.item = lun_number;
4679 * Now, before we insert this lun on the lun list, set the lun
4680 * inventory changed UA for all other luns.
4682 mtx_lock(&ctl_softc->ctl_lock);
4683 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4684 mtx_lock(&nlun->lun_lock);
4685 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4686 mtx_unlock(&nlun->lun_lock);
4688 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4689 ctl_softc->ctl_luns[lun_number] = lun;
4690 ctl_softc->num_luns++;
4691 mtx_unlock(&ctl_softc->ctl_lock);
4693 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4700 * - LUN has already been marked invalid and any pending I/O has been taken
4704 ctl_free_lun(struct ctl_lun *lun)
4706 struct ctl_softc *softc = lun->ctl_softc;
4707 struct ctl_lun *nlun;
4710 mtx_assert(&softc->ctl_lock, MA_OWNED);
4712 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4714 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4716 softc->ctl_luns[lun->lun] = NULL;
4718 if (!TAILQ_EMPTY(&lun->ooa_queue))
4719 panic("Freeing a LUN %p with outstanding I/O!!\n", lun);
4724 * Tell the backend to free resources, if this LUN has a backend.
4726 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4727 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4729 lun->ie_reportcnt = UINT32_MAX;
4730 callout_drain(&lun->ie_callout);
4732 ctl_tpc_lun_shutdown(lun);
4733 mtx_destroy(&lun->lun_lock);
4734 free(lun->lun_devid, M_CTL);
4735 for (i = 0; i < CTL_MAX_PORTS; i++)
4736 free(lun->pending_ua[i], M_CTL);
4737 for (i = 0; i < CTL_MAX_PORTS; i++)
4738 free(lun->pr_keys[i], M_CTL);
4739 free(lun->write_buffer, M_CTL);
4740 free(lun->prevent, M_CTL);
4741 if (lun->flags & CTL_LUN_MALLOCED)
4744 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4745 mtx_lock(&nlun->lun_lock);
4746 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4747 mtx_unlock(&nlun->lun_lock);
4754 ctl_create_lun(struct ctl_be_lun *be_lun)
4758 * ctl_alloc_lun() should handle all potential failure cases.
4760 ctl_alloc_lun(control_softc, NULL, be_lun);
4764 ctl_add_lun(struct ctl_be_lun *be_lun)
4766 struct ctl_softc *softc = control_softc;
4768 mtx_lock(&softc->ctl_lock);
4769 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4770 mtx_unlock(&softc->ctl_lock);
4771 wakeup(&softc->pending_lun_queue);
4777 ctl_enable_lun(struct ctl_be_lun *be_lun)
4779 struct ctl_softc *softc;
4780 struct ctl_port *port, *nport;
4781 struct ctl_lun *lun;
4784 lun = (struct ctl_lun *)be_lun->ctl_lun;
4785 softc = lun->ctl_softc;
4787 mtx_lock(&softc->ctl_lock);
4788 mtx_lock(&lun->lun_lock);
4789 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4791 * eh? Why did we get called if the LUN is already
4794 mtx_unlock(&lun->lun_lock);
4795 mtx_unlock(&softc->ctl_lock);
4798 lun->flags &= ~CTL_LUN_DISABLED;
4799 mtx_unlock(&lun->lun_lock);
4801 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4802 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4803 port->lun_map != NULL || port->lun_enable == NULL)
4807 * Drop the lock while we call the FETD's enable routine.
4808 * This can lead to a callback into CTL (at least in the
4809 * case of the internal initiator frontend.
4811 mtx_unlock(&softc->ctl_lock);
4812 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4813 mtx_lock(&softc->ctl_lock);
4815 printf("%s: FETD %s port %d returned error "
4816 "%d for lun_enable on lun %jd\n",
4817 __func__, port->port_name, port->targ_port,
4818 retval, (intmax_t)lun->lun);
4822 mtx_unlock(&softc->ctl_lock);
4823 ctl_isc_announce_lun(lun);
4829 ctl_disable_lun(struct ctl_be_lun *be_lun)
4831 struct ctl_softc *softc;
4832 struct ctl_port *port;
4833 struct ctl_lun *lun;
4836 lun = (struct ctl_lun *)be_lun->ctl_lun;
4837 softc = lun->ctl_softc;
4839 mtx_lock(&softc->ctl_lock);
4840 mtx_lock(&lun->lun_lock);
4841 if (lun->flags & CTL_LUN_DISABLED) {
4842 mtx_unlock(&lun->lun_lock);
4843 mtx_unlock(&softc->ctl_lock);
4846 lun->flags |= CTL_LUN_DISABLED;
4847 mtx_unlock(&lun->lun_lock);
4849 STAILQ_FOREACH(port, &softc->port_list, links) {
4850 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4851 port->lun_map != NULL || port->lun_disable == NULL)
4855 * Drop the lock before we call the frontend's disable
4856 * routine, to avoid lock order reversals.
4858 * XXX KDM what happens if the frontend list changes while
4859 * we're traversing it? It's unlikely, but should be handled.
4861 mtx_unlock(&softc->ctl_lock);
4862 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4863 mtx_lock(&softc->ctl_lock);
4865 printf("%s: FETD %s port %d returned error "
4866 "%d for lun_disable on lun %jd\n",
4867 __func__, port->port_name, port->targ_port,
4868 retval, (intmax_t)lun->lun);
4872 mtx_unlock(&softc->ctl_lock);
4873 ctl_isc_announce_lun(lun);
4879 ctl_start_lun(struct ctl_be_lun *be_lun)
4881 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4883 mtx_lock(&lun->lun_lock);
4884 lun->flags &= ~CTL_LUN_STOPPED;
4885 mtx_unlock(&lun->lun_lock);
4890 ctl_stop_lun(struct ctl_be_lun *be_lun)
4892 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4894 mtx_lock(&lun->lun_lock);
4895 lun->flags |= CTL_LUN_STOPPED;
4896 mtx_unlock(&lun->lun_lock);
4901 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4903 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4905 mtx_lock(&lun->lun_lock);
4906 lun->flags |= CTL_LUN_NO_MEDIA;
4907 mtx_unlock(&lun->lun_lock);
4912 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4914 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4915 union ctl_ha_msg msg;
4917 mtx_lock(&lun->lun_lock);
4918 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4919 if (lun->flags & CTL_LUN_REMOVABLE)
4920 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4921 mtx_unlock(&lun->lun_lock);
4922 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4923 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4924 bzero(&msg.ua, sizeof(msg.ua));
4925 msg.hdr.msg_type = CTL_MSG_UA;
4926 msg.hdr.nexus.initid = -1;
4927 msg.hdr.nexus.targ_port = -1;
4928 msg.hdr.nexus.targ_lun = lun->lun;
4929 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4932 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4933 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4940 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4942 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4944 mtx_lock(&lun->lun_lock);
4945 lun->flags |= CTL_LUN_EJECTED;
4946 mtx_unlock(&lun->lun_lock);
4951 ctl_lun_primary(struct ctl_be_lun *be_lun)
4953 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4955 mtx_lock(&lun->lun_lock);
4956 lun->flags |= CTL_LUN_PRIMARY_SC;
4957 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4958 mtx_unlock(&lun->lun_lock);
4959 ctl_isc_announce_lun(lun);
4964 ctl_lun_secondary(struct ctl_be_lun *be_lun)
4966 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4968 mtx_lock(&lun->lun_lock);
4969 lun->flags &= ~CTL_LUN_PRIMARY_SC;
4970 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
4971 mtx_unlock(&lun->lun_lock);
4972 ctl_isc_announce_lun(lun);
4977 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
4979 struct ctl_softc *softc;
4980 struct ctl_lun *lun;
4982 lun = (struct ctl_lun *)be_lun->ctl_lun;
4983 softc = lun->ctl_softc;
4985 mtx_lock(&lun->lun_lock);
4988 * The LUN needs to be disabled before it can be marked invalid.
4990 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4991 mtx_unlock(&lun->lun_lock);
4995 * Mark the LUN invalid.
4997 lun->flags |= CTL_LUN_INVALID;
5000 * If there is nothing in the OOA queue, go ahead and free the LUN.
5001 * If we have something in the OOA queue, we'll free it when the
5002 * last I/O completes.
5004 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5005 mtx_unlock(&lun->lun_lock);
5006 mtx_lock(&softc->ctl_lock);
5008 mtx_unlock(&softc->ctl_lock);
5010 mtx_unlock(&lun->lun_lock);
5016 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5018 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5019 union ctl_ha_msg msg;
5021 mtx_lock(&lun->lun_lock);
5022 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5023 mtx_unlock(&lun->lun_lock);
5024 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5025 /* Send msg to other side. */
5026 bzero(&msg.ua, sizeof(msg.ua));
5027 msg.hdr.msg_type = CTL_MSG_UA;
5028 msg.hdr.nexus.initid = -1;
5029 msg.hdr.nexus.targ_port = -1;
5030 msg.hdr.nexus.targ_lun = lun->lun;
5031 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5034 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5035 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5041 * Backend "memory move is complete" callback for requests that never
5042 * make it down to say RAIDCore's configuration code.
5045 ctl_config_move_done(union ctl_io *io)
5049 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5050 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5051 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5053 if ((io->io_hdr.port_status != 0) &&
5054 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5055 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5057 * For hardware error sense keys, the sense key
5058 * specific value is defined to be a retry count,
5059 * but we use it to pass back an internal FETD
5060 * error code. XXX KDM Hopefully the FETD is only
5061 * using 16 bits for an error code, since that's
5062 * all the space we have in the sks field.
5064 ctl_set_internal_failure(&io->scsiio,
5067 io->io_hdr.port_status);
5070 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5072 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5073 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5074 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5075 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5077 * XXX KDM just assuming a single pointer here, and not a
5078 * S/G list. If we start using S/G lists for config data,
5079 * we'll need to know how to clean them up here as well.
5081 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5082 free(io->scsiio.kern_data_ptr, M_CTL);
5084 retval = CTL_RETVAL_COMPLETE;
5087 * XXX KDM now we need to continue data movement. Some
5089 * - call ctl_scsiio() again? We don't do this for data
5090 * writes, because for those at least we know ahead of
5091 * time where the write will go and how long it is. For
5092 * config writes, though, that information is largely
5093 * contained within the write itself, thus we need to
5094 * parse out the data again.
5096 * - Call some other function once the data is in?
5100 * XXX KDM call ctl_scsiio() again for now, and check flag
5101 * bits to see whether we're allocated or not.
5103 retval = ctl_scsiio(&io->scsiio);
5109 * This gets called by a backend driver when it is done with a
5110 * data_submit method.
5113 ctl_data_submit_done(union ctl_io *io)
5116 * If the IO_CONT flag is set, we need to call the supplied
5117 * function to continue processing the I/O, instead of completing
5120 * If there is an error, though, we don't want to keep processing.
5121 * Instead, just send status back to the initiator.
5123 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5124 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5125 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5126 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5127 io->scsiio.io_cont(io);
5134 * This gets called by a backend driver when it is done with a
5135 * configuration write.
5138 ctl_config_write_done(union ctl_io *io)
5143 * If the IO_CONT flag is set, we need to call the supplied
5144 * function to continue processing the I/O, instead of completing
5147 * If there is an error, though, we don't want to keep processing.
5148 * Instead, just send status back to the initiator.
5150 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5151 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5152 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5153 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5154 io->scsiio.io_cont(io);
5158 * Since a configuration write can be done for commands that actually
5159 * have data allocated, like write buffer, and commands that have
5160 * no data, like start/stop unit, we need to check here.
5162 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5163 buf = io->scsiio.kern_data_ptr;
5172 ctl_config_read_done(union ctl_io *io)
5177 * If there is some error -- we are done, skip data transfer.
5179 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5180 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5181 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5182 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5183 buf = io->scsiio.kern_data_ptr;
5193 * If the IO_CONT flag is set, we need to call the supplied
5194 * function to continue processing the I/O, instead of completing
5197 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5198 io->scsiio.io_cont(io);
5206 * SCSI release command.
5209 ctl_scsi_release(struct ctl_scsiio *ctsio)
5211 struct ctl_lun *lun = CTL_LUN(ctsio);
5214 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5216 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5219 * XXX KDM right now, we only support LUN reservation. We don't
5220 * support 3rd party reservations, or extent reservations, which
5221 * might actually need the parameter list. If we've gotten this
5222 * far, we've got a LUN reservation. Anything else got kicked out
5223 * above. So, according to SPC, ignore the length.
5226 mtx_lock(&lun->lun_lock);
5229 * According to SPC, it is not an error for an intiator to attempt
5230 * to release a reservation on a LUN that isn't reserved, or that
5231 * is reserved by another initiator. The reservation can only be
5232 * released, though, by the initiator who made it or by one of
5233 * several reset type events.
5235 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5236 lun->flags &= ~CTL_LUN_RESERVED;
5238 mtx_unlock(&lun->lun_lock);
5240 ctl_set_success(ctsio);
5241 ctl_done((union ctl_io *)ctsio);
5242 return (CTL_RETVAL_COMPLETE);
5246 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5248 struct ctl_lun *lun = CTL_LUN(ctsio);
5251 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5253 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5256 * XXX KDM right now, we only support LUN reservation. We don't
5257 * support 3rd party reservations, or extent reservations, which
5258 * might actually need the parameter list. If we've gotten this
5259 * far, we've got a LUN reservation. Anything else got kicked out
5260 * above. So, according to SPC, ignore the length.
5263 mtx_lock(&lun->lun_lock);
5264 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5265 ctl_set_reservation_conflict(ctsio);
5269 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5270 if (lun->flags & CTL_LUN_PR_RESERVED) {
5271 ctl_set_success(ctsio);
5275 lun->flags |= CTL_LUN_RESERVED;
5276 lun->res_idx = residx;
5277 ctl_set_success(ctsio);
5280 mtx_unlock(&lun->lun_lock);
5281 ctl_done((union ctl_io *)ctsio);
5282 return (CTL_RETVAL_COMPLETE);
5286 ctl_start_stop(struct ctl_scsiio *ctsio)
5288 struct ctl_lun *lun = CTL_LUN(ctsio);
5289 struct scsi_start_stop_unit *cdb;
5292 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5294 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5296 if ((cdb->how & SSS_PC_MASK) == 0) {
5297 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5298 (cdb->how & SSS_START) == 0) {
5301 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5302 if (ctl_get_prkey(lun, residx) == 0 ||
5303 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5305 ctl_set_reservation_conflict(ctsio);
5306 ctl_done((union ctl_io *)ctsio);
5307 return (CTL_RETVAL_COMPLETE);
5311 if ((cdb->how & SSS_LOEJ) &&
5312 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5313 ctl_set_invalid_field(ctsio,
5319 ctl_done((union ctl_io *)ctsio);
5320 return (CTL_RETVAL_COMPLETE);
5323 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5324 lun->prevent_count > 0) {
5325 /* "Medium removal prevented" */
5326 ctl_set_sense(ctsio, /*current_error*/ 1,
5327 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5328 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5329 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5330 ctl_done((union ctl_io *)ctsio);
5331 return (CTL_RETVAL_COMPLETE);
5335 retval = lun->backend->config_write((union ctl_io *)ctsio);
5340 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5342 struct ctl_lun *lun = CTL_LUN(ctsio);
5343 struct scsi_prevent *cdb;
5347 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5349 cdb = (struct scsi_prevent *)ctsio->cdb;
5351 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5352 ctl_set_invalid_opcode(ctsio);
5353 ctl_done((union ctl_io *)ctsio);
5354 return (CTL_RETVAL_COMPLETE);
5357 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5358 mtx_lock(&lun->lun_lock);
5359 if ((cdb->how & PR_PREVENT) &&
5360 ctl_is_set(lun->prevent, initidx) == 0) {
5361 ctl_set_mask(lun->prevent, initidx);
5362 lun->prevent_count++;
5363 } else if ((cdb->how & PR_PREVENT) == 0 &&
5364 ctl_is_set(lun->prevent, initidx)) {
5365 ctl_clear_mask(lun->prevent, initidx);
5366 lun->prevent_count--;
5368 mtx_unlock(&lun->lun_lock);
5369 retval = lun->backend->config_write((union ctl_io *)ctsio);
5374 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5375 * we don't really do anything with the LBA and length fields if the user
5376 * passes them in. Instead we'll just flush out the cache for the entire
5380 ctl_sync_cache(struct ctl_scsiio *ctsio)
5382 struct ctl_lun *lun = CTL_LUN(ctsio);
5383 struct ctl_lba_len_flags *lbalen;
5384 uint64_t starting_lba;
5385 uint32_t block_count;
5389 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5393 switch (ctsio->cdb[0]) {
5394 case SYNCHRONIZE_CACHE: {
5395 struct scsi_sync_cache *cdb;
5396 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5398 starting_lba = scsi_4btoul(cdb->begin_lba);
5399 block_count = scsi_2btoul(cdb->lb_count);
5403 case SYNCHRONIZE_CACHE_16: {
5404 struct scsi_sync_cache_16 *cdb;
5405 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5407 starting_lba = scsi_8btou64(cdb->begin_lba);
5408 block_count = scsi_4btoul(cdb->lb_count);
5413 ctl_set_invalid_opcode(ctsio);
5414 ctl_done((union ctl_io *)ctsio);
5416 break; /* NOTREACHED */
5420 * We check the LBA and length, but don't do anything with them.
5421 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5422 * get flushed. This check will just help satisfy anyone who wants
5423 * to see an error for an out of range LBA.
5425 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5426 ctl_set_lba_out_of_range(ctsio,
5427 MAX(starting_lba, lun->be_lun->maxlba + 1));
5428 ctl_done((union ctl_io *)ctsio);
5432 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5433 lbalen->lba = starting_lba;
5434 lbalen->len = block_count;
5435 lbalen->flags = byte2;
5436 retval = lun->backend->config_write((union ctl_io *)ctsio);
5443 ctl_format(struct ctl_scsiio *ctsio)
5445 struct scsi_format *cdb;
5446 int length, defect_list_len;
5448 CTL_DEBUG_PRINT(("ctl_format\n"));
5450 cdb = (struct scsi_format *)ctsio->cdb;
5453 if (cdb->byte2 & SF_FMTDATA) {
5454 if (cdb->byte2 & SF_LONGLIST)
5455 length = sizeof(struct scsi_format_header_long);
5457 length = sizeof(struct scsi_format_header_short);
5460 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5462 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5463 ctsio->kern_data_len = length;
5464 ctsio->kern_total_len = length;
5465 ctsio->kern_data_resid = 0;
5466 ctsio->kern_rel_offset = 0;
5467 ctsio->kern_sg_entries = 0;
5468 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5469 ctsio->be_move_done = ctl_config_move_done;
5470 ctl_datamove((union ctl_io *)ctsio);
5472 return (CTL_RETVAL_COMPLETE);
5475 defect_list_len = 0;
5477 if (cdb->byte2 & SF_FMTDATA) {
5478 if (cdb->byte2 & SF_LONGLIST) {
5479 struct scsi_format_header_long *header;
5481 header = (struct scsi_format_header_long *)
5482 ctsio->kern_data_ptr;
5484 defect_list_len = scsi_4btoul(header->defect_list_len);
5485 if (defect_list_len != 0) {
5486 ctl_set_invalid_field(ctsio,
5495 struct scsi_format_header_short *header;
5497 header = (struct scsi_format_header_short *)
5498 ctsio->kern_data_ptr;
5500 defect_list_len = scsi_2btoul(header->defect_list_len);
5501 if (defect_list_len != 0) {
5502 ctl_set_invalid_field(ctsio,
5513 ctl_set_success(ctsio);
5516 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5517 free(ctsio->kern_data_ptr, M_CTL);
5518 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5521 ctl_done((union ctl_io *)ctsio);
5522 return (CTL_RETVAL_COMPLETE);
5526 ctl_read_buffer(struct ctl_scsiio *ctsio)
5528 struct ctl_lun *lun = CTL_LUN(ctsio);
5529 uint64_t buffer_offset;
5532 static uint8_t descr[4];
5533 static uint8_t echo_descr[4] = { 0 };
5535 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5537 switch (ctsio->cdb[0]) {
5539 struct scsi_read_buffer *cdb;
5541 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5542 buffer_offset = scsi_3btoul(cdb->offset);
5543 len = scsi_3btoul(cdb->length);
5547 case READ_BUFFER_16: {
5548 struct scsi_read_buffer_16 *cdb;
5550 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5551 buffer_offset = scsi_8btou64(cdb->offset);
5552 len = scsi_4btoul(cdb->length);
5556 default: /* This shouldn't happen. */
5557 ctl_set_invalid_opcode(ctsio);
5558 ctl_done((union ctl_io *)ctsio);
5559 return (CTL_RETVAL_COMPLETE);
5562 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5563 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5564 ctl_set_invalid_field(ctsio,
5570 ctl_done((union ctl_io *)ctsio);
5571 return (CTL_RETVAL_COMPLETE);
5574 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5576 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5577 ctsio->kern_data_ptr = descr;
5578 len = min(len, sizeof(descr));
5579 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5580 ctsio->kern_data_ptr = echo_descr;
5581 len = min(len, sizeof(echo_descr));
5583 if (lun->write_buffer == NULL) {
5584 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5587 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5589 ctsio->kern_data_len = len;
5590 ctsio->kern_total_len = len;
5591 ctsio->kern_data_resid = 0;
5592 ctsio->kern_rel_offset = 0;
5593 ctsio->kern_sg_entries = 0;
5594 ctl_set_success(ctsio);
5595 ctsio->be_move_done = ctl_config_move_done;
5596 ctl_datamove((union ctl_io *)ctsio);
5597 return (CTL_RETVAL_COMPLETE);
5601 ctl_write_buffer(struct ctl_scsiio *ctsio)
5603 struct ctl_lun *lun = CTL_LUN(ctsio);
5604 struct scsi_write_buffer *cdb;
5605 int buffer_offset, len;
5607 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5609 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5611 len = scsi_3btoul(cdb->length);
5612 buffer_offset = scsi_3btoul(cdb->offset);
5614 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5615 ctl_set_invalid_field(ctsio,
5621 ctl_done((union ctl_io *)ctsio);
5622 return (CTL_RETVAL_COMPLETE);
5626 * If we've got a kernel request that hasn't been malloced yet,
5627 * malloc it and tell the caller the data buffer is here.
5629 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5630 if (lun->write_buffer == NULL) {
5631 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5634 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5635 ctsio->kern_data_len = len;
5636 ctsio->kern_total_len = len;
5637 ctsio->kern_data_resid = 0;
5638 ctsio->kern_rel_offset = 0;
5639 ctsio->kern_sg_entries = 0;
5640 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5641 ctsio->be_move_done = ctl_config_move_done;
5642 ctl_datamove((union ctl_io *)ctsio);
5644 return (CTL_RETVAL_COMPLETE);
5647 ctl_set_success(ctsio);
5648 ctl_done((union ctl_io *)ctsio);
5649 return (CTL_RETVAL_COMPLETE);
5653 ctl_write_same(struct ctl_scsiio *ctsio)
5655 struct ctl_lun *lun = CTL_LUN(ctsio);
5656 struct ctl_lba_len_flags *lbalen;
5658 uint32_t num_blocks;
5662 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5664 switch (ctsio->cdb[0]) {
5665 case WRITE_SAME_10: {
5666 struct scsi_write_same_10 *cdb;
5668 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5670 lba = scsi_4btoul(cdb->addr);
5671 num_blocks = scsi_2btoul(cdb->length);
5675 case WRITE_SAME_16: {
5676 struct scsi_write_same_16 *cdb;
5678 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5680 lba = scsi_8btou64(cdb->addr);
5681 num_blocks = scsi_4btoul(cdb->length);
5687 * We got a command we don't support. This shouldn't
5688 * happen, commands should be filtered out above us.
5690 ctl_set_invalid_opcode(ctsio);
5691 ctl_done((union ctl_io *)ctsio);
5693 return (CTL_RETVAL_COMPLETE);
5694 break; /* NOTREACHED */
5697 /* ANCHOR flag can be used only together with UNMAP */
5698 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5699 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5700 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5701 ctl_done((union ctl_io *)ctsio);
5702 return (CTL_RETVAL_COMPLETE);
5706 * The first check is to make sure we're in bounds, the second
5707 * check is to catch wrap-around problems. If the lba + num blocks
5708 * is less than the lba, then we've wrapped around and the block
5709 * range is invalid anyway.
5711 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5712 || ((lba + num_blocks) < lba)) {
5713 ctl_set_lba_out_of_range(ctsio,
5714 MAX(lba, lun->be_lun->maxlba + 1));
5715 ctl_done((union ctl_io *)ctsio);
5716 return (CTL_RETVAL_COMPLETE);
5719 /* Zero number of blocks means "to the last logical block" */
5720 if (num_blocks == 0) {
5721 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5722 ctl_set_invalid_field(ctsio,
5728 ctl_done((union ctl_io *)ctsio);
5729 return (CTL_RETVAL_COMPLETE);
5731 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5734 len = lun->be_lun->blocksize;
5737 * If we've got a kernel request that hasn't been malloced yet,
5738 * malloc it and tell the caller the data buffer is here.
5740 if ((byte2 & SWS_NDOB) == 0 &&
5741 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5742 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5743 ctsio->kern_data_len = len;
5744 ctsio->kern_total_len = len;
5745 ctsio->kern_data_resid = 0;
5746 ctsio->kern_rel_offset = 0;
5747 ctsio->kern_sg_entries = 0;
5748 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5749 ctsio->be_move_done = ctl_config_move_done;
5750 ctl_datamove((union ctl_io *)ctsio);
5752 return (CTL_RETVAL_COMPLETE);
5755 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5757 lbalen->len = num_blocks;
5758 lbalen->flags = byte2;
5759 retval = lun->backend->config_write((union ctl_io *)ctsio);
5765 ctl_unmap(struct ctl_scsiio *ctsio)
5767 struct ctl_lun *lun = CTL_LUN(ctsio);
5768 struct scsi_unmap *cdb;
5769 struct ctl_ptr_len_flags *ptrlen;
5770 struct scsi_unmap_header *hdr;
5771 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5773 uint32_t num_blocks;
5777 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5779 cdb = (struct scsi_unmap *)ctsio->cdb;
5780 len = scsi_2btoul(cdb->length);
5784 * If we've got a kernel request that hasn't been malloced yet,
5785 * malloc it and tell the caller the data buffer is here.
5787 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5788 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5789 ctsio->kern_data_len = len;
5790 ctsio->kern_total_len = len;
5791 ctsio->kern_data_resid = 0;
5792 ctsio->kern_rel_offset = 0;
5793 ctsio->kern_sg_entries = 0;
5794 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5795 ctsio->be_move_done = ctl_config_move_done;
5796 ctl_datamove((union ctl_io *)ctsio);
5798 return (CTL_RETVAL_COMPLETE);
5801 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5802 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5803 if (len < sizeof (*hdr) ||
5804 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5805 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5806 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5807 ctl_set_invalid_field(ctsio,
5815 len = scsi_2btoul(hdr->desc_length);
5816 buf = (struct scsi_unmap_desc *)(hdr + 1);
5817 end = buf + len / sizeof(*buf);
5820 for (range = buf; range < end; range++) {
5821 lba = scsi_8btou64(range->lba);
5822 num_blocks = scsi_4btoul(range->length);
5823 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5824 || ((lba + num_blocks) < lba)) {
5825 ctl_set_lba_out_of_range(ctsio,
5826 MAX(lba, lun->be_lun->maxlba + 1));
5827 ctl_done((union ctl_io *)ctsio);
5828 return (CTL_RETVAL_COMPLETE);
5830 if (num_blocks != 0)
5835 * Block backend can not handle zero last range.
5836 * Filter it out and return if there is nothing left.
5838 len = (uint8_t *)endnz - (uint8_t *)buf;
5840 ctl_set_success(ctsio);
5844 mtx_lock(&lun->lun_lock);
5845 ptrlen = (struct ctl_ptr_len_flags *)
5846 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5847 ptrlen->ptr = (void *)buf;
5849 ptrlen->flags = byte2;
5850 ctl_check_blocked(lun);
5851 mtx_unlock(&lun->lun_lock);
5853 retval = lun->backend->config_write((union ctl_io *)ctsio);
5857 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5858 free(ctsio->kern_data_ptr, M_CTL);
5859 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5861 ctl_done((union ctl_io *)ctsio);
5862 return (CTL_RETVAL_COMPLETE);
5866 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5867 struct ctl_page_index *page_index, uint8_t *page_ptr)
5869 struct ctl_lun *lun = CTL_LUN(ctsio);
5870 uint8_t *current_cp;
5874 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5877 current_cp = (page_index->page_data + (page_index->page_len *
5880 mtx_lock(&lun->lun_lock);
5881 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5882 memcpy(current_cp, page_ptr, page_index->page_len);
5886 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5887 mtx_unlock(&lun->lun_lock);
5889 ctl_isc_announce_mode(lun,
5890 ctl_get_initindex(&ctsio->io_hdr.nexus),
5891 page_index->page_code, page_index->subpage);
5893 return (CTL_RETVAL_COMPLETE);
5897 ctl_ie_timer(void *arg)
5899 struct ctl_lun *lun = arg;
5902 if (lun->ie_asc == 0)
5905 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5906 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5908 lun->ie_reported = 0;
5910 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5911 lun->ie_reportcnt++;
5912 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5913 if (t == 0 || t == UINT32_MAX)
5914 t = 3000; /* 5 min */
5915 callout_schedule(&lun->ie_callout, t * hz / 10);
5920 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5921 struct ctl_page_index *page_index, uint8_t *page_ptr)
5923 struct ctl_lun *lun = CTL_LUN(ctsio);
5924 struct scsi_info_exceptions_page *pg;
5927 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5929 pg = (struct scsi_info_exceptions_page *)page_ptr;
5930 mtx_lock(&lun->lun_lock);
5931 if (pg->info_flags & SIEP_FLAGS_TEST) {
5933 lun->ie_ascq = 0xff;
5934 if (pg->mrie == SIEP_MRIE_UA) {
5935 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5936 lun->ie_reported = 1;
5938 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5939 lun->ie_reported = -1;
5941 lun->ie_reportcnt = 1;
5942 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5943 lun->ie_reportcnt++;
5944 t = scsi_4btoul(pg->interval_timer);
5945 if (t == 0 || t == UINT32_MAX)
5946 t = 3000; /* 5 min */
5947 callout_reset(&lun->ie_callout, t * hz / 10,
5953 lun->ie_reported = 1;
5954 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5955 lun->ie_reportcnt = UINT32_MAX;
5956 callout_stop(&lun->ie_callout);
5958 mtx_unlock(&lun->lun_lock);
5959 return (CTL_RETVAL_COMPLETE);
5963 ctl_do_mode_select(union ctl_io *io)
5965 struct ctl_lun *lun = CTL_LUN(io);
5966 struct scsi_mode_page_header *page_header;
5967 struct ctl_page_index *page_index;
5968 struct ctl_scsiio *ctsio;
5969 int page_len, page_len_offset, page_len_size;
5970 union ctl_modepage_info *modepage_info;
5971 uint16_t *len_left, *len_used;
5974 ctsio = &io->scsiio;
5978 modepage_info = (union ctl_modepage_info *)
5979 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
5980 len_left = &modepage_info->header.len_left;
5981 len_used = &modepage_info->header.len_used;
5985 page_header = (struct scsi_mode_page_header *)
5986 (ctsio->kern_data_ptr + *len_used);
5988 if (*len_left == 0) {
5989 free(ctsio->kern_data_ptr, M_CTL);
5990 ctl_set_success(ctsio);
5991 ctl_done((union ctl_io *)ctsio);
5992 return (CTL_RETVAL_COMPLETE);
5993 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
5995 free(ctsio->kern_data_ptr, M_CTL);
5996 ctl_set_param_len_error(ctsio);
5997 ctl_done((union ctl_io *)ctsio);
5998 return (CTL_RETVAL_COMPLETE);
6000 } else if ((page_header->page_code & SMPH_SPF)
6001 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6003 free(ctsio->kern_data_ptr, M_CTL);
6004 ctl_set_param_len_error(ctsio);
6005 ctl_done((union ctl_io *)ctsio);
6006 return (CTL_RETVAL_COMPLETE);
6011 * XXX KDM should we do something with the block descriptor?
6013 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6014 page_index = &lun->mode_pages.index[i];
6015 if (lun->be_lun->lun_type == T_DIRECT &&
6016 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6018 if (lun->be_lun->lun_type == T_PROCESSOR &&
6019 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6021 if (lun->be_lun->lun_type == T_CDROM &&
6022 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6025 if ((page_index->page_code & SMPH_PC_MASK) !=
6026 (page_header->page_code & SMPH_PC_MASK))
6030 * If neither page has a subpage code, then we've got a
6033 if (((page_index->page_code & SMPH_SPF) == 0)
6034 && ((page_header->page_code & SMPH_SPF) == 0)) {
6035 page_len = page_header->page_length;
6040 * If both pages have subpages, then the subpage numbers
6043 if ((page_index->page_code & SMPH_SPF)
6044 && (page_header->page_code & SMPH_SPF)) {
6045 struct scsi_mode_page_header_sp *sph;
6047 sph = (struct scsi_mode_page_header_sp *)page_header;
6048 if (page_index->subpage == sph->subpage) {
6049 page_len = scsi_2btoul(sph->page_length);
6056 * If we couldn't find the page, or if we don't have a mode select
6057 * handler for it, send back an error to the user.
6059 if ((i >= CTL_NUM_MODE_PAGES)
6060 || (page_index->select_handler == NULL)) {
6061 ctl_set_invalid_field(ctsio,
6064 /*field*/ *len_used,
6067 free(ctsio->kern_data_ptr, M_CTL);
6068 ctl_done((union ctl_io *)ctsio);
6069 return (CTL_RETVAL_COMPLETE);
6072 if (page_index->page_code & SMPH_SPF) {
6073 page_len_offset = 2;
6077 page_len_offset = 1;
6081 * If the length the initiator gives us isn't the one we specify in
6082 * the mode page header, or if they didn't specify enough data in
6083 * the CDB to avoid truncating this page, kick out the request.
6085 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6086 ctl_set_invalid_field(ctsio,
6089 /*field*/ *len_used + page_len_offset,
6092 free(ctsio->kern_data_ptr, M_CTL);
6093 ctl_done((union ctl_io *)ctsio);
6094 return (CTL_RETVAL_COMPLETE);
6096 if (*len_left < page_index->page_len) {
6097 free(ctsio->kern_data_ptr, M_CTL);
6098 ctl_set_param_len_error(ctsio);
6099 ctl_done((union ctl_io *)ctsio);
6100 return (CTL_RETVAL_COMPLETE);
6104 * Run through the mode page, checking to make sure that the bits
6105 * the user changed are actually legal for him to change.
6107 for (i = 0; i < page_index->page_len; i++) {
6108 uint8_t *user_byte, *change_mask, *current_byte;
6112 user_byte = (uint8_t *)page_header + i;
6113 change_mask = page_index->page_data +
6114 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6115 current_byte = page_index->page_data +
6116 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6119 * Check to see whether the user set any bits in this byte
6120 * that he is not allowed to set.
6122 if ((*user_byte & ~(*change_mask)) ==
6123 (*current_byte & ~(*change_mask)))
6127 * Go through bit by bit to determine which one is illegal.
6130 for (j = 7; j >= 0; j--) {
6131 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6132 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6137 ctl_set_invalid_field(ctsio,
6140 /*field*/ *len_used + i,
6143 free(ctsio->kern_data_ptr, M_CTL);
6144 ctl_done((union ctl_io *)ctsio);
6145 return (CTL_RETVAL_COMPLETE);
6149 * Decrement these before we call the page handler, since we may
6150 * end up getting called back one way or another before the handler
6151 * returns to this context.
6153 *len_left -= page_index->page_len;
6154 *len_used += page_index->page_len;
6156 retval = page_index->select_handler(ctsio, page_index,
6157 (uint8_t *)page_header);
6160 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6161 * wait until this queued command completes to finish processing
6162 * the mode page. If it returns anything other than
6163 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6164 * already set the sense information, freed the data pointer, and
6165 * completed the io for us.
6167 if (retval != CTL_RETVAL_COMPLETE)
6168 goto bailout_no_done;
6171 * If the initiator sent us more than one page, parse the next one.
6176 ctl_set_success(ctsio);
6177 free(ctsio->kern_data_ptr, M_CTL);
6178 ctl_done((union ctl_io *)ctsio);
6182 return (CTL_RETVAL_COMPLETE);
6187 ctl_mode_select(struct ctl_scsiio *ctsio)
6189 struct ctl_lun *lun = CTL_LUN(ctsio);
6190 union ctl_modepage_info *modepage_info;
6191 int bd_len, i, header_size, param_len, pf, rtd, sp;
6194 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6195 switch (ctsio->cdb[0]) {
6196 case MODE_SELECT_6: {
6197 struct scsi_mode_select_6 *cdb;
6199 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6201 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6202 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6203 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6204 param_len = cdb->length;
6205 header_size = sizeof(struct scsi_mode_header_6);
6208 case MODE_SELECT_10: {
6209 struct scsi_mode_select_10 *cdb;
6211 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6213 pf = (cdb->byte2 & SMS_PF) ? 1 : 0;
6214 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6215 sp = (cdb->byte2 & SMS_SP) ? 1 : 0;
6216 param_len = scsi_2btoul(cdb->length);
6217 header_size = sizeof(struct scsi_mode_header_10);
6221 ctl_set_invalid_opcode(ctsio);
6222 ctl_done((union ctl_io *)ctsio);
6223 return (CTL_RETVAL_COMPLETE);
6227 if (param_len != 0) {
6228 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6229 /*command*/ 1, /*field*/ 0,
6230 /*bit_valid*/ 0, /*bit*/ 0);
6231 ctl_done((union ctl_io *)ctsio);
6232 return (CTL_RETVAL_COMPLETE);
6235 /* Revert to defaults. */
6236 ctl_init_page_index(lun);
6237 mtx_lock(&lun->lun_lock);
6238 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6239 mtx_unlock(&lun->lun_lock);
6240 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6241 ctl_isc_announce_mode(lun, -1,
6242 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6243 lun->mode_pages.index[i].subpage);
6245 ctl_set_success(ctsio);
6246 ctl_done((union ctl_io *)ctsio);
6247 return (CTL_RETVAL_COMPLETE);
6252 * "A parameter list length of zero indicates that the Data-Out Buffer
6253 * shall be empty. This condition shall not be considered as an error."
6255 if (param_len == 0) {
6256 ctl_set_success(ctsio);
6257 ctl_done((union ctl_io *)ctsio);
6258 return (CTL_RETVAL_COMPLETE);
6262 * Since we'll hit this the first time through, prior to
6263 * allocation, we don't need to free a data buffer here.
6265 if (param_len < header_size) {
6266 ctl_set_param_len_error(ctsio);
6267 ctl_done((union ctl_io *)ctsio);
6268 return (CTL_RETVAL_COMPLETE);
6272 * Allocate the data buffer and grab the user's data. In theory,
6273 * we shouldn't have to sanity check the parameter list length here
6274 * because the maximum size is 64K. We should be able to malloc
6275 * that much without too many problems.
6277 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6278 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6279 ctsio->kern_data_len = param_len;
6280 ctsio->kern_total_len = param_len;
6281 ctsio->kern_data_resid = 0;
6282 ctsio->kern_rel_offset = 0;
6283 ctsio->kern_sg_entries = 0;
6284 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6285 ctsio->be_move_done = ctl_config_move_done;
6286 ctl_datamove((union ctl_io *)ctsio);
6288 return (CTL_RETVAL_COMPLETE);
6291 switch (ctsio->cdb[0]) {
6292 case MODE_SELECT_6: {
6293 struct scsi_mode_header_6 *mh6;
6295 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6296 bd_len = mh6->blk_desc_len;
6299 case MODE_SELECT_10: {
6300 struct scsi_mode_header_10 *mh10;
6302 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6303 bd_len = scsi_2btoul(mh10->blk_desc_len);
6307 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6310 if (param_len < (header_size + bd_len)) {
6311 free(ctsio->kern_data_ptr, M_CTL);
6312 ctl_set_param_len_error(ctsio);
6313 ctl_done((union ctl_io *)ctsio);
6314 return (CTL_RETVAL_COMPLETE);
6318 * Set the IO_CONT flag, so that if this I/O gets passed to
6319 * ctl_config_write_done(), it'll get passed back to
6320 * ctl_do_mode_select() for further processing, or completion if
6323 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6324 ctsio->io_cont = ctl_do_mode_select;
6326 modepage_info = (union ctl_modepage_info *)
6327 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6328 memset(modepage_info, 0, sizeof(*modepage_info));
6329 modepage_info->header.len_left = param_len - header_size - bd_len;
6330 modepage_info->header.len_used = header_size + bd_len;
6332 return (ctl_do_mode_select((union ctl_io *)ctsio));
6336 ctl_mode_sense(struct ctl_scsiio *ctsio)
6338 struct ctl_lun *lun = CTL_LUN(ctsio);
6339 int pc, page_code, dbd, llba, subpage;
6340 int alloc_len, page_len, header_len, total_len;
6341 struct scsi_mode_block_descr *block_desc;
6342 struct ctl_page_index *page_index;
6348 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6350 switch (ctsio->cdb[0]) {
6351 case MODE_SENSE_6: {
6352 struct scsi_mode_sense_6 *cdb;
6354 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6356 header_len = sizeof(struct scsi_mode_hdr_6);
6357 if (cdb->byte2 & SMS_DBD)
6360 header_len += sizeof(struct scsi_mode_block_descr);
6362 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6363 page_code = cdb->page & SMS_PAGE_CODE;
6364 subpage = cdb->subpage;
6365 alloc_len = cdb->length;
6368 case MODE_SENSE_10: {
6369 struct scsi_mode_sense_10 *cdb;
6371 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6373 header_len = sizeof(struct scsi_mode_hdr_10);
6375 if (cdb->byte2 & SMS_DBD)
6378 header_len += sizeof(struct scsi_mode_block_descr);
6379 if (cdb->byte2 & SMS10_LLBAA)
6381 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6382 page_code = cdb->page & SMS_PAGE_CODE;
6383 subpage = cdb->subpage;
6384 alloc_len = scsi_2btoul(cdb->length);
6388 ctl_set_invalid_opcode(ctsio);
6389 ctl_done((union ctl_io *)ctsio);
6390 return (CTL_RETVAL_COMPLETE);
6391 break; /* NOTREACHED */
6395 * We have to make a first pass through to calculate the size of
6396 * the pages that match the user's query. Then we allocate enough
6397 * memory to hold it, and actually copy the data into the buffer.
6399 switch (page_code) {
6400 case SMS_ALL_PAGES_PAGE: {
6406 * At the moment, values other than 0 and 0xff here are
6407 * reserved according to SPC-3.
6409 if ((subpage != SMS_SUBPAGE_PAGE_0)
6410 && (subpage != SMS_SUBPAGE_ALL)) {
6411 ctl_set_invalid_field(ctsio,
6417 ctl_done((union ctl_io *)ctsio);
6418 return (CTL_RETVAL_COMPLETE);
6421 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6422 page_index = &lun->mode_pages.index[i];
6424 /* Make sure the page is supported for this dev type */
6425 if (lun->be_lun->lun_type == T_DIRECT &&
6426 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6428 if (lun->be_lun->lun_type == T_PROCESSOR &&
6429 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6431 if (lun->be_lun->lun_type == T_CDROM &&
6432 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6436 * We don't use this subpage if the user didn't
6437 * request all subpages.
6439 if ((page_index->subpage != 0)
6440 && (subpage == SMS_SUBPAGE_PAGE_0))
6444 printf("found page %#x len %d\n",
6445 page_index->page_code & SMPH_PC_MASK,
6446 page_index->page_len);
6448 page_len += page_index->page_len;
6457 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6458 page_index = &lun->mode_pages.index[i];
6460 /* Make sure the page is supported for this dev type */
6461 if (lun->be_lun->lun_type == T_DIRECT &&
6462 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6464 if (lun->be_lun->lun_type == T_PROCESSOR &&
6465 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6467 if (lun->be_lun->lun_type == T_CDROM &&
6468 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6471 /* Look for the right page code */
6472 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6475 /* Look for the right subpage or the subpage wildcard*/
6476 if ((page_index->subpage != subpage)
6477 && (subpage != SMS_SUBPAGE_ALL))
6481 printf("found page %#x len %d\n",
6482 page_index->page_code & SMPH_PC_MASK,
6483 page_index->page_len);
6486 page_len += page_index->page_len;
6489 if (page_len == 0) {
6490 ctl_set_invalid_field(ctsio,
6496 ctl_done((union ctl_io *)ctsio);
6497 return (CTL_RETVAL_COMPLETE);
6503 total_len = header_len + page_len;
6505 printf("header_len = %d, page_len = %d, total_len = %d\n",
6506 header_len, page_len, total_len);
6509 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6510 ctsio->kern_sg_entries = 0;
6511 ctsio->kern_data_resid = 0;
6512 ctsio->kern_rel_offset = 0;
6513 if (total_len < alloc_len) {
6514 ctsio->residual = alloc_len - total_len;
6515 ctsio->kern_data_len = total_len;
6516 ctsio->kern_total_len = total_len;
6518 ctsio->residual = 0;
6519 ctsio->kern_data_len = alloc_len;
6520 ctsio->kern_total_len = alloc_len;
6523 switch (ctsio->cdb[0]) {
6524 case MODE_SENSE_6: {
6525 struct scsi_mode_hdr_6 *header;
6527 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6529 header->datalen = MIN(total_len - 1, 254);
6530 if (lun->be_lun->lun_type == T_DIRECT) {
6531 header->dev_specific = 0x10; /* DPOFUA */
6532 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6533 (lun->MODE_CTRL.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_CTRL.eca_and_aen & SCP_SWP) != 0)
6556 header->dev_specific |= 0x80; /* WP */
6559 scsi_ulto2b(0, header->block_descr_len);
6561 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6562 header->block_descr_len);
6563 block_desc = (struct scsi_mode_block_descr *)&header[1];
6567 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6571 * If we've got a disk, use its blocksize in the block
6572 * descriptor. Otherwise, just set it to 0.
6575 if (lun->be_lun->lun_type == T_DIRECT)
6576 scsi_ulto3b(lun->be_lun->blocksize,
6577 block_desc->block_len);
6579 scsi_ulto3b(0, block_desc->block_len);
6582 switch (page_code) {
6583 case SMS_ALL_PAGES_PAGE: {
6586 data_used = header_len;
6587 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6588 struct ctl_page_index *page_index;
6590 page_index = &lun->mode_pages.index[i];
6591 if (lun->be_lun->lun_type == T_DIRECT &&
6592 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6594 if (lun->be_lun->lun_type == T_PROCESSOR &&
6595 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6597 if (lun->be_lun->lun_type == T_CDROM &&
6598 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6602 * We don't use this subpage if the user didn't
6603 * request all subpages. We already checked (above)
6604 * to make sure the user only specified a subpage
6605 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6607 if ((page_index->subpage != 0)
6608 && (subpage == SMS_SUBPAGE_PAGE_0))
6612 * Call the handler, if it exists, to update the
6613 * page to the latest values.
6615 if (page_index->sense_handler != NULL)
6616 page_index->sense_handler(ctsio, page_index,pc);
6618 memcpy(ctsio->kern_data_ptr + data_used,
6619 page_index->page_data +
6620 (page_index->page_len * pc),
6621 page_index->page_len);
6622 data_used += page_index->page_len;
6629 data_used = header_len;
6631 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6632 struct ctl_page_index *page_index;
6634 page_index = &lun->mode_pages.index[i];
6636 /* Look for the right page code */
6637 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6640 /* Look for the right subpage or the subpage wildcard*/
6641 if ((page_index->subpage != subpage)
6642 && (subpage != SMS_SUBPAGE_ALL))
6645 /* Make sure the page is supported for this dev type */
6646 if (lun->be_lun->lun_type == T_DIRECT &&
6647 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6649 if (lun->be_lun->lun_type == T_PROCESSOR &&
6650 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6652 if (lun->be_lun->lun_type == T_CDROM &&
6653 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6657 * Call the handler, if it exists, to update the
6658 * page to the latest values.
6660 if (page_index->sense_handler != NULL)
6661 page_index->sense_handler(ctsio, page_index,pc);
6663 memcpy(ctsio->kern_data_ptr + data_used,
6664 page_index->page_data +
6665 (page_index->page_len * pc),
6666 page_index->page_len);
6667 data_used += page_index->page_len;
6673 ctl_set_success(ctsio);
6674 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6675 ctsio->be_move_done = ctl_config_move_done;
6676 ctl_datamove((union ctl_io *)ctsio);
6677 return (CTL_RETVAL_COMPLETE);
6681 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6682 struct ctl_page_index *page_index,
6685 struct ctl_lun *lun = CTL_LUN(ctsio);
6686 struct scsi_log_param_header *phdr;
6690 data = page_index->page_data;
6692 if (lun->backend->lun_attr != NULL &&
6693 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6695 phdr = (struct scsi_log_param_header *)data;
6696 scsi_ulto2b(0x0001, phdr->param_code);
6697 phdr->param_control = SLP_LBIN | SLP_LP;
6698 phdr->param_len = 8;
6699 data = (uint8_t *)(phdr + 1);
6700 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6701 data[4] = 0x02; /* per-pool */
6702 data += phdr->param_len;
6705 if (lun->backend->lun_attr != NULL &&
6706 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6708 phdr = (struct scsi_log_param_header *)data;
6709 scsi_ulto2b(0x0002, phdr->param_code);
6710 phdr->param_control = SLP_LBIN | SLP_LP;
6711 phdr->param_len = 8;
6712 data = (uint8_t *)(phdr + 1);
6713 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6714 data[4] = 0x01; /* per-LUN */
6715 data += phdr->param_len;
6718 if (lun->backend->lun_attr != NULL &&
6719 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6721 phdr = (struct scsi_log_param_header *)data;
6722 scsi_ulto2b(0x00f1, phdr->param_code);
6723 phdr->param_control = SLP_LBIN | SLP_LP;
6724 phdr->param_len = 8;
6725 data = (uint8_t *)(phdr + 1);
6726 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6727 data[4] = 0x02; /* per-pool */
6728 data += phdr->param_len;
6731 if (lun->backend->lun_attr != NULL &&
6732 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6734 phdr = (struct scsi_log_param_header *)data;
6735 scsi_ulto2b(0x00f2, phdr->param_code);
6736 phdr->param_control = SLP_LBIN | SLP_LP;
6737 phdr->param_len = 8;
6738 data = (uint8_t *)(phdr + 1);
6739 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6740 data[4] = 0x02; /* per-pool */
6741 data += phdr->param_len;
6744 page_index->page_len = data - page_index->page_data;
6749 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6750 struct ctl_page_index *page_index,
6753 struct ctl_lun *lun = CTL_LUN(ctsio);
6754 struct stat_page *data;
6757 data = (struct stat_page *)page_index->page_data;
6759 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6760 data->sap.hdr.param_control = SLP_LBIN;
6761 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6762 sizeof(struct scsi_log_param_header);
6763 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6764 data->sap.read_num);
6765 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6766 data->sap.write_num);
6767 if (lun->be_lun->blocksize > 0) {
6768 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6769 lun->be_lun->blocksize, data->sap.recvieved_lba);
6770 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6771 lun->be_lun->blocksize, data->sap.transmitted_lba);
6773 t = &lun->stats.time[CTL_STATS_READ];
6774 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6775 data->sap.read_int);
6776 t = &lun->stats.time[CTL_STATS_WRITE];
6777 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6778 data->sap.write_int);
6779 scsi_u64to8b(0, data->sap.weighted_num);
6780 scsi_u64to8b(0, data->sap.weighted_int);
6781 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6782 data->it.hdr.param_control = SLP_LBIN;
6783 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6784 sizeof(struct scsi_log_param_header);
6786 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6788 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6789 data->it.hdr.param_control = SLP_LBIN;
6790 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6791 sizeof(struct scsi_log_param_header);
6792 scsi_ulto4b(3, data->ti.exponent);
6793 scsi_ulto4b(1, data->ti.integer);
6798 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6799 struct ctl_page_index *page_index,
6802 struct ctl_lun *lun = CTL_LUN(ctsio);
6803 struct scsi_log_informational_exceptions *data;
6805 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6807 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6808 data->hdr.param_control = SLP_LBIN;
6809 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6810 sizeof(struct scsi_log_param_header);
6811 data->ie_asc = lun->ie_asc;
6812 data->ie_ascq = lun->ie_ascq;
6813 data->temperature = 0xff;
6818 ctl_log_sense(struct ctl_scsiio *ctsio)
6820 struct ctl_lun *lun = CTL_LUN(ctsio);
6821 int i, pc, page_code, subpage;
6822 int alloc_len, total_len;
6823 struct ctl_page_index *page_index;
6824 struct scsi_log_sense *cdb;
6825 struct scsi_log_header *header;
6827 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6829 cdb = (struct scsi_log_sense *)ctsio->cdb;
6830 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6831 page_code = cdb->page & SLS_PAGE_CODE;
6832 subpage = cdb->subpage;
6833 alloc_len = scsi_2btoul(cdb->length);
6836 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6837 page_index = &lun->log_pages.index[i];
6839 /* Look for the right page code */
6840 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6843 /* Look for the right subpage or the subpage wildcard*/
6844 if (page_index->subpage != subpage)
6849 if (i >= CTL_NUM_LOG_PAGES) {
6850 ctl_set_invalid_field(ctsio,
6856 ctl_done((union ctl_io *)ctsio);
6857 return (CTL_RETVAL_COMPLETE);
6860 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6862 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6863 ctsio->kern_sg_entries = 0;
6864 ctsio->kern_data_resid = 0;
6865 ctsio->kern_rel_offset = 0;
6866 if (total_len < alloc_len) {
6867 ctsio->residual = alloc_len - total_len;
6868 ctsio->kern_data_len = total_len;
6869 ctsio->kern_total_len = total_len;
6871 ctsio->residual = 0;
6872 ctsio->kern_data_len = alloc_len;
6873 ctsio->kern_total_len = alloc_len;
6876 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6877 header->page = page_index->page_code;
6878 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6879 header->page |= SL_DS;
6880 if (page_index->subpage) {
6881 header->page |= SL_SPF;
6882 header->subpage = page_index->subpage;
6884 scsi_ulto2b(page_index->page_len, header->datalen);
6887 * Call the handler, if it exists, to update the
6888 * page to the latest values.
6890 if (page_index->sense_handler != NULL)
6891 page_index->sense_handler(ctsio, page_index, pc);
6893 memcpy(header + 1, page_index->page_data, page_index->page_len);
6895 ctl_set_success(ctsio);
6896 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6897 ctsio->be_move_done = ctl_config_move_done;
6898 ctl_datamove((union ctl_io *)ctsio);
6899 return (CTL_RETVAL_COMPLETE);
6903 ctl_read_capacity(struct ctl_scsiio *ctsio)
6905 struct ctl_lun *lun = CTL_LUN(ctsio);
6906 struct scsi_read_capacity *cdb;
6907 struct scsi_read_capacity_data *data;
6910 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6912 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6914 lba = scsi_4btoul(cdb->addr);
6915 if (((cdb->pmi & SRC_PMI) == 0)
6917 ctl_set_invalid_field(/*ctsio*/ ctsio,
6923 ctl_done((union ctl_io *)ctsio);
6924 return (CTL_RETVAL_COMPLETE);
6927 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6928 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6929 ctsio->residual = 0;
6930 ctsio->kern_data_len = sizeof(*data);
6931 ctsio->kern_total_len = sizeof(*data);
6932 ctsio->kern_data_resid = 0;
6933 ctsio->kern_rel_offset = 0;
6934 ctsio->kern_sg_entries = 0;
6937 * If the maximum LBA is greater than 0xfffffffe, the user must
6938 * issue a SERVICE ACTION IN (16) command, with the read capacity
6939 * serivce action set.
6941 if (lun->be_lun->maxlba > 0xfffffffe)
6942 scsi_ulto4b(0xffffffff, data->addr);
6944 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6947 * XXX KDM this may not be 512 bytes...
6949 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6951 ctl_set_success(ctsio);
6952 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6953 ctsio->be_move_done = ctl_config_move_done;
6954 ctl_datamove((union ctl_io *)ctsio);
6955 return (CTL_RETVAL_COMPLETE);
6959 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6961 struct ctl_lun *lun = CTL_LUN(ctsio);
6962 struct scsi_read_capacity_16 *cdb;
6963 struct scsi_read_capacity_data_long *data;
6967 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6969 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6971 alloc_len = scsi_4btoul(cdb->alloc_len);
6972 lba = scsi_8btou64(cdb->addr);
6974 if ((cdb->reladr & SRC16_PMI)
6976 ctl_set_invalid_field(/*ctsio*/ ctsio,
6982 ctl_done((union ctl_io *)ctsio);
6983 return (CTL_RETVAL_COMPLETE);
6986 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6987 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6989 if (sizeof(*data) < alloc_len) {
6990 ctsio->residual = alloc_len - sizeof(*data);
6991 ctsio->kern_data_len = sizeof(*data);
6992 ctsio->kern_total_len = sizeof(*data);
6994 ctsio->residual = 0;
6995 ctsio->kern_data_len = alloc_len;
6996 ctsio->kern_total_len = alloc_len;
6998 ctsio->kern_data_resid = 0;
6999 ctsio->kern_rel_offset = 0;
7000 ctsio->kern_sg_entries = 0;
7002 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7003 /* XXX KDM this may not be 512 bytes... */
7004 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7005 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7006 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7007 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7008 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7010 ctl_set_success(ctsio);
7011 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7012 ctsio->be_move_done = ctl_config_move_done;
7013 ctl_datamove((union ctl_io *)ctsio);
7014 return (CTL_RETVAL_COMPLETE);
7018 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7020 struct ctl_lun *lun = CTL_LUN(ctsio);
7021 struct scsi_get_lba_status *cdb;
7022 struct scsi_get_lba_status_data *data;
7023 struct ctl_lba_len_flags *lbalen;
7025 uint32_t alloc_len, total_len;
7028 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
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, lba);
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 ctl_softc *softc = CTL_SOFTC(ctsio);
7143 struct ctl_lun *lun = CTL_LUN(ctsio);
7144 struct scsi_maintenance_in *cdb;
7146 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7147 int num_ha_groups, num_target_ports, shared_group;
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 retval = CTL_RETVAL_COMPLETE;
7158 switch (cdb->byte2 & STG_PDF_MASK) {
7159 case STG_PDF_LENGTH:
7162 case STG_PDF_EXTENDED:
7166 ctl_set_invalid_field(/*ctsio*/ ctsio,
7172 ctl_done((union ctl_io *)ctsio);
7176 num_target_ports = 0;
7177 shared_group = (softc->is_single != 0);
7178 mtx_lock(&softc->ctl_lock);
7179 STAILQ_FOREACH(port, &softc->port_list, links) {
7180 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7182 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7185 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7188 mtx_unlock(&softc->ctl_lock);
7189 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7192 total_len = sizeof(struct scsi_target_group_data_extended);
7194 total_len = sizeof(struct scsi_target_group_data);
7195 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7196 (shared_group + num_ha_groups) +
7197 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7199 alloc_len = scsi_4btoul(cdb->length);
7201 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7203 ctsio->kern_sg_entries = 0;
7205 if (total_len < alloc_len) {
7206 ctsio->residual = alloc_len - total_len;
7207 ctsio->kern_data_len = total_len;
7208 ctsio->kern_total_len = total_len;
7210 ctsio->residual = 0;
7211 ctsio->kern_data_len = alloc_len;
7212 ctsio->kern_total_len = alloc_len;
7214 ctsio->kern_data_resid = 0;
7215 ctsio->kern_rel_offset = 0;
7218 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7219 ctsio->kern_data_ptr;
7220 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7221 rtg_ext_ptr->format_type = 0x10;
7222 rtg_ext_ptr->implicit_transition_time = 0;
7223 tpg_desc = &rtg_ext_ptr->groups[0];
7225 rtg_ptr = (struct scsi_target_group_data *)
7226 ctsio->kern_data_ptr;
7227 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7228 tpg_desc = &rtg_ptr->groups[0];
7231 mtx_lock(&softc->ctl_lock);
7232 pg = softc->port_min / softc->port_cnt;
7233 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7234 /* Some shelf is known to be primary. */
7235 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7236 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7237 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7238 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7239 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7240 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7242 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7243 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7244 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7247 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7250 /* No known primary shelf. */
7251 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7252 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7253 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7254 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7255 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7256 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7258 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7262 tpg_desc->pref_state = ts;
7263 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7264 TPG_U_SUP | TPG_T_SUP;
7265 scsi_ulto2b(1, tpg_desc->target_port_group);
7266 tpg_desc->status = TPG_IMPLICIT;
7268 STAILQ_FOREACH(port, &softc->port_list, links) {
7269 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7271 if (!softc->is_single &&
7272 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7274 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7276 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7277 relative_target_port_identifier);
7280 tpg_desc->target_port_count = pc;
7281 tpg_desc = (struct scsi_target_port_group_descriptor *)
7282 &tpg_desc->descriptors[pc];
7284 for (g = 0; g < num_ha_groups; g++) {
7285 tpg_desc->pref_state = (g == pg) ? ts : os;
7286 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7287 TPG_U_SUP | TPG_T_SUP;
7288 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7289 tpg_desc->status = TPG_IMPLICIT;
7291 STAILQ_FOREACH(port, &softc->port_list, links) {
7292 if (port->targ_port < g * softc->port_cnt ||
7293 port->targ_port >= (g + 1) * softc->port_cnt)
7295 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7297 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7299 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7301 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7302 relative_target_port_identifier);
7305 tpg_desc->target_port_count = pc;
7306 tpg_desc = (struct scsi_target_port_group_descriptor *)
7307 &tpg_desc->descriptors[pc];
7309 mtx_unlock(&softc->ctl_lock);
7311 ctl_set_success(ctsio);
7312 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7313 ctsio->be_move_done = ctl_config_move_done;
7314 ctl_datamove((union ctl_io *)ctsio);
7319 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7321 struct ctl_lun *lun = CTL_LUN(ctsio);
7322 struct scsi_report_supported_opcodes *cdb;
7323 const struct ctl_cmd_entry *entry, *sentry;
7324 struct scsi_report_supported_opcodes_all *all;
7325 struct scsi_report_supported_opcodes_descr *descr;
7326 struct scsi_report_supported_opcodes_one *one;
7328 int alloc_len, total_len;
7329 int opcode, service_action, i, j, num;
7331 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7333 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7334 retval = CTL_RETVAL_COMPLETE;
7336 opcode = cdb->requested_opcode;
7337 service_action = scsi_2btoul(cdb->requested_service_action);
7338 switch (cdb->options & RSO_OPTIONS_MASK) {
7339 case RSO_OPTIONS_ALL:
7341 for (i = 0; i < 256; i++) {
7342 entry = &ctl_cmd_table[i];
7343 if (entry->flags & CTL_CMD_FLAG_SA5) {
7344 for (j = 0; j < 32; j++) {
7345 sentry = &((const struct ctl_cmd_entry *)
7347 if (ctl_cmd_applicable(
7348 lun->be_lun->lun_type, sentry))
7352 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7357 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7358 num * sizeof(struct scsi_report_supported_opcodes_descr);
7360 case RSO_OPTIONS_OC:
7361 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7362 ctl_set_invalid_field(/*ctsio*/ ctsio,
7368 ctl_done((union ctl_io *)ctsio);
7369 return (CTL_RETVAL_COMPLETE);
7371 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7373 case RSO_OPTIONS_OC_SA:
7374 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7375 service_action >= 32) {
7376 ctl_set_invalid_field(/*ctsio*/ ctsio,
7382 ctl_done((union ctl_io *)ctsio);
7383 return (CTL_RETVAL_COMPLETE);
7386 case RSO_OPTIONS_OC_ASA:
7387 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7390 ctl_set_invalid_field(/*ctsio*/ ctsio,
7396 ctl_done((union ctl_io *)ctsio);
7397 return (CTL_RETVAL_COMPLETE);
7400 alloc_len = scsi_4btoul(cdb->length);
7402 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7404 ctsio->kern_sg_entries = 0;
7406 if (total_len < alloc_len) {
7407 ctsio->residual = alloc_len - total_len;
7408 ctsio->kern_data_len = total_len;
7409 ctsio->kern_total_len = total_len;
7411 ctsio->residual = 0;
7412 ctsio->kern_data_len = alloc_len;
7413 ctsio->kern_total_len = alloc_len;
7415 ctsio->kern_data_resid = 0;
7416 ctsio->kern_rel_offset = 0;
7418 switch (cdb->options & RSO_OPTIONS_MASK) {
7419 case RSO_OPTIONS_ALL:
7420 all = (struct scsi_report_supported_opcodes_all *)
7421 ctsio->kern_data_ptr;
7423 for (i = 0; i < 256; i++) {
7424 entry = &ctl_cmd_table[i];
7425 if (entry->flags & CTL_CMD_FLAG_SA5) {
7426 for (j = 0; j < 32; j++) {
7427 sentry = &((const struct ctl_cmd_entry *)
7429 if (!ctl_cmd_applicable(
7430 lun->be_lun->lun_type, sentry))
7432 descr = &all->descr[num++];
7434 scsi_ulto2b(j, descr->service_action);
7435 descr->flags = RSO_SERVACTV;
7436 scsi_ulto2b(sentry->length,
7440 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7443 descr = &all->descr[num++];
7445 scsi_ulto2b(0, descr->service_action);
7447 scsi_ulto2b(entry->length, descr->cdb_length);
7451 num * sizeof(struct scsi_report_supported_opcodes_descr),
7454 case RSO_OPTIONS_OC:
7455 one = (struct scsi_report_supported_opcodes_one *)
7456 ctsio->kern_data_ptr;
7457 entry = &ctl_cmd_table[opcode];
7459 case RSO_OPTIONS_OC_SA:
7460 one = (struct scsi_report_supported_opcodes_one *)
7461 ctsio->kern_data_ptr;
7462 entry = &ctl_cmd_table[opcode];
7463 entry = &((const struct ctl_cmd_entry *)
7464 entry->execute)[service_action];
7466 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7468 scsi_ulto2b(entry->length, one->cdb_length);
7469 one->cdb_usage[0] = opcode;
7470 memcpy(&one->cdb_usage[1], entry->usage,
7475 case RSO_OPTIONS_OC_ASA:
7476 one = (struct scsi_report_supported_opcodes_one *)
7477 ctsio->kern_data_ptr;
7478 entry = &ctl_cmd_table[opcode];
7479 if (entry->flags & CTL_CMD_FLAG_SA5) {
7480 entry = &((const struct ctl_cmd_entry *)
7481 entry->execute)[service_action];
7482 } else if (service_action != 0) {
7489 ctl_set_success(ctsio);
7490 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7491 ctsio->be_move_done = ctl_config_move_done;
7492 ctl_datamove((union ctl_io *)ctsio);
7497 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7499 struct scsi_report_supported_tmf *cdb;
7500 struct scsi_report_supported_tmf_ext_data *data;
7502 int alloc_len, total_len;
7504 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7506 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7508 retval = CTL_RETVAL_COMPLETE;
7510 if (cdb->options & RST_REPD)
7511 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7513 total_len = sizeof(struct scsi_report_supported_tmf_data);
7514 alloc_len = scsi_4btoul(cdb->length);
7516 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7518 ctsio->kern_sg_entries = 0;
7520 if (total_len < alloc_len) {
7521 ctsio->residual = alloc_len - total_len;
7522 ctsio->kern_data_len = total_len;
7523 ctsio->kern_total_len = total_len;
7525 ctsio->residual = 0;
7526 ctsio->kern_data_len = alloc_len;
7527 ctsio->kern_total_len = alloc_len;
7529 ctsio->kern_data_resid = 0;
7530 ctsio->kern_rel_offset = 0;
7532 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7533 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7535 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7536 data->length = total_len - 4;
7538 ctl_set_success(ctsio);
7539 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7540 ctsio->be_move_done = ctl_config_move_done;
7541 ctl_datamove((union ctl_io *)ctsio);
7546 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7548 struct scsi_report_timestamp *cdb;
7549 struct scsi_report_timestamp_data *data;
7553 int alloc_len, total_len;
7555 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7557 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7559 retval = CTL_RETVAL_COMPLETE;
7561 total_len = sizeof(struct scsi_report_timestamp_data);
7562 alloc_len = scsi_4btoul(cdb->length);
7564 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7566 ctsio->kern_sg_entries = 0;
7568 if (total_len < alloc_len) {
7569 ctsio->residual = alloc_len - total_len;
7570 ctsio->kern_data_len = total_len;
7571 ctsio->kern_total_len = total_len;
7573 ctsio->residual = 0;
7574 ctsio->kern_data_len = alloc_len;
7575 ctsio->kern_total_len = alloc_len;
7577 ctsio->kern_data_resid = 0;
7578 ctsio->kern_rel_offset = 0;
7580 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7581 scsi_ulto2b(sizeof(*data) - 2, data->length);
7582 data->origin = RTS_ORIG_OUTSIDE;
7584 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7585 scsi_ulto4b(timestamp >> 16, data->timestamp);
7586 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7588 ctl_set_success(ctsio);
7589 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7590 ctsio->be_move_done = ctl_config_move_done;
7591 ctl_datamove((union ctl_io *)ctsio);
7596 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7598 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7599 struct ctl_lun *lun = CTL_LUN(ctsio);
7600 struct scsi_per_res_in *cdb;
7601 int alloc_len, total_len = 0;
7602 /* struct scsi_per_res_in_rsrv in_data; */
7605 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7607 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7609 alloc_len = scsi_2btoul(cdb->length);
7612 mtx_lock(&lun->lun_lock);
7613 switch (cdb->action) {
7614 case SPRI_RK: /* read keys */
7615 total_len = sizeof(struct scsi_per_res_in_keys) +
7617 sizeof(struct scsi_per_res_key);
7619 case SPRI_RR: /* read reservation */
7620 if (lun->flags & CTL_LUN_PR_RESERVED)
7621 total_len = sizeof(struct scsi_per_res_in_rsrv);
7623 total_len = sizeof(struct scsi_per_res_in_header);
7625 case SPRI_RC: /* report capabilities */
7626 total_len = sizeof(struct scsi_per_res_cap);
7628 case SPRI_RS: /* read full status */
7629 total_len = sizeof(struct scsi_per_res_in_header) +
7630 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7634 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7636 mtx_unlock(&lun->lun_lock);
7638 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7640 if (total_len < alloc_len) {
7641 ctsio->residual = alloc_len - total_len;
7642 ctsio->kern_data_len = total_len;
7643 ctsio->kern_total_len = total_len;
7645 ctsio->residual = 0;
7646 ctsio->kern_data_len = alloc_len;
7647 ctsio->kern_total_len = alloc_len;
7650 ctsio->kern_data_resid = 0;
7651 ctsio->kern_rel_offset = 0;
7652 ctsio->kern_sg_entries = 0;
7654 mtx_lock(&lun->lun_lock);
7655 switch (cdb->action) {
7656 case SPRI_RK: { // read keys
7657 struct scsi_per_res_in_keys *res_keys;
7660 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7663 * We had to drop the lock to allocate our buffer, which
7664 * leaves time for someone to come in with another
7665 * persistent reservation. (That is unlikely, though,
7666 * since this should be the only persistent reservation
7667 * command active right now.)
7669 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7670 (lun->pr_key_count *
7671 sizeof(struct scsi_per_res_key)))){
7672 mtx_unlock(&lun->lun_lock);
7673 free(ctsio->kern_data_ptr, M_CTL);
7674 printf("%s: reservation length changed, retrying\n",
7679 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7681 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7682 lun->pr_key_count, res_keys->header.length);
7684 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7685 if ((key = ctl_get_prkey(lun, i)) == 0)
7689 * We used lun->pr_key_count to calculate the
7690 * size to allocate. If it turns out the number of
7691 * initiators with the registered flag set is
7692 * larger than that (i.e. they haven't been kept in
7693 * sync), we've got a problem.
7695 if (key_count >= lun->pr_key_count) {
7699 scsi_u64to8b(key, res_keys->keys[key_count].key);
7704 case SPRI_RR: { // read reservation
7705 struct scsi_per_res_in_rsrv *res;
7706 int tmp_len, header_only;
7708 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7710 scsi_ulto4b(lun->pr_generation, res->header.generation);
7712 if (lun->flags & CTL_LUN_PR_RESERVED)
7714 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7715 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7716 res->header.length);
7719 tmp_len = sizeof(struct scsi_per_res_in_header);
7720 scsi_ulto4b(0, res->header.length);
7725 * We had to drop the lock to allocate our buffer, which
7726 * leaves time for someone to come in with another
7727 * persistent reservation. (That is unlikely, though,
7728 * since this should be the only persistent reservation
7729 * command active right now.)
7731 if (tmp_len != total_len) {
7732 mtx_unlock(&lun->lun_lock);
7733 free(ctsio->kern_data_ptr, M_CTL);
7734 printf("%s: reservation status changed, retrying\n",
7740 * No reservation held, so we're done.
7742 if (header_only != 0)
7746 * If the registration is an All Registrants type, the key
7747 * is 0, since it doesn't really matter.
7749 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7750 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7751 res->data.reservation);
7753 res->data.scopetype = lun->pr_res_type;
7756 case SPRI_RC: //report capabilities
7758 struct scsi_per_res_cap *res_cap;
7761 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7762 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7763 res_cap->flags1 = SPRI_CRH;
7764 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7765 type_mask = SPRI_TM_WR_EX_AR |
7771 scsi_ulto2b(type_mask, res_cap->type_mask);
7774 case SPRI_RS: { // read full status
7775 struct scsi_per_res_in_full *res_status;
7776 struct scsi_per_res_in_full_desc *res_desc;
7777 struct ctl_port *port;
7780 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7783 * We had to drop the lock to allocate our buffer, which
7784 * leaves time for someone to come in with another
7785 * persistent reservation. (That is unlikely, though,
7786 * since this should be the only persistent reservation
7787 * command active right now.)
7789 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7790 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7791 lun->pr_key_count)){
7792 mtx_unlock(&lun->lun_lock);
7793 free(ctsio->kern_data_ptr, M_CTL);
7794 printf("%s: reservation length changed, retrying\n",
7799 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7801 res_desc = &res_status->desc[0];
7802 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7803 if ((key = ctl_get_prkey(lun, i)) == 0)
7806 scsi_u64to8b(key, res_desc->res_key.key);
7807 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7808 (lun->pr_res_idx == i ||
7809 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7810 res_desc->flags = SPRI_FULL_R_HOLDER;
7811 res_desc->scopetype = lun->pr_res_type;
7813 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7814 res_desc->rel_trgt_port_id);
7816 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7818 len = ctl_create_iid(port,
7819 i % CTL_MAX_INIT_PER_PORT,
7820 res_desc->transport_id);
7821 scsi_ulto4b(len, res_desc->additional_length);
7822 res_desc = (struct scsi_per_res_in_full_desc *)
7823 &res_desc->transport_id[len];
7825 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7826 res_status->header.length);
7830 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7832 mtx_unlock(&lun->lun_lock);
7834 ctl_set_success(ctsio);
7835 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7836 ctsio->be_move_done = ctl_config_move_done;
7837 ctl_datamove((union ctl_io *)ctsio);
7838 return (CTL_RETVAL_COMPLETE);
7842 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7846 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7847 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7848 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7849 struct scsi_per_res_out_parms* param)
7851 union ctl_ha_msg persis_io;
7854 mtx_lock(&lun->lun_lock);
7855 if (sa_res_key == 0) {
7856 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7857 /* validate scope and type */
7858 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7860 mtx_unlock(&lun->lun_lock);
7861 ctl_set_invalid_field(/*ctsio*/ ctsio,
7867 ctl_done((union ctl_io *)ctsio);
7871 if (type>8 || type==2 || type==4 || type==0) {
7872 mtx_unlock(&lun->lun_lock);
7873 ctl_set_invalid_field(/*ctsio*/ ctsio,
7879 ctl_done((union ctl_io *)ctsio);
7884 * Unregister everybody else and build UA for
7887 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7888 if (i == residx || ctl_get_prkey(lun, i) == 0)
7891 ctl_clr_prkey(lun, i);
7892 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7894 lun->pr_key_count = 1;
7895 lun->pr_res_type = type;
7896 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7897 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7898 lun->pr_res_idx = residx;
7899 lun->pr_generation++;
7900 mtx_unlock(&lun->lun_lock);
7902 /* send msg to other side */
7903 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7904 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7905 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7906 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7907 persis_io.pr.pr_info.res_type = type;
7908 memcpy(persis_io.pr.pr_info.sa_res_key,
7909 param->serv_act_res_key,
7910 sizeof(param->serv_act_res_key));
7911 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7912 sizeof(persis_io.pr), M_WAITOK);
7914 /* not all registrants */
7915 mtx_unlock(&lun->lun_lock);
7916 free(ctsio->kern_data_ptr, M_CTL);
7917 ctl_set_invalid_field(ctsio,
7923 ctl_done((union ctl_io *)ctsio);
7926 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7927 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7930 if (res_key == sa_res_key) {
7933 * The spec implies this is not good but doesn't
7934 * say what to do. There are two choices either
7935 * generate a res conflict or check condition
7936 * with illegal field in parameter data. Since
7937 * that is what is done when the sa_res_key is
7938 * zero I'll take that approach since this has
7939 * to do with the sa_res_key.
7941 mtx_unlock(&lun->lun_lock);
7942 free(ctsio->kern_data_ptr, M_CTL);
7943 ctl_set_invalid_field(ctsio,
7949 ctl_done((union ctl_io *)ctsio);
7953 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7954 if (ctl_get_prkey(lun, i) != sa_res_key)
7958 ctl_clr_prkey(lun, i);
7959 lun->pr_key_count--;
7960 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7963 mtx_unlock(&lun->lun_lock);
7964 free(ctsio->kern_data_ptr, M_CTL);
7965 ctl_set_reservation_conflict(ctsio);
7966 ctl_done((union ctl_io *)ctsio);
7967 return (CTL_RETVAL_COMPLETE);
7969 lun->pr_generation++;
7970 mtx_unlock(&lun->lun_lock);
7972 /* send msg to other side */
7973 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7974 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7975 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7976 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7977 persis_io.pr.pr_info.res_type = type;
7978 memcpy(persis_io.pr.pr_info.sa_res_key,
7979 param->serv_act_res_key,
7980 sizeof(param->serv_act_res_key));
7981 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7982 sizeof(persis_io.pr), M_WAITOK);
7984 /* Reserved but not all registrants */
7985 /* sa_res_key is res holder */
7986 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7987 /* validate scope and type */
7988 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7990 mtx_unlock(&lun->lun_lock);
7991 ctl_set_invalid_field(/*ctsio*/ ctsio,
7997 ctl_done((union ctl_io *)ctsio);
8001 if (type>8 || type==2 || type==4 || type==0) {
8002 mtx_unlock(&lun->lun_lock);
8003 ctl_set_invalid_field(/*ctsio*/ ctsio,
8009 ctl_done((union ctl_io *)ctsio);
8015 * if sa_res_key != res_key remove all
8016 * registrants w/sa_res_key and generate UA
8017 * for these registrants(Registrations
8018 * Preempted) if it wasn't an exclusive
8019 * reservation generate UA(Reservations
8020 * Preempted) for all other registered nexuses
8021 * if the type has changed. Establish the new
8022 * reservation and holder. If res_key and
8023 * sa_res_key are the same do the above
8024 * except don't unregister the res holder.
8027 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8028 if (i == residx || ctl_get_prkey(lun, i) == 0)
8031 if (sa_res_key == ctl_get_prkey(lun, i)) {
8032 ctl_clr_prkey(lun, i);
8033 lun->pr_key_count--;
8034 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8035 } else if (type != lun->pr_res_type &&
8036 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8037 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8038 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8041 lun->pr_res_type = type;
8042 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8043 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8044 lun->pr_res_idx = residx;
8046 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8047 lun->pr_generation++;
8048 mtx_unlock(&lun->lun_lock);
8050 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8051 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8052 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8053 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8054 persis_io.pr.pr_info.res_type = type;
8055 memcpy(persis_io.pr.pr_info.sa_res_key,
8056 param->serv_act_res_key,
8057 sizeof(param->serv_act_res_key));
8058 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8059 sizeof(persis_io.pr), M_WAITOK);
8062 * sa_res_key is not the res holder just
8063 * remove registrants
8067 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8068 if (sa_res_key != ctl_get_prkey(lun, i))
8072 ctl_clr_prkey(lun, i);
8073 lun->pr_key_count--;
8074 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8078 mtx_unlock(&lun->lun_lock);
8079 free(ctsio->kern_data_ptr, M_CTL);
8080 ctl_set_reservation_conflict(ctsio);
8081 ctl_done((union ctl_io *)ctsio);
8084 lun->pr_generation++;
8085 mtx_unlock(&lun->lun_lock);
8087 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8088 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8089 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8090 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8091 persis_io.pr.pr_info.res_type = type;
8092 memcpy(persis_io.pr.pr_info.sa_res_key,
8093 param->serv_act_res_key,
8094 sizeof(param->serv_act_res_key));
8095 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8096 sizeof(persis_io.pr), M_WAITOK);
8103 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8105 uint64_t sa_res_key;
8108 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8110 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8111 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8112 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8113 if (sa_res_key == 0) {
8115 * Unregister everybody else and build UA for
8118 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8119 if (i == msg->pr.pr_info.residx ||
8120 ctl_get_prkey(lun, i) == 0)
8123 ctl_clr_prkey(lun, i);
8124 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8127 lun->pr_key_count = 1;
8128 lun->pr_res_type = msg->pr.pr_info.res_type;
8129 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8130 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8131 lun->pr_res_idx = msg->pr.pr_info.residx;
8133 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8134 if (sa_res_key == ctl_get_prkey(lun, i))
8137 ctl_clr_prkey(lun, i);
8138 lun->pr_key_count--;
8139 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8143 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8144 if (i == msg->pr.pr_info.residx ||
8145 ctl_get_prkey(lun, i) == 0)
8148 if (sa_res_key == ctl_get_prkey(lun, i)) {
8149 ctl_clr_prkey(lun, i);
8150 lun->pr_key_count--;
8151 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8152 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8153 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8154 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8155 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8158 lun->pr_res_type = msg->pr.pr_info.res_type;
8159 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8160 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8161 lun->pr_res_idx = msg->pr.pr_info.residx;
8163 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8165 lun->pr_generation++;
8171 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8173 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8174 struct ctl_lun *lun = CTL_LUN(ctsio);
8176 u_int32_t param_len;
8177 struct scsi_per_res_out *cdb;
8178 struct scsi_per_res_out_parms* param;
8180 uint64_t res_key, sa_res_key, key;
8182 union ctl_ha_msg persis_io;
8185 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8187 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8188 retval = CTL_RETVAL_COMPLETE;
8191 * We only support whole-LUN scope. The scope & type are ignored for
8192 * register, register and ignore existing key and clear.
8193 * We sometimes ignore scope and type on preempts too!!
8194 * Verify reservation type here as well.
8196 type = cdb->scope_type & SPR_TYPE_MASK;
8197 if ((cdb->action == SPRO_RESERVE)
8198 || (cdb->action == SPRO_RELEASE)) {
8199 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8200 ctl_set_invalid_field(/*ctsio*/ ctsio,
8206 ctl_done((union ctl_io *)ctsio);
8207 return (CTL_RETVAL_COMPLETE);
8210 if (type>8 || type==2 || type==4 || type==0) {
8211 ctl_set_invalid_field(/*ctsio*/ ctsio,
8217 ctl_done((union ctl_io *)ctsio);
8218 return (CTL_RETVAL_COMPLETE);
8222 param_len = scsi_4btoul(cdb->length);
8224 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8225 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8226 ctsio->kern_data_len = param_len;
8227 ctsio->kern_total_len = param_len;
8228 ctsio->kern_data_resid = 0;
8229 ctsio->kern_rel_offset = 0;
8230 ctsio->kern_sg_entries = 0;
8231 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8232 ctsio->be_move_done = ctl_config_move_done;
8233 ctl_datamove((union ctl_io *)ctsio);
8235 return (CTL_RETVAL_COMPLETE);
8238 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8240 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8241 res_key = scsi_8btou64(param->res_key.key);
8242 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8245 * Validate the reservation key here except for SPRO_REG_IGNO
8246 * This must be done for all other service actions
8248 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8249 mtx_lock(&lun->lun_lock);
8250 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8251 if (res_key != key) {
8253 * The current key passed in doesn't match
8254 * the one the initiator previously
8257 mtx_unlock(&lun->lun_lock);
8258 free(ctsio->kern_data_ptr, M_CTL);
8259 ctl_set_reservation_conflict(ctsio);
8260 ctl_done((union ctl_io *)ctsio);
8261 return (CTL_RETVAL_COMPLETE);
8263 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8265 * We are not registered
8267 mtx_unlock(&lun->lun_lock);
8268 free(ctsio->kern_data_ptr, M_CTL);
8269 ctl_set_reservation_conflict(ctsio);
8270 ctl_done((union ctl_io *)ctsio);
8271 return (CTL_RETVAL_COMPLETE);
8272 } else if (res_key != 0) {
8274 * We are not registered and trying to register but
8275 * the register key isn't zero.
8277 mtx_unlock(&lun->lun_lock);
8278 free(ctsio->kern_data_ptr, M_CTL);
8279 ctl_set_reservation_conflict(ctsio);
8280 ctl_done((union ctl_io *)ctsio);
8281 return (CTL_RETVAL_COMPLETE);
8283 mtx_unlock(&lun->lun_lock);
8286 switch (cdb->action & SPRO_ACTION_MASK) {
8288 case SPRO_REG_IGNO: {
8291 printf("Registration received\n");
8295 * We don't support any of these options, as we report in
8296 * the read capabilities request (see
8297 * ctl_persistent_reserve_in(), above).
8299 if ((param->flags & SPR_SPEC_I_PT)
8300 || (param->flags & SPR_ALL_TG_PT)
8301 || (param->flags & SPR_APTPL)) {
8304 if (param->flags & SPR_APTPL)
8306 else if (param->flags & SPR_ALL_TG_PT)
8308 else /* SPR_SPEC_I_PT */
8311 free(ctsio->kern_data_ptr, M_CTL);
8312 ctl_set_invalid_field(ctsio,
8318 ctl_done((union ctl_io *)ctsio);
8319 return (CTL_RETVAL_COMPLETE);
8322 mtx_lock(&lun->lun_lock);
8325 * The initiator wants to clear the
8328 if (sa_res_key == 0) {
8330 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8331 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8332 && ctl_get_prkey(lun, residx) == 0)) {
8333 mtx_unlock(&lun->lun_lock);
8337 ctl_clr_prkey(lun, residx);
8338 lun->pr_key_count--;
8340 if (residx == lun->pr_res_idx) {
8341 lun->flags &= ~CTL_LUN_PR_RESERVED;
8342 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8344 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8345 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8346 lun->pr_key_count) {
8348 * If the reservation is a registrants
8349 * only type we need to generate a UA
8350 * for other registered inits. The
8351 * sense code should be RESERVATIONS
8355 for (i = softc->init_min; i < softc->init_max; i++){
8356 if (ctl_get_prkey(lun, i) == 0)
8359 CTL_UA_RES_RELEASE);
8362 lun->pr_res_type = 0;
8363 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8364 if (lun->pr_key_count==0) {
8365 lun->flags &= ~CTL_LUN_PR_RESERVED;
8366 lun->pr_res_type = 0;
8367 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8370 lun->pr_generation++;
8371 mtx_unlock(&lun->lun_lock);
8373 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8374 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8375 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8376 persis_io.pr.pr_info.residx = residx;
8377 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8378 sizeof(persis_io.pr), M_WAITOK);
8379 } else /* sa_res_key != 0 */ {
8382 * If we aren't registered currently then increment
8383 * the key count and set the registered flag.
8385 ctl_alloc_prkey(lun, residx);
8386 if (ctl_get_prkey(lun, residx) == 0)
8387 lun->pr_key_count++;
8388 ctl_set_prkey(lun, residx, sa_res_key);
8389 lun->pr_generation++;
8390 mtx_unlock(&lun->lun_lock);
8392 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8393 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8394 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8395 persis_io.pr.pr_info.residx = residx;
8396 memcpy(persis_io.pr.pr_info.sa_res_key,
8397 param->serv_act_res_key,
8398 sizeof(param->serv_act_res_key));
8399 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8400 sizeof(persis_io.pr), M_WAITOK);
8407 printf("Reserve executed type %d\n", type);
8409 mtx_lock(&lun->lun_lock);
8410 if (lun->flags & CTL_LUN_PR_RESERVED) {
8412 * if this isn't the reservation holder and it's
8413 * not a "all registrants" type or if the type is
8414 * different then we have a conflict
8416 if ((lun->pr_res_idx != residx
8417 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8418 || lun->pr_res_type != type) {
8419 mtx_unlock(&lun->lun_lock);
8420 free(ctsio->kern_data_ptr, M_CTL);
8421 ctl_set_reservation_conflict(ctsio);
8422 ctl_done((union ctl_io *)ctsio);
8423 return (CTL_RETVAL_COMPLETE);
8425 mtx_unlock(&lun->lun_lock);
8426 } else /* create a reservation */ {
8428 * If it's not an "all registrants" type record
8429 * reservation holder
8431 if (type != SPR_TYPE_WR_EX_AR
8432 && type != SPR_TYPE_EX_AC_AR)
8433 lun->pr_res_idx = residx; /* Res holder */
8435 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8437 lun->flags |= CTL_LUN_PR_RESERVED;
8438 lun->pr_res_type = type;
8440 mtx_unlock(&lun->lun_lock);
8442 /* send msg to other side */
8443 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8444 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8445 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8446 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8447 persis_io.pr.pr_info.res_type = type;
8448 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8449 sizeof(persis_io.pr), M_WAITOK);
8454 mtx_lock(&lun->lun_lock);
8455 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8456 /* No reservation exists return good status */
8457 mtx_unlock(&lun->lun_lock);
8461 * Is this nexus a reservation holder?
8463 if (lun->pr_res_idx != residx
8464 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8466 * not a res holder return good status but
8469 mtx_unlock(&lun->lun_lock);
8473 if (lun->pr_res_type != type) {
8474 mtx_unlock(&lun->lun_lock);
8475 free(ctsio->kern_data_ptr, M_CTL);
8476 ctl_set_illegal_pr_release(ctsio);
8477 ctl_done((union ctl_io *)ctsio);
8478 return (CTL_RETVAL_COMPLETE);
8481 /* okay to release */
8482 lun->flags &= ~CTL_LUN_PR_RESERVED;
8483 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8484 lun->pr_res_type = 0;
8487 * If this isn't an exclusive access reservation and NUAR
8488 * is not set, generate UA for all other registrants.
8490 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8491 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8492 for (i = softc->init_min; i < softc->init_max; i++) {
8493 if (i == residx || ctl_get_prkey(lun, i) == 0)
8495 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8498 mtx_unlock(&lun->lun_lock);
8500 /* Send msg to other side */
8501 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8502 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8503 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8504 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8505 sizeof(persis_io.pr), M_WAITOK);
8509 /* send msg to other side */
8511 mtx_lock(&lun->lun_lock);
8512 lun->flags &= ~CTL_LUN_PR_RESERVED;
8513 lun->pr_res_type = 0;
8514 lun->pr_key_count = 0;
8515 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8517 ctl_clr_prkey(lun, residx);
8518 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8519 if (ctl_get_prkey(lun, i) != 0) {
8520 ctl_clr_prkey(lun, i);
8521 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8523 lun->pr_generation++;
8524 mtx_unlock(&lun->lun_lock);
8526 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8527 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8528 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8529 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8530 sizeof(persis_io.pr), M_WAITOK);
8534 case SPRO_PRE_ABO: {
8537 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8538 residx, ctsio, cdb, param);
8540 return (CTL_RETVAL_COMPLETE);
8544 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8548 free(ctsio->kern_data_ptr, M_CTL);
8549 ctl_set_success(ctsio);
8550 ctl_done((union ctl_io *)ctsio);
8556 * This routine is for handling a message from the other SC pertaining to
8557 * persistent reserve out. All the error checking will have been done
8558 * so only perorming the action need be done here to keep the two
8562 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8564 struct ctl_softc *softc = CTL_SOFTC(io);
8565 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8566 struct ctl_lun *lun;
8568 uint32_t residx, targ_lun;
8570 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8571 mtx_lock(&softc->ctl_lock);
8572 if (targ_lun >= CTL_MAX_LUNS ||
8573 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8574 mtx_unlock(&softc->ctl_lock);
8577 mtx_lock(&lun->lun_lock);
8578 mtx_unlock(&softc->ctl_lock);
8579 if (lun->flags & CTL_LUN_DISABLED) {
8580 mtx_unlock(&lun->lun_lock);
8583 residx = ctl_get_initindex(&msg->hdr.nexus);
8584 switch(msg->pr.pr_info.action) {
8585 case CTL_PR_REG_KEY:
8586 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8587 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8588 lun->pr_key_count++;
8589 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8590 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8591 lun->pr_generation++;
8594 case CTL_PR_UNREG_KEY:
8595 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8596 lun->pr_key_count--;
8598 /* XXX Need to see if the reservation has been released */
8599 /* if so do we need to generate UA? */
8600 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8601 lun->flags &= ~CTL_LUN_PR_RESERVED;
8602 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8604 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8605 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8606 lun->pr_key_count) {
8608 * If the reservation is a registrants
8609 * only type we need to generate a UA
8610 * for other registered inits. The
8611 * sense code should be RESERVATIONS
8615 for (i = softc->init_min; i < softc->init_max; i++) {
8616 if (ctl_get_prkey(lun, i) == 0)
8619 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8622 lun->pr_res_type = 0;
8623 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8624 if (lun->pr_key_count==0) {
8625 lun->flags &= ~CTL_LUN_PR_RESERVED;
8626 lun->pr_res_type = 0;
8627 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8630 lun->pr_generation++;
8633 case CTL_PR_RESERVE:
8634 lun->flags |= CTL_LUN_PR_RESERVED;
8635 lun->pr_res_type = msg->pr.pr_info.res_type;
8636 lun->pr_res_idx = msg->pr.pr_info.residx;
8640 case CTL_PR_RELEASE:
8642 * If this isn't an exclusive access reservation and NUAR
8643 * is not set, generate UA for all other registrants.
8645 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8646 lun->pr_res_type != SPR_TYPE_WR_EX &&
8647 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8648 for (i = softc->init_min; i < softc->init_max; i++)
8649 if (i == residx || ctl_get_prkey(lun, i) == 0)
8651 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8654 lun->flags &= ~CTL_LUN_PR_RESERVED;
8655 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8656 lun->pr_res_type = 0;
8659 case CTL_PR_PREEMPT:
8660 ctl_pro_preempt_other(lun, msg);
8663 lun->flags &= ~CTL_LUN_PR_RESERVED;
8664 lun->pr_res_type = 0;
8665 lun->pr_key_count = 0;
8666 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8668 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8669 if (ctl_get_prkey(lun, i) == 0)
8671 ctl_clr_prkey(lun, i);
8672 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8674 lun->pr_generation++;
8678 mtx_unlock(&lun->lun_lock);
8682 ctl_read_write(struct ctl_scsiio *ctsio)
8684 struct ctl_lun *lun = CTL_LUN(ctsio);
8685 struct ctl_lba_len_flags *lbalen;
8687 uint32_t num_blocks;
8691 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8694 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8695 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8696 switch (ctsio->cdb[0]) {
8699 struct scsi_rw_6 *cdb;
8701 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8703 lba = scsi_3btoul(cdb->addr);
8704 /* only 5 bits are valid in the most significant address byte */
8706 num_blocks = cdb->length;
8708 * This is correct according to SBC-2.
8710 if (num_blocks == 0)
8716 struct scsi_rw_10 *cdb;
8718 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8719 if (cdb->byte2 & SRW10_FUA)
8720 flags |= CTL_LLF_FUA;
8721 if (cdb->byte2 & SRW10_DPO)
8722 flags |= CTL_LLF_DPO;
8723 lba = scsi_4btoul(cdb->addr);
8724 num_blocks = scsi_2btoul(cdb->length);
8727 case WRITE_VERIFY_10: {
8728 struct scsi_write_verify_10 *cdb;
8730 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8731 flags |= CTL_LLF_FUA;
8732 if (cdb->byte2 & SWV_DPO)
8733 flags |= CTL_LLF_DPO;
8734 lba = scsi_4btoul(cdb->addr);
8735 num_blocks = scsi_2btoul(cdb->length);
8740 struct scsi_rw_12 *cdb;
8742 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8743 if (cdb->byte2 & SRW12_FUA)
8744 flags |= CTL_LLF_FUA;
8745 if (cdb->byte2 & SRW12_DPO)
8746 flags |= CTL_LLF_DPO;
8747 lba = scsi_4btoul(cdb->addr);
8748 num_blocks = scsi_4btoul(cdb->length);
8751 case WRITE_VERIFY_12: {
8752 struct scsi_write_verify_12 *cdb;
8754 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8755 flags |= CTL_LLF_FUA;
8756 if (cdb->byte2 & SWV_DPO)
8757 flags |= CTL_LLF_DPO;
8758 lba = scsi_4btoul(cdb->addr);
8759 num_blocks = scsi_4btoul(cdb->length);
8764 struct scsi_rw_16 *cdb;
8766 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8767 if (cdb->byte2 & SRW12_FUA)
8768 flags |= CTL_LLF_FUA;
8769 if (cdb->byte2 & SRW12_DPO)
8770 flags |= CTL_LLF_DPO;
8771 lba = scsi_8btou64(cdb->addr);
8772 num_blocks = scsi_4btoul(cdb->length);
8775 case WRITE_ATOMIC_16: {
8776 struct scsi_write_atomic_16 *cdb;
8778 if (lun->be_lun->atomicblock == 0) {
8779 ctl_set_invalid_opcode(ctsio);
8780 ctl_done((union ctl_io *)ctsio);
8781 return (CTL_RETVAL_COMPLETE);
8784 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8785 if (cdb->byte2 & SRW12_FUA)
8786 flags |= CTL_LLF_FUA;
8787 if (cdb->byte2 & SRW12_DPO)
8788 flags |= CTL_LLF_DPO;
8789 lba = scsi_8btou64(cdb->addr);
8790 num_blocks = scsi_2btoul(cdb->length);
8791 if (num_blocks > lun->be_lun->atomicblock) {
8792 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8793 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8795 ctl_done((union ctl_io *)ctsio);
8796 return (CTL_RETVAL_COMPLETE);
8800 case WRITE_VERIFY_16: {
8801 struct scsi_write_verify_16 *cdb;
8803 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8804 flags |= CTL_LLF_FUA;
8805 if (cdb->byte2 & SWV_DPO)
8806 flags |= CTL_LLF_DPO;
8807 lba = scsi_8btou64(cdb->addr);
8808 num_blocks = scsi_4btoul(cdb->length);
8813 * We got a command we don't support. This shouldn't
8814 * happen, commands should be filtered out above us.
8816 ctl_set_invalid_opcode(ctsio);
8817 ctl_done((union ctl_io *)ctsio);
8819 return (CTL_RETVAL_COMPLETE);
8820 break; /* NOTREACHED */
8824 * The first check is to make sure we're in bounds, the second
8825 * check is to catch wrap-around problems. If the lba + num blocks
8826 * is less than the lba, then we've wrapped around and the block
8827 * range is invalid anyway.
8829 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8830 || ((lba + num_blocks) < lba)) {
8831 ctl_set_lba_out_of_range(ctsio,
8832 MAX(lba, lun->be_lun->maxlba + 1));
8833 ctl_done((union ctl_io *)ctsio);
8834 return (CTL_RETVAL_COMPLETE);
8838 * According to SBC-3, a transfer length of 0 is not an error.
8839 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8840 * translates to 256 blocks for those commands.
8842 if (num_blocks == 0) {
8843 ctl_set_success(ctsio);
8844 ctl_done((union ctl_io *)ctsio);
8845 return (CTL_RETVAL_COMPLETE);
8848 /* Set FUA and/or DPO if caches are disabled. */
8850 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8851 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8853 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8854 flags |= CTL_LLF_FUA;
8857 lbalen = (struct ctl_lba_len_flags *)
8858 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8860 lbalen->len = num_blocks;
8861 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8863 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8864 ctsio->kern_rel_offset = 0;
8866 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8868 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8873 ctl_cnw_cont(union ctl_io *io)
8875 struct ctl_lun *lun = CTL_LUN(io);
8876 struct ctl_scsiio *ctsio;
8877 struct ctl_lba_len_flags *lbalen;
8880 ctsio = &io->scsiio;
8881 ctsio->io_hdr.status = CTL_STATUS_NONE;
8882 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8883 lbalen = (struct ctl_lba_len_flags *)
8884 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8885 lbalen->flags &= ~CTL_LLF_COMPARE;
8886 lbalen->flags |= CTL_LLF_WRITE;
8888 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8889 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8894 ctl_cnw(struct ctl_scsiio *ctsio)
8896 struct ctl_lun *lun = CTL_LUN(ctsio);
8897 struct ctl_lba_len_flags *lbalen;
8899 uint32_t num_blocks;
8902 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8905 switch (ctsio->cdb[0]) {
8906 case COMPARE_AND_WRITE: {
8907 struct scsi_compare_and_write *cdb;
8909 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8910 if (cdb->byte2 & SRW10_FUA)
8911 flags |= CTL_LLF_FUA;
8912 if (cdb->byte2 & SRW10_DPO)
8913 flags |= CTL_LLF_DPO;
8914 lba = scsi_8btou64(cdb->addr);
8915 num_blocks = cdb->length;
8920 * We got a command we don't support. This shouldn't
8921 * happen, commands should be filtered out above us.
8923 ctl_set_invalid_opcode(ctsio);
8924 ctl_done((union ctl_io *)ctsio);
8926 return (CTL_RETVAL_COMPLETE);
8927 break; /* NOTREACHED */
8931 * The first check is to make sure we're in bounds, the second
8932 * check is to catch wrap-around problems. If the lba + num blocks
8933 * is less than the lba, then we've wrapped around and the block
8934 * range is invalid anyway.
8936 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8937 || ((lba + num_blocks) < lba)) {
8938 ctl_set_lba_out_of_range(ctsio,
8939 MAX(lba, lun->be_lun->maxlba + 1));
8940 ctl_done((union ctl_io *)ctsio);
8941 return (CTL_RETVAL_COMPLETE);
8945 * According to SBC-3, a transfer length of 0 is not an error.
8947 if (num_blocks == 0) {
8948 ctl_set_success(ctsio);
8949 ctl_done((union ctl_io *)ctsio);
8950 return (CTL_RETVAL_COMPLETE);
8953 /* Set FUA if write cache is disabled. */
8954 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8955 flags |= CTL_LLF_FUA;
8957 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8958 ctsio->kern_rel_offset = 0;
8961 * Set the IO_CONT flag, so that if this I/O gets passed to
8962 * ctl_data_submit_done(), it'll get passed back to
8963 * ctl_ctl_cnw_cont() for further processing.
8965 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8966 ctsio->io_cont = ctl_cnw_cont;
8968 lbalen = (struct ctl_lba_len_flags *)
8969 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8971 lbalen->len = num_blocks;
8972 lbalen->flags = CTL_LLF_COMPARE | flags;
8974 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8975 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8980 ctl_verify(struct ctl_scsiio *ctsio)
8982 struct ctl_lun *lun = CTL_LUN(ctsio);
8983 struct ctl_lba_len_flags *lbalen;
8985 uint32_t num_blocks;
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 MAX(lba, lun->be_lun->maxlba + 1));
9050 ctl_done((union ctl_io *)ctsio);
9051 return (CTL_RETVAL_COMPLETE);
9055 * According to SBC-3, a transfer length of 0 is not an error.
9057 if (num_blocks == 0) {
9058 ctl_set_success(ctsio);
9059 ctl_done((union ctl_io *)ctsio);
9060 return (CTL_RETVAL_COMPLETE);
9063 lbalen = (struct ctl_lba_len_flags *)
9064 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9066 lbalen->len = num_blocks;
9068 lbalen->flags = CTL_LLF_COMPARE | flags;
9069 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9071 lbalen->flags = CTL_LLF_VERIFY | flags;
9072 ctsio->kern_total_len = 0;
9074 ctsio->kern_rel_offset = 0;
9076 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9077 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9082 ctl_report_luns(struct ctl_scsiio *ctsio)
9084 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9085 struct ctl_port *port = CTL_PORT(ctsio);
9086 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9087 struct scsi_report_luns *cdb;
9088 struct scsi_report_luns_data *lun_data;
9089 int num_filled, num_luns, num_port_luns, retval;
9090 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;
9096 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9099 num_port_luns = port->lun_map ? port->lun_map_size : CTL_MAX_LUNS;
9100 mtx_lock(&softc->ctl_lock);
9101 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9102 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9105 mtx_unlock(&softc->ctl_lock);
9107 switch (cdb->select_report) {
9108 case RPL_REPORT_DEFAULT:
9109 case RPL_REPORT_ALL:
9110 case RPL_REPORT_NONSUBSID:
9112 case RPL_REPORT_WELLKNOWN:
9113 case RPL_REPORT_ADMIN:
9114 case RPL_REPORT_CONGLOM:
9118 ctl_set_invalid_field(ctsio,
9124 ctl_done((union ctl_io *)ctsio);
9126 break; /* NOTREACHED */
9129 alloc_len = scsi_4btoul(cdb->length);
9131 * The initiator has to allocate at least 16 bytes for this request,
9132 * so he can at least get the header and the first LUN. Otherwise
9133 * we reject the request (per SPC-3 rev 14, section 6.21).
9135 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9136 sizeof(struct scsi_report_luns_lundata))) {
9137 ctl_set_invalid_field(ctsio,
9143 ctl_done((union ctl_io *)ctsio);
9147 lun_datalen = sizeof(*lun_data) +
9148 (num_luns * sizeof(struct scsi_report_luns_lundata));
9150 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9151 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9152 ctsio->kern_sg_entries = 0;
9154 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9156 mtx_lock(&softc->ctl_lock);
9157 for (targ_lun_id = 0, num_filled = 0;
9158 targ_lun_id < num_port_luns && num_filled < num_luns;
9160 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9161 if (lun_id == UINT32_MAX)
9163 lun = softc->ctl_luns[lun_id];
9167 be64enc(lun_data->luns[num_filled++].lundata,
9168 ctl_encode_lun(targ_lun_id));
9171 * According to SPC-3, rev 14 section 6.21:
9173 * "The execution of a REPORT LUNS command to any valid and
9174 * installed logical unit shall clear the REPORTED LUNS DATA
9175 * HAS CHANGED unit attention condition for all logical
9176 * units of that target with respect to the requesting
9177 * initiator. A valid and installed logical unit is one
9178 * having a PERIPHERAL QUALIFIER of 000b in the standard
9179 * INQUIRY data (see 6.4.2)."
9181 * If request_lun is NULL, the LUN this report luns command
9182 * was issued to is either disabled or doesn't exist. In that
9183 * case, we shouldn't clear any pending lun change unit
9186 if (request_lun != NULL) {
9187 mtx_lock(&lun->lun_lock);
9188 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9189 mtx_unlock(&lun->lun_lock);
9192 mtx_unlock(&softc->ctl_lock);
9195 * It's quite possible that we've returned fewer LUNs than we allocated
9196 * space for. Trim it.
9198 lun_datalen = sizeof(*lun_data) +
9199 (num_filled * sizeof(struct scsi_report_luns_lundata));
9201 if (lun_datalen < alloc_len) {
9202 ctsio->residual = alloc_len - lun_datalen;
9203 ctsio->kern_data_len = lun_datalen;
9204 ctsio->kern_total_len = lun_datalen;
9206 ctsio->residual = 0;
9207 ctsio->kern_data_len = alloc_len;
9208 ctsio->kern_total_len = alloc_len;
9210 ctsio->kern_data_resid = 0;
9211 ctsio->kern_rel_offset = 0;
9212 ctsio->kern_sg_entries = 0;
9215 * We set this to the actual data length, regardless of how much
9216 * space we actually have to return results. If the user looks at
9217 * this value, he'll know whether or not he allocated enough space
9218 * and reissue the command if necessary. We don't support well
9219 * known logical units, so if the user asks for that, return none.
9221 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9224 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9227 ctl_set_success(ctsio);
9228 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9229 ctsio->be_move_done = ctl_config_move_done;
9230 ctl_datamove((union ctl_io *)ctsio);
9235 ctl_request_sense(struct ctl_scsiio *ctsio)
9237 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9238 struct ctl_lun *lun = CTL_LUN(ctsio);
9239 struct scsi_request_sense *cdb;
9240 struct scsi_sense_data *sense_ptr;
9243 u_int sense_len = SSD_FULL_SIZE;
9244 scsi_sense_data_type sense_format;
9245 ctl_ua_type ua_type;
9246 uint8_t asc = 0, ascq = 0;
9248 cdb = (struct scsi_request_sense *)ctsio->cdb;
9250 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9253 * Determine which sense format the user wants.
9255 if (cdb->byte2 & SRS_DESC)
9256 sense_format = SSD_TYPE_DESC;
9258 sense_format = SSD_TYPE_FIXED;
9260 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9261 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9262 ctsio->kern_sg_entries = 0;
9265 * struct scsi_sense_data, which is currently set to 256 bytes, is
9266 * larger than the largest allowed value for the length field in the
9267 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9269 ctsio->residual = 0;
9270 ctsio->kern_data_len = cdb->length;
9271 ctsio->kern_total_len = cdb->length;
9273 ctsio->kern_data_resid = 0;
9274 ctsio->kern_rel_offset = 0;
9275 ctsio->kern_sg_entries = 0;
9278 * If we don't have a LUN, we don't have any pending sense.
9281 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9282 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9283 /* "Logical unit not supported" */
9284 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9285 /*current_error*/ 1,
9286 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9294 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9296 * Check for pending sense, and then for pending unit attentions.
9297 * Pending sense gets returned first, then pending unit attentions.
9299 mtx_lock(&lun->lun_lock);
9301 if (ctl_is_set(lun->have_ca, initidx)) {
9302 scsi_sense_data_type stored_format;
9305 * Check to see which sense format was used for the stored
9308 stored_format = scsi_sense_type(&lun->pending_sense[initidx]);
9311 * If the user requested a different sense format than the
9312 * one we stored, then we need to convert it to the other
9313 * format. If we're going from descriptor to fixed format
9314 * sense data, we may lose things in translation, depending
9315 * on what options were used.
9317 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9318 * for some reason we'll just copy it out as-is.
9320 if ((stored_format == SSD_TYPE_FIXED)
9321 && (sense_format == SSD_TYPE_DESC))
9322 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9323 &lun->pending_sense[initidx],
9324 (struct scsi_sense_data_desc *)sense_ptr);
9325 else if ((stored_format == SSD_TYPE_DESC)
9326 && (sense_format == SSD_TYPE_FIXED))
9327 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9328 &lun->pending_sense[initidx],
9329 (struct scsi_sense_data_fixed *)sense_ptr);
9331 memcpy(sense_ptr, &lun->pending_sense[initidx],
9332 MIN(sizeof(*sense_ptr),
9333 sizeof(lun->pending_sense[initidx])));
9335 ctl_clear_mask(lun->have_ca, initidx);
9339 if (have_error == 0) {
9340 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9342 if (ua_type != CTL_UA_NONE)
9345 if (have_error == 0) {
9347 * Report informational exception if have one and allowed.
9349 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9351 ascq = lun->ie_ascq;
9353 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9354 /*current_error*/ 1,
9355 /*sense_key*/ SSD_KEY_NO_SENSE,
9360 mtx_unlock(&lun->lun_lock);
9364 * We report the SCSI status as OK, since the status of the command
9365 * itself is OK. We're reporting sense as parameter data.
9367 ctl_set_success(ctsio);
9368 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9369 ctsio->be_move_done = ctl_config_move_done;
9370 ctl_datamove((union ctl_io *)ctsio);
9371 return (CTL_RETVAL_COMPLETE);
9375 ctl_tur(struct ctl_scsiio *ctsio)
9378 CTL_DEBUG_PRINT(("ctl_tur\n"));
9380 ctl_set_success(ctsio);
9381 ctl_done((union ctl_io *)ctsio);
9383 return (CTL_RETVAL_COMPLETE);
9387 * SCSI VPD page 0x00, the Supported VPD Pages page.
9390 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9392 struct ctl_lun *lun = CTL_LUN(ctsio);
9393 struct scsi_vpd_supported_pages *pages;
9397 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9398 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9399 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9400 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9401 ctsio->kern_sg_entries = 0;
9403 if (sup_page_size < alloc_len) {
9404 ctsio->residual = alloc_len - sup_page_size;
9405 ctsio->kern_data_len = sup_page_size;
9406 ctsio->kern_total_len = sup_page_size;
9408 ctsio->residual = 0;
9409 ctsio->kern_data_len = alloc_len;
9410 ctsio->kern_total_len = alloc_len;
9412 ctsio->kern_data_resid = 0;
9413 ctsio->kern_rel_offset = 0;
9414 ctsio->kern_sg_entries = 0;
9417 * The control device is always connected. The disk device, on the
9418 * other hand, may not be online all the time. Need to change this
9419 * to figure out whether the disk device is actually online or not.
9422 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9423 lun->be_lun->lun_type;
9425 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9428 /* Supported VPD pages */
9429 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9431 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9432 /* Device Identification */
9433 pages->page_list[p++] = SVPD_DEVICE_ID;
9434 /* Extended INQUIRY Data */
9435 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9436 /* Mode Page Policy */
9437 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9439 pages->page_list[p++] = SVPD_SCSI_PORTS;
9440 /* Third-party Copy */
9441 pages->page_list[p++] = SVPD_SCSI_TPC;
9442 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9444 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9445 /* Block Device Characteristics */
9446 pages->page_list[p++] = SVPD_BDC;
9447 /* Logical Block Provisioning */
9448 pages->page_list[p++] = SVPD_LBP;
9452 ctl_set_success(ctsio);
9453 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9454 ctsio->be_move_done = ctl_config_move_done;
9455 ctl_datamove((union ctl_io *)ctsio);
9456 return (CTL_RETVAL_COMPLETE);
9460 * SCSI VPD page 0x80, the Unit Serial Number page.
9463 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9465 struct ctl_lun *lun = CTL_LUN(ctsio);
9466 struct scsi_vpd_unit_serial_number *sn_ptr;
9469 data_len = 4 + CTL_SN_LEN;
9470 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9471 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9472 if (data_len < alloc_len) {
9473 ctsio->residual = alloc_len - data_len;
9474 ctsio->kern_data_len = data_len;
9475 ctsio->kern_total_len = data_len;
9477 ctsio->residual = 0;
9478 ctsio->kern_data_len = alloc_len;
9479 ctsio->kern_total_len = alloc_len;
9481 ctsio->kern_data_resid = 0;
9482 ctsio->kern_rel_offset = 0;
9483 ctsio->kern_sg_entries = 0;
9486 * The control device is always connected. The disk device, on the
9487 * other hand, may not be online all the time. Need to change this
9488 * to figure out whether the disk device is actually online or not.
9491 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9492 lun->be_lun->lun_type;
9494 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9496 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9497 sn_ptr->length = CTL_SN_LEN;
9499 * If we don't have a LUN, we just leave the serial number as
9503 strncpy((char *)sn_ptr->serial_num,
9504 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9506 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9508 ctl_set_success(ctsio);
9509 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9510 ctsio->be_move_done = ctl_config_move_done;
9511 ctl_datamove((union ctl_io *)ctsio);
9512 return (CTL_RETVAL_COMPLETE);
9517 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9520 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9522 struct ctl_lun *lun = CTL_LUN(ctsio);
9523 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9526 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9527 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9528 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9529 ctsio->kern_sg_entries = 0;
9531 if (data_len < alloc_len) {
9532 ctsio->residual = alloc_len - data_len;
9533 ctsio->kern_data_len = data_len;
9534 ctsio->kern_total_len = data_len;
9536 ctsio->residual = 0;
9537 ctsio->kern_data_len = alloc_len;
9538 ctsio->kern_total_len = alloc_len;
9540 ctsio->kern_data_resid = 0;
9541 ctsio->kern_rel_offset = 0;
9542 ctsio->kern_sg_entries = 0;
9545 * The control device is always connected. The disk device, on the
9546 * other hand, may not be online all the time.
9549 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9550 lun->be_lun->lun_type;
9552 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9553 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9554 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9556 * We support head of queue, ordered and simple tags.
9558 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9560 * Volatile cache supported.
9562 eid_ptr->flags3 = SVPD_EID_V_SUP;
9565 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9566 * attention for a particular IT nexus on all LUNs once we report
9567 * it to that nexus once. This bit is required as of SPC-4.
9569 eid_ptr->flags4 = SVPD_EID_LUICLR;
9572 * We support revert to defaults (RTD) bit in MODE SELECT.
9574 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9577 * XXX KDM in order to correctly answer this, we would need
9578 * information from the SIM to determine how much sense data it
9579 * can send. So this would really be a path inquiry field, most
9580 * likely. This can be set to a maximum of 252 according to SPC-4,
9581 * but the hardware may or may not be able to support that much.
9582 * 0 just means that the maximum sense data length is not reported.
9584 eid_ptr->max_sense_length = 0;
9586 ctl_set_success(ctsio);
9587 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9588 ctsio->be_move_done = ctl_config_move_done;
9589 ctl_datamove((union ctl_io *)ctsio);
9590 return (CTL_RETVAL_COMPLETE);
9594 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9596 struct ctl_lun *lun = CTL_LUN(ctsio);
9597 struct scsi_vpd_mode_page_policy *mpp_ptr;
9600 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9601 sizeof(struct scsi_vpd_mode_page_policy_descr);
9603 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9604 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9605 ctsio->kern_sg_entries = 0;
9607 if (data_len < alloc_len) {
9608 ctsio->residual = alloc_len - data_len;
9609 ctsio->kern_data_len = data_len;
9610 ctsio->kern_total_len = data_len;
9612 ctsio->residual = 0;
9613 ctsio->kern_data_len = alloc_len;
9614 ctsio->kern_total_len = alloc_len;
9616 ctsio->kern_data_resid = 0;
9617 ctsio->kern_rel_offset = 0;
9618 ctsio->kern_sg_entries = 0;
9621 * The control device is always connected. The disk device, on the
9622 * other hand, may not be online all the time.
9625 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9626 lun->be_lun->lun_type;
9628 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9629 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9630 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9631 mpp_ptr->descr[0].page_code = 0x3f;
9632 mpp_ptr->descr[0].subpage_code = 0xff;
9633 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9635 ctl_set_success(ctsio);
9636 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9637 ctsio->be_move_done = ctl_config_move_done;
9638 ctl_datamove((union ctl_io *)ctsio);
9639 return (CTL_RETVAL_COMPLETE);
9643 * SCSI VPD page 0x83, the Device Identification page.
9646 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9648 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9649 struct ctl_port *port = CTL_PORT(ctsio);
9650 struct ctl_lun *lun = CTL_LUN(ctsio);
9651 struct scsi_vpd_device_id *devid_ptr;
9652 struct scsi_vpd_id_descriptor *desc;
9656 data_len = sizeof(struct scsi_vpd_device_id) +
9657 sizeof(struct scsi_vpd_id_descriptor) +
9658 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9659 sizeof(struct scsi_vpd_id_descriptor) +
9660 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9661 if (lun && lun->lun_devid)
9662 data_len += lun->lun_devid->len;
9663 if (port && port->port_devid)
9664 data_len += port->port_devid->len;
9665 if (port && port->target_devid)
9666 data_len += port->target_devid->len;
9668 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9669 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9670 ctsio->kern_sg_entries = 0;
9672 if (data_len < alloc_len) {
9673 ctsio->residual = alloc_len - data_len;
9674 ctsio->kern_data_len = data_len;
9675 ctsio->kern_total_len = data_len;
9677 ctsio->residual = 0;
9678 ctsio->kern_data_len = alloc_len;
9679 ctsio->kern_total_len = alloc_len;
9681 ctsio->kern_data_resid = 0;
9682 ctsio->kern_rel_offset = 0;
9683 ctsio->kern_sg_entries = 0;
9686 * The control device is always connected. The disk device, on the
9687 * other hand, may not be online all the time.
9690 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9691 lun->be_lun->lun_type;
9693 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9694 devid_ptr->page_code = SVPD_DEVICE_ID;
9695 scsi_ulto2b(data_len - 4, devid_ptr->length);
9697 if (port && port->port_type == CTL_PORT_FC)
9698 proto = SCSI_PROTO_FC << 4;
9699 else if (port && port->port_type == CTL_PORT_ISCSI)
9700 proto = SCSI_PROTO_ISCSI << 4;
9702 proto = SCSI_PROTO_SPI << 4;
9703 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9706 * We're using a LUN association here. i.e., this device ID is a
9707 * per-LUN identifier.
9709 if (lun && lun->lun_devid) {
9710 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9711 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9712 lun->lun_devid->len);
9716 * This is for the WWPN which is a port association.
9718 if (port && port->port_devid) {
9719 memcpy(desc, port->port_devid->data, port->port_devid->len);
9720 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9721 port->port_devid->len);
9725 * This is for the Relative Target Port(type 4h) identifier
9727 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9728 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9729 SVPD_ID_TYPE_RELTARG;
9731 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9732 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9733 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9736 * This is for the Target Port Group(type 5h) identifier
9738 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9739 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9740 SVPD_ID_TYPE_TPORTGRP;
9742 if (softc->is_single ||
9743 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9746 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9747 scsi_ulto2b(g, &desc->identifier[2]);
9748 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9749 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9752 * This is for the Target identifier
9754 if (port && port->target_devid) {
9755 memcpy(desc, port->target_devid->data, port->target_devid->len);
9758 ctl_set_success(ctsio);
9759 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9760 ctsio->be_move_done = ctl_config_move_done;
9761 ctl_datamove((union ctl_io *)ctsio);
9762 return (CTL_RETVAL_COMPLETE);
9766 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9768 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9769 struct ctl_lun *lun = CTL_LUN(ctsio);
9770 struct scsi_vpd_scsi_ports *sp;
9771 struct scsi_vpd_port_designation *pd;
9772 struct scsi_vpd_port_designation_cont *pdc;
9773 struct ctl_port *port;
9774 int data_len, num_target_ports, iid_len, id_len;
9776 num_target_ports = 0;
9779 mtx_lock(&softc->ctl_lock);
9780 STAILQ_FOREACH(port, &softc->port_list, links) {
9781 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9784 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9787 if (port->init_devid)
9788 iid_len += port->init_devid->len;
9789 if (port->port_devid)
9790 id_len += port->port_devid->len;
9792 mtx_unlock(&softc->ctl_lock);
9794 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9795 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9796 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9797 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9798 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9799 ctsio->kern_sg_entries = 0;
9801 if (data_len < alloc_len) {
9802 ctsio->residual = alloc_len - data_len;
9803 ctsio->kern_data_len = data_len;
9804 ctsio->kern_total_len = data_len;
9806 ctsio->residual = 0;
9807 ctsio->kern_data_len = alloc_len;
9808 ctsio->kern_total_len = alloc_len;
9810 ctsio->kern_data_resid = 0;
9811 ctsio->kern_rel_offset = 0;
9812 ctsio->kern_sg_entries = 0;
9815 * The control device is always connected. The disk device, on the
9816 * other hand, may not be online all the time. Need to change this
9817 * to figure out whether the disk device is actually online or not.
9820 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9821 lun->be_lun->lun_type;
9823 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9825 sp->page_code = SVPD_SCSI_PORTS;
9826 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9828 pd = &sp->design[0];
9830 mtx_lock(&softc->ctl_lock);
9831 STAILQ_FOREACH(port, &softc->port_list, links) {
9832 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9835 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9837 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9838 if (port->init_devid) {
9839 iid_len = port->init_devid->len;
9840 memcpy(pd->initiator_transportid,
9841 port->init_devid->data, port->init_devid->len);
9844 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9845 pdc = (struct scsi_vpd_port_designation_cont *)
9846 (&pd->initiator_transportid[iid_len]);
9847 if (port->port_devid) {
9848 id_len = port->port_devid->len;
9849 memcpy(pdc->target_port_descriptors,
9850 port->port_devid->data, port->port_devid->len);
9853 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9854 pd = (struct scsi_vpd_port_designation *)
9855 ((uint8_t *)pdc->target_port_descriptors + id_len);
9857 mtx_unlock(&softc->ctl_lock);
9859 ctl_set_success(ctsio);
9860 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9861 ctsio->be_move_done = ctl_config_move_done;
9862 ctl_datamove((union ctl_io *)ctsio);
9863 return (CTL_RETVAL_COMPLETE);
9867 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9869 struct ctl_lun *lun = CTL_LUN(ctsio);
9870 struct scsi_vpd_block_limits *bl_ptr;
9873 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9874 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9875 ctsio->kern_sg_entries = 0;
9877 if (sizeof(*bl_ptr) < alloc_len) {
9878 ctsio->residual = alloc_len - sizeof(*bl_ptr);
9879 ctsio->kern_data_len = sizeof(*bl_ptr);
9880 ctsio->kern_total_len = sizeof(*bl_ptr);
9882 ctsio->residual = 0;
9883 ctsio->kern_data_len = alloc_len;
9884 ctsio->kern_total_len = alloc_len;
9886 ctsio->kern_data_resid = 0;
9887 ctsio->kern_rel_offset = 0;
9888 ctsio->kern_sg_entries = 0;
9891 * The control device is always connected. The disk device, on the
9892 * other hand, may not be online all the time. Need to change this
9893 * to figure out whether the disk device is actually online or not.
9896 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9897 lun->be_lun->lun_type;
9899 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9901 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9902 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9903 bl_ptr->max_cmp_write_len = 0xff;
9904 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9906 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9907 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9909 ctl_get_opt_number(&lun->be_lun->options,
9910 "unmap_max_lba", &ival);
9911 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9913 ctl_get_opt_number(&lun->be_lun->options,
9914 "unmap_max_descr", &ival);
9915 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9916 if (lun->be_lun->ublockexp != 0) {
9917 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9918 bl_ptr->opt_unmap_grain);
9919 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9920 bl_ptr->unmap_grain_align);
9923 scsi_ulto4b(lun->be_lun->atomicblock,
9924 bl_ptr->max_atomic_transfer_length);
9925 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9926 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9927 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9928 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9930 ctl_get_opt_number(&lun->be_lun->options, "write_same_max_lba", &ival);
9931 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9934 ctl_set_success(ctsio);
9935 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9936 ctsio->be_move_done = ctl_config_move_done;
9937 ctl_datamove((union ctl_io *)ctsio);
9938 return (CTL_RETVAL_COMPLETE);
9942 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9944 struct ctl_lun *lun = CTL_LUN(ctsio);
9945 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9949 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9950 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9951 ctsio->kern_sg_entries = 0;
9953 if (sizeof(*bdc_ptr) < alloc_len) {
9954 ctsio->residual = alloc_len - sizeof(*bdc_ptr);
9955 ctsio->kern_data_len = sizeof(*bdc_ptr);
9956 ctsio->kern_total_len = sizeof(*bdc_ptr);
9958 ctsio->residual = 0;
9959 ctsio->kern_data_len = alloc_len;
9960 ctsio->kern_total_len = alloc_len;
9962 ctsio->kern_data_resid = 0;
9963 ctsio->kern_rel_offset = 0;
9964 ctsio->kern_sg_entries = 0;
9967 * The control device is always connected. The disk device, on the
9968 * other hand, may not be online all the time. Need to change this
9969 * to figure out whether the disk device is actually online or not.
9972 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9973 lun->be_lun->lun_type;
9975 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9976 bdc_ptr->page_code = SVPD_BDC;
9977 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9979 (value = ctl_get_opt(&lun->be_lun->options, "rpm")) != NULL)
9980 i = strtol(value, NULL, 0);
9982 i = CTL_DEFAULT_ROTATION_RATE;
9983 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9985 (value = ctl_get_opt(&lun->be_lun->options, "formfactor")) != NULL)
9986 i = strtol(value, NULL, 0);
9989 bdc_ptr->wab_wac_ff = (i & 0x0f);
9990 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9992 ctl_set_success(ctsio);
9993 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9994 ctsio->be_move_done = ctl_config_move_done;
9995 ctl_datamove((union ctl_io *)ctsio);
9996 return (CTL_RETVAL_COMPLETE);
10000 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
10002 struct ctl_lun *lun = CTL_LUN(ctsio);
10003 struct scsi_vpd_logical_block_prov *lbp_ptr;
10006 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
10007 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
10008 ctsio->kern_sg_entries = 0;
10010 if (sizeof(*lbp_ptr) < alloc_len) {
10011 ctsio->residual = alloc_len - sizeof(*lbp_ptr);
10012 ctsio->kern_data_len = sizeof(*lbp_ptr);
10013 ctsio->kern_total_len = sizeof(*lbp_ptr);
10015 ctsio->residual = 0;
10016 ctsio->kern_data_len = alloc_len;
10017 ctsio->kern_total_len = alloc_len;
10019 ctsio->kern_data_resid = 0;
10020 ctsio->kern_rel_offset = 0;
10021 ctsio->kern_sg_entries = 0;
10024 * The control device is always connected. The disk device, on the
10025 * other hand, may not be online all the time. Need to change this
10026 * to figure out whether the disk device is actually online or not.
10029 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10030 lun->be_lun->lun_type;
10032 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
10034 lbp_ptr->page_code = SVPD_LBP;
10035 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
10036 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10037 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10038 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10039 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10040 value = ctl_get_opt(&lun->be_lun->options, "provisioning_type");
10041 if (value != NULL) {
10042 if (strcmp(value, "resource") == 0)
10043 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10044 else if (strcmp(value, "thin") == 0)
10045 lbp_ptr->prov_type = SVPD_LBP_THIN;
10047 lbp_ptr->prov_type = SVPD_LBP_THIN;
10050 ctl_set_success(ctsio);
10051 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10052 ctsio->be_move_done = ctl_config_move_done;
10053 ctl_datamove((union ctl_io *)ctsio);
10054 return (CTL_RETVAL_COMPLETE);
10058 * INQUIRY with the EVPD bit set.
10061 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10063 struct ctl_lun *lun = CTL_LUN(ctsio);
10064 struct scsi_inquiry *cdb;
10065 int alloc_len, retval;
10067 cdb = (struct scsi_inquiry *)ctsio->cdb;
10068 alloc_len = scsi_2btoul(cdb->length);
10070 switch (cdb->page_code) {
10071 case SVPD_SUPPORTED_PAGES:
10072 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10074 case SVPD_UNIT_SERIAL_NUMBER:
10075 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10077 case SVPD_DEVICE_ID:
10078 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10080 case SVPD_EXTENDED_INQUIRY_DATA:
10081 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10083 case SVPD_MODE_PAGE_POLICY:
10084 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10086 case SVPD_SCSI_PORTS:
10087 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10089 case SVPD_SCSI_TPC:
10090 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10092 case SVPD_BLOCK_LIMITS:
10093 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10095 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10098 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10100 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10103 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10105 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10109 ctl_set_invalid_field(ctsio,
10115 ctl_done((union ctl_io *)ctsio);
10116 retval = CTL_RETVAL_COMPLETE;
10124 * Standard INQUIRY data.
10127 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10129 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10130 struct ctl_port *port = CTL_PORT(ctsio);
10131 struct ctl_lun *lun = CTL_LUN(ctsio);
10132 struct scsi_inquiry_data *inq_ptr;
10133 struct scsi_inquiry *cdb;
10135 uint32_t alloc_len, data_len;
10136 ctl_port_type port_type;
10138 port_type = port->port_type;
10139 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10140 port_type = CTL_PORT_SCSI;
10142 cdb = (struct scsi_inquiry *)ctsio->cdb;
10143 alloc_len = scsi_2btoul(cdb->length);
10146 * We malloc the full inquiry data size here and fill it
10147 * in. If the user only asks for less, we'll give him
10150 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10151 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10152 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10153 ctsio->kern_sg_entries = 0;
10154 ctsio->kern_data_resid = 0;
10155 ctsio->kern_rel_offset = 0;
10157 if (data_len < alloc_len) {
10158 ctsio->residual = alloc_len - data_len;
10159 ctsio->kern_data_len = data_len;
10160 ctsio->kern_total_len = data_len;
10162 ctsio->residual = 0;
10163 ctsio->kern_data_len = alloc_len;
10164 ctsio->kern_total_len = alloc_len;
10168 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10169 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10170 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10171 lun->be_lun->lun_type;
10173 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10174 lun->be_lun->lun_type;
10176 if (lun->flags & CTL_LUN_REMOVABLE)
10177 inq_ptr->dev_qual2 |= SID_RMB;
10179 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10181 /* RMB in byte 2 is 0 */
10182 inq_ptr->version = SCSI_REV_SPC5;
10185 * According to SAM-3, even if a device only supports a single
10186 * level of LUN addressing, it should still set the HISUP bit:
10188 * 4.9.1 Logical unit numbers overview
10190 * All logical unit number formats described in this standard are
10191 * hierarchical in structure even when only a single level in that
10192 * hierarchy is used. The HISUP bit shall be set to one in the
10193 * standard INQUIRY data (see SPC-2) when any logical unit number
10194 * format described in this standard is used. Non-hierarchical
10195 * formats are outside the scope of this standard.
10197 * Therefore we set the HiSup bit here.
10199 * The response format is 2, per SPC-3.
10201 inq_ptr->response_format = SID_HiSup | 2;
10203 inq_ptr->additional_length = data_len -
10204 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10205 CTL_DEBUG_PRINT(("additional_length = %d\n",
10206 inq_ptr->additional_length));
10208 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10209 if (port_type == CTL_PORT_SCSI)
10210 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10211 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10212 inq_ptr->flags = SID_CmdQue;
10213 if (port_type == CTL_PORT_SCSI)
10214 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10217 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10218 * We have 8 bytes for the vendor name, and 16 bytes for the device
10219 * name and 4 bytes for the revision.
10221 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10222 "vendor")) == NULL) {
10223 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10225 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10226 strncpy(inq_ptr->vendor, val,
10227 min(sizeof(inq_ptr->vendor), strlen(val)));
10230 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10231 sizeof(inq_ptr->product));
10232 } else if ((val = ctl_get_opt(&lun->be_lun->options, "product")) == NULL) {
10233 switch (lun->be_lun->lun_type) {
10235 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10236 sizeof(inq_ptr->product));
10239 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10240 sizeof(inq_ptr->product));
10243 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10244 sizeof(inq_ptr->product));
10247 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10248 sizeof(inq_ptr->product));
10252 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10253 strncpy(inq_ptr->product, val,
10254 min(sizeof(inq_ptr->product), strlen(val)));
10258 * XXX make this a macro somewhere so it automatically gets
10259 * incremented when we make changes.
10261 if (lun == NULL || (val = ctl_get_opt(&lun->be_lun->options,
10262 "revision")) == NULL) {
10263 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10265 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10266 strncpy(inq_ptr->revision, val,
10267 min(sizeof(inq_ptr->revision), strlen(val)));
10271 * For parallel SCSI, we support double transition and single
10272 * transition clocking. We also support QAS (Quick Arbitration
10273 * and Selection) and Information Unit transfers on both the
10274 * control and array devices.
10276 if (port_type == CTL_PORT_SCSI)
10277 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10280 /* SAM-6 (no version claimed) */
10281 scsi_ulto2b(0x00C0, inq_ptr->version1);
10282 /* SPC-5 (no version claimed) */
10283 scsi_ulto2b(0x05C0, inq_ptr->version2);
10284 if (port_type == CTL_PORT_FC) {
10285 /* FCP-2 ANSI INCITS.350:2003 */
10286 scsi_ulto2b(0x0917, inq_ptr->version3);
10287 } else if (port_type == CTL_PORT_SCSI) {
10288 /* SPI-4 ANSI INCITS.362:200x */
10289 scsi_ulto2b(0x0B56, inq_ptr->version3);
10290 } else if (port_type == CTL_PORT_ISCSI) {
10291 /* iSCSI (no version claimed) */
10292 scsi_ulto2b(0x0960, inq_ptr->version3);
10293 } else if (port_type == CTL_PORT_SAS) {
10294 /* SAS (no version claimed) */
10295 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10299 /* SBC-4 (no version claimed) */
10300 scsi_ulto2b(0x0600, inq_ptr->version4);
10302 switch (lun->be_lun->lun_type) {
10304 /* SBC-4 (no version claimed) */
10305 scsi_ulto2b(0x0600, inq_ptr->version4);
10310 /* MMC-6 (no version claimed) */
10311 scsi_ulto2b(0x04E0, inq_ptr->version4);
10318 ctl_set_success(ctsio);
10319 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10320 ctsio->be_move_done = ctl_config_move_done;
10321 ctl_datamove((union ctl_io *)ctsio);
10322 return (CTL_RETVAL_COMPLETE);
10326 ctl_inquiry(struct ctl_scsiio *ctsio)
10328 struct scsi_inquiry *cdb;
10331 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10333 cdb = (struct scsi_inquiry *)ctsio->cdb;
10334 if (cdb->byte2 & SI_EVPD)
10335 retval = ctl_inquiry_evpd(ctsio);
10336 else if (cdb->page_code == 0)
10337 retval = ctl_inquiry_std(ctsio);
10339 ctl_set_invalid_field(ctsio,
10345 ctl_done((union ctl_io *)ctsio);
10346 return (CTL_RETVAL_COMPLETE);
10353 ctl_get_config(struct ctl_scsiio *ctsio)
10355 struct ctl_lun *lun = CTL_LUN(ctsio);
10356 struct scsi_get_config_header *hdr;
10357 struct scsi_get_config_feature *feature;
10358 struct scsi_get_config *cdb;
10359 uint32_t alloc_len, data_len;
10362 cdb = (struct scsi_get_config *)ctsio->cdb;
10363 rt = (cdb->rt & SGC_RT_MASK);
10364 starting = scsi_2btoul(cdb->starting_feature);
10365 alloc_len = scsi_2btoul(cdb->length);
10367 data_len = sizeof(struct scsi_get_config_header) +
10368 sizeof(struct scsi_get_config_feature) + 8 +
10369 sizeof(struct scsi_get_config_feature) + 8 +
10370 sizeof(struct scsi_get_config_feature) + 4 +
10371 sizeof(struct scsi_get_config_feature) + 4 +
10372 sizeof(struct scsi_get_config_feature) + 8 +
10373 sizeof(struct scsi_get_config_feature) +
10374 sizeof(struct scsi_get_config_feature) + 4 +
10375 sizeof(struct scsi_get_config_feature) + 4 +
10376 sizeof(struct scsi_get_config_feature) + 4 +
10377 sizeof(struct scsi_get_config_feature) + 4 +
10378 sizeof(struct scsi_get_config_feature) + 4 +
10379 sizeof(struct scsi_get_config_feature) + 4;
10380 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10381 ctsio->kern_sg_entries = 0;
10382 ctsio->kern_data_resid = 0;
10383 ctsio->kern_rel_offset = 0;
10385 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10386 if (lun->flags & CTL_LUN_NO_MEDIA)
10387 scsi_ulto2b(0x0000, hdr->current_profile);
10389 scsi_ulto2b(0x0010, hdr->current_profile);
10390 feature = (struct scsi_get_config_feature *)(hdr + 1);
10392 if (starting > 0x003b)
10394 if (starting > 0x003a)
10396 if (starting > 0x002b)
10398 if (starting > 0x002a)
10400 if (starting > 0x001f)
10402 if (starting > 0x001e)
10404 if (starting > 0x001d)
10406 if (starting > 0x0010)
10408 if (starting > 0x0003)
10410 if (starting > 0x0002)
10412 if (starting > 0x0001)
10414 if (starting > 0x0000)
10418 scsi_ulto2b(0x0000, feature->feature_code);
10419 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10420 feature->add_length = 8;
10421 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10422 feature->feature_data[2] = 0x00;
10423 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10424 feature->feature_data[6] = 0x01;
10425 feature = (struct scsi_get_config_feature *)
10426 &feature->feature_data[feature->add_length];
10429 scsi_ulto2b(0x0001, feature->feature_code);
10430 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10431 feature->add_length = 8;
10432 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10433 feature->feature_data[4] = 0x03;
10434 feature = (struct scsi_get_config_feature *)
10435 &feature->feature_data[feature->add_length];
10438 scsi_ulto2b(0x0002, feature->feature_code);
10439 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10440 feature->add_length = 4;
10441 feature->feature_data[0] = 0x02;
10442 feature = (struct scsi_get_config_feature *)
10443 &feature->feature_data[feature->add_length];
10445 f3: /* Removable Medium */
10446 scsi_ulto2b(0x0003, feature->feature_code);
10447 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10448 feature->add_length = 4;
10449 feature->feature_data[0] = 0x39;
10450 feature = (struct scsi_get_config_feature *)
10451 &feature->feature_data[feature->add_length];
10453 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10456 f10: /* Random Read */
10457 scsi_ulto2b(0x0010, feature->feature_code);
10458 feature->flags = 0x00;
10459 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10460 feature->flags |= SGC_F_CURRENT;
10461 feature->add_length = 8;
10462 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10463 scsi_ulto2b(1, &feature->feature_data[4]);
10464 feature->feature_data[6] = 0x00;
10465 feature = (struct scsi_get_config_feature *)
10466 &feature->feature_data[feature->add_length];
10468 f1d: /* Multi-Read */
10469 scsi_ulto2b(0x001D, feature->feature_code);
10470 feature->flags = 0x00;
10471 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10472 feature->flags |= SGC_F_CURRENT;
10473 feature->add_length = 0;
10474 feature = (struct scsi_get_config_feature *)
10475 &feature->feature_data[feature->add_length];
10478 scsi_ulto2b(0x001E, feature->feature_code);
10479 feature->flags = 0x00;
10480 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10481 feature->flags |= SGC_F_CURRENT;
10482 feature->add_length = 4;
10483 feature->feature_data[0] = 0x00;
10484 feature = (struct scsi_get_config_feature *)
10485 &feature->feature_data[feature->add_length];
10487 f1f: /* DVD Read */
10488 scsi_ulto2b(0x001F, feature->feature_code);
10489 feature->flags = 0x08;
10490 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10491 feature->flags |= SGC_F_CURRENT;
10492 feature->add_length = 4;
10493 feature->feature_data[0] = 0x01;
10494 feature->feature_data[2] = 0x03;
10495 feature = (struct scsi_get_config_feature *)
10496 &feature->feature_data[feature->add_length];
10499 scsi_ulto2b(0x002A, feature->feature_code);
10500 feature->flags = 0x04;
10501 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10502 feature->flags |= SGC_F_CURRENT;
10503 feature->add_length = 4;
10504 feature->feature_data[0] = 0x00;
10505 feature->feature_data[1] = 0x00;
10506 feature = (struct scsi_get_config_feature *)
10507 &feature->feature_data[feature->add_length];
10510 scsi_ulto2b(0x002B, feature->feature_code);
10511 feature->flags = 0x00;
10512 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10513 feature->flags |= SGC_F_CURRENT;
10514 feature->add_length = 4;
10515 feature->feature_data[0] = 0x00;
10516 feature = (struct scsi_get_config_feature *)
10517 &feature->feature_data[feature->add_length];
10519 f3a: /* DVD+RW Dual Layer */
10520 scsi_ulto2b(0x003A, feature->feature_code);
10521 feature->flags = 0x00;
10522 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10523 feature->flags |= SGC_F_CURRENT;
10524 feature->add_length = 4;
10525 feature->feature_data[0] = 0x00;
10526 feature->feature_data[1] = 0x00;
10527 feature = (struct scsi_get_config_feature *)
10528 &feature->feature_data[feature->add_length];
10530 f3b: /* DVD+R Dual Layer */
10531 scsi_ulto2b(0x003B, feature->feature_code);
10532 feature->flags = 0x00;
10533 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10534 feature->flags |= SGC_F_CURRENT;
10535 feature->add_length = 4;
10536 feature->feature_data[0] = 0x00;
10537 feature = (struct scsi_get_config_feature *)
10538 &feature->feature_data[feature->add_length];
10541 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10542 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10543 feature = (struct scsi_get_config_feature *)(hdr + 1);
10544 if (scsi_2btoul(feature->feature_code) == starting)
10545 feature = (struct scsi_get_config_feature *)
10546 &feature->feature_data[feature->add_length];
10547 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10549 scsi_ulto4b(data_len - 4, hdr->data_length);
10550 if (data_len < alloc_len) {
10551 ctsio->residual = alloc_len - data_len;
10552 ctsio->kern_data_len = data_len;
10553 ctsio->kern_total_len = data_len;
10555 ctsio->residual = 0;
10556 ctsio->kern_data_len = alloc_len;
10557 ctsio->kern_total_len = alloc_len;
10560 ctl_set_success(ctsio);
10561 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10562 ctsio->be_move_done = ctl_config_move_done;
10563 ctl_datamove((union ctl_io *)ctsio);
10564 return (CTL_RETVAL_COMPLETE);
10568 ctl_get_event_status(struct ctl_scsiio *ctsio)
10570 struct scsi_get_event_status_header *hdr;
10571 struct scsi_get_event_status *cdb;
10572 uint32_t alloc_len, data_len;
10575 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10576 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10577 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10578 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10579 ctl_done((union ctl_io *)ctsio);
10580 return (CTL_RETVAL_COMPLETE);
10582 notif_class = cdb->notif_class;
10583 alloc_len = scsi_2btoul(cdb->length);
10585 data_len = sizeof(struct scsi_get_event_status_header);
10586 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10587 ctsio->kern_sg_entries = 0;
10588 ctsio->kern_data_resid = 0;
10589 ctsio->kern_rel_offset = 0;
10591 if (data_len < alloc_len) {
10592 ctsio->residual = alloc_len - data_len;
10593 ctsio->kern_data_len = data_len;
10594 ctsio->kern_total_len = data_len;
10596 ctsio->residual = 0;
10597 ctsio->kern_data_len = alloc_len;
10598 ctsio->kern_total_len = alloc_len;
10601 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10602 scsi_ulto2b(0, hdr->descr_length);
10603 hdr->nea_class = SGESN_NEA;
10604 hdr->supported_class = 0;
10606 ctl_set_success(ctsio);
10607 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10608 ctsio->be_move_done = ctl_config_move_done;
10609 ctl_datamove((union ctl_io *)ctsio);
10610 return (CTL_RETVAL_COMPLETE);
10614 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10616 struct scsi_mechanism_status_header *hdr;
10617 struct scsi_mechanism_status *cdb;
10618 uint32_t alloc_len, data_len;
10620 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10621 alloc_len = scsi_2btoul(cdb->length);
10623 data_len = sizeof(struct scsi_mechanism_status_header);
10624 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10625 ctsio->kern_sg_entries = 0;
10626 ctsio->kern_data_resid = 0;
10627 ctsio->kern_rel_offset = 0;
10629 if (data_len < alloc_len) {
10630 ctsio->residual = alloc_len - data_len;
10631 ctsio->kern_data_len = data_len;
10632 ctsio->kern_total_len = data_len;
10634 ctsio->residual = 0;
10635 ctsio->kern_data_len = alloc_len;
10636 ctsio->kern_total_len = alloc_len;
10639 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10640 hdr->state1 = 0x00;
10641 hdr->state2 = 0xe0;
10642 scsi_ulto3b(0, hdr->lba);
10643 hdr->slots_num = 0;
10644 scsi_ulto2b(0, hdr->slots_length);
10646 ctl_set_success(ctsio);
10647 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10648 ctsio->be_move_done = ctl_config_move_done;
10649 ctl_datamove((union ctl_io *)ctsio);
10650 return (CTL_RETVAL_COMPLETE);
10654 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10659 buf[1] = bin2bcd((lba / 75) / 60);
10660 buf[2] = bin2bcd((lba / 75) % 60);
10661 buf[3] = bin2bcd(lba % 75);
10665 ctl_read_toc(struct ctl_scsiio *ctsio)
10667 struct ctl_lun *lun = CTL_LUN(ctsio);
10668 struct scsi_read_toc_hdr *hdr;
10669 struct scsi_read_toc_type01_descr *descr;
10670 struct scsi_read_toc *cdb;
10671 uint32_t alloc_len, data_len;
10674 cdb = (struct scsi_read_toc *)ctsio->cdb;
10675 msf = (cdb->byte2 & CD_MSF) != 0;
10676 format = cdb->format;
10677 alloc_len = scsi_2btoul(cdb->data_len);
10679 data_len = sizeof(struct scsi_read_toc_hdr);
10681 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10683 data_len += sizeof(struct scsi_read_toc_type01_descr);
10684 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10685 ctsio->kern_sg_entries = 0;
10686 ctsio->kern_data_resid = 0;
10687 ctsio->kern_rel_offset = 0;
10689 if (data_len < alloc_len) {
10690 ctsio->residual = alloc_len - data_len;
10691 ctsio->kern_data_len = data_len;
10692 ctsio->kern_total_len = data_len;
10694 ctsio->residual = 0;
10695 ctsio->kern_data_len = alloc_len;
10696 ctsio->kern_total_len = alloc_len;
10699 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10701 scsi_ulto2b(0x12, hdr->data_length);
10704 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10705 descr->addr_ctl = 0x14;
10706 descr->track_number = 1;
10708 ctl_ultomsf(0, descr->track_start);
10710 scsi_ulto4b(0, descr->track_start);
10712 descr->addr_ctl = 0x14;
10713 descr->track_number = 0xaa;
10715 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10717 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10719 scsi_ulto2b(0x0a, hdr->data_length);
10722 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10723 descr->addr_ctl = 0x14;
10724 descr->track_number = 1;
10726 ctl_ultomsf(0, descr->track_start);
10728 scsi_ulto4b(0, descr->track_start);
10731 ctl_set_success(ctsio);
10732 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10733 ctsio->be_move_done = ctl_config_move_done;
10734 ctl_datamove((union ctl_io *)ctsio);
10735 return (CTL_RETVAL_COMPLETE);
10739 * For known CDB types, parse the LBA and length.
10742 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10744 if (io->io_hdr.io_type != CTL_IO_SCSI)
10747 switch (io->scsiio.cdb[0]) {
10748 case COMPARE_AND_WRITE: {
10749 struct scsi_compare_and_write *cdb;
10751 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10753 *lba = scsi_8btou64(cdb->addr);
10754 *len = cdb->length;
10759 struct scsi_rw_6 *cdb;
10761 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10763 *lba = scsi_3btoul(cdb->addr);
10764 /* only 5 bits are valid in the most significant address byte */
10766 *len = cdb->length;
10771 struct scsi_rw_10 *cdb;
10773 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10775 *lba = scsi_4btoul(cdb->addr);
10776 *len = scsi_2btoul(cdb->length);
10779 case WRITE_VERIFY_10: {
10780 struct scsi_write_verify_10 *cdb;
10782 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10784 *lba = scsi_4btoul(cdb->addr);
10785 *len = scsi_2btoul(cdb->length);
10790 struct scsi_rw_12 *cdb;
10792 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10794 *lba = scsi_4btoul(cdb->addr);
10795 *len = scsi_4btoul(cdb->length);
10798 case WRITE_VERIFY_12: {
10799 struct scsi_write_verify_12 *cdb;
10801 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10803 *lba = scsi_4btoul(cdb->addr);
10804 *len = scsi_4btoul(cdb->length);
10809 struct scsi_rw_16 *cdb;
10811 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10813 *lba = scsi_8btou64(cdb->addr);
10814 *len = scsi_4btoul(cdb->length);
10817 case WRITE_ATOMIC_16: {
10818 struct scsi_write_atomic_16 *cdb;
10820 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10822 *lba = scsi_8btou64(cdb->addr);
10823 *len = scsi_2btoul(cdb->length);
10826 case WRITE_VERIFY_16: {
10827 struct scsi_write_verify_16 *cdb;
10829 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10831 *lba = scsi_8btou64(cdb->addr);
10832 *len = scsi_4btoul(cdb->length);
10835 case WRITE_SAME_10: {
10836 struct scsi_write_same_10 *cdb;
10838 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10840 *lba = scsi_4btoul(cdb->addr);
10841 *len = scsi_2btoul(cdb->length);
10844 case WRITE_SAME_16: {
10845 struct scsi_write_same_16 *cdb;
10847 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10849 *lba = scsi_8btou64(cdb->addr);
10850 *len = scsi_4btoul(cdb->length);
10854 struct scsi_verify_10 *cdb;
10856 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10858 *lba = scsi_4btoul(cdb->addr);
10859 *len = scsi_2btoul(cdb->length);
10863 struct scsi_verify_12 *cdb;
10865 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10867 *lba = scsi_4btoul(cdb->addr);
10868 *len = scsi_4btoul(cdb->length);
10872 struct scsi_verify_16 *cdb;
10874 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10876 *lba = scsi_8btou64(cdb->addr);
10877 *len = scsi_4btoul(cdb->length);
10885 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10886 struct scsi_get_lba_status *cdb;
10888 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10889 *lba = scsi_8btou64(cdb->addr);
10895 break; /* NOTREACHED */
10902 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10905 uint64_t endlba1, endlba2;
10907 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10908 endlba2 = lba2 + len2 - 1;
10910 if ((endlba1 < lba2) || (endlba2 < lba1))
10911 return (CTL_ACTION_PASS);
10913 return (CTL_ACTION_BLOCK);
10917 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10919 struct ctl_ptr_len_flags *ptrlen;
10920 struct scsi_unmap_desc *buf, *end, *range;
10924 /* If not UNMAP -- go other way. */
10925 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10926 io->scsiio.cdb[0] != UNMAP)
10927 return (CTL_ACTION_ERROR);
10929 /* If UNMAP without data -- block and wait for data. */
10930 ptrlen = (struct ctl_ptr_len_flags *)
10931 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10932 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10933 ptrlen->ptr == NULL)
10934 return (CTL_ACTION_BLOCK);
10936 /* UNMAP with data -- check for collision. */
10937 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10938 end = buf + ptrlen->len / sizeof(*buf);
10939 for (range = buf; range < end; range++) {
10940 lba = scsi_8btou64(range->lba);
10941 len = scsi_4btoul(range->length);
10942 if ((lba < lba2 + len2) && (lba + len > lba2))
10943 return (CTL_ACTION_BLOCK);
10945 return (CTL_ACTION_PASS);
10949 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10951 uint64_t lba1, lba2;
10952 uint64_t len1, len2;
10955 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10956 return (CTL_ACTION_ERROR);
10958 retval = ctl_extent_check_unmap(io1, lba2, len2);
10959 if (retval != CTL_ACTION_ERROR)
10962 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10963 return (CTL_ACTION_ERROR);
10965 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10967 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10971 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10973 uint64_t lba1, lba2;
10974 uint64_t len1, len2;
10976 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10977 return (CTL_ACTION_PASS);
10978 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10979 return (CTL_ACTION_ERROR);
10980 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10981 return (CTL_ACTION_ERROR);
10983 if (lba1 + len1 == lba2)
10984 return (CTL_ACTION_BLOCK);
10985 return (CTL_ACTION_PASS);
10989 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10990 union ctl_io *ooa_io)
10992 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10993 const ctl_serialize_action *serialize_row;
10996 * The initiator attempted multiple untagged commands at the same
10997 * time. Can't do that.
10999 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11000 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11001 && ((pending_io->io_hdr.nexus.targ_port ==
11002 ooa_io->io_hdr.nexus.targ_port)
11003 && (pending_io->io_hdr.nexus.initid ==
11004 ooa_io->io_hdr.nexus.initid))
11005 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11006 CTL_FLAG_STATUS_SENT)) == 0))
11007 return (CTL_ACTION_OVERLAP);
11010 * The initiator attempted to send multiple tagged commands with
11011 * the same ID. (It's fine if different initiators have the same
11014 * Even if all of those conditions are true, we don't kill the I/O
11015 * if the command ahead of us has been aborted. We won't end up
11016 * sending it to the FETD, and it's perfectly legal to resend a
11017 * command with the same tag number as long as the previous
11018 * instance of this tag number has been aborted somehow.
11020 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11021 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
11022 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
11023 && ((pending_io->io_hdr.nexus.targ_port ==
11024 ooa_io->io_hdr.nexus.targ_port)
11025 && (pending_io->io_hdr.nexus.initid ==
11026 ooa_io->io_hdr.nexus.initid))
11027 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
11028 CTL_FLAG_STATUS_SENT)) == 0))
11029 return (CTL_ACTION_OVERLAP_TAG);
11032 * If we get a head of queue tag, SAM-3 says that we should
11033 * immediately execute it.
11035 * What happens if this command would normally block for some other
11036 * reason? e.g. a request sense with a head of queue tag
11037 * immediately after a write. Normally that would block, but this
11038 * will result in its getting executed immediately...
11040 * We currently return "pass" instead of "skip", so we'll end up
11041 * going through the rest of the queue to check for overlapped tags.
11043 * XXX KDM check for other types of blockage first??
11045 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11046 return (CTL_ACTION_PASS);
11049 * Ordered tags have to block until all items ahead of them
11050 * have completed. If we get called with an ordered tag, we always
11051 * block, if something else is ahead of us in the queue.
11053 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
11054 return (CTL_ACTION_BLOCK);
11057 * Simple tags get blocked until all head of queue and ordered tags
11058 * ahead of them have completed. I'm lumping untagged commands in
11059 * with simple tags here. XXX KDM is that the right thing to do?
11061 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
11062 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
11063 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
11064 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
11065 return (CTL_ACTION_BLOCK);
11067 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11068 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11069 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11070 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11071 pending_io->scsiio.cdb[1], pending_io));
11072 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11073 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11074 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11075 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11076 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11077 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11078 ooa_io->scsiio.cdb[1], ooa_io));
11080 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11082 switch (serialize_row[pending_entry->seridx]) {
11083 case CTL_SER_BLOCK:
11084 return (CTL_ACTION_BLOCK);
11085 case CTL_SER_EXTENT:
11086 return (ctl_extent_check(ooa_io, pending_io,
11087 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11088 case CTL_SER_EXTENTOPT:
11089 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11090 SCP_QUEUE_ALG_UNRESTRICTED)
11091 return (ctl_extent_check(ooa_io, pending_io,
11093 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11094 return (CTL_ACTION_PASS);
11095 case CTL_SER_EXTENTSEQ:
11096 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11097 return (ctl_extent_check_seq(ooa_io, pending_io));
11098 return (CTL_ACTION_PASS);
11100 return (CTL_ACTION_PASS);
11101 case CTL_SER_BLOCKOPT:
11102 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11103 SCP_QUEUE_ALG_UNRESTRICTED)
11104 return (CTL_ACTION_BLOCK);
11105 return (CTL_ACTION_PASS);
11107 return (CTL_ACTION_SKIP);
11109 panic("%s: Invalid serialization value %d for %d => %d",
11110 __func__, serialize_row[pending_entry->seridx],
11111 pending_entry->seridx, ooa_entry->seridx);
11114 return (CTL_ACTION_ERROR);
11118 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11120 * - pending_io is generally either incoming, or on the blocked queue
11121 * - starting I/O is the I/O we want to start the check with.
11124 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11125 union ctl_io *starting_io)
11127 union ctl_io *ooa_io;
11130 mtx_assert(&lun->lun_lock, MA_OWNED);
11133 * Run back along the OOA queue, starting with the current
11134 * blocked I/O and going through every I/O before it on the
11135 * queue. If starting_io is NULL, we'll just end up returning
11138 for (ooa_io = starting_io; ooa_io != NULL;
11139 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11143 * This routine just checks to see whether
11144 * cur_blocked is blocked by ooa_io, which is ahead
11145 * of it in the queue. It doesn't queue/dequeue
11148 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11150 case CTL_ACTION_BLOCK:
11151 case CTL_ACTION_OVERLAP:
11152 case CTL_ACTION_OVERLAP_TAG:
11153 case CTL_ACTION_SKIP:
11154 case CTL_ACTION_ERROR:
11156 break; /* NOTREACHED */
11157 case CTL_ACTION_PASS:
11160 panic("%s: Invalid action %d\n", __func__, action);
11164 return (CTL_ACTION_PASS);
11169 * - An I/O has just completed, and has been removed from the per-LUN OOA
11170 * queue, so some items on the blocked queue may now be unblocked.
11173 ctl_check_blocked(struct ctl_lun *lun)
11175 struct ctl_softc *softc = lun->ctl_softc;
11176 union ctl_io *cur_blocked, *next_blocked;
11178 mtx_assert(&lun->lun_lock, MA_OWNED);
11181 * Run forward from the head of the blocked queue, checking each
11182 * entry against the I/Os prior to it on the OOA queue to see if
11183 * there is still any blockage.
11185 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11186 * with our removing a variable on it while it is traversing the
11189 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11190 cur_blocked != NULL; cur_blocked = next_blocked) {
11191 union ctl_io *prev_ooa;
11194 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11197 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11198 ctl_ooaq, ooa_links);
11201 * If cur_blocked happens to be the first item in the OOA
11202 * queue now, prev_ooa will be NULL, and the action
11203 * returned will just be CTL_ACTION_PASS.
11205 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11208 case CTL_ACTION_BLOCK:
11209 /* Nothing to do here, still blocked */
11211 case CTL_ACTION_OVERLAP:
11212 case CTL_ACTION_OVERLAP_TAG:
11214 * This shouldn't happen! In theory we've already
11215 * checked this command for overlap...
11218 case CTL_ACTION_PASS:
11219 case CTL_ACTION_SKIP: {
11220 const struct ctl_cmd_entry *entry;
11223 * The skip case shouldn't happen, this transaction
11224 * should have never made it onto the blocked queue.
11227 * This I/O is no longer blocked, we can remove it
11228 * from the blocked queue. Since this is a TAILQ
11229 * (doubly linked list), we can do O(1) removals
11230 * from any place on the list.
11232 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11234 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11236 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11237 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11239 * Need to send IO back to original side to
11242 union ctl_ha_msg msg_info;
11244 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11245 msg_info.hdr.original_sc =
11246 cur_blocked->io_hdr.original_sc;
11247 msg_info.hdr.serializing_sc = cur_blocked;
11248 msg_info.hdr.msg_type = CTL_MSG_R2R;
11249 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11250 sizeof(msg_info.hdr), M_NOWAIT);
11253 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11256 * Check this I/O for LUN state changes that may
11257 * have happened while this command was blocked.
11258 * The LUN state may have been changed by a command
11259 * ahead of us in the queue, so we need to re-check
11260 * for any states that can be caused by SCSI
11263 if (ctl_scsiio_lun_check(lun, entry,
11264 &cur_blocked->scsiio) == 0) {
11265 cur_blocked->io_hdr.flags |=
11266 CTL_FLAG_IS_WAS_ON_RTR;
11267 ctl_enqueue_rtr(cur_blocked);
11269 ctl_done(cur_blocked);
11274 * This probably shouldn't happen -- we shouldn't
11275 * get CTL_ACTION_ERROR, or anything else.
11281 return (CTL_RETVAL_COMPLETE);
11285 * This routine (with one exception) checks LUN flags that can be set by
11286 * commands ahead of us in the OOA queue. These flags have to be checked
11287 * when a command initially comes in, and when we pull a command off the
11288 * blocked queue and are preparing to execute it. The reason we have to
11289 * check these flags for commands on the blocked queue is that the LUN
11290 * state may have been changed by a command ahead of us while we're on the
11293 * Ordering is somewhat important with these checks, so please pay
11294 * careful attention to the placement of any new checks.
11297 ctl_scsiio_lun_check(struct ctl_lun *lun,
11298 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11300 struct ctl_softc *softc = lun->ctl_softc;
11306 mtx_assert(&lun->lun_lock, MA_OWNED);
11309 * If this shelf is a secondary shelf controller, we may have to
11310 * reject some commands disallowed by HA mode and link state.
11312 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11313 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11314 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11315 ctl_set_lun_unavail(ctsio);
11319 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11320 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11321 ctl_set_lun_transit(ctsio);
11325 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11326 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11327 ctl_set_lun_standby(ctsio);
11332 /* The rest of checks are only done on executing side */
11333 if (softc->ha_mode == CTL_HA_MODE_XFER)
11337 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11339 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11340 ctl_set_hw_write_protected(ctsio);
11344 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11345 ctl_set_sense(ctsio, /*current_error*/ 1,
11346 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11347 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11354 * Check for a reservation conflict. If this command isn't allowed
11355 * even on reserved LUNs, and if this initiator isn't the one who
11356 * reserved us, reject the command with a reservation conflict.
11358 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11359 if ((lun->flags & CTL_LUN_RESERVED)
11360 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11361 if (lun->res_idx != residx) {
11362 ctl_set_reservation_conflict(ctsio);
11368 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11369 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11370 /* No reservation or command is allowed. */;
11371 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11372 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11373 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11374 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11375 /* The command is allowed for Write Exclusive resv. */;
11378 * if we aren't registered or it's a res holder type
11379 * reservation and this isn't the res holder then set a
11382 if (ctl_get_prkey(lun, residx) == 0 ||
11383 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11384 ctl_set_reservation_conflict(ctsio);
11390 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11391 if (lun->flags & CTL_LUN_EJECTED)
11392 ctl_set_lun_ejected(ctsio);
11393 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11394 if (lun->flags & CTL_LUN_REMOVABLE)
11395 ctl_set_lun_no_media(ctsio);
11397 ctl_set_lun_int_reqd(ctsio);
11398 } else if (lun->flags & CTL_LUN_STOPPED)
11399 ctl_set_lun_stopped(ctsio);
11411 ctl_failover_io(union ctl_io *io, int have_lock)
11413 ctl_set_busy(&io->scsiio);
11418 ctl_failover_lun(union ctl_io *rio)
11420 struct ctl_softc *softc = CTL_SOFTC(rio);
11421 struct ctl_lun *lun;
11422 struct ctl_io_hdr *io, *next_io;
11425 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11426 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11428 /* Find and lock the LUN. */
11429 mtx_lock(&softc->ctl_lock);
11430 if (targ_lun > CTL_MAX_LUNS ||
11431 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11432 mtx_unlock(&softc->ctl_lock);
11435 mtx_lock(&lun->lun_lock);
11436 mtx_unlock(&softc->ctl_lock);
11437 if (lun->flags & CTL_LUN_DISABLED) {
11438 mtx_unlock(&lun->lun_lock);
11442 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11443 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11444 /* We are master */
11445 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11446 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11447 io->flags |= CTL_FLAG_ABORT;
11448 io->flags |= CTL_FLAG_FAILOVER;
11449 } else { /* This can be only due to DATAMOVE */
11450 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11451 io->flags &= ~CTL_FLAG_DMA_INPROG;
11452 io->flags |= CTL_FLAG_IO_ACTIVE;
11453 io->port_status = 31340;
11454 ctl_enqueue_isc((union ctl_io *)io);
11458 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11459 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11460 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11461 io->flags |= CTL_FLAG_FAILOVER;
11463 ctl_set_busy(&((union ctl_io *)io)->
11465 ctl_done((union ctl_io *)io);
11469 } else { /* SERIALIZE modes */
11470 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11472 /* We are master */
11473 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11474 TAILQ_REMOVE(&lun->blocked_queue, io,
11476 io->flags &= ~CTL_FLAG_BLOCKED;
11477 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11478 ctl_free_io((union ctl_io *)io);
11481 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11482 /* We are master */
11483 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11484 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11485 ctl_free_io((union ctl_io *)io);
11488 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11489 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11490 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11491 ctl_set_busy(&((union ctl_io *)io)->
11493 ctl_done((union ctl_io *)io);
11497 ctl_check_blocked(lun);
11499 mtx_unlock(&lun->lun_lock);
11503 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11505 struct ctl_lun *lun;
11506 const struct ctl_cmd_entry *entry;
11507 uint32_t initidx, targ_lun;
11511 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11512 if (targ_lun < CTL_MAX_LUNS)
11513 lun = softc->ctl_luns[targ_lun];
11516 * If the LUN is invalid, pretend that it doesn't exist.
11517 * It will go away as soon as all pending I/O has been
11520 mtx_lock(&lun->lun_lock);
11521 if (lun->flags & CTL_LUN_DISABLED) {
11522 mtx_unlock(&lun->lun_lock);
11526 CTL_LUN(ctsio) = lun;
11528 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11531 * Every I/O goes into the OOA queue for a particular LUN,
11532 * and stays there until completion.
11535 if (TAILQ_EMPTY(&lun->ooa_queue))
11536 lun->idle_time += getsbinuptime() - lun->last_busy;
11538 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11541 /* Get command entry and return error if it is unsuppotyed. */
11542 entry = ctl_validate_command(ctsio);
11543 if (entry == NULL) {
11545 mtx_unlock(&lun->lun_lock);
11549 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11550 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11553 * Check to see whether we can send this command to LUNs that don't
11554 * exist. This should pretty much only be the case for inquiry
11555 * and request sense. Further checks, below, really require having
11556 * a LUN, so we can't really check the command anymore. Just put
11557 * it on the rtr queue.
11560 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11561 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11562 ctl_enqueue_rtr((union ctl_io *)ctsio);
11566 ctl_set_unsupported_lun(ctsio);
11567 ctl_done((union ctl_io *)ctsio);
11568 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11572 * Make sure we support this particular command on this LUN.
11573 * e.g., we don't support writes to the control LUN.
11575 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11576 mtx_unlock(&lun->lun_lock);
11577 ctl_set_invalid_opcode(ctsio);
11578 ctl_done((union ctl_io *)ctsio);
11583 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11587 * If we've got a request sense, it'll clear the contingent
11588 * allegiance condition. Otherwise, if we have a CA condition for
11589 * this initiator, clear it, because it sent down a command other
11590 * than request sense.
11592 if ((ctsio->cdb[0] != REQUEST_SENSE)
11593 && (ctl_is_set(lun->have_ca, initidx)))
11594 ctl_clear_mask(lun->have_ca, initidx);
11598 * If the command has this flag set, it handles its own unit
11599 * attention reporting, we shouldn't do anything. Otherwise we
11600 * check for any pending unit attentions, and send them back to the
11601 * initiator. We only do this when a command initially comes in,
11602 * not when we pull it off the blocked queue.
11604 * According to SAM-3, section 5.3.2, the order that things get
11605 * presented back to the host is basically unit attentions caused
11606 * by some sort of reset event, busy status, reservation conflicts
11607 * or task set full, and finally any other status.
11609 * One issue here is that some of the unit attentions we report
11610 * don't fall into the "reset" category (e.g. "reported luns data
11611 * has changed"). So reporting it here, before the reservation
11612 * check, may be technically wrong. I guess the only thing to do
11613 * would be to check for and report the reset events here, and then
11614 * check for the other unit attention types after we check for a
11615 * reservation conflict.
11617 * XXX KDM need to fix this
11619 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11620 ctl_ua_type ua_type;
11621 u_int sense_len = 0;
11623 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11624 &sense_len, SSD_TYPE_NONE);
11625 if (ua_type != CTL_UA_NONE) {
11626 mtx_unlock(&lun->lun_lock);
11627 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11628 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11629 ctsio->sense_len = sense_len;
11630 ctl_done((union ctl_io *)ctsio);
11636 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11637 mtx_unlock(&lun->lun_lock);
11638 ctl_done((union ctl_io *)ctsio);
11643 * XXX CHD this is where we want to send IO to other side if
11644 * this LUN is secondary on this SC. We will need to make a copy
11645 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11646 * the copy we send as FROM_OTHER.
11647 * We also need to stuff the address of the original IO so we can
11648 * find it easily. Something similar will need be done on the other
11649 * side so when we are done we can find the copy.
11651 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11652 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11653 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11654 union ctl_ha_msg msg_info;
11657 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11658 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11659 mtx_unlock(&lun->lun_lock);
11661 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11662 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11663 msg_info.hdr.serializing_sc = NULL;
11664 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11665 msg_info.scsi.tag_num = ctsio->tag_num;
11666 msg_info.scsi.tag_type = ctsio->tag_type;
11667 msg_info.scsi.cdb_len = ctsio->cdb_len;
11668 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11670 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11671 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11672 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11673 ctl_set_busy(ctsio);
11674 ctl_done((union ctl_io *)ctsio);
11680 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11681 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11682 ctl_ooaq, ooa_links))) {
11683 case CTL_ACTION_BLOCK:
11684 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11685 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11687 mtx_unlock(&lun->lun_lock);
11689 case CTL_ACTION_PASS:
11690 case CTL_ACTION_SKIP:
11691 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11692 mtx_unlock(&lun->lun_lock);
11693 ctl_enqueue_rtr((union ctl_io *)ctsio);
11695 case CTL_ACTION_OVERLAP:
11696 mtx_unlock(&lun->lun_lock);
11697 ctl_set_overlapped_cmd(ctsio);
11698 ctl_done((union ctl_io *)ctsio);
11700 case CTL_ACTION_OVERLAP_TAG:
11701 mtx_unlock(&lun->lun_lock);
11702 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11703 ctl_done((union ctl_io *)ctsio);
11705 case CTL_ACTION_ERROR:
11707 mtx_unlock(&lun->lun_lock);
11708 ctl_set_internal_failure(ctsio,
11710 /*retry_count*/ 0);
11711 ctl_done((union ctl_io *)ctsio);
11717 const struct ctl_cmd_entry *
11718 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11720 const struct ctl_cmd_entry *entry;
11721 int service_action;
11723 entry = &ctl_cmd_table[ctsio->cdb[0]];
11725 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11726 if (entry->flags & CTL_CMD_FLAG_SA5) {
11727 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11728 entry = &((const struct ctl_cmd_entry *)
11729 entry->execute)[service_action];
11734 const struct ctl_cmd_entry *
11735 ctl_validate_command(struct ctl_scsiio *ctsio)
11737 const struct ctl_cmd_entry *entry;
11741 entry = ctl_get_cmd_entry(ctsio, &sa);
11742 if (entry->execute == NULL) {
11744 ctl_set_invalid_field(ctsio,
11751 ctl_set_invalid_opcode(ctsio);
11752 ctl_done((union ctl_io *)ctsio);
11755 KASSERT(entry->length > 0,
11756 ("Not defined length for command 0x%02x/0x%02x",
11757 ctsio->cdb[0], ctsio->cdb[1]));
11758 for (i = 1; i < entry->length; i++) {
11759 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11762 ctl_set_invalid_field(ctsio,
11767 /*bit*/ fls(diff) - 1);
11768 ctl_done((union ctl_io *)ctsio);
11775 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11778 switch (lun_type) {
11780 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11784 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11788 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11798 ctl_scsiio(struct ctl_scsiio *ctsio)
11801 const struct ctl_cmd_entry *entry;
11803 retval = CTL_RETVAL_COMPLETE;
11805 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11807 entry = ctl_get_cmd_entry(ctsio, NULL);
11810 * If this I/O has been aborted, just send it straight to
11811 * ctl_done() without executing it.
11813 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11814 ctl_done((union ctl_io *)ctsio);
11819 * All the checks should have been handled by ctl_scsiio_precheck().
11820 * We should be clear now to just execute the I/O.
11822 retval = entry->execute(ctsio);
11829 * Since we only implement one target right now, a bus reset simply resets
11830 * our single target.
11833 ctl_bus_reset(struct ctl_softc *softc, union ctl_io *io)
11835 return(ctl_target_reset(softc, io, CTL_UA_BUS_RESET));
11839 ctl_target_reset(struct ctl_softc *softc, union ctl_io *io,
11840 ctl_ua_type ua_type)
11842 struct ctl_port *port = CTL_PORT(io);
11843 struct ctl_lun *lun;
11846 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11847 union ctl_ha_msg msg_info;
11849 msg_info.hdr.nexus = io->io_hdr.nexus;
11850 if (ua_type==CTL_UA_TARG_RESET)
11851 msg_info.task.task_action = CTL_TASK_TARGET_RESET;
11853 msg_info.task.task_action = CTL_TASK_BUS_RESET;
11854 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11855 msg_info.hdr.original_sc = NULL;
11856 msg_info.hdr.serializing_sc = NULL;
11857 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11858 sizeof(msg_info.task), M_WAITOK);
11862 mtx_lock(&softc->ctl_lock);
11863 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11864 if (port != NULL &&
11865 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11867 retval += ctl_do_lun_reset(lun, io, ua_type);
11869 mtx_unlock(&softc->ctl_lock);
11870 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11875 * The LUN should always be set. The I/O is optional, and is used to
11876 * distinguish between I/Os sent by this initiator, and by other
11877 * initiators. We set unit attention for initiators other than this one.
11878 * SAM-3 is vague on this point. It does say that a unit attention should
11879 * be established for other initiators when a LUN is reset (see section
11880 * 5.7.3), but it doesn't specifically say that the unit attention should
11881 * be established for this particular initiator when a LUN is reset. Here
11882 * is the relevant text, from SAM-3 rev 8:
11884 * 5.7.2 When a SCSI initiator port aborts its own tasks
11886 * When a SCSI initiator port causes its own task(s) to be aborted, no
11887 * notification that the task(s) have been aborted shall be returned to
11888 * the SCSI initiator port other than the completion response for the
11889 * command or task management function action that caused the task(s) to
11890 * be aborted and notification(s) associated with related effects of the
11891 * action (e.g., a reset unit attention condition).
11893 * XXX KDM for now, we're setting unit attention for all initiators.
11896 ctl_do_lun_reset(struct ctl_lun *lun, union ctl_io *io, ctl_ua_type ua_type)
11904 mtx_lock(&lun->lun_lock);
11906 * Run through the OOA queue and abort each I/O.
11908 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11909 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11910 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11914 * This version sets unit attention for every
11917 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11918 ctl_est_ua_all(lun, initidx, ua_type);
11920 ctl_est_ua_all(lun, -1, ua_type);
11924 * A reset (any kind, really) clears reservations established with
11925 * RESERVE/RELEASE. It does not clear reservations established
11926 * with PERSISTENT RESERVE OUT, but we don't support that at the
11927 * moment anyway. See SPC-2, section 5.6. SPC-3 doesn't address
11928 * reservations made with the RESERVE/RELEASE commands, because
11929 * those commands are obsolete in SPC-3.
11931 lun->flags &= ~CTL_LUN_RESERVED;
11934 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11935 ctl_clear_mask(lun->have_ca, i);
11937 lun->prevent_count = 0;
11938 if (lun->prevent) {
11939 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11940 ctl_clear_mask(lun->prevent, i);
11942 mtx_unlock(&lun->lun_lock);
11948 ctl_lun_reset(struct ctl_softc *softc, union ctl_io *io)
11950 struct ctl_lun *lun;
11954 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11955 mtx_lock(&softc->ctl_lock);
11956 if (targ_lun >= CTL_MAX_LUNS ||
11957 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11958 mtx_unlock(&softc->ctl_lock);
11959 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11962 retval = ctl_do_lun_reset(lun, io, CTL_UA_LUN_RESET);
11963 mtx_unlock(&softc->ctl_lock);
11964 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11966 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11967 union ctl_ha_msg msg_info;
11969 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11970 msg_info.hdr.nexus = io->io_hdr.nexus;
11971 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11972 msg_info.hdr.original_sc = NULL;
11973 msg_info.hdr.serializing_sc = NULL;
11974 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11975 sizeof(msg_info.task), M_WAITOK);
11981 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11986 mtx_assert(&lun->lun_lock, MA_OWNED);
11989 * Run through the OOA queue and attempt to find the given I/O.
11990 * The target port, initiator ID, tag type and tag number have to
11991 * match the values that we got from the initiator. If we have an
11992 * untagged command to abort, simply abort the first untagged command
11993 * we come to. We only allow one untagged command at a time of course.
11995 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11996 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11998 if ((targ_port == UINT32_MAX ||
11999 targ_port == xio->io_hdr.nexus.targ_port) &&
12000 (init_id == UINT32_MAX ||
12001 init_id == xio->io_hdr.nexus.initid)) {
12002 if (targ_port != xio->io_hdr.nexus.targ_port ||
12003 init_id != xio->io_hdr.nexus.initid)
12004 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
12005 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12006 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12007 union ctl_ha_msg msg_info;
12009 msg_info.hdr.nexus = xio->io_hdr.nexus;
12010 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12011 msg_info.task.tag_num = xio->scsiio.tag_num;
12012 msg_info.task.tag_type = xio->scsiio.tag_type;
12013 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12014 msg_info.hdr.original_sc = NULL;
12015 msg_info.hdr.serializing_sc = NULL;
12016 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12017 sizeof(msg_info.task), M_NOWAIT);
12024 ctl_abort_task_set(union ctl_io *io)
12026 struct ctl_softc *softc = CTL_SOFTC(io);
12027 struct ctl_lun *lun;
12033 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12034 mtx_lock(&softc->ctl_lock);
12035 if (targ_lun >= CTL_MAX_LUNS ||
12036 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12037 mtx_unlock(&softc->ctl_lock);
12038 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12042 mtx_lock(&lun->lun_lock);
12043 mtx_unlock(&softc->ctl_lock);
12044 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
12045 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12046 io->io_hdr.nexus.initid,
12047 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12048 } else { /* CTL_TASK_CLEAR_TASK_SET */
12049 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
12050 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
12052 mtx_unlock(&lun->lun_lock);
12053 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12058 ctl_i_t_nexus_reset(union ctl_io *io)
12060 struct ctl_softc *softc = CTL_SOFTC(io);
12061 struct ctl_lun *lun;
12064 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12065 union ctl_ha_msg msg_info;
12067 msg_info.hdr.nexus = io->io_hdr.nexus;
12068 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12069 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12070 msg_info.hdr.original_sc = NULL;
12071 msg_info.hdr.serializing_sc = NULL;
12072 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12073 sizeof(msg_info.task), M_WAITOK);
12076 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12077 mtx_lock(&softc->ctl_lock);
12078 STAILQ_FOREACH(lun, &softc->lun_list, links) {
12079 mtx_lock(&lun->lun_lock);
12080 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
12081 io->io_hdr.nexus.initid, 1);
12083 ctl_clear_mask(lun->have_ca, initidx);
12085 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
12086 lun->flags &= ~CTL_LUN_RESERVED;
12087 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
12088 ctl_clear_mask(lun->prevent, initidx);
12089 lun->prevent_count--;
12091 ctl_est_ua(lun, initidx, CTL_UA_I_T_NEXUS_LOSS);
12092 mtx_unlock(&lun->lun_lock);
12094 mtx_unlock(&softc->ctl_lock);
12095 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12100 ctl_abort_task(union ctl_io *io)
12102 struct ctl_softc *softc = CTL_SOFTC(io);
12104 struct ctl_lun *lun;
12107 char printbuf[128];
12117 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12118 mtx_lock(&softc->ctl_lock);
12119 if (targ_lun >= CTL_MAX_LUNS ||
12120 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12121 mtx_unlock(&softc->ctl_lock);
12122 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12127 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
12128 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
12131 mtx_lock(&lun->lun_lock);
12132 mtx_unlock(&softc->ctl_lock);
12134 * Run through the OOA queue and attempt to find the given I/O.
12135 * The target port, initiator ID, tag type and tag number have to
12136 * match the values that we got from the initiator. If we have an
12137 * untagged command to abort, simply abort the first untagged command
12138 * we come to. We only allow one untagged command at a time of course.
12140 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12141 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12143 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
12145 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
12146 lun->lun, xio->scsiio.tag_num,
12147 xio->scsiio.tag_type,
12148 (xio->io_hdr.blocked_links.tqe_prev
12149 == NULL) ? "" : " BLOCKED",
12150 (xio->io_hdr.flags &
12151 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
12152 (xio->io_hdr.flags &
12153 CTL_FLAG_ABORT) ? " ABORT" : "",
12154 (xio->io_hdr.flags &
12155 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
12156 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
12158 printf("%s\n", sbuf_data(&sb));
12161 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12162 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12163 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12167 * If the abort says that the task is untagged, the
12168 * task in the queue must be untagged. Otherwise,
12169 * we just check to see whether the tag numbers
12170 * match. This is because the QLogic firmware
12171 * doesn't pass back the tag type in an abort
12175 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12176 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12177 || (xio->scsiio.tag_num == io->taskio.tag_num))
12180 * XXX KDM we've got problems with FC, because it
12181 * doesn't send down a tag type with aborts. So we
12182 * can only really go by the tag number...
12183 * This may cause problems with parallel SCSI.
12184 * Need to figure that out!!
12186 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12187 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12189 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12190 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12191 union ctl_ha_msg msg_info;
12193 msg_info.hdr.nexus = io->io_hdr.nexus;
12194 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12195 msg_info.task.tag_num = io->taskio.tag_num;
12196 msg_info.task.tag_type = io->taskio.tag_type;
12197 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12198 msg_info.hdr.original_sc = NULL;
12199 msg_info.hdr.serializing_sc = NULL;
12201 printf("Sent Abort to other side\n");
12203 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12204 sizeof(msg_info.task), M_NOWAIT);
12207 printf("ctl_abort_task: found I/O to abort\n");
12211 mtx_unlock(&lun->lun_lock);
12215 * This isn't really an error. It's entirely possible for
12216 * the abort and command completion to cross on the wire.
12217 * This is more of an informative/diagnostic error.
12220 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12221 "%u:%u:%u tag %d type %d\n",
12222 io->io_hdr.nexus.initid,
12223 io->io_hdr.nexus.targ_port,
12224 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12225 io->taskio.tag_type);
12228 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12233 ctl_query_task(union ctl_io *io, int task_set)
12235 struct ctl_softc *softc = CTL_SOFTC(io);
12237 struct ctl_lun *lun;
12241 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12242 mtx_lock(&softc->ctl_lock);
12243 if (targ_lun >= CTL_MAX_LUNS ||
12244 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12245 mtx_unlock(&softc->ctl_lock);
12246 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12249 mtx_lock(&lun->lun_lock);
12250 mtx_unlock(&softc->ctl_lock);
12251 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12252 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12254 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12255 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12256 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12259 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12264 mtx_unlock(&lun->lun_lock);
12266 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12268 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12273 ctl_query_async_event(union ctl_io *io)
12275 struct ctl_softc *softc = CTL_SOFTC(io);
12276 struct ctl_lun *lun;
12278 uint32_t targ_lun, initidx;
12280 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12281 mtx_lock(&softc->ctl_lock);
12282 if (targ_lun >= CTL_MAX_LUNS ||
12283 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12284 mtx_unlock(&softc->ctl_lock);
12285 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12288 mtx_lock(&lun->lun_lock);
12289 mtx_unlock(&softc->ctl_lock);
12290 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12291 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12292 mtx_unlock(&lun->lun_lock);
12293 if (ua != CTL_UA_NONE)
12294 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12296 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12301 ctl_run_task(union ctl_io *io)
12303 struct ctl_softc *softc = CTL_SOFTC(io);
12306 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12307 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12308 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12309 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12310 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12311 switch (io->taskio.task_action) {
12312 case CTL_TASK_ABORT_TASK:
12313 retval = ctl_abort_task(io);
12315 case CTL_TASK_ABORT_TASK_SET:
12316 case CTL_TASK_CLEAR_TASK_SET:
12317 retval = ctl_abort_task_set(io);
12319 case CTL_TASK_CLEAR_ACA:
12321 case CTL_TASK_I_T_NEXUS_RESET:
12322 retval = ctl_i_t_nexus_reset(io);
12324 case CTL_TASK_LUN_RESET:
12325 retval = ctl_lun_reset(softc, io);
12327 case CTL_TASK_TARGET_RESET:
12328 retval = ctl_target_reset(softc, io, CTL_UA_TARG_RESET);
12330 case CTL_TASK_BUS_RESET:
12331 retval = ctl_bus_reset(softc, io);
12333 case CTL_TASK_PORT_LOGIN:
12335 case CTL_TASK_PORT_LOGOUT:
12337 case CTL_TASK_QUERY_TASK:
12338 retval = ctl_query_task(io, 0);
12340 case CTL_TASK_QUERY_TASK_SET:
12341 retval = ctl_query_task(io, 1);
12343 case CTL_TASK_QUERY_ASYNC_EVENT:
12344 retval = ctl_query_async_event(io);
12347 printf("%s: got unknown task management event %d\n",
12348 __func__, io->taskio.task_action);
12352 io->io_hdr.status = CTL_SUCCESS;
12354 io->io_hdr.status = CTL_ERROR;
12359 * For HA operation. Handle commands that come in from the other
12363 ctl_handle_isc(union ctl_io *io)
12365 struct ctl_softc *softc = CTL_SOFTC(io);
12366 struct ctl_lun *lun;
12367 const struct ctl_cmd_entry *entry;
12370 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12371 switch (io->io_hdr.msg_type) {
12372 case CTL_MSG_SERIALIZE:
12373 ctl_serialize_other_sc_cmd(&io->scsiio);
12375 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12376 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12377 if (targ_lun >= CTL_MAX_LUNS ||
12378 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12382 mtx_lock(&lun->lun_lock);
12383 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12384 mtx_unlock(&lun->lun_lock);
12388 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12389 mtx_unlock(&lun->lun_lock);
12390 ctl_enqueue_rtr(io);
12392 case CTL_MSG_FINISH_IO:
12393 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12397 if (targ_lun >= CTL_MAX_LUNS ||
12398 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12402 mtx_lock(&lun->lun_lock);
12403 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12404 ctl_check_blocked(lun);
12405 mtx_unlock(&lun->lun_lock);
12408 case CTL_MSG_PERS_ACTION:
12409 ctl_hndl_per_res_out_on_other_sc(io);
12412 case CTL_MSG_BAD_JUJU:
12415 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12416 ctl_datamove_remote(io);
12418 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12419 io->scsiio.be_move_done(io);
12421 case CTL_MSG_FAILOVER:
12422 ctl_failover_lun(io);
12426 printf("%s: Invalid message type %d\n",
12427 __func__, io->io_hdr.msg_type);
12436 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12437 * there is no match.
12439 static ctl_lun_error_pattern
12440 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12442 const struct ctl_cmd_entry *entry;
12443 ctl_lun_error_pattern filtered_pattern, pattern;
12445 pattern = desc->error_pattern;
12448 * XXX KDM we need more data passed into this function to match a
12449 * custom pattern, and we actually need to implement custom pattern
12452 if (pattern & CTL_LUN_PAT_CMD)
12453 return (CTL_LUN_PAT_CMD);
12455 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12456 return (CTL_LUN_PAT_ANY);
12458 entry = ctl_get_cmd_entry(ctsio, NULL);
12460 filtered_pattern = entry->pattern & pattern;
12463 * If the user requested specific flags in the pattern (e.g.
12464 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12467 * If the user did not specify any flags, it doesn't matter whether
12468 * or not the command supports the flags.
12470 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12471 (pattern & ~CTL_LUN_PAT_MASK))
12472 return (CTL_LUN_PAT_NONE);
12475 * If the user asked for a range check, see if the requested LBA
12476 * range overlaps with this command's LBA range.
12478 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12484 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12486 return (CTL_LUN_PAT_NONE);
12488 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12489 desc->lba_range.len, FALSE);
12491 * A "pass" means that the LBA ranges don't overlap, so
12492 * this doesn't match the user's range criteria.
12494 if (action == CTL_ACTION_PASS)
12495 return (CTL_LUN_PAT_NONE);
12498 return (filtered_pattern);
12502 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12504 struct ctl_error_desc *desc, *desc2;
12506 mtx_assert(&lun->lun_lock, MA_OWNED);
12508 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12509 ctl_lun_error_pattern pattern;
12511 * Check to see whether this particular command matches
12512 * the pattern in the descriptor.
12514 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12515 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12518 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12519 case CTL_LUN_INJ_ABORTED:
12520 ctl_set_aborted(&io->scsiio);
12522 case CTL_LUN_INJ_MEDIUM_ERR:
12523 ctl_set_medium_error(&io->scsiio,
12524 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12525 CTL_FLAG_DATA_OUT);
12527 case CTL_LUN_INJ_UA:
12528 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12530 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12532 case CTL_LUN_INJ_CUSTOM:
12534 * We're assuming the user knows what he is doing.
12535 * Just copy the sense information without doing
12538 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12539 MIN(sizeof(desc->custom_sense),
12540 sizeof(io->scsiio.sense_data)));
12541 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12542 io->scsiio.sense_len = SSD_FULL_SIZE;
12543 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12545 case CTL_LUN_INJ_NONE:
12548 * If this is an error injection type we don't know
12549 * about, clear the continuous flag (if it is set)
12550 * so it will get deleted below.
12552 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12556 * By default, each error injection action is a one-shot
12558 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12561 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12567 #ifdef CTL_IO_DELAY
12569 ctl_datamove_timer_wakeup(void *arg)
12573 io = (union ctl_io *)arg;
12577 #endif /* CTL_IO_DELAY */
12580 ctl_datamove(union ctl_io *io)
12582 void (*fe_datamove)(union ctl_io *io);
12584 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12586 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12589 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12594 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12595 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12597 sbuf_cat(&sb, path_str);
12598 switch (io->io_hdr.io_type) {
12600 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12601 sbuf_printf(&sb, "\n");
12602 sbuf_cat(&sb, path_str);
12603 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12604 io->scsiio.tag_num, io->scsiio.tag_type);
12607 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12608 "Tag Type: %d\n", io->taskio.task_action,
12609 io->taskio.tag_num, io->taskio.tag_type);
12612 panic("%s: Invalid CTL I/O type %d\n",
12613 __func__, io->io_hdr.io_type);
12615 sbuf_cat(&sb, path_str);
12616 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12617 (intmax_t)time_uptime - io->io_hdr.start_time);
12619 printf("%s", sbuf_data(&sb));
12621 #endif /* CTL_TIME_IO */
12623 #ifdef CTL_IO_DELAY
12624 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12625 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12628 && (lun->delay_info.datamove_delay > 0)) {
12630 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12631 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12632 callout_reset(&io->io_hdr.delay_callout,
12633 lun->delay_info.datamove_delay * hz,
12634 ctl_datamove_timer_wakeup, io);
12635 if (lun->delay_info.datamove_type ==
12636 CTL_DELAY_TYPE_ONESHOT)
12637 lun->delay_info.datamove_delay = 0;
12644 * This command has been aborted. Set the port status, so we fail
12647 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12648 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12649 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12650 io->io_hdr.nexus.targ_port,
12651 io->io_hdr.nexus.targ_lun);
12652 io->io_hdr.port_status = 31337;
12654 * Note that the backend, in this case, will get the
12655 * callback in its context. In other cases it may get
12656 * called in the frontend's interrupt thread context.
12658 io->scsiio.be_move_done(io);
12662 /* Don't confuse frontend with zero length data move. */
12663 if (io->scsiio.kern_data_len == 0) {
12664 io->scsiio.be_move_done(io);
12668 fe_datamove = CTL_PORT(io)->fe_datamove;
12673 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12675 union ctl_ha_msg msg;
12677 struct bintime cur_bt;
12680 memset(&msg, 0, sizeof(msg));
12681 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12682 msg.hdr.original_sc = io;
12683 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12684 msg.hdr.nexus = io->io_hdr.nexus;
12685 msg.hdr.status = io->io_hdr.status;
12686 msg.scsi.tag_num = io->scsiio.tag_num;
12687 msg.scsi.tag_type = io->scsiio.tag_type;
12688 msg.scsi.scsi_status = io->scsiio.scsi_status;
12689 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12690 io->scsiio.sense_len);
12691 msg.scsi.sense_len = io->scsiio.sense_len;
12692 msg.scsi.sense_residual = io->scsiio.sense_residual;
12693 msg.scsi.fetd_status = io->io_hdr.port_status;
12694 msg.scsi.residual = io->scsiio.residual;
12695 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12696 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12697 ctl_failover_io(io, /*have_lock*/ have_lock);
12700 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12701 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12702 msg.scsi.sense_len, M_WAITOK);
12705 getbinuptime(&cur_bt);
12706 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12707 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12709 io->io_hdr.num_dmas++;
12713 * The DMA to the remote side is done, now we need to tell the other side
12714 * we're done so it can continue with its data movement.
12717 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12724 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12725 printf("%s: ISC DMA write failed with error %d", __func__,
12727 ctl_set_internal_failure(&io->scsiio,
12729 /*retry_count*/ rq->ret);
12732 ctl_dt_req_free(rq);
12734 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12735 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12736 free(io->io_hdr.remote_sglist, M_CTL);
12737 io->io_hdr.remote_sglist = NULL;
12738 io->io_hdr.local_sglist = NULL;
12741 * The data is in local and remote memory, so now we need to send
12742 * status (good or back) back to the other side.
12744 ctl_send_datamove_done(io, /*have_lock*/ 0);
12748 * We've moved the data from the host/controller into local memory. Now we
12749 * need to push it over to the remote controller's memory.
12752 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12756 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12757 ctl_datamove_remote_write_cb);
12762 ctl_datamove_remote_write(union ctl_io *io)
12765 void (*fe_datamove)(union ctl_io *io);
12768 * - Get the data from the host/HBA into local memory.
12769 * - DMA memory from the local controller to the remote controller.
12770 * - Send status back to the remote controller.
12773 retval = ctl_datamove_remote_sgl_setup(io);
12777 /* Switch the pointer over so the FETD knows what to do */
12778 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12781 * Use a custom move done callback, since we need to send completion
12782 * back to the other controller, not to the backend on this side.
12784 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12786 fe_datamove = CTL_PORT(io)->fe_datamove;
12791 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12800 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12801 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12802 free(io->io_hdr.remote_sglist, M_CTL);
12803 io->io_hdr.remote_sglist = NULL;
12804 io->io_hdr.local_sglist = NULL;
12807 scsi_path_string(io, path_str, sizeof(path_str));
12808 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12809 sbuf_cat(&sb, path_str);
12810 scsi_command_string(&io->scsiio, NULL, &sb);
12811 sbuf_printf(&sb, "\n");
12812 sbuf_cat(&sb, path_str);
12813 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12814 io->scsiio.tag_num, io->scsiio.tag_type);
12815 sbuf_cat(&sb, path_str);
12816 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12817 io->io_hdr.flags, io->io_hdr.status);
12819 printk("%s", sbuf_data(&sb));
12824 * The read is done, now we need to send status (good or bad) back
12825 * to the other side.
12827 ctl_send_datamove_done(io, /*have_lock*/ 0);
12833 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12836 void (*fe_datamove)(union ctl_io *io);
12840 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12841 printf("%s: ISC DMA read failed with error %d\n", __func__,
12843 ctl_set_internal_failure(&io->scsiio,
12845 /*retry_count*/ rq->ret);
12848 ctl_dt_req_free(rq);
12850 /* Switch the pointer over so the FETD knows what to do */
12851 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12854 * Use a custom move done callback, since we need to send completion
12855 * back to the other controller, not to the backend on this side.
12857 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12859 /* XXX KDM add checks like the ones in ctl_datamove? */
12861 fe_datamove = CTL_PORT(io)->fe_datamove;
12866 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12868 struct ctl_sg_entry *local_sglist;
12869 uint32_t len_to_go;
12874 local_sglist = io->io_hdr.local_sglist;
12875 len_to_go = io->scsiio.kern_data_len;
12878 * The difficult thing here is that the size of the various
12879 * S/G segments may be different than the size from the
12880 * remote controller. That'll make it harder when DMAing
12881 * the data back to the other side.
12883 for (i = 0; len_to_go > 0; i++) {
12884 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12885 local_sglist[i].addr =
12886 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12888 len_to_go -= local_sglist[i].len;
12891 * Reset the number of S/G entries accordingly. The original
12892 * number of S/G entries is available in rem_sg_entries.
12894 io->scsiio.kern_sg_entries = i;
12897 printf("%s: kern_sg_entries = %d\n", __func__,
12898 io->scsiio.kern_sg_entries);
12899 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12900 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12901 local_sglist[i].addr, local_sglist[i].len);
12908 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12909 ctl_ha_dt_cb callback)
12911 struct ctl_ha_dt_req *rq;
12912 struct ctl_sg_entry *remote_sglist, *local_sglist;
12913 uint32_t local_used, remote_used, total_used;
12916 rq = ctl_dt_req_alloc();
12919 * If we failed to allocate the request, and if the DMA didn't fail
12920 * anyway, set busy status. This is just a resource allocation
12924 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12925 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12926 ctl_set_busy(&io->scsiio);
12928 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12929 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12932 ctl_dt_req_free(rq);
12935 * The data move failed. We need to return status back
12936 * to the other controller. No point in trying to DMA
12937 * data to the remote controller.
12940 ctl_send_datamove_done(io, /*have_lock*/ 0);
12945 local_sglist = io->io_hdr.local_sglist;
12946 remote_sglist = io->io_hdr.remote_sglist;
12952 * Pull/push the data over the wire from/to the other controller.
12953 * This takes into account the possibility that the local and
12954 * remote sglists may not be identical in terms of the size of
12955 * the elements and the number of elements.
12957 * One fundamental assumption here is that the length allocated for
12958 * both the local and remote sglists is identical. Otherwise, we've
12959 * essentially got a coding error of some sort.
12961 isc_ret = CTL_HA_STATUS_SUCCESS;
12962 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12966 rq->command = command;
12970 * Both pointers should be aligned. But it is possible
12971 * that the allocation length is not. They should both
12972 * also have enough slack left over at the end, though,
12973 * to round up to the next 8 byte boundary.
12975 cur_len = MIN(local_sglist[i].len - local_used,
12976 remote_sglist[j].len - remote_used);
12977 rq->size = cur_len;
12979 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12980 tmp_ptr += local_used;
12983 /* Use physical addresses when talking to ISC hardware */
12984 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12985 /* XXX KDM use busdma */
12986 rq->local = vtophys(tmp_ptr);
12988 rq->local = tmp_ptr;
12990 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12991 ("HA does not support BUS_ADDR"));
12992 rq->local = tmp_ptr;
12995 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12996 tmp_ptr += remote_used;
12997 rq->remote = tmp_ptr;
12999 rq->callback = NULL;
13001 local_used += cur_len;
13002 if (local_used >= local_sglist[i].len) {
13007 remote_used += cur_len;
13008 if (remote_used >= remote_sglist[j].len) {
13012 total_used += cur_len;
13014 if (total_used >= io->scsiio.kern_data_len)
13015 rq->callback = callback;
13018 printf("%s: %s: local %p remote %p size %d\n", __func__,
13019 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
13020 rq->local, rq->remote, rq->size);
13023 isc_ret = ctl_dt_single(rq);
13024 if (isc_ret > CTL_HA_STATUS_SUCCESS)
13027 if (isc_ret != CTL_HA_STATUS_WAIT) {
13036 ctl_datamove_remote_read(union ctl_io *io)
13042 * This will send an error to the other controller in the case of a
13045 retval = ctl_datamove_remote_sgl_setup(io);
13049 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
13050 ctl_datamove_remote_read_cb);
13053 * Make sure we free memory if there was an error.. The
13054 * ctl_datamove_remote_xfer() function will send the
13055 * datamove done message, or call the callback with an
13056 * error if there is a problem.
13058 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
13059 free(io->io_hdr.local_sglist[i].addr, M_CTL);
13060 free(io->io_hdr.remote_sglist, M_CTL);
13061 io->io_hdr.remote_sglist = NULL;
13062 io->io_hdr.local_sglist = NULL;
13067 * Process a datamove request from the other controller. This is used for
13068 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
13069 * first. Once that is complete, the data gets DMAed into the remote
13070 * controller's memory. For reads, we DMA from the remote controller's
13071 * memory into our memory first, and then move it out to the FETD.
13074 ctl_datamove_remote(union ctl_io *io)
13077 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
13079 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
13080 ctl_failover_io(io, /*have_lock*/ 0);
13085 * Note that we look for an aborted I/O here, but don't do some of
13086 * the other checks that ctl_datamove() normally does.
13087 * We don't need to run the datamove delay code, since that should
13088 * have been done if need be on the other controller.
13090 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
13091 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
13092 io->scsiio.tag_num, io->io_hdr.nexus.initid,
13093 io->io_hdr.nexus.targ_port,
13094 io->io_hdr.nexus.targ_lun);
13095 io->io_hdr.port_status = 31338;
13096 ctl_send_datamove_done(io, /*have_lock*/ 0);
13100 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
13101 ctl_datamove_remote_write(io);
13102 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
13103 ctl_datamove_remote_read(io);
13105 io->io_hdr.port_status = 31339;
13106 ctl_send_datamove_done(io, /*have_lock*/ 0);
13111 ctl_process_done(union ctl_io *io)
13113 struct ctl_softc *softc = CTL_SOFTC(io);
13114 struct ctl_port *port = CTL_PORT(io);
13115 struct ctl_lun *lun = CTL_LUN(io);
13116 void (*fe_done)(union ctl_io *io);
13117 union ctl_ha_msg msg;
13119 CTL_DEBUG_PRINT(("ctl_process_done\n"));
13120 fe_done = port->fe_done;
13123 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
13128 ctl_scsi_path_string(io, path_str, sizeof(path_str));
13129 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
13131 sbuf_cat(&sb, path_str);
13132 switch (io->io_hdr.io_type) {
13134 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
13135 sbuf_printf(&sb, "\n");
13136 sbuf_cat(&sb, path_str);
13137 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
13138 io->scsiio.tag_num, io->scsiio.tag_type);
13141 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
13142 "Tag Type: %d\n", io->taskio.task_action,
13143 io->taskio.tag_num, io->taskio.tag_type);
13146 panic("%s: Invalid CTL I/O type %d\n",
13147 __func__, io->io_hdr.io_type);
13149 sbuf_cat(&sb, path_str);
13150 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
13151 (intmax_t)time_uptime - io->io_hdr.start_time);
13153 printf("%s", sbuf_data(&sb));
13155 #endif /* CTL_TIME_IO */
13157 switch (io->io_hdr.io_type) {
13161 if (ctl_debug & CTL_DEBUG_INFO)
13162 ctl_io_error_print(io, NULL);
13166 panic("%s: Invalid CTL I/O type %d\n",
13167 __func__, io->io_hdr.io_type);
13171 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13172 io->io_hdr.nexus.targ_mapped_lun));
13176 mtx_lock(&lun->lun_lock);
13179 * Check to see if we have any informational exception and status
13180 * of this command can be modified to report it in form of either
13181 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13183 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13184 io->io_hdr.status == CTL_SUCCESS &&
13185 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13186 uint8_t mrie = lun->MODE_IE.mrie;
13187 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13188 (lun->MODE_VER.byte3 & SMS_VER_PER));
13189 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13190 mrie == SIEP_MRIE_REC_UNCOND ||
13191 mrie == SIEP_MRIE_NO_SENSE) &&
13192 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13193 CTL_CMD_FLAG_NO_SENSE) == 0) {
13194 ctl_set_sense(&io->scsiio,
13195 /*current_error*/ 1,
13196 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13197 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13198 /*asc*/ lun->ie_asc,
13199 /*ascq*/ lun->ie_ascq,
13201 lun->ie_reported = 1;
13203 } else if (lun->ie_reported < 0)
13204 lun->ie_reported = 0;
13207 * Check to see if we have any errors to inject here. We only
13208 * inject errors for commands that don't already have errors set.
13210 if (!STAILQ_EMPTY(&lun->error_list) &&
13211 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13212 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13213 ctl_inject_error(lun, io);
13216 * XXX KDM how do we treat commands that aren't completed
13219 * XXX KDM should we also track I/O latency?
13221 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13222 io->io_hdr.io_type == CTL_IO_SCSI) {
13228 bintime_sub(&bt, &io->io_hdr.start_bt);
13230 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13232 type = CTL_STATS_READ;
13233 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13235 type = CTL_STATS_WRITE;
13237 type = CTL_STATS_NO_IO;
13239 #ifdef CTL_LEGACY_STATS
13240 uint32_t targ_port = port->targ_port;
13241 lun->legacy_stats.ports[targ_port].bytes[type] +=
13242 io->scsiio.kern_total_len;
13243 lun->legacy_stats.ports[targ_port].operations[type] ++;
13244 lun->legacy_stats.ports[targ_port].num_dmas[type] +=
13245 io->io_hdr.num_dmas;
13247 bintime_add(&lun->legacy_stats.ports[targ_port].dma_time[type],
13248 &io->io_hdr.dma_bt);
13249 bintime_add(&lun->legacy_stats.ports[targ_port].time[type],
13252 #endif /* CTL_LEGACY_STATS */
13254 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13255 lun->stats.operations[type] ++;
13256 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13258 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13259 bintime_add(&lun->stats.time[type], &bt);
13262 mtx_lock(&port->port_lock);
13263 port->stats.bytes[type] += io->scsiio.kern_total_len;
13264 port->stats.operations[type] ++;
13265 port->stats.dmas[type] += io->io_hdr.num_dmas;
13267 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13268 bintime_add(&port->stats.time[type], &bt);
13270 mtx_unlock(&port->port_lock);
13274 * Remove this from the OOA queue.
13276 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13278 if (TAILQ_EMPTY(&lun->ooa_queue))
13279 lun->last_busy = getsbinuptime();
13283 * Run through the blocked queue on this LUN and see if anything
13284 * has become unblocked, now that this transaction is done.
13286 ctl_check_blocked(lun);
13289 * If the LUN has been invalidated, free it if there is nothing
13290 * left on its OOA queue.
13292 if ((lun->flags & CTL_LUN_INVALID)
13293 && TAILQ_EMPTY(&lun->ooa_queue)) {
13294 mtx_unlock(&lun->lun_lock);
13295 mtx_lock(&softc->ctl_lock);
13297 mtx_unlock(&softc->ctl_lock);
13299 mtx_unlock(&lun->lun_lock);
13304 * If this command has been aborted, make sure we set the status
13305 * properly. The FETD is responsible for freeing the I/O and doing
13306 * whatever it needs to do to clean up its state.
13308 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13309 ctl_set_task_aborted(&io->scsiio);
13312 * If enabled, print command error status.
13314 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13315 (ctl_debug & CTL_DEBUG_INFO) != 0)
13316 ctl_io_error_print(io, NULL);
13319 * Tell the FETD or the other shelf controller we're done with this
13320 * command. Note that only SCSI commands get to this point. Task
13321 * management commands are completed above.
13323 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13324 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13325 memset(&msg, 0, sizeof(msg));
13326 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13327 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13328 msg.hdr.nexus = io->io_hdr.nexus;
13329 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13330 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13339 * Front end should call this if it doesn't do autosense. When the request
13340 * sense comes back in from the initiator, we'll dequeue this and send it.
13343 ctl_queue_sense(union ctl_io *io)
13345 struct ctl_softc *softc = CTL_SOFTC(io);
13346 struct ctl_port *port = CTL_PORT(io);
13347 struct ctl_lun *lun;
13348 uint32_t initidx, targ_lun;
13350 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13352 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13355 * LUN lookup will likely move to the ctl_work_thread() once we
13356 * have our new queueing infrastructure (that doesn't put things on
13357 * a per-LUN queue initially). That is so that we can handle
13358 * things like an INQUIRY to a LUN that we don't have enabled. We
13359 * can't deal with that right now.
13360 * If we don't have a LUN for this, just toss the sense information.
13362 mtx_lock(&softc->ctl_lock);
13363 if (targ_lun >= CTL_MAX_LUNS ||
13364 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13365 mtx_unlock(&softc->ctl_lock);
13368 mtx_lock(&lun->lun_lock);
13369 mtx_unlock(&softc->ctl_lock);
13372 * Already have CA set for this LUN...toss the sense information.
13374 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13375 if (ctl_is_set(lun->have_ca, initidx)) {
13376 mtx_unlock(&lun->lun_lock);
13380 memcpy(&lun->pending_sense[initidx], &io->scsiio.sense_data,
13381 MIN(sizeof(lun->pending_sense[initidx]),
13382 sizeof(io->scsiio.sense_data)));
13383 ctl_set_mask(lun->have_ca, initidx);
13384 mtx_unlock(&lun->lun_lock);
13388 return (CTL_RETVAL_COMPLETE);
13393 * Primary command inlet from frontend ports. All SCSI and task I/O
13394 * requests must go through this function.
13397 ctl_queue(union ctl_io *io)
13399 struct ctl_port *port = CTL_PORT(io);
13401 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13404 io->io_hdr.start_time = time_uptime;
13405 getbinuptime(&io->io_hdr.start_bt);
13406 #endif /* CTL_TIME_IO */
13408 /* Map FE-specific LUN ID into global one. */
13409 io->io_hdr.nexus.targ_mapped_lun =
13410 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13412 switch (io->io_hdr.io_type) {
13415 if (ctl_debug & CTL_DEBUG_CDB)
13417 ctl_enqueue_incoming(io);
13420 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13424 return (CTL_RETVAL_COMPLETE);
13427 #ifdef CTL_IO_DELAY
13429 ctl_done_timer_wakeup(void *arg)
13433 io = (union ctl_io *)arg;
13436 #endif /* CTL_IO_DELAY */
13439 ctl_serseq_done(union ctl_io *io)
13441 struct ctl_lun *lun = CTL_LUN(io);;
13443 if (lun->be_lun == NULL ||
13444 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13446 mtx_lock(&lun->lun_lock);
13447 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13448 ctl_check_blocked(lun);
13449 mtx_unlock(&lun->lun_lock);
13453 ctl_done(union ctl_io *io)
13457 * Enable this to catch duplicate completion issues.
13460 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13461 printf("%s: type %d msg %d cdb %x iptl: "
13462 "%u:%u:%u tag 0x%04x "
13463 "flag %#x status %x\n",
13465 io->io_hdr.io_type,
13466 io->io_hdr.msg_type,
13468 io->io_hdr.nexus.initid,
13469 io->io_hdr.nexus.targ_port,
13470 io->io_hdr.nexus.targ_lun,
13471 (io->io_hdr.io_type ==
13473 io->taskio.tag_num :
13474 io->scsiio.tag_num,
13476 io->io_hdr.status);
13478 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13482 * This is an internal copy of an I/O, and should not go through
13483 * the normal done processing logic.
13485 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13488 #ifdef CTL_IO_DELAY
13489 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13490 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13492 struct ctl_lun *lun = CTL_LUN(io);
13495 && (lun->delay_info.done_delay > 0)) {
13497 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13498 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13499 callout_reset(&io->io_hdr.delay_callout,
13500 lun->delay_info.done_delay * hz,
13501 ctl_done_timer_wakeup, io);
13502 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13503 lun->delay_info.done_delay = 0;
13507 #endif /* CTL_IO_DELAY */
13509 ctl_enqueue_done(io);
13513 ctl_work_thread(void *arg)
13515 struct ctl_thread *thr = (struct ctl_thread *)arg;
13516 struct ctl_softc *softc = thr->ctl_softc;
13520 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13524 * We handle the queues in this order:
13526 * - done queue (to free up resources, unblock other commands)
13530 * If those queues are empty, we break out of the loop and
13533 mtx_lock(&thr->queue_lock);
13534 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13536 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13537 mtx_unlock(&thr->queue_lock);
13538 ctl_handle_isc(io);
13541 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13543 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13544 /* clear any blocked commands, call fe_done */
13545 mtx_unlock(&thr->queue_lock);
13546 ctl_process_done(io);
13549 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13551 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13552 mtx_unlock(&thr->queue_lock);
13553 if (io->io_hdr.io_type == CTL_IO_TASK)
13556 ctl_scsiio_precheck(softc, &io->scsiio);
13559 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13561 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13562 mtx_unlock(&thr->queue_lock);
13563 retval = ctl_scsiio(&io->scsiio);
13564 if (retval != CTL_RETVAL_COMPLETE)
13565 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13569 /* Sleep until we have something to do. */
13570 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13575 ctl_lun_thread(void *arg)
13577 struct ctl_softc *softc = (struct ctl_softc *)arg;
13578 struct ctl_be_lun *be_lun;
13580 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13583 mtx_lock(&softc->ctl_lock);
13584 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13585 if (be_lun != NULL) {
13586 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13587 mtx_unlock(&softc->ctl_lock);
13588 ctl_create_lun(be_lun);
13592 /* Sleep until we have something to do. */
13593 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13594 PDROP | PRIBIO, "-", 0);
13599 ctl_thresh_thread(void *arg)
13601 struct ctl_softc *softc = (struct ctl_softc *)arg;
13602 struct ctl_lun *lun;
13603 struct ctl_logical_block_provisioning_page *page;
13605 union ctl_ha_msg msg;
13606 uint64_t thres, val;
13609 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13612 mtx_lock(&softc->ctl_lock);
13613 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13614 if ((lun->flags & CTL_LUN_DISABLED) ||
13615 (lun->flags & CTL_LUN_NO_MEDIA) ||
13616 lun->backend->lun_attr == NULL)
13618 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13619 softc->ha_mode == CTL_HA_MODE_XFER)
13621 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13624 page = &lun->MODE_LBP;
13625 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13626 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13628 thres = scsi_4btoul(page->descr[i].count);
13629 thres <<= CTL_LBP_EXPONENT;
13630 switch (page->descr[i].resource) {
13632 attr = "blocksavail";
13635 attr = "blocksused";
13638 attr = "poolblocksavail";
13641 attr = "poolblocksused";
13646 mtx_unlock(&softc->ctl_lock); // XXX
13647 val = lun->backend->lun_attr(
13648 lun->be_lun->be_lun, attr);
13649 mtx_lock(&softc->ctl_lock);
13650 if (val == UINT64_MAX)
13652 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13653 == SLBPPD_ARMING_INC)
13654 e = (val >= thres);
13656 e = (val <= thres);
13660 mtx_lock(&lun->lun_lock);
13662 scsi_u64to8b((uint8_t *)&page->descr[i] -
13663 (uint8_t *)page, lun->ua_tpt_info);
13664 if (lun->lasttpt == 0 ||
13665 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13666 lun->lasttpt = time_uptime;
13667 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13673 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13676 mtx_unlock(&lun->lun_lock);
13678 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13679 /* Send msg to other side. */
13680 bzero(&msg.ua, sizeof(msg.ua));
13681 msg.hdr.msg_type = CTL_MSG_UA;
13682 msg.hdr.nexus.initid = -1;
13683 msg.hdr.nexus.targ_port = -1;
13684 msg.hdr.nexus.targ_lun = lun->lun;
13685 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13687 msg.ua.ua_set = (set > 0);
13688 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13689 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13690 mtx_unlock(&softc->ctl_lock); // XXX
13691 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13692 sizeof(msg.ua), M_WAITOK);
13693 mtx_lock(&softc->ctl_lock);
13696 mtx_unlock(&softc->ctl_lock);
13697 pause("-", CTL_LBP_PERIOD * hz);
13702 ctl_enqueue_incoming(union ctl_io *io)
13704 struct ctl_softc *softc = CTL_SOFTC(io);
13705 struct ctl_thread *thr;
13708 idx = (io->io_hdr.nexus.targ_port * 127 +
13709 io->io_hdr.nexus.initid) % worker_threads;
13710 thr = &softc->threads[idx];
13711 mtx_lock(&thr->queue_lock);
13712 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13713 mtx_unlock(&thr->queue_lock);
13718 ctl_enqueue_rtr(union ctl_io *io)
13720 struct ctl_softc *softc = CTL_SOFTC(io);
13721 struct ctl_thread *thr;
13723 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13724 mtx_lock(&thr->queue_lock);
13725 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13726 mtx_unlock(&thr->queue_lock);
13731 ctl_enqueue_done(union ctl_io *io)
13733 struct ctl_softc *softc = CTL_SOFTC(io);
13734 struct ctl_thread *thr;
13736 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13737 mtx_lock(&thr->queue_lock);
13738 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13739 mtx_unlock(&thr->queue_lock);
13744 ctl_enqueue_isc(union ctl_io *io)
13746 struct ctl_softc *softc = CTL_SOFTC(io);
13747 struct ctl_thread *thr;
13749 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13750 mtx_lock(&thr->queue_lock);
13751 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13752 mtx_unlock(&thr->queue_lock);