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.remote_io;
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.remote_io;
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.remote_io = 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.remote_io = 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,
1512 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries];
1514 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1516 io->scsiio.kern_sg_entries =
1517 msg->dt.kern_sg_entries;
1518 io->scsiio.rem_sg_entries =
1519 msg->dt.kern_sg_entries;
1520 io->scsiio.kern_data_len =
1521 msg->dt.kern_data_len;
1522 io->scsiio.kern_total_len =
1523 msg->dt.kern_total_len;
1524 io->scsiio.kern_data_resid =
1525 msg->dt.kern_data_resid;
1526 io->scsiio.kern_rel_offset =
1527 msg->dt.kern_rel_offset;
1528 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1529 io->io_hdr.flags |= msg->dt.flags &
1532 sgl = (struct ctl_sg_entry *)
1533 io->scsiio.kern_data_ptr;
1535 for (i = msg->dt.sent_sg_entries, j = 0;
1536 i < (msg->dt.sent_sg_entries +
1537 msg->dt.cur_sg_entries); i++, j++) {
1538 sgl[i].addr = msg->dt.sg_list[j].addr;
1539 sgl[i].len = msg->dt.sg_list[j].len;
1542 printf("%s: DATAMOVE: %p,%lu j=%d, i=%d\n",
1543 __func__, sgl[i].addr, sgl[i].len, j, i);
1548 * If this is the last piece of the I/O, we've got
1549 * the full S/G list. Queue processing in the thread.
1550 * Otherwise wait for the next piece.
1552 if (msg->dt.sg_last != 0)
1553 ctl_enqueue_isc(io);
1556 /* Performed on the Serializing (primary) SC, XFER mode only */
1557 case CTL_MSG_DATAMOVE_DONE: {
1558 if (msg->hdr.serializing_sc == NULL) {
1559 printf("%s: serializing_sc == NULL!\n",
1561 /* XXX KDM now what? */
1565 * We grab the sense information here in case
1566 * there was a failure, so we can return status
1567 * back to the initiator.
1569 io = msg->hdr.serializing_sc;
1570 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1571 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1572 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1573 io->io_hdr.port_status = msg->scsi.port_status;
1574 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1575 if (msg->hdr.status != CTL_STATUS_NONE) {
1576 io->io_hdr.status = msg->hdr.status;
1577 io->scsiio.scsi_status = msg->scsi.scsi_status;
1578 io->scsiio.sense_len = msg->scsi.sense_len;
1579 memcpy(&io->scsiio.sense_data,
1580 &msg->scsi.sense_data,
1581 msg->scsi.sense_len);
1582 if (msg->hdr.status == CTL_SUCCESS)
1583 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1585 ctl_enqueue_isc(io);
1589 /* Preformed on Originating SC, SER_ONLY mode */
1591 io = msg->hdr.original_sc;
1593 printf("%s: original_sc == NULL!\n",
1597 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1598 io->io_hdr.msg_type = CTL_MSG_R2R;
1599 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1600 ctl_enqueue_isc(io);
1604 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1606 * Performed on the Originating (i.e. secondary) SC in XFER
1609 case CTL_MSG_FINISH_IO:
1610 if (softc->ha_mode == CTL_HA_MODE_XFER)
1611 ctl_isc_handler_finish_xfer(softc, msg);
1613 ctl_isc_handler_finish_ser_only(softc, msg);
1616 /* Preformed on Originating SC */
1617 case CTL_MSG_BAD_JUJU:
1618 io = msg->hdr.original_sc;
1620 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1624 ctl_copy_sense_data(msg, io);
1626 * IO should have already been cleaned up on other
1627 * SC so clear this flag so we won't send a message
1628 * back to finish the IO there.
1630 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1631 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1633 /* io = msg->hdr.serializing_sc; */
1634 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1635 ctl_enqueue_isc(io);
1638 /* Handle resets sent from the other side */
1639 case CTL_MSG_MANAGE_TASKS: {
1640 struct ctl_taskio *taskio;
1641 taskio = (struct ctl_taskio *)ctl_alloc_io(
1642 softc->othersc_pool);
1643 ctl_zero_io((union ctl_io *)taskio);
1644 taskio->io_hdr.io_type = CTL_IO_TASK;
1645 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1646 taskio->io_hdr.nexus = msg->hdr.nexus;
1647 taskio->task_action = msg->task.task_action;
1648 taskio->tag_num = msg->task.tag_num;
1649 taskio->tag_type = msg->task.tag_type;
1651 taskio->io_hdr.start_time = time_uptime;
1652 getbinuptime(&taskio->io_hdr.start_bt);
1653 #endif /* CTL_TIME_IO */
1654 ctl_run_task((union ctl_io *)taskio);
1657 /* Persistent Reserve action which needs attention */
1658 case CTL_MSG_PERS_ACTION:
1659 presio = (struct ctl_prio *)ctl_alloc_io(
1660 softc->othersc_pool);
1661 ctl_zero_io((union ctl_io *)presio);
1662 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1663 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1664 presio->io_hdr.nexus = msg->hdr.nexus;
1665 presio->pr_msg = msg->pr;
1666 ctl_enqueue_isc((union ctl_io *)presio);
1669 ctl_isc_ua(softc, msg, param);
1671 case CTL_MSG_PORT_SYNC:
1672 ctl_isc_port_sync(softc, msg, param);
1674 case CTL_MSG_LUN_SYNC:
1675 ctl_isc_lun_sync(softc, msg, param);
1677 case CTL_MSG_IID_SYNC:
1678 ctl_isc_iid_sync(softc, msg, param);
1681 ctl_isc_login(softc, msg, param);
1683 case CTL_MSG_MODE_SYNC:
1684 ctl_isc_mode_sync(softc, msg, param);
1687 printf("Received HA message of unknown type %d\n",
1689 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1694 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1695 printf("CTL: HA link status changed from %d to %d\n",
1696 softc->ha_link, param);
1697 if (param == softc->ha_link)
1699 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1700 softc->ha_link = param;
1701 ctl_isc_ha_link_down(softc);
1703 softc->ha_link = param;
1704 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1705 ctl_isc_ha_link_up(softc);
1709 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1715 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1718 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1719 src->scsi.sense_len);
1720 dest->scsiio.scsi_status = src->scsi.scsi_status;
1721 dest->scsiio.sense_len = src->scsi.sense_len;
1722 dest->io_hdr.status = src->hdr.status;
1726 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1729 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1730 src->scsiio.sense_len);
1731 dest->scsi.scsi_status = src->scsiio.scsi_status;
1732 dest->scsi.sense_len = src->scsiio.sense_len;
1733 dest->hdr.status = src->io_hdr.status;
1737 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1739 struct ctl_softc *softc = lun->ctl_softc;
1742 if (initidx < softc->init_min || initidx >= softc->init_max)
1744 mtx_assert(&lun->lun_lock, MA_OWNED);
1745 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1748 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1752 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1756 mtx_assert(&lun->lun_lock, MA_OWNED);
1757 if (lun->pending_ua[port] == NULL)
1759 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1760 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1762 lun->pending_ua[port][i] |= ua;
1767 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1769 struct ctl_softc *softc = lun->ctl_softc;
1772 mtx_assert(&lun->lun_lock, MA_OWNED);
1773 for (i = softc->port_min; i < softc->port_max; i++)
1774 ctl_est_ua_port(lun, i, except, ua);
1778 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1780 struct ctl_softc *softc = lun->ctl_softc;
1783 if (initidx < softc->init_min || initidx >= softc->init_max)
1785 mtx_assert(&lun->lun_lock, MA_OWNED);
1786 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1789 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1793 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1795 struct ctl_softc *softc = lun->ctl_softc;
1798 mtx_assert(&lun->lun_lock, MA_OWNED);
1799 for (i = softc->port_min; i < softc->port_max; i++) {
1800 if (lun->pending_ua[i] == NULL)
1802 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1803 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1805 lun->pending_ua[i][j] &= ~ua;
1811 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1812 ctl_ua_type ua_type)
1814 struct ctl_lun *lun;
1816 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1817 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1818 mtx_lock(&lun->lun_lock);
1819 ctl_clr_ua(lun, initidx, ua_type);
1820 mtx_unlock(&lun->lun_lock);
1825 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1827 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1828 struct ctl_lun *lun;
1829 struct ctl_lun_req ireq;
1832 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1833 error = sysctl_handle_int(oidp, &value, 0, req);
1834 if ((error != 0) || (req->newptr == NULL))
1837 mtx_lock(&softc->ctl_lock);
1839 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1841 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1842 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1843 mtx_unlock(&softc->ctl_lock);
1844 bzero(&ireq, sizeof(ireq));
1845 ireq.reqtype = CTL_LUNREQ_MODIFY;
1846 ireq.reqdata.modify.lun_id = lun->lun;
1847 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1849 if (ireq.status != CTL_LUN_OK) {
1850 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1851 __func__, ireq.status, ireq.error_str);
1853 mtx_lock(&softc->ctl_lock);
1855 mtx_unlock(&softc->ctl_lock);
1862 struct make_dev_args args;
1863 struct ctl_softc *softc;
1866 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1869 make_dev_args_init(&args);
1870 args.mda_devsw = &ctl_cdevsw;
1871 args.mda_uid = UID_ROOT;
1872 args.mda_gid = GID_OPERATOR;
1873 args.mda_mode = 0600;
1874 args.mda_si_drv1 = softc;
1875 args.mda_si_drv2 = NULL;
1876 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1878 free(softc, M_DEVBUF);
1879 control_softc = NULL;
1883 sysctl_ctx_init(&softc->sysctl_ctx);
1884 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1885 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1886 CTLFLAG_RD, 0, "CAM Target Layer");
1888 if (softc->sysctl_tree == NULL) {
1889 printf("%s: unable to allocate sysctl tree\n", __func__);
1890 destroy_dev(softc->dev);
1891 free(softc, M_DEVBUF);
1892 control_softc = NULL;
1896 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1897 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1898 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1901 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1902 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1903 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1905 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) {
1906 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n",
1907 ctl_max_luns, CTL_DEFAULT_MAX_LUNS);
1908 ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
1910 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns,
1911 M_DEVBUF, M_WAITOK | M_ZERO);
1912 softc->ctl_lun_mask = malloc(sizeof(uint32_t) *
1913 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1914 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) {
1915 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n",
1916 ctl_max_ports, CTL_DEFAULT_MAX_PORTS);
1917 ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
1919 softc->ctl_port_mask = malloc(sizeof(uint32_t) *
1920 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1921 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports,
1922 M_DEVBUF, M_WAITOK | M_ZERO);
1926 * In Copan's HA scheme, the "master" and "slave" roles are
1927 * figured out through the slot the controller is in. Although it
1928 * is an active/active system, someone has to be in charge.
1930 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1931 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1932 "HA head ID (0 - no HA)");
1933 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1934 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1935 softc->is_single = 1;
1936 softc->port_cnt = ctl_max_ports;
1937 softc->port_min = 0;
1939 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES;
1940 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1942 softc->port_max = softc->port_min + softc->port_cnt;
1943 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1944 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1946 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1947 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1948 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1950 STAILQ_INIT(&softc->lun_list);
1951 STAILQ_INIT(&softc->pending_lun_queue);
1952 STAILQ_INIT(&softc->fe_list);
1953 STAILQ_INIT(&softc->port_list);
1954 STAILQ_INIT(&softc->be_list);
1955 ctl_tpc_init(softc);
1957 if (worker_threads <= 0)
1958 worker_threads = max(1, mp_ncpus / 4);
1959 if (worker_threads > CTL_MAX_THREADS)
1960 worker_threads = CTL_MAX_THREADS;
1962 for (i = 0; i < worker_threads; i++) {
1963 struct ctl_thread *thr = &softc->threads[i];
1965 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1966 thr->ctl_softc = softc;
1967 STAILQ_INIT(&thr->incoming_queue);
1968 STAILQ_INIT(&thr->rtr_queue);
1969 STAILQ_INIT(&thr->done_queue);
1970 STAILQ_INIT(&thr->isc_queue);
1972 error = kproc_kthread_add(ctl_work_thread, thr,
1973 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1975 printf("error creating CTL work thread!\n");
1979 error = kproc_kthread_add(ctl_lun_thread, softc,
1980 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun");
1982 printf("error creating CTL lun thread!\n");
1985 error = kproc_kthread_add(ctl_thresh_thread, softc,
1986 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
1988 printf("error creating CTL threshold thread!\n");
1992 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1993 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1994 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1996 if (softc->is_single == 0) {
1997 if (ctl_frontend_register(&ha_frontend) != 0)
1998 softc->is_single = 1;
2006 struct ctl_softc *softc = control_softc;
2009 if (softc->is_single == 0)
2010 ctl_frontend_deregister(&ha_frontend);
2012 destroy_dev(softc->dev);
2014 /* Shutdown CTL threads. */
2015 softc->shutdown = 1;
2016 for (i = 0; i < worker_threads; i++) {
2017 struct ctl_thread *thr = &softc->threads[i];
2018 while (thr->thread != NULL) {
2020 if (thr->thread != NULL)
2021 pause("CTL thr shutdown", 1);
2023 mtx_destroy(&thr->queue_lock);
2025 while (softc->lun_thread != NULL) {
2026 wakeup(&softc->pending_lun_queue);
2027 if (softc->lun_thread != NULL)
2028 pause("CTL thr shutdown", 1);
2030 while (softc->thresh_thread != NULL) {
2031 wakeup(softc->thresh_thread);
2032 if (softc->thresh_thread != NULL)
2033 pause("CTL thr shutdown", 1);
2036 ctl_tpc_shutdown(softc);
2037 uma_zdestroy(softc->io_zone);
2038 mtx_destroy(&softc->ctl_lock);
2040 free(softc->ctl_luns, M_DEVBUF);
2041 free(softc->ctl_lun_mask, M_DEVBUF);
2042 free(softc->ctl_port_mask, M_DEVBUF);
2043 free(softc->ctl_ports, M_DEVBUF);
2045 sysctl_ctx_free(&softc->sysctl_ctx);
2047 free(softc, M_DEVBUF);
2048 control_softc = NULL;
2053 ctl_module_event_handler(module_t mod, int what, void *arg)
2058 return (ctl_init());
2060 return (ctl_shutdown());
2062 return (EOPNOTSUPP);
2067 * XXX KDM should we do some access checks here? Bump a reference count to
2068 * prevent a CTL module from being unloaded while someone has it open?
2071 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2077 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2083 * Remove an initiator by port number and initiator ID.
2084 * Returns 0 for success, -1 for failure.
2087 ctl_remove_initiator(struct ctl_port *port, int iid)
2089 struct ctl_softc *softc = port->ctl_softc;
2092 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2094 if (iid > CTL_MAX_INIT_PER_PORT) {
2095 printf("%s: initiator ID %u > maximun %u!\n",
2096 __func__, iid, CTL_MAX_INIT_PER_PORT);
2100 mtx_lock(&softc->ctl_lock);
2101 last = (--port->wwpn_iid[iid].in_use == 0);
2102 port->wwpn_iid[iid].last_use = time_uptime;
2103 mtx_unlock(&softc->ctl_lock);
2105 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2106 ctl_isc_announce_iid(port, iid);
2112 * Add an initiator to the initiator map.
2113 * Returns iid for success, < 0 for failure.
2116 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2118 struct ctl_softc *softc = port->ctl_softc;
2122 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2124 if (iid >= CTL_MAX_INIT_PER_PORT) {
2125 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2126 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2131 mtx_lock(&softc->ctl_lock);
2133 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2134 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2135 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2139 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2140 strcmp(name, port->wwpn_iid[i].name) == 0) {
2148 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2149 if (port->wwpn_iid[i].in_use == 0 &&
2150 port->wwpn_iid[i].wwpn == 0 &&
2151 port->wwpn_iid[i].name == NULL) {
2160 best_time = INT32_MAX;
2161 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2162 if (port->wwpn_iid[i].in_use == 0) {
2163 if (port->wwpn_iid[i].last_use < best_time) {
2165 best_time = port->wwpn_iid[i].last_use;
2173 mtx_unlock(&softc->ctl_lock);
2178 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2180 * This is not an error yet.
2182 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2184 printf("%s: port %d iid %u WWPN %#jx arrived"
2185 " again\n", __func__, port->targ_port,
2186 iid, (uintmax_t)wwpn);
2190 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2191 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2193 printf("%s: port %d iid %u name '%s' arrived"
2194 " again\n", __func__, port->targ_port,
2201 * This is an error, but what do we do about it? The
2202 * driver is telling us we have a new WWPN for this
2203 * initiator ID, so we pretty much need to use it.
2205 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2206 " but WWPN %#jx '%s' is still at that address\n",
2207 __func__, port->targ_port, iid, wwpn, name,
2208 (uintmax_t)port->wwpn_iid[iid].wwpn,
2209 port->wwpn_iid[iid].name);
2212 free(port->wwpn_iid[iid].name, M_CTL);
2213 port->wwpn_iid[iid].name = name;
2214 port->wwpn_iid[iid].wwpn = wwpn;
2215 port->wwpn_iid[iid].in_use++;
2216 mtx_unlock(&softc->ctl_lock);
2217 ctl_isc_announce_iid(port, iid);
2223 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2227 switch (port->port_type) {
2230 struct scsi_transportid_fcp *id =
2231 (struct scsi_transportid_fcp *)buf;
2232 if (port->wwpn_iid[iid].wwpn == 0)
2234 memset(id, 0, sizeof(*id));
2235 id->format_protocol = SCSI_PROTO_FC;
2236 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2237 return (sizeof(*id));
2239 case CTL_PORT_ISCSI:
2241 struct scsi_transportid_iscsi_port *id =
2242 (struct scsi_transportid_iscsi_port *)buf;
2243 if (port->wwpn_iid[iid].name == NULL)
2246 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2248 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2249 len = roundup2(min(len, 252), 4);
2250 scsi_ulto2b(len, id->additional_length);
2251 return (sizeof(*id) + len);
2255 struct scsi_transportid_sas *id =
2256 (struct scsi_transportid_sas *)buf;
2257 if (port->wwpn_iid[iid].wwpn == 0)
2259 memset(id, 0, sizeof(*id));
2260 id->format_protocol = SCSI_PROTO_SAS;
2261 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2262 return (sizeof(*id));
2266 struct scsi_transportid_spi *id =
2267 (struct scsi_transportid_spi *)buf;
2268 memset(id, 0, sizeof(*id));
2269 id->format_protocol = SCSI_PROTO_SPI;
2270 scsi_ulto2b(iid, id->scsi_addr);
2271 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2272 return (sizeof(*id));
2278 * Serialize a command that went down the "wrong" side, and so was sent to
2279 * this controller for execution. The logic is a little different than the
2280 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2281 * sent back to the other side, but in the success case, we execute the
2282 * command on this side (XFER mode) or tell the other side to execute it
2286 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2288 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2289 struct ctl_port *port = CTL_PORT(ctsio);
2290 union ctl_ha_msg msg_info;
2291 struct ctl_lun *lun;
2292 const struct ctl_cmd_entry *entry;
2295 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2297 /* Make sure that we know about this port. */
2298 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2299 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2304 /* Make sure that we know about this LUN. */
2305 mtx_lock(&softc->ctl_lock);
2306 if (targ_lun >= ctl_max_luns ||
2307 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2308 mtx_unlock(&softc->ctl_lock);
2311 * The other node would not send this request to us unless
2312 * received announce that we are primary node for this LUN.
2313 * If this LUN does not exist now, it is probably result of
2314 * a race, so respond to initiator in the most opaque way.
2316 ctl_set_busy(ctsio);
2319 mtx_lock(&lun->lun_lock);
2320 mtx_unlock(&softc->ctl_lock);
2323 * If the LUN is invalid, pretend that it doesn't exist.
2324 * It will go away as soon as all pending I/Os completed.
2326 if (lun->flags & CTL_LUN_DISABLED) {
2327 mtx_unlock(&lun->lun_lock);
2328 ctl_set_busy(ctsio);
2332 entry = ctl_get_cmd_entry(ctsio, NULL);
2333 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2334 mtx_unlock(&lun->lun_lock);
2338 CTL_LUN(ctsio) = lun;
2339 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2342 * Every I/O goes into the OOA queue for a
2343 * particular LUN, and stays there until completion.
2346 if (TAILQ_EMPTY(&lun->ooa_queue))
2347 lun->idle_time += getsbinuptime() - lun->last_busy;
2349 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2351 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
2352 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq,
2354 case CTL_ACTION_BLOCK:
2355 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
2356 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
2358 mtx_unlock(&lun->lun_lock);
2360 case CTL_ACTION_PASS:
2361 case CTL_ACTION_SKIP:
2362 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2363 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2364 ctl_enqueue_rtr((union ctl_io *)ctsio);
2365 mtx_unlock(&lun->lun_lock);
2367 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2368 mtx_unlock(&lun->lun_lock);
2370 /* send msg back to other side */
2371 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2372 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2373 msg_info.hdr.msg_type = CTL_MSG_R2R;
2374 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2375 sizeof(msg_info.hdr), M_WAITOK);
2378 case CTL_ACTION_OVERLAP:
2379 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2380 mtx_unlock(&lun->lun_lock);
2381 ctl_set_overlapped_cmd(ctsio);
2383 case CTL_ACTION_OVERLAP_TAG:
2384 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2385 mtx_unlock(&lun->lun_lock);
2386 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2388 case CTL_ACTION_ERROR:
2390 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2391 mtx_unlock(&lun->lun_lock);
2393 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2396 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2397 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2398 msg_info.hdr.serializing_sc = NULL;
2399 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2400 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2401 sizeof(msg_info.scsi), M_WAITOK);
2402 ctl_free_io((union ctl_io *)ctsio);
2408 * Returns 0 for success, errno for failure.
2411 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2412 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2416 mtx_lock(&lun->lun_lock);
2417 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2418 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2420 struct ctl_ooa_entry *entry;
2423 * If we've got more than we can fit, just count the
2424 * remaining entries.
2426 if (*cur_fill_num >= ooa_hdr->alloc_num)
2429 entry = &kern_entries[*cur_fill_num];
2431 entry->tag_num = io->scsiio.tag_num;
2432 entry->lun_num = lun->lun;
2434 entry->start_bt = io->io_hdr.start_bt;
2436 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2437 entry->cdb_len = io->scsiio.cdb_len;
2438 if (io->io_hdr.flags & CTL_FLAG_BLOCKED)
2439 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2441 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2442 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2444 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2445 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2447 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2448 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2450 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2451 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2453 mtx_unlock(&lun->lun_lock);
2457 * Escape characters that are illegal or not recommended in XML.
2460 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2462 char *end = str + size;
2467 for (; *str && str < end; str++) {
2470 retval = sbuf_printf(sb, "&");
2473 retval = sbuf_printf(sb, ">");
2476 retval = sbuf_printf(sb, "<");
2479 retval = sbuf_putc(sb, *str);
2492 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2494 struct scsi_vpd_id_descriptor *desc;
2497 if (id == NULL || id->len < 4)
2499 desc = (struct scsi_vpd_id_descriptor *)id->data;
2500 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2501 case SVPD_ID_TYPE_T10:
2502 sbuf_printf(sb, "t10.");
2504 case SVPD_ID_TYPE_EUI64:
2505 sbuf_printf(sb, "eui.");
2507 case SVPD_ID_TYPE_NAA:
2508 sbuf_printf(sb, "naa.");
2510 case SVPD_ID_TYPE_SCSI_NAME:
2513 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2514 case SVPD_ID_CODESET_BINARY:
2515 for (i = 0; i < desc->length; i++)
2516 sbuf_printf(sb, "%02x", desc->identifier[i]);
2518 case SVPD_ID_CODESET_ASCII:
2519 sbuf_printf(sb, "%.*s", (int)desc->length,
2520 (char *)desc->identifier);
2522 case SVPD_ID_CODESET_UTF8:
2523 sbuf_printf(sb, "%s", (char *)desc->identifier);
2529 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2532 struct ctl_softc *softc = dev->si_drv1;
2533 struct ctl_port *port;
2534 struct ctl_lun *lun;
2541 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2543 case CTL_ENABLE_PORT:
2544 case CTL_DISABLE_PORT:
2545 case CTL_SET_PORT_WWNS: {
2546 struct ctl_port *port;
2547 struct ctl_port_entry *entry;
2549 entry = (struct ctl_port_entry *)addr;
2551 mtx_lock(&softc->ctl_lock);
2552 STAILQ_FOREACH(port, &softc->port_list, links) {
2555 if (port->targ_port < softc->port_min ||
2556 port->targ_port >= softc->port_max)
2561 if ((entry->port_type == CTL_PORT_NONE)
2562 && (entry->targ_port == port->targ_port)) {
2564 * If the user only wants to enable or
2565 * disable or set WWNs on a specific port,
2566 * do the operation and we're done.
2570 } else if (entry->port_type & port->port_type) {
2572 * Compare the user's type mask with the
2573 * particular frontend type to see if we
2580 * Make sure the user isn't trying to set
2581 * WWNs on multiple ports at the same time.
2583 if (cmd == CTL_SET_PORT_WWNS) {
2584 printf("%s: Can't set WWNs on "
2585 "multiple ports\n", __func__);
2594 * XXX KDM we have to drop the lock here, because
2595 * the online/offline operations can potentially
2596 * block. We need to reference count the frontends
2597 * so they can't go away,
2599 if (cmd == CTL_ENABLE_PORT) {
2600 mtx_unlock(&softc->ctl_lock);
2601 ctl_port_online(port);
2602 mtx_lock(&softc->ctl_lock);
2603 } else if (cmd == CTL_DISABLE_PORT) {
2604 mtx_unlock(&softc->ctl_lock);
2605 ctl_port_offline(port);
2606 mtx_lock(&softc->ctl_lock);
2607 } else if (cmd == CTL_SET_PORT_WWNS) {
2608 ctl_port_set_wwns(port,
2609 (entry->flags & CTL_PORT_WWNN_VALID) ?
2611 (entry->flags & CTL_PORT_WWPN_VALID) ?
2612 1 : 0, entry->wwpn);
2617 mtx_unlock(&softc->ctl_lock);
2621 struct ctl_ooa *ooa_hdr;
2622 struct ctl_ooa_entry *entries;
2623 uint32_t cur_fill_num;
2625 ooa_hdr = (struct ctl_ooa *)addr;
2627 if ((ooa_hdr->alloc_len == 0)
2628 || (ooa_hdr->alloc_num == 0)) {
2629 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2630 "must be non-zero\n", __func__,
2631 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2636 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2637 sizeof(struct ctl_ooa_entry))) {
2638 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2639 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2640 __func__, ooa_hdr->alloc_len,
2641 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2646 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2647 if (entries == NULL) {
2648 printf("%s: could not allocate %d bytes for OOA "
2649 "dump\n", __func__, ooa_hdr->alloc_len);
2654 mtx_lock(&softc->ctl_lock);
2655 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2656 (ooa_hdr->lun_num >= ctl_max_luns ||
2657 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2658 mtx_unlock(&softc->ctl_lock);
2659 free(entries, M_CTL);
2660 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2661 __func__, (uintmax_t)ooa_hdr->lun_num);
2668 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2669 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2670 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2674 lun = softc->ctl_luns[ooa_hdr->lun_num];
2675 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2678 mtx_unlock(&softc->ctl_lock);
2680 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2681 ooa_hdr->fill_len = ooa_hdr->fill_num *
2682 sizeof(struct ctl_ooa_entry);
2683 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2685 printf("%s: error copying out %d bytes for OOA dump\n",
2686 __func__, ooa_hdr->fill_len);
2689 getbinuptime(&ooa_hdr->cur_bt);
2691 if (cur_fill_num > ooa_hdr->alloc_num) {
2692 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2693 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2695 ooa_hdr->dropped_num = 0;
2696 ooa_hdr->status = CTL_OOA_OK;
2699 free(entries, M_CTL);
2702 case CTL_DELAY_IO: {
2703 struct ctl_io_delay_info *delay_info;
2705 delay_info = (struct ctl_io_delay_info *)addr;
2708 mtx_lock(&softc->ctl_lock);
2709 if (delay_info->lun_id >= ctl_max_luns ||
2710 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2711 mtx_unlock(&softc->ctl_lock);
2712 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2715 mtx_lock(&lun->lun_lock);
2716 mtx_unlock(&softc->ctl_lock);
2717 delay_info->status = CTL_DELAY_STATUS_OK;
2718 switch (delay_info->delay_type) {
2719 case CTL_DELAY_TYPE_CONT:
2720 case CTL_DELAY_TYPE_ONESHOT:
2723 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2726 switch (delay_info->delay_loc) {
2727 case CTL_DELAY_LOC_DATAMOVE:
2728 lun->delay_info.datamove_type = delay_info->delay_type;
2729 lun->delay_info.datamove_delay = delay_info->delay_secs;
2731 case CTL_DELAY_LOC_DONE:
2732 lun->delay_info.done_type = delay_info->delay_type;
2733 lun->delay_info.done_delay = delay_info->delay_secs;
2736 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2739 mtx_unlock(&lun->lun_lock);
2741 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2742 #endif /* CTL_IO_DELAY */
2745 case CTL_ERROR_INJECT: {
2746 struct ctl_error_desc *err_desc, *new_err_desc;
2748 err_desc = (struct ctl_error_desc *)addr;
2750 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2752 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2754 mtx_lock(&softc->ctl_lock);
2755 if (err_desc->lun_id >= ctl_max_luns ||
2756 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2757 mtx_unlock(&softc->ctl_lock);
2758 free(new_err_desc, M_CTL);
2759 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2760 __func__, (uintmax_t)err_desc->lun_id);
2764 mtx_lock(&lun->lun_lock);
2765 mtx_unlock(&softc->ctl_lock);
2768 * We could do some checking here to verify the validity
2769 * of the request, but given the complexity of error
2770 * injection requests, the checking logic would be fairly
2773 * For now, if the request is invalid, it just won't get
2774 * executed and might get deleted.
2776 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2779 * XXX KDM check to make sure the serial number is unique,
2780 * in case we somehow manage to wrap. That shouldn't
2781 * happen for a very long time, but it's the right thing to
2784 new_err_desc->serial = lun->error_serial;
2785 err_desc->serial = lun->error_serial;
2786 lun->error_serial++;
2788 mtx_unlock(&lun->lun_lock);
2791 case CTL_ERROR_INJECT_DELETE: {
2792 struct ctl_error_desc *delete_desc, *desc, *desc2;
2795 delete_desc = (struct ctl_error_desc *)addr;
2798 mtx_lock(&softc->ctl_lock);
2799 if (delete_desc->lun_id >= ctl_max_luns ||
2800 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2801 mtx_unlock(&softc->ctl_lock);
2802 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2803 __func__, (uintmax_t)delete_desc->lun_id);
2807 mtx_lock(&lun->lun_lock);
2808 mtx_unlock(&softc->ctl_lock);
2809 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2810 if (desc->serial != delete_desc->serial)
2813 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2818 mtx_unlock(&lun->lun_lock);
2819 if (delete_done == 0) {
2820 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2821 "error serial %ju on LUN %u\n", __func__,
2822 delete_desc->serial, delete_desc->lun_id);
2828 case CTL_DUMP_STRUCTS: {
2830 struct ctl_port *port;
2831 struct ctl_frontend *fe;
2833 mtx_lock(&softc->ctl_lock);
2834 printf("CTL Persistent Reservation information start:\n");
2835 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2836 mtx_lock(&lun->lun_lock);
2837 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2838 mtx_unlock(&lun->lun_lock);
2842 for (j = 0; j < ctl_max_ports; j++) {
2843 if (lun->pr_keys[j] == NULL)
2845 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2846 if (lun->pr_keys[j][k] == 0)
2848 printf(" LUN %ju port %d iid %d key "
2849 "%#jx\n", lun->lun, j, k,
2850 (uintmax_t)lun->pr_keys[j][k]);
2853 mtx_unlock(&lun->lun_lock);
2855 printf("CTL Persistent Reservation information end\n");
2856 printf("CTL Ports:\n");
2857 STAILQ_FOREACH(port, &softc->port_list, links) {
2858 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2859 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2860 port->frontend->name, port->port_type,
2861 port->physical_port, port->virtual_port,
2862 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2863 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2864 if (port->wwpn_iid[j].in_use == 0 &&
2865 port->wwpn_iid[j].wwpn == 0 &&
2866 port->wwpn_iid[j].name == NULL)
2869 printf(" iid %u use %d WWPN %#jx '%s'\n",
2870 j, port->wwpn_iid[j].in_use,
2871 (uintmax_t)port->wwpn_iid[j].wwpn,
2872 port->wwpn_iid[j].name);
2875 printf("CTL Port information end\n");
2876 mtx_unlock(&softc->ctl_lock);
2878 * XXX KDM calling this without a lock. We'd likely want
2879 * to drop the lock before calling the frontend's dump
2882 printf("CTL Frontends:\n");
2883 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2884 printf(" Frontend '%s'\n", fe->name);
2885 if (fe->fe_dump != NULL)
2888 printf("CTL Frontend information end\n");
2892 struct ctl_lun_req *lun_req;
2893 struct ctl_backend_driver *backend;
2895 nvlist_t *tmp_args_nvl;
2898 lun_req = (struct ctl_lun_req *)addr;
2899 tmp_args_nvl = lun_req->args_nvl;
2901 backend = ctl_backend_find(lun_req->backend);
2902 if (backend == NULL) {
2903 lun_req->status = CTL_LUN_ERROR;
2904 snprintf(lun_req->error_str,
2905 sizeof(lun_req->error_str),
2906 "Backend \"%s\" not found.",
2911 if (lun_req->args != NULL) {
2912 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK);
2913 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) {
2914 free(packed, M_CTL);
2915 lun_req->status = CTL_LUN_ERROR;
2916 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2917 "Cannot copyin args.");
2920 lun_req->args_nvl = nvlist_unpack(packed,
2921 lun_req->args_len, 0);
2922 free(packed, M_CTL);
2924 if (lun_req->args_nvl == NULL) {
2925 lun_req->status = CTL_LUN_ERROR;
2926 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2927 "Cannot unpack args nvlist.");
2931 lun_req->args_nvl = nvlist_create(0);
2933 retval = backend->ioctl(dev, cmd, addr, flag, td);
2934 nvlist_destroy(lun_req->args_nvl);
2935 lun_req->args_nvl = tmp_args_nvl;
2937 if (lun_req->result_nvl != NULL) {
2938 if (lun_req->result != NULL) {
2939 packed = nvlist_pack(lun_req->result_nvl,
2941 if (packed == NULL) {
2942 lun_req->status = CTL_LUN_ERROR;
2943 snprintf(lun_req->error_str,
2944 sizeof(lun_req->error_str),
2945 "Cannot pack result nvlist.");
2949 if (packed_len > lun_req->result_len) {
2950 lun_req->status = CTL_LUN_ERROR;
2951 snprintf(lun_req->error_str,
2952 sizeof(lun_req->error_str),
2953 "Result nvlist too large.");
2954 free(packed, M_NVLIST);
2958 if (copyout(packed, lun_req->result, packed_len)) {
2959 lun_req->status = CTL_LUN_ERROR;
2960 snprintf(lun_req->error_str,
2961 sizeof(lun_req->error_str),
2962 "Cannot copyout() the result.");
2963 free(packed, M_NVLIST);
2967 lun_req->result_len = packed_len;
2968 free(packed, M_NVLIST);
2971 nvlist_destroy(lun_req->result_nvl);
2975 case CTL_LUN_LIST: {
2977 struct ctl_lun_list *list;
2978 const char *name, *value;
2982 list = (struct ctl_lun_list *)addr;
2985 * Allocate a fixed length sbuf here, based on the length
2986 * of the user's buffer. We could allocate an auto-extending
2987 * buffer, and then tell the user how much larger our
2988 * amount of data is than his buffer, but that presents
2991 * 1. The sbuf(9) routines use a blocking malloc, and so
2992 * we can't hold a lock while calling them with an
2993 * auto-extending buffer.
2995 * 2. There is not currently a LUN reference counting
2996 * mechanism, outside of outstanding transactions on
2997 * the LUN's OOA queue. So a LUN could go away on us
2998 * while we're getting the LUN number, backend-specific
2999 * information, etc. Thus, given the way things
3000 * currently work, we need to hold the CTL lock while
3001 * grabbing LUN information.
3003 * So, from the user's standpoint, the best thing to do is
3004 * allocate what he thinks is a reasonable buffer length,
3005 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3006 * double the buffer length and try again. (And repeat
3007 * that until he succeeds.)
3009 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3011 list->status = CTL_LUN_LIST_ERROR;
3012 snprintf(list->error_str, sizeof(list->error_str),
3013 "Unable to allocate %d bytes for LUN list",
3018 sbuf_printf(sb, "<ctllunlist>\n");
3020 mtx_lock(&softc->ctl_lock);
3021 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3022 mtx_lock(&lun->lun_lock);
3023 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3024 (uintmax_t)lun->lun);
3027 * Bail out as soon as we see that we've overfilled
3033 retval = sbuf_printf(sb, "\t<backend_type>%s"
3034 "</backend_type>\n",
3035 (lun->backend == NULL) ? "none" :
3036 lun->backend->name);
3041 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3042 lun->be_lun->lun_type);
3047 if (lun->backend == NULL) {
3048 retval = sbuf_printf(sb, "</lun>\n");
3054 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3055 (lun->be_lun->maxlba > 0) ?
3056 lun->be_lun->maxlba + 1 : 0);
3061 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3062 lun->be_lun->blocksize);
3067 retval = sbuf_printf(sb, "\t<serial_number>");
3072 retval = ctl_sbuf_printf_esc(sb,
3073 lun->be_lun->serial_num,
3074 sizeof(lun->be_lun->serial_num));
3079 retval = sbuf_printf(sb, "</serial_number>\n");
3084 retval = sbuf_printf(sb, "\t<device_id>");
3089 retval = ctl_sbuf_printf_esc(sb,
3090 lun->be_lun->device_id,
3091 sizeof(lun->be_lun->device_id));
3096 retval = sbuf_printf(sb, "</device_id>\n");
3101 if (lun->backend->lun_info != NULL) {
3102 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3108 while ((name = nvlist_next(lun->be_lun->options, &type,
3109 &cookie)) != NULL) {
3110 sbuf_printf(sb, "\t<%s>", name);
3112 if (type == NV_TYPE_STRING) {
3113 value = dnvlist_get_string(
3114 lun->be_lun->options, name, NULL);
3116 sbuf_printf(sb, "%s", value);
3119 sbuf_printf(sb, "</%s>\n", name);
3122 retval = sbuf_printf(sb, "</lun>\n");
3126 mtx_unlock(&lun->lun_lock);
3129 mtx_unlock(&lun->lun_lock);
3130 mtx_unlock(&softc->ctl_lock);
3133 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3136 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3137 snprintf(list->error_str, sizeof(list->error_str),
3138 "Out of space, %d bytes is too small",
3145 retval = copyout(sbuf_data(sb), list->lun_xml,
3148 list->fill_len = sbuf_len(sb) + 1;
3149 list->status = CTL_LUN_LIST_OK;
3154 struct ctl_iscsi *ci;
3155 struct ctl_frontend *fe;
3157 ci = (struct ctl_iscsi *)addr;
3159 fe = ctl_frontend_find("iscsi");
3161 ci->status = CTL_ISCSI_ERROR;
3162 snprintf(ci->error_str, sizeof(ci->error_str),
3163 "Frontend \"iscsi\" not found.");
3167 retval = fe->ioctl(dev, cmd, addr, flag, td);
3170 case CTL_PORT_REQ: {
3171 struct ctl_req *req;
3172 struct ctl_frontend *fe;
3174 nvlist_t *tmp_args_nvl;
3177 req = (struct ctl_req *)addr;
3178 tmp_args_nvl = req->args_nvl;
3180 fe = ctl_frontend_find(req->driver);
3182 req->status = CTL_LUN_ERROR;
3183 snprintf(req->error_str, sizeof(req->error_str),
3184 "Frontend \"%s\" not found.", req->driver);
3188 if (req->args != NULL) {
3189 packed = malloc(req->args_len, M_CTL, M_WAITOK);
3190 if (copyin(req->args, packed, req->args_len) != 0) {
3191 free(packed, M_CTL);
3192 req->status = CTL_LUN_ERROR;
3193 snprintf(req->error_str, sizeof(req->error_str),
3194 "Cannot copyin args.");
3197 req->args_nvl = nvlist_unpack(packed,
3199 free(packed, M_CTL);
3201 if (req->args_nvl == NULL) {
3202 req->status = CTL_LUN_ERROR;
3203 snprintf(req->error_str, sizeof(req->error_str),
3204 "Cannot unpack args nvlist.");
3208 req->args_nvl = nvlist_create(0);
3211 retval = fe->ioctl(dev, cmd, addr, flag, td);
3215 nvlist_destroy(req->args_nvl);
3216 req->args_nvl = tmp_args_nvl;
3218 if (req->result_nvl != NULL) {
3219 if (req->result != NULL) {
3220 packed = nvlist_pack(req->result_nvl,
3222 if (packed == NULL) {
3223 req->status = CTL_LUN_ERROR;
3224 snprintf(req->error_str,
3225 sizeof(req->error_str),
3226 "Cannot pack result nvlist.");
3230 if (packed_len > req->result_len) {
3231 req->status = CTL_LUN_ERROR;
3232 snprintf(req->error_str,
3233 sizeof(req->error_str),
3234 "Result nvlist too large.");
3235 free(packed, M_NVLIST);
3239 if (copyout(packed, req->result, packed_len)) {
3240 req->status = CTL_LUN_ERROR;
3241 snprintf(req->error_str,
3242 sizeof(req->error_str),
3243 "Cannot copyout() the result.");
3244 free(packed, M_NVLIST);
3248 req->result_len = packed_len;
3249 free(packed, M_NVLIST);
3252 nvlist_destroy(req->result_nvl);
3256 case CTL_PORT_LIST: {
3258 struct ctl_port *port;
3259 struct ctl_lun_list *list;
3260 const char *name, *value;
3265 list = (struct ctl_lun_list *)addr;
3267 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3269 list->status = CTL_LUN_LIST_ERROR;
3270 snprintf(list->error_str, sizeof(list->error_str),
3271 "Unable to allocate %d bytes for LUN list",
3276 sbuf_printf(sb, "<ctlportlist>\n");
3278 mtx_lock(&softc->ctl_lock);
3279 STAILQ_FOREACH(port, &softc->port_list, links) {
3280 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3281 (uintmax_t)port->targ_port);
3284 * Bail out as soon as we see that we've overfilled
3290 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3291 "</frontend_type>\n", port->frontend->name);
3295 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3300 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3301 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3305 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3310 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3311 port->physical_port);
3315 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3316 port->virtual_port);
3320 if (port->target_devid != NULL) {
3321 sbuf_printf(sb, "\t<target>");
3322 ctl_id_sbuf(port->target_devid, sb);
3323 sbuf_printf(sb, "</target>\n");
3326 if (port->port_devid != NULL) {
3327 sbuf_printf(sb, "\t<port>");
3328 ctl_id_sbuf(port->port_devid, sb);
3329 sbuf_printf(sb, "</port>\n");
3332 if (port->port_info != NULL) {
3333 retval = port->port_info(port->onoff_arg, sb);
3339 while ((name = nvlist_next(port->options, &type,
3340 &cookie)) != NULL) {
3341 sbuf_printf(sb, "\t<%s>", name);
3343 if (type == NV_TYPE_STRING) {
3344 value = dnvlist_get_string(port->options,
3347 sbuf_printf(sb, "%s", value);
3350 sbuf_printf(sb, "</%s>\n", name);
3353 if (port->lun_map != NULL) {
3354 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3355 for (j = 0; j < port->lun_map_size; j++) {
3356 plun = ctl_lun_map_from_port(port, j);
3357 if (plun == UINT32_MAX)
3360 "\t<lun id=\"%u\">%u</lun>\n",
3365 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3366 if (port->wwpn_iid[j].in_use == 0 ||
3367 (port->wwpn_iid[j].wwpn == 0 &&
3368 port->wwpn_iid[j].name == NULL))
3371 if (port->wwpn_iid[j].name != NULL)
3372 retval = sbuf_printf(sb,
3373 "\t<initiator id=\"%u\">%s</initiator>\n",
3374 j, port->wwpn_iid[j].name);
3376 retval = sbuf_printf(sb,
3377 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3378 j, port->wwpn_iid[j].wwpn);
3385 retval = sbuf_printf(sb, "</targ_port>\n");
3389 mtx_unlock(&softc->ctl_lock);
3392 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3395 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3396 snprintf(list->error_str, sizeof(list->error_str),
3397 "Out of space, %d bytes is too small",
3404 retval = copyout(sbuf_data(sb), list->lun_xml,
3407 list->fill_len = sbuf_len(sb) + 1;
3408 list->status = CTL_LUN_LIST_OK;
3413 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3414 struct ctl_port *port;
3416 mtx_lock(&softc->ctl_lock);
3417 if (lm->port < softc->port_min ||
3418 lm->port >= softc->port_max ||
3419 (port = softc->ctl_ports[lm->port]) == NULL) {
3420 mtx_unlock(&softc->ctl_lock);
3423 if (port->status & CTL_PORT_STATUS_ONLINE) {
3424 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3425 if (ctl_lun_map_to_port(port, lun->lun) ==
3428 mtx_lock(&lun->lun_lock);
3429 ctl_est_ua_port(lun, lm->port, -1,
3431 mtx_unlock(&lun->lun_lock);
3434 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3435 if (lm->plun != UINT32_MAX) {
3436 if (lm->lun == UINT32_MAX)
3437 retval = ctl_lun_map_unset(port, lm->plun);
3438 else if (lm->lun < ctl_max_luns &&
3439 softc->ctl_luns[lm->lun] != NULL)
3440 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3444 if (lm->lun == UINT32_MAX)
3445 retval = ctl_lun_map_deinit(port);
3447 retval = ctl_lun_map_init(port);
3449 if (port->status & CTL_PORT_STATUS_ONLINE)
3450 ctl_isc_announce_port(port);
3453 case CTL_GET_LUN_STATS: {
3454 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3458 * XXX KDM no locking here. If the LUN list changes,
3459 * things can blow up.
3462 stats->status = CTL_SS_OK;
3463 stats->fill_len = 0;
3464 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3465 if (lun->lun < stats->first_item)
3467 if (stats->fill_len + sizeof(lun->stats) >
3469 stats->status = CTL_SS_NEED_MORE_SPACE;
3472 retval = copyout(&lun->stats, &stats->stats[i++],
3473 sizeof(lun->stats));
3476 stats->fill_len += sizeof(lun->stats);
3478 stats->num_items = softc->num_luns;
3479 stats->flags = CTL_STATS_FLAG_NONE;
3481 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3483 getnanouptime(&stats->timestamp);
3486 case CTL_GET_PORT_STATS: {
3487 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3491 * XXX KDM no locking here. If the LUN list changes,
3492 * things can blow up.
3495 stats->status = CTL_SS_OK;
3496 stats->fill_len = 0;
3497 STAILQ_FOREACH(port, &softc->port_list, links) {
3498 if (port->targ_port < stats->first_item)
3500 if (stats->fill_len + sizeof(port->stats) >
3502 stats->status = CTL_SS_NEED_MORE_SPACE;
3505 retval = copyout(&port->stats, &stats->stats[i++],
3506 sizeof(port->stats));
3509 stats->fill_len += sizeof(port->stats);
3511 stats->num_items = softc->num_ports;
3512 stats->flags = CTL_STATS_FLAG_NONE;
3514 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3516 getnanouptime(&stats->timestamp);
3520 /* XXX KDM should we fix this? */
3522 struct ctl_backend_driver *backend;
3529 * We encode the backend type as the ioctl type for backend
3530 * ioctls. So parse it out here, and then search for a
3531 * backend of this type.
3533 type = _IOC_TYPE(cmd);
3535 STAILQ_FOREACH(backend, &softc->be_list, links) {
3536 if (backend->type == type) {
3542 printf("ctl: unknown ioctl command %#lx or backend "
3547 retval = backend->ioctl(dev, cmd, addr, flag, td);
3557 ctl_get_initindex(struct ctl_nexus *nexus)
3559 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3563 ctl_lun_map_init(struct ctl_port *port)
3565 struct ctl_softc *softc = port->ctl_softc;
3566 struct ctl_lun *lun;
3567 int size = ctl_lun_map_size;
3570 if (port->lun_map == NULL || port->lun_map_size < size) {
3571 port->lun_map_size = 0;
3572 free(port->lun_map, M_CTL);
3573 port->lun_map = malloc(size * sizeof(uint32_t),
3576 if (port->lun_map == NULL)
3578 for (i = 0; i < size; i++)
3579 port->lun_map[i] = UINT32_MAX;
3580 port->lun_map_size = size;
3581 if (port->status & CTL_PORT_STATUS_ONLINE) {
3582 if (port->lun_disable != NULL) {
3583 STAILQ_FOREACH(lun, &softc->lun_list, links)
3584 port->lun_disable(port->targ_lun_arg, lun->lun);
3586 ctl_isc_announce_port(port);
3592 ctl_lun_map_deinit(struct ctl_port *port)
3594 struct ctl_softc *softc = port->ctl_softc;
3595 struct ctl_lun *lun;
3597 if (port->lun_map == NULL)
3599 port->lun_map_size = 0;
3600 free(port->lun_map, M_CTL);
3601 port->lun_map = NULL;
3602 if (port->status & CTL_PORT_STATUS_ONLINE) {
3603 if (port->lun_enable != NULL) {
3604 STAILQ_FOREACH(lun, &softc->lun_list, links)
3605 port->lun_enable(port->targ_lun_arg, lun->lun);
3607 ctl_isc_announce_port(port);
3613 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3618 if (port->lun_map == NULL) {
3619 status = ctl_lun_map_init(port);
3623 if (plun >= port->lun_map_size)
3625 old = port->lun_map[plun];
3626 port->lun_map[plun] = glun;
3627 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3628 if (port->lun_enable != NULL)
3629 port->lun_enable(port->targ_lun_arg, plun);
3630 ctl_isc_announce_port(port);
3636 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3640 if (port->lun_map == NULL || plun >= port->lun_map_size)
3642 old = port->lun_map[plun];
3643 port->lun_map[plun] = UINT32_MAX;
3644 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3645 if (port->lun_disable != NULL)
3646 port->lun_disable(port->targ_lun_arg, plun);
3647 ctl_isc_announce_port(port);
3653 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3657 return (UINT32_MAX);
3658 if (port->lun_map == NULL)
3660 if (lun_id > port->lun_map_size)
3661 return (UINT32_MAX);
3662 return (port->lun_map[lun_id]);
3666 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3671 return (UINT32_MAX);
3672 if (port->lun_map == NULL)
3674 for (i = 0; i < port->lun_map_size; i++) {
3675 if (port->lun_map[i] == lun_id)
3678 return (UINT32_MAX);
3682 ctl_decode_lun(uint64_t encoded)
3685 uint32_t result = 0xffffffff;
3687 be64enc(lun, encoded);
3688 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3689 case RPL_LUNDATA_ATYP_PERIPH:
3690 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3691 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3694 case RPL_LUNDATA_ATYP_FLAT:
3695 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3696 lun[6] == 0 && lun[7] == 0)
3697 result = ((lun[0] & 0x3f) << 8) + lun[1];
3699 case RPL_LUNDATA_ATYP_EXTLUN:
3700 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3702 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3707 result = (lun[1] << 16) + (lun[2] << 8) +
3711 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3712 result = (lun[2] << 24) +
3713 (lun[3] << 16) + (lun[4] << 8) +
3718 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3719 result = 0xffffffff;
3728 ctl_encode_lun(uint32_t decoded)
3730 uint64_t l = decoded;
3733 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3735 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3737 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3739 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3743 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3747 for (i = first; i < last; i++) {
3748 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3755 ctl_set_mask(uint32_t *mask, uint32_t bit)
3757 uint32_t chunk, piece;
3760 piece = bit % (sizeof(uint32_t) * 8);
3762 if ((mask[chunk] & (1 << piece)) != 0)
3765 mask[chunk] |= (1 << piece);
3771 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3773 uint32_t chunk, piece;
3776 piece = bit % (sizeof(uint32_t) * 8);
3778 if ((mask[chunk] & (1 << piece)) == 0)
3781 mask[chunk] &= ~(1 << piece);
3787 ctl_is_set(uint32_t *mask, uint32_t bit)
3789 uint32_t chunk, piece;
3792 piece = bit % (sizeof(uint32_t) * 8);
3794 if ((mask[chunk] & (1 << piece)) == 0)
3801 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3805 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3808 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3812 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3816 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3819 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3823 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3828 i = residx/CTL_MAX_INIT_PER_PORT;
3829 if (lun->pr_keys[i] != NULL)
3831 mtx_unlock(&lun->lun_lock);
3832 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3834 mtx_lock(&lun->lun_lock);
3835 if (lun->pr_keys[i] == NULL)
3836 lun->pr_keys[i] = p;
3842 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3846 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3847 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3848 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3852 * ctl_softc, pool_name, total_ctl_io are passed in.
3853 * npool is passed out.
3856 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3857 uint32_t total_ctl_io, void **npool)
3859 struct ctl_io_pool *pool;
3861 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3866 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3867 pool->ctl_softc = ctl_softc;
3869 pool->zone = uma_zsecond_create(pool->name, NULL,
3870 NULL, NULL, NULL, ctl_softc->io_zone);
3871 /* uma_prealloc(pool->zone, total_ctl_io); */
3873 pool->zone = ctl_softc->io_zone;
3881 ctl_pool_free(struct ctl_io_pool *pool)
3888 uma_zdestroy(pool->zone);
3894 ctl_alloc_io(void *pool_ref)
3896 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3899 io = uma_zalloc(pool->zone, M_WAITOK);
3901 io->io_hdr.pool = pool_ref;
3902 CTL_SOFTC(io) = pool->ctl_softc;
3908 ctl_alloc_io_nowait(void *pool_ref)
3910 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3913 io = uma_zalloc(pool->zone, M_NOWAIT);
3915 io->io_hdr.pool = pool_ref;
3916 CTL_SOFTC(io) = pool->ctl_softc;
3922 ctl_free_io(union ctl_io *io)
3924 struct ctl_io_pool *pool;
3929 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3930 uma_zfree(pool->zone, io);
3934 ctl_zero_io(union ctl_io *io)
3936 struct ctl_io_pool *pool;
3942 * May need to preserve linked list pointers at some point too.
3944 pool = io->io_hdr.pool;
3945 memset(io, 0, sizeof(*io));
3946 io->io_hdr.pool = pool;
3947 CTL_SOFTC(io) = pool->ctl_softc;
3951 ctl_expand_number(const char *buf, uint64_t *num)
3957 number = strtoq(buf, &endptr, 0);
3959 switch (tolower((unsigned char)*endptr)) {
3979 case '\0': /* No unit. */
3983 /* Unrecognized unit. */
3987 if ((number << shift) >> shift != number) {
3991 *num = number << shift;
3997 * This routine could be used in the future to load default and/or saved
3998 * mode page parameters for a particuar lun.
4001 ctl_init_page_index(struct ctl_lun *lun)
4004 struct ctl_page_index *page_index;
4008 memcpy(&lun->mode_pages.index, page_index_template,
4009 sizeof(page_index_template));
4011 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4013 page_index = &lun->mode_pages.index[i];
4014 if (lun->be_lun->lun_type == T_DIRECT &&
4015 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4017 if (lun->be_lun->lun_type == T_PROCESSOR &&
4018 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4020 if (lun->be_lun->lun_type == T_CDROM &&
4021 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4024 page_code = page_index->page_code & SMPH_PC_MASK;
4025 switch (page_code) {
4026 case SMS_RW_ERROR_RECOVERY_PAGE: {
4027 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4028 ("subpage %#x for page %#x is incorrect!",
4029 page_index->subpage, page_code));
4030 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4031 &rw_er_page_default,
4032 sizeof(rw_er_page_default));
4033 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4034 &rw_er_page_changeable,
4035 sizeof(rw_er_page_changeable));
4036 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4037 &rw_er_page_default,
4038 sizeof(rw_er_page_default));
4039 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4040 &rw_er_page_default,
4041 sizeof(rw_er_page_default));
4042 page_index->page_data =
4043 (uint8_t *)lun->mode_pages.rw_er_page;
4046 case SMS_FORMAT_DEVICE_PAGE: {
4047 struct scsi_format_page *format_page;
4049 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4050 ("subpage %#x for page %#x is incorrect!",
4051 page_index->subpage, page_code));
4054 * Sectors per track are set above. Bytes per
4055 * sector need to be set here on a per-LUN basis.
4057 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4058 &format_page_default,
4059 sizeof(format_page_default));
4060 memcpy(&lun->mode_pages.format_page[
4061 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4062 sizeof(format_page_changeable));
4063 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4064 &format_page_default,
4065 sizeof(format_page_default));
4066 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4067 &format_page_default,
4068 sizeof(format_page_default));
4070 format_page = &lun->mode_pages.format_page[
4072 scsi_ulto2b(lun->be_lun->blocksize,
4073 format_page->bytes_per_sector);
4075 format_page = &lun->mode_pages.format_page[
4077 scsi_ulto2b(lun->be_lun->blocksize,
4078 format_page->bytes_per_sector);
4080 format_page = &lun->mode_pages.format_page[
4082 scsi_ulto2b(lun->be_lun->blocksize,
4083 format_page->bytes_per_sector);
4085 page_index->page_data =
4086 (uint8_t *)lun->mode_pages.format_page;
4089 case SMS_RIGID_DISK_PAGE: {
4090 struct scsi_rigid_disk_page *rigid_disk_page;
4091 uint32_t sectors_per_cylinder;
4095 #endif /* !__XSCALE__ */
4097 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4098 ("subpage %#x for page %#x is incorrect!",
4099 page_index->subpage, page_code));
4102 * Rotation rate and sectors per track are set
4103 * above. We calculate the cylinders here based on
4104 * capacity. Due to the number of heads and
4105 * sectors per track we're using, smaller arrays
4106 * may turn out to have 0 cylinders. Linux and
4107 * FreeBSD don't pay attention to these mode pages
4108 * to figure out capacity, but Solaris does. It
4109 * seems to deal with 0 cylinders just fine, and
4110 * works out a fake geometry based on the capacity.
4112 memcpy(&lun->mode_pages.rigid_disk_page[
4113 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4114 sizeof(rigid_disk_page_default));
4115 memcpy(&lun->mode_pages.rigid_disk_page[
4116 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4117 sizeof(rigid_disk_page_changeable));
4119 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4123 * The divide method here will be more accurate,
4124 * probably, but results in floating point being
4125 * used in the kernel on i386 (__udivdi3()). On the
4126 * XScale, though, __udivdi3() is implemented in
4129 * The shift method for cylinder calculation is
4130 * accurate if sectors_per_cylinder is a power of
4131 * 2. Otherwise it might be slightly off -- you
4132 * might have a bit of a truncation problem.
4135 cylinders = (lun->be_lun->maxlba + 1) /
4136 sectors_per_cylinder;
4138 for (shift = 31; shift > 0; shift--) {
4139 if (sectors_per_cylinder & (1 << shift))
4142 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4146 * We've basically got 3 bytes, or 24 bits for the
4147 * cylinder size in the mode page. If we're over,
4148 * just round down to 2^24.
4150 if (cylinders > 0xffffff)
4151 cylinders = 0xffffff;
4153 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4155 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4157 if ((value = dnvlist_get_string(lun->be_lun->options,
4158 "rpm", NULL)) != NULL) {
4159 scsi_ulto2b(strtol(value, NULL, 0),
4160 rigid_disk_page->rotation_rate);
4163 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4164 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4165 sizeof(rigid_disk_page_default));
4166 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4167 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4168 sizeof(rigid_disk_page_default));
4170 page_index->page_data =
4171 (uint8_t *)lun->mode_pages.rigid_disk_page;
4174 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4175 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4176 ("subpage %#x for page %#x is incorrect!",
4177 page_index->subpage, page_code));
4178 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4179 &verify_er_page_default,
4180 sizeof(verify_er_page_default));
4181 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4182 &verify_er_page_changeable,
4183 sizeof(verify_er_page_changeable));
4184 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4185 &verify_er_page_default,
4186 sizeof(verify_er_page_default));
4187 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4188 &verify_er_page_default,
4189 sizeof(verify_er_page_default));
4190 page_index->page_data =
4191 (uint8_t *)lun->mode_pages.verify_er_page;
4194 case SMS_CACHING_PAGE: {
4195 struct scsi_caching_page *caching_page;
4197 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4198 ("subpage %#x for page %#x is incorrect!",
4199 page_index->subpage, page_code));
4200 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4201 &caching_page_default,
4202 sizeof(caching_page_default));
4203 memcpy(&lun->mode_pages.caching_page[
4204 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4205 sizeof(caching_page_changeable));
4206 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4207 &caching_page_default,
4208 sizeof(caching_page_default));
4209 caching_page = &lun->mode_pages.caching_page[
4211 value = dnvlist_get_string(lun->be_lun->options,
4212 "writecache", NULL);
4213 if (value != NULL && strcmp(value, "off") == 0)
4214 caching_page->flags1 &= ~SCP_WCE;
4215 value = dnvlist_get_string(lun->be_lun->options,
4217 if (value != NULL && strcmp(value, "off") == 0)
4218 caching_page->flags1 |= SCP_RCD;
4219 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4220 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4221 sizeof(caching_page_default));
4222 page_index->page_data =
4223 (uint8_t *)lun->mode_pages.caching_page;
4226 case SMS_CONTROL_MODE_PAGE: {
4227 switch (page_index->subpage) {
4228 case SMS_SUBPAGE_PAGE_0: {
4229 struct scsi_control_page *control_page;
4231 memcpy(&lun->mode_pages.control_page[
4233 &control_page_default,
4234 sizeof(control_page_default));
4235 memcpy(&lun->mode_pages.control_page[
4236 CTL_PAGE_CHANGEABLE],
4237 &control_page_changeable,
4238 sizeof(control_page_changeable));
4239 memcpy(&lun->mode_pages.control_page[
4241 &control_page_default,
4242 sizeof(control_page_default));
4243 control_page = &lun->mode_pages.control_page[
4245 value = dnvlist_get_string(lun->be_lun->options,
4246 "reordering", NULL);
4247 if (value != NULL &&
4248 strcmp(value, "unrestricted") == 0) {
4249 control_page->queue_flags &=
4250 ~SCP_QUEUE_ALG_MASK;
4251 control_page->queue_flags |=
4252 SCP_QUEUE_ALG_UNRESTRICTED;
4254 memcpy(&lun->mode_pages.control_page[
4256 &lun->mode_pages.control_page[
4258 sizeof(control_page_default));
4259 page_index->page_data =
4260 (uint8_t *)lun->mode_pages.control_page;
4264 memcpy(&lun->mode_pages.control_ext_page[
4266 &control_ext_page_default,
4267 sizeof(control_ext_page_default));
4268 memcpy(&lun->mode_pages.control_ext_page[
4269 CTL_PAGE_CHANGEABLE],
4270 &control_ext_page_changeable,
4271 sizeof(control_ext_page_changeable));
4272 memcpy(&lun->mode_pages.control_ext_page[
4274 &control_ext_page_default,
4275 sizeof(control_ext_page_default));
4276 memcpy(&lun->mode_pages.control_ext_page[
4278 &lun->mode_pages.control_ext_page[
4280 sizeof(control_ext_page_default));
4281 page_index->page_data =
4282 (uint8_t *)lun->mode_pages.control_ext_page;
4285 panic("subpage %#x for page %#x is incorrect!",
4286 page_index->subpage, page_code);
4290 case SMS_INFO_EXCEPTIONS_PAGE: {
4291 switch (page_index->subpage) {
4292 case SMS_SUBPAGE_PAGE_0:
4293 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4295 sizeof(ie_page_default));
4296 memcpy(&lun->mode_pages.ie_page[
4297 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4298 sizeof(ie_page_changeable));
4299 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4301 sizeof(ie_page_default));
4302 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4304 sizeof(ie_page_default));
4305 page_index->page_data =
4306 (uint8_t *)lun->mode_pages.ie_page;
4309 struct ctl_logical_block_provisioning_page *page;
4311 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4313 sizeof(lbp_page_default));
4314 memcpy(&lun->mode_pages.lbp_page[
4315 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4316 sizeof(lbp_page_changeable));
4317 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4319 sizeof(lbp_page_default));
4320 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4321 value = dnvlist_get_string(lun->be_lun->options,
4322 "avail-threshold", NULL);
4323 if (value != NULL &&
4324 ctl_expand_number(value, &ival) == 0) {
4325 page->descr[0].flags |= SLBPPD_ENABLED |
4327 if (lun->be_lun->blocksize)
4328 ival /= lun->be_lun->blocksize;
4331 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4332 page->descr[0].count);
4334 value = dnvlist_get_string(lun->be_lun->options,
4335 "used-threshold", NULL);
4336 if (value != NULL &&
4337 ctl_expand_number(value, &ival) == 0) {
4338 page->descr[1].flags |= SLBPPD_ENABLED |
4340 if (lun->be_lun->blocksize)
4341 ival /= lun->be_lun->blocksize;
4344 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4345 page->descr[1].count);
4347 value = dnvlist_get_string(lun->be_lun->options,
4348 "pool-avail-threshold", NULL);
4349 if (value != NULL &&
4350 ctl_expand_number(value, &ival) == 0) {
4351 page->descr[2].flags |= SLBPPD_ENABLED |
4353 if (lun->be_lun->blocksize)
4354 ival /= lun->be_lun->blocksize;
4357 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4358 page->descr[2].count);
4360 value = dnvlist_get_string(lun->be_lun->options,
4361 "pool-used-threshold", NULL);
4362 if (value != NULL &&
4363 ctl_expand_number(value, &ival) == 0) {
4364 page->descr[3].flags |= SLBPPD_ENABLED |
4366 if (lun->be_lun->blocksize)
4367 ival /= lun->be_lun->blocksize;
4370 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4371 page->descr[3].count);
4373 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4374 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4375 sizeof(lbp_page_default));
4376 page_index->page_data =
4377 (uint8_t *)lun->mode_pages.lbp_page;
4381 panic("subpage %#x for page %#x is incorrect!",
4382 page_index->subpage, page_code);
4386 case SMS_CDDVD_CAPS_PAGE:{
4387 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4388 ("subpage %#x for page %#x is incorrect!",
4389 page_index->subpage, page_code));
4390 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4391 &cddvd_page_default,
4392 sizeof(cddvd_page_default));
4393 memcpy(&lun->mode_pages.cddvd_page[
4394 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4395 sizeof(cddvd_page_changeable));
4396 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4397 &cddvd_page_default,
4398 sizeof(cddvd_page_default));
4399 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4400 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4401 sizeof(cddvd_page_default));
4402 page_index->page_data =
4403 (uint8_t *)lun->mode_pages.cddvd_page;
4407 panic("invalid page code value %#x", page_code);
4411 return (CTL_RETVAL_COMPLETE);
4415 ctl_init_log_page_index(struct ctl_lun *lun)
4417 struct ctl_page_index *page_index;
4420 memcpy(&lun->log_pages.index, log_page_index_template,
4421 sizeof(log_page_index_template));
4424 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4426 page_index = &lun->log_pages.index[i];
4427 if (lun->be_lun->lun_type == T_DIRECT &&
4428 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4430 if (lun->be_lun->lun_type == T_PROCESSOR &&
4431 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4433 if (lun->be_lun->lun_type == T_CDROM &&
4434 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4437 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4438 lun->backend->lun_attr == NULL)
4441 if (page_index->page_code != prev) {
4442 lun->log_pages.pages_page[j] = page_index->page_code;
4443 prev = page_index->page_code;
4446 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4447 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4450 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4451 lun->log_pages.index[0].page_len = j;
4452 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4453 lun->log_pages.index[1].page_len = k * 2;
4454 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4455 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4456 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4457 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4458 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4459 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4461 return (CTL_RETVAL_COMPLETE);
4465 hex2bin(const char *str, uint8_t *buf, int buf_size)
4470 memset(buf, 0, buf_size);
4471 while (isspace(str[0]))
4473 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4476 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4477 while (str[i] == '-') /* Skip dashes in UUIDs. */
4482 else if (isalpha(c))
4483 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4489 buf[i / 2] |= (c << 4);
4493 return ((i + 1) / 2);
4500 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4501 * wants us to allocate the LUN and he can block.
4502 * - ctl_softc is always set
4503 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4505 * Returns 0 for success, non-zero (errno) for failure.
4508 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4509 struct ctl_be_lun *const be_lun)
4511 struct ctl_lun *nlun, *lun;
4512 struct scsi_vpd_id_descriptor *desc;
4513 struct scsi_vpd_id_t10 *t10id;
4514 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4515 int lun_number, lun_malloced;
4516 int devidlen, idlen1, idlen2 = 0, len;
4522 * We currently only support Direct Access or Processor LUN types.
4524 switch (be_lun->lun_type) {
4532 be_lun->lun_config_status(be_lun->be_lun,
4533 CTL_LUN_CONFIG_FAILURE);
4536 if (ctl_lun == NULL) {
4537 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4544 memset(lun, 0, sizeof(*lun));
4546 lun->flags = CTL_LUN_MALLOCED;
4548 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4549 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4550 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4551 M_DEVBUF, M_WAITOK | M_ZERO);
4552 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4553 M_DEVBUF, M_WAITOK | M_ZERO);
4555 /* Generate LUN ID. */
4556 devidlen = max(CTL_DEVID_MIN_LEN,
4557 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4558 idlen1 = sizeof(*t10id) + devidlen;
4559 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4560 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL);
4561 if (scsiname != NULL) {
4562 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4563 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4565 eui = dnvlist_get_string(be_lun->options, "eui", NULL);
4567 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4569 naa = dnvlist_get_string(be_lun->options, "naa", NULL);
4571 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4573 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL);
4575 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4577 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4578 M_CTL, M_WAITOK | M_ZERO);
4579 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4580 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4581 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4582 desc->length = idlen1;
4583 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4584 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4585 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) {
4586 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4588 strncpy(t10id->vendor, vendor,
4589 min(sizeof(t10id->vendor), strlen(vendor)));
4591 strncpy((char *)t10id->vendor_spec_id,
4592 (char *)be_lun->device_id, devidlen);
4593 if (scsiname != NULL) {
4594 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4596 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4597 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4598 SVPD_ID_TYPE_SCSI_NAME;
4599 desc->length = idlen2;
4600 strlcpy(desc->identifier, scsiname, idlen2);
4603 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4605 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4606 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4608 desc->length = hex2bin(eui, desc->identifier, 16);
4609 desc->length = desc->length > 12 ? 16 :
4610 (desc->length > 8 ? 12 : 8);
4611 len -= 16 - desc->length;
4614 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4616 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4617 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4619 desc->length = hex2bin(naa, desc->identifier, 16);
4620 desc->length = desc->length > 8 ? 16 : 8;
4621 len -= 16 - desc->length;
4624 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4626 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4627 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4629 desc->identifier[0] = 0x10;
4630 hex2bin(uuid, &desc->identifier[2], 16);
4633 lun->lun_devid->len = len;
4635 mtx_lock(&ctl_softc->ctl_lock);
4637 * See if the caller requested a particular LUN number. If so, see
4638 * if it is available. Otherwise, allocate the first available LUN.
4640 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4641 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4642 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4643 mtx_unlock(&ctl_softc->ctl_lock);
4644 if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4645 printf("ctl: requested LUN ID %d is higher "
4646 "than ctl_max_luns - 1 (%d)\n",
4647 be_lun->req_lun_id, ctl_max_luns - 1);
4650 * XXX KDM return an error, or just assign
4651 * another LUN ID in this case??
4653 printf("ctl: requested LUN ID %d is already "
4654 "in use\n", be_lun->req_lun_id);
4657 free(lun->lun_devid, M_CTL);
4658 if (lun->flags & CTL_LUN_MALLOCED)
4660 be_lun->lun_config_status(be_lun->be_lun,
4661 CTL_LUN_CONFIG_FAILURE);
4664 lun_number = be_lun->req_lun_id;
4666 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4667 if (lun_number == -1) {
4668 mtx_unlock(&ctl_softc->ctl_lock);
4669 printf("ctl: can't allocate LUN, out of LUNs\n");
4673 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4674 mtx_unlock(&ctl_softc->ctl_lock);
4676 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4677 lun->lun = lun_number;
4678 lun->be_lun = be_lun;
4680 * The processor LUN is always enabled. Disk LUNs come on line
4681 * disabled, and must be enabled by the backend.
4683 lun->flags |= CTL_LUN_DISABLED;
4684 lun->backend = be_lun->be;
4685 be_lun->ctl_lun = lun;
4686 be_lun->lun_id = lun_number;
4687 atomic_add_int(&be_lun->be->num_luns, 1);
4688 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4689 lun->flags |= CTL_LUN_EJECTED;
4690 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4691 lun->flags |= CTL_LUN_NO_MEDIA;
4692 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4693 lun->flags |= CTL_LUN_STOPPED;
4695 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4696 lun->flags |= CTL_LUN_PRIMARY_SC;
4698 value = dnvlist_get_string(be_lun->options, "removable", NULL);
4699 if (value != NULL) {
4700 if (strcmp(value, "on") == 0)
4701 lun->flags |= CTL_LUN_REMOVABLE;
4702 } else if (be_lun->lun_type == T_CDROM)
4703 lun->flags |= CTL_LUN_REMOVABLE;
4705 lun->ctl_softc = ctl_softc;
4707 lun->last_busy = getsbinuptime();
4709 TAILQ_INIT(&lun->ooa_queue);
4710 TAILQ_INIT(&lun->blocked_queue);
4711 STAILQ_INIT(&lun->error_list);
4712 lun->ie_reported = 1;
4713 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4714 ctl_tpc_lun_init(lun);
4715 if (lun->flags & CTL_LUN_REMOVABLE) {
4716 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4721 * Initialize the mode and log page index.
4723 ctl_init_page_index(lun);
4724 ctl_init_log_page_index(lun);
4726 /* Setup statistics gathering */
4727 lun->stats.item = lun_number;
4730 * Now, before we insert this lun on the lun list, set the lun
4731 * inventory changed UA for all other luns.
4733 mtx_lock(&ctl_softc->ctl_lock);
4734 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4735 mtx_lock(&nlun->lun_lock);
4736 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4737 mtx_unlock(&nlun->lun_lock);
4739 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4740 ctl_softc->ctl_luns[lun_number] = lun;
4741 ctl_softc->num_luns++;
4742 mtx_unlock(&ctl_softc->ctl_lock);
4744 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4751 * - LUN has already been marked invalid and any pending I/O has been taken
4755 ctl_free_lun(struct ctl_lun *lun)
4757 struct ctl_softc *softc = lun->ctl_softc;
4758 struct ctl_lun *nlun;
4761 KASSERT(TAILQ_EMPTY(&lun->ooa_queue),
4762 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4764 mtx_lock(&softc->ctl_lock);
4765 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4766 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4767 softc->ctl_luns[lun->lun] = NULL;
4769 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4770 mtx_lock(&nlun->lun_lock);
4771 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4772 mtx_unlock(&nlun->lun_lock);
4774 mtx_unlock(&softc->ctl_lock);
4777 * Tell the backend to free resources, if this LUN has a backend.
4779 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4780 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4782 lun->ie_reportcnt = UINT32_MAX;
4783 callout_drain(&lun->ie_callout);
4784 ctl_tpc_lun_shutdown(lun);
4785 mtx_destroy(&lun->lun_lock);
4786 free(lun->lun_devid, M_CTL);
4787 for (i = 0; i < ctl_max_ports; i++)
4788 free(lun->pending_ua[i], M_CTL);
4789 free(lun->pending_ua, M_DEVBUF);
4790 for (i = 0; i < ctl_max_ports; i++)
4791 free(lun->pr_keys[i], M_CTL);
4792 free(lun->pr_keys, M_DEVBUF);
4793 free(lun->write_buffer, M_CTL);
4794 free(lun->prevent, M_CTL);
4795 if (lun->flags & CTL_LUN_MALLOCED)
4802 ctl_create_lun(struct ctl_be_lun *be_lun)
4806 * ctl_alloc_lun() should handle all potential failure cases.
4808 ctl_alloc_lun(control_softc, NULL, be_lun);
4812 ctl_add_lun(struct ctl_be_lun *be_lun)
4814 struct ctl_softc *softc = control_softc;
4816 mtx_lock(&softc->ctl_lock);
4817 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4818 mtx_unlock(&softc->ctl_lock);
4819 wakeup(&softc->pending_lun_queue);
4825 ctl_enable_lun(struct ctl_be_lun *be_lun)
4827 struct ctl_softc *softc;
4828 struct ctl_port *port, *nport;
4829 struct ctl_lun *lun;
4832 lun = (struct ctl_lun *)be_lun->ctl_lun;
4833 softc = lun->ctl_softc;
4835 mtx_lock(&softc->ctl_lock);
4836 mtx_lock(&lun->lun_lock);
4837 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4839 * eh? Why did we get called if the LUN is already
4842 mtx_unlock(&lun->lun_lock);
4843 mtx_unlock(&softc->ctl_lock);
4846 lun->flags &= ~CTL_LUN_DISABLED;
4847 mtx_unlock(&lun->lun_lock);
4849 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4850 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4851 port->lun_map != NULL || port->lun_enable == NULL)
4855 * Drop the lock while we call the FETD's enable routine.
4856 * This can lead to a callback into CTL (at least in the
4857 * case of the internal initiator frontend.
4859 mtx_unlock(&softc->ctl_lock);
4860 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4861 mtx_lock(&softc->ctl_lock);
4863 printf("%s: FETD %s port %d returned error "
4864 "%d for lun_enable on lun %jd\n",
4865 __func__, port->port_name, port->targ_port,
4866 retval, (intmax_t)lun->lun);
4870 mtx_unlock(&softc->ctl_lock);
4871 ctl_isc_announce_lun(lun);
4877 ctl_disable_lun(struct ctl_be_lun *be_lun)
4879 struct ctl_softc *softc;
4880 struct ctl_port *port;
4881 struct ctl_lun *lun;
4884 lun = (struct ctl_lun *)be_lun->ctl_lun;
4885 softc = lun->ctl_softc;
4887 mtx_lock(&softc->ctl_lock);
4888 mtx_lock(&lun->lun_lock);
4889 if (lun->flags & CTL_LUN_DISABLED) {
4890 mtx_unlock(&lun->lun_lock);
4891 mtx_unlock(&softc->ctl_lock);
4894 lun->flags |= CTL_LUN_DISABLED;
4895 mtx_unlock(&lun->lun_lock);
4897 STAILQ_FOREACH(port, &softc->port_list, links) {
4898 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4899 port->lun_map != NULL || port->lun_disable == NULL)
4903 * Drop the lock before we call the frontend's disable
4904 * routine, to avoid lock order reversals.
4906 * XXX KDM what happens if the frontend list changes while
4907 * we're traversing it? It's unlikely, but should be handled.
4909 mtx_unlock(&softc->ctl_lock);
4910 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4911 mtx_lock(&softc->ctl_lock);
4913 printf("%s: FETD %s port %d returned error "
4914 "%d for lun_disable on lun %jd\n",
4915 __func__, port->port_name, port->targ_port,
4916 retval, (intmax_t)lun->lun);
4920 mtx_unlock(&softc->ctl_lock);
4921 ctl_isc_announce_lun(lun);
4927 ctl_start_lun(struct ctl_be_lun *be_lun)
4929 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4931 mtx_lock(&lun->lun_lock);
4932 lun->flags &= ~CTL_LUN_STOPPED;
4933 mtx_unlock(&lun->lun_lock);
4938 ctl_stop_lun(struct ctl_be_lun *be_lun)
4940 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4942 mtx_lock(&lun->lun_lock);
4943 lun->flags |= CTL_LUN_STOPPED;
4944 mtx_unlock(&lun->lun_lock);
4949 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4951 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4953 mtx_lock(&lun->lun_lock);
4954 lun->flags |= CTL_LUN_NO_MEDIA;
4955 mtx_unlock(&lun->lun_lock);
4960 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4962 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4963 union ctl_ha_msg msg;
4965 mtx_lock(&lun->lun_lock);
4966 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4967 if (lun->flags & CTL_LUN_REMOVABLE)
4968 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4969 mtx_unlock(&lun->lun_lock);
4970 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4971 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4972 bzero(&msg.ua, sizeof(msg.ua));
4973 msg.hdr.msg_type = CTL_MSG_UA;
4974 msg.hdr.nexus.initid = -1;
4975 msg.hdr.nexus.targ_port = -1;
4976 msg.hdr.nexus.targ_lun = lun->lun;
4977 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4980 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4981 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4988 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4990 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4992 mtx_lock(&lun->lun_lock);
4993 lun->flags |= CTL_LUN_EJECTED;
4994 mtx_unlock(&lun->lun_lock);
4999 ctl_lun_primary(struct ctl_be_lun *be_lun)
5001 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5003 mtx_lock(&lun->lun_lock);
5004 lun->flags |= CTL_LUN_PRIMARY_SC;
5005 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5006 mtx_unlock(&lun->lun_lock);
5007 ctl_isc_announce_lun(lun);
5012 ctl_lun_secondary(struct ctl_be_lun *be_lun)
5014 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5016 mtx_lock(&lun->lun_lock);
5017 lun->flags &= ~CTL_LUN_PRIMARY_SC;
5018 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5019 mtx_unlock(&lun->lun_lock);
5020 ctl_isc_announce_lun(lun);
5025 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5027 struct ctl_lun *lun;
5029 lun = (struct ctl_lun *)be_lun->ctl_lun;
5031 mtx_lock(&lun->lun_lock);
5034 * The LUN needs to be disabled before it can be marked invalid.
5036 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5037 mtx_unlock(&lun->lun_lock);
5041 * Mark the LUN invalid.
5043 lun->flags |= CTL_LUN_INVALID;
5046 * If there is nothing in the OOA queue, go ahead and free the LUN.
5047 * If we have something in the OOA queue, we'll free it when the
5048 * last I/O completes.
5050 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5051 mtx_unlock(&lun->lun_lock);
5054 mtx_unlock(&lun->lun_lock);
5060 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5062 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5063 union ctl_ha_msg msg;
5065 mtx_lock(&lun->lun_lock);
5066 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5067 mtx_unlock(&lun->lun_lock);
5068 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5069 /* Send msg to other side. */
5070 bzero(&msg.ua, sizeof(msg.ua));
5071 msg.hdr.msg_type = CTL_MSG_UA;
5072 msg.hdr.nexus.initid = -1;
5073 msg.hdr.nexus.targ_port = -1;
5074 msg.hdr.nexus.targ_lun = lun->lun;
5075 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5078 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5079 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5085 * Backend "memory move is complete" callback for requests that never
5086 * make it down to say RAIDCore's configuration code.
5089 ctl_config_move_done(union ctl_io *io)
5093 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5094 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5095 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5097 if ((io->io_hdr.port_status != 0) &&
5098 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5099 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5100 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5101 /*retry_count*/ io->io_hdr.port_status);
5102 } else if (io->scsiio.kern_data_resid != 0 &&
5103 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5104 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5105 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5106 ctl_set_invalid_field_ciu(&io->scsiio);
5109 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5111 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5112 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5113 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5114 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5116 * XXX KDM just assuming a single pointer here, and not a
5117 * S/G list. If we start using S/G lists for config data,
5118 * we'll need to know how to clean them up here as well.
5120 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5121 free(io->scsiio.kern_data_ptr, M_CTL);
5123 retval = CTL_RETVAL_COMPLETE;
5126 * XXX KDM now we need to continue data movement. Some
5128 * - call ctl_scsiio() again? We don't do this for data
5129 * writes, because for those at least we know ahead of
5130 * time where the write will go and how long it is. For
5131 * config writes, though, that information is largely
5132 * contained within the write itself, thus we need to
5133 * parse out the data again.
5135 * - Call some other function once the data is in?
5139 * XXX KDM call ctl_scsiio() again for now, and check flag
5140 * bits to see whether we're allocated or not.
5142 retval = ctl_scsiio(&io->scsiio);
5148 * This gets called by a backend driver when it is done with a
5149 * data_submit method.
5152 ctl_data_submit_done(union ctl_io *io)
5155 * If the IO_CONT flag is set, we need to call the supplied
5156 * function to continue processing the I/O, instead of completing
5159 * If there is an error, though, we don't want to keep processing.
5160 * Instead, just send status back to the initiator.
5162 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5163 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5164 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5165 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5166 io->scsiio.io_cont(io);
5173 * This gets called by a backend driver when it is done with a
5174 * configuration write.
5177 ctl_config_write_done(union ctl_io *io)
5182 * If the IO_CONT flag is set, we need to call the supplied
5183 * function to continue processing the I/O, instead of completing
5186 * If there is an error, though, we don't want to keep processing.
5187 * Instead, just send status back to the initiator.
5189 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5190 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5191 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5192 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5193 io->scsiio.io_cont(io);
5197 * Since a configuration write can be done for commands that actually
5198 * have data allocated, like write buffer, and commands that have
5199 * no data, like start/stop unit, we need to check here.
5201 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5202 buf = io->scsiio.kern_data_ptr;
5211 ctl_config_read_done(union ctl_io *io)
5216 * If there is some error -- we are done, skip data transfer.
5218 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5219 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5220 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5221 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5222 buf = io->scsiio.kern_data_ptr;
5232 * If the IO_CONT flag is set, we need to call the supplied
5233 * function to continue processing the I/O, instead of completing
5236 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5237 io->scsiio.io_cont(io);
5245 * SCSI release command.
5248 ctl_scsi_release(struct ctl_scsiio *ctsio)
5250 struct ctl_lun *lun = CTL_LUN(ctsio);
5253 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5255 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5258 * XXX KDM right now, we only support LUN reservation. We don't
5259 * support 3rd party reservations, or extent reservations, which
5260 * might actually need the parameter list. If we've gotten this
5261 * far, we've got a LUN reservation. Anything else got kicked out
5262 * above. So, according to SPC, ignore the length.
5265 mtx_lock(&lun->lun_lock);
5268 * According to SPC, it is not an error for an intiator to attempt
5269 * to release a reservation on a LUN that isn't reserved, or that
5270 * is reserved by another initiator. The reservation can only be
5271 * released, though, by the initiator who made it or by one of
5272 * several reset type events.
5274 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5275 lun->flags &= ~CTL_LUN_RESERVED;
5277 mtx_unlock(&lun->lun_lock);
5279 ctl_set_success(ctsio);
5280 ctl_done((union ctl_io *)ctsio);
5281 return (CTL_RETVAL_COMPLETE);
5285 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5287 struct ctl_lun *lun = CTL_LUN(ctsio);
5290 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5292 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5295 * XXX KDM right now, we only support LUN reservation. We don't
5296 * support 3rd party reservations, or extent reservations, which
5297 * might actually need the parameter list. If we've gotten this
5298 * far, we've got a LUN reservation. Anything else got kicked out
5299 * above. So, according to SPC, ignore the length.
5302 mtx_lock(&lun->lun_lock);
5303 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5304 ctl_set_reservation_conflict(ctsio);
5308 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5309 if (lun->flags & CTL_LUN_PR_RESERVED) {
5310 ctl_set_success(ctsio);
5314 lun->flags |= CTL_LUN_RESERVED;
5315 lun->res_idx = residx;
5316 ctl_set_success(ctsio);
5319 mtx_unlock(&lun->lun_lock);
5320 ctl_done((union ctl_io *)ctsio);
5321 return (CTL_RETVAL_COMPLETE);
5325 ctl_start_stop(struct ctl_scsiio *ctsio)
5327 struct ctl_lun *lun = CTL_LUN(ctsio);
5328 struct scsi_start_stop_unit *cdb;
5331 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5333 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5335 if ((cdb->how & SSS_PC_MASK) == 0) {
5336 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5337 (cdb->how & SSS_START) == 0) {
5340 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5341 if (ctl_get_prkey(lun, residx) == 0 ||
5342 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5344 ctl_set_reservation_conflict(ctsio);
5345 ctl_done((union ctl_io *)ctsio);
5346 return (CTL_RETVAL_COMPLETE);
5350 if ((cdb->how & SSS_LOEJ) &&
5351 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5352 ctl_set_invalid_field(ctsio,
5358 ctl_done((union ctl_io *)ctsio);
5359 return (CTL_RETVAL_COMPLETE);
5362 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5363 lun->prevent_count > 0) {
5364 /* "Medium removal prevented" */
5365 ctl_set_sense(ctsio, /*current_error*/ 1,
5366 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5367 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5368 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5369 ctl_done((union ctl_io *)ctsio);
5370 return (CTL_RETVAL_COMPLETE);
5374 retval = lun->backend->config_write((union ctl_io *)ctsio);
5379 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5381 struct ctl_lun *lun = CTL_LUN(ctsio);
5382 struct scsi_prevent *cdb;
5386 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5388 cdb = (struct scsi_prevent *)ctsio->cdb;
5390 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5391 ctl_set_invalid_opcode(ctsio);
5392 ctl_done((union ctl_io *)ctsio);
5393 return (CTL_RETVAL_COMPLETE);
5396 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5397 mtx_lock(&lun->lun_lock);
5398 if ((cdb->how & PR_PREVENT) &&
5399 ctl_is_set(lun->prevent, initidx) == 0) {
5400 ctl_set_mask(lun->prevent, initidx);
5401 lun->prevent_count++;
5402 } else if ((cdb->how & PR_PREVENT) == 0 &&
5403 ctl_is_set(lun->prevent, initidx)) {
5404 ctl_clear_mask(lun->prevent, initidx);
5405 lun->prevent_count--;
5407 mtx_unlock(&lun->lun_lock);
5408 retval = lun->backend->config_write((union ctl_io *)ctsio);
5413 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5414 * we don't really do anything with the LBA and length fields if the user
5415 * passes them in. Instead we'll just flush out the cache for the entire
5419 ctl_sync_cache(struct ctl_scsiio *ctsio)
5421 struct ctl_lun *lun = CTL_LUN(ctsio);
5422 struct ctl_lba_len_flags *lbalen;
5423 uint64_t starting_lba;
5424 uint32_t block_count;
5428 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5432 switch (ctsio->cdb[0]) {
5433 case SYNCHRONIZE_CACHE: {
5434 struct scsi_sync_cache *cdb;
5435 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5437 starting_lba = scsi_4btoul(cdb->begin_lba);
5438 block_count = scsi_2btoul(cdb->lb_count);
5442 case SYNCHRONIZE_CACHE_16: {
5443 struct scsi_sync_cache_16 *cdb;
5444 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5446 starting_lba = scsi_8btou64(cdb->begin_lba);
5447 block_count = scsi_4btoul(cdb->lb_count);
5452 ctl_set_invalid_opcode(ctsio);
5453 ctl_done((union ctl_io *)ctsio);
5455 break; /* NOTREACHED */
5459 * We check the LBA and length, but don't do anything with them.
5460 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5461 * get flushed. This check will just help satisfy anyone who wants
5462 * to see an error for an out of range LBA.
5464 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5465 ctl_set_lba_out_of_range(ctsio,
5466 MAX(starting_lba, lun->be_lun->maxlba + 1));
5467 ctl_done((union ctl_io *)ctsio);
5471 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5472 lbalen->lba = starting_lba;
5473 lbalen->len = block_count;
5474 lbalen->flags = byte2;
5475 retval = lun->backend->config_write((union ctl_io *)ctsio);
5482 ctl_format(struct ctl_scsiio *ctsio)
5484 struct scsi_format *cdb;
5485 int length, defect_list_len;
5487 CTL_DEBUG_PRINT(("ctl_format\n"));
5489 cdb = (struct scsi_format *)ctsio->cdb;
5492 if (cdb->byte2 & SF_FMTDATA) {
5493 if (cdb->byte2 & SF_LONGLIST)
5494 length = sizeof(struct scsi_format_header_long);
5496 length = sizeof(struct scsi_format_header_short);
5499 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5501 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5502 ctsio->kern_data_len = length;
5503 ctsio->kern_total_len = length;
5504 ctsio->kern_rel_offset = 0;
5505 ctsio->kern_sg_entries = 0;
5506 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5507 ctsio->be_move_done = ctl_config_move_done;
5508 ctl_datamove((union ctl_io *)ctsio);
5510 return (CTL_RETVAL_COMPLETE);
5513 defect_list_len = 0;
5515 if (cdb->byte2 & SF_FMTDATA) {
5516 if (cdb->byte2 & SF_LONGLIST) {
5517 struct scsi_format_header_long *header;
5519 header = (struct scsi_format_header_long *)
5520 ctsio->kern_data_ptr;
5522 defect_list_len = scsi_4btoul(header->defect_list_len);
5523 if (defect_list_len != 0) {
5524 ctl_set_invalid_field(ctsio,
5533 struct scsi_format_header_short *header;
5535 header = (struct scsi_format_header_short *)
5536 ctsio->kern_data_ptr;
5538 defect_list_len = scsi_2btoul(header->defect_list_len);
5539 if (defect_list_len != 0) {
5540 ctl_set_invalid_field(ctsio,
5551 ctl_set_success(ctsio);
5554 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5555 free(ctsio->kern_data_ptr, M_CTL);
5556 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5559 ctl_done((union ctl_io *)ctsio);
5560 return (CTL_RETVAL_COMPLETE);
5564 ctl_read_buffer(struct ctl_scsiio *ctsio)
5566 struct ctl_lun *lun = CTL_LUN(ctsio);
5567 uint64_t buffer_offset;
5570 static uint8_t descr[4];
5571 static uint8_t echo_descr[4] = { 0 };
5573 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5575 switch (ctsio->cdb[0]) {
5577 struct scsi_read_buffer *cdb;
5579 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5580 buffer_offset = scsi_3btoul(cdb->offset);
5581 len = scsi_3btoul(cdb->length);
5585 case READ_BUFFER_16: {
5586 struct scsi_read_buffer_16 *cdb;
5588 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5589 buffer_offset = scsi_8btou64(cdb->offset);
5590 len = scsi_4btoul(cdb->length);
5594 default: /* This shouldn't happen. */
5595 ctl_set_invalid_opcode(ctsio);
5596 ctl_done((union ctl_io *)ctsio);
5597 return (CTL_RETVAL_COMPLETE);
5600 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5601 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5602 ctl_set_invalid_field(ctsio,
5608 ctl_done((union ctl_io *)ctsio);
5609 return (CTL_RETVAL_COMPLETE);
5612 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5614 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5615 ctsio->kern_data_ptr = descr;
5616 len = min(len, sizeof(descr));
5617 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5618 ctsio->kern_data_ptr = echo_descr;
5619 len = min(len, sizeof(echo_descr));
5621 if (lun->write_buffer == NULL) {
5622 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5625 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5627 ctsio->kern_data_len = len;
5628 ctsio->kern_total_len = len;
5629 ctsio->kern_rel_offset = 0;
5630 ctsio->kern_sg_entries = 0;
5631 ctl_set_success(ctsio);
5632 ctsio->be_move_done = ctl_config_move_done;
5633 ctl_datamove((union ctl_io *)ctsio);
5634 return (CTL_RETVAL_COMPLETE);
5638 ctl_write_buffer(struct ctl_scsiio *ctsio)
5640 struct ctl_lun *lun = CTL_LUN(ctsio);
5641 struct scsi_write_buffer *cdb;
5642 int buffer_offset, len;
5644 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5646 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5648 len = scsi_3btoul(cdb->length);
5649 buffer_offset = scsi_3btoul(cdb->offset);
5651 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5652 ctl_set_invalid_field(ctsio,
5658 ctl_done((union ctl_io *)ctsio);
5659 return (CTL_RETVAL_COMPLETE);
5663 * If we've got a kernel request that hasn't been malloced yet,
5664 * malloc it and tell the caller the data buffer is here.
5666 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5667 if (lun->write_buffer == NULL) {
5668 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5671 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 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5677 ctsio->be_move_done = ctl_config_move_done;
5678 ctl_datamove((union ctl_io *)ctsio);
5680 return (CTL_RETVAL_COMPLETE);
5683 ctl_set_success(ctsio);
5684 ctl_done((union ctl_io *)ctsio);
5685 return (CTL_RETVAL_COMPLETE);
5689 ctl_write_same(struct ctl_scsiio *ctsio)
5691 struct ctl_lun *lun = CTL_LUN(ctsio);
5692 struct ctl_lba_len_flags *lbalen;
5694 uint32_t num_blocks;
5698 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5700 switch (ctsio->cdb[0]) {
5701 case WRITE_SAME_10: {
5702 struct scsi_write_same_10 *cdb;
5704 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5706 lba = scsi_4btoul(cdb->addr);
5707 num_blocks = scsi_2btoul(cdb->length);
5711 case WRITE_SAME_16: {
5712 struct scsi_write_same_16 *cdb;
5714 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5716 lba = scsi_8btou64(cdb->addr);
5717 num_blocks = scsi_4btoul(cdb->length);
5723 * We got a command we don't support. This shouldn't
5724 * happen, commands should be filtered out above us.
5726 ctl_set_invalid_opcode(ctsio);
5727 ctl_done((union ctl_io *)ctsio);
5729 return (CTL_RETVAL_COMPLETE);
5730 break; /* NOTREACHED */
5733 /* ANCHOR flag can be used only together with UNMAP */
5734 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5735 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5736 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5737 ctl_done((union ctl_io *)ctsio);
5738 return (CTL_RETVAL_COMPLETE);
5742 * The first check is to make sure we're in bounds, the second
5743 * check is to catch wrap-around problems. If the lba + num blocks
5744 * is less than the lba, then we've wrapped around and the block
5745 * range is invalid anyway.
5747 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5748 || ((lba + num_blocks) < lba)) {
5749 ctl_set_lba_out_of_range(ctsio,
5750 MAX(lba, lun->be_lun->maxlba + 1));
5751 ctl_done((union ctl_io *)ctsio);
5752 return (CTL_RETVAL_COMPLETE);
5755 /* Zero number of blocks means "to the last logical block" */
5756 if (num_blocks == 0) {
5757 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5758 ctl_set_invalid_field(ctsio,
5764 ctl_done((union ctl_io *)ctsio);
5765 return (CTL_RETVAL_COMPLETE);
5767 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5770 len = lun->be_lun->blocksize;
5773 * If we've got a kernel request that hasn't been malloced yet,
5774 * malloc it and tell the caller the data buffer is here.
5776 if ((byte2 & SWS_NDOB) == 0 &&
5777 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5778 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5779 ctsio->kern_data_len = len;
5780 ctsio->kern_total_len = len;
5781 ctsio->kern_rel_offset = 0;
5782 ctsio->kern_sg_entries = 0;
5783 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5784 ctsio->be_move_done = ctl_config_move_done;
5785 ctl_datamove((union ctl_io *)ctsio);
5787 return (CTL_RETVAL_COMPLETE);
5790 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5792 lbalen->len = num_blocks;
5793 lbalen->flags = byte2;
5794 retval = lun->backend->config_write((union ctl_io *)ctsio);
5800 ctl_unmap(struct ctl_scsiio *ctsio)
5802 struct ctl_lun *lun = CTL_LUN(ctsio);
5803 struct scsi_unmap *cdb;
5804 struct ctl_ptr_len_flags *ptrlen;
5805 struct scsi_unmap_header *hdr;
5806 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5808 uint32_t num_blocks;
5812 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5814 cdb = (struct scsi_unmap *)ctsio->cdb;
5815 len = scsi_2btoul(cdb->length);
5819 * If we've got a kernel request that hasn't been malloced yet,
5820 * malloc it and tell the caller the data buffer is here.
5822 if ((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 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5836 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5837 if (len < sizeof (*hdr) ||
5838 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5839 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5840 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5841 ctl_set_invalid_field(ctsio,
5849 len = scsi_2btoul(hdr->desc_length);
5850 buf = (struct scsi_unmap_desc *)(hdr + 1);
5851 end = buf + len / sizeof(*buf);
5854 for (range = buf; range < end; range++) {
5855 lba = scsi_8btou64(range->lba);
5856 num_blocks = scsi_4btoul(range->length);
5857 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5858 || ((lba + num_blocks) < lba)) {
5859 ctl_set_lba_out_of_range(ctsio,
5860 MAX(lba, lun->be_lun->maxlba + 1));
5861 ctl_done((union ctl_io *)ctsio);
5862 return (CTL_RETVAL_COMPLETE);
5864 if (num_blocks != 0)
5869 * Block backend can not handle zero last range.
5870 * Filter it out and return if there is nothing left.
5872 len = (uint8_t *)endnz - (uint8_t *)buf;
5874 ctl_set_success(ctsio);
5878 mtx_lock(&lun->lun_lock);
5879 ptrlen = (struct ctl_ptr_len_flags *)
5880 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5881 ptrlen->ptr = (void *)buf;
5883 ptrlen->flags = byte2;
5884 ctl_check_blocked(lun);
5885 mtx_unlock(&lun->lun_lock);
5887 retval = lun->backend->config_write((union ctl_io *)ctsio);
5891 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5892 free(ctsio->kern_data_ptr, M_CTL);
5893 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5895 ctl_done((union ctl_io *)ctsio);
5896 return (CTL_RETVAL_COMPLETE);
5900 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5901 struct ctl_page_index *page_index, uint8_t *page_ptr)
5903 struct ctl_lun *lun = CTL_LUN(ctsio);
5904 uint8_t *current_cp;
5908 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5911 current_cp = (page_index->page_data + (page_index->page_len *
5914 mtx_lock(&lun->lun_lock);
5915 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5916 memcpy(current_cp, page_ptr, page_index->page_len);
5920 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5921 mtx_unlock(&lun->lun_lock);
5923 ctl_isc_announce_mode(lun,
5924 ctl_get_initindex(&ctsio->io_hdr.nexus),
5925 page_index->page_code, page_index->subpage);
5927 return (CTL_RETVAL_COMPLETE);
5931 ctl_ie_timer(void *arg)
5933 struct ctl_lun *lun = arg;
5936 if (lun->ie_asc == 0)
5939 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5940 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5942 lun->ie_reported = 0;
5944 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5945 lun->ie_reportcnt++;
5946 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5947 if (t == 0 || t == UINT32_MAX)
5948 t = 3000; /* 5 min */
5949 callout_schedule(&lun->ie_callout, t * hz / 10);
5954 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5955 struct ctl_page_index *page_index, uint8_t *page_ptr)
5957 struct ctl_lun *lun = CTL_LUN(ctsio);
5958 struct scsi_info_exceptions_page *pg;
5961 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5963 pg = (struct scsi_info_exceptions_page *)page_ptr;
5964 mtx_lock(&lun->lun_lock);
5965 if (pg->info_flags & SIEP_FLAGS_TEST) {
5967 lun->ie_ascq = 0xff;
5968 if (pg->mrie == SIEP_MRIE_UA) {
5969 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5970 lun->ie_reported = 1;
5972 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5973 lun->ie_reported = -1;
5975 lun->ie_reportcnt = 1;
5976 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5977 lun->ie_reportcnt++;
5978 t = scsi_4btoul(pg->interval_timer);
5979 if (t == 0 || t == UINT32_MAX)
5980 t = 3000; /* 5 min */
5981 callout_reset(&lun->ie_callout, t * hz / 10,
5987 lun->ie_reported = 1;
5988 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5989 lun->ie_reportcnt = UINT32_MAX;
5990 callout_stop(&lun->ie_callout);
5992 mtx_unlock(&lun->lun_lock);
5993 return (CTL_RETVAL_COMPLETE);
5997 ctl_do_mode_select(union ctl_io *io)
5999 struct ctl_lun *lun = CTL_LUN(io);
6000 struct scsi_mode_page_header *page_header;
6001 struct ctl_page_index *page_index;
6002 struct ctl_scsiio *ctsio;
6003 int page_len, page_len_offset, page_len_size;
6004 union ctl_modepage_info *modepage_info;
6005 uint16_t *len_left, *len_used;
6008 ctsio = &io->scsiio;
6012 modepage_info = (union ctl_modepage_info *)
6013 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6014 len_left = &modepage_info->header.len_left;
6015 len_used = &modepage_info->header.len_used;
6019 page_header = (struct scsi_mode_page_header *)
6020 (ctsio->kern_data_ptr + *len_used);
6022 if (*len_left == 0) {
6023 free(ctsio->kern_data_ptr, M_CTL);
6024 ctl_set_success(ctsio);
6025 ctl_done((union ctl_io *)ctsio);
6026 return (CTL_RETVAL_COMPLETE);
6027 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6029 free(ctsio->kern_data_ptr, M_CTL);
6030 ctl_set_param_len_error(ctsio);
6031 ctl_done((union ctl_io *)ctsio);
6032 return (CTL_RETVAL_COMPLETE);
6034 } else if ((page_header->page_code & SMPH_SPF)
6035 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6037 free(ctsio->kern_data_ptr, M_CTL);
6038 ctl_set_param_len_error(ctsio);
6039 ctl_done((union ctl_io *)ctsio);
6040 return (CTL_RETVAL_COMPLETE);
6045 * XXX KDM should we do something with the block descriptor?
6047 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6048 page_index = &lun->mode_pages.index[i];
6049 if (lun->be_lun->lun_type == T_DIRECT &&
6050 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6052 if (lun->be_lun->lun_type == T_PROCESSOR &&
6053 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6055 if (lun->be_lun->lun_type == T_CDROM &&
6056 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6059 if ((page_index->page_code & SMPH_PC_MASK) !=
6060 (page_header->page_code & SMPH_PC_MASK))
6064 * If neither page has a subpage code, then we've got a
6067 if (((page_index->page_code & SMPH_SPF) == 0)
6068 && ((page_header->page_code & SMPH_SPF) == 0)) {
6069 page_len = page_header->page_length;
6074 * If both pages have subpages, then the subpage numbers
6077 if ((page_index->page_code & SMPH_SPF)
6078 && (page_header->page_code & SMPH_SPF)) {
6079 struct scsi_mode_page_header_sp *sph;
6081 sph = (struct scsi_mode_page_header_sp *)page_header;
6082 if (page_index->subpage == sph->subpage) {
6083 page_len = scsi_2btoul(sph->page_length);
6090 * If we couldn't find the page, or if we don't have a mode select
6091 * handler for it, send back an error to the user.
6093 if ((i >= CTL_NUM_MODE_PAGES)
6094 || (page_index->select_handler == NULL)) {
6095 ctl_set_invalid_field(ctsio,
6098 /*field*/ *len_used,
6101 free(ctsio->kern_data_ptr, M_CTL);
6102 ctl_done((union ctl_io *)ctsio);
6103 return (CTL_RETVAL_COMPLETE);
6106 if (page_index->page_code & SMPH_SPF) {
6107 page_len_offset = 2;
6111 page_len_offset = 1;
6115 * If the length the initiator gives us isn't the one we specify in
6116 * the mode page header, or if they didn't specify enough data in
6117 * the CDB to avoid truncating this page, kick out the request.
6119 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6120 ctl_set_invalid_field(ctsio,
6123 /*field*/ *len_used + page_len_offset,
6126 free(ctsio->kern_data_ptr, M_CTL);
6127 ctl_done((union ctl_io *)ctsio);
6128 return (CTL_RETVAL_COMPLETE);
6130 if (*len_left < page_index->page_len) {
6131 free(ctsio->kern_data_ptr, M_CTL);
6132 ctl_set_param_len_error(ctsio);
6133 ctl_done((union ctl_io *)ctsio);
6134 return (CTL_RETVAL_COMPLETE);
6138 * Run through the mode page, checking to make sure that the bits
6139 * the user changed are actually legal for him to change.
6141 for (i = 0; i < page_index->page_len; i++) {
6142 uint8_t *user_byte, *change_mask, *current_byte;
6146 user_byte = (uint8_t *)page_header + i;
6147 change_mask = page_index->page_data +
6148 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6149 current_byte = page_index->page_data +
6150 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6153 * Check to see whether the user set any bits in this byte
6154 * that he is not allowed to set.
6156 if ((*user_byte & ~(*change_mask)) ==
6157 (*current_byte & ~(*change_mask)))
6161 * Go through bit by bit to determine which one is illegal.
6164 for (j = 7; j >= 0; j--) {
6165 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6166 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6171 ctl_set_invalid_field(ctsio,
6174 /*field*/ *len_used + i,
6177 free(ctsio->kern_data_ptr, M_CTL);
6178 ctl_done((union ctl_io *)ctsio);
6179 return (CTL_RETVAL_COMPLETE);
6183 * Decrement these before we call the page handler, since we may
6184 * end up getting called back one way or another before the handler
6185 * returns to this context.
6187 *len_left -= page_index->page_len;
6188 *len_used += page_index->page_len;
6190 retval = page_index->select_handler(ctsio, page_index,
6191 (uint8_t *)page_header);
6194 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6195 * wait until this queued command completes to finish processing
6196 * the mode page. If it returns anything other than
6197 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6198 * already set the sense information, freed the data pointer, and
6199 * completed the io for us.
6201 if (retval != CTL_RETVAL_COMPLETE)
6202 goto bailout_no_done;
6205 * If the initiator sent us more than one page, parse the next one.
6210 ctl_set_success(ctsio);
6211 free(ctsio->kern_data_ptr, M_CTL);
6212 ctl_done((union ctl_io *)ctsio);
6216 return (CTL_RETVAL_COMPLETE);
6221 ctl_mode_select(struct ctl_scsiio *ctsio)
6223 struct ctl_lun *lun = CTL_LUN(ctsio);
6224 union ctl_modepage_info *modepage_info;
6225 int bd_len, i, header_size, param_len, rtd;
6228 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6229 switch (ctsio->cdb[0]) {
6230 case MODE_SELECT_6: {
6231 struct scsi_mode_select_6 *cdb;
6233 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6235 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6236 param_len = cdb->length;
6237 header_size = sizeof(struct scsi_mode_header_6);
6240 case MODE_SELECT_10: {
6241 struct scsi_mode_select_10 *cdb;
6243 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6245 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6246 param_len = scsi_2btoul(cdb->length);
6247 header_size = sizeof(struct scsi_mode_header_10);
6251 ctl_set_invalid_opcode(ctsio);
6252 ctl_done((union ctl_io *)ctsio);
6253 return (CTL_RETVAL_COMPLETE);
6257 if (param_len != 0) {
6258 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6259 /*command*/ 1, /*field*/ 0,
6260 /*bit_valid*/ 0, /*bit*/ 0);
6261 ctl_done((union ctl_io *)ctsio);
6262 return (CTL_RETVAL_COMPLETE);
6265 /* Revert to defaults. */
6266 ctl_init_page_index(lun);
6267 mtx_lock(&lun->lun_lock);
6268 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6269 mtx_unlock(&lun->lun_lock);
6270 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6271 ctl_isc_announce_mode(lun, -1,
6272 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6273 lun->mode_pages.index[i].subpage);
6275 ctl_set_success(ctsio);
6276 ctl_done((union ctl_io *)ctsio);
6277 return (CTL_RETVAL_COMPLETE);
6282 * "A parameter list length of zero indicates that the Data-Out Buffer
6283 * shall be empty. This condition shall not be considered as an error."
6285 if (param_len == 0) {
6286 ctl_set_success(ctsio);
6287 ctl_done((union ctl_io *)ctsio);
6288 return (CTL_RETVAL_COMPLETE);
6292 * Since we'll hit this the first time through, prior to
6293 * allocation, we don't need to free a data buffer here.
6295 if (param_len < header_size) {
6296 ctl_set_param_len_error(ctsio);
6297 ctl_done((union ctl_io *)ctsio);
6298 return (CTL_RETVAL_COMPLETE);
6302 * Allocate the data buffer and grab the user's data. In theory,
6303 * we shouldn't have to sanity check the parameter list length here
6304 * because the maximum size is 64K. We should be able to malloc
6305 * that much without too many problems.
6307 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6308 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6309 ctsio->kern_data_len = param_len;
6310 ctsio->kern_total_len = param_len;
6311 ctsio->kern_rel_offset = 0;
6312 ctsio->kern_sg_entries = 0;
6313 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6314 ctsio->be_move_done = ctl_config_move_done;
6315 ctl_datamove((union ctl_io *)ctsio);
6317 return (CTL_RETVAL_COMPLETE);
6320 switch (ctsio->cdb[0]) {
6321 case MODE_SELECT_6: {
6322 struct scsi_mode_header_6 *mh6;
6324 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6325 bd_len = mh6->blk_desc_len;
6328 case MODE_SELECT_10: {
6329 struct scsi_mode_header_10 *mh10;
6331 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6332 bd_len = scsi_2btoul(mh10->blk_desc_len);
6336 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6339 if (param_len < (header_size + bd_len)) {
6340 free(ctsio->kern_data_ptr, M_CTL);
6341 ctl_set_param_len_error(ctsio);
6342 ctl_done((union ctl_io *)ctsio);
6343 return (CTL_RETVAL_COMPLETE);
6347 * Set the IO_CONT flag, so that if this I/O gets passed to
6348 * ctl_config_write_done(), it'll get passed back to
6349 * ctl_do_mode_select() for further processing, or completion if
6352 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6353 ctsio->io_cont = ctl_do_mode_select;
6355 modepage_info = (union ctl_modepage_info *)
6356 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6357 memset(modepage_info, 0, sizeof(*modepage_info));
6358 modepage_info->header.len_left = param_len - header_size - bd_len;
6359 modepage_info->header.len_used = header_size + bd_len;
6361 return (ctl_do_mode_select((union ctl_io *)ctsio));
6365 ctl_mode_sense(struct ctl_scsiio *ctsio)
6367 struct ctl_lun *lun = CTL_LUN(ctsio);
6368 int pc, page_code, dbd, subpage;
6369 int alloc_len, page_len, header_len, total_len;
6370 struct scsi_mode_block_descr *block_desc;
6371 struct ctl_page_index *page_index;
6376 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6378 switch (ctsio->cdb[0]) {
6379 case MODE_SENSE_6: {
6380 struct scsi_mode_sense_6 *cdb;
6382 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6384 header_len = sizeof(struct scsi_mode_hdr_6);
6385 if (cdb->byte2 & SMS_DBD)
6388 header_len += sizeof(struct scsi_mode_block_descr);
6390 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6391 page_code = cdb->page & SMS_PAGE_CODE;
6392 subpage = cdb->subpage;
6393 alloc_len = cdb->length;
6396 case MODE_SENSE_10: {
6397 struct scsi_mode_sense_10 *cdb;
6399 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6401 header_len = sizeof(struct scsi_mode_hdr_10);
6403 if (cdb->byte2 & SMS_DBD)
6406 header_len += sizeof(struct scsi_mode_block_descr);
6407 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6408 page_code = cdb->page & SMS_PAGE_CODE;
6409 subpage = cdb->subpage;
6410 alloc_len = scsi_2btoul(cdb->length);
6414 ctl_set_invalid_opcode(ctsio);
6415 ctl_done((union ctl_io *)ctsio);
6416 return (CTL_RETVAL_COMPLETE);
6417 break; /* NOTREACHED */
6421 * We have to make a first pass through to calculate the size of
6422 * the pages that match the user's query. Then we allocate enough
6423 * memory to hold it, and actually copy the data into the buffer.
6425 switch (page_code) {
6426 case SMS_ALL_PAGES_PAGE: {
6432 * At the moment, values other than 0 and 0xff here are
6433 * reserved according to SPC-3.
6435 if ((subpage != SMS_SUBPAGE_PAGE_0)
6436 && (subpage != SMS_SUBPAGE_ALL)) {
6437 ctl_set_invalid_field(ctsio,
6443 ctl_done((union ctl_io *)ctsio);
6444 return (CTL_RETVAL_COMPLETE);
6447 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6448 page_index = &lun->mode_pages.index[i];
6450 /* Make sure the page is supported for this dev type */
6451 if (lun->be_lun->lun_type == T_DIRECT &&
6452 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6454 if (lun->be_lun->lun_type == T_PROCESSOR &&
6455 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6457 if (lun->be_lun->lun_type == T_CDROM &&
6458 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6462 * We don't use this subpage if the user didn't
6463 * request all subpages.
6465 if ((page_index->subpage != 0)
6466 && (subpage == SMS_SUBPAGE_PAGE_0))
6470 printf("found page %#x len %d\n",
6471 page_index->page_code & SMPH_PC_MASK,
6472 page_index->page_len);
6474 page_len += page_index->page_len;
6483 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6484 page_index = &lun->mode_pages.index[i];
6486 /* Make sure the page is supported for this dev type */
6487 if (lun->be_lun->lun_type == T_DIRECT &&
6488 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6490 if (lun->be_lun->lun_type == T_PROCESSOR &&
6491 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6493 if (lun->be_lun->lun_type == T_CDROM &&
6494 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6497 /* Look for the right page code */
6498 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6501 /* Look for the right subpage or the subpage wildcard*/
6502 if ((page_index->subpage != subpage)
6503 && (subpage != SMS_SUBPAGE_ALL))
6507 printf("found page %#x len %d\n",
6508 page_index->page_code & SMPH_PC_MASK,
6509 page_index->page_len);
6512 page_len += page_index->page_len;
6515 if (page_len == 0) {
6516 ctl_set_invalid_field(ctsio,
6522 ctl_done((union ctl_io *)ctsio);
6523 return (CTL_RETVAL_COMPLETE);
6529 total_len = header_len + page_len;
6531 printf("header_len = %d, page_len = %d, total_len = %d\n",
6532 header_len, page_len, total_len);
6535 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6536 ctsio->kern_sg_entries = 0;
6537 ctsio->kern_rel_offset = 0;
6538 ctsio->kern_data_len = min(total_len, alloc_len);
6539 ctsio->kern_total_len = ctsio->kern_data_len;
6541 switch (ctsio->cdb[0]) {
6542 case MODE_SENSE_6: {
6543 struct scsi_mode_hdr_6 *header;
6545 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6547 header->datalen = MIN(total_len - 1, 254);
6548 if (lun->be_lun->lun_type == T_DIRECT) {
6549 header->dev_specific = 0x10; /* DPOFUA */
6550 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6551 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6552 header->dev_specific |= 0x80; /* WP */
6555 header->block_descr_len = 0;
6557 header->block_descr_len =
6558 sizeof(struct scsi_mode_block_descr);
6559 block_desc = (struct scsi_mode_block_descr *)&header[1];
6562 case MODE_SENSE_10: {
6563 struct scsi_mode_hdr_10 *header;
6566 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6568 datalen = MIN(total_len - 2, 65533);
6569 scsi_ulto2b(datalen, header->datalen);
6570 if (lun->be_lun->lun_type == T_DIRECT) {
6571 header->dev_specific = 0x10; /* DPOFUA */
6572 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6573 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6574 header->dev_specific |= 0x80; /* WP */
6577 scsi_ulto2b(0, header->block_descr_len);
6579 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6580 header->block_descr_len);
6581 block_desc = (struct scsi_mode_block_descr *)&header[1];
6585 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6589 * If we've got a disk, use its blocksize in the block
6590 * descriptor. Otherwise, just set it to 0.
6593 if (lun->be_lun->lun_type == T_DIRECT)
6594 scsi_ulto3b(lun->be_lun->blocksize,
6595 block_desc->block_len);
6597 scsi_ulto3b(0, block_desc->block_len);
6600 switch (page_code) {
6601 case SMS_ALL_PAGES_PAGE: {
6604 data_used = header_len;
6605 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6606 struct ctl_page_index *page_index;
6608 page_index = &lun->mode_pages.index[i];
6609 if (lun->be_lun->lun_type == T_DIRECT &&
6610 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6612 if (lun->be_lun->lun_type == T_PROCESSOR &&
6613 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6615 if (lun->be_lun->lun_type == T_CDROM &&
6616 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6620 * We don't use this subpage if the user didn't
6621 * request all subpages. We already checked (above)
6622 * to make sure the user only specified a subpage
6623 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6625 if ((page_index->subpage != 0)
6626 && (subpage == SMS_SUBPAGE_PAGE_0))
6630 * Call the handler, if it exists, to update the
6631 * page to the latest values.
6633 if (page_index->sense_handler != NULL)
6634 page_index->sense_handler(ctsio, page_index,pc);
6636 memcpy(ctsio->kern_data_ptr + data_used,
6637 page_index->page_data +
6638 (page_index->page_len * pc),
6639 page_index->page_len);
6640 data_used += page_index->page_len;
6647 data_used = header_len;
6649 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6650 struct ctl_page_index *page_index;
6652 page_index = &lun->mode_pages.index[i];
6654 /* Look for the right page code */
6655 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6658 /* Look for the right subpage or the subpage wildcard*/
6659 if ((page_index->subpage != subpage)
6660 && (subpage != SMS_SUBPAGE_ALL))
6663 /* Make sure the page is supported for this dev type */
6664 if (lun->be_lun->lun_type == T_DIRECT &&
6665 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6667 if (lun->be_lun->lun_type == T_PROCESSOR &&
6668 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6670 if (lun->be_lun->lun_type == T_CDROM &&
6671 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 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;
6691 ctl_set_success(ctsio);
6692 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6693 ctsio->be_move_done = ctl_config_move_done;
6694 ctl_datamove((union ctl_io *)ctsio);
6695 return (CTL_RETVAL_COMPLETE);
6699 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6700 struct ctl_page_index *page_index,
6703 struct ctl_lun *lun = CTL_LUN(ctsio);
6704 struct scsi_log_param_header *phdr;
6708 data = page_index->page_data;
6710 if (lun->backend->lun_attr != NULL &&
6711 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6713 phdr = (struct scsi_log_param_header *)data;
6714 scsi_ulto2b(0x0001, phdr->param_code);
6715 phdr->param_control = SLP_LBIN | SLP_LP;
6716 phdr->param_len = 8;
6717 data = (uint8_t *)(phdr + 1);
6718 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6719 data[4] = 0x02; /* per-pool */
6720 data += phdr->param_len;
6723 if (lun->backend->lun_attr != NULL &&
6724 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6726 phdr = (struct scsi_log_param_header *)data;
6727 scsi_ulto2b(0x0002, phdr->param_code);
6728 phdr->param_control = SLP_LBIN | SLP_LP;
6729 phdr->param_len = 8;
6730 data = (uint8_t *)(phdr + 1);
6731 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6732 data[4] = 0x01; /* per-LUN */
6733 data += phdr->param_len;
6736 if (lun->backend->lun_attr != NULL &&
6737 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6739 phdr = (struct scsi_log_param_header *)data;
6740 scsi_ulto2b(0x00f1, phdr->param_code);
6741 phdr->param_control = SLP_LBIN | SLP_LP;
6742 phdr->param_len = 8;
6743 data = (uint8_t *)(phdr + 1);
6744 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6745 data[4] = 0x02; /* per-pool */
6746 data += phdr->param_len;
6749 if (lun->backend->lun_attr != NULL &&
6750 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6752 phdr = (struct scsi_log_param_header *)data;
6753 scsi_ulto2b(0x00f2, phdr->param_code);
6754 phdr->param_control = SLP_LBIN | SLP_LP;
6755 phdr->param_len = 8;
6756 data = (uint8_t *)(phdr + 1);
6757 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6758 data[4] = 0x02; /* per-pool */
6759 data += phdr->param_len;
6762 page_index->page_len = data - page_index->page_data;
6767 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6768 struct ctl_page_index *page_index,
6771 struct ctl_lun *lun = CTL_LUN(ctsio);
6772 struct stat_page *data;
6775 data = (struct stat_page *)page_index->page_data;
6777 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6778 data->sap.hdr.param_control = SLP_LBIN;
6779 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6780 sizeof(struct scsi_log_param_header);
6781 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6782 data->sap.read_num);
6783 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6784 data->sap.write_num);
6785 if (lun->be_lun->blocksize > 0) {
6786 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6787 lun->be_lun->blocksize, data->sap.recvieved_lba);
6788 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6789 lun->be_lun->blocksize, data->sap.transmitted_lba);
6791 t = &lun->stats.time[CTL_STATS_READ];
6792 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6793 data->sap.read_int);
6794 t = &lun->stats.time[CTL_STATS_WRITE];
6795 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6796 data->sap.write_int);
6797 scsi_u64to8b(0, data->sap.weighted_num);
6798 scsi_u64to8b(0, data->sap.weighted_int);
6799 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6800 data->it.hdr.param_control = SLP_LBIN;
6801 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6802 sizeof(struct scsi_log_param_header);
6804 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6806 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6807 data->it.hdr.param_control = SLP_LBIN;
6808 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6809 sizeof(struct scsi_log_param_header);
6810 scsi_ulto4b(3, data->ti.exponent);
6811 scsi_ulto4b(1, data->ti.integer);
6816 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6817 struct ctl_page_index *page_index,
6820 struct ctl_lun *lun = CTL_LUN(ctsio);
6821 struct scsi_log_informational_exceptions *data;
6823 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6825 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6826 data->hdr.param_control = SLP_LBIN;
6827 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6828 sizeof(struct scsi_log_param_header);
6829 data->ie_asc = lun->ie_asc;
6830 data->ie_ascq = lun->ie_ascq;
6831 data->temperature = 0xff;
6836 ctl_log_sense(struct ctl_scsiio *ctsio)
6838 struct ctl_lun *lun = CTL_LUN(ctsio);
6839 int i, pc, page_code, subpage;
6840 int alloc_len, total_len;
6841 struct ctl_page_index *page_index;
6842 struct scsi_log_sense *cdb;
6843 struct scsi_log_header *header;
6845 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6847 cdb = (struct scsi_log_sense *)ctsio->cdb;
6848 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6849 page_code = cdb->page & SLS_PAGE_CODE;
6850 subpage = cdb->subpage;
6851 alloc_len = scsi_2btoul(cdb->length);
6854 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6855 page_index = &lun->log_pages.index[i];
6857 /* Look for the right page code */
6858 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6861 /* Look for the right subpage or the subpage wildcard*/
6862 if (page_index->subpage != subpage)
6867 if (i >= CTL_NUM_LOG_PAGES) {
6868 ctl_set_invalid_field(ctsio,
6874 ctl_done((union ctl_io *)ctsio);
6875 return (CTL_RETVAL_COMPLETE);
6878 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6880 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6881 ctsio->kern_sg_entries = 0;
6882 ctsio->kern_rel_offset = 0;
6883 ctsio->kern_data_len = min(total_len, alloc_len);
6884 ctsio->kern_total_len = ctsio->kern_data_len;
6886 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6887 header->page = page_index->page_code;
6888 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6889 header->page |= SL_DS;
6890 if (page_index->subpage) {
6891 header->page |= SL_SPF;
6892 header->subpage = page_index->subpage;
6894 scsi_ulto2b(page_index->page_len, header->datalen);
6897 * Call the handler, if it exists, to update the
6898 * page to the latest values.
6900 if (page_index->sense_handler != NULL)
6901 page_index->sense_handler(ctsio, page_index, pc);
6903 memcpy(header + 1, page_index->page_data, page_index->page_len);
6905 ctl_set_success(ctsio);
6906 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6907 ctsio->be_move_done = ctl_config_move_done;
6908 ctl_datamove((union ctl_io *)ctsio);
6909 return (CTL_RETVAL_COMPLETE);
6913 ctl_read_capacity(struct ctl_scsiio *ctsio)
6915 struct ctl_lun *lun = CTL_LUN(ctsio);
6916 struct scsi_read_capacity *cdb;
6917 struct scsi_read_capacity_data *data;
6920 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6922 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6924 lba = scsi_4btoul(cdb->addr);
6925 if (((cdb->pmi & SRC_PMI) == 0)
6927 ctl_set_invalid_field(/*ctsio*/ ctsio,
6933 ctl_done((union ctl_io *)ctsio);
6934 return (CTL_RETVAL_COMPLETE);
6937 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6938 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6939 ctsio->kern_data_len = sizeof(*data);
6940 ctsio->kern_total_len = sizeof(*data);
6941 ctsio->kern_rel_offset = 0;
6942 ctsio->kern_sg_entries = 0;
6945 * If the maximum LBA is greater than 0xfffffffe, the user must
6946 * issue a SERVICE ACTION IN (16) command, with the read capacity
6947 * serivce action set.
6949 if (lun->be_lun->maxlba > 0xfffffffe)
6950 scsi_ulto4b(0xffffffff, data->addr);
6952 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6955 * XXX KDM this may not be 512 bytes...
6957 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6959 ctl_set_success(ctsio);
6960 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6961 ctsio->be_move_done = ctl_config_move_done;
6962 ctl_datamove((union ctl_io *)ctsio);
6963 return (CTL_RETVAL_COMPLETE);
6967 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6969 struct ctl_lun *lun = CTL_LUN(ctsio);
6970 struct scsi_read_capacity_16 *cdb;
6971 struct scsi_read_capacity_data_long *data;
6975 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6977 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6979 alloc_len = scsi_4btoul(cdb->alloc_len);
6980 lba = scsi_8btou64(cdb->addr);
6982 if ((cdb->reladr & SRC16_PMI)
6984 ctl_set_invalid_field(/*ctsio*/ ctsio,
6990 ctl_done((union ctl_io *)ctsio);
6991 return (CTL_RETVAL_COMPLETE);
6994 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6995 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6996 ctsio->kern_rel_offset = 0;
6997 ctsio->kern_sg_entries = 0;
6998 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
6999 ctsio->kern_total_len = ctsio->kern_data_len;
7001 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7002 /* XXX KDM this may not be 512 bytes... */
7003 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7004 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7005 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7006 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7007 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7009 ctl_set_success(ctsio);
7010 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7011 ctsio->be_move_done = ctl_config_move_done;
7012 ctl_datamove((union ctl_io *)ctsio);
7013 return (CTL_RETVAL_COMPLETE);
7017 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7019 struct ctl_lun *lun = CTL_LUN(ctsio);
7020 struct scsi_get_lba_status *cdb;
7021 struct scsi_get_lba_status_data *data;
7022 struct ctl_lba_len_flags *lbalen;
7024 uint32_t alloc_len, total_len;
7027 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7029 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7030 lba = scsi_8btou64(cdb->addr);
7031 alloc_len = scsi_4btoul(cdb->alloc_len);
7033 if (lba > lun->be_lun->maxlba) {
7034 ctl_set_lba_out_of_range(ctsio, lba);
7035 ctl_done((union ctl_io *)ctsio);
7036 return (CTL_RETVAL_COMPLETE);
7039 total_len = sizeof(*data) + sizeof(data->descr[0]);
7040 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7041 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7042 ctsio->kern_rel_offset = 0;
7043 ctsio->kern_sg_entries = 0;
7044 ctsio->kern_data_len = min(total_len, alloc_len);
7045 ctsio->kern_total_len = ctsio->kern_data_len;
7047 /* Fill dummy data in case backend can't tell anything. */
7048 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7049 scsi_u64to8b(lba, data->descr[0].addr);
7050 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7051 data->descr[0].length);
7052 data->descr[0].status = 0; /* Mapped or unknown. */
7054 ctl_set_success(ctsio);
7055 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7056 ctsio->be_move_done = ctl_config_move_done;
7058 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7060 lbalen->len = total_len;
7062 retval = lun->backend->config_read((union ctl_io *)ctsio);
7067 ctl_read_defect(struct ctl_scsiio *ctsio)
7069 struct scsi_read_defect_data_10 *ccb10;
7070 struct scsi_read_defect_data_12 *ccb12;
7071 struct scsi_read_defect_data_hdr_10 *data10;
7072 struct scsi_read_defect_data_hdr_12 *data12;
7073 uint32_t alloc_len, data_len;
7076 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7078 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7079 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7080 format = ccb10->format;
7081 alloc_len = scsi_2btoul(ccb10->alloc_length);
7082 data_len = sizeof(*data10);
7084 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7085 format = ccb12->format;
7086 alloc_len = scsi_4btoul(ccb12->alloc_length);
7087 data_len = sizeof(*data12);
7089 if (alloc_len == 0) {
7090 ctl_set_success(ctsio);
7091 ctl_done((union ctl_io *)ctsio);
7092 return (CTL_RETVAL_COMPLETE);
7095 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7096 ctsio->kern_rel_offset = 0;
7097 ctsio->kern_sg_entries = 0;
7098 ctsio->kern_data_len = min(data_len, alloc_len);
7099 ctsio->kern_total_len = ctsio->kern_data_len;
7101 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7102 data10 = (struct scsi_read_defect_data_hdr_10 *)
7103 ctsio->kern_data_ptr;
7104 data10->format = format;
7105 scsi_ulto2b(0, data10->length);
7107 data12 = (struct scsi_read_defect_data_hdr_12 *)
7108 ctsio->kern_data_ptr;
7109 data12->format = format;
7110 scsi_ulto2b(0, data12->generation);
7111 scsi_ulto4b(0, data12->length);
7114 ctl_set_success(ctsio);
7115 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7116 ctsio->be_move_done = ctl_config_move_done;
7117 ctl_datamove((union ctl_io *)ctsio);
7118 return (CTL_RETVAL_COMPLETE);
7122 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7124 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7125 struct ctl_lun *lun = CTL_LUN(ctsio);
7126 struct scsi_maintenance_in *cdb;
7128 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7129 int num_ha_groups, num_target_ports, shared_group;
7130 struct ctl_port *port;
7131 struct scsi_target_group_data *rtg_ptr;
7132 struct scsi_target_group_data_extended *rtg_ext_ptr;
7133 struct scsi_target_port_group_descriptor *tpg_desc;
7135 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7137 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7138 retval = CTL_RETVAL_COMPLETE;
7140 switch (cdb->byte2 & STG_PDF_MASK) {
7141 case STG_PDF_LENGTH:
7144 case STG_PDF_EXTENDED:
7148 ctl_set_invalid_field(/*ctsio*/ ctsio,
7154 ctl_done((union ctl_io *)ctsio);
7158 num_target_ports = 0;
7159 shared_group = (softc->is_single != 0);
7160 mtx_lock(&softc->ctl_lock);
7161 STAILQ_FOREACH(port, &softc->port_list, links) {
7162 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7164 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7167 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7170 mtx_unlock(&softc->ctl_lock);
7171 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7174 total_len = sizeof(struct scsi_target_group_data_extended);
7176 total_len = sizeof(struct scsi_target_group_data);
7177 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7178 (shared_group + num_ha_groups) +
7179 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7181 alloc_len = scsi_4btoul(cdb->length);
7183 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7184 ctsio->kern_sg_entries = 0;
7185 ctsio->kern_rel_offset = 0;
7186 ctsio->kern_data_len = min(total_len, alloc_len);
7187 ctsio->kern_total_len = ctsio->kern_data_len;
7190 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7191 ctsio->kern_data_ptr;
7192 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7193 rtg_ext_ptr->format_type = 0x10;
7194 rtg_ext_ptr->implicit_transition_time = 0;
7195 tpg_desc = &rtg_ext_ptr->groups[0];
7197 rtg_ptr = (struct scsi_target_group_data *)
7198 ctsio->kern_data_ptr;
7199 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7200 tpg_desc = &rtg_ptr->groups[0];
7203 mtx_lock(&softc->ctl_lock);
7204 pg = softc->port_min / softc->port_cnt;
7205 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7206 /* Some shelf is known to be primary. */
7207 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7208 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7209 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7210 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7211 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7212 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7214 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7215 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7216 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7219 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7222 /* No known primary shelf. */
7223 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7224 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7225 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7226 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7227 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7228 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7230 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7234 tpg_desc->pref_state = ts;
7235 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7236 TPG_U_SUP | TPG_T_SUP;
7237 scsi_ulto2b(1, tpg_desc->target_port_group);
7238 tpg_desc->status = TPG_IMPLICIT;
7240 STAILQ_FOREACH(port, &softc->port_list, links) {
7241 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7243 if (!softc->is_single &&
7244 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7246 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7248 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7249 relative_target_port_identifier);
7252 tpg_desc->target_port_count = pc;
7253 tpg_desc = (struct scsi_target_port_group_descriptor *)
7254 &tpg_desc->descriptors[pc];
7256 for (g = 0; g < num_ha_groups; g++) {
7257 tpg_desc->pref_state = (g == pg) ? ts : os;
7258 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7259 TPG_U_SUP | TPG_T_SUP;
7260 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7261 tpg_desc->status = TPG_IMPLICIT;
7263 STAILQ_FOREACH(port, &softc->port_list, links) {
7264 if (port->targ_port < g * softc->port_cnt ||
7265 port->targ_port >= (g + 1) * softc->port_cnt)
7267 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7269 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7271 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7273 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7274 relative_target_port_identifier);
7277 tpg_desc->target_port_count = pc;
7278 tpg_desc = (struct scsi_target_port_group_descriptor *)
7279 &tpg_desc->descriptors[pc];
7281 mtx_unlock(&softc->ctl_lock);
7283 ctl_set_success(ctsio);
7284 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7285 ctsio->be_move_done = ctl_config_move_done;
7286 ctl_datamove((union ctl_io *)ctsio);
7291 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7293 struct ctl_lun *lun = CTL_LUN(ctsio);
7294 struct scsi_report_supported_opcodes *cdb;
7295 const struct ctl_cmd_entry *entry, *sentry;
7296 struct scsi_report_supported_opcodes_all *all;
7297 struct scsi_report_supported_opcodes_descr *descr;
7298 struct scsi_report_supported_opcodes_one *one;
7300 int alloc_len, total_len;
7301 int opcode, service_action, i, j, num;
7303 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7305 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7306 retval = CTL_RETVAL_COMPLETE;
7308 opcode = cdb->requested_opcode;
7309 service_action = scsi_2btoul(cdb->requested_service_action);
7310 switch (cdb->options & RSO_OPTIONS_MASK) {
7311 case RSO_OPTIONS_ALL:
7313 for (i = 0; i < 256; i++) {
7314 entry = &ctl_cmd_table[i];
7315 if (entry->flags & CTL_CMD_FLAG_SA5) {
7316 for (j = 0; j < 32; j++) {
7317 sentry = &((const struct ctl_cmd_entry *)
7319 if (ctl_cmd_applicable(
7320 lun->be_lun->lun_type, sentry))
7324 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7329 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7330 num * sizeof(struct scsi_report_supported_opcodes_descr);
7332 case RSO_OPTIONS_OC:
7333 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7334 ctl_set_invalid_field(/*ctsio*/ ctsio,
7340 ctl_done((union ctl_io *)ctsio);
7341 return (CTL_RETVAL_COMPLETE);
7343 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7345 case RSO_OPTIONS_OC_SA:
7346 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7347 service_action >= 32) {
7348 ctl_set_invalid_field(/*ctsio*/ ctsio,
7354 ctl_done((union ctl_io *)ctsio);
7355 return (CTL_RETVAL_COMPLETE);
7358 case RSO_OPTIONS_OC_ASA:
7359 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7362 ctl_set_invalid_field(/*ctsio*/ ctsio,
7368 ctl_done((union ctl_io *)ctsio);
7369 return (CTL_RETVAL_COMPLETE);
7372 alloc_len = scsi_4btoul(cdb->length);
7374 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7375 ctsio->kern_sg_entries = 0;
7376 ctsio->kern_rel_offset = 0;
7377 ctsio->kern_data_len = min(total_len, alloc_len);
7378 ctsio->kern_total_len = ctsio->kern_data_len;
7380 switch (cdb->options & RSO_OPTIONS_MASK) {
7381 case RSO_OPTIONS_ALL:
7382 all = (struct scsi_report_supported_opcodes_all *)
7383 ctsio->kern_data_ptr;
7385 for (i = 0; i < 256; i++) {
7386 entry = &ctl_cmd_table[i];
7387 if (entry->flags & CTL_CMD_FLAG_SA5) {
7388 for (j = 0; j < 32; j++) {
7389 sentry = &((const struct ctl_cmd_entry *)
7391 if (!ctl_cmd_applicable(
7392 lun->be_lun->lun_type, sentry))
7394 descr = &all->descr[num++];
7396 scsi_ulto2b(j, descr->service_action);
7397 descr->flags = RSO_SERVACTV;
7398 scsi_ulto2b(sentry->length,
7402 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7405 descr = &all->descr[num++];
7407 scsi_ulto2b(0, descr->service_action);
7409 scsi_ulto2b(entry->length, descr->cdb_length);
7413 num * sizeof(struct scsi_report_supported_opcodes_descr),
7416 case RSO_OPTIONS_OC:
7417 one = (struct scsi_report_supported_opcodes_one *)
7418 ctsio->kern_data_ptr;
7419 entry = &ctl_cmd_table[opcode];
7421 case RSO_OPTIONS_OC_SA:
7422 one = (struct scsi_report_supported_opcodes_one *)
7423 ctsio->kern_data_ptr;
7424 entry = &ctl_cmd_table[opcode];
7425 entry = &((const struct ctl_cmd_entry *)
7426 entry->execute)[service_action];
7428 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7430 scsi_ulto2b(entry->length, one->cdb_length);
7431 one->cdb_usage[0] = opcode;
7432 memcpy(&one->cdb_usage[1], entry->usage,
7437 case RSO_OPTIONS_OC_ASA:
7438 one = (struct scsi_report_supported_opcodes_one *)
7439 ctsio->kern_data_ptr;
7440 entry = &ctl_cmd_table[opcode];
7441 if (entry->flags & CTL_CMD_FLAG_SA5) {
7442 entry = &((const struct ctl_cmd_entry *)
7443 entry->execute)[service_action];
7444 } else if (service_action != 0) {
7451 ctl_set_success(ctsio);
7452 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7453 ctsio->be_move_done = ctl_config_move_done;
7454 ctl_datamove((union ctl_io *)ctsio);
7459 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7461 struct scsi_report_supported_tmf *cdb;
7462 struct scsi_report_supported_tmf_ext_data *data;
7464 int alloc_len, total_len;
7466 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7468 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7470 retval = CTL_RETVAL_COMPLETE;
7472 if (cdb->options & RST_REPD)
7473 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7475 total_len = sizeof(struct scsi_report_supported_tmf_data);
7476 alloc_len = scsi_4btoul(cdb->length);
7478 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7479 ctsio->kern_sg_entries = 0;
7480 ctsio->kern_rel_offset = 0;
7481 ctsio->kern_data_len = min(total_len, alloc_len);
7482 ctsio->kern_total_len = ctsio->kern_data_len;
7484 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7485 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7487 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7488 data->length = total_len - 4;
7490 ctl_set_success(ctsio);
7491 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7492 ctsio->be_move_done = ctl_config_move_done;
7493 ctl_datamove((union ctl_io *)ctsio);
7498 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7500 struct scsi_report_timestamp *cdb;
7501 struct scsi_report_timestamp_data *data;
7505 int alloc_len, total_len;
7507 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7509 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7511 retval = CTL_RETVAL_COMPLETE;
7513 total_len = sizeof(struct scsi_report_timestamp_data);
7514 alloc_len = scsi_4btoul(cdb->length);
7516 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7517 ctsio->kern_sg_entries = 0;
7518 ctsio->kern_rel_offset = 0;
7519 ctsio->kern_data_len = min(total_len, alloc_len);
7520 ctsio->kern_total_len = ctsio->kern_data_len;
7522 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7523 scsi_ulto2b(sizeof(*data) - 2, data->length);
7524 data->origin = RTS_ORIG_OUTSIDE;
7526 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7527 scsi_ulto4b(timestamp >> 16, data->timestamp);
7528 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7530 ctl_set_success(ctsio);
7531 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7532 ctsio->be_move_done = ctl_config_move_done;
7533 ctl_datamove((union ctl_io *)ctsio);
7538 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7540 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7541 struct ctl_lun *lun = CTL_LUN(ctsio);
7542 struct scsi_per_res_in *cdb;
7543 int alloc_len, total_len = 0;
7544 /* struct scsi_per_res_in_rsrv in_data; */
7547 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7549 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7551 alloc_len = scsi_2btoul(cdb->length);
7554 mtx_lock(&lun->lun_lock);
7555 switch (cdb->action) {
7556 case SPRI_RK: /* read keys */
7557 total_len = sizeof(struct scsi_per_res_in_keys) +
7559 sizeof(struct scsi_per_res_key);
7561 case SPRI_RR: /* read reservation */
7562 if (lun->flags & CTL_LUN_PR_RESERVED)
7563 total_len = sizeof(struct scsi_per_res_in_rsrv);
7565 total_len = sizeof(struct scsi_per_res_in_header);
7567 case SPRI_RC: /* report capabilities */
7568 total_len = sizeof(struct scsi_per_res_cap);
7570 case SPRI_RS: /* read full status */
7571 total_len = sizeof(struct scsi_per_res_in_header) +
7572 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7576 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7578 mtx_unlock(&lun->lun_lock);
7580 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7581 ctsio->kern_rel_offset = 0;
7582 ctsio->kern_sg_entries = 0;
7583 ctsio->kern_data_len = min(total_len, alloc_len);
7584 ctsio->kern_total_len = ctsio->kern_data_len;
7586 mtx_lock(&lun->lun_lock);
7587 switch (cdb->action) {
7588 case SPRI_RK: { // read keys
7589 struct scsi_per_res_in_keys *res_keys;
7592 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7595 * We had to drop the lock to allocate our buffer, which
7596 * leaves time for someone to come in with another
7597 * persistent reservation. (That is unlikely, though,
7598 * since this should be the only persistent reservation
7599 * command active right now.)
7601 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7602 (lun->pr_key_count *
7603 sizeof(struct scsi_per_res_key)))){
7604 mtx_unlock(&lun->lun_lock);
7605 free(ctsio->kern_data_ptr, M_CTL);
7606 printf("%s: reservation length changed, retrying\n",
7611 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7613 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7614 lun->pr_key_count, res_keys->header.length);
7616 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7617 if ((key = ctl_get_prkey(lun, i)) == 0)
7621 * We used lun->pr_key_count to calculate the
7622 * size to allocate. If it turns out the number of
7623 * initiators with the registered flag set is
7624 * larger than that (i.e. they haven't been kept in
7625 * sync), we've got a problem.
7627 if (key_count >= lun->pr_key_count) {
7631 scsi_u64to8b(key, res_keys->keys[key_count].key);
7636 case SPRI_RR: { // read reservation
7637 struct scsi_per_res_in_rsrv *res;
7638 int tmp_len, header_only;
7640 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7642 scsi_ulto4b(lun->pr_generation, res->header.generation);
7644 if (lun->flags & CTL_LUN_PR_RESERVED)
7646 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7647 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7648 res->header.length);
7651 tmp_len = sizeof(struct scsi_per_res_in_header);
7652 scsi_ulto4b(0, res->header.length);
7657 * We had to drop the lock to allocate our buffer, which
7658 * leaves time for someone to come in with another
7659 * persistent reservation. (That is unlikely, though,
7660 * since this should be the only persistent reservation
7661 * command active right now.)
7663 if (tmp_len != total_len) {
7664 mtx_unlock(&lun->lun_lock);
7665 free(ctsio->kern_data_ptr, M_CTL);
7666 printf("%s: reservation status changed, retrying\n",
7672 * No reservation held, so we're done.
7674 if (header_only != 0)
7678 * If the registration is an All Registrants type, the key
7679 * is 0, since it doesn't really matter.
7681 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7682 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7683 res->data.reservation);
7685 res->data.scopetype = lun->pr_res_type;
7688 case SPRI_RC: //report capabilities
7690 struct scsi_per_res_cap *res_cap;
7693 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7694 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7695 res_cap->flags1 = SPRI_CRH;
7696 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7697 type_mask = SPRI_TM_WR_EX_AR |
7703 scsi_ulto2b(type_mask, res_cap->type_mask);
7706 case SPRI_RS: { // read full status
7707 struct scsi_per_res_in_full *res_status;
7708 struct scsi_per_res_in_full_desc *res_desc;
7709 struct ctl_port *port;
7712 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7715 * We had to drop the lock to allocate our buffer, which
7716 * leaves time for someone to come in with another
7717 * persistent reservation. (That is unlikely, though,
7718 * since this should be the only persistent reservation
7719 * command active right now.)
7721 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7722 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7723 lun->pr_key_count)){
7724 mtx_unlock(&lun->lun_lock);
7725 free(ctsio->kern_data_ptr, M_CTL);
7726 printf("%s: reservation length changed, retrying\n",
7731 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7733 res_desc = &res_status->desc[0];
7734 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7735 if ((key = ctl_get_prkey(lun, i)) == 0)
7738 scsi_u64to8b(key, res_desc->res_key.key);
7739 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7740 (lun->pr_res_idx == i ||
7741 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7742 res_desc->flags = SPRI_FULL_R_HOLDER;
7743 res_desc->scopetype = lun->pr_res_type;
7745 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7746 res_desc->rel_trgt_port_id);
7748 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7750 len = ctl_create_iid(port,
7751 i % CTL_MAX_INIT_PER_PORT,
7752 res_desc->transport_id);
7753 scsi_ulto4b(len, res_desc->additional_length);
7754 res_desc = (struct scsi_per_res_in_full_desc *)
7755 &res_desc->transport_id[len];
7757 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7758 res_status->header.length);
7762 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7764 mtx_unlock(&lun->lun_lock);
7766 ctl_set_success(ctsio);
7767 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7768 ctsio->be_move_done = ctl_config_move_done;
7769 ctl_datamove((union ctl_io *)ctsio);
7770 return (CTL_RETVAL_COMPLETE);
7774 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7778 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7779 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7780 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7781 struct scsi_per_res_out_parms* param)
7783 union ctl_ha_msg persis_io;
7786 mtx_lock(&lun->lun_lock);
7787 if (sa_res_key == 0) {
7788 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7789 /* validate scope and type */
7790 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7792 mtx_unlock(&lun->lun_lock);
7793 ctl_set_invalid_field(/*ctsio*/ ctsio,
7799 ctl_done((union ctl_io *)ctsio);
7803 if (type>8 || type==2 || type==4 || type==0) {
7804 mtx_unlock(&lun->lun_lock);
7805 ctl_set_invalid_field(/*ctsio*/ ctsio,
7811 ctl_done((union ctl_io *)ctsio);
7816 * Unregister everybody else and build UA for
7819 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7820 if (i == residx || ctl_get_prkey(lun, i) == 0)
7823 ctl_clr_prkey(lun, i);
7824 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7826 lun->pr_key_count = 1;
7827 lun->pr_res_type = type;
7828 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7829 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7830 lun->pr_res_idx = residx;
7831 lun->pr_generation++;
7832 mtx_unlock(&lun->lun_lock);
7834 /* send msg to other side */
7835 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7836 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7837 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7838 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7839 persis_io.pr.pr_info.res_type = type;
7840 memcpy(persis_io.pr.pr_info.sa_res_key,
7841 param->serv_act_res_key,
7842 sizeof(param->serv_act_res_key));
7843 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7844 sizeof(persis_io.pr), M_WAITOK);
7846 /* not all registrants */
7847 mtx_unlock(&lun->lun_lock);
7848 free(ctsio->kern_data_ptr, M_CTL);
7849 ctl_set_invalid_field(ctsio,
7855 ctl_done((union ctl_io *)ctsio);
7858 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7859 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7862 if (res_key == sa_res_key) {
7865 * The spec implies this is not good but doesn't
7866 * say what to do. There are two choices either
7867 * generate a res conflict or check condition
7868 * with illegal field in parameter data. Since
7869 * that is what is done when the sa_res_key is
7870 * zero I'll take that approach since this has
7871 * to do with the sa_res_key.
7873 mtx_unlock(&lun->lun_lock);
7874 free(ctsio->kern_data_ptr, M_CTL);
7875 ctl_set_invalid_field(ctsio,
7881 ctl_done((union ctl_io *)ctsio);
7885 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7886 if (ctl_get_prkey(lun, i) != sa_res_key)
7890 ctl_clr_prkey(lun, i);
7891 lun->pr_key_count--;
7892 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7895 mtx_unlock(&lun->lun_lock);
7896 free(ctsio->kern_data_ptr, M_CTL);
7897 ctl_set_reservation_conflict(ctsio);
7898 ctl_done((union ctl_io *)ctsio);
7899 return (CTL_RETVAL_COMPLETE);
7901 lun->pr_generation++;
7902 mtx_unlock(&lun->lun_lock);
7904 /* send msg to other side */
7905 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7906 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7907 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7908 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7909 persis_io.pr.pr_info.res_type = type;
7910 memcpy(persis_io.pr.pr_info.sa_res_key,
7911 param->serv_act_res_key,
7912 sizeof(param->serv_act_res_key));
7913 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7914 sizeof(persis_io.pr), M_WAITOK);
7916 /* Reserved but not all registrants */
7917 /* sa_res_key is res holder */
7918 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7919 /* validate scope and type */
7920 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7922 mtx_unlock(&lun->lun_lock);
7923 ctl_set_invalid_field(/*ctsio*/ ctsio,
7929 ctl_done((union ctl_io *)ctsio);
7933 if (type>8 || type==2 || type==4 || type==0) {
7934 mtx_unlock(&lun->lun_lock);
7935 ctl_set_invalid_field(/*ctsio*/ ctsio,
7941 ctl_done((union ctl_io *)ctsio);
7947 * if sa_res_key != res_key remove all
7948 * registrants w/sa_res_key and generate UA
7949 * for these registrants(Registrations
7950 * Preempted) if it wasn't an exclusive
7951 * reservation generate UA(Reservations
7952 * Preempted) for all other registered nexuses
7953 * if the type has changed. Establish the new
7954 * reservation and holder. If res_key and
7955 * sa_res_key are the same do the above
7956 * except don't unregister the res holder.
7959 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7960 if (i == residx || ctl_get_prkey(lun, i) == 0)
7963 if (sa_res_key == ctl_get_prkey(lun, i)) {
7964 ctl_clr_prkey(lun, i);
7965 lun->pr_key_count--;
7966 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7967 } else if (type != lun->pr_res_type &&
7968 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7969 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
7970 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7973 lun->pr_res_type = type;
7974 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7975 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7976 lun->pr_res_idx = residx;
7978 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7979 lun->pr_generation++;
7980 mtx_unlock(&lun->lun_lock);
7982 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7983 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7984 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7985 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7986 persis_io.pr.pr_info.res_type = type;
7987 memcpy(persis_io.pr.pr_info.sa_res_key,
7988 param->serv_act_res_key,
7989 sizeof(param->serv_act_res_key));
7990 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7991 sizeof(persis_io.pr), M_WAITOK);
7994 * sa_res_key is not the res holder just
7995 * remove registrants
7999 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8000 if (sa_res_key != ctl_get_prkey(lun, i))
8004 ctl_clr_prkey(lun, i);
8005 lun->pr_key_count--;
8006 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8010 mtx_unlock(&lun->lun_lock);
8011 free(ctsio->kern_data_ptr, M_CTL);
8012 ctl_set_reservation_conflict(ctsio);
8013 ctl_done((union ctl_io *)ctsio);
8016 lun->pr_generation++;
8017 mtx_unlock(&lun->lun_lock);
8019 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8020 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8021 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8022 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8023 persis_io.pr.pr_info.res_type = type;
8024 memcpy(persis_io.pr.pr_info.sa_res_key,
8025 param->serv_act_res_key,
8026 sizeof(param->serv_act_res_key));
8027 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8028 sizeof(persis_io.pr), M_WAITOK);
8035 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8037 uint64_t sa_res_key;
8040 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8042 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8043 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8044 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8045 if (sa_res_key == 0) {
8047 * Unregister everybody else and build UA for
8050 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8051 if (i == msg->pr.pr_info.residx ||
8052 ctl_get_prkey(lun, i) == 0)
8055 ctl_clr_prkey(lun, i);
8056 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8059 lun->pr_key_count = 1;
8060 lun->pr_res_type = msg->pr.pr_info.res_type;
8061 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8062 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8063 lun->pr_res_idx = msg->pr.pr_info.residx;
8065 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8066 if (sa_res_key == ctl_get_prkey(lun, i))
8069 ctl_clr_prkey(lun, i);
8070 lun->pr_key_count--;
8071 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8075 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8076 if (i == msg->pr.pr_info.residx ||
8077 ctl_get_prkey(lun, i) == 0)
8080 if (sa_res_key == ctl_get_prkey(lun, i)) {
8081 ctl_clr_prkey(lun, i);
8082 lun->pr_key_count--;
8083 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8084 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8085 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8086 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8087 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8090 lun->pr_res_type = msg->pr.pr_info.res_type;
8091 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8092 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8093 lun->pr_res_idx = msg->pr.pr_info.residx;
8095 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8097 lun->pr_generation++;
8103 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8105 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8106 struct ctl_lun *lun = CTL_LUN(ctsio);
8108 u_int32_t param_len;
8109 struct scsi_per_res_out *cdb;
8110 struct scsi_per_res_out_parms* param;
8112 uint64_t res_key, sa_res_key, key;
8114 union ctl_ha_msg persis_io;
8117 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8119 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8120 retval = CTL_RETVAL_COMPLETE;
8123 * We only support whole-LUN scope. The scope & type are ignored for
8124 * register, register and ignore existing key and clear.
8125 * We sometimes ignore scope and type on preempts too!!
8126 * Verify reservation type here as well.
8128 type = cdb->scope_type & SPR_TYPE_MASK;
8129 if ((cdb->action == SPRO_RESERVE)
8130 || (cdb->action == SPRO_RELEASE)) {
8131 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8132 ctl_set_invalid_field(/*ctsio*/ ctsio,
8138 ctl_done((union ctl_io *)ctsio);
8139 return (CTL_RETVAL_COMPLETE);
8142 if (type>8 || type==2 || type==4 || type==0) {
8143 ctl_set_invalid_field(/*ctsio*/ ctsio,
8149 ctl_done((union ctl_io *)ctsio);
8150 return (CTL_RETVAL_COMPLETE);
8154 param_len = scsi_4btoul(cdb->length);
8156 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8157 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8158 ctsio->kern_data_len = param_len;
8159 ctsio->kern_total_len = param_len;
8160 ctsio->kern_rel_offset = 0;
8161 ctsio->kern_sg_entries = 0;
8162 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8163 ctsio->be_move_done = ctl_config_move_done;
8164 ctl_datamove((union ctl_io *)ctsio);
8166 return (CTL_RETVAL_COMPLETE);
8169 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8171 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8172 res_key = scsi_8btou64(param->res_key.key);
8173 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8176 * Validate the reservation key here except for SPRO_REG_IGNO
8177 * This must be done for all other service actions
8179 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8180 mtx_lock(&lun->lun_lock);
8181 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8182 if (res_key != key) {
8184 * The current key passed in doesn't match
8185 * the one the initiator previously
8188 mtx_unlock(&lun->lun_lock);
8189 free(ctsio->kern_data_ptr, M_CTL);
8190 ctl_set_reservation_conflict(ctsio);
8191 ctl_done((union ctl_io *)ctsio);
8192 return (CTL_RETVAL_COMPLETE);
8194 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8196 * We are not registered
8198 mtx_unlock(&lun->lun_lock);
8199 free(ctsio->kern_data_ptr, M_CTL);
8200 ctl_set_reservation_conflict(ctsio);
8201 ctl_done((union ctl_io *)ctsio);
8202 return (CTL_RETVAL_COMPLETE);
8203 } else if (res_key != 0) {
8205 * We are not registered and trying to register but
8206 * the register key isn't zero.
8208 mtx_unlock(&lun->lun_lock);
8209 free(ctsio->kern_data_ptr, M_CTL);
8210 ctl_set_reservation_conflict(ctsio);
8211 ctl_done((union ctl_io *)ctsio);
8212 return (CTL_RETVAL_COMPLETE);
8214 mtx_unlock(&lun->lun_lock);
8217 switch (cdb->action & SPRO_ACTION_MASK) {
8219 case SPRO_REG_IGNO: {
8222 printf("Registration received\n");
8226 * We don't support any of these options, as we report in
8227 * the read capabilities request (see
8228 * ctl_persistent_reserve_in(), above).
8230 if ((param->flags & SPR_SPEC_I_PT)
8231 || (param->flags & SPR_ALL_TG_PT)
8232 || (param->flags & SPR_APTPL)) {
8235 if (param->flags & SPR_APTPL)
8237 else if (param->flags & SPR_ALL_TG_PT)
8239 else /* SPR_SPEC_I_PT */
8242 free(ctsio->kern_data_ptr, M_CTL);
8243 ctl_set_invalid_field(ctsio,
8249 ctl_done((union ctl_io *)ctsio);
8250 return (CTL_RETVAL_COMPLETE);
8253 mtx_lock(&lun->lun_lock);
8256 * The initiator wants to clear the
8259 if (sa_res_key == 0) {
8261 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8262 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8263 && ctl_get_prkey(lun, residx) == 0)) {
8264 mtx_unlock(&lun->lun_lock);
8268 ctl_clr_prkey(lun, residx);
8269 lun->pr_key_count--;
8271 if (residx == lun->pr_res_idx) {
8272 lun->flags &= ~CTL_LUN_PR_RESERVED;
8273 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8275 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8276 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8277 lun->pr_key_count) {
8279 * If the reservation is a registrants
8280 * only type we need to generate a UA
8281 * for other registered inits. The
8282 * sense code should be RESERVATIONS
8286 for (i = softc->init_min; i < softc->init_max; i++){
8287 if (ctl_get_prkey(lun, i) == 0)
8290 CTL_UA_RES_RELEASE);
8293 lun->pr_res_type = 0;
8294 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8295 if (lun->pr_key_count==0) {
8296 lun->flags &= ~CTL_LUN_PR_RESERVED;
8297 lun->pr_res_type = 0;
8298 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8301 lun->pr_generation++;
8302 mtx_unlock(&lun->lun_lock);
8304 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8305 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8306 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8307 persis_io.pr.pr_info.residx = residx;
8308 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8309 sizeof(persis_io.pr), M_WAITOK);
8310 } else /* sa_res_key != 0 */ {
8313 * If we aren't registered currently then increment
8314 * the key count and set the registered flag.
8316 ctl_alloc_prkey(lun, residx);
8317 if (ctl_get_prkey(lun, residx) == 0)
8318 lun->pr_key_count++;
8319 ctl_set_prkey(lun, residx, sa_res_key);
8320 lun->pr_generation++;
8321 mtx_unlock(&lun->lun_lock);
8323 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8324 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8325 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8326 persis_io.pr.pr_info.residx = residx;
8327 memcpy(persis_io.pr.pr_info.sa_res_key,
8328 param->serv_act_res_key,
8329 sizeof(param->serv_act_res_key));
8330 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8331 sizeof(persis_io.pr), M_WAITOK);
8338 printf("Reserve executed type %d\n", type);
8340 mtx_lock(&lun->lun_lock);
8341 if (lun->flags & CTL_LUN_PR_RESERVED) {
8343 * if this isn't the reservation holder and it's
8344 * not a "all registrants" type or if the type is
8345 * different then we have a conflict
8347 if ((lun->pr_res_idx != residx
8348 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8349 || lun->pr_res_type != type) {
8350 mtx_unlock(&lun->lun_lock);
8351 free(ctsio->kern_data_ptr, M_CTL);
8352 ctl_set_reservation_conflict(ctsio);
8353 ctl_done((union ctl_io *)ctsio);
8354 return (CTL_RETVAL_COMPLETE);
8356 mtx_unlock(&lun->lun_lock);
8357 } else /* create a reservation */ {
8359 * If it's not an "all registrants" type record
8360 * reservation holder
8362 if (type != SPR_TYPE_WR_EX_AR
8363 && type != SPR_TYPE_EX_AC_AR)
8364 lun->pr_res_idx = residx; /* Res holder */
8366 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8368 lun->flags |= CTL_LUN_PR_RESERVED;
8369 lun->pr_res_type = type;
8371 mtx_unlock(&lun->lun_lock);
8373 /* send msg to other side */
8374 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8375 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8376 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8377 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8378 persis_io.pr.pr_info.res_type = type;
8379 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8380 sizeof(persis_io.pr), M_WAITOK);
8385 mtx_lock(&lun->lun_lock);
8386 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8387 /* No reservation exists return good status */
8388 mtx_unlock(&lun->lun_lock);
8392 * Is this nexus a reservation holder?
8394 if (lun->pr_res_idx != residx
8395 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8397 * not a res holder return good status but
8400 mtx_unlock(&lun->lun_lock);
8404 if (lun->pr_res_type != type) {
8405 mtx_unlock(&lun->lun_lock);
8406 free(ctsio->kern_data_ptr, M_CTL);
8407 ctl_set_illegal_pr_release(ctsio);
8408 ctl_done((union ctl_io *)ctsio);
8409 return (CTL_RETVAL_COMPLETE);
8412 /* okay to release */
8413 lun->flags &= ~CTL_LUN_PR_RESERVED;
8414 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8415 lun->pr_res_type = 0;
8418 * If this isn't an exclusive access reservation and NUAR
8419 * is not set, generate UA for all other registrants.
8421 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8422 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8423 for (i = softc->init_min; i < softc->init_max; i++) {
8424 if (i == residx || ctl_get_prkey(lun, i) == 0)
8426 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8429 mtx_unlock(&lun->lun_lock);
8431 /* Send msg to other side */
8432 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8433 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8434 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8435 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8436 sizeof(persis_io.pr), M_WAITOK);
8440 /* send msg to other side */
8442 mtx_lock(&lun->lun_lock);
8443 lun->flags &= ~CTL_LUN_PR_RESERVED;
8444 lun->pr_res_type = 0;
8445 lun->pr_key_count = 0;
8446 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8448 ctl_clr_prkey(lun, residx);
8449 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8450 if (ctl_get_prkey(lun, i) != 0) {
8451 ctl_clr_prkey(lun, i);
8452 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8454 lun->pr_generation++;
8455 mtx_unlock(&lun->lun_lock);
8457 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8458 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8459 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8460 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8461 sizeof(persis_io.pr), M_WAITOK);
8465 case SPRO_PRE_ABO: {
8468 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8469 residx, ctsio, cdb, param);
8471 return (CTL_RETVAL_COMPLETE);
8475 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8479 free(ctsio->kern_data_ptr, M_CTL);
8480 ctl_set_success(ctsio);
8481 ctl_done((union ctl_io *)ctsio);
8487 * This routine is for handling a message from the other SC pertaining to
8488 * persistent reserve out. All the error checking will have been done
8489 * so only perorming the action need be done here to keep the two
8493 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8495 struct ctl_softc *softc = CTL_SOFTC(io);
8496 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8497 struct ctl_lun *lun;
8499 uint32_t residx, targ_lun;
8501 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8502 mtx_lock(&softc->ctl_lock);
8503 if (targ_lun >= ctl_max_luns ||
8504 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8505 mtx_unlock(&softc->ctl_lock);
8508 mtx_lock(&lun->lun_lock);
8509 mtx_unlock(&softc->ctl_lock);
8510 if (lun->flags & CTL_LUN_DISABLED) {
8511 mtx_unlock(&lun->lun_lock);
8514 residx = ctl_get_initindex(&msg->hdr.nexus);
8515 switch(msg->pr.pr_info.action) {
8516 case CTL_PR_REG_KEY:
8517 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8518 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8519 lun->pr_key_count++;
8520 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8521 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8522 lun->pr_generation++;
8525 case CTL_PR_UNREG_KEY:
8526 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8527 lun->pr_key_count--;
8529 /* XXX Need to see if the reservation has been released */
8530 /* if so do we need to generate UA? */
8531 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8532 lun->flags &= ~CTL_LUN_PR_RESERVED;
8533 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8535 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8536 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8537 lun->pr_key_count) {
8539 * If the reservation is a registrants
8540 * only type we need to generate a UA
8541 * for other registered inits. The
8542 * sense code should be RESERVATIONS
8546 for (i = softc->init_min; i < softc->init_max; i++) {
8547 if (ctl_get_prkey(lun, i) == 0)
8550 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8553 lun->pr_res_type = 0;
8554 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8555 if (lun->pr_key_count==0) {
8556 lun->flags &= ~CTL_LUN_PR_RESERVED;
8557 lun->pr_res_type = 0;
8558 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8561 lun->pr_generation++;
8564 case CTL_PR_RESERVE:
8565 lun->flags |= CTL_LUN_PR_RESERVED;
8566 lun->pr_res_type = msg->pr.pr_info.res_type;
8567 lun->pr_res_idx = msg->pr.pr_info.residx;
8571 case CTL_PR_RELEASE:
8573 * If this isn't an exclusive access reservation and NUAR
8574 * is not set, generate UA for all other registrants.
8576 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8577 lun->pr_res_type != SPR_TYPE_WR_EX &&
8578 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8579 for (i = softc->init_min; i < softc->init_max; i++) {
8580 if (i == residx || ctl_get_prkey(lun, i) == 0)
8582 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8586 lun->flags &= ~CTL_LUN_PR_RESERVED;
8587 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8588 lun->pr_res_type = 0;
8591 case CTL_PR_PREEMPT:
8592 ctl_pro_preempt_other(lun, msg);
8595 lun->flags &= ~CTL_LUN_PR_RESERVED;
8596 lun->pr_res_type = 0;
8597 lun->pr_key_count = 0;
8598 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8600 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8601 if (ctl_get_prkey(lun, i) == 0)
8603 ctl_clr_prkey(lun, i);
8604 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8606 lun->pr_generation++;
8610 mtx_unlock(&lun->lun_lock);
8614 ctl_read_write(struct ctl_scsiio *ctsio)
8616 struct ctl_lun *lun = CTL_LUN(ctsio);
8617 struct ctl_lba_len_flags *lbalen;
8619 uint32_t num_blocks;
8623 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8626 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8627 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8628 switch (ctsio->cdb[0]) {
8631 struct scsi_rw_6 *cdb;
8633 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8635 lba = scsi_3btoul(cdb->addr);
8636 /* only 5 bits are valid in the most significant address byte */
8638 num_blocks = cdb->length;
8640 * This is correct according to SBC-2.
8642 if (num_blocks == 0)
8648 struct scsi_rw_10 *cdb;
8650 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8651 if (cdb->byte2 & SRW10_FUA)
8652 flags |= CTL_LLF_FUA;
8653 if (cdb->byte2 & SRW10_DPO)
8654 flags |= CTL_LLF_DPO;
8655 lba = scsi_4btoul(cdb->addr);
8656 num_blocks = scsi_2btoul(cdb->length);
8659 case WRITE_VERIFY_10: {
8660 struct scsi_write_verify_10 *cdb;
8662 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8663 flags |= CTL_LLF_FUA;
8664 if (cdb->byte2 & SWV_DPO)
8665 flags |= CTL_LLF_DPO;
8666 lba = scsi_4btoul(cdb->addr);
8667 num_blocks = scsi_2btoul(cdb->length);
8672 struct scsi_rw_12 *cdb;
8674 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8675 if (cdb->byte2 & SRW12_FUA)
8676 flags |= CTL_LLF_FUA;
8677 if (cdb->byte2 & SRW12_DPO)
8678 flags |= CTL_LLF_DPO;
8679 lba = scsi_4btoul(cdb->addr);
8680 num_blocks = scsi_4btoul(cdb->length);
8683 case WRITE_VERIFY_12: {
8684 struct scsi_write_verify_12 *cdb;
8686 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8687 flags |= CTL_LLF_FUA;
8688 if (cdb->byte2 & SWV_DPO)
8689 flags |= CTL_LLF_DPO;
8690 lba = scsi_4btoul(cdb->addr);
8691 num_blocks = scsi_4btoul(cdb->length);
8696 struct scsi_rw_16 *cdb;
8698 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8699 if (cdb->byte2 & SRW12_FUA)
8700 flags |= CTL_LLF_FUA;
8701 if (cdb->byte2 & SRW12_DPO)
8702 flags |= CTL_LLF_DPO;
8703 lba = scsi_8btou64(cdb->addr);
8704 num_blocks = scsi_4btoul(cdb->length);
8707 case WRITE_ATOMIC_16: {
8708 struct scsi_write_atomic_16 *cdb;
8710 if (lun->be_lun->atomicblock == 0) {
8711 ctl_set_invalid_opcode(ctsio);
8712 ctl_done((union ctl_io *)ctsio);
8713 return (CTL_RETVAL_COMPLETE);
8716 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8717 if (cdb->byte2 & SRW12_FUA)
8718 flags |= CTL_LLF_FUA;
8719 if (cdb->byte2 & SRW12_DPO)
8720 flags |= CTL_LLF_DPO;
8721 lba = scsi_8btou64(cdb->addr);
8722 num_blocks = scsi_2btoul(cdb->length);
8723 if (num_blocks > lun->be_lun->atomicblock) {
8724 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8725 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8727 ctl_done((union ctl_io *)ctsio);
8728 return (CTL_RETVAL_COMPLETE);
8732 case WRITE_VERIFY_16: {
8733 struct scsi_write_verify_16 *cdb;
8735 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8736 flags |= CTL_LLF_FUA;
8737 if (cdb->byte2 & SWV_DPO)
8738 flags |= CTL_LLF_DPO;
8739 lba = scsi_8btou64(cdb->addr);
8740 num_blocks = scsi_4btoul(cdb->length);
8745 * We got a command we don't support. This shouldn't
8746 * happen, commands should be filtered out above us.
8748 ctl_set_invalid_opcode(ctsio);
8749 ctl_done((union ctl_io *)ctsio);
8751 return (CTL_RETVAL_COMPLETE);
8752 break; /* NOTREACHED */
8756 * The first check is to make sure we're in bounds, the second
8757 * check is to catch wrap-around problems. If the lba + num blocks
8758 * is less than the lba, then we've wrapped around and the block
8759 * range is invalid anyway.
8761 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8762 || ((lba + num_blocks) < lba)) {
8763 ctl_set_lba_out_of_range(ctsio,
8764 MAX(lba, lun->be_lun->maxlba + 1));
8765 ctl_done((union ctl_io *)ctsio);
8766 return (CTL_RETVAL_COMPLETE);
8770 * According to SBC-3, a transfer length of 0 is not an error.
8771 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8772 * translates to 256 blocks for those commands.
8774 if (num_blocks == 0) {
8775 ctl_set_success(ctsio);
8776 ctl_done((union ctl_io *)ctsio);
8777 return (CTL_RETVAL_COMPLETE);
8780 /* Set FUA and/or DPO if caches are disabled. */
8782 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8783 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8785 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8786 flags |= CTL_LLF_FUA;
8789 lbalen = (struct ctl_lba_len_flags *)
8790 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8792 lbalen->len = num_blocks;
8793 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8795 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8796 ctsio->kern_rel_offset = 0;
8798 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8800 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8805 ctl_cnw_cont(union ctl_io *io)
8807 struct ctl_lun *lun = CTL_LUN(io);
8808 struct ctl_scsiio *ctsio;
8809 struct ctl_lba_len_flags *lbalen;
8812 ctsio = &io->scsiio;
8813 ctsio->io_hdr.status = CTL_STATUS_NONE;
8814 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8815 lbalen = (struct ctl_lba_len_flags *)
8816 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8817 lbalen->flags &= ~CTL_LLF_COMPARE;
8818 lbalen->flags |= CTL_LLF_WRITE;
8820 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8821 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8826 ctl_cnw(struct ctl_scsiio *ctsio)
8828 struct ctl_lun *lun = CTL_LUN(ctsio);
8829 struct ctl_lba_len_flags *lbalen;
8831 uint32_t num_blocks;
8834 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8837 switch (ctsio->cdb[0]) {
8838 case COMPARE_AND_WRITE: {
8839 struct scsi_compare_and_write *cdb;
8841 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8842 if (cdb->byte2 & SRW10_FUA)
8843 flags |= CTL_LLF_FUA;
8844 if (cdb->byte2 & SRW10_DPO)
8845 flags |= CTL_LLF_DPO;
8846 lba = scsi_8btou64(cdb->addr);
8847 num_blocks = cdb->length;
8852 * We got a command we don't support. This shouldn't
8853 * happen, commands should be filtered out above us.
8855 ctl_set_invalid_opcode(ctsio);
8856 ctl_done((union ctl_io *)ctsio);
8858 return (CTL_RETVAL_COMPLETE);
8859 break; /* NOTREACHED */
8863 * The first check is to make sure we're in bounds, the second
8864 * check is to catch wrap-around problems. If the lba + num blocks
8865 * is less than the lba, then we've wrapped around and the block
8866 * range is invalid anyway.
8868 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8869 || ((lba + num_blocks) < lba)) {
8870 ctl_set_lba_out_of_range(ctsio,
8871 MAX(lba, lun->be_lun->maxlba + 1));
8872 ctl_done((union ctl_io *)ctsio);
8873 return (CTL_RETVAL_COMPLETE);
8877 * According to SBC-3, a transfer length of 0 is not an error.
8879 if (num_blocks == 0) {
8880 ctl_set_success(ctsio);
8881 ctl_done((union ctl_io *)ctsio);
8882 return (CTL_RETVAL_COMPLETE);
8885 /* Set FUA if write cache is disabled. */
8886 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8887 flags |= CTL_LLF_FUA;
8889 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8890 ctsio->kern_rel_offset = 0;
8893 * Set the IO_CONT flag, so that if this I/O gets passed to
8894 * ctl_data_submit_done(), it'll get passed back to
8895 * ctl_ctl_cnw_cont() for further processing.
8897 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8898 ctsio->io_cont = ctl_cnw_cont;
8900 lbalen = (struct ctl_lba_len_flags *)
8901 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8903 lbalen->len = num_blocks;
8904 lbalen->flags = CTL_LLF_COMPARE | flags;
8906 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8907 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8912 ctl_verify(struct ctl_scsiio *ctsio)
8914 struct ctl_lun *lun = CTL_LUN(ctsio);
8915 struct ctl_lba_len_flags *lbalen;
8917 uint32_t num_blocks;
8921 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8924 flags = CTL_LLF_FUA;
8925 switch (ctsio->cdb[0]) {
8927 struct scsi_verify_10 *cdb;
8929 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8930 if (cdb->byte2 & SVFY_BYTCHK)
8932 if (cdb->byte2 & SVFY_DPO)
8933 flags |= CTL_LLF_DPO;
8934 lba = scsi_4btoul(cdb->addr);
8935 num_blocks = scsi_2btoul(cdb->length);
8939 struct scsi_verify_12 *cdb;
8941 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8942 if (cdb->byte2 & SVFY_BYTCHK)
8944 if (cdb->byte2 & SVFY_DPO)
8945 flags |= CTL_LLF_DPO;
8946 lba = scsi_4btoul(cdb->addr);
8947 num_blocks = scsi_4btoul(cdb->length);
8951 struct scsi_rw_16 *cdb;
8953 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8954 if (cdb->byte2 & SVFY_BYTCHK)
8956 if (cdb->byte2 & SVFY_DPO)
8957 flags |= CTL_LLF_DPO;
8958 lba = scsi_8btou64(cdb->addr);
8959 num_blocks = scsi_4btoul(cdb->length);
8964 * We got a command we don't support. This shouldn't
8965 * happen, commands should be filtered out above us.
8967 ctl_set_invalid_opcode(ctsio);
8968 ctl_done((union ctl_io *)ctsio);
8969 return (CTL_RETVAL_COMPLETE);
8973 * The first check is to make sure we're in bounds, the second
8974 * check is to catch wrap-around problems. If the lba + num blocks
8975 * is less than the lba, then we've wrapped around and the block
8976 * range is invalid anyway.
8978 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8979 || ((lba + num_blocks) < lba)) {
8980 ctl_set_lba_out_of_range(ctsio,
8981 MAX(lba, lun->be_lun->maxlba + 1));
8982 ctl_done((union ctl_io *)ctsio);
8983 return (CTL_RETVAL_COMPLETE);
8987 * According to SBC-3, a transfer length of 0 is not an error.
8989 if (num_blocks == 0) {
8990 ctl_set_success(ctsio);
8991 ctl_done((union ctl_io *)ctsio);
8992 return (CTL_RETVAL_COMPLETE);
8995 lbalen = (struct ctl_lba_len_flags *)
8996 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8998 lbalen->len = num_blocks;
9000 lbalen->flags = CTL_LLF_COMPARE | flags;
9001 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9003 lbalen->flags = CTL_LLF_VERIFY | flags;
9004 ctsio->kern_total_len = 0;
9006 ctsio->kern_rel_offset = 0;
9008 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9009 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9014 ctl_report_luns(struct ctl_scsiio *ctsio)
9016 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9017 struct ctl_port *port = CTL_PORT(ctsio);
9018 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9019 struct scsi_report_luns *cdb;
9020 struct scsi_report_luns_data *lun_data;
9021 int num_filled, num_luns, num_port_luns, retval;
9022 uint32_t alloc_len, lun_datalen;
9023 uint32_t initidx, targ_lun_id, lun_id;
9025 retval = CTL_RETVAL_COMPLETE;
9026 cdb = (struct scsi_report_luns *)ctsio->cdb;
9028 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9031 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9032 mtx_lock(&softc->ctl_lock);
9033 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9034 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9037 mtx_unlock(&softc->ctl_lock);
9039 switch (cdb->select_report) {
9040 case RPL_REPORT_DEFAULT:
9041 case RPL_REPORT_ALL:
9042 case RPL_REPORT_NONSUBSID:
9044 case RPL_REPORT_WELLKNOWN:
9045 case RPL_REPORT_ADMIN:
9046 case RPL_REPORT_CONGLOM:
9050 ctl_set_invalid_field(ctsio,
9056 ctl_done((union ctl_io *)ctsio);
9058 break; /* NOTREACHED */
9061 alloc_len = scsi_4btoul(cdb->length);
9063 * The initiator has to allocate at least 16 bytes for this request,
9064 * so he can at least get the header and the first LUN. Otherwise
9065 * we reject the request (per SPC-3 rev 14, section 6.21).
9067 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9068 sizeof(struct scsi_report_luns_lundata))) {
9069 ctl_set_invalid_field(ctsio,
9075 ctl_done((union ctl_io *)ctsio);
9079 lun_datalen = sizeof(*lun_data) +
9080 (num_luns * sizeof(struct scsi_report_luns_lundata));
9082 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9083 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9084 ctsio->kern_sg_entries = 0;
9086 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9088 mtx_lock(&softc->ctl_lock);
9089 for (targ_lun_id = 0, num_filled = 0;
9090 targ_lun_id < num_port_luns && num_filled < num_luns;
9092 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9093 if (lun_id == UINT32_MAX)
9095 lun = softc->ctl_luns[lun_id];
9099 be64enc(lun_data->luns[num_filled++].lundata,
9100 ctl_encode_lun(targ_lun_id));
9103 * According to SPC-3, rev 14 section 6.21:
9105 * "The execution of a REPORT LUNS command to any valid and
9106 * installed logical unit shall clear the REPORTED LUNS DATA
9107 * HAS CHANGED unit attention condition for all logical
9108 * units of that target with respect to the requesting
9109 * initiator. A valid and installed logical unit is one
9110 * having a PERIPHERAL QUALIFIER of 000b in the standard
9111 * INQUIRY data (see 6.4.2)."
9113 * If request_lun is NULL, the LUN this report luns command
9114 * was issued to is either disabled or doesn't exist. In that
9115 * case, we shouldn't clear any pending lun change unit
9118 if (request_lun != NULL) {
9119 mtx_lock(&lun->lun_lock);
9120 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9121 mtx_unlock(&lun->lun_lock);
9124 mtx_unlock(&softc->ctl_lock);
9127 * It's quite possible that we've returned fewer LUNs than we allocated
9128 * space for. Trim it.
9130 lun_datalen = sizeof(*lun_data) +
9131 (num_filled * sizeof(struct scsi_report_luns_lundata));
9132 ctsio->kern_rel_offset = 0;
9133 ctsio->kern_sg_entries = 0;
9134 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9135 ctsio->kern_total_len = ctsio->kern_data_len;
9138 * We set this to the actual data length, regardless of how much
9139 * space we actually have to return results. If the user looks at
9140 * this value, he'll know whether or not he allocated enough space
9141 * and reissue the command if necessary. We don't support well
9142 * known logical units, so if the user asks for that, return none.
9144 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9147 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9150 ctl_set_success(ctsio);
9151 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9152 ctsio->be_move_done = ctl_config_move_done;
9153 ctl_datamove((union ctl_io *)ctsio);
9158 ctl_request_sense(struct ctl_scsiio *ctsio)
9160 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9161 struct ctl_lun *lun = CTL_LUN(ctsio);
9162 struct scsi_request_sense *cdb;
9163 struct scsi_sense_data *sense_ptr, *ps;
9166 u_int sense_len = SSD_FULL_SIZE;
9167 scsi_sense_data_type sense_format;
9168 ctl_ua_type ua_type;
9169 uint8_t asc = 0, ascq = 0;
9171 cdb = (struct scsi_request_sense *)ctsio->cdb;
9173 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9176 * Determine which sense format the user wants.
9178 if (cdb->byte2 & SRS_DESC)
9179 sense_format = SSD_TYPE_DESC;
9181 sense_format = SSD_TYPE_FIXED;
9183 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9184 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9185 ctsio->kern_sg_entries = 0;
9186 ctsio->kern_rel_offset = 0;
9189 * struct scsi_sense_data, which is currently set to 256 bytes, is
9190 * larger than the largest allowed value for the length field in the
9191 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9193 ctsio->kern_data_len = cdb->length;
9194 ctsio->kern_total_len = cdb->length;
9197 * If we don't have a LUN, we don't have any pending sense.
9200 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9201 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9202 /* "Logical unit not supported" */
9203 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9204 /*current_error*/ 1,
9205 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9213 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9215 * Check for pending sense, and then for pending unit attentions.
9216 * Pending sense gets returned first, then pending unit attentions.
9218 mtx_lock(&lun->lun_lock);
9219 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9221 ps += initidx % CTL_MAX_INIT_PER_PORT;
9222 if (ps != NULL && ps->error_code != 0) {
9223 scsi_sense_data_type stored_format;
9226 * Check to see which sense format was used for the stored
9229 stored_format = scsi_sense_type(ps);
9232 * If the user requested a different sense format than the
9233 * one we stored, then we need to convert it to the other
9234 * format. If we're going from descriptor to fixed format
9235 * sense data, we may lose things in translation, depending
9236 * on what options were used.
9238 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9239 * for some reason we'll just copy it out as-is.
9241 if ((stored_format == SSD_TYPE_FIXED)
9242 && (sense_format == SSD_TYPE_DESC))
9243 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9244 ps, (struct scsi_sense_data_desc *)sense_ptr);
9245 else if ((stored_format == SSD_TYPE_DESC)
9246 && (sense_format == SSD_TYPE_FIXED))
9247 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9248 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9250 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9255 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9257 if (ua_type != CTL_UA_NONE)
9260 if (have_error == 0) {
9262 * Report informational exception if have one and allowed.
9264 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9266 ascq = lun->ie_ascq;
9268 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9269 /*current_error*/ 1,
9270 /*sense_key*/ SSD_KEY_NO_SENSE,
9275 mtx_unlock(&lun->lun_lock);
9279 * We report the SCSI status as OK, since the status of the command
9280 * itself is OK. We're reporting sense as parameter data.
9282 ctl_set_success(ctsio);
9283 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9284 ctsio->be_move_done = ctl_config_move_done;
9285 ctl_datamove((union ctl_io *)ctsio);
9286 return (CTL_RETVAL_COMPLETE);
9290 ctl_tur(struct ctl_scsiio *ctsio)
9293 CTL_DEBUG_PRINT(("ctl_tur\n"));
9295 ctl_set_success(ctsio);
9296 ctl_done((union ctl_io *)ctsio);
9298 return (CTL_RETVAL_COMPLETE);
9302 * SCSI VPD page 0x00, the Supported VPD Pages page.
9305 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9307 struct ctl_lun *lun = CTL_LUN(ctsio);
9308 struct scsi_vpd_supported_pages *pages;
9312 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9313 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9314 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9315 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9316 ctsio->kern_rel_offset = 0;
9317 ctsio->kern_sg_entries = 0;
9318 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9319 ctsio->kern_total_len = ctsio->kern_data_len;
9322 * The control device is always connected. The disk device, on the
9323 * other hand, may not be online all the time. Need to change this
9324 * to figure out whether the disk device is actually online or not.
9327 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9328 lun->be_lun->lun_type;
9330 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9333 /* Supported VPD pages */
9334 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9336 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9337 /* Device Identification */
9338 pages->page_list[p++] = SVPD_DEVICE_ID;
9339 /* Extended INQUIRY Data */
9340 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9341 /* Mode Page Policy */
9342 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9344 pages->page_list[p++] = SVPD_SCSI_PORTS;
9345 /* Third-party Copy */
9346 pages->page_list[p++] = SVPD_SCSI_TPC;
9347 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9349 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9350 /* Block Device Characteristics */
9351 pages->page_list[p++] = SVPD_BDC;
9352 /* Logical Block Provisioning */
9353 pages->page_list[p++] = SVPD_LBP;
9357 ctl_set_success(ctsio);
9358 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9359 ctsio->be_move_done = ctl_config_move_done;
9360 ctl_datamove((union ctl_io *)ctsio);
9361 return (CTL_RETVAL_COMPLETE);
9365 * SCSI VPD page 0x80, the Unit Serial Number page.
9368 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9370 struct ctl_lun *lun = CTL_LUN(ctsio);
9371 struct scsi_vpd_unit_serial_number *sn_ptr;
9374 data_len = 4 + CTL_SN_LEN;
9375 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9376 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9377 ctsio->kern_rel_offset = 0;
9378 ctsio->kern_sg_entries = 0;
9379 ctsio->kern_data_len = min(data_len, alloc_len);
9380 ctsio->kern_total_len = ctsio->kern_data_len;
9383 * The control device is always connected. The disk device, on the
9384 * other hand, may not be online all the time. Need to change this
9385 * to figure out whether the disk device is actually online or not.
9388 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9389 lun->be_lun->lun_type;
9391 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9393 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9394 sn_ptr->length = CTL_SN_LEN;
9396 * If we don't have a LUN, we just leave the serial number as
9400 strncpy((char *)sn_ptr->serial_num,
9401 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9403 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9405 ctl_set_success(ctsio);
9406 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9407 ctsio->be_move_done = ctl_config_move_done;
9408 ctl_datamove((union ctl_io *)ctsio);
9409 return (CTL_RETVAL_COMPLETE);
9414 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9417 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9419 struct ctl_lun *lun = CTL_LUN(ctsio);
9420 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9423 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9424 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9425 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9426 ctsio->kern_sg_entries = 0;
9427 ctsio->kern_rel_offset = 0;
9428 ctsio->kern_data_len = min(data_len, alloc_len);
9429 ctsio->kern_total_len = ctsio->kern_data_len;
9432 * The control device is always connected. The disk device, on the
9433 * other hand, may not be online all the time.
9436 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9437 lun->be_lun->lun_type;
9439 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9440 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9441 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9443 * We support head of queue, ordered and simple tags.
9445 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9447 * Volatile cache supported.
9449 eid_ptr->flags3 = SVPD_EID_V_SUP;
9452 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9453 * attention for a particular IT nexus on all LUNs once we report
9454 * it to that nexus once. This bit is required as of SPC-4.
9456 eid_ptr->flags4 = SVPD_EID_LUICLR;
9459 * We support revert to defaults (RTD) bit in MODE SELECT.
9461 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9464 * XXX KDM in order to correctly answer this, we would need
9465 * information from the SIM to determine how much sense data it
9466 * can send. So this would really be a path inquiry field, most
9467 * likely. This can be set to a maximum of 252 according to SPC-4,
9468 * but the hardware may or may not be able to support that much.
9469 * 0 just means that the maximum sense data length is not reported.
9471 eid_ptr->max_sense_length = 0;
9473 ctl_set_success(ctsio);
9474 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9475 ctsio->be_move_done = ctl_config_move_done;
9476 ctl_datamove((union ctl_io *)ctsio);
9477 return (CTL_RETVAL_COMPLETE);
9481 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9483 struct ctl_lun *lun = CTL_LUN(ctsio);
9484 struct scsi_vpd_mode_page_policy *mpp_ptr;
9487 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9488 sizeof(struct scsi_vpd_mode_page_policy_descr);
9490 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9491 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9492 ctsio->kern_rel_offset = 0;
9493 ctsio->kern_sg_entries = 0;
9494 ctsio->kern_data_len = min(data_len, alloc_len);
9495 ctsio->kern_total_len = ctsio->kern_data_len;
9498 * The control device is always connected. The disk device, on the
9499 * other hand, may not be online all the time.
9502 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9503 lun->be_lun->lun_type;
9505 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9506 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9507 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9508 mpp_ptr->descr[0].page_code = 0x3f;
9509 mpp_ptr->descr[0].subpage_code = 0xff;
9510 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9512 ctl_set_success(ctsio);
9513 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9514 ctsio->be_move_done = ctl_config_move_done;
9515 ctl_datamove((union ctl_io *)ctsio);
9516 return (CTL_RETVAL_COMPLETE);
9520 * SCSI VPD page 0x83, the Device Identification page.
9523 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9525 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9526 struct ctl_port *port = CTL_PORT(ctsio);
9527 struct ctl_lun *lun = CTL_LUN(ctsio);
9528 struct scsi_vpd_device_id *devid_ptr;
9529 struct scsi_vpd_id_descriptor *desc;
9533 data_len = sizeof(struct scsi_vpd_device_id) +
9534 sizeof(struct scsi_vpd_id_descriptor) +
9535 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9536 sizeof(struct scsi_vpd_id_descriptor) +
9537 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9538 if (lun && lun->lun_devid)
9539 data_len += lun->lun_devid->len;
9540 if (port && port->port_devid)
9541 data_len += port->port_devid->len;
9542 if (port && port->target_devid)
9543 data_len += port->target_devid->len;
9545 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9546 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9547 ctsio->kern_sg_entries = 0;
9548 ctsio->kern_rel_offset = 0;
9549 ctsio->kern_sg_entries = 0;
9550 ctsio->kern_data_len = min(data_len, alloc_len);
9551 ctsio->kern_total_len = ctsio->kern_data_len;
9554 * The control device is always connected. The disk device, on the
9555 * other hand, may not be online all the time.
9558 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9559 lun->be_lun->lun_type;
9561 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9562 devid_ptr->page_code = SVPD_DEVICE_ID;
9563 scsi_ulto2b(data_len - 4, devid_ptr->length);
9565 if (port && port->port_type == CTL_PORT_FC)
9566 proto = SCSI_PROTO_FC << 4;
9567 else if (port && port->port_type == CTL_PORT_SAS)
9568 proto = SCSI_PROTO_SAS << 4;
9569 else if (port && port->port_type == CTL_PORT_ISCSI)
9570 proto = SCSI_PROTO_ISCSI << 4;
9572 proto = SCSI_PROTO_SPI << 4;
9573 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9576 * We're using a LUN association here. i.e., this device ID is a
9577 * per-LUN identifier.
9579 if (lun && lun->lun_devid) {
9580 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9581 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9582 lun->lun_devid->len);
9586 * This is for the WWPN which is a port association.
9588 if (port && port->port_devid) {
9589 memcpy(desc, port->port_devid->data, port->port_devid->len);
9590 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9591 port->port_devid->len);
9595 * This is for the Relative Target Port(type 4h) identifier
9597 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9598 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9599 SVPD_ID_TYPE_RELTARG;
9601 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9602 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9603 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9606 * This is for the Target Port Group(type 5h) identifier
9608 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9609 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9610 SVPD_ID_TYPE_TPORTGRP;
9612 if (softc->is_single ||
9613 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9616 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9617 scsi_ulto2b(g, &desc->identifier[2]);
9618 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9619 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9622 * This is for the Target identifier
9624 if (port && port->target_devid) {
9625 memcpy(desc, port->target_devid->data, port->target_devid->len);
9628 ctl_set_success(ctsio);
9629 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9630 ctsio->be_move_done = ctl_config_move_done;
9631 ctl_datamove((union ctl_io *)ctsio);
9632 return (CTL_RETVAL_COMPLETE);
9636 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9638 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9639 struct ctl_lun *lun = CTL_LUN(ctsio);
9640 struct scsi_vpd_scsi_ports *sp;
9641 struct scsi_vpd_port_designation *pd;
9642 struct scsi_vpd_port_designation_cont *pdc;
9643 struct ctl_port *port;
9644 int data_len, num_target_ports, iid_len, id_len;
9646 num_target_ports = 0;
9649 mtx_lock(&softc->ctl_lock);
9650 STAILQ_FOREACH(port, &softc->port_list, links) {
9651 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9654 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9657 if (port->init_devid)
9658 iid_len += port->init_devid->len;
9659 if (port->port_devid)
9660 id_len += port->port_devid->len;
9662 mtx_unlock(&softc->ctl_lock);
9664 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9665 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9666 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9667 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9668 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9669 ctsio->kern_sg_entries = 0;
9670 ctsio->kern_rel_offset = 0;
9671 ctsio->kern_sg_entries = 0;
9672 ctsio->kern_data_len = min(data_len, alloc_len);
9673 ctsio->kern_total_len = ctsio->kern_data_len;
9676 * The control device is always connected. The disk device, on the
9677 * other hand, may not be online all the time. Need to change this
9678 * to figure out whether the disk device is actually online or not.
9681 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9682 lun->be_lun->lun_type;
9684 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9686 sp->page_code = SVPD_SCSI_PORTS;
9687 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9689 pd = &sp->design[0];
9691 mtx_lock(&softc->ctl_lock);
9692 STAILQ_FOREACH(port, &softc->port_list, links) {
9693 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9696 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9698 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9699 if (port->init_devid) {
9700 iid_len = port->init_devid->len;
9701 memcpy(pd->initiator_transportid,
9702 port->init_devid->data, port->init_devid->len);
9705 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9706 pdc = (struct scsi_vpd_port_designation_cont *)
9707 (&pd->initiator_transportid[iid_len]);
9708 if (port->port_devid) {
9709 id_len = port->port_devid->len;
9710 memcpy(pdc->target_port_descriptors,
9711 port->port_devid->data, port->port_devid->len);
9714 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9715 pd = (struct scsi_vpd_port_designation *)
9716 ((uint8_t *)pdc->target_port_descriptors + id_len);
9718 mtx_unlock(&softc->ctl_lock);
9720 ctl_set_success(ctsio);
9721 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9722 ctsio->be_move_done = ctl_config_move_done;
9723 ctl_datamove((union ctl_io *)ctsio);
9724 return (CTL_RETVAL_COMPLETE);
9728 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9730 struct ctl_lun *lun = CTL_LUN(ctsio);
9731 struct scsi_vpd_block_limits *bl_ptr;
9735 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9736 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9737 ctsio->kern_sg_entries = 0;
9738 ctsio->kern_rel_offset = 0;
9739 ctsio->kern_sg_entries = 0;
9740 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9741 ctsio->kern_total_len = ctsio->kern_data_len;
9744 * The control device is always connected. The disk device, on the
9745 * other hand, may not be online all the time. Need to change this
9746 * to figure out whether the disk device is actually online or not.
9749 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9750 lun->be_lun->lun_type;
9752 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9754 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9755 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9756 bl_ptr->max_cmp_write_len = 0xff;
9757 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9759 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9760 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9762 val = dnvlist_get_string(lun->be_lun->options,
9763 "unmap_max_lba", NULL);
9765 ctl_expand_number(val, &ival);
9766 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9768 val = dnvlist_get_string(lun->be_lun->options,
9769 "unmap_max_descr", NULL);
9771 ctl_expand_number(val, &ival);
9772 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9773 if (lun->be_lun->ublockexp != 0) {
9774 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9775 bl_ptr->opt_unmap_grain);
9776 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9777 bl_ptr->unmap_grain_align);
9780 scsi_ulto4b(lun->be_lun->atomicblock,
9781 bl_ptr->max_atomic_transfer_length);
9782 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9783 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9784 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9785 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9787 val = dnvlist_get_string(lun->be_lun->options,
9788 "write_same_max_lba", NULL);
9790 ctl_expand_number(val, &ival);
9791 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9794 ctl_set_success(ctsio);
9795 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9796 ctsio->be_move_done = ctl_config_move_done;
9797 ctl_datamove((union ctl_io *)ctsio);
9798 return (CTL_RETVAL_COMPLETE);
9802 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9804 struct ctl_lun *lun = CTL_LUN(ctsio);
9805 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9809 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9810 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9811 ctsio->kern_sg_entries = 0;
9812 ctsio->kern_rel_offset = 0;
9813 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9814 ctsio->kern_total_len = ctsio->kern_data_len;
9817 * The control device is always connected. The disk device, on the
9818 * other hand, may not be online all the time. Need to change this
9819 * to figure out whether the disk device is actually online or not.
9822 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9823 lun->be_lun->lun_type;
9825 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9826 bdc_ptr->page_code = SVPD_BDC;
9827 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9829 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL)
9830 i = strtol(value, NULL, 0);
9832 i = CTL_DEFAULT_ROTATION_RATE;
9833 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9835 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL)
9836 i = strtol(value, NULL, 0);
9839 bdc_ptr->wab_wac_ff = (i & 0x0f);
9840 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9842 ctl_set_success(ctsio);
9843 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9844 ctsio->be_move_done = ctl_config_move_done;
9845 ctl_datamove((union ctl_io *)ctsio);
9846 return (CTL_RETVAL_COMPLETE);
9850 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9852 struct ctl_lun *lun = CTL_LUN(ctsio);
9853 struct scsi_vpd_logical_block_prov *lbp_ptr;
9856 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9857 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9858 ctsio->kern_sg_entries = 0;
9859 ctsio->kern_rel_offset = 0;
9860 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9861 ctsio->kern_total_len = ctsio->kern_data_len;
9864 * The control device is always connected. The disk device, on the
9865 * other hand, may not be online all the time. Need to change this
9866 * to figure out whether the disk device is actually online or not.
9869 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9870 lun->be_lun->lun_type;
9872 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9874 lbp_ptr->page_code = SVPD_LBP;
9875 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9876 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9877 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9878 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9879 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9880 value = dnvlist_get_string(lun->be_lun->options,
9881 "provisioning_type", NULL);
9882 if (value != NULL) {
9883 if (strcmp(value, "resource") == 0)
9884 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9885 else if (strcmp(value, "thin") == 0)
9886 lbp_ptr->prov_type = SVPD_LBP_THIN;
9888 lbp_ptr->prov_type = SVPD_LBP_THIN;
9891 ctl_set_success(ctsio);
9892 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9893 ctsio->be_move_done = ctl_config_move_done;
9894 ctl_datamove((union ctl_io *)ctsio);
9895 return (CTL_RETVAL_COMPLETE);
9899 * INQUIRY with the EVPD bit set.
9902 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9904 struct ctl_lun *lun = CTL_LUN(ctsio);
9905 struct scsi_inquiry *cdb;
9906 int alloc_len, retval;
9908 cdb = (struct scsi_inquiry *)ctsio->cdb;
9909 alloc_len = scsi_2btoul(cdb->length);
9911 switch (cdb->page_code) {
9912 case SVPD_SUPPORTED_PAGES:
9913 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9915 case SVPD_UNIT_SERIAL_NUMBER:
9916 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9918 case SVPD_DEVICE_ID:
9919 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9921 case SVPD_EXTENDED_INQUIRY_DATA:
9922 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9924 case SVPD_MODE_PAGE_POLICY:
9925 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9927 case SVPD_SCSI_PORTS:
9928 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9931 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9933 case SVPD_BLOCK_LIMITS:
9934 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9936 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9939 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9941 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
9944 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9946 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9950 ctl_set_invalid_field(ctsio,
9956 ctl_done((union ctl_io *)ctsio);
9957 retval = CTL_RETVAL_COMPLETE;
9965 * Standard INQUIRY data.
9968 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9970 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9971 struct ctl_port *port = CTL_PORT(ctsio);
9972 struct ctl_lun *lun = CTL_LUN(ctsio);
9973 struct scsi_inquiry_data *inq_ptr;
9974 struct scsi_inquiry *cdb;
9976 uint32_t alloc_len, data_len;
9977 ctl_port_type port_type;
9979 port_type = port->port_type;
9980 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
9981 port_type = CTL_PORT_SCSI;
9983 cdb = (struct scsi_inquiry *)ctsio->cdb;
9984 alloc_len = scsi_2btoul(cdb->length);
9987 * We malloc the full inquiry data size here and fill it
9988 * in. If the user only asks for less, we'll give him
9991 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
9992 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9993 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9994 ctsio->kern_sg_entries = 0;
9995 ctsio->kern_rel_offset = 0;
9996 ctsio->kern_data_len = min(data_len, alloc_len);
9997 ctsio->kern_total_len = ctsio->kern_data_len;
10000 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10001 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10002 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10003 lun->be_lun->lun_type;
10005 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10006 lun->be_lun->lun_type;
10008 if (lun->flags & CTL_LUN_REMOVABLE)
10009 inq_ptr->dev_qual2 |= SID_RMB;
10011 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10013 /* RMB in byte 2 is 0 */
10014 inq_ptr->version = SCSI_REV_SPC5;
10017 * According to SAM-3, even if a device only supports a single
10018 * level of LUN addressing, it should still set the HISUP bit:
10020 * 4.9.1 Logical unit numbers overview
10022 * All logical unit number formats described in this standard are
10023 * hierarchical in structure even when only a single level in that
10024 * hierarchy is used. The HISUP bit shall be set to one in the
10025 * standard INQUIRY data (see SPC-2) when any logical unit number
10026 * format described in this standard is used. Non-hierarchical
10027 * formats are outside the scope of this standard.
10029 * Therefore we set the HiSup bit here.
10031 * The response format is 2, per SPC-3.
10033 inq_ptr->response_format = SID_HiSup | 2;
10035 inq_ptr->additional_length = data_len -
10036 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10037 CTL_DEBUG_PRINT(("additional_length = %d\n",
10038 inq_ptr->additional_length));
10040 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10041 if (port_type == CTL_PORT_SCSI)
10042 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10043 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10044 inq_ptr->flags = SID_CmdQue;
10045 if (port_type == CTL_PORT_SCSI)
10046 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10049 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10050 * We have 8 bytes for the vendor name, and 16 bytes for the device
10051 * name and 4 bytes for the revision.
10053 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10054 "vendor", NULL)) == NULL) {
10055 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10057 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10058 strncpy(inq_ptr->vendor, val,
10059 min(sizeof(inq_ptr->vendor), strlen(val)));
10062 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10063 sizeof(inq_ptr->product));
10064 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product",
10066 switch (lun->be_lun->lun_type) {
10068 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10069 sizeof(inq_ptr->product));
10072 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10073 sizeof(inq_ptr->product));
10076 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10077 sizeof(inq_ptr->product));
10080 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10081 sizeof(inq_ptr->product));
10085 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10086 strncpy(inq_ptr->product, val,
10087 min(sizeof(inq_ptr->product), strlen(val)));
10091 * XXX make this a macro somewhere so it automatically gets
10092 * incremented when we make changes.
10094 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10095 "revision", NULL)) == NULL) {
10096 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10098 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10099 strncpy(inq_ptr->revision, val,
10100 min(sizeof(inq_ptr->revision), strlen(val)));
10104 * For parallel SCSI, we support double transition and single
10105 * transition clocking. We also support QAS (Quick Arbitration
10106 * and Selection) and Information Unit transfers on both the
10107 * control and array devices.
10109 if (port_type == CTL_PORT_SCSI)
10110 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10113 /* SAM-6 (no version claimed) */
10114 scsi_ulto2b(0x00C0, inq_ptr->version1);
10115 /* SPC-5 (no version claimed) */
10116 scsi_ulto2b(0x05C0, inq_ptr->version2);
10117 if (port_type == CTL_PORT_FC) {
10118 /* FCP-2 ANSI INCITS.350:2003 */
10119 scsi_ulto2b(0x0917, inq_ptr->version3);
10120 } else if (port_type == CTL_PORT_SCSI) {
10121 /* SPI-4 ANSI INCITS.362:200x */
10122 scsi_ulto2b(0x0B56, inq_ptr->version3);
10123 } else if (port_type == CTL_PORT_ISCSI) {
10124 /* iSCSI (no version claimed) */
10125 scsi_ulto2b(0x0960, inq_ptr->version3);
10126 } else if (port_type == CTL_PORT_SAS) {
10127 /* SAS (no version claimed) */
10128 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10129 } else if (port_type == CTL_PORT_UMASS) {
10130 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10131 scsi_ulto2b(0x1730, inq_ptr->version3);
10135 /* SBC-4 (no version claimed) */
10136 scsi_ulto2b(0x0600, inq_ptr->version4);
10138 switch (lun->be_lun->lun_type) {
10140 /* SBC-4 (no version claimed) */
10141 scsi_ulto2b(0x0600, inq_ptr->version4);
10146 /* MMC-6 (no version claimed) */
10147 scsi_ulto2b(0x04E0, inq_ptr->version4);
10154 ctl_set_success(ctsio);
10155 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10156 ctsio->be_move_done = ctl_config_move_done;
10157 ctl_datamove((union ctl_io *)ctsio);
10158 return (CTL_RETVAL_COMPLETE);
10162 ctl_inquiry(struct ctl_scsiio *ctsio)
10164 struct scsi_inquiry *cdb;
10167 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10169 cdb = (struct scsi_inquiry *)ctsio->cdb;
10170 if (cdb->byte2 & SI_EVPD)
10171 retval = ctl_inquiry_evpd(ctsio);
10172 else if (cdb->page_code == 0)
10173 retval = ctl_inquiry_std(ctsio);
10175 ctl_set_invalid_field(ctsio,
10181 ctl_done((union ctl_io *)ctsio);
10182 return (CTL_RETVAL_COMPLETE);
10189 ctl_get_config(struct ctl_scsiio *ctsio)
10191 struct ctl_lun *lun = CTL_LUN(ctsio);
10192 struct scsi_get_config_header *hdr;
10193 struct scsi_get_config_feature *feature;
10194 struct scsi_get_config *cdb;
10195 uint32_t alloc_len, data_len;
10198 cdb = (struct scsi_get_config *)ctsio->cdb;
10199 rt = (cdb->rt & SGC_RT_MASK);
10200 starting = scsi_2btoul(cdb->starting_feature);
10201 alloc_len = scsi_2btoul(cdb->length);
10203 data_len = sizeof(struct scsi_get_config_header) +
10204 sizeof(struct scsi_get_config_feature) + 8 +
10205 sizeof(struct scsi_get_config_feature) + 8 +
10206 sizeof(struct scsi_get_config_feature) + 4 +
10207 sizeof(struct scsi_get_config_feature) + 4 +
10208 sizeof(struct scsi_get_config_feature) + 8 +
10209 sizeof(struct scsi_get_config_feature) +
10210 sizeof(struct scsi_get_config_feature) + 4 +
10211 sizeof(struct scsi_get_config_feature) + 4 +
10212 sizeof(struct scsi_get_config_feature) + 4 +
10213 sizeof(struct scsi_get_config_feature) + 4 +
10214 sizeof(struct scsi_get_config_feature) + 4 +
10215 sizeof(struct scsi_get_config_feature) + 4;
10216 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10217 ctsio->kern_sg_entries = 0;
10218 ctsio->kern_rel_offset = 0;
10220 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10221 if (lun->flags & CTL_LUN_NO_MEDIA)
10222 scsi_ulto2b(0x0000, hdr->current_profile);
10224 scsi_ulto2b(0x0010, hdr->current_profile);
10225 feature = (struct scsi_get_config_feature *)(hdr + 1);
10227 if (starting > 0x003b)
10229 if (starting > 0x003a)
10231 if (starting > 0x002b)
10233 if (starting > 0x002a)
10235 if (starting > 0x001f)
10237 if (starting > 0x001e)
10239 if (starting > 0x001d)
10241 if (starting > 0x0010)
10243 if (starting > 0x0003)
10245 if (starting > 0x0002)
10247 if (starting > 0x0001)
10249 if (starting > 0x0000)
10253 scsi_ulto2b(0x0000, feature->feature_code);
10254 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10255 feature->add_length = 8;
10256 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10257 feature->feature_data[2] = 0x00;
10258 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10259 feature->feature_data[6] = 0x01;
10260 feature = (struct scsi_get_config_feature *)
10261 &feature->feature_data[feature->add_length];
10264 scsi_ulto2b(0x0001, feature->feature_code);
10265 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10266 feature->add_length = 8;
10267 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10268 feature->feature_data[4] = 0x03;
10269 feature = (struct scsi_get_config_feature *)
10270 &feature->feature_data[feature->add_length];
10273 scsi_ulto2b(0x0002, feature->feature_code);
10274 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10275 feature->add_length = 4;
10276 feature->feature_data[0] = 0x02;
10277 feature = (struct scsi_get_config_feature *)
10278 &feature->feature_data[feature->add_length];
10280 f3: /* Removable Medium */
10281 scsi_ulto2b(0x0003, feature->feature_code);
10282 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10283 feature->add_length = 4;
10284 feature->feature_data[0] = 0x39;
10285 feature = (struct scsi_get_config_feature *)
10286 &feature->feature_data[feature->add_length];
10288 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10291 f10: /* Random Read */
10292 scsi_ulto2b(0x0010, feature->feature_code);
10293 feature->flags = 0x00;
10294 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10295 feature->flags |= SGC_F_CURRENT;
10296 feature->add_length = 8;
10297 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10298 scsi_ulto2b(1, &feature->feature_data[4]);
10299 feature->feature_data[6] = 0x00;
10300 feature = (struct scsi_get_config_feature *)
10301 &feature->feature_data[feature->add_length];
10303 f1d: /* Multi-Read */
10304 scsi_ulto2b(0x001D, feature->feature_code);
10305 feature->flags = 0x00;
10306 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10307 feature->flags |= SGC_F_CURRENT;
10308 feature->add_length = 0;
10309 feature = (struct scsi_get_config_feature *)
10310 &feature->feature_data[feature->add_length];
10313 scsi_ulto2b(0x001E, feature->feature_code);
10314 feature->flags = 0x00;
10315 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10316 feature->flags |= SGC_F_CURRENT;
10317 feature->add_length = 4;
10318 feature->feature_data[0] = 0x00;
10319 feature = (struct scsi_get_config_feature *)
10320 &feature->feature_data[feature->add_length];
10322 f1f: /* DVD Read */
10323 scsi_ulto2b(0x001F, feature->feature_code);
10324 feature->flags = 0x08;
10325 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10326 feature->flags |= SGC_F_CURRENT;
10327 feature->add_length = 4;
10328 feature->feature_data[0] = 0x01;
10329 feature->feature_data[2] = 0x03;
10330 feature = (struct scsi_get_config_feature *)
10331 &feature->feature_data[feature->add_length];
10334 scsi_ulto2b(0x002A, feature->feature_code);
10335 feature->flags = 0x04;
10336 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10337 feature->flags |= SGC_F_CURRENT;
10338 feature->add_length = 4;
10339 feature->feature_data[0] = 0x00;
10340 feature->feature_data[1] = 0x00;
10341 feature = (struct scsi_get_config_feature *)
10342 &feature->feature_data[feature->add_length];
10345 scsi_ulto2b(0x002B, feature->feature_code);
10346 feature->flags = 0x00;
10347 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10348 feature->flags |= SGC_F_CURRENT;
10349 feature->add_length = 4;
10350 feature->feature_data[0] = 0x00;
10351 feature = (struct scsi_get_config_feature *)
10352 &feature->feature_data[feature->add_length];
10354 f3a: /* DVD+RW Dual Layer */
10355 scsi_ulto2b(0x003A, feature->feature_code);
10356 feature->flags = 0x00;
10357 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10358 feature->flags |= SGC_F_CURRENT;
10359 feature->add_length = 4;
10360 feature->feature_data[0] = 0x00;
10361 feature->feature_data[1] = 0x00;
10362 feature = (struct scsi_get_config_feature *)
10363 &feature->feature_data[feature->add_length];
10365 f3b: /* DVD+R Dual Layer */
10366 scsi_ulto2b(0x003B, feature->feature_code);
10367 feature->flags = 0x00;
10368 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10369 feature->flags |= SGC_F_CURRENT;
10370 feature->add_length = 4;
10371 feature->feature_data[0] = 0x00;
10372 feature = (struct scsi_get_config_feature *)
10373 &feature->feature_data[feature->add_length];
10376 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10377 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10378 feature = (struct scsi_get_config_feature *)(hdr + 1);
10379 if (scsi_2btoul(feature->feature_code) == starting)
10380 feature = (struct scsi_get_config_feature *)
10381 &feature->feature_data[feature->add_length];
10382 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10384 scsi_ulto4b(data_len - 4, hdr->data_length);
10385 ctsio->kern_data_len = min(data_len, alloc_len);
10386 ctsio->kern_total_len = ctsio->kern_data_len;
10388 ctl_set_success(ctsio);
10389 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10390 ctsio->be_move_done = ctl_config_move_done;
10391 ctl_datamove((union ctl_io *)ctsio);
10392 return (CTL_RETVAL_COMPLETE);
10396 ctl_get_event_status(struct ctl_scsiio *ctsio)
10398 struct scsi_get_event_status_header *hdr;
10399 struct scsi_get_event_status *cdb;
10400 uint32_t alloc_len, data_len;
10402 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10403 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10404 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10405 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10406 ctl_done((union ctl_io *)ctsio);
10407 return (CTL_RETVAL_COMPLETE);
10409 alloc_len = scsi_2btoul(cdb->length);
10411 data_len = sizeof(struct scsi_get_event_status_header);
10412 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10413 ctsio->kern_sg_entries = 0;
10414 ctsio->kern_rel_offset = 0;
10415 ctsio->kern_data_len = min(data_len, alloc_len);
10416 ctsio->kern_total_len = ctsio->kern_data_len;
10418 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10419 scsi_ulto2b(0, hdr->descr_length);
10420 hdr->nea_class = SGESN_NEA;
10421 hdr->supported_class = 0;
10423 ctl_set_success(ctsio);
10424 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10425 ctsio->be_move_done = ctl_config_move_done;
10426 ctl_datamove((union ctl_io *)ctsio);
10427 return (CTL_RETVAL_COMPLETE);
10431 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10433 struct scsi_mechanism_status_header *hdr;
10434 struct scsi_mechanism_status *cdb;
10435 uint32_t alloc_len, data_len;
10437 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10438 alloc_len = scsi_2btoul(cdb->length);
10440 data_len = sizeof(struct scsi_mechanism_status_header);
10441 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10442 ctsio->kern_sg_entries = 0;
10443 ctsio->kern_rel_offset = 0;
10444 ctsio->kern_data_len = min(data_len, alloc_len);
10445 ctsio->kern_total_len = ctsio->kern_data_len;
10447 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10448 hdr->state1 = 0x00;
10449 hdr->state2 = 0xe0;
10450 scsi_ulto3b(0, hdr->lba);
10451 hdr->slots_num = 0;
10452 scsi_ulto2b(0, hdr->slots_length);
10454 ctl_set_success(ctsio);
10455 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10456 ctsio->be_move_done = ctl_config_move_done;
10457 ctl_datamove((union ctl_io *)ctsio);
10458 return (CTL_RETVAL_COMPLETE);
10462 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10467 buf[1] = bin2bcd((lba / 75) / 60);
10468 buf[2] = bin2bcd((lba / 75) % 60);
10469 buf[3] = bin2bcd(lba % 75);
10473 ctl_read_toc(struct ctl_scsiio *ctsio)
10475 struct ctl_lun *lun = CTL_LUN(ctsio);
10476 struct scsi_read_toc_hdr *hdr;
10477 struct scsi_read_toc_type01_descr *descr;
10478 struct scsi_read_toc *cdb;
10479 uint32_t alloc_len, data_len;
10482 cdb = (struct scsi_read_toc *)ctsio->cdb;
10483 msf = (cdb->byte2 & CD_MSF) != 0;
10484 format = cdb->format;
10485 alloc_len = scsi_2btoul(cdb->data_len);
10487 data_len = sizeof(struct scsi_read_toc_hdr);
10489 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10491 data_len += sizeof(struct scsi_read_toc_type01_descr);
10492 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10493 ctsio->kern_sg_entries = 0;
10494 ctsio->kern_rel_offset = 0;
10495 ctsio->kern_data_len = min(data_len, alloc_len);
10496 ctsio->kern_total_len = ctsio->kern_data_len;
10498 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10500 scsi_ulto2b(0x12, hdr->data_length);
10503 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10504 descr->addr_ctl = 0x14;
10505 descr->track_number = 1;
10507 ctl_ultomsf(0, descr->track_start);
10509 scsi_ulto4b(0, descr->track_start);
10511 descr->addr_ctl = 0x14;
10512 descr->track_number = 0xaa;
10514 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10516 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10518 scsi_ulto2b(0x0a, hdr->data_length);
10521 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10522 descr->addr_ctl = 0x14;
10523 descr->track_number = 1;
10525 ctl_ultomsf(0, descr->track_start);
10527 scsi_ulto4b(0, descr->track_start);
10530 ctl_set_success(ctsio);
10531 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10532 ctsio->be_move_done = ctl_config_move_done;
10533 ctl_datamove((union ctl_io *)ctsio);
10534 return (CTL_RETVAL_COMPLETE);
10538 * For known CDB types, parse the LBA and length.
10541 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10543 if (io->io_hdr.io_type != CTL_IO_SCSI)
10546 switch (io->scsiio.cdb[0]) {
10547 case COMPARE_AND_WRITE: {
10548 struct scsi_compare_and_write *cdb;
10550 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10552 *lba = scsi_8btou64(cdb->addr);
10553 *len = cdb->length;
10558 struct scsi_rw_6 *cdb;
10560 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10562 *lba = scsi_3btoul(cdb->addr);
10563 /* only 5 bits are valid in the most significant address byte */
10565 *len = cdb->length;
10570 struct scsi_rw_10 *cdb;
10572 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10574 *lba = scsi_4btoul(cdb->addr);
10575 *len = scsi_2btoul(cdb->length);
10578 case WRITE_VERIFY_10: {
10579 struct scsi_write_verify_10 *cdb;
10581 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10583 *lba = scsi_4btoul(cdb->addr);
10584 *len = scsi_2btoul(cdb->length);
10589 struct scsi_rw_12 *cdb;
10591 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10593 *lba = scsi_4btoul(cdb->addr);
10594 *len = scsi_4btoul(cdb->length);
10597 case WRITE_VERIFY_12: {
10598 struct scsi_write_verify_12 *cdb;
10600 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10602 *lba = scsi_4btoul(cdb->addr);
10603 *len = scsi_4btoul(cdb->length);
10608 struct scsi_rw_16 *cdb;
10610 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10612 *lba = scsi_8btou64(cdb->addr);
10613 *len = scsi_4btoul(cdb->length);
10616 case WRITE_ATOMIC_16: {
10617 struct scsi_write_atomic_16 *cdb;
10619 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10621 *lba = scsi_8btou64(cdb->addr);
10622 *len = scsi_2btoul(cdb->length);
10625 case WRITE_VERIFY_16: {
10626 struct scsi_write_verify_16 *cdb;
10628 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10630 *lba = scsi_8btou64(cdb->addr);
10631 *len = scsi_4btoul(cdb->length);
10634 case WRITE_SAME_10: {
10635 struct scsi_write_same_10 *cdb;
10637 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10639 *lba = scsi_4btoul(cdb->addr);
10640 *len = scsi_2btoul(cdb->length);
10643 case WRITE_SAME_16: {
10644 struct scsi_write_same_16 *cdb;
10646 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10648 *lba = scsi_8btou64(cdb->addr);
10649 *len = scsi_4btoul(cdb->length);
10653 struct scsi_verify_10 *cdb;
10655 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10657 *lba = scsi_4btoul(cdb->addr);
10658 *len = scsi_2btoul(cdb->length);
10662 struct scsi_verify_12 *cdb;
10664 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10666 *lba = scsi_4btoul(cdb->addr);
10667 *len = scsi_4btoul(cdb->length);
10671 struct scsi_verify_16 *cdb;
10673 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10675 *lba = scsi_8btou64(cdb->addr);
10676 *len = scsi_4btoul(cdb->length);
10684 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10685 struct scsi_get_lba_status *cdb;
10687 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10688 *lba = scsi_8btou64(cdb->addr);
10694 break; /* NOTREACHED */
10701 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10704 uint64_t endlba1, endlba2;
10706 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10707 endlba2 = lba2 + len2 - 1;
10709 if ((endlba1 < lba2) || (endlba2 < lba1))
10710 return (CTL_ACTION_PASS);
10712 return (CTL_ACTION_BLOCK);
10716 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10718 struct ctl_ptr_len_flags *ptrlen;
10719 struct scsi_unmap_desc *buf, *end, *range;
10723 /* If not UNMAP -- go other way. */
10724 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10725 io->scsiio.cdb[0] != UNMAP)
10726 return (CTL_ACTION_ERROR);
10728 /* If UNMAP without data -- block and wait for data. */
10729 ptrlen = (struct ctl_ptr_len_flags *)
10730 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10731 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10732 ptrlen->ptr == NULL)
10733 return (CTL_ACTION_BLOCK);
10735 /* UNMAP with data -- check for collision. */
10736 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10737 end = buf + ptrlen->len / sizeof(*buf);
10738 for (range = buf; range < end; range++) {
10739 lba = scsi_8btou64(range->lba);
10740 len = scsi_4btoul(range->length);
10741 if ((lba < lba2 + len2) && (lba + len > lba2))
10742 return (CTL_ACTION_BLOCK);
10744 return (CTL_ACTION_PASS);
10748 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10750 uint64_t lba1, lba2;
10751 uint64_t len1, len2;
10754 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10755 return (CTL_ACTION_ERROR);
10757 retval = ctl_extent_check_unmap(io1, lba2, len2);
10758 if (retval != CTL_ACTION_ERROR)
10761 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10762 return (CTL_ACTION_ERROR);
10764 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10766 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10770 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10772 uint64_t lba1, lba2;
10773 uint64_t len1, len2;
10775 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10776 return (CTL_ACTION_PASS);
10777 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10778 return (CTL_ACTION_ERROR);
10779 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10780 return (CTL_ACTION_ERROR);
10782 if (lba1 + len1 == lba2)
10783 return (CTL_ACTION_BLOCK);
10784 return (CTL_ACTION_PASS);
10788 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10789 union ctl_io *ooa_io)
10791 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10792 const ctl_serialize_action *serialize_row;
10795 * The initiator attempted multiple untagged commands at the same
10796 * time. Can't do that.
10798 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10799 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10800 && ((pending_io->io_hdr.nexus.targ_port ==
10801 ooa_io->io_hdr.nexus.targ_port)
10802 && (pending_io->io_hdr.nexus.initid ==
10803 ooa_io->io_hdr.nexus.initid))
10804 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10805 CTL_FLAG_STATUS_SENT)) == 0))
10806 return (CTL_ACTION_OVERLAP);
10809 * The initiator attempted to send multiple tagged commands with
10810 * the same ID. (It's fine if different initiators have the same
10813 * Even if all of those conditions are true, we don't kill the I/O
10814 * if the command ahead of us has been aborted. We won't end up
10815 * sending it to the FETD, and it's perfectly legal to resend a
10816 * command with the same tag number as long as the previous
10817 * instance of this tag number has been aborted somehow.
10819 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10820 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10821 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10822 && ((pending_io->io_hdr.nexus.targ_port ==
10823 ooa_io->io_hdr.nexus.targ_port)
10824 && (pending_io->io_hdr.nexus.initid ==
10825 ooa_io->io_hdr.nexus.initid))
10826 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10827 CTL_FLAG_STATUS_SENT)) == 0))
10828 return (CTL_ACTION_OVERLAP_TAG);
10831 * If we get a head of queue tag, SAM-3 says that we should
10832 * immediately execute it.
10834 * What happens if this command would normally block for some other
10835 * reason? e.g. a request sense with a head of queue tag
10836 * immediately after a write. Normally that would block, but this
10837 * will result in its getting executed immediately...
10839 * We currently return "pass" instead of "skip", so we'll end up
10840 * going through the rest of the queue to check for overlapped tags.
10842 * XXX KDM check for other types of blockage first??
10844 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10845 return (CTL_ACTION_PASS);
10848 * Ordered tags have to block until all items ahead of them
10849 * have completed. If we get called with an ordered tag, we always
10850 * block, if something else is ahead of us in the queue.
10852 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10853 return (CTL_ACTION_BLOCK);
10856 * Simple tags get blocked until all head of queue and ordered tags
10857 * ahead of them have completed. I'm lumping untagged commands in
10858 * with simple tags here. XXX KDM is that the right thing to do?
10860 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10861 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10862 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10863 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10864 return (CTL_ACTION_BLOCK);
10866 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10867 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
10868 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
10869 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
10870 pending_io->scsiio.cdb[1], pending_io));
10871 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10872 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
10873 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
10874 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
10875 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
10876 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
10877 ooa_io->scsiio.cdb[1], ooa_io));
10879 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10881 switch (serialize_row[pending_entry->seridx]) {
10882 case CTL_SER_BLOCK:
10883 return (CTL_ACTION_BLOCK);
10884 case CTL_SER_EXTENT:
10885 return (ctl_extent_check(ooa_io, pending_io,
10886 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10887 case CTL_SER_EXTENTOPT:
10888 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10889 SCP_QUEUE_ALG_UNRESTRICTED)
10890 return (ctl_extent_check(ooa_io, pending_io,
10892 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10893 return (CTL_ACTION_PASS);
10894 case CTL_SER_EXTENTSEQ:
10895 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10896 return (ctl_extent_check_seq(ooa_io, pending_io));
10897 return (CTL_ACTION_PASS);
10899 return (CTL_ACTION_PASS);
10900 case CTL_SER_BLOCKOPT:
10901 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10902 SCP_QUEUE_ALG_UNRESTRICTED)
10903 return (CTL_ACTION_BLOCK);
10904 return (CTL_ACTION_PASS);
10906 return (CTL_ACTION_SKIP);
10908 panic("%s: Invalid serialization value %d for %d => %d",
10909 __func__, serialize_row[pending_entry->seridx],
10910 pending_entry->seridx, ooa_entry->seridx);
10913 return (CTL_ACTION_ERROR);
10917 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10919 * - pending_io is generally either incoming, or on the blocked queue
10920 * - starting I/O is the I/O we want to start the check with.
10923 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10924 union ctl_io *starting_io)
10926 union ctl_io *ooa_io;
10929 mtx_assert(&lun->lun_lock, MA_OWNED);
10932 * Run back along the OOA queue, starting with the current
10933 * blocked I/O and going through every I/O before it on the
10934 * queue. If starting_io is NULL, we'll just end up returning
10937 for (ooa_io = starting_io; ooa_io != NULL;
10938 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10942 * This routine just checks to see whether
10943 * cur_blocked is blocked by ooa_io, which is ahead
10944 * of it in the queue. It doesn't queue/dequeue
10947 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10949 case CTL_ACTION_BLOCK:
10950 case CTL_ACTION_OVERLAP:
10951 case CTL_ACTION_OVERLAP_TAG:
10952 case CTL_ACTION_SKIP:
10953 case CTL_ACTION_ERROR:
10955 break; /* NOTREACHED */
10956 case CTL_ACTION_PASS:
10959 panic("%s: Invalid action %d\n", __func__, action);
10963 return (CTL_ACTION_PASS);
10968 * - An I/O has just completed, and has been removed from the per-LUN OOA
10969 * queue, so some items on the blocked queue may now be unblocked.
10972 ctl_check_blocked(struct ctl_lun *lun)
10974 struct ctl_softc *softc = lun->ctl_softc;
10975 union ctl_io *cur_blocked, *next_blocked;
10977 mtx_assert(&lun->lun_lock, MA_OWNED);
10980 * Run forward from the head of the blocked queue, checking each
10981 * entry against the I/Os prior to it on the OOA queue to see if
10982 * there is still any blockage.
10984 * We cannot use the TAILQ_FOREACH() macro, because it can't deal
10985 * with our removing a variable on it while it is traversing the
10988 for (cur_blocked = (union ctl_io *)TAILQ_FIRST(&lun->blocked_queue);
10989 cur_blocked != NULL; cur_blocked = next_blocked) {
10990 union ctl_io *prev_ooa;
10993 next_blocked = (union ctl_io *)TAILQ_NEXT(&cur_blocked->io_hdr,
10996 prev_ooa = (union ctl_io *)TAILQ_PREV(&cur_blocked->io_hdr,
10997 ctl_ooaq, ooa_links);
11000 * If cur_blocked happens to be the first item in the OOA
11001 * queue now, prev_ooa will be NULL, and the action
11002 * returned will just be CTL_ACTION_PASS.
11004 action = ctl_check_ooa(lun, cur_blocked, prev_ooa);
11007 case CTL_ACTION_BLOCK:
11008 /* Nothing to do here, still blocked */
11010 case CTL_ACTION_OVERLAP:
11011 case CTL_ACTION_OVERLAP_TAG:
11013 * This shouldn't happen! In theory we've already
11014 * checked this command for overlap...
11017 case CTL_ACTION_PASS:
11018 case CTL_ACTION_SKIP: {
11019 const struct ctl_cmd_entry *entry;
11022 * The skip case shouldn't happen, this transaction
11023 * should have never made it onto the blocked queue.
11026 * This I/O is no longer blocked, we can remove it
11027 * from the blocked queue. Since this is a TAILQ
11028 * (doubly linked list), we can do O(1) removals
11029 * from any place on the list.
11031 TAILQ_REMOVE(&lun->blocked_queue, &cur_blocked->io_hdr,
11033 cur_blocked->io_hdr.flags &= ~CTL_FLAG_BLOCKED;
11035 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
11036 (cur_blocked->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)){
11038 * Need to send IO back to original side to
11041 union ctl_ha_msg msg_info;
11043 cur_blocked->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11044 msg_info.hdr.original_sc =
11045 cur_blocked->io_hdr.remote_io;
11046 msg_info.hdr.serializing_sc = cur_blocked;
11047 msg_info.hdr.msg_type = CTL_MSG_R2R;
11048 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11049 sizeof(msg_info.hdr), M_NOWAIT);
11052 entry = ctl_get_cmd_entry(&cur_blocked->scsiio, NULL);
11055 * Check this I/O for LUN state changes that may
11056 * have happened while this command was blocked.
11057 * The LUN state may have been changed by a command
11058 * ahead of us in the queue, so we need to re-check
11059 * for any states that can be caused by SCSI
11062 if (ctl_scsiio_lun_check(lun, entry,
11063 &cur_blocked->scsiio) == 0) {
11064 cur_blocked->io_hdr.flags |=
11065 CTL_FLAG_IS_WAS_ON_RTR;
11066 ctl_enqueue_rtr(cur_blocked);
11068 ctl_done(cur_blocked);
11073 * This probably shouldn't happen -- we shouldn't
11074 * get CTL_ACTION_ERROR, or anything else.
11080 return (CTL_RETVAL_COMPLETE);
11084 * This routine (with one exception) checks LUN flags that can be set by
11085 * commands ahead of us in the OOA queue. These flags have to be checked
11086 * when a command initially comes in, and when we pull a command off the
11087 * blocked queue and are preparing to execute it. The reason we have to
11088 * check these flags for commands on the blocked queue is that the LUN
11089 * state may have been changed by a command ahead of us while we're on the
11092 * Ordering is somewhat important with these checks, so please pay
11093 * careful attention to the placement of any new checks.
11096 ctl_scsiio_lun_check(struct ctl_lun *lun,
11097 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11099 struct ctl_softc *softc = lun->ctl_softc;
11105 mtx_assert(&lun->lun_lock, MA_OWNED);
11108 * If this shelf is a secondary shelf controller, we may have to
11109 * reject some commands disallowed by HA mode and link state.
11111 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11112 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11113 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11114 ctl_set_lun_unavail(ctsio);
11118 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11119 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11120 ctl_set_lun_transit(ctsio);
11124 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11125 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11126 ctl_set_lun_standby(ctsio);
11131 /* The rest of checks are only done on executing side */
11132 if (softc->ha_mode == CTL_HA_MODE_XFER)
11136 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11138 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11139 ctl_set_hw_write_protected(ctsio);
11143 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11144 ctl_set_sense(ctsio, /*current_error*/ 1,
11145 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11146 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11153 * Check for a reservation conflict. If this command isn't allowed
11154 * even on reserved LUNs, and if this initiator isn't the one who
11155 * reserved us, reject the command with a reservation conflict.
11157 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11158 if ((lun->flags & CTL_LUN_RESERVED)
11159 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11160 if (lun->res_idx != residx) {
11161 ctl_set_reservation_conflict(ctsio);
11167 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11168 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11169 /* No reservation or command is allowed. */;
11170 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11171 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11172 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11173 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11174 /* The command is allowed for Write Exclusive resv. */;
11177 * if we aren't registered or it's a res holder type
11178 * reservation and this isn't the res holder then set a
11181 if (ctl_get_prkey(lun, residx) == 0 ||
11182 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11183 ctl_set_reservation_conflict(ctsio);
11189 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11190 if (lun->flags & CTL_LUN_EJECTED)
11191 ctl_set_lun_ejected(ctsio);
11192 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11193 if (lun->flags & CTL_LUN_REMOVABLE)
11194 ctl_set_lun_no_media(ctsio);
11196 ctl_set_lun_int_reqd(ctsio);
11197 } else if (lun->flags & CTL_LUN_STOPPED)
11198 ctl_set_lun_stopped(ctsio);
11210 ctl_failover_io(union ctl_io *io, int have_lock)
11212 ctl_set_busy(&io->scsiio);
11217 ctl_failover_lun(union ctl_io *rio)
11219 struct ctl_softc *softc = CTL_SOFTC(rio);
11220 struct ctl_lun *lun;
11221 struct ctl_io_hdr *io, *next_io;
11224 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11225 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11227 /* Find and lock the LUN. */
11228 mtx_lock(&softc->ctl_lock);
11229 if (targ_lun > ctl_max_luns ||
11230 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11231 mtx_unlock(&softc->ctl_lock);
11234 mtx_lock(&lun->lun_lock);
11235 mtx_unlock(&softc->ctl_lock);
11236 if (lun->flags & CTL_LUN_DISABLED) {
11237 mtx_unlock(&lun->lun_lock);
11241 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11242 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11243 /* We are master */
11244 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11245 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11246 io->flags |= CTL_FLAG_ABORT;
11247 io->flags |= CTL_FLAG_FAILOVER;
11248 } else { /* This can be only due to DATAMOVE */
11249 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11250 io->flags &= ~CTL_FLAG_DMA_INPROG;
11251 io->flags |= CTL_FLAG_IO_ACTIVE;
11252 io->port_status = 31340;
11253 ctl_enqueue_isc((union ctl_io *)io);
11257 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11258 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11259 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11260 io->flags |= CTL_FLAG_FAILOVER;
11262 ctl_set_busy(&((union ctl_io *)io)->
11264 ctl_done((union ctl_io *)io);
11268 } else { /* SERIALIZE modes */
11269 TAILQ_FOREACH_SAFE(io, &lun->blocked_queue, blocked_links,
11271 /* We are master */
11272 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11273 TAILQ_REMOVE(&lun->blocked_queue, io,
11275 io->flags &= ~CTL_FLAG_BLOCKED;
11276 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11277 ctl_free_io((union ctl_io *)io);
11280 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11281 /* We are master */
11282 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11283 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11284 ctl_free_io((union ctl_io *)io);
11287 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11288 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11289 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11290 ctl_set_busy(&((union ctl_io *)io)->
11292 ctl_done((union ctl_io *)io);
11296 ctl_check_blocked(lun);
11298 mtx_unlock(&lun->lun_lock);
11302 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11304 struct ctl_lun *lun;
11305 const struct ctl_cmd_entry *entry;
11306 uint32_t initidx, targ_lun;
11310 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11311 if (targ_lun < ctl_max_luns)
11312 lun = softc->ctl_luns[targ_lun];
11315 * If the LUN is invalid, pretend that it doesn't exist.
11316 * It will go away as soon as all pending I/O has been
11319 mtx_lock(&lun->lun_lock);
11320 if (lun->flags & CTL_LUN_DISABLED) {
11321 mtx_unlock(&lun->lun_lock);
11325 CTL_LUN(ctsio) = lun;
11327 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11330 * Every I/O goes into the OOA queue for a particular LUN,
11331 * and stays there until completion.
11334 if (TAILQ_EMPTY(&lun->ooa_queue))
11335 lun->idle_time += getsbinuptime() - lun->last_busy;
11337 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11340 /* Get command entry and return error if it is unsuppotyed. */
11341 entry = ctl_validate_command(ctsio);
11342 if (entry == NULL) {
11344 mtx_unlock(&lun->lun_lock);
11348 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11349 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11352 * Check to see whether we can send this command to LUNs that don't
11353 * exist. This should pretty much only be the case for inquiry
11354 * and request sense. Further checks, below, really require having
11355 * a LUN, so we can't really check the command anymore. Just put
11356 * it on the rtr queue.
11359 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11360 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11361 ctl_enqueue_rtr((union ctl_io *)ctsio);
11365 ctl_set_unsupported_lun(ctsio);
11366 ctl_done((union ctl_io *)ctsio);
11367 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11371 * Make sure we support this particular command on this LUN.
11372 * e.g., we don't support writes to the control LUN.
11374 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11375 mtx_unlock(&lun->lun_lock);
11376 ctl_set_invalid_opcode(ctsio);
11377 ctl_done((union ctl_io *)ctsio);
11382 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11385 * If we've got a request sense, it'll clear the contingent
11386 * allegiance condition. Otherwise, if we have a CA condition for
11387 * this initiator, clear it, because it sent down a command other
11388 * than request sense.
11390 if (ctsio->cdb[0] != REQUEST_SENSE) {
11391 struct scsi_sense_data *ps;
11393 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11395 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11399 * If the command has this flag set, it handles its own unit
11400 * attention reporting, we shouldn't do anything. Otherwise we
11401 * check for any pending unit attentions, and send them back to the
11402 * initiator. We only do this when a command initially comes in,
11403 * not when we pull it off the blocked queue.
11405 * According to SAM-3, section 5.3.2, the order that things get
11406 * presented back to the host is basically unit attentions caused
11407 * by some sort of reset event, busy status, reservation conflicts
11408 * or task set full, and finally any other status.
11410 * One issue here is that some of the unit attentions we report
11411 * don't fall into the "reset" category (e.g. "reported luns data
11412 * has changed"). So reporting it here, before the reservation
11413 * check, may be technically wrong. I guess the only thing to do
11414 * would be to check for and report the reset events here, and then
11415 * check for the other unit attention types after we check for a
11416 * reservation conflict.
11418 * XXX KDM need to fix this
11420 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11421 ctl_ua_type ua_type;
11422 u_int sense_len = 0;
11424 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11425 &sense_len, SSD_TYPE_NONE);
11426 if (ua_type != CTL_UA_NONE) {
11427 mtx_unlock(&lun->lun_lock);
11428 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11429 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11430 ctsio->sense_len = sense_len;
11431 ctl_done((union ctl_io *)ctsio);
11437 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11438 mtx_unlock(&lun->lun_lock);
11439 ctl_done((union ctl_io *)ctsio);
11444 * XXX CHD this is where we want to send IO to other side if
11445 * this LUN is secondary on this SC. We will need to make a copy
11446 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11447 * the copy we send as FROM_OTHER.
11448 * We also need to stuff the address of the original IO so we can
11449 * find it easily. Something similar will need be done on the other
11450 * side so when we are done we can find the copy.
11452 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11453 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11454 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11455 union ctl_ha_msg msg_info;
11458 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11459 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11460 mtx_unlock(&lun->lun_lock);
11462 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11463 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11464 msg_info.hdr.serializing_sc = NULL;
11465 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11466 msg_info.scsi.tag_num = ctsio->tag_num;
11467 msg_info.scsi.tag_type = ctsio->tag_type;
11468 msg_info.scsi.cdb_len = ctsio->cdb_len;
11469 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11471 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11472 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11473 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11474 ctl_set_busy(ctsio);
11475 ctl_done((union ctl_io *)ctsio);
11481 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio,
11482 (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr,
11483 ctl_ooaq, ooa_links))) {
11484 case CTL_ACTION_BLOCK:
11485 ctsio->io_hdr.flags |= CTL_FLAG_BLOCKED;
11486 TAILQ_INSERT_TAIL(&lun->blocked_queue, &ctsio->io_hdr,
11488 mtx_unlock(&lun->lun_lock);
11490 case CTL_ACTION_PASS:
11491 case CTL_ACTION_SKIP:
11492 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11493 mtx_unlock(&lun->lun_lock);
11494 ctl_enqueue_rtr((union ctl_io *)ctsio);
11496 case CTL_ACTION_OVERLAP:
11497 mtx_unlock(&lun->lun_lock);
11498 ctl_set_overlapped_cmd(ctsio);
11499 ctl_done((union ctl_io *)ctsio);
11501 case CTL_ACTION_OVERLAP_TAG:
11502 mtx_unlock(&lun->lun_lock);
11503 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11504 ctl_done((union ctl_io *)ctsio);
11506 case CTL_ACTION_ERROR:
11508 mtx_unlock(&lun->lun_lock);
11509 ctl_set_internal_failure(ctsio,
11511 /*retry_count*/ 0);
11512 ctl_done((union ctl_io *)ctsio);
11518 const struct ctl_cmd_entry *
11519 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11521 const struct ctl_cmd_entry *entry;
11522 int service_action;
11524 entry = &ctl_cmd_table[ctsio->cdb[0]];
11526 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11527 if (entry->flags & CTL_CMD_FLAG_SA5) {
11528 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11529 entry = &((const struct ctl_cmd_entry *)
11530 entry->execute)[service_action];
11535 const struct ctl_cmd_entry *
11536 ctl_validate_command(struct ctl_scsiio *ctsio)
11538 const struct ctl_cmd_entry *entry;
11542 entry = ctl_get_cmd_entry(ctsio, &sa);
11543 if (entry->execute == NULL) {
11545 ctl_set_invalid_field(ctsio,
11552 ctl_set_invalid_opcode(ctsio);
11553 ctl_done((union ctl_io *)ctsio);
11556 KASSERT(entry->length > 0,
11557 ("Not defined length for command 0x%02x/0x%02x",
11558 ctsio->cdb[0], ctsio->cdb[1]));
11559 for (i = 1; i < entry->length; i++) {
11560 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11563 ctl_set_invalid_field(ctsio,
11568 /*bit*/ fls(diff) - 1);
11569 ctl_done((union ctl_io *)ctsio);
11576 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11579 switch (lun_type) {
11581 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11585 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11589 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11599 ctl_scsiio(struct ctl_scsiio *ctsio)
11602 const struct ctl_cmd_entry *entry;
11604 retval = CTL_RETVAL_COMPLETE;
11606 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11608 entry = ctl_get_cmd_entry(ctsio, NULL);
11611 * If this I/O has been aborted, just send it straight to
11612 * ctl_done() without executing it.
11614 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11615 ctl_done((union ctl_io *)ctsio);
11620 * All the checks should have been handled by ctl_scsiio_precheck().
11621 * We should be clear now to just execute the I/O.
11623 retval = entry->execute(ctsio);
11630 ctl_target_reset(union ctl_io *io)
11632 struct ctl_softc *softc = CTL_SOFTC(io);
11633 struct ctl_port *port = CTL_PORT(io);
11634 struct ctl_lun *lun;
11636 ctl_ua_type ua_type;
11638 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11639 union ctl_ha_msg msg_info;
11641 msg_info.hdr.nexus = io->io_hdr.nexus;
11642 msg_info.task.task_action = io->taskio.task_action;
11643 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11644 msg_info.hdr.original_sc = NULL;
11645 msg_info.hdr.serializing_sc = NULL;
11646 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11647 sizeof(msg_info.task), M_WAITOK);
11650 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11651 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11652 ua_type = CTL_UA_TARG_RESET;
11654 ua_type = CTL_UA_BUS_RESET;
11655 mtx_lock(&softc->ctl_lock);
11656 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11657 if (port != NULL &&
11658 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11660 ctl_do_lun_reset(lun, initidx, ua_type);
11662 mtx_unlock(&softc->ctl_lock);
11663 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11668 * The LUN should always be set. The I/O is optional, and is used to
11669 * distinguish between I/Os sent by this initiator, and by other
11670 * initiators. We set unit attention for initiators other than this one.
11671 * SAM-3 is vague on this point. It does say that a unit attention should
11672 * be established for other initiators when a LUN is reset (see section
11673 * 5.7.3), but it doesn't specifically say that the unit attention should
11674 * be established for this particular initiator when a LUN is reset. Here
11675 * is the relevant text, from SAM-3 rev 8:
11677 * 5.7.2 When a SCSI initiator port aborts its own tasks
11679 * When a SCSI initiator port causes its own task(s) to be aborted, no
11680 * notification that the task(s) have been aborted shall be returned to
11681 * the SCSI initiator port other than the completion response for the
11682 * command or task management function action that caused the task(s) to
11683 * be aborted and notification(s) associated with related effects of the
11684 * action (e.g., a reset unit attention condition).
11686 * XXX KDM for now, we're setting unit attention for all initiators.
11689 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11694 mtx_lock(&lun->lun_lock);
11696 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11697 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11698 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11701 for (i = 0; i < ctl_max_ports; i++) {
11702 free(lun->pending_sense[i], M_CTL);
11703 lun->pending_sense[i] = NULL;
11705 /* Clear reservation. */
11706 lun->flags &= ~CTL_LUN_RESERVED;
11707 /* Clear prevent media removal. */
11708 if (lun->prevent) {
11709 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11710 ctl_clear_mask(lun->prevent, i);
11711 lun->prevent_count = 0;
11713 /* Clear TPC status */
11714 ctl_tpc_lun_clear(lun, -1);
11715 /* Establish UA. */
11717 ctl_est_ua_all(lun, initidx, ua_type);
11719 ctl_est_ua_all(lun, -1, ua_type);
11721 mtx_unlock(&lun->lun_lock);
11725 ctl_lun_reset(union ctl_io *io)
11727 struct ctl_softc *softc = CTL_SOFTC(io);
11728 struct ctl_lun *lun;
11729 uint32_t targ_lun, initidx;
11731 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11732 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11733 mtx_lock(&softc->ctl_lock);
11734 if (targ_lun >= ctl_max_luns ||
11735 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11736 mtx_unlock(&softc->ctl_lock);
11737 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11740 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11741 mtx_unlock(&softc->ctl_lock);
11742 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11744 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11745 union ctl_ha_msg msg_info;
11747 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11748 msg_info.hdr.nexus = io->io_hdr.nexus;
11749 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11750 msg_info.hdr.original_sc = NULL;
11751 msg_info.hdr.serializing_sc = NULL;
11752 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11753 sizeof(msg_info.task), M_WAITOK);
11759 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11764 mtx_assert(&lun->lun_lock, MA_OWNED);
11767 * Run through the OOA queue and attempt to find the given I/O.
11768 * The target port, initiator ID, tag type and tag number have to
11769 * match the values that we got from the initiator. If we have an
11770 * untagged command to abort, simply abort the first untagged command
11771 * we come to. We only allow one untagged command at a time of course.
11773 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11774 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11776 if ((targ_port == UINT32_MAX ||
11777 targ_port == xio->io_hdr.nexus.targ_port) &&
11778 (init_id == UINT32_MAX ||
11779 init_id == xio->io_hdr.nexus.initid)) {
11780 if (targ_port != xio->io_hdr.nexus.targ_port ||
11781 init_id != xio->io_hdr.nexus.initid)
11782 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11783 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11784 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11785 union ctl_ha_msg msg_info;
11787 msg_info.hdr.nexus = xio->io_hdr.nexus;
11788 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11789 msg_info.task.tag_num = xio->scsiio.tag_num;
11790 msg_info.task.tag_type = xio->scsiio.tag_type;
11791 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11792 msg_info.hdr.original_sc = NULL;
11793 msg_info.hdr.serializing_sc = NULL;
11794 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11795 sizeof(msg_info.task), M_NOWAIT);
11802 ctl_abort_task_set(union ctl_io *io)
11804 struct ctl_softc *softc = CTL_SOFTC(io);
11805 struct ctl_lun *lun;
11811 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11812 mtx_lock(&softc->ctl_lock);
11813 if (targ_lun >= ctl_max_luns ||
11814 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11815 mtx_unlock(&softc->ctl_lock);
11816 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11820 mtx_lock(&lun->lun_lock);
11821 mtx_unlock(&softc->ctl_lock);
11822 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11823 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11824 io->io_hdr.nexus.initid,
11825 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11826 } else { /* CTL_TASK_CLEAR_TASK_SET */
11827 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11828 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11830 mtx_unlock(&lun->lun_lock);
11831 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11836 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11837 ctl_ua_type ua_type)
11839 struct ctl_lun *lun;
11840 struct scsi_sense_data *ps;
11843 p = initidx / CTL_MAX_INIT_PER_PORT;
11844 i = initidx % CTL_MAX_INIT_PER_PORT;
11845 mtx_lock(&softc->ctl_lock);
11846 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11847 mtx_lock(&lun->lun_lock);
11849 ctl_abort_tasks_lun(lun, p, i, 1);
11851 ps = lun->pending_sense[p];
11853 ps[i].error_code = 0;
11854 /* Clear reservation. */
11855 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11856 lun->flags &= ~CTL_LUN_RESERVED;
11857 /* Clear prevent media removal. */
11858 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11859 ctl_clear_mask(lun->prevent, initidx);
11860 lun->prevent_count--;
11862 /* Clear TPC status */
11863 ctl_tpc_lun_clear(lun, initidx);
11864 /* Establish UA. */
11865 ctl_est_ua(lun, initidx, ua_type);
11866 mtx_unlock(&lun->lun_lock);
11868 mtx_unlock(&softc->ctl_lock);
11872 ctl_i_t_nexus_reset(union ctl_io *io)
11874 struct ctl_softc *softc = CTL_SOFTC(io);
11877 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11878 union ctl_ha_msg msg_info;
11880 msg_info.hdr.nexus = io->io_hdr.nexus;
11881 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11882 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11883 msg_info.hdr.original_sc = NULL;
11884 msg_info.hdr.serializing_sc = NULL;
11885 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11886 sizeof(msg_info.task), M_WAITOK);
11889 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11890 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
11891 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11896 ctl_abort_task(union ctl_io *io)
11898 struct ctl_softc *softc = CTL_SOFTC(io);
11900 struct ctl_lun *lun;
11903 char printbuf[128];
11913 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11914 mtx_lock(&softc->ctl_lock);
11915 if (targ_lun >= ctl_max_luns ||
11916 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11917 mtx_unlock(&softc->ctl_lock);
11918 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11923 printf("ctl_abort_task: called for lun %lld, tag %d type %d\n",
11924 lun->lun, io->taskio.tag_num, io->taskio.tag_type);
11927 mtx_lock(&lun->lun_lock);
11928 mtx_unlock(&softc->ctl_lock);
11930 * Run through the OOA queue and attempt to find the given I/O.
11931 * The target port, initiator ID, tag type and tag number have to
11932 * match the values that we got from the initiator. If we have an
11933 * untagged command to abort, simply abort the first untagged command
11934 * we come to. We only allow one untagged command at a time of course.
11936 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11937 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11939 sbuf_new(&sb, printbuf, sizeof(printbuf), SBUF_FIXEDLEN);
11941 sbuf_printf(&sb, "LUN %lld tag %d type %d%s%s%s%s: ",
11942 lun->lun, xio->scsiio.tag_num,
11943 xio->scsiio.tag_type,
11944 (xio->io_hdr.blocked_links.tqe_prev
11945 == NULL) ? "" : " BLOCKED",
11946 (xio->io_hdr.flags &
11947 CTL_FLAG_DMA_INPROG) ? " DMA" : "",
11948 (xio->io_hdr.flags &
11949 CTL_FLAG_ABORT) ? " ABORT" : "",
11950 (xio->io_hdr.flags &
11951 CTL_FLAG_IS_WAS_ON_RTR ? " RTR" : ""));
11952 ctl_scsi_command_string(&xio->scsiio, NULL, &sb);
11954 printf("%s\n", sbuf_data(&sb));
11957 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11958 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11959 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11963 * If the abort says that the task is untagged, the
11964 * task in the queue must be untagged. Otherwise,
11965 * we just check to see whether the tag numbers
11966 * match. This is because the QLogic firmware
11967 * doesn't pass back the tag type in an abort
11971 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11972 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11973 || (xio->scsiio.tag_num == io->taskio.tag_num))
11976 * XXX KDM we've got problems with FC, because it
11977 * doesn't send down a tag type with aborts. So we
11978 * can only really go by the tag number...
11979 * This may cause problems with parallel SCSI.
11980 * Need to figure that out!!
11982 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11983 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11985 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11986 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11987 union ctl_ha_msg msg_info;
11989 msg_info.hdr.nexus = io->io_hdr.nexus;
11990 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11991 msg_info.task.tag_num = io->taskio.tag_num;
11992 msg_info.task.tag_type = io->taskio.tag_type;
11993 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11994 msg_info.hdr.original_sc = NULL;
11995 msg_info.hdr.serializing_sc = NULL;
11997 printf("Sent Abort to other side\n");
11999 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12000 sizeof(msg_info.task), M_NOWAIT);
12003 printf("ctl_abort_task: found I/O to abort\n");
12007 mtx_unlock(&lun->lun_lock);
12011 * This isn't really an error. It's entirely possible for
12012 * the abort and command completion to cross on the wire.
12013 * This is more of an informative/diagnostic error.
12016 printf("ctl_abort_task: ABORT sent for nonexistent I/O: "
12017 "%u:%u:%u tag %d type %d\n",
12018 io->io_hdr.nexus.initid,
12019 io->io_hdr.nexus.targ_port,
12020 io->io_hdr.nexus.targ_lun, io->taskio.tag_num,
12021 io->taskio.tag_type);
12024 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12029 ctl_query_task(union ctl_io *io, int task_set)
12031 struct ctl_softc *softc = CTL_SOFTC(io);
12033 struct ctl_lun *lun;
12037 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12038 mtx_lock(&softc->ctl_lock);
12039 if (targ_lun >= ctl_max_luns ||
12040 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12041 mtx_unlock(&softc->ctl_lock);
12042 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12045 mtx_lock(&lun->lun_lock);
12046 mtx_unlock(&softc->ctl_lock);
12047 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12048 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12050 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12051 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12052 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12055 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12060 mtx_unlock(&lun->lun_lock);
12062 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12064 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12069 ctl_query_async_event(union ctl_io *io)
12071 struct ctl_softc *softc = CTL_SOFTC(io);
12072 struct ctl_lun *lun;
12074 uint32_t targ_lun, initidx;
12076 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12077 mtx_lock(&softc->ctl_lock);
12078 if (targ_lun >= ctl_max_luns ||
12079 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12080 mtx_unlock(&softc->ctl_lock);
12081 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12084 mtx_lock(&lun->lun_lock);
12085 mtx_unlock(&softc->ctl_lock);
12086 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12087 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12088 mtx_unlock(&lun->lun_lock);
12089 if (ua != CTL_UA_NONE)
12090 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12092 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12097 ctl_run_task(union ctl_io *io)
12101 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12102 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12103 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12104 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12105 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12106 switch (io->taskio.task_action) {
12107 case CTL_TASK_ABORT_TASK:
12108 retval = ctl_abort_task(io);
12110 case CTL_TASK_ABORT_TASK_SET:
12111 case CTL_TASK_CLEAR_TASK_SET:
12112 retval = ctl_abort_task_set(io);
12114 case CTL_TASK_CLEAR_ACA:
12116 case CTL_TASK_I_T_NEXUS_RESET:
12117 retval = ctl_i_t_nexus_reset(io);
12119 case CTL_TASK_LUN_RESET:
12120 retval = ctl_lun_reset(io);
12122 case CTL_TASK_TARGET_RESET:
12123 case CTL_TASK_BUS_RESET:
12124 retval = ctl_target_reset(io);
12126 case CTL_TASK_PORT_LOGIN:
12128 case CTL_TASK_PORT_LOGOUT:
12130 case CTL_TASK_QUERY_TASK:
12131 retval = ctl_query_task(io, 0);
12133 case CTL_TASK_QUERY_TASK_SET:
12134 retval = ctl_query_task(io, 1);
12136 case CTL_TASK_QUERY_ASYNC_EVENT:
12137 retval = ctl_query_async_event(io);
12140 printf("%s: got unknown task management event %d\n",
12141 __func__, io->taskio.task_action);
12145 io->io_hdr.status = CTL_SUCCESS;
12147 io->io_hdr.status = CTL_ERROR;
12152 * For HA operation. Handle commands that come in from the other
12156 ctl_handle_isc(union ctl_io *io)
12158 struct ctl_softc *softc = CTL_SOFTC(io);
12159 struct ctl_lun *lun;
12160 const struct ctl_cmd_entry *entry;
12163 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12164 switch (io->io_hdr.msg_type) {
12165 case CTL_MSG_SERIALIZE:
12166 ctl_serialize_other_sc_cmd(&io->scsiio);
12168 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12169 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12170 if (targ_lun >= ctl_max_luns ||
12171 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12175 mtx_lock(&lun->lun_lock);
12176 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12177 mtx_unlock(&lun->lun_lock);
12181 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12182 mtx_unlock(&lun->lun_lock);
12183 ctl_enqueue_rtr(io);
12185 case CTL_MSG_FINISH_IO:
12186 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12190 if (targ_lun >= ctl_max_luns ||
12191 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12195 mtx_lock(&lun->lun_lock);
12196 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12197 ctl_check_blocked(lun);
12198 mtx_unlock(&lun->lun_lock);
12201 case CTL_MSG_PERS_ACTION:
12202 ctl_hndl_per_res_out_on_other_sc(io);
12205 case CTL_MSG_BAD_JUJU:
12208 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12209 ctl_datamove_remote(io);
12211 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12212 io->scsiio.be_move_done(io);
12214 case CTL_MSG_FAILOVER:
12215 ctl_failover_lun(io);
12219 printf("%s: Invalid message type %d\n",
12220 __func__, io->io_hdr.msg_type);
12229 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12230 * there is no match.
12232 static ctl_lun_error_pattern
12233 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12235 const struct ctl_cmd_entry *entry;
12236 ctl_lun_error_pattern filtered_pattern, pattern;
12238 pattern = desc->error_pattern;
12241 * XXX KDM we need more data passed into this function to match a
12242 * custom pattern, and we actually need to implement custom pattern
12245 if (pattern & CTL_LUN_PAT_CMD)
12246 return (CTL_LUN_PAT_CMD);
12248 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12249 return (CTL_LUN_PAT_ANY);
12251 entry = ctl_get_cmd_entry(ctsio, NULL);
12253 filtered_pattern = entry->pattern & pattern;
12256 * If the user requested specific flags in the pattern (e.g.
12257 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12260 * If the user did not specify any flags, it doesn't matter whether
12261 * or not the command supports the flags.
12263 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12264 (pattern & ~CTL_LUN_PAT_MASK))
12265 return (CTL_LUN_PAT_NONE);
12268 * If the user asked for a range check, see if the requested LBA
12269 * range overlaps with this command's LBA range.
12271 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12277 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12279 return (CTL_LUN_PAT_NONE);
12281 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12282 desc->lba_range.len, FALSE);
12284 * A "pass" means that the LBA ranges don't overlap, so
12285 * this doesn't match the user's range criteria.
12287 if (action == CTL_ACTION_PASS)
12288 return (CTL_LUN_PAT_NONE);
12291 return (filtered_pattern);
12295 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12297 struct ctl_error_desc *desc, *desc2;
12299 mtx_assert(&lun->lun_lock, MA_OWNED);
12301 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12302 ctl_lun_error_pattern pattern;
12304 * Check to see whether this particular command matches
12305 * the pattern in the descriptor.
12307 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12308 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12311 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12312 case CTL_LUN_INJ_ABORTED:
12313 ctl_set_aborted(&io->scsiio);
12315 case CTL_LUN_INJ_MEDIUM_ERR:
12316 ctl_set_medium_error(&io->scsiio,
12317 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12318 CTL_FLAG_DATA_OUT);
12320 case CTL_LUN_INJ_UA:
12321 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12323 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12325 case CTL_LUN_INJ_CUSTOM:
12327 * We're assuming the user knows what he is doing.
12328 * Just copy the sense information without doing
12331 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12332 MIN(sizeof(desc->custom_sense),
12333 sizeof(io->scsiio.sense_data)));
12334 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12335 io->scsiio.sense_len = SSD_FULL_SIZE;
12336 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12338 case CTL_LUN_INJ_NONE:
12341 * If this is an error injection type we don't know
12342 * about, clear the continuous flag (if it is set)
12343 * so it will get deleted below.
12345 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12349 * By default, each error injection action is a one-shot
12351 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12354 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12360 #ifdef CTL_IO_DELAY
12362 ctl_datamove_timer_wakeup(void *arg)
12366 io = (union ctl_io *)arg;
12370 #endif /* CTL_IO_DELAY */
12373 ctl_datamove(union ctl_io *io)
12375 void (*fe_datamove)(union ctl_io *io);
12377 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12379 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12381 /* No data transferred yet. Frontend must update this when done. */
12382 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12385 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12390 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12391 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12393 sbuf_cat(&sb, path_str);
12394 switch (io->io_hdr.io_type) {
12396 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12397 sbuf_printf(&sb, "\n");
12398 sbuf_cat(&sb, path_str);
12399 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12400 io->scsiio.tag_num, io->scsiio.tag_type);
12403 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12404 "Tag Type: %d\n", io->taskio.task_action,
12405 io->taskio.tag_num, io->taskio.tag_type);
12408 panic("%s: Invalid CTL I/O type %d\n",
12409 __func__, io->io_hdr.io_type);
12411 sbuf_cat(&sb, path_str);
12412 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12413 (intmax_t)time_uptime - io->io_hdr.start_time);
12415 printf("%s", sbuf_data(&sb));
12417 #endif /* CTL_TIME_IO */
12419 #ifdef CTL_IO_DELAY
12420 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12421 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12423 struct ctl_lun *lun;
12427 && (lun->delay_info.datamove_delay > 0)) {
12429 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12430 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12431 callout_reset(&io->io_hdr.delay_callout,
12432 lun->delay_info.datamove_delay * hz,
12433 ctl_datamove_timer_wakeup, io);
12434 if (lun->delay_info.datamove_type ==
12435 CTL_DELAY_TYPE_ONESHOT)
12436 lun->delay_info.datamove_delay = 0;
12443 * This command has been aborted. Set the port status, so we fail
12446 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12447 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12448 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12449 io->io_hdr.nexus.targ_port,
12450 io->io_hdr.nexus.targ_lun);
12451 io->io_hdr.port_status = 31337;
12453 * Note that the backend, in this case, will get the
12454 * callback in its context. In other cases it may get
12455 * called in the frontend's interrupt thread context.
12457 io->scsiio.be_move_done(io);
12461 /* Don't confuse frontend with zero length data move. */
12462 if (io->scsiio.kern_data_len == 0) {
12463 io->scsiio.be_move_done(io);
12467 fe_datamove = CTL_PORT(io)->fe_datamove;
12472 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12474 union ctl_ha_msg msg;
12476 struct bintime cur_bt;
12479 memset(&msg, 0, sizeof(msg));
12480 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12481 msg.hdr.original_sc = io;
12482 msg.hdr.serializing_sc = io->io_hdr.remote_io;
12483 msg.hdr.nexus = io->io_hdr.nexus;
12484 msg.hdr.status = io->io_hdr.status;
12485 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12486 msg.scsi.tag_num = io->scsiio.tag_num;
12487 msg.scsi.tag_type = io->scsiio.tag_type;
12488 msg.scsi.scsi_status = io->scsiio.scsi_status;
12489 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12490 io->scsiio.sense_len);
12491 msg.scsi.sense_len = io->scsiio.sense_len;
12492 msg.scsi.port_status = io->io_hdr.port_status;
12493 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12494 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12495 ctl_failover_io(io, /*have_lock*/ have_lock);
12498 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12499 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12500 msg.scsi.sense_len, M_WAITOK);
12503 getbinuptime(&cur_bt);
12504 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12505 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12507 io->io_hdr.num_dmas++;
12511 * The DMA to the remote side is done, now we need to tell the other side
12512 * we're done so it can continue with its data movement.
12515 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12522 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12523 printf("%s: ISC DMA write failed with error %d", __func__,
12525 ctl_set_internal_failure(&io->scsiio,
12527 /*retry_count*/ rq->ret);
12530 ctl_dt_req_free(rq);
12532 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12533 free(CTL_LSGLT(io)[i].addr, M_CTL);
12534 free(CTL_RSGL(io), M_CTL);
12535 CTL_RSGL(io) = NULL;
12536 CTL_LSGL(io) = NULL;
12539 * The data is in local and remote memory, so now we need to send
12540 * status (good or back) back to the other side.
12542 ctl_send_datamove_done(io, /*have_lock*/ 0);
12546 * We've moved the data from the host/controller into local memory. Now we
12547 * need to push it over to the remote controller's memory.
12550 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12554 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12555 ctl_datamove_remote_write_cb);
12560 ctl_datamove_remote_write(union ctl_io *io)
12563 void (*fe_datamove)(union ctl_io *io);
12566 * - Get the data from the host/HBA into local memory.
12567 * - DMA memory from the local controller to the remote controller.
12568 * - Send status back to the remote controller.
12571 retval = ctl_datamove_remote_sgl_setup(io);
12575 /* Switch the pointer over so the FETD knows what to do */
12576 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12579 * Use a custom move done callback, since we need to send completion
12580 * back to the other controller, not to the backend on this side.
12582 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12584 fe_datamove = CTL_PORT(io)->fe_datamove;
12589 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12598 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12599 free(CTL_LSGLT(io)[i].addr, M_CTL);
12600 free(CTL_RSGL(io), M_CTL);
12601 CTL_RSGL(io) = NULL;
12602 CTL_LSGL(io) = NULL;
12605 scsi_path_string(io, path_str, sizeof(path_str));
12606 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12607 sbuf_cat(&sb, path_str);
12608 scsi_command_string(&io->scsiio, NULL, &sb);
12609 sbuf_printf(&sb, "\n");
12610 sbuf_cat(&sb, path_str);
12611 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12612 io->scsiio.tag_num, io->scsiio.tag_type);
12613 sbuf_cat(&sb, path_str);
12614 sbuf_printf(&sb, "%s: flags %#x, status %#x\n", __func__,
12615 io->io_hdr.flags, io->io_hdr.status);
12617 printk("%s", sbuf_data(&sb));
12622 * The read is done, now we need to send status (good or bad) back
12623 * to the other side.
12625 ctl_send_datamove_done(io, /*have_lock*/ 0);
12631 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12634 void (*fe_datamove)(union ctl_io *io);
12638 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12639 printf("%s: ISC DMA read failed with error %d\n", __func__,
12641 ctl_set_internal_failure(&io->scsiio,
12643 /*retry_count*/ rq->ret);
12646 ctl_dt_req_free(rq);
12648 /* Switch the pointer over so the FETD knows what to do */
12649 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12652 * Use a custom move done callback, since we need to send completion
12653 * back to the other controller, not to the backend on this side.
12655 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12657 /* XXX KDM add checks like the ones in ctl_datamove? */
12659 fe_datamove = CTL_PORT(io)->fe_datamove;
12664 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12666 struct ctl_sg_entry *local_sglist;
12667 uint32_t len_to_go;
12672 local_sglist = CTL_LSGL(io);
12673 len_to_go = io->scsiio.kern_data_len;
12676 * The difficult thing here is that the size of the various
12677 * S/G segments may be different than the size from the
12678 * remote controller. That'll make it harder when DMAing
12679 * the data back to the other side.
12681 for (i = 0; len_to_go > 0; i++) {
12682 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12683 local_sglist[i].addr =
12684 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12686 len_to_go -= local_sglist[i].len;
12689 * Reset the number of S/G entries accordingly. The original
12690 * number of S/G entries is available in rem_sg_entries.
12692 io->scsiio.kern_sg_entries = i;
12695 printf("%s: kern_sg_entries = %d\n", __func__,
12696 io->scsiio.kern_sg_entries);
12697 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12698 printf("%s: sg[%d] = %p, %lu\n", __func__, i,
12699 local_sglist[i].addr, local_sglist[i].len);
12706 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12707 ctl_ha_dt_cb callback)
12709 struct ctl_ha_dt_req *rq;
12710 struct ctl_sg_entry *remote_sglist, *local_sglist;
12711 uint32_t local_used, remote_used, total_used;
12714 rq = ctl_dt_req_alloc();
12717 * If we failed to allocate the request, and if the DMA didn't fail
12718 * anyway, set busy status. This is just a resource allocation
12722 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12723 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12724 ctl_set_busy(&io->scsiio);
12726 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12727 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12730 ctl_dt_req_free(rq);
12733 * The data move failed. We need to return status back
12734 * to the other controller. No point in trying to DMA
12735 * data to the remote controller.
12738 ctl_send_datamove_done(io, /*have_lock*/ 0);
12743 local_sglist = CTL_LSGL(io);
12744 remote_sglist = CTL_RSGL(io);
12750 * Pull/push the data over the wire from/to the other controller.
12751 * This takes into account the possibility that the local and
12752 * remote sglists may not be identical in terms of the size of
12753 * the elements and the number of elements.
12755 * One fundamental assumption here is that the length allocated for
12756 * both the local and remote sglists is identical. Otherwise, we've
12757 * essentially got a coding error of some sort.
12759 isc_ret = CTL_HA_STATUS_SUCCESS;
12760 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12764 rq->command = command;
12768 * Both pointers should be aligned. But it is possible
12769 * that the allocation length is not. They should both
12770 * also have enough slack left over at the end, though,
12771 * to round up to the next 8 byte boundary.
12773 cur_len = MIN(local_sglist[i].len - local_used,
12774 remote_sglist[j].len - remote_used);
12775 rq->size = cur_len;
12777 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12778 tmp_ptr += local_used;
12781 /* Use physical addresses when talking to ISC hardware */
12782 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12783 /* XXX KDM use busdma */
12784 rq->local = vtophys(tmp_ptr);
12786 rq->local = tmp_ptr;
12788 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12789 ("HA does not support BUS_ADDR"));
12790 rq->local = tmp_ptr;
12793 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12794 tmp_ptr += remote_used;
12795 rq->remote = tmp_ptr;
12797 rq->callback = NULL;
12799 local_used += cur_len;
12800 if (local_used >= local_sglist[i].len) {
12805 remote_used += cur_len;
12806 if (remote_used >= remote_sglist[j].len) {
12810 total_used += cur_len;
12812 if (total_used >= io->scsiio.kern_data_len)
12813 rq->callback = callback;
12816 printf("%s: %s: local %p remote %p size %d\n", __func__,
12817 (command == CTL_HA_DT_CMD_WRITE) ? "WRITE" : "READ",
12818 rq->local, rq->remote, rq->size);
12821 isc_ret = ctl_dt_single(rq);
12822 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12825 if (isc_ret != CTL_HA_STATUS_WAIT) {
12834 ctl_datamove_remote_read(union ctl_io *io)
12840 * This will send an error to the other controller in the case of a
12843 retval = ctl_datamove_remote_sgl_setup(io);
12847 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12848 ctl_datamove_remote_read_cb);
12851 * Make sure we free memory if there was an error.. The
12852 * ctl_datamove_remote_xfer() function will send the
12853 * datamove done message, or call the callback with an
12854 * error if there is a problem.
12856 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12857 free(CTL_LSGLT(io)[i].addr, M_CTL);
12858 free(CTL_RSGL(io), M_CTL);
12859 CTL_RSGL(io) = NULL;
12860 CTL_LSGL(io) = NULL;
12865 * Process a datamove request from the other controller. This is used for
12866 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12867 * first. Once that is complete, the data gets DMAed into the remote
12868 * controller's memory. For reads, we DMA from the remote controller's
12869 * memory into our memory first, and then move it out to the FETD.
12872 ctl_datamove_remote(union ctl_io *io)
12875 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12877 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12878 ctl_failover_io(io, /*have_lock*/ 0);
12883 * Note that we look for an aborted I/O here, but don't do some of
12884 * the other checks that ctl_datamove() normally does.
12885 * We don't need to run the datamove delay code, since that should
12886 * have been done if need be on the other controller.
12888 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12889 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12890 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12891 io->io_hdr.nexus.targ_port,
12892 io->io_hdr.nexus.targ_lun);
12893 io->io_hdr.port_status = 31338;
12894 ctl_send_datamove_done(io, /*have_lock*/ 0);
12898 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12899 ctl_datamove_remote_write(io);
12900 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12901 ctl_datamove_remote_read(io);
12903 io->io_hdr.port_status = 31339;
12904 ctl_send_datamove_done(io, /*have_lock*/ 0);
12909 ctl_process_done(union ctl_io *io)
12911 struct ctl_softc *softc = CTL_SOFTC(io);
12912 struct ctl_port *port = CTL_PORT(io);
12913 struct ctl_lun *lun = CTL_LUN(io);
12914 void (*fe_done)(union ctl_io *io);
12915 union ctl_ha_msg msg;
12917 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12918 fe_done = port->fe_done;
12921 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12926 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12927 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12929 sbuf_cat(&sb, path_str);
12930 switch (io->io_hdr.io_type) {
12932 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12933 sbuf_printf(&sb, "\n");
12934 sbuf_cat(&sb, path_str);
12935 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12936 io->scsiio.tag_num, io->scsiio.tag_type);
12939 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12940 "Tag Type: %d\n", io->taskio.task_action,
12941 io->taskio.tag_num, io->taskio.tag_type);
12944 panic("%s: Invalid CTL I/O type %d\n",
12945 __func__, io->io_hdr.io_type);
12947 sbuf_cat(&sb, path_str);
12948 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12949 (intmax_t)time_uptime - io->io_hdr.start_time);
12951 printf("%s", sbuf_data(&sb));
12953 #endif /* CTL_TIME_IO */
12955 switch (io->io_hdr.io_type) {
12959 if (ctl_debug & CTL_DEBUG_INFO)
12960 ctl_io_error_print(io, NULL);
12964 panic("%s: Invalid CTL I/O type %d\n",
12965 __func__, io->io_hdr.io_type);
12969 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12970 io->io_hdr.nexus.targ_mapped_lun));
12974 mtx_lock(&lun->lun_lock);
12977 * Check to see if we have any informational exception and status
12978 * of this command can be modified to report it in form of either
12979 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
12981 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
12982 io->io_hdr.status == CTL_SUCCESS &&
12983 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
12984 uint8_t mrie = lun->MODE_IE.mrie;
12985 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
12986 (lun->MODE_VER.byte3 & SMS_VER_PER));
12987 if (((mrie == SIEP_MRIE_REC_COND && per) ||
12988 mrie == SIEP_MRIE_REC_UNCOND ||
12989 mrie == SIEP_MRIE_NO_SENSE) &&
12990 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
12991 CTL_CMD_FLAG_NO_SENSE) == 0) {
12992 ctl_set_sense(&io->scsiio,
12993 /*current_error*/ 1,
12994 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
12995 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
12996 /*asc*/ lun->ie_asc,
12997 /*ascq*/ lun->ie_ascq,
12999 lun->ie_reported = 1;
13001 } else if (lun->ie_reported < 0)
13002 lun->ie_reported = 0;
13005 * Check to see if we have any errors to inject here. We only
13006 * inject errors for commands that don't already have errors set.
13008 if (!STAILQ_EMPTY(&lun->error_list) &&
13009 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13010 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13011 ctl_inject_error(lun, io);
13014 * XXX KDM how do we treat commands that aren't completed
13017 * XXX KDM should we also track I/O latency?
13019 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13020 io->io_hdr.io_type == CTL_IO_SCSI) {
13026 bintime_sub(&bt, &io->io_hdr.start_bt);
13028 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13030 type = CTL_STATS_READ;
13031 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13033 type = CTL_STATS_WRITE;
13035 type = CTL_STATS_NO_IO;
13037 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13038 lun->stats.operations[type] ++;
13039 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13041 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13042 bintime_add(&lun->stats.time[type], &bt);
13045 mtx_lock(&port->port_lock);
13046 port->stats.bytes[type] += io->scsiio.kern_total_len;
13047 port->stats.operations[type] ++;
13048 port->stats.dmas[type] += io->io_hdr.num_dmas;
13050 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13051 bintime_add(&port->stats.time[type], &bt);
13053 mtx_unlock(&port->port_lock);
13057 * Remove this from the OOA queue.
13059 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13061 if (TAILQ_EMPTY(&lun->ooa_queue))
13062 lun->last_busy = getsbinuptime();
13066 * Run through the blocked queue on this LUN and see if anything
13067 * has become unblocked, now that this transaction is done.
13069 ctl_check_blocked(lun);
13072 * If the LUN has been invalidated, free it if there is nothing
13073 * left on its OOA queue.
13075 if ((lun->flags & CTL_LUN_INVALID)
13076 && TAILQ_EMPTY(&lun->ooa_queue)) {
13077 mtx_unlock(&lun->lun_lock);
13080 mtx_unlock(&lun->lun_lock);
13085 * If this command has been aborted, make sure we set the status
13086 * properly. The FETD is responsible for freeing the I/O and doing
13087 * whatever it needs to do to clean up its state.
13089 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13090 ctl_set_task_aborted(&io->scsiio);
13093 * If enabled, print command error status.
13095 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13096 (ctl_debug & CTL_DEBUG_INFO) != 0)
13097 ctl_io_error_print(io, NULL);
13100 * Tell the FETD or the other shelf controller we're done with this
13101 * command. Note that only SCSI commands get to this point. Task
13102 * management commands are completed above.
13104 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13105 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13106 memset(&msg, 0, sizeof(msg));
13107 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13108 msg.hdr.serializing_sc = io->io_hdr.remote_io;
13109 msg.hdr.nexus = io->io_hdr.nexus;
13110 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13111 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13119 * Front end should call this if it doesn't do autosense. When the request
13120 * sense comes back in from the initiator, we'll dequeue this and send it.
13123 ctl_queue_sense(union ctl_io *io)
13125 struct ctl_softc *softc = CTL_SOFTC(io);
13126 struct ctl_port *port = CTL_PORT(io);
13127 struct ctl_lun *lun;
13128 struct scsi_sense_data *ps;
13129 uint32_t initidx, p, targ_lun;
13131 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13133 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13136 * LUN lookup will likely move to the ctl_work_thread() once we
13137 * have our new queueing infrastructure (that doesn't put things on
13138 * a per-LUN queue initially). That is so that we can handle
13139 * things like an INQUIRY to a LUN that we don't have enabled. We
13140 * can't deal with that right now.
13141 * If we don't have a LUN for this, just toss the sense information.
13143 mtx_lock(&softc->ctl_lock);
13144 if (targ_lun >= ctl_max_luns ||
13145 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13146 mtx_unlock(&softc->ctl_lock);
13149 mtx_lock(&lun->lun_lock);
13150 mtx_unlock(&softc->ctl_lock);
13152 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13153 p = initidx / CTL_MAX_INIT_PER_PORT;
13154 if (lun->pending_sense[p] == NULL) {
13155 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13156 M_CTL, M_NOWAIT | M_ZERO);
13158 if ((ps = lun->pending_sense[p]) != NULL) {
13159 ps += initidx % CTL_MAX_INIT_PER_PORT;
13160 memset(ps, 0, sizeof(*ps));
13161 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13163 mtx_unlock(&lun->lun_lock);
13167 return (CTL_RETVAL_COMPLETE);
13171 * Primary command inlet from frontend ports. All SCSI and task I/O
13172 * requests must go through this function.
13175 ctl_queue(union ctl_io *io)
13177 struct ctl_port *port = CTL_PORT(io);
13179 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13182 io->io_hdr.start_time = time_uptime;
13183 getbinuptime(&io->io_hdr.start_bt);
13184 #endif /* CTL_TIME_IO */
13186 /* Map FE-specific LUN ID into global one. */
13187 io->io_hdr.nexus.targ_mapped_lun =
13188 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13190 switch (io->io_hdr.io_type) {
13193 if (ctl_debug & CTL_DEBUG_CDB)
13195 ctl_enqueue_incoming(io);
13198 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13202 return (CTL_RETVAL_COMPLETE);
13205 #ifdef CTL_IO_DELAY
13207 ctl_done_timer_wakeup(void *arg)
13211 io = (union ctl_io *)arg;
13214 #endif /* CTL_IO_DELAY */
13217 ctl_serseq_done(union ctl_io *io)
13219 struct ctl_lun *lun = CTL_LUN(io);;
13221 if (lun->be_lun == NULL ||
13222 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13224 mtx_lock(&lun->lun_lock);
13225 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13226 ctl_check_blocked(lun);
13227 mtx_unlock(&lun->lun_lock);
13231 ctl_done(union ctl_io *io)
13235 * Enable this to catch duplicate completion issues.
13238 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13239 printf("%s: type %d msg %d cdb %x iptl: "
13240 "%u:%u:%u tag 0x%04x "
13241 "flag %#x status %x\n",
13243 io->io_hdr.io_type,
13244 io->io_hdr.msg_type,
13246 io->io_hdr.nexus.initid,
13247 io->io_hdr.nexus.targ_port,
13248 io->io_hdr.nexus.targ_lun,
13249 (io->io_hdr.io_type ==
13251 io->taskio.tag_num :
13252 io->scsiio.tag_num,
13254 io->io_hdr.status);
13256 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13260 * This is an internal copy of an I/O, and should not go through
13261 * the normal done processing logic.
13263 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13266 #ifdef CTL_IO_DELAY
13267 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13268 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13270 struct ctl_lun *lun = CTL_LUN(io);
13273 && (lun->delay_info.done_delay > 0)) {
13275 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13276 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13277 callout_reset(&io->io_hdr.delay_callout,
13278 lun->delay_info.done_delay * hz,
13279 ctl_done_timer_wakeup, io);
13280 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13281 lun->delay_info.done_delay = 0;
13285 #endif /* CTL_IO_DELAY */
13287 ctl_enqueue_done(io);
13291 ctl_work_thread(void *arg)
13293 struct ctl_thread *thr = (struct ctl_thread *)arg;
13294 struct ctl_softc *softc = thr->ctl_softc;
13298 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13300 while (!softc->shutdown) {
13302 * We handle the queues in this order:
13304 * - done queue (to free up resources, unblock other commands)
13308 * If those queues are empty, we break out of the loop and
13311 mtx_lock(&thr->queue_lock);
13312 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13314 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13315 mtx_unlock(&thr->queue_lock);
13316 ctl_handle_isc(io);
13319 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13321 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13322 /* clear any blocked commands, call fe_done */
13323 mtx_unlock(&thr->queue_lock);
13324 ctl_process_done(io);
13327 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13329 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13330 mtx_unlock(&thr->queue_lock);
13331 if (io->io_hdr.io_type == CTL_IO_TASK)
13334 ctl_scsiio_precheck(softc, &io->scsiio);
13337 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13339 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13340 mtx_unlock(&thr->queue_lock);
13341 retval = ctl_scsiio(&io->scsiio);
13342 if (retval != CTL_RETVAL_COMPLETE)
13343 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13347 /* Sleep until we have something to do. */
13348 mtx_sleep(thr, &thr->queue_lock, PDROP | PRIBIO, "-", 0);
13350 thr->thread = NULL;
13355 ctl_lun_thread(void *arg)
13357 struct ctl_softc *softc = (struct ctl_softc *)arg;
13358 struct ctl_be_lun *be_lun;
13360 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13362 while (!softc->shutdown) {
13363 mtx_lock(&softc->ctl_lock);
13364 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13365 if (be_lun != NULL) {
13366 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13367 mtx_unlock(&softc->ctl_lock);
13368 ctl_create_lun(be_lun);
13372 /* Sleep until we have something to do. */
13373 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13374 PDROP | PRIBIO, "-", 0);
13376 softc->lun_thread = NULL;
13381 ctl_thresh_thread(void *arg)
13383 struct ctl_softc *softc = (struct ctl_softc *)arg;
13384 struct ctl_lun *lun;
13385 struct ctl_logical_block_provisioning_page *page;
13387 union ctl_ha_msg msg;
13388 uint64_t thres, val;
13391 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13393 while (!softc->shutdown) {
13394 mtx_lock(&softc->ctl_lock);
13395 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13396 if ((lun->flags & CTL_LUN_DISABLED) ||
13397 (lun->flags & CTL_LUN_NO_MEDIA) ||
13398 lun->backend->lun_attr == NULL)
13400 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13401 softc->ha_mode == CTL_HA_MODE_XFER)
13403 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13406 page = &lun->MODE_LBP;
13407 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13408 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13410 thres = scsi_4btoul(page->descr[i].count);
13411 thres <<= CTL_LBP_EXPONENT;
13412 switch (page->descr[i].resource) {
13414 attr = "blocksavail";
13417 attr = "blocksused";
13420 attr = "poolblocksavail";
13423 attr = "poolblocksused";
13428 mtx_unlock(&softc->ctl_lock); // XXX
13429 val = lun->backend->lun_attr(
13430 lun->be_lun->be_lun, attr);
13431 mtx_lock(&softc->ctl_lock);
13432 if (val == UINT64_MAX)
13434 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13435 == SLBPPD_ARMING_INC)
13436 e = (val >= thres);
13438 e = (val <= thres);
13442 mtx_lock(&lun->lun_lock);
13444 scsi_u64to8b((uint8_t *)&page->descr[i] -
13445 (uint8_t *)page, lun->ua_tpt_info);
13446 if (lun->lasttpt == 0 ||
13447 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13448 lun->lasttpt = time_uptime;
13449 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13455 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13458 mtx_unlock(&lun->lun_lock);
13460 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13461 /* Send msg to other side. */
13462 bzero(&msg.ua, sizeof(msg.ua));
13463 msg.hdr.msg_type = CTL_MSG_UA;
13464 msg.hdr.nexus.initid = -1;
13465 msg.hdr.nexus.targ_port = -1;
13466 msg.hdr.nexus.targ_lun = lun->lun;
13467 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13469 msg.ua.ua_set = (set > 0);
13470 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13471 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13472 mtx_unlock(&softc->ctl_lock); // XXX
13473 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13474 sizeof(msg.ua), M_WAITOK);
13475 mtx_lock(&softc->ctl_lock);
13478 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13479 PDROP | PRIBIO, "-", CTL_LBP_PERIOD * hz);
13481 softc->thresh_thread = NULL;
13486 ctl_enqueue_incoming(union ctl_io *io)
13488 struct ctl_softc *softc = CTL_SOFTC(io);
13489 struct ctl_thread *thr;
13492 idx = (io->io_hdr.nexus.targ_port * 127 +
13493 io->io_hdr.nexus.initid) % worker_threads;
13494 thr = &softc->threads[idx];
13495 mtx_lock(&thr->queue_lock);
13496 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13497 mtx_unlock(&thr->queue_lock);
13502 ctl_enqueue_rtr(union ctl_io *io)
13504 struct ctl_softc *softc = CTL_SOFTC(io);
13505 struct ctl_thread *thr;
13507 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13508 mtx_lock(&thr->queue_lock);
13509 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13510 mtx_unlock(&thr->queue_lock);
13515 ctl_enqueue_done(union ctl_io *io)
13517 struct ctl_softc *softc = CTL_SOFTC(io);
13518 struct ctl_thread *thr;
13520 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13521 mtx_lock(&thr->queue_lock);
13522 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13523 mtx_unlock(&thr->queue_lock);
13528 ctl_enqueue_isc(union ctl_io *io)
13530 struct ctl_softc *softc = CTL_SOFTC(io);
13531 struct ctl_thread *thr;
13533 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13534 mtx_lock(&thr->queue_lock);
13535 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13536 mtx_unlock(&thr->queue_lock);