2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003-2009 Silicon Graphics International Corp.
5 * Copyright (c) 2012 The FreeBSD Foundation
6 * Copyright (c) 2014-2017 Alexander Motin <mav@FreeBSD.org>
7 * Copyright (c) 2017 Jakub Wojciech Klama <jceel@FreeBSD.org>
8 * Copyright (c) 2018 Marcelo Araujo <araujo@FreeBSD.org>
11 * Portions of this software were developed by Edward Tomasz Napierala
12 * under sponsorship from the FreeBSD Foundation.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions, and the following disclaimer,
19 * without modification.
20 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
21 * substantially similar to the "NO WARRANTY" disclaimer below
22 * ("Disclaimer") and any redistribution must be conditioned upon
23 * including a substantially similar Disclaimer requirement for further
24 * binary redistribution.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGES.
42 * CAM Target Layer, a SCSI device emulation subsystem.
44 * Author: Ken Merry <ken@FreeBSD.org>
47 #include <sys/cdefs.h>
48 __FBSDID("$FreeBSD$");
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/ctype.h>
53 #include <sys/kernel.h>
54 #include <sys/types.h>
55 #include <sys/kthread.h>
57 #include <sys/fcntl.h>
59 #include <sys/module.h>
60 #include <sys/mutex.h>
61 #include <sys/condvar.h>
62 #include <sys/malloc.h>
64 #include <sys/ioccom.h>
65 #include <sys/queue.h>
68 #include <sys/endian.h>
69 #include <sys/sysctl.h>
75 #include <cam/scsi/scsi_all.h>
76 #include <cam/scsi/scsi_cd.h>
77 #include <cam/scsi/scsi_da.h>
78 #include <cam/ctl/ctl_io.h>
79 #include <cam/ctl/ctl.h>
80 #include <cam/ctl/ctl_frontend.h>
81 #include <cam/ctl/ctl_util.h>
82 #include <cam/ctl/ctl_backend.h>
83 #include <cam/ctl/ctl_ioctl.h>
84 #include <cam/ctl/ctl_ha.h>
85 #include <cam/ctl/ctl_private.h>
86 #include <cam/ctl/ctl_debug.h>
87 #include <cam/ctl/ctl_scsi_all.h>
88 #include <cam/ctl/ctl_error.h>
90 struct ctl_softc *control_softc = NULL;
93 * Template mode pages.
97 * Note that these are default values only. The actual values will be
98 * filled in when the user does a mode sense.
100 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
101 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
102 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
103 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
104 /*read_retry_count*/0,
105 /*correction_span*/0,
106 /*head_offset_count*/0,
107 /*data_strobe_offset_cnt*/0,
108 /*byte8*/SMS_RWER_LBPERE,
109 /*write_retry_count*/0,
111 /*recovery_time_limit*/{0, 0},
114 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
115 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
116 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
117 /*byte3*/SMS_RWER_PER,
118 /*read_retry_count*/0,
119 /*correction_span*/0,
120 /*head_offset_count*/0,
121 /*data_strobe_offset_cnt*/0,
122 /*byte8*/SMS_RWER_LBPERE,
123 /*write_retry_count*/0,
125 /*recovery_time_limit*/{0, 0},
128 const static struct scsi_format_page format_page_default = {
129 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
130 /*page_length*/sizeof(struct scsi_format_page) - 2,
131 /*tracks_per_zone*/ {0, 0},
132 /*alt_sectors_per_zone*/ {0, 0},
133 /*alt_tracks_per_zone*/ {0, 0},
134 /*alt_tracks_per_lun*/ {0, 0},
135 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
136 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
137 /*bytes_per_sector*/ {0, 0},
138 /*interleave*/ {0, 0},
139 /*track_skew*/ {0, 0},
140 /*cylinder_skew*/ {0, 0},
142 /*reserved*/ {0, 0, 0}
145 const static struct scsi_format_page format_page_changeable = {
146 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
147 /*page_length*/sizeof(struct scsi_format_page) - 2,
148 /*tracks_per_zone*/ {0, 0},
149 /*alt_sectors_per_zone*/ {0, 0},
150 /*alt_tracks_per_zone*/ {0, 0},
151 /*alt_tracks_per_lun*/ {0, 0},
152 /*sectors_per_track*/ {0, 0},
153 /*bytes_per_sector*/ {0, 0},
154 /*interleave*/ {0, 0},
155 /*track_skew*/ {0, 0},
156 /*cylinder_skew*/ {0, 0},
158 /*reserved*/ {0, 0, 0}
161 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
162 /*page_code*/SMS_RIGID_DISK_PAGE,
163 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
164 /*cylinders*/ {0, 0, 0},
165 /*heads*/ CTL_DEFAULT_HEADS,
166 /*start_write_precomp*/ {0, 0, 0},
167 /*start_reduced_current*/ {0, 0, 0},
168 /*step_rate*/ {0, 0},
169 /*landing_zone_cylinder*/ {0, 0, 0},
170 /*rpl*/ SRDP_RPL_DISABLED,
171 /*rotational_offset*/ 0,
173 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
174 CTL_DEFAULT_ROTATION_RATE & 0xff},
178 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
179 /*page_code*/SMS_RIGID_DISK_PAGE,
180 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
181 /*cylinders*/ {0, 0, 0},
183 /*start_write_precomp*/ {0, 0, 0},
184 /*start_reduced_current*/ {0, 0, 0},
185 /*step_rate*/ {0, 0},
186 /*landing_zone_cylinder*/ {0, 0, 0},
188 /*rotational_offset*/ 0,
190 /*rotation_rate*/ {0, 0},
194 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
195 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
196 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
198 /*read_retry_count*/0,
199 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
200 /*recovery_time_limit*/{0, 0},
203 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
204 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
205 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
206 /*byte3*/SMS_VER_PER,
207 /*read_retry_count*/0,
208 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
209 /*recovery_time_limit*/{0, 0},
212 const static struct scsi_caching_page caching_page_default = {
213 /*page_code*/SMS_CACHING_PAGE,
214 /*page_length*/sizeof(struct scsi_caching_page) - 2,
215 /*flags1*/ SCP_DISC | SCP_WCE,
217 /*disable_pf_transfer_len*/ {0xff, 0xff},
218 /*min_prefetch*/ {0, 0},
219 /*max_prefetch*/ {0xff, 0xff},
220 /*max_pf_ceiling*/ {0xff, 0xff},
222 /*cache_segments*/ 0,
223 /*cache_seg_size*/ {0, 0},
225 /*non_cache_seg_size*/ {0, 0, 0}
228 const static struct scsi_caching_page caching_page_changeable = {
229 /*page_code*/SMS_CACHING_PAGE,
230 /*page_length*/sizeof(struct scsi_caching_page) - 2,
231 /*flags1*/ SCP_WCE | SCP_RCD,
233 /*disable_pf_transfer_len*/ {0, 0},
234 /*min_prefetch*/ {0, 0},
235 /*max_prefetch*/ {0, 0},
236 /*max_pf_ceiling*/ {0, 0},
238 /*cache_segments*/ 0,
239 /*cache_seg_size*/ {0, 0},
241 /*non_cache_seg_size*/ {0, 0, 0}
244 const static struct scsi_control_page control_page_default = {
245 /*page_code*/SMS_CONTROL_MODE_PAGE,
246 /*page_length*/sizeof(struct scsi_control_page) - 2,
248 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
251 /*aen_holdoff_period*/{0, 0},
252 /*busy_timeout_period*/{0, 0},
253 /*extended_selftest_completion_time*/{0, 0}
256 const static struct scsi_control_page control_page_changeable = {
257 /*page_code*/SMS_CONTROL_MODE_PAGE,
258 /*page_length*/sizeof(struct scsi_control_page) - 2,
260 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
261 /*eca_and_aen*/SCP_SWP,
263 /*aen_holdoff_period*/{0, 0},
264 /*busy_timeout_period*/{0, 0},
265 /*extended_selftest_completion_time*/{0, 0}
268 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
270 const static struct scsi_control_ext_page control_ext_page_default = {
271 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
272 /*subpage_code*/0x01,
273 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
279 const static struct scsi_control_ext_page control_ext_page_changeable = {
280 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
281 /*subpage_code*/0x01,
282 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
288 const static struct scsi_info_exceptions_page ie_page_default = {
289 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
290 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
291 /*info_flags*/SIEP_FLAGS_EWASC,
292 /*mrie*/SIEP_MRIE_NO,
293 /*interval_timer*/{0, 0, 0, 0},
294 /*report_count*/{0, 0, 0, 1}
297 const static struct scsi_info_exceptions_page ie_page_changeable = {
298 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
299 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
300 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
303 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
304 /*report_count*/{0xff, 0xff, 0xff, 0xff}
307 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
309 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
310 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
311 /*subpage_code*/0x02,
312 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
314 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
319 /*count*/{0, 0, 0, 0}},
323 /*count*/{0, 0, 0, 0}},
327 /*count*/{0, 0, 0, 0}},
331 /*count*/{0, 0, 0, 0}}
335 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
336 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
337 /*subpage_code*/0x02,
338 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
339 /*flags*/SLBPP_SITUA,
340 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
345 /*count*/{0, 0, 0, 0}},
349 /*count*/{0, 0, 0, 0}},
353 /*count*/{0, 0, 0, 0}},
357 /*count*/{0, 0, 0, 0}}
361 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
362 /*page_code*/SMS_CDDVD_CAPS_PAGE,
363 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
371 /*nvol_levels*/{0, 0},
372 /*buffer_size*/{8, 0},
377 /*copy_management*/0,
379 /*rotation_control*/0,
380 /*cur_write_speed*/0,
381 /*num_speed_descr*/0,
384 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
385 /*page_code*/SMS_CDDVD_CAPS_PAGE,
386 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
394 /*nvol_levels*/{0, 0},
395 /*buffer_size*/{0, 0},
400 /*copy_management*/0,
402 /*rotation_control*/0,
403 /*cur_write_speed*/0,
404 /*num_speed_descr*/0,
407 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD, 0, "CAM Target Layer");
408 static int worker_threads = -1;
409 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
410 &worker_threads, 1, "Number of worker threads");
411 static int ctl_debug = CTL_DEBUG_NONE;
412 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
413 &ctl_debug, 0, "Enabled debug flags");
414 static int ctl_lun_map_size = 1024;
415 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
416 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
418 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS;
419 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN,
420 &ctl_time_io_secs, 0, "Log requests taking more seconds");
424 * Maximum number of LUNs we support. MUST be a power of 2.
426 #define CTL_DEFAULT_MAX_LUNS 1024
427 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
428 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns);
429 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN,
430 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs");
433 * Maximum number of ports registered at one time.
435 #define CTL_DEFAULT_MAX_PORTS 256
436 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
437 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports);
438 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN,
439 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports");
442 * Maximum number of initiators we support.
444 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports)
447 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
448 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
449 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
450 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
452 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
454 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
456 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
457 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
458 static int ctl_init(void);
459 static int ctl_shutdown(void);
460 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
461 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
462 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
463 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
464 struct ctl_ooa *ooa_hdr,
465 struct ctl_ooa_entry *kern_entries);
466 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
468 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
469 struct ctl_be_lun *be_lun);
470 static int ctl_free_lun(struct ctl_lun *lun);
471 static void ctl_create_lun(struct ctl_be_lun *be_lun);
473 static int ctl_do_mode_select(union ctl_io *io);
474 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
475 uint64_t res_key, uint64_t sa_res_key,
476 uint8_t type, uint32_t residx,
477 struct ctl_scsiio *ctsio,
478 struct scsi_per_res_out *cdb,
479 struct scsi_per_res_out_parms* param);
480 static void ctl_pro_preempt_other(struct ctl_lun *lun,
481 union ctl_ha_msg *msg);
482 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
483 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
484 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
485 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
486 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
487 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
488 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
490 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
492 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
493 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
494 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
495 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
496 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
497 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
499 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
500 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
501 union ctl_io *pending_io, union ctl_io *ooa_io);
502 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
503 union ctl_io *starting_io);
504 static int ctl_check_blocked(struct ctl_lun *lun);
505 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
506 const struct ctl_cmd_entry *entry,
507 struct ctl_scsiio *ctsio);
508 static void ctl_failover_lun(union ctl_io *io);
509 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
510 struct ctl_scsiio *ctsio);
511 static int ctl_scsiio(struct ctl_scsiio *ctsio);
513 static int ctl_target_reset(union ctl_io *io);
514 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
515 ctl_ua_type ua_type);
516 static int ctl_lun_reset(union ctl_io *io);
517 static int ctl_abort_task(union ctl_io *io);
518 static int ctl_abort_task_set(union ctl_io *io);
519 static int ctl_query_task(union ctl_io *io, int task_set);
520 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
521 ctl_ua_type ua_type);
522 static int ctl_i_t_nexus_reset(union ctl_io *io);
523 static int ctl_query_async_event(union ctl_io *io);
524 static void ctl_run_task(union ctl_io *io);
526 static void ctl_datamove_timer_wakeup(void *arg);
527 static void ctl_done_timer_wakeup(void *arg);
528 #endif /* CTL_IO_DELAY */
530 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
531 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
532 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
533 static void ctl_datamove_remote_write(union ctl_io *io);
534 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
535 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
536 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
537 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
538 ctl_ha_dt_cb callback);
539 static void ctl_datamove_remote_read(union ctl_io *io);
540 static void ctl_datamove_remote(union ctl_io *io);
541 static void ctl_process_done(union ctl_io *io);
542 static void ctl_lun_thread(void *arg);
543 static void ctl_thresh_thread(void *arg);
544 static void ctl_work_thread(void *arg);
545 static void ctl_enqueue_incoming(union ctl_io *io);
546 static void ctl_enqueue_rtr(union ctl_io *io);
547 static void ctl_enqueue_done(union ctl_io *io);
548 static void ctl_enqueue_isc(union ctl_io *io);
549 static const struct ctl_cmd_entry *
550 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
551 static const struct ctl_cmd_entry *
552 ctl_validate_command(struct ctl_scsiio *ctsio);
553 static int ctl_cmd_applicable(uint8_t lun_type,
554 const struct ctl_cmd_entry *entry);
555 static int ctl_ha_init(void);
556 static int ctl_ha_shutdown(void);
558 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
559 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
560 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
561 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
564 * Load the serialization table. This isn't very pretty, but is probably
565 * the easiest way to do it.
567 #include "ctl_ser_table.c"
570 * We only need to define open, close and ioctl routines for this driver.
572 static struct cdevsw ctl_cdevsw = {
573 .d_version = D_VERSION,
576 .d_close = ctl_close,
577 .d_ioctl = ctl_ioctl,
582 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
584 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
586 static moduledata_t ctl_moduledata = {
588 ctl_module_event_handler,
592 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
593 MODULE_VERSION(ctl, 1);
595 static struct ctl_frontend ha_frontend =
599 .shutdown = ctl_ha_shutdown,
605 struct ctl_softc *softc = control_softc;
607 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
608 &softc->othersc_pool) != 0)
610 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
611 ctl_pool_free(softc->othersc_pool);
614 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
615 != CTL_HA_STATUS_SUCCESS) {
616 ctl_ha_msg_destroy(softc);
617 ctl_pool_free(softc->othersc_pool);
624 ctl_ha_shutdown(void)
626 struct ctl_softc *softc = control_softc;
627 struct ctl_port *port;
629 ctl_ha_msg_shutdown(softc);
630 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
632 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
634 ctl_pool_free(softc->othersc_pool);
635 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
636 ctl_port_deregister(port);
637 free(port->port_name, M_CTL);
644 ctl_ha_datamove(union ctl_io *io)
646 struct ctl_lun *lun = CTL_LUN(io);
647 struct ctl_sg_entry *sgl;
648 union ctl_ha_msg msg;
649 uint32_t sg_entries_sent;
650 int do_sg_copy, i, j;
652 memset(&msg.dt, 0, sizeof(msg.dt));
653 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
654 msg.hdr.original_sc = io->io_hdr.original_sc;
655 msg.hdr.serializing_sc = io;
656 msg.hdr.nexus = io->io_hdr.nexus;
657 msg.hdr.status = io->io_hdr.status;
658 msg.dt.flags = io->io_hdr.flags;
661 * We convert everything into a S/G list here. We can't
662 * pass by reference, only by value between controllers.
663 * So we can't pass a pointer to the S/G list, only as many
664 * S/G entries as we can fit in here. If it's possible for
665 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
666 * then we need to break this up into multiple transfers.
668 if (io->scsiio.kern_sg_entries == 0) {
669 msg.dt.kern_sg_entries = 1;
671 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
672 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
674 /* XXX KDM use busdma here! */
675 msg.dt.sg_list[0].addr =
676 (void *)vtophys(io->scsiio.kern_data_ptr);
679 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
680 ("HA does not support BUS_ADDR"));
681 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
683 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
686 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
690 msg.dt.kern_data_len = io->scsiio.kern_data_len;
691 msg.dt.kern_total_len = io->scsiio.kern_total_len;
692 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
693 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
694 msg.dt.sg_sequence = 0;
697 * Loop until we've sent all of the S/G entries. On the
698 * other end, we'll recompose these S/G entries into one
699 * contiguous list before processing.
701 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
702 msg.dt.sg_sequence++) {
703 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
704 sizeof(msg.dt.sg_list[0])),
705 msg.dt.kern_sg_entries - sg_entries_sent);
706 if (do_sg_copy != 0) {
707 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
708 for (i = sg_entries_sent, j = 0;
709 i < msg.dt.cur_sg_entries; i++, j++) {
711 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
712 msg.dt.sg_list[j].addr = sgl[i].addr;
714 /* XXX KDM use busdma here! */
715 msg.dt.sg_list[j].addr =
716 (void *)vtophys(sgl[i].addr);
719 KASSERT((io->io_hdr.flags &
720 CTL_FLAG_BUS_ADDR) == 0,
721 ("HA does not support BUS_ADDR"));
722 msg.dt.sg_list[j].addr = sgl[i].addr;
724 msg.dt.sg_list[j].len = sgl[i].len;
728 sg_entries_sent += msg.dt.cur_sg_entries;
729 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
730 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
731 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
732 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
733 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
734 io->io_hdr.port_status = 31341;
735 io->scsiio.be_move_done(io);
738 msg.dt.sent_sg_entries = sg_entries_sent;
742 * Officially handover the request from us to peer.
743 * If failover has just happened, then we must return error.
744 * If failover happen just after, then it is not our problem.
747 mtx_lock(&lun->lun_lock);
748 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
750 mtx_unlock(&lun->lun_lock);
751 io->io_hdr.port_status = 31342;
752 io->scsiio.be_move_done(io);
755 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
756 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
758 mtx_unlock(&lun->lun_lock);
762 ctl_ha_done(union ctl_io *io)
764 union ctl_ha_msg msg;
766 if (io->io_hdr.io_type == CTL_IO_SCSI) {
767 memset(&msg, 0, sizeof(msg));
768 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
769 msg.hdr.original_sc = io->io_hdr.original_sc;
770 msg.hdr.nexus = io->io_hdr.nexus;
771 msg.hdr.status = io->io_hdr.status;
772 msg.scsi.scsi_status = io->scsiio.scsi_status;
773 msg.scsi.tag_num = io->scsiio.tag_num;
774 msg.scsi.tag_type = io->scsiio.tag_type;
775 msg.scsi.sense_len = io->scsiio.sense_len;
776 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
777 io->scsiio.sense_len);
778 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
779 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
780 msg.scsi.sense_len, M_WAITOK);
786 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
787 union ctl_ha_msg *msg_info)
789 struct ctl_scsiio *ctsio;
791 if (msg_info->hdr.original_sc == NULL) {
792 printf("%s: original_sc == NULL!\n", __func__);
793 /* XXX KDM now what? */
797 ctsio = &msg_info->hdr.original_sc->scsiio;
798 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
799 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
800 ctsio->io_hdr.status = msg_info->hdr.status;
801 ctsio->scsi_status = msg_info->scsi.scsi_status;
802 ctsio->sense_len = msg_info->scsi.sense_len;
803 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
804 msg_info->scsi.sense_len);
805 ctl_enqueue_isc((union ctl_io *)ctsio);
809 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
810 union ctl_ha_msg *msg_info)
812 struct ctl_scsiio *ctsio;
814 if (msg_info->hdr.serializing_sc == NULL) {
815 printf("%s: serializing_sc == NULL!\n", __func__);
816 /* XXX KDM now what? */
820 ctsio = &msg_info->hdr.serializing_sc->scsiio;
821 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
822 ctl_enqueue_isc((union ctl_io *)ctsio);
826 ctl_isc_announce_lun(struct ctl_lun *lun)
828 struct ctl_softc *softc = lun->ctl_softc;
829 union ctl_ha_msg *msg;
830 struct ctl_ha_msg_lun_pr_key pr_key;
833 if (softc->ha_link != CTL_HA_LINK_ONLINE)
835 mtx_lock(&lun->lun_lock);
836 i = sizeof(msg->lun);
838 i += lun->lun_devid->len;
839 i += sizeof(pr_key) * lun->pr_key_count;
841 mtx_unlock(&lun->lun_lock);
842 msg = malloc(i, M_CTL, M_WAITOK);
843 mtx_lock(&lun->lun_lock);
844 k = sizeof(msg->lun);
846 k += lun->lun_devid->len;
847 k += sizeof(pr_key) * lun->pr_key_count;
853 bzero(&msg->lun, sizeof(msg->lun));
854 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
855 msg->hdr.nexus.targ_lun = lun->lun;
856 msg->hdr.nexus.targ_mapped_lun = lun->lun;
857 msg->lun.flags = lun->flags;
858 msg->lun.pr_generation = lun->pr_generation;
859 msg->lun.pr_res_idx = lun->pr_res_idx;
860 msg->lun.pr_res_type = lun->pr_res_type;
861 msg->lun.pr_key_count = lun->pr_key_count;
863 if (lun->lun_devid) {
864 msg->lun.lun_devid_len = lun->lun_devid->len;
865 memcpy(&msg->lun.data[i], lun->lun_devid->data,
866 msg->lun.lun_devid_len);
867 i += msg->lun.lun_devid_len;
869 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
870 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
873 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
876 mtx_unlock(&lun->lun_lock);
877 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
881 if (lun->flags & CTL_LUN_PRIMARY_SC) {
882 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
883 ctl_isc_announce_mode(lun, -1,
884 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
885 lun->mode_pages.index[i].subpage);
891 ctl_isc_announce_port(struct ctl_port *port)
893 struct ctl_softc *softc = port->ctl_softc;
894 union ctl_ha_msg *msg;
897 if (port->targ_port < softc->port_min ||
898 port->targ_port >= softc->port_max ||
899 softc->ha_link != CTL_HA_LINK_ONLINE)
901 i = sizeof(msg->port) + strlen(port->port_name) + 1;
903 i += port->lun_map_size * sizeof(uint32_t);
904 if (port->port_devid)
905 i += port->port_devid->len;
906 if (port->target_devid)
907 i += port->target_devid->len;
908 if (port->init_devid)
909 i += port->init_devid->len;
910 msg = malloc(i, M_CTL, M_WAITOK);
911 bzero(&msg->port, sizeof(msg->port));
912 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
913 msg->hdr.nexus.targ_port = port->targ_port;
914 msg->port.port_type = port->port_type;
915 msg->port.physical_port = port->physical_port;
916 msg->port.virtual_port = port->virtual_port;
917 msg->port.status = port->status;
919 msg->port.name_len = sprintf(&msg->port.data[i],
920 "%d:%s", softc->ha_id, port->port_name) + 1;
921 i += msg->port.name_len;
923 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
924 memcpy(&msg->port.data[i], port->lun_map,
925 msg->port.lun_map_len);
926 i += msg->port.lun_map_len;
928 if (port->port_devid) {
929 msg->port.port_devid_len = port->port_devid->len;
930 memcpy(&msg->port.data[i], port->port_devid->data,
931 msg->port.port_devid_len);
932 i += msg->port.port_devid_len;
934 if (port->target_devid) {
935 msg->port.target_devid_len = port->target_devid->len;
936 memcpy(&msg->port.data[i], port->target_devid->data,
937 msg->port.target_devid_len);
938 i += msg->port.target_devid_len;
940 if (port->init_devid) {
941 msg->port.init_devid_len = port->init_devid->len;
942 memcpy(&msg->port.data[i], port->init_devid->data,
943 msg->port.init_devid_len);
944 i += msg->port.init_devid_len;
946 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
952 ctl_isc_announce_iid(struct ctl_port *port, int iid)
954 struct ctl_softc *softc = port->ctl_softc;
955 union ctl_ha_msg *msg;
958 if (port->targ_port < softc->port_min ||
959 port->targ_port >= softc->port_max ||
960 softc->ha_link != CTL_HA_LINK_ONLINE)
962 mtx_lock(&softc->ctl_lock);
963 i = sizeof(msg->iid);
965 if (port->wwpn_iid[iid].name)
966 l = strlen(port->wwpn_iid[iid].name) + 1;
968 msg = malloc(i, M_CTL, M_NOWAIT);
970 mtx_unlock(&softc->ctl_lock);
973 bzero(&msg->iid, sizeof(msg->iid));
974 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
975 msg->hdr.nexus.targ_port = port->targ_port;
976 msg->hdr.nexus.initid = iid;
977 msg->iid.in_use = port->wwpn_iid[iid].in_use;
978 msg->iid.name_len = l;
979 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
980 if (port->wwpn_iid[iid].name)
981 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
982 mtx_unlock(&softc->ctl_lock);
983 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
988 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
989 uint8_t page, uint8_t subpage)
991 struct ctl_softc *softc = lun->ctl_softc;
992 union ctl_ha_msg msg;
995 if (softc->ha_link != CTL_HA_LINK_ONLINE)
997 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
998 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
999 page && lun->mode_pages.index[i].subpage == subpage)
1002 if (i == CTL_NUM_MODE_PAGES)
1005 /* Don't try to replicate pages not present on this device. */
1006 if (lun->mode_pages.index[i].page_data == NULL)
1009 bzero(&msg.mode, sizeof(msg.mode));
1010 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
1011 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
1012 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
1013 msg.hdr.nexus.targ_lun = lun->lun;
1014 msg.hdr.nexus.targ_mapped_lun = lun->lun;
1015 msg.mode.page_code = page;
1016 msg.mode.subpage = subpage;
1017 msg.mode.page_len = lun->mode_pages.index[i].page_len;
1018 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
1020 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
1025 ctl_isc_ha_link_up(struct ctl_softc *softc)
1027 struct ctl_port *port;
1028 struct ctl_lun *lun;
1029 union ctl_ha_msg msg;
1032 /* Announce this node parameters to peer for validation. */
1033 msg.login.msg_type = CTL_MSG_LOGIN;
1034 msg.login.version = CTL_HA_VERSION;
1035 msg.login.ha_mode = softc->ha_mode;
1036 msg.login.ha_id = softc->ha_id;
1037 msg.login.max_luns = ctl_max_luns;
1038 msg.login.max_ports = ctl_max_ports;
1039 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1040 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1043 STAILQ_FOREACH(port, &softc->port_list, links) {
1044 ctl_isc_announce_port(port);
1045 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1046 if (port->wwpn_iid[i].in_use)
1047 ctl_isc_announce_iid(port, i);
1050 STAILQ_FOREACH(lun, &softc->lun_list, links)
1051 ctl_isc_announce_lun(lun);
1055 ctl_isc_ha_link_down(struct ctl_softc *softc)
1057 struct ctl_port *port;
1058 struct ctl_lun *lun;
1062 mtx_lock(&softc->ctl_lock);
1063 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1064 mtx_lock(&lun->lun_lock);
1065 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1066 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1067 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1069 mtx_unlock(&lun->lun_lock);
1071 mtx_unlock(&softc->ctl_lock);
1072 io = ctl_alloc_io(softc->othersc_pool);
1073 mtx_lock(&softc->ctl_lock);
1075 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1076 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1077 ctl_enqueue_isc(io);
1080 STAILQ_FOREACH(port, &softc->port_list, links) {
1081 if (port->targ_port >= softc->port_min &&
1082 port->targ_port < softc->port_max)
1084 port->status &= ~CTL_PORT_STATUS_ONLINE;
1085 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1086 port->wwpn_iid[i].in_use = 0;
1087 free(port->wwpn_iid[i].name, M_CTL);
1088 port->wwpn_iid[i].name = NULL;
1091 mtx_unlock(&softc->ctl_lock);
1095 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1097 struct ctl_lun *lun;
1098 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1100 mtx_lock(&softc->ctl_lock);
1101 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns ||
1102 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1103 mtx_unlock(&softc->ctl_lock);
1106 mtx_lock(&lun->lun_lock);
1107 mtx_unlock(&softc->ctl_lock);
1108 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1109 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1110 if (msg->ua.ua_all) {
1112 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1114 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1117 ctl_est_ua(lun, iid, msg->ua.ua_type);
1119 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1121 mtx_unlock(&lun->lun_lock);
1125 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1127 struct ctl_lun *lun;
1128 struct ctl_ha_msg_lun_pr_key pr_key;
1130 ctl_lun_flags oflags;
1133 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1134 mtx_lock(&softc->ctl_lock);
1135 if (targ_lun >= ctl_max_luns ||
1136 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1137 mtx_unlock(&softc->ctl_lock);
1140 mtx_lock(&lun->lun_lock);
1141 mtx_unlock(&softc->ctl_lock);
1142 if (lun->flags & CTL_LUN_DISABLED) {
1143 mtx_unlock(&lun->lun_lock);
1146 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1147 if (msg->lun.lun_devid_len != i || (i > 0 &&
1148 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1149 mtx_unlock(&lun->lun_lock);
1150 printf("%s: Received conflicting HA LUN %d\n",
1151 __func__, targ_lun);
1154 /* Record whether peer is primary. */
1155 oflags = lun->flags;
1156 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1157 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1158 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1160 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1161 if (oflags != lun->flags)
1162 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1164 /* If peer is primary and we are not -- use data */
1165 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1166 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1167 lun->pr_generation = msg->lun.pr_generation;
1168 lun->pr_res_idx = msg->lun.pr_res_idx;
1169 lun->pr_res_type = msg->lun.pr_res_type;
1170 lun->pr_key_count = msg->lun.pr_key_count;
1171 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1172 ctl_clr_prkey(lun, k);
1173 for (k = 0; k < msg->lun.pr_key_count; k++) {
1174 memcpy(&pr_key, &msg->lun.data[i],
1176 ctl_alloc_prkey(lun, pr_key.pr_iid);
1177 ctl_set_prkey(lun, pr_key.pr_iid,
1179 i += sizeof(pr_key);
1183 mtx_unlock(&lun->lun_lock);
1184 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1186 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1187 "primary" : "secondary"));
1189 /* If we are primary but peer doesn't know -- notify */
1190 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1191 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1192 ctl_isc_announce_lun(lun);
1197 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1199 struct ctl_port *port;
1200 struct ctl_lun *lun;
1203 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1205 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1206 msg->hdr.nexus.targ_port));
1208 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1209 port->frontend = &ha_frontend;
1210 port->targ_port = msg->hdr.nexus.targ_port;
1211 port->fe_datamove = ctl_ha_datamove;
1212 port->fe_done = ctl_ha_done;
1213 } else if (port->frontend == &ha_frontend) {
1214 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1215 msg->hdr.nexus.targ_port));
1218 printf("%s: Received conflicting HA port %d\n",
1219 __func__, msg->hdr.nexus.targ_port);
1222 port->port_type = msg->port.port_type;
1223 port->physical_port = msg->port.physical_port;
1224 port->virtual_port = msg->port.virtual_port;
1225 port->status = msg->port.status;
1227 free(port->port_name, M_CTL);
1228 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1230 i += msg->port.name_len;
1231 if (msg->port.lun_map_len != 0) {
1232 if (port->lun_map == NULL ||
1233 port->lun_map_size * sizeof(uint32_t) <
1234 msg->port.lun_map_len) {
1235 port->lun_map_size = 0;
1236 free(port->lun_map, M_CTL);
1237 port->lun_map = malloc(msg->port.lun_map_len,
1240 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1241 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1242 i += msg->port.lun_map_len;
1244 port->lun_map_size = 0;
1245 free(port->lun_map, M_CTL);
1246 port->lun_map = NULL;
1248 if (msg->port.port_devid_len != 0) {
1249 if (port->port_devid == NULL ||
1250 port->port_devid->len < msg->port.port_devid_len) {
1251 free(port->port_devid, M_CTL);
1252 port->port_devid = malloc(sizeof(struct ctl_devid) +
1253 msg->port.port_devid_len, M_CTL, M_WAITOK);
1255 memcpy(port->port_devid->data, &msg->port.data[i],
1256 msg->port.port_devid_len);
1257 port->port_devid->len = msg->port.port_devid_len;
1258 i += msg->port.port_devid_len;
1260 free(port->port_devid, M_CTL);
1261 port->port_devid = NULL;
1263 if (msg->port.target_devid_len != 0) {
1264 if (port->target_devid == NULL ||
1265 port->target_devid->len < msg->port.target_devid_len) {
1266 free(port->target_devid, M_CTL);
1267 port->target_devid = malloc(sizeof(struct ctl_devid) +
1268 msg->port.target_devid_len, M_CTL, M_WAITOK);
1270 memcpy(port->target_devid->data, &msg->port.data[i],
1271 msg->port.target_devid_len);
1272 port->target_devid->len = msg->port.target_devid_len;
1273 i += msg->port.target_devid_len;
1275 free(port->target_devid, M_CTL);
1276 port->target_devid = NULL;
1278 if (msg->port.init_devid_len != 0) {
1279 if (port->init_devid == NULL ||
1280 port->init_devid->len < msg->port.init_devid_len) {
1281 free(port->init_devid, M_CTL);
1282 port->init_devid = malloc(sizeof(struct ctl_devid) +
1283 msg->port.init_devid_len, M_CTL, M_WAITOK);
1285 memcpy(port->init_devid->data, &msg->port.data[i],
1286 msg->port.init_devid_len);
1287 port->init_devid->len = msg->port.init_devid_len;
1288 i += msg->port.init_devid_len;
1290 free(port->init_devid, M_CTL);
1291 port->init_devid = NULL;
1294 if (ctl_port_register(port) != 0) {
1295 printf("%s: ctl_port_register() failed with error\n",
1299 mtx_lock(&softc->ctl_lock);
1300 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1301 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1303 mtx_lock(&lun->lun_lock);
1304 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1305 mtx_unlock(&lun->lun_lock);
1307 mtx_unlock(&softc->ctl_lock);
1311 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1313 struct ctl_port *port;
1316 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1318 printf("%s: Received IID for unknown port %d\n",
1319 __func__, msg->hdr.nexus.targ_port);
1322 iid = msg->hdr.nexus.initid;
1323 if (port->wwpn_iid[iid].in_use != 0 &&
1324 msg->iid.in_use == 0)
1325 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1326 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1327 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1328 free(port->wwpn_iid[iid].name, M_CTL);
1329 if (msg->iid.name_len) {
1330 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1331 msg->iid.name_len, M_CTL);
1333 port->wwpn_iid[iid].name = NULL;
1337 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1340 if (msg->login.version != CTL_HA_VERSION) {
1341 printf("CTL HA peers have different versions %d != %d\n",
1342 msg->login.version, CTL_HA_VERSION);
1343 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1346 if (msg->login.ha_mode != softc->ha_mode) {
1347 printf("CTL HA peers have different ha_mode %d != %d\n",
1348 msg->login.ha_mode, softc->ha_mode);
1349 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1352 if (msg->login.ha_id == softc->ha_id) {
1353 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1354 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1357 if (msg->login.max_luns != ctl_max_luns ||
1358 msg->login.max_ports != ctl_max_ports ||
1359 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1360 printf("CTL HA peers have different limits\n");
1361 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1367 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1369 struct ctl_lun *lun;
1371 uint32_t initidx, targ_lun;
1373 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1374 mtx_lock(&softc->ctl_lock);
1375 if (targ_lun >= ctl_max_luns ||
1376 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1377 mtx_unlock(&softc->ctl_lock);
1380 mtx_lock(&lun->lun_lock);
1381 mtx_unlock(&softc->ctl_lock);
1382 if (lun->flags & CTL_LUN_DISABLED) {
1383 mtx_unlock(&lun->lun_lock);
1386 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1387 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1388 msg->mode.page_code &&
1389 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1392 if (i == CTL_NUM_MODE_PAGES) {
1393 mtx_unlock(&lun->lun_lock);
1396 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1397 lun->mode_pages.index[i].page_len);
1398 initidx = ctl_get_initindex(&msg->hdr.nexus);
1400 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1401 mtx_unlock(&lun->lun_lock);
1405 * ISC (Inter Shelf Communication) event handler. Events from the HA
1406 * subsystem come in here.
1409 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1411 struct ctl_softc *softc = control_softc;
1413 struct ctl_prio *presio;
1414 ctl_ha_status isc_status;
1416 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1417 if (event == CTL_HA_EVT_MSG_RECV) {
1418 union ctl_ha_msg *msg, msgbuf;
1420 if (param > sizeof(msgbuf))
1421 msg = malloc(param, M_CTL, M_WAITOK);
1424 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1426 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1427 printf("%s: Error receiving message: %d\n",
1428 __func__, isc_status);
1434 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1435 switch (msg->hdr.msg_type) {
1436 case CTL_MSG_SERIALIZE:
1437 io = ctl_alloc_io(softc->othersc_pool);
1439 // populate ctsio from msg
1440 io->io_hdr.io_type = CTL_IO_SCSI;
1441 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1442 io->io_hdr.original_sc = msg->hdr.original_sc;
1443 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1446 * If we're in serialization-only mode, we don't
1447 * want to go through full done processing. Thus
1450 * XXX KDM add another flag that is more specific.
1452 if (softc->ha_mode != CTL_HA_MODE_XFER)
1453 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1454 io->io_hdr.nexus = msg->hdr.nexus;
1456 printf("port %u, iid %u, lun %u\n",
1457 io->io_hdr.nexus.targ_port,
1458 io->io_hdr.nexus.initid,
1459 io->io_hdr.nexus.targ_lun);
1461 io->scsiio.tag_num = msg->scsi.tag_num;
1462 io->scsiio.tag_type = msg->scsi.tag_type;
1464 io->io_hdr.start_time = time_uptime;
1465 getbinuptime(&io->io_hdr.start_bt);
1466 #endif /* CTL_TIME_IO */
1467 io->scsiio.cdb_len = msg->scsi.cdb_len;
1468 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1470 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1471 const struct ctl_cmd_entry *entry;
1473 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1474 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1476 entry->flags & CTL_FLAG_DATA_MASK;
1478 ctl_enqueue_isc(io);
1481 /* Performed on the Originating SC, XFER mode only */
1482 case CTL_MSG_DATAMOVE: {
1483 struct ctl_sg_entry *sgl;
1486 io = msg->hdr.original_sc;
1488 printf("%s: original_sc == NULL!\n", __func__);
1489 /* XXX KDM do something here */
1492 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1493 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1495 * Keep track of this, we need to send it back over
1496 * when the datamove is complete.
1498 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1499 if (msg->hdr.status == CTL_SUCCESS)
1500 io->io_hdr.status = msg->hdr.status;
1502 if (msg->dt.sg_sequence == 0) {
1504 getbinuptime(&io->io_hdr.dma_start_bt);
1506 i = msg->dt.kern_sg_entries +
1507 msg->dt.kern_data_len /
1508 CTL_HA_DATAMOVE_SEGMENT + 1;
1509 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1511 io->io_hdr.remote_sglist = sgl;
1512 io->io_hdr.local_sglist =
1513 &sgl[msg->dt.kern_sg_entries];
1515 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1517 io->scsiio.kern_sg_entries =
1518 msg->dt.kern_sg_entries;
1519 io->scsiio.rem_sg_entries =
1520 msg->dt.kern_sg_entries;
1521 io->scsiio.kern_data_len =
1522 msg->dt.kern_data_len;
1523 io->scsiio.kern_total_len =
1524 msg->dt.kern_total_len;
1525 io->scsiio.kern_data_resid =
1526 msg->dt.kern_data_resid;
1527 io->scsiio.kern_rel_offset =
1528 msg->dt.kern_rel_offset;
1529 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1530 io->io_hdr.flags |= msg->dt.flags &
1533 sgl = (struct ctl_sg_entry *)
1534 io->scsiio.kern_data_ptr;
1536 for (i = msg->dt.sent_sg_entries, j = 0;
1537 i < (msg->dt.sent_sg_entries +
1538 msg->dt.cur_sg_entries); i++, j++) {
1539 sgl[i].addr = msg->dt.sg_list[j].addr;
1540 sgl[i].len = msg->dt.sg_list[j].len;
1543 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1544 __func__, sgl[i].addr, sgl[i].len, j, i);
1549 * If this is the last piece of the I/O, we've got
1550 * the full S/G list. Queue processing in the thread.
1551 * Otherwise wait for the next piece.
1553 if (msg->dt.sg_last != 0)
1554 ctl_enqueue_isc(io);
1557 /* Performed on the Serializing (primary) SC, XFER mode only */
1558 case CTL_MSG_DATAMOVE_DONE: {
1559 if (msg->hdr.serializing_sc == NULL) {
1560 printf("%s: serializing_sc == NULL!\n",
1562 /* XXX KDM now what? */
1566 * We grab the sense information here in case
1567 * there was a failure, so we can return status
1568 * back to the initiator.
1570 io = msg->hdr.serializing_sc;
1571 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1572 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1573 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1574 io->io_hdr.port_status = msg->scsi.port_status;
1575 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1576 if (msg->hdr.status != CTL_STATUS_NONE) {
1577 io->io_hdr.status = msg->hdr.status;
1578 io->scsiio.scsi_status = msg->scsi.scsi_status;
1579 io->scsiio.sense_len = msg->scsi.sense_len;
1580 memcpy(&io->scsiio.sense_data,
1581 &msg->scsi.sense_data,
1582 msg->scsi.sense_len);
1583 if (msg->hdr.status == CTL_SUCCESS)
1584 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1586 ctl_enqueue_isc(io);
1590 /* Preformed on Originating SC, SER_ONLY mode */
1592 io = msg->hdr.original_sc;
1594 printf("%s: original_sc == NULL!\n",
1598 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1599 io->io_hdr.msg_type = CTL_MSG_R2R;
1600 io->io_hdr.serializing_sc = msg->hdr.serializing_sc;
1601 ctl_enqueue_isc(io);
1605 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1607 * Performed on the Originating (i.e. secondary) SC in XFER
1610 case CTL_MSG_FINISH_IO:
1611 if (softc->ha_mode == CTL_HA_MODE_XFER)
1612 ctl_isc_handler_finish_xfer(softc, msg);
1614 ctl_isc_handler_finish_ser_only(softc, msg);
1617 /* Preformed on Originating SC */
1618 case CTL_MSG_BAD_JUJU:
1619 io = msg->hdr.original_sc;
1621 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1625 ctl_copy_sense_data(msg, io);
1627 * IO should have already been cleaned up on other
1628 * SC so clear this flag so we won't send a message
1629 * back to finish the IO there.
1631 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1632 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1634 /* io = msg->hdr.serializing_sc; */
1635 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1636 ctl_enqueue_isc(io);
1639 /* Handle resets sent from the other side */
1640 case CTL_MSG_MANAGE_TASKS: {
1641 struct ctl_taskio *taskio;
1642 taskio = (struct ctl_taskio *)ctl_alloc_io(
1643 softc->othersc_pool);
1644 ctl_zero_io((union ctl_io *)taskio);
1645 taskio->io_hdr.io_type = CTL_IO_TASK;
1646 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1647 taskio->io_hdr.nexus = msg->hdr.nexus;
1648 taskio->task_action = msg->task.task_action;
1649 taskio->tag_num = msg->task.tag_num;
1650 taskio->tag_type = msg->task.tag_type;
1652 taskio->io_hdr.start_time = time_uptime;
1653 getbinuptime(&taskio->io_hdr.start_bt);
1654 #endif /* CTL_TIME_IO */
1655 ctl_run_task((union ctl_io *)taskio);
1658 /* Persistent Reserve action which needs attention */
1659 case CTL_MSG_PERS_ACTION:
1660 presio = (struct ctl_prio *)ctl_alloc_io(
1661 softc->othersc_pool);
1662 ctl_zero_io((union ctl_io *)presio);
1663 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1664 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1665 presio->io_hdr.nexus = msg->hdr.nexus;
1666 presio->pr_msg = msg->pr;
1667 ctl_enqueue_isc((union ctl_io *)presio);
1670 ctl_isc_ua(softc, msg, param);
1672 case CTL_MSG_PORT_SYNC:
1673 ctl_isc_port_sync(softc, msg, param);
1675 case CTL_MSG_LUN_SYNC:
1676 ctl_isc_lun_sync(softc, msg, param);
1678 case CTL_MSG_IID_SYNC:
1679 ctl_isc_iid_sync(softc, msg, param);
1682 ctl_isc_login(softc, msg, param);
1684 case CTL_MSG_MODE_SYNC:
1685 ctl_isc_mode_sync(softc, msg, param);
1688 printf("Received HA message of unknown type %d\n",
1690 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1695 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1696 printf("CTL: HA link status changed from %d to %d\n",
1697 softc->ha_link, param);
1698 if (param == softc->ha_link)
1700 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1701 softc->ha_link = param;
1702 ctl_isc_ha_link_down(softc);
1704 softc->ha_link = param;
1705 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1706 ctl_isc_ha_link_up(softc);
1710 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1716 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1719 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1720 src->scsi.sense_len);
1721 dest->scsiio.scsi_status = src->scsi.scsi_status;
1722 dest->scsiio.sense_len = src->scsi.sense_len;
1723 dest->io_hdr.status = src->hdr.status;
1727 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1730 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1731 src->scsiio.sense_len);
1732 dest->scsi.scsi_status = src->scsiio.scsi_status;
1733 dest->scsi.sense_len = src->scsiio.sense_len;
1734 dest->hdr.status = src->io_hdr.status;
1738 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1740 struct ctl_softc *softc = lun->ctl_softc;
1743 if (initidx < softc->init_min || initidx >= softc->init_max)
1745 mtx_assert(&lun->lun_lock, MA_OWNED);
1746 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1749 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1753 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1757 mtx_assert(&lun->lun_lock, MA_OWNED);
1758 if (lun->pending_ua[port] == NULL)
1760 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1761 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1763 lun->pending_ua[port][i] |= ua;
1768 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1770 struct ctl_softc *softc = lun->ctl_softc;
1773 mtx_assert(&lun->lun_lock, MA_OWNED);
1774 for (i = softc->port_min; i < softc->port_max; i++)
1775 ctl_est_ua_port(lun, i, except, ua);
1779 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1781 struct ctl_softc *softc = lun->ctl_softc;
1784 if (initidx < softc->init_min || initidx >= softc->init_max)
1786 mtx_assert(&lun->lun_lock, MA_OWNED);
1787 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1790 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1794 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1796 struct ctl_softc *softc = lun->ctl_softc;
1799 mtx_assert(&lun->lun_lock, MA_OWNED);
1800 for (i = softc->port_min; i < softc->port_max; i++) {
1801 if (lun->pending_ua[i] == NULL)
1803 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1804 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1806 lun->pending_ua[i][j] &= ~ua;
1812 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1813 ctl_ua_type ua_type)
1815 struct ctl_lun *lun;
1817 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1818 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1819 mtx_lock(&lun->lun_lock);
1820 ctl_clr_ua(lun, initidx, ua_type);
1821 mtx_unlock(&lun->lun_lock);
1826 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1828 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1829 struct ctl_lun *lun;
1830 struct ctl_lun_req ireq;
1833 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1834 error = sysctl_handle_int(oidp, &value, 0, req);
1835 if ((error != 0) || (req->newptr == NULL))
1838 mtx_lock(&softc->ctl_lock);
1840 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1842 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1843 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1844 mtx_unlock(&softc->ctl_lock);
1845 bzero(&ireq, sizeof(ireq));
1846 ireq.reqtype = CTL_LUNREQ_MODIFY;
1847 ireq.reqdata.modify.lun_id = lun->lun;
1848 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1850 if (ireq.status != CTL_LUN_OK) {
1851 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1852 __func__, ireq.status, ireq.error_str);
1854 mtx_lock(&softc->ctl_lock);
1856 mtx_unlock(&softc->ctl_lock);
1863 struct make_dev_args args;
1864 struct ctl_softc *softc;
1867 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1870 make_dev_args_init(&args);
1871 args.mda_devsw = &ctl_cdevsw;
1872 args.mda_uid = UID_ROOT;
1873 args.mda_gid = GID_OPERATOR;
1874 args.mda_mode = 0600;
1875 args.mda_si_drv1 = softc;
1876 args.mda_si_drv2 = NULL;
1877 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1879 free(softc, M_DEVBUF);
1880 control_softc = NULL;
1884 sysctl_ctx_init(&softc->sysctl_ctx);
1885 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1886 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1887 CTLFLAG_RD, 0, "CAM Target Layer");
1889 if (softc->sysctl_tree == NULL) {
1890 printf("%s: unable to allocate sysctl tree\n", __func__);
1891 destroy_dev(softc->dev);
1892 free(softc, M_DEVBUF);
1893 control_softc = NULL;
1897 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1898 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1899 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1902 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1903 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1904 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1906 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) {
1907 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n",
1908 ctl_max_luns, CTL_DEFAULT_MAX_LUNS);
1909 ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
1911 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns,
1912 M_DEVBUF, M_WAITOK | M_ZERO);
1913 softc->ctl_lun_mask = malloc(sizeof(uint32_t) *
1914 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1915 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) {
1916 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n",
1917 ctl_max_ports, CTL_DEFAULT_MAX_PORTS);
1918 ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
1920 softc->ctl_port_mask = malloc(sizeof(uint32_t) *
1921 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1922 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports,
1923 M_DEVBUF, M_WAITOK | M_ZERO);
1927 * In Copan's HA scheme, the "master" and "slave" roles are
1928 * figured out through the slot the controller is in. Although it
1929 * is an active/active system, someone has to be in charge.
1931 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1932 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1933 "HA head ID (0 - no HA)");
1934 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1935 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1936 softc->is_single = 1;
1937 softc->port_cnt = ctl_max_ports;
1938 softc->port_min = 0;
1940 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES;
1941 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1943 softc->port_max = softc->port_min + softc->port_cnt;
1944 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1945 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1947 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1948 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1949 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1951 STAILQ_INIT(&softc->lun_list);
1952 STAILQ_INIT(&softc->pending_lun_queue);
1953 STAILQ_INIT(&softc->fe_list);
1954 STAILQ_INIT(&softc->port_list);
1955 STAILQ_INIT(&softc->be_list);
1956 ctl_tpc_init(softc);
1958 if (worker_threads <= 0)
1959 worker_threads = max(1, mp_ncpus / 4);
1960 if (worker_threads > CTL_MAX_THREADS)
1961 worker_threads = CTL_MAX_THREADS;
1963 for (i = 0; i < worker_threads; i++) {
1964 struct ctl_thread *thr = &softc->threads[i];
1966 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1967 thr->ctl_softc = softc;
1968 STAILQ_INIT(&thr->incoming_queue);
1969 STAILQ_INIT(&thr->rtr_queue);
1970 STAILQ_INIT(&thr->done_queue);
1971 STAILQ_INIT(&thr->isc_queue);
1973 error = kproc_kthread_add(ctl_work_thread, thr,
1974 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1976 printf("error creating CTL work thread!\n");
1980 error = kproc_kthread_add(ctl_lun_thread, softc,
1981 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun");
1983 printf("error creating CTL lun thread!\n");
1986 error = kproc_kthread_add(ctl_thresh_thread, softc,
1987 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
1989 printf("error creating CTL threshold thread!\n");
1993 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1994 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1995 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1997 if (softc->is_single == 0) {
1998 if (ctl_frontend_register(&ha_frontend) != 0)
1999 softc->is_single = 1;
2007 struct ctl_softc *softc = control_softc;
2010 if (softc->is_single == 0)
2011 ctl_frontend_deregister(&ha_frontend);
2013 destroy_dev(softc->dev);
2015 /* Shutdown CTL threads. */
2016 softc->shutdown = 1;
2017 for (i = 0; i < worker_threads; i++) {
2018 struct ctl_thread *thr = &softc->threads[i];
2019 while (thr->thread != NULL) {
2021 if (thr->thread != NULL)
2022 pause("CTL thr shutdown", 1);
2024 mtx_destroy(&thr->queue_lock);
2026 while (softc->lun_thread != NULL) {
2027 wakeup(&softc->pending_lun_queue);
2028 if (softc->lun_thread != NULL)
2029 pause("CTL thr shutdown", 1);
2031 while (softc->thresh_thread != NULL) {
2032 wakeup(softc->thresh_thread);
2033 if (softc->thresh_thread != NULL)
2034 pause("CTL thr shutdown", 1);
2037 ctl_tpc_shutdown(softc);
2038 uma_zdestroy(softc->io_zone);
2039 mtx_destroy(&softc->ctl_lock);
2041 free(softc->ctl_luns, M_DEVBUF);
2042 free(softc->ctl_lun_mask, M_DEVBUF);
2043 free(softc->ctl_port_mask, M_DEVBUF);
2044 free(softc->ctl_ports, M_DEVBUF);
2046 sysctl_ctx_free(&softc->sysctl_ctx);
2048 free(softc, M_DEVBUF);
2049 control_softc = NULL;
2054 ctl_module_event_handler(module_t mod, int what, void *arg)
2059 return (ctl_init());
2061 return (ctl_shutdown());
2063 return (EOPNOTSUPP);
2068 * XXX KDM should we do some access checks here? Bump a reference count to
2069 * prevent a CTL module from being unloaded while someone has it open?
2072 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2078 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2084 * Remove an initiator by port number and initiator ID.
2085 * Returns 0 for success, -1 for failure.
2088 ctl_remove_initiator(struct ctl_port *port, int iid)
2090 struct ctl_softc *softc = port->ctl_softc;
2093 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2095 if (iid > CTL_MAX_INIT_PER_PORT) {
2096 printf("%s: initiator ID %u > maximun %u!\n",
2097 __func__, iid, CTL_MAX_INIT_PER_PORT);
2101 mtx_lock(&softc->ctl_lock);
2102 last = (--port->wwpn_iid[iid].in_use == 0);
2103 port->wwpn_iid[iid].last_use = time_uptime;
2104 mtx_unlock(&softc->ctl_lock);
2106 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2107 ctl_isc_announce_iid(port, iid);
2113 * Add an initiator to the initiator map.
2114 * Returns iid for success, < 0 for failure.
2117 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2119 struct ctl_softc *softc = port->ctl_softc;
2123 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2125 if (iid >= CTL_MAX_INIT_PER_PORT) {
2126 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2127 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2132 mtx_lock(&softc->ctl_lock);
2134 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2135 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2136 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2140 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2141 strcmp(name, port->wwpn_iid[i].name) == 0) {
2149 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2150 if (port->wwpn_iid[i].in_use == 0 &&
2151 port->wwpn_iid[i].wwpn == 0 &&
2152 port->wwpn_iid[i].name == NULL) {
2161 best_time = INT32_MAX;
2162 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2163 if (port->wwpn_iid[i].in_use == 0) {
2164 if (port->wwpn_iid[i].last_use < best_time) {
2166 best_time = port->wwpn_iid[i].last_use;
2174 mtx_unlock(&softc->ctl_lock);
2179 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2181 * This is not an error yet.
2183 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2185 printf("%s: port %d iid %u WWPN %#jx arrived"
2186 " again\n", __func__, port->targ_port,
2187 iid, (uintmax_t)wwpn);
2191 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2192 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2194 printf("%s: port %d iid %u name '%s' arrived"
2195 " again\n", __func__, port->targ_port,
2202 * This is an error, but what do we do about it? The
2203 * driver is telling us we have a new WWPN for this
2204 * initiator ID, so we pretty much need to use it.
2206 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2207 " but WWPN %#jx '%s' is still at that address\n",
2208 __func__, port->targ_port, iid, wwpn, name,
2209 (uintmax_t)port->wwpn_iid[iid].wwpn,
2210 port->wwpn_iid[iid].name);
2213 free(port->wwpn_iid[iid].name, M_CTL);
2214 port->wwpn_iid[iid].name = name;
2215 port->wwpn_iid[iid].wwpn = wwpn;
2216 port->wwpn_iid[iid].in_use++;
2217 mtx_unlock(&softc->ctl_lock);
2218 ctl_isc_announce_iid(port, iid);
2224 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2228 switch (port->port_type) {
2231 struct scsi_transportid_fcp *id =
2232 (struct scsi_transportid_fcp *)buf;
2233 if (port->wwpn_iid[iid].wwpn == 0)
2235 memset(id, 0, sizeof(*id));
2236 id->format_protocol = SCSI_PROTO_FC;
2237 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2238 return (sizeof(*id));
2240 case CTL_PORT_ISCSI:
2242 struct scsi_transportid_iscsi_port *id =
2243 (struct scsi_transportid_iscsi_port *)buf;
2244 if (port->wwpn_iid[iid].name == NULL)
2247 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2249 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2250 len = roundup2(min(len, 252), 4);
2251 scsi_ulto2b(len, id->additional_length);
2252 return (sizeof(*id) + len);
2256 struct scsi_transportid_sas *id =
2257 (struct scsi_transportid_sas *)buf;
2258 if (port->wwpn_iid[iid].wwpn == 0)
2260 memset(id, 0, sizeof(*id));
2261 id->format_protocol = SCSI_PROTO_SAS;
2262 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2263 return (sizeof(*id));
2267 struct scsi_transportid_spi *id =
2268 (struct scsi_transportid_spi *)buf;
2269 memset(id, 0, sizeof(*id));
2270 id->format_protocol = SCSI_PROTO_SPI;
2271 scsi_ulto2b(iid, id->scsi_addr);
2272 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2273 return (sizeof(*id));
2279 * Serialize a command that went down the "wrong" side, and so was sent to
2280 * this controller for execution. The logic is a little different than the
2281 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2282 * sent back to the other side, but in the success case, we execute the
2283 * command on this side (XFER mode) or tell the other side to execute it
2287 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2289 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2290 struct ctl_port *port = CTL_PORT(ctsio);
2291 union ctl_ha_msg msg_info;
2292 struct ctl_lun *lun;
2293 const struct ctl_cmd_entry *entry;
2296 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2298 /* Make sure that we know about this port. */
2299 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2300 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2305 /* Make sure that we know about this LUN. */
2306 mtx_lock(&softc->ctl_lock);
2307 if (targ_lun >= ctl_max_luns ||
2308 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2309 mtx_unlock(&softc->ctl_lock);
2312 * The other node would not send this request to us unless
2313 * received announce that we are primary node for this LUN.
2314 * If this LUN does not exist now, it is probably result of
2315 * a race, so respond to initiator in the most opaque way.
2317 ctl_set_busy(ctsio);
2320 mtx_lock(&lun->lun_lock);
2321 mtx_unlock(&softc->ctl_lock);
2324 * If the LUN is invalid, pretend that it doesn't exist.
2325 * It will go away as soon as all pending I/Os completed.
2327 if (lun->flags & CTL_LUN_DISABLED) {
2328 mtx_unlock(&lun->lun_lock);
2329 ctl_set_busy(ctsio);
2333 entry = ctl_get_cmd_entry(ctsio, NULL);
2334 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2335 mtx_unlock(&lun->lun_lock);
2339 CTL_LUN(ctsio) = lun;
2340 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2343 * Every I/O goes into the OOA queue for a
2344 * particular LUN, and stays there until completion.
2347 if (TAILQ_EMPTY(&lun->ooa_queue))
2348 lun->idle_time += getsbinuptime() - lun->last_busy;
2350 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2352 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2353 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2355 case CTL_ACTION_BLOCK:
2356 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2357 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2359 mtx_unlock(&lun->lun_lock);
2361 case CTL_ACTION_PASS:
2362 case CTL_ACTION_SKIP:
2363 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2364 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2365 ctl_enqueue_rtr((union ctl_io *)ctsio);
2366 mtx_unlock(&lun->lun_lock);
2368 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2369 mtx_unlock(&lun->lun_lock);
2371 /* send msg back to other side */
2372 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2373 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2374 msg_info.hdr.msg_type = CTL_MSG_R2R;
2375 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2376 sizeof(msg_info.hdr), M_WAITOK);
2379 case CTL_ACTION_OVERLAP:
2380 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2381 mtx_unlock(&lun->lun_lock);
2382 ctl_set_overlapped_cmd(ctsio);
2384 case CTL_ACTION_OVERLAP_TAG:
2385 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2386 mtx_unlock(&lun->lun_lock);
2387 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2389 case CTL_ACTION_ERROR:
2391 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2392 mtx_unlock(&lun->lun_lock);
2394 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2397 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2398 msg_info.hdr.original_sc = ctsio->io_hdr.original_sc;
2399 msg_info.hdr.serializing_sc = NULL;
2400 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2401 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2402 sizeof(msg_info.scsi), M_WAITOK);
2403 ctl_free_io((union ctl_io *)ctsio);
2409 * Returns 0 for success, errno for failure.
2412 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2413 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2417 mtx_lock(&lun->lun_lock);
2418 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2419 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2421 struct ctl_ooa_entry *entry;
2424 * If we've got more than we can fit, just count the
2425 * remaining entries.
2427 if (*cur_fill_num >= ooa_hdr->alloc_num)
2430 entry = &kern_entries[*cur_fill_num];
2432 entry->tag_num = io->scsiio.tag_num;
2433 entry->lun_num = lun->lun;
2435 entry->start_bt = io->io_hdr.start_bt;
2437 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2438 entry->cdb_len = io->scsiio.cdb_len;
2439 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2440 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2442 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2443 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2445 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2446 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2448 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2449 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2451 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2452 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2454 mtx_unlock(&lun->lun_lock);
2458 * Escape characters that are illegal or not recommended in XML.
2461 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2463 char *end = str + size;
2468 for (; *str && str < end; str++) {
2471 retval = sbuf_printf(sb, "&");
2474 retval = sbuf_printf(sb, ">");
2477 retval = sbuf_printf(sb, "<");
2480 retval = sbuf_putc(sb, *str);
2493 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2495 struct scsi_vpd_id_descriptor *desc;
2498 if (id == NULL || id->len < 4)
2500 desc = (struct scsi_vpd_id_descriptor *)id->data;
2501 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2502 case SVPD_ID_TYPE_T10:
2503 sbuf_printf(sb, "t10.");
2505 case SVPD_ID_TYPE_EUI64:
2506 sbuf_printf(sb, "eui.");
2508 case SVPD_ID_TYPE_NAA:
2509 sbuf_printf(sb, "naa.");
2511 case SVPD_ID_TYPE_SCSI_NAME:
2514 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2515 case SVPD_ID_CODESET_BINARY:
2516 for (i = 0; i < desc->length; i++)
2517 sbuf_printf(sb, "%02x", desc->identifier[i]);
2519 case SVPD_ID_CODESET_ASCII:
2520 sbuf_printf(sb, "%.*s", (int)desc->length,
2521 (char *)desc->identifier);
2523 case SVPD_ID_CODESET_UTF8:
2524 sbuf_printf(sb, "%s", (char *)desc->identifier);
2530 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2533 struct ctl_softc *softc = dev->si_drv1;
2534 struct ctl_port *port;
2535 struct ctl_lun *lun;
2542 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2544 case CTL_ENABLE_PORT:
2545 case CTL_DISABLE_PORT:
2546 case CTL_SET_PORT_WWNS: {
2547 struct ctl_port *port;
2548 struct ctl_port_entry *entry;
2550 entry = (struct ctl_port_entry *)addr;
2552 mtx_lock(&softc->ctl_lock);
2553 STAILQ_FOREACH(port, &softc->port_list, links) {
2556 if (port->targ_port < softc->port_min ||
2557 port->targ_port >= softc->port_max)
2562 if ((entry->port_type == CTL_PORT_NONE)
2563 && (entry->targ_port == port->targ_port)) {
2565 * If the user only wants to enable or
2566 * disable or set WWNs on a specific port,
2567 * do the operation and we're done.
2571 } else if (entry->port_type & port->port_type) {
2573 * Compare the user's type mask with the
2574 * particular frontend type to see if we
2581 * Make sure the user isn't trying to set
2582 * WWNs on multiple ports at the same time.
2584 if (cmd == CTL_SET_PORT_WWNS) {
2585 printf("%s: Can't set WWNs on "
2586 "multiple ports\n", __func__);
2595 * XXX KDM we have to drop the lock here, because
2596 * the online/offline operations can potentially
2597 * block. We need to reference count the frontends
2598 * so they can't go away,
2600 if (cmd == CTL_ENABLE_PORT) {
2601 mtx_unlock(&softc->ctl_lock);
2602 ctl_port_online(port);
2603 mtx_lock(&softc->ctl_lock);
2604 } else if (cmd == CTL_DISABLE_PORT) {
2605 mtx_unlock(&softc->ctl_lock);
2606 ctl_port_offline(port);
2607 mtx_lock(&softc->ctl_lock);
2608 } else if (cmd == CTL_SET_PORT_WWNS) {
2609 ctl_port_set_wwns(port,
2610 (entry->flags & CTL_PORT_WWNN_VALID) ?
2612 (entry->flags & CTL_PORT_WWPN_VALID) ?
2613 1 : 0, entry->wwpn);
2618 mtx_unlock(&softc->ctl_lock);
2622 struct ctl_ooa *ooa_hdr;
2623 struct ctl_ooa_entry *entries;
2624 uint32_t cur_fill_num;
2626 ooa_hdr = (struct ctl_ooa *)addr;
2628 if ((ooa_hdr->alloc_len == 0)
2629 || (ooa_hdr->alloc_num == 0)) {
2630 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2631 "must be non-zero\n", __func__,
2632 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2637 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2638 sizeof(struct ctl_ooa_entry))) {
2639 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2640 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2641 __func__, ooa_hdr->alloc_len,
2642 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2647 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2648 if (entries == NULL) {
2649 printf("%s: could not allocate %d bytes for OOA "
2650 "dump\n", __func__, ooa_hdr->alloc_len);
2655 mtx_lock(&softc->ctl_lock);
2656 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2657 (ooa_hdr->lun_num >= ctl_max_luns ||
2658 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2659 mtx_unlock(&softc->ctl_lock);
2660 free(entries, M_CTL);
2661 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2662 __func__, (uintmax_t)ooa_hdr->lun_num);
2669 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2670 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2671 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2675 lun = softc->ctl_luns[ooa_hdr->lun_num];
2676 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2679 mtx_unlock(&softc->ctl_lock);
2681 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2682 ooa_hdr->fill_len = ooa_hdr->fill_num *
2683 sizeof(struct ctl_ooa_entry);
2684 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2686 printf("%s: error copying out %d bytes for OOA dump\n",
2687 __func__, ooa_hdr->fill_len);
2690 getbinuptime(&ooa_hdr->cur_bt);
2692 if (cur_fill_num > ooa_hdr->alloc_num) {
2693 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2694 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2696 ooa_hdr->dropped_num = 0;
2697 ooa_hdr->status = CTL_OOA_OK;
2700 free(entries, M_CTL);
2703 case CTL_DELAY_IO: {
2704 struct ctl_io_delay_info *delay_info;
2706 delay_info = (struct ctl_io_delay_info *)addr;
2709 mtx_lock(&softc->ctl_lock);
2710 if (delay_info->lun_id >= ctl_max_luns ||
2711 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2712 mtx_unlock(&softc->ctl_lock);
2713 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2716 mtx_lock(&lun->lun_lock);
2717 mtx_unlock(&softc->ctl_lock);
2718 delay_info->status = CTL_DELAY_STATUS_OK;
2719 switch (delay_info->delay_type) {
2720 case CTL_DELAY_TYPE_CONT:
2721 case CTL_DELAY_TYPE_ONESHOT:
2724 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2727 switch (delay_info->delay_loc) {
2728 case CTL_DELAY_LOC_DATAMOVE:
2729 lun->delay_info.datamove_type = delay_info->delay_type;
2730 lun->delay_info.datamove_delay = delay_info->delay_secs;
2732 case CTL_DELAY_LOC_DONE:
2733 lun->delay_info.done_type = delay_info->delay_type;
2734 lun->delay_info.done_delay = delay_info->delay_secs;
2737 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2740 mtx_unlock(&lun->lun_lock);
2742 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2743 #endif /* CTL_IO_DELAY */
2746 #ifdef CTL_LEGACY_STATS
2747 case CTL_GETSTATS: {
2748 struct ctl_stats *stats = (struct ctl_stats *)addr;
2752 * XXX KDM no locking here. If the LUN list changes,
2753 * things can blow up.
2756 stats->status = CTL_SS_OK;
2757 stats->fill_len = 0;
2758 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2759 if (stats->fill_len + sizeof(lun->legacy_stats) >
2761 stats->status = CTL_SS_NEED_MORE_SPACE;
2764 retval = copyout(&lun->legacy_stats, &stats->lun_stats[i++],
2765 sizeof(lun->legacy_stats));
2768 stats->fill_len += sizeof(lun->legacy_stats);
2770 stats->num_luns = softc->num_luns;
2771 stats->flags = CTL_STATS_FLAG_NONE;
2773 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
2775 getnanouptime(&stats->timestamp);
2778 #endif /* CTL_LEGACY_STATS */
2779 case CTL_ERROR_INJECT: {
2780 struct ctl_error_desc *err_desc, *new_err_desc;
2782 err_desc = (struct ctl_error_desc *)addr;
2784 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2786 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2788 mtx_lock(&softc->ctl_lock);
2789 if (err_desc->lun_id >= ctl_max_luns ||
2790 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2791 mtx_unlock(&softc->ctl_lock);
2792 free(new_err_desc, M_CTL);
2793 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2794 __func__, (uintmax_t)err_desc->lun_id);
2798 mtx_lock(&lun->lun_lock);
2799 mtx_unlock(&softc->ctl_lock);
2802 * We could do some checking here to verify the validity
2803 * of the request, but given the complexity of error
2804 * injection requests, the checking logic would be fairly
2807 * For now, if the request is invalid, it just won't get
2808 * executed and might get deleted.
2810 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2813 * XXX KDM check to make sure the serial number is unique,
2814 * in case we somehow manage to wrap. That shouldn't
2815 * happen for a very long time, but it's the right thing to
2818 new_err_desc->serial = lun->error_serial;
2819 err_desc->serial = lun->error_serial;
2820 lun->error_serial++;
2822 mtx_unlock(&lun->lun_lock);
2825 case CTL_ERROR_INJECT_DELETE: {
2826 struct ctl_error_desc *delete_desc, *desc, *desc2;
2829 delete_desc = (struct ctl_error_desc *)addr;
2832 mtx_lock(&softc->ctl_lock);
2833 if (delete_desc->lun_id >= ctl_max_luns ||
2834 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2835 mtx_unlock(&softc->ctl_lock);
2836 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2837 __func__, (uintmax_t)delete_desc->lun_id);
2841 mtx_lock(&lun->lun_lock);
2842 mtx_unlock(&softc->ctl_lock);
2843 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2844 if (desc->serial != delete_desc->serial)
2847 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2852 mtx_unlock(&lun->lun_lock);
2853 if (delete_done == 0) {
2854 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2855 "error serial %ju on LUN %u\n", __func__,
2856 delete_desc->serial, delete_desc->lun_id);
2862 case CTL_DUMP_STRUCTS: {
2864 struct ctl_port *port;
2865 struct ctl_frontend *fe;
2867 mtx_lock(&softc->ctl_lock);
2868 printf("CTL Persistent Reservation information start:\n");
2869 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2870 mtx_lock(&lun->lun_lock);
2871 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2872 mtx_unlock(&lun->lun_lock);
2876 for (j = 0; j < ctl_max_ports; j++) {
2877 if (lun->pr_keys[j] == NULL)
2879 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2880 if (lun->pr_keys[j][k] == 0)
2882 printf(" LUN %ju port %d iid %d key "
2883 "%#jx\n", lun->lun, j, k,
2884 (uintmax_t)lun->pr_keys[j][k]);
2887 mtx_unlock(&lun->lun_lock);
2889 printf("CTL Persistent Reservation information end\n");
2890 printf("CTL Ports:\n");
2891 STAILQ_FOREACH(port, &softc->port_list, links) {
2892 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2893 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2894 port->frontend->name, port->port_type,
2895 port->physical_port, port->virtual_port,
2896 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2897 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2898 if (port->wwpn_iid[j].in_use == 0 &&
2899 port->wwpn_iid[j].wwpn == 0 &&
2900 port->wwpn_iid[j].name == NULL)
2903 printf(" iid %u use %d WWPN %#jx '%s'\n",
2904 j, port->wwpn_iid[j].in_use,
2905 (uintmax_t)port->wwpn_iid[j].wwpn,
2906 port->wwpn_iid[j].name);
2909 printf("CTL Port information end\n");
2910 mtx_unlock(&softc->ctl_lock);
2912 * XXX KDM calling this without a lock. We'd likely want
2913 * to drop the lock before calling the frontend's dump
2916 printf("CTL Frontends:\n");
2917 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2918 printf(" Frontend '%s'\n", fe->name);
2919 if (fe->fe_dump != NULL)
2922 printf("CTL Frontend information end\n");
2926 struct ctl_lun_req *lun_req;
2927 struct ctl_backend_driver *backend;
2929 nvlist_t *tmp_args_nvl;
2932 lun_req = (struct ctl_lun_req *)addr;
2933 tmp_args_nvl = lun_req->args_nvl;
2935 backend = ctl_backend_find(lun_req->backend);
2936 if (backend == NULL) {
2937 lun_req->status = CTL_LUN_ERROR;
2938 snprintf(lun_req->error_str,
2939 sizeof(lun_req->error_str),
2940 "Backend \"%s\" not found.",
2945 if (lun_req->args != NULL) {
2946 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK);
2947 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) {
2948 free(packed, M_CTL);
2949 lun_req->status = CTL_LUN_ERROR;
2950 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2951 "Cannot copyin args.");
2954 lun_req->args_nvl = nvlist_unpack(packed,
2955 lun_req->args_len, 0);
2956 free(packed, M_CTL);
2958 if (lun_req->args_nvl == NULL) {
2959 lun_req->status = CTL_LUN_ERROR;
2960 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2961 "Cannot unpack args nvlist.");
2965 lun_req->args_nvl = nvlist_create(0);
2967 retval = backend->ioctl(dev, cmd, addr, flag, td);
2968 nvlist_destroy(lun_req->args_nvl);
2969 lun_req->args_nvl = tmp_args_nvl;
2971 if (lun_req->result_nvl != NULL) {
2972 if (lun_req->result != NULL) {
2973 packed = nvlist_pack(lun_req->result_nvl,
2975 if (packed == NULL) {
2976 lun_req->status = CTL_LUN_ERROR;
2977 snprintf(lun_req->error_str,
2978 sizeof(lun_req->error_str),
2979 "Cannot pack result nvlist.");
2983 if (packed_len > lun_req->result_len) {
2984 lun_req->status = CTL_LUN_ERROR;
2985 snprintf(lun_req->error_str,
2986 sizeof(lun_req->error_str),
2987 "Result nvlist too large.");
2988 free(packed, M_NVLIST);
2992 if (copyout(packed, lun_req->result, packed_len)) {
2993 lun_req->status = CTL_LUN_ERROR;
2994 snprintf(lun_req->error_str,
2995 sizeof(lun_req->error_str),
2996 "Cannot copyout() the result.");
2997 free(packed, M_NVLIST);
3001 lun_req->result_len = packed_len;
3002 free(packed, M_NVLIST);
3005 nvlist_destroy(lun_req->result_nvl);
3009 case CTL_LUN_LIST: {
3011 struct ctl_lun_list *list;
3012 const char *name, *value;
3016 list = (struct ctl_lun_list *)addr;
3019 * Allocate a fixed length sbuf here, based on the length
3020 * of the user's buffer. We could allocate an auto-extending
3021 * buffer, and then tell the user how much larger our
3022 * amount of data is than his buffer, but that presents
3025 * 1. The sbuf(9) routines use a blocking malloc, and so
3026 * we can't hold a lock while calling them with an
3027 * auto-extending buffer.
3029 * 2. There is not currently a LUN reference counting
3030 * mechanism, outside of outstanding transactions on
3031 * the LUN's OOA queue. So a LUN could go away on us
3032 * while we're getting the LUN number, backend-specific
3033 * information, etc. Thus, given the way things
3034 * currently work, we need to hold the CTL lock while
3035 * grabbing LUN information.
3037 * So, from the user's standpoint, the best thing to do is
3038 * allocate what he thinks is a reasonable buffer length,
3039 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3040 * double the buffer length and try again. (And repeat
3041 * that until he succeeds.)
3043 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3045 list->status = CTL_LUN_LIST_ERROR;
3046 snprintf(list->error_str, sizeof(list->error_str),
3047 "Unable to allocate %d bytes for LUN list",
3052 sbuf_printf(sb, "<ctllunlist>\n");
3054 mtx_lock(&softc->ctl_lock);
3055 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3056 mtx_lock(&lun->lun_lock);
3057 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3058 (uintmax_t)lun->lun);
3061 * Bail out as soon as we see that we've overfilled
3067 retval = sbuf_printf(sb, "\t<backend_type>%s"
3068 "</backend_type>\n",
3069 (lun->backend == NULL) ? "none" :
3070 lun->backend->name);
3075 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3076 lun->be_lun->lun_type);
3081 if (lun->backend == NULL) {
3082 retval = sbuf_printf(sb, "</lun>\n");
3088 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3089 (lun->be_lun->maxlba > 0) ?
3090 lun->be_lun->maxlba + 1 : 0);
3095 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3096 lun->be_lun->blocksize);
3101 retval = sbuf_printf(sb, "\t<serial_number>");
3106 retval = ctl_sbuf_printf_esc(sb,
3107 lun->be_lun->serial_num,
3108 sizeof(lun->be_lun->serial_num));
3113 retval = sbuf_printf(sb, "</serial_number>\n");
3118 retval = sbuf_printf(sb, "\t<device_id>");
3123 retval = ctl_sbuf_printf_esc(sb,
3124 lun->be_lun->device_id,
3125 sizeof(lun->be_lun->device_id));
3130 retval = sbuf_printf(sb, "</device_id>\n");
3135 if (lun->backend->lun_info != NULL) {
3136 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3142 while ((name = nvlist_next(lun->be_lun->options, &type,
3143 &cookie)) != NULL) {
3144 sbuf_printf(sb, "\t<%s>", name);
3146 if (type == NV_TYPE_STRING) {
3147 value = dnvlist_get_string(
3148 lun->be_lun->options, name, NULL);
3150 sbuf_printf(sb, "%s", value);
3153 sbuf_printf(sb, "</%s>\n", name);
3156 retval = sbuf_printf(sb, "</lun>\n");
3160 mtx_unlock(&lun->lun_lock);
3163 mtx_unlock(&lun->lun_lock);
3164 mtx_unlock(&softc->ctl_lock);
3167 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3170 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3171 snprintf(list->error_str, sizeof(list->error_str),
3172 "Out of space, %d bytes is too small",
3179 retval = copyout(sbuf_data(sb), list->lun_xml,
3182 list->fill_len = sbuf_len(sb) + 1;
3183 list->status = CTL_LUN_LIST_OK;
3188 struct ctl_iscsi *ci;
3189 struct ctl_frontend *fe;
3191 ci = (struct ctl_iscsi *)addr;
3193 fe = ctl_frontend_find("iscsi");
3195 ci->status = CTL_ISCSI_ERROR;
3196 snprintf(ci->error_str, sizeof(ci->error_str),
3197 "Frontend \"iscsi\" not found.");
3201 retval = fe->ioctl(dev, cmd, addr, flag, td);
3204 case CTL_PORT_REQ: {
3205 struct ctl_req *req;
3206 struct ctl_frontend *fe;
3208 nvlist_t *tmp_args_nvl;
3211 req = (struct ctl_req *)addr;
3212 tmp_args_nvl = req->args_nvl;
3214 fe = ctl_frontend_find(req->driver);
3216 req->status = CTL_LUN_ERROR;
3217 snprintf(req->error_str, sizeof(req->error_str),
3218 "Frontend \"%s\" not found.", req->driver);
3222 if (req->args != NULL) {
3223 packed = malloc(req->args_len, M_CTL, M_WAITOK);
3224 if (copyin(req->args, packed, req->args_len) != 0) {
3225 free(packed, M_CTL);
3226 req->status = CTL_LUN_ERROR;
3227 snprintf(req->error_str, sizeof(req->error_str),
3228 "Cannot copyin args.");
3231 req->args_nvl = nvlist_unpack(packed,
3233 free(packed, M_CTL);
3235 if (req->args_nvl == NULL) {
3236 req->status = CTL_LUN_ERROR;
3237 snprintf(req->error_str, sizeof(req->error_str),
3238 "Cannot unpack args nvlist.");
3242 req->args_nvl = nvlist_create(0);
3245 retval = fe->ioctl(dev, cmd, addr, flag, td);
3249 nvlist_destroy(req->args_nvl);
3250 req->args_nvl = tmp_args_nvl;
3252 if (req->result_nvl != NULL) {
3253 if (req->result != NULL) {
3254 packed = nvlist_pack(req->result_nvl,
3256 if (packed == NULL) {
3257 req->status = CTL_LUN_ERROR;
3258 snprintf(req->error_str,
3259 sizeof(req->error_str),
3260 "Cannot pack result nvlist.");
3264 if (packed_len > req->result_len) {
3265 req->status = CTL_LUN_ERROR;
3266 snprintf(req->error_str,
3267 sizeof(req->error_str),
3268 "Result nvlist too large.");
3269 free(packed, M_NVLIST);
3273 if (copyout(packed, req->result, packed_len)) {
3274 req->status = CTL_LUN_ERROR;
3275 snprintf(req->error_str,
3276 sizeof(req->error_str),
3277 "Cannot copyout() the result.");
3278 free(packed, M_NVLIST);
3282 req->result_len = packed_len;
3283 free(packed, M_NVLIST);
3286 nvlist_destroy(req->result_nvl);
3290 case CTL_PORT_LIST: {
3292 struct ctl_port *port;
3293 struct ctl_lun_list *list;
3294 const char *name, *value;
3299 list = (struct ctl_lun_list *)addr;
3301 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3303 list->status = CTL_LUN_LIST_ERROR;
3304 snprintf(list->error_str, sizeof(list->error_str),
3305 "Unable to allocate %d bytes for LUN list",
3310 sbuf_printf(sb, "<ctlportlist>\n");
3312 mtx_lock(&softc->ctl_lock);
3313 STAILQ_FOREACH(port, &softc->port_list, links) {
3314 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3315 (uintmax_t)port->targ_port);
3318 * Bail out as soon as we see that we've overfilled
3324 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3325 "</frontend_type>\n", port->frontend->name);
3329 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3334 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3335 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3339 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3344 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3345 port->physical_port);
3349 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3350 port->virtual_port);
3354 if (port->target_devid != NULL) {
3355 sbuf_printf(sb, "\t<target>");
3356 ctl_id_sbuf(port->target_devid, sb);
3357 sbuf_printf(sb, "</target>\n");
3360 if (port->port_devid != NULL) {
3361 sbuf_printf(sb, "\t<port>");
3362 ctl_id_sbuf(port->port_devid, sb);
3363 sbuf_printf(sb, "</port>\n");
3366 if (port->port_info != NULL) {
3367 retval = port->port_info(port->onoff_arg, sb);
3373 while ((name = nvlist_next(port->options, &type,
3374 &cookie)) != NULL) {
3375 sbuf_printf(sb, "\t<%s>", name);
3377 if (type == NV_TYPE_STRING) {
3378 value = dnvlist_get_string(port->options,
3381 sbuf_printf(sb, "%s", value);
3384 sbuf_printf(sb, "</%s>\n", name);
3387 if (port->lun_map != NULL) {
3388 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3389 for (j = 0; j < port->lun_map_size; j++) {
3390 plun = ctl_lun_map_from_port(port, j);
3391 if (plun == UINT32_MAX)
3394 "\t<lun id=\"%u\">%u</lun>\n",
3399 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3400 if (port->wwpn_iid[j].in_use == 0 ||
3401 (port->wwpn_iid[j].wwpn == 0 &&
3402 port->wwpn_iid[j].name == NULL))
3405 if (port->wwpn_iid[j].name != NULL)
3406 retval = sbuf_printf(sb,
3407 "\t<initiator id=\"%u\">%s</initiator>\n",
3408 j, port->wwpn_iid[j].name);
3410 retval = sbuf_printf(sb,
3411 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3412 j, port->wwpn_iid[j].wwpn);
3419 retval = sbuf_printf(sb, "</targ_port>\n");
3423 mtx_unlock(&softc->ctl_lock);
3426 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3429 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3430 snprintf(list->error_str, sizeof(list->error_str),
3431 "Out of space, %d bytes is too small",
3438 retval = copyout(sbuf_data(sb), list->lun_xml,
3441 list->fill_len = sbuf_len(sb) + 1;
3442 list->status = CTL_LUN_LIST_OK;
3447 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3448 struct ctl_port *port;
3450 mtx_lock(&softc->ctl_lock);
3451 if (lm->port < softc->port_min ||
3452 lm->port >= softc->port_max ||
3453 (port = softc->ctl_ports[lm->port]) == NULL) {
3454 mtx_unlock(&softc->ctl_lock);
3457 if (port->status & CTL_PORT_STATUS_ONLINE) {
3458 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3459 if (ctl_lun_map_to_port(port, lun->lun) ==
3462 mtx_lock(&lun->lun_lock);
3463 ctl_est_ua_port(lun, lm->port, -1,
3465 mtx_unlock(&lun->lun_lock);
3468 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3469 if (lm->plun != UINT32_MAX) {
3470 if (lm->lun == UINT32_MAX)
3471 retval = ctl_lun_map_unset(port, lm->plun);
3472 else if (lm->lun < ctl_max_luns &&
3473 softc->ctl_luns[lm->lun] != NULL)
3474 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3478 if (lm->lun == UINT32_MAX)
3479 retval = ctl_lun_map_deinit(port);
3481 retval = ctl_lun_map_init(port);
3483 if (port->status & CTL_PORT_STATUS_ONLINE)
3484 ctl_isc_announce_port(port);
3487 case CTL_GET_LUN_STATS: {
3488 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3492 * XXX KDM no locking here. If the LUN list changes,
3493 * things can blow up.
3496 stats->status = CTL_SS_OK;
3497 stats->fill_len = 0;
3498 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3499 if (lun->lun < stats->first_item)
3501 if (stats->fill_len + sizeof(lun->stats) >
3503 stats->status = CTL_SS_NEED_MORE_SPACE;
3506 retval = copyout(&lun->stats, &stats->stats[i++],
3507 sizeof(lun->stats));
3510 stats->fill_len += sizeof(lun->stats);
3512 stats->num_items = softc->num_luns;
3513 stats->flags = CTL_STATS_FLAG_NONE;
3515 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3517 getnanouptime(&stats->timestamp);
3520 case CTL_GET_PORT_STATS: {
3521 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3525 * XXX KDM no locking here. If the LUN list changes,
3526 * things can blow up.
3529 stats->status = CTL_SS_OK;
3530 stats->fill_len = 0;
3531 STAILQ_FOREACH(port, &softc->port_list, links) {
3532 if (port->targ_port < stats->first_item)
3534 if (stats->fill_len + sizeof(port->stats) >
3536 stats->status = CTL_SS_NEED_MORE_SPACE;
3539 retval = copyout(&port->stats, &stats->stats[i++],
3540 sizeof(port->stats));
3543 stats->fill_len += sizeof(port->stats);
3545 stats->num_items = softc->num_ports;
3546 stats->flags = CTL_STATS_FLAG_NONE;
3548 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3550 getnanouptime(&stats->timestamp);
3554 /* XXX KDM should we fix this? */
3556 struct ctl_backend_driver *backend;
3563 * We encode the backend type as the ioctl type for backend
3564 * ioctls. So parse it out here, and then search for a
3565 * backend of this type.
3567 type = _IOC_TYPE(cmd);
3569 STAILQ_FOREACH(backend, &softc->be_list, links) {
3570 if (backend->type == type) {
3576 printf("ctl: unknown ioctl command %#lx or backend "
3581 retval = backend->ioctl(dev, cmd, addr, flag, td);
3591 ctl_get_initindex(struct ctl_nexus *nexus)
3593 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3597 ctl_lun_map_init(struct ctl_port *port)
3599 struct ctl_softc *softc = port->ctl_softc;
3600 struct ctl_lun *lun;
3601 int size = ctl_lun_map_size;
3604 if (port->lun_map == NULL || port->lun_map_size < size) {
3605 port->lun_map_size = 0;
3606 free(port->lun_map, M_CTL);
3607 port->lun_map = malloc(size * sizeof(uint32_t),
3610 if (port->lun_map == NULL)
3612 for (i = 0; i < size; i++)
3613 port->lun_map[i] = UINT32_MAX;
3614 port->lun_map_size = size;
3615 if (port->status & CTL_PORT_STATUS_ONLINE) {
3616 if (port->lun_disable != NULL) {
3617 STAILQ_FOREACH(lun, &softc->lun_list, links)
3618 port->lun_disable(port->targ_lun_arg, lun->lun);
3620 ctl_isc_announce_port(port);
3626 ctl_lun_map_deinit(struct ctl_port *port)
3628 struct ctl_softc *softc = port->ctl_softc;
3629 struct ctl_lun *lun;
3631 if (port->lun_map == NULL)
3633 port->lun_map_size = 0;
3634 free(port->lun_map, M_CTL);
3635 port->lun_map = NULL;
3636 if (port->status & CTL_PORT_STATUS_ONLINE) {
3637 if (port->lun_enable != NULL) {
3638 STAILQ_FOREACH(lun, &softc->lun_list, links)
3639 port->lun_enable(port->targ_lun_arg, lun->lun);
3641 ctl_isc_announce_port(port);
3647 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3652 if (port->lun_map == NULL) {
3653 status = ctl_lun_map_init(port);
3657 if (plun >= port->lun_map_size)
3659 old = port->lun_map[plun];
3660 port->lun_map[plun] = glun;
3661 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3662 if (port->lun_enable != NULL)
3663 port->lun_enable(port->targ_lun_arg, plun);
3664 ctl_isc_announce_port(port);
3670 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3674 if (port->lun_map == NULL || plun >= port->lun_map_size)
3676 old = port->lun_map[plun];
3677 port->lun_map[plun] = UINT32_MAX;
3678 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3679 if (port->lun_disable != NULL)
3680 port->lun_disable(port->targ_lun_arg, plun);
3681 ctl_isc_announce_port(port);
3687 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3691 return (UINT32_MAX);
3692 if (port->lun_map == NULL)
3694 if (lun_id > port->lun_map_size)
3695 return (UINT32_MAX);
3696 return (port->lun_map[lun_id]);
3700 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3705 return (UINT32_MAX);
3706 if (port->lun_map == NULL)
3708 for (i = 0; i < port->lun_map_size; i++) {
3709 if (port->lun_map[i] == lun_id)
3712 return (UINT32_MAX);
3716 ctl_decode_lun(uint64_t encoded)
3719 uint32_t result = 0xffffffff;
3721 be64enc(lun, encoded);
3722 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3723 case RPL_LUNDATA_ATYP_PERIPH:
3724 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3725 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3728 case RPL_LUNDATA_ATYP_FLAT:
3729 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3730 lun[6] == 0 && lun[7] == 0)
3731 result = ((lun[0] & 0x3f) << 8) + lun[1];
3733 case RPL_LUNDATA_ATYP_EXTLUN:
3734 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3736 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3741 result = (lun[1] << 16) + (lun[2] << 8) +
3745 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3746 result = (lun[2] << 24) +
3747 (lun[3] << 16) + (lun[4] << 8) +
3752 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3753 result = 0xffffffff;
3762 ctl_encode_lun(uint32_t decoded)
3764 uint64_t l = decoded;
3767 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3769 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3771 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3773 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3777 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3781 for (i = first; i < last; i++) {
3782 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3789 ctl_set_mask(uint32_t *mask, uint32_t bit)
3791 uint32_t chunk, piece;
3794 piece = bit % (sizeof(uint32_t) * 8);
3796 if ((mask[chunk] & (1 << piece)) != 0)
3799 mask[chunk] |= (1 << piece);
3805 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3807 uint32_t chunk, piece;
3810 piece = bit % (sizeof(uint32_t) * 8);
3812 if ((mask[chunk] & (1 << piece)) == 0)
3815 mask[chunk] &= ~(1 << piece);
3821 ctl_is_set(uint32_t *mask, uint32_t bit)
3823 uint32_t chunk, piece;
3826 piece = bit % (sizeof(uint32_t) * 8);
3828 if ((mask[chunk] & (1 << piece)) == 0)
3835 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3839 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3842 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3846 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3850 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3853 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3857 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3862 i = residx/CTL_MAX_INIT_PER_PORT;
3863 if (lun->pr_keys[i] != NULL)
3865 mtx_unlock(&lun->lun_lock);
3866 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3868 mtx_lock(&lun->lun_lock);
3869 if (lun->pr_keys[i] == NULL)
3870 lun->pr_keys[i] = p;
3876 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3880 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3881 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3882 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3886 * ctl_softc, pool_name, total_ctl_io are passed in.
3887 * npool is passed out.
3890 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3891 uint32_t total_ctl_io, void **npool)
3893 struct ctl_io_pool *pool;
3895 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3900 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3901 pool->ctl_softc = ctl_softc;
3903 pool->zone = uma_zsecond_create(pool->name, NULL,
3904 NULL, NULL, NULL, ctl_softc->io_zone);
3905 /* uma_prealloc(pool->zone, total_ctl_io); */
3907 pool->zone = ctl_softc->io_zone;
3915 ctl_pool_free(struct ctl_io_pool *pool)
3922 uma_zdestroy(pool->zone);
3928 ctl_alloc_io(void *pool_ref)
3930 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3933 io = uma_zalloc(pool->zone, M_WAITOK);
3935 io->io_hdr.pool = pool_ref;
3936 CTL_SOFTC(io) = pool->ctl_softc;
3942 ctl_alloc_io_nowait(void *pool_ref)
3944 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3947 io = uma_zalloc(pool->zone, M_NOWAIT);
3949 io->io_hdr.pool = pool_ref;
3950 CTL_SOFTC(io) = pool->ctl_softc;
3956 ctl_free_io(union ctl_io *io)
3958 struct ctl_io_pool *pool;
3963 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3964 uma_zfree(pool->zone, io);
3968 ctl_zero_io(union ctl_io *io)
3970 struct ctl_io_pool *pool;
3976 * May need to preserve linked list pointers at some point too.
3978 pool = io->io_hdr.pool;
3979 memset(io, 0, sizeof(*io));
3980 io->io_hdr.pool = pool;
3981 CTL_SOFTC(io) = pool->ctl_softc;
3985 ctl_expand_number(const char *buf, uint64_t *num)
3991 number = strtoq(buf, &endptr, 0);
3993 switch (tolower((unsigned char)*endptr)) {
4013 case '\0': /* No unit. */
4017 /* Unrecognized unit. */
4021 if ((number << shift) >> shift != number) {
4025 *num = number << shift;
4031 * This routine could be used in the future to load default and/or saved
4032 * mode page parameters for a particuar lun.
4035 ctl_init_page_index(struct ctl_lun *lun)
4038 struct ctl_page_index *page_index;
4042 memcpy(&lun->mode_pages.index, page_index_template,
4043 sizeof(page_index_template));
4045 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4047 page_index = &lun->mode_pages.index[i];
4048 if (lun->be_lun->lun_type == T_DIRECT &&
4049 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4051 if (lun->be_lun->lun_type == T_PROCESSOR &&
4052 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4054 if (lun->be_lun->lun_type == T_CDROM &&
4055 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4058 page_code = page_index->page_code & SMPH_PC_MASK;
4059 switch (page_code) {
4060 case SMS_RW_ERROR_RECOVERY_PAGE: {
4061 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4062 ("subpage %#x for page %#x is incorrect!",
4063 page_index->subpage, page_code));
4064 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4065 &rw_er_page_default,
4066 sizeof(rw_er_page_default));
4067 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4068 &rw_er_page_changeable,
4069 sizeof(rw_er_page_changeable));
4070 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4071 &rw_er_page_default,
4072 sizeof(rw_er_page_default));
4073 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4074 &rw_er_page_default,
4075 sizeof(rw_er_page_default));
4076 page_index->page_data =
4077 (uint8_t *)lun->mode_pages.rw_er_page;
4080 case SMS_FORMAT_DEVICE_PAGE: {
4081 struct scsi_format_page *format_page;
4083 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4084 ("subpage %#x for page %#x is incorrect!",
4085 page_index->subpage, page_code));
4088 * Sectors per track are set above. Bytes per
4089 * sector need to be set here on a per-LUN basis.
4091 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4092 &format_page_default,
4093 sizeof(format_page_default));
4094 memcpy(&lun->mode_pages.format_page[
4095 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4096 sizeof(format_page_changeable));
4097 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4098 &format_page_default,
4099 sizeof(format_page_default));
4100 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4101 &format_page_default,
4102 sizeof(format_page_default));
4104 format_page = &lun->mode_pages.format_page[
4106 scsi_ulto2b(lun->be_lun->blocksize,
4107 format_page->bytes_per_sector);
4109 format_page = &lun->mode_pages.format_page[
4111 scsi_ulto2b(lun->be_lun->blocksize,
4112 format_page->bytes_per_sector);
4114 format_page = &lun->mode_pages.format_page[
4116 scsi_ulto2b(lun->be_lun->blocksize,
4117 format_page->bytes_per_sector);
4119 page_index->page_data =
4120 (uint8_t *)lun->mode_pages.format_page;
4123 case SMS_RIGID_DISK_PAGE: {
4124 struct scsi_rigid_disk_page *rigid_disk_page;
4125 uint32_t sectors_per_cylinder;
4129 #endif /* !__XSCALE__ */
4131 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4132 ("subpage %#x for page %#x is incorrect!",
4133 page_index->subpage, page_code));
4136 * Rotation rate and sectors per track are set
4137 * above. We calculate the cylinders here based on
4138 * capacity. Due to the number of heads and
4139 * sectors per track we're using, smaller arrays
4140 * may turn out to have 0 cylinders. Linux and
4141 * FreeBSD don't pay attention to these mode pages
4142 * to figure out capacity, but Solaris does. It
4143 * seems to deal with 0 cylinders just fine, and
4144 * works out a fake geometry based on the capacity.
4146 memcpy(&lun->mode_pages.rigid_disk_page[
4147 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4148 sizeof(rigid_disk_page_default));
4149 memcpy(&lun->mode_pages.rigid_disk_page[
4150 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4151 sizeof(rigid_disk_page_changeable));
4153 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4157 * The divide method here will be more accurate,
4158 * probably, but results in floating point being
4159 * used in the kernel on i386 (__udivdi3()). On the
4160 * XScale, though, __udivdi3() is implemented in
4163 * The shift method for cylinder calculation is
4164 * accurate if sectors_per_cylinder is a power of
4165 * 2. Otherwise it might be slightly off -- you
4166 * might have a bit of a truncation problem.
4169 cylinders = (lun->be_lun->maxlba + 1) /
4170 sectors_per_cylinder;
4172 for (shift = 31; shift > 0; shift--) {
4173 if (sectors_per_cylinder & (1 << shift))
4176 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4180 * We've basically got 3 bytes, or 24 bits for the
4181 * cylinder size in the mode page. If we're over,
4182 * just round down to 2^24.
4184 if (cylinders > 0xffffff)
4185 cylinders = 0xffffff;
4187 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4189 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4191 if ((value = dnvlist_get_string(lun->be_lun->options,
4192 "rpm", NULL)) != NULL) {
4193 scsi_ulto2b(strtol(value, NULL, 0),
4194 rigid_disk_page->rotation_rate);
4197 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4198 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4199 sizeof(rigid_disk_page_default));
4200 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4201 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4202 sizeof(rigid_disk_page_default));
4204 page_index->page_data =
4205 (uint8_t *)lun->mode_pages.rigid_disk_page;
4208 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4209 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4210 ("subpage %#x for page %#x is incorrect!",
4211 page_index->subpage, page_code));
4212 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4213 &verify_er_page_default,
4214 sizeof(verify_er_page_default));
4215 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4216 &verify_er_page_changeable,
4217 sizeof(verify_er_page_changeable));
4218 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4219 &verify_er_page_default,
4220 sizeof(verify_er_page_default));
4221 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4222 &verify_er_page_default,
4223 sizeof(verify_er_page_default));
4224 page_index->page_data =
4225 (uint8_t *)lun->mode_pages.verify_er_page;
4228 case SMS_CACHING_PAGE: {
4229 struct scsi_caching_page *caching_page;
4231 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4232 ("subpage %#x for page %#x is incorrect!",
4233 page_index->subpage, page_code));
4234 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4235 &caching_page_default,
4236 sizeof(caching_page_default));
4237 memcpy(&lun->mode_pages.caching_page[
4238 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4239 sizeof(caching_page_changeable));
4240 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4241 &caching_page_default,
4242 sizeof(caching_page_default));
4243 caching_page = &lun->mode_pages.caching_page[
4245 value = dnvlist_get_string(lun->be_lun->options,
4246 "writecache", NULL);
4247 if (value != NULL && strcmp(value, "off") == 0)
4248 caching_page->flags1 &= ~SCP_WCE;
4249 value = dnvlist_get_string(lun->be_lun->options,
4251 if (value != NULL && strcmp(value, "off") == 0)
4252 caching_page->flags1 |= SCP_RCD;
4253 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4254 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4255 sizeof(caching_page_default));
4256 page_index->page_data =
4257 (uint8_t *)lun->mode_pages.caching_page;
4260 case SMS_CONTROL_MODE_PAGE: {
4261 switch (page_index->subpage) {
4262 case SMS_SUBPAGE_PAGE_0: {
4263 struct scsi_control_page *control_page;
4265 memcpy(&lun->mode_pages.control_page[
4267 &control_page_default,
4268 sizeof(control_page_default));
4269 memcpy(&lun->mode_pages.control_page[
4270 CTL_PAGE_CHANGEABLE],
4271 &control_page_changeable,
4272 sizeof(control_page_changeable));
4273 memcpy(&lun->mode_pages.control_page[
4275 &control_page_default,
4276 sizeof(control_page_default));
4277 control_page = &lun->mode_pages.control_page[
4279 value = dnvlist_get_string(lun->be_lun->options,
4280 "reordering", NULL);
4281 if (value != NULL &&
4282 strcmp(value, "unrestricted") == 0) {
4283 control_page->queue_flags &=
4284 ~SCP_QUEUE_ALG_MASK;
4285 control_page->queue_flags |=
4286 SCP_QUEUE_ALG_UNRESTRICTED;
4288 memcpy(&lun->mode_pages.control_page[
4290 &lun->mode_pages.control_page[
4292 sizeof(control_page_default));
4293 page_index->page_data =
4294 (uint8_t *)lun->mode_pages.control_page;
4298 memcpy(&lun->mode_pages.control_ext_page[
4300 &control_ext_page_default,
4301 sizeof(control_ext_page_default));
4302 memcpy(&lun->mode_pages.control_ext_page[
4303 CTL_PAGE_CHANGEABLE],
4304 &control_ext_page_changeable,
4305 sizeof(control_ext_page_changeable));
4306 memcpy(&lun->mode_pages.control_ext_page[
4308 &control_ext_page_default,
4309 sizeof(control_ext_page_default));
4310 memcpy(&lun->mode_pages.control_ext_page[
4312 &lun->mode_pages.control_ext_page[
4314 sizeof(control_ext_page_default));
4315 page_index->page_data =
4316 (uint8_t *)lun->mode_pages.control_ext_page;
4319 panic("subpage %#x for page %#x is incorrect!",
4320 page_index->subpage, page_code);
4324 case SMS_INFO_EXCEPTIONS_PAGE: {
4325 switch (page_index->subpage) {
4326 case SMS_SUBPAGE_PAGE_0:
4327 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4329 sizeof(ie_page_default));
4330 memcpy(&lun->mode_pages.ie_page[
4331 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4332 sizeof(ie_page_changeable));
4333 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4335 sizeof(ie_page_default));
4336 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4338 sizeof(ie_page_default));
4339 page_index->page_data =
4340 (uint8_t *)lun->mode_pages.ie_page;
4343 struct ctl_logical_block_provisioning_page *page;
4345 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4347 sizeof(lbp_page_default));
4348 memcpy(&lun->mode_pages.lbp_page[
4349 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4350 sizeof(lbp_page_changeable));
4351 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4353 sizeof(lbp_page_default));
4354 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4355 value = dnvlist_get_string(lun->be_lun->options,
4356 "avail-threshold", NULL);
4357 if (value != NULL &&
4358 ctl_expand_number(value, &ival) == 0) {
4359 page->descr[0].flags |= SLBPPD_ENABLED |
4361 if (lun->be_lun->blocksize)
4362 ival /= lun->be_lun->blocksize;
4365 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4366 page->descr[0].count);
4368 value = dnvlist_get_string(lun->be_lun->options,
4369 "used-threshold", NULL);
4370 if (value != NULL &&
4371 ctl_expand_number(value, &ival) == 0) {
4372 page->descr[1].flags |= SLBPPD_ENABLED |
4374 if (lun->be_lun->blocksize)
4375 ival /= lun->be_lun->blocksize;
4378 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4379 page->descr[1].count);
4381 value = dnvlist_get_string(lun->be_lun->options,
4382 "pool-avail-threshold", NULL);
4383 if (value != NULL &&
4384 ctl_expand_number(value, &ival) == 0) {
4385 page->descr[2].flags |= SLBPPD_ENABLED |
4387 if (lun->be_lun->blocksize)
4388 ival /= lun->be_lun->blocksize;
4391 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4392 page->descr[2].count);
4394 value = dnvlist_get_string(lun->be_lun->options,
4395 "pool-used-threshold", NULL);
4396 if (value != NULL &&
4397 ctl_expand_number(value, &ival) == 0) {
4398 page->descr[3].flags |= SLBPPD_ENABLED |
4400 if (lun->be_lun->blocksize)
4401 ival /= lun->be_lun->blocksize;
4404 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4405 page->descr[3].count);
4407 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4408 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4409 sizeof(lbp_page_default));
4410 page_index->page_data =
4411 (uint8_t *)lun->mode_pages.lbp_page;
4415 panic("subpage %#x for page %#x is incorrect!",
4416 page_index->subpage, page_code);
4420 case SMS_CDDVD_CAPS_PAGE:{
4421 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4422 ("subpage %#x for page %#x is incorrect!",
4423 page_index->subpage, page_code));
4424 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4425 &cddvd_page_default,
4426 sizeof(cddvd_page_default));
4427 memcpy(&lun->mode_pages.cddvd_page[
4428 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4429 sizeof(cddvd_page_changeable));
4430 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4431 &cddvd_page_default,
4432 sizeof(cddvd_page_default));
4433 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4434 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4435 sizeof(cddvd_page_default));
4436 page_index->page_data =
4437 (uint8_t *)lun->mode_pages.cddvd_page;
4441 panic("invalid page code value %#x", page_code);
4445 return (CTL_RETVAL_COMPLETE);
4449 ctl_init_log_page_index(struct ctl_lun *lun)
4451 struct ctl_page_index *page_index;
4454 memcpy(&lun->log_pages.index, log_page_index_template,
4455 sizeof(log_page_index_template));
4458 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4460 page_index = &lun->log_pages.index[i];
4461 if (lun->be_lun->lun_type == T_DIRECT &&
4462 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4464 if (lun->be_lun->lun_type == T_PROCESSOR &&
4465 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4467 if (lun->be_lun->lun_type == T_CDROM &&
4468 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4471 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4472 lun->backend->lun_attr == NULL)
4475 if (page_index->page_code != prev) {
4476 lun->log_pages.pages_page[j] = page_index->page_code;
4477 prev = page_index->page_code;
4480 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4481 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4484 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4485 lun->log_pages.index[0].page_len = j;
4486 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4487 lun->log_pages.index[1].page_len = k * 2;
4488 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4489 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4490 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4491 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4492 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4493 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4495 return (CTL_RETVAL_COMPLETE);
4499 hex2bin(const char *str, uint8_t *buf, int buf_size)
4504 memset(buf, 0, buf_size);
4505 while (isspace(str[0]))
4507 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4510 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4511 while (str[i] == '-') /* Skip dashes in UUIDs. */
4516 else if (isalpha(c))
4517 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4523 buf[i / 2] |= (c << 4);
4527 return ((i + 1) / 2);
4534 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4535 * wants us to allocate the LUN and he can block.
4536 * - ctl_softc is always set
4537 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4539 * Returns 0 for success, non-zero (errno) for failure.
4542 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4543 struct ctl_be_lun *const be_lun)
4545 struct ctl_lun *nlun, *lun;
4546 struct scsi_vpd_id_descriptor *desc;
4547 struct scsi_vpd_id_t10 *t10id;
4548 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4549 int lun_number, lun_malloced;
4550 int devidlen, idlen1, idlen2 = 0, len;
4556 * We currently only support Direct Access or Processor LUN types.
4558 switch (be_lun->lun_type) {
4566 be_lun->lun_config_status(be_lun->be_lun,
4567 CTL_LUN_CONFIG_FAILURE);
4570 if (ctl_lun == NULL) {
4571 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4578 memset(lun, 0, sizeof(*lun));
4580 lun->flags = CTL_LUN_MALLOCED;
4582 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4583 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4584 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4585 M_DEVBUF, M_WAITOK | M_ZERO);
4586 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4587 M_DEVBUF, M_WAITOK | M_ZERO);
4589 /* Generate LUN ID. */
4590 devidlen = max(CTL_DEVID_MIN_LEN,
4591 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4592 idlen1 = sizeof(*t10id) + devidlen;
4593 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4594 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL);
4595 if (scsiname != NULL) {
4596 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4597 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4599 eui = dnvlist_get_string(be_lun->options, "eui", NULL);
4601 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4603 naa = dnvlist_get_string(be_lun->options, "naa", NULL);
4605 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4607 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL);
4609 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4611 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4612 M_CTL, M_WAITOK | M_ZERO);
4613 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4614 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4615 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4616 desc->length = idlen1;
4617 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4618 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4619 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) {
4620 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4622 strncpy(t10id->vendor, vendor,
4623 min(sizeof(t10id->vendor), strlen(vendor)));
4625 strncpy((char *)t10id->vendor_spec_id,
4626 (char *)be_lun->device_id, devidlen);
4627 if (scsiname != NULL) {
4628 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4630 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4631 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4632 SVPD_ID_TYPE_SCSI_NAME;
4633 desc->length = idlen2;
4634 strlcpy(desc->identifier, scsiname, idlen2);
4637 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4639 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4640 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4642 desc->length = hex2bin(eui, desc->identifier, 16);
4643 desc->length = desc->length > 12 ? 16 :
4644 (desc->length > 8 ? 12 : 8);
4645 len -= 16 - desc->length;
4648 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4650 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4651 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4653 desc->length = hex2bin(naa, desc->identifier, 16);
4654 desc->length = desc->length > 8 ? 16 : 8;
4655 len -= 16 - desc->length;
4658 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4660 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4661 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4663 desc->identifier[0] = 0x10;
4664 hex2bin(uuid, &desc->identifier[2], 16);
4667 lun->lun_devid->len = len;
4669 mtx_lock(&ctl_softc->ctl_lock);
4671 * See if the caller requested a particular LUN number. If so, see
4672 * if it is available. Otherwise, allocate the first available LUN.
4674 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4675 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4676 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4677 mtx_unlock(&ctl_softc->ctl_lock);
4678 if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4679 printf("ctl: requested LUN ID %d is higher "
4680 "than ctl_max_luns - 1 (%d)\n",
4681 be_lun->req_lun_id, ctl_max_luns - 1);
4684 * XXX KDM return an error, or just assign
4685 * another LUN ID in this case??
4687 printf("ctl: requested LUN ID %d is already "
4688 "in use\n", be_lun->req_lun_id);
4691 free(lun->lun_devid, M_CTL);
4692 if (lun->flags & CTL_LUN_MALLOCED)
4694 be_lun->lun_config_status(be_lun->be_lun,
4695 CTL_LUN_CONFIG_FAILURE);
4698 lun_number = be_lun->req_lun_id;
4700 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4701 if (lun_number == -1) {
4702 mtx_unlock(&ctl_softc->ctl_lock);
4703 printf("ctl: can't allocate LUN, out of LUNs\n");
4707 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4708 mtx_unlock(&ctl_softc->ctl_lock);
4710 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4711 lun->lun = lun_number;
4712 lun->be_lun = be_lun;
4714 * The processor LUN is always enabled. Disk LUNs come on line
4715 * disabled, and must be enabled by the backend.
4717 lun->flags |= CTL_LUN_DISABLED;
4718 lun->backend = be_lun->be;
4719 be_lun->ctl_lun = lun;
4720 be_lun->lun_id = lun_number;
4721 atomic_add_int(&be_lun->be->num_luns, 1);
4722 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4723 lun->flags |= CTL_LUN_EJECTED;
4724 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4725 lun->flags |= CTL_LUN_NO_MEDIA;
4726 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4727 lun->flags |= CTL_LUN_STOPPED;
4729 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4730 lun->flags |= CTL_LUN_PRIMARY_SC;
4732 value = dnvlist_get_string(be_lun->options, "removable", NULL);
4733 if (value != NULL) {
4734 if (strcmp(value, "on") == 0)
4735 lun->flags |= CTL_LUN_REMOVABLE;
4736 } else if (be_lun->lun_type == T_CDROM)
4737 lun->flags |= CTL_LUN_REMOVABLE;
4739 lun->ctl_softc = ctl_softc;
4741 lun->last_busy = getsbinuptime();
4743 TAILQ_INIT(&lun->ooa_queue);
4744 TAILQ_INIT(&lun->blocked_queue);
4745 STAILQ_INIT(&lun->error_list);
4746 lun->ie_reported = 1;
4747 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4748 ctl_tpc_lun_init(lun);
4749 if (lun->flags & CTL_LUN_REMOVABLE) {
4750 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4755 * Initialize the mode and log page index.
4757 ctl_init_page_index(lun);
4758 ctl_init_log_page_index(lun);
4760 /* Setup statistics gathering */
4761 #ifdef CTL_LEGACY_STATS
4762 lun->legacy_stats.device_type = be_lun->lun_type;
4763 lun->legacy_stats.lun_number = lun_number;
4764 lun->legacy_stats.blocksize = be_lun->blocksize;
4765 if (be_lun->blocksize == 0)
4766 lun->legacy_stats.flags = CTL_LUN_STATS_NO_BLOCKSIZE;
4767 lun->legacy_stats.ports = malloc(sizeof(struct ctl_lun_io_port_stats) *
4768 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4769 for (len = 0; len < ctl_max_ports; len++)
4770 lun->legacy_stats.ports[len].targ_port = len;
4771 #endif /* CTL_LEGACY_STATS */
4772 lun->stats.item = lun_number;
4775 * Now, before we insert this lun on the lun list, set the lun
4776 * inventory changed UA for all other luns.
4778 mtx_lock(&ctl_softc->ctl_lock);
4779 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4780 mtx_lock(&nlun->lun_lock);
4781 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4782 mtx_unlock(&nlun->lun_lock);
4784 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4785 ctl_softc->ctl_luns[lun_number] = lun;
4786 ctl_softc->num_luns++;
4787 mtx_unlock(&ctl_softc->ctl_lock);
4789 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4796 * - LUN has already been marked invalid and any pending I/O has been taken
4800 ctl_free_lun(struct ctl_lun *lun)
4802 struct ctl_softc *softc = lun->ctl_softc;
4803 struct ctl_lun *nlun;
4806 KASSERT(TAILQ_EMPTY(&lun->ooa_queue),
4807 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4809 mtx_lock(&softc->ctl_lock);
4810 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4811 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4812 softc->ctl_luns[lun->lun] = NULL;
4814 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4815 mtx_lock(&nlun->lun_lock);
4816 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4817 mtx_unlock(&nlun->lun_lock);
4819 mtx_unlock(&softc->ctl_lock);
4822 * Tell the backend to free resources, if this LUN has a backend.
4824 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4825 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4827 lun->ie_reportcnt = UINT32_MAX;
4828 callout_drain(&lun->ie_callout);
4829 ctl_tpc_lun_shutdown(lun);
4830 mtx_destroy(&lun->lun_lock);
4831 free(lun->lun_devid, M_CTL);
4832 for (i = 0; i < ctl_max_ports; i++)
4833 free(lun->pending_ua[i], M_CTL);
4834 free(lun->pending_ua, M_DEVBUF);
4835 for (i = 0; i < ctl_max_ports; i++)
4836 free(lun->pr_keys[i], M_CTL);
4837 free(lun->pr_keys, M_DEVBUF);
4838 free(lun->write_buffer, M_CTL);
4839 free(lun->prevent, M_CTL);
4840 if (lun->flags & CTL_LUN_MALLOCED)
4847 ctl_create_lun(struct ctl_be_lun *be_lun)
4851 * ctl_alloc_lun() should handle all potential failure cases.
4853 ctl_alloc_lun(control_softc, NULL, be_lun);
4857 ctl_add_lun(struct ctl_be_lun *be_lun)
4859 struct ctl_softc *softc = control_softc;
4861 mtx_lock(&softc->ctl_lock);
4862 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4863 mtx_unlock(&softc->ctl_lock);
4864 wakeup(&softc->pending_lun_queue);
4870 ctl_enable_lun(struct ctl_be_lun *be_lun)
4872 struct ctl_softc *softc;
4873 struct ctl_port *port, *nport;
4874 struct ctl_lun *lun;
4877 lun = (struct ctl_lun *)be_lun->ctl_lun;
4878 softc = lun->ctl_softc;
4880 mtx_lock(&softc->ctl_lock);
4881 mtx_lock(&lun->lun_lock);
4882 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4884 * eh? Why did we get called if the LUN is already
4887 mtx_unlock(&lun->lun_lock);
4888 mtx_unlock(&softc->ctl_lock);
4891 lun->flags &= ~CTL_LUN_DISABLED;
4892 mtx_unlock(&lun->lun_lock);
4894 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4895 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4896 port->lun_map != NULL || port->lun_enable == NULL)
4900 * Drop the lock while we call the FETD's enable routine.
4901 * This can lead to a callback into CTL (at least in the
4902 * case of the internal initiator frontend.
4904 mtx_unlock(&softc->ctl_lock);
4905 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4906 mtx_lock(&softc->ctl_lock);
4908 printf("%s: FETD %s port %d returned error "
4909 "%d for lun_enable on lun %jd\n",
4910 __func__, port->port_name, port->targ_port,
4911 retval, (intmax_t)lun->lun);
4915 mtx_unlock(&softc->ctl_lock);
4916 ctl_isc_announce_lun(lun);
4922 ctl_disable_lun(struct ctl_be_lun *be_lun)
4924 struct ctl_softc *softc;
4925 struct ctl_port *port;
4926 struct ctl_lun *lun;
4929 lun = (struct ctl_lun *)be_lun->ctl_lun;
4930 softc = lun->ctl_softc;
4932 mtx_lock(&softc->ctl_lock);
4933 mtx_lock(&lun->lun_lock);
4934 if (lun->flags & CTL_LUN_DISABLED) {
4935 mtx_unlock(&lun->lun_lock);
4936 mtx_unlock(&softc->ctl_lock);
4939 lun->flags |= CTL_LUN_DISABLED;
4940 mtx_unlock(&lun->lun_lock);
4942 STAILQ_FOREACH(port, &softc->port_list, links) {
4943 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4944 port->lun_map != NULL || port->lun_disable == NULL)
4948 * Drop the lock before we call the frontend's disable
4949 * routine, to avoid lock order reversals.
4951 * XXX KDM what happens if the frontend list changes while
4952 * we're traversing it? It's unlikely, but should be handled.
4954 mtx_unlock(&softc->ctl_lock);
4955 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4956 mtx_lock(&softc->ctl_lock);
4958 printf("%s: FETD %s port %d returned error "
4959 "%d for lun_disable on lun %jd\n",
4960 __func__, port->port_name, port->targ_port,
4961 retval, (intmax_t)lun->lun);
4965 mtx_unlock(&softc->ctl_lock);
4966 ctl_isc_announce_lun(lun);
4972 ctl_start_lun(struct ctl_be_lun *be_lun)
4974 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4976 mtx_lock(&lun->lun_lock);
4977 lun->flags &= ~CTL_LUN_STOPPED;
4978 mtx_unlock(&lun->lun_lock);
4983 ctl_stop_lun(struct ctl_be_lun *be_lun)
4985 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4987 mtx_lock(&lun->lun_lock);
4988 lun->flags |= CTL_LUN_STOPPED;
4989 mtx_unlock(&lun->lun_lock);
4994 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4996 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4998 mtx_lock(&lun->lun_lock);
4999 lun->flags |= CTL_LUN_NO_MEDIA;
5000 mtx_unlock(&lun->lun_lock);
5005 ctl_lun_has_media(struct ctl_be_lun *be_lun)
5007 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5008 union ctl_ha_msg msg;
5010 mtx_lock(&lun->lun_lock);
5011 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
5012 if (lun->flags & CTL_LUN_REMOVABLE)
5013 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
5014 mtx_unlock(&lun->lun_lock);
5015 if ((lun->flags & CTL_LUN_REMOVABLE) &&
5016 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5017 bzero(&msg.ua, sizeof(msg.ua));
5018 msg.hdr.msg_type = CTL_MSG_UA;
5019 msg.hdr.nexus.initid = -1;
5020 msg.hdr.nexus.targ_port = -1;
5021 msg.hdr.nexus.targ_lun = lun->lun;
5022 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5025 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
5026 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5033 ctl_lun_ejected(struct ctl_be_lun *be_lun)
5035 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5037 mtx_lock(&lun->lun_lock);
5038 lun->flags |= CTL_LUN_EJECTED;
5039 mtx_unlock(&lun->lun_lock);
5044 ctl_lun_primary(struct ctl_be_lun *be_lun)
5046 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5048 mtx_lock(&lun->lun_lock);
5049 lun->flags |= CTL_LUN_PRIMARY_SC;
5050 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5051 mtx_unlock(&lun->lun_lock);
5052 ctl_isc_announce_lun(lun);
5057 ctl_lun_secondary(struct ctl_be_lun *be_lun)
5059 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5061 mtx_lock(&lun->lun_lock);
5062 lun->flags &= ~CTL_LUN_PRIMARY_SC;
5063 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5064 mtx_unlock(&lun->lun_lock);
5065 ctl_isc_announce_lun(lun);
5070 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5072 struct ctl_lun *lun;
5074 lun = (struct ctl_lun *)be_lun->ctl_lun;
5076 mtx_lock(&lun->lun_lock);
5079 * The LUN needs to be disabled before it can be marked invalid.
5081 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5082 mtx_unlock(&lun->lun_lock);
5086 * Mark the LUN invalid.
5088 lun->flags |= CTL_LUN_INVALID;
5091 * If there is nothing in the OOA queue, go ahead and free the LUN.
5092 * If we have something in the OOA queue, we'll free it when the
5093 * last I/O completes.
5095 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5096 mtx_unlock(&lun->lun_lock);
5099 mtx_unlock(&lun->lun_lock);
5105 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5107 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5108 union ctl_ha_msg msg;
5110 mtx_lock(&lun->lun_lock);
5111 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5112 mtx_unlock(&lun->lun_lock);
5113 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5114 /* Send msg to other side. */
5115 bzero(&msg.ua, sizeof(msg.ua));
5116 msg.hdr.msg_type = CTL_MSG_UA;
5117 msg.hdr.nexus.initid = -1;
5118 msg.hdr.nexus.targ_port = -1;
5119 msg.hdr.nexus.targ_lun = lun->lun;
5120 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5123 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5124 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5130 * Backend "memory move is complete" callback for requests that never
5131 * make it down to say RAIDCore's configuration code.
5134 ctl_config_move_done(union ctl_io *io)
5138 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5139 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5140 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5142 if ((io->io_hdr.port_status != 0) &&
5143 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5144 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5145 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5146 /*retry_count*/ io->io_hdr.port_status);
5147 } else if (io->scsiio.kern_data_resid != 0 &&
5148 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5149 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5150 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5151 ctl_set_invalid_field_ciu(&io->scsiio);
5154 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5156 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5157 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5158 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5159 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5161 * XXX KDM just assuming a single pointer here, and not a
5162 * S/G list. If we start using S/G lists for config data,
5163 * we'll need to know how to clean them up here as well.
5165 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5166 free(io->scsiio.kern_data_ptr, M_CTL);
5168 retval = CTL_RETVAL_COMPLETE;
5171 * XXX KDM now we need to continue data movement. Some
5173 * - call ctl_scsiio() again? We don't do this for data
5174 * writes, because for those at least we know ahead of
5175 * time where the write will go and how long it is. For
5176 * config writes, though, that information is largely
5177 * contained within the write itself, thus we need to
5178 * parse out the data again.
5180 * - Call some other function once the data is in?
5184 * XXX KDM call ctl_scsiio() again for now, and check flag
5185 * bits to see whether we're allocated or not.
5187 retval = ctl_scsiio(&io->scsiio);
5193 * This gets called by a backend driver when it is done with a
5194 * data_submit method.
5197 ctl_data_submit_done(union ctl_io *io)
5200 * If the IO_CONT flag is set, we need to call the supplied
5201 * function to continue processing the I/O, instead of completing
5204 * If there is an error, though, we don't want to keep processing.
5205 * Instead, just send status back to the initiator.
5207 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5208 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5209 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5210 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5211 io->scsiio.io_cont(io);
5218 * This gets called by a backend driver when it is done with a
5219 * configuration write.
5222 ctl_config_write_done(union ctl_io *io)
5227 * If the IO_CONT flag is set, we need to call the supplied
5228 * function to continue processing the I/O, instead of completing
5231 * If there is an error, though, we don't want to keep processing.
5232 * Instead, just send status back to the initiator.
5234 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5235 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5236 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5237 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5238 io->scsiio.io_cont(io);
5242 * Since a configuration write can be done for commands that actually
5243 * have data allocated, like write buffer, and commands that have
5244 * no data, like start/stop unit, we need to check here.
5246 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5247 buf = io->scsiio.kern_data_ptr;
5256 ctl_config_read_done(union ctl_io *io)
5261 * If there is some error -- we are done, skip data transfer.
5263 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5264 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5265 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5266 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5267 buf = io->scsiio.kern_data_ptr;
5277 * If the IO_CONT flag is set, we need to call the supplied
5278 * function to continue processing the I/O, instead of completing
5281 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5282 io->scsiio.io_cont(io);
5290 * SCSI release command.
5293 ctl_scsi_release(struct ctl_scsiio *ctsio)
5295 struct ctl_lun *lun = CTL_LUN(ctsio);
5298 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5300 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5303 * XXX KDM right now, we only support LUN reservation. We don't
5304 * support 3rd party reservations, or extent reservations, which
5305 * might actually need the parameter list. If we've gotten this
5306 * far, we've got a LUN reservation. Anything else got kicked out
5307 * above. So, according to SPC, ignore the length.
5310 mtx_lock(&lun->lun_lock);
5313 * According to SPC, it is not an error for an intiator to attempt
5314 * to release a reservation on a LUN that isn't reserved, or that
5315 * is reserved by another initiator. The reservation can only be
5316 * released, though, by the initiator who made it or by one of
5317 * several reset type events.
5319 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5320 lun->flags &= ~CTL_LUN_RESERVED;
5322 mtx_unlock(&lun->lun_lock);
5324 ctl_set_success(ctsio);
5325 ctl_done((union ctl_io *)ctsio);
5326 return (CTL_RETVAL_COMPLETE);
5330 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5332 struct ctl_lun *lun = CTL_LUN(ctsio);
5335 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5337 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5340 * XXX KDM right now, we only support LUN reservation. We don't
5341 * support 3rd party reservations, or extent reservations, which
5342 * might actually need the parameter list. If we've gotten this
5343 * far, we've got a LUN reservation. Anything else got kicked out
5344 * above. So, according to SPC, ignore the length.
5347 mtx_lock(&lun->lun_lock);
5348 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5349 ctl_set_reservation_conflict(ctsio);
5353 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5354 if (lun->flags & CTL_LUN_PR_RESERVED) {
5355 ctl_set_success(ctsio);
5359 lun->flags |= CTL_LUN_RESERVED;
5360 lun->res_idx = residx;
5361 ctl_set_success(ctsio);
5364 mtx_unlock(&lun->lun_lock);
5365 ctl_done((union ctl_io *)ctsio);
5366 return (CTL_RETVAL_COMPLETE);
5370 ctl_start_stop(struct ctl_scsiio *ctsio)
5372 struct ctl_lun *lun = CTL_LUN(ctsio);
5373 struct scsi_start_stop_unit *cdb;
5376 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5378 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5380 if ((cdb->how & SSS_PC_MASK) == 0) {
5381 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5382 (cdb->how & SSS_START) == 0) {
5385 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5386 if (ctl_get_prkey(lun, residx) == 0 ||
5387 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5389 ctl_set_reservation_conflict(ctsio);
5390 ctl_done((union ctl_io *)ctsio);
5391 return (CTL_RETVAL_COMPLETE);
5395 if ((cdb->how & SSS_LOEJ) &&
5396 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5397 ctl_set_invalid_field(ctsio,
5403 ctl_done((union ctl_io *)ctsio);
5404 return (CTL_RETVAL_COMPLETE);
5407 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5408 lun->prevent_count > 0) {
5409 /* "Medium removal prevented" */
5410 ctl_set_sense(ctsio, /*current_error*/ 1,
5411 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5412 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5413 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5414 ctl_done((union ctl_io *)ctsio);
5415 return (CTL_RETVAL_COMPLETE);
5419 retval = lun->backend->config_write((union ctl_io *)ctsio);
5424 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5426 struct ctl_lun *lun = CTL_LUN(ctsio);
5427 struct scsi_prevent *cdb;
5431 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5433 cdb = (struct scsi_prevent *)ctsio->cdb;
5435 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5436 ctl_set_invalid_opcode(ctsio);
5437 ctl_done((union ctl_io *)ctsio);
5438 return (CTL_RETVAL_COMPLETE);
5441 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5442 mtx_lock(&lun->lun_lock);
5443 if ((cdb->how & PR_PREVENT) &&
5444 ctl_is_set(lun->prevent, initidx) == 0) {
5445 ctl_set_mask(lun->prevent, initidx);
5446 lun->prevent_count++;
5447 } else if ((cdb->how & PR_PREVENT) == 0 &&
5448 ctl_is_set(lun->prevent, initidx)) {
5449 ctl_clear_mask(lun->prevent, initidx);
5450 lun->prevent_count--;
5452 mtx_unlock(&lun->lun_lock);
5453 retval = lun->backend->config_write((union ctl_io *)ctsio);
5458 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5459 * we don't really do anything with the LBA and length fields if the user
5460 * passes them in. Instead we'll just flush out the cache for the entire
5464 ctl_sync_cache(struct ctl_scsiio *ctsio)
5466 struct ctl_lun *lun = CTL_LUN(ctsio);
5467 struct ctl_lba_len_flags *lbalen;
5468 uint64_t starting_lba;
5469 uint32_t block_count;
5473 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5477 switch (ctsio->cdb[0]) {
5478 case SYNCHRONIZE_CACHE: {
5479 struct scsi_sync_cache *cdb;
5480 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5482 starting_lba = scsi_4btoul(cdb->begin_lba);
5483 block_count = scsi_2btoul(cdb->lb_count);
5487 case SYNCHRONIZE_CACHE_16: {
5488 struct scsi_sync_cache_16 *cdb;
5489 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5491 starting_lba = scsi_8btou64(cdb->begin_lba);
5492 block_count = scsi_4btoul(cdb->lb_count);
5497 ctl_set_invalid_opcode(ctsio);
5498 ctl_done((union ctl_io *)ctsio);
5500 break; /* NOTREACHED */
5504 * We check the LBA and length, but don't do anything with them.
5505 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5506 * get flushed. This check will just help satisfy anyone who wants
5507 * to see an error for an out of range LBA.
5509 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5510 ctl_set_lba_out_of_range(ctsio,
5511 MAX(starting_lba, lun->be_lun->maxlba + 1));
5512 ctl_done((union ctl_io *)ctsio);
5516 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5517 lbalen->lba = starting_lba;
5518 lbalen->len = block_count;
5519 lbalen->flags = byte2;
5520 retval = lun->backend->config_write((union ctl_io *)ctsio);
5527 ctl_format(struct ctl_scsiio *ctsio)
5529 struct scsi_format *cdb;
5530 int length, defect_list_len;
5532 CTL_DEBUG_PRINT(("ctl_format\n"));
5534 cdb = (struct scsi_format *)ctsio->cdb;
5537 if (cdb->byte2 & SF_FMTDATA) {
5538 if (cdb->byte2 & SF_LONGLIST)
5539 length = sizeof(struct scsi_format_header_long);
5541 length = sizeof(struct scsi_format_header_short);
5544 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5546 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5547 ctsio->kern_data_len = length;
5548 ctsio->kern_total_len = length;
5549 ctsio->kern_rel_offset = 0;
5550 ctsio->kern_sg_entries = 0;
5551 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5552 ctsio->be_move_done = ctl_config_move_done;
5553 ctl_datamove((union ctl_io *)ctsio);
5555 return (CTL_RETVAL_COMPLETE);
5558 defect_list_len = 0;
5560 if (cdb->byte2 & SF_FMTDATA) {
5561 if (cdb->byte2 & SF_LONGLIST) {
5562 struct scsi_format_header_long *header;
5564 header = (struct scsi_format_header_long *)
5565 ctsio->kern_data_ptr;
5567 defect_list_len = scsi_4btoul(header->defect_list_len);
5568 if (defect_list_len != 0) {
5569 ctl_set_invalid_field(ctsio,
5578 struct scsi_format_header_short *header;
5580 header = (struct scsi_format_header_short *)
5581 ctsio->kern_data_ptr;
5583 defect_list_len = scsi_2btoul(header->defect_list_len);
5584 if (defect_list_len != 0) {
5585 ctl_set_invalid_field(ctsio,
5596 ctl_set_success(ctsio);
5599 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5600 free(ctsio->kern_data_ptr, M_CTL);
5601 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5604 ctl_done((union ctl_io *)ctsio);
5605 return (CTL_RETVAL_COMPLETE);
5609 ctl_read_buffer(struct ctl_scsiio *ctsio)
5611 struct ctl_lun *lun = CTL_LUN(ctsio);
5612 uint64_t buffer_offset;
5615 static uint8_t descr[4];
5616 static uint8_t echo_descr[4] = { 0 };
5618 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5620 switch (ctsio->cdb[0]) {
5622 struct scsi_read_buffer *cdb;
5624 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5625 buffer_offset = scsi_3btoul(cdb->offset);
5626 len = scsi_3btoul(cdb->length);
5630 case READ_BUFFER_16: {
5631 struct scsi_read_buffer_16 *cdb;
5633 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5634 buffer_offset = scsi_8btou64(cdb->offset);
5635 len = scsi_4btoul(cdb->length);
5639 default: /* This shouldn't happen. */
5640 ctl_set_invalid_opcode(ctsio);
5641 ctl_done((union ctl_io *)ctsio);
5642 return (CTL_RETVAL_COMPLETE);
5645 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5646 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5647 ctl_set_invalid_field(ctsio,
5653 ctl_done((union ctl_io *)ctsio);
5654 return (CTL_RETVAL_COMPLETE);
5657 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5659 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5660 ctsio->kern_data_ptr = descr;
5661 len = min(len, sizeof(descr));
5662 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5663 ctsio->kern_data_ptr = echo_descr;
5664 len = min(len, sizeof(echo_descr));
5666 if (lun->write_buffer == NULL) {
5667 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5670 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5672 ctsio->kern_data_len = len;
5673 ctsio->kern_total_len = len;
5674 ctsio->kern_rel_offset = 0;
5675 ctsio->kern_sg_entries = 0;
5676 ctl_set_success(ctsio);
5677 ctsio->be_move_done = ctl_config_move_done;
5678 ctl_datamove((union ctl_io *)ctsio);
5679 return (CTL_RETVAL_COMPLETE);
5683 ctl_write_buffer(struct ctl_scsiio *ctsio)
5685 struct ctl_lun *lun = CTL_LUN(ctsio);
5686 struct scsi_write_buffer *cdb;
5687 int buffer_offset, len;
5689 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5691 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5693 len = scsi_3btoul(cdb->length);
5694 buffer_offset = scsi_3btoul(cdb->offset);
5696 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5697 ctl_set_invalid_field(ctsio,
5703 ctl_done((union ctl_io *)ctsio);
5704 return (CTL_RETVAL_COMPLETE);
5708 * If we've got a kernel request that hasn't been malloced yet,
5709 * malloc it and tell the caller the data buffer is here.
5711 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5712 if (lun->write_buffer == NULL) {
5713 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5716 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5717 ctsio->kern_data_len = len;
5718 ctsio->kern_total_len = len;
5719 ctsio->kern_rel_offset = 0;
5720 ctsio->kern_sg_entries = 0;
5721 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5722 ctsio->be_move_done = ctl_config_move_done;
5723 ctl_datamove((union ctl_io *)ctsio);
5725 return (CTL_RETVAL_COMPLETE);
5728 ctl_set_success(ctsio);
5729 ctl_done((union ctl_io *)ctsio);
5730 return (CTL_RETVAL_COMPLETE);
5734 ctl_write_same(struct ctl_scsiio *ctsio)
5736 struct ctl_lun *lun = CTL_LUN(ctsio);
5737 struct ctl_lba_len_flags *lbalen;
5739 uint32_t num_blocks;
5743 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5745 switch (ctsio->cdb[0]) {
5746 case WRITE_SAME_10: {
5747 struct scsi_write_same_10 *cdb;
5749 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5751 lba = scsi_4btoul(cdb->addr);
5752 num_blocks = scsi_2btoul(cdb->length);
5756 case WRITE_SAME_16: {
5757 struct scsi_write_same_16 *cdb;
5759 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5761 lba = scsi_8btou64(cdb->addr);
5762 num_blocks = scsi_4btoul(cdb->length);
5768 * We got a command we don't support. This shouldn't
5769 * happen, commands should be filtered out above us.
5771 ctl_set_invalid_opcode(ctsio);
5772 ctl_done((union ctl_io *)ctsio);
5774 return (CTL_RETVAL_COMPLETE);
5775 break; /* NOTREACHED */
5778 /* ANCHOR flag can be used only together with UNMAP */
5779 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5780 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5781 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5782 ctl_done((union ctl_io *)ctsio);
5783 return (CTL_RETVAL_COMPLETE);
5787 * The first check is to make sure we're in bounds, the second
5788 * check is to catch wrap-around problems. If the lba + num blocks
5789 * is less than the lba, then we've wrapped around and the block
5790 * range is invalid anyway.
5792 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5793 || ((lba + num_blocks) < lba)) {
5794 ctl_set_lba_out_of_range(ctsio,
5795 MAX(lba, lun->be_lun->maxlba + 1));
5796 ctl_done((union ctl_io *)ctsio);
5797 return (CTL_RETVAL_COMPLETE);
5800 /* Zero number of blocks means "to the last logical block" */
5801 if (num_blocks == 0) {
5802 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5803 ctl_set_invalid_field(ctsio,
5809 ctl_done((union ctl_io *)ctsio);
5810 return (CTL_RETVAL_COMPLETE);
5812 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5815 len = lun->be_lun->blocksize;
5818 * If we've got a kernel request that hasn't been malloced yet,
5819 * malloc it and tell the caller the data buffer is here.
5821 if ((byte2 & SWS_NDOB) == 0 &&
5822 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5823 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5824 ctsio->kern_data_len = len;
5825 ctsio->kern_total_len = len;
5826 ctsio->kern_rel_offset = 0;
5827 ctsio->kern_sg_entries = 0;
5828 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5829 ctsio->be_move_done = ctl_config_move_done;
5830 ctl_datamove((union ctl_io *)ctsio);
5832 return (CTL_RETVAL_COMPLETE);
5835 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5837 lbalen->len = num_blocks;
5838 lbalen->flags = byte2;
5839 retval = lun->backend->config_write((union ctl_io *)ctsio);
5845 ctl_unmap(struct ctl_scsiio *ctsio)
5847 struct ctl_lun *lun = CTL_LUN(ctsio);
5848 struct scsi_unmap *cdb;
5849 struct ctl_ptr_len_flags *ptrlen;
5850 struct scsi_unmap_header *hdr;
5851 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5853 uint32_t num_blocks;
5857 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5859 cdb = (struct scsi_unmap *)ctsio->cdb;
5860 len = scsi_2btoul(cdb->length);
5864 * If we've got a kernel request that hasn't been malloced yet,
5865 * malloc it and tell the caller the data buffer is here.
5867 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5868 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5869 ctsio->kern_data_len = len;
5870 ctsio->kern_total_len = len;
5871 ctsio->kern_rel_offset = 0;
5872 ctsio->kern_sg_entries = 0;
5873 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5874 ctsio->be_move_done = ctl_config_move_done;
5875 ctl_datamove((union ctl_io *)ctsio);
5877 return (CTL_RETVAL_COMPLETE);
5880 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5881 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5882 if (len < sizeof (*hdr) ||
5883 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5884 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5885 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5886 ctl_set_invalid_field(ctsio,
5894 len = scsi_2btoul(hdr->desc_length);
5895 buf = (struct scsi_unmap_desc *)(hdr + 1);
5896 end = buf + len / sizeof(*buf);
5899 for (range = buf; range < end; range++) {
5900 lba = scsi_8btou64(range->lba);
5901 num_blocks = scsi_4btoul(range->length);
5902 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5903 || ((lba + num_blocks) < lba)) {
5904 ctl_set_lba_out_of_range(ctsio,
5905 MAX(lba, lun->be_lun->maxlba + 1));
5906 ctl_done((union ctl_io *)ctsio);
5907 return (CTL_RETVAL_COMPLETE);
5909 if (num_blocks != 0)
5914 * Block backend can not handle zero last range.
5915 * Filter it out and return if there is nothing left.
5917 len = (uint8_t *)endnz - (uint8_t *)buf;
5919 ctl_set_success(ctsio);
5923 mtx_lock(&lun->lun_lock);
5924 ptrlen = (struct ctl_ptr_len_flags *)
5925 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5926 ptrlen->ptr = (void *)buf;
5928 ptrlen->flags = byte2;
5929 ctl_check_blocked(lun);
5930 mtx_unlock(&lun->lun_lock);
5932 retval = lun->backend->config_write((union ctl_io *)ctsio);
5936 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5937 free(ctsio->kern_data_ptr, M_CTL);
5938 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5940 ctl_done((union ctl_io *)ctsio);
5941 return (CTL_RETVAL_COMPLETE);
5945 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5946 struct ctl_page_index *page_index, uint8_t *page_ptr)
5948 struct ctl_lun *lun = CTL_LUN(ctsio);
5949 uint8_t *current_cp;
5953 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5956 current_cp = (page_index->page_data + (page_index->page_len *
5959 mtx_lock(&lun->lun_lock);
5960 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5961 memcpy(current_cp, page_ptr, page_index->page_len);
5965 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5966 mtx_unlock(&lun->lun_lock);
5968 ctl_isc_announce_mode(lun,
5969 ctl_get_initindex(&ctsio->io_hdr.nexus),
5970 page_index->page_code, page_index->subpage);
5972 return (CTL_RETVAL_COMPLETE);
5976 ctl_ie_timer(void *arg)
5978 struct ctl_lun *lun = arg;
5981 if (lun->ie_asc == 0)
5984 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5985 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5987 lun->ie_reported = 0;
5989 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5990 lun->ie_reportcnt++;
5991 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5992 if (t == 0 || t == UINT32_MAX)
5993 t = 3000; /* 5 min */
5994 callout_schedule(&lun->ie_callout, t * hz / 10);
5999 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
6000 struct ctl_page_index *page_index, uint8_t *page_ptr)
6002 struct ctl_lun *lun = CTL_LUN(ctsio);
6003 struct scsi_info_exceptions_page *pg;
6006 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
6008 pg = (struct scsi_info_exceptions_page *)page_ptr;
6009 mtx_lock(&lun->lun_lock);
6010 if (pg->info_flags & SIEP_FLAGS_TEST) {
6012 lun->ie_ascq = 0xff;
6013 if (pg->mrie == SIEP_MRIE_UA) {
6014 ctl_est_ua_all(lun, -1, CTL_UA_IE);
6015 lun->ie_reported = 1;
6017 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6018 lun->ie_reported = -1;
6020 lun->ie_reportcnt = 1;
6021 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
6022 lun->ie_reportcnt++;
6023 t = scsi_4btoul(pg->interval_timer);
6024 if (t == 0 || t == UINT32_MAX)
6025 t = 3000; /* 5 min */
6026 callout_reset(&lun->ie_callout, t * hz / 10,
6032 lun->ie_reported = 1;
6033 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6034 lun->ie_reportcnt = UINT32_MAX;
6035 callout_stop(&lun->ie_callout);
6037 mtx_unlock(&lun->lun_lock);
6038 return (CTL_RETVAL_COMPLETE);
6042 ctl_do_mode_select(union ctl_io *io)
6044 struct ctl_lun *lun = CTL_LUN(io);
6045 struct scsi_mode_page_header *page_header;
6046 struct ctl_page_index *page_index;
6047 struct ctl_scsiio *ctsio;
6048 int page_len, page_len_offset, page_len_size;
6049 union ctl_modepage_info *modepage_info;
6050 uint16_t *len_left, *len_used;
6053 ctsio = &io->scsiio;
6057 modepage_info = (union ctl_modepage_info *)
6058 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6059 len_left = &modepage_info->header.len_left;
6060 len_used = &modepage_info->header.len_used;
6064 page_header = (struct scsi_mode_page_header *)
6065 (ctsio->kern_data_ptr + *len_used);
6067 if (*len_left == 0) {
6068 free(ctsio->kern_data_ptr, M_CTL);
6069 ctl_set_success(ctsio);
6070 ctl_done((union ctl_io *)ctsio);
6071 return (CTL_RETVAL_COMPLETE);
6072 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6074 free(ctsio->kern_data_ptr, M_CTL);
6075 ctl_set_param_len_error(ctsio);
6076 ctl_done((union ctl_io *)ctsio);
6077 return (CTL_RETVAL_COMPLETE);
6079 } else if ((page_header->page_code & SMPH_SPF)
6080 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6082 free(ctsio->kern_data_ptr, M_CTL);
6083 ctl_set_param_len_error(ctsio);
6084 ctl_done((union ctl_io *)ctsio);
6085 return (CTL_RETVAL_COMPLETE);
6090 * XXX KDM should we do something with the block descriptor?
6092 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6093 page_index = &lun->mode_pages.index[i];
6094 if (lun->be_lun->lun_type == T_DIRECT &&
6095 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6097 if (lun->be_lun->lun_type == T_PROCESSOR &&
6098 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6100 if (lun->be_lun->lun_type == T_CDROM &&
6101 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6104 if ((page_index->page_code & SMPH_PC_MASK) !=
6105 (page_header->page_code & SMPH_PC_MASK))
6109 * If neither page has a subpage code, then we've got a
6112 if (((page_index->page_code & SMPH_SPF) == 0)
6113 && ((page_header->page_code & SMPH_SPF) == 0)) {
6114 page_len = page_header->page_length;
6119 * If both pages have subpages, then the subpage numbers
6122 if ((page_index->page_code & SMPH_SPF)
6123 && (page_header->page_code & SMPH_SPF)) {
6124 struct scsi_mode_page_header_sp *sph;
6126 sph = (struct scsi_mode_page_header_sp *)page_header;
6127 if (page_index->subpage == sph->subpage) {
6128 page_len = scsi_2btoul(sph->page_length);
6135 * If we couldn't find the page, or if we don't have a mode select
6136 * handler for it, send back an error to the user.
6138 if ((i >= CTL_NUM_MODE_PAGES)
6139 || (page_index->select_handler == NULL)) {
6140 ctl_set_invalid_field(ctsio,
6143 /*field*/ *len_used,
6146 free(ctsio->kern_data_ptr, M_CTL);
6147 ctl_done((union ctl_io *)ctsio);
6148 return (CTL_RETVAL_COMPLETE);
6151 if (page_index->page_code & SMPH_SPF) {
6152 page_len_offset = 2;
6156 page_len_offset = 1;
6160 * If the length the initiator gives us isn't the one we specify in
6161 * the mode page header, or if they didn't specify enough data in
6162 * the CDB to avoid truncating this page, kick out the request.
6164 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6165 ctl_set_invalid_field(ctsio,
6168 /*field*/ *len_used + page_len_offset,
6171 free(ctsio->kern_data_ptr, M_CTL);
6172 ctl_done((union ctl_io *)ctsio);
6173 return (CTL_RETVAL_COMPLETE);
6175 if (*len_left < page_index->page_len) {
6176 free(ctsio->kern_data_ptr, M_CTL);
6177 ctl_set_param_len_error(ctsio);
6178 ctl_done((union ctl_io *)ctsio);
6179 return (CTL_RETVAL_COMPLETE);
6183 * Run through the mode page, checking to make sure that the bits
6184 * the user changed are actually legal for him to change.
6186 for (i = 0; i < page_index->page_len; i++) {
6187 uint8_t *user_byte, *change_mask, *current_byte;
6191 user_byte = (uint8_t *)page_header + i;
6192 change_mask = page_index->page_data +
6193 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6194 current_byte = page_index->page_data +
6195 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6198 * Check to see whether the user set any bits in this byte
6199 * that he is not allowed to set.
6201 if ((*user_byte & ~(*change_mask)) ==
6202 (*current_byte & ~(*change_mask)))
6206 * Go through bit by bit to determine which one is illegal.
6209 for (j = 7; j >= 0; j--) {
6210 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6211 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6216 ctl_set_invalid_field(ctsio,
6219 /*field*/ *len_used + i,
6222 free(ctsio->kern_data_ptr, M_CTL);
6223 ctl_done((union ctl_io *)ctsio);
6224 return (CTL_RETVAL_COMPLETE);
6228 * Decrement these before we call the page handler, since we may
6229 * end up getting called back one way or another before the handler
6230 * returns to this context.
6232 *len_left -= page_index->page_len;
6233 *len_used += page_index->page_len;
6235 retval = page_index->select_handler(ctsio, page_index,
6236 (uint8_t *)page_header);
6239 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6240 * wait until this queued command completes to finish processing
6241 * the mode page. If it returns anything other than
6242 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6243 * already set the sense information, freed the data pointer, and
6244 * completed the io for us.
6246 if (retval != CTL_RETVAL_COMPLETE)
6247 goto bailout_no_done;
6250 * If the initiator sent us more than one page, parse the next one.
6255 ctl_set_success(ctsio);
6256 free(ctsio->kern_data_ptr, M_CTL);
6257 ctl_done((union ctl_io *)ctsio);
6261 return (CTL_RETVAL_COMPLETE);
6266 ctl_mode_select(struct ctl_scsiio *ctsio)
6268 struct ctl_lun *lun = CTL_LUN(ctsio);
6269 union ctl_modepage_info *modepage_info;
6270 int bd_len, i, header_size, param_len, rtd;
6273 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6274 switch (ctsio->cdb[0]) {
6275 case MODE_SELECT_6: {
6276 struct scsi_mode_select_6 *cdb;
6278 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6280 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6281 param_len = cdb->length;
6282 header_size = sizeof(struct scsi_mode_header_6);
6285 case MODE_SELECT_10: {
6286 struct scsi_mode_select_10 *cdb;
6288 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6290 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6291 param_len = scsi_2btoul(cdb->length);
6292 header_size = sizeof(struct scsi_mode_header_10);
6296 ctl_set_invalid_opcode(ctsio);
6297 ctl_done((union ctl_io *)ctsio);
6298 return (CTL_RETVAL_COMPLETE);
6302 if (param_len != 0) {
6303 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6304 /*command*/ 1, /*field*/ 0,
6305 /*bit_valid*/ 0, /*bit*/ 0);
6306 ctl_done((union ctl_io *)ctsio);
6307 return (CTL_RETVAL_COMPLETE);
6310 /* Revert to defaults. */
6311 ctl_init_page_index(lun);
6312 mtx_lock(&lun->lun_lock);
6313 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6314 mtx_unlock(&lun->lun_lock);
6315 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6316 ctl_isc_announce_mode(lun, -1,
6317 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6318 lun->mode_pages.index[i].subpage);
6320 ctl_set_success(ctsio);
6321 ctl_done((union ctl_io *)ctsio);
6322 return (CTL_RETVAL_COMPLETE);
6327 * "A parameter list length of zero indicates that the Data-Out Buffer
6328 * shall be empty. This condition shall not be considered as an error."
6330 if (param_len == 0) {
6331 ctl_set_success(ctsio);
6332 ctl_done((union ctl_io *)ctsio);
6333 return (CTL_RETVAL_COMPLETE);
6337 * Since we'll hit this the first time through, prior to
6338 * allocation, we don't need to free a data buffer here.
6340 if (param_len < header_size) {
6341 ctl_set_param_len_error(ctsio);
6342 ctl_done((union ctl_io *)ctsio);
6343 return (CTL_RETVAL_COMPLETE);
6347 * Allocate the data buffer and grab the user's data. In theory,
6348 * we shouldn't have to sanity check the parameter list length here
6349 * because the maximum size is 64K. We should be able to malloc
6350 * that much without too many problems.
6352 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6353 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6354 ctsio->kern_data_len = param_len;
6355 ctsio->kern_total_len = param_len;
6356 ctsio->kern_rel_offset = 0;
6357 ctsio->kern_sg_entries = 0;
6358 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6359 ctsio->be_move_done = ctl_config_move_done;
6360 ctl_datamove((union ctl_io *)ctsio);
6362 return (CTL_RETVAL_COMPLETE);
6365 switch (ctsio->cdb[0]) {
6366 case MODE_SELECT_6: {
6367 struct scsi_mode_header_6 *mh6;
6369 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6370 bd_len = mh6->blk_desc_len;
6373 case MODE_SELECT_10: {
6374 struct scsi_mode_header_10 *mh10;
6376 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6377 bd_len = scsi_2btoul(mh10->blk_desc_len);
6381 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6384 if (param_len < (header_size + bd_len)) {
6385 free(ctsio->kern_data_ptr, M_CTL);
6386 ctl_set_param_len_error(ctsio);
6387 ctl_done((union ctl_io *)ctsio);
6388 return (CTL_RETVAL_COMPLETE);
6392 * Set the IO_CONT flag, so that if this I/O gets passed to
6393 * ctl_config_write_done(), it'll get passed back to
6394 * ctl_do_mode_select() for further processing, or completion if
6397 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6398 ctsio->io_cont = ctl_do_mode_select;
6400 modepage_info = (union ctl_modepage_info *)
6401 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6402 memset(modepage_info, 0, sizeof(*modepage_info));
6403 modepage_info->header.len_left = param_len - header_size - bd_len;
6404 modepage_info->header.len_used = header_size + bd_len;
6406 return (ctl_do_mode_select((union ctl_io *)ctsio));
6410 ctl_mode_sense(struct ctl_scsiio *ctsio)
6412 struct ctl_lun *lun = CTL_LUN(ctsio);
6413 int pc, page_code, dbd, subpage;
6414 int alloc_len, page_len, header_len, total_len;
6415 struct scsi_mode_block_descr *block_desc;
6416 struct ctl_page_index *page_index;
6421 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6423 switch (ctsio->cdb[0]) {
6424 case MODE_SENSE_6: {
6425 struct scsi_mode_sense_6 *cdb;
6427 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6429 header_len = sizeof(struct scsi_mode_hdr_6);
6430 if (cdb->byte2 & SMS_DBD)
6433 header_len += sizeof(struct scsi_mode_block_descr);
6435 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6436 page_code = cdb->page & SMS_PAGE_CODE;
6437 subpage = cdb->subpage;
6438 alloc_len = cdb->length;
6441 case MODE_SENSE_10: {
6442 struct scsi_mode_sense_10 *cdb;
6444 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6446 header_len = sizeof(struct scsi_mode_hdr_10);
6448 if (cdb->byte2 & SMS_DBD)
6451 header_len += sizeof(struct scsi_mode_block_descr);
6452 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6453 page_code = cdb->page & SMS_PAGE_CODE;
6454 subpage = cdb->subpage;
6455 alloc_len = scsi_2btoul(cdb->length);
6459 ctl_set_invalid_opcode(ctsio);
6460 ctl_done((union ctl_io *)ctsio);
6461 return (CTL_RETVAL_COMPLETE);
6462 break; /* NOTREACHED */
6466 * We have to make a first pass through to calculate the size of
6467 * the pages that match the user's query. Then we allocate enough
6468 * memory to hold it, and actually copy the data into the buffer.
6470 switch (page_code) {
6471 case SMS_ALL_PAGES_PAGE: {
6477 * At the moment, values other than 0 and 0xff here are
6478 * reserved according to SPC-3.
6480 if ((subpage != SMS_SUBPAGE_PAGE_0)
6481 && (subpage != SMS_SUBPAGE_ALL)) {
6482 ctl_set_invalid_field(ctsio,
6488 ctl_done((union ctl_io *)ctsio);
6489 return (CTL_RETVAL_COMPLETE);
6492 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6493 page_index = &lun->mode_pages.index[i];
6495 /* Make sure the page is supported for this dev type */
6496 if (lun->be_lun->lun_type == T_DIRECT &&
6497 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6499 if (lun->be_lun->lun_type == T_PROCESSOR &&
6500 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6502 if (lun->be_lun->lun_type == T_CDROM &&
6503 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6507 * We don't use this subpage if the user didn't
6508 * request all subpages.
6510 if ((page_index->subpage != 0)
6511 && (subpage == SMS_SUBPAGE_PAGE_0))
6515 printf("found page %#x len %d\n",
6516 page_index->page_code & SMPH_PC_MASK,
6517 page_index->page_len);
6519 page_len += page_index->page_len;
6528 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6529 page_index = &lun->mode_pages.index[i];
6531 /* Make sure the page is supported for this dev type */
6532 if (lun->be_lun->lun_type == T_DIRECT &&
6533 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6535 if (lun->be_lun->lun_type == T_PROCESSOR &&
6536 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6538 if (lun->be_lun->lun_type == T_CDROM &&
6539 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6542 /* Look for the right page code */
6543 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6546 /* Look for the right subpage or the subpage wildcard*/
6547 if ((page_index->subpage != subpage)
6548 && (subpage != SMS_SUBPAGE_ALL))
6552 printf("found page %#x len %d\n",
6553 page_index->page_code & SMPH_PC_MASK,
6554 page_index->page_len);
6557 page_len += page_index->page_len;
6560 if (page_len == 0) {
6561 ctl_set_invalid_field(ctsio,
6567 ctl_done((union ctl_io *)ctsio);
6568 return (CTL_RETVAL_COMPLETE);
6574 total_len = header_len + page_len;
6576 printf("header_len = %d, page_len = %d, total_len = %d\n",
6577 header_len, page_len, total_len);
6580 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6581 ctsio->kern_sg_entries = 0;
6582 ctsio->kern_rel_offset = 0;
6583 ctsio->kern_data_len = min(total_len, alloc_len);
6584 ctsio->kern_total_len = ctsio->kern_data_len;
6586 switch (ctsio->cdb[0]) {
6587 case MODE_SENSE_6: {
6588 struct scsi_mode_hdr_6 *header;
6590 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6592 header->datalen = MIN(total_len - 1, 254);
6593 if (lun->be_lun->lun_type == T_DIRECT) {
6594 header->dev_specific = 0x10; /* DPOFUA */
6595 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6596 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6597 header->dev_specific |= 0x80; /* WP */
6600 header->block_descr_len = 0;
6602 header->block_descr_len =
6603 sizeof(struct scsi_mode_block_descr);
6604 block_desc = (struct scsi_mode_block_descr *)&header[1];
6607 case MODE_SENSE_10: {
6608 struct scsi_mode_hdr_10 *header;
6611 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6613 datalen = MIN(total_len - 2, 65533);
6614 scsi_ulto2b(datalen, header->datalen);
6615 if (lun->be_lun->lun_type == T_DIRECT) {
6616 header->dev_specific = 0x10; /* DPOFUA */
6617 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6618 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6619 header->dev_specific |= 0x80; /* WP */
6622 scsi_ulto2b(0, header->block_descr_len);
6624 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6625 header->block_descr_len);
6626 block_desc = (struct scsi_mode_block_descr *)&header[1];
6630 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6634 * If we've got a disk, use its blocksize in the block
6635 * descriptor. Otherwise, just set it to 0.
6638 if (lun->be_lun->lun_type == T_DIRECT)
6639 scsi_ulto3b(lun->be_lun->blocksize,
6640 block_desc->block_len);
6642 scsi_ulto3b(0, block_desc->block_len);
6645 switch (page_code) {
6646 case SMS_ALL_PAGES_PAGE: {
6649 data_used = header_len;
6650 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6651 struct ctl_page_index *page_index;
6653 page_index = &lun->mode_pages.index[i];
6654 if (lun->be_lun->lun_type == T_DIRECT &&
6655 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6657 if (lun->be_lun->lun_type == T_PROCESSOR &&
6658 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6660 if (lun->be_lun->lun_type == T_CDROM &&
6661 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6665 * We don't use this subpage if the user didn't
6666 * request all subpages. We already checked (above)
6667 * to make sure the user only specified a subpage
6668 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6670 if ((page_index->subpage != 0)
6671 && (subpage == SMS_SUBPAGE_PAGE_0))
6675 * Call the handler, if it exists, to update the
6676 * page to the latest values.
6678 if (page_index->sense_handler != NULL)
6679 page_index->sense_handler(ctsio, page_index,pc);
6681 memcpy(ctsio->kern_data_ptr + data_used,
6682 page_index->page_data +
6683 (page_index->page_len * pc),
6684 page_index->page_len);
6685 data_used += page_index->page_len;
6692 data_used = header_len;
6694 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6695 struct ctl_page_index *page_index;
6697 page_index = &lun->mode_pages.index[i];
6699 /* Look for the right page code */
6700 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6703 /* Look for the right subpage or the subpage wildcard*/
6704 if ((page_index->subpage != subpage)
6705 && (subpage != SMS_SUBPAGE_ALL))
6708 /* Make sure the page is supported for this dev type */
6709 if (lun->be_lun->lun_type == T_DIRECT &&
6710 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6712 if (lun->be_lun->lun_type == T_PROCESSOR &&
6713 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6715 if (lun->be_lun->lun_type == T_CDROM &&
6716 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6720 * Call the handler, if it exists, to update the
6721 * page to the latest values.
6723 if (page_index->sense_handler != NULL)
6724 page_index->sense_handler(ctsio, page_index,pc);
6726 memcpy(ctsio->kern_data_ptr + data_used,
6727 page_index->page_data +
6728 (page_index->page_len * pc),
6729 page_index->page_len);
6730 data_used += page_index->page_len;
6736 ctl_set_success(ctsio);
6737 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6738 ctsio->be_move_done = ctl_config_move_done;
6739 ctl_datamove((union ctl_io *)ctsio);
6740 return (CTL_RETVAL_COMPLETE);
6744 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6745 struct ctl_page_index *page_index,
6748 struct ctl_lun *lun = CTL_LUN(ctsio);
6749 struct scsi_log_param_header *phdr;
6753 data = page_index->page_data;
6755 if (lun->backend->lun_attr != NULL &&
6756 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6758 phdr = (struct scsi_log_param_header *)data;
6759 scsi_ulto2b(0x0001, phdr->param_code);
6760 phdr->param_control = SLP_LBIN | SLP_LP;
6761 phdr->param_len = 8;
6762 data = (uint8_t *)(phdr + 1);
6763 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6764 data[4] = 0x02; /* per-pool */
6765 data += phdr->param_len;
6768 if (lun->backend->lun_attr != NULL &&
6769 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6771 phdr = (struct scsi_log_param_header *)data;
6772 scsi_ulto2b(0x0002, phdr->param_code);
6773 phdr->param_control = SLP_LBIN | SLP_LP;
6774 phdr->param_len = 8;
6775 data = (uint8_t *)(phdr + 1);
6776 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6777 data[4] = 0x01; /* per-LUN */
6778 data += phdr->param_len;
6781 if (lun->backend->lun_attr != NULL &&
6782 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6784 phdr = (struct scsi_log_param_header *)data;
6785 scsi_ulto2b(0x00f1, phdr->param_code);
6786 phdr->param_control = SLP_LBIN | SLP_LP;
6787 phdr->param_len = 8;
6788 data = (uint8_t *)(phdr + 1);
6789 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6790 data[4] = 0x02; /* per-pool */
6791 data += phdr->param_len;
6794 if (lun->backend->lun_attr != NULL &&
6795 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6797 phdr = (struct scsi_log_param_header *)data;
6798 scsi_ulto2b(0x00f2, phdr->param_code);
6799 phdr->param_control = SLP_LBIN | SLP_LP;
6800 phdr->param_len = 8;
6801 data = (uint8_t *)(phdr + 1);
6802 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6803 data[4] = 0x02; /* per-pool */
6804 data += phdr->param_len;
6807 page_index->page_len = data - page_index->page_data;
6812 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6813 struct ctl_page_index *page_index,
6816 struct ctl_lun *lun = CTL_LUN(ctsio);
6817 struct stat_page *data;
6820 data = (struct stat_page *)page_index->page_data;
6822 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6823 data->sap.hdr.param_control = SLP_LBIN;
6824 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6825 sizeof(struct scsi_log_param_header);
6826 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6827 data->sap.read_num);
6828 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6829 data->sap.write_num);
6830 if (lun->be_lun->blocksize > 0) {
6831 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6832 lun->be_lun->blocksize, data->sap.recvieved_lba);
6833 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6834 lun->be_lun->blocksize, data->sap.transmitted_lba);
6836 t = &lun->stats.time[CTL_STATS_READ];
6837 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6838 data->sap.read_int);
6839 t = &lun->stats.time[CTL_STATS_WRITE];
6840 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6841 data->sap.write_int);
6842 scsi_u64to8b(0, data->sap.weighted_num);
6843 scsi_u64to8b(0, data->sap.weighted_int);
6844 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6845 data->it.hdr.param_control = SLP_LBIN;
6846 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6847 sizeof(struct scsi_log_param_header);
6849 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6851 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6852 data->it.hdr.param_control = SLP_LBIN;
6853 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6854 sizeof(struct scsi_log_param_header);
6855 scsi_ulto4b(3, data->ti.exponent);
6856 scsi_ulto4b(1, data->ti.integer);
6861 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6862 struct ctl_page_index *page_index,
6865 struct ctl_lun *lun = CTL_LUN(ctsio);
6866 struct scsi_log_informational_exceptions *data;
6868 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6870 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6871 data->hdr.param_control = SLP_LBIN;
6872 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6873 sizeof(struct scsi_log_param_header);
6874 data->ie_asc = lun->ie_asc;
6875 data->ie_ascq = lun->ie_ascq;
6876 data->temperature = 0xff;
6881 ctl_log_sense(struct ctl_scsiio *ctsio)
6883 struct ctl_lun *lun = CTL_LUN(ctsio);
6884 int i, pc, page_code, subpage;
6885 int alloc_len, total_len;
6886 struct ctl_page_index *page_index;
6887 struct scsi_log_sense *cdb;
6888 struct scsi_log_header *header;
6890 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6892 cdb = (struct scsi_log_sense *)ctsio->cdb;
6893 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6894 page_code = cdb->page & SLS_PAGE_CODE;
6895 subpage = cdb->subpage;
6896 alloc_len = scsi_2btoul(cdb->length);
6899 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6900 page_index = &lun->log_pages.index[i];
6902 /* Look for the right page code */
6903 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6906 /* Look for the right subpage or the subpage wildcard*/
6907 if (page_index->subpage != subpage)
6912 if (i >= CTL_NUM_LOG_PAGES) {
6913 ctl_set_invalid_field(ctsio,
6919 ctl_done((union ctl_io *)ctsio);
6920 return (CTL_RETVAL_COMPLETE);
6923 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6925 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6926 ctsio->kern_sg_entries = 0;
6927 ctsio->kern_rel_offset = 0;
6928 ctsio->kern_data_len = min(total_len, alloc_len);
6929 ctsio->kern_total_len = ctsio->kern_data_len;
6931 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6932 header->page = page_index->page_code;
6933 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6934 header->page |= SL_DS;
6935 if (page_index->subpage) {
6936 header->page |= SL_SPF;
6937 header->subpage = page_index->subpage;
6939 scsi_ulto2b(page_index->page_len, header->datalen);
6942 * Call the handler, if it exists, to update the
6943 * page to the latest values.
6945 if (page_index->sense_handler != NULL)
6946 page_index->sense_handler(ctsio, page_index, pc);
6948 memcpy(header + 1, page_index->page_data, page_index->page_len);
6950 ctl_set_success(ctsio);
6951 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6952 ctsio->be_move_done = ctl_config_move_done;
6953 ctl_datamove((union ctl_io *)ctsio);
6954 return (CTL_RETVAL_COMPLETE);
6958 ctl_read_capacity(struct ctl_scsiio *ctsio)
6960 struct ctl_lun *lun = CTL_LUN(ctsio);
6961 struct scsi_read_capacity *cdb;
6962 struct scsi_read_capacity_data *data;
6965 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6967 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6969 lba = scsi_4btoul(cdb->addr);
6970 if (((cdb->pmi & SRC_PMI) == 0)
6972 ctl_set_invalid_field(/*ctsio*/ ctsio,
6978 ctl_done((union ctl_io *)ctsio);
6979 return (CTL_RETVAL_COMPLETE);
6982 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6983 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6984 ctsio->kern_data_len = sizeof(*data);
6985 ctsio->kern_total_len = sizeof(*data);
6986 ctsio->kern_rel_offset = 0;
6987 ctsio->kern_sg_entries = 0;
6990 * If the maximum LBA is greater than 0xfffffffe, the user must
6991 * issue a SERVICE ACTION IN (16) command, with the read capacity
6992 * serivce action set.
6994 if (lun->be_lun->maxlba > 0xfffffffe)
6995 scsi_ulto4b(0xffffffff, data->addr);
6997 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7000 * XXX KDM this may not be 512 bytes...
7002 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7004 ctl_set_success(ctsio);
7005 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7006 ctsio->be_move_done = ctl_config_move_done;
7007 ctl_datamove((union ctl_io *)ctsio);
7008 return (CTL_RETVAL_COMPLETE);
7012 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7014 struct ctl_lun *lun = CTL_LUN(ctsio);
7015 struct scsi_read_capacity_16 *cdb;
7016 struct scsi_read_capacity_data_long *data;
7020 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7022 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7024 alloc_len = scsi_4btoul(cdb->alloc_len);
7025 lba = scsi_8btou64(cdb->addr);
7027 if ((cdb->reladr & SRC16_PMI)
7029 ctl_set_invalid_field(/*ctsio*/ ctsio,
7035 ctl_done((union ctl_io *)ctsio);
7036 return (CTL_RETVAL_COMPLETE);
7039 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7040 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7041 ctsio->kern_rel_offset = 0;
7042 ctsio->kern_sg_entries = 0;
7043 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
7044 ctsio->kern_total_len = ctsio->kern_data_len;
7046 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7047 /* XXX KDM this may not be 512 bytes... */
7048 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7049 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7050 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7051 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7052 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7054 ctl_set_success(ctsio);
7055 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7056 ctsio->be_move_done = ctl_config_move_done;
7057 ctl_datamove((union ctl_io *)ctsio);
7058 return (CTL_RETVAL_COMPLETE);
7062 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7064 struct ctl_lun *lun = CTL_LUN(ctsio);
7065 struct scsi_get_lba_status *cdb;
7066 struct scsi_get_lba_status_data *data;
7067 struct ctl_lba_len_flags *lbalen;
7069 uint32_t alloc_len, total_len;
7072 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7074 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7075 lba = scsi_8btou64(cdb->addr);
7076 alloc_len = scsi_4btoul(cdb->alloc_len);
7078 if (lba > lun->be_lun->maxlba) {
7079 ctl_set_lba_out_of_range(ctsio, lba);
7080 ctl_done((union ctl_io *)ctsio);
7081 return (CTL_RETVAL_COMPLETE);
7084 total_len = sizeof(*data) + sizeof(data->descr[0]);
7085 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7086 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7087 ctsio->kern_rel_offset = 0;
7088 ctsio->kern_sg_entries = 0;
7089 ctsio->kern_data_len = min(total_len, alloc_len);
7090 ctsio->kern_total_len = ctsio->kern_data_len;
7092 /* Fill dummy data in case backend can't tell anything. */
7093 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7094 scsi_u64to8b(lba, data->descr[0].addr);
7095 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7096 data->descr[0].length);
7097 data->descr[0].status = 0; /* Mapped or unknown. */
7099 ctl_set_success(ctsio);
7100 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7101 ctsio->be_move_done = ctl_config_move_done;
7103 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7105 lbalen->len = total_len;
7107 retval = lun->backend->config_read((union ctl_io *)ctsio);
7112 ctl_read_defect(struct ctl_scsiio *ctsio)
7114 struct scsi_read_defect_data_10 *ccb10;
7115 struct scsi_read_defect_data_12 *ccb12;
7116 struct scsi_read_defect_data_hdr_10 *data10;
7117 struct scsi_read_defect_data_hdr_12 *data12;
7118 uint32_t alloc_len, data_len;
7121 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7123 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7124 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7125 format = ccb10->format;
7126 alloc_len = scsi_2btoul(ccb10->alloc_length);
7127 data_len = sizeof(*data10);
7129 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7130 format = ccb12->format;
7131 alloc_len = scsi_4btoul(ccb12->alloc_length);
7132 data_len = sizeof(*data12);
7134 if (alloc_len == 0) {
7135 ctl_set_success(ctsio);
7136 ctl_done((union ctl_io *)ctsio);
7137 return (CTL_RETVAL_COMPLETE);
7140 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7141 ctsio->kern_rel_offset = 0;
7142 ctsio->kern_sg_entries = 0;
7143 ctsio->kern_data_len = min(data_len, alloc_len);
7144 ctsio->kern_total_len = ctsio->kern_data_len;
7146 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7147 data10 = (struct scsi_read_defect_data_hdr_10 *)
7148 ctsio->kern_data_ptr;
7149 data10->format = format;
7150 scsi_ulto2b(0, data10->length);
7152 data12 = (struct scsi_read_defect_data_hdr_12 *)
7153 ctsio->kern_data_ptr;
7154 data12->format = format;
7155 scsi_ulto2b(0, data12->generation);
7156 scsi_ulto4b(0, data12->length);
7159 ctl_set_success(ctsio);
7160 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7161 ctsio->be_move_done = ctl_config_move_done;
7162 ctl_datamove((union ctl_io *)ctsio);
7163 return (CTL_RETVAL_COMPLETE);
7167 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7169 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7170 struct ctl_lun *lun = CTL_LUN(ctsio);
7171 struct scsi_maintenance_in *cdb;
7173 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7174 int num_ha_groups, num_target_ports, shared_group;
7175 struct ctl_port *port;
7176 struct scsi_target_group_data *rtg_ptr;
7177 struct scsi_target_group_data_extended *rtg_ext_ptr;
7178 struct scsi_target_port_group_descriptor *tpg_desc;
7180 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7182 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7183 retval = CTL_RETVAL_COMPLETE;
7185 switch (cdb->byte2 & STG_PDF_MASK) {
7186 case STG_PDF_LENGTH:
7189 case STG_PDF_EXTENDED:
7193 ctl_set_invalid_field(/*ctsio*/ ctsio,
7199 ctl_done((union ctl_io *)ctsio);
7203 num_target_ports = 0;
7204 shared_group = (softc->is_single != 0);
7205 mtx_lock(&softc->ctl_lock);
7206 STAILQ_FOREACH(port, &softc->port_list, links) {
7207 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7209 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7212 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7215 mtx_unlock(&softc->ctl_lock);
7216 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7219 total_len = sizeof(struct scsi_target_group_data_extended);
7221 total_len = sizeof(struct scsi_target_group_data);
7222 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7223 (shared_group + num_ha_groups) +
7224 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7226 alloc_len = scsi_4btoul(cdb->length);
7228 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7229 ctsio->kern_sg_entries = 0;
7230 ctsio->kern_rel_offset = 0;
7231 ctsio->kern_data_len = min(total_len, alloc_len);
7232 ctsio->kern_total_len = ctsio->kern_data_len;
7235 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7236 ctsio->kern_data_ptr;
7237 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7238 rtg_ext_ptr->format_type = 0x10;
7239 rtg_ext_ptr->implicit_transition_time = 0;
7240 tpg_desc = &rtg_ext_ptr->groups[0];
7242 rtg_ptr = (struct scsi_target_group_data *)
7243 ctsio->kern_data_ptr;
7244 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7245 tpg_desc = &rtg_ptr->groups[0];
7248 mtx_lock(&softc->ctl_lock);
7249 pg = softc->port_min / softc->port_cnt;
7250 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7251 /* Some shelf is known to be primary. */
7252 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7253 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7254 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7255 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7256 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7257 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7259 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7260 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7261 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7264 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7267 /* No known primary shelf. */
7268 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7269 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7270 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7271 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7272 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7273 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7275 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7279 tpg_desc->pref_state = ts;
7280 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7281 TPG_U_SUP | TPG_T_SUP;
7282 scsi_ulto2b(1, tpg_desc->target_port_group);
7283 tpg_desc->status = TPG_IMPLICIT;
7285 STAILQ_FOREACH(port, &softc->port_list, links) {
7286 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7288 if (!softc->is_single &&
7289 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7291 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7293 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7294 relative_target_port_identifier);
7297 tpg_desc->target_port_count = pc;
7298 tpg_desc = (struct scsi_target_port_group_descriptor *)
7299 &tpg_desc->descriptors[pc];
7301 for (g = 0; g < num_ha_groups; g++) {
7302 tpg_desc->pref_state = (g == pg) ? ts : os;
7303 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7304 TPG_U_SUP | TPG_T_SUP;
7305 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7306 tpg_desc->status = TPG_IMPLICIT;
7308 STAILQ_FOREACH(port, &softc->port_list, links) {
7309 if (port->targ_port < g * softc->port_cnt ||
7310 port->targ_port >= (g + 1) * softc->port_cnt)
7312 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7314 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7316 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7318 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7319 relative_target_port_identifier);
7322 tpg_desc->target_port_count = pc;
7323 tpg_desc = (struct scsi_target_port_group_descriptor *)
7324 &tpg_desc->descriptors[pc];
7326 mtx_unlock(&softc->ctl_lock);
7328 ctl_set_success(ctsio);
7329 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7330 ctsio->be_move_done = ctl_config_move_done;
7331 ctl_datamove((union ctl_io *)ctsio);
7336 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7338 struct ctl_lun *lun = CTL_LUN(ctsio);
7339 struct scsi_report_supported_opcodes *cdb;
7340 const struct ctl_cmd_entry *entry, *sentry;
7341 struct scsi_report_supported_opcodes_all *all;
7342 struct scsi_report_supported_opcodes_descr *descr;
7343 struct scsi_report_supported_opcodes_one *one;
7345 int alloc_len, total_len;
7346 int opcode, service_action, i, j, num;
7348 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7350 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7351 retval = CTL_RETVAL_COMPLETE;
7353 opcode = cdb->requested_opcode;
7354 service_action = scsi_2btoul(cdb->requested_service_action);
7355 switch (cdb->options & RSO_OPTIONS_MASK) {
7356 case RSO_OPTIONS_ALL:
7358 for (i = 0; i < 256; i++) {
7359 entry = &ctl_cmd_table[i];
7360 if (entry->flags & CTL_CMD_FLAG_SA5) {
7361 for (j = 0; j < 32; j++) {
7362 sentry = &((const struct ctl_cmd_entry *)
7364 if (ctl_cmd_applicable(
7365 lun->be_lun->lun_type, sentry))
7369 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7374 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7375 num * sizeof(struct scsi_report_supported_opcodes_descr);
7377 case RSO_OPTIONS_OC:
7378 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7379 ctl_set_invalid_field(/*ctsio*/ ctsio,
7385 ctl_done((union ctl_io *)ctsio);
7386 return (CTL_RETVAL_COMPLETE);
7388 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7390 case RSO_OPTIONS_OC_SA:
7391 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7392 service_action >= 32) {
7393 ctl_set_invalid_field(/*ctsio*/ ctsio,
7399 ctl_done((union ctl_io *)ctsio);
7400 return (CTL_RETVAL_COMPLETE);
7403 case RSO_OPTIONS_OC_ASA:
7404 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7407 ctl_set_invalid_field(/*ctsio*/ ctsio,
7413 ctl_done((union ctl_io *)ctsio);
7414 return (CTL_RETVAL_COMPLETE);
7417 alloc_len = scsi_4btoul(cdb->length);
7419 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7420 ctsio->kern_sg_entries = 0;
7421 ctsio->kern_rel_offset = 0;
7422 ctsio->kern_data_len = min(total_len, alloc_len);
7423 ctsio->kern_total_len = ctsio->kern_data_len;
7425 switch (cdb->options & RSO_OPTIONS_MASK) {
7426 case RSO_OPTIONS_ALL:
7427 all = (struct scsi_report_supported_opcodes_all *)
7428 ctsio->kern_data_ptr;
7430 for (i = 0; i < 256; i++) {
7431 entry = &ctl_cmd_table[i];
7432 if (entry->flags & CTL_CMD_FLAG_SA5) {
7433 for (j = 0; j < 32; j++) {
7434 sentry = &((const struct ctl_cmd_entry *)
7436 if (!ctl_cmd_applicable(
7437 lun->be_lun->lun_type, sentry))
7439 descr = &all->descr[num++];
7441 scsi_ulto2b(j, descr->service_action);
7442 descr->flags = RSO_SERVACTV;
7443 scsi_ulto2b(sentry->length,
7447 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7450 descr = &all->descr[num++];
7452 scsi_ulto2b(0, descr->service_action);
7454 scsi_ulto2b(entry->length, descr->cdb_length);
7458 num * sizeof(struct scsi_report_supported_opcodes_descr),
7461 case RSO_OPTIONS_OC:
7462 one = (struct scsi_report_supported_opcodes_one *)
7463 ctsio->kern_data_ptr;
7464 entry = &ctl_cmd_table[opcode];
7466 case RSO_OPTIONS_OC_SA:
7467 one = (struct scsi_report_supported_opcodes_one *)
7468 ctsio->kern_data_ptr;
7469 entry = &ctl_cmd_table[opcode];
7470 entry = &((const struct ctl_cmd_entry *)
7471 entry->execute)[service_action];
7473 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7475 scsi_ulto2b(entry->length, one->cdb_length);
7476 one->cdb_usage[0] = opcode;
7477 memcpy(&one->cdb_usage[1], entry->usage,
7482 case RSO_OPTIONS_OC_ASA:
7483 one = (struct scsi_report_supported_opcodes_one *)
7484 ctsio->kern_data_ptr;
7485 entry = &ctl_cmd_table[opcode];
7486 if (entry->flags & CTL_CMD_FLAG_SA5) {
7487 entry = &((const struct ctl_cmd_entry *)
7488 entry->execute)[service_action];
7489 } else if (service_action != 0) {
7496 ctl_set_success(ctsio);
7497 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7498 ctsio->be_move_done = ctl_config_move_done;
7499 ctl_datamove((union ctl_io *)ctsio);
7504 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7506 struct scsi_report_supported_tmf *cdb;
7507 struct scsi_report_supported_tmf_ext_data *data;
7509 int alloc_len, total_len;
7511 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7513 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7515 retval = CTL_RETVAL_COMPLETE;
7517 if (cdb->options & RST_REPD)
7518 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7520 total_len = sizeof(struct scsi_report_supported_tmf_data);
7521 alloc_len = scsi_4btoul(cdb->length);
7523 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7524 ctsio->kern_sg_entries = 0;
7525 ctsio->kern_rel_offset = 0;
7526 ctsio->kern_data_len = min(total_len, alloc_len);
7527 ctsio->kern_total_len = ctsio->kern_data_len;
7529 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7530 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7532 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7533 data->length = total_len - 4;
7535 ctl_set_success(ctsio);
7536 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7537 ctsio->be_move_done = ctl_config_move_done;
7538 ctl_datamove((union ctl_io *)ctsio);
7543 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7545 struct scsi_report_timestamp *cdb;
7546 struct scsi_report_timestamp_data *data;
7550 int alloc_len, total_len;
7552 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7554 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7556 retval = CTL_RETVAL_COMPLETE;
7558 total_len = sizeof(struct scsi_report_timestamp_data);
7559 alloc_len = scsi_4btoul(cdb->length);
7561 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7562 ctsio->kern_sg_entries = 0;
7563 ctsio->kern_rel_offset = 0;
7564 ctsio->kern_data_len = min(total_len, alloc_len);
7565 ctsio->kern_total_len = ctsio->kern_data_len;
7567 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7568 scsi_ulto2b(sizeof(*data) - 2, data->length);
7569 data->origin = RTS_ORIG_OUTSIDE;
7571 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7572 scsi_ulto4b(timestamp >> 16, data->timestamp);
7573 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7575 ctl_set_success(ctsio);
7576 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7577 ctsio->be_move_done = ctl_config_move_done;
7578 ctl_datamove((union ctl_io *)ctsio);
7583 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7585 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7586 struct ctl_lun *lun = CTL_LUN(ctsio);
7587 struct scsi_per_res_in *cdb;
7588 int alloc_len, total_len = 0;
7589 /* struct scsi_per_res_in_rsrv in_data; */
7592 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7594 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7596 alloc_len = scsi_2btoul(cdb->length);
7599 mtx_lock(&lun->lun_lock);
7600 switch (cdb->action) {
7601 case SPRI_RK: /* read keys */
7602 total_len = sizeof(struct scsi_per_res_in_keys) +
7604 sizeof(struct scsi_per_res_key);
7606 case SPRI_RR: /* read reservation */
7607 if (lun->flags & CTL_LUN_PR_RESERVED)
7608 total_len = sizeof(struct scsi_per_res_in_rsrv);
7610 total_len = sizeof(struct scsi_per_res_in_header);
7612 case SPRI_RC: /* report capabilities */
7613 total_len = sizeof(struct scsi_per_res_cap);
7615 case SPRI_RS: /* read full status */
7616 total_len = sizeof(struct scsi_per_res_in_header) +
7617 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7621 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7623 mtx_unlock(&lun->lun_lock);
7625 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7626 ctsio->kern_rel_offset = 0;
7627 ctsio->kern_sg_entries = 0;
7628 ctsio->kern_data_len = min(total_len, alloc_len);
7629 ctsio->kern_total_len = ctsio->kern_data_len;
7631 mtx_lock(&lun->lun_lock);
7632 switch (cdb->action) {
7633 case SPRI_RK: { // read keys
7634 struct scsi_per_res_in_keys *res_keys;
7637 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7640 * We had to drop the lock to allocate our buffer, which
7641 * leaves time for someone to come in with another
7642 * persistent reservation. (That is unlikely, though,
7643 * since this should be the only persistent reservation
7644 * command active right now.)
7646 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7647 (lun->pr_key_count *
7648 sizeof(struct scsi_per_res_key)))){
7649 mtx_unlock(&lun->lun_lock);
7650 free(ctsio->kern_data_ptr, M_CTL);
7651 printf("%s: reservation length changed, retrying\n",
7656 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7658 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7659 lun->pr_key_count, res_keys->header.length);
7661 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7662 if ((key = ctl_get_prkey(lun, i)) == 0)
7666 * We used lun->pr_key_count to calculate the
7667 * size to allocate. If it turns out the number of
7668 * initiators with the registered flag set is
7669 * larger than that (i.e. they haven't been kept in
7670 * sync), we've got a problem.
7672 if (key_count >= lun->pr_key_count) {
7676 scsi_u64to8b(key, res_keys->keys[key_count].key);
7681 case SPRI_RR: { // read reservation
7682 struct scsi_per_res_in_rsrv *res;
7683 int tmp_len, header_only;
7685 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7687 scsi_ulto4b(lun->pr_generation, res->header.generation);
7689 if (lun->flags & CTL_LUN_PR_RESERVED)
7691 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7692 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7693 res->header.length);
7696 tmp_len = sizeof(struct scsi_per_res_in_header);
7697 scsi_ulto4b(0, res->header.length);
7702 * We had to drop the lock to allocate our buffer, which
7703 * leaves time for someone to come in with another
7704 * persistent reservation. (That is unlikely, though,
7705 * since this should be the only persistent reservation
7706 * command active right now.)
7708 if (tmp_len != total_len) {
7709 mtx_unlock(&lun->lun_lock);
7710 free(ctsio->kern_data_ptr, M_CTL);
7711 printf("%s: reservation status changed, retrying\n",
7717 * No reservation held, so we're done.
7719 if (header_only != 0)
7723 * If the registration is an All Registrants type, the key
7724 * is 0, since it doesn't really matter.
7726 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7727 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7728 res->data.reservation);
7730 res->data.scopetype = lun->pr_res_type;
7733 case SPRI_RC: //report capabilities
7735 struct scsi_per_res_cap *res_cap;
7738 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7739 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7740 res_cap->flags1 = SPRI_CRH;
7741 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7742 type_mask = SPRI_TM_WR_EX_AR |
7748 scsi_ulto2b(type_mask, res_cap->type_mask);
7751 case SPRI_RS: { // read full status
7752 struct scsi_per_res_in_full *res_status;
7753 struct scsi_per_res_in_full_desc *res_desc;
7754 struct ctl_port *port;
7757 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7760 * We had to drop the lock to allocate our buffer, which
7761 * leaves time for someone to come in with another
7762 * persistent reservation. (That is unlikely, though,
7763 * since this should be the only persistent reservation
7764 * command active right now.)
7766 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7767 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7768 lun->pr_key_count)){
7769 mtx_unlock(&lun->lun_lock);
7770 free(ctsio->kern_data_ptr, M_CTL);
7771 printf("%s: reservation length changed, retrying\n",
7776 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7778 res_desc = &res_status->desc[0];
7779 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7780 if ((key = ctl_get_prkey(lun, i)) == 0)
7783 scsi_u64to8b(key, res_desc->res_key.key);
7784 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7785 (lun->pr_res_idx == i ||
7786 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7787 res_desc->flags = SPRI_FULL_R_HOLDER;
7788 res_desc->scopetype = lun->pr_res_type;
7790 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7791 res_desc->rel_trgt_port_id);
7793 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7795 len = ctl_create_iid(port,
7796 i % CTL_MAX_INIT_PER_PORT,
7797 res_desc->transport_id);
7798 scsi_ulto4b(len, res_desc->additional_length);
7799 res_desc = (struct scsi_per_res_in_full_desc *)
7800 &res_desc->transport_id[len];
7802 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7803 res_status->header.length);
7807 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7809 mtx_unlock(&lun->lun_lock);
7811 ctl_set_success(ctsio);
7812 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7813 ctsio->be_move_done = ctl_config_move_done;
7814 ctl_datamove((union ctl_io *)ctsio);
7815 return (CTL_RETVAL_COMPLETE);
7819 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7823 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7824 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7825 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7826 struct scsi_per_res_out_parms* param)
7828 union ctl_ha_msg persis_io;
7831 mtx_lock(&lun->lun_lock);
7832 if (sa_res_key == 0) {
7833 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7834 /* validate scope and type */
7835 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7837 mtx_unlock(&lun->lun_lock);
7838 ctl_set_invalid_field(/*ctsio*/ ctsio,
7844 ctl_done((union ctl_io *)ctsio);
7848 if (type>8 || type==2 || type==4 || type==0) {
7849 mtx_unlock(&lun->lun_lock);
7850 ctl_set_invalid_field(/*ctsio*/ ctsio,
7856 ctl_done((union ctl_io *)ctsio);
7861 * Unregister everybody else and build UA for
7864 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7865 if (i == residx || ctl_get_prkey(lun, i) == 0)
7868 ctl_clr_prkey(lun, i);
7869 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7871 lun->pr_key_count = 1;
7872 lun->pr_res_type = type;
7873 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7874 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7875 lun->pr_res_idx = residx;
7876 lun->pr_generation++;
7877 mtx_unlock(&lun->lun_lock);
7879 /* send msg to other side */
7880 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7881 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7882 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7883 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7884 persis_io.pr.pr_info.res_type = type;
7885 memcpy(persis_io.pr.pr_info.sa_res_key,
7886 param->serv_act_res_key,
7887 sizeof(param->serv_act_res_key));
7888 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7889 sizeof(persis_io.pr), M_WAITOK);
7891 /* not all registrants */
7892 mtx_unlock(&lun->lun_lock);
7893 free(ctsio->kern_data_ptr, M_CTL);
7894 ctl_set_invalid_field(ctsio,
7900 ctl_done((union ctl_io *)ctsio);
7903 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7904 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7907 if (res_key == sa_res_key) {
7910 * The spec implies this is not good but doesn't
7911 * say what to do. There are two choices either
7912 * generate a res conflict or check condition
7913 * with illegal field in parameter data. Since
7914 * that is what is done when the sa_res_key is
7915 * zero I'll take that approach since this has
7916 * to do with the sa_res_key.
7918 mtx_unlock(&lun->lun_lock);
7919 free(ctsio->kern_data_ptr, M_CTL);
7920 ctl_set_invalid_field(ctsio,
7926 ctl_done((union ctl_io *)ctsio);
7930 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7931 if (ctl_get_prkey(lun, i) != sa_res_key)
7935 ctl_clr_prkey(lun, i);
7936 lun->pr_key_count--;
7937 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7940 mtx_unlock(&lun->lun_lock);
7941 free(ctsio->kern_data_ptr, M_CTL);
7942 ctl_set_reservation_conflict(ctsio);
7943 ctl_done((union ctl_io *)ctsio);
7944 return (CTL_RETVAL_COMPLETE);
7946 lun->pr_generation++;
7947 mtx_unlock(&lun->lun_lock);
7949 /* send msg to other side */
7950 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7951 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7952 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7953 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7954 persis_io.pr.pr_info.res_type = type;
7955 memcpy(persis_io.pr.pr_info.sa_res_key,
7956 param->serv_act_res_key,
7957 sizeof(param->serv_act_res_key));
7958 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7959 sizeof(persis_io.pr), M_WAITOK);
7961 /* Reserved but not all registrants */
7962 /* sa_res_key is res holder */
7963 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7964 /* validate scope and type */
7965 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7967 mtx_unlock(&lun->lun_lock);
7968 ctl_set_invalid_field(/*ctsio*/ ctsio,
7974 ctl_done((union ctl_io *)ctsio);
7978 if (type>8 || type==2 || type==4 || type==0) {
7979 mtx_unlock(&lun->lun_lock);
7980 ctl_set_invalid_field(/*ctsio*/ ctsio,
7986 ctl_done((union ctl_io *)ctsio);
7992 * if sa_res_key != res_key remove all
7993 * registrants w/sa_res_key and generate UA
7994 * for these registrants(Registrations
7995 * Preempted) if it wasn't an exclusive
7996 * reservation generate UA(Reservations
7997 * Preempted) for all other registered nexuses
7998 * if the type has changed. Establish the new
7999 * reservation and holder. If res_key and
8000 * sa_res_key are the same do the above
8001 * except don't unregister the res holder.
8004 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8005 if (i == residx || ctl_get_prkey(lun, i) == 0)
8008 if (sa_res_key == ctl_get_prkey(lun, i)) {
8009 ctl_clr_prkey(lun, i);
8010 lun->pr_key_count--;
8011 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8012 } else if (type != lun->pr_res_type &&
8013 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8014 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8015 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8018 lun->pr_res_type = type;
8019 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8020 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8021 lun->pr_res_idx = residx;
8023 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8024 lun->pr_generation++;
8025 mtx_unlock(&lun->lun_lock);
8027 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8028 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8029 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8030 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8031 persis_io.pr.pr_info.res_type = type;
8032 memcpy(persis_io.pr.pr_info.sa_res_key,
8033 param->serv_act_res_key,
8034 sizeof(param->serv_act_res_key));
8035 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8036 sizeof(persis_io.pr), M_WAITOK);
8039 * sa_res_key is not the res holder just
8040 * remove registrants
8044 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8045 if (sa_res_key != ctl_get_prkey(lun, i))
8049 ctl_clr_prkey(lun, i);
8050 lun->pr_key_count--;
8051 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8055 mtx_unlock(&lun->lun_lock);
8056 free(ctsio->kern_data_ptr, M_CTL);
8057 ctl_set_reservation_conflict(ctsio);
8058 ctl_done((union ctl_io *)ctsio);
8061 lun->pr_generation++;
8062 mtx_unlock(&lun->lun_lock);
8064 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8065 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8066 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8067 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8068 persis_io.pr.pr_info.res_type = type;
8069 memcpy(persis_io.pr.pr_info.sa_res_key,
8070 param->serv_act_res_key,
8071 sizeof(param->serv_act_res_key));
8072 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8073 sizeof(persis_io.pr), M_WAITOK);
8080 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8082 uint64_t sa_res_key;
8085 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8087 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8088 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8089 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8090 if (sa_res_key == 0) {
8092 * Unregister everybody else and build UA for
8095 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8096 if (i == msg->pr.pr_info.residx ||
8097 ctl_get_prkey(lun, i) == 0)
8100 ctl_clr_prkey(lun, i);
8101 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8104 lun->pr_key_count = 1;
8105 lun->pr_res_type = msg->pr.pr_info.res_type;
8106 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8107 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8108 lun->pr_res_idx = msg->pr.pr_info.residx;
8110 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8111 if (sa_res_key == ctl_get_prkey(lun, i))
8114 ctl_clr_prkey(lun, i);
8115 lun->pr_key_count--;
8116 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8120 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8121 if (i == msg->pr.pr_info.residx ||
8122 ctl_get_prkey(lun, i) == 0)
8125 if (sa_res_key == ctl_get_prkey(lun, i)) {
8126 ctl_clr_prkey(lun, i);
8127 lun->pr_key_count--;
8128 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8129 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8130 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8131 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8132 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8135 lun->pr_res_type = msg->pr.pr_info.res_type;
8136 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8137 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8138 lun->pr_res_idx = msg->pr.pr_info.residx;
8140 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8142 lun->pr_generation++;
8148 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8150 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8151 struct ctl_lun *lun = CTL_LUN(ctsio);
8153 u_int32_t param_len;
8154 struct scsi_per_res_out *cdb;
8155 struct scsi_per_res_out_parms* param;
8157 uint64_t res_key, sa_res_key, key;
8159 union ctl_ha_msg persis_io;
8162 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8164 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8165 retval = CTL_RETVAL_COMPLETE;
8168 * We only support whole-LUN scope. The scope & type are ignored for
8169 * register, register and ignore existing key and clear.
8170 * We sometimes ignore scope and type on preempts too!!
8171 * Verify reservation type here as well.
8173 type = cdb->scope_type & SPR_TYPE_MASK;
8174 if ((cdb->action == SPRO_RESERVE)
8175 || (cdb->action == SPRO_RELEASE)) {
8176 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8177 ctl_set_invalid_field(/*ctsio*/ ctsio,
8183 ctl_done((union ctl_io *)ctsio);
8184 return (CTL_RETVAL_COMPLETE);
8187 if (type>8 || type==2 || type==4 || type==0) {
8188 ctl_set_invalid_field(/*ctsio*/ ctsio,
8194 ctl_done((union ctl_io *)ctsio);
8195 return (CTL_RETVAL_COMPLETE);
8199 param_len = scsi_4btoul(cdb->length);
8201 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8202 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8203 ctsio->kern_data_len = param_len;
8204 ctsio->kern_total_len = param_len;
8205 ctsio->kern_rel_offset = 0;
8206 ctsio->kern_sg_entries = 0;
8207 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8208 ctsio->be_move_done = ctl_config_move_done;
8209 ctl_datamove((union ctl_io *)ctsio);
8211 return (CTL_RETVAL_COMPLETE);
8214 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8216 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8217 res_key = scsi_8btou64(param->res_key.key);
8218 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8221 * Validate the reservation key here except for SPRO_REG_IGNO
8222 * This must be done for all other service actions
8224 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8225 mtx_lock(&lun->lun_lock);
8226 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8227 if (res_key != key) {
8229 * The current key passed in doesn't match
8230 * the one the initiator previously
8233 mtx_unlock(&lun->lun_lock);
8234 free(ctsio->kern_data_ptr, M_CTL);
8235 ctl_set_reservation_conflict(ctsio);
8236 ctl_done((union ctl_io *)ctsio);
8237 return (CTL_RETVAL_COMPLETE);
8239 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8241 * We are not registered
8243 mtx_unlock(&lun->lun_lock);
8244 free(ctsio->kern_data_ptr, M_CTL);
8245 ctl_set_reservation_conflict(ctsio);
8246 ctl_done((union ctl_io *)ctsio);
8247 return (CTL_RETVAL_COMPLETE);
8248 } else if (res_key != 0) {
8250 * We are not registered and trying to register but
8251 * the register key isn't zero.
8253 mtx_unlock(&lun->lun_lock);
8254 free(ctsio->kern_data_ptr, M_CTL);
8255 ctl_set_reservation_conflict(ctsio);
8256 ctl_done((union ctl_io *)ctsio);
8257 return (CTL_RETVAL_COMPLETE);
8259 mtx_unlock(&lun->lun_lock);
8262 switch (cdb->action & SPRO_ACTION_MASK) {
8264 case SPRO_REG_IGNO: {
8267 printf("Registration received\n");
8271 * We don't support any of these options, as we report in
8272 * the read capabilities request (see
8273 * ctl_persistent_reserve_in(), above).
8275 if ((param->flags & SPR_SPEC_I_PT)
8276 || (param->flags & SPR_ALL_TG_PT)
8277 || (param->flags & SPR_APTPL)) {
8280 if (param->flags & SPR_APTPL)
8282 else if (param->flags & SPR_ALL_TG_PT)
8284 else /* SPR_SPEC_I_PT */
8287 free(ctsio->kern_data_ptr, M_CTL);
8288 ctl_set_invalid_field(ctsio,
8294 ctl_done((union ctl_io *)ctsio);
8295 return (CTL_RETVAL_COMPLETE);
8298 mtx_lock(&lun->lun_lock);
8301 * The initiator wants to clear the
8304 if (sa_res_key == 0) {
8306 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8307 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8308 && ctl_get_prkey(lun, residx) == 0)) {
8309 mtx_unlock(&lun->lun_lock);
8313 ctl_clr_prkey(lun, residx);
8314 lun->pr_key_count--;
8316 if (residx == lun->pr_res_idx) {
8317 lun->flags &= ~CTL_LUN_PR_RESERVED;
8318 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8320 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8321 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8322 lun->pr_key_count) {
8324 * If the reservation is a registrants
8325 * only type we need to generate a UA
8326 * for other registered inits. The
8327 * sense code should be RESERVATIONS
8331 for (i = softc->init_min; i < softc->init_max; i++){
8332 if (ctl_get_prkey(lun, i) == 0)
8335 CTL_UA_RES_RELEASE);
8338 lun->pr_res_type = 0;
8339 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8340 if (lun->pr_key_count==0) {
8341 lun->flags &= ~CTL_LUN_PR_RESERVED;
8342 lun->pr_res_type = 0;
8343 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8346 lun->pr_generation++;
8347 mtx_unlock(&lun->lun_lock);
8349 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8350 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8351 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8352 persis_io.pr.pr_info.residx = residx;
8353 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8354 sizeof(persis_io.pr), M_WAITOK);
8355 } else /* sa_res_key != 0 */ {
8358 * If we aren't registered currently then increment
8359 * the key count and set the registered flag.
8361 ctl_alloc_prkey(lun, residx);
8362 if (ctl_get_prkey(lun, residx) == 0)
8363 lun->pr_key_count++;
8364 ctl_set_prkey(lun, residx, sa_res_key);
8365 lun->pr_generation++;
8366 mtx_unlock(&lun->lun_lock);
8368 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8369 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8370 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8371 persis_io.pr.pr_info.residx = residx;
8372 memcpy(persis_io.pr.pr_info.sa_res_key,
8373 param->serv_act_res_key,
8374 sizeof(param->serv_act_res_key));
8375 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8376 sizeof(persis_io.pr), M_WAITOK);
8383 printf("Reserve executed type %d\n", type);
8385 mtx_lock(&lun->lun_lock);
8386 if (lun->flags & CTL_LUN_PR_RESERVED) {
8388 * if this isn't the reservation holder and it's
8389 * not a "all registrants" type or if the type is
8390 * different then we have a conflict
8392 if ((lun->pr_res_idx != residx
8393 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8394 || lun->pr_res_type != type) {
8395 mtx_unlock(&lun->lun_lock);
8396 free(ctsio->kern_data_ptr, M_CTL);
8397 ctl_set_reservation_conflict(ctsio);
8398 ctl_done((union ctl_io *)ctsio);
8399 return (CTL_RETVAL_COMPLETE);
8401 mtx_unlock(&lun->lun_lock);
8402 } else /* create a reservation */ {
8404 * If it's not an "all registrants" type record
8405 * reservation holder
8407 if (type != SPR_TYPE_WR_EX_AR
8408 && type != SPR_TYPE_EX_AC_AR)
8409 lun->pr_res_idx = residx; /* Res holder */
8411 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8413 lun->flags |= CTL_LUN_PR_RESERVED;
8414 lun->pr_res_type = type;
8416 mtx_unlock(&lun->lun_lock);
8418 /* send msg to other side */
8419 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8420 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8421 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8422 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8423 persis_io.pr.pr_info.res_type = type;
8424 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8425 sizeof(persis_io.pr), M_WAITOK);
8430 mtx_lock(&lun->lun_lock);
8431 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8432 /* No reservation exists return good status */
8433 mtx_unlock(&lun->lun_lock);
8437 * Is this nexus a reservation holder?
8439 if (lun->pr_res_idx != residx
8440 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8442 * not a res holder return good status but
8445 mtx_unlock(&lun->lun_lock);
8449 if (lun->pr_res_type != type) {
8450 mtx_unlock(&lun->lun_lock);
8451 free(ctsio->kern_data_ptr, M_CTL);
8452 ctl_set_illegal_pr_release(ctsio);
8453 ctl_done((union ctl_io *)ctsio);
8454 return (CTL_RETVAL_COMPLETE);
8457 /* okay to release */
8458 lun->flags &= ~CTL_LUN_PR_RESERVED;
8459 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8460 lun->pr_res_type = 0;
8463 * If this isn't an exclusive access reservation and NUAR
8464 * is not set, generate UA for all other registrants.
8466 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8467 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8468 for (i = softc->init_min; i < softc->init_max; i++) {
8469 if (i == residx || ctl_get_prkey(lun, i) == 0)
8471 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8474 mtx_unlock(&lun->lun_lock);
8476 /* Send msg to other side */
8477 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8478 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8479 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8480 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8481 sizeof(persis_io.pr), M_WAITOK);
8485 /* send msg to other side */
8487 mtx_lock(&lun->lun_lock);
8488 lun->flags &= ~CTL_LUN_PR_RESERVED;
8489 lun->pr_res_type = 0;
8490 lun->pr_key_count = 0;
8491 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8493 ctl_clr_prkey(lun, residx);
8494 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8495 if (ctl_get_prkey(lun, i) != 0) {
8496 ctl_clr_prkey(lun, i);
8497 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8499 lun->pr_generation++;
8500 mtx_unlock(&lun->lun_lock);
8502 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8503 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8504 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8505 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8506 sizeof(persis_io.pr), M_WAITOK);
8510 case SPRO_PRE_ABO: {
8513 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8514 residx, ctsio, cdb, param);
8516 return (CTL_RETVAL_COMPLETE);
8520 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8524 free(ctsio->kern_data_ptr, M_CTL);
8525 ctl_set_success(ctsio);
8526 ctl_done((union ctl_io *)ctsio);
8532 * This routine is for handling a message from the other SC pertaining to
8533 * persistent reserve out. All the error checking will have been done
8534 * so only perorming the action need be done here to keep the two
8538 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8540 struct ctl_softc *softc = CTL_SOFTC(io);
8541 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8542 struct ctl_lun *lun;
8544 uint32_t residx, targ_lun;
8546 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8547 mtx_lock(&softc->ctl_lock);
8548 if (targ_lun >= ctl_max_luns ||
8549 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8550 mtx_unlock(&softc->ctl_lock);
8553 mtx_lock(&lun->lun_lock);
8554 mtx_unlock(&softc->ctl_lock);
8555 if (lun->flags & CTL_LUN_DISABLED) {
8556 mtx_unlock(&lun->lun_lock);
8559 residx = ctl_get_initindex(&msg->hdr.nexus);
8560 switch(msg->pr.pr_info.action) {
8561 case CTL_PR_REG_KEY:
8562 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8563 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8564 lun->pr_key_count++;
8565 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8566 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8567 lun->pr_generation++;
8570 case CTL_PR_UNREG_KEY:
8571 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8572 lun->pr_key_count--;
8574 /* XXX Need to see if the reservation has been released */
8575 /* if so do we need to generate UA? */
8576 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8577 lun->flags &= ~CTL_LUN_PR_RESERVED;
8578 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8580 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8581 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8582 lun->pr_key_count) {
8584 * If the reservation is a registrants
8585 * only type we need to generate a UA
8586 * for other registered inits. The
8587 * sense code should be RESERVATIONS
8591 for (i = softc->init_min; i < softc->init_max; i++) {
8592 if (ctl_get_prkey(lun, i) == 0)
8595 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8598 lun->pr_res_type = 0;
8599 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8600 if (lun->pr_key_count==0) {
8601 lun->flags &= ~CTL_LUN_PR_RESERVED;
8602 lun->pr_res_type = 0;
8603 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8606 lun->pr_generation++;
8609 case CTL_PR_RESERVE:
8610 lun->flags |= CTL_LUN_PR_RESERVED;
8611 lun->pr_res_type = msg->pr.pr_info.res_type;
8612 lun->pr_res_idx = msg->pr.pr_info.residx;
8616 case CTL_PR_RELEASE:
8618 * If this isn't an exclusive access reservation and NUAR
8619 * is not set, generate UA for all other registrants.
8621 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8622 lun->pr_res_type != SPR_TYPE_WR_EX &&
8623 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8624 for (i = softc->init_min; i < softc->init_max; i++) {
8625 if (i == residx || ctl_get_prkey(lun, i) == 0)
8627 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8631 lun->flags &= ~CTL_LUN_PR_RESERVED;
8632 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8633 lun->pr_res_type = 0;
8636 case CTL_PR_PREEMPT:
8637 ctl_pro_preempt_other(lun, msg);
8640 lun->flags &= ~CTL_LUN_PR_RESERVED;
8641 lun->pr_res_type = 0;
8642 lun->pr_key_count = 0;
8643 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8645 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8646 if (ctl_get_prkey(lun, i) == 0)
8648 ctl_clr_prkey(lun, i);
8649 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8651 lun->pr_generation++;
8655 mtx_unlock(&lun->lun_lock);
8659 ctl_read_write(struct ctl_scsiio *ctsio)
8661 struct ctl_lun *lun = CTL_LUN(ctsio);
8662 struct ctl_lba_len_flags *lbalen;
8664 uint32_t num_blocks;
8668 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8671 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8672 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8673 switch (ctsio->cdb[0]) {
8676 struct scsi_rw_6 *cdb;
8678 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8680 lba = scsi_3btoul(cdb->addr);
8681 /* only 5 bits are valid in the most significant address byte */
8683 num_blocks = cdb->length;
8685 * This is correct according to SBC-2.
8687 if (num_blocks == 0)
8693 struct scsi_rw_10 *cdb;
8695 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8696 if (cdb->byte2 & SRW10_FUA)
8697 flags |= CTL_LLF_FUA;
8698 if (cdb->byte2 & SRW10_DPO)
8699 flags |= CTL_LLF_DPO;
8700 lba = scsi_4btoul(cdb->addr);
8701 num_blocks = scsi_2btoul(cdb->length);
8704 case WRITE_VERIFY_10: {
8705 struct scsi_write_verify_10 *cdb;
8707 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8708 flags |= CTL_LLF_FUA;
8709 if (cdb->byte2 & SWV_DPO)
8710 flags |= CTL_LLF_DPO;
8711 lba = scsi_4btoul(cdb->addr);
8712 num_blocks = scsi_2btoul(cdb->length);
8717 struct scsi_rw_12 *cdb;
8719 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8720 if (cdb->byte2 & SRW12_FUA)
8721 flags |= CTL_LLF_FUA;
8722 if (cdb->byte2 & SRW12_DPO)
8723 flags |= CTL_LLF_DPO;
8724 lba = scsi_4btoul(cdb->addr);
8725 num_blocks = scsi_4btoul(cdb->length);
8728 case WRITE_VERIFY_12: {
8729 struct scsi_write_verify_12 *cdb;
8731 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8732 flags |= CTL_LLF_FUA;
8733 if (cdb->byte2 & SWV_DPO)
8734 flags |= CTL_LLF_DPO;
8735 lba = scsi_4btoul(cdb->addr);
8736 num_blocks = scsi_4btoul(cdb->length);
8741 struct scsi_rw_16 *cdb;
8743 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8744 if (cdb->byte2 & SRW12_FUA)
8745 flags |= CTL_LLF_FUA;
8746 if (cdb->byte2 & SRW12_DPO)
8747 flags |= CTL_LLF_DPO;
8748 lba = scsi_8btou64(cdb->addr);
8749 num_blocks = scsi_4btoul(cdb->length);
8752 case WRITE_ATOMIC_16: {
8753 struct scsi_write_atomic_16 *cdb;
8755 if (lun->be_lun->atomicblock == 0) {
8756 ctl_set_invalid_opcode(ctsio);
8757 ctl_done((union ctl_io *)ctsio);
8758 return (CTL_RETVAL_COMPLETE);
8761 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8762 if (cdb->byte2 & SRW12_FUA)
8763 flags |= CTL_LLF_FUA;
8764 if (cdb->byte2 & SRW12_DPO)
8765 flags |= CTL_LLF_DPO;
8766 lba = scsi_8btou64(cdb->addr);
8767 num_blocks = scsi_2btoul(cdb->length);
8768 if (num_blocks > lun->be_lun->atomicblock) {
8769 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8770 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8772 ctl_done((union ctl_io *)ctsio);
8773 return (CTL_RETVAL_COMPLETE);
8777 case WRITE_VERIFY_16: {
8778 struct scsi_write_verify_16 *cdb;
8780 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8781 flags |= CTL_LLF_FUA;
8782 if (cdb->byte2 & SWV_DPO)
8783 flags |= CTL_LLF_DPO;
8784 lba = scsi_8btou64(cdb->addr);
8785 num_blocks = scsi_4btoul(cdb->length);
8790 * We got a command we don't support. This shouldn't
8791 * happen, commands should be filtered out above us.
8793 ctl_set_invalid_opcode(ctsio);
8794 ctl_done((union ctl_io *)ctsio);
8796 return (CTL_RETVAL_COMPLETE);
8797 break; /* NOTREACHED */
8801 * The first check is to make sure we're in bounds, the second
8802 * check is to catch wrap-around problems. If the lba + num blocks
8803 * is less than the lba, then we've wrapped around and the block
8804 * range is invalid anyway.
8806 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8807 || ((lba + num_blocks) < lba)) {
8808 ctl_set_lba_out_of_range(ctsio,
8809 MAX(lba, lun->be_lun->maxlba + 1));
8810 ctl_done((union ctl_io *)ctsio);
8811 return (CTL_RETVAL_COMPLETE);
8815 * According to SBC-3, a transfer length of 0 is not an error.
8816 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8817 * translates to 256 blocks for those commands.
8819 if (num_blocks == 0) {
8820 ctl_set_success(ctsio);
8821 ctl_done((union ctl_io *)ctsio);
8822 return (CTL_RETVAL_COMPLETE);
8825 /* Set FUA and/or DPO if caches are disabled. */
8827 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8828 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8830 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8831 flags |= CTL_LLF_FUA;
8834 lbalen = (struct ctl_lba_len_flags *)
8835 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8837 lbalen->len = num_blocks;
8838 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8840 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8841 ctsio->kern_rel_offset = 0;
8843 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8845 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8850 ctl_cnw_cont(union ctl_io *io)
8852 struct ctl_lun *lun = CTL_LUN(io);
8853 struct ctl_scsiio *ctsio;
8854 struct ctl_lba_len_flags *lbalen;
8857 ctsio = &io->scsiio;
8858 ctsio->io_hdr.status = CTL_STATUS_NONE;
8859 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8860 lbalen = (struct ctl_lba_len_flags *)
8861 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8862 lbalen->flags &= ~CTL_LLF_COMPARE;
8863 lbalen->flags |= CTL_LLF_WRITE;
8865 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8866 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8871 ctl_cnw(struct ctl_scsiio *ctsio)
8873 struct ctl_lun *lun = CTL_LUN(ctsio);
8874 struct ctl_lba_len_flags *lbalen;
8876 uint32_t num_blocks;
8879 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8882 switch (ctsio->cdb[0]) {
8883 case COMPARE_AND_WRITE: {
8884 struct scsi_compare_and_write *cdb;
8886 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8887 if (cdb->byte2 & SRW10_FUA)
8888 flags |= CTL_LLF_FUA;
8889 if (cdb->byte2 & SRW10_DPO)
8890 flags |= CTL_LLF_DPO;
8891 lba = scsi_8btou64(cdb->addr);
8892 num_blocks = cdb->length;
8897 * We got a command we don't support. This shouldn't
8898 * happen, commands should be filtered out above us.
8900 ctl_set_invalid_opcode(ctsio);
8901 ctl_done((union ctl_io *)ctsio);
8903 return (CTL_RETVAL_COMPLETE);
8904 break; /* NOTREACHED */
8908 * The first check is to make sure we're in bounds, the second
8909 * check is to catch wrap-around problems. If the lba + num blocks
8910 * is less than the lba, then we've wrapped around and the block
8911 * range is invalid anyway.
8913 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8914 || ((lba + num_blocks) < lba)) {
8915 ctl_set_lba_out_of_range(ctsio,
8916 MAX(lba, lun->be_lun->maxlba + 1));
8917 ctl_done((union ctl_io *)ctsio);
8918 return (CTL_RETVAL_COMPLETE);
8922 * According to SBC-3, a transfer length of 0 is not an error.
8924 if (num_blocks == 0) {
8925 ctl_set_success(ctsio);
8926 ctl_done((union ctl_io *)ctsio);
8927 return (CTL_RETVAL_COMPLETE);
8930 /* Set FUA if write cache is disabled. */
8931 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8932 flags |= CTL_LLF_FUA;
8934 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8935 ctsio->kern_rel_offset = 0;
8938 * Set the IO_CONT flag, so that if this I/O gets passed to
8939 * ctl_data_submit_done(), it'll get passed back to
8940 * ctl_ctl_cnw_cont() for further processing.
8942 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8943 ctsio->io_cont = ctl_cnw_cont;
8945 lbalen = (struct ctl_lba_len_flags *)
8946 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8948 lbalen->len = num_blocks;
8949 lbalen->flags = CTL_LLF_COMPARE | flags;
8951 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8952 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8957 ctl_verify(struct ctl_scsiio *ctsio)
8959 struct ctl_lun *lun = CTL_LUN(ctsio);
8960 struct ctl_lba_len_flags *lbalen;
8962 uint32_t num_blocks;
8966 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8969 flags = CTL_LLF_FUA;
8970 switch (ctsio->cdb[0]) {
8972 struct scsi_verify_10 *cdb;
8974 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8975 if (cdb->byte2 & SVFY_BYTCHK)
8977 if (cdb->byte2 & SVFY_DPO)
8978 flags |= CTL_LLF_DPO;
8979 lba = scsi_4btoul(cdb->addr);
8980 num_blocks = scsi_2btoul(cdb->length);
8984 struct scsi_verify_12 *cdb;
8986 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8987 if (cdb->byte2 & SVFY_BYTCHK)
8989 if (cdb->byte2 & SVFY_DPO)
8990 flags |= CTL_LLF_DPO;
8991 lba = scsi_4btoul(cdb->addr);
8992 num_blocks = scsi_4btoul(cdb->length);
8996 struct scsi_rw_16 *cdb;
8998 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8999 if (cdb->byte2 & SVFY_BYTCHK)
9001 if (cdb->byte2 & SVFY_DPO)
9002 flags |= CTL_LLF_DPO;
9003 lba = scsi_8btou64(cdb->addr);
9004 num_blocks = scsi_4btoul(cdb->length);
9009 * We got a command we don't support. This shouldn't
9010 * happen, commands should be filtered out above us.
9012 ctl_set_invalid_opcode(ctsio);
9013 ctl_done((union ctl_io *)ctsio);
9014 return (CTL_RETVAL_COMPLETE);
9018 * The first check is to make sure we're in bounds, the second
9019 * check is to catch wrap-around problems. If the lba + num blocks
9020 * is less than the lba, then we've wrapped around and the block
9021 * range is invalid anyway.
9023 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9024 || ((lba + num_blocks) < lba)) {
9025 ctl_set_lba_out_of_range(ctsio,
9026 MAX(lba, lun->be_lun->maxlba + 1));
9027 ctl_done((union ctl_io *)ctsio);
9028 return (CTL_RETVAL_COMPLETE);
9032 * According to SBC-3, a transfer length of 0 is not an error.
9034 if (num_blocks == 0) {
9035 ctl_set_success(ctsio);
9036 ctl_done((union ctl_io *)ctsio);
9037 return (CTL_RETVAL_COMPLETE);
9040 lbalen = (struct ctl_lba_len_flags *)
9041 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9043 lbalen->len = num_blocks;
9045 lbalen->flags = CTL_LLF_COMPARE | flags;
9046 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9048 lbalen->flags = CTL_LLF_VERIFY | flags;
9049 ctsio->kern_total_len = 0;
9051 ctsio->kern_rel_offset = 0;
9053 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9054 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9059 ctl_report_luns(struct ctl_scsiio *ctsio)
9061 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9062 struct ctl_port *port = CTL_PORT(ctsio);
9063 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9064 struct scsi_report_luns *cdb;
9065 struct scsi_report_luns_data *lun_data;
9066 int num_filled, num_luns, num_port_luns, retval;
9067 uint32_t alloc_len, lun_datalen;
9068 uint32_t initidx, targ_lun_id, lun_id;
9070 retval = CTL_RETVAL_COMPLETE;
9071 cdb = (struct scsi_report_luns *)ctsio->cdb;
9073 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9076 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9077 mtx_lock(&softc->ctl_lock);
9078 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9079 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9082 mtx_unlock(&softc->ctl_lock);
9084 switch (cdb->select_report) {
9085 case RPL_REPORT_DEFAULT:
9086 case RPL_REPORT_ALL:
9087 case RPL_REPORT_NONSUBSID:
9089 case RPL_REPORT_WELLKNOWN:
9090 case RPL_REPORT_ADMIN:
9091 case RPL_REPORT_CONGLOM:
9095 ctl_set_invalid_field(ctsio,
9101 ctl_done((union ctl_io *)ctsio);
9103 break; /* NOTREACHED */
9106 alloc_len = scsi_4btoul(cdb->length);
9108 * The initiator has to allocate at least 16 bytes for this request,
9109 * so he can at least get the header and the first LUN. Otherwise
9110 * we reject the request (per SPC-3 rev 14, section 6.21).
9112 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9113 sizeof(struct scsi_report_luns_lundata))) {
9114 ctl_set_invalid_field(ctsio,
9120 ctl_done((union ctl_io *)ctsio);
9124 lun_datalen = sizeof(*lun_data) +
9125 (num_luns * sizeof(struct scsi_report_luns_lundata));
9127 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9128 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9129 ctsio->kern_sg_entries = 0;
9131 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9133 mtx_lock(&softc->ctl_lock);
9134 for (targ_lun_id = 0, num_filled = 0;
9135 targ_lun_id < num_port_luns && num_filled < num_luns;
9137 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9138 if (lun_id == UINT32_MAX)
9140 lun = softc->ctl_luns[lun_id];
9144 be64enc(lun_data->luns[num_filled++].lundata,
9145 ctl_encode_lun(targ_lun_id));
9148 * According to SPC-3, rev 14 section 6.21:
9150 * "The execution of a REPORT LUNS command to any valid and
9151 * installed logical unit shall clear the REPORTED LUNS DATA
9152 * HAS CHANGED unit attention condition for all logical
9153 * units of that target with respect to the requesting
9154 * initiator. A valid and installed logical unit is one
9155 * having a PERIPHERAL QUALIFIER of 000b in the standard
9156 * INQUIRY data (see 6.4.2)."
9158 * If request_lun is NULL, the LUN this report luns command
9159 * was issued to is either disabled or doesn't exist. In that
9160 * case, we shouldn't clear any pending lun change unit
9163 if (request_lun != NULL) {
9164 mtx_lock(&lun->lun_lock);
9165 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9166 mtx_unlock(&lun->lun_lock);
9169 mtx_unlock(&softc->ctl_lock);
9172 * It's quite possible that we've returned fewer LUNs than we allocated
9173 * space for. Trim it.
9175 lun_datalen = sizeof(*lun_data) +
9176 (num_filled * sizeof(struct scsi_report_luns_lundata));
9177 ctsio->kern_rel_offset = 0;
9178 ctsio->kern_sg_entries = 0;
9179 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9180 ctsio->kern_total_len = ctsio->kern_data_len;
9183 * We set this to the actual data length, regardless of how much
9184 * space we actually have to return results. If the user looks at
9185 * this value, he'll know whether or not he allocated enough space
9186 * and reissue the command if necessary. We don't support well
9187 * known logical units, so if the user asks for that, return none.
9189 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9192 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9195 ctl_set_success(ctsio);
9196 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9197 ctsio->be_move_done = ctl_config_move_done;
9198 ctl_datamove((union ctl_io *)ctsio);
9203 ctl_request_sense(struct ctl_scsiio *ctsio)
9205 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9206 struct ctl_lun *lun = CTL_LUN(ctsio);
9207 struct scsi_request_sense *cdb;
9208 struct scsi_sense_data *sense_ptr, *ps;
9211 u_int sense_len = SSD_FULL_SIZE;
9212 scsi_sense_data_type sense_format;
9213 ctl_ua_type ua_type;
9214 uint8_t asc = 0, ascq = 0;
9216 cdb = (struct scsi_request_sense *)ctsio->cdb;
9218 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9221 * Determine which sense format the user wants.
9223 if (cdb->byte2 & SRS_DESC)
9224 sense_format = SSD_TYPE_DESC;
9226 sense_format = SSD_TYPE_FIXED;
9228 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9229 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9230 ctsio->kern_sg_entries = 0;
9231 ctsio->kern_rel_offset = 0;
9234 * struct scsi_sense_data, which is currently set to 256 bytes, is
9235 * larger than the largest allowed value for the length field in the
9236 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9238 ctsio->kern_data_len = cdb->length;
9239 ctsio->kern_total_len = cdb->length;
9242 * If we don't have a LUN, we don't have any pending sense.
9245 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9246 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9247 /* "Logical unit not supported" */
9248 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9249 /*current_error*/ 1,
9250 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9258 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9260 * Check for pending sense, and then for pending unit attentions.
9261 * Pending sense gets returned first, then pending unit attentions.
9263 mtx_lock(&lun->lun_lock);
9264 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9266 ps += initidx % CTL_MAX_INIT_PER_PORT;
9267 if (ps != NULL && ps->error_code != 0) {
9268 scsi_sense_data_type stored_format;
9271 * Check to see which sense format was used for the stored
9274 stored_format = scsi_sense_type(ps);
9277 * If the user requested a different sense format than the
9278 * one we stored, then we need to convert it to the other
9279 * format. If we're going from descriptor to fixed format
9280 * sense data, we may lose things in translation, depending
9281 * on what options were used.
9283 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9284 * for some reason we'll just copy it out as-is.
9286 if ((stored_format == SSD_TYPE_FIXED)
9287 && (sense_format == SSD_TYPE_DESC))
9288 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9289 ps, (struct scsi_sense_data_desc *)sense_ptr);
9290 else if ((stored_format == SSD_TYPE_DESC)
9291 && (sense_format == SSD_TYPE_FIXED))
9292 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9293 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9295 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9300 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9302 if (ua_type != CTL_UA_NONE)
9305 if (have_error == 0) {
9307 * Report informational exception if have one and allowed.
9309 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9311 ascq = lun->ie_ascq;
9313 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9314 /*current_error*/ 1,
9315 /*sense_key*/ SSD_KEY_NO_SENSE,
9320 mtx_unlock(&lun->lun_lock);
9324 * We report the SCSI status as OK, since the status of the command
9325 * itself is OK. We're reporting sense as parameter data.
9327 ctl_set_success(ctsio);
9328 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9329 ctsio->be_move_done = ctl_config_move_done;
9330 ctl_datamove((union ctl_io *)ctsio);
9331 return (CTL_RETVAL_COMPLETE);
9335 ctl_tur(struct ctl_scsiio *ctsio)
9338 CTL_DEBUG_PRINT(("ctl_tur\n"));
9340 ctl_set_success(ctsio);
9341 ctl_done((union ctl_io *)ctsio);
9343 return (CTL_RETVAL_COMPLETE);
9347 * SCSI VPD page 0x00, the Supported VPD Pages page.
9350 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9352 struct ctl_lun *lun = CTL_LUN(ctsio);
9353 struct scsi_vpd_supported_pages *pages;
9357 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9358 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9359 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9360 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9361 ctsio->kern_rel_offset = 0;
9362 ctsio->kern_sg_entries = 0;
9363 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9364 ctsio->kern_total_len = ctsio->kern_data_len;
9367 * The control device is always connected. The disk device, on the
9368 * other hand, may not be online all the time. Need to change this
9369 * to figure out whether the disk device is actually online or not.
9372 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9373 lun->be_lun->lun_type;
9375 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9378 /* Supported VPD pages */
9379 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9381 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9382 /* Device Identification */
9383 pages->page_list[p++] = SVPD_DEVICE_ID;
9384 /* Extended INQUIRY Data */
9385 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9386 /* Mode Page Policy */
9387 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9389 pages->page_list[p++] = SVPD_SCSI_PORTS;
9390 /* Third-party Copy */
9391 pages->page_list[p++] = SVPD_SCSI_TPC;
9392 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9394 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9395 /* Block Device Characteristics */
9396 pages->page_list[p++] = SVPD_BDC;
9397 /* Logical Block Provisioning */
9398 pages->page_list[p++] = SVPD_LBP;
9402 ctl_set_success(ctsio);
9403 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9404 ctsio->be_move_done = ctl_config_move_done;
9405 ctl_datamove((union ctl_io *)ctsio);
9406 return (CTL_RETVAL_COMPLETE);
9410 * SCSI VPD page 0x80, the Unit Serial Number page.
9413 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9415 struct ctl_lun *lun = CTL_LUN(ctsio);
9416 struct scsi_vpd_unit_serial_number *sn_ptr;
9419 data_len = 4 + CTL_SN_LEN;
9420 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9421 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9422 ctsio->kern_rel_offset = 0;
9423 ctsio->kern_sg_entries = 0;
9424 ctsio->kern_data_len = min(data_len, alloc_len);
9425 ctsio->kern_total_len = ctsio->kern_data_len;
9428 * The control device is always connected. The disk device, on the
9429 * other hand, may not be online all the time. Need to change this
9430 * to figure out whether the disk device is actually online or not.
9433 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9434 lun->be_lun->lun_type;
9436 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9438 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9439 sn_ptr->length = CTL_SN_LEN;
9441 * If we don't have a LUN, we just leave the serial number as
9445 strncpy((char *)sn_ptr->serial_num,
9446 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9448 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9450 ctl_set_success(ctsio);
9451 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9452 ctsio->be_move_done = ctl_config_move_done;
9453 ctl_datamove((union ctl_io *)ctsio);
9454 return (CTL_RETVAL_COMPLETE);
9459 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9462 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9464 struct ctl_lun *lun = CTL_LUN(ctsio);
9465 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9468 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9469 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9470 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9471 ctsio->kern_sg_entries = 0;
9472 ctsio->kern_rel_offset = 0;
9473 ctsio->kern_data_len = min(data_len, alloc_len);
9474 ctsio->kern_total_len = ctsio->kern_data_len;
9477 * The control device is always connected. The disk device, on the
9478 * other hand, may not be online all the time.
9481 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9482 lun->be_lun->lun_type;
9484 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9485 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9486 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9488 * We support head of queue, ordered and simple tags.
9490 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9492 * Volatile cache supported.
9494 eid_ptr->flags3 = SVPD_EID_V_SUP;
9497 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9498 * attention for a particular IT nexus on all LUNs once we report
9499 * it to that nexus once. This bit is required as of SPC-4.
9501 eid_ptr->flags4 = SVPD_EID_LUICLR;
9504 * We support revert to defaults (RTD) bit in MODE SELECT.
9506 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9509 * XXX KDM in order to correctly answer this, we would need
9510 * information from the SIM to determine how much sense data it
9511 * can send. So this would really be a path inquiry field, most
9512 * likely. This can be set to a maximum of 252 according to SPC-4,
9513 * but the hardware may or may not be able to support that much.
9514 * 0 just means that the maximum sense data length is not reported.
9516 eid_ptr->max_sense_length = 0;
9518 ctl_set_success(ctsio);
9519 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9520 ctsio->be_move_done = ctl_config_move_done;
9521 ctl_datamove((union ctl_io *)ctsio);
9522 return (CTL_RETVAL_COMPLETE);
9526 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9528 struct ctl_lun *lun = CTL_LUN(ctsio);
9529 struct scsi_vpd_mode_page_policy *mpp_ptr;
9532 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9533 sizeof(struct scsi_vpd_mode_page_policy_descr);
9535 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9536 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9537 ctsio->kern_rel_offset = 0;
9538 ctsio->kern_sg_entries = 0;
9539 ctsio->kern_data_len = min(data_len, alloc_len);
9540 ctsio->kern_total_len = ctsio->kern_data_len;
9543 * The control device is always connected. The disk device, on the
9544 * other hand, may not be online all the time.
9547 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9548 lun->be_lun->lun_type;
9550 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9551 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9552 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9553 mpp_ptr->descr[0].page_code = 0x3f;
9554 mpp_ptr->descr[0].subpage_code = 0xff;
9555 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9557 ctl_set_success(ctsio);
9558 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9559 ctsio->be_move_done = ctl_config_move_done;
9560 ctl_datamove((union ctl_io *)ctsio);
9561 return (CTL_RETVAL_COMPLETE);
9565 * SCSI VPD page 0x83, the Device Identification page.
9568 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9570 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9571 struct ctl_port *port = CTL_PORT(ctsio);
9572 struct ctl_lun *lun = CTL_LUN(ctsio);
9573 struct scsi_vpd_device_id *devid_ptr;
9574 struct scsi_vpd_id_descriptor *desc;
9578 data_len = sizeof(struct scsi_vpd_device_id) +
9579 sizeof(struct scsi_vpd_id_descriptor) +
9580 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9581 sizeof(struct scsi_vpd_id_descriptor) +
9582 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9583 if (lun && lun->lun_devid)
9584 data_len += lun->lun_devid->len;
9585 if (port && port->port_devid)
9586 data_len += port->port_devid->len;
9587 if (port && port->target_devid)
9588 data_len += port->target_devid->len;
9590 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9591 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9592 ctsio->kern_sg_entries = 0;
9593 ctsio->kern_rel_offset = 0;
9594 ctsio->kern_sg_entries = 0;
9595 ctsio->kern_data_len = min(data_len, alloc_len);
9596 ctsio->kern_total_len = ctsio->kern_data_len;
9599 * The control device is always connected. The disk device, on the
9600 * other hand, may not be online all the time.
9603 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9604 lun->be_lun->lun_type;
9606 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9607 devid_ptr->page_code = SVPD_DEVICE_ID;
9608 scsi_ulto2b(data_len - 4, devid_ptr->length);
9610 if (port && port->port_type == CTL_PORT_FC)
9611 proto = SCSI_PROTO_FC << 4;
9612 else if (port && port->port_type == CTL_PORT_SAS)
9613 proto = SCSI_PROTO_SAS << 4;
9614 else if (port && port->port_type == CTL_PORT_ISCSI)
9615 proto = SCSI_PROTO_ISCSI << 4;
9617 proto = SCSI_PROTO_SPI << 4;
9618 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9621 * We're using a LUN association here. i.e., this device ID is a
9622 * per-LUN identifier.
9624 if (lun && lun->lun_devid) {
9625 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9626 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9627 lun->lun_devid->len);
9631 * This is for the WWPN which is a port association.
9633 if (port && port->port_devid) {
9634 memcpy(desc, port->port_devid->data, port->port_devid->len);
9635 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9636 port->port_devid->len);
9640 * This is for the Relative Target Port(type 4h) identifier
9642 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9643 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9644 SVPD_ID_TYPE_RELTARG;
9646 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9647 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9648 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9651 * This is for the Target Port Group(type 5h) identifier
9653 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9654 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9655 SVPD_ID_TYPE_TPORTGRP;
9657 if (softc->is_single ||
9658 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9661 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9662 scsi_ulto2b(g, &desc->identifier[2]);
9663 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9664 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9667 * This is for the Target identifier
9669 if (port && port->target_devid) {
9670 memcpy(desc, port->target_devid->data, port->target_devid->len);
9673 ctl_set_success(ctsio);
9674 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9675 ctsio->be_move_done = ctl_config_move_done;
9676 ctl_datamove((union ctl_io *)ctsio);
9677 return (CTL_RETVAL_COMPLETE);
9681 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9683 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9684 struct ctl_lun *lun = CTL_LUN(ctsio);
9685 struct scsi_vpd_scsi_ports *sp;
9686 struct scsi_vpd_port_designation *pd;
9687 struct scsi_vpd_port_designation_cont *pdc;
9688 struct ctl_port *port;
9689 int data_len, num_target_ports, iid_len, id_len;
9691 num_target_ports = 0;
9694 mtx_lock(&softc->ctl_lock);
9695 STAILQ_FOREACH(port, &softc->port_list, links) {
9696 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9699 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9702 if (port->init_devid)
9703 iid_len += port->init_devid->len;
9704 if (port->port_devid)
9705 id_len += port->port_devid->len;
9707 mtx_unlock(&softc->ctl_lock);
9709 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9710 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9711 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9712 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9713 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9714 ctsio->kern_sg_entries = 0;
9715 ctsio->kern_rel_offset = 0;
9716 ctsio->kern_sg_entries = 0;
9717 ctsio->kern_data_len = min(data_len, alloc_len);
9718 ctsio->kern_total_len = ctsio->kern_data_len;
9721 * The control device is always connected. The disk device, on the
9722 * other hand, may not be online all the time. Need to change this
9723 * to figure out whether the disk device is actually online or not.
9726 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9727 lun->be_lun->lun_type;
9729 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9731 sp->page_code = SVPD_SCSI_PORTS;
9732 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9734 pd = &sp->design[0];
9736 mtx_lock(&softc->ctl_lock);
9737 STAILQ_FOREACH(port, &softc->port_list, links) {
9738 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9741 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9743 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9744 if (port->init_devid) {
9745 iid_len = port->init_devid->len;
9746 memcpy(pd->initiator_transportid,
9747 port->init_devid->data, port->init_devid->len);
9750 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9751 pdc = (struct scsi_vpd_port_designation_cont *)
9752 (&pd->initiator_transportid[iid_len]);
9753 if (port->port_devid) {
9754 id_len = port->port_devid->len;
9755 memcpy(pdc->target_port_descriptors,
9756 port->port_devid->data, port->port_devid->len);
9759 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9760 pd = (struct scsi_vpd_port_designation *)
9761 ((uint8_t *)pdc->target_port_descriptors + id_len);
9763 mtx_unlock(&softc->ctl_lock);
9765 ctl_set_success(ctsio);
9766 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9767 ctsio->be_move_done = ctl_config_move_done;
9768 ctl_datamove((union ctl_io *)ctsio);
9769 return (CTL_RETVAL_COMPLETE);
9773 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9775 struct ctl_lun *lun = CTL_LUN(ctsio);
9776 struct scsi_vpd_block_limits *bl_ptr;
9780 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9781 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9782 ctsio->kern_sg_entries = 0;
9783 ctsio->kern_rel_offset = 0;
9784 ctsio->kern_sg_entries = 0;
9785 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9786 ctsio->kern_total_len = ctsio->kern_data_len;
9789 * The control device is always connected. The disk device, on the
9790 * other hand, may not be online all the time. Need to change this
9791 * to figure out whether the disk device is actually online or not.
9794 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9795 lun->be_lun->lun_type;
9797 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9799 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9800 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9801 bl_ptr->max_cmp_write_len = 0xff;
9802 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9804 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9805 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9807 val = dnvlist_get_string(lun->be_lun->options,
9808 "unmap_max_lba", NULL);
9810 ctl_expand_number(val, &ival);
9811 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9813 val = dnvlist_get_string(lun->be_lun->options,
9814 "unmap_max_descr", NULL);
9816 ctl_expand_number(val, &ival);
9817 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9818 if (lun->be_lun->ublockexp != 0) {
9819 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9820 bl_ptr->opt_unmap_grain);
9821 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9822 bl_ptr->unmap_grain_align);
9825 scsi_ulto4b(lun->be_lun->atomicblock,
9826 bl_ptr->max_atomic_transfer_length);
9827 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9828 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9829 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9830 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9832 val = dnvlist_get_string(lun->be_lun->options,
9833 "write_same_max_lba", NULL);
9835 ctl_expand_number(val, &ival);
9836 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9839 ctl_set_success(ctsio);
9840 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9841 ctsio->be_move_done = ctl_config_move_done;
9842 ctl_datamove((union ctl_io *)ctsio);
9843 return (CTL_RETVAL_COMPLETE);
9847 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9849 struct ctl_lun *lun = CTL_LUN(ctsio);
9850 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9854 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9855 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9856 ctsio->kern_sg_entries = 0;
9857 ctsio->kern_rel_offset = 0;
9858 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9859 ctsio->kern_total_len = ctsio->kern_data_len;
9862 * The control device is always connected. The disk device, on the
9863 * other hand, may not be online all the time. Need to change this
9864 * to figure out whether the disk device is actually online or not.
9867 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9868 lun->be_lun->lun_type;
9870 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9871 bdc_ptr->page_code = SVPD_BDC;
9872 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9874 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL)
9875 i = strtol(value, NULL, 0);
9877 i = CTL_DEFAULT_ROTATION_RATE;
9878 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9880 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL)
9881 i = strtol(value, NULL, 0);
9884 bdc_ptr->wab_wac_ff = (i & 0x0f);
9885 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9887 ctl_set_success(ctsio);
9888 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9889 ctsio->be_move_done = ctl_config_move_done;
9890 ctl_datamove((union ctl_io *)ctsio);
9891 return (CTL_RETVAL_COMPLETE);
9895 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9897 struct ctl_lun *lun = CTL_LUN(ctsio);
9898 struct scsi_vpd_logical_block_prov *lbp_ptr;
9901 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9902 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9903 ctsio->kern_sg_entries = 0;
9904 ctsio->kern_rel_offset = 0;
9905 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9906 ctsio->kern_total_len = ctsio->kern_data_len;
9909 * The control device is always connected. The disk device, on the
9910 * other hand, may not be online all the time. Need to change this
9911 * to figure out whether the disk device is actually online or not.
9914 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9915 lun->be_lun->lun_type;
9917 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9919 lbp_ptr->page_code = SVPD_LBP;
9920 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9921 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9922 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9923 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9924 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9925 value = dnvlist_get_string(lun->be_lun->options,
9926 "provisioning_type", NULL);
9927 if (value != NULL) {
9928 if (strcmp(value, "resource") == 0)
9929 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9930 else if (strcmp(value, "thin") == 0)
9931 lbp_ptr->prov_type = SVPD_LBP_THIN;
9933 lbp_ptr->prov_type = SVPD_LBP_THIN;
9936 ctl_set_success(ctsio);
9937 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9938 ctsio->be_move_done = ctl_config_move_done;
9939 ctl_datamove((union ctl_io *)ctsio);
9940 return (CTL_RETVAL_COMPLETE);
9944 * INQUIRY with the EVPD bit set.
9947 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9949 struct ctl_lun *lun = CTL_LUN(ctsio);
9950 struct scsi_inquiry *cdb;
9951 int alloc_len, retval;
9953 cdb = (struct scsi_inquiry *)ctsio->cdb;
9954 alloc_len = scsi_2btoul(cdb->length);
9956 switch (cdb->page_code) {
9957 case SVPD_SUPPORTED_PAGES:
9958 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9960 case SVPD_UNIT_SERIAL_NUMBER:
9961 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9963 case SVPD_DEVICE_ID:
9964 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9966 case SVPD_EXTENDED_INQUIRY_DATA:
9967 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9969 case SVPD_MODE_PAGE_POLICY:
9970 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9972 case SVPD_SCSI_PORTS:
9973 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9976 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9978 case SVPD_BLOCK_LIMITS:
9979 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9981 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9984 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9986 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
9989 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9991 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9995 ctl_set_invalid_field(ctsio,
10001 ctl_done((union ctl_io *)ctsio);
10002 retval = CTL_RETVAL_COMPLETE;
10010 * Standard INQUIRY data.
10013 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10015 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10016 struct ctl_port *port = CTL_PORT(ctsio);
10017 struct ctl_lun *lun = CTL_LUN(ctsio);
10018 struct scsi_inquiry_data *inq_ptr;
10019 struct scsi_inquiry *cdb;
10021 uint32_t alloc_len, data_len;
10022 ctl_port_type port_type;
10024 port_type = port->port_type;
10025 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10026 port_type = CTL_PORT_SCSI;
10028 cdb = (struct scsi_inquiry *)ctsio->cdb;
10029 alloc_len = scsi_2btoul(cdb->length);
10032 * We malloc the full inquiry data size here and fill it
10033 * in. If the user only asks for less, we'll give him
10036 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10037 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10038 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10039 ctsio->kern_sg_entries = 0;
10040 ctsio->kern_rel_offset = 0;
10041 ctsio->kern_data_len = min(data_len, alloc_len);
10042 ctsio->kern_total_len = ctsio->kern_data_len;
10045 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10046 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10047 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10048 lun->be_lun->lun_type;
10050 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10051 lun->be_lun->lun_type;
10053 if (lun->flags & CTL_LUN_REMOVABLE)
10054 inq_ptr->dev_qual2 |= SID_RMB;
10056 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10058 /* RMB in byte 2 is 0 */
10059 inq_ptr->version = SCSI_REV_SPC5;
10062 * According to SAM-3, even if a device only supports a single
10063 * level of LUN addressing, it should still set the HISUP bit:
10065 * 4.9.1 Logical unit numbers overview
10067 * All logical unit number formats described in this standard are
10068 * hierarchical in structure even when only a single level in that
10069 * hierarchy is used. The HISUP bit shall be set to one in the
10070 * standard INQUIRY data (see SPC-2) when any logical unit number
10071 * format described in this standard is used. Non-hierarchical
10072 * formats are outside the scope of this standard.
10074 * Therefore we set the HiSup bit here.
10076 * The response format is 2, per SPC-3.
10078 inq_ptr->response_format = SID_HiSup | 2;
10080 inq_ptr->additional_length = data_len -
10081 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10082 CTL_DEBUG_PRINT(("additional_length = %d\n",
10083 inq_ptr->additional_length));
10085 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10086 if (port_type == CTL_PORT_SCSI)
10087 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10088 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10089 inq_ptr->flags = SID_CmdQue;
10090 if (port_type == CTL_PORT_SCSI)
10091 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10094 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10095 * We have 8 bytes for the vendor name, and 16 bytes for the device
10096 * name and 4 bytes for the revision.
10098 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10099 "vendor", NULL)) == NULL) {
10100 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10102 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10103 strncpy(inq_ptr->vendor, val,
10104 min(sizeof(inq_ptr->vendor), strlen(val)));
10107 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10108 sizeof(inq_ptr->product));
10109 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product",
10111 switch (lun->be_lun->lun_type) {
10113 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10114 sizeof(inq_ptr->product));
10117 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10118 sizeof(inq_ptr->product));
10121 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10122 sizeof(inq_ptr->product));
10125 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10126 sizeof(inq_ptr->product));
10130 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10131 strncpy(inq_ptr->product, val,
10132 min(sizeof(inq_ptr->product), strlen(val)));
10136 * XXX make this a macro somewhere so it automatically gets
10137 * incremented when we make changes.
10139 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10140 "revision", NULL)) == NULL) {
10141 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10143 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10144 strncpy(inq_ptr->revision, val,
10145 min(sizeof(inq_ptr->revision), strlen(val)));
10149 * For parallel SCSI, we support double transition and single
10150 * transition clocking. We also support QAS (Quick Arbitration
10151 * and Selection) and Information Unit transfers on both the
10152 * control and array devices.
10154 if (port_type == CTL_PORT_SCSI)
10155 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10158 /* SAM-6 (no version claimed) */
10159 scsi_ulto2b(0x00C0, inq_ptr->version1);
10160 /* SPC-5 (no version claimed) */
10161 scsi_ulto2b(0x05C0, inq_ptr->version2);
10162 if (port_type == CTL_PORT_FC) {
10163 /* FCP-2 ANSI INCITS.350:2003 */
10164 scsi_ulto2b(0x0917, inq_ptr->version3);
10165 } else if (port_type == CTL_PORT_SCSI) {
10166 /* SPI-4 ANSI INCITS.362:200x */
10167 scsi_ulto2b(0x0B56, inq_ptr->version3);
10168 } else if (port_type == CTL_PORT_ISCSI) {
10169 /* iSCSI (no version claimed) */
10170 scsi_ulto2b(0x0960, inq_ptr->version3);
10171 } else if (port_type == CTL_PORT_SAS) {
10172 /* SAS (no version claimed) */
10173 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10174 } else if (port_type == CTL_PORT_UMASS) {
10175 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10176 scsi_ulto2b(0x1730, inq_ptr->version3);
10180 /* SBC-4 (no version claimed) */
10181 scsi_ulto2b(0x0600, inq_ptr->version4);
10183 switch (lun->be_lun->lun_type) {
10185 /* SBC-4 (no version claimed) */
10186 scsi_ulto2b(0x0600, inq_ptr->version4);
10191 /* MMC-6 (no version claimed) */
10192 scsi_ulto2b(0x04E0, inq_ptr->version4);
10199 ctl_set_success(ctsio);
10200 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10201 ctsio->be_move_done = ctl_config_move_done;
10202 ctl_datamove((union ctl_io *)ctsio);
10203 return (CTL_RETVAL_COMPLETE);
10207 ctl_inquiry(struct ctl_scsiio *ctsio)
10209 struct scsi_inquiry *cdb;
10212 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10214 cdb = (struct scsi_inquiry *)ctsio->cdb;
10215 if (cdb->byte2 & SI_EVPD)
10216 retval = ctl_inquiry_evpd(ctsio);
10217 else if (cdb->page_code == 0)
10218 retval = ctl_inquiry_std(ctsio);
10220 ctl_set_invalid_field(ctsio,
10226 ctl_done((union ctl_io *)ctsio);
10227 return (CTL_RETVAL_COMPLETE);
10234 ctl_get_config(struct ctl_scsiio *ctsio)
10236 struct ctl_lun *lun = CTL_LUN(ctsio);
10237 struct scsi_get_config_header *hdr;
10238 struct scsi_get_config_feature *feature;
10239 struct scsi_get_config *cdb;
10240 uint32_t alloc_len, data_len;
10243 cdb = (struct scsi_get_config *)ctsio->cdb;
10244 rt = (cdb->rt & SGC_RT_MASK);
10245 starting = scsi_2btoul(cdb->starting_feature);
10246 alloc_len = scsi_2btoul(cdb->length);
10248 data_len = sizeof(struct scsi_get_config_header) +
10249 sizeof(struct scsi_get_config_feature) + 8 +
10250 sizeof(struct scsi_get_config_feature) + 8 +
10251 sizeof(struct scsi_get_config_feature) + 4 +
10252 sizeof(struct scsi_get_config_feature) + 4 +
10253 sizeof(struct scsi_get_config_feature) + 8 +
10254 sizeof(struct scsi_get_config_feature) +
10255 sizeof(struct scsi_get_config_feature) + 4 +
10256 sizeof(struct scsi_get_config_feature) + 4 +
10257 sizeof(struct scsi_get_config_feature) + 4 +
10258 sizeof(struct scsi_get_config_feature) + 4 +
10259 sizeof(struct scsi_get_config_feature) + 4 +
10260 sizeof(struct scsi_get_config_feature) + 4;
10261 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10262 ctsio->kern_sg_entries = 0;
10263 ctsio->kern_rel_offset = 0;
10265 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10266 if (lun->flags & CTL_LUN_NO_MEDIA)
10267 scsi_ulto2b(0x0000, hdr->current_profile);
10269 scsi_ulto2b(0x0010, hdr->current_profile);
10270 feature = (struct scsi_get_config_feature *)(hdr + 1);
10272 if (starting > 0x003b)
10274 if (starting > 0x003a)
10276 if (starting > 0x002b)
10278 if (starting > 0x002a)
10280 if (starting > 0x001f)
10282 if (starting > 0x001e)
10284 if (starting > 0x001d)
10286 if (starting > 0x0010)
10288 if (starting > 0x0003)
10290 if (starting > 0x0002)
10292 if (starting > 0x0001)
10294 if (starting > 0x0000)
10298 scsi_ulto2b(0x0000, feature->feature_code);
10299 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10300 feature->add_length = 8;
10301 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10302 feature->feature_data[2] = 0x00;
10303 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10304 feature->feature_data[6] = 0x01;
10305 feature = (struct scsi_get_config_feature *)
10306 &feature->feature_data[feature->add_length];
10309 scsi_ulto2b(0x0001, feature->feature_code);
10310 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10311 feature->add_length = 8;
10312 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10313 feature->feature_data[4] = 0x03;
10314 feature = (struct scsi_get_config_feature *)
10315 &feature->feature_data[feature->add_length];
10318 scsi_ulto2b(0x0002, feature->feature_code);
10319 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10320 feature->add_length = 4;
10321 feature->feature_data[0] = 0x02;
10322 feature = (struct scsi_get_config_feature *)
10323 &feature->feature_data[feature->add_length];
10325 f3: /* Removable Medium */
10326 scsi_ulto2b(0x0003, feature->feature_code);
10327 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10328 feature->add_length = 4;
10329 feature->feature_data[0] = 0x39;
10330 feature = (struct scsi_get_config_feature *)
10331 &feature->feature_data[feature->add_length];
10333 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10336 f10: /* Random Read */
10337 scsi_ulto2b(0x0010, feature->feature_code);
10338 feature->flags = 0x00;
10339 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10340 feature->flags |= SGC_F_CURRENT;
10341 feature->add_length = 8;
10342 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10343 scsi_ulto2b(1, &feature->feature_data[4]);
10344 feature->feature_data[6] = 0x00;
10345 feature = (struct scsi_get_config_feature *)
10346 &feature->feature_data[feature->add_length];
10348 f1d: /* Multi-Read */
10349 scsi_ulto2b(0x001D, feature->feature_code);
10350 feature->flags = 0x00;
10351 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10352 feature->flags |= SGC_F_CURRENT;
10353 feature->add_length = 0;
10354 feature = (struct scsi_get_config_feature *)
10355 &feature->feature_data[feature->add_length];
10358 scsi_ulto2b(0x001E, feature->feature_code);
10359 feature->flags = 0x00;
10360 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10361 feature->flags |= SGC_F_CURRENT;
10362 feature->add_length = 4;
10363 feature->feature_data[0] = 0x00;
10364 feature = (struct scsi_get_config_feature *)
10365 &feature->feature_data[feature->add_length];
10367 f1f: /* DVD Read */
10368 scsi_ulto2b(0x001F, feature->feature_code);
10369 feature->flags = 0x08;
10370 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10371 feature->flags |= SGC_F_CURRENT;
10372 feature->add_length = 4;
10373 feature->feature_data[0] = 0x01;
10374 feature->feature_data[2] = 0x03;
10375 feature = (struct scsi_get_config_feature *)
10376 &feature->feature_data[feature->add_length];
10379 scsi_ulto2b(0x002A, feature->feature_code);
10380 feature->flags = 0x04;
10381 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10382 feature->flags |= SGC_F_CURRENT;
10383 feature->add_length = 4;
10384 feature->feature_data[0] = 0x00;
10385 feature->feature_data[1] = 0x00;
10386 feature = (struct scsi_get_config_feature *)
10387 &feature->feature_data[feature->add_length];
10390 scsi_ulto2b(0x002B, feature->feature_code);
10391 feature->flags = 0x00;
10392 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10393 feature->flags |= SGC_F_CURRENT;
10394 feature->add_length = 4;
10395 feature->feature_data[0] = 0x00;
10396 feature = (struct scsi_get_config_feature *)
10397 &feature->feature_data[feature->add_length];
10399 f3a: /* DVD+RW Dual Layer */
10400 scsi_ulto2b(0x003A, feature->feature_code);
10401 feature->flags = 0x00;
10402 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10403 feature->flags |= SGC_F_CURRENT;
10404 feature->add_length = 4;
10405 feature->feature_data[0] = 0x00;
10406 feature->feature_data[1] = 0x00;
10407 feature = (struct scsi_get_config_feature *)
10408 &feature->feature_data[feature->add_length];
10410 f3b: /* DVD+R Dual Layer */
10411 scsi_ulto2b(0x003B, feature->feature_code);
10412 feature->flags = 0x00;
10413 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10414 feature->flags |= SGC_F_CURRENT;
10415 feature->add_length = 4;
10416 feature->feature_data[0] = 0x00;
10417 feature = (struct scsi_get_config_feature *)
10418 &feature->feature_data[feature->add_length];
10421 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10422 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10423 feature = (struct scsi_get_config_feature *)(hdr + 1);
10424 if (scsi_2btoul(feature->feature_code) == starting)
10425 feature = (struct scsi_get_config_feature *)
10426 &feature->feature_data[feature->add_length];
10427 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10429 scsi_ulto4b(data_len - 4, hdr->data_length);
10430 ctsio->kern_data_len = min(data_len, alloc_len);
10431 ctsio->kern_total_len = ctsio->kern_data_len;
10433 ctl_set_success(ctsio);
10434 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10435 ctsio->be_move_done = ctl_config_move_done;
10436 ctl_datamove((union ctl_io *)ctsio);
10437 return (CTL_RETVAL_COMPLETE);
10441 ctl_get_event_status(struct ctl_scsiio *ctsio)
10443 struct scsi_get_event_status_header *hdr;
10444 struct scsi_get_event_status *cdb;
10445 uint32_t alloc_len, data_len;
10447 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10448 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10449 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10450 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10451 ctl_done((union ctl_io *)ctsio);
10452 return (CTL_RETVAL_COMPLETE);
10454 alloc_len = scsi_2btoul(cdb->length);
10456 data_len = sizeof(struct scsi_get_event_status_header);
10457 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10458 ctsio->kern_sg_entries = 0;
10459 ctsio->kern_rel_offset = 0;
10460 ctsio->kern_data_len = min(data_len, alloc_len);
10461 ctsio->kern_total_len = ctsio->kern_data_len;
10463 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10464 scsi_ulto2b(0, hdr->descr_length);
10465 hdr->nea_class = SGESN_NEA;
10466 hdr->supported_class = 0;
10468 ctl_set_success(ctsio);
10469 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10470 ctsio->be_move_done = ctl_config_move_done;
10471 ctl_datamove((union ctl_io *)ctsio);
10472 return (CTL_RETVAL_COMPLETE);
10476 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10478 struct scsi_mechanism_status_header *hdr;
10479 struct scsi_mechanism_status *cdb;
10480 uint32_t alloc_len, data_len;
10482 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10483 alloc_len = scsi_2btoul(cdb->length);
10485 data_len = sizeof(struct scsi_mechanism_status_header);
10486 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10487 ctsio->kern_sg_entries = 0;
10488 ctsio->kern_rel_offset = 0;
10489 ctsio->kern_data_len = min(data_len, alloc_len);
10490 ctsio->kern_total_len = ctsio->kern_data_len;
10492 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10493 hdr->state1 = 0x00;
10494 hdr->state2 = 0xe0;
10495 scsi_ulto3b(0, hdr->lba);
10496 hdr->slots_num = 0;
10497 scsi_ulto2b(0, hdr->slots_length);
10499 ctl_set_success(ctsio);
10500 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10501 ctsio->be_move_done = ctl_config_move_done;
10502 ctl_datamove((union ctl_io *)ctsio);
10503 return (CTL_RETVAL_COMPLETE);
10507 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10512 buf[1] = bin2bcd((lba / 75) / 60);
10513 buf[2] = bin2bcd((lba / 75) % 60);
10514 buf[3] = bin2bcd(lba % 75);
10518 ctl_read_toc(struct ctl_scsiio *ctsio)
10520 struct ctl_lun *lun = CTL_LUN(ctsio);
10521 struct scsi_read_toc_hdr *hdr;
10522 struct scsi_read_toc_type01_descr *descr;
10523 struct scsi_read_toc *cdb;
10524 uint32_t alloc_len, data_len;
10527 cdb = (struct scsi_read_toc *)ctsio->cdb;
10528 msf = (cdb->byte2 & CD_MSF) != 0;
10529 format = cdb->format;
10530 alloc_len = scsi_2btoul(cdb->data_len);
10532 data_len = sizeof(struct scsi_read_toc_hdr);
10534 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10536 data_len += sizeof(struct scsi_read_toc_type01_descr);
10537 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10538 ctsio->kern_sg_entries = 0;
10539 ctsio->kern_rel_offset = 0;
10540 ctsio->kern_data_len = min(data_len, alloc_len);
10541 ctsio->kern_total_len = ctsio->kern_data_len;
10543 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10545 scsi_ulto2b(0x12, hdr->data_length);
10548 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10549 descr->addr_ctl = 0x14;
10550 descr->track_number = 1;
10552 ctl_ultomsf(0, descr->track_start);
10554 scsi_ulto4b(0, descr->track_start);
10556 descr->addr_ctl = 0x14;
10557 descr->track_number = 0xaa;
10559 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10561 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10563 scsi_ulto2b(0x0a, hdr->data_length);
10566 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10567 descr->addr_ctl = 0x14;
10568 descr->track_number = 1;
10570 ctl_ultomsf(0, descr->track_start);
10572 scsi_ulto4b(0, descr->track_start);
10575 ctl_set_success(ctsio);
10576 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10577 ctsio->be_move_done = ctl_config_move_done;
10578 ctl_datamove((union ctl_io *)ctsio);
10579 return (CTL_RETVAL_COMPLETE);
10583 * For known CDB types, parse the LBA and length.
10586 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10588 if (io->io_hdr.io_type != CTL_IO_SCSI)
10591 switch (io->scsiio.cdb[0]) {
10592 case COMPARE_AND_WRITE: {
10593 struct scsi_compare_and_write *cdb;
10595 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10597 *lba = scsi_8btou64(cdb->addr);
10598 *len = cdb->length;
10603 struct scsi_rw_6 *cdb;
10605 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10607 *lba = scsi_3btoul(cdb->addr);
10608 /* only 5 bits are valid in the most significant address byte */
10610 *len = cdb->length;
10615 struct scsi_rw_10 *cdb;
10617 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10619 *lba = scsi_4btoul(cdb->addr);
10620 *len = scsi_2btoul(cdb->length);
10623 case WRITE_VERIFY_10: {
10624 struct scsi_write_verify_10 *cdb;
10626 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10628 *lba = scsi_4btoul(cdb->addr);
10629 *len = scsi_2btoul(cdb->length);
10634 struct scsi_rw_12 *cdb;
10636 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10638 *lba = scsi_4btoul(cdb->addr);
10639 *len = scsi_4btoul(cdb->length);
10642 case WRITE_VERIFY_12: {
10643 struct scsi_write_verify_12 *cdb;
10645 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10647 *lba = scsi_4btoul(cdb->addr);
10648 *len = scsi_4btoul(cdb->length);
10653 struct scsi_rw_16 *cdb;
10655 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10657 *lba = scsi_8btou64(cdb->addr);
10658 *len = scsi_4btoul(cdb->length);
10661 case WRITE_ATOMIC_16: {
10662 struct scsi_write_atomic_16 *cdb;
10664 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10666 *lba = scsi_8btou64(cdb->addr);
10667 *len = scsi_2btoul(cdb->length);
10670 case WRITE_VERIFY_16: {
10671 struct scsi_write_verify_16 *cdb;
10673 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10675 *lba = scsi_8btou64(cdb->addr);
10676 *len = scsi_4btoul(cdb->length);
10679 case WRITE_SAME_10: {
10680 struct scsi_write_same_10 *cdb;
10682 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10684 *lba = scsi_4btoul(cdb->addr);
10685 *len = scsi_2btoul(cdb->length);
10688 case WRITE_SAME_16: {
10689 struct scsi_write_same_16 *cdb;
10691 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10693 *lba = scsi_8btou64(cdb->addr);
10694 *len = scsi_4btoul(cdb->length);
10698 struct scsi_verify_10 *cdb;
10700 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10702 *lba = scsi_4btoul(cdb->addr);
10703 *len = scsi_2btoul(cdb->length);
10707 struct scsi_verify_12 *cdb;
10709 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10711 *lba = scsi_4btoul(cdb->addr);
10712 *len = scsi_4btoul(cdb->length);
10716 struct scsi_verify_16 *cdb;
10718 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10720 *lba = scsi_8btou64(cdb->addr);
10721 *len = scsi_4btoul(cdb->length);
10729 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10730 struct scsi_get_lba_status *cdb;
10732 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10733 *lba = scsi_8btou64(cdb->addr);
10739 break; /* NOTREACHED */
10746 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10749 uint64_t endlba1, endlba2;
10751 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10752 endlba2 = lba2 + len2 - 1;
10754 if ((endlba1 < lba2) || (endlba2 < lba1))
10755 return (CTL_ACTION_PASS);
10757 return (CTL_ACTION_BLOCK);
10761 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10763 struct ctl_ptr_len_flags *ptrlen;
10764 struct scsi_unmap_desc *buf, *end, *range;
10768 /* If not UNMAP -- go other way. */
10769 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10770 io->scsiio.cdb[0] != UNMAP)
10771 return (CTL_ACTION_ERROR);
10773 /* If UNMAP without data -- block and wait for data. */
10774 ptrlen = (struct ctl_ptr_len_flags *)
10775 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10776 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10777 ptrlen->ptr == NULL)
10778 return (CTL_ACTION_BLOCK);
10780 /* UNMAP with data -- check for collision. */
10781 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10782 end = buf + ptrlen->len / sizeof(*buf);
10783 for (range = buf; range < end; range++) {
10784 lba = scsi_8btou64(range->lba);
10785 len = scsi_4btoul(range->length);
10786 if ((lba < lba2 + len2) && (lba + len > lba2))
10787 return (CTL_ACTION_BLOCK);
10789 return (CTL_ACTION_PASS);
10793 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10795 uint64_t lba1, lba2;
10796 uint64_t len1, len2;
10799 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10800 return (CTL_ACTION_ERROR);
10802 retval = ctl_extent_check_unmap(io1, lba2, len2);
10803 if (retval != CTL_ACTION_ERROR)
10806 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10807 return (CTL_ACTION_ERROR);
10809 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10811 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10815 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10817 uint64_t lba1, lba2;
10818 uint64_t len1, len2;
10820 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10821 return (CTL_ACTION_PASS);
10822 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10823 return (CTL_ACTION_ERROR);
10824 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10825 return (CTL_ACTION_ERROR);
10827 if (lba1 + len1 == lba2)
10828 return (CTL_ACTION_BLOCK);
10829 return (CTL_ACTION_PASS);
10833 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10834 union ctl_io *ooa_io)
10836 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10837 const ctl_serialize_action *serialize_row;
10840 * The initiator attempted multiple untagged commands at the same
10841 * time. Can't do that.
10843 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10844 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10845 && ((pending_io->io_hdr.nexus.targ_port ==
10846 ooa_io->io_hdr.nexus.targ_port)
10847 && (pending_io->io_hdr.nexus.initid ==
10848 ooa_io->io_hdr.nexus.initid))
10849 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10850 CTL_FLAG_STATUS_SENT)) == 0))
10851 return (CTL_ACTION_OVERLAP);
10854 * The initiator attempted to send multiple tagged commands with
10855 * the same ID. (It's fine if different initiators have the same
10858 * Even if all of those conditions are true, we don't kill the I/O
10859 * if the command ahead of us has been aborted. We won't end up
10860 * sending it to the FETD, and it's perfectly legal to resend a
10861 * command with the same tag number as long as the previous
10862 * instance of this tag number has been aborted somehow.
10864 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10865 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10866 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10867 && ((pending_io->io_hdr.nexus.targ_port ==
10868 ooa_io->io_hdr.nexus.targ_port)
10869 && (pending_io->io_hdr.nexus.initid ==
10870 ooa_io->io_hdr.nexus.initid))
10871 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10872 CTL_FLAG_STATUS_SENT)) == 0))
10873 return (CTL_ACTION_OVERLAP_TAG);
10876 * If we get a head of queue tag, SAM-3 says that we should
10877 * immediately execute it.
10879 * What happens if this command would normally block for some other
10880 * reason? e.g. a request sense with a head of queue tag
10881 * immediately after a write. Normally that would block, but this
10882 * will result in its getting executed immediately...
10884 * We currently return "pass" instead of "skip", so we'll end up
10885 * going through the rest of the queue to check for overlapped tags.
10887 * XXX KDM check for other types of blockage first??
10889 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10890 return (CTL_ACTION_PASS);
10893 * Ordered tags have to block until all items ahead of them
10894 * have completed. If we get called with an ordered tag, we always
10895 * block, if something else is ahead of us in the queue.
10897 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10898 return (CTL_ACTION_BLOCK);
10901 * Simple tags get blocked until all head of queue and ordered tags
10902 * ahead of them have completed. I'm lumping untagged commands in
10903 * with simple tags here. XXX KDM is that the right thing to do?
10905 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10906 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10907 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10908 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10909 return (CTL_ACTION_BLOCK);
10911 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10912 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
10913 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
10914 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
10915 pending_io->scsiio.cdb[1], pending_io));
10916 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10917 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
10918 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
10919 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
10920 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
10921 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
10922 ooa_io->scsiio.cdb[1], ooa_io));
10924 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10926 switch (serialize_row[pending_entry->seridx]) {
10927 case CTL_SER_BLOCK:
10928 return (CTL_ACTION_BLOCK);
10929 case CTL_SER_EXTENT:
10930 return (ctl_extent_check(ooa_io, pending_io,
10931 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10932 case CTL_SER_EXTENTOPT:
10933 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10934 SCP_QUEUE_ALG_UNRESTRICTED)
10935 return (ctl_extent_check(ooa_io, pending_io,
10937 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10938 return (CTL_ACTION_PASS);
10939 case CTL_SER_EXTENTSEQ:
10940 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10941 return (ctl_extent_check_seq(ooa_io, pending_io));
10942 return (CTL_ACTION_PASS);
10944 return (CTL_ACTION_PASS);
10945 case CTL_SER_BLOCKOPT:
10946 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10947 SCP_QUEUE_ALG_UNRESTRICTED)
10948 return (CTL_ACTION_BLOCK);
10949 return (CTL_ACTION_PASS);
10951 return (CTL_ACTION_SKIP);
10953 panic("%s: Invalid serialization value %d for %d => %d",
10954 __func__, serialize_row[pending_entry->seridx],
10955 pending_entry->seridx, ooa_entry->seridx);
10958 return (CTL_ACTION_ERROR);
10962 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10964 * - pending_io is generally either incoming, or on the blocked queue
10965 * - starting I/O is the I/O we want to start the check with.
10968 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10969 union ctl_io *starting_io)
10971 union ctl_io *ooa_io;
10974 mtx_assert(&lun->lun_lock, MA_OWNED);
10977 * Run back along the OOA queue, starting with the current
10978 * blocked I/O and going through every I/O before it on the
10979 * queue. If starting_io is NULL, we'll just end up returning
10982 for (ooa_io = starting_io; ooa_io != NULL;
10983 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10987 * This routine just checks to see whether
10988 * cur_blocked is blocked by ooa_io, which is ahead
10989 * of it in the queue. It doesn't queue/dequeue
10992 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10994 case CTL_ACTION_BLOCK:
10995 case CTL_ACTION_OVERLAP:
10996 case CTL_ACTION_OVERLAP_TAG:
10997 case CTL_ACTION_SKIP:
10998 case CTL_ACTION_ERROR:
11000 break; /* NOTREACHED */
11001 case CTL_ACTION_PASS:
11004 panic("%s: Invalid action %d\n", __func__, action);
11008 return (CTL_ACTION_PASS);
11013 * - An I/O has just completed, and has been removed from the per-LUN OOA
11014 * queue, so some items on the blocked queue may now be unblocked.
11017 ctl_check_blocked(struct ctl_lun *lun)
11019 struct ctl_softc *softc = lun->ctl_softc;
11020 union ctl_io *cur_blocked, *next_blocked;
11022 mtx_assert(&lun->lun_lock, MA_OWNED);
11025 * Run forward from the head of the blocked queue, checking each
11026 * entry against the I/Os prior to it on the OOA queue to see if
11027 * there is still any blockage.
11029 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
11030 * with our removing a variable on it while it is traversing the
11033 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
11034 cur_blocked != NULL; cur_blocked = next_blocked) {
11035 union ctl_io *prev_ooa;
11038 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
11041 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
11042 ctl_ooaq, ooa_links);
11045 * If cur_blocked happens to be the first item in the OOA
11046 * queue now, prev_ooa will be NULL, and the action
11047 * returned will just be CTL_ACTION_PASS.
11049 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11052 case CTL_ACTION_BLOCK:
11053 /* Nothing to do here, still blocked */
11055 case CTL_ACTION_OVERLAP:
11056 case CTL_ACTION_OVERLAP_TAG:
11058 * This shouldn't happen! In theory we've already
11059 * checked this command for overlap...
11062 case CTL_ACTION_PASS:
11063 case CTL_ACTION_SKIP: {
11064 const struct ctl_cmd_entry *entry;
11067 * The skip case shouldn't happen, this transaction
11068 * should have never made it onto the blocked queue.
11071 * This I/O is no longer blocked, we can remove it
11072 * from the blocked queue. Since this is a TAILQ
11073 * (doubly linked list), we can do O(1) removals
11074 * from any place on the list.
11076 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11078 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11080 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11081 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11083 * Need to send IO back to original side to
11086 union ctl_ha_msg msg_info;
11088 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11089 msg_info.hdr.original_sc =
11090 cur_blocked->io_hdr.original_sc;
11091 msg_info.hdr.serializing_sc = cur_blocked;
11092 msg_info.hdr.msg_type = CTL_MSG_R2R;
11093 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11094 sizeof(msg_info.hdr), M_NOWAIT);
11097 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11100 * Check this I/O for LUN state changes that may
11101 * have happened while this command was blocked.
11102 * The LUN state may have been changed by a command
11103 * ahead of us in the queue, so we need to re-check
11104 * for any states that can be caused by SCSI
11107 if (ctl_scsiio_lun_check(lun, entry,
11108 &cur_blocked->scsiio) == 0) {
11109 cur_blocked->io_hdr.flags |=
11110 CTL_FLAG_IS_WAS_ON_RTR;
11111 ctl_enqueue_rtr(cur_blocked);
11113 ctl_done(cur_blocked);
11118 * This probably shouldn't happen -- we shouldn't
11119 * get CTL_ACTION_ERROR, or anything else.
11125 return (CTL_RETVAL_COMPLETE);
11129 * This routine (with one exception) checks LUN flags that can be set by
11130 * commands ahead of us in the OOA queue. These flags have to be checked
11131 * when a command initially comes in, and when we pull a command off the
11132 * blocked queue and are preparing to execute it. The reason we have to
11133 * check these flags for commands on the blocked queue is that the LUN
11134 * state may have been changed by a command ahead of us while we're on the
11137 * Ordering is somewhat important with these checks, so please pay
11138 * careful attention to the placement of any new checks.
11141 ctl_scsiio_lun_check(struct ctl_lun *lun,
11142 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11144 struct ctl_softc *softc = lun->ctl_softc;
11150 mtx_assert(&lun->lun_lock, MA_OWNED);
11153 * If this shelf is a secondary shelf controller, we may have to
11154 * reject some commands disallowed by HA mode and link state.
11156 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11157 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11158 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11159 ctl_set_lun_unavail(ctsio);
11163 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11164 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11165 ctl_set_lun_transit(ctsio);
11169 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11170 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11171 ctl_set_lun_standby(ctsio);
11176 /* The rest of checks are only done on executing side */
11177 if (softc->ha_mode == CTL_HA_MODE_XFER)
11181 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11183 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11184 ctl_set_hw_write_protected(ctsio);
11188 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11189 ctl_set_sense(ctsio, /*current_error*/ 1,
11190 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11191 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11198 * Check for a reservation conflict. If this command isn't allowed
11199 * even on reserved LUNs, and if this initiator isn't the one who
11200 * reserved us, reject the command with a reservation conflict.
11202 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11203 if ((lun->flags & CTL_LUN_RESERVED)
11204 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11205 if (lun->res_idx != residx) {
11206 ctl_set_reservation_conflict(ctsio);
11212 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11213 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11214 /* No reservation or command is allowed. */;
11215 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11216 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11217 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11218 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11219 /* The command is allowed for Write Exclusive resv. */;
11222 * if we aren't registered or it's a res holder type
11223 * reservation and this isn't the res holder then set a
11226 if (ctl_get_prkey(lun, residx) == 0 ||
11227 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11228 ctl_set_reservation_conflict(ctsio);
11234 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11235 if (lun->flags & CTL_LUN_EJECTED)
11236 ctl_set_lun_ejected(ctsio);
11237 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11238 if (lun->flags & CTL_LUN_REMOVABLE)
11239 ctl_set_lun_no_media(ctsio);
11241 ctl_set_lun_int_reqd(ctsio);
11242 } else if (lun->flags & CTL_LUN_STOPPED)
11243 ctl_set_lun_stopped(ctsio);
11255 ctl_failover_io(union ctl_io *io, int have_lock)
11257 ctl_set_busy(&io->scsiio);
11262 ctl_failover_lun(union ctl_io *rio)
11264 struct ctl_softc *softc = CTL_SOFTC(rio);
11265 struct ctl_lun *lun;
11266 struct ctl_io_hdr *io, *next_io;
11269 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11270 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11272 /* Find and lock the LUN. */
11273 mtx_lock(&softc->ctl_lock);
11274 if (targ_lun > ctl_max_luns ||
11275 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11276 mtx_unlock(&softc->ctl_lock);
11279 mtx_lock(&lun->lun_lock);
11280 mtx_unlock(&softc->ctl_lock);
11281 if (lun->flags & CTL_LUN_DISABLED) {
11282 mtx_unlock(&lun->lun_lock);
11286 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11287 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11288 /* We are master */
11289 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11290 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11291 io->flags |= CTL_FLAG_ABORT;
11292 io->flags |= CTL_FLAG_FAILOVER;
11293 } else { /* This can be only due to DATAMOVE */
11294 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11295 io->flags &= ~CTL_FLAG_DMA_INPROG;
11296 io->flags |= CTL_FLAG_IO_ACTIVE;
11297 io->port_status = 31340;
11298 ctl_enqueue_isc((union ctl_io *)io);
11302 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11303 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11304 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11305 io->flags |= CTL_FLAG_FAILOVER;
11307 ctl_set_busy(&((union ctl_io *)io)->
11309 ctl_done((union ctl_io *)io);
11313 } else { /* SERIALIZE modes */
11314 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11316 /* We are master */
11317 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11318 TAILQ_REMOVE(&lun->blocked_queue, io,
11320 io->flags &= ~CTL_FLAG_BLOCKED;
11321 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11322 ctl_free_io((union ctl_io *)io);
11325 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11326 /* We are master */
11327 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11328 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11329 ctl_free_io((union ctl_io *)io);
11332 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11333 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11334 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11335 ctl_set_busy(&((union ctl_io *)io)->
11337 ctl_done((union ctl_io *)io);
11341 ctl_check_blocked(lun);
11343 mtx_unlock(&lun->lun_lock);
11347 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11349 struct ctl_lun *lun;
11350 const struct ctl_cmd_entry *entry;
11351 uint32_t initidx, targ_lun;
11355 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11356 if (targ_lun < ctl_max_luns)
11357 lun = softc->ctl_luns[targ_lun];
11360 * If the LUN is invalid, pretend that it doesn't exist.
11361 * It will go away as soon as all pending I/O has been
11364 mtx_lock(&lun->lun_lock);
11365 if (lun->flags & CTL_LUN_DISABLED) {
11366 mtx_unlock(&lun->lun_lock);
11370 CTL_LUN(ctsio) = lun;
11372 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11375 * Every I/O goes into the OOA queue for a particular LUN,
11376 * and stays there until completion.
11379 if (TAILQ_EMPTY(&lun->ooa_queue))
11380 lun->idle_time += getsbinuptime() - lun->last_busy;
11382 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11385 /* Get command entry and return error if it is unsuppotyed. */
11386 entry = ctl_validate_command(ctsio);
11387 if (entry == NULL) {
11389 mtx_unlock(&lun->lun_lock);
11393 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11394 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11397 * Check to see whether we can send this command to LUNs that don't
11398 * exist. This should pretty much only be the case for inquiry
11399 * and request sense. Further checks, below, really require having
11400 * a LUN, so we can't really check the command anymore. Just put
11401 * it on the rtr queue.
11404 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11405 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11406 ctl_enqueue_rtr((union ctl_io *)ctsio);
11410 ctl_set_unsupported_lun(ctsio);
11411 ctl_done((union ctl_io *)ctsio);
11412 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11416 * Make sure we support this particular command on this LUN.
11417 * e.g., we don't support writes to the control LUN.
11419 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11420 mtx_unlock(&lun->lun_lock);
11421 ctl_set_invalid_opcode(ctsio);
11422 ctl_done((union ctl_io *)ctsio);
11427 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11430 * If we've got a request sense, it'll clear the contingent
11431 * allegiance condition. Otherwise, if we have a CA condition for
11432 * this initiator, clear it, because it sent down a command other
11433 * than request sense.
11435 if (ctsio->cdb[0] != REQUEST_SENSE) {
11436 struct scsi_sense_data *ps;
11438 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11440 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11444 * If the command has this flag set, it handles its own unit
11445 * attention reporting, we shouldn't do anything. Otherwise we
11446 * check for any pending unit attentions, and send them back to the
11447 * initiator. We only do this when a command initially comes in,
11448 * not when we pull it off the blocked queue.
11450 * According to SAM-3, section 5.3.2, the order that things get
11451 * presented back to the host is basically unit attentions caused
11452 * by some sort of reset event, busy status, reservation conflicts
11453 * or task set full, and finally any other status.
11455 * One issue here is that some of the unit attentions we report
11456 * don't fall into the "reset" category (e.g. "reported luns data
11457 * has changed"). So reporting it here, before the reservation
11458 * check, may be technically wrong. I guess the only thing to do
11459 * would be to check for and report the reset events here, and then
11460 * check for the other unit attention types after we check for a
11461 * reservation conflict.
11463 * XXX KDM need to fix this
11465 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11466 ctl_ua_type ua_type;
11467 u_int sense_len = 0;
11469 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11470 &sense_len, SSD_TYPE_NONE);
11471 if (ua_type != CTL_UA_NONE) {
11472 mtx_unlock(&lun->lun_lock);
11473 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11474 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11475 ctsio->sense_len = sense_len;
11476 ctl_done((union ctl_io *)ctsio);
11482 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11483 mtx_unlock(&lun->lun_lock);
11484 ctl_done((union ctl_io *)ctsio);
11489 * XXX CHD this is where we want to send IO to other side if
11490 * this LUN is secondary on this SC. We will need to make a copy
11491 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11492 * the copy we send as FROM_OTHER.
11493 * We also need to stuff the address of the original IO so we can
11494 * find it easily. Something similar will need be done on the other
11495 * side so when we are done we can find the copy.
11497 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11498 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11499 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11500 union ctl_ha_msg msg_info;
11503 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11504 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11505 mtx_unlock(&lun->lun_lock);
11507 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11508 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11509 msg_info.hdr.serializing_sc = NULL;
11510 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11511 msg_info.scsi.tag_num = ctsio->tag_num;
11512 msg_info.scsi.tag_type = ctsio->tag_type;
11513 msg_info.scsi.cdb_len = ctsio->cdb_len;
11514 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11516 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11517 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11518 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11519 ctl_set_busy(ctsio);
11520 ctl_done((union ctl_io *)ctsio);
11526 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11527 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11528 ctl_ooaq, ooa_links))) {
11529 case CTL_ACTION_BLOCK:
11530 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11531 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11533 mtx_unlock(&lun->lun_lock);
11535 case CTL_ACTION_PASS:
11536 case CTL_ACTION_SKIP:
11537 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11538 mtx_unlock(&lun->lun_lock);
11539 ctl_enqueue_rtr((union ctl_io *)ctsio);
11541 case CTL_ACTION_OVERLAP:
11542 mtx_unlock(&lun->lun_lock);
11543 ctl_set_overlapped_cmd(ctsio);
11544 ctl_done((union ctl_io *)ctsio);
11546 case CTL_ACTION_OVERLAP_TAG:
11547 mtx_unlock(&lun->lun_lock);
11548 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11549 ctl_done((union ctl_io *)ctsio);
11551 case CTL_ACTION_ERROR:
11553 mtx_unlock(&lun->lun_lock);
11554 ctl_set_internal_failure(ctsio,
11556 /*retry_count*/ 0);
11557 ctl_done((union ctl_io *)ctsio);
11563 const struct ctl_cmd_entry *
11564 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11566 const struct ctl_cmd_entry *entry;
11567 int service_action;
11569 entry = &ctl_cmd_table[ctsio->cdb[0]];
11571 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11572 if (entry->flags & CTL_CMD_FLAG_SA5) {
11573 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11574 entry = &((const struct ctl_cmd_entry *)
11575 entry->execute)[service_action];
11580 const struct ctl_cmd_entry *
11581 ctl_validate_command(struct ctl_scsiio *ctsio)
11583 const struct ctl_cmd_entry *entry;
11587 entry = ctl_get_cmd_entry(ctsio, &sa);
11588 if (entry->execute == NULL) {
11590 ctl_set_invalid_field(ctsio,
11597 ctl_set_invalid_opcode(ctsio);
11598 ctl_done((union ctl_io *)ctsio);
11601 KASSERT(entry->length > 0,
11602 ("Not defined length for command 0x%02x/0x%02x",
11603 ctsio->cdb[0], ctsio->cdb[1]));
11604 for (i = 1; i < entry->length; i++) {
11605 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11608 ctl_set_invalid_field(ctsio,
11613 /*bit*/ fls(diff) - 1);
11614 ctl_done((union ctl_io *)ctsio);
11621 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11624 switch (lun_type) {
11626 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11630 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11634 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11644 ctl_scsiio(struct ctl_scsiio *ctsio)
11647 const struct ctl_cmd_entry *entry;
11649 retval = CTL_RETVAL_COMPLETE;
11651 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11653 entry = ctl_get_cmd_entry(ctsio, NULL);
11656 * If this I/O has been aborted, just send it straight to
11657 * ctl_done() without executing it.
11659 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11660 ctl_done((union ctl_io *)ctsio);
11665 * All the checks should have been handled by ctl_scsiio_precheck().
11666 * We should be clear now to just execute the I/O.
11668 retval = entry->execute(ctsio);
11675 ctl_target_reset(union ctl_io *io)
11677 struct ctl_softc *softc = CTL_SOFTC(io);
11678 struct ctl_port *port = CTL_PORT(io);
11679 struct ctl_lun *lun;
11681 ctl_ua_type ua_type;
11683 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11684 union ctl_ha_msg msg_info;
11686 msg_info.hdr.nexus = io->io_hdr.nexus;
11687 msg_info.task.task_action = io->taskio.task_action;
11688 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11689 msg_info.hdr.original_sc = NULL;
11690 msg_info.hdr.serializing_sc = NULL;
11691 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11692 sizeof(msg_info.task), M_WAITOK);
11695 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11696 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11697 ua_type = CTL_UA_TARG_RESET;
11699 ua_type = CTL_UA_BUS_RESET;
11700 mtx_lock(&softc->ctl_lock);
11701 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11702 if (port != NULL &&
11703 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11705 ctl_do_lun_reset(lun, initidx, ua_type);
11707 mtx_unlock(&softc->ctl_lock);
11708 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11713 * The LUN should always be set. The I/O is optional, and is used to
11714 * distinguish between I/Os sent by this initiator, and by other
11715 * initiators. We set unit attention for initiators other than this one.
11716 * SAM-3 is vague on this point. It does say that a unit attention should
11717 * be established for other initiators when a LUN is reset (see section
11718 * 5.7.3), but it doesn't specifically say that the unit attention should
11719 * be established for this particular initiator when a LUN is reset. Here
11720 * is the relevant text, from SAM-3 rev 8:
11722 * 5.7.2 When a SCSI initiator port aborts its own tasks
11724 * When a SCSI initiator port causes its own task(s) to be aborted, no
11725 * notification that the task(s) have been aborted shall be returned to
11726 * the SCSI initiator port other than the completion response for the
11727 * command or task management function action that caused the task(s) to
11728 * be aborted and notification(s) associated with related effects of the
11729 * action (e.g., a reset unit attention condition).
11731 * XXX KDM for now, we're setting unit attention for all initiators.
11734 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11739 mtx_lock(&lun->lun_lock);
11741 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11742 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11743 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11746 for (i = 0; i < ctl_max_ports; i++) {
11747 free(lun->pending_sense[i], M_CTL);
11748 lun->pending_sense[i] = NULL;
11750 /* Clear reservation. */
11751 lun->flags &= ~CTL_LUN_RESERVED;
11752 /* Clear prevent media removal. */
11753 if (lun->prevent) {
11754 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11755 ctl_clear_mask(lun->prevent, i);
11756 lun->prevent_count = 0;
11758 /* Clear TPC status */
11759 ctl_tpc_lun_clear(lun, -1);
11760 /* Establish UA. */
11762 ctl_est_ua_all(lun, initidx, ua_type);
11764 ctl_est_ua_all(lun, -1, ua_type);
11766 mtx_unlock(&lun->lun_lock);
11770 ctl_lun_reset(union ctl_io *io)
11772 struct ctl_softc *softc = CTL_SOFTC(io);
11773 struct ctl_lun *lun;
11774 uint32_t targ_lun, initidx;
11776 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11777 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11778 mtx_lock(&softc->ctl_lock);
11779 if (targ_lun >= ctl_max_luns ||
11780 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11781 mtx_unlock(&softc->ctl_lock);
11782 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11785 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11786 mtx_unlock(&softc->ctl_lock);
11787 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11789 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11790 union ctl_ha_msg msg_info;
11792 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11793 msg_info.hdr.nexus = io->io_hdr.nexus;
11794 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11795 msg_info.hdr.original_sc = NULL;
11796 msg_info.hdr.serializing_sc = NULL;
11797 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11798 sizeof(msg_info.task), M_WAITOK);
11804 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11809 mtx_assert(&lun->lun_lock, MA_OWNED);
11812 * Run through the OOA queue and attempt to find the given I/O.
11813 * The target port, initiator ID, tag type and tag number have to
11814 * match the values that we got from the initiator. If we have an
11815 * untagged command to abort, simply abort the first untagged command
11816 * we come to. We only allow one untagged command at a time of course.
11818 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11819 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11821 if ((targ_port == UINT32_MAX ||
11822 targ_port == xio->io_hdr.nexus.targ_port) &&
11823 (init_id == UINT32_MAX ||
11824 init_id == xio->io_hdr.nexus.initid)) {
11825 if (targ_port != xio->io_hdr.nexus.targ_port ||
11826 init_id != xio->io_hdr.nexus.initid)
11827 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11828 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11829 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11830 union ctl_ha_msg msg_info;
11832 msg_info.hdr.nexus = xio->io_hdr.nexus;
11833 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11834 msg_info.task.tag_num = xio->scsiio.tag_num;
11835 msg_info.task.tag_type = xio->scsiio.tag_type;
11836 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11837 msg_info.hdr.original_sc = NULL;
11838 msg_info.hdr.serializing_sc = NULL;
11839 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11840 sizeof(msg_info.task), M_NOWAIT);
11847 ctl_abort_task_set(union ctl_io *io)
11849 struct ctl_softc *softc = CTL_SOFTC(io);
11850 struct ctl_lun *lun;
11856 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11857 mtx_lock(&softc->ctl_lock);
11858 if (targ_lun >= ctl_max_luns ||
11859 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11860 mtx_unlock(&softc->ctl_lock);
11861 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11865 mtx_lock(&lun->lun_lock);
11866 mtx_unlock(&softc->ctl_lock);
11867 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11868 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11869 io->io_hdr.nexus.initid,
11870 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11871 } else { /* CTL_TASK_CLEAR_TASK_SET */
11872 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11873 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11875 mtx_unlock(&lun->lun_lock);
11876 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11881 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11882 ctl_ua_type ua_type)
11884 struct ctl_lun *lun;
11885 struct scsi_sense_data *ps;
11888 p = initidx / CTL_MAX_INIT_PER_PORT;
11889 i = initidx % CTL_MAX_INIT_PER_PORT;
11890 mtx_lock(&softc->ctl_lock);
11891 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11892 mtx_lock(&lun->lun_lock);
11894 ctl_abort_tasks_lun(lun, p, i, 1);
11896 ps = lun->pending_sense[p];
11898 ps[i].error_code = 0;
11899 /* Clear reservation. */
11900 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11901 lun->flags &= ~CTL_LUN_RESERVED;
11902 /* Clear prevent media removal. */
11903 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11904 ctl_clear_mask(lun->prevent, initidx);
11905 lun->prevent_count--;
11907 /* Clear TPC status */
11908 ctl_tpc_lun_clear(lun, initidx);
11909 /* Establish UA. */
11910 ctl_est_ua(lun, initidx, ua_type);
11911 mtx_unlock(&lun->lun_lock);
11913 mtx_unlock(&softc->ctl_lock);
11917 ctl_i_t_nexus_reset(union ctl_io *io)
11919 struct ctl_softc *softc = CTL_SOFTC(io);
11922 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11923 union ctl_ha_msg msg_info;
11925 msg_info.hdr.nexus = io->io_hdr.nexus;
11926 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11927 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11928 msg_info.hdr.original_sc = NULL;
11929 msg_info.hdr.serializing_sc = NULL;
11930 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11931 sizeof(msg_info.task), M_WAITOK);
11934 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11935 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
11936 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11941 ctl_abort_task(union ctl_io *io)
11943 struct ctl_softc *softc = CTL_SOFTC(io);
11945 struct ctl_lun *lun;
11948 char printbuf[128];
11958 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11959 mtx_lock(&softc->ctl_lock);
11960 if (targ_lun >= ctl_max_luns ||
11961 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11962 mtx_unlock(&softc->ctl_lock);
11963 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11968 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11969 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11972 mtx_lock(&lun->lun_lock);
11973 mtx_unlock(&softc->ctl_lock);
11975 * Run through the OOA queue and attempt to find the given I/O.
11976 * The target port, initiator ID, tag type and tag number have to
11977 * match the values that we got from the initiator. If we have an
11978 * untagged command to abort, simply abort the first untagged command
11979 * we come to. We only allow one untagged command at a time of course.
11981 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11982 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11984 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11986 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11987 lun->lun, xio->scsiio.tag_num,
11988 xio->scsiio.tag_type,
11989 (xio->io_hdr.blocked_links.tqe_prev
11990 == NULL) ? "" : " BLOCKED",
11991 (xio->io_hdr.flags &
11992 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11993 (xio->io_hdr.flags &
11994 CTL_FLAG_ABORT) ? " ABORT" : "",
11995 (xio->io_hdr.flags &
11996 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11997 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11999 printf("%s\n", sbuf_data(&sb));
12002 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12003 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12004 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12008 * If the abort says that the task is untagged, the
12009 * task in the queue must be untagged. Otherwise,
12010 * we just check to see whether the tag numbers
12011 * match. This is because the QLogic firmware
12012 * doesn't pass back the tag type in an abort
12016 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12017 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12018 || (xio->scsiio.tag_num == io->taskio.tag_num))
12021 * XXX KDM we've got problems with FC, because it
12022 * doesn't send down a tag type with aborts. So we
12023 * can only really go by the tag number...
12024 * This may cause problems with parallel SCSI.
12025 * Need to figure that out!!
12027 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12028 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12030 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12031 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12032 union ctl_ha_msg msg_info;
12034 msg_info.hdr.nexus = io->io_hdr.nexus;
12035 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12036 msg_info.task.tag_num = io->taskio.tag_num;
12037 msg_info.task.tag_type = io->taskio.tag_type;
12038 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12039 msg_info.hdr.original_sc = NULL;
12040 msg_info.hdr.serializing_sc = NULL;
12042 printf("Sent Abort to other side\n");
12044 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12045 sizeof(msg_info.task), M_NOWAIT);
12048 printf("ctl_abort_task: found I/O to abort\n");
12052 mtx_unlock(&lun->lun_lock);
12056 * This isn't really an error. It's entirely possible for
12057 * the abort and command completion to cross on the wire.
12058 * This is more of an informative/diagnostic error.
12061 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12062 "%u:%u:%u tag %d type %d\n",
12063 io->io_hdr.nexus.initid,
12064 io->io_hdr.nexus.targ_port,
12065 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12066 io->taskio.tag_type);
12069 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12074 ctl_query_task(union ctl_io *io, int task_set)
12076 struct ctl_softc *softc = CTL_SOFTC(io);
12078 struct ctl_lun *lun;
12082 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12083 mtx_lock(&softc->ctl_lock);
12084 if (targ_lun >= ctl_max_luns ||
12085 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12086 mtx_unlock(&softc->ctl_lock);
12087 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12090 mtx_lock(&lun->lun_lock);
12091 mtx_unlock(&softc->ctl_lock);
12092 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12093 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12095 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12096 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12097 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12100 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12105 mtx_unlock(&lun->lun_lock);
12107 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12109 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12114 ctl_query_async_event(union ctl_io *io)
12116 struct ctl_softc *softc = CTL_SOFTC(io);
12117 struct ctl_lun *lun;
12119 uint32_t targ_lun, initidx;
12121 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12122 mtx_lock(&softc->ctl_lock);
12123 if (targ_lun >= ctl_max_luns ||
12124 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12125 mtx_unlock(&softc->ctl_lock);
12126 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12129 mtx_lock(&lun->lun_lock);
12130 mtx_unlock(&softc->ctl_lock);
12131 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12132 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12133 mtx_unlock(&lun->lun_lock);
12134 if (ua != CTL_UA_NONE)
12135 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12137 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12142 ctl_run_task(union ctl_io *io)
12146 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12147 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12148 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12149 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12150 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12151 switch (io->taskio.task_action) {
12152 case CTL_TASK_ABORT_TASK:
12153 retval = ctl_abort_task(io);
12155 case CTL_TASK_ABORT_TASK_SET:
12156 case CTL_TASK_CLEAR_TASK_SET:
12157 retval = ctl_abort_task_set(io);
12159 case CTL_TASK_CLEAR_ACA:
12161 case CTL_TASK_I_T_NEXUS_RESET:
12162 retval = ctl_i_t_nexus_reset(io);
12164 case CTL_TASK_LUN_RESET:
12165 retval = ctl_lun_reset(io);
12167 case CTL_TASK_TARGET_RESET:
12168 case CTL_TASK_BUS_RESET:
12169 retval = ctl_target_reset(io);
12171 case CTL_TASK_PORT_LOGIN:
12173 case CTL_TASK_PORT_LOGOUT:
12175 case CTL_TASK_QUERY_TASK:
12176 retval = ctl_query_task(io, 0);
12178 case CTL_TASK_QUERY_TASK_SET:
12179 retval = ctl_query_task(io, 1);
12181 case CTL_TASK_QUERY_ASYNC_EVENT:
12182 retval = ctl_query_async_event(io);
12185 printf("%s: got unknown task management event %d\n",
12186 __func__, io->taskio.task_action);
12190 io->io_hdr.status = CTL_SUCCESS;
12192 io->io_hdr.status = CTL_ERROR;
12197 * For HA operation. Handle commands that come in from the other
12201 ctl_handle_isc(union ctl_io *io)
12203 struct ctl_softc *softc = CTL_SOFTC(io);
12204 struct ctl_lun *lun;
12205 const struct ctl_cmd_entry *entry;
12208 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12209 switch (io->io_hdr.msg_type) {
12210 case CTL_MSG_SERIALIZE:
12211 ctl_serialize_other_sc_cmd(&io->scsiio);
12213 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12214 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12215 if (targ_lun >= ctl_max_luns ||
12216 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12220 mtx_lock(&lun->lun_lock);
12221 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12222 mtx_unlock(&lun->lun_lock);
12226 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12227 mtx_unlock(&lun->lun_lock);
12228 ctl_enqueue_rtr(io);
12230 case CTL_MSG_FINISH_IO:
12231 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12235 if (targ_lun >= ctl_max_luns ||
12236 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12240 mtx_lock(&lun->lun_lock);
12241 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12242 ctl_check_blocked(lun);
12243 mtx_unlock(&lun->lun_lock);
12246 case CTL_MSG_PERS_ACTION:
12247 ctl_hndl_per_res_out_on_other_sc(io);
12250 case CTL_MSG_BAD_JUJU:
12253 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12254 ctl_datamove_remote(io);
12256 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12257 io->scsiio.be_move_done(io);
12259 case CTL_MSG_FAILOVER:
12260 ctl_failover_lun(io);
12264 printf("%s: Invalid message type %d\n",
12265 __func__, io->io_hdr.msg_type);
12274 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12275 * there is no match.
12277 static ctl_lun_error_pattern
12278 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12280 const struct ctl_cmd_entry *entry;
12281 ctl_lun_error_pattern filtered_pattern, pattern;
12283 pattern = desc->error_pattern;
12286 * XXX KDM we need more data passed into this function to match a
12287 * custom pattern, and we actually need to implement custom pattern
12290 if (pattern & CTL_LUN_PAT_CMD)
12291 return (CTL_LUN_PAT_CMD);
12293 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12294 return (CTL_LUN_PAT_ANY);
12296 entry = ctl_get_cmd_entry(ctsio, NULL);
12298 filtered_pattern = entry->pattern & pattern;
12301 * If the user requested specific flags in the pattern (e.g.
12302 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12305 * If the user did not specify any flags, it doesn't matter whether
12306 * or not the command supports the flags.
12308 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12309 (pattern & ~CTL_LUN_PAT_MASK))
12310 return (CTL_LUN_PAT_NONE);
12313 * If the user asked for a range check, see if the requested LBA
12314 * range overlaps with this command's LBA range.
12316 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12322 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12324 return (CTL_LUN_PAT_NONE);
12326 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12327 desc->lba_range.len, FALSE);
12329 * A "pass" means that the LBA ranges don't overlap, so
12330 * this doesn't match the user's range criteria.
12332 if (action == CTL_ACTION_PASS)
12333 return (CTL_LUN_PAT_NONE);
12336 return (filtered_pattern);
12340 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12342 struct ctl_error_desc *desc, *desc2;
12344 mtx_assert(&lun->lun_lock, MA_OWNED);
12346 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12347 ctl_lun_error_pattern pattern;
12349 * Check to see whether this particular command matches
12350 * the pattern in the descriptor.
12352 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12353 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12356 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12357 case CTL_LUN_INJ_ABORTED:
12358 ctl_set_aborted(&io->scsiio);
12360 case CTL_LUN_INJ_MEDIUM_ERR:
12361 ctl_set_medium_error(&io->scsiio,
12362 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12363 CTL_FLAG_DATA_OUT);
12365 case CTL_LUN_INJ_UA:
12366 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12368 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12370 case CTL_LUN_INJ_CUSTOM:
12372 * We're assuming the user knows what he is doing.
12373 * Just copy the sense information without doing
12376 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12377 MIN(sizeof(desc->custom_sense),
12378 sizeof(io->scsiio.sense_data)));
12379 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12380 io->scsiio.sense_len = SSD_FULL_SIZE;
12381 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12383 case CTL_LUN_INJ_NONE:
12386 * If this is an error injection type we don't know
12387 * about, clear the continuous flag (if it is set)
12388 * so it will get deleted below.
12390 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12394 * By default, each error injection action is a one-shot
12396 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12399 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12405 #ifdef CTL_IO_DELAY
12407 ctl_datamove_timer_wakeup(void *arg)
12411 io = (union ctl_io *)arg;
12415 #endif /* CTL_IO_DELAY */
12418 ctl_datamove(union ctl_io *io)
12420 void (*fe_datamove)(union ctl_io *io);
12422 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12424 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12426 /* No data transferred yet. Frontend must update this when done. */
12427 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12430 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12435 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12436 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12438 sbuf_cat(&sb, path_str);
12439 switch (io->io_hdr.io_type) {
12441 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12442 sbuf_printf(&sb, "\n");
12443 sbuf_cat(&sb, path_str);
12444 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12445 io->scsiio.tag_num, io->scsiio.tag_type);
12448 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12449 "Tag Type: %d\n", io->taskio.task_action,
12450 io->taskio.tag_num, io->taskio.tag_type);
12453 panic("%s: Invalid CTL I/O type %d\n",
12454 __func__, io->io_hdr.io_type);
12456 sbuf_cat(&sb, path_str);
12457 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12458 (intmax_t)time_uptime - io->io_hdr.start_time);
12460 printf("%s", sbuf_data(&sb));
12462 #endif /* CTL_TIME_IO */
12464 #ifdef CTL_IO_DELAY
12465 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12466 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12468 struct ctl_lun *lun;
12472 && (lun->delay_info.datamove_delay > 0)) {
12474 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12475 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12476 callout_reset(&io->io_hdr.delay_callout,
12477 lun->delay_info.datamove_delay * hz,
12478 ctl_datamove_timer_wakeup, io);
12479 if (lun->delay_info.datamove_type ==
12480 CTL_DELAY_TYPE_ONESHOT)
12481 lun->delay_info.datamove_delay = 0;
12488 * This command has been aborted. Set the port status, so we fail
12491 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12492 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12493 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12494 io->io_hdr.nexus.targ_port,
12495 io->io_hdr.nexus.targ_lun);
12496 io->io_hdr.port_status = 31337;
12498 * Note that the backend, in this case, will get the
12499 * callback in its context. In other cases it may get
12500 * called in the frontend's interrupt thread context.
12502 io->scsiio.be_move_done(io);
12506 /* Don't confuse frontend with zero length data move. */
12507 if (io->scsiio.kern_data_len == 0) {
12508 io->scsiio.be_move_done(io);
12512 fe_datamove = CTL_PORT(io)->fe_datamove;
12517 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12519 union ctl_ha_msg msg;
12521 struct bintime cur_bt;
12524 memset(&msg, 0, sizeof(msg));
12525 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12526 msg.hdr.original_sc = io;
12527 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
12528 msg.hdr.nexus = io->io_hdr.nexus;
12529 msg.hdr.status = io->io_hdr.status;
12530 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12531 msg.scsi.tag_num = io->scsiio.tag_num;
12532 msg.scsi.tag_type = io->scsiio.tag_type;
12533 msg.scsi.scsi_status = io->scsiio.scsi_status;
12534 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12535 io->scsiio.sense_len);
12536 msg.scsi.sense_len = io->scsiio.sense_len;
12537 msg.scsi.port_status = io->io_hdr.port_status;
12538 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12539 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12540 ctl_failover_io(io, /*have_lock*/ have_lock);
12543 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12544 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12545 msg.scsi.sense_len, M_WAITOK);
12548 getbinuptime(&cur_bt);
12549 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12550 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12552 io->io_hdr.num_dmas++;
12556 * The DMA to the remote side is done, now we need to tell the other side
12557 * we're done so it can continue with its data movement.
12560 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12567 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12568 printf("%s: ISC DMA write failed with error %d", __func__,
12570 ctl_set_internal_failure(&io->scsiio,
12572 /*retry_count*/ rq->ret);
12575 ctl_dt_req_free(rq);
12577 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12578 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12579 free(io->io_hdr.remote_sglist, M_CTL);
12580 io->io_hdr.remote_sglist = NULL;
12581 io->io_hdr.local_sglist = NULL;
12584 * The data is in local and remote memory, so now we need to send
12585 * status (good or back) back to the other side.
12587 ctl_send_datamove_done(io, /*have_lock*/ 0);
12591 * We've moved the data from the host/controller into local memory. Now we
12592 * need to push it over to the remote controller's memory.
12595 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12599 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12600 ctl_datamove_remote_write_cb);
12605 ctl_datamove_remote_write(union ctl_io *io)
12608 void (*fe_datamove)(union ctl_io *io);
12611 * - Get the data from the host/HBA into local memory.
12612 * - DMA memory from the local controller to the remote controller.
12613 * - Send status back to the remote controller.
12616 retval = ctl_datamove_remote_sgl_setup(io);
12620 /* Switch the pointer over so the FETD knows what to do */
12621 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12624 * Use a custom move done callback, since we need to send completion
12625 * back to the other controller, not to the backend on this side.
12627 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12629 fe_datamove = CTL_PORT(io)->fe_datamove;
12634 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12643 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12644 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12645 free(io->io_hdr.remote_sglist, M_CTL);
12646 io->io_hdr.remote_sglist = NULL;
12647 io->io_hdr.local_sglist = NULL;
12650 scsi_path_string(io, path_str, sizeof(path_str));
12651 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12652 sbuf_cat(&sb, path_str);
12653 scsi_command_string(&io->scsiio, NULL, &sb);
12654 sbuf_printf(&sb, "\n");
12655 sbuf_cat(&sb, path_str);
12656 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12657 io->scsiio.tag_num, io->scsiio.tag_type);
12658 sbuf_cat(&sb, path_str);
12659 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12660 io->io_hdr.flags, io->io_hdr.status);
12662 printk("%s", sbuf_data(&sb));
12667 * The read is done, now we need to send status (good or bad) back
12668 * to the other side.
12670 ctl_send_datamove_done(io, /*have_lock*/ 0);
12676 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12679 void (*fe_datamove)(union ctl_io *io);
12683 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12684 printf("%s: ISC DMA read failed with error %d\n", __func__,
12686 ctl_set_internal_failure(&io->scsiio,
12688 /*retry_count*/ rq->ret);
12691 ctl_dt_req_free(rq);
12693 /* Switch the pointer over so the FETD knows what to do */
12694 io->scsiio.kern_data_ptr = (uint8_t *)io->io_hdr.local_sglist;
12697 * Use a custom move done callback, since we need to send completion
12698 * back to the other controller, not to the backend on this side.
12700 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12702 /* XXX KDM add checks like the ones in ctl_datamove? */
12704 fe_datamove = CTL_PORT(io)->fe_datamove;
12709 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12711 struct ctl_sg_entry *local_sglist;
12712 uint32_t len_to_go;
12717 local_sglist = io->io_hdr.local_sglist;
12718 len_to_go = io->scsiio.kern_data_len;
12721 * The difficult thing here is that the size of the various
12722 * S/G segments may be different than the size from the
12723 * remote controller. That'll make it harder when DMAing
12724 * the data back to the other side.
12726 for (i = 0; len_to_go > 0; i++) {
12727 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12728 local_sglist[i].addr =
12729 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12731 len_to_go -= local_sglist[i].len;
12734 * Reset the number of S/G entries accordingly. The original
12735 * number of S/G entries is available in rem_sg_entries.
12737 io->scsiio.kern_sg_entries = i;
12740 printf("%s: kern_sg_entries = %d\n", __func__,
12741 io->scsiio.kern_sg_entries);
12742 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12743 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12744 local_sglist[i].addr, local_sglist[i].len);
12751 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12752 ctl_ha_dt_cb callback)
12754 struct ctl_ha_dt_req *rq;
12755 struct ctl_sg_entry *remote_sglist, *local_sglist;
12756 uint32_t local_used, remote_used, total_used;
12759 rq = ctl_dt_req_alloc();
12762 * If we failed to allocate the request, and if the DMA didn't fail
12763 * anyway, set busy status. This is just a resource allocation
12767 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12768 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12769 ctl_set_busy(&io->scsiio);
12771 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12772 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12775 ctl_dt_req_free(rq);
12778 * The data move failed. We need to return status back
12779 * to the other controller. No point in trying to DMA
12780 * data to the remote controller.
12783 ctl_send_datamove_done(io, /*have_lock*/ 0);
12788 local_sglist = io->io_hdr.local_sglist;
12789 remote_sglist = io->io_hdr.remote_sglist;
12795 * Pull/push the data over the wire from/to the other controller.
12796 * This takes into account the possibility that the local and
12797 * remote sglists may not be identical in terms of the size of
12798 * the elements and the number of elements.
12800 * One fundamental assumption here is that the length allocated for
12801 * both the local and remote sglists is identical. Otherwise, we've
12802 * essentially got a coding error of some sort.
12804 isc_ret = CTL_HA_STATUS_SUCCESS;
12805 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12809 rq->command = command;
12813 * Both pointers should be aligned. But it is possible
12814 * that the allocation length is not. They should both
12815 * also have enough slack left over at the end, though,
12816 * to round up to the next 8 byte boundary.
12818 cur_len = MIN(local_sglist[i].len - local_used,
12819 remote_sglist[j].len - remote_used);
12820 rq->size = cur_len;
12822 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12823 tmp_ptr += local_used;
12826 /* Use physical addresses when talking to ISC hardware */
12827 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12828 /* XXX KDM use busdma */
12829 rq->local = vtophys(tmp_ptr);
12831 rq->local = tmp_ptr;
12833 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12834 ("HA does not support BUS_ADDR"));
12835 rq->local = tmp_ptr;
12838 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12839 tmp_ptr += remote_used;
12840 rq->remote = tmp_ptr;
12842 rq->callback = NULL;
12844 local_used += cur_len;
12845 if (local_used >= local_sglist[i].len) {
12850 remote_used += cur_len;
12851 if (remote_used >= remote_sglist[j].len) {
12855 total_used += cur_len;
12857 if (total_used >= io->scsiio.kern_data_len)
12858 rq->callback = callback;
12861 printf("%s: %s: local %p remote %p size %d\n", __func__,
12862 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12863 rq->local, rq->remote, rq->size);
12866 isc_ret = ctl_dt_single(rq);
12867 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12870 if (isc_ret != CTL_HA_STATUS_WAIT) {
12879 ctl_datamove_remote_read(union ctl_io *io)
12885 * This will send an error to the other controller in the case of a
12888 retval = ctl_datamove_remote_sgl_setup(io);
12892 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12893 ctl_datamove_remote_read_cb);
12896 * Make sure we free memory if there was an error.. The
12897 * ctl_datamove_remote_xfer() function will send the
12898 * datamove done message, or call the callback with an
12899 * error if there is a problem.
12901 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12902 free(io->io_hdr.local_sglist[i].addr, M_CTL);
12903 free(io->io_hdr.remote_sglist, M_CTL);
12904 io->io_hdr.remote_sglist = NULL;
12905 io->io_hdr.local_sglist = NULL;
12910 * Process a datamove request from the other controller. This is used for
12911 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12912 * first. Once that is complete, the data gets DMAed into the remote
12913 * controller's memory. For reads, we DMA from the remote controller's
12914 * memory into our memory first, and then move it out to the FETD.
12917 ctl_datamove_remote(union ctl_io *io)
12920 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12922 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12923 ctl_failover_io(io, /*have_lock*/ 0);
12928 * Note that we look for an aborted I/O here, but don't do some of
12929 * the other checks that ctl_datamove() normally does.
12930 * We don't need to run the datamove delay code, since that should
12931 * have been done if need be on the other controller.
12933 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12934 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12935 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12936 io->io_hdr.nexus.targ_port,
12937 io->io_hdr.nexus.targ_lun);
12938 io->io_hdr.port_status = 31338;
12939 ctl_send_datamove_done(io, /*have_lock*/ 0);
12943 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12944 ctl_datamove_remote_write(io);
12945 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12946 ctl_datamove_remote_read(io);
12948 io->io_hdr.port_status = 31339;
12949 ctl_send_datamove_done(io, /*have_lock*/ 0);
12954 ctl_process_done(union ctl_io *io)
12956 struct ctl_softc *softc = CTL_SOFTC(io);
12957 struct ctl_port *port = CTL_PORT(io);
12958 struct ctl_lun *lun = CTL_LUN(io);
12959 void (*fe_done)(union ctl_io *io);
12960 union ctl_ha_msg msg;
12962 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12963 fe_done = port->fe_done;
12966 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12971 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12972 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12974 sbuf_cat(&sb, path_str);
12975 switch (io->io_hdr.io_type) {
12977 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12978 sbuf_printf(&sb, "\n");
12979 sbuf_cat(&sb, path_str);
12980 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12981 io->scsiio.tag_num, io->scsiio.tag_type);
12984 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12985 "Tag Type: %d\n", io->taskio.task_action,
12986 io->taskio.tag_num, io->taskio.tag_type);
12989 panic("%s: Invalid CTL I/O type %d\n",
12990 __func__, io->io_hdr.io_type);
12992 sbuf_cat(&sb, path_str);
12993 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12994 (intmax_t)time_uptime - io->io_hdr.start_time);
12996 printf("%s", sbuf_data(&sb));
12998 #endif /* CTL_TIME_IO */
13000 switch (io->io_hdr.io_type) {
13004 if (ctl_debug & CTL_DEBUG_INFO)
13005 ctl_io_error_print(io, NULL);
13009 panic("%s: Invalid CTL I/O type %d\n",
13010 __func__, io->io_hdr.io_type);
13014 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13015 io->io_hdr.nexus.targ_mapped_lun));
13019 mtx_lock(&lun->lun_lock);
13022 * Check to see if we have any informational exception and status
13023 * of this command can be modified to report it in form of either
13024 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13026 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13027 io->io_hdr.status == CTL_SUCCESS &&
13028 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13029 uint8_t mrie = lun->MODE_IE.mrie;
13030 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13031 (lun->MODE_VER.byte3 & SMS_VER_PER));
13032 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13033 mrie == SIEP_MRIE_REC_UNCOND ||
13034 mrie == SIEP_MRIE_NO_SENSE) &&
13035 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13036 CTL_CMD_FLAG_NO_SENSE) == 0) {
13037 ctl_set_sense(&io->scsiio,
13038 /*current_error*/ 1,
13039 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13040 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13041 /*asc*/ lun->ie_asc,
13042 /*ascq*/ lun->ie_ascq,
13044 lun->ie_reported = 1;
13046 } else if (lun->ie_reported < 0)
13047 lun->ie_reported = 0;
13050 * Check to see if we have any errors to inject here. We only
13051 * inject errors for commands that don't already have errors set.
13053 if (!STAILQ_EMPTY(&lun->error_list) &&
13054 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13055 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13056 ctl_inject_error(lun, io);
13059 * XXX KDM how do we treat commands that aren't completed
13062 * XXX KDM should we also track I/O latency?
13064 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13065 io->io_hdr.io_type == CTL_IO_SCSI) {
13071 bintime_sub(&bt, &io->io_hdr.start_bt);
13073 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13075 type = CTL_STATS_READ;
13076 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13078 type = CTL_STATS_WRITE;
13080 type = CTL_STATS_NO_IO;
13082 #ifdef CTL_LEGACY_STATS
13083 uint32_t targ_port = port->targ_port;
13084 lun->legacy_stats.ports[targ_port].bytes[type] +=
13085 io->scsiio.kern_total_len;
13086 lun->legacy_stats.ports[targ_port].operations[type] ++;
13087 lun->legacy_stats.ports[targ_port].num_dmas[type] +=
13088 io->io_hdr.num_dmas;
13090 bintime_add(&lun->legacy_stats.ports[targ_port].dma_time[type],
13091 &io->io_hdr.dma_bt);
13092 bintime_add(&lun->legacy_stats.ports[targ_port].time[type],
13095 #endif /* CTL_LEGACY_STATS */
13097 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13098 lun->stats.operations[type] ++;
13099 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13101 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13102 bintime_add(&lun->stats.time[type], &bt);
13105 mtx_lock(&port->port_lock);
13106 port->stats.bytes[type] += io->scsiio.kern_total_len;
13107 port->stats.operations[type] ++;
13108 port->stats.dmas[type] += io->io_hdr.num_dmas;
13110 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13111 bintime_add(&port->stats.time[type], &bt);
13113 mtx_unlock(&port->port_lock);
13117 * Remove this from the OOA queue.
13119 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13121 if (TAILQ_EMPTY(&lun->ooa_queue))
13122 lun->last_busy = getsbinuptime();
13126 * Run through the blocked queue on this LUN and see if anything
13127 * has become unblocked, now that this transaction is done.
13129 ctl_check_blocked(lun);
13132 * If the LUN has been invalidated, free it if there is nothing
13133 * left on its OOA queue.
13135 if ((lun->flags & CTL_LUN_INVALID)
13136 && TAILQ_EMPTY(&lun->ooa_queue)) {
13137 mtx_unlock(&lun->lun_lock);
13140 mtx_unlock(&lun->lun_lock);
13145 * If this command has been aborted, make sure we set the status
13146 * properly. The FETD is responsible for freeing the I/O and doing
13147 * whatever it needs to do to clean up its state.
13149 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13150 ctl_set_task_aborted(&io->scsiio);
13153 * If enabled, print command error status.
13155 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13156 (ctl_debug & CTL_DEBUG_INFO) != 0)
13157 ctl_io_error_print(io, NULL);
13160 * Tell the FETD or the other shelf controller we're done with this
13161 * command. Note that only SCSI commands get to this point. Task
13162 * management commands are completed above.
13164 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13165 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13166 memset(&msg, 0, sizeof(msg));
13167 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13168 msg.hdr.serializing_sc = io->io_hdr.serializing_sc;
13169 msg.hdr.nexus = io->io_hdr.nexus;
13170 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13171 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13179 * Front end should call this if it doesn't do autosense. When the request
13180 * sense comes back in from the initiator, we'll dequeue this and send it.
13183 ctl_queue_sense(union ctl_io *io)
13185 struct ctl_softc *softc = CTL_SOFTC(io);
13186 struct ctl_port *port = CTL_PORT(io);
13187 struct ctl_lun *lun;
13188 struct scsi_sense_data *ps;
13189 uint32_t initidx, p, targ_lun;
13191 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13193 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13196 * LUN lookup will likely move to the ctl_work_thread() once we
13197 * have our new queueing infrastructure (that doesn't put things on
13198 * a per-LUN queue initially). That is so that we can handle
13199 * things like an INQUIRY to a LUN that we don't have enabled. We
13200 * can't deal with that right now.
13201 * If we don't have a LUN for this, just toss the sense information.
13203 mtx_lock(&softc->ctl_lock);
13204 if (targ_lun >= ctl_max_luns ||
13205 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13206 mtx_unlock(&softc->ctl_lock);
13209 mtx_lock(&lun->lun_lock);
13210 mtx_unlock(&softc->ctl_lock);
13212 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13213 p = initidx / CTL_MAX_INIT_PER_PORT;
13214 if (lun->pending_sense[p] == NULL) {
13215 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13216 M_CTL, M_NOWAIT | M_ZERO);
13218 if ((ps = lun->pending_sense[p]) != NULL) {
13219 ps += initidx % CTL_MAX_INIT_PER_PORT;
13220 memset(ps, 0, sizeof(*ps));
13221 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13223 mtx_unlock(&lun->lun_lock);
13227 return (CTL_RETVAL_COMPLETE);
13231 * Primary command inlet from frontend ports. All SCSI and task I/O
13232 * requests must go through this function.
13235 ctl_queue(union ctl_io *io)
13237 struct ctl_port *port = CTL_PORT(io);
13239 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13242 io->io_hdr.start_time = time_uptime;
13243 getbinuptime(&io->io_hdr.start_bt);
13244 #endif /* CTL_TIME_IO */
13246 /* Map FE-specific LUN ID into global one. */
13247 io->io_hdr.nexus.targ_mapped_lun =
13248 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13250 switch (io->io_hdr.io_type) {
13253 if (ctl_debug & CTL_DEBUG_CDB)
13255 ctl_enqueue_incoming(io);
13258 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13262 return (CTL_RETVAL_COMPLETE);
13265 #ifdef CTL_IO_DELAY
13267 ctl_done_timer_wakeup(void *arg)
13271 io = (union ctl_io *)arg;
13274 #endif /* CTL_IO_DELAY */
13277 ctl_serseq_done(union ctl_io *io)
13279 struct ctl_lun *lun = CTL_LUN(io);;
13281 if (lun->be_lun == NULL ||
13282 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13284 mtx_lock(&lun->lun_lock);
13285 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13286 ctl_check_blocked(lun);
13287 mtx_unlock(&lun->lun_lock);
13291 ctl_done(union ctl_io *io)
13295 * Enable this to catch duplicate completion issues.
13298 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13299 printf("%s: type %d msg %d cdb %x iptl: "
13300 "%u:%u:%u tag 0x%04x "
13301 "flag %#x status %x\n",
13303 io->io_hdr.io_type,
13304 io->io_hdr.msg_type,
13306 io->io_hdr.nexus.initid,
13307 io->io_hdr.nexus.targ_port,
13308 io->io_hdr.nexus.targ_lun,
13309 (io->io_hdr.io_type ==
13311 io->taskio.tag_num :
13312 io->scsiio.tag_num,
13314 io->io_hdr.status);
13316 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13320 * This is an internal copy of an I/O, and should not go through
13321 * the normal done processing logic.
13323 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13326 #ifdef CTL_IO_DELAY
13327 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13328 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13330 struct ctl_lun *lun = CTL_LUN(io);
13333 && (lun->delay_info.done_delay > 0)) {
13335 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13336 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13337 callout_reset(&io->io_hdr.delay_callout,
13338 lun->delay_info.done_delay * hz,
13339 ctl_done_timer_wakeup, io);
13340 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13341 lun->delay_info.done_delay = 0;
13345 #endif /* CTL_IO_DELAY */
13347 ctl_enqueue_done(io);
13351 ctl_work_thread(void *arg)
13353 struct ctl_thread *thr = (struct ctl_thread *)arg;
13354 struct ctl_softc *softc = thr->ctl_softc;
13358 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13360 while (!softc->shutdown) {
13362 * We handle the queues in this order:
13364 * - done queue (to free up resources, unblock other commands)
13368 * If those queues are empty, we break out of the loop and
13371 mtx_lock(&thr->queue_lock);
13372 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13374 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13375 mtx_unlock(&thr->queue_lock);
13376 ctl_handle_isc(io);
13379 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13381 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13382 /* clear any blocked commands, call fe_done */
13383 mtx_unlock(&thr->queue_lock);
13384 ctl_process_done(io);
13387 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13389 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13390 mtx_unlock(&thr->queue_lock);
13391 if (io->io_hdr.io_type == CTL_IO_TASK)
13394 ctl_scsiio_precheck(softc, &io->scsiio);
13397 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13399 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13400 mtx_unlock(&thr->queue_lock);
13401 retval = ctl_scsiio(&io->scsiio);
13402 if (retval != CTL_RETVAL_COMPLETE)
13403 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13407 /* Sleep until we have something to do. */
13408 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13410 thr->thread = NULL;
13415 ctl_lun_thread(void *arg)
13417 struct ctl_softc *softc = (struct ctl_softc *)arg;
13418 struct ctl_be_lun *be_lun;
13420 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13422 while (!softc->shutdown) {
13423 mtx_lock(&softc->ctl_lock);
13424 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13425 if (be_lun != NULL) {
13426 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13427 mtx_unlock(&softc->ctl_lock);
13428 ctl_create_lun(be_lun);
13432 /* Sleep until we have something to do. */
13433 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13434 PDROP | PRIBIO, "-", 0);
13436 softc->lun_thread = NULL;
13441 ctl_thresh_thread(void *arg)
13443 struct ctl_softc *softc = (struct ctl_softc *)arg;
13444 struct ctl_lun *lun;
13445 struct ctl_logical_block_provisioning_page *page;
13447 union ctl_ha_msg msg;
13448 uint64_t thres, val;
13451 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13453 while (!softc->shutdown) {
13454 mtx_lock(&softc->ctl_lock);
13455 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13456 if ((lun->flags & CTL_LUN_DISABLED) ||
13457 (lun->flags & CTL_LUN_NO_MEDIA) ||
13458 lun->backend->lun_attr == NULL)
13460 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13461 softc->ha_mode == CTL_HA_MODE_XFER)
13463 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13466 page = &lun->MODE_LBP;
13467 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13468 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13470 thres = scsi_4btoul(page->descr[i].count);
13471 thres <<= CTL_LBP_EXPONENT;
13472 switch (page->descr[i].resource) {
13474 attr = "blocksavail";
13477 attr = "blocksused";
13480 attr = "poolblocksavail";
13483 attr = "poolblocksused";
13488 mtx_unlock(&softc->ctl_lock); // XXX
13489 val = lun->backend->lun_attr(
13490 lun->be_lun->be_lun, attr);
13491 mtx_lock(&softc->ctl_lock);
13492 if (val == UINT64_MAX)
13494 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13495 == SLBPPD_ARMING_INC)
13496 e = (val >= thres);
13498 e = (val <= thres);
13502 mtx_lock(&lun->lun_lock);
13504 scsi_u64to8b((uint8_t *)&page->descr[i] -
13505 (uint8_t *)page, lun->ua_tpt_info);
13506 if (lun->lasttpt == 0 ||
13507 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13508 lun->lasttpt = time_uptime;
13509 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13515 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13518 mtx_unlock(&lun->lun_lock);
13520 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13521 /* Send msg to other side. */
13522 bzero(&msg.ua, sizeof(msg.ua));
13523 msg.hdr.msg_type = CTL_MSG_UA;
13524 msg.hdr.nexus.initid = -1;
13525 msg.hdr.nexus.targ_port = -1;
13526 msg.hdr.nexus.targ_lun = lun->lun;
13527 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13529 msg.ua.ua_set = (set > 0);
13530 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13531 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13532 mtx_unlock(&softc->ctl_lock); // XXX
13533 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13534 sizeof(msg.ua), M_WAITOK);
13535 mtx_lock(&softc->ctl_lock);
13538 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13539 PDROP | PRIBIO, "-", CTL_LBP_PERIOD * hz);
13541 softc->thresh_thread = NULL;
13546 ctl_enqueue_incoming(union ctl_io *io)
13548 struct ctl_softc *softc = CTL_SOFTC(io);
13549 struct ctl_thread *thr;
13552 idx = (io->io_hdr.nexus.targ_port * 127 +
13553 io->io_hdr.nexus.initid) % worker_threads;
13554 thr = &softc->threads[idx];
13555 mtx_lock(&thr->queue_lock);
13556 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13557 mtx_unlock(&thr->queue_lock);
13562 ctl_enqueue_rtr(union ctl_io *io)
13564 struct ctl_softc *softc = CTL_SOFTC(io);
13565 struct ctl_thread *thr;
13567 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13568 mtx_lock(&thr->queue_lock);
13569 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13570 mtx_unlock(&thr->queue_lock);
13575 ctl_enqueue_done(union ctl_io *io)
13577 struct ctl_softc *softc = CTL_SOFTC(io);
13578 struct ctl_thread *thr;
13580 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13581 mtx_lock(&thr->queue_lock);
13582 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13583 mtx_unlock(&thr->queue_lock);
13588 ctl_enqueue_isc(union ctl_io *io)
13590 struct ctl_softc *softc = CTL_SOFTC(io);
13591 struct ctl_thread *thr;
13593 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13594 mtx_lock(&thr->queue_lock);
13595 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13596 mtx_unlock(&thr->queue_lock);