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>
70 #include <sys/sched.h>
71 #include <sys/sysctl.h>
77 #include <cam/scsi/scsi_all.h>
78 #include <cam/scsi/scsi_cd.h>
79 #include <cam/scsi/scsi_da.h>
80 #include <cam/ctl/ctl_io.h>
81 #include <cam/ctl/ctl.h>
82 #include <cam/ctl/ctl_frontend.h>
83 #include <cam/ctl/ctl_util.h>
84 #include <cam/ctl/ctl_backend.h>
85 #include <cam/ctl/ctl_ioctl.h>
86 #include <cam/ctl/ctl_ha.h>
87 #include <cam/ctl/ctl_private.h>
88 #include <cam/ctl/ctl_debug.h>
89 #include <cam/ctl/ctl_scsi_all.h>
90 #include <cam/ctl/ctl_error.h>
92 struct ctl_softc *control_softc = NULL;
95 * Template mode pages.
99 * Note that these are default values only. The actual values will be
100 * filled in when the user does a mode sense.
102 const static struct scsi_da_rw_recovery_page rw_er_page_default = {
103 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
104 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
105 /*byte3*/SMS_RWER_AWRE|SMS_RWER_ARRE,
106 /*read_retry_count*/0,
107 /*correction_span*/0,
108 /*head_offset_count*/0,
109 /*data_strobe_offset_cnt*/0,
110 /*byte8*/SMS_RWER_LBPERE,
111 /*write_retry_count*/0,
113 /*recovery_time_limit*/{0, 0},
116 const static struct scsi_da_rw_recovery_page rw_er_page_changeable = {
117 /*page_code*/SMS_RW_ERROR_RECOVERY_PAGE,
118 /*page_length*/sizeof(struct scsi_da_rw_recovery_page) - 2,
119 /*byte3*/SMS_RWER_PER,
120 /*read_retry_count*/0,
121 /*correction_span*/0,
122 /*head_offset_count*/0,
123 /*data_strobe_offset_cnt*/0,
124 /*byte8*/SMS_RWER_LBPERE,
125 /*write_retry_count*/0,
127 /*recovery_time_limit*/{0, 0},
130 const static struct scsi_format_page format_page_default = {
131 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
132 /*page_length*/sizeof(struct scsi_format_page) - 2,
133 /*tracks_per_zone*/ {0, 0},
134 /*alt_sectors_per_zone*/ {0, 0},
135 /*alt_tracks_per_zone*/ {0, 0},
136 /*alt_tracks_per_lun*/ {0, 0},
137 /*sectors_per_track*/ {(CTL_DEFAULT_SECTORS_PER_TRACK >> 8) & 0xff,
138 CTL_DEFAULT_SECTORS_PER_TRACK & 0xff},
139 /*bytes_per_sector*/ {0, 0},
140 /*interleave*/ {0, 0},
141 /*track_skew*/ {0, 0},
142 /*cylinder_skew*/ {0, 0},
144 /*reserved*/ {0, 0, 0}
147 const static struct scsi_format_page format_page_changeable = {
148 /*page_code*/SMS_FORMAT_DEVICE_PAGE,
149 /*page_length*/sizeof(struct scsi_format_page) - 2,
150 /*tracks_per_zone*/ {0, 0},
151 /*alt_sectors_per_zone*/ {0, 0},
152 /*alt_tracks_per_zone*/ {0, 0},
153 /*alt_tracks_per_lun*/ {0, 0},
154 /*sectors_per_track*/ {0, 0},
155 /*bytes_per_sector*/ {0, 0},
156 /*interleave*/ {0, 0},
157 /*track_skew*/ {0, 0},
158 /*cylinder_skew*/ {0, 0},
160 /*reserved*/ {0, 0, 0}
163 const static struct scsi_rigid_disk_page rigid_disk_page_default = {
164 /*page_code*/SMS_RIGID_DISK_PAGE,
165 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
166 /*cylinders*/ {0, 0, 0},
167 /*heads*/ CTL_DEFAULT_HEADS,
168 /*start_write_precomp*/ {0, 0, 0},
169 /*start_reduced_current*/ {0, 0, 0},
170 /*step_rate*/ {0, 0},
171 /*landing_zone_cylinder*/ {0, 0, 0},
172 /*rpl*/ SRDP_RPL_DISABLED,
173 /*rotational_offset*/ 0,
175 /*rotation_rate*/ {(CTL_DEFAULT_ROTATION_RATE >> 8) & 0xff,
176 CTL_DEFAULT_ROTATION_RATE & 0xff},
180 const static struct scsi_rigid_disk_page rigid_disk_page_changeable = {
181 /*page_code*/SMS_RIGID_DISK_PAGE,
182 /*page_length*/sizeof(struct scsi_rigid_disk_page) - 2,
183 /*cylinders*/ {0, 0, 0},
185 /*start_write_precomp*/ {0, 0, 0},
186 /*start_reduced_current*/ {0, 0, 0},
187 /*step_rate*/ {0, 0},
188 /*landing_zone_cylinder*/ {0, 0, 0},
190 /*rotational_offset*/ 0,
192 /*rotation_rate*/ {0, 0},
196 const static struct scsi_da_verify_recovery_page verify_er_page_default = {
197 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
198 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
200 /*read_retry_count*/0,
201 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
202 /*recovery_time_limit*/{0, 0},
205 const static struct scsi_da_verify_recovery_page verify_er_page_changeable = {
206 /*page_code*/SMS_VERIFY_ERROR_RECOVERY_PAGE,
207 /*page_length*/sizeof(struct scsi_da_verify_recovery_page) - 2,
208 /*byte3*/SMS_VER_PER,
209 /*read_retry_count*/0,
210 /*reserved*/{ 0, 0, 0, 0, 0, 0 },
211 /*recovery_time_limit*/{0, 0},
214 const static struct scsi_caching_page caching_page_default = {
215 /*page_code*/SMS_CACHING_PAGE,
216 /*page_length*/sizeof(struct scsi_caching_page) - 2,
217 /*flags1*/ SCP_DISC | SCP_WCE,
219 /*disable_pf_transfer_len*/ {0xff, 0xff},
220 /*min_prefetch*/ {0, 0},
221 /*max_prefetch*/ {0xff, 0xff},
222 /*max_pf_ceiling*/ {0xff, 0xff},
224 /*cache_segments*/ 0,
225 /*cache_seg_size*/ {0, 0},
227 /*non_cache_seg_size*/ {0, 0, 0}
230 const static struct scsi_caching_page caching_page_changeable = {
231 /*page_code*/SMS_CACHING_PAGE,
232 /*page_length*/sizeof(struct scsi_caching_page) - 2,
233 /*flags1*/ SCP_WCE | SCP_RCD,
235 /*disable_pf_transfer_len*/ {0, 0},
236 /*min_prefetch*/ {0, 0},
237 /*max_prefetch*/ {0, 0},
238 /*max_pf_ceiling*/ {0, 0},
240 /*cache_segments*/ 0,
241 /*cache_seg_size*/ {0, 0},
243 /*non_cache_seg_size*/ {0, 0, 0}
246 const static struct scsi_control_page control_page_default = {
247 /*page_code*/SMS_CONTROL_MODE_PAGE,
248 /*page_length*/sizeof(struct scsi_control_page) - 2,
250 /*queue_flags*/SCP_QUEUE_ALG_RESTRICTED,
253 /*aen_holdoff_period*/{0, 0},
254 /*busy_timeout_period*/{0, 0},
255 /*extended_selftest_completion_time*/{0, 0}
258 const static struct scsi_control_page control_page_changeable = {
259 /*page_code*/SMS_CONTROL_MODE_PAGE,
260 /*page_length*/sizeof(struct scsi_control_page) - 2,
262 /*queue_flags*/SCP_QUEUE_ALG_MASK | SCP_NUAR,
263 /*eca_and_aen*/SCP_SWP,
265 /*aen_holdoff_period*/{0, 0},
266 /*busy_timeout_period*/{0, 0},
267 /*extended_selftest_completion_time*/{0, 0}
270 #define CTL_CEM_LEN (sizeof(struct scsi_control_ext_page) - 4)
272 const static struct scsi_control_ext_page control_ext_page_default = {
273 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
274 /*subpage_code*/0x01,
275 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
281 const static struct scsi_control_ext_page control_ext_page_changeable = {
282 /*page_code*/SMS_CONTROL_MODE_PAGE | SMPH_SPF,
283 /*subpage_code*/0x01,
284 /*page_length*/{CTL_CEM_LEN >> 8, CTL_CEM_LEN},
290 const static struct scsi_info_exceptions_page ie_page_default = {
291 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
292 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
293 /*info_flags*/SIEP_FLAGS_EWASC,
294 /*mrie*/SIEP_MRIE_NO,
295 /*interval_timer*/{0, 0, 0, 0},
296 /*report_count*/{0, 0, 0, 1}
299 const static struct scsi_info_exceptions_page ie_page_changeable = {
300 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE,
301 /*page_length*/sizeof(struct scsi_info_exceptions_page) - 2,
302 /*info_flags*/SIEP_FLAGS_EWASC | SIEP_FLAGS_DEXCPT | SIEP_FLAGS_TEST |
305 /*interval_timer*/{0xff, 0xff, 0xff, 0xff},
306 /*report_count*/{0xff, 0xff, 0xff, 0xff}
309 #define CTL_LBPM_LEN (sizeof(struct ctl_logical_block_provisioning_page) - 4)
311 const static struct ctl_logical_block_provisioning_page lbp_page_default = {{
312 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
313 /*subpage_code*/0x02,
314 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
316 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
321 /*count*/{0, 0, 0, 0}},
325 /*count*/{0, 0, 0, 0}},
329 /*count*/{0, 0, 0, 0}},
333 /*count*/{0, 0, 0, 0}}
337 const static struct ctl_logical_block_provisioning_page lbp_page_changeable = {{
338 /*page_code*/SMS_INFO_EXCEPTIONS_PAGE | SMPH_SPF,
339 /*subpage_code*/0x02,
340 /*page_length*/{CTL_LBPM_LEN >> 8, CTL_LBPM_LEN},
341 /*flags*/SLBPP_SITUA,
342 /*reserved*/{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
347 /*count*/{0, 0, 0, 0}},
351 /*count*/{0, 0, 0, 0}},
355 /*count*/{0, 0, 0, 0}},
359 /*count*/{0, 0, 0, 0}}
363 const static struct scsi_cddvd_capabilities_page cddvd_page_default = {
364 /*page_code*/SMS_CDDVD_CAPS_PAGE,
365 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
373 /*nvol_levels*/{0, 0},
374 /*buffer_size*/{8, 0},
379 /*copy_management*/0,
381 /*rotation_control*/0,
382 /*cur_write_speed*/0,
383 /*num_speed_descr*/0,
386 const static struct scsi_cddvd_capabilities_page cddvd_page_changeable = {
387 /*page_code*/SMS_CDDVD_CAPS_PAGE,
388 /*page_length*/sizeof(struct scsi_cddvd_capabilities_page) - 2,
396 /*nvol_levels*/{0, 0},
397 /*buffer_size*/{0, 0},
402 /*copy_management*/0,
404 /*rotation_control*/0,
405 /*cur_write_speed*/0,
406 /*num_speed_descr*/0,
409 SYSCTL_NODE(_kern_cam, OID_AUTO, ctl, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
411 static int worker_threads = -1;
412 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
413 &worker_threads, 1, "Number of worker threads");
414 static int ctl_debug = CTL_DEBUG_NONE;
415 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
416 &ctl_debug, 0, "Enabled debug flags");
417 static int ctl_lun_map_size = 1024;
418 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
419 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
421 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS;
422 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN,
423 &ctl_time_io_secs, 0, "Log requests taking more seconds");
427 * Maximum number of LUNs we support. MUST be a power of 2.
429 #define CTL_DEFAULT_MAX_LUNS 1024
430 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
431 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns);
432 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN,
433 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs");
436 * Maximum number of ports registered at one time.
438 #define CTL_DEFAULT_MAX_PORTS 256
439 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
440 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports);
441 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN,
442 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports");
445 * Maximum number of initiators we support.
447 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports)
450 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
451 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
452 * SCSI Ports (0x88), Third-party Copy (0x8F), SCSI Feature Sets (0x92),
453 * Block limits (0xB0), Block Device Characteristics (0xB1) and
454 * Logical Block Provisioning (0xB2)
456 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 11
458 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
460 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
461 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
462 static int ctl_init(void);
463 static int ctl_shutdown(void);
464 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
465 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
466 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
467 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
468 struct ctl_ooa *ooa_hdr,
469 struct ctl_ooa_entry *kern_entries);
470 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
472 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
473 struct ctl_be_lun *be_lun);
474 static int ctl_free_lun(struct ctl_lun *lun);
475 static void ctl_create_lun(struct ctl_be_lun *be_lun);
477 static int ctl_do_mode_select(union ctl_io *io);
478 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
479 uint64_t res_key, uint64_t sa_res_key,
480 uint8_t type, uint32_t residx,
481 struct ctl_scsiio *ctsio,
482 struct scsi_per_res_out *cdb,
483 struct scsi_per_res_out_parms* param);
484 static void ctl_pro_preempt_other(struct ctl_lun *lun,
485 union ctl_ha_msg *msg);
486 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
487 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
488 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
489 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
490 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
491 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
492 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
494 static int ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len);
495 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
497 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
498 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
499 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
500 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
501 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
502 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
504 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
505 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
506 union ctl_io *pending_io, union ctl_io *ooa_io);
507 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
508 union ctl_io **starting_io);
509 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io,
511 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io,
513 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
514 const struct ctl_cmd_entry *entry,
515 struct ctl_scsiio *ctsio);
516 static void ctl_failover_lun(union ctl_io *io);
517 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
518 struct ctl_scsiio *ctsio);
519 static int ctl_scsiio(struct ctl_scsiio *ctsio);
521 static int ctl_target_reset(union ctl_io *io);
522 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
523 ctl_ua_type ua_type);
524 static int ctl_lun_reset(union ctl_io *io);
525 static int ctl_abort_task(union ctl_io *io);
526 static int ctl_abort_task_set(union ctl_io *io);
527 static int ctl_query_task(union ctl_io *io, int task_set);
528 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
529 ctl_ua_type ua_type);
530 static int ctl_i_t_nexus_reset(union ctl_io *io);
531 static int ctl_query_async_event(union ctl_io *io);
532 static void ctl_run_task(union ctl_io *io);
534 static void ctl_datamove_timer_wakeup(void *arg);
535 static void ctl_done_timer_wakeup(void *arg);
536 #endif /* CTL_IO_DELAY */
538 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
539 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
540 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
541 static void ctl_datamove_remote_write(union ctl_io *io);
542 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
543 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
544 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
545 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
546 ctl_ha_dt_cb callback);
547 static void ctl_datamove_remote_read(union ctl_io *io);
548 static void ctl_datamove_remote(union ctl_io *io);
549 static void ctl_process_done(union ctl_io *io);
550 static void ctl_lun_thread(void *arg);
551 static void ctl_thresh_thread(void *arg);
552 static void ctl_work_thread(void *arg);
553 static void ctl_enqueue_incoming(union ctl_io *io);
554 static void ctl_enqueue_rtr(union ctl_io *io);
555 static void ctl_enqueue_done(union ctl_io *io);
556 static void ctl_enqueue_isc(union ctl_io *io);
557 static const struct ctl_cmd_entry *
558 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
559 static const struct ctl_cmd_entry *
560 ctl_validate_command(struct ctl_scsiio *ctsio);
561 static int ctl_cmd_applicable(uint8_t lun_type,
562 const struct ctl_cmd_entry *entry);
563 static int ctl_ha_init(void);
564 static int ctl_ha_shutdown(void);
566 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
567 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
568 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
569 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
572 * Load the serialization table. This isn't very pretty, but is probably
573 * the easiest way to do it.
575 #include "ctl_ser_table.c"
578 * We only need to define open, close and ioctl routines for this driver.
580 static struct cdevsw ctl_cdevsw = {
581 .d_version = D_VERSION,
584 .d_close = ctl_close,
585 .d_ioctl = ctl_ioctl,
590 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
592 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
594 static moduledata_t ctl_moduledata = {
596 ctl_module_event_handler,
600 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
601 MODULE_VERSION(ctl, 1);
603 static struct ctl_frontend ha_frontend =
607 .shutdown = ctl_ha_shutdown,
613 struct ctl_softc *softc = control_softc;
615 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
616 &softc->othersc_pool) != 0)
618 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
619 ctl_pool_free(softc->othersc_pool);
622 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
623 != CTL_HA_STATUS_SUCCESS) {
624 ctl_ha_msg_destroy(softc);
625 ctl_pool_free(softc->othersc_pool);
632 ctl_ha_shutdown(void)
634 struct ctl_softc *softc = control_softc;
635 struct ctl_port *port;
637 ctl_ha_msg_shutdown(softc);
638 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
640 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
642 ctl_pool_free(softc->othersc_pool);
643 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
644 ctl_port_deregister(port);
645 free(port->port_name, M_CTL);
652 ctl_ha_datamove(union ctl_io *io)
654 struct ctl_lun *lun = CTL_LUN(io);
655 struct ctl_sg_entry *sgl;
656 union ctl_ha_msg msg;
657 uint32_t sg_entries_sent;
658 int do_sg_copy, i, j;
660 memset(&msg.dt, 0, sizeof(msg.dt));
661 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
662 msg.hdr.original_sc = io->io_hdr.remote_io;
663 msg.hdr.serializing_sc = io;
664 msg.hdr.nexus = io->io_hdr.nexus;
665 msg.hdr.status = io->io_hdr.status;
666 msg.dt.flags = io->io_hdr.flags;
669 * We convert everything into a S/G list here. We can't
670 * pass by reference, only by value between controllers.
671 * So we can't pass a pointer to the S/G list, only as many
672 * S/G entries as we can fit in here. If it's possible for
673 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
674 * then we need to break this up into multiple transfers.
676 if (io->scsiio.kern_sg_entries == 0) {
677 msg.dt.kern_sg_entries = 1;
679 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
680 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
682 /* XXX KDM use busdma here! */
683 msg.dt.sg_list[0].addr =
684 (void *)vtophys(io->scsiio.kern_data_ptr);
687 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
688 ("HA does not support BUS_ADDR"));
689 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
691 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
694 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
698 msg.dt.kern_data_len = io->scsiio.kern_data_len;
699 msg.dt.kern_total_len = io->scsiio.kern_total_len;
700 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
701 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
702 msg.dt.sg_sequence = 0;
705 * Loop until we've sent all of the S/G entries. On the
706 * other end, we'll recompose these S/G entries into one
707 * contiguous list before processing.
709 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
710 msg.dt.sg_sequence++) {
711 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
712 sizeof(msg.dt.sg_list[0])),
713 msg.dt.kern_sg_entries - sg_entries_sent);
714 if (do_sg_copy != 0) {
715 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
716 for (i = sg_entries_sent, j = 0;
717 i < msg.dt.cur_sg_entries; i++, j++) {
719 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
720 msg.dt.sg_list[j].addr = sgl[i].addr;
722 /* XXX KDM use busdma here! */
723 msg.dt.sg_list[j].addr =
724 (void *)vtophys(sgl[i].addr);
727 KASSERT((io->io_hdr.flags &
728 CTL_FLAG_BUS_ADDR) == 0,
729 ("HA does not support BUS_ADDR"));
730 msg.dt.sg_list[j].addr = sgl[i].addr;
732 msg.dt.sg_list[j].len = sgl[i].len;
736 sg_entries_sent += msg.dt.cur_sg_entries;
737 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
738 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
739 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
740 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
741 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
742 io->io_hdr.port_status = 31341;
743 io->scsiio.be_move_done(io);
746 msg.dt.sent_sg_entries = sg_entries_sent;
750 * Officially handover the request from us to peer.
751 * If failover has just happened, then we must return error.
752 * If failover happen just after, then it is not our problem.
755 mtx_lock(&lun->lun_lock);
756 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
758 mtx_unlock(&lun->lun_lock);
759 io->io_hdr.port_status = 31342;
760 io->scsiio.be_move_done(io);
763 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
764 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
766 mtx_unlock(&lun->lun_lock);
770 ctl_ha_done(union ctl_io *io)
772 union ctl_ha_msg msg;
774 if (io->io_hdr.io_type == CTL_IO_SCSI) {
775 memset(&msg, 0, sizeof(msg));
776 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
777 msg.hdr.original_sc = io->io_hdr.remote_io;
778 msg.hdr.nexus = io->io_hdr.nexus;
779 msg.hdr.status = io->io_hdr.status;
780 msg.scsi.scsi_status = io->scsiio.scsi_status;
781 msg.scsi.tag_num = io->scsiio.tag_num;
782 msg.scsi.tag_type = io->scsiio.tag_type;
783 msg.scsi.sense_len = io->scsiio.sense_len;
784 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
785 io->scsiio.sense_len);
786 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
787 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
788 msg.scsi.sense_len, M_WAITOK);
794 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
795 union ctl_ha_msg *msg_info)
797 struct ctl_scsiio *ctsio;
799 if (msg_info->hdr.original_sc == NULL) {
800 printf("%s: original_sc == NULL!\n", __func__);
801 /* XXX KDM now what? */
805 ctsio = &msg_info->hdr.original_sc->scsiio;
806 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
807 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
808 ctsio->io_hdr.status = msg_info->hdr.status;
809 ctsio->scsi_status = msg_info->scsi.scsi_status;
810 ctsio->sense_len = msg_info->scsi.sense_len;
811 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
812 msg_info->scsi.sense_len);
813 ctl_enqueue_isc((union ctl_io *)ctsio);
817 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
818 union ctl_ha_msg *msg_info)
820 struct ctl_scsiio *ctsio;
822 if (msg_info->hdr.serializing_sc == NULL) {
823 printf("%s: serializing_sc == NULL!\n", __func__);
824 /* XXX KDM now what? */
828 ctsio = &msg_info->hdr.serializing_sc->scsiio;
829 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
830 ctl_enqueue_isc((union ctl_io *)ctsio);
834 ctl_isc_announce_lun(struct ctl_lun *lun)
836 struct ctl_softc *softc = lun->ctl_softc;
837 union ctl_ha_msg *msg;
838 struct ctl_ha_msg_lun_pr_key pr_key;
841 if (softc->ha_link != CTL_HA_LINK_ONLINE)
843 mtx_lock(&lun->lun_lock);
844 i = sizeof(msg->lun);
846 i += lun->lun_devid->len;
847 i += sizeof(pr_key) * lun->pr_key_count;
849 mtx_unlock(&lun->lun_lock);
850 msg = malloc(i, M_CTL, M_WAITOK);
851 mtx_lock(&lun->lun_lock);
852 k = sizeof(msg->lun);
854 k += lun->lun_devid->len;
855 k += sizeof(pr_key) * lun->pr_key_count;
861 bzero(&msg->lun, sizeof(msg->lun));
862 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
863 msg->hdr.nexus.targ_lun = lun->lun;
864 msg->hdr.nexus.targ_mapped_lun = lun->lun;
865 msg->lun.flags = lun->flags;
866 msg->lun.pr_generation = lun->pr_generation;
867 msg->lun.pr_res_idx = lun->pr_res_idx;
868 msg->lun.pr_res_type = lun->pr_res_type;
869 msg->lun.pr_key_count = lun->pr_key_count;
871 if (lun->lun_devid) {
872 msg->lun.lun_devid_len = lun->lun_devid->len;
873 memcpy(&msg->lun.data[i], lun->lun_devid->data,
874 msg->lun.lun_devid_len);
875 i += msg->lun.lun_devid_len;
877 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
878 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
881 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
884 mtx_unlock(&lun->lun_lock);
885 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
889 if (lun->flags & CTL_LUN_PRIMARY_SC) {
890 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
891 ctl_isc_announce_mode(lun, -1,
892 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
893 lun->mode_pages.index[i].subpage);
899 ctl_isc_announce_port(struct ctl_port *port)
901 struct ctl_softc *softc = port->ctl_softc;
902 union ctl_ha_msg *msg;
905 if (port->targ_port < softc->port_min ||
906 port->targ_port >= softc->port_max ||
907 softc->ha_link != CTL_HA_LINK_ONLINE)
909 i = sizeof(msg->port) + strlen(port->port_name) + 1;
911 i += port->lun_map_size * sizeof(uint32_t);
912 if (port->port_devid)
913 i += port->port_devid->len;
914 if (port->target_devid)
915 i += port->target_devid->len;
916 if (port->init_devid)
917 i += port->init_devid->len;
918 msg = malloc(i, M_CTL, M_WAITOK);
919 bzero(&msg->port, sizeof(msg->port));
920 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
921 msg->hdr.nexus.targ_port = port->targ_port;
922 msg->port.port_type = port->port_type;
923 msg->port.physical_port = port->physical_port;
924 msg->port.virtual_port = port->virtual_port;
925 msg->port.status = port->status;
927 msg->port.name_len = sprintf(&msg->port.data[i],
928 "%d:%s", softc->ha_id, port->port_name) + 1;
929 i += msg->port.name_len;
931 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
932 memcpy(&msg->port.data[i], port->lun_map,
933 msg->port.lun_map_len);
934 i += msg->port.lun_map_len;
936 if (port->port_devid) {
937 msg->port.port_devid_len = port->port_devid->len;
938 memcpy(&msg->port.data[i], port->port_devid->data,
939 msg->port.port_devid_len);
940 i += msg->port.port_devid_len;
942 if (port->target_devid) {
943 msg->port.target_devid_len = port->target_devid->len;
944 memcpy(&msg->port.data[i], port->target_devid->data,
945 msg->port.target_devid_len);
946 i += msg->port.target_devid_len;
948 if (port->init_devid) {
949 msg->port.init_devid_len = port->init_devid->len;
950 memcpy(&msg->port.data[i], port->init_devid->data,
951 msg->port.init_devid_len);
952 i += msg->port.init_devid_len;
954 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
960 ctl_isc_announce_iid(struct ctl_port *port, int iid)
962 struct ctl_softc *softc = port->ctl_softc;
963 union ctl_ha_msg *msg;
966 if (port->targ_port < softc->port_min ||
967 port->targ_port >= softc->port_max ||
968 softc->ha_link != CTL_HA_LINK_ONLINE)
970 mtx_lock(&softc->ctl_lock);
971 i = sizeof(msg->iid);
973 if (port->wwpn_iid[iid].name)
974 l = strlen(port->wwpn_iid[iid].name) + 1;
976 msg = malloc(i, M_CTL, M_NOWAIT);
978 mtx_unlock(&softc->ctl_lock);
981 bzero(&msg->iid, sizeof(msg->iid));
982 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
983 msg->hdr.nexus.targ_port = port->targ_port;
984 msg->hdr.nexus.initid = iid;
985 msg->iid.in_use = port->wwpn_iid[iid].in_use;
986 msg->iid.name_len = l;
987 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
988 if (port->wwpn_iid[iid].name)
989 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
990 mtx_unlock(&softc->ctl_lock);
991 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
996 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
997 uint8_t page, uint8_t subpage)
999 struct ctl_softc *softc = lun->ctl_softc;
1000 union ctl_ha_msg msg;
1003 if (softc->ha_link != CTL_HA_LINK_ONLINE)
1005 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1006 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1007 page && lun->mode_pages.index[i].subpage == subpage)
1010 if (i == CTL_NUM_MODE_PAGES)
1013 /* Don't try to replicate pages not present on this device. */
1014 if (lun->mode_pages.index[i].page_data == NULL)
1017 bzero(&msg.mode, sizeof(msg.mode));
1018 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
1019 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
1020 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
1021 msg.hdr.nexus.targ_lun = lun->lun;
1022 msg.hdr.nexus.targ_mapped_lun = lun->lun;
1023 msg.mode.page_code = page;
1024 msg.mode.subpage = subpage;
1025 msg.mode.page_len = lun->mode_pages.index[i].page_len;
1026 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
1028 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
1033 ctl_isc_ha_link_up(struct ctl_softc *softc)
1035 struct ctl_port *port;
1036 struct ctl_lun *lun;
1037 union ctl_ha_msg msg;
1040 /* Announce this node parameters to peer for validation. */
1041 msg.login.msg_type = CTL_MSG_LOGIN;
1042 msg.login.version = CTL_HA_VERSION;
1043 msg.login.ha_mode = softc->ha_mode;
1044 msg.login.ha_id = softc->ha_id;
1045 msg.login.max_luns = ctl_max_luns;
1046 msg.login.max_ports = ctl_max_ports;
1047 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1048 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1051 STAILQ_FOREACH(port, &softc->port_list, links) {
1052 ctl_isc_announce_port(port);
1053 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1054 if (port->wwpn_iid[i].in_use)
1055 ctl_isc_announce_iid(port, i);
1058 STAILQ_FOREACH(lun, &softc->lun_list, links)
1059 ctl_isc_announce_lun(lun);
1063 ctl_isc_ha_link_down(struct ctl_softc *softc)
1065 struct ctl_port *port;
1066 struct ctl_lun *lun;
1070 mtx_lock(&softc->ctl_lock);
1071 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1072 mtx_lock(&lun->lun_lock);
1073 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1074 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1075 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1077 mtx_unlock(&lun->lun_lock);
1079 mtx_unlock(&softc->ctl_lock);
1080 io = ctl_alloc_io(softc->othersc_pool);
1081 mtx_lock(&softc->ctl_lock);
1083 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1084 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1085 ctl_enqueue_isc(io);
1088 STAILQ_FOREACH(port, &softc->port_list, links) {
1089 if (port->targ_port >= softc->port_min &&
1090 port->targ_port < softc->port_max)
1092 port->status &= ~CTL_PORT_STATUS_ONLINE;
1093 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1094 port->wwpn_iid[i].in_use = 0;
1095 free(port->wwpn_iid[i].name, M_CTL);
1096 port->wwpn_iid[i].name = NULL;
1099 mtx_unlock(&softc->ctl_lock);
1103 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1105 struct ctl_lun *lun;
1106 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1108 mtx_lock(&softc->ctl_lock);
1109 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns ||
1110 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1111 mtx_unlock(&softc->ctl_lock);
1114 mtx_lock(&lun->lun_lock);
1115 mtx_unlock(&softc->ctl_lock);
1116 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1117 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1118 if (msg->ua.ua_all) {
1120 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1122 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1125 ctl_est_ua(lun, iid, msg->ua.ua_type);
1127 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1129 mtx_unlock(&lun->lun_lock);
1133 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1135 struct ctl_lun *lun;
1136 struct ctl_ha_msg_lun_pr_key pr_key;
1138 ctl_lun_flags oflags;
1141 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1142 mtx_lock(&softc->ctl_lock);
1143 if (targ_lun >= ctl_max_luns ||
1144 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1145 mtx_unlock(&softc->ctl_lock);
1148 mtx_lock(&lun->lun_lock);
1149 mtx_unlock(&softc->ctl_lock);
1150 if (lun->flags & CTL_LUN_DISABLED) {
1151 mtx_unlock(&lun->lun_lock);
1154 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1155 if (msg->lun.lun_devid_len != i || (i > 0 &&
1156 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1157 mtx_unlock(&lun->lun_lock);
1158 printf("%s: Received conflicting HA LUN %d\n",
1159 __func__, targ_lun);
1162 /* Record whether peer is primary. */
1163 oflags = lun->flags;
1164 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1165 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1166 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1168 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1169 if (oflags != lun->flags)
1170 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1172 /* If peer is primary and we are not -- use data */
1173 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1174 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1175 lun->pr_generation = msg->lun.pr_generation;
1176 lun->pr_res_idx = msg->lun.pr_res_idx;
1177 lun->pr_res_type = msg->lun.pr_res_type;
1178 lun->pr_key_count = msg->lun.pr_key_count;
1179 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1180 ctl_clr_prkey(lun, k);
1181 for (k = 0; k < msg->lun.pr_key_count; k++) {
1182 memcpy(&pr_key, &msg->lun.data[i],
1184 ctl_alloc_prkey(lun, pr_key.pr_iid);
1185 ctl_set_prkey(lun, pr_key.pr_iid,
1187 i += sizeof(pr_key);
1191 mtx_unlock(&lun->lun_lock);
1192 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1194 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1195 "primary" : "secondary"));
1197 /* If we are primary but peer doesn't know -- notify */
1198 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1199 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1200 ctl_isc_announce_lun(lun);
1205 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1207 struct ctl_port *port;
1208 struct ctl_lun *lun;
1211 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1213 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1214 msg->hdr.nexus.targ_port));
1216 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1217 port->frontend = &ha_frontend;
1218 port->targ_port = msg->hdr.nexus.targ_port;
1219 port->fe_datamove = ctl_ha_datamove;
1220 port->fe_done = ctl_ha_done;
1221 } else if (port->frontend == &ha_frontend) {
1222 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1223 msg->hdr.nexus.targ_port));
1226 printf("%s: Received conflicting HA port %d\n",
1227 __func__, msg->hdr.nexus.targ_port);
1230 port->port_type = msg->port.port_type;
1231 port->physical_port = msg->port.physical_port;
1232 port->virtual_port = msg->port.virtual_port;
1233 port->status = msg->port.status;
1235 free(port->port_name, M_CTL);
1236 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1238 i += msg->port.name_len;
1239 if (msg->port.lun_map_len != 0) {
1240 if (port->lun_map == NULL ||
1241 port->lun_map_size * sizeof(uint32_t) <
1242 msg->port.lun_map_len) {
1243 port->lun_map_size = 0;
1244 free(port->lun_map, M_CTL);
1245 port->lun_map = malloc(msg->port.lun_map_len,
1248 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1249 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1250 i += msg->port.lun_map_len;
1252 port->lun_map_size = 0;
1253 free(port->lun_map, M_CTL);
1254 port->lun_map = NULL;
1256 if (msg->port.port_devid_len != 0) {
1257 if (port->port_devid == NULL ||
1258 port->port_devid->len < msg->port.port_devid_len) {
1259 free(port->port_devid, M_CTL);
1260 port->port_devid = malloc(sizeof(struct ctl_devid) +
1261 msg->port.port_devid_len, M_CTL, M_WAITOK);
1263 memcpy(port->port_devid->data, &msg->port.data[i],
1264 msg->port.port_devid_len);
1265 port->port_devid->len = msg->port.port_devid_len;
1266 i += msg->port.port_devid_len;
1268 free(port->port_devid, M_CTL);
1269 port->port_devid = NULL;
1271 if (msg->port.target_devid_len != 0) {
1272 if (port->target_devid == NULL ||
1273 port->target_devid->len < msg->port.target_devid_len) {
1274 free(port->target_devid, M_CTL);
1275 port->target_devid = malloc(sizeof(struct ctl_devid) +
1276 msg->port.target_devid_len, M_CTL, M_WAITOK);
1278 memcpy(port->target_devid->data, &msg->port.data[i],
1279 msg->port.target_devid_len);
1280 port->target_devid->len = msg->port.target_devid_len;
1281 i += msg->port.target_devid_len;
1283 free(port->target_devid, M_CTL);
1284 port->target_devid = NULL;
1286 if (msg->port.init_devid_len != 0) {
1287 if (port->init_devid == NULL ||
1288 port->init_devid->len < msg->port.init_devid_len) {
1289 free(port->init_devid, M_CTL);
1290 port->init_devid = malloc(sizeof(struct ctl_devid) +
1291 msg->port.init_devid_len, M_CTL, M_WAITOK);
1293 memcpy(port->init_devid->data, &msg->port.data[i],
1294 msg->port.init_devid_len);
1295 port->init_devid->len = msg->port.init_devid_len;
1296 i += msg->port.init_devid_len;
1298 free(port->init_devid, M_CTL);
1299 port->init_devid = NULL;
1302 if (ctl_port_register(port) != 0) {
1303 printf("%s: ctl_port_register() failed with error\n",
1307 mtx_lock(&softc->ctl_lock);
1308 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1309 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1311 mtx_lock(&lun->lun_lock);
1312 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1313 mtx_unlock(&lun->lun_lock);
1315 mtx_unlock(&softc->ctl_lock);
1319 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1321 struct ctl_port *port;
1324 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1326 printf("%s: Received IID for unknown port %d\n",
1327 __func__, msg->hdr.nexus.targ_port);
1330 iid = msg->hdr.nexus.initid;
1331 if (port->wwpn_iid[iid].in_use != 0 &&
1332 msg->iid.in_use == 0)
1333 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1334 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1335 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1336 free(port->wwpn_iid[iid].name, M_CTL);
1337 if (msg->iid.name_len) {
1338 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1339 msg->iid.name_len, M_CTL);
1341 port->wwpn_iid[iid].name = NULL;
1345 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1348 if (msg->login.version != CTL_HA_VERSION) {
1349 printf("CTL HA peers have different versions %d != %d\n",
1350 msg->login.version, CTL_HA_VERSION);
1351 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1354 if (msg->login.ha_mode != softc->ha_mode) {
1355 printf("CTL HA peers have different ha_mode %d != %d\n",
1356 msg->login.ha_mode, softc->ha_mode);
1357 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1360 if (msg->login.ha_id == softc->ha_id) {
1361 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1362 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1365 if (msg->login.max_luns != ctl_max_luns ||
1366 msg->login.max_ports != ctl_max_ports ||
1367 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1368 printf("CTL HA peers have different limits\n");
1369 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1375 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1377 struct ctl_lun *lun;
1379 uint32_t initidx, targ_lun;
1381 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1382 mtx_lock(&softc->ctl_lock);
1383 if (targ_lun >= ctl_max_luns ||
1384 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1385 mtx_unlock(&softc->ctl_lock);
1388 mtx_lock(&lun->lun_lock);
1389 mtx_unlock(&softc->ctl_lock);
1390 if (lun->flags & CTL_LUN_DISABLED) {
1391 mtx_unlock(&lun->lun_lock);
1394 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1395 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1396 msg->mode.page_code &&
1397 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1400 if (i == CTL_NUM_MODE_PAGES) {
1401 mtx_unlock(&lun->lun_lock);
1404 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1405 lun->mode_pages.index[i].page_len);
1406 initidx = ctl_get_initindex(&msg->hdr.nexus);
1408 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1409 mtx_unlock(&lun->lun_lock);
1413 * ISC (Inter Shelf Communication) event handler. Events from the HA
1414 * subsystem come in here.
1417 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1419 struct ctl_softc *softc = control_softc;
1421 struct ctl_prio *presio;
1422 ctl_ha_status isc_status;
1424 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1425 if (event == CTL_HA_EVT_MSG_RECV) {
1426 union ctl_ha_msg *msg, msgbuf;
1428 if (param > sizeof(msgbuf))
1429 msg = malloc(param, M_CTL, M_WAITOK);
1432 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1434 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1435 printf("%s: Error receiving message: %d\n",
1436 __func__, isc_status);
1442 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->hdr.msg_type));
1443 switch (msg->hdr.msg_type) {
1444 case CTL_MSG_SERIALIZE:
1445 io = ctl_alloc_io(softc->othersc_pool);
1447 // populate ctsio from msg
1448 io->io_hdr.io_type = CTL_IO_SCSI;
1449 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1450 io->io_hdr.remote_io = msg->hdr.original_sc;
1451 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1454 * If we're in serialization-only mode, we don't
1455 * want to go through full done processing. Thus
1458 * XXX KDM add another flag that is more specific.
1460 if (softc->ha_mode != CTL_HA_MODE_XFER)
1461 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1462 io->io_hdr.nexus = msg->hdr.nexus;
1463 io->scsiio.tag_num = msg->scsi.tag_num;
1464 io->scsiio.tag_type = msg->scsi.tag_type;
1466 io->io_hdr.start_time = time_uptime;
1467 getbinuptime(&io->io_hdr.start_bt);
1468 #endif /* CTL_TIME_IO */
1469 io->scsiio.cdb_len = msg->scsi.cdb_len;
1470 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1472 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1473 const struct ctl_cmd_entry *entry;
1475 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1476 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1478 entry->flags & CTL_FLAG_DATA_MASK;
1480 ctl_enqueue_isc(io);
1483 /* Performed on the Originating SC, XFER mode only */
1484 case CTL_MSG_DATAMOVE: {
1485 struct ctl_sg_entry *sgl;
1488 io = msg->hdr.original_sc;
1490 printf("%s: original_sc == NULL!\n", __func__);
1491 /* XXX KDM do something here */
1494 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1495 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1497 * Keep track of this, we need to send it back over
1498 * when the datamove is complete.
1500 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1501 if (msg->hdr.status == CTL_SUCCESS)
1502 io->io_hdr.status = msg->hdr.status;
1504 if (msg->dt.sg_sequence == 0) {
1506 getbinuptime(&io->io_hdr.dma_start_bt);
1508 i = msg->dt.kern_sg_entries +
1509 msg->dt.kern_data_len /
1510 CTL_HA_DATAMOVE_SEGMENT + 1;
1511 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1514 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries];
1516 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1518 io->scsiio.kern_sg_entries =
1519 msg->dt.kern_sg_entries;
1520 io->scsiio.rem_sg_entries =
1521 msg->dt.kern_sg_entries;
1522 io->scsiio.kern_data_len =
1523 msg->dt.kern_data_len;
1524 io->scsiio.kern_total_len =
1525 msg->dt.kern_total_len;
1526 io->scsiio.kern_data_resid =
1527 msg->dt.kern_data_resid;
1528 io->scsiio.kern_rel_offset =
1529 msg->dt.kern_rel_offset;
1530 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1531 io->io_hdr.flags |= msg->dt.flags &
1534 sgl = (struct ctl_sg_entry *)
1535 io->scsiio.kern_data_ptr;
1537 for (i = msg->dt.sent_sg_entries, j = 0;
1538 i < (msg->dt.sent_sg_entries +
1539 msg->dt.cur_sg_entries); i++, j++) {
1540 sgl[i].addr = msg->dt.sg_list[j].addr;
1541 sgl[i].len = msg->dt.sg_list[j].len;
1545 * If this is the last piece of the I/O, we've got
1546 * the full S/G list. Queue processing in the thread.
1547 * Otherwise wait for the next piece.
1549 if (msg->dt.sg_last != 0)
1550 ctl_enqueue_isc(io);
1553 /* Performed on the Serializing (primary) SC, XFER mode only */
1554 case CTL_MSG_DATAMOVE_DONE: {
1555 if (msg->hdr.serializing_sc == NULL) {
1556 printf("%s: serializing_sc == NULL!\n",
1558 /* XXX KDM now what? */
1562 * We grab the sense information here in case
1563 * there was a failure, so we can return status
1564 * back to the initiator.
1566 io = msg->hdr.serializing_sc;
1567 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1568 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1569 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1570 io->io_hdr.port_status = msg->scsi.port_status;
1571 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1572 if (msg->hdr.status != CTL_STATUS_NONE) {
1573 io->io_hdr.status = msg->hdr.status;
1574 io->scsiio.scsi_status = msg->scsi.scsi_status;
1575 io->scsiio.sense_len = msg->scsi.sense_len;
1576 memcpy(&io->scsiio.sense_data,
1577 &msg->scsi.sense_data,
1578 msg->scsi.sense_len);
1579 if (msg->hdr.status == CTL_SUCCESS)
1580 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1582 ctl_enqueue_isc(io);
1586 /* Preformed on Originating SC, SER_ONLY mode */
1588 io = msg->hdr.original_sc;
1590 printf("%s: original_sc == NULL!\n",
1594 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1595 io->io_hdr.msg_type = CTL_MSG_R2R;
1596 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1597 ctl_enqueue_isc(io);
1601 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1603 * Performed on the Originating (i.e. secondary) SC in XFER
1606 case CTL_MSG_FINISH_IO:
1607 if (softc->ha_mode == CTL_HA_MODE_XFER)
1608 ctl_isc_handler_finish_xfer(softc, msg);
1610 ctl_isc_handler_finish_ser_only(softc, msg);
1613 /* Preformed on Originating SC */
1614 case CTL_MSG_BAD_JUJU:
1615 io = msg->hdr.original_sc;
1617 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1621 ctl_copy_sense_data(msg, io);
1623 * IO should have already been cleaned up on other
1624 * SC so clear this flag so we won't send a message
1625 * back to finish the IO there.
1627 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1628 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1630 /* io = msg->hdr.serializing_sc; */
1631 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1632 ctl_enqueue_isc(io);
1635 /* Handle resets sent from the other side */
1636 case CTL_MSG_MANAGE_TASKS: {
1637 struct ctl_taskio *taskio;
1638 taskio = (struct ctl_taskio *)ctl_alloc_io(
1639 softc->othersc_pool);
1640 ctl_zero_io((union ctl_io *)taskio);
1641 taskio->io_hdr.io_type = CTL_IO_TASK;
1642 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1643 taskio->io_hdr.nexus = msg->hdr.nexus;
1644 taskio->task_action = msg->task.task_action;
1645 taskio->tag_num = msg->task.tag_num;
1646 taskio->tag_type = msg->task.tag_type;
1648 taskio->io_hdr.start_time = time_uptime;
1649 getbinuptime(&taskio->io_hdr.start_bt);
1650 #endif /* CTL_TIME_IO */
1651 ctl_run_task((union ctl_io *)taskio);
1654 /* Persistent Reserve action which needs attention */
1655 case CTL_MSG_PERS_ACTION:
1656 presio = (struct ctl_prio *)ctl_alloc_io(
1657 softc->othersc_pool);
1658 ctl_zero_io((union ctl_io *)presio);
1659 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1660 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1661 presio->io_hdr.nexus = msg->hdr.nexus;
1662 presio->pr_msg = msg->pr;
1663 ctl_enqueue_isc((union ctl_io *)presio);
1666 ctl_isc_ua(softc, msg, param);
1668 case CTL_MSG_PORT_SYNC:
1669 ctl_isc_port_sync(softc, msg, param);
1671 case CTL_MSG_LUN_SYNC:
1672 ctl_isc_lun_sync(softc, msg, param);
1674 case CTL_MSG_IID_SYNC:
1675 ctl_isc_iid_sync(softc, msg, param);
1678 ctl_isc_login(softc, msg, param);
1680 case CTL_MSG_MODE_SYNC:
1681 ctl_isc_mode_sync(softc, msg, param);
1684 printf("Received HA message of unknown type %d\n",
1686 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1691 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1692 printf("CTL: HA link status changed from %d to %d\n",
1693 softc->ha_link, param);
1694 if (param == softc->ha_link)
1696 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1697 softc->ha_link = param;
1698 ctl_isc_ha_link_down(softc);
1700 softc->ha_link = param;
1701 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1702 ctl_isc_ha_link_up(softc);
1706 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1712 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1715 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1716 src->scsi.sense_len);
1717 dest->scsiio.scsi_status = src->scsi.scsi_status;
1718 dest->scsiio.sense_len = src->scsi.sense_len;
1719 dest->io_hdr.status = src->hdr.status;
1723 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1726 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1727 src->scsiio.sense_len);
1728 dest->scsi.scsi_status = src->scsiio.scsi_status;
1729 dest->scsi.sense_len = src->scsiio.sense_len;
1730 dest->hdr.status = src->io_hdr.status;
1734 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1736 struct ctl_softc *softc = lun->ctl_softc;
1739 if (initidx < softc->init_min || initidx >= softc->init_max)
1741 mtx_assert(&lun->lun_lock, MA_OWNED);
1742 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1745 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1749 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1753 mtx_assert(&lun->lun_lock, MA_OWNED);
1754 if (lun->pending_ua[port] == NULL)
1756 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1757 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1759 lun->pending_ua[port][i] |= ua;
1764 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1766 struct ctl_softc *softc = lun->ctl_softc;
1769 mtx_assert(&lun->lun_lock, MA_OWNED);
1770 for (i = softc->port_min; i < softc->port_max; i++)
1771 ctl_est_ua_port(lun, i, except, ua);
1775 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1777 struct ctl_softc *softc = lun->ctl_softc;
1780 if (initidx < softc->init_min || initidx >= softc->init_max)
1782 mtx_assert(&lun->lun_lock, MA_OWNED);
1783 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1786 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1790 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1792 struct ctl_softc *softc = lun->ctl_softc;
1795 mtx_assert(&lun->lun_lock, MA_OWNED);
1796 for (i = softc->port_min; i < softc->port_max; i++) {
1797 if (lun->pending_ua[i] == NULL)
1799 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1800 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1802 lun->pending_ua[i][j] &= ~ua;
1808 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1809 ctl_ua_type ua_type)
1811 struct ctl_lun *lun;
1813 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1814 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1815 mtx_lock(&lun->lun_lock);
1816 ctl_clr_ua(lun, initidx, ua_type);
1817 mtx_unlock(&lun->lun_lock);
1822 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1824 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1825 struct ctl_lun *lun;
1826 struct ctl_lun_req ireq;
1829 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1830 error = sysctl_handle_int(oidp, &value, 0, req);
1831 if ((error != 0) || (req->newptr == NULL))
1834 mtx_lock(&softc->ctl_lock);
1836 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1838 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1839 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1840 mtx_unlock(&softc->ctl_lock);
1841 bzero(&ireq, sizeof(ireq));
1842 ireq.reqtype = CTL_LUNREQ_MODIFY;
1843 ireq.reqdata.modify.lun_id = lun->lun;
1844 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1846 if (ireq.status != CTL_LUN_OK) {
1847 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1848 __func__, ireq.status, ireq.error_str);
1850 mtx_lock(&softc->ctl_lock);
1852 mtx_unlock(&softc->ctl_lock);
1859 struct make_dev_args args;
1860 struct ctl_softc *softc;
1863 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1866 make_dev_args_init(&args);
1867 args.mda_devsw = &ctl_cdevsw;
1868 args.mda_uid = UID_ROOT;
1869 args.mda_gid = GID_OPERATOR;
1870 args.mda_mode = 0600;
1871 args.mda_si_drv1 = softc;
1872 args.mda_si_drv2 = NULL;
1873 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1875 free(softc, M_DEVBUF);
1876 control_softc = NULL;
1880 sysctl_ctx_init(&softc->sysctl_ctx);
1881 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1882 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1883 CTLFLAG_RD | CTLFLAG_MPSAFE, 0, "CAM Target Layer");
1885 if (softc->sysctl_tree == NULL) {
1886 printf("%s: unable to allocate sysctl tree\n", __func__);
1887 destroy_dev(softc->dev);
1888 free(softc, M_DEVBUF);
1889 control_softc = NULL;
1893 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1894 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1895 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1898 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1899 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1900 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1902 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) {
1903 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n",
1904 ctl_max_luns, CTL_DEFAULT_MAX_LUNS);
1905 ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
1907 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns,
1908 M_DEVBUF, M_WAITOK | M_ZERO);
1909 softc->ctl_lun_mask = malloc(sizeof(uint32_t) *
1910 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1911 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) {
1912 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n",
1913 ctl_max_ports, CTL_DEFAULT_MAX_PORTS);
1914 ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
1916 softc->ctl_port_mask = malloc(sizeof(uint32_t) *
1917 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1918 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports,
1919 M_DEVBUF, M_WAITOK | M_ZERO);
1923 * In Copan's HA scheme, the "master" and "slave" roles are
1924 * figured out through the slot the controller is in. Although it
1925 * is an active/active system, someone has to be in charge.
1927 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1928 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1929 "HA head ID (0 - no HA)");
1930 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1931 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1932 softc->is_single = 1;
1933 softc->port_cnt = ctl_max_ports;
1934 softc->port_min = 0;
1936 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES;
1937 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1939 softc->port_max = softc->port_min + softc->port_cnt;
1940 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1941 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1943 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1944 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1945 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1947 STAILQ_INIT(&softc->lun_list);
1948 STAILQ_INIT(&softc->pending_lun_queue);
1949 STAILQ_INIT(&softc->fe_list);
1950 STAILQ_INIT(&softc->port_list);
1951 STAILQ_INIT(&softc->be_list);
1952 ctl_tpc_init(softc);
1954 if (worker_threads <= 0)
1955 worker_threads = max(1, mp_ncpus / 4);
1956 if (worker_threads > CTL_MAX_THREADS)
1957 worker_threads = CTL_MAX_THREADS;
1959 for (i = 0; i < worker_threads; i++) {
1960 struct ctl_thread *thr = &softc->threads[i];
1962 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1963 thr->ctl_softc = softc;
1964 STAILQ_INIT(&thr->incoming_queue);
1965 STAILQ_INIT(&thr->rtr_queue);
1966 STAILQ_INIT(&thr->done_queue);
1967 STAILQ_INIT(&thr->isc_queue);
1969 error = kproc_kthread_add(ctl_work_thread, thr,
1970 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1972 printf("error creating CTL work thread!\n");
1976 error = kproc_kthread_add(ctl_lun_thread, softc,
1977 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun");
1979 printf("error creating CTL lun thread!\n");
1982 error = kproc_kthread_add(ctl_thresh_thread, softc,
1983 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
1985 printf("error creating CTL threshold thread!\n");
1989 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1990 OID_AUTO, "ha_role",
1991 CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_NEEDGIANT,
1992 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1994 if (softc->is_single == 0) {
1995 if (ctl_frontend_register(&ha_frontend) != 0)
1996 softc->is_single = 1;
2004 struct ctl_softc *softc = control_softc;
2007 if (softc->is_single == 0)
2008 ctl_frontend_deregister(&ha_frontend);
2010 destroy_dev(softc->dev);
2012 /* Shutdown CTL threads. */
2013 softc->shutdown = 1;
2014 for (i = 0; i < worker_threads; i++) {
2015 struct ctl_thread *thr = &softc->threads[i];
2016 while (thr->thread != NULL) {
2018 if (thr->thread != NULL)
2019 pause("CTL thr shutdown", 1);
2021 mtx_destroy(&thr->queue_lock);
2023 while (softc->lun_thread != NULL) {
2024 wakeup(&softc->pending_lun_queue);
2025 if (softc->lun_thread != NULL)
2026 pause("CTL thr shutdown", 1);
2028 while (softc->thresh_thread != NULL) {
2029 wakeup(softc->thresh_thread);
2030 if (softc->thresh_thread != NULL)
2031 pause("CTL thr shutdown", 1);
2034 ctl_tpc_shutdown(softc);
2035 uma_zdestroy(softc->io_zone);
2036 mtx_destroy(&softc->ctl_lock);
2038 free(softc->ctl_luns, M_DEVBUF);
2039 free(softc->ctl_lun_mask, M_DEVBUF);
2040 free(softc->ctl_port_mask, M_DEVBUF);
2041 free(softc->ctl_ports, M_DEVBUF);
2043 sysctl_ctx_free(&softc->sysctl_ctx);
2045 free(softc, M_DEVBUF);
2046 control_softc = NULL;
2051 ctl_module_event_handler(module_t mod, int what, void *arg)
2056 return (ctl_init());
2058 return (ctl_shutdown());
2060 return (EOPNOTSUPP);
2065 * XXX KDM should we do some access checks here? Bump a reference count to
2066 * prevent a CTL module from being unloaded while someone has it open?
2069 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2075 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2081 * Remove an initiator by port number and initiator ID.
2082 * Returns 0 for success, -1 for failure.
2085 ctl_remove_initiator(struct ctl_port *port, int iid)
2087 struct ctl_softc *softc = port->ctl_softc;
2090 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2092 if (iid > CTL_MAX_INIT_PER_PORT) {
2093 printf("%s: initiator ID %u > maximun %u!\n",
2094 __func__, iid, CTL_MAX_INIT_PER_PORT);
2098 mtx_lock(&softc->ctl_lock);
2099 last = (--port->wwpn_iid[iid].in_use == 0);
2100 port->wwpn_iid[iid].last_use = time_uptime;
2101 mtx_unlock(&softc->ctl_lock);
2103 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2104 ctl_isc_announce_iid(port, iid);
2110 * Add an initiator to the initiator map.
2111 * Returns iid for success, < 0 for failure.
2114 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2116 struct ctl_softc *softc = port->ctl_softc;
2120 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2122 if (iid >= CTL_MAX_INIT_PER_PORT) {
2123 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2124 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2129 mtx_lock(&softc->ctl_lock);
2131 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2132 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2133 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2137 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2138 strcmp(name, port->wwpn_iid[i].name) == 0) {
2146 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2147 if (port->wwpn_iid[i].in_use == 0 &&
2148 port->wwpn_iid[i].wwpn == 0 &&
2149 port->wwpn_iid[i].name == NULL) {
2158 best_time = INT32_MAX;
2159 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2160 if (port->wwpn_iid[i].in_use == 0) {
2161 if (port->wwpn_iid[i].last_use < best_time) {
2163 best_time = port->wwpn_iid[i].last_use;
2171 mtx_unlock(&softc->ctl_lock);
2176 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2178 * This is not an error yet.
2180 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2182 printf("%s: port %d iid %u WWPN %#jx arrived"
2183 " again\n", __func__, port->targ_port,
2184 iid, (uintmax_t)wwpn);
2188 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2189 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2191 printf("%s: port %d iid %u name '%s' arrived"
2192 " again\n", __func__, port->targ_port,
2199 * This is an error, but what do we do about it? The
2200 * driver is telling us we have a new WWPN for this
2201 * initiator ID, so we pretty much need to use it.
2203 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2204 " but WWPN %#jx '%s' is still at that address\n",
2205 __func__, port->targ_port, iid, wwpn, name,
2206 (uintmax_t)port->wwpn_iid[iid].wwpn,
2207 port->wwpn_iid[iid].name);
2210 free(port->wwpn_iid[iid].name, M_CTL);
2211 port->wwpn_iid[iid].name = name;
2212 port->wwpn_iid[iid].wwpn = wwpn;
2213 port->wwpn_iid[iid].in_use++;
2214 mtx_unlock(&softc->ctl_lock);
2215 ctl_isc_announce_iid(port, iid);
2221 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2225 switch (port->port_type) {
2228 struct scsi_transportid_fcp *id =
2229 (struct scsi_transportid_fcp *)buf;
2230 if (port->wwpn_iid[iid].wwpn == 0)
2232 memset(id, 0, sizeof(*id));
2233 id->format_protocol = SCSI_PROTO_FC;
2234 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2235 return (sizeof(*id));
2237 case CTL_PORT_ISCSI:
2239 struct scsi_transportid_iscsi_port *id =
2240 (struct scsi_transportid_iscsi_port *)buf;
2241 if (port->wwpn_iid[iid].name == NULL)
2244 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2246 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2247 len = roundup2(min(len, 252), 4);
2248 scsi_ulto2b(len, id->additional_length);
2249 return (sizeof(*id) + len);
2253 struct scsi_transportid_sas *id =
2254 (struct scsi_transportid_sas *)buf;
2255 if (port->wwpn_iid[iid].wwpn == 0)
2257 memset(id, 0, sizeof(*id));
2258 id->format_protocol = SCSI_PROTO_SAS;
2259 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2260 return (sizeof(*id));
2264 struct scsi_transportid_spi *id =
2265 (struct scsi_transportid_spi *)buf;
2266 memset(id, 0, sizeof(*id));
2267 id->format_protocol = SCSI_PROTO_SPI;
2268 scsi_ulto2b(iid, id->scsi_addr);
2269 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2270 return (sizeof(*id));
2276 * Serialize a command that went down the "wrong" side, and so was sent to
2277 * this controller for execution. The logic is a little different than the
2278 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2279 * sent back to the other side, but in the success case, we execute the
2280 * command on this side (XFER mode) or tell the other side to execute it
2284 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2286 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2287 struct ctl_port *port = CTL_PORT(ctsio);
2288 union ctl_ha_msg msg_info;
2289 struct ctl_lun *lun;
2290 const struct ctl_cmd_entry *entry;
2294 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2296 /* Make sure that we know about this port. */
2297 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2298 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2303 /* Make sure that we know about this LUN. */
2304 mtx_lock(&softc->ctl_lock);
2305 if (targ_lun >= ctl_max_luns ||
2306 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2307 mtx_unlock(&softc->ctl_lock);
2310 * The other node would not send this request to us unless
2311 * received announce that we are primary node for this LUN.
2312 * If this LUN does not exist now, it is probably result of
2313 * a race, so respond to initiator in the most opaque way.
2315 ctl_set_busy(ctsio);
2318 mtx_lock(&lun->lun_lock);
2319 mtx_unlock(&softc->ctl_lock);
2322 * If the LUN is invalid, pretend that it doesn't exist.
2323 * It will go away as soon as all pending I/Os completed.
2325 if (lun->flags & CTL_LUN_DISABLED) {
2326 mtx_unlock(&lun->lun_lock);
2327 ctl_set_busy(ctsio);
2331 entry = ctl_get_cmd_entry(ctsio, NULL);
2332 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2333 mtx_unlock(&lun->lun_lock);
2337 CTL_LUN(ctsio) = lun;
2338 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2341 * Every I/O goes into the OOA queue for a
2342 * particular LUN, and stays there until completion.
2345 if (TAILQ_EMPTY(&lun->ooa_queue))
2346 lun->idle_time += getsbinuptime() - lun->last_busy;
2348 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2350 bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links);
2351 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
2352 case CTL_ACTION_BLOCK:
2353 ctsio->io_hdr.blocker = bio;
2354 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
2356 mtx_unlock(&lun->lun_lock);
2358 case CTL_ACTION_PASS:
2359 case CTL_ACTION_SKIP:
2360 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2361 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2362 ctl_enqueue_rtr((union ctl_io *)ctsio);
2363 mtx_unlock(&lun->lun_lock);
2365 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2366 mtx_unlock(&lun->lun_lock);
2368 /* send msg back to other side */
2369 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2370 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2371 msg_info.hdr.msg_type = CTL_MSG_R2R;
2372 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2373 sizeof(msg_info.hdr), M_WAITOK);
2376 case CTL_ACTION_OVERLAP:
2377 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2378 mtx_unlock(&lun->lun_lock);
2379 ctl_set_overlapped_cmd(ctsio);
2381 case CTL_ACTION_OVERLAP_TAG:
2382 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2383 mtx_unlock(&lun->lun_lock);
2384 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2386 case CTL_ACTION_ERROR:
2388 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2389 mtx_unlock(&lun->lun_lock);
2391 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2394 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2395 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2396 msg_info.hdr.serializing_sc = NULL;
2397 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2398 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2399 sizeof(msg_info.scsi), M_WAITOK);
2400 ctl_free_io((union ctl_io *)ctsio);
2406 * Returns 0 for success, errno for failure.
2409 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2410 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2414 mtx_lock(&lun->lun_lock);
2415 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2416 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2418 struct ctl_ooa_entry *entry;
2421 * If we've got more than we can fit, just count the
2422 * remaining entries.
2424 if (*cur_fill_num >= ooa_hdr->alloc_num)
2427 entry = &kern_entries[*cur_fill_num];
2429 entry->tag_num = io->scsiio.tag_num;
2430 entry->lun_num = lun->lun;
2432 entry->start_bt = io->io_hdr.start_bt;
2434 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2435 entry->cdb_len = io->scsiio.cdb_len;
2436 if (io->io_hdr.blocker != NULL)
2437 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2439 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2440 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2442 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2443 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2445 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2446 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2448 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2449 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2451 mtx_unlock(&lun->lun_lock);
2455 * Escape characters that are illegal or not recommended in XML.
2458 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2460 char *end = str + size;
2465 for (; *str && str < end; str++) {
2468 retval = sbuf_printf(sb, "&");
2471 retval = sbuf_printf(sb, ">");
2474 retval = sbuf_printf(sb, "<");
2477 retval = sbuf_putc(sb, *str);
2490 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2492 struct scsi_vpd_id_descriptor *desc;
2495 if (id == NULL || id->len < 4)
2497 desc = (struct scsi_vpd_id_descriptor *)id->data;
2498 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2499 case SVPD_ID_TYPE_T10:
2500 sbuf_printf(sb, "t10.");
2502 case SVPD_ID_TYPE_EUI64:
2503 sbuf_printf(sb, "eui.");
2505 case SVPD_ID_TYPE_NAA:
2506 sbuf_printf(sb, "naa.");
2508 case SVPD_ID_TYPE_SCSI_NAME:
2511 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2512 case SVPD_ID_CODESET_BINARY:
2513 for (i = 0; i < desc->length; i++)
2514 sbuf_printf(sb, "%02x", desc->identifier[i]);
2516 case SVPD_ID_CODESET_ASCII:
2517 sbuf_printf(sb, "%.*s", (int)desc->length,
2518 (char *)desc->identifier);
2520 case SVPD_ID_CODESET_UTF8:
2521 sbuf_printf(sb, "%s", (char *)desc->identifier);
2527 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2530 struct ctl_softc *softc = dev->si_drv1;
2531 struct ctl_port *port;
2532 struct ctl_lun *lun;
2539 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2541 case CTL_ENABLE_PORT:
2542 case CTL_DISABLE_PORT:
2543 case CTL_SET_PORT_WWNS: {
2544 struct ctl_port *port;
2545 struct ctl_port_entry *entry;
2547 entry = (struct ctl_port_entry *)addr;
2549 mtx_lock(&softc->ctl_lock);
2550 STAILQ_FOREACH(port, &softc->port_list, links) {
2553 if (port->targ_port < softc->port_min ||
2554 port->targ_port >= softc->port_max)
2559 if ((entry->port_type == CTL_PORT_NONE)
2560 && (entry->targ_port == port->targ_port)) {
2562 * If the user only wants to enable or
2563 * disable or set WWNs on a specific port,
2564 * do the operation and we're done.
2568 } else if (entry->port_type & port->port_type) {
2570 * Compare the user's type mask with the
2571 * particular frontend type to see if we
2578 * Make sure the user isn't trying to set
2579 * WWNs on multiple ports at the same time.
2581 if (cmd == CTL_SET_PORT_WWNS) {
2582 printf("%s: Can't set WWNs on "
2583 "multiple ports\n", __func__);
2592 * XXX KDM we have to drop the lock here, because
2593 * the online/offline operations can potentially
2594 * block. We need to reference count the frontends
2595 * so they can't go away,
2597 if (cmd == CTL_ENABLE_PORT) {
2598 mtx_unlock(&softc->ctl_lock);
2599 ctl_port_online(port);
2600 mtx_lock(&softc->ctl_lock);
2601 } else if (cmd == CTL_DISABLE_PORT) {
2602 mtx_unlock(&softc->ctl_lock);
2603 ctl_port_offline(port);
2604 mtx_lock(&softc->ctl_lock);
2605 } else if (cmd == CTL_SET_PORT_WWNS) {
2606 ctl_port_set_wwns(port,
2607 (entry->flags & CTL_PORT_WWNN_VALID) ?
2609 (entry->flags & CTL_PORT_WWPN_VALID) ?
2610 1 : 0, entry->wwpn);
2615 mtx_unlock(&softc->ctl_lock);
2619 struct ctl_ooa *ooa_hdr;
2620 struct ctl_ooa_entry *entries;
2621 uint32_t cur_fill_num;
2623 ooa_hdr = (struct ctl_ooa *)addr;
2625 if ((ooa_hdr->alloc_len == 0)
2626 || (ooa_hdr->alloc_num == 0)) {
2627 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2628 "must be non-zero\n", __func__,
2629 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2634 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2635 sizeof(struct ctl_ooa_entry))) {
2636 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2637 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2638 __func__, ooa_hdr->alloc_len,
2639 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2644 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2645 if (entries == NULL) {
2646 printf("%s: could not allocate %d bytes for OOA "
2647 "dump\n", __func__, ooa_hdr->alloc_len);
2652 mtx_lock(&softc->ctl_lock);
2653 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2654 (ooa_hdr->lun_num >= ctl_max_luns ||
2655 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2656 mtx_unlock(&softc->ctl_lock);
2657 free(entries, M_CTL);
2658 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2659 __func__, (uintmax_t)ooa_hdr->lun_num);
2666 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2667 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2668 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2672 lun = softc->ctl_luns[ooa_hdr->lun_num];
2673 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2676 mtx_unlock(&softc->ctl_lock);
2678 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2679 ooa_hdr->fill_len = ooa_hdr->fill_num *
2680 sizeof(struct ctl_ooa_entry);
2681 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2683 printf("%s: error copying out %d bytes for OOA dump\n",
2684 __func__, ooa_hdr->fill_len);
2687 getbinuptime(&ooa_hdr->cur_bt);
2689 if (cur_fill_num > ooa_hdr->alloc_num) {
2690 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2691 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2693 ooa_hdr->dropped_num = 0;
2694 ooa_hdr->status = CTL_OOA_OK;
2697 free(entries, M_CTL);
2700 case CTL_DELAY_IO: {
2701 struct ctl_io_delay_info *delay_info;
2703 delay_info = (struct ctl_io_delay_info *)addr;
2706 mtx_lock(&softc->ctl_lock);
2707 if (delay_info->lun_id >= ctl_max_luns ||
2708 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2709 mtx_unlock(&softc->ctl_lock);
2710 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2713 mtx_lock(&lun->lun_lock);
2714 mtx_unlock(&softc->ctl_lock);
2715 delay_info->status = CTL_DELAY_STATUS_OK;
2716 switch (delay_info->delay_type) {
2717 case CTL_DELAY_TYPE_CONT:
2718 case CTL_DELAY_TYPE_ONESHOT:
2721 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2724 switch (delay_info->delay_loc) {
2725 case CTL_DELAY_LOC_DATAMOVE:
2726 lun->delay_info.datamove_type = delay_info->delay_type;
2727 lun->delay_info.datamove_delay = delay_info->delay_secs;
2729 case CTL_DELAY_LOC_DONE:
2730 lun->delay_info.done_type = delay_info->delay_type;
2731 lun->delay_info.done_delay = delay_info->delay_secs;
2734 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2737 mtx_unlock(&lun->lun_lock);
2739 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2740 #endif /* CTL_IO_DELAY */
2743 case CTL_ERROR_INJECT: {
2744 struct ctl_error_desc *err_desc, *new_err_desc;
2746 err_desc = (struct ctl_error_desc *)addr;
2748 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2750 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2752 mtx_lock(&softc->ctl_lock);
2753 if (err_desc->lun_id >= ctl_max_luns ||
2754 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2755 mtx_unlock(&softc->ctl_lock);
2756 free(new_err_desc, M_CTL);
2757 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2758 __func__, (uintmax_t)err_desc->lun_id);
2762 mtx_lock(&lun->lun_lock);
2763 mtx_unlock(&softc->ctl_lock);
2766 * We could do some checking here to verify the validity
2767 * of the request, but given the complexity of error
2768 * injection requests, the checking logic would be fairly
2771 * For now, if the request is invalid, it just won't get
2772 * executed and might get deleted.
2774 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2777 * XXX KDM check to make sure the serial number is unique,
2778 * in case we somehow manage to wrap. That shouldn't
2779 * happen for a very long time, but it's the right thing to
2782 new_err_desc->serial = lun->error_serial;
2783 err_desc->serial = lun->error_serial;
2784 lun->error_serial++;
2786 mtx_unlock(&lun->lun_lock);
2789 case CTL_ERROR_INJECT_DELETE: {
2790 struct ctl_error_desc *delete_desc, *desc, *desc2;
2793 delete_desc = (struct ctl_error_desc *)addr;
2796 mtx_lock(&softc->ctl_lock);
2797 if (delete_desc->lun_id >= ctl_max_luns ||
2798 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2799 mtx_unlock(&softc->ctl_lock);
2800 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2801 __func__, (uintmax_t)delete_desc->lun_id);
2805 mtx_lock(&lun->lun_lock);
2806 mtx_unlock(&softc->ctl_lock);
2807 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2808 if (desc->serial != delete_desc->serial)
2811 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2816 mtx_unlock(&lun->lun_lock);
2817 if (delete_done == 0) {
2818 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2819 "error serial %ju on LUN %u\n", __func__,
2820 delete_desc->serial, delete_desc->lun_id);
2826 case CTL_DUMP_STRUCTS: {
2828 struct ctl_port *port;
2829 struct ctl_frontend *fe;
2831 mtx_lock(&softc->ctl_lock);
2832 printf("CTL Persistent Reservation information start:\n");
2833 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2834 mtx_lock(&lun->lun_lock);
2835 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2836 mtx_unlock(&lun->lun_lock);
2840 for (j = 0; j < ctl_max_ports; j++) {
2841 if (lun->pr_keys[j] == NULL)
2843 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2844 if (lun->pr_keys[j][k] == 0)
2846 printf(" LUN %ju port %d iid %d key "
2847 "%#jx\n", lun->lun, j, k,
2848 (uintmax_t)lun->pr_keys[j][k]);
2851 mtx_unlock(&lun->lun_lock);
2853 printf("CTL Persistent Reservation information end\n");
2854 printf("CTL Ports:\n");
2855 STAILQ_FOREACH(port, &softc->port_list, links) {
2856 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2857 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2858 port->frontend->name, port->port_type,
2859 port->physical_port, port->virtual_port,
2860 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2861 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2862 if (port->wwpn_iid[j].in_use == 0 &&
2863 port->wwpn_iid[j].wwpn == 0 &&
2864 port->wwpn_iid[j].name == NULL)
2867 printf(" iid %u use %d WWPN %#jx '%s'\n",
2868 j, port->wwpn_iid[j].in_use,
2869 (uintmax_t)port->wwpn_iid[j].wwpn,
2870 port->wwpn_iid[j].name);
2873 printf("CTL Port information end\n");
2874 mtx_unlock(&softc->ctl_lock);
2876 * XXX KDM calling this without a lock. We'd likely want
2877 * to drop the lock before calling the frontend's dump
2880 printf("CTL Frontends:\n");
2881 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2882 printf(" Frontend '%s'\n", fe->name);
2883 if (fe->fe_dump != NULL)
2886 printf("CTL Frontend information end\n");
2890 struct ctl_lun_req *lun_req;
2891 struct ctl_backend_driver *backend;
2893 nvlist_t *tmp_args_nvl;
2896 lun_req = (struct ctl_lun_req *)addr;
2897 tmp_args_nvl = lun_req->args_nvl;
2899 backend = ctl_backend_find(lun_req->backend);
2900 if (backend == NULL) {
2901 lun_req->status = CTL_LUN_ERROR;
2902 snprintf(lun_req->error_str,
2903 sizeof(lun_req->error_str),
2904 "Backend \"%s\" not found.",
2909 if (lun_req->args != NULL) {
2910 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK);
2911 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) {
2912 free(packed, M_CTL);
2913 lun_req->status = CTL_LUN_ERROR;
2914 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2915 "Cannot copyin args.");
2918 lun_req->args_nvl = nvlist_unpack(packed,
2919 lun_req->args_len, 0);
2920 free(packed, M_CTL);
2922 if (lun_req->args_nvl == NULL) {
2923 lun_req->status = CTL_LUN_ERROR;
2924 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2925 "Cannot unpack args nvlist.");
2929 lun_req->args_nvl = nvlist_create(0);
2931 retval = backend->ioctl(dev, cmd, addr, flag, td);
2932 nvlist_destroy(lun_req->args_nvl);
2933 lun_req->args_nvl = tmp_args_nvl;
2935 if (lun_req->result_nvl != NULL) {
2936 if (lun_req->result != NULL) {
2937 packed = nvlist_pack(lun_req->result_nvl,
2939 if (packed == NULL) {
2940 lun_req->status = CTL_LUN_ERROR;
2941 snprintf(lun_req->error_str,
2942 sizeof(lun_req->error_str),
2943 "Cannot pack result nvlist.");
2947 if (packed_len > lun_req->result_len) {
2948 lun_req->status = CTL_LUN_ERROR;
2949 snprintf(lun_req->error_str,
2950 sizeof(lun_req->error_str),
2951 "Result nvlist too large.");
2952 free(packed, M_NVLIST);
2956 if (copyout(packed, lun_req->result, packed_len)) {
2957 lun_req->status = CTL_LUN_ERROR;
2958 snprintf(lun_req->error_str,
2959 sizeof(lun_req->error_str),
2960 "Cannot copyout() the result.");
2961 free(packed, M_NVLIST);
2965 lun_req->result_len = packed_len;
2966 free(packed, M_NVLIST);
2969 nvlist_destroy(lun_req->result_nvl);
2973 case CTL_LUN_LIST: {
2975 struct ctl_lun_list *list;
2976 const char *name, *value;
2980 list = (struct ctl_lun_list *)addr;
2983 * Allocate a fixed length sbuf here, based on the length
2984 * of the user's buffer. We could allocate an auto-extending
2985 * buffer, and then tell the user how much larger our
2986 * amount of data is than his buffer, but that presents
2989 * 1. The sbuf(9) routines use a blocking malloc, and so
2990 * we can't hold a lock while calling them with an
2991 * auto-extending buffer.
2993 * 2. There is not currently a LUN reference counting
2994 * mechanism, outside of outstanding transactions on
2995 * the LUN's OOA queue. So a LUN could go away on us
2996 * while we're getting the LUN number, backend-specific
2997 * information, etc. Thus, given the way things
2998 * currently work, we need to hold the CTL lock while
2999 * grabbing LUN information.
3001 * So, from the user's standpoint, the best thing to do is
3002 * allocate what he thinks is a reasonable buffer length,
3003 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3004 * double the buffer length and try again. (And repeat
3005 * that until he succeeds.)
3007 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3009 list->status = CTL_LUN_LIST_ERROR;
3010 snprintf(list->error_str, sizeof(list->error_str),
3011 "Unable to allocate %d bytes for LUN list",
3016 sbuf_printf(sb, "<ctllunlist>\n");
3018 mtx_lock(&softc->ctl_lock);
3019 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3020 mtx_lock(&lun->lun_lock);
3021 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3022 (uintmax_t)lun->lun);
3025 * Bail out as soon as we see that we've overfilled
3031 retval = sbuf_printf(sb, "\t<backend_type>%s"
3032 "</backend_type>\n",
3033 (lun->backend == NULL) ? "none" :
3034 lun->backend->name);
3039 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3040 lun->be_lun->lun_type);
3045 if (lun->backend == NULL) {
3046 retval = sbuf_printf(sb, "</lun>\n");
3052 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3053 (lun->be_lun->maxlba > 0) ?
3054 lun->be_lun->maxlba + 1 : 0);
3059 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3060 lun->be_lun->blocksize);
3065 retval = sbuf_printf(sb, "\t<serial_number>");
3070 retval = ctl_sbuf_printf_esc(sb,
3071 lun->be_lun->serial_num,
3072 sizeof(lun->be_lun->serial_num));
3077 retval = sbuf_printf(sb, "</serial_number>\n");
3082 retval = sbuf_printf(sb, "\t<device_id>");
3087 retval = ctl_sbuf_printf_esc(sb,
3088 lun->be_lun->device_id,
3089 sizeof(lun->be_lun->device_id));
3094 retval = sbuf_printf(sb, "</device_id>\n");
3099 if (lun->backend->lun_info != NULL) {
3100 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3106 while ((name = nvlist_next(lun->be_lun->options, &type,
3107 &cookie)) != NULL) {
3108 sbuf_printf(sb, "\t<%s>", name);
3110 if (type == NV_TYPE_STRING) {
3111 value = dnvlist_get_string(
3112 lun->be_lun->options, name, NULL);
3114 sbuf_printf(sb, "%s", value);
3117 sbuf_printf(sb, "</%s>\n", name);
3120 retval = sbuf_printf(sb, "</lun>\n");
3124 mtx_unlock(&lun->lun_lock);
3127 mtx_unlock(&lun->lun_lock);
3128 mtx_unlock(&softc->ctl_lock);
3131 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3134 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3135 snprintf(list->error_str, sizeof(list->error_str),
3136 "Out of space, %d bytes is too small",
3143 retval = copyout(sbuf_data(sb), list->lun_xml,
3146 list->fill_len = sbuf_len(sb) + 1;
3147 list->status = CTL_LUN_LIST_OK;
3152 struct ctl_iscsi *ci;
3153 struct ctl_frontend *fe;
3155 ci = (struct ctl_iscsi *)addr;
3157 fe = ctl_frontend_find("iscsi");
3159 ci->status = CTL_ISCSI_ERROR;
3160 snprintf(ci->error_str, sizeof(ci->error_str),
3161 "Frontend \"iscsi\" not found.");
3165 retval = fe->ioctl(dev, cmd, addr, flag, td);
3168 case CTL_PORT_REQ: {
3169 struct ctl_req *req;
3170 struct ctl_frontend *fe;
3172 nvlist_t *tmp_args_nvl;
3175 req = (struct ctl_req *)addr;
3176 tmp_args_nvl = req->args_nvl;
3178 fe = ctl_frontend_find(req->driver);
3180 req->status = CTL_LUN_ERROR;
3181 snprintf(req->error_str, sizeof(req->error_str),
3182 "Frontend \"%s\" not found.", req->driver);
3186 if (req->args != NULL) {
3187 packed = malloc(req->args_len, M_CTL, M_WAITOK);
3188 if (copyin(req->args, packed, req->args_len) != 0) {
3189 free(packed, M_CTL);
3190 req->status = CTL_LUN_ERROR;
3191 snprintf(req->error_str, sizeof(req->error_str),
3192 "Cannot copyin args.");
3195 req->args_nvl = nvlist_unpack(packed,
3197 free(packed, M_CTL);
3199 if (req->args_nvl == NULL) {
3200 req->status = CTL_LUN_ERROR;
3201 snprintf(req->error_str, sizeof(req->error_str),
3202 "Cannot unpack args nvlist.");
3206 req->args_nvl = nvlist_create(0);
3209 retval = fe->ioctl(dev, cmd, addr, flag, td);
3213 nvlist_destroy(req->args_nvl);
3214 req->args_nvl = tmp_args_nvl;
3216 if (req->result_nvl != NULL) {
3217 if (req->result != NULL) {
3218 packed = nvlist_pack(req->result_nvl,
3220 if (packed == NULL) {
3221 req->status = CTL_LUN_ERROR;
3222 snprintf(req->error_str,
3223 sizeof(req->error_str),
3224 "Cannot pack result nvlist.");
3228 if (packed_len > req->result_len) {
3229 req->status = CTL_LUN_ERROR;
3230 snprintf(req->error_str,
3231 sizeof(req->error_str),
3232 "Result nvlist too large.");
3233 free(packed, M_NVLIST);
3237 if (copyout(packed, req->result, packed_len)) {
3238 req->status = CTL_LUN_ERROR;
3239 snprintf(req->error_str,
3240 sizeof(req->error_str),
3241 "Cannot copyout() the result.");
3242 free(packed, M_NVLIST);
3246 req->result_len = packed_len;
3247 free(packed, M_NVLIST);
3250 nvlist_destroy(req->result_nvl);
3254 case CTL_PORT_LIST: {
3256 struct ctl_port *port;
3257 struct ctl_lun_list *list;
3258 const char *name, *value;
3263 list = (struct ctl_lun_list *)addr;
3265 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3267 list->status = CTL_LUN_LIST_ERROR;
3268 snprintf(list->error_str, sizeof(list->error_str),
3269 "Unable to allocate %d bytes for LUN list",
3274 sbuf_printf(sb, "<ctlportlist>\n");
3276 mtx_lock(&softc->ctl_lock);
3277 STAILQ_FOREACH(port, &softc->port_list, links) {
3278 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3279 (uintmax_t)port->targ_port);
3282 * Bail out as soon as we see that we've overfilled
3288 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3289 "</frontend_type>\n", port->frontend->name);
3293 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3298 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3299 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3303 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3308 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3309 port->physical_port);
3313 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3314 port->virtual_port);
3318 if (port->target_devid != NULL) {
3319 sbuf_printf(sb, "\t<target>");
3320 ctl_id_sbuf(port->target_devid, sb);
3321 sbuf_printf(sb, "</target>\n");
3324 if (port->port_devid != NULL) {
3325 sbuf_printf(sb, "\t<port>");
3326 ctl_id_sbuf(port->port_devid, sb);
3327 sbuf_printf(sb, "</port>\n");
3330 if (port->port_info != NULL) {
3331 retval = port->port_info(port->onoff_arg, sb);
3337 while ((name = nvlist_next(port->options, &type,
3338 &cookie)) != NULL) {
3339 sbuf_printf(sb, "\t<%s>", name);
3341 if (type == NV_TYPE_STRING) {
3342 value = dnvlist_get_string(port->options,
3345 sbuf_printf(sb, "%s", value);
3348 sbuf_printf(sb, "</%s>\n", name);
3351 if (port->lun_map != NULL) {
3352 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3353 for (j = 0; j < port->lun_map_size; j++) {
3354 plun = ctl_lun_map_from_port(port, j);
3355 if (plun == UINT32_MAX)
3358 "\t<lun id=\"%u\">%u</lun>\n",
3363 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3364 if (port->wwpn_iid[j].in_use == 0 ||
3365 (port->wwpn_iid[j].wwpn == 0 &&
3366 port->wwpn_iid[j].name == NULL))
3369 if (port->wwpn_iid[j].name != NULL)
3370 retval = sbuf_printf(sb,
3371 "\t<initiator id=\"%u\">%s</initiator>\n",
3372 j, port->wwpn_iid[j].name);
3374 retval = sbuf_printf(sb,
3375 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3376 j, port->wwpn_iid[j].wwpn);
3383 retval = sbuf_printf(sb, "</targ_port>\n");
3387 mtx_unlock(&softc->ctl_lock);
3390 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3393 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3394 snprintf(list->error_str, sizeof(list->error_str),
3395 "Out of space, %d bytes is too small",
3402 retval = copyout(sbuf_data(sb), list->lun_xml,
3405 list->fill_len = sbuf_len(sb) + 1;
3406 list->status = CTL_LUN_LIST_OK;
3411 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3412 struct ctl_port *port;
3414 mtx_lock(&softc->ctl_lock);
3415 if (lm->port < softc->port_min ||
3416 lm->port >= softc->port_max ||
3417 (port = softc->ctl_ports[lm->port]) == NULL) {
3418 mtx_unlock(&softc->ctl_lock);
3421 if (port->status & CTL_PORT_STATUS_ONLINE) {
3422 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3423 if (ctl_lun_map_to_port(port, lun->lun) ==
3426 mtx_lock(&lun->lun_lock);
3427 ctl_est_ua_port(lun, lm->port, -1,
3429 mtx_unlock(&lun->lun_lock);
3432 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3433 if (lm->plun != UINT32_MAX) {
3434 if (lm->lun == UINT32_MAX)
3435 retval = ctl_lun_map_unset(port, lm->plun);
3436 else if (lm->lun < ctl_max_luns &&
3437 softc->ctl_luns[lm->lun] != NULL)
3438 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3442 if (lm->lun == UINT32_MAX)
3443 retval = ctl_lun_map_deinit(port);
3445 retval = ctl_lun_map_init(port);
3447 if (port->status & CTL_PORT_STATUS_ONLINE)
3448 ctl_isc_announce_port(port);
3451 case CTL_GET_LUN_STATS: {
3452 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3456 * XXX KDM no locking here. If the LUN list changes,
3457 * things can blow up.
3460 stats->status = CTL_SS_OK;
3461 stats->fill_len = 0;
3462 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3463 if (lun->lun < stats->first_item)
3465 if (stats->fill_len + sizeof(lun->stats) >
3467 stats->status = CTL_SS_NEED_MORE_SPACE;
3470 retval = copyout(&lun->stats, &stats->stats[i++],
3471 sizeof(lun->stats));
3474 stats->fill_len += sizeof(lun->stats);
3476 stats->num_items = softc->num_luns;
3477 stats->flags = CTL_STATS_FLAG_NONE;
3479 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3481 getnanouptime(&stats->timestamp);
3484 case CTL_GET_PORT_STATS: {
3485 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3489 * XXX KDM no locking here. If the LUN list changes,
3490 * things can blow up.
3493 stats->status = CTL_SS_OK;
3494 stats->fill_len = 0;
3495 STAILQ_FOREACH(port, &softc->port_list, links) {
3496 if (port->targ_port < stats->first_item)
3498 if (stats->fill_len + sizeof(port->stats) >
3500 stats->status = CTL_SS_NEED_MORE_SPACE;
3503 retval = copyout(&port->stats, &stats->stats[i++],
3504 sizeof(port->stats));
3507 stats->fill_len += sizeof(port->stats);
3509 stats->num_items = softc->num_ports;
3510 stats->flags = CTL_STATS_FLAG_NONE;
3512 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3514 getnanouptime(&stats->timestamp);
3518 /* XXX KDM should we fix this? */
3520 struct ctl_backend_driver *backend;
3527 * We encode the backend type as the ioctl type for backend
3528 * ioctls. So parse it out here, and then search for a
3529 * backend of this type.
3531 type = _IOC_TYPE(cmd);
3533 STAILQ_FOREACH(backend, &softc->be_list, links) {
3534 if (backend->type == type) {
3540 printf("ctl: unknown ioctl command %#lx or backend "
3545 retval = backend->ioctl(dev, cmd, addr, flag, td);
3555 ctl_get_initindex(struct ctl_nexus *nexus)
3557 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3561 ctl_lun_map_init(struct ctl_port *port)
3563 struct ctl_softc *softc = port->ctl_softc;
3564 struct ctl_lun *lun;
3565 int size = ctl_lun_map_size;
3568 if (port->lun_map == NULL || port->lun_map_size < size) {
3569 port->lun_map_size = 0;
3570 free(port->lun_map, M_CTL);
3571 port->lun_map = malloc(size * sizeof(uint32_t),
3574 if (port->lun_map == NULL)
3576 for (i = 0; i < size; i++)
3577 port->lun_map[i] = UINT32_MAX;
3578 port->lun_map_size = size;
3579 if (port->status & CTL_PORT_STATUS_ONLINE) {
3580 if (port->lun_disable != NULL) {
3581 STAILQ_FOREACH(lun, &softc->lun_list, links)
3582 port->lun_disable(port->targ_lun_arg, lun->lun);
3584 ctl_isc_announce_port(port);
3590 ctl_lun_map_deinit(struct ctl_port *port)
3592 struct ctl_softc *softc = port->ctl_softc;
3593 struct ctl_lun *lun;
3595 if (port->lun_map == NULL)
3597 port->lun_map_size = 0;
3598 free(port->lun_map, M_CTL);
3599 port->lun_map = NULL;
3600 if (port->status & CTL_PORT_STATUS_ONLINE) {
3601 if (port->lun_enable != NULL) {
3602 STAILQ_FOREACH(lun, &softc->lun_list, links)
3603 port->lun_enable(port->targ_lun_arg, lun->lun);
3605 ctl_isc_announce_port(port);
3611 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3616 if (port->lun_map == NULL) {
3617 status = ctl_lun_map_init(port);
3621 if (plun >= port->lun_map_size)
3623 old = port->lun_map[plun];
3624 port->lun_map[plun] = glun;
3625 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3626 if (port->lun_enable != NULL)
3627 port->lun_enable(port->targ_lun_arg, plun);
3628 ctl_isc_announce_port(port);
3634 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3638 if (port->lun_map == NULL || plun >= port->lun_map_size)
3640 old = port->lun_map[plun];
3641 port->lun_map[plun] = UINT32_MAX;
3642 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3643 if (port->lun_disable != NULL)
3644 port->lun_disable(port->targ_lun_arg, plun);
3645 ctl_isc_announce_port(port);
3651 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3655 return (UINT32_MAX);
3656 if (port->lun_map == NULL)
3658 if (lun_id > port->lun_map_size)
3659 return (UINT32_MAX);
3660 return (port->lun_map[lun_id]);
3664 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3669 return (UINT32_MAX);
3670 if (port->lun_map == NULL)
3672 for (i = 0; i < port->lun_map_size; i++) {
3673 if (port->lun_map[i] == lun_id)
3676 return (UINT32_MAX);
3680 ctl_decode_lun(uint64_t encoded)
3683 uint32_t result = 0xffffffff;
3685 be64enc(lun, encoded);
3686 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3687 case RPL_LUNDATA_ATYP_PERIPH:
3688 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3689 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3692 case RPL_LUNDATA_ATYP_FLAT:
3693 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3694 lun[6] == 0 && lun[7] == 0)
3695 result = ((lun[0] & 0x3f) << 8) + lun[1];
3697 case RPL_LUNDATA_ATYP_EXTLUN:
3698 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3700 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3705 result = (lun[1] << 16) + (lun[2] << 8) +
3709 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3710 result = (lun[2] << 24) +
3711 (lun[3] << 16) + (lun[4] << 8) +
3716 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3717 result = 0xffffffff;
3726 ctl_encode_lun(uint32_t decoded)
3728 uint64_t l = decoded;
3731 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3733 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3735 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3737 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3741 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3745 for (i = first; i < last; i++) {
3746 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3753 ctl_set_mask(uint32_t *mask, uint32_t bit)
3755 uint32_t chunk, piece;
3758 piece = bit % (sizeof(uint32_t) * 8);
3760 if ((mask[chunk] & (1 << piece)) != 0)
3763 mask[chunk] |= (1 << piece);
3769 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3771 uint32_t chunk, piece;
3774 piece = bit % (sizeof(uint32_t) * 8);
3776 if ((mask[chunk] & (1 << piece)) == 0)
3779 mask[chunk] &= ~(1 << piece);
3785 ctl_is_set(uint32_t *mask, uint32_t bit)
3787 uint32_t chunk, piece;
3790 piece = bit % (sizeof(uint32_t) * 8);
3792 if ((mask[chunk] & (1 << piece)) == 0)
3799 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3803 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3806 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3810 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3814 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3817 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3821 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3826 i = residx/CTL_MAX_INIT_PER_PORT;
3827 if (lun->pr_keys[i] != NULL)
3829 mtx_unlock(&lun->lun_lock);
3830 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3832 mtx_lock(&lun->lun_lock);
3833 if (lun->pr_keys[i] == NULL)
3834 lun->pr_keys[i] = p;
3840 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3844 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3845 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3846 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3850 * ctl_softc, pool_name, total_ctl_io are passed in.
3851 * npool is passed out.
3854 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3855 uint32_t total_ctl_io, void **npool)
3857 struct ctl_io_pool *pool;
3859 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3864 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3865 pool->ctl_softc = ctl_softc;
3867 pool->zone = uma_zsecond_create(pool->name, NULL,
3868 NULL, NULL, NULL, ctl_softc->io_zone);
3869 /* uma_prealloc(pool->zone, total_ctl_io); */
3871 pool->zone = ctl_softc->io_zone;
3879 ctl_pool_free(struct ctl_io_pool *pool)
3886 uma_zdestroy(pool->zone);
3892 ctl_alloc_io(void *pool_ref)
3894 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3897 io = uma_zalloc(pool->zone, M_WAITOK);
3899 io->io_hdr.pool = pool_ref;
3900 CTL_SOFTC(io) = pool->ctl_softc;
3901 TAILQ_INIT(&io->io_hdr.blocked_queue);
3907 ctl_alloc_io_nowait(void *pool_ref)
3909 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3912 io = uma_zalloc(pool->zone, M_NOWAIT);
3914 io->io_hdr.pool = pool_ref;
3915 CTL_SOFTC(io) = pool->ctl_softc;
3916 TAILQ_INIT(&io->io_hdr.blocked_queue);
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;
3948 TAILQ_INIT(&io->io_hdr.blocked_queue);
3952 ctl_expand_number(const char *buf, uint64_t *num)
3958 number = strtoq(buf, &endptr, 0);
3960 switch (tolower((unsigned char)*endptr)) {
3980 case '\0': /* No unit. */
3984 /* Unrecognized unit. */
3988 if ((number << shift) >> shift != number) {
3992 *num = number << shift;
3998 * This routine could be used in the future to load default and/or saved
3999 * mode page parameters for a particuar lun.
4002 ctl_init_page_index(struct ctl_lun *lun)
4005 struct ctl_page_index *page_index;
4009 memcpy(&lun->mode_pages.index, page_index_template,
4010 sizeof(page_index_template));
4012 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4014 page_index = &lun->mode_pages.index[i];
4015 if (lun->be_lun->lun_type == T_DIRECT &&
4016 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4018 if (lun->be_lun->lun_type == T_PROCESSOR &&
4019 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4021 if (lun->be_lun->lun_type == T_CDROM &&
4022 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4025 page_code = page_index->page_code & SMPH_PC_MASK;
4026 switch (page_code) {
4027 case SMS_RW_ERROR_RECOVERY_PAGE: {
4028 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4029 ("subpage %#x for page %#x is incorrect!",
4030 page_index->subpage, page_code));
4031 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4032 &rw_er_page_default,
4033 sizeof(rw_er_page_default));
4034 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4035 &rw_er_page_changeable,
4036 sizeof(rw_er_page_changeable));
4037 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4038 &rw_er_page_default,
4039 sizeof(rw_er_page_default));
4040 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4041 &rw_er_page_default,
4042 sizeof(rw_er_page_default));
4043 page_index->page_data =
4044 (uint8_t *)lun->mode_pages.rw_er_page;
4047 case SMS_FORMAT_DEVICE_PAGE: {
4048 struct scsi_format_page *format_page;
4050 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4051 ("subpage %#x for page %#x is incorrect!",
4052 page_index->subpage, page_code));
4055 * Sectors per track are set above. Bytes per
4056 * sector need to be set here on a per-LUN basis.
4058 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4059 &format_page_default,
4060 sizeof(format_page_default));
4061 memcpy(&lun->mode_pages.format_page[
4062 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4063 sizeof(format_page_changeable));
4064 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4065 &format_page_default,
4066 sizeof(format_page_default));
4067 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4068 &format_page_default,
4069 sizeof(format_page_default));
4071 format_page = &lun->mode_pages.format_page[
4073 scsi_ulto2b(lun->be_lun->blocksize,
4074 format_page->bytes_per_sector);
4076 format_page = &lun->mode_pages.format_page[
4078 scsi_ulto2b(lun->be_lun->blocksize,
4079 format_page->bytes_per_sector);
4081 format_page = &lun->mode_pages.format_page[
4083 scsi_ulto2b(lun->be_lun->blocksize,
4084 format_page->bytes_per_sector);
4086 page_index->page_data =
4087 (uint8_t *)lun->mode_pages.format_page;
4090 case SMS_RIGID_DISK_PAGE: {
4091 struct scsi_rigid_disk_page *rigid_disk_page;
4092 uint32_t sectors_per_cylinder;
4096 #endif /* !__XSCALE__ */
4098 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4099 ("subpage %#x for page %#x is incorrect!",
4100 page_index->subpage, page_code));
4103 * Rotation rate and sectors per track are set
4104 * above. We calculate the cylinders here based on
4105 * capacity. Due to the number of heads and
4106 * sectors per track we're using, smaller arrays
4107 * may turn out to have 0 cylinders. Linux and
4108 * FreeBSD don't pay attention to these mode pages
4109 * to figure out capacity, but Solaris does. It
4110 * seems to deal with 0 cylinders just fine, and
4111 * works out a fake geometry based on the capacity.
4113 memcpy(&lun->mode_pages.rigid_disk_page[
4114 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4115 sizeof(rigid_disk_page_default));
4116 memcpy(&lun->mode_pages.rigid_disk_page[
4117 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4118 sizeof(rigid_disk_page_changeable));
4120 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4124 * The divide method here will be more accurate,
4125 * probably, but results in floating point being
4126 * used in the kernel on i386 (__udivdi3()). On the
4127 * XScale, though, __udivdi3() is implemented in
4130 * The shift method for cylinder calculation is
4131 * accurate if sectors_per_cylinder is a power of
4132 * 2. Otherwise it might be slightly off -- you
4133 * might have a bit of a truncation problem.
4136 cylinders = (lun->be_lun->maxlba + 1) /
4137 sectors_per_cylinder;
4139 for (shift = 31; shift > 0; shift--) {
4140 if (sectors_per_cylinder & (1 << shift))
4143 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4147 * We've basically got 3 bytes, or 24 bits for the
4148 * cylinder size in the mode page. If we're over,
4149 * just round down to 2^24.
4151 if (cylinders > 0xffffff)
4152 cylinders = 0xffffff;
4154 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4156 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4158 if ((value = dnvlist_get_string(lun->be_lun->options,
4159 "rpm", NULL)) != NULL) {
4160 scsi_ulto2b(strtol(value, NULL, 0),
4161 rigid_disk_page->rotation_rate);
4164 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4165 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4166 sizeof(rigid_disk_page_default));
4167 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4168 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4169 sizeof(rigid_disk_page_default));
4171 page_index->page_data =
4172 (uint8_t *)lun->mode_pages.rigid_disk_page;
4175 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4176 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4177 ("subpage %#x for page %#x is incorrect!",
4178 page_index->subpage, page_code));
4179 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4180 &verify_er_page_default,
4181 sizeof(verify_er_page_default));
4182 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4183 &verify_er_page_changeable,
4184 sizeof(verify_er_page_changeable));
4185 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4186 &verify_er_page_default,
4187 sizeof(verify_er_page_default));
4188 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4189 &verify_er_page_default,
4190 sizeof(verify_er_page_default));
4191 page_index->page_data =
4192 (uint8_t *)lun->mode_pages.verify_er_page;
4195 case SMS_CACHING_PAGE: {
4196 struct scsi_caching_page *caching_page;
4198 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4199 ("subpage %#x for page %#x is incorrect!",
4200 page_index->subpage, page_code));
4201 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4202 &caching_page_default,
4203 sizeof(caching_page_default));
4204 memcpy(&lun->mode_pages.caching_page[
4205 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4206 sizeof(caching_page_changeable));
4207 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4208 &caching_page_default,
4209 sizeof(caching_page_default));
4210 caching_page = &lun->mode_pages.caching_page[
4212 value = dnvlist_get_string(lun->be_lun->options,
4213 "writecache", NULL);
4214 if (value != NULL && strcmp(value, "off") == 0)
4215 caching_page->flags1 &= ~SCP_WCE;
4216 value = dnvlist_get_string(lun->be_lun->options,
4218 if (value != NULL && strcmp(value, "off") == 0)
4219 caching_page->flags1 |= SCP_RCD;
4220 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4221 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4222 sizeof(caching_page_default));
4223 page_index->page_data =
4224 (uint8_t *)lun->mode_pages.caching_page;
4227 case SMS_CONTROL_MODE_PAGE: {
4228 switch (page_index->subpage) {
4229 case SMS_SUBPAGE_PAGE_0: {
4230 struct scsi_control_page *control_page;
4232 memcpy(&lun->mode_pages.control_page[
4234 &control_page_default,
4235 sizeof(control_page_default));
4236 memcpy(&lun->mode_pages.control_page[
4237 CTL_PAGE_CHANGEABLE],
4238 &control_page_changeable,
4239 sizeof(control_page_changeable));
4240 memcpy(&lun->mode_pages.control_page[
4242 &control_page_default,
4243 sizeof(control_page_default));
4244 control_page = &lun->mode_pages.control_page[
4246 value = dnvlist_get_string(lun->be_lun->options,
4247 "reordering", NULL);
4248 if (value != NULL &&
4249 strcmp(value, "unrestricted") == 0) {
4250 control_page->queue_flags &=
4251 ~SCP_QUEUE_ALG_MASK;
4252 control_page->queue_flags |=
4253 SCP_QUEUE_ALG_UNRESTRICTED;
4255 memcpy(&lun->mode_pages.control_page[
4257 &lun->mode_pages.control_page[
4259 sizeof(control_page_default));
4260 page_index->page_data =
4261 (uint8_t *)lun->mode_pages.control_page;
4265 memcpy(&lun->mode_pages.control_ext_page[
4267 &control_ext_page_default,
4268 sizeof(control_ext_page_default));
4269 memcpy(&lun->mode_pages.control_ext_page[
4270 CTL_PAGE_CHANGEABLE],
4271 &control_ext_page_changeable,
4272 sizeof(control_ext_page_changeable));
4273 memcpy(&lun->mode_pages.control_ext_page[
4275 &control_ext_page_default,
4276 sizeof(control_ext_page_default));
4277 memcpy(&lun->mode_pages.control_ext_page[
4279 &lun->mode_pages.control_ext_page[
4281 sizeof(control_ext_page_default));
4282 page_index->page_data =
4283 (uint8_t *)lun->mode_pages.control_ext_page;
4286 panic("subpage %#x for page %#x is incorrect!",
4287 page_index->subpage, page_code);
4291 case SMS_INFO_EXCEPTIONS_PAGE: {
4292 switch (page_index->subpage) {
4293 case SMS_SUBPAGE_PAGE_0:
4294 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4296 sizeof(ie_page_default));
4297 memcpy(&lun->mode_pages.ie_page[
4298 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4299 sizeof(ie_page_changeable));
4300 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4302 sizeof(ie_page_default));
4303 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4305 sizeof(ie_page_default));
4306 page_index->page_data =
4307 (uint8_t *)lun->mode_pages.ie_page;
4310 struct ctl_logical_block_provisioning_page *page;
4312 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4314 sizeof(lbp_page_default));
4315 memcpy(&lun->mode_pages.lbp_page[
4316 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4317 sizeof(lbp_page_changeable));
4318 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4320 sizeof(lbp_page_default));
4321 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4322 value = dnvlist_get_string(lun->be_lun->options,
4323 "avail-threshold", NULL);
4324 if (value != NULL &&
4325 ctl_expand_number(value, &ival) == 0) {
4326 page->descr[0].flags |= SLBPPD_ENABLED |
4328 if (lun->be_lun->blocksize)
4329 ival /= lun->be_lun->blocksize;
4332 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4333 page->descr[0].count);
4335 value = dnvlist_get_string(lun->be_lun->options,
4336 "used-threshold", NULL);
4337 if (value != NULL &&
4338 ctl_expand_number(value, &ival) == 0) {
4339 page->descr[1].flags |= SLBPPD_ENABLED |
4341 if (lun->be_lun->blocksize)
4342 ival /= lun->be_lun->blocksize;
4345 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4346 page->descr[1].count);
4348 value = dnvlist_get_string(lun->be_lun->options,
4349 "pool-avail-threshold", NULL);
4350 if (value != NULL &&
4351 ctl_expand_number(value, &ival) == 0) {
4352 page->descr[2].flags |= SLBPPD_ENABLED |
4354 if (lun->be_lun->blocksize)
4355 ival /= lun->be_lun->blocksize;
4358 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4359 page->descr[2].count);
4361 value = dnvlist_get_string(lun->be_lun->options,
4362 "pool-used-threshold", NULL);
4363 if (value != NULL &&
4364 ctl_expand_number(value, &ival) == 0) {
4365 page->descr[3].flags |= SLBPPD_ENABLED |
4367 if (lun->be_lun->blocksize)
4368 ival /= lun->be_lun->blocksize;
4371 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4372 page->descr[3].count);
4374 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4375 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4376 sizeof(lbp_page_default));
4377 page_index->page_data =
4378 (uint8_t *)lun->mode_pages.lbp_page;
4382 panic("subpage %#x for page %#x is incorrect!",
4383 page_index->subpage, page_code);
4387 case SMS_CDDVD_CAPS_PAGE:{
4388 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4389 ("subpage %#x for page %#x is incorrect!",
4390 page_index->subpage, page_code));
4391 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4392 &cddvd_page_default,
4393 sizeof(cddvd_page_default));
4394 memcpy(&lun->mode_pages.cddvd_page[
4395 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4396 sizeof(cddvd_page_changeable));
4397 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4398 &cddvd_page_default,
4399 sizeof(cddvd_page_default));
4400 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4401 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4402 sizeof(cddvd_page_default));
4403 page_index->page_data =
4404 (uint8_t *)lun->mode_pages.cddvd_page;
4408 panic("invalid page code value %#x", page_code);
4412 return (CTL_RETVAL_COMPLETE);
4416 ctl_init_log_page_index(struct ctl_lun *lun)
4418 struct ctl_page_index *page_index;
4421 memcpy(&lun->log_pages.index, log_page_index_template,
4422 sizeof(log_page_index_template));
4425 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4427 page_index = &lun->log_pages.index[i];
4428 if (lun->be_lun->lun_type == T_DIRECT &&
4429 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4431 if (lun->be_lun->lun_type == T_PROCESSOR &&
4432 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4434 if (lun->be_lun->lun_type == T_CDROM &&
4435 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4438 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4439 lun->backend->lun_attr == NULL)
4442 if (page_index->page_code != prev) {
4443 lun->log_pages.pages_page[j] = page_index->page_code;
4444 prev = page_index->page_code;
4447 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4448 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4451 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4452 lun->log_pages.index[0].page_len = j;
4453 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4454 lun->log_pages.index[1].page_len = k * 2;
4455 lun->log_pages.index[2].page_data = (uint8_t *)&lun->log_pages.temp_page;
4456 lun->log_pages.index[2].page_len = sizeof(lun->log_pages.temp_page);
4457 lun->log_pages.index[3].page_data = &lun->log_pages.lbp_page[0];
4458 lun->log_pages.index[3].page_len = 12*CTL_NUM_LBP_PARAMS;
4459 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.stat_page;
4460 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.stat_page);
4461 lun->log_pages.index[5].page_data = (uint8_t *)&lun->log_pages.ie_page;
4462 lun->log_pages.index[5].page_len = sizeof(lun->log_pages.ie_page);
4464 return (CTL_RETVAL_COMPLETE);
4468 hex2bin(const char *str, uint8_t *buf, int buf_size)
4473 memset(buf, 0, buf_size);
4474 while (isspace(str[0]))
4476 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4479 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4480 while (str[i] == '-') /* Skip dashes in UUIDs. */
4485 else if (isalpha(c))
4486 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4492 buf[i / 2] |= (c << 4);
4496 return ((i + 1) / 2);
4503 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4504 * wants us to allocate the LUN and he can block.
4505 * - ctl_softc is always set
4506 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4508 * Returns 0 for success, non-zero (errno) for failure.
4511 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4512 struct ctl_be_lun *const be_lun)
4514 struct ctl_lun *nlun, *lun;
4515 struct scsi_vpd_id_descriptor *desc;
4516 struct scsi_vpd_id_t10 *t10id;
4517 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4518 int lun_number, lun_malloced;
4519 int devidlen, idlen1, idlen2 = 0, len;
4525 * We currently only support Direct Access or Processor LUN types.
4527 switch (be_lun->lun_type) {
4535 be_lun->lun_config_status(be_lun->be_lun,
4536 CTL_LUN_CONFIG_FAILURE);
4539 if (ctl_lun == NULL) {
4540 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4547 memset(lun, 0, sizeof(*lun));
4549 lun->flags = CTL_LUN_MALLOCED;
4551 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4552 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4553 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4554 M_DEVBUF, M_WAITOK | M_ZERO);
4555 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4556 M_DEVBUF, M_WAITOK | M_ZERO);
4558 /* Generate LUN ID. */
4559 devidlen = max(CTL_DEVID_MIN_LEN,
4560 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4561 idlen1 = sizeof(*t10id) + devidlen;
4562 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4563 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL);
4564 if (scsiname != NULL) {
4565 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4566 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4568 eui = dnvlist_get_string(be_lun->options, "eui", NULL);
4570 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4572 naa = dnvlist_get_string(be_lun->options, "naa", NULL);
4574 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4576 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL);
4578 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4580 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4581 M_CTL, M_WAITOK | M_ZERO);
4582 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4583 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4584 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4585 desc->length = idlen1;
4586 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4587 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4588 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) {
4589 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4591 strncpy(t10id->vendor, vendor,
4592 min(sizeof(t10id->vendor), strlen(vendor)));
4594 strncpy((char *)t10id->vendor_spec_id,
4595 (char *)be_lun->device_id, devidlen);
4596 if (scsiname != NULL) {
4597 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4599 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4600 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4601 SVPD_ID_TYPE_SCSI_NAME;
4602 desc->length = idlen2;
4603 strlcpy(desc->identifier, scsiname, idlen2);
4606 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4608 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4609 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4611 desc->length = hex2bin(eui, desc->identifier, 16);
4612 desc->length = desc->length > 12 ? 16 :
4613 (desc->length > 8 ? 12 : 8);
4614 len -= 16 - desc->length;
4617 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4619 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4620 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4622 desc->length = hex2bin(naa, desc->identifier, 16);
4623 desc->length = desc->length > 8 ? 16 : 8;
4624 len -= 16 - desc->length;
4627 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4629 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4630 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4632 desc->identifier[0] = 0x10;
4633 hex2bin(uuid, &desc->identifier[2], 16);
4636 lun->lun_devid->len = len;
4638 mtx_lock(&ctl_softc->ctl_lock);
4640 * See if the caller requested a particular LUN number. If so, see
4641 * if it is available. Otherwise, allocate the first available LUN.
4643 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4644 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4645 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4646 mtx_unlock(&ctl_softc->ctl_lock);
4647 if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4648 printf("ctl: requested LUN ID %d is higher "
4649 "than ctl_max_luns - 1 (%d)\n",
4650 be_lun->req_lun_id, ctl_max_luns - 1);
4653 * XXX KDM return an error, or just assign
4654 * another LUN ID in this case??
4656 printf("ctl: requested LUN ID %d is already "
4657 "in use\n", be_lun->req_lun_id);
4660 free(lun->lun_devid, M_CTL);
4661 if (lun->flags & CTL_LUN_MALLOCED)
4663 be_lun->lun_config_status(be_lun->be_lun,
4664 CTL_LUN_CONFIG_FAILURE);
4667 lun_number = be_lun->req_lun_id;
4669 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4670 if (lun_number == -1) {
4671 mtx_unlock(&ctl_softc->ctl_lock);
4672 printf("ctl: can't allocate LUN, out of LUNs\n");
4676 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4677 mtx_unlock(&ctl_softc->ctl_lock);
4679 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4680 lun->lun = lun_number;
4681 lun->be_lun = be_lun;
4683 * The processor LUN is always enabled. Disk LUNs come on line
4684 * disabled, and must be enabled by the backend.
4686 lun->flags |= CTL_LUN_DISABLED;
4687 lun->backend = be_lun->be;
4688 be_lun->ctl_lun = lun;
4689 be_lun->lun_id = lun_number;
4690 atomic_add_int(&be_lun->be->num_luns, 1);
4691 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4692 lun->flags |= CTL_LUN_EJECTED;
4693 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4694 lun->flags |= CTL_LUN_NO_MEDIA;
4695 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4696 lun->flags |= CTL_LUN_STOPPED;
4698 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4699 lun->flags |= CTL_LUN_PRIMARY_SC;
4701 value = dnvlist_get_string(be_lun->options, "removable", NULL);
4702 if (value != NULL) {
4703 if (strcmp(value, "on") == 0)
4704 lun->flags |= CTL_LUN_REMOVABLE;
4705 } else if (be_lun->lun_type == T_CDROM)
4706 lun->flags |= CTL_LUN_REMOVABLE;
4708 lun->ctl_softc = ctl_softc;
4710 lun->last_busy = getsbinuptime();
4712 TAILQ_INIT(&lun->ooa_queue);
4713 STAILQ_INIT(&lun->error_list);
4714 lun->ie_reported = 1;
4715 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4716 ctl_tpc_lun_init(lun);
4717 if (lun->flags & CTL_LUN_REMOVABLE) {
4718 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4723 * Initialize the mode and log page index.
4725 ctl_init_page_index(lun);
4726 ctl_init_log_page_index(lun);
4728 /* Setup statistics gathering */
4729 lun->stats.item = lun_number;
4732 * Now, before we insert this lun on the lun list, set the lun
4733 * inventory changed UA for all other luns.
4735 mtx_lock(&ctl_softc->ctl_lock);
4736 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4737 mtx_lock(&nlun->lun_lock);
4738 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4739 mtx_unlock(&nlun->lun_lock);
4741 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4742 ctl_softc->ctl_luns[lun_number] = lun;
4743 ctl_softc->num_luns++;
4744 mtx_unlock(&ctl_softc->ctl_lock);
4746 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4753 * - LUN has already been marked invalid and any pending I/O has been taken
4757 ctl_free_lun(struct ctl_lun *lun)
4759 struct ctl_softc *softc = lun->ctl_softc;
4760 struct ctl_lun *nlun;
4763 KASSERT(TAILQ_EMPTY(&lun->ooa_queue),
4764 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4766 mtx_lock(&softc->ctl_lock);
4767 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4768 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4769 softc->ctl_luns[lun->lun] = NULL;
4771 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4772 mtx_lock(&nlun->lun_lock);
4773 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4774 mtx_unlock(&nlun->lun_lock);
4776 mtx_unlock(&softc->ctl_lock);
4779 * Tell the backend to free resources, if this LUN has a backend.
4781 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4782 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4784 lun->ie_reportcnt = UINT32_MAX;
4785 callout_drain(&lun->ie_callout);
4786 ctl_tpc_lun_shutdown(lun);
4787 mtx_destroy(&lun->lun_lock);
4788 free(lun->lun_devid, M_CTL);
4789 for (i = 0; i < ctl_max_ports; i++)
4790 free(lun->pending_ua[i], M_CTL);
4791 free(lun->pending_ua, M_DEVBUF);
4792 for (i = 0; i < ctl_max_ports; i++)
4793 free(lun->pr_keys[i], M_CTL);
4794 free(lun->pr_keys, M_DEVBUF);
4795 free(lun->write_buffer, M_CTL);
4796 free(lun->prevent, M_CTL);
4797 if (lun->flags & CTL_LUN_MALLOCED)
4804 ctl_create_lun(struct ctl_be_lun *be_lun)
4808 * ctl_alloc_lun() should handle all potential failure cases.
4810 ctl_alloc_lun(control_softc, NULL, be_lun);
4814 ctl_add_lun(struct ctl_be_lun *be_lun)
4816 struct ctl_softc *softc = control_softc;
4818 mtx_lock(&softc->ctl_lock);
4819 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4820 mtx_unlock(&softc->ctl_lock);
4821 wakeup(&softc->pending_lun_queue);
4827 ctl_enable_lun(struct ctl_be_lun *be_lun)
4829 struct ctl_softc *softc;
4830 struct ctl_port *port, *nport;
4831 struct ctl_lun *lun;
4834 lun = (struct ctl_lun *)be_lun->ctl_lun;
4835 softc = lun->ctl_softc;
4837 mtx_lock(&softc->ctl_lock);
4838 mtx_lock(&lun->lun_lock);
4839 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4841 * eh? Why did we get called if the LUN is already
4844 mtx_unlock(&lun->lun_lock);
4845 mtx_unlock(&softc->ctl_lock);
4848 lun->flags &= ~CTL_LUN_DISABLED;
4849 mtx_unlock(&lun->lun_lock);
4851 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4852 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4853 port->lun_map != NULL || port->lun_enable == NULL)
4857 * Drop the lock while we call the FETD's enable routine.
4858 * This can lead to a callback into CTL (at least in the
4859 * case of the internal initiator frontend.
4861 mtx_unlock(&softc->ctl_lock);
4862 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4863 mtx_lock(&softc->ctl_lock);
4865 printf("%s: FETD %s port %d returned error "
4866 "%d for lun_enable on lun %jd\n",
4867 __func__, port->port_name, port->targ_port,
4868 retval, (intmax_t)lun->lun);
4872 mtx_unlock(&softc->ctl_lock);
4873 ctl_isc_announce_lun(lun);
4879 ctl_disable_lun(struct ctl_be_lun *be_lun)
4881 struct ctl_softc *softc;
4882 struct ctl_port *port;
4883 struct ctl_lun *lun;
4886 lun = (struct ctl_lun *)be_lun->ctl_lun;
4887 softc = lun->ctl_softc;
4889 mtx_lock(&softc->ctl_lock);
4890 mtx_lock(&lun->lun_lock);
4891 if (lun->flags & CTL_LUN_DISABLED) {
4892 mtx_unlock(&lun->lun_lock);
4893 mtx_unlock(&softc->ctl_lock);
4896 lun->flags |= CTL_LUN_DISABLED;
4897 mtx_unlock(&lun->lun_lock);
4899 STAILQ_FOREACH(port, &softc->port_list, links) {
4900 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4901 port->lun_map != NULL || port->lun_disable == NULL)
4905 * Drop the lock before we call the frontend's disable
4906 * routine, to avoid lock order reversals.
4908 * XXX KDM what happens if the frontend list changes while
4909 * we're traversing it? It's unlikely, but should be handled.
4911 mtx_unlock(&softc->ctl_lock);
4912 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4913 mtx_lock(&softc->ctl_lock);
4915 printf("%s: FETD %s port %d returned error "
4916 "%d for lun_disable on lun %jd\n",
4917 __func__, port->port_name, port->targ_port,
4918 retval, (intmax_t)lun->lun);
4922 mtx_unlock(&softc->ctl_lock);
4923 ctl_isc_announce_lun(lun);
4929 ctl_start_lun(struct ctl_be_lun *be_lun)
4931 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4933 mtx_lock(&lun->lun_lock);
4934 lun->flags &= ~CTL_LUN_STOPPED;
4935 mtx_unlock(&lun->lun_lock);
4940 ctl_stop_lun(struct ctl_be_lun *be_lun)
4942 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4944 mtx_lock(&lun->lun_lock);
4945 lun->flags |= CTL_LUN_STOPPED;
4946 mtx_unlock(&lun->lun_lock);
4951 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4953 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4955 mtx_lock(&lun->lun_lock);
4956 lun->flags |= CTL_LUN_NO_MEDIA;
4957 mtx_unlock(&lun->lun_lock);
4962 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4964 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4965 union ctl_ha_msg msg;
4967 mtx_lock(&lun->lun_lock);
4968 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4969 if (lun->flags & CTL_LUN_REMOVABLE)
4970 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4971 mtx_unlock(&lun->lun_lock);
4972 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4973 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4974 bzero(&msg.ua, sizeof(msg.ua));
4975 msg.hdr.msg_type = CTL_MSG_UA;
4976 msg.hdr.nexus.initid = -1;
4977 msg.hdr.nexus.targ_port = -1;
4978 msg.hdr.nexus.targ_lun = lun->lun;
4979 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4982 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4983 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4990 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4992 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4994 mtx_lock(&lun->lun_lock);
4995 lun->flags |= CTL_LUN_EJECTED;
4996 mtx_unlock(&lun->lun_lock);
5001 ctl_lun_primary(struct ctl_be_lun *be_lun)
5003 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5005 mtx_lock(&lun->lun_lock);
5006 lun->flags |= CTL_LUN_PRIMARY_SC;
5007 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5008 mtx_unlock(&lun->lun_lock);
5009 ctl_isc_announce_lun(lun);
5014 ctl_lun_secondary(struct ctl_be_lun *be_lun)
5016 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5018 mtx_lock(&lun->lun_lock);
5019 lun->flags &= ~CTL_LUN_PRIMARY_SC;
5020 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5021 mtx_unlock(&lun->lun_lock);
5022 ctl_isc_announce_lun(lun);
5027 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5029 struct ctl_lun *lun;
5031 lun = (struct ctl_lun *)be_lun->ctl_lun;
5033 mtx_lock(&lun->lun_lock);
5036 * The LUN needs to be disabled before it can be marked invalid.
5038 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5039 mtx_unlock(&lun->lun_lock);
5043 * Mark the LUN invalid.
5045 lun->flags |= CTL_LUN_INVALID;
5048 * If there is nothing in the OOA queue, go ahead and free the LUN.
5049 * If we have something in the OOA queue, we'll free it when the
5050 * last I/O completes.
5052 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5053 mtx_unlock(&lun->lun_lock);
5056 mtx_unlock(&lun->lun_lock);
5062 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5064 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5065 union ctl_ha_msg msg;
5067 mtx_lock(&lun->lun_lock);
5068 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5069 mtx_unlock(&lun->lun_lock);
5070 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5071 /* Send msg to other side. */
5072 bzero(&msg.ua, sizeof(msg.ua));
5073 msg.hdr.msg_type = CTL_MSG_UA;
5074 msg.hdr.nexus.initid = -1;
5075 msg.hdr.nexus.targ_port = -1;
5076 msg.hdr.nexus.targ_lun = lun->lun;
5077 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5080 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5081 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5087 * Backend "memory move is complete" callback for requests that never
5088 * make it down to say RAIDCore's configuration code.
5091 ctl_config_move_done(union ctl_io *io)
5095 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5096 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5097 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5099 if ((io->io_hdr.port_status != 0) &&
5100 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5101 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5102 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5103 /*retry_count*/ io->io_hdr.port_status);
5104 } else if (io->scsiio.kern_data_resid != 0 &&
5105 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5106 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5107 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5108 ctl_set_invalid_field_ciu(&io->scsiio);
5111 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5113 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5114 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5115 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5116 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5118 * XXX KDM just assuming a single pointer here, and not a
5119 * S/G list. If we start using S/G lists for config data,
5120 * we'll need to know how to clean them up here as well.
5122 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5123 free(io->scsiio.kern_data_ptr, M_CTL);
5125 retval = CTL_RETVAL_COMPLETE;
5128 * XXX KDM now we need to continue data movement. Some
5130 * - call ctl_scsiio() again? We don't do this for data
5131 * writes, because for those at least we know ahead of
5132 * time where the write will go and how long it is. For
5133 * config writes, though, that information is largely
5134 * contained within the write itself, thus we need to
5135 * parse out the data again.
5137 * - Call some other function once the data is in?
5141 * XXX KDM call ctl_scsiio() again for now, and check flag
5142 * bits to see whether we're allocated or not.
5144 retval = ctl_scsiio(&io->scsiio);
5150 * This gets called by a backend driver when it is done with a
5151 * data_submit method.
5154 ctl_data_submit_done(union ctl_io *io)
5157 * If the IO_CONT flag is set, we need to call the supplied
5158 * function to continue processing the I/O, instead of completing
5161 * If there is an error, though, we don't want to keep processing.
5162 * Instead, just send status back to the initiator.
5164 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5165 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5166 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5167 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5168 io->scsiio.io_cont(io);
5175 * This gets called by a backend driver when it is done with a
5176 * configuration write.
5179 ctl_config_write_done(union ctl_io *io)
5184 * If the IO_CONT flag is set, we need to call the supplied
5185 * function to continue processing the I/O, instead of completing
5188 * If there is an error, though, we don't want to keep processing.
5189 * Instead, just send status back to the initiator.
5191 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5192 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5193 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5194 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5195 io->scsiio.io_cont(io);
5199 * Since a configuration write can be done for commands that actually
5200 * have data allocated, like write buffer, and commands that have
5201 * no data, like start/stop unit, we need to check here.
5203 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5204 buf = io->scsiio.kern_data_ptr;
5213 ctl_config_read_done(union ctl_io *io)
5218 * If there is some error -- we are done, skip data transfer.
5220 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5221 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5222 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5223 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5224 buf = io->scsiio.kern_data_ptr;
5234 * If the IO_CONT flag is set, we need to call the supplied
5235 * function to continue processing the I/O, instead of completing
5238 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5239 io->scsiio.io_cont(io);
5247 * SCSI release command.
5250 ctl_scsi_release(struct ctl_scsiio *ctsio)
5252 struct ctl_lun *lun = CTL_LUN(ctsio);
5255 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5257 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5260 * XXX KDM right now, we only support LUN reservation. We don't
5261 * support 3rd party reservations, or extent reservations, which
5262 * might actually need the parameter list. If we've gotten this
5263 * far, we've got a LUN reservation. Anything else got kicked out
5264 * above. So, according to SPC, ignore the length.
5267 mtx_lock(&lun->lun_lock);
5270 * According to SPC, it is not an error for an intiator to attempt
5271 * to release a reservation on a LUN that isn't reserved, or that
5272 * is reserved by another initiator. The reservation can only be
5273 * released, though, by the initiator who made it or by one of
5274 * several reset type events.
5276 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5277 lun->flags &= ~CTL_LUN_RESERVED;
5279 mtx_unlock(&lun->lun_lock);
5281 ctl_set_success(ctsio);
5282 ctl_done((union ctl_io *)ctsio);
5283 return (CTL_RETVAL_COMPLETE);
5287 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5289 struct ctl_lun *lun = CTL_LUN(ctsio);
5292 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5294 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5297 * XXX KDM right now, we only support LUN reservation. We don't
5298 * support 3rd party reservations, or extent reservations, which
5299 * might actually need the parameter list. If we've gotten this
5300 * far, we've got a LUN reservation. Anything else got kicked out
5301 * above. So, according to SPC, ignore the length.
5304 mtx_lock(&lun->lun_lock);
5305 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5306 ctl_set_reservation_conflict(ctsio);
5310 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5311 if (lun->flags & CTL_LUN_PR_RESERVED) {
5312 ctl_set_success(ctsio);
5316 lun->flags |= CTL_LUN_RESERVED;
5317 lun->res_idx = residx;
5318 ctl_set_success(ctsio);
5321 mtx_unlock(&lun->lun_lock);
5322 ctl_done((union ctl_io *)ctsio);
5323 return (CTL_RETVAL_COMPLETE);
5327 ctl_start_stop(struct ctl_scsiio *ctsio)
5329 struct ctl_lun *lun = CTL_LUN(ctsio);
5330 struct scsi_start_stop_unit *cdb;
5333 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5335 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5337 if ((cdb->how & SSS_PC_MASK) == 0) {
5338 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5339 (cdb->how & SSS_START) == 0) {
5342 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5343 if (ctl_get_prkey(lun, residx) == 0 ||
5344 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5346 ctl_set_reservation_conflict(ctsio);
5347 ctl_done((union ctl_io *)ctsio);
5348 return (CTL_RETVAL_COMPLETE);
5352 if ((cdb->how & SSS_LOEJ) &&
5353 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5354 ctl_set_invalid_field(ctsio,
5360 ctl_done((union ctl_io *)ctsio);
5361 return (CTL_RETVAL_COMPLETE);
5364 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5365 lun->prevent_count > 0) {
5366 /* "Medium removal prevented" */
5367 ctl_set_sense(ctsio, /*current_error*/ 1,
5368 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5369 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5370 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5371 ctl_done((union ctl_io *)ctsio);
5372 return (CTL_RETVAL_COMPLETE);
5376 retval = lun->backend->config_write((union ctl_io *)ctsio);
5381 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5383 struct ctl_lun *lun = CTL_LUN(ctsio);
5384 struct scsi_prevent *cdb;
5388 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5390 cdb = (struct scsi_prevent *)ctsio->cdb;
5392 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5393 ctl_set_invalid_opcode(ctsio);
5394 ctl_done((union ctl_io *)ctsio);
5395 return (CTL_RETVAL_COMPLETE);
5398 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5399 mtx_lock(&lun->lun_lock);
5400 if ((cdb->how & PR_PREVENT) &&
5401 ctl_is_set(lun->prevent, initidx) == 0) {
5402 ctl_set_mask(lun->prevent, initidx);
5403 lun->prevent_count++;
5404 } else if ((cdb->how & PR_PREVENT) == 0 &&
5405 ctl_is_set(lun->prevent, initidx)) {
5406 ctl_clear_mask(lun->prevent, initidx);
5407 lun->prevent_count--;
5409 mtx_unlock(&lun->lun_lock);
5410 retval = lun->backend->config_write((union ctl_io *)ctsio);
5415 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5416 * we don't really do anything with the LBA and length fields if the user
5417 * passes them in. Instead we'll just flush out the cache for the entire
5421 ctl_sync_cache(struct ctl_scsiio *ctsio)
5423 struct ctl_lun *lun = CTL_LUN(ctsio);
5424 struct ctl_lba_len_flags *lbalen;
5425 uint64_t starting_lba;
5426 uint32_t block_count;
5430 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5434 switch (ctsio->cdb[0]) {
5435 case SYNCHRONIZE_CACHE: {
5436 struct scsi_sync_cache *cdb;
5437 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5439 starting_lba = scsi_4btoul(cdb->begin_lba);
5440 block_count = scsi_2btoul(cdb->lb_count);
5444 case SYNCHRONIZE_CACHE_16: {
5445 struct scsi_sync_cache_16 *cdb;
5446 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5448 starting_lba = scsi_8btou64(cdb->begin_lba);
5449 block_count = scsi_4btoul(cdb->lb_count);
5454 ctl_set_invalid_opcode(ctsio);
5455 ctl_done((union ctl_io *)ctsio);
5457 break; /* NOTREACHED */
5461 * We check the LBA and length, but don't do anything with them.
5462 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5463 * get flushed. This check will just help satisfy anyone who wants
5464 * to see an error for an out of range LBA.
5466 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5467 ctl_set_lba_out_of_range(ctsio,
5468 MAX(starting_lba, lun->be_lun->maxlba + 1));
5469 ctl_done((union ctl_io *)ctsio);
5473 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5474 lbalen->lba = starting_lba;
5475 lbalen->len = block_count;
5476 lbalen->flags = byte2;
5477 retval = lun->backend->config_write((union ctl_io *)ctsio);
5484 ctl_format(struct ctl_scsiio *ctsio)
5486 struct scsi_format *cdb;
5487 int length, defect_list_len;
5489 CTL_DEBUG_PRINT(("ctl_format\n"));
5491 cdb = (struct scsi_format *)ctsio->cdb;
5494 if (cdb->byte2 & SF_FMTDATA) {
5495 if (cdb->byte2 & SF_LONGLIST)
5496 length = sizeof(struct scsi_format_header_long);
5498 length = sizeof(struct scsi_format_header_short);
5501 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5503 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5504 ctsio->kern_data_len = length;
5505 ctsio->kern_total_len = length;
5506 ctsio->kern_rel_offset = 0;
5507 ctsio->kern_sg_entries = 0;
5508 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5509 ctsio->be_move_done = ctl_config_move_done;
5510 ctl_datamove((union ctl_io *)ctsio);
5512 return (CTL_RETVAL_COMPLETE);
5515 defect_list_len = 0;
5517 if (cdb->byte2 & SF_FMTDATA) {
5518 if (cdb->byte2 & SF_LONGLIST) {
5519 struct scsi_format_header_long *header;
5521 header = (struct scsi_format_header_long *)
5522 ctsio->kern_data_ptr;
5524 defect_list_len = scsi_4btoul(header->defect_list_len);
5525 if (defect_list_len != 0) {
5526 ctl_set_invalid_field(ctsio,
5535 struct scsi_format_header_short *header;
5537 header = (struct scsi_format_header_short *)
5538 ctsio->kern_data_ptr;
5540 defect_list_len = scsi_2btoul(header->defect_list_len);
5541 if (defect_list_len != 0) {
5542 ctl_set_invalid_field(ctsio,
5553 ctl_set_success(ctsio);
5556 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5557 free(ctsio->kern_data_ptr, M_CTL);
5558 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5561 ctl_done((union ctl_io *)ctsio);
5562 return (CTL_RETVAL_COMPLETE);
5566 ctl_read_buffer(struct ctl_scsiio *ctsio)
5568 struct ctl_lun *lun = CTL_LUN(ctsio);
5569 uint64_t buffer_offset;
5572 static uint8_t descr[4];
5573 static uint8_t echo_descr[4] = { 0 };
5575 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5577 switch (ctsio->cdb[0]) {
5579 struct scsi_read_buffer *cdb;
5581 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5582 buffer_offset = scsi_3btoul(cdb->offset);
5583 len = scsi_3btoul(cdb->length);
5587 case READ_BUFFER_16: {
5588 struct scsi_read_buffer_16 *cdb;
5590 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5591 buffer_offset = scsi_8btou64(cdb->offset);
5592 len = scsi_4btoul(cdb->length);
5596 default: /* This shouldn't happen. */
5597 ctl_set_invalid_opcode(ctsio);
5598 ctl_done((union ctl_io *)ctsio);
5599 return (CTL_RETVAL_COMPLETE);
5602 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5603 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5604 ctl_set_invalid_field(ctsio,
5610 ctl_done((union ctl_io *)ctsio);
5611 return (CTL_RETVAL_COMPLETE);
5614 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5616 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5617 ctsio->kern_data_ptr = descr;
5618 len = min(len, sizeof(descr));
5619 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5620 ctsio->kern_data_ptr = echo_descr;
5621 len = min(len, sizeof(echo_descr));
5623 if (lun->write_buffer == NULL) {
5624 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5627 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5629 ctsio->kern_data_len = len;
5630 ctsio->kern_total_len = len;
5631 ctsio->kern_rel_offset = 0;
5632 ctsio->kern_sg_entries = 0;
5633 ctl_set_success(ctsio);
5634 ctsio->be_move_done = ctl_config_move_done;
5635 ctl_datamove((union ctl_io *)ctsio);
5636 return (CTL_RETVAL_COMPLETE);
5640 ctl_write_buffer(struct ctl_scsiio *ctsio)
5642 struct ctl_lun *lun = CTL_LUN(ctsio);
5643 struct scsi_write_buffer *cdb;
5644 int buffer_offset, len;
5646 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5648 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5650 len = scsi_3btoul(cdb->length);
5651 buffer_offset = scsi_3btoul(cdb->offset);
5653 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5654 ctl_set_invalid_field(ctsio,
5660 ctl_done((union ctl_io *)ctsio);
5661 return (CTL_RETVAL_COMPLETE);
5665 * If we've got a kernel request that hasn't been malloced yet,
5666 * malloc it and tell the caller the data buffer is here.
5668 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5669 if (lun->write_buffer == NULL) {
5670 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5673 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5674 ctsio->kern_data_len = len;
5675 ctsio->kern_total_len = len;
5676 ctsio->kern_rel_offset = 0;
5677 ctsio->kern_sg_entries = 0;
5678 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5679 ctsio->be_move_done = ctl_config_move_done;
5680 ctl_datamove((union ctl_io *)ctsio);
5682 return (CTL_RETVAL_COMPLETE);
5685 ctl_set_success(ctsio);
5686 ctl_done((union ctl_io *)ctsio);
5687 return (CTL_RETVAL_COMPLETE);
5691 ctl_write_same_cont(union ctl_io *io)
5693 struct ctl_lun *lun = CTL_LUN(io);
5694 struct ctl_scsiio *ctsio;
5695 struct ctl_lba_len_flags *lbalen;
5698 ctsio = &io->scsiio;
5699 ctsio->io_hdr.status = CTL_STATUS_NONE;
5700 lbalen = (struct ctl_lba_len_flags *)
5701 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5702 lbalen->lba += lbalen->len;
5703 if ((lun->be_lun->maxlba + 1) - lbalen->lba <= UINT32_MAX) {
5704 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
5705 lbalen->len = (lun->be_lun->maxlba + 1) - lbalen->lba;
5708 CTL_DEBUG_PRINT(("ctl_write_same_cont: calling config_write()\n"));
5709 retval = lun->backend->config_write((union ctl_io *)ctsio);
5714 ctl_write_same(struct ctl_scsiio *ctsio)
5716 struct ctl_lun *lun = CTL_LUN(ctsio);
5717 struct ctl_lba_len_flags *lbalen;
5720 uint32_t num_blocks;
5724 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5726 switch (ctsio->cdb[0]) {
5727 case WRITE_SAME_10: {
5728 struct scsi_write_same_10 *cdb;
5730 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5732 lba = scsi_4btoul(cdb->addr);
5733 num_blocks = scsi_2btoul(cdb->length);
5737 case WRITE_SAME_16: {
5738 struct scsi_write_same_16 *cdb;
5740 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5742 lba = scsi_8btou64(cdb->addr);
5743 num_blocks = scsi_4btoul(cdb->length);
5749 * We got a command we don't support. This shouldn't
5750 * happen, commands should be filtered out above us.
5752 ctl_set_invalid_opcode(ctsio);
5753 ctl_done((union ctl_io *)ctsio);
5755 return (CTL_RETVAL_COMPLETE);
5756 break; /* NOTREACHED */
5759 /* ANCHOR flag can be used only together with UNMAP */
5760 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5761 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5762 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5763 ctl_done((union ctl_io *)ctsio);
5764 return (CTL_RETVAL_COMPLETE);
5768 * The first check is to make sure we're in bounds, the second
5769 * check is to catch wrap-around problems. If the lba + num blocks
5770 * is less than the lba, then we've wrapped around and the block
5771 * range is invalid anyway.
5773 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5774 || ((lba + num_blocks) < lba)) {
5775 ctl_set_lba_out_of_range(ctsio,
5776 MAX(lba, lun->be_lun->maxlba + 1));
5777 ctl_done((union ctl_io *)ctsio);
5778 return (CTL_RETVAL_COMPLETE);
5781 /* Zero number of blocks means "to the last logical block" */
5782 if (num_blocks == 0) {
5784 val = dnvlist_get_string(lun->be_lun->options,
5785 "write_same_max_lba", NULL);
5787 ctl_expand_number(val, &ival);
5788 if ((lun->be_lun->maxlba + 1) - lba > ival) {
5789 ctl_set_invalid_field(ctsio,
5790 /*sks_valid*/ 1, /*command*/ 1,
5791 /*field*/ ctsio->cdb[0] == WRITE_SAME_10 ? 7 : 10,
5792 /*bit_valid*/ 0, /*bit*/ 0);
5793 ctl_done((union ctl_io *)ctsio);
5794 return (CTL_RETVAL_COMPLETE);
5796 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5797 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
5798 ctsio->io_cont = ctl_write_same_cont;
5799 num_blocks = 1 << 31;
5801 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5804 len = lun->be_lun->blocksize;
5807 * If we've got a kernel request that hasn't been malloced yet,
5808 * malloc it and tell the caller the data buffer is here.
5810 if ((byte2 & SWS_NDOB) == 0 &&
5811 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5812 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5813 ctsio->kern_data_len = len;
5814 ctsio->kern_total_len = len;
5815 ctsio->kern_rel_offset = 0;
5816 ctsio->kern_sg_entries = 0;
5817 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5818 ctsio->be_move_done = ctl_config_move_done;
5819 ctl_datamove((union ctl_io *)ctsio);
5821 return (CTL_RETVAL_COMPLETE);
5824 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5826 lbalen->len = num_blocks;
5827 lbalen->flags = byte2;
5828 retval = lun->backend->config_write((union ctl_io *)ctsio);
5834 ctl_unmap(struct ctl_scsiio *ctsio)
5836 struct ctl_lun *lun = CTL_LUN(ctsio);
5837 struct scsi_unmap *cdb;
5838 struct ctl_ptr_len_flags *ptrlen;
5839 struct scsi_unmap_header *hdr;
5840 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5842 uint32_t num_blocks;
5846 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5848 cdb = (struct scsi_unmap *)ctsio->cdb;
5849 len = scsi_2btoul(cdb->length);
5853 * If we've got a kernel request that hasn't been malloced yet,
5854 * malloc it and tell the caller the data buffer is here.
5856 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5857 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5858 ctsio->kern_data_len = len;
5859 ctsio->kern_total_len = len;
5860 ctsio->kern_rel_offset = 0;
5861 ctsio->kern_sg_entries = 0;
5862 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5863 ctsio->be_move_done = ctl_config_move_done;
5864 ctl_datamove((union ctl_io *)ctsio);
5866 return (CTL_RETVAL_COMPLETE);
5869 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5870 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5871 if (len < sizeof (*hdr) ||
5872 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5873 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5874 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5875 ctl_set_invalid_field(ctsio,
5883 len = scsi_2btoul(hdr->desc_length);
5884 buf = (struct scsi_unmap_desc *)(hdr + 1);
5885 end = buf + len / sizeof(*buf);
5888 for (range = buf; range < end; range++) {
5889 lba = scsi_8btou64(range->lba);
5890 num_blocks = scsi_4btoul(range->length);
5891 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5892 || ((lba + num_blocks) < lba)) {
5893 ctl_set_lba_out_of_range(ctsio,
5894 MAX(lba, lun->be_lun->maxlba + 1));
5895 ctl_done((union ctl_io *)ctsio);
5896 return (CTL_RETVAL_COMPLETE);
5898 if (num_blocks != 0)
5903 * Block backend can not handle zero last range.
5904 * Filter it out and return if there is nothing left.
5906 len = (uint8_t *)endnz - (uint8_t *)buf;
5908 ctl_set_success(ctsio);
5912 mtx_lock(&lun->lun_lock);
5913 ptrlen = (struct ctl_ptr_len_flags *)
5914 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5915 ptrlen->ptr = (void *)buf;
5917 ptrlen->flags = byte2;
5918 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE);
5919 mtx_unlock(&lun->lun_lock);
5921 retval = lun->backend->config_write((union ctl_io *)ctsio);
5925 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5926 free(ctsio->kern_data_ptr, M_CTL);
5927 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5929 ctl_done((union ctl_io *)ctsio);
5930 return (CTL_RETVAL_COMPLETE);
5934 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5935 struct ctl_page_index *page_index, uint8_t *page_ptr)
5937 struct ctl_lun *lun = CTL_LUN(ctsio);
5938 uint8_t *current_cp;
5942 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5945 current_cp = (page_index->page_data + (page_index->page_len *
5948 mtx_lock(&lun->lun_lock);
5949 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5950 memcpy(current_cp, page_ptr, page_index->page_len);
5954 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5955 mtx_unlock(&lun->lun_lock);
5957 ctl_isc_announce_mode(lun,
5958 ctl_get_initindex(&ctsio->io_hdr.nexus),
5959 page_index->page_code, page_index->subpage);
5961 return (CTL_RETVAL_COMPLETE);
5965 ctl_ie_timer(void *arg)
5967 struct ctl_lun *lun = arg;
5970 if (lun->ie_asc == 0)
5973 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5974 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5976 lun->ie_reported = 0;
5978 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5979 lun->ie_reportcnt++;
5980 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5981 if (t == 0 || t == UINT32_MAX)
5982 t = 3000; /* 5 min */
5983 callout_schedule(&lun->ie_callout, t * hz / 10);
5988 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5989 struct ctl_page_index *page_index, uint8_t *page_ptr)
5991 struct ctl_lun *lun = CTL_LUN(ctsio);
5992 struct scsi_info_exceptions_page *pg;
5995 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5997 pg = (struct scsi_info_exceptions_page *)page_ptr;
5998 mtx_lock(&lun->lun_lock);
5999 if (pg->info_flags & SIEP_FLAGS_TEST) {
6001 lun->ie_ascq = 0xff;
6002 if (pg->mrie == SIEP_MRIE_UA) {
6003 ctl_est_ua_all(lun, -1, CTL_UA_IE);
6004 lun->ie_reported = 1;
6006 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6007 lun->ie_reported = -1;
6009 lun->ie_reportcnt = 1;
6010 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
6011 lun->ie_reportcnt++;
6012 t = scsi_4btoul(pg->interval_timer);
6013 if (t == 0 || t == UINT32_MAX)
6014 t = 3000; /* 5 min */
6015 callout_reset(&lun->ie_callout, t * hz / 10,
6021 lun->ie_reported = 1;
6022 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
6023 lun->ie_reportcnt = UINT32_MAX;
6024 callout_stop(&lun->ie_callout);
6026 mtx_unlock(&lun->lun_lock);
6027 return (CTL_RETVAL_COMPLETE);
6031 ctl_do_mode_select(union ctl_io *io)
6033 struct ctl_lun *lun = CTL_LUN(io);
6034 struct scsi_mode_page_header *page_header;
6035 struct ctl_page_index *page_index;
6036 struct ctl_scsiio *ctsio;
6037 int page_len, page_len_offset, page_len_size;
6038 union ctl_modepage_info *modepage_info;
6039 uint16_t *len_left, *len_used;
6042 ctsio = &io->scsiio;
6046 modepage_info = (union ctl_modepage_info *)
6047 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6048 len_left = &modepage_info->header.len_left;
6049 len_used = &modepage_info->header.len_used;
6053 page_header = (struct scsi_mode_page_header *)
6054 (ctsio->kern_data_ptr + *len_used);
6056 if (*len_left == 0) {
6057 free(ctsio->kern_data_ptr, M_CTL);
6058 ctl_set_success(ctsio);
6059 ctl_done((union ctl_io *)ctsio);
6060 return (CTL_RETVAL_COMPLETE);
6061 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6063 free(ctsio->kern_data_ptr, M_CTL);
6064 ctl_set_param_len_error(ctsio);
6065 ctl_done((union ctl_io *)ctsio);
6066 return (CTL_RETVAL_COMPLETE);
6068 } else if ((page_header->page_code & SMPH_SPF)
6069 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6071 free(ctsio->kern_data_ptr, M_CTL);
6072 ctl_set_param_len_error(ctsio);
6073 ctl_done((union ctl_io *)ctsio);
6074 return (CTL_RETVAL_COMPLETE);
6079 * XXX KDM should we do something with the block descriptor?
6081 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6082 page_index = &lun->mode_pages.index[i];
6083 if (lun->be_lun->lun_type == T_DIRECT &&
6084 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6086 if (lun->be_lun->lun_type == T_PROCESSOR &&
6087 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6089 if (lun->be_lun->lun_type == T_CDROM &&
6090 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6093 if ((page_index->page_code & SMPH_PC_MASK) !=
6094 (page_header->page_code & SMPH_PC_MASK))
6098 * If neither page has a subpage code, then we've got a
6101 if (((page_index->page_code & SMPH_SPF) == 0)
6102 && ((page_header->page_code & SMPH_SPF) == 0)) {
6103 page_len = page_header->page_length;
6108 * If both pages have subpages, then the subpage numbers
6111 if ((page_index->page_code & SMPH_SPF)
6112 && (page_header->page_code & SMPH_SPF)) {
6113 struct scsi_mode_page_header_sp *sph;
6115 sph = (struct scsi_mode_page_header_sp *)page_header;
6116 if (page_index->subpage == sph->subpage) {
6117 page_len = scsi_2btoul(sph->page_length);
6124 * If we couldn't find the page, or if we don't have a mode select
6125 * handler for it, send back an error to the user.
6127 if ((i >= CTL_NUM_MODE_PAGES)
6128 || (page_index->select_handler == NULL)) {
6129 ctl_set_invalid_field(ctsio,
6132 /*field*/ *len_used,
6135 free(ctsio->kern_data_ptr, M_CTL);
6136 ctl_done((union ctl_io *)ctsio);
6137 return (CTL_RETVAL_COMPLETE);
6140 if (page_index->page_code & SMPH_SPF) {
6141 page_len_offset = 2;
6145 page_len_offset = 1;
6149 * If the length the initiator gives us isn't the one we specify in
6150 * the mode page header, or if they didn't specify enough data in
6151 * the CDB to avoid truncating this page, kick out the request.
6153 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6154 ctl_set_invalid_field(ctsio,
6157 /*field*/ *len_used + page_len_offset,
6160 free(ctsio->kern_data_ptr, M_CTL);
6161 ctl_done((union ctl_io *)ctsio);
6162 return (CTL_RETVAL_COMPLETE);
6164 if (*len_left < page_index->page_len) {
6165 free(ctsio->kern_data_ptr, M_CTL);
6166 ctl_set_param_len_error(ctsio);
6167 ctl_done((union ctl_io *)ctsio);
6168 return (CTL_RETVAL_COMPLETE);
6172 * Run through the mode page, checking to make sure that the bits
6173 * the user changed are actually legal for him to change.
6175 for (i = 0; i < page_index->page_len; i++) {
6176 uint8_t *user_byte, *change_mask, *current_byte;
6180 user_byte = (uint8_t *)page_header + i;
6181 change_mask = page_index->page_data +
6182 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6183 current_byte = page_index->page_data +
6184 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6187 * Check to see whether the user set any bits in this byte
6188 * that he is not allowed to set.
6190 if ((*user_byte & ~(*change_mask)) ==
6191 (*current_byte & ~(*change_mask)))
6195 * Go through bit by bit to determine which one is illegal.
6198 for (j = 7; j >= 0; j--) {
6199 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6200 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6205 ctl_set_invalid_field(ctsio,
6208 /*field*/ *len_used + i,
6211 free(ctsio->kern_data_ptr, M_CTL);
6212 ctl_done((union ctl_io *)ctsio);
6213 return (CTL_RETVAL_COMPLETE);
6217 * Decrement these before we call the page handler, since we may
6218 * end up getting called back one way or another before the handler
6219 * returns to this context.
6221 *len_left -= page_index->page_len;
6222 *len_used += page_index->page_len;
6224 retval = page_index->select_handler(ctsio, page_index,
6225 (uint8_t *)page_header);
6228 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6229 * wait until this queued command completes to finish processing
6230 * the mode page. If it returns anything other than
6231 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6232 * already set the sense information, freed the data pointer, and
6233 * completed the io for us.
6235 if (retval != CTL_RETVAL_COMPLETE)
6236 goto bailout_no_done;
6239 * If the initiator sent us more than one page, parse the next one.
6244 ctl_set_success(ctsio);
6245 free(ctsio->kern_data_ptr, M_CTL);
6246 ctl_done((union ctl_io *)ctsio);
6250 return (CTL_RETVAL_COMPLETE);
6255 ctl_mode_select(struct ctl_scsiio *ctsio)
6257 struct ctl_lun *lun = CTL_LUN(ctsio);
6258 union ctl_modepage_info *modepage_info;
6259 int bd_len, i, header_size, param_len, rtd;
6262 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6263 switch (ctsio->cdb[0]) {
6264 case MODE_SELECT_6: {
6265 struct scsi_mode_select_6 *cdb;
6267 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6269 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6270 param_len = cdb->length;
6271 header_size = sizeof(struct scsi_mode_header_6);
6274 case MODE_SELECT_10: {
6275 struct scsi_mode_select_10 *cdb;
6277 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6279 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6280 param_len = scsi_2btoul(cdb->length);
6281 header_size = sizeof(struct scsi_mode_header_10);
6285 ctl_set_invalid_opcode(ctsio);
6286 ctl_done((union ctl_io *)ctsio);
6287 return (CTL_RETVAL_COMPLETE);
6291 if (param_len != 0) {
6292 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6293 /*command*/ 1, /*field*/ 0,
6294 /*bit_valid*/ 0, /*bit*/ 0);
6295 ctl_done((union ctl_io *)ctsio);
6296 return (CTL_RETVAL_COMPLETE);
6299 /* Revert to defaults. */
6300 ctl_init_page_index(lun);
6301 mtx_lock(&lun->lun_lock);
6302 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6303 mtx_unlock(&lun->lun_lock);
6304 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6305 ctl_isc_announce_mode(lun, -1,
6306 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6307 lun->mode_pages.index[i].subpage);
6309 ctl_set_success(ctsio);
6310 ctl_done((union ctl_io *)ctsio);
6311 return (CTL_RETVAL_COMPLETE);
6316 * "A parameter list length of zero indicates that the Data-Out Buffer
6317 * shall be empty. This condition shall not be considered as an error."
6319 if (param_len == 0) {
6320 ctl_set_success(ctsio);
6321 ctl_done((union ctl_io *)ctsio);
6322 return (CTL_RETVAL_COMPLETE);
6326 * Since we'll hit this the first time through, prior to
6327 * allocation, we don't need to free a data buffer here.
6329 if (param_len < header_size) {
6330 ctl_set_param_len_error(ctsio);
6331 ctl_done((union ctl_io *)ctsio);
6332 return (CTL_RETVAL_COMPLETE);
6336 * Allocate the data buffer and grab the user's data. In theory,
6337 * we shouldn't have to sanity check the parameter list length here
6338 * because the maximum size is 64K. We should be able to malloc
6339 * that much without too many problems.
6341 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6342 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6343 ctsio->kern_data_len = param_len;
6344 ctsio->kern_total_len = param_len;
6345 ctsio->kern_rel_offset = 0;
6346 ctsio->kern_sg_entries = 0;
6347 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6348 ctsio->be_move_done = ctl_config_move_done;
6349 ctl_datamove((union ctl_io *)ctsio);
6351 return (CTL_RETVAL_COMPLETE);
6354 switch (ctsio->cdb[0]) {
6355 case MODE_SELECT_6: {
6356 struct scsi_mode_header_6 *mh6;
6358 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6359 bd_len = mh6->blk_desc_len;
6362 case MODE_SELECT_10: {
6363 struct scsi_mode_header_10 *mh10;
6365 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6366 bd_len = scsi_2btoul(mh10->blk_desc_len);
6370 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6373 if (param_len < (header_size + bd_len)) {
6374 free(ctsio->kern_data_ptr, M_CTL);
6375 ctl_set_param_len_error(ctsio);
6376 ctl_done((union ctl_io *)ctsio);
6377 return (CTL_RETVAL_COMPLETE);
6381 * Set the IO_CONT flag, so that if this I/O gets passed to
6382 * ctl_config_write_done(), it'll get passed back to
6383 * ctl_do_mode_select() for further processing, or completion if
6386 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6387 ctsio->io_cont = ctl_do_mode_select;
6389 modepage_info = (union ctl_modepage_info *)
6390 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6391 memset(modepage_info, 0, sizeof(*modepage_info));
6392 modepage_info->header.len_left = param_len - header_size - bd_len;
6393 modepage_info->header.len_used = header_size + bd_len;
6395 return (ctl_do_mode_select((union ctl_io *)ctsio));
6399 ctl_mode_sense(struct ctl_scsiio *ctsio)
6401 struct ctl_lun *lun = CTL_LUN(ctsio);
6402 int pc, page_code, llba, subpage;
6403 int alloc_len, page_len, header_len, bd_len, total_len;
6405 struct ctl_page_index *page_index;
6409 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6411 switch (ctsio->cdb[0]) {
6412 case MODE_SENSE_6: {
6413 struct scsi_mode_sense_6 *cdb;
6415 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6417 header_len = sizeof(struct scsi_mode_hdr_6);
6418 if (cdb->byte2 & SMS_DBD)
6421 bd_len = sizeof(struct scsi_mode_block_descr);
6422 header_len += bd_len;
6424 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6425 page_code = cdb->page & SMS_PAGE_CODE;
6426 subpage = cdb->subpage;
6427 alloc_len = cdb->length;
6430 case MODE_SENSE_10: {
6431 struct scsi_mode_sense_10 *cdb;
6433 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6435 header_len = sizeof(struct scsi_mode_hdr_10);
6436 if (cdb->byte2 & SMS_DBD) {
6438 } else if (lun->be_lun->lun_type == T_DIRECT) {
6439 if (cdb->byte2 & SMS10_LLBAA) {
6441 bd_len = sizeof(struct scsi_mode_block_descr_dlong);
6443 bd_len = sizeof(struct scsi_mode_block_descr_dshort);
6445 bd_len = sizeof(struct scsi_mode_block_descr);
6446 header_len += bd_len;
6448 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6449 page_code = cdb->page & SMS_PAGE_CODE;
6450 subpage = cdb->subpage;
6451 alloc_len = scsi_2btoul(cdb->length);
6455 ctl_set_invalid_opcode(ctsio);
6456 ctl_done((union ctl_io *)ctsio);
6457 return (CTL_RETVAL_COMPLETE);
6458 break; /* NOTREACHED */
6462 * We have to make a first pass through to calculate the size of
6463 * the pages that match the user's query. Then we allocate enough
6464 * memory to hold it, and actually copy the data into the buffer.
6466 switch (page_code) {
6467 case SMS_ALL_PAGES_PAGE: {
6473 * At the moment, values other than 0 and 0xff here are
6474 * reserved according to SPC-3.
6476 if ((subpage != SMS_SUBPAGE_PAGE_0)
6477 && (subpage != SMS_SUBPAGE_ALL)) {
6478 ctl_set_invalid_field(ctsio,
6484 ctl_done((union ctl_io *)ctsio);
6485 return (CTL_RETVAL_COMPLETE);
6488 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6489 page_index = &lun->mode_pages.index[i];
6491 /* Make sure the page is supported for this dev type */
6492 if (lun->be_lun->lun_type == T_DIRECT &&
6493 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6495 if (lun->be_lun->lun_type == T_PROCESSOR &&
6496 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6498 if (lun->be_lun->lun_type == T_CDROM &&
6499 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6503 * We don't use this subpage if the user didn't
6504 * request all subpages.
6506 if ((page_index->subpage != 0)
6507 && (subpage == SMS_SUBPAGE_PAGE_0))
6510 page_len += page_index->page_len;
6519 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6520 page_index = &lun->mode_pages.index[i];
6522 /* Make sure the page is supported for this dev type */
6523 if (lun->be_lun->lun_type == T_DIRECT &&
6524 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6526 if (lun->be_lun->lun_type == T_PROCESSOR &&
6527 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6529 if (lun->be_lun->lun_type == T_CDROM &&
6530 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6533 /* Look for the right page code */
6534 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6537 /* Look for the right subpage or the subpage wildcard*/
6538 if ((page_index->subpage != subpage)
6539 && (subpage != SMS_SUBPAGE_ALL))
6542 page_len += page_index->page_len;
6545 if (page_len == 0) {
6546 ctl_set_invalid_field(ctsio,
6552 ctl_done((union ctl_io *)ctsio);
6553 return (CTL_RETVAL_COMPLETE);
6559 total_len = header_len + page_len;
6561 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6562 ctsio->kern_sg_entries = 0;
6563 ctsio->kern_rel_offset = 0;
6564 ctsio->kern_data_len = min(total_len, alloc_len);
6565 ctsio->kern_total_len = ctsio->kern_data_len;
6567 switch (ctsio->cdb[0]) {
6568 case MODE_SENSE_6: {
6569 struct scsi_mode_hdr_6 *header;
6571 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6573 header->datalen = MIN(total_len - 1, 254);
6574 if (lun->be_lun->lun_type == T_DIRECT) {
6575 header->dev_specific = 0x10; /* DPOFUA */
6576 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6577 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6578 header->dev_specific |= 0x80; /* WP */
6580 header->block_descr_len = bd_len;
6581 block_desc = &header[1];
6584 case MODE_SENSE_10: {
6585 struct scsi_mode_hdr_10 *header;
6588 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6590 datalen = MIN(total_len - 2, 65533);
6591 scsi_ulto2b(datalen, header->datalen);
6592 if (lun->be_lun->lun_type == T_DIRECT) {
6593 header->dev_specific = 0x10; /* DPOFUA */
6594 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6595 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6596 header->dev_specific |= 0x80; /* WP */
6599 header->flags |= SMH_LONGLBA;
6600 scsi_ulto2b(bd_len, header->block_descr_len);
6601 block_desc = &header[1];
6605 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6609 * If we've got a disk, use its blocksize in the block
6610 * descriptor. Otherwise, just set it to 0.
6613 if (lun->be_lun->lun_type == T_DIRECT) {
6615 struct scsi_mode_block_descr_dlong *bd = block_desc;
6616 if (lun->be_lun->maxlba != 0)
6617 scsi_u64to8b(lun->be_lun->maxlba + 1,
6619 scsi_ulto4b(lun->be_lun->blocksize,
6622 struct scsi_mode_block_descr_dshort *bd = block_desc;
6623 if (lun->be_lun->maxlba != 0)
6624 scsi_ulto4b(MIN(lun->be_lun->maxlba+1,
6625 UINT32_MAX), bd->num_blocks);
6626 scsi_ulto3b(lun->be_lun->blocksize,
6630 struct scsi_mode_block_descr *bd = block_desc;
6631 scsi_ulto3b(0, bd->block_len);
6635 switch (page_code) {
6636 case SMS_ALL_PAGES_PAGE: {
6639 data_used = header_len;
6640 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6641 struct ctl_page_index *page_index;
6643 page_index = &lun->mode_pages.index[i];
6644 if (lun->be_lun->lun_type == T_DIRECT &&
6645 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6647 if (lun->be_lun->lun_type == T_PROCESSOR &&
6648 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6650 if (lun->be_lun->lun_type == T_CDROM &&
6651 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6655 * We don't use this subpage if the user didn't
6656 * request all subpages. We already checked (above)
6657 * to make sure the user only specified a subpage
6658 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6660 if ((page_index->subpage != 0)
6661 && (subpage == SMS_SUBPAGE_PAGE_0))
6665 * Call the handler, if it exists, to update the
6666 * page to the latest values.
6668 if (page_index->sense_handler != NULL)
6669 page_index->sense_handler(ctsio, page_index,pc);
6671 memcpy(ctsio->kern_data_ptr + data_used,
6672 page_index->page_data +
6673 (page_index->page_len * pc),
6674 page_index->page_len);
6675 data_used += page_index->page_len;
6682 data_used = header_len;
6684 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6685 struct ctl_page_index *page_index;
6687 page_index = &lun->mode_pages.index[i];
6689 /* Look for the right page code */
6690 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6693 /* Look for the right subpage or the subpage wildcard*/
6694 if ((page_index->subpage != subpage)
6695 && (subpage != SMS_SUBPAGE_ALL))
6698 /* Make sure the page is supported for this dev type */
6699 if (lun->be_lun->lun_type == T_DIRECT &&
6700 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6702 if (lun->be_lun->lun_type == T_PROCESSOR &&
6703 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6705 if (lun->be_lun->lun_type == T_CDROM &&
6706 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6710 * Call the handler, if it exists, to update the
6711 * page to the latest values.
6713 if (page_index->sense_handler != NULL)
6714 page_index->sense_handler(ctsio, page_index,pc);
6716 memcpy(ctsio->kern_data_ptr + data_used,
6717 page_index->page_data +
6718 (page_index->page_len * pc),
6719 page_index->page_len);
6720 data_used += page_index->page_len;
6726 ctl_set_success(ctsio);
6727 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6728 ctsio->be_move_done = ctl_config_move_done;
6729 ctl_datamove((union ctl_io *)ctsio);
6730 return (CTL_RETVAL_COMPLETE);
6734 ctl_temp_log_sense_handler(struct ctl_scsiio *ctsio,
6735 struct ctl_page_index *page_index,
6738 struct ctl_lun *lun = CTL_LUN(ctsio);
6739 struct scsi_log_temperature *data;
6742 data = (struct scsi_log_temperature *)page_index->page_data;
6744 scsi_ulto2b(SLP_TEMPERATURE, data->hdr.param_code);
6745 data->hdr.param_control = SLP_LBIN;
6746 data->hdr.param_len = sizeof(struct scsi_log_temperature) -
6747 sizeof(struct scsi_log_param_header);
6748 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature",
6750 data->temperature = strtol(value, NULL, 0);
6752 data->temperature = 0xff;
6755 scsi_ulto2b(SLP_REFTEMPERATURE, data->hdr.param_code);
6756 data->hdr.param_control = SLP_LBIN;
6757 data->hdr.param_len = sizeof(struct scsi_log_temperature) -
6758 sizeof(struct scsi_log_param_header);
6759 if ((value = dnvlist_get_string(lun->be_lun->options, "reftemperature",
6761 data->temperature = strtol(value, NULL, 0);
6763 data->temperature = 0xff;
6768 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6769 struct ctl_page_index *page_index,
6772 struct ctl_lun *lun = CTL_LUN(ctsio);
6773 struct scsi_log_param_header *phdr;
6777 data = page_index->page_data;
6779 if (lun->backend->lun_attr != NULL &&
6780 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6782 phdr = (struct scsi_log_param_header *)data;
6783 scsi_ulto2b(0x0001, phdr->param_code);
6784 phdr->param_control = SLP_LBIN | SLP_LP;
6785 phdr->param_len = 8;
6786 data = (uint8_t *)(phdr + 1);
6787 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6788 data[4] = 0x02; /* per-pool */
6789 data += phdr->param_len;
6792 if (lun->backend->lun_attr != NULL &&
6793 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6795 phdr = (struct scsi_log_param_header *)data;
6796 scsi_ulto2b(0x0002, phdr->param_code);
6797 phdr->param_control = SLP_LBIN | SLP_LP;
6798 phdr->param_len = 8;
6799 data = (uint8_t *)(phdr + 1);
6800 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6801 data[4] = 0x01; /* per-LUN */
6802 data += phdr->param_len;
6805 if (lun->backend->lun_attr != NULL &&
6806 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6808 phdr = (struct scsi_log_param_header *)data;
6809 scsi_ulto2b(0x00f1, phdr->param_code);
6810 phdr->param_control = SLP_LBIN | SLP_LP;
6811 phdr->param_len = 8;
6812 data = (uint8_t *)(phdr + 1);
6813 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6814 data[4] = 0x02; /* per-pool */
6815 data += phdr->param_len;
6818 if (lun->backend->lun_attr != NULL &&
6819 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6821 phdr = (struct scsi_log_param_header *)data;
6822 scsi_ulto2b(0x00f2, phdr->param_code);
6823 phdr->param_control = SLP_LBIN | SLP_LP;
6824 phdr->param_len = 8;
6825 data = (uint8_t *)(phdr + 1);
6826 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6827 data[4] = 0x02; /* per-pool */
6828 data += phdr->param_len;
6831 page_index->page_len = data - page_index->page_data;
6836 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6837 struct ctl_page_index *page_index,
6840 struct ctl_lun *lun = CTL_LUN(ctsio);
6841 struct stat_page *data;
6844 data = (struct stat_page *)page_index->page_data;
6846 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6847 data->sap.hdr.param_control = SLP_LBIN;
6848 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6849 sizeof(struct scsi_log_param_header);
6850 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6851 data->sap.read_num);
6852 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6853 data->sap.write_num);
6854 if (lun->be_lun->blocksize > 0) {
6855 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6856 lun->be_lun->blocksize, data->sap.recvieved_lba);
6857 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6858 lun->be_lun->blocksize, data->sap.transmitted_lba);
6860 t = &lun->stats.time[CTL_STATS_READ];
6861 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6862 data->sap.read_int);
6863 t = &lun->stats.time[CTL_STATS_WRITE];
6864 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6865 data->sap.write_int);
6866 scsi_u64to8b(0, data->sap.weighted_num);
6867 scsi_u64to8b(0, data->sap.weighted_int);
6868 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6869 data->it.hdr.param_control = SLP_LBIN;
6870 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6871 sizeof(struct scsi_log_param_header);
6873 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6875 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6876 data->it.hdr.param_control = SLP_LBIN;
6877 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6878 sizeof(struct scsi_log_param_header);
6879 scsi_ulto4b(3, data->ti.exponent);
6880 scsi_ulto4b(1, data->ti.integer);
6885 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6886 struct ctl_page_index *page_index,
6889 struct ctl_lun *lun = CTL_LUN(ctsio);
6890 struct scsi_log_informational_exceptions *data;
6893 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6895 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6896 data->hdr.param_control = SLP_LBIN;
6897 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6898 sizeof(struct scsi_log_param_header);
6899 data->ie_asc = lun->ie_asc;
6900 data->ie_ascq = lun->ie_ascq;
6901 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature",
6903 data->temperature = strtol(value, NULL, 0);
6905 data->temperature = 0xff;
6910 ctl_log_sense(struct ctl_scsiio *ctsio)
6912 struct ctl_lun *lun = CTL_LUN(ctsio);
6913 int i, pc, page_code, subpage;
6914 int alloc_len, total_len;
6915 struct ctl_page_index *page_index;
6916 struct scsi_log_sense *cdb;
6917 struct scsi_log_header *header;
6919 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6921 cdb = (struct scsi_log_sense *)ctsio->cdb;
6922 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6923 page_code = cdb->page & SLS_PAGE_CODE;
6924 subpage = cdb->subpage;
6925 alloc_len = scsi_2btoul(cdb->length);
6928 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6929 page_index = &lun->log_pages.index[i];
6931 /* Look for the right page code */
6932 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6935 /* Look for the right subpage or the subpage wildcard*/
6936 if (page_index->subpage != subpage)
6941 if (i >= CTL_NUM_LOG_PAGES) {
6942 ctl_set_invalid_field(ctsio,
6948 ctl_done((union ctl_io *)ctsio);
6949 return (CTL_RETVAL_COMPLETE);
6952 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6954 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6955 ctsio->kern_sg_entries = 0;
6956 ctsio->kern_rel_offset = 0;
6957 ctsio->kern_data_len = min(total_len, alloc_len);
6958 ctsio->kern_total_len = ctsio->kern_data_len;
6960 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6961 header->page = page_index->page_code;
6962 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6963 header->page |= SL_DS;
6964 if (page_index->subpage) {
6965 header->page |= SL_SPF;
6966 header->subpage = page_index->subpage;
6968 scsi_ulto2b(page_index->page_len, header->datalen);
6971 * Call the handler, if it exists, to update the
6972 * page to the latest values.
6974 if (page_index->sense_handler != NULL)
6975 page_index->sense_handler(ctsio, page_index, pc);
6977 memcpy(header + 1, page_index->page_data, page_index->page_len);
6979 ctl_set_success(ctsio);
6980 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6981 ctsio->be_move_done = ctl_config_move_done;
6982 ctl_datamove((union ctl_io *)ctsio);
6983 return (CTL_RETVAL_COMPLETE);
6987 ctl_read_capacity(struct ctl_scsiio *ctsio)
6989 struct ctl_lun *lun = CTL_LUN(ctsio);
6990 struct scsi_read_capacity *cdb;
6991 struct scsi_read_capacity_data *data;
6994 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6996 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6998 lba = scsi_4btoul(cdb->addr);
6999 if (((cdb->pmi & SRC_PMI) == 0)
7001 ctl_set_invalid_field(/*ctsio*/ ctsio,
7007 ctl_done((union ctl_io *)ctsio);
7008 return (CTL_RETVAL_COMPLETE);
7011 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7012 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
7013 ctsio->kern_data_len = sizeof(*data);
7014 ctsio->kern_total_len = sizeof(*data);
7015 ctsio->kern_rel_offset = 0;
7016 ctsio->kern_sg_entries = 0;
7019 * If the maximum LBA is greater than 0xfffffffe, the user must
7020 * issue a SERVICE ACTION IN (16) command, with the read capacity
7021 * serivce action set.
7023 if (lun->be_lun->maxlba > 0xfffffffe)
7024 scsi_ulto4b(0xffffffff, data->addr);
7026 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
7029 * XXX KDM this may not be 512 bytes...
7031 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7033 ctl_set_success(ctsio);
7034 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7035 ctsio->be_move_done = ctl_config_move_done;
7036 ctl_datamove((union ctl_io *)ctsio);
7037 return (CTL_RETVAL_COMPLETE);
7041 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
7043 struct ctl_lun *lun = CTL_LUN(ctsio);
7044 struct scsi_read_capacity_16 *cdb;
7045 struct scsi_read_capacity_data_long *data;
7049 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7051 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7053 alloc_len = scsi_4btoul(cdb->alloc_len);
7054 lba = scsi_8btou64(cdb->addr);
7056 if ((cdb->reladr & SRC16_PMI)
7058 ctl_set_invalid_field(/*ctsio*/ ctsio,
7064 ctl_done((union ctl_io *)ctsio);
7065 return (CTL_RETVAL_COMPLETE);
7068 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7069 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7070 ctsio->kern_rel_offset = 0;
7071 ctsio->kern_sg_entries = 0;
7072 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
7073 ctsio->kern_total_len = ctsio->kern_data_len;
7075 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7076 /* XXX KDM this may not be 512 bytes... */
7077 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7078 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7079 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7080 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7081 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7083 ctl_set_success(ctsio);
7084 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7085 ctsio->be_move_done = ctl_config_move_done;
7086 ctl_datamove((union ctl_io *)ctsio);
7087 return (CTL_RETVAL_COMPLETE);
7091 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7093 struct ctl_lun *lun = CTL_LUN(ctsio);
7094 struct scsi_get_lba_status *cdb;
7095 struct scsi_get_lba_status_data *data;
7096 struct ctl_lba_len_flags *lbalen;
7098 uint32_t alloc_len, total_len;
7101 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7103 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7104 lba = scsi_8btou64(cdb->addr);
7105 alloc_len = scsi_4btoul(cdb->alloc_len);
7107 if (lba > lun->be_lun->maxlba) {
7108 ctl_set_lba_out_of_range(ctsio, lba);
7109 ctl_done((union ctl_io *)ctsio);
7110 return (CTL_RETVAL_COMPLETE);
7113 total_len = sizeof(*data) + sizeof(data->descr[0]);
7114 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7115 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7116 ctsio->kern_rel_offset = 0;
7117 ctsio->kern_sg_entries = 0;
7118 ctsio->kern_data_len = min(total_len, alloc_len);
7119 ctsio->kern_total_len = ctsio->kern_data_len;
7121 /* Fill dummy data in case backend can't tell anything. */
7122 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7123 scsi_u64to8b(lba, data->descr[0].addr);
7124 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7125 data->descr[0].length);
7126 data->descr[0].status = 0; /* Mapped or unknown. */
7128 ctl_set_success(ctsio);
7129 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7130 ctsio->be_move_done = ctl_config_move_done;
7132 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7134 lbalen->len = total_len;
7136 retval = lun->backend->config_read((union ctl_io *)ctsio);
7141 ctl_read_defect(struct ctl_scsiio *ctsio)
7143 struct scsi_read_defect_data_10 *ccb10;
7144 struct scsi_read_defect_data_12 *ccb12;
7145 struct scsi_read_defect_data_hdr_10 *data10;
7146 struct scsi_read_defect_data_hdr_12 *data12;
7147 uint32_t alloc_len, data_len;
7150 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7152 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7153 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7154 format = ccb10->format;
7155 alloc_len = scsi_2btoul(ccb10->alloc_length);
7156 data_len = sizeof(*data10);
7158 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7159 format = ccb12->format;
7160 alloc_len = scsi_4btoul(ccb12->alloc_length);
7161 data_len = sizeof(*data12);
7163 if (alloc_len == 0) {
7164 ctl_set_success(ctsio);
7165 ctl_done((union ctl_io *)ctsio);
7166 return (CTL_RETVAL_COMPLETE);
7169 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7170 ctsio->kern_rel_offset = 0;
7171 ctsio->kern_sg_entries = 0;
7172 ctsio->kern_data_len = min(data_len, alloc_len);
7173 ctsio->kern_total_len = ctsio->kern_data_len;
7175 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7176 data10 = (struct scsi_read_defect_data_hdr_10 *)
7177 ctsio->kern_data_ptr;
7178 data10->format = format;
7179 scsi_ulto2b(0, data10->length);
7181 data12 = (struct scsi_read_defect_data_hdr_12 *)
7182 ctsio->kern_data_ptr;
7183 data12->format = format;
7184 scsi_ulto2b(0, data12->generation);
7185 scsi_ulto4b(0, data12->length);
7188 ctl_set_success(ctsio);
7189 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7190 ctsio->be_move_done = ctl_config_move_done;
7191 ctl_datamove((union ctl_io *)ctsio);
7192 return (CTL_RETVAL_COMPLETE);
7196 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7198 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7199 struct ctl_lun *lun = CTL_LUN(ctsio);
7200 struct scsi_maintenance_in *cdb;
7202 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7203 int num_ha_groups, num_target_ports, shared_group;
7204 struct ctl_port *port;
7205 struct scsi_target_group_data *rtg_ptr;
7206 struct scsi_target_group_data_extended *rtg_ext_ptr;
7207 struct scsi_target_port_group_descriptor *tpg_desc;
7209 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7211 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7212 retval = CTL_RETVAL_COMPLETE;
7214 switch (cdb->byte2 & STG_PDF_MASK) {
7215 case STG_PDF_LENGTH:
7218 case STG_PDF_EXTENDED:
7222 ctl_set_invalid_field(/*ctsio*/ ctsio,
7228 ctl_done((union ctl_io *)ctsio);
7232 num_target_ports = 0;
7233 shared_group = (softc->is_single != 0);
7234 mtx_lock(&softc->ctl_lock);
7235 STAILQ_FOREACH(port, &softc->port_list, links) {
7236 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7238 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7241 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7244 mtx_unlock(&softc->ctl_lock);
7245 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7248 total_len = sizeof(struct scsi_target_group_data_extended);
7250 total_len = sizeof(struct scsi_target_group_data);
7251 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7252 (shared_group + num_ha_groups) +
7253 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7255 alloc_len = scsi_4btoul(cdb->length);
7257 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7258 ctsio->kern_sg_entries = 0;
7259 ctsio->kern_rel_offset = 0;
7260 ctsio->kern_data_len = min(total_len, alloc_len);
7261 ctsio->kern_total_len = ctsio->kern_data_len;
7264 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7265 ctsio->kern_data_ptr;
7266 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7267 rtg_ext_ptr->format_type = 0x10;
7268 rtg_ext_ptr->implicit_transition_time = 0;
7269 tpg_desc = &rtg_ext_ptr->groups[0];
7271 rtg_ptr = (struct scsi_target_group_data *)
7272 ctsio->kern_data_ptr;
7273 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7274 tpg_desc = &rtg_ptr->groups[0];
7277 mtx_lock(&softc->ctl_lock);
7278 pg = softc->port_min / softc->port_cnt;
7279 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7280 /* Some shelf is known to be primary. */
7281 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7282 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7283 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7284 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7285 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7286 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7288 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7289 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7290 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7293 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7296 /* No known primary shelf. */
7297 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7298 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7299 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7300 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7301 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7302 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7304 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7308 tpg_desc->pref_state = ts;
7309 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7310 TPG_U_SUP | TPG_T_SUP;
7311 scsi_ulto2b(1, tpg_desc->target_port_group);
7312 tpg_desc->status = TPG_IMPLICIT;
7314 STAILQ_FOREACH(port, &softc->port_list, links) {
7315 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7317 if (!softc->is_single &&
7318 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7320 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7322 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7323 relative_target_port_identifier);
7326 tpg_desc->target_port_count = pc;
7327 tpg_desc = (struct scsi_target_port_group_descriptor *)
7328 &tpg_desc->descriptors[pc];
7330 for (g = 0; g < num_ha_groups; g++) {
7331 tpg_desc->pref_state = (g == pg) ? ts : os;
7332 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7333 TPG_U_SUP | TPG_T_SUP;
7334 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7335 tpg_desc->status = TPG_IMPLICIT;
7337 STAILQ_FOREACH(port, &softc->port_list, links) {
7338 if (port->targ_port < g * softc->port_cnt ||
7339 port->targ_port >= (g + 1) * softc->port_cnt)
7341 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7343 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7345 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7347 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7348 relative_target_port_identifier);
7351 tpg_desc->target_port_count = pc;
7352 tpg_desc = (struct scsi_target_port_group_descriptor *)
7353 &tpg_desc->descriptors[pc];
7355 mtx_unlock(&softc->ctl_lock);
7357 ctl_set_success(ctsio);
7358 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7359 ctsio->be_move_done = ctl_config_move_done;
7360 ctl_datamove((union ctl_io *)ctsio);
7365 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7367 struct ctl_lun *lun = CTL_LUN(ctsio);
7368 struct scsi_report_supported_opcodes *cdb;
7369 const struct ctl_cmd_entry *entry, *sentry;
7370 struct scsi_report_supported_opcodes_all *all;
7371 struct scsi_report_supported_opcodes_descr *descr;
7372 struct scsi_report_supported_opcodes_one *one;
7374 int alloc_len, total_len;
7375 int opcode, service_action, i, j, num;
7377 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7379 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7380 retval = CTL_RETVAL_COMPLETE;
7382 opcode = cdb->requested_opcode;
7383 service_action = scsi_2btoul(cdb->requested_service_action);
7384 switch (cdb->options & RSO_OPTIONS_MASK) {
7385 case RSO_OPTIONS_ALL:
7387 for (i = 0; i < 256; i++) {
7388 entry = &ctl_cmd_table[i];
7389 if (entry->flags & CTL_CMD_FLAG_SA5) {
7390 for (j = 0; j < 32; j++) {
7391 sentry = &((const struct ctl_cmd_entry *)
7393 if (ctl_cmd_applicable(
7394 lun->be_lun->lun_type, sentry))
7398 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7403 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7404 num * sizeof(struct scsi_report_supported_opcodes_descr);
7406 case RSO_OPTIONS_OC:
7407 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7408 ctl_set_invalid_field(/*ctsio*/ ctsio,
7414 ctl_done((union ctl_io *)ctsio);
7415 return (CTL_RETVAL_COMPLETE);
7417 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7419 case RSO_OPTIONS_OC_SA:
7420 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7421 service_action >= 32) {
7422 ctl_set_invalid_field(/*ctsio*/ ctsio,
7428 ctl_done((union ctl_io *)ctsio);
7429 return (CTL_RETVAL_COMPLETE);
7432 case RSO_OPTIONS_OC_ASA:
7433 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7436 ctl_set_invalid_field(/*ctsio*/ ctsio,
7442 ctl_done((union ctl_io *)ctsio);
7443 return (CTL_RETVAL_COMPLETE);
7446 alloc_len = scsi_4btoul(cdb->length);
7448 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7449 ctsio->kern_sg_entries = 0;
7450 ctsio->kern_rel_offset = 0;
7451 ctsio->kern_data_len = min(total_len, alloc_len);
7452 ctsio->kern_total_len = ctsio->kern_data_len;
7454 switch (cdb->options & RSO_OPTIONS_MASK) {
7455 case RSO_OPTIONS_ALL:
7456 all = (struct scsi_report_supported_opcodes_all *)
7457 ctsio->kern_data_ptr;
7459 for (i = 0; i < 256; i++) {
7460 entry = &ctl_cmd_table[i];
7461 if (entry->flags & CTL_CMD_FLAG_SA5) {
7462 for (j = 0; j < 32; j++) {
7463 sentry = &((const struct ctl_cmd_entry *)
7465 if (!ctl_cmd_applicable(
7466 lun->be_lun->lun_type, sentry))
7468 descr = &all->descr[num++];
7470 scsi_ulto2b(j, descr->service_action);
7471 descr->flags = RSO_SERVACTV;
7472 scsi_ulto2b(sentry->length,
7476 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7479 descr = &all->descr[num++];
7481 scsi_ulto2b(0, descr->service_action);
7483 scsi_ulto2b(entry->length, descr->cdb_length);
7487 num * sizeof(struct scsi_report_supported_opcodes_descr),
7490 case RSO_OPTIONS_OC:
7491 one = (struct scsi_report_supported_opcodes_one *)
7492 ctsio->kern_data_ptr;
7493 entry = &ctl_cmd_table[opcode];
7495 case RSO_OPTIONS_OC_SA:
7496 one = (struct scsi_report_supported_opcodes_one *)
7497 ctsio->kern_data_ptr;
7498 entry = &ctl_cmd_table[opcode];
7499 entry = &((const struct ctl_cmd_entry *)
7500 entry->execute)[service_action];
7502 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7504 scsi_ulto2b(entry->length, one->cdb_length);
7505 one->cdb_usage[0] = opcode;
7506 memcpy(&one->cdb_usage[1], entry->usage,
7511 case RSO_OPTIONS_OC_ASA:
7512 one = (struct scsi_report_supported_opcodes_one *)
7513 ctsio->kern_data_ptr;
7514 entry = &ctl_cmd_table[opcode];
7515 if (entry->flags & CTL_CMD_FLAG_SA5) {
7516 entry = &((const struct ctl_cmd_entry *)
7517 entry->execute)[service_action];
7518 } else if (service_action != 0) {
7525 ctl_set_success(ctsio);
7526 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7527 ctsio->be_move_done = ctl_config_move_done;
7528 ctl_datamove((union ctl_io *)ctsio);
7533 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7535 struct scsi_report_supported_tmf *cdb;
7536 struct scsi_report_supported_tmf_ext_data *data;
7538 int alloc_len, total_len;
7540 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7542 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7544 retval = CTL_RETVAL_COMPLETE;
7546 if (cdb->options & RST_REPD)
7547 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7549 total_len = sizeof(struct scsi_report_supported_tmf_data);
7550 alloc_len = scsi_4btoul(cdb->length);
7552 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7553 ctsio->kern_sg_entries = 0;
7554 ctsio->kern_rel_offset = 0;
7555 ctsio->kern_data_len = min(total_len, alloc_len);
7556 ctsio->kern_total_len = ctsio->kern_data_len;
7558 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7559 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7561 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7562 data->length = total_len - 4;
7564 ctl_set_success(ctsio);
7565 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7566 ctsio->be_move_done = ctl_config_move_done;
7567 ctl_datamove((union ctl_io *)ctsio);
7572 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7574 struct scsi_report_timestamp *cdb;
7575 struct scsi_report_timestamp_data *data;
7579 int alloc_len, total_len;
7581 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7583 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7585 retval = CTL_RETVAL_COMPLETE;
7587 total_len = sizeof(struct scsi_report_timestamp_data);
7588 alloc_len = scsi_4btoul(cdb->length);
7590 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7591 ctsio->kern_sg_entries = 0;
7592 ctsio->kern_rel_offset = 0;
7593 ctsio->kern_data_len = min(total_len, alloc_len);
7594 ctsio->kern_total_len = ctsio->kern_data_len;
7596 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7597 scsi_ulto2b(sizeof(*data) - 2, data->length);
7598 data->origin = RTS_ORIG_OUTSIDE;
7600 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7601 scsi_ulto4b(timestamp >> 16, data->timestamp);
7602 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7604 ctl_set_success(ctsio);
7605 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7606 ctsio->be_move_done = ctl_config_move_done;
7607 ctl_datamove((union ctl_io *)ctsio);
7612 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7614 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7615 struct ctl_lun *lun = CTL_LUN(ctsio);
7616 struct scsi_per_res_in *cdb;
7617 int alloc_len, total_len = 0;
7618 /* struct scsi_per_res_in_rsrv in_data; */
7621 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7623 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7625 alloc_len = scsi_2btoul(cdb->length);
7628 mtx_lock(&lun->lun_lock);
7629 switch (cdb->action) {
7630 case SPRI_RK: /* read keys */
7631 total_len = sizeof(struct scsi_per_res_in_keys) +
7633 sizeof(struct scsi_per_res_key);
7635 case SPRI_RR: /* read reservation */
7636 if (lun->flags & CTL_LUN_PR_RESERVED)
7637 total_len = sizeof(struct scsi_per_res_in_rsrv);
7639 total_len = sizeof(struct scsi_per_res_in_header);
7641 case SPRI_RC: /* report capabilities */
7642 total_len = sizeof(struct scsi_per_res_cap);
7644 case SPRI_RS: /* read full status */
7645 total_len = sizeof(struct scsi_per_res_in_header) +
7646 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7650 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7652 mtx_unlock(&lun->lun_lock);
7654 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7655 ctsio->kern_rel_offset = 0;
7656 ctsio->kern_sg_entries = 0;
7657 ctsio->kern_data_len = min(total_len, alloc_len);
7658 ctsio->kern_total_len = ctsio->kern_data_len;
7660 mtx_lock(&lun->lun_lock);
7661 switch (cdb->action) {
7662 case SPRI_RK: { // read keys
7663 struct scsi_per_res_in_keys *res_keys;
7666 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7669 * We had to drop the lock to allocate our buffer, which
7670 * leaves time for someone to come in with another
7671 * persistent reservation. (That is unlikely, though,
7672 * since this should be the only persistent reservation
7673 * command active right now.)
7675 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7676 (lun->pr_key_count *
7677 sizeof(struct scsi_per_res_key)))){
7678 mtx_unlock(&lun->lun_lock);
7679 free(ctsio->kern_data_ptr, M_CTL);
7680 printf("%s: reservation length changed, retrying\n",
7685 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7687 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7688 lun->pr_key_count, res_keys->header.length);
7690 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7691 if ((key = ctl_get_prkey(lun, i)) == 0)
7695 * We used lun->pr_key_count to calculate the
7696 * size to allocate. If it turns out the number of
7697 * initiators with the registered flag set is
7698 * larger than that (i.e. they haven't been kept in
7699 * sync), we've got a problem.
7701 if (key_count >= lun->pr_key_count) {
7705 scsi_u64to8b(key, res_keys->keys[key_count].key);
7710 case SPRI_RR: { // read reservation
7711 struct scsi_per_res_in_rsrv *res;
7712 int tmp_len, header_only;
7714 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7716 scsi_ulto4b(lun->pr_generation, res->header.generation);
7718 if (lun->flags & CTL_LUN_PR_RESERVED)
7720 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7721 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7722 res->header.length);
7725 tmp_len = sizeof(struct scsi_per_res_in_header);
7726 scsi_ulto4b(0, res->header.length);
7731 * We had to drop the lock to allocate our buffer, which
7732 * leaves time for someone to come in with another
7733 * persistent reservation. (That is unlikely, though,
7734 * since this should be the only persistent reservation
7735 * command active right now.)
7737 if (tmp_len != total_len) {
7738 mtx_unlock(&lun->lun_lock);
7739 free(ctsio->kern_data_ptr, M_CTL);
7740 printf("%s: reservation status changed, retrying\n",
7746 * No reservation held, so we're done.
7748 if (header_only != 0)
7752 * If the registration is an All Registrants type, the key
7753 * is 0, since it doesn't really matter.
7755 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7756 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7757 res->data.reservation);
7759 res->data.scopetype = lun->pr_res_type;
7762 case SPRI_RC: //report capabilities
7764 struct scsi_per_res_cap *res_cap;
7767 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7768 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7769 res_cap->flags1 = SPRI_CRH;
7770 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7771 type_mask = SPRI_TM_WR_EX_AR |
7777 scsi_ulto2b(type_mask, res_cap->type_mask);
7780 case SPRI_RS: { // read full status
7781 struct scsi_per_res_in_full *res_status;
7782 struct scsi_per_res_in_full_desc *res_desc;
7783 struct ctl_port *port;
7786 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7789 * We had to drop the lock to allocate our buffer, which
7790 * leaves time for someone to come in with another
7791 * persistent reservation. (That is unlikely, though,
7792 * since this should be the only persistent reservation
7793 * command active right now.)
7795 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7796 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7797 lun->pr_key_count)){
7798 mtx_unlock(&lun->lun_lock);
7799 free(ctsio->kern_data_ptr, M_CTL);
7800 printf("%s: reservation length changed, retrying\n",
7805 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7807 res_desc = &res_status->desc[0];
7808 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7809 if ((key = ctl_get_prkey(lun, i)) == 0)
7812 scsi_u64to8b(key, res_desc->res_key.key);
7813 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7814 (lun->pr_res_idx == i ||
7815 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7816 res_desc->flags = SPRI_FULL_R_HOLDER;
7817 res_desc->scopetype = lun->pr_res_type;
7819 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7820 res_desc->rel_trgt_port_id);
7822 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7824 len = ctl_create_iid(port,
7825 i % CTL_MAX_INIT_PER_PORT,
7826 res_desc->transport_id);
7827 scsi_ulto4b(len, res_desc->additional_length);
7828 res_desc = (struct scsi_per_res_in_full_desc *)
7829 &res_desc->transport_id[len];
7831 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7832 res_status->header.length);
7836 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7838 mtx_unlock(&lun->lun_lock);
7840 ctl_set_success(ctsio);
7841 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7842 ctsio->be_move_done = ctl_config_move_done;
7843 ctl_datamove((union ctl_io *)ctsio);
7844 return (CTL_RETVAL_COMPLETE);
7848 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7852 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7853 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7854 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7855 struct scsi_per_res_out_parms* param)
7857 union ctl_ha_msg persis_io;
7860 mtx_lock(&lun->lun_lock);
7861 if (sa_res_key == 0) {
7862 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7863 /* validate scope and type */
7864 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7866 mtx_unlock(&lun->lun_lock);
7867 ctl_set_invalid_field(/*ctsio*/ ctsio,
7873 ctl_done((union ctl_io *)ctsio);
7877 if (type>8 || type==2 || type==4 || type==0) {
7878 mtx_unlock(&lun->lun_lock);
7879 ctl_set_invalid_field(/*ctsio*/ ctsio,
7885 ctl_done((union ctl_io *)ctsio);
7890 * Unregister everybody else and build UA for
7893 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7894 if (i == residx || ctl_get_prkey(lun, i) == 0)
7897 ctl_clr_prkey(lun, i);
7898 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7900 lun->pr_key_count = 1;
7901 lun->pr_res_type = type;
7902 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7903 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7904 lun->pr_res_idx = residx;
7905 lun->pr_generation++;
7906 mtx_unlock(&lun->lun_lock);
7908 /* send msg to other side */
7909 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7910 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7911 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7912 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7913 persis_io.pr.pr_info.res_type = type;
7914 memcpy(persis_io.pr.pr_info.sa_res_key,
7915 param->serv_act_res_key,
7916 sizeof(param->serv_act_res_key));
7917 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7918 sizeof(persis_io.pr), M_WAITOK);
7920 /* not all registrants */
7921 mtx_unlock(&lun->lun_lock);
7922 free(ctsio->kern_data_ptr, M_CTL);
7923 ctl_set_invalid_field(ctsio,
7929 ctl_done((union ctl_io *)ctsio);
7932 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7933 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7936 if (res_key == sa_res_key) {
7939 * The spec implies this is not good but doesn't
7940 * say what to do. There are two choices either
7941 * generate a res conflict or check condition
7942 * with illegal field in parameter data. Since
7943 * that is what is done when the sa_res_key is
7944 * zero I'll take that approach since this has
7945 * to do with the sa_res_key.
7947 mtx_unlock(&lun->lun_lock);
7948 free(ctsio->kern_data_ptr, M_CTL);
7949 ctl_set_invalid_field(ctsio,
7955 ctl_done((union ctl_io *)ctsio);
7959 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7960 if (ctl_get_prkey(lun, i) != sa_res_key)
7964 ctl_clr_prkey(lun, i);
7965 lun->pr_key_count--;
7966 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7969 mtx_unlock(&lun->lun_lock);
7970 free(ctsio->kern_data_ptr, M_CTL);
7971 ctl_set_reservation_conflict(ctsio);
7972 ctl_done((union ctl_io *)ctsio);
7973 return (CTL_RETVAL_COMPLETE);
7975 lun->pr_generation++;
7976 mtx_unlock(&lun->lun_lock);
7978 /* send msg to other side */
7979 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7980 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7981 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7982 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7983 persis_io.pr.pr_info.res_type = type;
7984 memcpy(persis_io.pr.pr_info.sa_res_key,
7985 param->serv_act_res_key,
7986 sizeof(param->serv_act_res_key));
7987 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7988 sizeof(persis_io.pr), M_WAITOK);
7990 /* Reserved but not all registrants */
7991 /* sa_res_key is res holder */
7992 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7993 /* validate scope and type */
7994 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7996 mtx_unlock(&lun->lun_lock);
7997 ctl_set_invalid_field(/*ctsio*/ ctsio,
8003 ctl_done((union ctl_io *)ctsio);
8007 if (type>8 || type==2 || type==4 || type==0) {
8008 mtx_unlock(&lun->lun_lock);
8009 ctl_set_invalid_field(/*ctsio*/ ctsio,
8015 ctl_done((union ctl_io *)ctsio);
8021 * if sa_res_key != res_key remove all
8022 * registrants w/sa_res_key and generate UA
8023 * for these registrants(Registrations
8024 * Preempted) if it wasn't an exclusive
8025 * reservation generate UA(Reservations
8026 * Preempted) for all other registered nexuses
8027 * if the type has changed. Establish the new
8028 * reservation and holder. If res_key and
8029 * sa_res_key are the same do the above
8030 * except don't unregister the res holder.
8033 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8034 if (i == residx || ctl_get_prkey(lun, i) == 0)
8037 if (sa_res_key == ctl_get_prkey(lun, i)) {
8038 ctl_clr_prkey(lun, i);
8039 lun->pr_key_count--;
8040 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8041 } else if (type != lun->pr_res_type &&
8042 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8043 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8044 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8047 lun->pr_res_type = type;
8048 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8049 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8050 lun->pr_res_idx = residx;
8052 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8053 lun->pr_generation++;
8054 mtx_unlock(&lun->lun_lock);
8056 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8057 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8058 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8059 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8060 persis_io.pr.pr_info.res_type = type;
8061 memcpy(persis_io.pr.pr_info.sa_res_key,
8062 param->serv_act_res_key,
8063 sizeof(param->serv_act_res_key));
8064 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8065 sizeof(persis_io.pr), M_WAITOK);
8068 * sa_res_key is not the res holder just
8069 * remove registrants
8073 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8074 if (sa_res_key != ctl_get_prkey(lun, i))
8078 ctl_clr_prkey(lun, i);
8079 lun->pr_key_count--;
8080 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8084 mtx_unlock(&lun->lun_lock);
8085 free(ctsio->kern_data_ptr, M_CTL);
8086 ctl_set_reservation_conflict(ctsio);
8087 ctl_done((union ctl_io *)ctsio);
8090 lun->pr_generation++;
8091 mtx_unlock(&lun->lun_lock);
8093 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8094 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8095 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8096 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8097 persis_io.pr.pr_info.res_type = type;
8098 memcpy(persis_io.pr.pr_info.sa_res_key,
8099 param->serv_act_res_key,
8100 sizeof(param->serv_act_res_key));
8101 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8102 sizeof(persis_io.pr), M_WAITOK);
8109 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8111 uint64_t sa_res_key;
8114 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8116 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8117 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8118 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8119 if (sa_res_key == 0) {
8121 * Unregister everybody else and build UA for
8124 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8125 if (i == msg->pr.pr_info.residx ||
8126 ctl_get_prkey(lun, i) == 0)
8129 ctl_clr_prkey(lun, i);
8130 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8133 lun->pr_key_count = 1;
8134 lun->pr_res_type = msg->pr.pr_info.res_type;
8135 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8136 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8137 lun->pr_res_idx = msg->pr.pr_info.residx;
8139 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8140 if (sa_res_key == ctl_get_prkey(lun, i))
8143 ctl_clr_prkey(lun, i);
8144 lun->pr_key_count--;
8145 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8149 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8150 if (i == msg->pr.pr_info.residx ||
8151 ctl_get_prkey(lun, i) == 0)
8154 if (sa_res_key == ctl_get_prkey(lun, i)) {
8155 ctl_clr_prkey(lun, i);
8156 lun->pr_key_count--;
8157 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8158 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8159 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8160 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8161 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8164 lun->pr_res_type = msg->pr.pr_info.res_type;
8165 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8166 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8167 lun->pr_res_idx = msg->pr.pr_info.residx;
8169 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8171 lun->pr_generation++;
8177 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8179 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8180 struct ctl_lun *lun = CTL_LUN(ctsio);
8182 u_int32_t param_len;
8183 struct scsi_per_res_out *cdb;
8184 struct scsi_per_res_out_parms* param;
8186 uint64_t res_key, sa_res_key, key;
8188 union ctl_ha_msg persis_io;
8191 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8193 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8194 retval = CTL_RETVAL_COMPLETE;
8197 * We only support whole-LUN scope. The scope & type are ignored for
8198 * register, register and ignore existing key and clear.
8199 * We sometimes ignore scope and type on preempts too!!
8200 * Verify reservation type here as well.
8202 type = cdb->scope_type & SPR_TYPE_MASK;
8203 if ((cdb->action == SPRO_RESERVE)
8204 || (cdb->action == SPRO_RELEASE)) {
8205 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8206 ctl_set_invalid_field(/*ctsio*/ ctsio,
8212 ctl_done((union ctl_io *)ctsio);
8213 return (CTL_RETVAL_COMPLETE);
8216 if (type>8 || type==2 || type==4 || type==0) {
8217 ctl_set_invalid_field(/*ctsio*/ ctsio,
8223 ctl_done((union ctl_io *)ctsio);
8224 return (CTL_RETVAL_COMPLETE);
8228 param_len = scsi_4btoul(cdb->length);
8230 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8231 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8232 ctsio->kern_data_len = param_len;
8233 ctsio->kern_total_len = param_len;
8234 ctsio->kern_rel_offset = 0;
8235 ctsio->kern_sg_entries = 0;
8236 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8237 ctsio->be_move_done = ctl_config_move_done;
8238 ctl_datamove((union ctl_io *)ctsio);
8240 return (CTL_RETVAL_COMPLETE);
8243 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8245 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8246 res_key = scsi_8btou64(param->res_key.key);
8247 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8250 * Validate the reservation key here except for SPRO_REG_IGNO
8251 * This must be done for all other service actions
8253 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8254 mtx_lock(&lun->lun_lock);
8255 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8256 if (res_key != key) {
8258 * The current key passed in doesn't match
8259 * the one the initiator previously
8262 mtx_unlock(&lun->lun_lock);
8263 free(ctsio->kern_data_ptr, M_CTL);
8264 ctl_set_reservation_conflict(ctsio);
8265 ctl_done((union ctl_io *)ctsio);
8266 return (CTL_RETVAL_COMPLETE);
8268 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8270 * We are not registered
8272 mtx_unlock(&lun->lun_lock);
8273 free(ctsio->kern_data_ptr, M_CTL);
8274 ctl_set_reservation_conflict(ctsio);
8275 ctl_done((union ctl_io *)ctsio);
8276 return (CTL_RETVAL_COMPLETE);
8277 } else if (res_key != 0) {
8279 * We are not registered and trying to register but
8280 * the register key isn't zero.
8282 mtx_unlock(&lun->lun_lock);
8283 free(ctsio->kern_data_ptr, M_CTL);
8284 ctl_set_reservation_conflict(ctsio);
8285 ctl_done((union ctl_io *)ctsio);
8286 return (CTL_RETVAL_COMPLETE);
8288 mtx_unlock(&lun->lun_lock);
8291 switch (cdb->action & SPRO_ACTION_MASK) {
8293 case SPRO_REG_IGNO: {
8296 * We don't support any of these options, as we report in
8297 * the read capabilities request (see
8298 * ctl_persistent_reserve_in(), above).
8300 if ((param->flags & SPR_SPEC_I_PT)
8301 || (param->flags & SPR_ALL_TG_PT)
8302 || (param->flags & SPR_APTPL)) {
8305 if (param->flags & SPR_APTPL)
8307 else if (param->flags & SPR_ALL_TG_PT)
8309 else /* SPR_SPEC_I_PT */
8312 free(ctsio->kern_data_ptr, M_CTL);
8313 ctl_set_invalid_field(ctsio,
8319 ctl_done((union ctl_io *)ctsio);
8320 return (CTL_RETVAL_COMPLETE);
8323 mtx_lock(&lun->lun_lock);
8326 * The initiator wants to clear the
8329 if (sa_res_key == 0) {
8331 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8332 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8333 && ctl_get_prkey(lun, residx) == 0)) {
8334 mtx_unlock(&lun->lun_lock);
8338 ctl_clr_prkey(lun, residx);
8339 lun->pr_key_count--;
8341 if (residx == lun->pr_res_idx) {
8342 lun->flags &= ~CTL_LUN_PR_RESERVED;
8343 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8345 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8346 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8347 lun->pr_key_count) {
8349 * If the reservation is a registrants
8350 * only type we need to generate a UA
8351 * for other registered inits. The
8352 * sense code should be RESERVATIONS
8356 for (i = softc->init_min; i < softc->init_max; i++){
8357 if (ctl_get_prkey(lun, i) == 0)
8360 CTL_UA_RES_RELEASE);
8363 lun->pr_res_type = 0;
8364 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8365 if (lun->pr_key_count==0) {
8366 lun->flags &= ~CTL_LUN_PR_RESERVED;
8367 lun->pr_res_type = 0;
8368 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8371 lun->pr_generation++;
8372 mtx_unlock(&lun->lun_lock);
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_UNREG_KEY;
8377 persis_io.pr.pr_info.residx = residx;
8378 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8379 sizeof(persis_io.pr), M_WAITOK);
8380 } else /* sa_res_key != 0 */ {
8383 * If we aren't registered currently then increment
8384 * the key count and set the registered flag.
8386 ctl_alloc_prkey(lun, residx);
8387 if (ctl_get_prkey(lun, residx) == 0)
8388 lun->pr_key_count++;
8389 ctl_set_prkey(lun, residx, sa_res_key);
8390 lun->pr_generation++;
8391 mtx_unlock(&lun->lun_lock);
8393 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8394 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8395 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8396 persis_io.pr.pr_info.residx = residx;
8397 memcpy(persis_io.pr.pr_info.sa_res_key,
8398 param->serv_act_res_key,
8399 sizeof(param->serv_act_res_key));
8400 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8401 sizeof(persis_io.pr), M_WAITOK);
8407 mtx_lock(&lun->lun_lock);
8408 if (lun->flags & CTL_LUN_PR_RESERVED) {
8410 * if this isn't the reservation holder and it's
8411 * not a "all registrants" type or if the type is
8412 * different then we have a conflict
8414 if ((lun->pr_res_idx != residx
8415 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8416 || lun->pr_res_type != type) {
8417 mtx_unlock(&lun->lun_lock);
8418 free(ctsio->kern_data_ptr, M_CTL);
8419 ctl_set_reservation_conflict(ctsio);
8420 ctl_done((union ctl_io *)ctsio);
8421 return (CTL_RETVAL_COMPLETE);
8423 mtx_unlock(&lun->lun_lock);
8424 } else /* create a reservation */ {
8426 * If it's not an "all registrants" type record
8427 * reservation holder
8429 if (type != SPR_TYPE_WR_EX_AR
8430 && type != SPR_TYPE_EX_AC_AR)
8431 lun->pr_res_idx = residx; /* Res holder */
8433 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8435 lun->flags |= CTL_LUN_PR_RESERVED;
8436 lun->pr_res_type = type;
8438 mtx_unlock(&lun->lun_lock);
8440 /* send msg to other side */
8441 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8442 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8443 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8444 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8445 persis_io.pr.pr_info.res_type = type;
8446 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8447 sizeof(persis_io.pr), M_WAITOK);
8452 mtx_lock(&lun->lun_lock);
8453 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8454 /* No reservation exists return good status */
8455 mtx_unlock(&lun->lun_lock);
8459 * Is this nexus a reservation holder?
8461 if (lun->pr_res_idx != residx
8462 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8464 * not a res holder return good status but
8467 mtx_unlock(&lun->lun_lock);
8471 if (lun->pr_res_type != type) {
8472 mtx_unlock(&lun->lun_lock);
8473 free(ctsio->kern_data_ptr, M_CTL);
8474 ctl_set_illegal_pr_release(ctsio);
8475 ctl_done((union ctl_io *)ctsio);
8476 return (CTL_RETVAL_COMPLETE);
8479 /* okay to release */
8480 lun->flags &= ~CTL_LUN_PR_RESERVED;
8481 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8482 lun->pr_res_type = 0;
8485 * If this isn't an exclusive access reservation and NUAR
8486 * is not set, generate UA for all other registrants.
8488 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8489 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8490 for (i = softc->init_min; i < softc->init_max; i++) {
8491 if (i == residx || ctl_get_prkey(lun, i) == 0)
8493 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8496 mtx_unlock(&lun->lun_lock);
8498 /* Send msg to other side */
8499 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8500 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8501 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8502 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8503 sizeof(persis_io.pr), M_WAITOK);
8507 /* send msg to other side */
8509 mtx_lock(&lun->lun_lock);
8510 lun->flags &= ~CTL_LUN_PR_RESERVED;
8511 lun->pr_res_type = 0;
8512 lun->pr_key_count = 0;
8513 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8515 ctl_clr_prkey(lun, residx);
8516 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8517 if (ctl_get_prkey(lun, i) != 0) {
8518 ctl_clr_prkey(lun, i);
8519 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8521 lun->pr_generation++;
8522 mtx_unlock(&lun->lun_lock);
8524 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8525 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8526 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8527 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8528 sizeof(persis_io.pr), M_WAITOK);
8532 case SPRO_PRE_ABO: {
8535 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8536 residx, ctsio, cdb, param);
8538 return (CTL_RETVAL_COMPLETE);
8542 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8546 free(ctsio->kern_data_ptr, M_CTL);
8547 ctl_set_success(ctsio);
8548 ctl_done((union ctl_io *)ctsio);
8554 * This routine is for handling a message from the other SC pertaining to
8555 * persistent reserve out. All the error checking will have been done
8556 * so only perorming the action need be done here to keep the two
8560 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8562 struct ctl_softc *softc = CTL_SOFTC(io);
8563 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8564 struct ctl_lun *lun;
8566 uint32_t residx, targ_lun;
8568 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8569 mtx_lock(&softc->ctl_lock);
8570 if (targ_lun >= ctl_max_luns ||
8571 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8572 mtx_unlock(&softc->ctl_lock);
8575 mtx_lock(&lun->lun_lock);
8576 mtx_unlock(&softc->ctl_lock);
8577 if (lun->flags & CTL_LUN_DISABLED) {
8578 mtx_unlock(&lun->lun_lock);
8581 residx = ctl_get_initindex(&msg->hdr.nexus);
8582 switch(msg->pr.pr_info.action) {
8583 case CTL_PR_REG_KEY:
8584 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8585 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8586 lun->pr_key_count++;
8587 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8588 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8589 lun->pr_generation++;
8592 case CTL_PR_UNREG_KEY:
8593 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8594 lun->pr_key_count--;
8596 /* XXX Need to see if the reservation has been released */
8597 /* if so do we need to generate UA? */
8598 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8599 lun->flags &= ~CTL_LUN_PR_RESERVED;
8600 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8602 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8603 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8604 lun->pr_key_count) {
8606 * If the reservation is a registrants
8607 * only type we need to generate a UA
8608 * for other registered inits. The
8609 * sense code should be RESERVATIONS
8613 for (i = softc->init_min; i < softc->init_max; i++) {
8614 if (ctl_get_prkey(lun, i) == 0)
8617 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8620 lun->pr_res_type = 0;
8621 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8622 if (lun->pr_key_count==0) {
8623 lun->flags &= ~CTL_LUN_PR_RESERVED;
8624 lun->pr_res_type = 0;
8625 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8628 lun->pr_generation++;
8631 case CTL_PR_RESERVE:
8632 lun->flags |= CTL_LUN_PR_RESERVED;
8633 lun->pr_res_type = msg->pr.pr_info.res_type;
8634 lun->pr_res_idx = msg->pr.pr_info.residx;
8638 case CTL_PR_RELEASE:
8640 * If this isn't an exclusive access reservation and NUAR
8641 * is not set, generate UA for all other registrants.
8643 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8644 lun->pr_res_type != SPR_TYPE_WR_EX &&
8645 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8646 for (i = softc->init_min; i < softc->init_max; i++) {
8647 if (i == residx || ctl_get_prkey(lun, i) == 0)
8649 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8653 lun->flags &= ~CTL_LUN_PR_RESERVED;
8654 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8655 lun->pr_res_type = 0;
8658 case CTL_PR_PREEMPT:
8659 ctl_pro_preempt_other(lun, msg);
8662 lun->flags &= ~CTL_LUN_PR_RESERVED;
8663 lun->pr_res_type = 0;
8664 lun->pr_key_count = 0;
8665 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8667 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8668 if (ctl_get_prkey(lun, i) == 0)
8670 ctl_clr_prkey(lun, i);
8671 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8673 lun->pr_generation++;
8677 mtx_unlock(&lun->lun_lock);
8681 ctl_read_write(struct ctl_scsiio *ctsio)
8683 struct ctl_lun *lun = CTL_LUN(ctsio);
8684 struct ctl_lba_len_flags *lbalen;
8686 uint32_t num_blocks;
8690 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8693 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8694 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8695 switch (ctsio->cdb[0]) {
8698 struct scsi_rw_6 *cdb;
8700 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8702 lba = scsi_3btoul(cdb->addr);
8703 /* only 5 bits are valid in the most significant address byte */
8705 num_blocks = cdb->length;
8707 * This is correct according to SBC-2.
8709 if (num_blocks == 0)
8715 struct scsi_rw_10 *cdb;
8717 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8718 if (cdb->byte2 & SRW10_FUA)
8719 flags |= CTL_LLF_FUA;
8720 if (cdb->byte2 & SRW10_DPO)
8721 flags |= CTL_LLF_DPO;
8722 lba = scsi_4btoul(cdb->addr);
8723 num_blocks = scsi_2btoul(cdb->length);
8726 case WRITE_VERIFY_10: {
8727 struct scsi_write_verify_10 *cdb;
8729 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8730 flags |= CTL_LLF_FUA;
8731 if (cdb->byte2 & SWV_DPO)
8732 flags |= CTL_LLF_DPO;
8733 lba = scsi_4btoul(cdb->addr);
8734 num_blocks = scsi_2btoul(cdb->length);
8739 struct scsi_rw_12 *cdb;
8741 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8742 if (cdb->byte2 & SRW12_FUA)
8743 flags |= CTL_LLF_FUA;
8744 if (cdb->byte2 & SRW12_DPO)
8745 flags |= CTL_LLF_DPO;
8746 lba = scsi_4btoul(cdb->addr);
8747 num_blocks = scsi_4btoul(cdb->length);
8750 case WRITE_VERIFY_12: {
8751 struct scsi_write_verify_12 *cdb;
8753 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8754 flags |= CTL_LLF_FUA;
8755 if (cdb->byte2 & SWV_DPO)
8756 flags |= CTL_LLF_DPO;
8757 lba = scsi_4btoul(cdb->addr);
8758 num_blocks = scsi_4btoul(cdb->length);
8763 struct scsi_rw_16 *cdb;
8765 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8766 if (cdb->byte2 & SRW12_FUA)
8767 flags |= CTL_LLF_FUA;
8768 if (cdb->byte2 & SRW12_DPO)
8769 flags |= CTL_LLF_DPO;
8770 lba = scsi_8btou64(cdb->addr);
8771 num_blocks = scsi_4btoul(cdb->length);
8774 case WRITE_ATOMIC_16: {
8775 struct scsi_write_atomic_16 *cdb;
8777 if (lun->be_lun->atomicblock == 0) {
8778 ctl_set_invalid_opcode(ctsio);
8779 ctl_done((union ctl_io *)ctsio);
8780 return (CTL_RETVAL_COMPLETE);
8783 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8784 if (cdb->byte2 & SRW12_FUA)
8785 flags |= CTL_LLF_FUA;
8786 if (cdb->byte2 & SRW12_DPO)
8787 flags |= CTL_LLF_DPO;
8788 lba = scsi_8btou64(cdb->addr);
8789 num_blocks = scsi_2btoul(cdb->length);
8790 if (num_blocks > lun->be_lun->atomicblock) {
8791 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8792 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8794 ctl_done((union ctl_io *)ctsio);
8795 return (CTL_RETVAL_COMPLETE);
8799 case WRITE_VERIFY_16: {
8800 struct scsi_write_verify_16 *cdb;
8802 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8803 flags |= CTL_LLF_FUA;
8804 if (cdb->byte2 & SWV_DPO)
8805 flags |= CTL_LLF_DPO;
8806 lba = scsi_8btou64(cdb->addr);
8807 num_blocks = scsi_4btoul(cdb->length);
8812 * We got a command we don't support. This shouldn't
8813 * happen, commands should be filtered out above us.
8815 ctl_set_invalid_opcode(ctsio);
8816 ctl_done((union ctl_io *)ctsio);
8818 return (CTL_RETVAL_COMPLETE);
8819 break; /* NOTREACHED */
8823 * The first check is to make sure we're in bounds, the second
8824 * check is to catch wrap-around problems. If the lba + num blocks
8825 * is less than the lba, then we've wrapped around and the block
8826 * range is invalid anyway.
8828 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8829 || ((lba + num_blocks) < lba)) {
8830 ctl_set_lba_out_of_range(ctsio,
8831 MAX(lba, lun->be_lun->maxlba + 1));
8832 ctl_done((union ctl_io *)ctsio);
8833 return (CTL_RETVAL_COMPLETE);
8837 * According to SBC-3, a transfer length of 0 is not an error.
8838 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8839 * translates to 256 blocks for those commands.
8841 if (num_blocks == 0) {
8842 ctl_set_success(ctsio);
8843 ctl_done((union ctl_io *)ctsio);
8844 return (CTL_RETVAL_COMPLETE);
8847 /* Set FUA and/or DPO if caches are disabled. */
8849 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8850 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8852 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8853 flags |= CTL_LLF_FUA;
8856 lbalen = (struct ctl_lba_len_flags *)
8857 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8859 lbalen->len = num_blocks;
8860 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8862 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8863 ctsio->kern_rel_offset = 0;
8865 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8867 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8872 ctl_cnw_cont(union ctl_io *io)
8874 struct ctl_lun *lun = CTL_LUN(io);
8875 struct ctl_scsiio *ctsio;
8876 struct ctl_lba_len_flags *lbalen;
8879 ctsio = &io->scsiio;
8880 ctsio->io_hdr.status = CTL_STATUS_NONE;
8881 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8882 lbalen = (struct ctl_lba_len_flags *)
8883 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8884 lbalen->flags &= ~CTL_LLF_COMPARE;
8885 lbalen->flags |= CTL_LLF_WRITE;
8887 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8888 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8893 ctl_cnw(struct ctl_scsiio *ctsio)
8895 struct ctl_lun *lun = CTL_LUN(ctsio);
8896 struct ctl_lba_len_flags *lbalen;
8898 uint32_t num_blocks;
8901 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8904 switch (ctsio->cdb[0]) {
8905 case COMPARE_AND_WRITE: {
8906 struct scsi_compare_and_write *cdb;
8908 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8909 if (cdb->byte2 & SRW10_FUA)
8910 flags |= CTL_LLF_FUA;
8911 if (cdb->byte2 & SRW10_DPO)
8912 flags |= CTL_LLF_DPO;
8913 lba = scsi_8btou64(cdb->addr);
8914 num_blocks = cdb->length;
8919 * We got a command we don't support. This shouldn't
8920 * happen, commands should be filtered out above us.
8922 ctl_set_invalid_opcode(ctsio);
8923 ctl_done((union ctl_io *)ctsio);
8925 return (CTL_RETVAL_COMPLETE);
8926 break; /* NOTREACHED */
8930 * The first check is to make sure we're in bounds, the second
8931 * check is to catch wrap-around problems. If the lba + num blocks
8932 * is less than the lba, then we've wrapped around and the block
8933 * range is invalid anyway.
8935 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8936 || ((lba + num_blocks) < lba)) {
8937 ctl_set_lba_out_of_range(ctsio,
8938 MAX(lba, lun->be_lun->maxlba + 1));
8939 ctl_done((union ctl_io *)ctsio);
8940 return (CTL_RETVAL_COMPLETE);
8944 * According to SBC-3, a transfer length of 0 is not an error.
8946 if (num_blocks == 0) {
8947 ctl_set_success(ctsio);
8948 ctl_done((union ctl_io *)ctsio);
8949 return (CTL_RETVAL_COMPLETE);
8952 /* Set FUA if write cache is disabled. */
8953 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8954 flags |= CTL_LLF_FUA;
8956 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8957 ctsio->kern_rel_offset = 0;
8960 * Set the IO_CONT flag, so that if this I/O gets passed to
8961 * ctl_data_submit_done(), it'll get passed back to
8962 * ctl_ctl_cnw_cont() for further processing.
8964 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8965 ctsio->io_cont = ctl_cnw_cont;
8967 lbalen = (struct ctl_lba_len_flags *)
8968 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8970 lbalen->len = num_blocks;
8971 lbalen->flags = CTL_LLF_COMPARE | flags;
8973 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8974 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8979 ctl_verify(struct ctl_scsiio *ctsio)
8981 struct ctl_lun *lun = CTL_LUN(ctsio);
8982 struct ctl_lba_len_flags *lbalen;
8984 uint32_t num_blocks;
8988 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8991 flags = CTL_LLF_FUA;
8992 switch (ctsio->cdb[0]) {
8994 struct scsi_verify_10 *cdb;
8996 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8997 if (cdb->byte2 & SVFY_BYTCHK)
8999 if (cdb->byte2 & SVFY_DPO)
9000 flags |= CTL_LLF_DPO;
9001 lba = scsi_4btoul(cdb->addr);
9002 num_blocks = scsi_2btoul(cdb->length);
9006 struct scsi_verify_12 *cdb;
9008 cdb = (struct scsi_verify_12 *)ctsio->cdb;
9009 if (cdb->byte2 & SVFY_BYTCHK)
9011 if (cdb->byte2 & SVFY_DPO)
9012 flags |= CTL_LLF_DPO;
9013 lba = scsi_4btoul(cdb->addr);
9014 num_blocks = scsi_4btoul(cdb->length);
9018 struct scsi_rw_16 *cdb;
9020 cdb = (struct scsi_rw_16 *)ctsio->cdb;
9021 if (cdb->byte2 & SVFY_BYTCHK)
9023 if (cdb->byte2 & SVFY_DPO)
9024 flags |= CTL_LLF_DPO;
9025 lba = scsi_8btou64(cdb->addr);
9026 num_blocks = scsi_4btoul(cdb->length);
9031 * We got a command we don't support. This shouldn't
9032 * happen, commands should be filtered out above us.
9034 ctl_set_invalid_opcode(ctsio);
9035 ctl_done((union ctl_io *)ctsio);
9036 return (CTL_RETVAL_COMPLETE);
9040 * The first check is to make sure we're in bounds, the second
9041 * check is to catch wrap-around problems. If the lba + num blocks
9042 * is less than the lba, then we've wrapped around and the block
9043 * range is invalid anyway.
9045 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
9046 || ((lba + num_blocks) < lba)) {
9047 ctl_set_lba_out_of_range(ctsio,
9048 MAX(lba, lun->be_lun->maxlba + 1));
9049 ctl_done((union ctl_io *)ctsio);
9050 return (CTL_RETVAL_COMPLETE);
9054 * According to SBC-3, a transfer length of 0 is not an error.
9056 if (num_blocks == 0) {
9057 ctl_set_success(ctsio);
9058 ctl_done((union ctl_io *)ctsio);
9059 return (CTL_RETVAL_COMPLETE);
9062 lbalen = (struct ctl_lba_len_flags *)
9063 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9065 lbalen->len = num_blocks;
9067 lbalen->flags = CTL_LLF_COMPARE | flags;
9068 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9070 lbalen->flags = CTL_LLF_VERIFY | flags;
9071 ctsio->kern_total_len = 0;
9073 ctsio->kern_rel_offset = 0;
9075 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9076 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9081 ctl_report_luns(struct ctl_scsiio *ctsio)
9083 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9084 struct ctl_port *port = CTL_PORT(ctsio);
9085 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9086 struct scsi_report_luns *cdb;
9087 struct scsi_report_luns_data *lun_data;
9088 int num_filled, num_luns, num_port_luns, retval;
9089 uint32_t alloc_len, lun_datalen;
9090 uint32_t initidx, targ_lun_id, lun_id;
9092 retval = CTL_RETVAL_COMPLETE;
9093 cdb = (struct scsi_report_luns *)ctsio->cdb;
9095 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9098 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9099 mtx_lock(&softc->ctl_lock);
9100 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9101 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9104 mtx_unlock(&softc->ctl_lock);
9106 switch (cdb->select_report) {
9107 case RPL_REPORT_DEFAULT:
9108 case RPL_REPORT_ALL:
9109 case RPL_REPORT_NONSUBSID:
9111 case RPL_REPORT_WELLKNOWN:
9112 case RPL_REPORT_ADMIN:
9113 case RPL_REPORT_CONGLOM:
9117 ctl_set_invalid_field(ctsio,
9123 ctl_done((union ctl_io *)ctsio);
9125 break; /* NOTREACHED */
9128 alloc_len = scsi_4btoul(cdb->length);
9130 * The initiator has to allocate at least 16 bytes for this request,
9131 * so he can at least get the header and the first LUN. Otherwise
9132 * we reject the request (per SPC-3 rev 14, section 6.21).
9134 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9135 sizeof(struct scsi_report_luns_lundata))) {
9136 ctl_set_invalid_field(ctsio,
9142 ctl_done((union ctl_io *)ctsio);
9146 lun_datalen = sizeof(*lun_data) +
9147 (num_luns * sizeof(struct scsi_report_luns_lundata));
9149 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9150 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9151 ctsio->kern_sg_entries = 0;
9153 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9155 mtx_lock(&softc->ctl_lock);
9156 for (targ_lun_id = 0, num_filled = 0;
9157 targ_lun_id < num_port_luns && num_filled < num_luns;
9159 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9160 if (lun_id == UINT32_MAX)
9162 lun = softc->ctl_luns[lun_id];
9166 be64enc(lun_data->luns[num_filled++].lundata,
9167 ctl_encode_lun(targ_lun_id));
9170 * According to SPC-3, rev 14 section 6.21:
9172 * "The execution of a REPORT LUNS command to any valid and
9173 * installed logical unit shall clear the REPORTED LUNS DATA
9174 * HAS CHANGED unit attention condition for all logical
9175 * units of that target with respect to the requesting
9176 * initiator. A valid and installed logical unit is one
9177 * having a PERIPHERAL QUALIFIER of 000b in the standard
9178 * INQUIRY data (see 6.4.2)."
9180 * If request_lun is NULL, the LUN this report luns command
9181 * was issued to is either disabled or doesn't exist. In that
9182 * case, we shouldn't clear any pending lun change unit
9185 if (request_lun != NULL) {
9186 mtx_lock(&lun->lun_lock);
9187 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9188 mtx_unlock(&lun->lun_lock);
9191 mtx_unlock(&softc->ctl_lock);
9194 * It's quite possible that we've returned fewer LUNs than we allocated
9195 * space for. Trim it.
9197 lun_datalen = sizeof(*lun_data) +
9198 (num_filled * sizeof(struct scsi_report_luns_lundata));
9199 ctsio->kern_rel_offset = 0;
9200 ctsio->kern_sg_entries = 0;
9201 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9202 ctsio->kern_total_len = ctsio->kern_data_len;
9205 * We set this to the actual data length, regardless of how much
9206 * space we actually have to return results. If the user looks at
9207 * this value, he'll know whether or not he allocated enough space
9208 * and reissue the command if necessary. We don't support well
9209 * known logical units, so if the user asks for that, return none.
9211 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9214 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9217 ctl_set_success(ctsio);
9218 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9219 ctsio->be_move_done = ctl_config_move_done;
9220 ctl_datamove((union ctl_io *)ctsio);
9225 ctl_request_sense(struct ctl_scsiio *ctsio)
9227 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9228 struct ctl_lun *lun = CTL_LUN(ctsio);
9229 struct scsi_request_sense *cdb;
9230 struct scsi_sense_data *sense_ptr, *ps;
9233 u_int sense_len = SSD_FULL_SIZE;
9234 scsi_sense_data_type sense_format;
9235 ctl_ua_type ua_type;
9236 uint8_t asc = 0, ascq = 0;
9238 cdb = (struct scsi_request_sense *)ctsio->cdb;
9240 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9243 * Determine which sense format the user wants.
9245 if (cdb->byte2 & SRS_DESC)
9246 sense_format = SSD_TYPE_DESC;
9248 sense_format = SSD_TYPE_FIXED;
9250 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9251 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9252 ctsio->kern_sg_entries = 0;
9253 ctsio->kern_rel_offset = 0;
9256 * struct scsi_sense_data, which is currently set to 256 bytes, is
9257 * larger than the largest allowed value for the length field in the
9258 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9260 ctsio->kern_data_len = cdb->length;
9261 ctsio->kern_total_len = cdb->length;
9264 * If we don't have a LUN, we don't have any pending sense.
9267 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9268 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9269 /* "Logical unit not supported" */
9270 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9271 /*current_error*/ 1,
9272 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9280 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9282 * Check for pending sense, and then for pending unit attentions.
9283 * Pending sense gets returned first, then pending unit attentions.
9285 mtx_lock(&lun->lun_lock);
9286 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9288 ps += initidx % CTL_MAX_INIT_PER_PORT;
9289 if (ps != NULL && ps->error_code != 0) {
9290 scsi_sense_data_type stored_format;
9293 * Check to see which sense format was used for the stored
9296 stored_format = scsi_sense_type(ps);
9299 * If the user requested a different sense format than the
9300 * one we stored, then we need to convert it to the other
9301 * format. If we're going from descriptor to fixed format
9302 * sense data, we may lose things in translation, depending
9303 * on what options were used.
9305 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9306 * for some reason we'll just copy it out as-is.
9308 if ((stored_format == SSD_TYPE_FIXED)
9309 && (sense_format == SSD_TYPE_DESC))
9310 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9311 ps, (struct scsi_sense_data_desc *)sense_ptr);
9312 else if ((stored_format == SSD_TYPE_DESC)
9313 && (sense_format == SSD_TYPE_FIXED))
9314 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9315 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9317 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9322 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9324 if (ua_type != CTL_UA_NONE)
9327 if (have_error == 0) {
9329 * Report informational exception if have one and allowed.
9331 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9333 ascq = lun->ie_ascq;
9335 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9336 /*current_error*/ 1,
9337 /*sense_key*/ SSD_KEY_NO_SENSE,
9342 mtx_unlock(&lun->lun_lock);
9346 * We report the SCSI status as OK, since the status of the command
9347 * itself is OK. We're reporting sense as parameter data.
9349 ctl_set_success(ctsio);
9350 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9351 ctsio->be_move_done = ctl_config_move_done;
9352 ctl_datamove((union ctl_io *)ctsio);
9353 return (CTL_RETVAL_COMPLETE);
9357 ctl_tur(struct ctl_scsiio *ctsio)
9360 CTL_DEBUG_PRINT(("ctl_tur\n"));
9362 ctl_set_success(ctsio);
9363 ctl_done((union ctl_io *)ctsio);
9365 return (CTL_RETVAL_COMPLETE);
9369 * SCSI VPD page 0x00, the Supported VPD Pages page.
9372 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9374 struct ctl_lun *lun = CTL_LUN(ctsio);
9375 struct scsi_vpd_supported_pages *pages;
9379 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9380 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9381 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9382 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9383 ctsio->kern_rel_offset = 0;
9384 ctsio->kern_sg_entries = 0;
9385 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9386 ctsio->kern_total_len = ctsio->kern_data_len;
9389 * The control device is always connected. The disk device, on the
9390 * other hand, may not be online all the time. Need to change this
9391 * to figure out whether the disk device is actually online or not.
9394 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9395 lun->be_lun->lun_type;
9397 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9400 /* Supported VPD pages */
9401 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9403 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9404 /* Device Identification */
9405 pages->page_list[p++] = SVPD_DEVICE_ID;
9406 /* Extended INQUIRY Data */
9407 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9408 /* Mode Page Policy */
9409 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9411 pages->page_list[p++] = SVPD_SCSI_PORTS;
9412 /* Third-party Copy */
9413 pages->page_list[p++] = SVPD_SCSI_TPC;
9414 /* SCSI Feature Sets */
9415 pages->page_list[p++] = SVPD_SCSI_SFS;
9416 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9418 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9419 /* Block Device Characteristics */
9420 pages->page_list[p++] = SVPD_BDC;
9421 /* Logical Block Provisioning */
9422 pages->page_list[p++] = SVPD_LBP;
9426 ctl_set_success(ctsio);
9427 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9428 ctsio->be_move_done = ctl_config_move_done;
9429 ctl_datamove((union ctl_io *)ctsio);
9430 return (CTL_RETVAL_COMPLETE);
9434 * SCSI VPD page 0x80, the Unit Serial Number page.
9437 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9439 struct ctl_lun *lun = CTL_LUN(ctsio);
9440 struct scsi_vpd_unit_serial_number *sn_ptr;
9443 data_len = 4 + CTL_SN_LEN;
9444 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9445 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9446 ctsio->kern_rel_offset = 0;
9447 ctsio->kern_sg_entries = 0;
9448 ctsio->kern_data_len = min(data_len, alloc_len);
9449 ctsio->kern_total_len = ctsio->kern_data_len;
9452 * The control device is always connected. The disk device, on the
9453 * other hand, may not be online all the time. Need to change this
9454 * to figure out whether the disk device is actually online or not.
9457 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9458 lun->be_lun->lun_type;
9460 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9462 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9463 sn_ptr->length = CTL_SN_LEN;
9465 * If we don't have a LUN, we just leave the serial number as
9469 strncpy((char *)sn_ptr->serial_num,
9470 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9472 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9474 ctl_set_success(ctsio);
9475 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9476 ctsio->be_move_done = ctl_config_move_done;
9477 ctl_datamove((union ctl_io *)ctsio);
9478 return (CTL_RETVAL_COMPLETE);
9483 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9486 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9488 struct ctl_lun *lun = CTL_LUN(ctsio);
9489 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9492 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9493 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9494 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9495 ctsio->kern_sg_entries = 0;
9496 ctsio->kern_rel_offset = 0;
9497 ctsio->kern_data_len = min(data_len, alloc_len);
9498 ctsio->kern_total_len = ctsio->kern_data_len;
9501 * The control device is always connected. The disk device, on the
9502 * other hand, may not be online all the time.
9505 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9506 lun->be_lun->lun_type;
9508 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9509 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9510 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9512 * We support head of queue, ordered and simple tags.
9514 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9516 * Volatile cache supported.
9518 eid_ptr->flags3 = SVPD_EID_V_SUP;
9521 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9522 * attention for a particular IT nexus on all LUNs once we report
9523 * it to that nexus once. This bit is required as of SPC-4.
9525 eid_ptr->flags4 = SVPD_EID_LUICLR;
9528 * We support revert to defaults (RTD) bit in MODE SELECT.
9530 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9533 * XXX KDM in order to correctly answer this, we would need
9534 * information from the SIM to determine how much sense data it
9535 * can send. So this would really be a path inquiry field, most
9536 * likely. This can be set to a maximum of 252 according to SPC-4,
9537 * but the hardware may or may not be able to support that much.
9538 * 0 just means that the maximum sense data length is not reported.
9540 eid_ptr->max_sense_length = 0;
9542 ctl_set_success(ctsio);
9543 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9544 ctsio->be_move_done = ctl_config_move_done;
9545 ctl_datamove((union ctl_io *)ctsio);
9546 return (CTL_RETVAL_COMPLETE);
9550 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9552 struct ctl_lun *lun = CTL_LUN(ctsio);
9553 struct scsi_vpd_mode_page_policy *mpp_ptr;
9556 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9557 sizeof(struct scsi_vpd_mode_page_policy_descr);
9559 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9560 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9561 ctsio->kern_rel_offset = 0;
9562 ctsio->kern_sg_entries = 0;
9563 ctsio->kern_data_len = min(data_len, alloc_len);
9564 ctsio->kern_total_len = ctsio->kern_data_len;
9567 * The control device is always connected. The disk device, on the
9568 * other hand, may not be online all the time.
9571 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9572 lun->be_lun->lun_type;
9574 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9575 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9576 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9577 mpp_ptr->descr[0].page_code = 0x3f;
9578 mpp_ptr->descr[0].subpage_code = 0xff;
9579 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9581 ctl_set_success(ctsio);
9582 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9583 ctsio->be_move_done = ctl_config_move_done;
9584 ctl_datamove((union ctl_io *)ctsio);
9585 return (CTL_RETVAL_COMPLETE);
9589 * SCSI VPD page 0x83, the Device Identification page.
9592 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9594 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9595 struct ctl_port *port = CTL_PORT(ctsio);
9596 struct ctl_lun *lun = CTL_LUN(ctsio);
9597 struct scsi_vpd_device_id *devid_ptr;
9598 struct scsi_vpd_id_descriptor *desc;
9602 data_len = sizeof(struct scsi_vpd_device_id) +
9603 sizeof(struct scsi_vpd_id_descriptor) +
9604 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9605 sizeof(struct scsi_vpd_id_descriptor) +
9606 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9607 if (lun && lun->lun_devid)
9608 data_len += lun->lun_devid->len;
9609 if (port && port->port_devid)
9610 data_len += port->port_devid->len;
9611 if (port && port->target_devid)
9612 data_len += port->target_devid->len;
9614 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9615 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9616 ctsio->kern_sg_entries = 0;
9617 ctsio->kern_rel_offset = 0;
9618 ctsio->kern_sg_entries = 0;
9619 ctsio->kern_data_len = min(data_len, alloc_len);
9620 ctsio->kern_total_len = ctsio->kern_data_len;
9623 * The control device is always connected. The disk device, on the
9624 * other hand, may not be online all the time.
9627 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9628 lun->be_lun->lun_type;
9630 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9631 devid_ptr->page_code = SVPD_DEVICE_ID;
9632 scsi_ulto2b(data_len - 4, devid_ptr->length);
9634 if (port && port->port_type == CTL_PORT_FC)
9635 proto = SCSI_PROTO_FC << 4;
9636 else if (port && port->port_type == CTL_PORT_SAS)
9637 proto = SCSI_PROTO_SAS << 4;
9638 else if (port && port->port_type == CTL_PORT_ISCSI)
9639 proto = SCSI_PROTO_ISCSI << 4;
9641 proto = SCSI_PROTO_SPI << 4;
9642 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9645 * We're using a LUN association here. i.e., this device ID is a
9646 * per-LUN identifier.
9648 if (lun && lun->lun_devid) {
9649 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9650 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9651 lun->lun_devid->len);
9655 * This is for the WWPN which is a port association.
9657 if (port && port->port_devid) {
9658 memcpy(desc, port->port_devid->data, port->port_devid->len);
9659 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9660 port->port_devid->len);
9664 * This is for the Relative Target Port(type 4h) identifier
9666 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9667 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9668 SVPD_ID_TYPE_RELTARG;
9670 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9671 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9672 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9675 * This is for the Target Port Group(type 5h) identifier
9677 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9678 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9679 SVPD_ID_TYPE_TPORTGRP;
9681 if (softc->is_single ||
9682 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9685 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9686 scsi_ulto2b(g, &desc->identifier[2]);
9687 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9688 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9691 * This is for the Target identifier
9693 if (port && port->target_devid) {
9694 memcpy(desc, port->target_devid->data, port->target_devid->len);
9697 ctl_set_success(ctsio);
9698 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9699 ctsio->be_move_done = ctl_config_move_done;
9700 ctl_datamove((union ctl_io *)ctsio);
9701 return (CTL_RETVAL_COMPLETE);
9705 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9707 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9708 struct ctl_lun *lun = CTL_LUN(ctsio);
9709 struct scsi_vpd_scsi_ports *sp;
9710 struct scsi_vpd_port_designation *pd;
9711 struct scsi_vpd_port_designation_cont *pdc;
9712 struct ctl_port *port;
9713 int data_len, num_target_ports, iid_len, id_len;
9715 num_target_ports = 0;
9718 mtx_lock(&softc->ctl_lock);
9719 STAILQ_FOREACH(port, &softc->port_list, links) {
9720 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9723 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9726 if (port->init_devid)
9727 iid_len += port->init_devid->len;
9728 if (port->port_devid)
9729 id_len += port->port_devid->len;
9731 mtx_unlock(&softc->ctl_lock);
9733 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9734 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9735 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9736 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9737 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9738 ctsio->kern_sg_entries = 0;
9739 ctsio->kern_rel_offset = 0;
9740 ctsio->kern_sg_entries = 0;
9741 ctsio->kern_data_len = min(data_len, alloc_len);
9742 ctsio->kern_total_len = ctsio->kern_data_len;
9745 * The control device is always connected. The disk device, on the
9746 * other hand, may not be online all the time. Need to change this
9747 * to figure out whether the disk device is actually online or not.
9750 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9751 lun->be_lun->lun_type;
9753 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9755 sp->page_code = SVPD_SCSI_PORTS;
9756 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9758 pd = &sp->design[0];
9760 mtx_lock(&softc->ctl_lock);
9761 STAILQ_FOREACH(port, &softc->port_list, links) {
9762 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9765 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9767 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9768 if (port->init_devid) {
9769 iid_len = port->init_devid->len;
9770 memcpy(pd->initiator_transportid,
9771 port->init_devid->data, port->init_devid->len);
9774 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9775 pdc = (struct scsi_vpd_port_designation_cont *)
9776 (&pd->initiator_transportid[iid_len]);
9777 if (port->port_devid) {
9778 id_len = port->port_devid->len;
9779 memcpy(pdc->target_port_descriptors,
9780 port->port_devid->data, port->port_devid->len);
9783 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9784 pd = (struct scsi_vpd_port_designation *)
9785 ((uint8_t *)pdc->target_port_descriptors + id_len);
9787 mtx_unlock(&softc->ctl_lock);
9789 ctl_set_success(ctsio);
9790 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9791 ctsio->be_move_done = ctl_config_move_done;
9792 ctl_datamove((union ctl_io *)ctsio);
9793 return (CTL_RETVAL_COMPLETE);
9797 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len)
9799 struct ctl_lun *lun = CTL_LUN(ctsio);
9800 struct scsi_vpd_sfs *sfs_ptr;
9801 int sfs_page_size, n;
9803 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2;
9804 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO);
9805 sfs_ptr = (struct scsi_vpd_sfs *)ctsio->kern_data_ptr;
9806 ctsio->kern_sg_entries = 0;
9807 ctsio->kern_rel_offset = 0;
9808 ctsio->kern_sg_entries = 0;
9809 ctsio->kern_data_len = min(sfs_page_size, alloc_len);
9810 ctsio->kern_total_len = ctsio->kern_data_len;
9813 * The control device is always connected. The disk device, on the
9814 * other hand, may not be online all the time. Need to change this
9815 * to figure out whether the disk device is actually online or not.
9818 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9819 lun->be_lun->lun_type;
9821 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9823 sfs_ptr->page_code = SVPD_SCSI_SFS;
9825 /* Discovery 2016 */
9826 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]);
9827 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9829 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]);
9831 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]);
9832 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9833 /* Basic Provisioning 2016 */
9834 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]);
9836 /* Drive Maintenance 2016 */
9837 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]);
9839 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length);
9841 ctl_set_success(ctsio);
9842 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9843 ctsio->be_move_done = ctl_config_move_done;
9844 ctl_datamove((union ctl_io *)ctsio);
9845 return (CTL_RETVAL_COMPLETE);
9849 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9851 struct ctl_lun *lun = CTL_LUN(ctsio);
9852 struct scsi_vpd_block_limits *bl_ptr;
9856 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9857 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9858 ctsio->kern_sg_entries = 0;
9859 ctsio->kern_rel_offset = 0;
9860 ctsio->kern_sg_entries = 0;
9861 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9862 ctsio->kern_total_len = ctsio->kern_data_len;
9865 * The control device is always connected. The disk device, on the
9866 * other hand, may not be online all the time. Need to change this
9867 * to figure out whether the disk device is actually online or not.
9870 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9871 lun->be_lun->lun_type;
9873 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9875 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9876 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9877 bl_ptr->max_cmp_write_len = 0xff;
9878 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9880 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9881 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9883 val = dnvlist_get_string(lun->be_lun->options,
9884 "unmap_max_lba", NULL);
9886 ctl_expand_number(val, &ival);
9887 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9889 val = dnvlist_get_string(lun->be_lun->options,
9890 "unmap_max_descr", NULL);
9892 ctl_expand_number(val, &ival);
9893 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9894 if (lun->be_lun->ublockexp != 0) {
9895 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9896 bl_ptr->opt_unmap_grain);
9897 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9898 bl_ptr->unmap_grain_align);
9901 scsi_ulto4b(lun->be_lun->atomicblock,
9902 bl_ptr->max_atomic_transfer_length);
9903 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9904 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9905 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9906 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9908 val = dnvlist_get_string(lun->be_lun->options,
9909 "write_same_max_lba", NULL);
9911 ctl_expand_number(val, &ival);
9912 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9913 if (lun->be_lun->maxlba + 1 > ival)
9914 bl_ptr->flags |= SVPD_BL_WSNZ;
9917 ctl_set_success(ctsio);
9918 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9919 ctsio->be_move_done = ctl_config_move_done;
9920 ctl_datamove((union ctl_io *)ctsio);
9921 return (CTL_RETVAL_COMPLETE);
9925 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9927 struct ctl_lun *lun = CTL_LUN(ctsio);
9928 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9932 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9933 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9934 ctsio->kern_sg_entries = 0;
9935 ctsio->kern_rel_offset = 0;
9936 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9937 ctsio->kern_total_len = ctsio->kern_data_len;
9940 * The control device is always connected. The disk device, on the
9941 * other hand, may not be online all the time. Need to change this
9942 * to figure out whether the disk device is actually online or not.
9945 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9946 lun->be_lun->lun_type;
9948 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9949 bdc_ptr->page_code = SVPD_BDC;
9950 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9952 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL)
9953 i = strtol(value, NULL, 0);
9955 i = CTL_DEFAULT_ROTATION_RATE;
9956 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9958 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL)
9959 i = strtol(value, NULL, 0);
9962 bdc_ptr->wab_wac_ff = (i & 0x0f);
9963 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS;
9965 ctl_set_success(ctsio);
9966 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9967 ctsio->be_move_done = ctl_config_move_done;
9968 ctl_datamove((union ctl_io *)ctsio);
9969 return (CTL_RETVAL_COMPLETE);
9973 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9975 struct ctl_lun *lun = CTL_LUN(ctsio);
9976 struct scsi_vpd_logical_block_prov *lbp_ptr;
9979 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9980 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9981 ctsio->kern_sg_entries = 0;
9982 ctsio->kern_rel_offset = 0;
9983 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9984 ctsio->kern_total_len = ctsio->kern_data_len;
9987 * The control device is always connected. The disk device, on the
9988 * other hand, may not be online all the time. Need to change this
9989 * to figure out whether the disk device is actually online or not.
9992 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9993 lun->be_lun->lun_type;
9995 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9997 lbp_ptr->page_code = SVPD_LBP;
9998 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9999 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
10000 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
10001 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
10002 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
10003 value = dnvlist_get_string(lun->be_lun->options,
10004 "provisioning_type", NULL);
10005 if (value != NULL) {
10006 if (strcmp(value, "resource") == 0)
10007 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
10008 else if (strcmp(value, "thin") == 0)
10009 lbp_ptr->prov_type = SVPD_LBP_THIN;
10011 lbp_ptr->prov_type = SVPD_LBP_THIN;
10014 ctl_set_success(ctsio);
10015 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10016 ctsio->be_move_done = ctl_config_move_done;
10017 ctl_datamove((union ctl_io *)ctsio);
10018 return (CTL_RETVAL_COMPLETE);
10022 * INQUIRY with the EVPD bit set.
10025 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
10027 struct ctl_lun *lun = CTL_LUN(ctsio);
10028 struct scsi_inquiry *cdb;
10029 int alloc_len, retval;
10031 cdb = (struct scsi_inquiry *)ctsio->cdb;
10032 alloc_len = scsi_2btoul(cdb->length);
10034 switch (cdb->page_code) {
10035 case SVPD_SUPPORTED_PAGES:
10036 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
10038 case SVPD_UNIT_SERIAL_NUMBER:
10039 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
10041 case SVPD_DEVICE_ID:
10042 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
10044 case SVPD_EXTENDED_INQUIRY_DATA:
10045 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
10047 case SVPD_MODE_PAGE_POLICY:
10048 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
10050 case SVPD_SCSI_PORTS:
10051 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10053 case SVPD_SCSI_TPC:
10054 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10056 case SVPD_SCSI_SFS:
10057 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len);
10059 case SVPD_BLOCK_LIMITS:
10060 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10062 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10065 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10067 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10070 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10072 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10076 ctl_set_invalid_field(ctsio,
10082 ctl_done((union ctl_io *)ctsio);
10083 retval = CTL_RETVAL_COMPLETE;
10091 * Standard INQUIRY data.
10094 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10096 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10097 struct ctl_port *port = CTL_PORT(ctsio);
10098 struct ctl_lun *lun = CTL_LUN(ctsio);
10099 struct scsi_inquiry_data *inq_ptr;
10100 struct scsi_inquiry *cdb;
10102 uint32_t alloc_len, data_len;
10103 ctl_port_type port_type;
10105 port_type = port->port_type;
10106 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10107 port_type = CTL_PORT_SCSI;
10109 cdb = (struct scsi_inquiry *)ctsio->cdb;
10110 alloc_len = scsi_2btoul(cdb->length);
10113 * We malloc the full inquiry data size here and fill it
10114 * in. If the user only asks for less, we'll give him
10117 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10118 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10119 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10120 ctsio->kern_sg_entries = 0;
10121 ctsio->kern_rel_offset = 0;
10122 ctsio->kern_data_len = min(data_len, alloc_len);
10123 ctsio->kern_total_len = ctsio->kern_data_len;
10126 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10127 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10128 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10129 lun->be_lun->lun_type;
10131 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10132 lun->be_lun->lun_type;
10134 if (lun->flags & CTL_LUN_REMOVABLE)
10135 inq_ptr->dev_qual2 |= SID_RMB;
10137 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10139 /* RMB in byte 2 is 0 */
10140 inq_ptr->version = SCSI_REV_SPC5;
10143 * According to SAM-3, even if a device only supports a single
10144 * level of LUN addressing, it should still set the HISUP bit:
10146 * 4.9.1 Logical unit numbers overview
10148 * All logical unit number formats described in this standard are
10149 * hierarchical in structure even when only a single level in that
10150 * hierarchy is used. The HISUP bit shall be set to one in the
10151 * standard INQUIRY data (see SPC-2) when any logical unit number
10152 * format described in this standard is used. Non-hierarchical
10153 * formats are outside the scope of this standard.
10155 * Therefore we set the HiSup bit here.
10157 * The response format is 2, per SPC-3.
10159 inq_ptr->response_format = SID_HiSup | 2;
10161 inq_ptr->additional_length = data_len -
10162 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10163 CTL_DEBUG_PRINT(("additional_length = %d\n",
10164 inq_ptr->additional_length));
10166 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10167 if (port_type == CTL_PORT_SCSI)
10168 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10169 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10170 inq_ptr->flags = SID_CmdQue;
10171 if (port_type == CTL_PORT_SCSI)
10172 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10175 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10176 * We have 8 bytes for the vendor name, and 16 bytes for the device
10177 * name and 4 bytes for the revision.
10179 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10180 "vendor", NULL)) == NULL) {
10181 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10183 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10184 strncpy(inq_ptr->vendor, val,
10185 min(sizeof(inq_ptr->vendor), strlen(val)));
10188 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10189 sizeof(inq_ptr->product));
10190 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product",
10192 switch (lun->be_lun->lun_type) {
10194 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10195 sizeof(inq_ptr->product));
10198 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10199 sizeof(inq_ptr->product));
10202 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10203 sizeof(inq_ptr->product));
10206 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10207 sizeof(inq_ptr->product));
10211 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10212 strncpy(inq_ptr->product, val,
10213 min(sizeof(inq_ptr->product), strlen(val)));
10217 * XXX make this a macro somewhere so it automatically gets
10218 * incremented when we make changes.
10220 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10221 "revision", NULL)) == NULL) {
10222 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10224 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10225 strncpy(inq_ptr->revision, val,
10226 min(sizeof(inq_ptr->revision), strlen(val)));
10230 * For parallel SCSI, we support double transition and single
10231 * transition clocking. We also support QAS (Quick Arbitration
10232 * and Selection) and Information Unit transfers on both the
10233 * control and array devices.
10235 if (port_type == CTL_PORT_SCSI)
10236 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10239 /* SAM-6 (no version claimed) */
10240 scsi_ulto2b(0x00C0, inq_ptr->version1);
10241 /* SPC-5 (no version claimed) */
10242 scsi_ulto2b(0x05C0, inq_ptr->version2);
10243 if (port_type == CTL_PORT_FC) {
10244 /* FCP-2 ANSI INCITS.350:2003 */
10245 scsi_ulto2b(0x0917, inq_ptr->version3);
10246 } else if (port_type == CTL_PORT_SCSI) {
10247 /* SPI-4 ANSI INCITS.362:200x */
10248 scsi_ulto2b(0x0B56, inq_ptr->version3);
10249 } else if (port_type == CTL_PORT_ISCSI) {
10250 /* iSCSI (no version claimed) */
10251 scsi_ulto2b(0x0960, inq_ptr->version3);
10252 } else if (port_type == CTL_PORT_SAS) {
10253 /* SAS (no version claimed) */
10254 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10255 } else if (port_type == CTL_PORT_UMASS) {
10256 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10257 scsi_ulto2b(0x1730, inq_ptr->version3);
10261 /* SBC-4 (no version claimed) */
10262 scsi_ulto2b(0x0600, inq_ptr->version4);
10264 switch (lun->be_lun->lun_type) {
10266 /* SBC-4 (no version claimed) */
10267 scsi_ulto2b(0x0600, inq_ptr->version4);
10272 /* MMC-6 (no version claimed) */
10273 scsi_ulto2b(0x04E0, inq_ptr->version4);
10280 ctl_set_success(ctsio);
10281 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10282 ctsio->be_move_done = ctl_config_move_done;
10283 ctl_datamove((union ctl_io *)ctsio);
10284 return (CTL_RETVAL_COMPLETE);
10288 ctl_inquiry(struct ctl_scsiio *ctsio)
10290 struct scsi_inquiry *cdb;
10293 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10295 cdb = (struct scsi_inquiry *)ctsio->cdb;
10296 if (cdb->byte2 & SI_EVPD)
10297 retval = ctl_inquiry_evpd(ctsio);
10298 else if (cdb->page_code == 0)
10299 retval = ctl_inquiry_std(ctsio);
10301 ctl_set_invalid_field(ctsio,
10307 ctl_done((union ctl_io *)ctsio);
10308 return (CTL_RETVAL_COMPLETE);
10315 ctl_get_config(struct ctl_scsiio *ctsio)
10317 struct ctl_lun *lun = CTL_LUN(ctsio);
10318 struct scsi_get_config_header *hdr;
10319 struct scsi_get_config_feature *feature;
10320 struct scsi_get_config *cdb;
10321 uint32_t alloc_len, data_len;
10324 cdb = (struct scsi_get_config *)ctsio->cdb;
10325 rt = (cdb->rt & SGC_RT_MASK);
10326 starting = scsi_2btoul(cdb->starting_feature);
10327 alloc_len = scsi_2btoul(cdb->length);
10329 data_len = sizeof(struct scsi_get_config_header) +
10330 sizeof(struct scsi_get_config_feature) + 8 +
10331 sizeof(struct scsi_get_config_feature) + 8 +
10332 sizeof(struct scsi_get_config_feature) + 4 +
10333 sizeof(struct scsi_get_config_feature) + 4 +
10334 sizeof(struct scsi_get_config_feature) + 8 +
10335 sizeof(struct scsi_get_config_feature) +
10336 sizeof(struct scsi_get_config_feature) + 4 +
10337 sizeof(struct scsi_get_config_feature) + 4 +
10338 sizeof(struct scsi_get_config_feature) + 4 +
10339 sizeof(struct scsi_get_config_feature) + 4 +
10340 sizeof(struct scsi_get_config_feature) + 4 +
10341 sizeof(struct scsi_get_config_feature) + 4;
10342 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10343 ctsio->kern_sg_entries = 0;
10344 ctsio->kern_rel_offset = 0;
10346 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10347 if (lun->flags & CTL_LUN_NO_MEDIA)
10348 scsi_ulto2b(0x0000, hdr->current_profile);
10350 scsi_ulto2b(0x0010, hdr->current_profile);
10351 feature = (struct scsi_get_config_feature *)(hdr + 1);
10353 if (starting > 0x003b)
10355 if (starting > 0x003a)
10357 if (starting > 0x002b)
10359 if (starting > 0x002a)
10361 if (starting > 0x001f)
10363 if (starting > 0x001e)
10365 if (starting > 0x001d)
10367 if (starting > 0x0010)
10369 if (starting > 0x0003)
10371 if (starting > 0x0002)
10373 if (starting > 0x0001)
10375 if (starting > 0x0000)
10379 scsi_ulto2b(0x0000, feature->feature_code);
10380 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10381 feature->add_length = 8;
10382 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10383 feature->feature_data[2] = 0x00;
10384 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10385 feature->feature_data[6] = 0x01;
10386 feature = (struct scsi_get_config_feature *)
10387 &feature->feature_data[feature->add_length];
10390 scsi_ulto2b(0x0001, feature->feature_code);
10391 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10392 feature->add_length = 8;
10393 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10394 feature->feature_data[4] = 0x03;
10395 feature = (struct scsi_get_config_feature *)
10396 &feature->feature_data[feature->add_length];
10399 scsi_ulto2b(0x0002, feature->feature_code);
10400 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10401 feature->add_length = 4;
10402 feature->feature_data[0] = 0x02;
10403 feature = (struct scsi_get_config_feature *)
10404 &feature->feature_data[feature->add_length];
10406 f3: /* Removable Medium */
10407 scsi_ulto2b(0x0003, feature->feature_code);
10408 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10409 feature->add_length = 4;
10410 feature->feature_data[0] = 0x39;
10411 feature = (struct scsi_get_config_feature *)
10412 &feature->feature_data[feature->add_length];
10414 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10417 f10: /* Random Read */
10418 scsi_ulto2b(0x0010, feature->feature_code);
10419 feature->flags = 0x00;
10420 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10421 feature->flags |= SGC_F_CURRENT;
10422 feature->add_length = 8;
10423 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10424 scsi_ulto2b(1, &feature->feature_data[4]);
10425 feature->feature_data[6] = 0x00;
10426 feature = (struct scsi_get_config_feature *)
10427 &feature->feature_data[feature->add_length];
10429 f1d: /* Multi-Read */
10430 scsi_ulto2b(0x001D, feature->feature_code);
10431 feature->flags = 0x00;
10432 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10433 feature->flags |= SGC_F_CURRENT;
10434 feature->add_length = 0;
10435 feature = (struct scsi_get_config_feature *)
10436 &feature->feature_data[feature->add_length];
10439 scsi_ulto2b(0x001E, feature->feature_code);
10440 feature->flags = 0x00;
10441 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10442 feature->flags |= SGC_F_CURRENT;
10443 feature->add_length = 4;
10444 feature->feature_data[0] = 0x00;
10445 feature = (struct scsi_get_config_feature *)
10446 &feature->feature_data[feature->add_length];
10448 f1f: /* DVD Read */
10449 scsi_ulto2b(0x001F, feature->feature_code);
10450 feature->flags = 0x08;
10451 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10452 feature->flags |= SGC_F_CURRENT;
10453 feature->add_length = 4;
10454 feature->feature_data[0] = 0x01;
10455 feature->feature_data[2] = 0x03;
10456 feature = (struct scsi_get_config_feature *)
10457 &feature->feature_data[feature->add_length];
10460 scsi_ulto2b(0x002A, feature->feature_code);
10461 feature->flags = 0x04;
10462 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10463 feature->flags |= SGC_F_CURRENT;
10464 feature->add_length = 4;
10465 feature->feature_data[0] = 0x00;
10466 feature->feature_data[1] = 0x00;
10467 feature = (struct scsi_get_config_feature *)
10468 &feature->feature_data[feature->add_length];
10471 scsi_ulto2b(0x002B, feature->feature_code);
10472 feature->flags = 0x00;
10473 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10474 feature->flags |= SGC_F_CURRENT;
10475 feature->add_length = 4;
10476 feature->feature_data[0] = 0x00;
10477 feature = (struct scsi_get_config_feature *)
10478 &feature->feature_data[feature->add_length];
10480 f3a: /* DVD+RW Dual Layer */
10481 scsi_ulto2b(0x003A, feature->feature_code);
10482 feature->flags = 0x00;
10483 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10484 feature->flags |= SGC_F_CURRENT;
10485 feature->add_length = 4;
10486 feature->feature_data[0] = 0x00;
10487 feature->feature_data[1] = 0x00;
10488 feature = (struct scsi_get_config_feature *)
10489 &feature->feature_data[feature->add_length];
10491 f3b: /* DVD+R Dual Layer */
10492 scsi_ulto2b(0x003B, feature->feature_code);
10493 feature->flags = 0x00;
10494 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10495 feature->flags |= SGC_F_CURRENT;
10496 feature->add_length = 4;
10497 feature->feature_data[0] = 0x00;
10498 feature = (struct scsi_get_config_feature *)
10499 &feature->feature_data[feature->add_length];
10502 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10503 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10504 feature = (struct scsi_get_config_feature *)(hdr + 1);
10505 if (scsi_2btoul(feature->feature_code) == starting)
10506 feature = (struct scsi_get_config_feature *)
10507 &feature->feature_data[feature->add_length];
10508 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10510 scsi_ulto4b(data_len - 4, hdr->data_length);
10511 ctsio->kern_data_len = min(data_len, alloc_len);
10512 ctsio->kern_total_len = ctsio->kern_data_len;
10514 ctl_set_success(ctsio);
10515 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10516 ctsio->be_move_done = ctl_config_move_done;
10517 ctl_datamove((union ctl_io *)ctsio);
10518 return (CTL_RETVAL_COMPLETE);
10522 ctl_get_event_status(struct ctl_scsiio *ctsio)
10524 struct scsi_get_event_status_header *hdr;
10525 struct scsi_get_event_status *cdb;
10526 uint32_t alloc_len, data_len;
10528 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10529 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10530 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10531 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10532 ctl_done((union ctl_io *)ctsio);
10533 return (CTL_RETVAL_COMPLETE);
10535 alloc_len = scsi_2btoul(cdb->length);
10537 data_len = sizeof(struct scsi_get_event_status_header);
10538 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10539 ctsio->kern_sg_entries = 0;
10540 ctsio->kern_rel_offset = 0;
10541 ctsio->kern_data_len = min(data_len, alloc_len);
10542 ctsio->kern_total_len = ctsio->kern_data_len;
10544 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10545 scsi_ulto2b(0, hdr->descr_length);
10546 hdr->nea_class = SGESN_NEA;
10547 hdr->supported_class = 0;
10549 ctl_set_success(ctsio);
10550 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10551 ctsio->be_move_done = ctl_config_move_done;
10552 ctl_datamove((union ctl_io *)ctsio);
10553 return (CTL_RETVAL_COMPLETE);
10557 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10559 struct scsi_mechanism_status_header *hdr;
10560 struct scsi_mechanism_status *cdb;
10561 uint32_t alloc_len, data_len;
10563 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10564 alloc_len = scsi_2btoul(cdb->length);
10566 data_len = sizeof(struct scsi_mechanism_status_header);
10567 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10568 ctsio->kern_sg_entries = 0;
10569 ctsio->kern_rel_offset = 0;
10570 ctsio->kern_data_len = min(data_len, alloc_len);
10571 ctsio->kern_total_len = ctsio->kern_data_len;
10573 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10574 hdr->state1 = 0x00;
10575 hdr->state2 = 0xe0;
10576 scsi_ulto3b(0, hdr->lba);
10577 hdr->slots_num = 0;
10578 scsi_ulto2b(0, hdr->slots_length);
10580 ctl_set_success(ctsio);
10581 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10582 ctsio->be_move_done = ctl_config_move_done;
10583 ctl_datamove((union ctl_io *)ctsio);
10584 return (CTL_RETVAL_COMPLETE);
10588 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10593 buf[1] = bin2bcd((lba / 75) / 60);
10594 buf[2] = bin2bcd((lba / 75) % 60);
10595 buf[3] = bin2bcd(lba % 75);
10599 ctl_read_toc(struct ctl_scsiio *ctsio)
10601 struct ctl_lun *lun = CTL_LUN(ctsio);
10602 struct scsi_read_toc_hdr *hdr;
10603 struct scsi_read_toc_type01_descr *descr;
10604 struct scsi_read_toc *cdb;
10605 uint32_t alloc_len, data_len;
10608 cdb = (struct scsi_read_toc *)ctsio->cdb;
10609 msf = (cdb->byte2 & CD_MSF) != 0;
10610 format = cdb->format;
10611 alloc_len = scsi_2btoul(cdb->data_len);
10613 data_len = sizeof(struct scsi_read_toc_hdr);
10615 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10617 data_len += sizeof(struct scsi_read_toc_type01_descr);
10618 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10619 ctsio->kern_sg_entries = 0;
10620 ctsio->kern_rel_offset = 0;
10621 ctsio->kern_data_len = min(data_len, alloc_len);
10622 ctsio->kern_total_len = ctsio->kern_data_len;
10624 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10626 scsi_ulto2b(0x12, hdr->data_length);
10629 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10630 descr->addr_ctl = 0x14;
10631 descr->track_number = 1;
10633 ctl_ultomsf(0, descr->track_start);
10635 scsi_ulto4b(0, descr->track_start);
10637 descr->addr_ctl = 0x14;
10638 descr->track_number = 0xaa;
10640 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10642 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10644 scsi_ulto2b(0x0a, hdr->data_length);
10647 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10648 descr->addr_ctl = 0x14;
10649 descr->track_number = 1;
10651 ctl_ultomsf(0, descr->track_start);
10653 scsi_ulto4b(0, descr->track_start);
10656 ctl_set_success(ctsio);
10657 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10658 ctsio->be_move_done = ctl_config_move_done;
10659 ctl_datamove((union ctl_io *)ctsio);
10660 return (CTL_RETVAL_COMPLETE);
10664 * For known CDB types, parse the LBA and length.
10667 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10669 if (io->io_hdr.io_type != CTL_IO_SCSI)
10672 switch (io->scsiio.cdb[0]) {
10673 case COMPARE_AND_WRITE: {
10674 struct scsi_compare_and_write *cdb;
10676 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10678 *lba = scsi_8btou64(cdb->addr);
10679 *len = cdb->length;
10684 struct scsi_rw_6 *cdb;
10686 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10688 *lba = scsi_3btoul(cdb->addr);
10689 /* only 5 bits are valid in the most significant address byte */
10691 *len = cdb->length;
10696 struct scsi_rw_10 *cdb;
10698 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10700 *lba = scsi_4btoul(cdb->addr);
10701 *len = scsi_2btoul(cdb->length);
10704 case WRITE_VERIFY_10: {
10705 struct scsi_write_verify_10 *cdb;
10707 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10709 *lba = scsi_4btoul(cdb->addr);
10710 *len = scsi_2btoul(cdb->length);
10715 struct scsi_rw_12 *cdb;
10717 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10719 *lba = scsi_4btoul(cdb->addr);
10720 *len = scsi_4btoul(cdb->length);
10723 case WRITE_VERIFY_12: {
10724 struct scsi_write_verify_12 *cdb;
10726 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10728 *lba = scsi_4btoul(cdb->addr);
10729 *len = scsi_4btoul(cdb->length);
10734 struct scsi_rw_16 *cdb;
10736 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10738 *lba = scsi_8btou64(cdb->addr);
10739 *len = scsi_4btoul(cdb->length);
10742 case WRITE_ATOMIC_16: {
10743 struct scsi_write_atomic_16 *cdb;
10745 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10747 *lba = scsi_8btou64(cdb->addr);
10748 *len = scsi_2btoul(cdb->length);
10751 case WRITE_VERIFY_16: {
10752 struct scsi_write_verify_16 *cdb;
10754 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10756 *lba = scsi_8btou64(cdb->addr);
10757 *len = scsi_4btoul(cdb->length);
10760 case WRITE_SAME_10: {
10761 struct scsi_write_same_10 *cdb;
10763 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10765 *lba = scsi_4btoul(cdb->addr);
10766 *len = scsi_2btoul(cdb->length);
10769 case WRITE_SAME_16: {
10770 struct scsi_write_same_16 *cdb;
10772 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10774 *lba = scsi_8btou64(cdb->addr);
10775 *len = scsi_4btoul(cdb->length);
10779 struct scsi_verify_10 *cdb;
10781 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10783 *lba = scsi_4btoul(cdb->addr);
10784 *len = scsi_2btoul(cdb->length);
10788 struct scsi_verify_12 *cdb;
10790 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10792 *lba = scsi_4btoul(cdb->addr);
10793 *len = scsi_4btoul(cdb->length);
10797 struct scsi_verify_16 *cdb;
10799 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10801 *lba = scsi_8btou64(cdb->addr);
10802 *len = scsi_4btoul(cdb->length);
10810 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10811 struct scsi_get_lba_status *cdb;
10813 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10814 *lba = scsi_8btou64(cdb->addr);
10820 break; /* NOTREACHED */
10827 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10830 uint64_t endlba1, endlba2;
10832 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10833 endlba2 = lba2 + len2 - 1;
10835 if ((endlba1 < lba2) || (endlba2 < lba1))
10836 return (CTL_ACTION_PASS);
10838 return (CTL_ACTION_BLOCK);
10842 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10844 struct ctl_ptr_len_flags *ptrlen;
10845 struct scsi_unmap_desc *buf, *end, *range;
10849 /* If not UNMAP -- go other way. */
10850 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10851 io->scsiio.cdb[0] != UNMAP)
10852 return (CTL_ACTION_ERROR);
10854 /* If UNMAP without data -- block and wait for data. */
10855 ptrlen = (struct ctl_ptr_len_flags *)
10856 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10857 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10858 ptrlen->ptr == NULL)
10859 return (CTL_ACTION_BLOCK);
10861 /* UNMAP with data -- check for collision. */
10862 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10863 end = buf + ptrlen->len / sizeof(*buf);
10864 for (range = buf; range < end; range++) {
10865 lba = scsi_8btou64(range->lba);
10866 len = scsi_4btoul(range->length);
10867 if ((lba < lba2 + len2) && (lba + len > lba2))
10868 return (CTL_ACTION_BLOCK);
10870 return (CTL_ACTION_PASS);
10874 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10876 uint64_t lba1, lba2;
10877 uint64_t len1, len2;
10880 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10881 return (CTL_ACTION_ERROR);
10883 retval = ctl_extent_check_unmap(io1, lba2, len2);
10884 if (retval != CTL_ACTION_ERROR)
10887 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10888 return (CTL_ACTION_ERROR);
10890 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10892 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10896 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10898 uint64_t lba1, lba2;
10899 uint64_t len1, len2;
10901 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10902 return (CTL_ACTION_PASS);
10903 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10904 return (CTL_ACTION_ERROR);
10905 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10906 return (CTL_ACTION_ERROR);
10908 if (lba1 + len1 == lba2)
10909 return (CTL_ACTION_BLOCK);
10910 return (CTL_ACTION_PASS);
10914 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10915 union ctl_io *ooa_io)
10917 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10918 const ctl_serialize_action *serialize_row;
10921 * Aborted commands are not going to be executed and may even
10922 * not report completion, so we don't care about their order.
10923 * Let them complete ASAP to clean the OOA queue.
10925 if (pending_io->io_hdr.flags & CTL_FLAG_ABORT)
10926 return (CTL_ACTION_SKIP);
10929 * The initiator attempted multiple untagged commands at the same
10930 * time. Can't do that.
10932 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10933 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10934 && ((pending_io->io_hdr.nexus.targ_port ==
10935 ooa_io->io_hdr.nexus.targ_port)
10936 && (pending_io->io_hdr.nexus.initid ==
10937 ooa_io->io_hdr.nexus.initid))
10938 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10939 CTL_FLAG_STATUS_SENT)) == 0))
10940 return (CTL_ACTION_OVERLAP);
10943 * The initiator attempted to send multiple tagged commands with
10944 * the same ID. (It's fine if different initiators have the same
10947 * Even if all of those conditions are true, we don't kill the I/O
10948 * if the command ahead of us has been aborted. We won't end up
10949 * sending it to the FETD, and it's perfectly legal to resend a
10950 * command with the same tag number as long as the previous
10951 * instance of this tag number has been aborted somehow.
10953 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10954 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10955 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10956 && ((pending_io->io_hdr.nexus.targ_port ==
10957 ooa_io->io_hdr.nexus.targ_port)
10958 && (pending_io->io_hdr.nexus.initid ==
10959 ooa_io->io_hdr.nexus.initid))
10960 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10961 CTL_FLAG_STATUS_SENT)) == 0))
10962 return (CTL_ACTION_OVERLAP_TAG);
10965 * If we get a head of queue tag, SAM-3 says that we should
10966 * immediately execute it.
10968 * What happens if this command would normally block for some other
10969 * reason? e.g. a request sense with a head of queue tag
10970 * immediately after a write. Normally that would block, but this
10971 * will result in its getting executed immediately...
10973 * We currently return "pass" instead of "skip", so we'll end up
10974 * going through the rest of the queue to check for overlapped tags.
10976 * XXX KDM check for other types of blockage first??
10978 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10979 return (CTL_ACTION_PASS);
10982 * Ordered tags have to block until all items ahead of them
10983 * have completed. If we get called with an ordered tag, we always
10984 * block, if something else is ahead of us in the queue.
10986 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10987 return (CTL_ACTION_BLOCK);
10990 * Simple tags get blocked until all head of queue and ordered tags
10991 * ahead of them have completed. I'm lumping untagged commands in
10992 * with simple tags here. XXX KDM is that the right thing to do?
10994 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10995 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10996 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10997 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10998 return (CTL_ACTION_BLOCK);
11000 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
11001 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
11002 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
11003 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
11004 pending_io->scsiio.cdb[1], pending_io));
11005 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
11006 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
11007 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
11008 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
11009 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
11010 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
11011 ooa_io->scsiio.cdb[1], ooa_io));
11013 serialize_row = ctl_serialize_table[ooa_entry->seridx];
11015 switch (serialize_row[pending_entry->seridx]) {
11016 case CTL_SER_BLOCK:
11017 return (CTL_ACTION_BLOCK);
11018 case CTL_SER_EXTENT:
11019 return (ctl_extent_check(ooa_io, pending_io,
11020 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11021 case CTL_SER_EXTENTOPT:
11022 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11023 SCP_QUEUE_ALG_UNRESTRICTED)
11024 return (ctl_extent_check(ooa_io, pending_io,
11026 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
11027 return (CTL_ACTION_PASS);
11028 case CTL_SER_EXTENTSEQ:
11029 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
11030 return (ctl_extent_check_seq(ooa_io, pending_io));
11031 return (CTL_ACTION_PASS);
11033 return (CTL_ACTION_PASS);
11034 case CTL_SER_BLOCKOPT:
11035 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
11036 SCP_QUEUE_ALG_UNRESTRICTED)
11037 return (CTL_ACTION_BLOCK);
11038 return (CTL_ACTION_PASS);
11040 return (CTL_ACTION_SKIP);
11042 panic("%s: Invalid serialization value %d for %d => %d",
11043 __func__, serialize_row[pending_entry->seridx],
11044 pending_entry->seridx, ooa_entry->seridx);
11047 return (CTL_ACTION_ERROR);
11051 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11053 * - pending_io is generally either incoming, or on the blocked queue
11054 * - starting I/O is the I/O we want to start the check with.
11057 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11058 union ctl_io **starting_io)
11060 union ctl_io *ooa_io;
11063 mtx_assert(&lun->lun_lock, MA_OWNED);
11066 * Run back along the OOA queue, starting with the current
11067 * blocked I/O and going through every I/O before it on the
11068 * queue. If starting_io is NULL, we'll just end up returning
11071 for (ooa_io = *starting_io; ooa_io != NULL;
11072 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11074 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11075 if (action != CTL_ACTION_PASS) {
11076 *starting_io = ooa_io;
11081 *starting_io = NULL;
11082 return (CTL_ACTION_PASS);
11086 * Try to unblock the specified I/O.
11088 * skip parameter allows explicitly skip present blocker of the I/O,
11089 * starting from the previous one on OOA queue. It can be used when
11090 * we know for sure that the blocker I/O does no longer count.
11093 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip)
11095 struct ctl_softc *softc = lun->ctl_softc;
11096 union ctl_io *bio, *obio;
11097 const struct ctl_cmd_entry *entry;
11098 union ctl_ha_msg msg_info;
11101 mtx_assert(&lun->lun_lock, MA_OWNED);
11103 if (io->io_hdr.blocker == NULL)
11106 obio = bio = io->io_hdr.blocker;
11108 bio = (union ctl_io *)TAILQ_PREV(&bio->io_hdr, ctl_ooaq,
11110 action = ctl_check_ooa(lun, io, &bio);
11111 if (action == CTL_ACTION_BLOCK) {
11112 /* Still blocked, but may be by different I/O now. */
11114 TAILQ_REMOVE(&obio->io_hdr.blocked_queue,
11115 &io->io_hdr, blocked_links);
11116 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue,
11117 &io->io_hdr, blocked_links);
11118 io->io_hdr.blocker = bio;
11123 /* No longer blocked, one way or another. */
11124 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links);
11125 io->io_hdr.blocker = NULL;
11128 case CTL_ACTION_OVERLAP:
11129 ctl_set_overlapped_cmd(&io->scsiio);
11131 case CTL_ACTION_OVERLAP_TAG:
11132 ctl_set_overlapped_tag(&io->scsiio,
11133 io->scsiio.tag_num & 0xff);
11135 case CTL_ACTION_PASS:
11136 case CTL_ACTION_SKIP:
11138 /* Serializing commands from the other SC retire there. */
11139 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11140 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11141 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11142 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11143 msg_info.hdr.serializing_sc = io;
11144 msg_info.hdr.msg_type = CTL_MSG_R2R;
11145 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11146 sizeof(msg_info.hdr), M_NOWAIT);
11151 * Check this I/O for LUN state changes that may have happened
11152 * while this command was blocked. The LUN state may have been
11153 * changed by a command ahead of us in the queue.
11155 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11156 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
11161 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11162 ctl_enqueue_rtr(io);
11164 case CTL_ACTION_ERROR:
11166 ctl_set_internal_failure(&io->scsiio,
11168 /*retry_count*/ 0);
11171 /* Serializing commands from the other SC are done here. */
11172 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11173 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11174 ctl_try_unblock_others(lun, io, TRUE);
11175 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
11177 ctl_copy_sense_data_back(io, &msg_info);
11178 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11179 msg_info.hdr.serializing_sc = NULL;
11180 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
11181 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11182 sizeof(msg_info.scsi), M_WAITOK);
11193 * Try to unblock I/Os blocked by the specified I/O.
11195 * skip parameter allows explicitly skip the specified I/O as blocker,
11196 * starting from the previous one on the OOA queue. It can be used when
11197 * we know for sure that the specified I/O does no longer count (done).
11198 * It has to be still on OOA queue though so that we know where to start.
11201 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip)
11203 union ctl_io *io, *next_io;
11205 mtx_assert(&lun->lun_lock, MA_OWNED);
11207 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue);
11208 io != NULL; io = next_io) {
11209 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links);
11211 KASSERT(io->io_hdr.blocker != NULL,
11212 ("I/O %p on blocked list without blocker", io));
11213 ctl_try_unblock_io(lun, io, skip);
11215 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue),
11216 ("blocked_queue is not empty after skipping %p", bio));
11220 * This routine (with one exception) checks LUN flags that can be set by
11221 * commands ahead of us in the OOA queue. These flags have to be checked
11222 * when a command initially comes in, and when we pull a command off the
11223 * blocked queue and are preparing to execute it. The reason we have to
11224 * check these flags for commands on the blocked queue is that the LUN
11225 * state may have been changed by a command ahead of us while we're on the
11228 * Ordering is somewhat important with these checks, so please pay
11229 * careful attention to the placement of any new checks.
11232 ctl_scsiio_lun_check(struct ctl_lun *lun,
11233 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11235 struct ctl_softc *softc = lun->ctl_softc;
11241 mtx_assert(&lun->lun_lock, MA_OWNED);
11244 * If this shelf is a secondary shelf controller, we may have to
11245 * reject some commands disallowed by HA mode and link state.
11247 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11248 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11249 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11250 ctl_set_lun_unavail(ctsio);
11254 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11255 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11256 ctl_set_lun_transit(ctsio);
11260 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11261 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11262 ctl_set_lun_standby(ctsio);
11267 /* The rest of checks are only done on executing side */
11268 if (softc->ha_mode == CTL_HA_MODE_XFER)
11272 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11274 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11275 ctl_set_hw_write_protected(ctsio);
11279 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11280 ctl_set_sense(ctsio, /*current_error*/ 1,
11281 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11282 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11289 * Check for a reservation conflict. If this command isn't allowed
11290 * even on reserved LUNs, and if this initiator isn't the one who
11291 * reserved us, reject the command with a reservation conflict.
11293 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11294 if ((lun->flags & CTL_LUN_RESERVED)
11295 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11296 if (lun->res_idx != residx) {
11297 ctl_set_reservation_conflict(ctsio);
11303 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11304 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11305 /* No reservation or command is allowed. */;
11306 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11307 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11308 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11309 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11310 /* The command is allowed for Write Exclusive resv. */;
11313 * if we aren't registered or it's a res holder type
11314 * reservation and this isn't the res holder then set a
11317 if (ctl_get_prkey(lun, residx) == 0 ||
11318 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11319 ctl_set_reservation_conflict(ctsio);
11325 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11326 if (lun->flags & CTL_LUN_EJECTED)
11327 ctl_set_lun_ejected(ctsio);
11328 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11329 if (lun->flags & CTL_LUN_REMOVABLE)
11330 ctl_set_lun_no_media(ctsio);
11332 ctl_set_lun_int_reqd(ctsio);
11333 } else if (lun->flags & CTL_LUN_STOPPED)
11334 ctl_set_lun_stopped(ctsio);
11346 ctl_failover_io(union ctl_io *io, int have_lock)
11348 ctl_set_busy(&io->scsiio);
11353 ctl_failover_lun(union ctl_io *rio)
11355 struct ctl_softc *softc = CTL_SOFTC(rio);
11356 struct ctl_lun *lun;
11357 struct ctl_io_hdr *io, *next_io;
11360 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11361 CTL_DEBUG_PRINT(("FAILOVER for lun %u\n", targ_lun));
11363 /* Find and lock the LUN. */
11364 mtx_lock(&softc->ctl_lock);
11365 if (targ_lun > ctl_max_luns ||
11366 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11367 mtx_unlock(&softc->ctl_lock);
11370 mtx_lock(&lun->lun_lock);
11371 mtx_unlock(&softc->ctl_lock);
11372 if (lun->flags & CTL_LUN_DISABLED) {
11373 mtx_unlock(&lun->lun_lock);
11377 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11378 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11379 /* We are master */
11380 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11381 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11382 io->flags |= CTL_FLAG_ABORT;
11383 io->flags |= CTL_FLAG_FAILOVER;
11384 ctl_try_unblock_io(lun,
11385 (union ctl_io *)io, FALSE);
11386 } else { /* This can be only due to DATAMOVE */
11387 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11388 io->flags &= ~CTL_FLAG_DMA_INPROG;
11389 io->flags |= CTL_FLAG_IO_ACTIVE;
11390 io->port_status = 31340;
11391 ctl_enqueue_isc((union ctl_io *)io);
11395 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11396 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11397 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11398 io->flags |= CTL_FLAG_FAILOVER;
11400 ctl_set_busy(&((union ctl_io *)io)->
11402 ctl_done((union ctl_io *)io);
11406 } else { /* SERIALIZE modes */
11407 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11408 /* We are master */
11409 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11410 if (io->blocker != NULL) {
11411 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue,
11412 io, blocked_links);
11413 io->blocker = NULL;
11415 ctl_try_unblock_others(lun, (union ctl_io *)io,
11417 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11418 ctl_free_io((union ctl_io *)io);
11421 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11422 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11423 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11424 ctl_set_busy(&((union ctl_io *)io)->
11426 ctl_done((union ctl_io *)io);
11431 mtx_unlock(&lun->lun_lock);
11435 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11437 struct ctl_lun *lun;
11438 const struct ctl_cmd_entry *entry;
11440 uint32_t initidx, targ_lun;
11444 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11445 if (targ_lun < ctl_max_luns)
11446 lun = softc->ctl_luns[targ_lun];
11449 * If the LUN is invalid, pretend that it doesn't exist.
11450 * It will go away as soon as all pending I/O has been
11453 mtx_lock(&lun->lun_lock);
11454 if (lun->flags & CTL_LUN_DISABLED) {
11455 mtx_unlock(&lun->lun_lock);
11459 CTL_LUN(ctsio) = lun;
11461 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11464 * Every I/O goes into the OOA queue for a particular LUN,
11465 * and stays there until completion.
11468 if (TAILQ_EMPTY(&lun->ooa_queue))
11469 lun->idle_time += getsbinuptime() - lun->last_busy;
11471 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11474 /* Get command entry and return error if it is unsuppotyed. */
11475 entry = ctl_validate_command(ctsio);
11476 if (entry == NULL) {
11478 mtx_unlock(&lun->lun_lock);
11482 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11483 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11486 * Check to see whether we can send this command to LUNs that don't
11487 * exist. This should pretty much only be the case for inquiry
11488 * and request sense. Further checks, below, really require having
11489 * a LUN, so we can't really check the command anymore. Just put
11490 * it on the rtr queue.
11493 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11494 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11495 ctl_enqueue_rtr((union ctl_io *)ctsio);
11499 ctl_set_unsupported_lun(ctsio);
11500 ctl_done((union ctl_io *)ctsio);
11501 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11505 * Make sure we support this particular command on this LUN.
11506 * e.g., we don't support writes to the control LUN.
11508 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11509 mtx_unlock(&lun->lun_lock);
11510 ctl_set_invalid_opcode(ctsio);
11511 ctl_done((union ctl_io *)ctsio);
11516 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11519 * If we've got a request sense, it'll clear the contingent
11520 * allegiance condition. Otherwise, if we have a CA condition for
11521 * this initiator, clear it, because it sent down a command other
11522 * than request sense.
11524 if (ctsio->cdb[0] != REQUEST_SENSE) {
11525 struct scsi_sense_data *ps;
11527 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11529 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11533 * If the command has this flag set, it handles its own unit
11534 * attention reporting, we shouldn't do anything. Otherwise we
11535 * check for any pending unit attentions, and send them back to the
11536 * initiator. We only do this when a command initially comes in,
11537 * not when we pull it off the blocked queue.
11539 * According to SAM-3, section 5.3.2, the order that things get
11540 * presented back to the host is basically unit attentions caused
11541 * by some sort of reset event, busy status, reservation conflicts
11542 * or task set full, and finally any other status.
11544 * One issue here is that some of the unit attentions we report
11545 * don't fall into the "reset" category (e.g. "reported luns data
11546 * has changed"). So reporting it here, before the reservation
11547 * check, may be technically wrong. I guess the only thing to do
11548 * would be to check for and report the reset events here, and then
11549 * check for the other unit attention types after we check for a
11550 * reservation conflict.
11552 * XXX KDM need to fix this
11554 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11555 ctl_ua_type ua_type;
11556 u_int sense_len = 0;
11558 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11559 &sense_len, SSD_TYPE_NONE);
11560 if (ua_type != CTL_UA_NONE) {
11561 mtx_unlock(&lun->lun_lock);
11562 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11563 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11564 ctsio->sense_len = sense_len;
11565 ctl_done((union ctl_io *)ctsio);
11571 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11572 mtx_unlock(&lun->lun_lock);
11573 ctl_done((union ctl_io *)ctsio);
11578 * XXX CHD this is where we want to send IO to other side if
11579 * this LUN is secondary on this SC. We will need to make a copy
11580 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11581 * the copy we send as FROM_OTHER.
11582 * We also need to stuff the address of the original IO so we can
11583 * find it easily. Something similar will need be done on the other
11584 * side so when we are done we can find the copy.
11586 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11587 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11588 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11589 union ctl_ha_msg msg_info;
11592 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11593 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11594 mtx_unlock(&lun->lun_lock);
11596 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11597 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11598 msg_info.hdr.serializing_sc = NULL;
11599 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11600 msg_info.scsi.tag_num = ctsio->tag_num;
11601 msg_info.scsi.tag_type = ctsio->tag_type;
11602 msg_info.scsi.cdb_len = ctsio->cdb_len;
11603 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11605 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11606 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11607 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11608 ctl_set_busy(ctsio);
11609 ctl_done((union ctl_io *)ctsio);
11615 bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links);
11616 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
11617 case CTL_ACTION_BLOCK:
11618 ctsio->io_hdr.blocker = bio;
11619 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
11621 mtx_unlock(&lun->lun_lock);
11623 case CTL_ACTION_PASS:
11624 case CTL_ACTION_SKIP:
11625 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11626 mtx_unlock(&lun->lun_lock);
11627 ctl_enqueue_rtr((union ctl_io *)ctsio);
11629 case CTL_ACTION_OVERLAP:
11630 mtx_unlock(&lun->lun_lock);
11631 ctl_set_overlapped_cmd(ctsio);
11632 ctl_done((union ctl_io *)ctsio);
11634 case CTL_ACTION_OVERLAP_TAG:
11635 mtx_unlock(&lun->lun_lock);
11636 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11637 ctl_done((union ctl_io *)ctsio);
11639 case CTL_ACTION_ERROR:
11641 mtx_unlock(&lun->lun_lock);
11642 ctl_set_internal_failure(ctsio,
11644 /*retry_count*/ 0);
11645 ctl_done((union ctl_io *)ctsio);
11651 const struct ctl_cmd_entry *
11652 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11654 const struct ctl_cmd_entry *entry;
11655 int service_action;
11657 entry = &ctl_cmd_table[ctsio->cdb[0]];
11659 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11660 if (entry->flags & CTL_CMD_FLAG_SA5) {
11661 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11662 entry = &((const struct ctl_cmd_entry *)
11663 entry->execute)[service_action];
11668 const struct ctl_cmd_entry *
11669 ctl_validate_command(struct ctl_scsiio *ctsio)
11671 const struct ctl_cmd_entry *entry;
11675 entry = ctl_get_cmd_entry(ctsio, &sa);
11676 if (entry->execute == NULL) {
11678 ctl_set_invalid_field(ctsio,
11685 ctl_set_invalid_opcode(ctsio);
11686 ctl_done((union ctl_io *)ctsio);
11689 KASSERT(entry->length > 0,
11690 ("Not defined length for command 0x%02x/0x%02x",
11691 ctsio->cdb[0], ctsio->cdb[1]));
11692 for (i = 1; i < entry->length; i++) {
11693 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11696 ctl_set_invalid_field(ctsio,
11701 /*bit*/ fls(diff) - 1);
11702 ctl_done((union ctl_io *)ctsio);
11709 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11712 switch (lun_type) {
11714 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11718 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11722 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11732 ctl_scsiio(struct ctl_scsiio *ctsio)
11735 const struct ctl_cmd_entry *entry;
11737 retval = CTL_RETVAL_COMPLETE;
11739 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11741 entry = ctl_get_cmd_entry(ctsio, NULL);
11744 * If this I/O has been aborted, just send it straight to
11745 * ctl_done() without executing it.
11747 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11748 ctl_done((union ctl_io *)ctsio);
11753 * All the checks should have been handled by ctl_scsiio_precheck().
11754 * We should be clear now to just execute the I/O.
11756 retval = entry->execute(ctsio);
11763 ctl_target_reset(union ctl_io *io)
11765 struct ctl_softc *softc = CTL_SOFTC(io);
11766 struct ctl_port *port = CTL_PORT(io);
11767 struct ctl_lun *lun;
11769 ctl_ua_type ua_type;
11771 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11772 union ctl_ha_msg msg_info;
11774 msg_info.hdr.nexus = io->io_hdr.nexus;
11775 msg_info.task.task_action = io->taskio.task_action;
11776 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11777 msg_info.hdr.original_sc = NULL;
11778 msg_info.hdr.serializing_sc = NULL;
11779 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11780 sizeof(msg_info.task), M_WAITOK);
11783 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11784 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11785 ua_type = CTL_UA_TARG_RESET;
11787 ua_type = CTL_UA_BUS_RESET;
11788 mtx_lock(&softc->ctl_lock);
11789 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11790 if (port != NULL &&
11791 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11793 ctl_do_lun_reset(lun, initidx, ua_type);
11795 mtx_unlock(&softc->ctl_lock);
11796 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11801 * The LUN should always be set. The I/O is optional, and is used to
11802 * distinguish between I/Os sent by this initiator, and by other
11803 * initiators. We set unit attention for initiators other than this one.
11804 * SAM-3 is vague on this point. It does say that a unit attention should
11805 * be established for other initiators when a LUN is reset (see section
11806 * 5.7.3), but it doesn't specifically say that the unit attention should
11807 * be established for this particular initiator when a LUN is reset. Here
11808 * is the relevant text, from SAM-3 rev 8:
11810 * 5.7.2 When a SCSI initiator port aborts its own tasks
11812 * When a SCSI initiator port causes its own task(s) to be aborted, no
11813 * notification that the task(s) have been aborted shall be returned to
11814 * the SCSI initiator port other than the completion response for the
11815 * command or task management function action that caused the task(s) to
11816 * be aborted and notification(s) associated with related effects of the
11817 * action (e.g., a reset unit attention condition).
11819 * XXX KDM for now, we're setting unit attention for all initiators.
11822 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11827 mtx_lock(&lun->lun_lock);
11829 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11830 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11831 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11832 ctl_try_unblock_io(lun, xio, FALSE);
11835 for (i = 0; i < ctl_max_ports; i++) {
11836 free(lun->pending_sense[i], M_CTL);
11837 lun->pending_sense[i] = NULL;
11839 /* Clear reservation. */
11840 lun->flags &= ~CTL_LUN_RESERVED;
11841 /* Clear prevent media removal. */
11842 if (lun->prevent) {
11843 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11844 ctl_clear_mask(lun->prevent, i);
11845 lun->prevent_count = 0;
11847 /* Clear TPC status */
11848 ctl_tpc_lun_clear(lun, -1);
11849 /* Establish UA. */
11851 ctl_est_ua_all(lun, initidx, ua_type);
11853 ctl_est_ua_all(lun, -1, ua_type);
11855 mtx_unlock(&lun->lun_lock);
11859 ctl_lun_reset(union ctl_io *io)
11861 struct ctl_softc *softc = CTL_SOFTC(io);
11862 struct ctl_lun *lun;
11863 uint32_t targ_lun, initidx;
11865 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11866 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11867 mtx_lock(&softc->ctl_lock);
11868 if (targ_lun >= ctl_max_luns ||
11869 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11870 mtx_unlock(&softc->ctl_lock);
11871 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11874 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11875 mtx_unlock(&softc->ctl_lock);
11876 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11878 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11879 union ctl_ha_msg msg_info;
11881 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11882 msg_info.hdr.nexus = io->io_hdr.nexus;
11883 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11884 msg_info.hdr.original_sc = NULL;
11885 msg_info.hdr.serializing_sc = NULL;
11886 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11887 sizeof(msg_info.task), M_WAITOK);
11893 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11898 mtx_assert(&lun->lun_lock, MA_OWNED);
11901 * Run through the OOA queue and attempt to find the given I/O.
11902 * The target port, initiator ID, tag type and tag number have to
11903 * match the values that we got from the initiator. If we have an
11904 * untagged command to abort, simply abort the first untagged command
11905 * we come to. We only allow one untagged command at a time of course.
11907 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11908 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11910 if ((targ_port == UINT32_MAX ||
11911 targ_port == xio->io_hdr.nexus.targ_port) &&
11912 (init_id == UINT32_MAX ||
11913 init_id == xio->io_hdr.nexus.initid)) {
11914 if (targ_port != xio->io_hdr.nexus.targ_port ||
11915 init_id != xio->io_hdr.nexus.initid)
11916 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11917 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11918 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11919 union ctl_ha_msg msg_info;
11921 msg_info.hdr.nexus = xio->io_hdr.nexus;
11922 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11923 msg_info.task.tag_num = xio->scsiio.tag_num;
11924 msg_info.task.tag_type = xio->scsiio.tag_type;
11925 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11926 msg_info.hdr.original_sc = NULL;
11927 msg_info.hdr.serializing_sc = NULL;
11928 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11929 sizeof(msg_info.task), M_NOWAIT);
11931 ctl_try_unblock_io(lun, xio, FALSE);
11937 ctl_abort_task_set(union ctl_io *io)
11939 struct ctl_softc *softc = CTL_SOFTC(io);
11940 struct ctl_lun *lun;
11946 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11947 mtx_lock(&softc->ctl_lock);
11948 if (targ_lun >= ctl_max_luns ||
11949 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11950 mtx_unlock(&softc->ctl_lock);
11951 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11955 mtx_lock(&lun->lun_lock);
11956 mtx_unlock(&softc->ctl_lock);
11957 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11958 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11959 io->io_hdr.nexus.initid,
11960 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11961 } else { /* CTL_TASK_CLEAR_TASK_SET */
11962 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11963 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11965 mtx_unlock(&lun->lun_lock);
11966 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11971 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11972 ctl_ua_type ua_type)
11974 struct ctl_lun *lun;
11975 struct scsi_sense_data *ps;
11978 p = initidx / CTL_MAX_INIT_PER_PORT;
11979 i = initidx % CTL_MAX_INIT_PER_PORT;
11980 mtx_lock(&softc->ctl_lock);
11981 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11982 mtx_lock(&lun->lun_lock);
11984 ctl_abort_tasks_lun(lun, p, i, 1);
11986 ps = lun->pending_sense[p];
11988 ps[i].error_code = 0;
11989 /* Clear reservation. */
11990 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11991 lun->flags &= ~CTL_LUN_RESERVED;
11992 /* Clear prevent media removal. */
11993 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11994 ctl_clear_mask(lun->prevent, initidx);
11995 lun->prevent_count--;
11997 /* Clear TPC status */
11998 ctl_tpc_lun_clear(lun, initidx);
11999 /* Establish UA. */
12000 ctl_est_ua(lun, initidx, ua_type);
12001 mtx_unlock(&lun->lun_lock);
12003 mtx_unlock(&softc->ctl_lock);
12007 ctl_i_t_nexus_reset(union ctl_io *io)
12009 struct ctl_softc *softc = CTL_SOFTC(io);
12012 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
12013 union ctl_ha_msg msg_info;
12015 msg_info.hdr.nexus = io->io_hdr.nexus;
12016 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
12017 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12018 msg_info.hdr.original_sc = NULL;
12019 msg_info.hdr.serializing_sc = NULL;
12020 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12021 sizeof(msg_info.task), M_WAITOK);
12024 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12025 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
12026 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12031 ctl_abort_task(union ctl_io *io)
12033 struct ctl_softc *softc = CTL_SOFTC(io);
12035 struct ctl_lun *lun;
12041 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12042 mtx_lock(&softc->ctl_lock);
12043 if (targ_lun >= ctl_max_luns ||
12044 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12045 mtx_unlock(&softc->ctl_lock);
12046 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12050 mtx_lock(&lun->lun_lock);
12051 mtx_unlock(&softc->ctl_lock);
12053 * Run through the OOA queue and attempt to find the given I/O.
12054 * The target port, initiator ID, tag type and tag number have to
12055 * match the values that we got from the initiator. If we have an
12056 * untagged command to abort, simply abort the first untagged command
12057 * we come to. We only allow one untagged command at a time of course.
12059 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12060 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12062 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12063 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12064 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12068 * If the abort says that the task is untagged, the
12069 * task in the queue must be untagged. Otherwise,
12070 * we just check to see whether the tag numbers
12071 * match. This is because the QLogic firmware
12072 * doesn't pass back the tag type in an abort
12076 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12077 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12078 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12081 * XXX KDM we've got problems with FC, because it
12082 * doesn't send down a tag type with aborts. So we
12083 * can only really go by the tag number...
12084 * This may cause problems with parallel SCSI.
12085 * Need to figure that out!!
12087 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12089 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12090 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12091 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12092 union ctl_ha_msg msg_info;
12094 msg_info.hdr.nexus = io->io_hdr.nexus;
12095 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12096 msg_info.task.tag_num = io->taskio.tag_num;
12097 msg_info.task.tag_type = io->taskio.tag_type;
12098 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12099 msg_info.hdr.original_sc = NULL;
12100 msg_info.hdr.serializing_sc = NULL;
12101 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12102 sizeof(msg_info.task), M_NOWAIT);
12104 ctl_try_unblock_io(lun, xio, FALSE);
12107 mtx_unlock(&lun->lun_lock);
12108 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12113 ctl_query_task(union ctl_io *io, int task_set)
12115 struct ctl_softc *softc = CTL_SOFTC(io);
12117 struct ctl_lun *lun;
12121 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12122 mtx_lock(&softc->ctl_lock);
12123 if (targ_lun >= ctl_max_luns ||
12124 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12125 mtx_unlock(&softc->ctl_lock);
12126 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12129 mtx_lock(&lun->lun_lock);
12130 mtx_unlock(&softc->ctl_lock);
12131 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12132 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12134 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12135 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12136 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12139 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12144 mtx_unlock(&lun->lun_lock);
12146 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12148 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12153 ctl_query_async_event(union ctl_io *io)
12155 struct ctl_softc *softc = CTL_SOFTC(io);
12156 struct ctl_lun *lun;
12158 uint32_t targ_lun, initidx;
12160 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12161 mtx_lock(&softc->ctl_lock);
12162 if (targ_lun >= ctl_max_luns ||
12163 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12164 mtx_unlock(&softc->ctl_lock);
12165 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12168 mtx_lock(&lun->lun_lock);
12169 mtx_unlock(&softc->ctl_lock);
12170 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12171 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12172 mtx_unlock(&lun->lun_lock);
12173 if (ua != CTL_UA_NONE)
12174 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12176 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12181 ctl_run_task(union ctl_io *io)
12185 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12186 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12187 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12188 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12189 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12190 switch (io->taskio.task_action) {
12191 case CTL_TASK_ABORT_TASK:
12192 retval = ctl_abort_task(io);
12194 case CTL_TASK_ABORT_TASK_SET:
12195 case CTL_TASK_CLEAR_TASK_SET:
12196 retval = ctl_abort_task_set(io);
12198 case CTL_TASK_CLEAR_ACA:
12200 case CTL_TASK_I_T_NEXUS_RESET:
12201 retval = ctl_i_t_nexus_reset(io);
12203 case CTL_TASK_LUN_RESET:
12204 retval = ctl_lun_reset(io);
12206 case CTL_TASK_TARGET_RESET:
12207 case CTL_TASK_BUS_RESET:
12208 retval = ctl_target_reset(io);
12210 case CTL_TASK_PORT_LOGIN:
12212 case CTL_TASK_PORT_LOGOUT:
12214 case CTL_TASK_QUERY_TASK:
12215 retval = ctl_query_task(io, 0);
12217 case CTL_TASK_QUERY_TASK_SET:
12218 retval = ctl_query_task(io, 1);
12220 case CTL_TASK_QUERY_ASYNC_EVENT:
12221 retval = ctl_query_async_event(io);
12224 printf("%s: got unknown task management event %d\n",
12225 __func__, io->taskio.task_action);
12229 io->io_hdr.status = CTL_SUCCESS;
12231 io->io_hdr.status = CTL_ERROR;
12236 * For HA operation. Handle commands that come in from the other
12240 ctl_handle_isc(union ctl_io *io)
12242 struct ctl_softc *softc = CTL_SOFTC(io);
12243 struct ctl_lun *lun;
12244 const struct ctl_cmd_entry *entry;
12247 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12248 switch (io->io_hdr.msg_type) {
12249 case CTL_MSG_SERIALIZE:
12250 ctl_serialize_other_sc_cmd(&io->scsiio);
12252 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12253 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12254 if (targ_lun >= ctl_max_luns ||
12255 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12259 mtx_lock(&lun->lun_lock);
12260 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12261 mtx_unlock(&lun->lun_lock);
12265 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12266 mtx_unlock(&lun->lun_lock);
12267 ctl_enqueue_rtr(io);
12269 case CTL_MSG_FINISH_IO:
12270 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12274 if (targ_lun >= ctl_max_luns ||
12275 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12279 mtx_lock(&lun->lun_lock);
12280 ctl_try_unblock_others(lun, io, TRUE);
12281 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12282 mtx_unlock(&lun->lun_lock);
12285 case CTL_MSG_PERS_ACTION:
12286 ctl_hndl_per_res_out_on_other_sc(io);
12289 case CTL_MSG_BAD_JUJU:
12292 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12293 ctl_datamove_remote(io);
12295 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12296 io->scsiio.be_move_done(io);
12298 case CTL_MSG_FAILOVER:
12299 ctl_failover_lun(io);
12303 printf("%s: Invalid message type %d\n",
12304 __func__, io->io_hdr.msg_type);
12313 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12314 * there is no match.
12316 static ctl_lun_error_pattern
12317 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12319 const struct ctl_cmd_entry *entry;
12320 ctl_lun_error_pattern filtered_pattern, pattern;
12322 pattern = desc->error_pattern;
12325 * XXX KDM we need more data passed into this function to match a
12326 * custom pattern, and we actually need to implement custom pattern
12329 if (pattern & CTL_LUN_PAT_CMD)
12330 return (CTL_LUN_PAT_CMD);
12332 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12333 return (CTL_LUN_PAT_ANY);
12335 entry = ctl_get_cmd_entry(ctsio, NULL);
12337 filtered_pattern = entry->pattern & pattern;
12340 * If the user requested specific flags in the pattern (e.g.
12341 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12344 * If the user did not specify any flags, it doesn't matter whether
12345 * or not the command supports the flags.
12347 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12348 (pattern & ~CTL_LUN_PAT_MASK))
12349 return (CTL_LUN_PAT_NONE);
12352 * If the user asked for a range check, see if the requested LBA
12353 * range overlaps with this command's LBA range.
12355 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12361 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12363 return (CTL_LUN_PAT_NONE);
12365 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12366 desc->lba_range.len, FALSE);
12368 * A "pass" means that the LBA ranges don't overlap, so
12369 * this doesn't match the user's range criteria.
12371 if (action == CTL_ACTION_PASS)
12372 return (CTL_LUN_PAT_NONE);
12375 return (filtered_pattern);
12379 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12381 struct ctl_error_desc *desc, *desc2;
12383 mtx_assert(&lun->lun_lock, MA_OWNED);
12385 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12386 ctl_lun_error_pattern pattern;
12388 * Check to see whether this particular command matches
12389 * the pattern in the descriptor.
12391 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12392 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12395 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12396 case CTL_LUN_INJ_ABORTED:
12397 ctl_set_aborted(&io->scsiio);
12399 case CTL_LUN_INJ_MEDIUM_ERR:
12400 ctl_set_medium_error(&io->scsiio,
12401 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12402 CTL_FLAG_DATA_OUT);
12404 case CTL_LUN_INJ_UA:
12405 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12407 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12409 case CTL_LUN_INJ_CUSTOM:
12411 * We're assuming the user knows what he is doing.
12412 * Just copy the sense information without doing
12415 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12416 MIN(sizeof(desc->custom_sense),
12417 sizeof(io->scsiio.sense_data)));
12418 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12419 io->scsiio.sense_len = SSD_FULL_SIZE;
12420 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12422 case CTL_LUN_INJ_NONE:
12425 * If this is an error injection type we don't know
12426 * about, clear the continuous flag (if it is set)
12427 * so it will get deleted below.
12429 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12433 * By default, each error injection action is a one-shot
12435 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12438 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12444 #ifdef CTL_IO_DELAY
12446 ctl_datamove_timer_wakeup(void *arg)
12450 io = (union ctl_io *)arg;
12454 #endif /* CTL_IO_DELAY */
12457 ctl_datamove(union ctl_io *io)
12459 void (*fe_datamove)(union ctl_io *io);
12461 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12463 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12465 /* No data transferred yet. Frontend must update this when done. */
12466 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12469 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12474 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12475 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12477 sbuf_cat(&sb, path_str);
12478 switch (io->io_hdr.io_type) {
12480 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12481 sbuf_printf(&sb, "\n");
12482 sbuf_cat(&sb, path_str);
12483 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12484 io->scsiio.tag_num, io->scsiio.tag_type);
12487 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12488 "Tag Type: %d\n", io->taskio.task_action,
12489 io->taskio.tag_num, io->taskio.tag_type);
12492 panic("%s: Invalid CTL I/O type %d\n",
12493 __func__, io->io_hdr.io_type);
12495 sbuf_cat(&sb, path_str);
12496 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12497 (intmax_t)time_uptime - io->io_hdr.start_time);
12499 printf("%s", sbuf_data(&sb));
12501 #endif /* CTL_TIME_IO */
12503 #ifdef CTL_IO_DELAY
12504 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12505 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12507 struct ctl_lun *lun;
12511 && (lun->delay_info.datamove_delay > 0)) {
12513 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12514 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12515 callout_reset(&io->io_hdr.delay_callout,
12516 lun->delay_info.datamove_delay * hz,
12517 ctl_datamove_timer_wakeup, io);
12518 if (lun->delay_info.datamove_type ==
12519 CTL_DELAY_TYPE_ONESHOT)
12520 lun->delay_info.datamove_delay = 0;
12527 * This command has been aborted. Set the port status, so we fail
12530 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12531 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12532 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12533 io->io_hdr.nexus.targ_port,
12534 io->io_hdr.nexus.targ_lun);
12535 io->io_hdr.port_status = 31337;
12537 * Note that the backend, in this case, will get the
12538 * callback in its context. In other cases it may get
12539 * called in the frontend's interrupt thread context.
12541 io->scsiio.be_move_done(io);
12545 /* Don't confuse frontend with zero length data move. */
12546 if (io->scsiio.kern_data_len == 0) {
12547 io->scsiio.be_move_done(io);
12551 fe_datamove = CTL_PORT(io)->fe_datamove;
12556 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12558 union ctl_ha_msg msg;
12560 struct bintime cur_bt;
12563 memset(&msg, 0, sizeof(msg));
12564 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12565 msg.hdr.original_sc = io;
12566 msg.hdr.serializing_sc = io->io_hdr.remote_io;
12567 msg.hdr.nexus = io->io_hdr.nexus;
12568 msg.hdr.status = io->io_hdr.status;
12569 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12570 msg.scsi.tag_num = io->scsiio.tag_num;
12571 msg.scsi.tag_type = io->scsiio.tag_type;
12572 msg.scsi.scsi_status = io->scsiio.scsi_status;
12573 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12574 io->scsiio.sense_len);
12575 msg.scsi.sense_len = io->scsiio.sense_len;
12576 msg.scsi.port_status = io->io_hdr.port_status;
12577 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12578 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12579 ctl_failover_io(io, /*have_lock*/ have_lock);
12582 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12583 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12584 msg.scsi.sense_len, M_WAITOK);
12587 getbinuptime(&cur_bt);
12588 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12589 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12591 io->io_hdr.num_dmas++;
12595 * The DMA to the remote side is done, now we need to tell the other side
12596 * we're done so it can continue with its data movement.
12599 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12606 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12607 printf("%s: ISC DMA write failed with error %d", __func__,
12609 ctl_set_internal_failure(&io->scsiio,
12611 /*retry_count*/ rq->ret);
12614 ctl_dt_req_free(rq);
12616 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12617 free(CTL_LSGLT(io)[i].addr, M_CTL);
12618 free(CTL_RSGL(io), M_CTL);
12619 CTL_RSGL(io) = NULL;
12620 CTL_LSGL(io) = NULL;
12623 * The data is in local and remote memory, so now we need to send
12624 * status (good or back) back to the other side.
12626 ctl_send_datamove_done(io, /*have_lock*/ 0);
12630 * We've moved the data from the host/controller into local memory. Now we
12631 * need to push it over to the remote controller's memory.
12634 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12638 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12639 ctl_datamove_remote_write_cb);
12644 ctl_datamove_remote_write(union ctl_io *io)
12647 void (*fe_datamove)(union ctl_io *io);
12650 * - Get the data from the host/HBA into local memory.
12651 * - DMA memory from the local controller to the remote controller.
12652 * - Send status back to the remote controller.
12655 retval = ctl_datamove_remote_sgl_setup(io);
12659 /* Switch the pointer over so the FETD knows what to do */
12660 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12663 * Use a custom move done callback, since we need to send completion
12664 * back to the other controller, not to the backend on this side.
12666 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12668 fe_datamove = CTL_PORT(io)->fe_datamove;
12673 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12677 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12678 free(CTL_LSGLT(io)[i].addr, M_CTL);
12679 free(CTL_RSGL(io), M_CTL);
12680 CTL_RSGL(io) = NULL;
12681 CTL_LSGL(io) = NULL;
12684 * The read is done, now we need to send status (good or bad) back
12685 * to the other side.
12687 ctl_send_datamove_done(io, /*have_lock*/ 0);
12693 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12696 void (*fe_datamove)(union ctl_io *io);
12700 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12701 printf("%s: ISC DMA read failed with error %d\n", __func__,
12703 ctl_set_internal_failure(&io->scsiio,
12705 /*retry_count*/ rq->ret);
12708 ctl_dt_req_free(rq);
12710 /* Switch the pointer over so the FETD knows what to do */
12711 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12714 * Use a custom move done callback, since we need to send completion
12715 * back to the other controller, not to the backend on this side.
12717 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12719 /* XXX KDM add checks like the ones in ctl_datamove? */
12721 fe_datamove = CTL_PORT(io)->fe_datamove;
12726 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12728 struct ctl_sg_entry *local_sglist;
12729 uint32_t len_to_go;
12734 local_sglist = CTL_LSGL(io);
12735 len_to_go = io->scsiio.kern_data_len;
12738 * The difficult thing here is that the size of the various
12739 * S/G segments may be different than the size from the
12740 * remote controller. That'll make it harder when DMAing
12741 * the data back to the other side.
12743 for (i = 0; len_to_go > 0; i++) {
12744 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12745 local_sglist[i].addr =
12746 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12748 len_to_go -= local_sglist[i].len;
12751 * Reset the number of S/G entries accordingly. The original
12752 * number of S/G entries is available in rem_sg_entries.
12754 io->scsiio.kern_sg_entries = i;
12760 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12761 ctl_ha_dt_cb callback)
12763 struct ctl_ha_dt_req *rq;
12764 struct ctl_sg_entry *remote_sglist, *local_sglist;
12765 uint32_t local_used, remote_used, total_used;
12768 rq = ctl_dt_req_alloc();
12771 * If we failed to allocate the request, and if the DMA didn't fail
12772 * anyway, set busy status. This is just a resource allocation
12776 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12777 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12778 ctl_set_busy(&io->scsiio);
12780 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12781 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12784 ctl_dt_req_free(rq);
12787 * The data move failed. We need to return status back
12788 * to the other controller. No point in trying to DMA
12789 * data to the remote controller.
12792 ctl_send_datamove_done(io, /*have_lock*/ 0);
12797 local_sglist = CTL_LSGL(io);
12798 remote_sglist = CTL_RSGL(io);
12804 * Pull/push the data over the wire from/to the other controller.
12805 * This takes into account the possibility that the local and
12806 * remote sglists may not be identical in terms of the size of
12807 * the elements and the number of elements.
12809 * One fundamental assumption here is that the length allocated for
12810 * both the local and remote sglists is identical. Otherwise, we've
12811 * essentially got a coding error of some sort.
12813 isc_ret = CTL_HA_STATUS_SUCCESS;
12814 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12818 rq->command = command;
12822 * Both pointers should be aligned. But it is possible
12823 * that the allocation length is not. They should both
12824 * also have enough slack left over at the end, though,
12825 * to round up to the next 8 byte boundary.
12827 cur_len = MIN(local_sglist[i].len - local_used,
12828 remote_sglist[j].len - remote_used);
12829 rq->size = cur_len;
12831 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12832 tmp_ptr += local_used;
12835 /* Use physical addresses when talking to ISC hardware */
12836 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12837 /* XXX KDM use busdma */
12838 rq->local = vtophys(tmp_ptr);
12840 rq->local = tmp_ptr;
12842 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12843 ("HA does not support BUS_ADDR"));
12844 rq->local = tmp_ptr;
12847 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12848 tmp_ptr += remote_used;
12849 rq->remote = tmp_ptr;
12851 rq->callback = NULL;
12853 local_used += cur_len;
12854 if (local_used >= local_sglist[i].len) {
12859 remote_used += cur_len;
12860 if (remote_used >= remote_sglist[j].len) {
12864 total_used += cur_len;
12866 if (total_used >= io->scsiio.kern_data_len)
12867 rq->callback = callback;
12869 isc_ret = ctl_dt_single(rq);
12870 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12873 if (isc_ret != CTL_HA_STATUS_WAIT) {
12882 ctl_datamove_remote_read(union ctl_io *io)
12888 * This will send an error to the other controller in the case of a
12891 retval = ctl_datamove_remote_sgl_setup(io);
12895 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12896 ctl_datamove_remote_read_cb);
12899 * Make sure we free memory if there was an error.. The
12900 * ctl_datamove_remote_xfer() function will send the
12901 * datamove done message, or call the callback with an
12902 * error if there is a problem.
12904 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12905 free(CTL_LSGLT(io)[i].addr, M_CTL);
12906 free(CTL_RSGL(io), M_CTL);
12907 CTL_RSGL(io) = NULL;
12908 CTL_LSGL(io) = NULL;
12913 * Process a datamove request from the other controller. This is used for
12914 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12915 * first. Once that is complete, the data gets DMAed into the remote
12916 * controller's memory. For reads, we DMA from the remote controller's
12917 * memory into our memory first, and then move it out to the FETD.
12920 ctl_datamove_remote(union ctl_io *io)
12923 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12925 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12926 ctl_failover_io(io, /*have_lock*/ 0);
12931 * Note that we look for an aborted I/O here, but don't do some of
12932 * the other checks that ctl_datamove() normally does.
12933 * We don't need to run the datamove delay code, since that should
12934 * have been done if need be on the other controller.
12936 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12937 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12938 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12939 io->io_hdr.nexus.targ_port,
12940 io->io_hdr.nexus.targ_lun);
12941 io->io_hdr.port_status = 31338;
12942 ctl_send_datamove_done(io, /*have_lock*/ 0);
12946 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12947 ctl_datamove_remote_write(io);
12948 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12949 ctl_datamove_remote_read(io);
12951 io->io_hdr.port_status = 31339;
12952 ctl_send_datamove_done(io, /*have_lock*/ 0);
12957 ctl_process_done(union ctl_io *io)
12959 struct ctl_softc *softc = CTL_SOFTC(io);
12960 struct ctl_port *port = CTL_PORT(io);
12961 struct ctl_lun *lun = CTL_LUN(io);
12962 void (*fe_done)(union ctl_io *io);
12963 union ctl_ha_msg msg;
12965 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12966 fe_done = port->fe_done;
12969 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12974 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12975 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12977 sbuf_cat(&sb, path_str);
12978 switch (io->io_hdr.io_type) {
12980 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12981 sbuf_printf(&sb, "\n");
12982 sbuf_cat(&sb, path_str);
12983 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12984 io->scsiio.tag_num, io->scsiio.tag_type);
12987 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12988 "Tag Type: %d\n", io->taskio.task_action,
12989 io->taskio.tag_num, io->taskio.tag_type);
12992 panic("%s: Invalid CTL I/O type %d\n",
12993 __func__, io->io_hdr.io_type);
12995 sbuf_cat(&sb, path_str);
12996 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12997 (intmax_t)time_uptime - io->io_hdr.start_time);
12999 printf("%s", sbuf_data(&sb));
13001 #endif /* CTL_TIME_IO */
13003 switch (io->io_hdr.io_type) {
13007 if (ctl_debug & CTL_DEBUG_INFO)
13008 ctl_io_error_print(io, NULL);
13012 panic("%s: Invalid CTL I/O type %d\n",
13013 __func__, io->io_hdr.io_type);
13017 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
13018 io->io_hdr.nexus.targ_mapped_lun));
13022 mtx_lock(&lun->lun_lock);
13025 * Check to see if we have any informational exception and status
13026 * of this command can be modified to report it in form of either
13027 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
13029 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
13030 io->io_hdr.status == CTL_SUCCESS &&
13031 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
13032 uint8_t mrie = lun->MODE_IE.mrie;
13033 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
13034 (lun->MODE_VER.byte3 & SMS_VER_PER));
13035 if (((mrie == SIEP_MRIE_REC_COND && per) ||
13036 mrie == SIEP_MRIE_REC_UNCOND ||
13037 mrie == SIEP_MRIE_NO_SENSE) &&
13038 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
13039 CTL_CMD_FLAG_NO_SENSE) == 0) {
13040 ctl_set_sense(&io->scsiio,
13041 /*current_error*/ 1,
13042 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
13043 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
13044 /*asc*/ lun->ie_asc,
13045 /*ascq*/ lun->ie_ascq,
13047 lun->ie_reported = 1;
13049 } else if (lun->ie_reported < 0)
13050 lun->ie_reported = 0;
13053 * Check to see if we have any errors to inject here. We only
13054 * inject errors for commands that don't already have errors set.
13056 if (!STAILQ_EMPTY(&lun->error_list) &&
13057 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13058 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13059 ctl_inject_error(lun, io);
13062 * XXX KDM how do we treat commands that aren't completed
13065 * XXX KDM should we also track I/O latency?
13067 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13068 io->io_hdr.io_type == CTL_IO_SCSI) {
13074 bintime_sub(&bt, &io->io_hdr.start_bt);
13076 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13078 type = CTL_STATS_READ;
13079 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13081 type = CTL_STATS_WRITE;
13083 type = CTL_STATS_NO_IO;
13085 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13086 lun->stats.operations[type] ++;
13087 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13089 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13090 bintime_add(&lun->stats.time[type], &bt);
13093 mtx_lock(&port->port_lock);
13094 port->stats.bytes[type] += io->scsiio.kern_total_len;
13095 port->stats.operations[type] ++;
13096 port->stats.dmas[type] += io->io_hdr.num_dmas;
13098 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13099 bintime_add(&port->stats.time[type], &bt);
13101 mtx_unlock(&port->port_lock);
13105 * Run through the blocked queue of this I/O and see if anything
13106 * can be unblocked, now that this I/O is done and will be removed.
13107 * We need to do it before removal to have OOA position to start.
13109 ctl_try_unblock_others(lun, io, TRUE);
13112 * Remove this from the OOA queue.
13114 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13116 if (TAILQ_EMPTY(&lun->ooa_queue))
13117 lun->last_busy = getsbinuptime();
13121 * If the LUN has been invalidated, free it if there is nothing
13122 * left on its OOA queue.
13124 if ((lun->flags & CTL_LUN_INVALID)
13125 && TAILQ_EMPTY(&lun->ooa_queue)) {
13126 mtx_unlock(&lun->lun_lock);
13129 mtx_unlock(&lun->lun_lock);
13134 * If this command has been aborted, make sure we set the status
13135 * properly. The FETD is responsible for freeing the I/O and doing
13136 * whatever it needs to do to clean up its state.
13138 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13139 ctl_set_task_aborted(&io->scsiio);
13142 * If enabled, print command error status.
13144 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13145 (ctl_debug & CTL_DEBUG_INFO) != 0)
13146 ctl_io_error_print(io, NULL);
13149 * Tell the FETD or the other shelf controller we're done with this
13150 * command. Note that only SCSI commands get to this point. Task
13151 * management commands are completed above.
13153 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13154 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13155 memset(&msg, 0, sizeof(msg));
13156 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13157 msg.hdr.serializing_sc = io->io_hdr.remote_io;
13158 msg.hdr.nexus = io->io_hdr.nexus;
13159 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13160 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13168 * Front end should call this if it doesn't do autosense. When the request
13169 * sense comes back in from the initiator, we'll dequeue this and send it.
13172 ctl_queue_sense(union ctl_io *io)
13174 struct ctl_softc *softc = CTL_SOFTC(io);
13175 struct ctl_port *port = CTL_PORT(io);
13176 struct ctl_lun *lun;
13177 struct scsi_sense_data *ps;
13178 uint32_t initidx, p, targ_lun;
13180 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13182 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13185 * LUN lookup will likely move to the ctl_work_thread() once we
13186 * have our new queueing infrastructure (that doesn't put things on
13187 * a per-LUN queue initially). That is so that we can handle
13188 * things like an INQUIRY to a LUN that we don't have enabled. We
13189 * can't deal with that right now.
13190 * If we don't have a LUN for this, just toss the sense information.
13192 mtx_lock(&softc->ctl_lock);
13193 if (targ_lun >= ctl_max_luns ||
13194 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13195 mtx_unlock(&softc->ctl_lock);
13198 mtx_lock(&lun->lun_lock);
13199 mtx_unlock(&softc->ctl_lock);
13201 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13202 p = initidx / CTL_MAX_INIT_PER_PORT;
13203 if (lun->pending_sense[p] == NULL) {
13204 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13205 M_CTL, M_NOWAIT | M_ZERO);
13207 if ((ps = lun->pending_sense[p]) != NULL) {
13208 ps += initidx % CTL_MAX_INIT_PER_PORT;
13209 memset(ps, 0, sizeof(*ps));
13210 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13212 mtx_unlock(&lun->lun_lock);
13216 return (CTL_RETVAL_COMPLETE);
13220 * Primary command inlet from frontend ports. All SCSI and task I/O
13221 * requests must go through this function.
13224 ctl_queue(union ctl_io *io)
13226 struct ctl_port *port = CTL_PORT(io);
13228 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13231 io->io_hdr.start_time = time_uptime;
13232 getbinuptime(&io->io_hdr.start_bt);
13233 #endif /* CTL_TIME_IO */
13235 /* Map FE-specific LUN ID into global one. */
13236 io->io_hdr.nexus.targ_mapped_lun =
13237 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13239 switch (io->io_hdr.io_type) {
13242 if (ctl_debug & CTL_DEBUG_CDB)
13244 ctl_enqueue_incoming(io);
13247 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13251 return (CTL_RETVAL_COMPLETE);
13254 #ifdef CTL_IO_DELAY
13256 ctl_done_timer_wakeup(void *arg)
13260 io = (union ctl_io *)arg;
13263 #endif /* CTL_IO_DELAY */
13266 ctl_serseq_done(union ctl_io *io)
13268 struct ctl_lun *lun = CTL_LUN(io);;
13270 if (lun->be_lun == NULL ||
13271 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13273 mtx_lock(&lun->lun_lock);
13274 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13275 ctl_try_unblock_others(lun, io, FALSE);
13276 mtx_unlock(&lun->lun_lock);
13280 ctl_done(union ctl_io *io)
13284 * Enable this to catch duplicate completion issues.
13287 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13288 printf("%s: type %d msg %d cdb %x iptl: "
13289 "%u:%u:%u tag 0x%04x "
13290 "flag %#x status %x\n",
13292 io->io_hdr.io_type,
13293 io->io_hdr.msg_type,
13295 io->io_hdr.nexus.initid,
13296 io->io_hdr.nexus.targ_port,
13297 io->io_hdr.nexus.targ_lun,
13298 (io->io_hdr.io_type ==
13300 io->taskio.tag_num :
13301 io->scsiio.tag_num,
13303 io->io_hdr.status);
13305 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13309 * This is an internal copy of an I/O, and should not go through
13310 * the normal done processing logic.
13312 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13315 #ifdef CTL_IO_DELAY
13316 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13317 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13319 struct ctl_lun *lun = CTL_LUN(io);
13322 && (lun->delay_info.done_delay > 0)) {
13324 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13325 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13326 callout_reset(&io->io_hdr.delay_callout,
13327 lun->delay_info.done_delay * hz,
13328 ctl_done_timer_wakeup, io);
13329 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13330 lun->delay_info.done_delay = 0;
13334 #endif /* CTL_IO_DELAY */
13336 ctl_enqueue_done(io);
13340 ctl_work_thread(void *arg)
13342 struct ctl_thread *thr = (struct ctl_thread *)arg;
13343 struct ctl_softc *softc = thr->ctl_softc;
13347 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13348 thread_lock(curthread);
13349 sched_prio(curthread, PUSER - 1);
13350 thread_unlock(curthread);
13352 while (!softc->shutdown) {
13354 * We handle the queues in this order:
13356 * - done queue (to free up resources, unblock other commands)
13360 * If those queues are empty, we break out of the loop and
13363 mtx_lock(&thr->queue_lock);
13364 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13366 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13367 mtx_unlock(&thr->queue_lock);
13368 ctl_handle_isc(io);
13371 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13373 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13374 /* clear any blocked commands, call fe_done */
13375 mtx_unlock(&thr->queue_lock);
13376 ctl_process_done(io);
13379 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13381 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13382 mtx_unlock(&thr->queue_lock);
13383 if (io->io_hdr.io_type == CTL_IO_TASK)
13386 ctl_scsiio_precheck(softc, &io->scsiio);
13389 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13391 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13392 mtx_unlock(&thr->queue_lock);
13393 retval = ctl_scsiio(&io->scsiio);
13394 if (retval != CTL_RETVAL_COMPLETE)
13395 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13399 /* Sleep until we have something to do. */
13400 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0);
13402 thr->thread = NULL;
13407 ctl_lun_thread(void *arg)
13409 struct ctl_softc *softc = (struct ctl_softc *)arg;
13410 struct ctl_be_lun *be_lun;
13412 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13413 thread_lock(curthread);
13414 sched_prio(curthread, PUSER - 1);
13415 thread_unlock(curthread);
13417 while (!softc->shutdown) {
13418 mtx_lock(&softc->ctl_lock);
13419 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13420 if (be_lun != NULL) {
13421 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13422 mtx_unlock(&softc->ctl_lock);
13423 ctl_create_lun(be_lun);
13427 /* Sleep until we have something to do. */
13428 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13431 softc->lun_thread = NULL;
13436 ctl_thresh_thread(void *arg)
13438 struct ctl_softc *softc = (struct ctl_softc *)arg;
13439 struct ctl_lun *lun;
13440 struct ctl_logical_block_provisioning_page *page;
13442 union ctl_ha_msg msg;
13443 uint64_t thres, val;
13446 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13447 thread_lock(curthread);
13448 sched_prio(curthread, PUSER - 1);
13449 thread_unlock(curthread);
13451 while (!softc->shutdown) {
13452 mtx_lock(&softc->ctl_lock);
13453 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13454 if ((lun->flags & CTL_LUN_DISABLED) ||
13455 (lun->flags & CTL_LUN_NO_MEDIA) ||
13456 lun->backend->lun_attr == NULL)
13458 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13459 softc->ha_mode == CTL_HA_MODE_XFER)
13461 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13464 page = &lun->MODE_LBP;
13465 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13466 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13468 thres = scsi_4btoul(page->descr[i].count);
13469 thres <<= CTL_LBP_EXPONENT;
13470 switch (page->descr[i].resource) {
13472 attr = "blocksavail";
13475 attr = "blocksused";
13478 attr = "poolblocksavail";
13481 attr = "poolblocksused";
13486 mtx_unlock(&softc->ctl_lock); // XXX
13487 val = lun->backend->lun_attr(
13488 lun->be_lun->be_lun, attr);
13489 mtx_lock(&softc->ctl_lock);
13490 if (val == UINT64_MAX)
13492 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13493 == SLBPPD_ARMING_INC)
13494 e = (val >= thres);
13496 e = (val <= thres);
13500 mtx_lock(&lun->lun_lock);
13502 scsi_u64to8b((uint8_t *)&page->descr[i] -
13503 (uint8_t *)page, lun->ua_tpt_info);
13504 if (lun->lasttpt == 0 ||
13505 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13506 lun->lasttpt = time_uptime;
13507 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13513 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13516 mtx_unlock(&lun->lun_lock);
13518 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13519 /* Send msg to other side. */
13520 bzero(&msg.ua, sizeof(msg.ua));
13521 msg.hdr.msg_type = CTL_MSG_UA;
13522 msg.hdr.nexus.initid = -1;
13523 msg.hdr.nexus.targ_port = -1;
13524 msg.hdr.nexus.targ_lun = lun->lun;
13525 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13527 msg.ua.ua_set = (set > 0);
13528 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13529 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13530 mtx_unlock(&softc->ctl_lock); // XXX
13531 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13532 sizeof(msg.ua), M_WAITOK);
13533 mtx_lock(&softc->ctl_lock);
13536 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13537 PDROP, "-", CTL_LBP_PERIOD * hz);
13539 softc->thresh_thread = NULL;
13544 ctl_enqueue_incoming(union ctl_io *io)
13546 struct ctl_softc *softc = CTL_SOFTC(io);
13547 struct ctl_thread *thr;
13550 idx = (io->io_hdr.nexus.targ_port * 127 +
13551 io->io_hdr.nexus.initid) % worker_threads;
13552 thr = &softc->threads[idx];
13553 mtx_lock(&thr->queue_lock);
13554 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13555 mtx_unlock(&thr->queue_lock);
13560 ctl_enqueue_rtr(union ctl_io *io)
13562 struct ctl_softc *softc = CTL_SOFTC(io);
13563 struct ctl_thread *thr;
13565 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13566 mtx_lock(&thr->queue_lock);
13567 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13568 mtx_unlock(&thr->queue_lock);
13573 ctl_enqueue_done(union ctl_io *io)
13575 struct ctl_softc *softc = CTL_SOFTC(io);
13576 struct ctl_thread *thr;
13578 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13579 mtx_lock(&thr->queue_lock);
13580 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13581 mtx_unlock(&thr->queue_lock);
13586 ctl_enqueue_isc(union ctl_io *io)
13588 struct ctl_softc *softc = CTL_SOFTC(io);
13589 struct ctl_thread *thr;
13591 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13592 mtx_lock(&thr->queue_lock);
13593 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13594 mtx_unlock(&thr->queue_lock);