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, 0, "CAM Target Layer");
410 static int worker_threads = -1;
411 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, worker_threads, CTLFLAG_RDTUN,
412 &worker_threads, 1, "Number of worker threads");
413 static int ctl_debug = CTL_DEBUG_NONE;
414 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, debug, CTLFLAG_RWTUN,
415 &ctl_debug, 0, "Enabled debug flags");
416 static int ctl_lun_map_size = 1024;
417 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, lun_map_size, CTLFLAG_RWTUN,
418 &ctl_lun_map_size, 0, "Size of per-port LUN map (max LUN + 1)");
420 static int ctl_time_io_secs = CTL_TIME_IO_DEFAULT_SECS;
421 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, time_io_secs, CTLFLAG_RWTUN,
422 &ctl_time_io_secs, 0, "Log requests taking more seconds");
426 * Maximum number of LUNs we support. MUST be a power of 2.
428 #define CTL_DEFAULT_MAX_LUNS 1024
429 static int ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
430 TUNABLE_INT("kern.cam.ctl.max_luns", &ctl_max_luns);
431 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_luns, CTLFLAG_RDTUN,
432 &ctl_max_luns, CTL_DEFAULT_MAX_LUNS, "Maximum number of LUNs");
435 * Maximum number of ports registered at one time.
437 #define CTL_DEFAULT_MAX_PORTS 256
438 static int ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
439 TUNABLE_INT("kern.cam.ctl.max_ports", &ctl_max_ports);
440 SYSCTL_INT(_kern_cam_ctl, OID_AUTO, max_ports, CTLFLAG_RDTUN,
441 &ctl_max_ports, CTL_DEFAULT_MAX_LUNS, "Maximum number of ports");
444 * Maximum number of initiators we support.
446 #define CTL_MAX_INITIATORS (CTL_MAX_INIT_PER_PORT * ctl_max_ports)
449 * Supported pages (0x00), Serial number (0x80), Device ID (0x83),
450 * Extended INQUIRY Data (0x86), Mode Page Policy (0x87),
451 * SCSI Ports (0x88), Third-party Copy (0x8F), Block limits (0xB0),
452 * Block Device Characteristics (0xB1) and Logical Block Provisioning (0xB2)
454 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 10
456 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
458 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
459 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
460 static int ctl_init(void);
461 static int ctl_shutdown(void);
462 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
463 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
464 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
465 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
466 struct ctl_ooa *ooa_hdr,
467 struct ctl_ooa_entry *kern_entries);
468 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
470 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
471 struct ctl_be_lun *be_lun);
472 static int ctl_free_lun(struct ctl_lun *lun);
473 static void ctl_create_lun(struct ctl_be_lun *be_lun);
475 static int ctl_do_mode_select(union ctl_io *io);
476 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
477 uint64_t res_key, uint64_t sa_res_key,
478 uint8_t type, uint32_t residx,
479 struct ctl_scsiio *ctsio,
480 struct scsi_per_res_out *cdb,
481 struct scsi_per_res_out_parms* param);
482 static void ctl_pro_preempt_other(struct ctl_lun *lun,
483 union ctl_ha_msg *msg);
484 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
485 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
486 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
487 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
488 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
489 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
490 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
492 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
494 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
495 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
496 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
497 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
498 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
499 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
501 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
502 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
503 union ctl_io *pending_io, union ctl_io *ooa_io);
504 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
505 union ctl_io **starting_io);
506 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io,
508 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io,
510 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
511 const struct ctl_cmd_entry *entry,
512 struct ctl_scsiio *ctsio);
513 static void ctl_failover_lun(union ctl_io *io);
514 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
515 struct ctl_scsiio *ctsio);
516 static int ctl_scsiio(struct ctl_scsiio *ctsio);
518 static int ctl_target_reset(union ctl_io *io);
519 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
520 ctl_ua_type ua_type);
521 static int ctl_lun_reset(union ctl_io *io);
522 static int ctl_abort_task(union ctl_io *io);
523 static int ctl_abort_task_set(union ctl_io *io);
524 static int ctl_query_task(union ctl_io *io, int task_set);
525 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
526 ctl_ua_type ua_type);
527 static int ctl_i_t_nexus_reset(union ctl_io *io);
528 static int ctl_query_async_event(union ctl_io *io);
529 static void ctl_run_task(union ctl_io *io);
531 static void ctl_datamove_timer_wakeup(void *arg);
532 static void ctl_done_timer_wakeup(void *arg);
533 #endif /* CTL_IO_DELAY */
535 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
536 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
537 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
538 static void ctl_datamove_remote_write(union ctl_io *io);
539 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
540 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
541 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
542 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
543 ctl_ha_dt_cb callback);
544 static void ctl_datamove_remote_read(union ctl_io *io);
545 static void ctl_datamove_remote(union ctl_io *io);
546 static void ctl_process_done(union ctl_io *io);
547 static void ctl_lun_thread(void *arg);
548 static void ctl_thresh_thread(void *arg);
549 static void ctl_work_thread(void *arg);
550 static void ctl_enqueue_incoming(union ctl_io *io);
551 static void ctl_enqueue_rtr(union ctl_io *io);
552 static void ctl_enqueue_done(union ctl_io *io);
553 static void ctl_enqueue_isc(union ctl_io *io);
554 static const struct ctl_cmd_entry *
555 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
556 static const struct ctl_cmd_entry *
557 ctl_validate_command(struct ctl_scsiio *ctsio);
558 static int ctl_cmd_applicable(uint8_t lun_type,
559 const struct ctl_cmd_entry *entry);
560 static int ctl_ha_init(void);
561 static int ctl_ha_shutdown(void);
563 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
564 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
565 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
566 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
569 * Load the serialization table. This isn't very pretty, but is probably
570 * the easiest way to do it.
572 #include "ctl_ser_table.c"
575 * We only need to define open, close and ioctl routines for this driver.
577 static struct cdevsw ctl_cdevsw = {
578 .d_version = D_VERSION,
581 .d_close = ctl_close,
582 .d_ioctl = ctl_ioctl,
587 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
589 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
591 static moduledata_t ctl_moduledata = {
593 ctl_module_event_handler,
597 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
598 MODULE_VERSION(ctl, 1);
600 static struct ctl_frontend ha_frontend =
604 .shutdown = ctl_ha_shutdown,
610 struct ctl_softc *softc = control_softc;
612 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
613 &softc->othersc_pool) != 0)
615 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
616 ctl_pool_free(softc->othersc_pool);
619 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
620 != CTL_HA_STATUS_SUCCESS) {
621 ctl_ha_msg_destroy(softc);
622 ctl_pool_free(softc->othersc_pool);
629 ctl_ha_shutdown(void)
631 struct ctl_softc *softc = control_softc;
632 struct ctl_port *port;
634 ctl_ha_msg_shutdown(softc);
635 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
637 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
639 ctl_pool_free(softc->othersc_pool);
640 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
641 ctl_port_deregister(port);
642 free(port->port_name, M_CTL);
649 ctl_ha_datamove(union ctl_io *io)
651 struct ctl_lun *lun = CTL_LUN(io);
652 struct ctl_sg_entry *sgl;
653 union ctl_ha_msg msg;
654 uint32_t sg_entries_sent;
655 int do_sg_copy, i, j;
657 memset(&msg.dt, 0, sizeof(msg.dt));
658 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
659 msg.hdr.original_sc = io->io_hdr.remote_io;
660 msg.hdr.serializing_sc = io;
661 msg.hdr.nexus = io->io_hdr.nexus;
662 msg.hdr.status = io->io_hdr.status;
663 msg.dt.flags = io->io_hdr.flags;
666 * We convert everything into a S/G list here. We can't
667 * pass by reference, only by value between controllers.
668 * So we can't pass a pointer to the S/G list, only as many
669 * S/G entries as we can fit in here. If it's possible for
670 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
671 * then we need to break this up into multiple transfers.
673 if (io->scsiio.kern_sg_entries == 0) {
674 msg.dt.kern_sg_entries = 1;
676 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
677 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
679 /* XXX KDM use busdma here! */
680 msg.dt.sg_list[0].addr =
681 (void *)vtophys(io->scsiio.kern_data_ptr);
684 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
685 ("HA does not support BUS_ADDR"));
686 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
688 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
691 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
695 msg.dt.kern_data_len = io->scsiio.kern_data_len;
696 msg.dt.kern_total_len = io->scsiio.kern_total_len;
697 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
698 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
699 msg.dt.sg_sequence = 0;
702 * Loop until we've sent all of the S/G entries. On the
703 * other end, we'll recompose these S/G entries into one
704 * contiguous list before processing.
706 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
707 msg.dt.sg_sequence++) {
708 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
709 sizeof(msg.dt.sg_list[0])),
710 msg.dt.kern_sg_entries - sg_entries_sent);
711 if (do_sg_copy != 0) {
712 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
713 for (i = sg_entries_sent, j = 0;
714 i < msg.dt.cur_sg_entries; i++, j++) {
716 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
717 msg.dt.sg_list[j].addr = sgl[i].addr;
719 /* XXX KDM use busdma here! */
720 msg.dt.sg_list[j].addr =
721 (void *)vtophys(sgl[i].addr);
724 KASSERT((io->io_hdr.flags &
725 CTL_FLAG_BUS_ADDR) == 0,
726 ("HA does not support BUS_ADDR"));
727 msg.dt.sg_list[j].addr = sgl[i].addr;
729 msg.dt.sg_list[j].len = sgl[i].len;
733 sg_entries_sent += msg.dt.cur_sg_entries;
734 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
735 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
736 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
737 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
738 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
739 io->io_hdr.port_status = 31341;
740 io->scsiio.be_move_done(io);
743 msg.dt.sent_sg_entries = sg_entries_sent;
747 * Officially handover the request from us to peer.
748 * If failover has just happened, then we must return error.
749 * If failover happen just after, then it is not our problem.
752 mtx_lock(&lun->lun_lock);
753 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
755 mtx_unlock(&lun->lun_lock);
756 io->io_hdr.port_status = 31342;
757 io->scsiio.be_move_done(io);
760 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
761 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
763 mtx_unlock(&lun->lun_lock);
767 ctl_ha_done(union ctl_io *io)
769 union ctl_ha_msg msg;
771 if (io->io_hdr.io_type == CTL_IO_SCSI) {
772 memset(&msg, 0, sizeof(msg));
773 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
774 msg.hdr.original_sc = io->io_hdr.remote_io;
775 msg.hdr.nexus = io->io_hdr.nexus;
776 msg.hdr.status = io->io_hdr.status;
777 msg.scsi.scsi_status = io->scsiio.scsi_status;
778 msg.scsi.tag_num = io->scsiio.tag_num;
779 msg.scsi.tag_type = io->scsiio.tag_type;
780 msg.scsi.sense_len = io->scsiio.sense_len;
781 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
782 io->scsiio.sense_len);
783 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
784 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
785 msg.scsi.sense_len, M_WAITOK);
791 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
792 union ctl_ha_msg *msg_info)
794 struct ctl_scsiio *ctsio;
796 if (msg_info->hdr.original_sc == NULL) {
797 printf("%s: original_sc == NULL!\n", __func__);
798 /* XXX KDM now what? */
802 ctsio = &msg_info->hdr.original_sc->scsiio;
803 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
804 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
805 ctsio->io_hdr.status = msg_info->hdr.status;
806 ctsio->scsi_status = msg_info->scsi.scsi_status;
807 ctsio->sense_len = msg_info->scsi.sense_len;
808 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
809 msg_info->scsi.sense_len);
810 ctl_enqueue_isc((union ctl_io *)ctsio);
814 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
815 union ctl_ha_msg *msg_info)
817 struct ctl_scsiio *ctsio;
819 if (msg_info->hdr.serializing_sc == NULL) {
820 printf("%s: serializing_sc == NULL!\n", __func__);
821 /* XXX KDM now what? */
825 ctsio = &msg_info->hdr.serializing_sc->scsiio;
826 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
827 ctl_enqueue_isc((union ctl_io *)ctsio);
831 ctl_isc_announce_lun(struct ctl_lun *lun)
833 struct ctl_softc *softc = lun->ctl_softc;
834 union ctl_ha_msg *msg;
835 struct ctl_ha_msg_lun_pr_key pr_key;
838 if (softc->ha_link != CTL_HA_LINK_ONLINE)
840 mtx_lock(&lun->lun_lock);
841 i = sizeof(msg->lun);
843 i += lun->lun_devid->len;
844 i += sizeof(pr_key) * lun->pr_key_count;
846 mtx_unlock(&lun->lun_lock);
847 msg = malloc(i, M_CTL, M_WAITOK);
848 mtx_lock(&lun->lun_lock);
849 k = sizeof(msg->lun);
851 k += lun->lun_devid->len;
852 k += sizeof(pr_key) * lun->pr_key_count;
858 bzero(&msg->lun, sizeof(msg->lun));
859 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
860 msg->hdr.nexus.targ_lun = lun->lun;
861 msg->hdr.nexus.targ_mapped_lun = lun->lun;
862 msg->lun.flags = lun->flags;
863 msg->lun.pr_generation = lun->pr_generation;
864 msg->lun.pr_res_idx = lun->pr_res_idx;
865 msg->lun.pr_res_type = lun->pr_res_type;
866 msg->lun.pr_key_count = lun->pr_key_count;
868 if (lun->lun_devid) {
869 msg->lun.lun_devid_len = lun->lun_devid->len;
870 memcpy(&msg->lun.data[i], lun->lun_devid->data,
871 msg->lun.lun_devid_len);
872 i += msg->lun.lun_devid_len;
874 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
875 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
878 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
881 mtx_unlock(&lun->lun_lock);
882 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
886 if (lun->flags & CTL_LUN_PRIMARY_SC) {
887 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
888 ctl_isc_announce_mode(lun, -1,
889 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
890 lun->mode_pages.index[i].subpage);
896 ctl_isc_announce_port(struct ctl_port *port)
898 struct ctl_softc *softc = port->ctl_softc;
899 union ctl_ha_msg *msg;
902 if (port->targ_port < softc->port_min ||
903 port->targ_port >= softc->port_max ||
904 softc->ha_link != CTL_HA_LINK_ONLINE)
906 i = sizeof(msg->port) + strlen(port->port_name) + 1;
908 i += port->lun_map_size * sizeof(uint32_t);
909 if (port->port_devid)
910 i += port->port_devid->len;
911 if (port->target_devid)
912 i += port->target_devid->len;
913 if (port->init_devid)
914 i += port->init_devid->len;
915 msg = malloc(i, M_CTL, M_WAITOK);
916 bzero(&msg->port, sizeof(msg->port));
917 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
918 msg->hdr.nexus.targ_port = port->targ_port;
919 msg->port.port_type = port->port_type;
920 msg->port.physical_port = port->physical_port;
921 msg->port.virtual_port = port->virtual_port;
922 msg->port.status = port->status;
924 msg->port.name_len = sprintf(&msg->port.data[i],
925 "%d:%s", softc->ha_id, port->port_name) + 1;
926 i += msg->port.name_len;
928 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
929 memcpy(&msg->port.data[i], port->lun_map,
930 msg->port.lun_map_len);
931 i += msg->port.lun_map_len;
933 if (port->port_devid) {
934 msg->port.port_devid_len = port->port_devid->len;
935 memcpy(&msg->port.data[i], port->port_devid->data,
936 msg->port.port_devid_len);
937 i += msg->port.port_devid_len;
939 if (port->target_devid) {
940 msg->port.target_devid_len = port->target_devid->len;
941 memcpy(&msg->port.data[i], port->target_devid->data,
942 msg->port.target_devid_len);
943 i += msg->port.target_devid_len;
945 if (port->init_devid) {
946 msg->port.init_devid_len = port->init_devid->len;
947 memcpy(&msg->port.data[i], port->init_devid->data,
948 msg->port.init_devid_len);
949 i += msg->port.init_devid_len;
951 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
957 ctl_isc_announce_iid(struct ctl_port *port, int iid)
959 struct ctl_softc *softc = port->ctl_softc;
960 union ctl_ha_msg *msg;
963 if (port->targ_port < softc->port_min ||
964 port->targ_port >= softc->port_max ||
965 softc->ha_link != CTL_HA_LINK_ONLINE)
967 mtx_lock(&softc->ctl_lock);
968 i = sizeof(msg->iid);
970 if (port->wwpn_iid[iid].name)
971 l = strlen(port->wwpn_iid[iid].name) + 1;
973 msg = malloc(i, M_CTL, M_NOWAIT);
975 mtx_unlock(&softc->ctl_lock);
978 bzero(&msg->iid, sizeof(msg->iid));
979 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
980 msg->hdr.nexus.targ_port = port->targ_port;
981 msg->hdr.nexus.initid = iid;
982 msg->iid.in_use = port->wwpn_iid[iid].in_use;
983 msg->iid.name_len = l;
984 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
985 if (port->wwpn_iid[iid].name)
986 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
987 mtx_unlock(&softc->ctl_lock);
988 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
993 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
994 uint8_t page, uint8_t subpage)
996 struct ctl_softc *softc = lun->ctl_softc;
997 union ctl_ha_msg msg;
1000 if (softc->ha_link != CTL_HA_LINK_ONLINE)
1002 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1003 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1004 page && lun->mode_pages.index[i].subpage == subpage)
1007 if (i == CTL_NUM_MODE_PAGES)
1010 /* Don't try to replicate pages not present on this device. */
1011 if (lun->mode_pages.index[i].page_data == NULL)
1014 bzero(&msg.mode, sizeof(msg.mode));
1015 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
1016 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
1017 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
1018 msg.hdr.nexus.targ_lun = lun->lun;
1019 msg.hdr.nexus.targ_mapped_lun = lun->lun;
1020 msg.mode.page_code = page;
1021 msg.mode.subpage = subpage;
1022 msg.mode.page_len = lun->mode_pages.index[i].page_len;
1023 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
1025 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
1030 ctl_isc_ha_link_up(struct ctl_softc *softc)
1032 struct ctl_port *port;
1033 struct ctl_lun *lun;
1034 union ctl_ha_msg msg;
1037 /* Announce this node parameters to peer for validation. */
1038 msg.login.msg_type = CTL_MSG_LOGIN;
1039 msg.login.version = CTL_HA_VERSION;
1040 msg.login.ha_mode = softc->ha_mode;
1041 msg.login.ha_id = softc->ha_id;
1042 msg.login.max_luns = ctl_max_luns;
1043 msg.login.max_ports = ctl_max_ports;
1044 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1045 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1048 STAILQ_FOREACH(port, &softc->port_list, links) {
1049 ctl_isc_announce_port(port);
1050 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1051 if (port->wwpn_iid[i].in_use)
1052 ctl_isc_announce_iid(port, i);
1055 STAILQ_FOREACH(lun, &softc->lun_list, links)
1056 ctl_isc_announce_lun(lun);
1060 ctl_isc_ha_link_down(struct ctl_softc *softc)
1062 struct ctl_port *port;
1063 struct ctl_lun *lun;
1067 mtx_lock(&softc->ctl_lock);
1068 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1069 mtx_lock(&lun->lun_lock);
1070 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1071 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1072 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1074 mtx_unlock(&lun->lun_lock);
1076 mtx_unlock(&softc->ctl_lock);
1077 io = ctl_alloc_io(softc->othersc_pool);
1078 mtx_lock(&softc->ctl_lock);
1080 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1081 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1082 ctl_enqueue_isc(io);
1085 STAILQ_FOREACH(port, &softc->port_list, links) {
1086 if (port->targ_port >= softc->port_min &&
1087 port->targ_port < softc->port_max)
1089 port->status &= ~CTL_PORT_STATUS_ONLINE;
1090 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1091 port->wwpn_iid[i].in_use = 0;
1092 free(port->wwpn_iid[i].name, M_CTL);
1093 port->wwpn_iid[i].name = NULL;
1096 mtx_unlock(&softc->ctl_lock);
1100 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1102 struct ctl_lun *lun;
1103 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1105 mtx_lock(&softc->ctl_lock);
1106 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns ||
1107 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1108 mtx_unlock(&softc->ctl_lock);
1111 mtx_lock(&lun->lun_lock);
1112 mtx_unlock(&softc->ctl_lock);
1113 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1114 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1115 if (msg->ua.ua_all) {
1117 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1119 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1122 ctl_est_ua(lun, iid, msg->ua.ua_type);
1124 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1126 mtx_unlock(&lun->lun_lock);
1130 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1132 struct ctl_lun *lun;
1133 struct ctl_ha_msg_lun_pr_key pr_key;
1135 ctl_lun_flags oflags;
1138 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1139 mtx_lock(&softc->ctl_lock);
1140 if (targ_lun >= ctl_max_luns ||
1141 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1142 mtx_unlock(&softc->ctl_lock);
1145 mtx_lock(&lun->lun_lock);
1146 mtx_unlock(&softc->ctl_lock);
1147 if (lun->flags & CTL_LUN_DISABLED) {
1148 mtx_unlock(&lun->lun_lock);
1151 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1152 if (msg->lun.lun_devid_len != i || (i > 0 &&
1153 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1154 mtx_unlock(&lun->lun_lock);
1155 printf("%s: Received conflicting HA LUN %d\n",
1156 __func__, targ_lun);
1159 /* Record whether peer is primary. */
1160 oflags = lun->flags;
1161 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1162 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1163 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1165 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1166 if (oflags != lun->flags)
1167 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1169 /* If peer is primary and we are not -- use data */
1170 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1171 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1172 lun->pr_generation = msg->lun.pr_generation;
1173 lun->pr_res_idx = msg->lun.pr_res_idx;
1174 lun->pr_res_type = msg->lun.pr_res_type;
1175 lun->pr_key_count = msg->lun.pr_key_count;
1176 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1177 ctl_clr_prkey(lun, k);
1178 for (k = 0; k < msg->lun.pr_key_count; k++) {
1179 memcpy(&pr_key, &msg->lun.data[i],
1181 ctl_alloc_prkey(lun, pr_key.pr_iid);
1182 ctl_set_prkey(lun, pr_key.pr_iid,
1184 i += sizeof(pr_key);
1188 mtx_unlock(&lun->lun_lock);
1189 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1191 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1192 "primary" : "secondary"));
1194 /* If we are primary but peer doesn't know -- notify */
1195 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1196 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1197 ctl_isc_announce_lun(lun);
1202 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1204 struct ctl_port *port;
1205 struct ctl_lun *lun;
1208 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1210 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1211 msg->hdr.nexus.targ_port));
1213 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1214 port->frontend = &ha_frontend;
1215 port->targ_port = msg->hdr.nexus.targ_port;
1216 port->fe_datamove = ctl_ha_datamove;
1217 port->fe_done = ctl_ha_done;
1218 } else if (port->frontend == &ha_frontend) {
1219 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1220 msg->hdr.nexus.targ_port));
1223 printf("%s: Received conflicting HA port %d\n",
1224 __func__, msg->hdr.nexus.targ_port);
1227 port->port_type = msg->port.port_type;
1228 port->physical_port = msg->port.physical_port;
1229 port->virtual_port = msg->port.virtual_port;
1230 port->status = msg->port.status;
1232 free(port->port_name, M_CTL);
1233 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1235 i += msg->port.name_len;
1236 if (msg->port.lun_map_len != 0) {
1237 if (port->lun_map == NULL ||
1238 port->lun_map_size * sizeof(uint32_t) <
1239 msg->port.lun_map_len) {
1240 port->lun_map_size = 0;
1241 free(port->lun_map, M_CTL);
1242 port->lun_map = malloc(msg->port.lun_map_len,
1245 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1246 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1247 i += msg->port.lun_map_len;
1249 port->lun_map_size = 0;
1250 free(port->lun_map, M_CTL);
1251 port->lun_map = NULL;
1253 if (msg->port.port_devid_len != 0) {
1254 if (port->port_devid == NULL ||
1255 port->port_devid->len < msg->port.port_devid_len) {
1256 free(port->port_devid, M_CTL);
1257 port->port_devid = malloc(sizeof(struct ctl_devid) +
1258 msg->port.port_devid_len, M_CTL, M_WAITOK);
1260 memcpy(port->port_devid->data, &msg->port.data[i],
1261 msg->port.port_devid_len);
1262 port->port_devid->len = msg->port.port_devid_len;
1263 i += msg->port.port_devid_len;
1265 free(port->port_devid, M_CTL);
1266 port->port_devid = NULL;
1268 if (msg->port.target_devid_len != 0) {
1269 if (port->target_devid == NULL ||
1270 port->target_devid->len < msg->port.target_devid_len) {
1271 free(port->target_devid, M_CTL);
1272 port->target_devid = malloc(sizeof(struct ctl_devid) +
1273 msg->port.target_devid_len, M_CTL, M_WAITOK);
1275 memcpy(port->target_devid->data, &msg->port.data[i],
1276 msg->port.target_devid_len);
1277 port->target_devid->len = msg->port.target_devid_len;
1278 i += msg->port.target_devid_len;
1280 free(port->target_devid, M_CTL);
1281 port->target_devid = NULL;
1283 if (msg->port.init_devid_len != 0) {
1284 if (port->init_devid == NULL ||
1285 port->init_devid->len < msg->port.init_devid_len) {
1286 free(port->init_devid, M_CTL);
1287 port->init_devid = malloc(sizeof(struct ctl_devid) +
1288 msg->port.init_devid_len, M_CTL, M_WAITOK);
1290 memcpy(port->init_devid->data, &msg->port.data[i],
1291 msg->port.init_devid_len);
1292 port->init_devid->len = msg->port.init_devid_len;
1293 i += msg->port.init_devid_len;
1295 free(port->init_devid, M_CTL);
1296 port->init_devid = NULL;
1299 if (ctl_port_register(port) != 0) {
1300 printf("%s: ctl_port_register() failed with error\n",
1304 mtx_lock(&softc->ctl_lock);
1305 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1306 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1308 mtx_lock(&lun->lun_lock);
1309 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1310 mtx_unlock(&lun->lun_lock);
1312 mtx_unlock(&softc->ctl_lock);
1316 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1318 struct ctl_port *port;
1321 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1323 printf("%s: Received IID for unknown port %d\n",
1324 __func__, msg->hdr.nexus.targ_port);
1327 iid = msg->hdr.nexus.initid;
1328 if (port->wwpn_iid[iid].in_use != 0 &&
1329 msg->iid.in_use == 0)
1330 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1331 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1332 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1333 free(port->wwpn_iid[iid].name, M_CTL);
1334 if (msg->iid.name_len) {
1335 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1336 msg->iid.name_len, M_CTL);
1338 port->wwpn_iid[iid].name = NULL;
1342 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1345 if (msg->login.version != CTL_HA_VERSION) {
1346 printf("CTL HA peers have different versions %d != %d\n",
1347 msg->login.version, CTL_HA_VERSION);
1348 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1351 if (msg->login.ha_mode != softc->ha_mode) {
1352 printf("CTL HA peers have different ha_mode %d != %d\n",
1353 msg->login.ha_mode, softc->ha_mode);
1354 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1357 if (msg->login.ha_id == softc->ha_id) {
1358 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1359 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1362 if (msg->login.max_luns != ctl_max_luns ||
1363 msg->login.max_ports != ctl_max_ports ||
1364 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1365 printf("CTL HA peers have different limits\n");
1366 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1372 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1374 struct ctl_lun *lun;
1376 uint32_t initidx, targ_lun;
1378 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1379 mtx_lock(&softc->ctl_lock);
1380 if (targ_lun >= ctl_max_luns ||
1381 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1382 mtx_unlock(&softc->ctl_lock);
1385 mtx_lock(&lun->lun_lock);
1386 mtx_unlock(&softc->ctl_lock);
1387 if (lun->flags & CTL_LUN_DISABLED) {
1388 mtx_unlock(&lun->lun_lock);
1391 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1392 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1393 msg->mode.page_code &&
1394 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1397 if (i == CTL_NUM_MODE_PAGES) {
1398 mtx_unlock(&lun->lun_lock);
1401 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1402 lun->mode_pages.index[i].page_len);
1403 initidx = ctl_get_initindex(&msg->hdr.nexus);
1405 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1406 mtx_unlock(&lun->lun_lock);
1410 * ISC (Inter Shelf Communication) event handler. Events from the HA
1411 * subsystem come in here.
1414 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1416 struct ctl_softc *softc = control_softc;
1418 struct ctl_prio *presio;
1419 ctl_ha_status isc_status;
1421 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1422 if (event == CTL_HA_EVT_MSG_RECV) {
1423 union ctl_ha_msg *msg, msgbuf;
1425 if (param > sizeof(msgbuf))
1426 msg = malloc(param, M_CTL, M_WAITOK);
1429 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1431 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1432 printf("%s: Error receiving message: %d\n",
1433 __func__, isc_status);
1439 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1440 switch (msg->hdr.msg_type) {
1441 case CTL_MSG_SERIALIZE:
1442 io = ctl_alloc_io(softc->othersc_pool);
1444 // populate ctsio from msg
1445 io->io_hdr.io_type = CTL_IO_SCSI;
1446 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1447 io->io_hdr.remote_io = msg->hdr.original_sc;
1448 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1451 * If we're in serialization-only mode, we don't
1452 * want to go through full done processing. Thus
1455 * XXX KDM add another flag that is more specific.
1457 if (softc->ha_mode != CTL_HA_MODE_XFER)
1458 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1459 io->io_hdr.nexus = msg->hdr.nexus;
1460 io->scsiio.tag_num = msg->scsi.tag_num;
1461 io->scsiio.tag_type = msg->scsi.tag_type;
1463 io->io_hdr.start_time = time_uptime;
1464 getbinuptime(&io->io_hdr.start_bt);
1465 #endif /* CTL_TIME_IO */
1466 io->scsiio.cdb_len = msg->scsi.cdb_len;
1467 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1469 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1470 const struct ctl_cmd_entry *entry;
1472 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1473 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1475 entry->flags & CTL_FLAG_DATA_MASK;
1477 ctl_enqueue_isc(io);
1480 /* Performed on the Originating SC, XFER mode only */
1481 case CTL_MSG_DATAMOVE: {
1482 struct ctl_sg_entry *sgl;
1485 io = msg->hdr.original_sc;
1487 printf("%s: original_sc == NULL!\n", __func__);
1488 /* XXX KDM do something here */
1491 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1492 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1494 * Keep track of this, we need to send it back over
1495 * when the datamove is complete.
1497 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1498 if (msg->hdr.status == CTL_SUCCESS)
1499 io->io_hdr.status = msg->hdr.status;
1501 if (msg->dt.sg_sequence == 0) {
1503 getbinuptime(&io->io_hdr.dma_start_bt);
1505 i = msg->dt.kern_sg_entries +
1506 msg->dt.kern_data_len /
1507 CTL_HA_DATAMOVE_SEGMENT + 1;
1508 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1511 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries];
1513 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1515 io->scsiio.kern_sg_entries =
1516 msg->dt.kern_sg_entries;
1517 io->scsiio.rem_sg_entries =
1518 msg->dt.kern_sg_entries;
1519 io->scsiio.kern_data_len =
1520 msg->dt.kern_data_len;
1521 io->scsiio.kern_total_len =
1522 msg->dt.kern_total_len;
1523 io->scsiio.kern_data_resid =
1524 msg->dt.kern_data_resid;
1525 io->scsiio.kern_rel_offset =
1526 msg->dt.kern_rel_offset;
1527 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1528 io->io_hdr.flags |= msg->dt.flags &
1531 sgl = (struct ctl_sg_entry *)
1532 io->scsiio.kern_data_ptr;
1534 for (i = msg->dt.sent_sg_entries, j = 0;
1535 i < (msg->dt.sent_sg_entries +
1536 msg->dt.cur_sg_entries); i++, j++) {
1537 sgl[i].addr = msg->dt.sg_list[j].addr;
1538 sgl[i].len = msg->dt.sg_list[j].len;
1542 * If this is the last piece of the I/O, we've got
1543 * the full S/G list. Queue processing in the thread.
1544 * Otherwise wait for the next piece.
1546 if (msg->dt.sg_last != 0)
1547 ctl_enqueue_isc(io);
1550 /* Performed on the Serializing (primary) SC, XFER mode only */
1551 case CTL_MSG_DATAMOVE_DONE: {
1552 if (msg->hdr.serializing_sc == NULL) {
1553 printf("%s: serializing_sc == NULL!\n",
1555 /* XXX KDM now what? */
1559 * We grab the sense information here in case
1560 * there was a failure, so we can return status
1561 * back to the initiator.
1563 io = msg->hdr.serializing_sc;
1564 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1565 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1566 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1567 io->io_hdr.port_status = msg->scsi.port_status;
1568 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1569 if (msg->hdr.status != CTL_STATUS_NONE) {
1570 io->io_hdr.status = msg->hdr.status;
1571 io->scsiio.scsi_status = msg->scsi.scsi_status;
1572 io->scsiio.sense_len = msg->scsi.sense_len;
1573 memcpy(&io->scsiio.sense_data,
1574 &msg->scsi.sense_data,
1575 msg->scsi.sense_len);
1576 if (msg->hdr.status == CTL_SUCCESS)
1577 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1579 ctl_enqueue_isc(io);
1583 /* Preformed on Originating SC, SER_ONLY mode */
1585 io = msg->hdr.original_sc;
1587 printf("%s: original_sc == NULL!\n",
1591 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1592 io->io_hdr.msg_type = CTL_MSG_R2R;
1593 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1594 ctl_enqueue_isc(io);
1598 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1600 * Performed on the Originating (i.e. secondary) SC in XFER
1603 case CTL_MSG_FINISH_IO:
1604 if (softc->ha_mode == CTL_HA_MODE_XFER)
1605 ctl_isc_handler_finish_xfer(softc, msg);
1607 ctl_isc_handler_finish_ser_only(softc, msg);
1610 /* Preformed on Originating SC */
1611 case CTL_MSG_BAD_JUJU:
1612 io = msg->hdr.original_sc;
1614 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1618 ctl_copy_sense_data(msg, io);
1620 * IO should have already been cleaned up on other
1621 * SC so clear this flag so we won't send a message
1622 * back to finish the IO there.
1624 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1625 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1627 /* io = msg->hdr.serializing_sc; */
1628 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1629 ctl_enqueue_isc(io);
1632 /* Handle resets sent from the other side */
1633 case CTL_MSG_MANAGE_TASKS: {
1634 struct ctl_taskio *taskio;
1635 taskio = (struct ctl_taskio *)ctl_alloc_io(
1636 softc->othersc_pool);
1637 ctl_zero_io((union ctl_io *)taskio);
1638 taskio->io_hdr.io_type = CTL_IO_TASK;
1639 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1640 taskio->io_hdr.nexus = msg->hdr.nexus;
1641 taskio->task_action = msg->task.task_action;
1642 taskio->tag_num = msg->task.tag_num;
1643 taskio->tag_type = msg->task.tag_type;
1645 taskio->io_hdr.start_time = time_uptime;
1646 getbinuptime(&taskio->io_hdr.start_bt);
1647 #endif /* CTL_TIME_IO */
1648 ctl_run_task((union ctl_io *)taskio);
1651 /* Persistent Reserve action which needs attention */
1652 case CTL_MSG_PERS_ACTION:
1653 presio = (struct ctl_prio *)ctl_alloc_io(
1654 softc->othersc_pool);
1655 ctl_zero_io((union ctl_io *)presio);
1656 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1657 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1658 presio->io_hdr.nexus = msg->hdr.nexus;
1659 presio->pr_msg = msg->pr;
1660 ctl_enqueue_isc((union ctl_io *)presio);
1663 ctl_isc_ua(softc, msg, param);
1665 case CTL_MSG_PORT_SYNC:
1666 ctl_isc_port_sync(softc, msg, param);
1668 case CTL_MSG_LUN_SYNC:
1669 ctl_isc_lun_sync(softc, msg, param);
1671 case CTL_MSG_IID_SYNC:
1672 ctl_isc_iid_sync(softc, msg, param);
1675 ctl_isc_login(softc, msg, param);
1677 case CTL_MSG_MODE_SYNC:
1678 ctl_isc_mode_sync(softc, msg, param);
1681 printf("Received HA message of unknown type %d\n",
1683 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1688 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1689 printf("CTL: HA link status changed from %d to %d\n",
1690 softc->ha_link, param);
1691 if (param == softc->ha_link)
1693 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1694 softc->ha_link = param;
1695 ctl_isc_ha_link_down(softc);
1697 softc->ha_link = param;
1698 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1699 ctl_isc_ha_link_up(softc);
1703 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1709 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1712 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1713 src->scsi.sense_len);
1714 dest->scsiio.scsi_status = src->scsi.scsi_status;
1715 dest->scsiio.sense_len = src->scsi.sense_len;
1716 dest->io_hdr.status = src->hdr.status;
1720 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1723 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1724 src->scsiio.sense_len);
1725 dest->scsi.scsi_status = src->scsiio.scsi_status;
1726 dest->scsi.sense_len = src->scsiio.sense_len;
1727 dest->hdr.status = src->io_hdr.status;
1731 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1733 struct ctl_softc *softc = lun->ctl_softc;
1736 if (initidx < softc->init_min || initidx >= softc->init_max)
1738 mtx_assert(&lun->lun_lock, MA_OWNED);
1739 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1742 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1746 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1750 mtx_assert(&lun->lun_lock, MA_OWNED);
1751 if (lun->pending_ua[port] == NULL)
1753 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1754 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1756 lun->pending_ua[port][i] |= ua;
1761 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1763 struct ctl_softc *softc = lun->ctl_softc;
1766 mtx_assert(&lun->lun_lock, MA_OWNED);
1767 for (i = softc->port_min; i < softc->port_max; i++)
1768 ctl_est_ua_port(lun, i, except, ua);
1772 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1774 struct ctl_softc *softc = lun->ctl_softc;
1777 if (initidx < softc->init_min || initidx >= softc->init_max)
1779 mtx_assert(&lun->lun_lock, MA_OWNED);
1780 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1783 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1787 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1789 struct ctl_softc *softc = lun->ctl_softc;
1792 mtx_assert(&lun->lun_lock, MA_OWNED);
1793 for (i = softc->port_min; i < softc->port_max; i++) {
1794 if (lun->pending_ua[i] == NULL)
1796 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1797 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1799 lun->pending_ua[i][j] &= ~ua;
1805 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1806 ctl_ua_type ua_type)
1808 struct ctl_lun *lun;
1810 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1811 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1812 mtx_lock(&lun->lun_lock);
1813 ctl_clr_ua(lun, initidx, ua_type);
1814 mtx_unlock(&lun->lun_lock);
1819 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1821 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1822 struct ctl_lun *lun;
1823 struct ctl_lun_req ireq;
1826 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1827 error = sysctl_handle_int(oidp, &value, 0, req);
1828 if ((error != 0) || (req->newptr == NULL))
1831 mtx_lock(&softc->ctl_lock);
1833 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1835 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1836 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1837 mtx_unlock(&softc->ctl_lock);
1838 bzero(&ireq, sizeof(ireq));
1839 ireq.reqtype = CTL_LUNREQ_MODIFY;
1840 ireq.reqdata.modify.lun_id = lun->lun;
1841 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1843 if (ireq.status != CTL_LUN_OK) {
1844 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1845 __func__, ireq.status, ireq.error_str);
1847 mtx_lock(&softc->ctl_lock);
1849 mtx_unlock(&softc->ctl_lock);
1856 struct make_dev_args args;
1857 struct ctl_softc *softc;
1860 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1863 make_dev_args_init(&args);
1864 args.mda_devsw = &ctl_cdevsw;
1865 args.mda_uid = UID_ROOT;
1866 args.mda_gid = GID_OPERATOR;
1867 args.mda_mode = 0600;
1868 args.mda_si_drv1 = softc;
1869 args.mda_si_drv2 = NULL;
1870 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1872 free(softc, M_DEVBUF);
1873 control_softc = NULL;
1877 sysctl_ctx_init(&softc->sysctl_ctx);
1878 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1879 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1880 CTLFLAG_RD, 0, "CAM Target Layer");
1882 if (softc->sysctl_tree == NULL) {
1883 printf("%s: unable to allocate sysctl tree\n", __func__);
1884 destroy_dev(softc->dev);
1885 free(softc, M_DEVBUF);
1886 control_softc = NULL;
1890 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1891 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1892 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1895 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1896 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1897 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1899 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) {
1900 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n",
1901 ctl_max_luns, CTL_DEFAULT_MAX_LUNS);
1902 ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
1904 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns,
1905 M_DEVBUF, M_WAITOK | M_ZERO);
1906 softc->ctl_lun_mask = malloc(sizeof(uint32_t) *
1907 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1908 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) {
1909 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n",
1910 ctl_max_ports, CTL_DEFAULT_MAX_PORTS);
1911 ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
1913 softc->ctl_port_mask = malloc(sizeof(uint32_t) *
1914 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1915 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports,
1916 M_DEVBUF, M_WAITOK | M_ZERO);
1920 * In Copan's HA scheme, the "master" and "slave" roles are
1921 * figured out through the slot the controller is in. Although it
1922 * is an active/active system, someone has to be in charge.
1924 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1925 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1926 "HA head ID (0 - no HA)");
1927 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1928 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1929 softc->is_single = 1;
1930 softc->port_cnt = ctl_max_ports;
1931 softc->port_min = 0;
1933 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES;
1934 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1936 softc->port_max = softc->port_min + softc->port_cnt;
1937 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1938 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1940 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1941 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1942 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1944 STAILQ_INIT(&softc->lun_list);
1945 STAILQ_INIT(&softc->pending_lun_queue);
1946 STAILQ_INIT(&softc->fe_list);
1947 STAILQ_INIT(&softc->port_list);
1948 STAILQ_INIT(&softc->be_list);
1949 ctl_tpc_init(softc);
1951 if (worker_threads <= 0)
1952 worker_threads = max(1, mp_ncpus / 4);
1953 if (worker_threads > CTL_MAX_THREADS)
1954 worker_threads = CTL_MAX_THREADS;
1956 for (i = 0; i < worker_threads; i++) {
1957 struct ctl_thread *thr = &softc->threads[i];
1959 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1960 thr->ctl_softc = softc;
1961 STAILQ_INIT(&thr->incoming_queue);
1962 STAILQ_INIT(&thr->rtr_queue);
1963 STAILQ_INIT(&thr->done_queue);
1964 STAILQ_INIT(&thr->isc_queue);
1966 error = kproc_kthread_add(ctl_work_thread, thr,
1967 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1969 printf("error creating CTL work thread!\n");
1973 error = kproc_kthread_add(ctl_lun_thread, softc,
1974 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun");
1976 printf("error creating CTL lun thread!\n");
1979 error = kproc_kthread_add(ctl_thresh_thread, softc,
1980 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
1982 printf("error creating CTL threshold thread!\n");
1986 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1987 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1988 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1990 if (softc->is_single == 0) {
1991 if (ctl_frontend_register(&ha_frontend) != 0)
1992 softc->is_single = 1;
2000 struct ctl_softc *softc = control_softc;
2003 if (softc->is_single == 0)
2004 ctl_frontend_deregister(&ha_frontend);
2006 destroy_dev(softc->dev);
2008 /* Shutdown CTL threads. */
2009 softc->shutdown = 1;
2010 for (i = 0; i < worker_threads; i++) {
2011 struct ctl_thread *thr = &softc->threads[i];
2012 while (thr->thread != NULL) {
2014 if (thr->thread != NULL)
2015 pause("CTL thr shutdown", 1);
2017 mtx_destroy(&thr->queue_lock);
2019 while (softc->lun_thread != NULL) {
2020 wakeup(&softc->pending_lun_queue);
2021 if (softc->lun_thread != NULL)
2022 pause("CTL thr shutdown", 1);
2024 while (softc->thresh_thread != NULL) {
2025 wakeup(softc->thresh_thread);
2026 if (softc->thresh_thread != NULL)
2027 pause("CTL thr shutdown", 1);
2030 ctl_tpc_shutdown(softc);
2031 uma_zdestroy(softc->io_zone);
2032 mtx_destroy(&softc->ctl_lock);
2034 free(softc->ctl_luns, M_DEVBUF);
2035 free(softc->ctl_lun_mask, M_DEVBUF);
2036 free(softc->ctl_port_mask, M_DEVBUF);
2037 free(softc->ctl_ports, M_DEVBUF);
2039 sysctl_ctx_free(&softc->sysctl_ctx);
2041 free(softc, M_DEVBUF);
2042 control_softc = NULL;
2047 ctl_module_event_handler(module_t mod, int what, void *arg)
2052 return (ctl_init());
2054 return (ctl_shutdown());
2056 return (EOPNOTSUPP);
2061 * XXX KDM should we do some access checks here? Bump a reference count to
2062 * prevent a CTL module from being unloaded while someone has it open?
2065 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2071 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2077 * Remove an initiator by port number and initiator ID.
2078 * Returns 0 for success, -1 for failure.
2081 ctl_remove_initiator(struct ctl_port *port, int iid)
2083 struct ctl_softc *softc = port->ctl_softc;
2086 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2088 if (iid > CTL_MAX_INIT_PER_PORT) {
2089 printf("%s: initiator ID %u > maximun %u!\n",
2090 __func__, iid, CTL_MAX_INIT_PER_PORT);
2094 mtx_lock(&softc->ctl_lock);
2095 last = (--port->wwpn_iid[iid].in_use == 0);
2096 port->wwpn_iid[iid].last_use = time_uptime;
2097 mtx_unlock(&softc->ctl_lock);
2099 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2100 ctl_isc_announce_iid(port, iid);
2106 * Add an initiator to the initiator map.
2107 * Returns iid for success, < 0 for failure.
2110 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2112 struct ctl_softc *softc = port->ctl_softc;
2116 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2118 if (iid >= CTL_MAX_INIT_PER_PORT) {
2119 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2120 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2125 mtx_lock(&softc->ctl_lock);
2127 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2128 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2129 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2133 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2134 strcmp(name, port->wwpn_iid[i].name) == 0) {
2142 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2143 if (port->wwpn_iid[i].in_use == 0 &&
2144 port->wwpn_iid[i].wwpn == 0 &&
2145 port->wwpn_iid[i].name == NULL) {
2154 best_time = INT32_MAX;
2155 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2156 if (port->wwpn_iid[i].in_use == 0) {
2157 if (port->wwpn_iid[i].last_use < best_time) {
2159 best_time = port->wwpn_iid[i].last_use;
2167 mtx_unlock(&softc->ctl_lock);
2172 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2174 * This is not an error yet.
2176 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2178 printf("%s: port %d iid %u WWPN %#jx arrived"
2179 " again\n", __func__, port->targ_port,
2180 iid, (uintmax_t)wwpn);
2184 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2185 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2187 printf("%s: port %d iid %u name '%s' arrived"
2188 " again\n", __func__, port->targ_port,
2195 * This is an error, but what do we do about it? The
2196 * driver is telling us we have a new WWPN for this
2197 * initiator ID, so we pretty much need to use it.
2199 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2200 " but WWPN %#jx '%s' is still at that address\n",
2201 __func__, port->targ_port, iid, wwpn, name,
2202 (uintmax_t)port->wwpn_iid[iid].wwpn,
2203 port->wwpn_iid[iid].name);
2206 free(port->wwpn_iid[iid].name, M_CTL);
2207 port->wwpn_iid[iid].name = name;
2208 port->wwpn_iid[iid].wwpn = wwpn;
2209 port->wwpn_iid[iid].in_use++;
2210 mtx_unlock(&softc->ctl_lock);
2211 ctl_isc_announce_iid(port, iid);
2217 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2221 switch (port->port_type) {
2224 struct scsi_transportid_fcp *id =
2225 (struct scsi_transportid_fcp *)buf;
2226 if (port->wwpn_iid[iid].wwpn == 0)
2228 memset(id, 0, sizeof(*id));
2229 id->format_protocol = SCSI_PROTO_FC;
2230 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2231 return (sizeof(*id));
2233 case CTL_PORT_ISCSI:
2235 struct scsi_transportid_iscsi_port *id =
2236 (struct scsi_transportid_iscsi_port *)buf;
2237 if (port->wwpn_iid[iid].name == NULL)
2240 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2242 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2243 len = roundup2(min(len, 252), 4);
2244 scsi_ulto2b(len, id->additional_length);
2245 return (sizeof(*id) + len);
2249 struct scsi_transportid_sas *id =
2250 (struct scsi_transportid_sas *)buf;
2251 if (port->wwpn_iid[iid].wwpn == 0)
2253 memset(id, 0, sizeof(*id));
2254 id->format_protocol = SCSI_PROTO_SAS;
2255 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2256 return (sizeof(*id));
2260 struct scsi_transportid_spi *id =
2261 (struct scsi_transportid_spi *)buf;
2262 memset(id, 0, sizeof(*id));
2263 id->format_protocol = SCSI_PROTO_SPI;
2264 scsi_ulto2b(iid, id->scsi_addr);
2265 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2266 return (sizeof(*id));
2272 * Serialize a command that went down the "wrong" side, and so was sent to
2273 * this controller for execution. The logic is a little different than the
2274 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2275 * sent back to the other side, but in the success case, we execute the
2276 * command on this side (XFER mode) or tell the other side to execute it
2280 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2282 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2283 struct ctl_port *port = CTL_PORT(ctsio);
2284 union ctl_ha_msg msg_info;
2285 struct ctl_lun *lun;
2286 const struct ctl_cmd_entry *entry;
2290 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2292 /* Make sure that we know about this port. */
2293 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2294 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2299 /* Make sure that we know about this LUN. */
2300 mtx_lock(&softc->ctl_lock);
2301 if (targ_lun >= ctl_max_luns ||
2302 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2303 mtx_unlock(&softc->ctl_lock);
2306 * The other node would not send this request to us unless
2307 * received announce that we are primary node for this LUN.
2308 * If this LUN does not exist now, it is probably result of
2309 * a race, so respond to initiator in the most opaque way.
2311 ctl_set_busy(ctsio);
2314 mtx_lock(&lun->lun_lock);
2315 mtx_unlock(&softc->ctl_lock);
2318 * If the LUN is invalid, pretend that it doesn't exist.
2319 * It will go away as soon as all pending I/Os completed.
2321 if (lun->flags & CTL_LUN_DISABLED) {
2322 mtx_unlock(&lun->lun_lock);
2323 ctl_set_busy(ctsio);
2327 entry = ctl_get_cmd_entry(ctsio, NULL);
2328 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2329 mtx_unlock(&lun->lun_lock);
2333 CTL_LUN(ctsio) = lun;
2334 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2337 * Every I/O goes into the OOA queue for a
2338 * particular LUN, and stays there until completion.
2341 if (TAILQ_EMPTY(&lun->ooa_queue))
2342 lun->idle_time += getsbinuptime() - lun->last_busy;
2344 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2346 bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links);
2347 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
2348 case CTL_ACTION_BLOCK:
2349 ctsio->io_hdr.blocker = bio;
2350 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
2352 mtx_unlock(&lun->lun_lock);
2354 case CTL_ACTION_PASS:
2355 case CTL_ACTION_SKIP:
2356 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2357 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2358 ctl_enqueue_rtr((union ctl_io *)ctsio);
2359 mtx_unlock(&lun->lun_lock);
2361 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2362 mtx_unlock(&lun->lun_lock);
2364 /* send msg back to other side */
2365 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2366 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2367 msg_info.hdr.msg_type = CTL_MSG_R2R;
2368 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2369 sizeof(msg_info.hdr), M_WAITOK);
2372 case CTL_ACTION_OVERLAP:
2373 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2374 mtx_unlock(&lun->lun_lock);
2375 ctl_set_overlapped_cmd(ctsio);
2377 case CTL_ACTION_OVERLAP_TAG:
2378 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2379 mtx_unlock(&lun->lun_lock);
2380 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2382 case CTL_ACTION_ERROR:
2384 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2385 mtx_unlock(&lun->lun_lock);
2387 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2390 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2391 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2392 msg_info.hdr.serializing_sc = NULL;
2393 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2394 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2395 sizeof(msg_info.scsi), M_WAITOK);
2396 ctl_free_io((union ctl_io *)ctsio);
2402 * Returns 0 for success, errno for failure.
2405 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2406 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2410 mtx_lock(&lun->lun_lock);
2411 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2412 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2414 struct ctl_ooa_entry *entry;
2417 * If we've got more than we can fit, just count the
2418 * remaining entries.
2420 if (*cur_fill_num >= ooa_hdr->alloc_num)
2423 entry = &kern_entries[*cur_fill_num];
2425 entry->tag_num = io->scsiio.tag_num;
2426 entry->lun_num = lun->lun;
2428 entry->start_bt = io->io_hdr.start_bt;
2430 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2431 entry->cdb_len = io->scsiio.cdb_len;
2432 if (io->io_hdr.blocker != NULL)
2433 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2435 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2436 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2438 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2439 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2441 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2442 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2444 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2445 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2447 mtx_unlock(&lun->lun_lock);
2451 * Escape characters that are illegal or not recommended in XML.
2454 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2456 char *end = str + size;
2461 for (; *str && str < end; str++) {
2464 retval = sbuf_printf(sb, "&");
2467 retval = sbuf_printf(sb, ">");
2470 retval = sbuf_printf(sb, "<");
2473 retval = sbuf_putc(sb, *str);
2486 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2488 struct scsi_vpd_id_descriptor *desc;
2491 if (id == NULL || id->len < 4)
2493 desc = (struct scsi_vpd_id_descriptor *)id->data;
2494 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2495 case SVPD_ID_TYPE_T10:
2496 sbuf_printf(sb, "t10.");
2498 case SVPD_ID_TYPE_EUI64:
2499 sbuf_printf(sb, "eui.");
2501 case SVPD_ID_TYPE_NAA:
2502 sbuf_printf(sb, "naa.");
2504 case SVPD_ID_TYPE_SCSI_NAME:
2507 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2508 case SVPD_ID_CODESET_BINARY:
2509 for (i = 0; i < desc->length; i++)
2510 sbuf_printf(sb, "%02x", desc->identifier[i]);
2512 case SVPD_ID_CODESET_ASCII:
2513 sbuf_printf(sb, "%.*s", (int)desc->length,
2514 (char *)desc->identifier);
2516 case SVPD_ID_CODESET_UTF8:
2517 sbuf_printf(sb, "%s", (char *)desc->identifier);
2523 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2526 struct ctl_softc *softc = dev->si_drv1;
2527 struct ctl_port *port;
2528 struct ctl_lun *lun;
2535 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2537 case CTL_ENABLE_PORT:
2538 case CTL_DISABLE_PORT:
2539 case CTL_SET_PORT_WWNS: {
2540 struct ctl_port *port;
2541 struct ctl_port_entry *entry;
2543 entry = (struct ctl_port_entry *)addr;
2545 mtx_lock(&softc->ctl_lock);
2546 STAILQ_FOREACH(port, &softc->port_list, links) {
2549 if (port->targ_port < softc->port_min ||
2550 port->targ_port >= softc->port_max)
2555 if ((entry->port_type == CTL_PORT_NONE)
2556 && (entry->targ_port == port->targ_port)) {
2558 * If the user only wants to enable or
2559 * disable or set WWNs on a specific port,
2560 * do the operation and we're done.
2564 } else if (entry->port_type & port->port_type) {
2566 * Compare the user's type mask with the
2567 * particular frontend type to see if we
2574 * Make sure the user isn't trying to set
2575 * WWNs on multiple ports at the same time.
2577 if (cmd == CTL_SET_PORT_WWNS) {
2578 printf("%s: Can't set WWNs on "
2579 "multiple ports\n", __func__);
2588 * XXX KDM we have to drop the lock here, because
2589 * the online/offline operations can potentially
2590 * block. We need to reference count the frontends
2591 * so they can't go away,
2593 if (cmd == CTL_ENABLE_PORT) {
2594 mtx_unlock(&softc->ctl_lock);
2595 ctl_port_online(port);
2596 mtx_lock(&softc->ctl_lock);
2597 } else if (cmd == CTL_DISABLE_PORT) {
2598 mtx_unlock(&softc->ctl_lock);
2599 ctl_port_offline(port);
2600 mtx_lock(&softc->ctl_lock);
2601 } else if (cmd == CTL_SET_PORT_WWNS) {
2602 ctl_port_set_wwns(port,
2603 (entry->flags & CTL_PORT_WWNN_VALID) ?
2605 (entry->flags & CTL_PORT_WWPN_VALID) ?
2606 1 : 0, entry->wwpn);
2611 mtx_unlock(&softc->ctl_lock);
2615 struct ctl_ooa *ooa_hdr;
2616 struct ctl_ooa_entry *entries;
2617 uint32_t cur_fill_num;
2619 ooa_hdr = (struct ctl_ooa *)addr;
2621 if ((ooa_hdr->alloc_len == 0)
2622 || (ooa_hdr->alloc_num == 0)) {
2623 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2624 "must be non-zero\n", __func__,
2625 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2630 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2631 sizeof(struct ctl_ooa_entry))) {
2632 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2633 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2634 __func__, ooa_hdr->alloc_len,
2635 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2640 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2641 if (entries == NULL) {
2642 printf("%s: could not allocate %d bytes for OOA "
2643 "dump\n", __func__, ooa_hdr->alloc_len);
2648 mtx_lock(&softc->ctl_lock);
2649 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2650 (ooa_hdr->lun_num >= ctl_max_luns ||
2651 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2652 mtx_unlock(&softc->ctl_lock);
2653 free(entries, M_CTL);
2654 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2655 __func__, (uintmax_t)ooa_hdr->lun_num);
2662 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2663 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2664 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2668 lun = softc->ctl_luns[ooa_hdr->lun_num];
2669 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2672 mtx_unlock(&softc->ctl_lock);
2674 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2675 ooa_hdr->fill_len = ooa_hdr->fill_num *
2676 sizeof(struct ctl_ooa_entry);
2677 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2679 printf("%s: error copying out %d bytes for OOA dump\n",
2680 __func__, ooa_hdr->fill_len);
2683 getbinuptime(&ooa_hdr->cur_bt);
2685 if (cur_fill_num > ooa_hdr->alloc_num) {
2686 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2687 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2689 ooa_hdr->dropped_num = 0;
2690 ooa_hdr->status = CTL_OOA_OK;
2693 free(entries, M_CTL);
2696 case CTL_DELAY_IO: {
2697 struct ctl_io_delay_info *delay_info;
2699 delay_info = (struct ctl_io_delay_info *)addr;
2702 mtx_lock(&softc->ctl_lock);
2703 if (delay_info->lun_id >= ctl_max_luns ||
2704 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2705 mtx_unlock(&softc->ctl_lock);
2706 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2709 mtx_lock(&lun->lun_lock);
2710 mtx_unlock(&softc->ctl_lock);
2711 delay_info->status = CTL_DELAY_STATUS_OK;
2712 switch (delay_info->delay_type) {
2713 case CTL_DELAY_TYPE_CONT:
2714 case CTL_DELAY_TYPE_ONESHOT:
2717 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2720 switch (delay_info->delay_loc) {
2721 case CTL_DELAY_LOC_DATAMOVE:
2722 lun->delay_info.datamove_type = delay_info->delay_type;
2723 lun->delay_info.datamove_delay = delay_info->delay_secs;
2725 case CTL_DELAY_LOC_DONE:
2726 lun->delay_info.done_type = delay_info->delay_type;
2727 lun->delay_info.done_delay = delay_info->delay_secs;
2730 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2733 mtx_unlock(&lun->lun_lock);
2735 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2736 #endif /* CTL_IO_DELAY */
2739 case CTL_ERROR_INJECT: {
2740 struct ctl_error_desc *err_desc, *new_err_desc;
2742 err_desc = (struct ctl_error_desc *)addr;
2744 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2746 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2748 mtx_lock(&softc->ctl_lock);
2749 if (err_desc->lun_id >= ctl_max_luns ||
2750 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2751 mtx_unlock(&softc->ctl_lock);
2752 free(new_err_desc, M_CTL);
2753 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2754 __func__, (uintmax_t)err_desc->lun_id);
2758 mtx_lock(&lun->lun_lock);
2759 mtx_unlock(&softc->ctl_lock);
2762 * We could do some checking here to verify the validity
2763 * of the request, but given the complexity of error
2764 * injection requests, the checking logic would be fairly
2767 * For now, if the request is invalid, it just won't get
2768 * executed and might get deleted.
2770 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2773 * XXX KDM check to make sure the serial number is unique,
2774 * in case we somehow manage to wrap. That shouldn't
2775 * happen for a very long time, but it's the right thing to
2778 new_err_desc->serial = lun->error_serial;
2779 err_desc->serial = lun->error_serial;
2780 lun->error_serial++;
2782 mtx_unlock(&lun->lun_lock);
2785 case CTL_ERROR_INJECT_DELETE: {
2786 struct ctl_error_desc *delete_desc, *desc, *desc2;
2789 delete_desc = (struct ctl_error_desc *)addr;
2792 mtx_lock(&softc->ctl_lock);
2793 if (delete_desc->lun_id >= ctl_max_luns ||
2794 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2795 mtx_unlock(&softc->ctl_lock);
2796 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2797 __func__, (uintmax_t)delete_desc->lun_id);
2801 mtx_lock(&lun->lun_lock);
2802 mtx_unlock(&softc->ctl_lock);
2803 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2804 if (desc->serial != delete_desc->serial)
2807 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2812 mtx_unlock(&lun->lun_lock);
2813 if (delete_done == 0) {
2814 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2815 "error serial %ju on LUN %u\n", __func__,
2816 delete_desc->serial, delete_desc->lun_id);
2822 case CTL_DUMP_STRUCTS: {
2824 struct ctl_port *port;
2825 struct ctl_frontend *fe;
2827 mtx_lock(&softc->ctl_lock);
2828 printf("CTL Persistent Reservation information start:\n");
2829 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2830 mtx_lock(&lun->lun_lock);
2831 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2832 mtx_unlock(&lun->lun_lock);
2836 for (j = 0; j < ctl_max_ports; j++) {
2837 if (lun->pr_keys[j] == NULL)
2839 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2840 if (lun->pr_keys[j][k] == 0)
2842 printf(" LUN %ju port %d iid %d key "
2843 "%#jx\n", lun->lun, j, k,
2844 (uintmax_t)lun->pr_keys[j][k]);
2847 mtx_unlock(&lun->lun_lock);
2849 printf("CTL Persistent Reservation information end\n");
2850 printf("CTL Ports:\n");
2851 STAILQ_FOREACH(port, &softc->port_list, links) {
2852 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2853 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2854 port->frontend->name, port->port_type,
2855 port->physical_port, port->virtual_port,
2856 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2857 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2858 if (port->wwpn_iid[j].in_use == 0 &&
2859 port->wwpn_iid[j].wwpn == 0 &&
2860 port->wwpn_iid[j].name == NULL)
2863 printf(" iid %u use %d WWPN %#jx '%s'\n",
2864 j, port->wwpn_iid[j].in_use,
2865 (uintmax_t)port->wwpn_iid[j].wwpn,
2866 port->wwpn_iid[j].name);
2869 printf("CTL Port information end\n");
2870 mtx_unlock(&softc->ctl_lock);
2872 * XXX KDM calling this without a lock. We'd likely want
2873 * to drop the lock before calling the frontend's dump
2876 printf("CTL Frontends:\n");
2877 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2878 printf(" Frontend '%s'\n", fe->name);
2879 if (fe->fe_dump != NULL)
2882 printf("CTL Frontend information end\n");
2886 struct ctl_lun_req *lun_req;
2887 struct ctl_backend_driver *backend;
2889 nvlist_t *tmp_args_nvl;
2892 lun_req = (struct ctl_lun_req *)addr;
2893 tmp_args_nvl = lun_req->args_nvl;
2895 backend = ctl_backend_find(lun_req->backend);
2896 if (backend == NULL) {
2897 lun_req->status = CTL_LUN_ERROR;
2898 snprintf(lun_req->error_str,
2899 sizeof(lun_req->error_str),
2900 "Backend \"%s\" not found.",
2905 if (lun_req->args != NULL) {
2906 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK);
2907 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) {
2908 free(packed, M_CTL);
2909 lun_req->status = CTL_LUN_ERROR;
2910 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2911 "Cannot copyin args.");
2914 lun_req->args_nvl = nvlist_unpack(packed,
2915 lun_req->args_len, 0);
2916 free(packed, M_CTL);
2918 if (lun_req->args_nvl == NULL) {
2919 lun_req->status = CTL_LUN_ERROR;
2920 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2921 "Cannot unpack args nvlist.");
2925 lun_req->args_nvl = nvlist_create(0);
2927 retval = backend->ioctl(dev, cmd, addr, flag, td);
2928 nvlist_destroy(lun_req->args_nvl);
2929 lun_req->args_nvl = tmp_args_nvl;
2931 if (lun_req->result_nvl != NULL) {
2932 if (lun_req->result != NULL) {
2933 packed = nvlist_pack(lun_req->result_nvl,
2935 if (packed == NULL) {
2936 lun_req->status = CTL_LUN_ERROR;
2937 snprintf(lun_req->error_str,
2938 sizeof(lun_req->error_str),
2939 "Cannot pack result nvlist.");
2943 if (packed_len > lun_req->result_len) {
2944 lun_req->status = CTL_LUN_ERROR;
2945 snprintf(lun_req->error_str,
2946 sizeof(lun_req->error_str),
2947 "Result nvlist too large.");
2948 free(packed, M_NVLIST);
2952 if (copyout(packed, lun_req->result, packed_len)) {
2953 lun_req->status = CTL_LUN_ERROR;
2954 snprintf(lun_req->error_str,
2955 sizeof(lun_req->error_str),
2956 "Cannot copyout() the result.");
2957 free(packed, M_NVLIST);
2961 lun_req->result_len = packed_len;
2962 free(packed, M_NVLIST);
2965 nvlist_destroy(lun_req->result_nvl);
2969 case CTL_LUN_LIST: {
2971 struct ctl_lun_list *list;
2972 const char *name, *value;
2976 list = (struct ctl_lun_list *)addr;
2979 * Allocate a fixed length sbuf here, based on the length
2980 * of the user's buffer. We could allocate an auto-extending
2981 * buffer, and then tell the user how much larger our
2982 * amount of data is than his buffer, but that presents
2985 * 1. The sbuf(9) routines use a blocking malloc, and so
2986 * we can't hold a lock while calling them with an
2987 * auto-extending buffer.
2989 * 2. There is not currently a LUN reference counting
2990 * mechanism, outside of outstanding transactions on
2991 * the LUN's OOA queue. So a LUN could go away on us
2992 * while we're getting the LUN number, backend-specific
2993 * information, etc. Thus, given the way things
2994 * currently work, we need to hold the CTL lock while
2995 * grabbing LUN information.
2997 * So, from the user's standpoint, the best thing to do is
2998 * allocate what he thinks is a reasonable buffer length,
2999 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3000 * double the buffer length and try again. (And repeat
3001 * that until he succeeds.)
3003 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3005 list->status = CTL_LUN_LIST_ERROR;
3006 snprintf(list->error_str, sizeof(list->error_str),
3007 "Unable to allocate %d bytes for LUN list",
3012 sbuf_printf(sb, "<ctllunlist>\n");
3014 mtx_lock(&softc->ctl_lock);
3015 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3016 mtx_lock(&lun->lun_lock);
3017 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3018 (uintmax_t)lun->lun);
3021 * Bail out as soon as we see that we've overfilled
3027 retval = sbuf_printf(sb, "\t<backend_type>%s"
3028 "</backend_type>\n",
3029 (lun->backend == NULL) ? "none" :
3030 lun->backend->name);
3035 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3036 lun->be_lun->lun_type);
3041 if (lun->backend == NULL) {
3042 retval = sbuf_printf(sb, "</lun>\n");
3048 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3049 (lun->be_lun->maxlba > 0) ?
3050 lun->be_lun->maxlba + 1 : 0);
3055 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3056 lun->be_lun->blocksize);
3061 retval = sbuf_printf(sb, "\t<serial_number>");
3066 retval = ctl_sbuf_printf_esc(sb,
3067 lun->be_lun->serial_num,
3068 sizeof(lun->be_lun->serial_num));
3073 retval = sbuf_printf(sb, "</serial_number>\n");
3078 retval = sbuf_printf(sb, "\t<device_id>");
3083 retval = ctl_sbuf_printf_esc(sb,
3084 lun->be_lun->device_id,
3085 sizeof(lun->be_lun->device_id));
3090 retval = sbuf_printf(sb, "</device_id>\n");
3095 if (lun->backend->lun_info != NULL) {
3096 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3102 while ((name = nvlist_next(lun->be_lun->options, &type,
3103 &cookie)) != NULL) {
3104 sbuf_printf(sb, "\t<%s>", name);
3106 if (type == NV_TYPE_STRING) {
3107 value = dnvlist_get_string(
3108 lun->be_lun->options, name, NULL);
3110 sbuf_printf(sb, "%s", value);
3113 sbuf_printf(sb, "</%s>\n", name);
3116 retval = sbuf_printf(sb, "</lun>\n");
3120 mtx_unlock(&lun->lun_lock);
3123 mtx_unlock(&lun->lun_lock);
3124 mtx_unlock(&softc->ctl_lock);
3127 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3130 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3131 snprintf(list->error_str, sizeof(list->error_str),
3132 "Out of space, %d bytes is too small",
3139 retval = copyout(sbuf_data(sb), list->lun_xml,
3142 list->fill_len = sbuf_len(sb) + 1;
3143 list->status = CTL_LUN_LIST_OK;
3148 struct ctl_iscsi *ci;
3149 struct ctl_frontend *fe;
3151 ci = (struct ctl_iscsi *)addr;
3153 fe = ctl_frontend_find("iscsi");
3155 ci->status = CTL_ISCSI_ERROR;
3156 snprintf(ci->error_str, sizeof(ci->error_str),
3157 "Frontend \"iscsi\" not found.");
3161 retval = fe->ioctl(dev, cmd, addr, flag, td);
3164 case CTL_PORT_REQ: {
3165 struct ctl_req *req;
3166 struct ctl_frontend *fe;
3168 nvlist_t *tmp_args_nvl;
3171 req = (struct ctl_req *)addr;
3172 tmp_args_nvl = req->args_nvl;
3174 fe = ctl_frontend_find(req->driver);
3176 req->status = CTL_LUN_ERROR;
3177 snprintf(req->error_str, sizeof(req->error_str),
3178 "Frontend \"%s\" not found.", req->driver);
3182 if (req->args != NULL) {
3183 packed = malloc(req->args_len, M_CTL, M_WAITOK);
3184 if (copyin(req->args, packed, req->args_len) != 0) {
3185 free(packed, M_CTL);
3186 req->status = CTL_LUN_ERROR;
3187 snprintf(req->error_str, sizeof(req->error_str),
3188 "Cannot copyin args.");
3191 req->args_nvl = nvlist_unpack(packed,
3193 free(packed, M_CTL);
3195 if (req->args_nvl == NULL) {
3196 req->status = CTL_LUN_ERROR;
3197 snprintf(req->error_str, sizeof(req->error_str),
3198 "Cannot unpack args nvlist.");
3202 req->args_nvl = nvlist_create(0);
3205 retval = fe->ioctl(dev, cmd, addr, flag, td);
3209 nvlist_destroy(req->args_nvl);
3210 req->args_nvl = tmp_args_nvl;
3212 if (req->result_nvl != NULL) {
3213 if (req->result != NULL) {
3214 packed = nvlist_pack(req->result_nvl,
3216 if (packed == NULL) {
3217 req->status = CTL_LUN_ERROR;
3218 snprintf(req->error_str,
3219 sizeof(req->error_str),
3220 "Cannot pack result nvlist.");
3224 if (packed_len > req->result_len) {
3225 req->status = CTL_LUN_ERROR;
3226 snprintf(req->error_str,
3227 sizeof(req->error_str),
3228 "Result nvlist too large.");
3229 free(packed, M_NVLIST);
3233 if (copyout(packed, req->result, packed_len)) {
3234 req->status = CTL_LUN_ERROR;
3235 snprintf(req->error_str,
3236 sizeof(req->error_str),
3237 "Cannot copyout() the result.");
3238 free(packed, M_NVLIST);
3242 req->result_len = packed_len;
3243 free(packed, M_NVLIST);
3246 nvlist_destroy(req->result_nvl);
3250 case CTL_PORT_LIST: {
3252 struct ctl_port *port;
3253 struct ctl_lun_list *list;
3254 const char *name, *value;
3259 list = (struct ctl_lun_list *)addr;
3261 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3263 list->status = CTL_LUN_LIST_ERROR;
3264 snprintf(list->error_str, sizeof(list->error_str),
3265 "Unable to allocate %d bytes for LUN list",
3270 sbuf_printf(sb, "<ctlportlist>\n");
3272 mtx_lock(&softc->ctl_lock);
3273 STAILQ_FOREACH(port, &softc->port_list, links) {
3274 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3275 (uintmax_t)port->targ_port);
3278 * Bail out as soon as we see that we've overfilled
3284 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3285 "</frontend_type>\n", port->frontend->name);
3289 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3294 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3295 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3299 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3304 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3305 port->physical_port);
3309 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3310 port->virtual_port);
3314 if (port->target_devid != NULL) {
3315 sbuf_printf(sb, "\t<target>");
3316 ctl_id_sbuf(port->target_devid, sb);
3317 sbuf_printf(sb, "</target>\n");
3320 if (port->port_devid != NULL) {
3321 sbuf_printf(sb, "\t<port>");
3322 ctl_id_sbuf(port->port_devid, sb);
3323 sbuf_printf(sb, "</port>\n");
3326 if (port->port_info != NULL) {
3327 retval = port->port_info(port->onoff_arg, sb);
3333 while ((name = nvlist_next(port->options, &type,
3334 &cookie)) != NULL) {
3335 sbuf_printf(sb, "\t<%s>", name);
3337 if (type == NV_TYPE_STRING) {
3338 value = dnvlist_get_string(port->options,
3341 sbuf_printf(sb, "%s", value);
3344 sbuf_printf(sb, "</%s>\n", name);
3347 if (port->lun_map != NULL) {
3348 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3349 for (j = 0; j < port->lun_map_size; j++) {
3350 plun = ctl_lun_map_from_port(port, j);
3351 if (plun == UINT32_MAX)
3354 "\t<lun id=\"%u\">%u</lun>\n",
3359 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3360 if (port->wwpn_iid[j].in_use == 0 ||
3361 (port->wwpn_iid[j].wwpn == 0 &&
3362 port->wwpn_iid[j].name == NULL))
3365 if (port->wwpn_iid[j].name != NULL)
3366 retval = sbuf_printf(sb,
3367 "\t<initiator id=\"%u\">%s</initiator>\n",
3368 j, port->wwpn_iid[j].name);
3370 retval = sbuf_printf(sb,
3371 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3372 j, port->wwpn_iid[j].wwpn);
3379 retval = sbuf_printf(sb, "</targ_port>\n");
3383 mtx_unlock(&softc->ctl_lock);
3386 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3389 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3390 snprintf(list->error_str, sizeof(list->error_str),
3391 "Out of space, %d bytes is too small",
3398 retval = copyout(sbuf_data(sb), list->lun_xml,
3401 list->fill_len = sbuf_len(sb) + 1;
3402 list->status = CTL_LUN_LIST_OK;
3407 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3408 struct ctl_port *port;
3410 mtx_lock(&softc->ctl_lock);
3411 if (lm->port < softc->port_min ||
3412 lm->port >= softc->port_max ||
3413 (port = softc->ctl_ports[lm->port]) == NULL) {
3414 mtx_unlock(&softc->ctl_lock);
3417 if (port->status & CTL_PORT_STATUS_ONLINE) {
3418 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3419 if (ctl_lun_map_to_port(port, lun->lun) ==
3422 mtx_lock(&lun->lun_lock);
3423 ctl_est_ua_port(lun, lm->port, -1,
3425 mtx_unlock(&lun->lun_lock);
3428 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3429 if (lm->plun != UINT32_MAX) {
3430 if (lm->lun == UINT32_MAX)
3431 retval = ctl_lun_map_unset(port, lm->plun);
3432 else if (lm->lun < ctl_max_luns &&
3433 softc->ctl_luns[lm->lun] != NULL)
3434 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3438 if (lm->lun == UINT32_MAX)
3439 retval = ctl_lun_map_deinit(port);
3441 retval = ctl_lun_map_init(port);
3443 if (port->status & CTL_PORT_STATUS_ONLINE)
3444 ctl_isc_announce_port(port);
3447 case CTL_GET_LUN_STATS: {
3448 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3452 * XXX KDM no locking here. If the LUN list changes,
3453 * things can blow up.
3456 stats->status = CTL_SS_OK;
3457 stats->fill_len = 0;
3458 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3459 if (lun->lun < stats->first_item)
3461 if (stats->fill_len + sizeof(lun->stats) >
3463 stats->status = CTL_SS_NEED_MORE_SPACE;
3466 retval = copyout(&lun->stats, &stats->stats[i++],
3467 sizeof(lun->stats));
3470 stats->fill_len += sizeof(lun->stats);
3472 stats->num_items = softc->num_luns;
3473 stats->flags = CTL_STATS_FLAG_NONE;
3475 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3477 getnanouptime(&stats->timestamp);
3480 case CTL_GET_PORT_STATS: {
3481 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3485 * XXX KDM no locking here. If the LUN list changes,
3486 * things can blow up.
3489 stats->status = CTL_SS_OK;
3490 stats->fill_len = 0;
3491 STAILQ_FOREACH(port, &softc->port_list, links) {
3492 if (port->targ_port < stats->first_item)
3494 if (stats->fill_len + sizeof(port->stats) >
3496 stats->status = CTL_SS_NEED_MORE_SPACE;
3499 retval = copyout(&port->stats, &stats->stats[i++],
3500 sizeof(port->stats));
3503 stats->fill_len += sizeof(port->stats);
3505 stats->num_items = softc->num_ports;
3506 stats->flags = CTL_STATS_FLAG_NONE;
3508 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3510 getnanouptime(&stats->timestamp);
3514 /* XXX KDM should we fix this? */
3516 struct ctl_backend_driver *backend;
3523 * We encode the backend type as the ioctl type for backend
3524 * ioctls. So parse it out here, and then search for a
3525 * backend of this type.
3527 type = _IOC_TYPE(cmd);
3529 STAILQ_FOREACH(backend, &softc->be_list, links) {
3530 if (backend->type == type) {
3536 printf("ctl: unknown ioctl command %#lx or backend "
3541 retval = backend->ioctl(dev, cmd, addr, flag, td);
3551 ctl_get_initindex(struct ctl_nexus *nexus)
3553 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3557 ctl_lun_map_init(struct ctl_port *port)
3559 struct ctl_softc *softc = port->ctl_softc;
3560 struct ctl_lun *lun;
3561 int size = ctl_lun_map_size;
3564 if (port->lun_map == NULL || port->lun_map_size < size) {
3565 port->lun_map_size = 0;
3566 free(port->lun_map, M_CTL);
3567 port->lun_map = malloc(size * sizeof(uint32_t),
3570 if (port->lun_map == NULL)
3572 for (i = 0; i < size; i++)
3573 port->lun_map[i] = UINT32_MAX;
3574 port->lun_map_size = size;
3575 if (port->status & CTL_PORT_STATUS_ONLINE) {
3576 if (port->lun_disable != NULL) {
3577 STAILQ_FOREACH(lun, &softc->lun_list, links)
3578 port->lun_disable(port->targ_lun_arg, lun->lun);
3580 ctl_isc_announce_port(port);
3586 ctl_lun_map_deinit(struct ctl_port *port)
3588 struct ctl_softc *softc = port->ctl_softc;
3589 struct ctl_lun *lun;
3591 if (port->lun_map == NULL)
3593 port->lun_map_size = 0;
3594 free(port->lun_map, M_CTL);
3595 port->lun_map = NULL;
3596 if (port->status & CTL_PORT_STATUS_ONLINE) {
3597 if (port->lun_enable != NULL) {
3598 STAILQ_FOREACH(lun, &softc->lun_list, links)
3599 port->lun_enable(port->targ_lun_arg, lun->lun);
3601 ctl_isc_announce_port(port);
3607 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3612 if (port->lun_map == NULL) {
3613 status = ctl_lun_map_init(port);
3617 if (plun >= port->lun_map_size)
3619 old = port->lun_map[plun];
3620 port->lun_map[plun] = glun;
3621 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3622 if (port->lun_enable != NULL)
3623 port->lun_enable(port->targ_lun_arg, plun);
3624 ctl_isc_announce_port(port);
3630 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3634 if (port->lun_map == NULL || plun >= port->lun_map_size)
3636 old = port->lun_map[plun];
3637 port->lun_map[plun] = UINT32_MAX;
3638 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3639 if (port->lun_disable != NULL)
3640 port->lun_disable(port->targ_lun_arg, plun);
3641 ctl_isc_announce_port(port);
3647 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3651 return (UINT32_MAX);
3652 if (port->lun_map == NULL)
3654 if (lun_id > port->lun_map_size)
3655 return (UINT32_MAX);
3656 return (port->lun_map[lun_id]);
3660 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3665 return (UINT32_MAX);
3666 if (port->lun_map == NULL)
3668 for (i = 0; i < port->lun_map_size; i++) {
3669 if (port->lun_map[i] == lun_id)
3672 return (UINT32_MAX);
3676 ctl_decode_lun(uint64_t encoded)
3679 uint32_t result = 0xffffffff;
3681 be64enc(lun, encoded);
3682 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3683 case RPL_LUNDATA_ATYP_PERIPH:
3684 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3685 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3688 case RPL_LUNDATA_ATYP_FLAT:
3689 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3690 lun[6] == 0 && lun[7] == 0)
3691 result = ((lun[0] & 0x3f) << 8) + lun[1];
3693 case RPL_LUNDATA_ATYP_EXTLUN:
3694 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3696 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3701 result = (lun[1] << 16) + (lun[2] << 8) +
3705 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3706 result = (lun[2] << 24) +
3707 (lun[3] << 16) + (lun[4] << 8) +
3712 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3713 result = 0xffffffff;
3722 ctl_encode_lun(uint32_t decoded)
3724 uint64_t l = decoded;
3727 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3729 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3731 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3733 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3737 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3741 for (i = first; i < last; i++) {
3742 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3749 ctl_set_mask(uint32_t *mask, uint32_t bit)
3751 uint32_t chunk, piece;
3754 piece = bit % (sizeof(uint32_t) * 8);
3756 if ((mask[chunk] & (1 << piece)) != 0)
3759 mask[chunk] |= (1 << piece);
3765 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3767 uint32_t chunk, piece;
3770 piece = bit % (sizeof(uint32_t) * 8);
3772 if ((mask[chunk] & (1 << piece)) == 0)
3775 mask[chunk] &= ~(1 << piece);
3781 ctl_is_set(uint32_t *mask, uint32_t bit)
3783 uint32_t chunk, piece;
3786 piece = bit % (sizeof(uint32_t) * 8);
3788 if ((mask[chunk] & (1 << piece)) == 0)
3795 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3799 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3802 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3806 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3810 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3813 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3817 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3822 i = residx/CTL_MAX_INIT_PER_PORT;
3823 if (lun->pr_keys[i] != NULL)
3825 mtx_unlock(&lun->lun_lock);
3826 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3828 mtx_lock(&lun->lun_lock);
3829 if (lun->pr_keys[i] == NULL)
3830 lun->pr_keys[i] = p;
3836 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3840 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3841 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3842 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3846 * ctl_softc, pool_name, total_ctl_io are passed in.
3847 * npool is passed out.
3850 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3851 uint32_t total_ctl_io, void **npool)
3853 struct ctl_io_pool *pool;
3855 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3860 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3861 pool->ctl_softc = ctl_softc;
3863 pool->zone = uma_zsecond_create(pool->name, NULL,
3864 NULL, NULL, NULL, ctl_softc->io_zone);
3865 /* uma_prealloc(pool->zone, total_ctl_io); */
3867 pool->zone = ctl_softc->io_zone;
3875 ctl_pool_free(struct ctl_io_pool *pool)
3882 uma_zdestroy(pool->zone);
3888 ctl_alloc_io(void *pool_ref)
3890 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3893 io = uma_zalloc(pool->zone, M_WAITOK);
3895 io->io_hdr.pool = pool_ref;
3896 CTL_SOFTC(io) = pool->ctl_softc;
3897 TAILQ_INIT(&io->io_hdr.blocked_queue);
3903 ctl_alloc_io_nowait(void *pool_ref)
3905 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3908 io = uma_zalloc(pool->zone, M_NOWAIT);
3910 io->io_hdr.pool = pool_ref;
3911 CTL_SOFTC(io) = pool->ctl_softc;
3912 TAILQ_INIT(&io->io_hdr.blocked_queue);
3918 ctl_free_io(union ctl_io *io)
3920 struct ctl_io_pool *pool;
3925 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3926 uma_zfree(pool->zone, io);
3930 ctl_zero_io(union ctl_io *io)
3932 struct ctl_io_pool *pool;
3938 * May need to preserve linked list pointers at some point too.
3940 pool = io->io_hdr.pool;
3941 memset(io, 0, sizeof(*io));
3942 io->io_hdr.pool = pool;
3943 CTL_SOFTC(io) = pool->ctl_softc;
3944 TAILQ_INIT(&io->io_hdr.blocked_queue);
3948 ctl_expand_number(const char *buf, uint64_t *num)
3954 number = strtoq(buf, &endptr, 0);
3956 switch (tolower((unsigned char)*endptr)) {
3976 case '\0': /* No unit. */
3980 /* Unrecognized unit. */
3984 if ((number << shift) >> shift != number) {
3988 *num = number << shift;
3994 * This routine could be used in the future to load default and/or saved
3995 * mode page parameters for a particuar lun.
3998 ctl_init_page_index(struct ctl_lun *lun)
4001 struct ctl_page_index *page_index;
4005 memcpy(&lun->mode_pages.index, page_index_template,
4006 sizeof(page_index_template));
4008 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4010 page_index = &lun->mode_pages.index[i];
4011 if (lun->be_lun->lun_type == T_DIRECT &&
4012 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4014 if (lun->be_lun->lun_type == T_PROCESSOR &&
4015 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4017 if (lun->be_lun->lun_type == T_CDROM &&
4018 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4021 page_code = page_index->page_code & SMPH_PC_MASK;
4022 switch (page_code) {
4023 case SMS_RW_ERROR_RECOVERY_PAGE: {
4024 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4025 ("subpage %#x for page %#x is incorrect!",
4026 page_index->subpage, page_code));
4027 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4028 &rw_er_page_default,
4029 sizeof(rw_er_page_default));
4030 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4031 &rw_er_page_changeable,
4032 sizeof(rw_er_page_changeable));
4033 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4034 &rw_er_page_default,
4035 sizeof(rw_er_page_default));
4036 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4037 &rw_er_page_default,
4038 sizeof(rw_er_page_default));
4039 page_index->page_data =
4040 (uint8_t *)lun->mode_pages.rw_er_page;
4043 case SMS_FORMAT_DEVICE_PAGE: {
4044 struct scsi_format_page *format_page;
4046 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4047 ("subpage %#x for page %#x is incorrect!",
4048 page_index->subpage, page_code));
4051 * Sectors per track are set above. Bytes per
4052 * sector need to be set here on a per-LUN basis.
4054 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4055 &format_page_default,
4056 sizeof(format_page_default));
4057 memcpy(&lun->mode_pages.format_page[
4058 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4059 sizeof(format_page_changeable));
4060 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4061 &format_page_default,
4062 sizeof(format_page_default));
4063 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4064 &format_page_default,
4065 sizeof(format_page_default));
4067 format_page = &lun->mode_pages.format_page[
4069 scsi_ulto2b(lun->be_lun->blocksize,
4070 format_page->bytes_per_sector);
4072 format_page = &lun->mode_pages.format_page[
4074 scsi_ulto2b(lun->be_lun->blocksize,
4075 format_page->bytes_per_sector);
4077 format_page = &lun->mode_pages.format_page[
4079 scsi_ulto2b(lun->be_lun->blocksize,
4080 format_page->bytes_per_sector);
4082 page_index->page_data =
4083 (uint8_t *)lun->mode_pages.format_page;
4086 case SMS_RIGID_DISK_PAGE: {
4087 struct scsi_rigid_disk_page *rigid_disk_page;
4088 uint32_t sectors_per_cylinder;
4092 #endif /* !__XSCALE__ */
4094 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4095 ("subpage %#x for page %#x is incorrect!",
4096 page_index->subpage, page_code));
4099 * Rotation rate and sectors per track are set
4100 * above. We calculate the cylinders here based on
4101 * capacity. Due to the number of heads and
4102 * sectors per track we're using, smaller arrays
4103 * may turn out to have 0 cylinders. Linux and
4104 * FreeBSD don't pay attention to these mode pages
4105 * to figure out capacity, but Solaris does. It
4106 * seems to deal with 0 cylinders just fine, and
4107 * works out a fake geometry based on the capacity.
4109 memcpy(&lun->mode_pages.rigid_disk_page[
4110 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4111 sizeof(rigid_disk_page_default));
4112 memcpy(&lun->mode_pages.rigid_disk_page[
4113 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4114 sizeof(rigid_disk_page_changeable));
4116 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4120 * The divide method here will be more accurate,
4121 * probably, but results in floating point being
4122 * used in the kernel on i386 (__udivdi3()). On the
4123 * XScale, though, __udivdi3() is implemented in
4126 * The shift method for cylinder calculation is
4127 * accurate if sectors_per_cylinder is a power of
4128 * 2. Otherwise it might be slightly off -- you
4129 * might have a bit of a truncation problem.
4132 cylinders = (lun->be_lun->maxlba + 1) /
4133 sectors_per_cylinder;
4135 for (shift = 31; shift > 0; shift--) {
4136 if (sectors_per_cylinder & (1 << shift))
4139 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4143 * We've basically got 3 bytes, or 24 bits for the
4144 * cylinder size in the mode page. If we're over,
4145 * just round down to 2^24.
4147 if (cylinders > 0xffffff)
4148 cylinders = 0xffffff;
4150 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4152 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4154 if ((value = dnvlist_get_string(lun->be_lun->options,
4155 "rpm", NULL)) != NULL) {
4156 scsi_ulto2b(strtol(value, NULL, 0),
4157 rigid_disk_page->rotation_rate);
4160 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4161 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4162 sizeof(rigid_disk_page_default));
4163 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4164 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4165 sizeof(rigid_disk_page_default));
4167 page_index->page_data =
4168 (uint8_t *)lun->mode_pages.rigid_disk_page;
4171 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4172 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4173 ("subpage %#x for page %#x is incorrect!",
4174 page_index->subpage, page_code));
4175 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4176 &verify_er_page_default,
4177 sizeof(verify_er_page_default));
4178 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4179 &verify_er_page_changeable,
4180 sizeof(verify_er_page_changeable));
4181 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4182 &verify_er_page_default,
4183 sizeof(verify_er_page_default));
4184 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4185 &verify_er_page_default,
4186 sizeof(verify_er_page_default));
4187 page_index->page_data =
4188 (uint8_t *)lun->mode_pages.verify_er_page;
4191 case SMS_CACHING_PAGE: {
4192 struct scsi_caching_page *caching_page;
4194 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4195 ("subpage %#x for page %#x is incorrect!",
4196 page_index->subpage, page_code));
4197 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4198 &caching_page_default,
4199 sizeof(caching_page_default));
4200 memcpy(&lun->mode_pages.caching_page[
4201 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4202 sizeof(caching_page_changeable));
4203 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4204 &caching_page_default,
4205 sizeof(caching_page_default));
4206 caching_page = &lun->mode_pages.caching_page[
4208 value = dnvlist_get_string(lun->be_lun->options,
4209 "writecache", NULL);
4210 if (value != NULL && strcmp(value, "off") == 0)
4211 caching_page->flags1 &= ~SCP_WCE;
4212 value = dnvlist_get_string(lun->be_lun->options,
4214 if (value != NULL && strcmp(value, "off") == 0)
4215 caching_page->flags1 |= SCP_RCD;
4216 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4217 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4218 sizeof(caching_page_default));
4219 page_index->page_data =
4220 (uint8_t *)lun->mode_pages.caching_page;
4223 case SMS_CONTROL_MODE_PAGE: {
4224 switch (page_index->subpage) {
4225 case SMS_SUBPAGE_PAGE_0: {
4226 struct scsi_control_page *control_page;
4228 memcpy(&lun->mode_pages.control_page[
4230 &control_page_default,
4231 sizeof(control_page_default));
4232 memcpy(&lun->mode_pages.control_page[
4233 CTL_PAGE_CHANGEABLE],
4234 &control_page_changeable,
4235 sizeof(control_page_changeable));
4236 memcpy(&lun->mode_pages.control_page[
4238 &control_page_default,
4239 sizeof(control_page_default));
4240 control_page = &lun->mode_pages.control_page[
4242 value = dnvlist_get_string(lun->be_lun->options,
4243 "reordering", NULL);
4244 if (value != NULL &&
4245 strcmp(value, "unrestricted") == 0) {
4246 control_page->queue_flags &=
4247 ~SCP_QUEUE_ALG_MASK;
4248 control_page->queue_flags |=
4249 SCP_QUEUE_ALG_UNRESTRICTED;
4251 memcpy(&lun->mode_pages.control_page[
4253 &lun->mode_pages.control_page[
4255 sizeof(control_page_default));
4256 page_index->page_data =
4257 (uint8_t *)lun->mode_pages.control_page;
4261 memcpy(&lun->mode_pages.control_ext_page[
4263 &control_ext_page_default,
4264 sizeof(control_ext_page_default));
4265 memcpy(&lun->mode_pages.control_ext_page[
4266 CTL_PAGE_CHANGEABLE],
4267 &control_ext_page_changeable,
4268 sizeof(control_ext_page_changeable));
4269 memcpy(&lun->mode_pages.control_ext_page[
4271 &control_ext_page_default,
4272 sizeof(control_ext_page_default));
4273 memcpy(&lun->mode_pages.control_ext_page[
4275 &lun->mode_pages.control_ext_page[
4277 sizeof(control_ext_page_default));
4278 page_index->page_data =
4279 (uint8_t *)lun->mode_pages.control_ext_page;
4282 panic("subpage %#x for page %#x is incorrect!",
4283 page_index->subpage, page_code);
4287 case SMS_INFO_EXCEPTIONS_PAGE: {
4288 switch (page_index->subpage) {
4289 case SMS_SUBPAGE_PAGE_0:
4290 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4292 sizeof(ie_page_default));
4293 memcpy(&lun->mode_pages.ie_page[
4294 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4295 sizeof(ie_page_changeable));
4296 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4298 sizeof(ie_page_default));
4299 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4301 sizeof(ie_page_default));
4302 page_index->page_data =
4303 (uint8_t *)lun->mode_pages.ie_page;
4306 struct ctl_logical_block_provisioning_page *page;
4308 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4310 sizeof(lbp_page_default));
4311 memcpy(&lun->mode_pages.lbp_page[
4312 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4313 sizeof(lbp_page_changeable));
4314 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4316 sizeof(lbp_page_default));
4317 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4318 value = dnvlist_get_string(lun->be_lun->options,
4319 "avail-threshold", NULL);
4320 if (value != NULL &&
4321 ctl_expand_number(value, &ival) == 0) {
4322 page->descr[0].flags |= SLBPPD_ENABLED |
4324 if (lun->be_lun->blocksize)
4325 ival /= lun->be_lun->blocksize;
4328 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4329 page->descr[0].count);
4331 value = dnvlist_get_string(lun->be_lun->options,
4332 "used-threshold", NULL);
4333 if (value != NULL &&
4334 ctl_expand_number(value, &ival) == 0) {
4335 page->descr[1].flags |= SLBPPD_ENABLED |
4337 if (lun->be_lun->blocksize)
4338 ival /= lun->be_lun->blocksize;
4341 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4342 page->descr[1].count);
4344 value = dnvlist_get_string(lun->be_lun->options,
4345 "pool-avail-threshold", NULL);
4346 if (value != NULL &&
4347 ctl_expand_number(value, &ival) == 0) {
4348 page->descr[2].flags |= SLBPPD_ENABLED |
4350 if (lun->be_lun->blocksize)
4351 ival /= lun->be_lun->blocksize;
4354 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4355 page->descr[2].count);
4357 value = dnvlist_get_string(lun->be_lun->options,
4358 "pool-used-threshold", NULL);
4359 if (value != NULL &&
4360 ctl_expand_number(value, &ival) == 0) {
4361 page->descr[3].flags |= SLBPPD_ENABLED |
4363 if (lun->be_lun->blocksize)
4364 ival /= lun->be_lun->blocksize;
4367 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4368 page->descr[3].count);
4370 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4371 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4372 sizeof(lbp_page_default));
4373 page_index->page_data =
4374 (uint8_t *)lun->mode_pages.lbp_page;
4378 panic("subpage %#x for page %#x is incorrect!",
4379 page_index->subpage, page_code);
4383 case SMS_CDDVD_CAPS_PAGE:{
4384 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4385 ("subpage %#x for page %#x is incorrect!",
4386 page_index->subpage, page_code));
4387 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4388 &cddvd_page_default,
4389 sizeof(cddvd_page_default));
4390 memcpy(&lun->mode_pages.cddvd_page[
4391 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4392 sizeof(cddvd_page_changeable));
4393 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4394 &cddvd_page_default,
4395 sizeof(cddvd_page_default));
4396 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4397 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4398 sizeof(cddvd_page_default));
4399 page_index->page_data =
4400 (uint8_t *)lun->mode_pages.cddvd_page;
4404 panic("invalid page code value %#x", page_code);
4408 return (CTL_RETVAL_COMPLETE);
4412 ctl_init_log_page_index(struct ctl_lun *lun)
4414 struct ctl_page_index *page_index;
4417 memcpy(&lun->log_pages.index, log_page_index_template,
4418 sizeof(log_page_index_template));
4421 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4423 page_index = &lun->log_pages.index[i];
4424 if (lun->be_lun->lun_type == T_DIRECT &&
4425 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4427 if (lun->be_lun->lun_type == T_PROCESSOR &&
4428 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4430 if (lun->be_lun->lun_type == T_CDROM &&
4431 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4434 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4435 lun->backend->lun_attr == NULL)
4438 if (page_index->page_code != prev) {
4439 lun->log_pages.pages_page[j] = page_index->page_code;
4440 prev = page_index->page_code;
4443 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4444 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4447 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4448 lun->log_pages.index[0].page_len = j;
4449 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4450 lun->log_pages.index[1].page_len = k * 2;
4451 lun->log_pages.index[2].page_data = &lun->log_pages.lbp_page[0];
4452 lun->log_pages.index[2].page_len = 12*CTL_NUM_LBP_PARAMS;
4453 lun->log_pages.index[3].page_data = (uint8_t *)&lun->log_pages.stat_page;
4454 lun->log_pages.index[3].page_len = sizeof(lun->log_pages.stat_page);
4455 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.ie_page;
4456 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.ie_page);
4458 return (CTL_RETVAL_COMPLETE);
4462 hex2bin(const char *str, uint8_t *buf, int buf_size)
4467 memset(buf, 0, buf_size);
4468 while (isspace(str[0]))
4470 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4473 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4474 while (str[i] == '-') /* Skip dashes in UUIDs. */
4479 else if (isalpha(c))
4480 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4486 buf[i / 2] |= (c << 4);
4490 return ((i + 1) / 2);
4497 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4498 * wants us to allocate the LUN and he can block.
4499 * - ctl_softc is always set
4500 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4502 * Returns 0 for success, non-zero (errno) for failure.
4505 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4506 struct ctl_be_lun *const be_lun)
4508 struct ctl_lun *nlun, *lun;
4509 struct scsi_vpd_id_descriptor *desc;
4510 struct scsi_vpd_id_t10 *t10id;
4511 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4512 int lun_number, lun_malloced;
4513 int devidlen, idlen1, idlen2 = 0, len;
4519 * We currently only support Direct Access or Processor LUN types.
4521 switch (be_lun->lun_type) {
4529 be_lun->lun_config_status(be_lun->be_lun,
4530 CTL_LUN_CONFIG_FAILURE);
4533 if (ctl_lun == NULL) {
4534 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4541 memset(lun, 0, sizeof(*lun));
4543 lun->flags = CTL_LUN_MALLOCED;
4545 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4546 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4547 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4548 M_DEVBUF, M_WAITOK | M_ZERO);
4549 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4550 M_DEVBUF, M_WAITOK | M_ZERO);
4552 /* Generate LUN ID. */
4553 devidlen = max(CTL_DEVID_MIN_LEN,
4554 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4555 idlen1 = sizeof(*t10id) + devidlen;
4556 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4557 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL);
4558 if (scsiname != NULL) {
4559 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4560 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4562 eui = dnvlist_get_string(be_lun->options, "eui", NULL);
4564 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4566 naa = dnvlist_get_string(be_lun->options, "naa", NULL);
4568 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4570 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL);
4572 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4574 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4575 M_CTL, M_WAITOK | M_ZERO);
4576 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4577 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4578 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4579 desc->length = idlen1;
4580 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4581 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4582 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) {
4583 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4585 strncpy(t10id->vendor, vendor,
4586 min(sizeof(t10id->vendor), strlen(vendor)));
4588 strncpy((char *)t10id->vendor_spec_id,
4589 (char *)be_lun->device_id, devidlen);
4590 if (scsiname != NULL) {
4591 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4593 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4594 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4595 SVPD_ID_TYPE_SCSI_NAME;
4596 desc->length = idlen2;
4597 strlcpy(desc->identifier, scsiname, idlen2);
4600 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4602 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4603 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4605 desc->length = hex2bin(eui, desc->identifier, 16);
4606 desc->length = desc->length > 12 ? 16 :
4607 (desc->length > 8 ? 12 : 8);
4608 len -= 16 - desc->length;
4611 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4613 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4614 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4616 desc->length = hex2bin(naa, desc->identifier, 16);
4617 desc->length = desc->length > 8 ? 16 : 8;
4618 len -= 16 - desc->length;
4621 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4623 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4624 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4626 desc->identifier[0] = 0x10;
4627 hex2bin(uuid, &desc->identifier[2], 16);
4630 lun->lun_devid->len = len;
4632 mtx_lock(&ctl_softc->ctl_lock);
4634 * See if the caller requested a particular LUN number. If so, see
4635 * if it is available. Otherwise, allocate the first available LUN.
4637 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4638 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4639 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4640 mtx_unlock(&ctl_softc->ctl_lock);
4641 if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4642 printf("ctl: requested LUN ID %d is higher "
4643 "than ctl_max_luns - 1 (%d)\n",
4644 be_lun->req_lun_id, ctl_max_luns - 1);
4647 * XXX KDM return an error, or just assign
4648 * another LUN ID in this case??
4650 printf("ctl: requested LUN ID %d is already "
4651 "in use\n", be_lun->req_lun_id);
4654 free(lun->lun_devid, M_CTL);
4655 if (lun->flags & CTL_LUN_MALLOCED)
4657 be_lun->lun_config_status(be_lun->be_lun,
4658 CTL_LUN_CONFIG_FAILURE);
4661 lun_number = be_lun->req_lun_id;
4663 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4664 if (lun_number == -1) {
4665 mtx_unlock(&ctl_softc->ctl_lock);
4666 printf("ctl: can't allocate LUN, out of LUNs\n");
4670 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4671 mtx_unlock(&ctl_softc->ctl_lock);
4673 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4674 lun->lun = lun_number;
4675 lun->be_lun = be_lun;
4677 * The processor LUN is always enabled. Disk LUNs come on line
4678 * disabled, and must be enabled by the backend.
4680 lun->flags |= CTL_LUN_DISABLED;
4681 lun->backend = be_lun->be;
4682 be_lun->ctl_lun = lun;
4683 be_lun->lun_id = lun_number;
4684 atomic_add_int(&be_lun->be->num_luns, 1);
4685 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4686 lun->flags |= CTL_LUN_EJECTED;
4687 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4688 lun->flags |= CTL_LUN_NO_MEDIA;
4689 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4690 lun->flags |= CTL_LUN_STOPPED;
4692 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4693 lun->flags |= CTL_LUN_PRIMARY_SC;
4695 value = dnvlist_get_string(be_lun->options, "removable", NULL);
4696 if (value != NULL) {
4697 if (strcmp(value, "on") == 0)
4698 lun->flags |= CTL_LUN_REMOVABLE;
4699 } else if (be_lun->lun_type == T_CDROM)
4700 lun->flags |= CTL_LUN_REMOVABLE;
4702 lun->ctl_softc = ctl_softc;
4704 lun->last_busy = getsbinuptime();
4706 TAILQ_INIT(&lun->ooa_queue);
4707 STAILQ_INIT(&lun->error_list);
4708 lun->ie_reported = 1;
4709 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4710 ctl_tpc_lun_init(lun);
4711 if (lun->flags & CTL_LUN_REMOVABLE) {
4712 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4717 * Initialize the mode and log page index.
4719 ctl_init_page_index(lun);
4720 ctl_init_log_page_index(lun);
4722 /* Setup statistics gathering */
4723 lun->stats.item = lun_number;
4726 * Now, before we insert this lun on the lun list, set the lun
4727 * inventory changed UA for all other luns.
4729 mtx_lock(&ctl_softc->ctl_lock);
4730 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4731 mtx_lock(&nlun->lun_lock);
4732 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4733 mtx_unlock(&nlun->lun_lock);
4735 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4736 ctl_softc->ctl_luns[lun_number] = lun;
4737 ctl_softc->num_luns++;
4738 mtx_unlock(&ctl_softc->ctl_lock);
4740 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4747 * - LUN has already been marked invalid and any pending I/O has been taken
4751 ctl_free_lun(struct ctl_lun *lun)
4753 struct ctl_softc *softc = lun->ctl_softc;
4754 struct ctl_lun *nlun;
4757 KASSERT(TAILQ_EMPTY(&lun->ooa_queue),
4758 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4760 mtx_lock(&softc->ctl_lock);
4761 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4762 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4763 softc->ctl_luns[lun->lun] = NULL;
4765 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4766 mtx_lock(&nlun->lun_lock);
4767 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4768 mtx_unlock(&nlun->lun_lock);
4770 mtx_unlock(&softc->ctl_lock);
4773 * Tell the backend to free resources, if this LUN has a backend.
4775 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4776 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4778 lun->ie_reportcnt = UINT32_MAX;
4779 callout_drain(&lun->ie_callout);
4780 ctl_tpc_lun_shutdown(lun);
4781 mtx_destroy(&lun->lun_lock);
4782 free(lun->lun_devid, M_CTL);
4783 for (i = 0; i < ctl_max_ports; i++)
4784 free(lun->pending_ua[i], M_CTL);
4785 free(lun->pending_ua, M_DEVBUF);
4786 for (i = 0; i < ctl_max_ports; i++)
4787 free(lun->pr_keys[i], M_CTL);
4788 free(lun->pr_keys, M_DEVBUF);
4789 free(lun->write_buffer, M_CTL);
4790 free(lun->prevent, M_CTL);
4791 if (lun->flags & CTL_LUN_MALLOCED)
4798 ctl_create_lun(struct ctl_be_lun *be_lun)
4802 * ctl_alloc_lun() should handle all potential failure cases.
4804 ctl_alloc_lun(control_softc, NULL, be_lun);
4808 ctl_add_lun(struct ctl_be_lun *be_lun)
4810 struct ctl_softc *softc = control_softc;
4812 mtx_lock(&softc->ctl_lock);
4813 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4814 mtx_unlock(&softc->ctl_lock);
4815 wakeup(&softc->pending_lun_queue);
4821 ctl_enable_lun(struct ctl_be_lun *be_lun)
4823 struct ctl_softc *softc;
4824 struct ctl_port *port, *nport;
4825 struct ctl_lun *lun;
4828 lun = (struct ctl_lun *)be_lun->ctl_lun;
4829 softc = lun->ctl_softc;
4831 mtx_lock(&softc->ctl_lock);
4832 mtx_lock(&lun->lun_lock);
4833 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4835 * eh? Why did we get called if the LUN is already
4838 mtx_unlock(&lun->lun_lock);
4839 mtx_unlock(&softc->ctl_lock);
4842 lun->flags &= ~CTL_LUN_DISABLED;
4843 mtx_unlock(&lun->lun_lock);
4845 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4846 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4847 port->lun_map != NULL || port->lun_enable == NULL)
4851 * Drop the lock while we call the FETD's enable routine.
4852 * This can lead to a callback into CTL (at least in the
4853 * case of the internal initiator frontend.
4855 mtx_unlock(&softc->ctl_lock);
4856 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4857 mtx_lock(&softc->ctl_lock);
4859 printf("%s: FETD %s port %d returned error "
4860 "%d for lun_enable on lun %jd\n",
4861 __func__, port->port_name, port->targ_port,
4862 retval, (intmax_t)lun->lun);
4866 mtx_unlock(&softc->ctl_lock);
4867 ctl_isc_announce_lun(lun);
4873 ctl_disable_lun(struct ctl_be_lun *be_lun)
4875 struct ctl_softc *softc;
4876 struct ctl_port *port;
4877 struct ctl_lun *lun;
4880 lun = (struct ctl_lun *)be_lun->ctl_lun;
4881 softc = lun->ctl_softc;
4883 mtx_lock(&softc->ctl_lock);
4884 mtx_lock(&lun->lun_lock);
4885 if (lun->flags & CTL_LUN_DISABLED) {
4886 mtx_unlock(&lun->lun_lock);
4887 mtx_unlock(&softc->ctl_lock);
4890 lun->flags |= CTL_LUN_DISABLED;
4891 mtx_unlock(&lun->lun_lock);
4893 STAILQ_FOREACH(port, &softc->port_list, links) {
4894 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4895 port->lun_map != NULL || port->lun_disable == NULL)
4899 * Drop the lock before we call the frontend's disable
4900 * routine, to avoid lock order reversals.
4902 * XXX KDM what happens if the frontend list changes while
4903 * we're traversing it? It's unlikely, but should be handled.
4905 mtx_unlock(&softc->ctl_lock);
4906 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4907 mtx_lock(&softc->ctl_lock);
4909 printf("%s: FETD %s port %d returned error "
4910 "%d for lun_disable on lun %jd\n",
4911 __func__, port->port_name, port->targ_port,
4912 retval, (intmax_t)lun->lun);
4916 mtx_unlock(&softc->ctl_lock);
4917 ctl_isc_announce_lun(lun);
4923 ctl_start_lun(struct ctl_be_lun *be_lun)
4925 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4927 mtx_lock(&lun->lun_lock);
4928 lun->flags &= ~CTL_LUN_STOPPED;
4929 mtx_unlock(&lun->lun_lock);
4934 ctl_stop_lun(struct ctl_be_lun *be_lun)
4936 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4938 mtx_lock(&lun->lun_lock);
4939 lun->flags |= CTL_LUN_STOPPED;
4940 mtx_unlock(&lun->lun_lock);
4945 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4947 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4949 mtx_lock(&lun->lun_lock);
4950 lun->flags |= CTL_LUN_NO_MEDIA;
4951 mtx_unlock(&lun->lun_lock);
4956 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4958 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4959 union ctl_ha_msg msg;
4961 mtx_lock(&lun->lun_lock);
4962 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4963 if (lun->flags & CTL_LUN_REMOVABLE)
4964 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4965 mtx_unlock(&lun->lun_lock);
4966 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4967 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4968 bzero(&msg.ua, sizeof(msg.ua));
4969 msg.hdr.msg_type = CTL_MSG_UA;
4970 msg.hdr.nexus.initid = -1;
4971 msg.hdr.nexus.targ_port = -1;
4972 msg.hdr.nexus.targ_lun = lun->lun;
4973 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4976 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4977 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4984 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4986 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4988 mtx_lock(&lun->lun_lock);
4989 lun->flags |= CTL_LUN_EJECTED;
4990 mtx_unlock(&lun->lun_lock);
4995 ctl_lun_primary(struct ctl_be_lun *be_lun)
4997 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4999 mtx_lock(&lun->lun_lock);
5000 lun->flags |= CTL_LUN_PRIMARY_SC;
5001 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5002 mtx_unlock(&lun->lun_lock);
5003 ctl_isc_announce_lun(lun);
5008 ctl_lun_secondary(struct ctl_be_lun *be_lun)
5010 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5012 mtx_lock(&lun->lun_lock);
5013 lun->flags &= ~CTL_LUN_PRIMARY_SC;
5014 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5015 mtx_unlock(&lun->lun_lock);
5016 ctl_isc_announce_lun(lun);
5021 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5023 struct ctl_lun *lun;
5025 lun = (struct ctl_lun *)be_lun->ctl_lun;
5027 mtx_lock(&lun->lun_lock);
5030 * The LUN needs to be disabled before it can be marked invalid.
5032 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5033 mtx_unlock(&lun->lun_lock);
5037 * Mark the LUN invalid.
5039 lun->flags |= CTL_LUN_INVALID;
5042 * If there is nothing in the OOA queue, go ahead and free the LUN.
5043 * If we have something in the OOA queue, we'll free it when the
5044 * last I/O completes.
5046 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5047 mtx_unlock(&lun->lun_lock);
5050 mtx_unlock(&lun->lun_lock);
5056 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5058 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5059 union ctl_ha_msg msg;
5061 mtx_lock(&lun->lun_lock);
5062 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5063 mtx_unlock(&lun->lun_lock);
5064 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5065 /* Send msg to other side. */
5066 bzero(&msg.ua, sizeof(msg.ua));
5067 msg.hdr.msg_type = CTL_MSG_UA;
5068 msg.hdr.nexus.initid = -1;
5069 msg.hdr.nexus.targ_port = -1;
5070 msg.hdr.nexus.targ_lun = lun->lun;
5071 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5074 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5075 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5081 * Backend "memory move is complete" callback for requests that never
5082 * make it down to say RAIDCore's configuration code.
5085 ctl_config_move_done(union ctl_io *io)
5089 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5090 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5091 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5093 if ((io->io_hdr.port_status != 0) &&
5094 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5095 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5096 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5097 /*retry_count*/ io->io_hdr.port_status);
5098 } else if (io->scsiio.kern_data_resid != 0 &&
5099 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5100 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5101 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5102 ctl_set_invalid_field_ciu(&io->scsiio);
5105 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5107 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5108 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5109 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5110 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5112 * XXX KDM just assuming a single pointer here, and not a
5113 * S/G list. If we start using S/G lists for config data,
5114 * we'll need to know how to clean them up here as well.
5116 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5117 free(io->scsiio.kern_data_ptr, M_CTL);
5119 retval = CTL_RETVAL_COMPLETE;
5122 * XXX KDM now we need to continue data movement. Some
5124 * - call ctl_scsiio() again? We don't do this for data
5125 * writes, because for those at least we know ahead of
5126 * time where the write will go and how long it is. For
5127 * config writes, though, that information is largely
5128 * contained within the write itself, thus we need to
5129 * parse out the data again.
5131 * - Call some other function once the data is in?
5135 * XXX KDM call ctl_scsiio() again for now, and check flag
5136 * bits to see whether we're allocated or not.
5138 retval = ctl_scsiio(&io->scsiio);
5144 * This gets called by a backend driver when it is done with a
5145 * data_submit method.
5148 ctl_data_submit_done(union ctl_io *io)
5151 * If the IO_CONT flag is set, we need to call the supplied
5152 * function to continue processing the I/O, instead of completing
5155 * If there is an error, though, we don't want to keep processing.
5156 * Instead, just send status back to the initiator.
5158 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5159 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5160 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5161 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5162 io->scsiio.io_cont(io);
5169 * This gets called by a backend driver when it is done with a
5170 * configuration write.
5173 ctl_config_write_done(union ctl_io *io)
5178 * If the IO_CONT flag is set, we need to call the supplied
5179 * function to continue processing the I/O, instead of completing
5182 * If there is an error, though, we don't want to keep processing.
5183 * Instead, just send status back to the initiator.
5185 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5186 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5187 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5188 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5189 io->scsiio.io_cont(io);
5193 * Since a configuration write can be done for commands that actually
5194 * have data allocated, like write buffer, and commands that have
5195 * no data, like start/stop unit, we need to check here.
5197 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5198 buf = io->scsiio.kern_data_ptr;
5207 ctl_config_read_done(union ctl_io *io)
5212 * If there is some error -- we are done, skip data transfer.
5214 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5215 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5216 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5217 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5218 buf = io->scsiio.kern_data_ptr;
5228 * If the IO_CONT flag is set, we need to call the supplied
5229 * function to continue processing the I/O, instead of completing
5232 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5233 io->scsiio.io_cont(io);
5241 * SCSI release command.
5244 ctl_scsi_release(struct ctl_scsiio *ctsio)
5246 struct ctl_lun *lun = CTL_LUN(ctsio);
5249 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5251 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5254 * XXX KDM right now, we only support LUN reservation. We don't
5255 * support 3rd party reservations, or extent reservations, which
5256 * might actually need the parameter list. If we've gotten this
5257 * far, we've got a LUN reservation. Anything else got kicked out
5258 * above. So, according to SPC, ignore the length.
5261 mtx_lock(&lun->lun_lock);
5264 * According to SPC, it is not an error for an intiator to attempt
5265 * to release a reservation on a LUN that isn't reserved, or that
5266 * is reserved by another initiator. The reservation can only be
5267 * released, though, by the initiator who made it or by one of
5268 * several reset type events.
5270 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5271 lun->flags &= ~CTL_LUN_RESERVED;
5273 mtx_unlock(&lun->lun_lock);
5275 ctl_set_success(ctsio);
5276 ctl_done((union ctl_io *)ctsio);
5277 return (CTL_RETVAL_COMPLETE);
5281 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5283 struct ctl_lun *lun = CTL_LUN(ctsio);
5286 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5288 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5291 * XXX KDM right now, we only support LUN reservation. We don't
5292 * support 3rd party reservations, or extent reservations, which
5293 * might actually need the parameter list. If we've gotten this
5294 * far, we've got a LUN reservation. Anything else got kicked out
5295 * above. So, according to SPC, ignore the length.
5298 mtx_lock(&lun->lun_lock);
5299 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5300 ctl_set_reservation_conflict(ctsio);
5304 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5305 if (lun->flags & CTL_LUN_PR_RESERVED) {
5306 ctl_set_success(ctsio);
5310 lun->flags |= CTL_LUN_RESERVED;
5311 lun->res_idx = residx;
5312 ctl_set_success(ctsio);
5315 mtx_unlock(&lun->lun_lock);
5316 ctl_done((union ctl_io *)ctsio);
5317 return (CTL_RETVAL_COMPLETE);
5321 ctl_start_stop(struct ctl_scsiio *ctsio)
5323 struct ctl_lun *lun = CTL_LUN(ctsio);
5324 struct scsi_start_stop_unit *cdb;
5327 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5329 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5331 if ((cdb->how & SSS_PC_MASK) == 0) {
5332 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5333 (cdb->how & SSS_START) == 0) {
5336 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5337 if (ctl_get_prkey(lun, residx) == 0 ||
5338 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5340 ctl_set_reservation_conflict(ctsio);
5341 ctl_done((union ctl_io *)ctsio);
5342 return (CTL_RETVAL_COMPLETE);
5346 if ((cdb->how & SSS_LOEJ) &&
5347 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5348 ctl_set_invalid_field(ctsio,
5354 ctl_done((union ctl_io *)ctsio);
5355 return (CTL_RETVAL_COMPLETE);
5358 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5359 lun->prevent_count > 0) {
5360 /* "Medium removal prevented" */
5361 ctl_set_sense(ctsio, /*current_error*/ 1,
5362 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5363 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5364 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5365 ctl_done((union ctl_io *)ctsio);
5366 return (CTL_RETVAL_COMPLETE);
5370 retval = lun->backend->config_write((union ctl_io *)ctsio);
5375 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5377 struct ctl_lun *lun = CTL_LUN(ctsio);
5378 struct scsi_prevent *cdb;
5382 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5384 cdb = (struct scsi_prevent *)ctsio->cdb;
5386 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5387 ctl_set_invalid_opcode(ctsio);
5388 ctl_done((union ctl_io *)ctsio);
5389 return (CTL_RETVAL_COMPLETE);
5392 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5393 mtx_lock(&lun->lun_lock);
5394 if ((cdb->how & PR_PREVENT) &&
5395 ctl_is_set(lun->prevent, initidx) == 0) {
5396 ctl_set_mask(lun->prevent, initidx);
5397 lun->prevent_count++;
5398 } else if ((cdb->how & PR_PREVENT) == 0 &&
5399 ctl_is_set(lun->prevent, initidx)) {
5400 ctl_clear_mask(lun->prevent, initidx);
5401 lun->prevent_count--;
5403 mtx_unlock(&lun->lun_lock);
5404 retval = lun->backend->config_write((union ctl_io *)ctsio);
5409 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5410 * we don't really do anything with the LBA and length fields if the user
5411 * passes them in. Instead we'll just flush out the cache for the entire
5415 ctl_sync_cache(struct ctl_scsiio *ctsio)
5417 struct ctl_lun *lun = CTL_LUN(ctsio);
5418 struct ctl_lba_len_flags *lbalen;
5419 uint64_t starting_lba;
5420 uint32_t block_count;
5424 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5428 switch (ctsio->cdb[0]) {
5429 case SYNCHRONIZE_CACHE: {
5430 struct scsi_sync_cache *cdb;
5431 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5433 starting_lba = scsi_4btoul(cdb->begin_lba);
5434 block_count = scsi_2btoul(cdb->lb_count);
5438 case SYNCHRONIZE_CACHE_16: {
5439 struct scsi_sync_cache_16 *cdb;
5440 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5442 starting_lba = scsi_8btou64(cdb->begin_lba);
5443 block_count = scsi_4btoul(cdb->lb_count);
5448 ctl_set_invalid_opcode(ctsio);
5449 ctl_done((union ctl_io *)ctsio);
5451 break; /* NOTREACHED */
5455 * We check the LBA and length, but don't do anything with them.
5456 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5457 * get flushed. This check will just help satisfy anyone who wants
5458 * to see an error for an out of range LBA.
5460 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5461 ctl_set_lba_out_of_range(ctsio,
5462 MAX(starting_lba, lun->be_lun->maxlba + 1));
5463 ctl_done((union ctl_io *)ctsio);
5467 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5468 lbalen->lba = starting_lba;
5469 lbalen->len = block_count;
5470 lbalen->flags = byte2;
5471 retval = lun->backend->config_write((union ctl_io *)ctsio);
5478 ctl_format(struct ctl_scsiio *ctsio)
5480 struct scsi_format *cdb;
5481 int length, defect_list_len;
5483 CTL_DEBUG_PRINT(("ctl_format\n"));
5485 cdb = (struct scsi_format *)ctsio->cdb;
5488 if (cdb->byte2 & SF_FMTDATA) {
5489 if (cdb->byte2 & SF_LONGLIST)
5490 length = sizeof(struct scsi_format_header_long);
5492 length = sizeof(struct scsi_format_header_short);
5495 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5497 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5498 ctsio->kern_data_len = length;
5499 ctsio->kern_total_len = length;
5500 ctsio->kern_rel_offset = 0;
5501 ctsio->kern_sg_entries = 0;
5502 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5503 ctsio->be_move_done = ctl_config_move_done;
5504 ctl_datamove((union ctl_io *)ctsio);
5506 return (CTL_RETVAL_COMPLETE);
5509 defect_list_len = 0;
5511 if (cdb->byte2 & SF_FMTDATA) {
5512 if (cdb->byte2 & SF_LONGLIST) {
5513 struct scsi_format_header_long *header;
5515 header = (struct scsi_format_header_long *)
5516 ctsio->kern_data_ptr;
5518 defect_list_len = scsi_4btoul(header->defect_list_len);
5519 if (defect_list_len != 0) {
5520 ctl_set_invalid_field(ctsio,
5529 struct scsi_format_header_short *header;
5531 header = (struct scsi_format_header_short *)
5532 ctsio->kern_data_ptr;
5534 defect_list_len = scsi_2btoul(header->defect_list_len);
5535 if (defect_list_len != 0) {
5536 ctl_set_invalid_field(ctsio,
5547 ctl_set_success(ctsio);
5550 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5551 free(ctsio->kern_data_ptr, M_CTL);
5552 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5555 ctl_done((union ctl_io *)ctsio);
5556 return (CTL_RETVAL_COMPLETE);
5560 ctl_read_buffer(struct ctl_scsiio *ctsio)
5562 struct ctl_lun *lun = CTL_LUN(ctsio);
5563 uint64_t buffer_offset;
5566 static uint8_t descr[4];
5567 static uint8_t echo_descr[4] = { 0 };
5569 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5571 switch (ctsio->cdb[0]) {
5573 struct scsi_read_buffer *cdb;
5575 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5576 buffer_offset = scsi_3btoul(cdb->offset);
5577 len = scsi_3btoul(cdb->length);
5581 case READ_BUFFER_16: {
5582 struct scsi_read_buffer_16 *cdb;
5584 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5585 buffer_offset = scsi_8btou64(cdb->offset);
5586 len = scsi_4btoul(cdb->length);
5590 default: /* This shouldn't happen. */
5591 ctl_set_invalid_opcode(ctsio);
5592 ctl_done((union ctl_io *)ctsio);
5593 return (CTL_RETVAL_COMPLETE);
5596 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5597 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5598 ctl_set_invalid_field(ctsio,
5604 ctl_done((union ctl_io *)ctsio);
5605 return (CTL_RETVAL_COMPLETE);
5608 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5610 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5611 ctsio->kern_data_ptr = descr;
5612 len = min(len, sizeof(descr));
5613 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5614 ctsio->kern_data_ptr = echo_descr;
5615 len = min(len, sizeof(echo_descr));
5617 if (lun->write_buffer == NULL) {
5618 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5621 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5623 ctsio->kern_data_len = len;
5624 ctsio->kern_total_len = len;
5625 ctsio->kern_rel_offset = 0;
5626 ctsio->kern_sg_entries = 0;
5627 ctl_set_success(ctsio);
5628 ctsio->be_move_done = ctl_config_move_done;
5629 ctl_datamove((union ctl_io *)ctsio);
5630 return (CTL_RETVAL_COMPLETE);
5634 ctl_write_buffer(struct ctl_scsiio *ctsio)
5636 struct ctl_lun *lun = CTL_LUN(ctsio);
5637 struct scsi_write_buffer *cdb;
5638 int buffer_offset, len;
5640 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5642 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5644 len = scsi_3btoul(cdb->length);
5645 buffer_offset = scsi_3btoul(cdb->offset);
5647 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5648 ctl_set_invalid_field(ctsio,
5654 ctl_done((union ctl_io *)ctsio);
5655 return (CTL_RETVAL_COMPLETE);
5659 * If we've got a kernel request that hasn't been malloced yet,
5660 * malloc it and tell the caller the data buffer is here.
5662 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5663 if (lun->write_buffer == NULL) {
5664 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5667 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5668 ctsio->kern_data_len = len;
5669 ctsio->kern_total_len = len;
5670 ctsio->kern_rel_offset = 0;
5671 ctsio->kern_sg_entries = 0;
5672 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5673 ctsio->be_move_done = ctl_config_move_done;
5674 ctl_datamove((union ctl_io *)ctsio);
5676 return (CTL_RETVAL_COMPLETE);
5679 ctl_set_success(ctsio);
5680 ctl_done((union ctl_io *)ctsio);
5681 return (CTL_RETVAL_COMPLETE);
5685 ctl_write_same(struct ctl_scsiio *ctsio)
5687 struct ctl_lun *lun = CTL_LUN(ctsio);
5688 struct ctl_lba_len_flags *lbalen;
5690 uint32_t num_blocks;
5694 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5696 switch (ctsio->cdb[0]) {
5697 case WRITE_SAME_10: {
5698 struct scsi_write_same_10 *cdb;
5700 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5702 lba = scsi_4btoul(cdb->addr);
5703 num_blocks = scsi_2btoul(cdb->length);
5707 case WRITE_SAME_16: {
5708 struct scsi_write_same_16 *cdb;
5710 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5712 lba = scsi_8btou64(cdb->addr);
5713 num_blocks = scsi_4btoul(cdb->length);
5719 * We got a command we don't support. This shouldn't
5720 * happen, commands should be filtered out above us.
5722 ctl_set_invalid_opcode(ctsio);
5723 ctl_done((union ctl_io *)ctsio);
5725 return (CTL_RETVAL_COMPLETE);
5726 break; /* NOTREACHED */
5729 /* ANCHOR flag can be used only together with UNMAP */
5730 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5731 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5732 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5733 ctl_done((union ctl_io *)ctsio);
5734 return (CTL_RETVAL_COMPLETE);
5738 * The first check is to make sure we're in bounds, the second
5739 * check is to catch wrap-around problems. If the lba + num blocks
5740 * is less than the lba, then we've wrapped around and the block
5741 * range is invalid anyway.
5743 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5744 || ((lba + num_blocks) < lba)) {
5745 ctl_set_lba_out_of_range(ctsio,
5746 MAX(lba, lun->be_lun->maxlba + 1));
5747 ctl_done((union ctl_io *)ctsio);
5748 return (CTL_RETVAL_COMPLETE);
5751 /* Zero number of blocks means "to the last logical block" */
5752 if (num_blocks == 0) {
5753 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5754 ctl_set_invalid_field(ctsio,
5760 ctl_done((union ctl_io *)ctsio);
5761 return (CTL_RETVAL_COMPLETE);
5763 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5766 len = lun->be_lun->blocksize;
5769 * If we've got a kernel request that hasn't been malloced yet,
5770 * malloc it and tell the caller the data buffer is here.
5772 if ((byte2 & SWS_NDOB) == 0 &&
5773 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5774 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5775 ctsio->kern_data_len = len;
5776 ctsio->kern_total_len = len;
5777 ctsio->kern_rel_offset = 0;
5778 ctsio->kern_sg_entries = 0;
5779 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5780 ctsio->be_move_done = ctl_config_move_done;
5781 ctl_datamove((union ctl_io *)ctsio);
5783 return (CTL_RETVAL_COMPLETE);
5786 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5788 lbalen->len = num_blocks;
5789 lbalen->flags = byte2;
5790 retval = lun->backend->config_write((union ctl_io *)ctsio);
5796 ctl_unmap(struct ctl_scsiio *ctsio)
5798 struct ctl_lun *lun = CTL_LUN(ctsio);
5799 struct scsi_unmap *cdb;
5800 struct ctl_ptr_len_flags *ptrlen;
5801 struct scsi_unmap_header *hdr;
5802 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5804 uint32_t num_blocks;
5808 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5810 cdb = (struct scsi_unmap *)ctsio->cdb;
5811 len = scsi_2btoul(cdb->length);
5815 * If we've got a kernel request that hasn't been malloced yet,
5816 * malloc it and tell the caller the data buffer is here.
5818 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5819 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5820 ctsio->kern_data_len = len;
5821 ctsio->kern_total_len = len;
5822 ctsio->kern_rel_offset = 0;
5823 ctsio->kern_sg_entries = 0;
5824 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5825 ctsio->be_move_done = ctl_config_move_done;
5826 ctl_datamove((union ctl_io *)ctsio);
5828 return (CTL_RETVAL_COMPLETE);
5831 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5832 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5833 if (len < sizeof (*hdr) ||
5834 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5835 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5836 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5837 ctl_set_invalid_field(ctsio,
5845 len = scsi_2btoul(hdr->desc_length);
5846 buf = (struct scsi_unmap_desc *)(hdr + 1);
5847 end = buf + len / sizeof(*buf);
5850 for (range = buf; range < end; range++) {
5851 lba = scsi_8btou64(range->lba);
5852 num_blocks = scsi_4btoul(range->length);
5853 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5854 || ((lba + num_blocks) < lba)) {
5855 ctl_set_lba_out_of_range(ctsio,
5856 MAX(lba, lun->be_lun->maxlba + 1));
5857 ctl_done((union ctl_io *)ctsio);
5858 return (CTL_RETVAL_COMPLETE);
5860 if (num_blocks != 0)
5865 * Block backend can not handle zero last range.
5866 * Filter it out and return if there is nothing left.
5868 len = (uint8_t *)endnz - (uint8_t *)buf;
5870 ctl_set_success(ctsio);
5874 mtx_lock(&lun->lun_lock);
5875 ptrlen = (struct ctl_ptr_len_flags *)
5876 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5877 ptrlen->ptr = (void *)buf;
5879 ptrlen->flags = byte2;
5880 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE);
5881 mtx_unlock(&lun->lun_lock);
5883 retval = lun->backend->config_write((union ctl_io *)ctsio);
5887 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5888 free(ctsio->kern_data_ptr, M_CTL);
5889 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5891 ctl_done((union ctl_io *)ctsio);
5892 return (CTL_RETVAL_COMPLETE);
5896 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5897 struct ctl_page_index *page_index, uint8_t *page_ptr)
5899 struct ctl_lun *lun = CTL_LUN(ctsio);
5900 uint8_t *current_cp;
5904 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5907 current_cp = (page_index->page_data + (page_index->page_len *
5910 mtx_lock(&lun->lun_lock);
5911 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5912 memcpy(current_cp, page_ptr, page_index->page_len);
5916 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5917 mtx_unlock(&lun->lun_lock);
5919 ctl_isc_announce_mode(lun,
5920 ctl_get_initindex(&ctsio->io_hdr.nexus),
5921 page_index->page_code, page_index->subpage);
5923 return (CTL_RETVAL_COMPLETE);
5927 ctl_ie_timer(void *arg)
5929 struct ctl_lun *lun = arg;
5932 if (lun->ie_asc == 0)
5935 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5936 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5938 lun->ie_reported = 0;
5940 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5941 lun->ie_reportcnt++;
5942 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5943 if (t == 0 || t == UINT32_MAX)
5944 t = 3000; /* 5 min */
5945 callout_schedule(&lun->ie_callout, t * hz / 10);
5950 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5951 struct ctl_page_index *page_index, uint8_t *page_ptr)
5953 struct ctl_lun *lun = CTL_LUN(ctsio);
5954 struct scsi_info_exceptions_page *pg;
5957 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5959 pg = (struct scsi_info_exceptions_page *)page_ptr;
5960 mtx_lock(&lun->lun_lock);
5961 if (pg->info_flags & SIEP_FLAGS_TEST) {
5963 lun->ie_ascq = 0xff;
5964 if (pg->mrie == SIEP_MRIE_UA) {
5965 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5966 lun->ie_reported = 1;
5968 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5969 lun->ie_reported = -1;
5971 lun->ie_reportcnt = 1;
5972 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5973 lun->ie_reportcnt++;
5974 t = scsi_4btoul(pg->interval_timer);
5975 if (t == 0 || t == UINT32_MAX)
5976 t = 3000; /* 5 min */
5977 callout_reset(&lun->ie_callout, t * hz / 10,
5983 lun->ie_reported = 1;
5984 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5985 lun->ie_reportcnt = UINT32_MAX;
5986 callout_stop(&lun->ie_callout);
5988 mtx_unlock(&lun->lun_lock);
5989 return (CTL_RETVAL_COMPLETE);
5993 ctl_do_mode_select(union ctl_io *io)
5995 struct ctl_lun *lun = CTL_LUN(io);
5996 struct scsi_mode_page_header *page_header;
5997 struct ctl_page_index *page_index;
5998 struct ctl_scsiio *ctsio;
5999 int page_len, page_len_offset, page_len_size;
6000 union ctl_modepage_info *modepage_info;
6001 uint16_t *len_left, *len_used;
6004 ctsio = &io->scsiio;
6008 modepage_info = (union ctl_modepage_info *)
6009 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6010 len_left = &modepage_info->header.len_left;
6011 len_used = &modepage_info->header.len_used;
6015 page_header = (struct scsi_mode_page_header *)
6016 (ctsio->kern_data_ptr + *len_used);
6018 if (*len_left == 0) {
6019 free(ctsio->kern_data_ptr, M_CTL);
6020 ctl_set_success(ctsio);
6021 ctl_done((union ctl_io *)ctsio);
6022 return (CTL_RETVAL_COMPLETE);
6023 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6025 free(ctsio->kern_data_ptr, M_CTL);
6026 ctl_set_param_len_error(ctsio);
6027 ctl_done((union ctl_io *)ctsio);
6028 return (CTL_RETVAL_COMPLETE);
6030 } else if ((page_header->page_code & SMPH_SPF)
6031 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6033 free(ctsio->kern_data_ptr, M_CTL);
6034 ctl_set_param_len_error(ctsio);
6035 ctl_done((union ctl_io *)ctsio);
6036 return (CTL_RETVAL_COMPLETE);
6041 * XXX KDM should we do something with the block descriptor?
6043 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6044 page_index = &lun->mode_pages.index[i];
6045 if (lun->be_lun->lun_type == T_DIRECT &&
6046 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6048 if (lun->be_lun->lun_type == T_PROCESSOR &&
6049 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6051 if (lun->be_lun->lun_type == T_CDROM &&
6052 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6055 if ((page_index->page_code & SMPH_PC_MASK) !=
6056 (page_header->page_code & SMPH_PC_MASK))
6060 * If neither page has a subpage code, then we've got a
6063 if (((page_index->page_code & SMPH_SPF) == 0)
6064 && ((page_header->page_code & SMPH_SPF) == 0)) {
6065 page_len = page_header->page_length;
6070 * If both pages have subpages, then the subpage numbers
6073 if ((page_index->page_code & SMPH_SPF)
6074 && (page_header->page_code & SMPH_SPF)) {
6075 struct scsi_mode_page_header_sp *sph;
6077 sph = (struct scsi_mode_page_header_sp *)page_header;
6078 if (page_index->subpage == sph->subpage) {
6079 page_len = scsi_2btoul(sph->page_length);
6086 * If we couldn't find the page, or if we don't have a mode select
6087 * handler for it, send back an error to the user.
6089 if ((i >= CTL_NUM_MODE_PAGES)
6090 || (page_index->select_handler == NULL)) {
6091 ctl_set_invalid_field(ctsio,
6094 /*field*/ *len_used,
6097 free(ctsio->kern_data_ptr, M_CTL);
6098 ctl_done((union ctl_io *)ctsio);
6099 return (CTL_RETVAL_COMPLETE);
6102 if (page_index->page_code & SMPH_SPF) {
6103 page_len_offset = 2;
6107 page_len_offset = 1;
6111 * If the length the initiator gives us isn't the one we specify in
6112 * the mode page header, or if they didn't specify enough data in
6113 * the CDB to avoid truncating this page, kick out the request.
6115 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6116 ctl_set_invalid_field(ctsio,
6119 /*field*/ *len_used + page_len_offset,
6122 free(ctsio->kern_data_ptr, M_CTL);
6123 ctl_done((union ctl_io *)ctsio);
6124 return (CTL_RETVAL_COMPLETE);
6126 if (*len_left < page_index->page_len) {
6127 free(ctsio->kern_data_ptr, M_CTL);
6128 ctl_set_param_len_error(ctsio);
6129 ctl_done((union ctl_io *)ctsio);
6130 return (CTL_RETVAL_COMPLETE);
6134 * Run through the mode page, checking to make sure that the bits
6135 * the user changed are actually legal for him to change.
6137 for (i = 0; i < page_index->page_len; i++) {
6138 uint8_t *user_byte, *change_mask, *current_byte;
6142 user_byte = (uint8_t *)page_header + i;
6143 change_mask = page_index->page_data +
6144 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6145 current_byte = page_index->page_data +
6146 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6149 * Check to see whether the user set any bits in this byte
6150 * that he is not allowed to set.
6152 if ((*user_byte & ~(*change_mask)) ==
6153 (*current_byte & ~(*change_mask)))
6157 * Go through bit by bit to determine which one is illegal.
6160 for (j = 7; j >= 0; j--) {
6161 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6162 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6167 ctl_set_invalid_field(ctsio,
6170 /*field*/ *len_used + i,
6173 free(ctsio->kern_data_ptr, M_CTL);
6174 ctl_done((union ctl_io *)ctsio);
6175 return (CTL_RETVAL_COMPLETE);
6179 * Decrement these before we call the page handler, since we may
6180 * end up getting called back one way or another before the handler
6181 * returns to this context.
6183 *len_left -= page_index->page_len;
6184 *len_used += page_index->page_len;
6186 retval = page_index->select_handler(ctsio, page_index,
6187 (uint8_t *)page_header);
6190 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6191 * wait until this queued command completes to finish processing
6192 * the mode page. If it returns anything other than
6193 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6194 * already set the sense information, freed the data pointer, and
6195 * completed the io for us.
6197 if (retval != CTL_RETVAL_COMPLETE)
6198 goto bailout_no_done;
6201 * If the initiator sent us more than one page, parse the next one.
6206 ctl_set_success(ctsio);
6207 free(ctsio->kern_data_ptr, M_CTL);
6208 ctl_done((union ctl_io *)ctsio);
6212 return (CTL_RETVAL_COMPLETE);
6217 ctl_mode_select(struct ctl_scsiio *ctsio)
6219 struct ctl_lun *lun = CTL_LUN(ctsio);
6220 union ctl_modepage_info *modepage_info;
6221 int bd_len, i, header_size, param_len, rtd;
6224 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6225 switch (ctsio->cdb[0]) {
6226 case MODE_SELECT_6: {
6227 struct scsi_mode_select_6 *cdb;
6229 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6231 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6232 param_len = cdb->length;
6233 header_size = sizeof(struct scsi_mode_header_6);
6236 case MODE_SELECT_10: {
6237 struct scsi_mode_select_10 *cdb;
6239 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6241 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6242 param_len = scsi_2btoul(cdb->length);
6243 header_size = sizeof(struct scsi_mode_header_10);
6247 ctl_set_invalid_opcode(ctsio);
6248 ctl_done((union ctl_io *)ctsio);
6249 return (CTL_RETVAL_COMPLETE);
6253 if (param_len != 0) {
6254 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6255 /*command*/ 1, /*field*/ 0,
6256 /*bit_valid*/ 0, /*bit*/ 0);
6257 ctl_done((union ctl_io *)ctsio);
6258 return (CTL_RETVAL_COMPLETE);
6261 /* Revert to defaults. */
6262 ctl_init_page_index(lun);
6263 mtx_lock(&lun->lun_lock);
6264 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6265 mtx_unlock(&lun->lun_lock);
6266 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6267 ctl_isc_announce_mode(lun, -1,
6268 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6269 lun->mode_pages.index[i].subpage);
6271 ctl_set_success(ctsio);
6272 ctl_done((union ctl_io *)ctsio);
6273 return (CTL_RETVAL_COMPLETE);
6278 * "A parameter list length of zero indicates that the Data-Out Buffer
6279 * shall be empty. This condition shall not be considered as an error."
6281 if (param_len == 0) {
6282 ctl_set_success(ctsio);
6283 ctl_done((union ctl_io *)ctsio);
6284 return (CTL_RETVAL_COMPLETE);
6288 * Since we'll hit this the first time through, prior to
6289 * allocation, we don't need to free a data buffer here.
6291 if (param_len < header_size) {
6292 ctl_set_param_len_error(ctsio);
6293 ctl_done((union ctl_io *)ctsio);
6294 return (CTL_RETVAL_COMPLETE);
6298 * Allocate the data buffer and grab the user's data. In theory,
6299 * we shouldn't have to sanity check the parameter list length here
6300 * because the maximum size is 64K. We should be able to malloc
6301 * that much without too many problems.
6303 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6304 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6305 ctsio->kern_data_len = param_len;
6306 ctsio->kern_total_len = param_len;
6307 ctsio->kern_rel_offset = 0;
6308 ctsio->kern_sg_entries = 0;
6309 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6310 ctsio->be_move_done = ctl_config_move_done;
6311 ctl_datamove((union ctl_io *)ctsio);
6313 return (CTL_RETVAL_COMPLETE);
6316 switch (ctsio->cdb[0]) {
6317 case MODE_SELECT_6: {
6318 struct scsi_mode_header_6 *mh6;
6320 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6321 bd_len = mh6->blk_desc_len;
6324 case MODE_SELECT_10: {
6325 struct scsi_mode_header_10 *mh10;
6327 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6328 bd_len = scsi_2btoul(mh10->blk_desc_len);
6332 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6335 if (param_len < (header_size + bd_len)) {
6336 free(ctsio->kern_data_ptr, M_CTL);
6337 ctl_set_param_len_error(ctsio);
6338 ctl_done((union ctl_io *)ctsio);
6339 return (CTL_RETVAL_COMPLETE);
6343 * Set the IO_CONT flag, so that if this I/O gets passed to
6344 * ctl_config_write_done(), it'll get passed back to
6345 * ctl_do_mode_select() for further processing, or completion if
6348 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6349 ctsio->io_cont = ctl_do_mode_select;
6351 modepage_info = (union ctl_modepage_info *)
6352 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6353 memset(modepage_info, 0, sizeof(*modepage_info));
6354 modepage_info->header.len_left = param_len - header_size - bd_len;
6355 modepage_info->header.len_used = header_size + bd_len;
6357 return (ctl_do_mode_select((union ctl_io *)ctsio));
6361 ctl_mode_sense(struct ctl_scsiio *ctsio)
6363 struct ctl_lun *lun = CTL_LUN(ctsio);
6364 int pc, page_code, dbd, subpage;
6365 int alloc_len, page_len, header_len, total_len;
6366 struct scsi_mode_block_descr *block_desc;
6367 struct ctl_page_index *page_index;
6372 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6374 switch (ctsio->cdb[0]) {
6375 case MODE_SENSE_6: {
6376 struct scsi_mode_sense_6 *cdb;
6378 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6380 header_len = sizeof(struct scsi_mode_hdr_6);
6381 if (cdb->byte2 & SMS_DBD)
6384 header_len += sizeof(struct scsi_mode_block_descr);
6386 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6387 page_code = cdb->page & SMS_PAGE_CODE;
6388 subpage = cdb->subpage;
6389 alloc_len = cdb->length;
6392 case MODE_SENSE_10: {
6393 struct scsi_mode_sense_10 *cdb;
6395 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6397 header_len = sizeof(struct scsi_mode_hdr_10);
6399 if (cdb->byte2 & SMS_DBD)
6402 header_len += sizeof(struct scsi_mode_block_descr);
6403 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6404 page_code = cdb->page & SMS_PAGE_CODE;
6405 subpage = cdb->subpage;
6406 alloc_len = scsi_2btoul(cdb->length);
6410 ctl_set_invalid_opcode(ctsio);
6411 ctl_done((union ctl_io *)ctsio);
6412 return (CTL_RETVAL_COMPLETE);
6413 break; /* NOTREACHED */
6417 * We have to make a first pass through to calculate the size of
6418 * the pages that match the user's query. Then we allocate enough
6419 * memory to hold it, and actually copy the data into the buffer.
6421 switch (page_code) {
6422 case SMS_ALL_PAGES_PAGE: {
6428 * At the moment, values other than 0 and 0xff here are
6429 * reserved according to SPC-3.
6431 if ((subpage != SMS_SUBPAGE_PAGE_0)
6432 && (subpage != SMS_SUBPAGE_ALL)) {
6433 ctl_set_invalid_field(ctsio,
6439 ctl_done((union ctl_io *)ctsio);
6440 return (CTL_RETVAL_COMPLETE);
6443 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6444 page_index = &lun->mode_pages.index[i];
6446 /* Make sure the page is supported for this dev type */
6447 if (lun->be_lun->lun_type == T_DIRECT &&
6448 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6450 if (lun->be_lun->lun_type == T_PROCESSOR &&
6451 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6453 if (lun->be_lun->lun_type == T_CDROM &&
6454 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6458 * We don't use this subpage if the user didn't
6459 * request all subpages.
6461 if ((page_index->subpage != 0)
6462 && (subpage == SMS_SUBPAGE_PAGE_0))
6465 page_len += page_index->page_len;
6474 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6475 page_index = &lun->mode_pages.index[i];
6477 /* Make sure the page is supported for this dev type */
6478 if (lun->be_lun->lun_type == T_DIRECT &&
6479 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6481 if (lun->be_lun->lun_type == T_PROCESSOR &&
6482 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6484 if (lun->be_lun->lun_type == T_CDROM &&
6485 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6488 /* Look for the right page code */
6489 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6492 /* Look for the right subpage or the subpage wildcard*/
6493 if ((page_index->subpage != subpage)
6494 && (subpage != SMS_SUBPAGE_ALL))
6497 page_len += page_index->page_len;
6500 if (page_len == 0) {
6501 ctl_set_invalid_field(ctsio,
6507 ctl_done((union ctl_io *)ctsio);
6508 return (CTL_RETVAL_COMPLETE);
6514 total_len = header_len + page_len;
6516 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6517 ctsio->kern_sg_entries = 0;
6518 ctsio->kern_rel_offset = 0;
6519 ctsio->kern_data_len = min(total_len, alloc_len);
6520 ctsio->kern_total_len = ctsio->kern_data_len;
6522 switch (ctsio->cdb[0]) {
6523 case MODE_SENSE_6: {
6524 struct scsi_mode_hdr_6 *header;
6526 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6528 header->datalen = MIN(total_len - 1, 254);
6529 if (lun->be_lun->lun_type == T_DIRECT) {
6530 header->dev_specific = 0x10; /* DPOFUA */
6531 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6532 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6533 header->dev_specific |= 0x80; /* WP */
6536 header->block_descr_len = 0;
6538 header->block_descr_len =
6539 sizeof(struct scsi_mode_block_descr);
6540 block_desc = (struct scsi_mode_block_descr *)&header[1];
6543 case MODE_SENSE_10: {
6544 struct scsi_mode_hdr_10 *header;
6547 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6549 datalen = MIN(total_len - 2, 65533);
6550 scsi_ulto2b(datalen, header->datalen);
6551 if (lun->be_lun->lun_type == T_DIRECT) {
6552 header->dev_specific = 0x10; /* DPOFUA */
6553 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6554 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6555 header->dev_specific |= 0x80; /* WP */
6558 scsi_ulto2b(0, header->block_descr_len);
6560 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6561 header->block_descr_len);
6562 block_desc = (struct scsi_mode_block_descr *)&header[1];
6566 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6570 * If we've got a disk, use its blocksize in the block
6571 * descriptor. Otherwise, just set it to 0.
6574 if (lun->be_lun->lun_type == T_DIRECT)
6575 scsi_ulto3b(lun->be_lun->blocksize,
6576 block_desc->block_len);
6578 scsi_ulto3b(0, block_desc->block_len);
6581 switch (page_code) {
6582 case SMS_ALL_PAGES_PAGE: {
6585 data_used = header_len;
6586 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6587 struct ctl_page_index *page_index;
6589 page_index = &lun->mode_pages.index[i];
6590 if (lun->be_lun->lun_type == T_DIRECT &&
6591 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6593 if (lun->be_lun->lun_type == T_PROCESSOR &&
6594 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6596 if (lun->be_lun->lun_type == T_CDROM &&
6597 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6601 * We don't use this subpage if the user didn't
6602 * request all subpages. We already checked (above)
6603 * to make sure the user only specified a subpage
6604 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6606 if ((page_index->subpage != 0)
6607 && (subpage == SMS_SUBPAGE_PAGE_0))
6611 * Call the handler, if it exists, to update the
6612 * page to the latest values.
6614 if (page_index->sense_handler != NULL)
6615 page_index->sense_handler(ctsio, page_index,pc);
6617 memcpy(ctsio->kern_data_ptr + data_used,
6618 page_index->page_data +
6619 (page_index->page_len * pc),
6620 page_index->page_len);
6621 data_used += page_index->page_len;
6628 data_used = header_len;
6630 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6631 struct ctl_page_index *page_index;
6633 page_index = &lun->mode_pages.index[i];
6635 /* Look for the right page code */
6636 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6639 /* Look for the right subpage or the subpage wildcard*/
6640 if ((page_index->subpage != subpage)
6641 && (subpage != SMS_SUBPAGE_ALL))
6644 /* Make sure the page is supported for this dev type */
6645 if (lun->be_lun->lun_type == T_DIRECT &&
6646 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6648 if (lun->be_lun->lun_type == T_PROCESSOR &&
6649 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6651 if (lun->be_lun->lun_type == T_CDROM &&
6652 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6656 * Call the handler, if it exists, to update the
6657 * page to the latest values.
6659 if (page_index->sense_handler != NULL)
6660 page_index->sense_handler(ctsio, page_index,pc);
6662 memcpy(ctsio->kern_data_ptr + data_used,
6663 page_index->page_data +
6664 (page_index->page_len * pc),
6665 page_index->page_len);
6666 data_used += page_index->page_len;
6672 ctl_set_success(ctsio);
6673 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6674 ctsio->be_move_done = ctl_config_move_done;
6675 ctl_datamove((union ctl_io *)ctsio);
6676 return (CTL_RETVAL_COMPLETE);
6680 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6681 struct ctl_page_index *page_index,
6684 struct ctl_lun *lun = CTL_LUN(ctsio);
6685 struct scsi_log_param_header *phdr;
6689 data = page_index->page_data;
6691 if (lun->backend->lun_attr != NULL &&
6692 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6694 phdr = (struct scsi_log_param_header *)data;
6695 scsi_ulto2b(0x0001, phdr->param_code);
6696 phdr->param_control = SLP_LBIN | SLP_LP;
6697 phdr->param_len = 8;
6698 data = (uint8_t *)(phdr + 1);
6699 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6700 data[4] = 0x02; /* per-pool */
6701 data += phdr->param_len;
6704 if (lun->backend->lun_attr != NULL &&
6705 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6707 phdr = (struct scsi_log_param_header *)data;
6708 scsi_ulto2b(0x0002, phdr->param_code);
6709 phdr->param_control = SLP_LBIN | SLP_LP;
6710 phdr->param_len = 8;
6711 data = (uint8_t *)(phdr + 1);
6712 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6713 data[4] = 0x01; /* per-LUN */
6714 data += phdr->param_len;
6717 if (lun->backend->lun_attr != NULL &&
6718 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6720 phdr = (struct scsi_log_param_header *)data;
6721 scsi_ulto2b(0x00f1, phdr->param_code);
6722 phdr->param_control = SLP_LBIN | SLP_LP;
6723 phdr->param_len = 8;
6724 data = (uint8_t *)(phdr + 1);
6725 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6726 data[4] = 0x02; /* per-pool */
6727 data += phdr->param_len;
6730 if (lun->backend->lun_attr != NULL &&
6731 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6733 phdr = (struct scsi_log_param_header *)data;
6734 scsi_ulto2b(0x00f2, phdr->param_code);
6735 phdr->param_control = SLP_LBIN | SLP_LP;
6736 phdr->param_len = 8;
6737 data = (uint8_t *)(phdr + 1);
6738 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6739 data[4] = 0x02; /* per-pool */
6740 data += phdr->param_len;
6743 page_index->page_len = data - page_index->page_data;
6748 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6749 struct ctl_page_index *page_index,
6752 struct ctl_lun *lun = CTL_LUN(ctsio);
6753 struct stat_page *data;
6756 data = (struct stat_page *)page_index->page_data;
6758 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6759 data->sap.hdr.param_control = SLP_LBIN;
6760 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6761 sizeof(struct scsi_log_param_header);
6762 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6763 data->sap.read_num);
6764 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6765 data->sap.write_num);
6766 if (lun->be_lun->blocksize > 0) {
6767 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6768 lun->be_lun->blocksize, data->sap.recvieved_lba);
6769 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6770 lun->be_lun->blocksize, data->sap.transmitted_lba);
6772 t = &lun->stats.time[CTL_STATS_READ];
6773 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6774 data->sap.read_int);
6775 t = &lun->stats.time[CTL_STATS_WRITE];
6776 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6777 data->sap.write_int);
6778 scsi_u64to8b(0, data->sap.weighted_num);
6779 scsi_u64to8b(0, data->sap.weighted_int);
6780 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6781 data->it.hdr.param_control = SLP_LBIN;
6782 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6783 sizeof(struct scsi_log_param_header);
6785 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6787 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6788 data->it.hdr.param_control = SLP_LBIN;
6789 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6790 sizeof(struct scsi_log_param_header);
6791 scsi_ulto4b(3, data->ti.exponent);
6792 scsi_ulto4b(1, data->ti.integer);
6797 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6798 struct ctl_page_index *page_index,
6801 struct ctl_lun *lun = CTL_LUN(ctsio);
6802 struct scsi_log_informational_exceptions *data;
6804 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6806 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6807 data->hdr.param_control = SLP_LBIN;
6808 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6809 sizeof(struct scsi_log_param_header);
6810 data->ie_asc = lun->ie_asc;
6811 data->ie_ascq = lun->ie_ascq;
6812 data->temperature = 0xff;
6817 ctl_log_sense(struct ctl_scsiio *ctsio)
6819 struct ctl_lun *lun = CTL_LUN(ctsio);
6820 int i, pc, page_code, subpage;
6821 int alloc_len, total_len;
6822 struct ctl_page_index *page_index;
6823 struct scsi_log_sense *cdb;
6824 struct scsi_log_header *header;
6826 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6828 cdb = (struct scsi_log_sense *)ctsio->cdb;
6829 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6830 page_code = cdb->page & SLS_PAGE_CODE;
6831 subpage = cdb->subpage;
6832 alloc_len = scsi_2btoul(cdb->length);
6835 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6836 page_index = &lun->log_pages.index[i];
6838 /* Look for the right page code */
6839 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6842 /* Look for the right subpage or the subpage wildcard*/
6843 if (page_index->subpage != subpage)
6848 if (i >= CTL_NUM_LOG_PAGES) {
6849 ctl_set_invalid_field(ctsio,
6855 ctl_done((union ctl_io *)ctsio);
6856 return (CTL_RETVAL_COMPLETE);
6859 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6861 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6862 ctsio->kern_sg_entries = 0;
6863 ctsio->kern_rel_offset = 0;
6864 ctsio->kern_data_len = min(total_len, alloc_len);
6865 ctsio->kern_total_len = ctsio->kern_data_len;
6867 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6868 header->page = page_index->page_code;
6869 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6870 header->page |= SL_DS;
6871 if (page_index->subpage) {
6872 header->page |= SL_SPF;
6873 header->subpage = page_index->subpage;
6875 scsi_ulto2b(page_index->page_len, header->datalen);
6878 * Call the handler, if it exists, to update the
6879 * page to the latest values.
6881 if (page_index->sense_handler != NULL)
6882 page_index->sense_handler(ctsio, page_index, pc);
6884 memcpy(header + 1, page_index->page_data, page_index->page_len);
6886 ctl_set_success(ctsio);
6887 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6888 ctsio->be_move_done = ctl_config_move_done;
6889 ctl_datamove((union ctl_io *)ctsio);
6890 return (CTL_RETVAL_COMPLETE);
6894 ctl_read_capacity(struct ctl_scsiio *ctsio)
6896 struct ctl_lun *lun = CTL_LUN(ctsio);
6897 struct scsi_read_capacity *cdb;
6898 struct scsi_read_capacity_data *data;
6901 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6903 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6905 lba = scsi_4btoul(cdb->addr);
6906 if (((cdb->pmi & SRC_PMI) == 0)
6908 ctl_set_invalid_field(/*ctsio*/ ctsio,
6914 ctl_done((union ctl_io *)ctsio);
6915 return (CTL_RETVAL_COMPLETE);
6918 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6919 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6920 ctsio->kern_data_len = sizeof(*data);
6921 ctsio->kern_total_len = sizeof(*data);
6922 ctsio->kern_rel_offset = 0;
6923 ctsio->kern_sg_entries = 0;
6926 * If the maximum LBA is greater than 0xfffffffe, the user must
6927 * issue a SERVICE ACTION IN (16) command, with the read capacity
6928 * serivce action set.
6930 if (lun->be_lun->maxlba > 0xfffffffe)
6931 scsi_ulto4b(0xffffffff, data->addr);
6933 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6936 * XXX KDM this may not be 512 bytes...
6938 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6940 ctl_set_success(ctsio);
6941 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6942 ctsio->be_move_done = ctl_config_move_done;
6943 ctl_datamove((union ctl_io *)ctsio);
6944 return (CTL_RETVAL_COMPLETE);
6948 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6950 struct ctl_lun *lun = CTL_LUN(ctsio);
6951 struct scsi_read_capacity_16 *cdb;
6952 struct scsi_read_capacity_data_long *data;
6956 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
6958 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
6960 alloc_len = scsi_4btoul(cdb->alloc_len);
6961 lba = scsi_8btou64(cdb->addr);
6963 if ((cdb->reladr & SRC16_PMI)
6965 ctl_set_invalid_field(/*ctsio*/ ctsio,
6971 ctl_done((union ctl_io *)ctsio);
6972 return (CTL_RETVAL_COMPLETE);
6975 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6976 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
6977 ctsio->kern_rel_offset = 0;
6978 ctsio->kern_sg_entries = 0;
6979 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
6980 ctsio->kern_total_len = ctsio->kern_data_len;
6982 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
6983 /* XXX KDM this may not be 512 bytes... */
6984 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6985 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
6986 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
6987 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
6988 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
6990 ctl_set_success(ctsio);
6991 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6992 ctsio->be_move_done = ctl_config_move_done;
6993 ctl_datamove((union ctl_io *)ctsio);
6994 return (CTL_RETVAL_COMPLETE);
6998 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7000 struct ctl_lun *lun = CTL_LUN(ctsio);
7001 struct scsi_get_lba_status *cdb;
7002 struct scsi_get_lba_status_data *data;
7003 struct ctl_lba_len_flags *lbalen;
7005 uint32_t alloc_len, total_len;
7008 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7010 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7011 lba = scsi_8btou64(cdb->addr);
7012 alloc_len = scsi_4btoul(cdb->alloc_len);
7014 if (lba > lun->be_lun->maxlba) {
7015 ctl_set_lba_out_of_range(ctsio, lba);
7016 ctl_done((union ctl_io *)ctsio);
7017 return (CTL_RETVAL_COMPLETE);
7020 total_len = sizeof(*data) + sizeof(data->descr[0]);
7021 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7022 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7023 ctsio->kern_rel_offset = 0;
7024 ctsio->kern_sg_entries = 0;
7025 ctsio->kern_data_len = min(total_len, alloc_len);
7026 ctsio->kern_total_len = ctsio->kern_data_len;
7028 /* Fill dummy data in case backend can't tell anything. */
7029 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7030 scsi_u64to8b(lba, data->descr[0].addr);
7031 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7032 data->descr[0].length);
7033 data->descr[0].status = 0; /* Mapped or unknown. */
7035 ctl_set_success(ctsio);
7036 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7037 ctsio->be_move_done = ctl_config_move_done;
7039 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7041 lbalen->len = total_len;
7043 retval = lun->backend->config_read((union ctl_io *)ctsio);
7048 ctl_read_defect(struct ctl_scsiio *ctsio)
7050 struct scsi_read_defect_data_10 *ccb10;
7051 struct scsi_read_defect_data_12 *ccb12;
7052 struct scsi_read_defect_data_hdr_10 *data10;
7053 struct scsi_read_defect_data_hdr_12 *data12;
7054 uint32_t alloc_len, data_len;
7057 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7059 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7060 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7061 format = ccb10->format;
7062 alloc_len = scsi_2btoul(ccb10->alloc_length);
7063 data_len = sizeof(*data10);
7065 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7066 format = ccb12->format;
7067 alloc_len = scsi_4btoul(ccb12->alloc_length);
7068 data_len = sizeof(*data12);
7070 if (alloc_len == 0) {
7071 ctl_set_success(ctsio);
7072 ctl_done((union ctl_io *)ctsio);
7073 return (CTL_RETVAL_COMPLETE);
7076 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7077 ctsio->kern_rel_offset = 0;
7078 ctsio->kern_sg_entries = 0;
7079 ctsio->kern_data_len = min(data_len, alloc_len);
7080 ctsio->kern_total_len = ctsio->kern_data_len;
7082 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7083 data10 = (struct scsi_read_defect_data_hdr_10 *)
7084 ctsio->kern_data_ptr;
7085 data10->format = format;
7086 scsi_ulto2b(0, data10->length);
7088 data12 = (struct scsi_read_defect_data_hdr_12 *)
7089 ctsio->kern_data_ptr;
7090 data12->format = format;
7091 scsi_ulto2b(0, data12->generation);
7092 scsi_ulto4b(0, data12->length);
7095 ctl_set_success(ctsio);
7096 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7097 ctsio->be_move_done = ctl_config_move_done;
7098 ctl_datamove((union ctl_io *)ctsio);
7099 return (CTL_RETVAL_COMPLETE);
7103 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7105 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7106 struct ctl_lun *lun = CTL_LUN(ctsio);
7107 struct scsi_maintenance_in *cdb;
7109 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7110 int num_ha_groups, num_target_ports, shared_group;
7111 struct ctl_port *port;
7112 struct scsi_target_group_data *rtg_ptr;
7113 struct scsi_target_group_data_extended *rtg_ext_ptr;
7114 struct scsi_target_port_group_descriptor *tpg_desc;
7116 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7118 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7119 retval = CTL_RETVAL_COMPLETE;
7121 switch (cdb->byte2 & STG_PDF_MASK) {
7122 case STG_PDF_LENGTH:
7125 case STG_PDF_EXTENDED:
7129 ctl_set_invalid_field(/*ctsio*/ ctsio,
7135 ctl_done((union ctl_io *)ctsio);
7139 num_target_ports = 0;
7140 shared_group = (softc->is_single != 0);
7141 mtx_lock(&softc->ctl_lock);
7142 STAILQ_FOREACH(port, &softc->port_list, links) {
7143 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7145 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7148 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7151 mtx_unlock(&softc->ctl_lock);
7152 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7155 total_len = sizeof(struct scsi_target_group_data_extended);
7157 total_len = sizeof(struct scsi_target_group_data);
7158 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7159 (shared_group + num_ha_groups) +
7160 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7162 alloc_len = scsi_4btoul(cdb->length);
7164 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7165 ctsio->kern_sg_entries = 0;
7166 ctsio->kern_rel_offset = 0;
7167 ctsio->kern_data_len = min(total_len, alloc_len);
7168 ctsio->kern_total_len = ctsio->kern_data_len;
7171 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7172 ctsio->kern_data_ptr;
7173 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7174 rtg_ext_ptr->format_type = 0x10;
7175 rtg_ext_ptr->implicit_transition_time = 0;
7176 tpg_desc = &rtg_ext_ptr->groups[0];
7178 rtg_ptr = (struct scsi_target_group_data *)
7179 ctsio->kern_data_ptr;
7180 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7181 tpg_desc = &rtg_ptr->groups[0];
7184 mtx_lock(&softc->ctl_lock);
7185 pg = softc->port_min / softc->port_cnt;
7186 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7187 /* Some shelf is known to be primary. */
7188 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7189 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7190 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7191 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7192 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7193 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7195 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7196 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7197 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7200 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7203 /* No known primary shelf. */
7204 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7205 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7206 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7207 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7208 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7209 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7211 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7215 tpg_desc->pref_state = ts;
7216 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7217 TPG_U_SUP | TPG_T_SUP;
7218 scsi_ulto2b(1, tpg_desc->target_port_group);
7219 tpg_desc->status = TPG_IMPLICIT;
7221 STAILQ_FOREACH(port, &softc->port_list, links) {
7222 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7224 if (!softc->is_single &&
7225 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7227 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7229 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7230 relative_target_port_identifier);
7233 tpg_desc->target_port_count = pc;
7234 tpg_desc = (struct scsi_target_port_group_descriptor *)
7235 &tpg_desc->descriptors[pc];
7237 for (g = 0; g < num_ha_groups; g++) {
7238 tpg_desc->pref_state = (g == pg) ? ts : os;
7239 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7240 TPG_U_SUP | TPG_T_SUP;
7241 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7242 tpg_desc->status = TPG_IMPLICIT;
7244 STAILQ_FOREACH(port, &softc->port_list, links) {
7245 if (port->targ_port < g * softc->port_cnt ||
7246 port->targ_port >= (g + 1) * softc->port_cnt)
7248 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7250 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7252 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7254 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7255 relative_target_port_identifier);
7258 tpg_desc->target_port_count = pc;
7259 tpg_desc = (struct scsi_target_port_group_descriptor *)
7260 &tpg_desc->descriptors[pc];
7262 mtx_unlock(&softc->ctl_lock);
7264 ctl_set_success(ctsio);
7265 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7266 ctsio->be_move_done = ctl_config_move_done;
7267 ctl_datamove((union ctl_io *)ctsio);
7272 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7274 struct ctl_lun *lun = CTL_LUN(ctsio);
7275 struct scsi_report_supported_opcodes *cdb;
7276 const struct ctl_cmd_entry *entry, *sentry;
7277 struct scsi_report_supported_opcodes_all *all;
7278 struct scsi_report_supported_opcodes_descr *descr;
7279 struct scsi_report_supported_opcodes_one *one;
7281 int alloc_len, total_len;
7282 int opcode, service_action, i, j, num;
7284 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7286 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7287 retval = CTL_RETVAL_COMPLETE;
7289 opcode = cdb->requested_opcode;
7290 service_action = scsi_2btoul(cdb->requested_service_action);
7291 switch (cdb->options & RSO_OPTIONS_MASK) {
7292 case RSO_OPTIONS_ALL:
7294 for (i = 0; i < 256; i++) {
7295 entry = &ctl_cmd_table[i];
7296 if (entry->flags & CTL_CMD_FLAG_SA5) {
7297 for (j = 0; j < 32; j++) {
7298 sentry = &((const struct ctl_cmd_entry *)
7300 if (ctl_cmd_applicable(
7301 lun->be_lun->lun_type, sentry))
7305 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7310 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7311 num * sizeof(struct scsi_report_supported_opcodes_descr);
7313 case RSO_OPTIONS_OC:
7314 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7315 ctl_set_invalid_field(/*ctsio*/ ctsio,
7321 ctl_done((union ctl_io *)ctsio);
7322 return (CTL_RETVAL_COMPLETE);
7324 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7326 case RSO_OPTIONS_OC_SA:
7327 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7328 service_action >= 32) {
7329 ctl_set_invalid_field(/*ctsio*/ ctsio,
7335 ctl_done((union ctl_io *)ctsio);
7336 return (CTL_RETVAL_COMPLETE);
7339 case RSO_OPTIONS_OC_ASA:
7340 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7343 ctl_set_invalid_field(/*ctsio*/ ctsio,
7349 ctl_done((union ctl_io *)ctsio);
7350 return (CTL_RETVAL_COMPLETE);
7353 alloc_len = scsi_4btoul(cdb->length);
7355 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7356 ctsio->kern_sg_entries = 0;
7357 ctsio->kern_rel_offset = 0;
7358 ctsio->kern_data_len = min(total_len, alloc_len);
7359 ctsio->kern_total_len = ctsio->kern_data_len;
7361 switch (cdb->options & RSO_OPTIONS_MASK) {
7362 case RSO_OPTIONS_ALL:
7363 all = (struct scsi_report_supported_opcodes_all *)
7364 ctsio->kern_data_ptr;
7366 for (i = 0; i < 256; i++) {
7367 entry = &ctl_cmd_table[i];
7368 if (entry->flags & CTL_CMD_FLAG_SA5) {
7369 for (j = 0; j < 32; j++) {
7370 sentry = &((const struct ctl_cmd_entry *)
7372 if (!ctl_cmd_applicable(
7373 lun->be_lun->lun_type, sentry))
7375 descr = &all->descr[num++];
7377 scsi_ulto2b(j, descr->service_action);
7378 descr->flags = RSO_SERVACTV;
7379 scsi_ulto2b(sentry->length,
7383 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7386 descr = &all->descr[num++];
7388 scsi_ulto2b(0, descr->service_action);
7390 scsi_ulto2b(entry->length, descr->cdb_length);
7394 num * sizeof(struct scsi_report_supported_opcodes_descr),
7397 case RSO_OPTIONS_OC:
7398 one = (struct scsi_report_supported_opcodes_one *)
7399 ctsio->kern_data_ptr;
7400 entry = &ctl_cmd_table[opcode];
7402 case RSO_OPTIONS_OC_SA:
7403 one = (struct scsi_report_supported_opcodes_one *)
7404 ctsio->kern_data_ptr;
7405 entry = &ctl_cmd_table[opcode];
7406 entry = &((const struct ctl_cmd_entry *)
7407 entry->execute)[service_action];
7409 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7411 scsi_ulto2b(entry->length, one->cdb_length);
7412 one->cdb_usage[0] = opcode;
7413 memcpy(&one->cdb_usage[1], entry->usage,
7418 case RSO_OPTIONS_OC_ASA:
7419 one = (struct scsi_report_supported_opcodes_one *)
7420 ctsio->kern_data_ptr;
7421 entry = &ctl_cmd_table[opcode];
7422 if (entry->flags & CTL_CMD_FLAG_SA5) {
7423 entry = &((const struct ctl_cmd_entry *)
7424 entry->execute)[service_action];
7425 } else if (service_action != 0) {
7432 ctl_set_success(ctsio);
7433 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7434 ctsio->be_move_done = ctl_config_move_done;
7435 ctl_datamove((union ctl_io *)ctsio);
7440 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7442 struct scsi_report_supported_tmf *cdb;
7443 struct scsi_report_supported_tmf_ext_data *data;
7445 int alloc_len, total_len;
7447 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7449 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7451 retval = CTL_RETVAL_COMPLETE;
7453 if (cdb->options & RST_REPD)
7454 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7456 total_len = sizeof(struct scsi_report_supported_tmf_data);
7457 alloc_len = scsi_4btoul(cdb->length);
7459 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7460 ctsio->kern_sg_entries = 0;
7461 ctsio->kern_rel_offset = 0;
7462 ctsio->kern_data_len = min(total_len, alloc_len);
7463 ctsio->kern_total_len = ctsio->kern_data_len;
7465 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7466 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7468 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7469 data->length = total_len - 4;
7471 ctl_set_success(ctsio);
7472 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7473 ctsio->be_move_done = ctl_config_move_done;
7474 ctl_datamove((union ctl_io *)ctsio);
7479 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7481 struct scsi_report_timestamp *cdb;
7482 struct scsi_report_timestamp_data *data;
7486 int alloc_len, total_len;
7488 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7490 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7492 retval = CTL_RETVAL_COMPLETE;
7494 total_len = sizeof(struct scsi_report_timestamp_data);
7495 alloc_len = scsi_4btoul(cdb->length);
7497 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7498 ctsio->kern_sg_entries = 0;
7499 ctsio->kern_rel_offset = 0;
7500 ctsio->kern_data_len = min(total_len, alloc_len);
7501 ctsio->kern_total_len = ctsio->kern_data_len;
7503 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7504 scsi_ulto2b(sizeof(*data) - 2, data->length);
7505 data->origin = RTS_ORIG_OUTSIDE;
7507 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7508 scsi_ulto4b(timestamp >> 16, data->timestamp);
7509 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7511 ctl_set_success(ctsio);
7512 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7513 ctsio->be_move_done = ctl_config_move_done;
7514 ctl_datamove((union ctl_io *)ctsio);
7519 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7521 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7522 struct ctl_lun *lun = CTL_LUN(ctsio);
7523 struct scsi_per_res_in *cdb;
7524 int alloc_len, total_len = 0;
7525 /* struct scsi_per_res_in_rsrv in_data; */
7528 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7530 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7532 alloc_len = scsi_2btoul(cdb->length);
7535 mtx_lock(&lun->lun_lock);
7536 switch (cdb->action) {
7537 case SPRI_RK: /* read keys */
7538 total_len = sizeof(struct scsi_per_res_in_keys) +
7540 sizeof(struct scsi_per_res_key);
7542 case SPRI_RR: /* read reservation */
7543 if (lun->flags & CTL_LUN_PR_RESERVED)
7544 total_len = sizeof(struct scsi_per_res_in_rsrv);
7546 total_len = sizeof(struct scsi_per_res_in_header);
7548 case SPRI_RC: /* report capabilities */
7549 total_len = sizeof(struct scsi_per_res_cap);
7551 case SPRI_RS: /* read full status */
7552 total_len = sizeof(struct scsi_per_res_in_header) +
7553 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7557 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7559 mtx_unlock(&lun->lun_lock);
7561 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7562 ctsio->kern_rel_offset = 0;
7563 ctsio->kern_sg_entries = 0;
7564 ctsio->kern_data_len = min(total_len, alloc_len);
7565 ctsio->kern_total_len = ctsio->kern_data_len;
7567 mtx_lock(&lun->lun_lock);
7568 switch (cdb->action) {
7569 case SPRI_RK: { // read keys
7570 struct scsi_per_res_in_keys *res_keys;
7573 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7576 * We had to drop the lock to allocate our buffer, which
7577 * leaves time for someone to come in with another
7578 * persistent reservation. (That is unlikely, though,
7579 * since this should be the only persistent reservation
7580 * command active right now.)
7582 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7583 (lun->pr_key_count *
7584 sizeof(struct scsi_per_res_key)))){
7585 mtx_unlock(&lun->lun_lock);
7586 free(ctsio->kern_data_ptr, M_CTL);
7587 printf("%s: reservation length changed, retrying\n",
7592 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7594 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7595 lun->pr_key_count, res_keys->header.length);
7597 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7598 if ((key = ctl_get_prkey(lun, i)) == 0)
7602 * We used lun->pr_key_count to calculate the
7603 * size to allocate. If it turns out the number of
7604 * initiators with the registered flag set is
7605 * larger than that (i.e. they haven't been kept in
7606 * sync), we've got a problem.
7608 if (key_count >= lun->pr_key_count) {
7612 scsi_u64to8b(key, res_keys->keys[key_count].key);
7617 case SPRI_RR: { // read reservation
7618 struct scsi_per_res_in_rsrv *res;
7619 int tmp_len, header_only;
7621 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7623 scsi_ulto4b(lun->pr_generation, res->header.generation);
7625 if (lun->flags & CTL_LUN_PR_RESERVED)
7627 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7628 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7629 res->header.length);
7632 tmp_len = sizeof(struct scsi_per_res_in_header);
7633 scsi_ulto4b(0, res->header.length);
7638 * We had to drop the lock to allocate our buffer, which
7639 * leaves time for someone to come in with another
7640 * persistent reservation. (That is unlikely, though,
7641 * since this should be the only persistent reservation
7642 * command active right now.)
7644 if (tmp_len != total_len) {
7645 mtx_unlock(&lun->lun_lock);
7646 free(ctsio->kern_data_ptr, M_CTL);
7647 printf("%s: reservation status changed, retrying\n",
7653 * No reservation held, so we're done.
7655 if (header_only != 0)
7659 * If the registration is an All Registrants type, the key
7660 * is 0, since it doesn't really matter.
7662 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7663 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7664 res->data.reservation);
7666 res->data.scopetype = lun->pr_res_type;
7669 case SPRI_RC: //report capabilities
7671 struct scsi_per_res_cap *res_cap;
7674 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7675 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7676 res_cap->flags1 = SPRI_CRH;
7677 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7678 type_mask = SPRI_TM_WR_EX_AR |
7684 scsi_ulto2b(type_mask, res_cap->type_mask);
7687 case SPRI_RS: { // read full status
7688 struct scsi_per_res_in_full *res_status;
7689 struct scsi_per_res_in_full_desc *res_desc;
7690 struct ctl_port *port;
7693 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7696 * We had to drop the lock to allocate our buffer, which
7697 * leaves time for someone to come in with another
7698 * persistent reservation. (That is unlikely, though,
7699 * since this should be the only persistent reservation
7700 * command active right now.)
7702 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7703 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7704 lun->pr_key_count)){
7705 mtx_unlock(&lun->lun_lock);
7706 free(ctsio->kern_data_ptr, M_CTL);
7707 printf("%s: reservation length changed, retrying\n",
7712 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7714 res_desc = &res_status->desc[0];
7715 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7716 if ((key = ctl_get_prkey(lun, i)) == 0)
7719 scsi_u64to8b(key, res_desc->res_key.key);
7720 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7721 (lun->pr_res_idx == i ||
7722 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7723 res_desc->flags = SPRI_FULL_R_HOLDER;
7724 res_desc->scopetype = lun->pr_res_type;
7726 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7727 res_desc->rel_trgt_port_id);
7729 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7731 len = ctl_create_iid(port,
7732 i % CTL_MAX_INIT_PER_PORT,
7733 res_desc->transport_id);
7734 scsi_ulto4b(len, res_desc->additional_length);
7735 res_desc = (struct scsi_per_res_in_full_desc *)
7736 &res_desc->transport_id[len];
7738 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7739 res_status->header.length);
7743 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7745 mtx_unlock(&lun->lun_lock);
7747 ctl_set_success(ctsio);
7748 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7749 ctsio->be_move_done = ctl_config_move_done;
7750 ctl_datamove((union ctl_io *)ctsio);
7751 return (CTL_RETVAL_COMPLETE);
7755 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7759 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7760 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7761 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7762 struct scsi_per_res_out_parms* param)
7764 union ctl_ha_msg persis_io;
7767 mtx_lock(&lun->lun_lock);
7768 if (sa_res_key == 0) {
7769 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7770 /* validate scope and type */
7771 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7773 mtx_unlock(&lun->lun_lock);
7774 ctl_set_invalid_field(/*ctsio*/ ctsio,
7780 ctl_done((union ctl_io *)ctsio);
7784 if (type>8 || type==2 || type==4 || type==0) {
7785 mtx_unlock(&lun->lun_lock);
7786 ctl_set_invalid_field(/*ctsio*/ ctsio,
7792 ctl_done((union ctl_io *)ctsio);
7797 * Unregister everybody else and build UA for
7800 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7801 if (i == residx || ctl_get_prkey(lun, i) == 0)
7804 ctl_clr_prkey(lun, i);
7805 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7807 lun->pr_key_count = 1;
7808 lun->pr_res_type = type;
7809 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7810 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7811 lun->pr_res_idx = residx;
7812 lun->pr_generation++;
7813 mtx_unlock(&lun->lun_lock);
7815 /* send msg to other side */
7816 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7817 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7818 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7819 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7820 persis_io.pr.pr_info.res_type = type;
7821 memcpy(persis_io.pr.pr_info.sa_res_key,
7822 param->serv_act_res_key,
7823 sizeof(param->serv_act_res_key));
7824 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7825 sizeof(persis_io.pr), M_WAITOK);
7827 /* not all registrants */
7828 mtx_unlock(&lun->lun_lock);
7829 free(ctsio->kern_data_ptr, M_CTL);
7830 ctl_set_invalid_field(ctsio,
7836 ctl_done((union ctl_io *)ctsio);
7839 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7840 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7843 if (res_key == sa_res_key) {
7846 * The spec implies this is not good but doesn't
7847 * say what to do. There are two choices either
7848 * generate a res conflict or check condition
7849 * with illegal field in parameter data. Since
7850 * that is what is done when the sa_res_key is
7851 * zero I'll take that approach since this has
7852 * to do with the sa_res_key.
7854 mtx_unlock(&lun->lun_lock);
7855 free(ctsio->kern_data_ptr, M_CTL);
7856 ctl_set_invalid_field(ctsio,
7862 ctl_done((union ctl_io *)ctsio);
7866 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7867 if (ctl_get_prkey(lun, i) != sa_res_key)
7871 ctl_clr_prkey(lun, i);
7872 lun->pr_key_count--;
7873 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7876 mtx_unlock(&lun->lun_lock);
7877 free(ctsio->kern_data_ptr, M_CTL);
7878 ctl_set_reservation_conflict(ctsio);
7879 ctl_done((union ctl_io *)ctsio);
7880 return (CTL_RETVAL_COMPLETE);
7882 lun->pr_generation++;
7883 mtx_unlock(&lun->lun_lock);
7885 /* send msg to other side */
7886 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7887 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7888 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7889 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7890 persis_io.pr.pr_info.res_type = type;
7891 memcpy(persis_io.pr.pr_info.sa_res_key,
7892 param->serv_act_res_key,
7893 sizeof(param->serv_act_res_key));
7894 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7895 sizeof(persis_io.pr), M_WAITOK);
7897 /* Reserved but not all registrants */
7898 /* sa_res_key is res holder */
7899 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7900 /* validate scope and type */
7901 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7903 mtx_unlock(&lun->lun_lock);
7904 ctl_set_invalid_field(/*ctsio*/ ctsio,
7910 ctl_done((union ctl_io *)ctsio);
7914 if (type>8 || type==2 || type==4 || type==0) {
7915 mtx_unlock(&lun->lun_lock);
7916 ctl_set_invalid_field(/*ctsio*/ ctsio,
7922 ctl_done((union ctl_io *)ctsio);
7928 * if sa_res_key != res_key remove all
7929 * registrants w/sa_res_key and generate UA
7930 * for these registrants(Registrations
7931 * Preempted) if it wasn't an exclusive
7932 * reservation generate UA(Reservations
7933 * Preempted) for all other registered nexuses
7934 * if the type has changed. Establish the new
7935 * reservation and holder. If res_key and
7936 * sa_res_key are the same do the above
7937 * except don't unregister the res holder.
7940 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7941 if (i == residx || ctl_get_prkey(lun, i) == 0)
7944 if (sa_res_key == ctl_get_prkey(lun, i)) {
7945 ctl_clr_prkey(lun, i);
7946 lun->pr_key_count--;
7947 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7948 } else if (type != lun->pr_res_type &&
7949 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7950 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
7951 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7954 lun->pr_res_type = type;
7955 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7956 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7957 lun->pr_res_idx = residx;
7959 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
7960 lun->pr_generation++;
7961 mtx_unlock(&lun->lun_lock);
7963 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7964 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7965 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7966 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7967 persis_io.pr.pr_info.res_type = type;
7968 memcpy(persis_io.pr.pr_info.sa_res_key,
7969 param->serv_act_res_key,
7970 sizeof(param->serv_act_res_key));
7971 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7972 sizeof(persis_io.pr), M_WAITOK);
7975 * sa_res_key is not the res holder just
7976 * remove registrants
7980 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7981 if (sa_res_key != ctl_get_prkey(lun, i))
7985 ctl_clr_prkey(lun, i);
7986 lun->pr_key_count--;
7987 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7991 mtx_unlock(&lun->lun_lock);
7992 free(ctsio->kern_data_ptr, M_CTL);
7993 ctl_set_reservation_conflict(ctsio);
7994 ctl_done((union ctl_io *)ctsio);
7997 lun->pr_generation++;
7998 mtx_unlock(&lun->lun_lock);
8000 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8001 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8002 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8003 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8004 persis_io.pr.pr_info.res_type = type;
8005 memcpy(persis_io.pr.pr_info.sa_res_key,
8006 param->serv_act_res_key,
8007 sizeof(param->serv_act_res_key));
8008 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8009 sizeof(persis_io.pr), M_WAITOK);
8016 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8018 uint64_t sa_res_key;
8021 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8023 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8024 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8025 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8026 if (sa_res_key == 0) {
8028 * Unregister everybody else and build UA for
8031 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8032 if (i == msg->pr.pr_info.residx ||
8033 ctl_get_prkey(lun, i) == 0)
8036 ctl_clr_prkey(lun, i);
8037 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8040 lun->pr_key_count = 1;
8041 lun->pr_res_type = msg->pr.pr_info.res_type;
8042 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8043 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8044 lun->pr_res_idx = msg->pr.pr_info.residx;
8046 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8047 if (sa_res_key == ctl_get_prkey(lun, i))
8050 ctl_clr_prkey(lun, i);
8051 lun->pr_key_count--;
8052 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8056 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8057 if (i == msg->pr.pr_info.residx ||
8058 ctl_get_prkey(lun, i) == 0)
8061 if (sa_res_key == ctl_get_prkey(lun, i)) {
8062 ctl_clr_prkey(lun, i);
8063 lun->pr_key_count--;
8064 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8065 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8066 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8067 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8068 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8071 lun->pr_res_type = msg->pr.pr_info.res_type;
8072 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8073 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8074 lun->pr_res_idx = msg->pr.pr_info.residx;
8076 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8078 lun->pr_generation++;
8084 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8086 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8087 struct ctl_lun *lun = CTL_LUN(ctsio);
8089 u_int32_t param_len;
8090 struct scsi_per_res_out *cdb;
8091 struct scsi_per_res_out_parms* param;
8093 uint64_t res_key, sa_res_key, key;
8095 union ctl_ha_msg persis_io;
8098 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8100 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8101 retval = CTL_RETVAL_COMPLETE;
8104 * We only support whole-LUN scope. The scope & type are ignored for
8105 * register, register and ignore existing key and clear.
8106 * We sometimes ignore scope and type on preempts too!!
8107 * Verify reservation type here as well.
8109 type = cdb->scope_type & SPR_TYPE_MASK;
8110 if ((cdb->action == SPRO_RESERVE)
8111 || (cdb->action == SPRO_RELEASE)) {
8112 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8113 ctl_set_invalid_field(/*ctsio*/ ctsio,
8119 ctl_done((union ctl_io *)ctsio);
8120 return (CTL_RETVAL_COMPLETE);
8123 if (type>8 || type==2 || type==4 || type==0) {
8124 ctl_set_invalid_field(/*ctsio*/ ctsio,
8130 ctl_done((union ctl_io *)ctsio);
8131 return (CTL_RETVAL_COMPLETE);
8135 param_len = scsi_4btoul(cdb->length);
8137 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8138 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8139 ctsio->kern_data_len = param_len;
8140 ctsio->kern_total_len = param_len;
8141 ctsio->kern_rel_offset = 0;
8142 ctsio->kern_sg_entries = 0;
8143 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8144 ctsio->be_move_done = ctl_config_move_done;
8145 ctl_datamove((union ctl_io *)ctsio);
8147 return (CTL_RETVAL_COMPLETE);
8150 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8152 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8153 res_key = scsi_8btou64(param->res_key.key);
8154 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8157 * Validate the reservation key here except for SPRO_REG_IGNO
8158 * This must be done for all other service actions
8160 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8161 mtx_lock(&lun->lun_lock);
8162 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8163 if (res_key != key) {
8165 * The current key passed in doesn't match
8166 * the one the initiator previously
8169 mtx_unlock(&lun->lun_lock);
8170 free(ctsio->kern_data_ptr, M_CTL);
8171 ctl_set_reservation_conflict(ctsio);
8172 ctl_done((union ctl_io *)ctsio);
8173 return (CTL_RETVAL_COMPLETE);
8175 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8177 * We are not registered
8179 mtx_unlock(&lun->lun_lock);
8180 free(ctsio->kern_data_ptr, M_CTL);
8181 ctl_set_reservation_conflict(ctsio);
8182 ctl_done((union ctl_io *)ctsio);
8183 return (CTL_RETVAL_COMPLETE);
8184 } else if (res_key != 0) {
8186 * We are not registered and trying to register but
8187 * the register key isn't zero.
8189 mtx_unlock(&lun->lun_lock);
8190 free(ctsio->kern_data_ptr, M_CTL);
8191 ctl_set_reservation_conflict(ctsio);
8192 ctl_done((union ctl_io *)ctsio);
8193 return (CTL_RETVAL_COMPLETE);
8195 mtx_unlock(&lun->lun_lock);
8198 switch (cdb->action & SPRO_ACTION_MASK) {
8200 case SPRO_REG_IGNO: {
8203 * We don't support any of these options, as we report in
8204 * the read capabilities request (see
8205 * ctl_persistent_reserve_in(), above).
8207 if ((param->flags & SPR_SPEC_I_PT)
8208 || (param->flags & SPR_ALL_TG_PT)
8209 || (param->flags & SPR_APTPL)) {
8212 if (param->flags & SPR_APTPL)
8214 else if (param->flags & SPR_ALL_TG_PT)
8216 else /* SPR_SPEC_I_PT */
8219 free(ctsio->kern_data_ptr, M_CTL);
8220 ctl_set_invalid_field(ctsio,
8226 ctl_done((union ctl_io *)ctsio);
8227 return (CTL_RETVAL_COMPLETE);
8230 mtx_lock(&lun->lun_lock);
8233 * The initiator wants to clear the
8236 if (sa_res_key == 0) {
8238 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8239 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8240 && ctl_get_prkey(lun, residx) == 0)) {
8241 mtx_unlock(&lun->lun_lock);
8245 ctl_clr_prkey(lun, residx);
8246 lun->pr_key_count--;
8248 if (residx == lun->pr_res_idx) {
8249 lun->flags &= ~CTL_LUN_PR_RESERVED;
8250 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8252 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8253 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8254 lun->pr_key_count) {
8256 * If the reservation is a registrants
8257 * only type we need to generate a UA
8258 * for other registered inits. The
8259 * sense code should be RESERVATIONS
8263 for (i = softc->init_min; i < softc->init_max; i++){
8264 if (ctl_get_prkey(lun, i) == 0)
8267 CTL_UA_RES_RELEASE);
8270 lun->pr_res_type = 0;
8271 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8272 if (lun->pr_key_count==0) {
8273 lun->flags &= ~CTL_LUN_PR_RESERVED;
8274 lun->pr_res_type = 0;
8275 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8278 lun->pr_generation++;
8279 mtx_unlock(&lun->lun_lock);
8281 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8282 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8283 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8284 persis_io.pr.pr_info.residx = residx;
8285 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8286 sizeof(persis_io.pr), M_WAITOK);
8287 } else /* sa_res_key != 0 */ {
8290 * If we aren't registered currently then increment
8291 * the key count and set the registered flag.
8293 ctl_alloc_prkey(lun, residx);
8294 if (ctl_get_prkey(lun, residx) == 0)
8295 lun->pr_key_count++;
8296 ctl_set_prkey(lun, residx, sa_res_key);
8297 lun->pr_generation++;
8298 mtx_unlock(&lun->lun_lock);
8300 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8301 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8302 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8303 persis_io.pr.pr_info.residx = residx;
8304 memcpy(persis_io.pr.pr_info.sa_res_key,
8305 param->serv_act_res_key,
8306 sizeof(param->serv_act_res_key));
8307 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8308 sizeof(persis_io.pr), M_WAITOK);
8314 mtx_lock(&lun->lun_lock);
8315 if (lun->flags & CTL_LUN_PR_RESERVED) {
8317 * if this isn't the reservation holder and it's
8318 * not a "all registrants" type or if the type is
8319 * different then we have a conflict
8321 if ((lun->pr_res_idx != residx
8322 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8323 || lun->pr_res_type != type) {
8324 mtx_unlock(&lun->lun_lock);
8325 free(ctsio->kern_data_ptr, M_CTL);
8326 ctl_set_reservation_conflict(ctsio);
8327 ctl_done((union ctl_io *)ctsio);
8328 return (CTL_RETVAL_COMPLETE);
8330 mtx_unlock(&lun->lun_lock);
8331 } else /* create a reservation */ {
8333 * If it's not an "all registrants" type record
8334 * reservation holder
8336 if (type != SPR_TYPE_WR_EX_AR
8337 && type != SPR_TYPE_EX_AC_AR)
8338 lun->pr_res_idx = residx; /* Res holder */
8340 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8342 lun->flags |= CTL_LUN_PR_RESERVED;
8343 lun->pr_res_type = type;
8345 mtx_unlock(&lun->lun_lock);
8347 /* send msg to other side */
8348 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8349 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8350 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8351 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8352 persis_io.pr.pr_info.res_type = type;
8353 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8354 sizeof(persis_io.pr), M_WAITOK);
8359 mtx_lock(&lun->lun_lock);
8360 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8361 /* No reservation exists return good status */
8362 mtx_unlock(&lun->lun_lock);
8366 * Is this nexus a reservation holder?
8368 if (lun->pr_res_idx != residx
8369 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8371 * not a res holder return good status but
8374 mtx_unlock(&lun->lun_lock);
8378 if (lun->pr_res_type != type) {
8379 mtx_unlock(&lun->lun_lock);
8380 free(ctsio->kern_data_ptr, M_CTL);
8381 ctl_set_illegal_pr_release(ctsio);
8382 ctl_done((union ctl_io *)ctsio);
8383 return (CTL_RETVAL_COMPLETE);
8386 /* okay to release */
8387 lun->flags &= ~CTL_LUN_PR_RESERVED;
8388 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8389 lun->pr_res_type = 0;
8392 * If this isn't an exclusive access reservation and NUAR
8393 * is not set, generate UA for all other registrants.
8395 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8396 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8397 for (i = softc->init_min; i < softc->init_max; i++) {
8398 if (i == residx || ctl_get_prkey(lun, i) == 0)
8400 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8403 mtx_unlock(&lun->lun_lock);
8405 /* Send msg to other side */
8406 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8407 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8408 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8409 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8410 sizeof(persis_io.pr), M_WAITOK);
8414 /* send msg to other side */
8416 mtx_lock(&lun->lun_lock);
8417 lun->flags &= ~CTL_LUN_PR_RESERVED;
8418 lun->pr_res_type = 0;
8419 lun->pr_key_count = 0;
8420 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8422 ctl_clr_prkey(lun, residx);
8423 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8424 if (ctl_get_prkey(lun, i) != 0) {
8425 ctl_clr_prkey(lun, i);
8426 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8428 lun->pr_generation++;
8429 mtx_unlock(&lun->lun_lock);
8431 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8432 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8433 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8434 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8435 sizeof(persis_io.pr), M_WAITOK);
8439 case SPRO_PRE_ABO: {
8442 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8443 residx, ctsio, cdb, param);
8445 return (CTL_RETVAL_COMPLETE);
8449 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8453 free(ctsio->kern_data_ptr, M_CTL);
8454 ctl_set_success(ctsio);
8455 ctl_done((union ctl_io *)ctsio);
8461 * This routine is for handling a message from the other SC pertaining to
8462 * persistent reserve out. All the error checking will have been done
8463 * so only perorming the action need be done here to keep the two
8467 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8469 struct ctl_softc *softc = CTL_SOFTC(io);
8470 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8471 struct ctl_lun *lun;
8473 uint32_t residx, targ_lun;
8475 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8476 mtx_lock(&softc->ctl_lock);
8477 if (targ_lun >= ctl_max_luns ||
8478 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8479 mtx_unlock(&softc->ctl_lock);
8482 mtx_lock(&lun->lun_lock);
8483 mtx_unlock(&softc->ctl_lock);
8484 if (lun->flags & CTL_LUN_DISABLED) {
8485 mtx_unlock(&lun->lun_lock);
8488 residx = ctl_get_initindex(&msg->hdr.nexus);
8489 switch(msg->pr.pr_info.action) {
8490 case CTL_PR_REG_KEY:
8491 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8492 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8493 lun->pr_key_count++;
8494 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8495 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8496 lun->pr_generation++;
8499 case CTL_PR_UNREG_KEY:
8500 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8501 lun->pr_key_count--;
8503 /* XXX Need to see if the reservation has been released */
8504 /* if so do we need to generate UA? */
8505 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8506 lun->flags &= ~CTL_LUN_PR_RESERVED;
8507 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8509 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8510 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8511 lun->pr_key_count) {
8513 * If the reservation is a registrants
8514 * only type we need to generate a UA
8515 * for other registered inits. The
8516 * sense code should be RESERVATIONS
8520 for (i = softc->init_min; i < softc->init_max; i++) {
8521 if (ctl_get_prkey(lun, i) == 0)
8524 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8527 lun->pr_res_type = 0;
8528 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8529 if (lun->pr_key_count==0) {
8530 lun->flags &= ~CTL_LUN_PR_RESERVED;
8531 lun->pr_res_type = 0;
8532 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8535 lun->pr_generation++;
8538 case CTL_PR_RESERVE:
8539 lun->flags |= CTL_LUN_PR_RESERVED;
8540 lun->pr_res_type = msg->pr.pr_info.res_type;
8541 lun->pr_res_idx = msg->pr.pr_info.residx;
8545 case CTL_PR_RELEASE:
8547 * If this isn't an exclusive access reservation and NUAR
8548 * is not set, generate UA for all other registrants.
8550 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8551 lun->pr_res_type != SPR_TYPE_WR_EX &&
8552 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8553 for (i = softc->init_min; i < softc->init_max; i++) {
8554 if (i == residx || ctl_get_prkey(lun, i) == 0)
8556 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8560 lun->flags &= ~CTL_LUN_PR_RESERVED;
8561 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8562 lun->pr_res_type = 0;
8565 case CTL_PR_PREEMPT:
8566 ctl_pro_preempt_other(lun, msg);
8569 lun->flags &= ~CTL_LUN_PR_RESERVED;
8570 lun->pr_res_type = 0;
8571 lun->pr_key_count = 0;
8572 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8574 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8575 if (ctl_get_prkey(lun, i) == 0)
8577 ctl_clr_prkey(lun, i);
8578 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8580 lun->pr_generation++;
8584 mtx_unlock(&lun->lun_lock);
8588 ctl_read_write(struct ctl_scsiio *ctsio)
8590 struct ctl_lun *lun = CTL_LUN(ctsio);
8591 struct ctl_lba_len_flags *lbalen;
8593 uint32_t num_blocks;
8597 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8600 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8601 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8602 switch (ctsio->cdb[0]) {
8605 struct scsi_rw_6 *cdb;
8607 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8609 lba = scsi_3btoul(cdb->addr);
8610 /* only 5 bits are valid in the most significant address byte */
8612 num_blocks = cdb->length;
8614 * This is correct according to SBC-2.
8616 if (num_blocks == 0)
8622 struct scsi_rw_10 *cdb;
8624 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8625 if (cdb->byte2 & SRW10_FUA)
8626 flags |= CTL_LLF_FUA;
8627 if (cdb->byte2 & SRW10_DPO)
8628 flags |= CTL_LLF_DPO;
8629 lba = scsi_4btoul(cdb->addr);
8630 num_blocks = scsi_2btoul(cdb->length);
8633 case WRITE_VERIFY_10: {
8634 struct scsi_write_verify_10 *cdb;
8636 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8637 flags |= CTL_LLF_FUA;
8638 if (cdb->byte2 & SWV_DPO)
8639 flags |= CTL_LLF_DPO;
8640 lba = scsi_4btoul(cdb->addr);
8641 num_blocks = scsi_2btoul(cdb->length);
8646 struct scsi_rw_12 *cdb;
8648 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8649 if (cdb->byte2 & SRW12_FUA)
8650 flags |= CTL_LLF_FUA;
8651 if (cdb->byte2 & SRW12_DPO)
8652 flags |= CTL_LLF_DPO;
8653 lba = scsi_4btoul(cdb->addr);
8654 num_blocks = scsi_4btoul(cdb->length);
8657 case WRITE_VERIFY_12: {
8658 struct scsi_write_verify_12 *cdb;
8660 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8661 flags |= CTL_LLF_FUA;
8662 if (cdb->byte2 & SWV_DPO)
8663 flags |= CTL_LLF_DPO;
8664 lba = scsi_4btoul(cdb->addr);
8665 num_blocks = scsi_4btoul(cdb->length);
8670 struct scsi_rw_16 *cdb;
8672 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8673 if (cdb->byte2 & SRW12_FUA)
8674 flags |= CTL_LLF_FUA;
8675 if (cdb->byte2 & SRW12_DPO)
8676 flags |= CTL_LLF_DPO;
8677 lba = scsi_8btou64(cdb->addr);
8678 num_blocks = scsi_4btoul(cdb->length);
8681 case WRITE_ATOMIC_16: {
8682 struct scsi_write_atomic_16 *cdb;
8684 if (lun->be_lun->atomicblock == 0) {
8685 ctl_set_invalid_opcode(ctsio);
8686 ctl_done((union ctl_io *)ctsio);
8687 return (CTL_RETVAL_COMPLETE);
8690 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8691 if (cdb->byte2 & SRW12_FUA)
8692 flags |= CTL_LLF_FUA;
8693 if (cdb->byte2 & SRW12_DPO)
8694 flags |= CTL_LLF_DPO;
8695 lba = scsi_8btou64(cdb->addr);
8696 num_blocks = scsi_2btoul(cdb->length);
8697 if (num_blocks > lun->be_lun->atomicblock) {
8698 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8699 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8701 ctl_done((union ctl_io *)ctsio);
8702 return (CTL_RETVAL_COMPLETE);
8706 case WRITE_VERIFY_16: {
8707 struct scsi_write_verify_16 *cdb;
8709 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8710 flags |= CTL_LLF_FUA;
8711 if (cdb->byte2 & SWV_DPO)
8712 flags |= CTL_LLF_DPO;
8713 lba = scsi_8btou64(cdb->addr);
8714 num_blocks = scsi_4btoul(cdb->length);
8719 * We got a command we don't support. This shouldn't
8720 * happen, commands should be filtered out above us.
8722 ctl_set_invalid_opcode(ctsio);
8723 ctl_done((union ctl_io *)ctsio);
8725 return (CTL_RETVAL_COMPLETE);
8726 break; /* NOTREACHED */
8730 * The first check is to make sure we're in bounds, the second
8731 * check is to catch wrap-around problems. If the lba + num blocks
8732 * is less than the lba, then we've wrapped around and the block
8733 * range is invalid anyway.
8735 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8736 || ((lba + num_blocks) < lba)) {
8737 ctl_set_lba_out_of_range(ctsio,
8738 MAX(lba, lun->be_lun->maxlba + 1));
8739 ctl_done((union ctl_io *)ctsio);
8740 return (CTL_RETVAL_COMPLETE);
8744 * According to SBC-3, a transfer length of 0 is not an error.
8745 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8746 * translates to 256 blocks for those commands.
8748 if (num_blocks == 0) {
8749 ctl_set_success(ctsio);
8750 ctl_done((union ctl_io *)ctsio);
8751 return (CTL_RETVAL_COMPLETE);
8754 /* Set FUA and/or DPO if caches are disabled. */
8756 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8757 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8759 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8760 flags |= CTL_LLF_FUA;
8763 lbalen = (struct ctl_lba_len_flags *)
8764 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8766 lbalen->len = num_blocks;
8767 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8769 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8770 ctsio->kern_rel_offset = 0;
8772 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8774 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8779 ctl_cnw_cont(union ctl_io *io)
8781 struct ctl_lun *lun = CTL_LUN(io);
8782 struct ctl_scsiio *ctsio;
8783 struct ctl_lba_len_flags *lbalen;
8786 ctsio = &io->scsiio;
8787 ctsio->io_hdr.status = CTL_STATUS_NONE;
8788 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8789 lbalen = (struct ctl_lba_len_flags *)
8790 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8791 lbalen->flags &= ~CTL_LLF_COMPARE;
8792 lbalen->flags |= CTL_LLF_WRITE;
8794 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8795 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8800 ctl_cnw(struct ctl_scsiio *ctsio)
8802 struct ctl_lun *lun = CTL_LUN(ctsio);
8803 struct ctl_lba_len_flags *lbalen;
8805 uint32_t num_blocks;
8808 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8811 switch (ctsio->cdb[0]) {
8812 case COMPARE_AND_WRITE: {
8813 struct scsi_compare_and_write *cdb;
8815 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8816 if (cdb->byte2 & SRW10_FUA)
8817 flags |= CTL_LLF_FUA;
8818 if (cdb->byte2 & SRW10_DPO)
8819 flags |= CTL_LLF_DPO;
8820 lba = scsi_8btou64(cdb->addr);
8821 num_blocks = cdb->length;
8826 * We got a command we don't support. This shouldn't
8827 * happen, commands should be filtered out above us.
8829 ctl_set_invalid_opcode(ctsio);
8830 ctl_done((union ctl_io *)ctsio);
8832 return (CTL_RETVAL_COMPLETE);
8833 break; /* NOTREACHED */
8837 * The first check is to make sure we're in bounds, the second
8838 * check is to catch wrap-around problems. If the lba + num blocks
8839 * is less than the lba, then we've wrapped around and the block
8840 * range is invalid anyway.
8842 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8843 || ((lba + num_blocks) < lba)) {
8844 ctl_set_lba_out_of_range(ctsio,
8845 MAX(lba, lun->be_lun->maxlba + 1));
8846 ctl_done((union ctl_io *)ctsio);
8847 return (CTL_RETVAL_COMPLETE);
8851 * According to SBC-3, a transfer length of 0 is not an error.
8853 if (num_blocks == 0) {
8854 ctl_set_success(ctsio);
8855 ctl_done((union ctl_io *)ctsio);
8856 return (CTL_RETVAL_COMPLETE);
8859 /* Set FUA if write cache is disabled. */
8860 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8861 flags |= CTL_LLF_FUA;
8863 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8864 ctsio->kern_rel_offset = 0;
8867 * Set the IO_CONT flag, so that if this I/O gets passed to
8868 * ctl_data_submit_done(), it'll get passed back to
8869 * ctl_ctl_cnw_cont() for further processing.
8871 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8872 ctsio->io_cont = ctl_cnw_cont;
8874 lbalen = (struct ctl_lba_len_flags *)
8875 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8877 lbalen->len = num_blocks;
8878 lbalen->flags = CTL_LLF_COMPARE | flags;
8880 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8881 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8886 ctl_verify(struct ctl_scsiio *ctsio)
8888 struct ctl_lun *lun = CTL_LUN(ctsio);
8889 struct ctl_lba_len_flags *lbalen;
8891 uint32_t num_blocks;
8895 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8898 flags = CTL_LLF_FUA;
8899 switch (ctsio->cdb[0]) {
8901 struct scsi_verify_10 *cdb;
8903 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8904 if (cdb->byte2 & SVFY_BYTCHK)
8906 if (cdb->byte2 & SVFY_DPO)
8907 flags |= CTL_LLF_DPO;
8908 lba = scsi_4btoul(cdb->addr);
8909 num_blocks = scsi_2btoul(cdb->length);
8913 struct scsi_verify_12 *cdb;
8915 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8916 if (cdb->byte2 & SVFY_BYTCHK)
8918 if (cdb->byte2 & SVFY_DPO)
8919 flags |= CTL_LLF_DPO;
8920 lba = scsi_4btoul(cdb->addr);
8921 num_blocks = scsi_4btoul(cdb->length);
8925 struct scsi_rw_16 *cdb;
8927 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8928 if (cdb->byte2 & SVFY_BYTCHK)
8930 if (cdb->byte2 & SVFY_DPO)
8931 flags |= CTL_LLF_DPO;
8932 lba = scsi_8btou64(cdb->addr);
8933 num_blocks = scsi_4btoul(cdb->length);
8938 * We got a command we don't support. This shouldn't
8939 * happen, commands should be filtered out above us.
8941 ctl_set_invalid_opcode(ctsio);
8942 ctl_done((union ctl_io *)ctsio);
8943 return (CTL_RETVAL_COMPLETE);
8947 * The first check is to make sure we're in bounds, the second
8948 * check is to catch wrap-around problems. If the lba + num blocks
8949 * is less than the lba, then we've wrapped around and the block
8950 * range is invalid anyway.
8952 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8953 || ((lba + num_blocks) < lba)) {
8954 ctl_set_lba_out_of_range(ctsio,
8955 MAX(lba, lun->be_lun->maxlba + 1));
8956 ctl_done((union ctl_io *)ctsio);
8957 return (CTL_RETVAL_COMPLETE);
8961 * According to SBC-3, a transfer length of 0 is not an error.
8963 if (num_blocks == 0) {
8964 ctl_set_success(ctsio);
8965 ctl_done((union ctl_io *)ctsio);
8966 return (CTL_RETVAL_COMPLETE);
8969 lbalen = (struct ctl_lba_len_flags *)
8970 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8972 lbalen->len = num_blocks;
8974 lbalen->flags = CTL_LLF_COMPARE | flags;
8975 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8977 lbalen->flags = CTL_LLF_VERIFY | flags;
8978 ctsio->kern_total_len = 0;
8980 ctsio->kern_rel_offset = 0;
8982 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
8983 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8988 ctl_report_luns(struct ctl_scsiio *ctsio)
8990 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8991 struct ctl_port *port = CTL_PORT(ctsio);
8992 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
8993 struct scsi_report_luns *cdb;
8994 struct scsi_report_luns_data *lun_data;
8995 int num_filled, num_luns, num_port_luns, retval;
8996 uint32_t alloc_len, lun_datalen;
8997 uint32_t initidx, targ_lun_id, lun_id;
8999 retval = CTL_RETVAL_COMPLETE;
9000 cdb = (struct scsi_report_luns *)ctsio->cdb;
9002 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9005 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9006 mtx_lock(&softc->ctl_lock);
9007 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9008 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9011 mtx_unlock(&softc->ctl_lock);
9013 switch (cdb->select_report) {
9014 case RPL_REPORT_DEFAULT:
9015 case RPL_REPORT_ALL:
9016 case RPL_REPORT_NONSUBSID:
9018 case RPL_REPORT_WELLKNOWN:
9019 case RPL_REPORT_ADMIN:
9020 case RPL_REPORT_CONGLOM:
9024 ctl_set_invalid_field(ctsio,
9030 ctl_done((union ctl_io *)ctsio);
9032 break; /* NOTREACHED */
9035 alloc_len = scsi_4btoul(cdb->length);
9037 * The initiator has to allocate at least 16 bytes for this request,
9038 * so he can at least get the header and the first LUN. Otherwise
9039 * we reject the request (per SPC-3 rev 14, section 6.21).
9041 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9042 sizeof(struct scsi_report_luns_lundata))) {
9043 ctl_set_invalid_field(ctsio,
9049 ctl_done((union ctl_io *)ctsio);
9053 lun_datalen = sizeof(*lun_data) +
9054 (num_luns * sizeof(struct scsi_report_luns_lundata));
9056 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9057 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9058 ctsio->kern_sg_entries = 0;
9060 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9062 mtx_lock(&softc->ctl_lock);
9063 for (targ_lun_id = 0, num_filled = 0;
9064 targ_lun_id < num_port_luns && num_filled < num_luns;
9066 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9067 if (lun_id == UINT32_MAX)
9069 lun = softc->ctl_luns[lun_id];
9073 be64enc(lun_data->luns[num_filled++].lundata,
9074 ctl_encode_lun(targ_lun_id));
9077 * According to SPC-3, rev 14 section 6.21:
9079 * "The execution of a REPORT LUNS command to any valid and
9080 * installed logical unit shall clear the REPORTED LUNS DATA
9081 * HAS CHANGED unit attention condition for all logical
9082 * units of that target with respect to the requesting
9083 * initiator. A valid and installed logical unit is one
9084 * having a PERIPHERAL QUALIFIER of 000b in the standard
9085 * INQUIRY data (see 6.4.2)."
9087 * If request_lun is NULL, the LUN this report luns command
9088 * was issued to is either disabled or doesn't exist. In that
9089 * case, we shouldn't clear any pending lun change unit
9092 if (request_lun != NULL) {
9093 mtx_lock(&lun->lun_lock);
9094 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9095 mtx_unlock(&lun->lun_lock);
9098 mtx_unlock(&softc->ctl_lock);
9101 * It's quite possible that we've returned fewer LUNs than we allocated
9102 * space for. Trim it.
9104 lun_datalen = sizeof(*lun_data) +
9105 (num_filled * sizeof(struct scsi_report_luns_lundata));
9106 ctsio->kern_rel_offset = 0;
9107 ctsio->kern_sg_entries = 0;
9108 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9109 ctsio->kern_total_len = ctsio->kern_data_len;
9112 * We set this to the actual data length, regardless of how much
9113 * space we actually have to return results. If the user looks at
9114 * this value, he'll know whether or not he allocated enough space
9115 * and reissue the command if necessary. We don't support well
9116 * known logical units, so if the user asks for that, return none.
9118 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9121 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9124 ctl_set_success(ctsio);
9125 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9126 ctsio->be_move_done = ctl_config_move_done;
9127 ctl_datamove((union ctl_io *)ctsio);
9132 ctl_request_sense(struct ctl_scsiio *ctsio)
9134 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9135 struct ctl_lun *lun = CTL_LUN(ctsio);
9136 struct scsi_request_sense *cdb;
9137 struct scsi_sense_data *sense_ptr, *ps;
9140 u_int sense_len = SSD_FULL_SIZE;
9141 scsi_sense_data_type sense_format;
9142 ctl_ua_type ua_type;
9143 uint8_t asc = 0, ascq = 0;
9145 cdb = (struct scsi_request_sense *)ctsio->cdb;
9147 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9150 * Determine which sense format the user wants.
9152 if (cdb->byte2 & SRS_DESC)
9153 sense_format = SSD_TYPE_DESC;
9155 sense_format = SSD_TYPE_FIXED;
9157 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9158 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9159 ctsio->kern_sg_entries = 0;
9160 ctsio->kern_rel_offset = 0;
9163 * struct scsi_sense_data, which is currently set to 256 bytes, is
9164 * larger than the largest allowed value for the length field in the
9165 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9167 ctsio->kern_data_len = cdb->length;
9168 ctsio->kern_total_len = cdb->length;
9171 * If we don't have a LUN, we don't have any pending sense.
9174 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9175 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9176 /* "Logical unit not supported" */
9177 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9178 /*current_error*/ 1,
9179 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9187 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9189 * Check for pending sense, and then for pending unit attentions.
9190 * Pending sense gets returned first, then pending unit attentions.
9192 mtx_lock(&lun->lun_lock);
9193 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9195 ps += initidx % CTL_MAX_INIT_PER_PORT;
9196 if (ps != NULL && ps->error_code != 0) {
9197 scsi_sense_data_type stored_format;
9200 * Check to see which sense format was used for the stored
9203 stored_format = scsi_sense_type(ps);
9206 * If the user requested a different sense format than the
9207 * one we stored, then we need to convert it to the other
9208 * format. If we're going from descriptor to fixed format
9209 * sense data, we may lose things in translation, depending
9210 * on what options were used.
9212 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9213 * for some reason we'll just copy it out as-is.
9215 if ((stored_format == SSD_TYPE_FIXED)
9216 && (sense_format == SSD_TYPE_DESC))
9217 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9218 ps, (struct scsi_sense_data_desc *)sense_ptr);
9219 else if ((stored_format == SSD_TYPE_DESC)
9220 && (sense_format == SSD_TYPE_FIXED))
9221 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9222 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9224 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9229 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9231 if (ua_type != CTL_UA_NONE)
9234 if (have_error == 0) {
9236 * Report informational exception if have one and allowed.
9238 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9240 ascq = lun->ie_ascq;
9242 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9243 /*current_error*/ 1,
9244 /*sense_key*/ SSD_KEY_NO_SENSE,
9249 mtx_unlock(&lun->lun_lock);
9253 * We report the SCSI status as OK, since the status of the command
9254 * itself is OK. We're reporting sense as parameter data.
9256 ctl_set_success(ctsio);
9257 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9258 ctsio->be_move_done = ctl_config_move_done;
9259 ctl_datamove((union ctl_io *)ctsio);
9260 return (CTL_RETVAL_COMPLETE);
9264 ctl_tur(struct ctl_scsiio *ctsio)
9267 CTL_DEBUG_PRINT(("ctl_tur\n"));
9269 ctl_set_success(ctsio);
9270 ctl_done((union ctl_io *)ctsio);
9272 return (CTL_RETVAL_COMPLETE);
9276 * SCSI VPD page 0x00, the Supported VPD Pages page.
9279 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9281 struct ctl_lun *lun = CTL_LUN(ctsio);
9282 struct scsi_vpd_supported_pages *pages;
9286 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9287 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9288 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9289 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9290 ctsio->kern_rel_offset = 0;
9291 ctsio->kern_sg_entries = 0;
9292 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9293 ctsio->kern_total_len = ctsio->kern_data_len;
9296 * The control device is always connected. The disk device, on the
9297 * other hand, may not be online all the time. Need to change this
9298 * to figure out whether the disk device is actually online or not.
9301 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9302 lun->be_lun->lun_type;
9304 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9307 /* Supported VPD pages */
9308 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9310 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9311 /* Device Identification */
9312 pages->page_list[p++] = SVPD_DEVICE_ID;
9313 /* Extended INQUIRY Data */
9314 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9315 /* Mode Page Policy */
9316 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9318 pages->page_list[p++] = SVPD_SCSI_PORTS;
9319 /* Third-party Copy */
9320 pages->page_list[p++] = SVPD_SCSI_TPC;
9321 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9323 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9324 /* Block Device Characteristics */
9325 pages->page_list[p++] = SVPD_BDC;
9326 /* Logical Block Provisioning */
9327 pages->page_list[p++] = SVPD_LBP;
9331 ctl_set_success(ctsio);
9332 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9333 ctsio->be_move_done = ctl_config_move_done;
9334 ctl_datamove((union ctl_io *)ctsio);
9335 return (CTL_RETVAL_COMPLETE);
9339 * SCSI VPD page 0x80, the Unit Serial Number page.
9342 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9344 struct ctl_lun *lun = CTL_LUN(ctsio);
9345 struct scsi_vpd_unit_serial_number *sn_ptr;
9348 data_len = 4 + CTL_SN_LEN;
9349 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9350 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9351 ctsio->kern_rel_offset = 0;
9352 ctsio->kern_sg_entries = 0;
9353 ctsio->kern_data_len = min(data_len, alloc_len);
9354 ctsio->kern_total_len = ctsio->kern_data_len;
9357 * The control device is always connected. The disk device, on the
9358 * other hand, may not be online all the time. Need to change this
9359 * to figure out whether the disk device is actually online or not.
9362 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9363 lun->be_lun->lun_type;
9365 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9367 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9368 sn_ptr->length = CTL_SN_LEN;
9370 * If we don't have a LUN, we just leave the serial number as
9374 strncpy((char *)sn_ptr->serial_num,
9375 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9377 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9379 ctl_set_success(ctsio);
9380 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9381 ctsio->be_move_done = ctl_config_move_done;
9382 ctl_datamove((union ctl_io *)ctsio);
9383 return (CTL_RETVAL_COMPLETE);
9388 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9391 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9393 struct ctl_lun *lun = CTL_LUN(ctsio);
9394 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9397 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9398 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9399 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9400 ctsio->kern_sg_entries = 0;
9401 ctsio->kern_rel_offset = 0;
9402 ctsio->kern_data_len = min(data_len, alloc_len);
9403 ctsio->kern_total_len = ctsio->kern_data_len;
9406 * The control device is always connected. The disk device, on the
9407 * other hand, may not be online all the time.
9410 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9411 lun->be_lun->lun_type;
9413 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9414 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9415 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9417 * We support head of queue, ordered and simple tags.
9419 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9421 * Volatile cache supported.
9423 eid_ptr->flags3 = SVPD_EID_V_SUP;
9426 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9427 * attention for a particular IT nexus on all LUNs once we report
9428 * it to that nexus once. This bit is required as of SPC-4.
9430 eid_ptr->flags4 = SVPD_EID_LUICLR;
9433 * We support revert to defaults (RTD) bit in MODE SELECT.
9435 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9438 * XXX KDM in order to correctly answer this, we would need
9439 * information from the SIM to determine how much sense data it
9440 * can send. So this would really be a path inquiry field, most
9441 * likely. This can be set to a maximum of 252 according to SPC-4,
9442 * but the hardware may or may not be able to support that much.
9443 * 0 just means that the maximum sense data length is not reported.
9445 eid_ptr->max_sense_length = 0;
9447 ctl_set_success(ctsio);
9448 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9449 ctsio->be_move_done = ctl_config_move_done;
9450 ctl_datamove((union ctl_io *)ctsio);
9451 return (CTL_RETVAL_COMPLETE);
9455 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9457 struct ctl_lun *lun = CTL_LUN(ctsio);
9458 struct scsi_vpd_mode_page_policy *mpp_ptr;
9461 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9462 sizeof(struct scsi_vpd_mode_page_policy_descr);
9464 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9465 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9466 ctsio->kern_rel_offset = 0;
9467 ctsio->kern_sg_entries = 0;
9468 ctsio->kern_data_len = min(data_len, alloc_len);
9469 ctsio->kern_total_len = ctsio->kern_data_len;
9472 * The control device is always connected. The disk device, on the
9473 * other hand, may not be online all the time.
9476 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9477 lun->be_lun->lun_type;
9479 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9480 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9481 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9482 mpp_ptr->descr[0].page_code = 0x3f;
9483 mpp_ptr->descr[0].subpage_code = 0xff;
9484 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9486 ctl_set_success(ctsio);
9487 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9488 ctsio->be_move_done = ctl_config_move_done;
9489 ctl_datamove((union ctl_io *)ctsio);
9490 return (CTL_RETVAL_COMPLETE);
9494 * SCSI VPD page 0x83, the Device Identification page.
9497 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9499 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9500 struct ctl_port *port = CTL_PORT(ctsio);
9501 struct ctl_lun *lun = CTL_LUN(ctsio);
9502 struct scsi_vpd_device_id *devid_ptr;
9503 struct scsi_vpd_id_descriptor *desc;
9507 data_len = sizeof(struct scsi_vpd_device_id) +
9508 sizeof(struct scsi_vpd_id_descriptor) +
9509 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9510 sizeof(struct scsi_vpd_id_descriptor) +
9511 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9512 if (lun && lun->lun_devid)
9513 data_len += lun->lun_devid->len;
9514 if (port && port->port_devid)
9515 data_len += port->port_devid->len;
9516 if (port && port->target_devid)
9517 data_len += port->target_devid->len;
9519 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9520 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9521 ctsio->kern_sg_entries = 0;
9522 ctsio->kern_rel_offset = 0;
9523 ctsio->kern_sg_entries = 0;
9524 ctsio->kern_data_len = min(data_len, alloc_len);
9525 ctsio->kern_total_len = ctsio->kern_data_len;
9528 * The control device is always connected. The disk device, on the
9529 * other hand, may not be online all the time.
9532 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9533 lun->be_lun->lun_type;
9535 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9536 devid_ptr->page_code = SVPD_DEVICE_ID;
9537 scsi_ulto2b(data_len - 4, devid_ptr->length);
9539 if (port && port->port_type == CTL_PORT_FC)
9540 proto = SCSI_PROTO_FC << 4;
9541 else if (port && port->port_type == CTL_PORT_SAS)
9542 proto = SCSI_PROTO_SAS << 4;
9543 else if (port && port->port_type == CTL_PORT_ISCSI)
9544 proto = SCSI_PROTO_ISCSI << 4;
9546 proto = SCSI_PROTO_SPI << 4;
9547 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9550 * We're using a LUN association here. i.e., this device ID is a
9551 * per-LUN identifier.
9553 if (lun && lun->lun_devid) {
9554 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9555 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9556 lun->lun_devid->len);
9560 * This is for the WWPN which is a port association.
9562 if (port && port->port_devid) {
9563 memcpy(desc, port->port_devid->data, port->port_devid->len);
9564 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9565 port->port_devid->len);
9569 * This is for the Relative Target Port(type 4h) identifier
9571 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9572 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9573 SVPD_ID_TYPE_RELTARG;
9575 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9576 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9577 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9580 * This is for the Target Port Group(type 5h) identifier
9582 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9583 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9584 SVPD_ID_TYPE_TPORTGRP;
9586 if (softc->is_single ||
9587 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9590 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9591 scsi_ulto2b(g, &desc->identifier[2]);
9592 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9593 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9596 * This is for the Target identifier
9598 if (port && port->target_devid) {
9599 memcpy(desc, port->target_devid->data, port->target_devid->len);
9602 ctl_set_success(ctsio);
9603 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9604 ctsio->be_move_done = ctl_config_move_done;
9605 ctl_datamove((union ctl_io *)ctsio);
9606 return (CTL_RETVAL_COMPLETE);
9610 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9612 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9613 struct ctl_lun *lun = CTL_LUN(ctsio);
9614 struct scsi_vpd_scsi_ports *sp;
9615 struct scsi_vpd_port_designation *pd;
9616 struct scsi_vpd_port_designation_cont *pdc;
9617 struct ctl_port *port;
9618 int data_len, num_target_ports, iid_len, id_len;
9620 num_target_ports = 0;
9623 mtx_lock(&softc->ctl_lock);
9624 STAILQ_FOREACH(port, &softc->port_list, links) {
9625 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9628 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9631 if (port->init_devid)
9632 iid_len += port->init_devid->len;
9633 if (port->port_devid)
9634 id_len += port->port_devid->len;
9636 mtx_unlock(&softc->ctl_lock);
9638 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9639 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9640 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9641 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9642 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9643 ctsio->kern_sg_entries = 0;
9644 ctsio->kern_rel_offset = 0;
9645 ctsio->kern_sg_entries = 0;
9646 ctsio->kern_data_len = min(data_len, alloc_len);
9647 ctsio->kern_total_len = ctsio->kern_data_len;
9650 * The control device is always connected. The disk device, on the
9651 * other hand, may not be online all the time. Need to change this
9652 * to figure out whether the disk device is actually online or not.
9655 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9656 lun->be_lun->lun_type;
9658 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9660 sp->page_code = SVPD_SCSI_PORTS;
9661 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9663 pd = &sp->design[0];
9665 mtx_lock(&softc->ctl_lock);
9666 STAILQ_FOREACH(port, &softc->port_list, links) {
9667 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9670 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9672 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9673 if (port->init_devid) {
9674 iid_len = port->init_devid->len;
9675 memcpy(pd->initiator_transportid,
9676 port->init_devid->data, port->init_devid->len);
9679 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9680 pdc = (struct scsi_vpd_port_designation_cont *)
9681 (&pd->initiator_transportid[iid_len]);
9682 if (port->port_devid) {
9683 id_len = port->port_devid->len;
9684 memcpy(pdc->target_port_descriptors,
9685 port->port_devid->data, port->port_devid->len);
9688 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9689 pd = (struct scsi_vpd_port_designation *)
9690 ((uint8_t *)pdc->target_port_descriptors + id_len);
9692 mtx_unlock(&softc->ctl_lock);
9694 ctl_set_success(ctsio);
9695 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9696 ctsio->be_move_done = ctl_config_move_done;
9697 ctl_datamove((union ctl_io *)ctsio);
9698 return (CTL_RETVAL_COMPLETE);
9702 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9704 struct ctl_lun *lun = CTL_LUN(ctsio);
9705 struct scsi_vpd_block_limits *bl_ptr;
9709 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9710 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9711 ctsio->kern_sg_entries = 0;
9712 ctsio->kern_rel_offset = 0;
9713 ctsio->kern_sg_entries = 0;
9714 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9715 ctsio->kern_total_len = ctsio->kern_data_len;
9718 * The control device is always connected. The disk device, on the
9719 * other hand, may not be online all the time. Need to change this
9720 * to figure out whether the disk device is actually online or not.
9723 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9724 lun->be_lun->lun_type;
9726 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9728 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9729 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9730 bl_ptr->max_cmp_write_len = 0xff;
9731 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9733 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9734 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9736 val = dnvlist_get_string(lun->be_lun->options,
9737 "unmap_max_lba", NULL);
9739 ctl_expand_number(val, &ival);
9740 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9742 val = dnvlist_get_string(lun->be_lun->options,
9743 "unmap_max_descr", NULL);
9745 ctl_expand_number(val, &ival);
9746 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9747 if (lun->be_lun->ublockexp != 0) {
9748 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9749 bl_ptr->opt_unmap_grain);
9750 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9751 bl_ptr->unmap_grain_align);
9754 scsi_ulto4b(lun->be_lun->atomicblock,
9755 bl_ptr->max_atomic_transfer_length);
9756 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9757 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9758 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9759 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9761 val = dnvlist_get_string(lun->be_lun->options,
9762 "write_same_max_lba", NULL);
9764 ctl_expand_number(val, &ival);
9765 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
9768 ctl_set_success(ctsio);
9769 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9770 ctsio->be_move_done = ctl_config_move_done;
9771 ctl_datamove((union ctl_io *)ctsio);
9772 return (CTL_RETVAL_COMPLETE);
9776 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9778 struct ctl_lun *lun = CTL_LUN(ctsio);
9779 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9783 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9784 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9785 ctsio->kern_sg_entries = 0;
9786 ctsio->kern_rel_offset = 0;
9787 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9788 ctsio->kern_total_len = ctsio->kern_data_len;
9791 * The control device is always connected. The disk device, on the
9792 * other hand, may not be online all the time. Need to change this
9793 * to figure out whether the disk device is actually online or not.
9796 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9797 lun->be_lun->lun_type;
9799 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9800 bdc_ptr->page_code = SVPD_BDC;
9801 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9803 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL)
9804 i = strtol(value, NULL, 0);
9806 i = CTL_DEFAULT_ROTATION_RATE;
9807 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9809 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL)
9810 i = strtol(value, NULL, 0);
9813 bdc_ptr->wab_wac_ff = (i & 0x0f);
9814 bdc_ptr->flags = SVPD_FUAB | SVPD_VBULS;
9816 ctl_set_success(ctsio);
9817 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9818 ctsio->be_move_done = ctl_config_move_done;
9819 ctl_datamove((union ctl_io *)ctsio);
9820 return (CTL_RETVAL_COMPLETE);
9824 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9826 struct ctl_lun *lun = CTL_LUN(ctsio);
9827 struct scsi_vpd_logical_block_prov *lbp_ptr;
9830 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9831 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9832 ctsio->kern_sg_entries = 0;
9833 ctsio->kern_rel_offset = 0;
9834 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9835 ctsio->kern_total_len = ctsio->kern_data_len;
9838 * The control device is always connected. The disk device, on the
9839 * other hand, may not be online all the time. Need to change this
9840 * to figure out whether the disk device is actually online or not.
9843 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9844 lun->be_lun->lun_type;
9846 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9848 lbp_ptr->page_code = SVPD_LBP;
9849 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9850 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9851 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9852 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9853 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9854 value = dnvlist_get_string(lun->be_lun->options,
9855 "provisioning_type", NULL);
9856 if (value != NULL) {
9857 if (strcmp(value, "resource") == 0)
9858 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9859 else if (strcmp(value, "thin") == 0)
9860 lbp_ptr->prov_type = SVPD_LBP_THIN;
9862 lbp_ptr->prov_type = SVPD_LBP_THIN;
9865 ctl_set_success(ctsio);
9866 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9867 ctsio->be_move_done = ctl_config_move_done;
9868 ctl_datamove((union ctl_io *)ctsio);
9869 return (CTL_RETVAL_COMPLETE);
9873 * INQUIRY with the EVPD bit set.
9876 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9878 struct ctl_lun *lun = CTL_LUN(ctsio);
9879 struct scsi_inquiry *cdb;
9880 int alloc_len, retval;
9882 cdb = (struct scsi_inquiry *)ctsio->cdb;
9883 alloc_len = scsi_2btoul(cdb->length);
9885 switch (cdb->page_code) {
9886 case SVPD_SUPPORTED_PAGES:
9887 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9889 case SVPD_UNIT_SERIAL_NUMBER:
9890 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9892 case SVPD_DEVICE_ID:
9893 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9895 case SVPD_EXTENDED_INQUIRY_DATA:
9896 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9898 case SVPD_MODE_PAGE_POLICY:
9899 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9901 case SVPD_SCSI_PORTS:
9902 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
9905 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
9907 case SVPD_BLOCK_LIMITS:
9908 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9910 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
9913 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9915 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
9918 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
9920 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
9924 ctl_set_invalid_field(ctsio,
9930 ctl_done((union ctl_io *)ctsio);
9931 retval = CTL_RETVAL_COMPLETE;
9939 * Standard INQUIRY data.
9942 ctl_inquiry_std(struct ctl_scsiio *ctsio)
9944 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9945 struct ctl_port *port = CTL_PORT(ctsio);
9946 struct ctl_lun *lun = CTL_LUN(ctsio);
9947 struct scsi_inquiry_data *inq_ptr;
9948 struct scsi_inquiry *cdb;
9950 uint32_t alloc_len, data_len;
9951 ctl_port_type port_type;
9953 port_type = port->port_type;
9954 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
9955 port_type = CTL_PORT_SCSI;
9957 cdb = (struct scsi_inquiry *)ctsio->cdb;
9958 alloc_len = scsi_2btoul(cdb->length);
9961 * We malloc the full inquiry data size here and fill it
9962 * in. If the user only asks for less, we'll give him
9965 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
9966 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9967 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
9968 ctsio->kern_sg_entries = 0;
9969 ctsio->kern_rel_offset = 0;
9970 ctsio->kern_data_len = min(data_len, alloc_len);
9971 ctsio->kern_total_len = ctsio->kern_data_len;
9974 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
9975 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
9976 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9977 lun->be_lun->lun_type;
9979 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
9980 lun->be_lun->lun_type;
9982 if (lun->flags & CTL_LUN_REMOVABLE)
9983 inq_ptr->dev_qual2 |= SID_RMB;
9985 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
9987 /* RMB in byte 2 is 0 */
9988 inq_ptr->version = SCSI_REV_SPC5;
9991 * According to SAM-3, even if a device only supports a single
9992 * level of LUN addressing, it should still set the HISUP bit:
9994 * 4.9.1 Logical unit numbers overview
9996 * All logical unit number formats described in this standard are
9997 * hierarchical in structure even when only a single level in that
9998 * hierarchy is used. The HISUP bit shall be set to one in the
9999 * standard INQUIRY data (see SPC-2) when any logical unit number
10000 * format described in this standard is used. Non-hierarchical
10001 * formats are outside the scope of this standard.
10003 * Therefore we set the HiSup bit here.
10005 * The response format is 2, per SPC-3.
10007 inq_ptr->response_format = SID_HiSup | 2;
10009 inq_ptr->additional_length = data_len -
10010 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10011 CTL_DEBUG_PRINT(("additional_length = %d\n",
10012 inq_ptr->additional_length));
10014 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10015 if (port_type == CTL_PORT_SCSI)
10016 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10017 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10018 inq_ptr->flags = SID_CmdQue;
10019 if (port_type == CTL_PORT_SCSI)
10020 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10023 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10024 * We have 8 bytes for the vendor name, and 16 bytes for the device
10025 * name and 4 bytes for the revision.
10027 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10028 "vendor", NULL)) == NULL) {
10029 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10031 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10032 strncpy(inq_ptr->vendor, val,
10033 min(sizeof(inq_ptr->vendor), strlen(val)));
10036 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10037 sizeof(inq_ptr->product));
10038 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product",
10040 switch (lun->be_lun->lun_type) {
10042 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10043 sizeof(inq_ptr->product));
10046 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10047 sizeof(inq_ptr->product));
10050 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10051 sizeof(inq_ptr->product));
10054 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10055 sizeof(inq_ptr->product));
10059 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10060 strncpy(inq_ptr->product, val,
10061 min(sizeof(inq_ptr->product), strlen(val)));
10065 * XXX make this a macro somewhere so it automatically gets
10066 * incremented when we make changes.
10068 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10069 "revision", NULL)) == NULL) {
10070 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10072 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10073 strncpy(inq_ptr->revision, val,
10074 min(sizeof(inq_ptr->revision), strlen(val)));
10078 * For parallel SCSI, we support double transition and single
10079 * transition clocking. We also support QAS (Quick Arbitration
10080 * and Selection) and Information Unit transfers on both the
10081 * control and array devices.
10083 if (port_type == CTL_PORT_SCSI)
10084 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10087 /* SAM-6 (no version claimed) */
10088 scsi_ulto2b(0x00C0, inq_ptr->version1);
10089 /* SPC-5 (no version claimed) */
10090 scsi_ulto2b(0x05C0, inq_ptr->version2);
10091 if (port_type == CTL_PORT_FC) {
10092 /* FCP-2 ANSI INCITS.350:2003 */
10093 scsi_ulto2b(0x0917, inq_ptr->version3);
10094 } else if (port_type == CTL_PORT_SCSI) {
10095 /* SPI-4 ANSI INCITS.362:200x */
10096 scsi_ulto2b(0x0B56, inq_ptr->version3);
10097 } else if (port_type == CTL_PORT_ISCSI) {
10098 /* iSCSI (no version claimed) */
10099 scsi_ulto2b(0x0960, inq_ptr->version3);
10100 } else if (port_type == CTL_PORT_SAS) {
10101 /* SAS (no version claimed) */
10102 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10103 } else if (port_type == CTL_PORT_UMASS) {
10104 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10105 scsi_ulto2b(0x1730, inq_ptr->version3);
10109 /* SBC-4 (no version claimed) */
10110 scsi_ulto2b(0x0600, inq_ptr->version4);
10112 switch (lun->be_lun->lun_type) {
10114 /* SBC-4 (no version claimed) */
10115 scsi_ulto2b(0x0600, inq_ptr->version4);
10120 /* MMC-6 (no version claimed) */
10121 scsi_ulto2b(0x04E0, inq_ptr->version4);
10128 ctl_set_success(ctsio);
10129 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10130 ctsio->be_move_done = ctl_config_move_done;
10131 ctl_datamove((union ctl_io *)ctsio);
10132 return (CTL_RETVAL_COMPLETE);
10136 ctl_inquiry(struct ctl_scsiio *ctsio)
10138 struct scsi_inquiry *cdb;
10141 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10143 cdb = (struct scsi_inquiry *)ctsio->cdb;
10144 if (cdb->byte2 & SI_EVPD)
10145 retval = ctl_inquiry_evpd(ctsio);
10146 else if (cdb->page_code == 0)
10147 retval = ctl_inquiry_std(ctsio);
10149 ctl_set_invalid_field(ctsio,
10155 ctl_done((union ctl_io *)ctsio);
10156 return (CTL_RETVAL_COMPLETE);
10163 ctl_get_config(struct ctl_scsiio *ctsio)
10165 struct ctl_lun *lun = CTL_LUN(ctsio);
10166 struct scsi_get_config_header *hdr;
10167 struct scsi_get_config_feature *feature;
10168 struct scsi_get_config *cdb;
10169 uint32_t alloc_len, data_len;
10172 cdb = (struct scsi_get_config *)ctsio->cdb;
10173 rt = (cdb->rt & SGC_RT_MASK);
10174 starting = scsi_2btoul(cdb->starting_feature);
10175 alloc_len = scsi_2btoul(cdb->length);
10177 data_len = sizeof(struct scsi_get_config_header) +
10178 sizeof(struct scsi_get_config_feature) + 8 +
10179 sizeof(struct scsi_get_config_feature) + 8 +
10180 sizeof(struct scsi_get_config_feature) + 4 +
10181 sizeof(struct scsi_get_config_feature) + 4 +
10182 sizeof(struct scsi_get_config_feature) + 8 +
10183 sizeof(struct scsi_get_config_feature) +
10184 sizeof(struct scsi_get_config_feature) + 4 +
10185 sizeof(struct scsi_get_config_feature) + 4 +
10186 sizeof(struct scsi_get_config_feature) + 4 +
10187 sizeof(struct scsi_get_config_feature) + 4 +
10188 sizeof(struct scsi_get_config_feature) + 4 +
10189 sizeof(struct scsi_get_config_feature) + 4;
10190 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10191 ctsio->kern_sg_entries = 0;
10192 ctsio->kern_rel_offset = 0;
10194 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10195 if (lun->flags & CTL_LUN_NO_MEDIA)
10196 scsi_ulto2b(0x0000, hdr->current_profile);
10198 scsi_ulto2b(0x0010, hdr->current_profile);
10199 feature = (struct scsi_get_config_feature *)(hdr + 1);
10201 if (starting > 0x003b)
10203 if (starting > 0x003a)
10205 if (starting > 0x002b)
10207 if (starting > 0x002a)
10209 if (starting > 0x001f)
10211 if (starting > 0x001e)
10213 if (starting > 0x001d)
10215 if (starting > 0x0010)
10217 if (starting > 0x0003)
10219 if (starting > 0x0002)
10221 if (starting > 0x0001)
10223 if (starting > 0x0000)
10227 scsi_ulto2b(0x0000, feature->feature_code);
10228 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10229 feature->add_length = 8;
10230 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10231 feature->feature_data[2] = 0x00;
10232 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10233 feature->feature_data[6] = 0x01;
10234 feature = (struct scsi_get_config_feature *)
10235 &feature->feature_data[feature->add_length];
10238 scsi_ulto2b(0x0001, feature->feature_code);
10239 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10240 feature->add_length = 8;
10241 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10242 feature->feature_data[4] = 0x03;
10243 feature = (struct scsi_get_config_feature *)
10244 &feature->feature_data[feature->add_length];
10247 scsi_ulto2b(0x0002, feature->feature_code);
10248 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10249 feature->add_length = 4;
10250 feature->feature_data[0] = 0x02;
10251 feature = (struct scsi_get_config_feature *)
10252 &feature->feature_data[feature->add_length];
10254 f3: /* Removable Medium */
10255 scsi_ulto2b(0x0003, feature->feature_code);
10256 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10257 feature->add_length = 4;
10258 feature->feature_data[0] = 0x39;
10259 feature = (struct scsi_get_config_feature *)
10260 &feature->feature_data[feature->add_length];
10262 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10265 f10: /* Random Read */
10266 scsi_ulto2b(0x0010, feature->feature_code);
10267 feature->flags = 0x00;
10268 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10269 feature->flags |= SGC_F_CURRENT;
10270 feature->add_length = 8;
10271 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10272 scsi_ulto2b(1, &feature->feature_data[4]);
10273 feature->feature_data[6] = 0x00;
10274 feature = (struct scsi_get_config_feature *)
10275 &feature->feature_data[feature->add_length];
10277 f1d: /* Multi-Read */
10278 scsi_ulto2b(0x001D, feature->feature_code);
10279 feature->flags = 0x00;
10280 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10281 feature->flags |= SGC_F_CURRENT;
10282 feature->add_length = 0;
10283 feature = (struct scsi_get_config_feature *)
10284 &feature->feature_data[feature->add_length];
10287 scsi_ulto2b(0x001E, feature->feature_code);
10288 feature->flags = 0x00;
10289 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10290 feature->flags |= SGC_F_CURRENT;
10291 feature->add_length = 4;
10292 feature->feature_data[0] = 0x00;
10293 feature = (struct scsi_get_config_feature *)
10294 &feature->feature_data[feature->add_length];
10296 f1f: /* DVD Read */
10297 scsi_ulto2b(0x001F, feature->feature_code);
10298 feature->flags = 0x08;
10299 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10300 feature->flags |= SGC_F_CURRENT;
10301 feature->add_length = 4;
10302 feature->feature_data[0] = 0x01;
10303 feature->feature_data[2] = 0x03;
10304 feature = (struct scsi_get_config_feature *)
10305 &feature->feature_data[feature->add_length];
10308 scsi_ulto2b(0x002A, feature->feature_code);
10309 feature->flags = 0x04;
10310 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10311 feature->flags |= SGC_F_CURRENT;
10312 feature->add_length = 4;
10313 feature->feature_data[0] = 0x00;
10314 feature->feature_data[1] = 0x00;
10315 feature = (struct scsi_get_config_feature *)
10316 &feature->feature_data[feature->add_length];
10319 scsi_ulto2b(0x002B, feature->feature_code);
10320 feature->flags = 0x00;
10321 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10322 feature->flags |= SGC_F_CURRENT;
10323 feature->add_length = 4;
10324 feature->feature_data[0] = 0x00;
10325 feature = (struct scsi_get_config_feature *)
10326 &feature->feature_data[feature->add_length];
10328 f3a: /* DVD+RW Dual Layer */
10329 scsi_ulto2b(0x003A, feature->feature_code);
10330 feature->flags = 0x00;
10331 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10332 feature->flags |= SGC_F_CURRENT;
10333 feature->add_length = 4;
10334 feature->feature_data[0] = 0x00;
10335 feature->feature_data[1] = 0x00;
10336 feature = (struct scsi_get_config_feature *)
10337 &feature->feature_data[feature->add_length];
10339 f3b: /* DVD+R Dual Layer */
10340 scsi_ulto2b(0x003B, feature->feature_code);
10341 feature->flags = 0x00;
10342 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10343 feature->flags |= SGC_F_CURRENT;
10344 feature->add_length = 4;
10345 feature->feature_data[0] = 0x00;
10346 feature = (struct scsi_get_config_feature *)
10347 &feature->feature_data[feature->add_length];
10350 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10351 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10352 feature = (struct scsi_get_config_feature *)(hdr + 1);
10353 if (scsi_2btoul(feature->feature_code) == starting)
10354 feature = (struct scsi_get_config_feature *)
10355 &feature->feature_data[feature->add_length];
10356 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10358 scsi_ulto4b(data_len - 4, hdr->data_length);
10359 ctsio->kern_data_len = min(data_len, alloc_len);
10360 ctsio->kern_total_len = ctsio->kern_data_len;
10362 ctl_set_success(ctsio);
10363 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10364 ctsio->be_move_done = ctl_config_move_done;
10365 ctl_datamove((union ctl_io *)ctsio);
10366 return (CTL_RETVAL_COMPLETE);
10370 ctl_get_event_status(struct ctl_scsiio *ctsio)
10372 struct scsi_get_event_status_header *hdr;
10373 struct scsi_get_event_status *cdb;
10374 uint32_t alloc_len, data_len;
10376 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10377 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10378 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10379 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10380 ctl_done((union ctl_io *)ctsio);
10381 return (CTL_RETVAL_COMPLETE);
10383 alloc_len = scsi_2btoul(cdb->length);
10385 data_len = sizeof(struct scsi_get_event_status_header);
10386 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10387 ctsio->kern_sg_entries = 0;
10388 ctsio->kern_rel_offset = 0;
10389 ctsio->kern_data_len = min(data_len, alloc_len);
10390 ctsio->kern_total_len = ctsio->kern_data_len;
10392 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10393 scsi_ulto2b(0, hdr->descr_length);
10394 hdr->nea_class = SGESN_NEA;
10395 hdr->supported_class = 0;
10397 ctl_set_success(ctsio);
10398 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10399 ctsio->be_move_done = ctl_config_move_done;
10400 ctl_datamove((union ctl_io *)ctsio);
10401 return (CTL_RETVAL_COMPLETE);
10405 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10407 struct scsi_mechanism_status_header *hdr;
10408 struct scsi_mechanism_status *cdb;
10409 uint32_t alloc_len, data_len;
10411 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10412 alloc_len = scsi_2btoul(cdb->length);
10414 data_len = sizeof(struct scsi_mechanism_status_header);
10415 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10416 ctsio->kern_sg_entries = 0;
10417 ctsio->kern_rel_offset = 0;
10418 ctsio->kern_data_len = min(data_len, alloc_len);
10419 ctsio->kern_total_len = ctsio->kern_data_len;
10421 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10422 hdr->state1 = 0x00;
10423 hdr->state2 = 0xe0;
10424 scsi_ulto3b(0, hdr->lba);
10425 hdr->slots_num = 0;
10426 scsi_ulto2b(0, hdr->slots_length);
10428 ctl_set_success(ctsio);
10429 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10430 ctsio->be_move_done = ctl_config_move_done;
10431 ctl_datamove((union ctl_io *)ctsio);
10432 return (CTL_RETVAL_COMPLETE);
10436 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10441 buf[1] = bin2bcd((lba / 75) / 60);
10442 buf[2] = bin2bcd((lba / 75) % 60);
10443 buf[3] = bin2bcd(lba % 75);
10447 ctl_read_toc(struct ctl_scsiio *ctsio)
10449 struct ctl_lun *lun = CTL_LUN(ctsio);
10450 struct scsi_read_toc_hdr *hdr;
10451 struct scsi_read_toc_type01_descr *descr;
10452 struct scsi_read_toc *cdb;
10453 uint32_t alloc_len, data_len;
10456 cdb = (struct scsi_read_toc *)ctsio->cdb;
10457 msf = (cdb->byte2 & CD_MSF) != 0;
10458 format = cdb->format;
10459 alloc_len = scsi_2btoul(cdb->data_len);
10461 data_len = sizeof(struct scsi_read_toc_hdr);
10463 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10465 data_len += sizeof(struct scsi_read_toc_type01_descr);
10466 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10467 ctsio->kern_sg_entries = 0;
10468 ctsio->kern_rel_offset = 0;
10469 ctsio->kern_data_len = min(data_len, alloc_len);
10470 ctsio->kern_total_len = ctsio->kern_data_len;
10472 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10474 scsi_ulto2b(0x12, hdr->data_length);
10477 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10478 descr->addr_ctl = 0x14;
10479 descr->track_number = 1;
10481 ctl_ultomsf(0, descr->track_start);
10483 scsi_ulto4b(0, descr->track_start);
10485 descr->addr_ctl = 0x14;
10486 descr->track_number = 0xaa;
10488 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10490 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10492 scsi_ulto2b(0x0a, hdr->data_length);
10495 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10496 descr->addr_ctl = 0x14;
10497 descr->track_number = 1;
10499 ctl_ultomsf(0, descr->track_start);
10501 scsi_ulto4b(0, descr->track_start);
10504 ctl_set_success(ctsio);
10505 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10506 ctsio->be_move_done = ctl_config_move_done;
10507 ctl_datamove((union ctl_io *)ctsio);
10508 return (CTL_RETVAL_COMPLETE);
10512 * For known CDB types, parse the LBA and length.
10515 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10517 if (io->io_hdr.io_type != CTL_IO_SCSI)
10520 switch (io->scsiio.cdb[0]) {
10521 case COMPARE_AND_WRITE: {
10522 struct scsi_compare_and_write *cdb;
10524 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10526 *lba = scsi_8btou64(cdb->addr);
10527 *len = cdb->length;
10532 struct scsi_rw_6 *cdb;
10534 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10536 *lba = scsi_3btoul(cdb->addr);
10537 /* only 5 bits are valid in the most significant address byte */
10539 *len = cdb->length;
10544 struct scsi_rw_10 *cdb;
10546 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10548 *lba = scsi_4btoul(cdb->addr);
10549 *len = scsi_2btoul(cdb->length);
10552 case WRITE_VERIFY_10: {
10553 struct scsi_write_verify_10 *cdb;
10555 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10557 *lba = scsi_4btoul(cdb->addr);
10558 *len = scsi_2btoul(cdb->length);
10563 struct scsi_rw_12 *cdb;
10565 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10567 *lba = scsi_4btoul(cdb->addr);
10568 *len = scsi_4btoul(cdb->length);
10571 case WRITE_VERIFY_12: {
10572 struct scsi_write_verify_12 *cdb;
10574 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10576 *lba = scsi_4btoul(cdb->addr);
10577 *len = scsi_4btoul(cdb->length);
10582 struct scsi_rw_16 *cdb;
10584 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10586 *lba = scsi_8btou64(cdb->addr);
10587 *len = scsi_4btoul(cdb->length);
10590 case WRITE_ATOMIC_16: {
10591 struct scsi_write_atomic_16 *cdb;
10593 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10595 *lba = scsi_8btou64(cdb->addr);
10596 *len = scsi_2btoul(cdb->length);
10599 case WRITE_VERIFY_16: {
10600 struct scsi_write_verify_16 *cdb;
10602 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10604 *lba = scsi_8btou64(cdb->addr);
10605 *len = scsi_4btoul(cdb->length);
10608 case WRITE_SAME_10: {
10609 struct scsi_write_same_10 *cdb;
10611 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10613 *lba = scsi_4btoul(cdb->addr);
10614 *len = scsi_2btoul(cdb->length);
10617 case WRITE_SAME_16: {
10618 struct scsi_write_same_16 *cdb;
10620 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10622 *lba = scsi_8btou64(cdb->addr);
10623 *len = scsi_4btoul(cdb->length);
10627 struct scsi_verify_10 *cdb;
10629 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10631 *lba = scsi_4btoul(cdb->addr);
10632 *len = scsi_2btoul(cdb->length);
10636 struct scsi_verify_12 *cdb;
10638 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10640 *lba = scsi_4btoul(cdb->addr);
10641 *len = scsi_4btoul(cdb->length);
10645 struct scsi_verify_16 *cdb;
10647 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10649 *lba = scsi_8btou64(cdb->addr);
10650 *len = scsi_4btoul(cdb->length);
10658 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10659 struct scsi_get_lba_status *cdb;
10661 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10662 *lba = scsi_8btou64(cdb->addr);
10668 break; /* NOTREACHED */
10675 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10678 uint64_t endlba1, endlba2;
10680 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10681 endlba2 = lba2 + len2 - 1;
10683 if ((endlba1 < lba2) || (endlba2 < lba1))
10684 return (CTL_ACTION_PASS);
10686 return (CTL_ACTION_BLOCK);
10690 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10692 struct ctl_ptr_len_flags *ptrlen;
10693 struct scsi_unmap_desc *buf, *end, *range;
10697 /* If not UNMAP -- go other way. */
10698 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10699 io->scsiio.cdb[0] != UNMAP)
10700 return (CTL_ACTION_ERROR);
10702 /* If UNMAP without data -- block and wait for data. */
10703 ptrlen = (struct ctl_ptr_len_flags *)
10704 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10705 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10706 ptrlen->ptr == NULL)
10707 return (CTL_ACTION_BLOCK);
10709 /* UNMAP with data -- check for collision. */
10710 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10711 end = buf + ptrlen->len / sizeof(*buf);
10712 for (range = buf; range < end; range++) {
10713 lba = scsi_8btou64(range->lba);
10714 len = scsi_4btoul(range->length);
10715 if ((lba < lba2 + len2) && (lba + len > lba2))
10716 return (CTL_ACTION_BLOCK);
10718 return (CTL_ACTION_PASS);
10722 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10724 uint64_t lba1, lba2;
10725 uint64_t len1, len2;
10728 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10729 return (CTL_ACTION_ERROR);
10731 retval = ctl_extent_check_unmap(io1, lba2, len2);
10732 if (retval != CTL_ACTION_ERROR)
10735 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10736 return (CTL_ACTION_ERROR);
10738 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10740 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10744 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10746 uint64_t lba1, lba2;
10747 uint64_t len1, len2;
10749 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10750 return (CTL_ACTION_PASS);
10751 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10752 return (CTL_ACTION_ERROR);
10753 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10754 return (CTL_ACTION_ERROR);
10756 if (lba1 + len1 == lba2)
10757 return (CTL_ACTION_BLOCK);
10758 return (CTL_ACTION_PASS);
10762 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10763 union ctl_io *ooa_io)
10765 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10766 const ctl_serialize_action *serialize_row;
10769 * Aborted commands are not going to be executed and may even
10770 * not report completion, so we don't care about their order.
10771 * Let them complete ASAP to clean the OOA queue.
10773 if (pending_io->io_hdr.flags & CTL_FLAG_ABORT)
10774 return (CTL_ACTION_SKIP);
10777 * The initiator attempted multiple untagged commands at the same
10778 * time. Can't do that.
10780 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10781 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10782 && ((pending_io->io_hdr.nexus.targ_port ==
10783 ooa_io->io_hdr.nexus.targ_port)
10784 && (pending_io->io_hdr.nexus.initid ==
10785 ooa_io->io_hdr.nexus.initid))
10786 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10787 CTL_FLAG_STATUS_SENT)) == 0))
10788 return (CTL_ACTION_OVERLAP);
10791 * The initiator attempted to send multiple tagged commands with
10792 * the same ID. (It's fine if different initiators have the same
10795 * Even if all of those conditions are true, we don't kill the I/O
10796 * if the command ahead of us has been aborted. We won't end up
10797 * sending it to the FETD, and it's perfectly legal to resend a
10798 * command with the same tag number as long as the previous
10799 * instance of this tag number has been aborted somehow.
10801 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10802 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10803 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10804 && ((pending_io->io_hdr.nexus.targ_port ==
10805 ooa_io->io_hdr.nexus.targ_port)
10806 && (pending_io->io_hdr.nexus.initid ==
10807 ooa_io->io_hdr.nexus.initid))
10808 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10809 CTL_FLAG_STATUS_SENT)) == 0))
10810 return (CTL_ACTION_OVERLAP_TAG);
10813 * If we get a head of queue tag, SAM-3 says that we should
10814 * immediately execute it.
10816 * What happens if this command would normally block for some other
10817 * reason? e.g. a request sense with a head of queue tag
10818 * immediately after a write. Normally that would block, but this
10819 * will result in its getting executed immediately...
10821 * We currently return "pass" instead of "skip", so we'll end up
10822 * going through the rest of the queue to check for overlapped tags.
10824 * XXX KDM check for other types of blockage first??
10826 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10827 return (CTL_ACTION_PASS);
10830 * Ordered tags have to block until all items ahead of them
10831 * have completed. If we get called with an ordered tag, we always
10832 * block, if something else is ahead of us in the queue.
10834 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10835 return (CTL_ACTION_BLOCK);
10838 * Simple tags get blocked until all head of queue and ordered tags
10839 * ahead of them have completed. I'm lumping untagged commands in
10840 * with simple tags here. XXX KDM is that the right thing to do?
10842 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10843 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10844 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10845 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10846 return (CTL_ACTION_BLOCK);
10848 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10849 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
10850 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
10851 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
10852 pending_io->scsiio.cdb[1], pending_io));
10853 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10854 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
10855 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
10856 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
10857 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
10858 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
10859 ooa_io->scsiio.cdb[1], ooa_io));
10861 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10863 switch (serialize_row[pending_entry->seridx]) {
10864 case CTL_SER_BLOCK:
10865 return (CTL_ACTION_BLOCK);
10866 case CTL_SER_EXTENT:
10867 return (ctl_extent_check(ooa_io, pending_io,
10868 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10869 case CTL_SER_EXTENTOPT:
10870 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10871 SCP_QUEUE_ALG_UNRESTRICTED)
10872 return (ctl_extent_check(ooa_io, pending_io,
10874 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10875 return (CTL_ACTION_PASS);
10876 case CTL_SER_EXTENTSEQ:
10877 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10878 return (ctl_extent_check_seq(ooa_io, pending_io));
10879 return (CTL_ACTION_PASS);
10881 return (CTL_ACTION_PASS);
10882 case CTL_SER_BLOCKOPT:
10883 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10884 SCP_QUEUE_ALG_UNRESTRICTED)
10885 return (CTL_ACTION_BLOCK);
10886 return (CTL_ACTION_PASS);
10888 return (CTL_ACTION_SKIP);
10890 panic("%s: Invalid serialization value %d for %d => %d",
10891 __func__, serialize_row[pending_entry->seridx],
10892 pending_entry->seridx, ooa_entry->seridx);
10895 return (CTL_ACTION_ERROR);
10899 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
10901 * - pending_io is generally either incoming, or on the blocked queue
10902 * - starting I/O is the I/O we want to start the check with.
10905 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
10906 union ctl_io **starting_io)
10908 union ctl_io *ooa_io;
10911 mtx_assert(&lun->lun_lock, MA_OWNED);
10914 * Run back along the OOA queue, starting with the current
10915 * blocked I/O and going through every I/O before it on the
10916 * queue. If starting_io is NULL, we'll just end up returning
10919 for (ooa_io = *starting_io; ooa_io != NULL;
10920 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
10922 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
10923 if (action != CTL_ACTION_PASS) {
10924 *starting_io = ooa_io;
10929 *starting_io = NULL;
10930 return (CTL_ACTION_PASS);
10934 * Try to unblock the specified I/O.
10936 * skip parameter allows explicitly skip present blocker of the I/O,
10937 * starting from the previous one on OOA queue. It can be used when
10938 * we know for sure that the blocker I/O does no longer count.
10941 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip)
10943 struct ctl_softc *softc = lun->ctl_softc;
10944 union ctl_io *bio, *obio;
10945 const struct ctl_cmd_entry *entry;
10946 union ctl_ha_msg msg_info;
10949 mtx_assert(&lun->lun_lock, MA_OWNED);
10951 if (io->io_hdr.blocker == NULL)
10954 obio = bio = io->io_hdr.blocker;
10956 bio = (union ctl_io *)TAILQ_PREV(&bio->io_hdr, ctl_ooaq,
10958 action = ctl_check_ooa(lun, io, &bio);
10959 if (action == CTL_ACTION_BLOCK) {
10960 /* Still blocked, but may be by different I/O now. */
10962 TAILQ_REMOVE(&obio->io_hdr.blocked_queue,
10963 &io->io_hdr, blocked_links);
10964 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue,
10965 &io->io_hdr, blocked_links);
10966 io->io_hdr.blocker = bio;
10971 /* No longer blocked, one way or another. */
10972 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links);
10973 io->io_hdr.blocker = NULL;
10976 case CTL_ACTION_OVERLAP:
10977 ctl_set_overlapped_cmd(&io->scsiio);
10979 case CTL_ACTION_OVERLAP_TAG:
10980 ctl_set_overlapped_tag(&io->scsiio,
10981 io->scsiio.tag_num & 0xff);
10983 case CTL_ACTION_PASS:
10984 case CTL_ACTION_SKIP:
10986 /* Serializing commands from the other SC retire there. */
10987 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
10988 (softc->ha_mode != CTL_HA_MODE_XFER)) {
10989 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
10990 msg_info.hdr.original_sc = io->io_hdr.remote_io;
10991 msg_info.hdr.serializing_sc = io;
10992 msg_info.hdr.msg_type = CTL_MSG_R2R;
10993 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
10994 sizeof(msg_info.hdr), M_NOWAIT);
10999 * Check this I/O for LUN state changes that may have happened
11000 * while this command was blocked. The LUN state may have been
11001 * changed by a command ahead of us in the queue.
11003 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11004 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
11009 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11010 ctl_enqueue_rtr(io);
11012 case CTL_ACTION_ERROR:
11014 ctl_set_internal_failure(&io->scsiio,
11016 /*retry_count*/ 0);
11019 /* Serializing commands from the other SC are done here. */
11020 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11021 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11022 ctl_try_unblock_others(lun, io, TRUE);
11023 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
11025 ctl_copy_sense_data_back(io, &msg_info);
11026 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11027 msg_info.hdr.serializing_sc = NULL;
11028 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
11029 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11030 sizeof(msg_info.scsi), M_WAITOK);
11041 * Try to unblock I/Os blocked by the specified I/O.
11043 * skip parameter allows explicitly skip the specified I/O as blocker,
11044 * starting from the previous one on the OOA queue. It can be used when
11045 * we know for sure that the specified I/O does no longer count (done).
11046 * It has to be still on OOA queue though so that we know where to start.
11049 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip)
11051 union ctl_io *io, *next_io;
11053 mtx_assert(&lun->lun_lock, MA_OWNED);
11055 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue);
11056 io != NULL; io = next_io) {
11057 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links);
11059 KASSERT(io->io_hdr.blocker != NULL,
11060 ("I/O %p on blocked list without blocker", io));
11061 ctl_try_unblock_io(lun, io, skip);
11063 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue),
11064 ("blocked_queue is not empty after skipping %p", bio));
11068 * This routine (with one exception) checks LUN flags that can be set by
11069 * commands ahead of us in the OOA queue. These flags have to be checked
11070 * when a command initially comes in, and when we pull a command off the
11071 * blocked queue and are preparing to execute it. The reason we have to
11072 * check these flags for commands on the blocked queue is that the LUN
11073 * state may have been changed by a command ahead of us while we're on the
11076 * Ordering is somewhat important with these checks, so please pay
11077 * careful attention to the placement of any new checks.
11080 ctl_scsiio_lun_check(struct ctl_lun *lun,
11081 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11083 struct ctl_softc *softc = lun->ctl_softc;
11089 mtx_assert(&lun->lun_lock, MA_OWNED);
11092 * If this shelf is a secondary shelf controller, we may have to
11093 * reject some commands disallowed by HA mode and link state.
11095 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11096 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11097 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11098 ctl_set_lun_unavail(ctsio);
11102 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11103 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11104 ctl_set_lun_transit(ctsio);
11108 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11109 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11110 ctl_set_lun_standby(ctsio);
11115 /* The rest of checks are only done on executing side */
11116 if (softc->ha_mode == CTL_HA_MODE_XFER)
11120 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11122 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11123 ctl_set_hw_write_protected(ctsio);
11127 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11128 ctl_set_sense(ctsio, /*current_error*/ 1,
11129 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11130 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11137 * Check for a reservation conflict. If this command isn't allowed
11138 * even on reserved LUNs, and if this initiator isn't the one who
11139 * reserved us, reject the command with a reservation conflict.
11141 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11142 if ((lun->flags & CTL_LUN_RESERVED)
11143 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11144 if (lun->res_idx != residx) {
11145 ctl_set_reservation_conflict(ctsio);
11151 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11152 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11153 /* No reservation or command is allowed. */;
11154 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11155 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11156 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11157 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11158 /* The command is allowed for Write Exclusive resv. */;
11161 * if we aren't registered or it's a res holder type
11162 * reservation and this isn't the res holder then set a
11165 if (ctl_get_prkey(lun, residx) == 0 ||
11166 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11167 ctl_set_reservation_conflict(ctsio);
11173 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11174 if (lun->flags & CTL_LUN_EJECTED)
11175 ctl_set_lun_ejected(ctsio);
11176 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11177 if (lun->flags & CTL_LUN_REMOVABLE)
11178 ctl_set_lun_no_media(ctsio);
11180 ctl_set_lun_int_reqd(ctsio);
11181 } else if (lun->flags & CTL_LUN_STOPPED)
11182 ctl_set_lun_stopped(ctsio);
11194 ctl_failover_io(union ctl_io *io, int have_lock)
11196 ctl_set_busy(&io->scsiio);
11201 ctl_failover_lun(union ctl_io *rio)
11203 struct ctl_softc *softc = CTL_SOFTC(rio);
11204 struct ctl_lun *lun;
11205 struct ctl_io_hdr *io, *next_io;
11208 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11209 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11211 /* Find and lock the LUN. */
11212 mtx_lock(&softc->ctl_lock);
11213 if (targ_lun > ctl_max_luns ||
11214 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11215 mtx_unlock(&softc->ctl_lock);
11218 mtx_lock(&lun->lun_lock);
11219 mtx_unlock(&softc->ctl_lock);
11220 if (lun->flags & CTL_LUN_DISABLED) {
11221 mtx_unlock(&lun->lun_lock);
11225 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11226 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11227 /* We are master */
11228 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11229 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11230 io->flags |= CTL_FLAG_ABORT;
11231 io->flags |= CTL_FLAG_FAILOVER;
11232 ctl_try_unblock_io(lun,
11233 (union ctl_io *)io, FALSE);
11234 } else { /* This can be only due to DATAMOVE */
11235 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11236 io->flags &= ~CTL_FLAG_DMA_INPROG;
11237 io->flags |= CTL_FLAG_IO_ACTIVE;
11238 io->port_status = 31340;
11239 ctl_enqueue_isc((union ctl_io *)io);
11243 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11244 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11245 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11246 io->flags |= CTL_FLAG_FAILOVER;
11248 ctl_set_busy(&((union ctl_io *)io)->
11250 ctl_done((union ctl_io *)io);
11254 } else { /* SERIALIZE modes */
11255 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11256 /* We are master */
11257 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11258 if (io->blocker != NULL) {
11259 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue,
11260 io, blocked_links);
11261 io->blocker = NULL;
11263 ctl_try_unblock_others(lun, (union ctl_io *)io,
11265 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11266 ctl_free_io((union ctl_io *)io);
11269 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11270 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11271 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11272 ctl_set_busy(&((union ctl_io *)io)->
11274 ctl_done((union ctl_io *)io);
11279 mtx_unlock(&lun->lun_lock);
11283 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11285 struct ctl_lun *lun;
11286 const struct ctl_cmd_entry *entry;
11288 uint32_t initidx, targ_lun;
11292 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11293 if (targ_lun < ctl_max_luns)
11294 lun = softc->ctl_luns[targ_lun];
11297 * If the LUN is invalid, pretend that it doesn't exist.
11298 * It will go away as soon as all pending I/O has been
11301 mtx_lock(&lun->lun_lock);
11302 if (lun->flags & CTL_LUN_DISABLED) {
11303 mtx_unlock(&lun->lun_lock);
11307 CTL_LUN(ctsio) = lun;
11309 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11312 * Every I/O goes into the OOA queue for a particular LUN,
11313 * and stays there until completion.
11316 if (TAILQ_EMPTY(&lun->ooa_queue))
11317 lun->idle_time += getsbinuptime() - lun->last_busy;
11319 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11322 /* Get command entry and return error if it is unsuppotyed. */
11323 entry = ctl_validate_command(ctsio);
11324 if (entry == NULL) {
11326 mtx_unlock(&lun->lun_lock);
11330 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11331 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11334 * Check to see whether we can send this command to LUNs that don't
11335 * exist. This should pretty much only be the case for inquiry
11336 * and request sense. Further checks, below, really require having
11337 * a LUN, so we can't really check the command anymore. Just put
11338 * it on the rtr queue.
11341 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11342 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11343 ctl_enqueue_rtr((union ctl_io *)ctsio);
11347 ctl_set_unsupported_lun(ctsio);
11348 ctl_done((union ctl_io *)ctsio);
11349 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11353 * Make sure we support this particular command on this LUN.
11354 * e.g., we don't support writes to the control LUN.
11356 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11357 mtx_unlock(&lun->lun_lock);
11358 ctl_set_invalid_opcode(ctsio);
11359 ctl_done((union ctl_io *)ctsio);
11364 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11367 * If we've got a request sense, it'll clear the contingent
11368 * allegiance condition. Otherwise, if we have a CA condition for
11369 * this initiator, clear it, because it sent down a command other
11370 * than request sense.
11372 if (ctsio->cdb[0] != REQUEST_SENSE) {
11373 struct scsi_sense_data *ps;
11375 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11377 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11381 * If the command has this flag set, it handles its own unit
11382 * attention reporting, we shouldn't do anything. Otherwise we
11383 * check for any pending unit attentions, and send them back to the
11384 * initiator. We only do this when a command initially comes in,
11385 * not when we pull it off the blocked queue.
11387 * According to SAM-3, section 5.3.2, the order that things get
11388 * presented back to the host is basically unit attentions caused
11389 * by some sort of reset event, busy status, reservation conflicts
11390 * or task set full, and finally any other status.
11392 * One issue here is that some of the unit attentions we report
11393 * don't fall into the "reset" category (e.g. "reported luns data
11394 * has changed"). So reporting it here, before the reservation
11395 * check, may be technically wrong. I guess the only thing to do
11396 * would be to check for and report the reset events here, and then
11397 * check for the other unit attention types after we check for a
11398 * reservation conflict.
11400 * XXX KDM need to fix this
11402 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11403 ctl_ua_type ua_type;
11404 u_int sense_len = 0;
11406 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11407 &sense_len, SSD_TYPE_NONE);
11408 if (ua_type != CTL_UA_NONE) {
11409 mtx_unlock(&lun->lun_lock);
11410 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11411 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11412 ctsio->sense_len = sense_len;
11413 ctl_done((union ctl_io *)ctsio);
11419 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11420 mtx_unlock(&lun->lun_lock);
11421 ctl_done((union ctl_io *)ctsio);
11426 * XXX CHD this is where we want to send IO to other side if
11427 * this LUN is secondary on this SC. We will need to make a copy
11428 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11429 * the copy we send as FROM_OTHER.
11430 * We also need to stuff the address of the original IO so we can
11431 * find it easily. Something similar will need be done on the other
11432 * side so when we are done we can find the copy.
11434 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11435 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11436 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11437 union ctl_ha_msg msg_info;
11440 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11441 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11442 mtx_unlock(&lun->lun_lock);
11444 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11445 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11446 msg_info.hdr.serializing_sc = NULL;
11447 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11448 msg_info.scsi.tag_num = ctsio->tag_num;
11449 msg_info.scsi.tag_type = ctsio->tag_type;
11450 msg_info.scsi.cdb_len = ctsio->cdb_len;
11451 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11453 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11454 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11455 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11456 ctl_set_busy(ctsio);
11457 ctl_done((union ctl_io *)ctsio);
11463 bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links);
11464 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
11465 case CTL_ACTION_BLOCK:
11466 ctsio->io_hdr.blocker = bio;
11467 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
11469 mtx_unlock(&lun->lun_lock);
11471 case CTL_ACTION_PASS:
11472 case CTL_ACTION_SKIP:
11473 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11474 mtx_unlock(&lun->lun_lock);
11475 ctl_enqueue_rtr((union ctl_io *)ctsio);
11477 case CTL_ACTION_OVERLAP:
11478 mtx_unlock(&lun->lun_lock);
11479 ctl_set_overlapped_cmd(ctsio);
11480 ctl_done((union ctl_io *)ctsio);
11482 case CTL_ACTION_OVERLAP_TAG:
11483 mtx_unlock(&lun->lun_lock);
11484 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11485 ctl_done((union ctl_io *)ctsio);
11487 case CTL_ACTION_ERROR:
11489 mtx_unlock(&lun->lun_lock);
11490 ctl_set_internal_failure(ctsio,
11492 /*retry_count*/ 0);
11493 ctl_done((union ctl_io *)ctsio);
11499 const struct ctl_cmd_entry *
11500 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11502 const struct ctl_cmd_entry *entry;
11503 int service_action;
11505 entry = &ctl_cmd_table[ctsio->cdb[0]];
11507 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11508 if (entry->flags & CTL_CMD_FLAG_SA5) {
11509 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11510 entry = &((const struct ctl_cmd_entry *)
11511 entry->execute)[service_action];
11516 const struct ctl_cmd_entry *
11517 ctl_validate_command(struct ctl_scsiio *ctsio)
11519 const struct ctl_cmd_entry *entry;
11523 entry = ctl_get_cmd_entry(ctsio, &sa);
11524 if (entry->execute == NULL) {
11526 ctl_set_invalid_field(ctsio,
11533 ctl_set_invalid_opcode(ctsio);
11534 ctl_done((union ctl_io *)ctsio);
11537 KASSERT(entry->length > 0,
11538 ("Not defined length for command 0x%02x/0x%02x",
11539 ctsio->cdb[0], ctsio->cdb[1]));
11540 for (i = 1; i < entry->length; i++) {
11541 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11544 ctl_set_invalid_field(ctsio,
11549 /*bit*/ fls(diff) - 1);
11550 ctl_done((union ctl_io *)ctsio);
11557 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11560 switch (lun_type) {
11562 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11566 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11570 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11580 ctl_scsiio(struct ctl_scsiio *ctsio)
11583 const struct ctl_cmd_entry *entry;
11585 retval = CTL_RETVAL_COMPLETE;
11587 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11589 entry = ctl_get_cmd_entry(ctsio, NULL);
11592 * If this I/O has been aborted, just send it straight to
11593 * ctl_done() without executing it.
11595 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11596 ctl_done((union ctl_io *)ctsio);
11601 * All the checks should have been handled by ctl_scsiio_precheck().
11602 * We should be clear now to just execute the I/O.
11604 retval = entry->execute(ctsio);
11611 ctl_target_reset(union ctl_io *io)
11613 struct ctl_softc *softc = CTL_SOFTC(io);
11614 struct ctl_port *port = CTL_PORT(io);
11615 struct ctl_lun *lun;
11617 ctl_ua_type ua_type;
11619 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11620 union ctl_ha_msg msg_info;
11622 msg_info.hdr.nexus = io->io_hdr.nexus;
11623 msg_info.task.task_action = io->taskio.task_action;
11624 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11625 msg_info.hdr.original_sc = NULL;
11626 msg_info.hdr.serializing_sc = NULL;
11627 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11628 sizeof(msg_info.task), M_WAITOK);
11631 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11632 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11633 ua_type = CTL_UA_TARG_RESET;
11635 ua_type = CTL_UA_BUS_RESET;
11636 mtx_lock(&softc->ctl_lock);
11637 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11638 if (port != NULL &&
11639 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11641 ctl_do_lun_reset(lun, initidx, ua_type);
11643 mtx_unlock(&softc->ctl_lock);
11644 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11649 * The LUN should always be set. The I/O is optional, and is used to
11650 * distinguish between I/Os sent by this initiator, and by other
11651 * initiators. We set unit attention for initiators other than this one.
11652 * SAM-3 is vague on this point. It does say that a unit attention should
11653 * be established for other initiators when a LUN is reset (see section
11654 * 5.7.3), but it doesn't specifically say that the unit attention should
11655 * be established for this particular initiator when a LUN is reset. Here
11656 * is the relevant text, from SAM-3 rev 8:
11658 * 5.7.2 When a SCSI initiator port aborts its own tasks
11660 * When a SCSI initiator port causes its own task(s) to be aborted, no
11661 * notification that the task(s) have been aborted shall be returned to
11662 * the SCSI initiator port other than the completion response for the
11663 * command or task management function action that caused the task(s) to
11664 * be aborted and notification(s) associated with related effects of the
11665 * action (e.g., a reset unit attention condition).
11667 * XXX KDM for now, we're setting unit attention for all initiators.
11670 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11675 mtx_lock(&lun->lun_lock);
11677 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11678 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11679 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11680 ctl_try_unblock_io(lun, xio, FALSE);
11683 for (i = 0; i < ctl_max_ports; i++) {
11684 free(lun->pending_sense[i], M_CTL);
11685 lun->pending_sense[i] = NULL;
11687 /* Clear reservation. */
11688 lun->flags &= ~CTL_LUN_RESERVED;
11689 /* Clear prevent media removal. */
11690 if (lun->prevent) {
11691 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11692 ctl_clear_mask(lun->prevent, i);
11693 lun->prevent_count = 0;
11695 /* Clear TPC status */
11696 ctl_tpc_lun_clear(lun, -1);
11697 /* Establish UA. */
11699 ctl_est_ua_all(lun, initidx, ua_type);
11701 ctl_est_ua_all(lun, -1, ua_type);
11703 mtx_unlock(&lun->lun_lock);
11707 ctl_lun_reset(union ctl_io *io)
11709 struct ctl_softc *softc = CTL_SOFTC(io);
11710 struct ctl_lun *lun;
11711 uint32_t targ_lun, initidx;
11713 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11714 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11715 mtx_lock(&softc->ctl_lock);
11716 if (targ_lun >= ctl_max_luns ||
11717 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11718 mtx_unlock(&softc->ctl_lock);
11719 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11722 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11723 mtx_unlock(&softc->ctl_lock);
11724 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11726 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11727 union ctl_ha_msg msg_info;
11729 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11730 msg_info.hdr.nexus = io->io_hdr.nexus;
11731 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11732 msg_info.hdr.original_sc = NULL;
11733 msg_info.hdr.serializing_sc = NULL;
11734 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11735 sizeof(msg_info.task), M_WAITOK);
11741 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11746 mtx_assert(&lun->lun_lock, MA_OWNED);
11749 * Run through the OOA queue and attempt to find the given I/O.
11750 * The target port, initiator ID, tag type and tag number have to
11751 * match the values that we got from the initiator. If we have an
11752 * untagged command to abort, simply abort the first untagged command
11753 * we come to. We only allow one untagged command at a time of course.
11755 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11756 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11758 if ((targ_port == UINT32_MAX ||
11759 targ_port == xio->io_hdr.nexus.targ_port) &&
11760 (init_id == UINT32_MAX ||
11761 init_id == xio->io_hdr.nexus.initid)) {
11762 if (targ_port != xio->io_hdr.nexus.targ_port ||
11763 init_id != xio->io_hdr.nexus.initid)
11764 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11765 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11766 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11767 union ctl_ha_msg msg_info;
11769 msg_info.hdr.nexus = xio->io_hdr.nexus;
11770 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11771 msg_info.task.tag_num = xio->scsiio.tag_num;
11772 msg_info.task.tag_type = xio->scsiio.tag_type;
11773 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11774 msg_info.hdr.original_sc = NULL;
11775 msg_info.hdr.serializing_sc = NULL;
11776 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11777 sizeof(msg_info.task), M_NOWAIT);
11779 ctl_try_unblock_io(lun, xio, FALSE);
11785 ctl_abort_task_set(union ctl_io *io)
11787 struct ctl_softc *softc = CTL_SOFTC(io);
11788 struct ctl_lun *lun;
11794 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11795 mtx_lock(&softc->ctl_lock);
11796 if (targ_lun >= ctl_max_luns ||
11797 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11798 mtx_unlock(&softc->ctl_lock);
11799 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11803 mtx_lock(&lun->lun_lock);
11804 mtx_unlock(&softc->ctl_lock);
11805 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11806 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11807 io->io_hdr.nexus.initid,
11808 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11809 } else { /* CTL_TASK_CLEAR_TASK_SET */
11810 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11811 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11813 mtx_unlock(&lun->lun_lock);
11814 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11819 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11820 ctl_ua_type ua_type)
11822 struct ctl_lun *lun;
11823 struct scsi_sense_data *ps;
11826 p = initidx / CTL_MAX_INIT_PER_PORT;
11827 i = initidx % CTL_MAX_INIT_PER_PORT;
11828 mtx_lock(&softc->ctl_lock);
11829 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11830 mtx_lock(&lun->lun_lock);
11832 ctl_abort_tasks_lun(lun, p, i, 1);
11834 ps = lun->pending_sense[p];
11836 ps[i].error_code = 0;
11837 /* Clear reservation. */
11838 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11839 lun->flags &= ~CTL_LUN_RESERVED;
11840 /* Clear prevent media removal. */
11841 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11842 ctl_clear_mask(lun->prevent, initidx);
11843 lun->prevent_count--;
11845 /* Clear TPC status */
11846 ctl_tpc_lun_clear(lun, initidx);
11847 /* Establish UA. */
11848 ctl_est_ua(lun, initidx, ua_type);
11849 mtx_unlock(&lun->lun_lock);
11851 mtx_unlock(&softc->ctl_lock);
11855 ctl_i_t_nexus_reset(union ctl_io *io)
11857 struct ctl_softc *softc = CTL_SOFTC(io);
11860 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11861 union ctl_ha_msg msg_info;
11863 msg_info.hdr.nexus = io->io_hdr.nexus;
11864 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11865 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11866 msg_info.hdr.original_sc = NULL;
11867 msg_info.hdr.serializing_sc = NULL;
11868 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11869 sizeof(msg_info.task), M_WAITOK);
11872 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11873 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
11874 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11879 ctl_abort_task(union ctl_io *io)
11881 struct ctl_softc *softc = CTL_SOFTC(io);
11883 struct ctl_lun *lun;
11889 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11890 mtx_lock(&softc->ctl_lock);
11891 if (targ_lun >= ctl_max_luns ||
11892 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11893 mtx_unlock(&softc->ctl_lock);
11894 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11898 mtx_lock(&lun->lun_lock);
11899 mtx_unlock(&softc->ctl_lock);
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 ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11911 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11912 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11916 * If the abort says that the task is untagged, the
11917 * task in the queue must be untagged. Otherwise,
11918 * we just check to see whether the tag numbers
11919 * match. This is because the QLogic firmware
11920 * doesn't pass back the tag type in an abort
11924 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
11925 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
11926 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
11929 * XXX KDM we've got problems with FC, because it
11930 * doesn't send down a tag type with aborts. So we
11931 * can only really go by the tag number...
11932 * This may cause problems with parallel SCSI.
11933 * Need to figure that out!!
11935 if (xio->scsiio.tag_num == io->taskio.tag_num) {
11937 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11938 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
11939 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11940 union ctl_ha_msg msg_info;
11942 msg_info.hdr.nexus = io->io_hdr.nexus;
11943 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11944 msg_info.task.tag_num = io->taskio.tag_num;
11945 msg_info.task.tag_type = io->taskio.tag_type;
11946 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11947 msg_info.hdr.original_sc = NULL;
11948 msg_info.hdr.serializing_sc = NULL;
11949 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11950 sizeof(msg_info.task), M_NOWAIT);
11952 ctl_try_unblock_io(lun, xio, FALSE);
11955 mtx_unlock(&lun->lun_lock);
11956 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11961 ctl_query_task(union ctl_io *io, int task_set)
11963 struct ctl_softc *softc = CTL_SOFTC(io);
11965 struct ctl_lun *lun;
11969 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11970 mtx_lock(&softc->ctl_lock);
11971 if (targ_lun >= ctl_max_luns ||
11972 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11973 mtx_unlock(&softc->ctl_lock);
11974 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11977 mtx_lock(&lun->lun_lock);
11978 mtx_unlock(&softc->ctl_lock);
11979 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11980 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11982 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
11983 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
11984 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
11987 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
11992 mtx_unlock(&lun->lun_lock);
11994 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
11996 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12001 ctl_query_async_event(union ctl_io *io)
12003 struct ctl_softc *softc = CTL_SOFTC(io);
12004 struct ctl_lun *lun;
12006 uint32_t targ_lun, initidx;
12008 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12009 mtx_lock(&softc->ctl_lock);
12010 if (targ_lun >= ctl_max_luns ||
12011 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12012 mtx_unlock(&softc->ctl_lock);
12013 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12016 mtx_lock(&lun->lun_lock);
12017 mtx_unlock(&softc->ctl_lock);
12018 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12019 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12020 mtx_unlock(&lun->lun_lock);
12021 if (ua != CTL_UA_NONE)
12022 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12024 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12029 ctl_run_task(union ctl_io *io)
12033 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12034 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12035 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12036 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12037 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12038 switch (io->taskio.task_action) {
12039 case CTL_TASK_ABORT_TASK:
12040 retval = ctl_abort_task(io);
12042 case CTL_TASK_ABORT_TASK_SET:
12043 case CTL_TASK_CLEAR_TASK_SET:
12044 retval = ctl_abort_task_set(io);
12046 case CTL_TASK_CLEAR_ACA:
12048 case CTL_TASK_I_T_NEXUS_RESET:
12049 retval = ctl_i_t_nexus_reset(io);
12051 case CTL_TASK_LUN_RESET:
12052 retval = ctl_lun_reset(io);
12054 case CTL_TASK_TARGET_RESET:
12055 case CTL_TASK_BUS_RESET:
12056 retval = ctl_target_reset(io);
12058 case CTL_TASK_PORT_LOGIN:
12060 case CTL_TASK_PORT_LOGOUT:
12062 case CTL_TASK_QUERY_TASK:
12063 retval = ctl_query_task(io, 0);
12065 case CTL_TASK_QUERY_TASK_SET:
12066 retval = ctl_query_task(io, 1);
12068 case CTL_TASK_QUERY_ASYNC_EVENT:
12069 retval = ctl_query_async_event(io);
12072 printf("%s: got unknown task management event %d\n",
12073 __func__, io->taskio.task_action);
12077 io->io_hdr.status = CTL_SUCCESS;
12079 io->io_hdr.status = CTL_ERROR;
12084 * For HA operation. Handle commands that come in from the other
12088 ctl_handle_isc(union ctl_io *io)
12090 struct ctl_softc *softc = CTL_SOFTC(io);
12091 struct ctl_lun *lun;
12092 const struct ctl_cmd_entry *entry;
12095 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12096 switch (io->io_hdr.msg_type) {
12097 case CTL_MSG_SERIALIZE:
12098 ctl_serialize_other_sc_cmd(&io->scsiio);
12100 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12101 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12102 if (targ_lun >= ctl_max_luns ||
12103 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12107 mtx_lock(&lun->lun_lock);
12108 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12109 mtx_unlock(&lun->lun_lock);
12113 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12114 mtx_unlock(&lun->lun_lock);
12115 ctl_enqueue_rtr(io);
12117 case CTL_MSG_FINISH_IO:
12118 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12122 if (targ_lun >= ctl_max_luns ||
12123 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12127 mtx_lock(&lun->lun_lock);
12128 ctl_try_unblock_others(lun, io, TRUE);
12129 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12130 mtx_unlock(&lun->lun_lock);
12133 case CTL_MSG_PERS_ACTION:
12134 ctl_hndl_per_res_out_on_other_sc(io);
12137 case CTL_MSG_BAD_JUJU:
12140 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12141 ctl_datamove_remote(io);
12143 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12144 io->scsiio.be_move_done(io);
12146 case CTL_MSG_FAILOVER:
12147 ctl_failover_lun(io);
12151 printf("%s: Invalid message type %d\n",
12152 __func__, io->io_hdr.msg_type);
12161 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12162 * there is no match.
12164 static ctl_lun_error_pattern
12165 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12167 const struct ctl_cmd_entry *entry;
12168 ctl_lun_error_pattern filtered_pattern, pattern;
12170 pattern = desc->error_pattern;
12173 * XXX KDM we need more data passed into this function to match a
12174 * custom pattern, and we actually need to implement custom pattern
12177 if (pattern & CTL_LUN_PAT_CMD)
12178 return (CTL_LUN_PAT_CMD);
12180 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12181 return (CTL_LUN_PAT_ANY);
12183 entry = ctl_get_cmd_entry(ctsio, NULL);
12185 filtered_pattern = entry->pattern & pattern;
12188 * If the user requested specific flags in the pattern (e.g.
12189 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12192 * If the user did not specify any flags, it doesn't matter whether
12193 * or not the command supports the flags.
12195 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12196 (pattern & ~CTL_LUN_PAT_MASK))
12197 return (CTL_LUN_PAT_NONE);
12200 * If the user asked for a range check, see if the requested LBA
12201 * range overlaps with this command's LBA range.
12203 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12209 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12211 return (CTL_LUN_PAT_NONE);
12213 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12214 desc->lba_range.len, FALSE);
12216 * A "pass" means that the LBA ranges don't overlap, so
12217 * this doesn't match the user's range criteria.
12219 if (action == CTL_ACTION_PASS)
12220 return (CTL_LUN_PAT_NONE);
12223 return (filtered_pattern);
12227 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12229 struct ctl_error_desc *desc, *desc2;
12231 mtx_assert(&lun->lun_lock, MA_OWNED);
12233 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12234 ctl_lun_error_pattern pattern;
12236 * Check to see whether this particular command matches
12237 * the pattern in the descriptor.
12239 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12240 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12243 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12244 case CTL_LUN_INJ_ABORTED:
12245 ctl_set_aborted(&io->scsiio);
12247 case CTL_LUN_INJ_MEDIUM_ERR:
12248 ctl_set_medium_error(&io->scsiio,
12249 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12250 CTL_FLAG_DATA_OUT);
12252 case CTL_LUN_INJ_UA:
12253 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12255 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12257 case CTL_LUN_INJ_CUSTOM:
12259 * We're assuming the user knows what he is doing.
12260 * Just copy the sense information without doing
12263 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12264 MIN(sizeof(desc->custom_sense),
12265 sizeof(io->scsiio.sense_data)));
12266 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12267 io->scsiio.sense_len = SSD_FULL_SIZE;
12268 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12270 case CTL_LUN_INJ_NONE:
12273 * If this is an error injection type we don't know
12274 * about, clear the continuous flag (if it is set)
12275 * so it will get deleted below.
12277 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12281 * By default, each error injection action is a one-shot
12283 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12286 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12292 #ifdef CTL_IO_DELAY
12294 ctl_datamove_timer_wakeup(void *arg)
12298 io = (union ctl_io *)arg;
12302 #endif /* CTL_IO_DELAY */
12305 ctl_datamove(union ctl_io *io)
12307 void (*fe_datamove)(union ctl_io *io);
12309 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12311 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12313 /* No data transferred yet. Frontend must update this when done. */
12314 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12317 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12322 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12323 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12325 sbuf_cat(&sb, path_str);
12326 switch (io->io_hdr.io_type) {
12328 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12329 sbuf_printf(&sb, "\n");
12330 sbuf_cat(&sb, path_str);
12331 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12332 io->scsiio.tag_num, io->scsiio.tag_type);
12335 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12336 "Tag Type: %d\n", io->taskio.task_action,
12337 io->taskio.tag_num, io->taskio.tag_type);
12340 panic("%s: Invalid CTL I/O type %d\n",
12341 __func__, io->io_hdr.io_type);
12343 sbuf_cat(&sb, path_str);
12344 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12345 (intmax_t)time_uptime - io->io_hdr.start_time);
12347 printf("%s", sbuf_data(&sb));
12349 #endif /* CTL_TIME_IO */
12351 #ifdef CTL_IO_DELAY
12352 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12353 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12355 struct ctl_lun *lun;
12359 && (lun->delay_info.datamove_delay > 0)) {
12361 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12362 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12363 callout_reset(&io->io_hdr.delay_callout,
12364 lun->delay_info.datamove_delay * hz,
12365 ctl_datamove_timer_wakeup, io);
12366 if (lun->delay_info.datamove_type ==
12367 CTL_DELAY_TYPE_ONESHOT)
12368 lun->delay_info.datamove_delay = 0;
12375 * This command has been aborted. Set the port status, so we fail
12378 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12379 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12380 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12381 io->io_hdr.nexus.targ_port,
12382 io->io_hdr.nexus.targ_lun);
12383 io->io_hdr.port_status = 31337;
12385 * Note that the backend, in this case, will get the
12386 * callback in its context. In other cases it may get
12387 * called in the frontend's interrupt thread context.
12389 io->scsiio.be_move_done(io);
12393 /* Don't confuse frontend with zero length data move. */
12394 if (io->scsiio.kern_data_len == 0) {
12395 io->scsiio.be_move_done(io);
12399 fe_datamove = CTL_PORT(io)->fe_datamove;
12404 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12406 union ctl_ha_msg msg;
12408 struct bintime cur_bt;
12411 memset(&msg, 0, sizeof(msg));
12412 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12413 msg.hdr.original_sc = io;
12414 msg.hdr.serializing_sc = io->io_hdr.remote_io;
12415 msg.hdr.nexus = io->io_hdr.nexus;
12416 msg.hdr.status = io->io_hdr.status;
12417 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12418 msg.scsi.tag_num = io->scsiio.tag_num;
12419 msg.scsi.tag_type = io->scsiio.tag_type;
12420 msg.scsi.scsi_status = io->scsiio.scsi_status;
12421 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12422 io->scsiio.sense_len);
12423 msg.scsi.sense_len = io->scsiio.sense_len;
12424 msg.scsi.port_status = io->io_hdr.port_status;
12425 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12426 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12427 ctl_failover_io(io, /*have_lock*/ have_lock);
12430 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12431 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12432 msg.scsi.sense_len, M_WAITOK);
12435 getbinuptime(&cur_bt);
12436 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12437 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12439 io->io_hdr.num_dmas++;
12443 * The DMA to the remote side is done, now we need to tell the other side
12444 * we're done so it can continue with its data movement.
12447 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12454 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12455 printf("%s: ISC DMA write failed with error %d", __func__,
12457 ctl_set_internal_failure(&io->scsiio,
12459 /*retry_count*/ rq->ret);
12462 ctl_dt_req_free(rq);
12464 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12465 free(CTL_LSGLT(io)[i].addr, M_CTL);
12466 free(CTL_RSGL(io), M_CTL);
12467 CTL_RSGL(io) = NULL;
12468 CTL_LSGL(io) = NULL;
12471 * The data is in local and remote memory, so now we need to send
12472 * status (good or back) back to the other side.
12474 ctl_send_datamove_done(io, /*have_lock*/ 0);
12478 * We've moved the data from the host/controller into local memory. Now we
12479 * need to push it over to the remote controller's memory.
12482 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12486 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12487 ctl_datamove_remote_write_cb);
12492 ctl_datamove_remote_write(union ctl_io *io)
12495 void (*fe_datamove)(union ctl_io *io);
12498 * - Get the data from the host/HBA into local memory.
12499 * - DMA memory from the local controller to the remote controller.
12500 * - Send status back to the remote controller.
12503 retval = ctl_datamove_remote_sgl_setup(io);
12507 /* Switch the pointer over so the FETD knows what to do */
12508 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12511 * Use a custom move done callback, since we need to send completion
12512 * back to the other controller, not to the backend on this side.
12514 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12516 fe_datamove = CTL_PORT(io)->fe_datamove;
12521 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12525 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12526 free(CTL_LSGLT(io)[i].addr, M_CTL);
12527 free(CTL_RSGL(io), M_CTL);
12528 CTL_RSGL(io) = NULL;
12529 CTL_LSGL(io) = NULL;
12532 * The read is done, now we need to send status (good or bad) back
12533 * to the other side.
12535 ctl_send_datamove_done(io, /*have_lock*/ 0);
12541 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12544 void (*fe_datamove)(union ctl_io *io);
12548 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12549 printf("%s: ISC DMA read failed with error %d\n", __func__,
12551 ctl_set_internal_failure(&io->scsiio,
12553 /*retry_count*/ rq->ret);
12556 ctl_dt_req_free(rq);
12558 /* Switch the pointer over so the FETD knows what to do */
12559 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12562 * Use a custom move done callback, since we need to send completion
12563 * back to the other controller, not to the backend on this side.
12565 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12567 /* XXX KDM add checks like the ones in ctl_datamove? */
12569 fe_datamove = CTL_PORT(io)->fe_datamove;
12574 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12576 struct ctl_sg_entry *local_sglist;
12577 uint32_t len_to_go;
12582 local_sglist = CTL_LSGL(io);
12583 len_to_go = io->scsiio.kern_data_len;
12586 * The difficult thing here is that the size of the various
12587 * S/G segments may be different than the size from the
12588 * remote controller. That'll make it harder when DMAing
12589 * the data back to the other side.
12591 for (i = 0; len_to_go > 0; i++) {
12592 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12593 local_sglist[i].addr =
12594 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12596 len_to_go -= local_sglist[i].len;
12599 * Reset the number of S/G entries accordingly. The original
12600 * number of S/G entries is available in rem_sg_entries.
12602 io->scsiio.kern_sg_entries = i;
12608 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12609 ctl_ha_dt_cb callback)
12611 struct ctl_ha_dt_req *rq;
12612 struct ctl_sg_entry *remote_sglist, *local_sglist;
12613 uint32_t local_used, remote_used, total_used;
12616 rq = ctl_dt_req_alloc();
12619 * If we failed to allocate the request, and if the DMA didn't fail
12620 * anyway, set busy status. This is just a resource allocation
12624 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12625 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12626 ctl_set_busy(&io->scsiio);
12628 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12629 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12632 ctl_dt_req_free(rq);
12635 * The data move failed. We need to return status back
12636 * to the other controller. No point in trying to DMA
12637 * data to the remote controller.
12640 ctl_send_datamove_done(io, /*have_lock*/ 0);
12645 local_sglist = CTL_LSGL(io);
12646 remote_sglist = CTL_RSGL(io);
12652 * Pull/push the data over the wire from/to the other controller.
12653 * This takes into account the possibility that the local and
12654 * remote sglists may not be identical in terms of the size of
12655 * the elements and the number of elements.
12657 * One fundamental assumption here is that the length allocated for
12658 * both the local and remote sglists is identical. Otherwise, we've
12659 * essentially got a coding error of some sort.
12661 isc_ret = CTL_HA_STATUS_SUCCESS;
12662 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12666 rq->command = command;
12670 * Both pointers should be aligned. But it is possible
12671 * that the allocation length is not. They should both
12672 * also have enough slack left over at the end, though,
12673 * to round up to the next 8 byte boundary.
12675 cur_len = MIN(local_sglist[i].len - local_used,
12676 remote_sglist[j].len - remote_used);
12677 rq->size = cur_len;
12679 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12680 tmp_ptr += local_used;
12683 /* Use physical addresses when talking to ISC hardware */
12684 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12685 /* XXX KDM use busdma */
12686 rq->local = vtophys(tmp_ptr);
12688 rq->local = tmp_ptr;
12690 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12691 ("HA does not support BUS_ADDR"));
12692 rq->local = tmp_ptr;
12695 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12696 tmp_ptr += remote_used;
12697 rq->remote = tmp_ptr;
12699 rq->callback = NULL;
12701 local_used += cur_len;
12702 if (local_used >= local_sglist[i].len) {
12707 remote_used += cur_len;
12708 if (remote_used >= remote_sglist[j].len) {
12712 total_used += cur_len;
12714 if (total_used >= io->scsiio.kern_data_len)
12715 rq->callback = callback;
12717 isc_ret = ctl_dt_single(rq);
12718 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12721 if (isc_ret != CTL_HA_STATUS_WAIT) {
12730 ctl_datamove_remote_read(union ctl_io *io)
12736 * This will send an error to the other controller in the case of a
12739 retval = ctl_datamove_remote_sgl_setup(io);
12743 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12744 ctl_datamove_remote_read_cb);
12747 * Make sure we free memory if there was an error.. The
12748 * ctl_datamove_remote_xfer() function will send the
12749 * datamove done message, or call the callback with an
12750 * error if there is a problem.
12752 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12753 free(CTL_LSGLT(io)[i].addr, M_CTL);
12754 free(CTL_RSGL(io), M_CTL);
12755 CTL_RSGL(io) = NULL;
12756 CTL_LSGL(io) = NULL;
12761 * Process a datamove request from the other controller. This is used for
12762 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12763 * first. Once that is complete, the data gets DMAed into the remote
12764 * controller's memory. For reads, we DMA from the remote controller's
12765 * memory into our memory first, and then move it out to the FETD.
12768 ctl_datamove_remote(union ctl_io *io)
12771 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12773 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12774 ctl_failover_io(io, /*have_lock*/ 0);
12779 * Note that we look for an aborted I/O here, but don't do some of
12780 * the other checks that ctl_datamove() normally does.
12781 * We don't need to run the datamove delay code, since that should
12782 * have been done if need be on the other controller.
12784 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12785 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12786 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12787 io->io_hdr.nexus.targ_port,
12788 io->io_hdr.nexus.targ_lun);
12789 io->io_hdr.port_status = 31338;
12790 ctl_send_datamove_done(io, /*have_lock*/ 0);
12794 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12795 ctl_datamove_remote_write(io);
12796 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12797 ctl_datamove_remote_read(io);
12799 io->io_hdr.port_status = 31339;
12800 ctl_send_datamove_done(io, /*have_lock*/ 0);
12805 ctl_process_done(union ctl_io *io)
12807 struct ctl_softc *softc = CTL_SOFTC(io);
12808 struct ctl_port *port = CTL_PORT(io);
12809 struct ctl_lun *lun = CTL_LUN(io);
12810 void (*fe_done)(union ctl_io *io);
12811 union ctl_ha_msg msg;
12813 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12814 fe_done = port->fe_done;
12817 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12822 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12823 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12825 sbuf_cat(&sb, path_str);
12826 switch (io->io_hdr.io_type) {
12828 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12829 sbuf_printf(&sb, "\n");
12830 sbuf_cat(&sb, path_str);
12831 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12832 io->scsiio.tag_num, io->scsiio.tag_type);
12835 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12836 "Tag Type: %d\n", io->taskio.task_action,
12837 io->taskio.tag_num, io->taskio.tag_type);
12840 panic("%s: Invalid CTL I/O type %d\n",
12841 __func__, io->io_hdr.io_type);
12843 sbuf_cat(&sb, path_str);
12844 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12845 (intmax_t)time_uptime - io->io_hdr.start_time);
12847 printf("%s", sbuf_data(&sb));
12849 #endif /* CTL_TIME_IO */
12851 switch (io->io_hdr.io_type) {
12855 if (ctl_debug & CTL_DEBUG_INFO)
12856 ctl_io_error_print(io, NULL);
12860 panic("%s: Invalid CTL I/O type %d\n",
12861 __func__, io->io_hdr.io_type);
12865 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12866 io->io_hdr.nexus.targ_mapped_lun));
12870 mtx_lock(&lun->lun_lock);
12873 * Check to see if we have any informational exception and status
12874 * of this command can be modified to report it in form of either
12875 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
12877 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
12878 io->io_hdr.status == CTL_SUCCESS &&
12879 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
12880 uint8_t mrie = lun->MODE_IE.mrie;
12881 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
12882 (lun->MODE_VER.byte3 & SMS_VER_PER));
12883 if (((mrie == SIEP_MRIE_REC_COND && per) ||
12884 mrie == SIEP_MRIE_REC_UNCOND ||
12885 mrie == SIEP_MRIE_NO_SENSE) &&
12886 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
12887 CTL_CMD_FLAG_NO_SENSE) == 0) {
12888 ctl_set_sense(&io->scsiio,
12889 /*current_error*/ 1,
12890 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
12891 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
12892 /*asc*/ lun->ie_asc,
12893 /*ascq*/ lun->ie_ascq,
12895 lun->ie_reported = 1;
12897 } else if (lun->ie_reported < 0)
12898 lun->ie_reported = 0;
12901 * Check to see if we have any errors to inject here. We only
12902 * inject errors for commands that don't already have errors set.
12904 if (!STAILQ_EMPTY(&lun->error_list) &&
12905 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
12906 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
12907 ctl_inject_error(lun, io);
12910 * XXX KDM how do we treat commands that aren't completed
12913 * XXX KDM should we also track I/O latency?
12915 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
12916 io->io_hdr.io_type == CTL_IO_SCSI) {
12922 bintime_sub(&bt, &io->io_hdr.start_bt);
12924 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12926 type = CTL_STATS_READ;
12927 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
12929 type = CTL_STATS_WRITE;
12931 type = CTL_STATS_NO_IO;
12933 lun->stats.bytes[type] += io->scsiio.kern_total_len;
12934 lun->stats.operations[type] ++;
12935 lun->stats.dmas[type] += io->io_hdr.num_dmas;
12937 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
12938 bintime_add(&lun->stats.time[type], &bt);
12941 mtx_lock(&port->port_lock);
12942 port->stats.bytes[type] += io->scsiio.kern_total_len;
12943 port->stats.operations[type] ++;
12944 port->stats.dmas[type] += io->io_hdr.num_dmas;
12946 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
12947 bintime_add(&port->stats.time[type], &bt);
12949 mtx_unlock(&port->port_lock);
12953 * Run through the blocked queue of this I/O and see if anything
12954 * can be unblocked, now that this I/O is done and will be removed.
12955 * We need to do it before removal to have OOA position to start.
12957 ctl_try_unblock_others(lun, io, TRUE);
12960 * Remove this from the OOA queue.
12962 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12964 if (TAILQ_EMPTY(&lun->ooa_queue))
12965 lun->last_busy = getsbinuptime();
12969 * If the LUN has been invalidated, free it if there is nothing
12970 * left on its OOA queue.
12972 if ((lun->flags & CTL_LUN_INVALID)
12973 && TAILQ_EMPTY(&lun->ooa_queue)) {
12974 mtx_unlock(&lun->lun_lock);
12977 mtx_unlock(&lun->lun_lock);
12982 * If this command has been aborted, make sure we set the status
12983 * properly. The FETD is responsible for freeing the I/O and doing
12984 * whatever it needs to do to clean up its state.
12986 if (io->io_hdr.flags & CTL_FLAG_ABORT)
12987 ctl_set_task_aborted(&io->scsiio);
12990 * If enabled, print command error status.
12992 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
12993 (ctl_debug & CTL_DEBUG_INFO) != 0)
12994 ctl_io_error_print(io, NULL);
12997 * Tell the FETD or the other shelf controller we're done with this
12998 * command. Note that only SCSI commands get to this point. Task
12999 * management commands are completed above.
13001 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13002 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13003 memset(&msg, 0, sizeof(msg));
13004 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13005 msg.hdr.serializing_sc = io->io_hdr.remote_io;
13006 msg.hdr.nexus = io->io_hdr.nexus;
13007 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13008 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13016 * Front end should call this if it doesn't do autosense. When the request
13017 * sense comes back in from the initiator, we'll dequeue this and send it.
13020 ctl_queue_sense(union ctl_io *io)
13022 struct ctl_softc *softc = CTL_SOFTC(io);
13023 struct ctl_port *port = CTL_PORT(io);
13024 struct ctl_lun *lun;
13025 struct scsi_sense_data *ps;
13026 uint32_t initidx, p, targ_lun;
13028 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13030 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13033 * LUN lookup will likely move to the ctl_work_thread() once we
13034 * have our new queueing infrastructure (that doesn't put things on
13035 * a per-LUN queue initially). That is so that we can handle
13036 * things like an INQUIRY to a LUN that we don't have enabled. We
13037 * can't deal with that right now.
13038 * If we don't have a LUN for this, just toss the sense information.
13040 mtx_lock(&softc->ctl_lock);
13041 if (targ_lun >= ctl_max_luns ||
13042 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13043 mtx_unlock(&softc->ctl_lock);
13046 mtx_lock(&lun->lun_lock);
13047 mtx_unlock(&softc->ctl_lock);
13049 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13050 p = initidx / CTL_MAX_INIT_PER_PORT;
13051 if (lun->pending_sense[p] == NULL) {
13052 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13053 M_CTL, M_NOWAIT | M_ZERO);
13055 if ((ps = lun->pending_sense[p]) != NULL) {
13056 ps += initidx % CTL_MAX_INIT_PER_PORT;
13057 memset(ps, 0, sizeof(*ps));
13058 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13060 mtx_unlock(&lun->lun_lock);
13064 return (CTL_RETVAL_COMPLETE);
13068 * Primary command inlet from frontend ports. All SCSI and task I/O
13069 * requests must go through this function.
13072 ctl_queue(union ctl_io *io)
13074 struct ctl_port *port = CTL_PORT(io);
13076 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13079 io->io_hdr.start_time = time_uptime;
13080 getbinuptime(&io->io_hdr.start_bt);
13081 #endif /* CTL_TIME_IO */
13083 /* Map FE-specific LUN ID into global one. */
13084 io->io_hdr.nexus.targ_mapped_lun =
13085 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13087 switch (io->io_hdr.io_type) {
13090 if (ctl_debug & CTL_DEBUG_CDB)
13092 ctl_enqueue_incoming(io);
13095 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13099 return (CTL_RETVAL_COMPLETE);
13102 #ifdef CTL_IO_DELAY
13104 ctl_done_timer_wakeup(void *arg)
13108 io = (union ctl_io *)arg;
13111 #endif /* CTL_IO_DELAY */
13114 ctl_serseq_done(union ctl_io *io)
13116 struct ctl_lun *lun = CTL_LUN(io);;
13118 if (lun->be_lun == NULL ||
13119 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13121 mtx_lock(&lun->lun_lock);
13122 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13123 ctl_try_unblock_others(lun, io, FALSE);
13124 mtx_unlock(&lun->lun_lock);
13128 ctl_done(union ctl_io *io)
13132 * Enable this to catch duplicate completion issues.
13135 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13136 printf("%s: type %d msg %d cdb %x iptl: "
13137 "%u:%u:%u tag 0x%04x "
13138 "flag %#x status %x\n",
13140 io->io_hdr.io_type,
13141 io->io_hdr.msg_type,
13143 io->io_hdr.nexus.initid,
13144 io->io_hdr.nexus.targ_port,
13145 io->io_hdr.nexus.targ_lun,
13146 (io->io_hdr.io_type ==
13148 io->taskio.tag_num :
13149 io->scsiio.tag_num,
13151 io->io_hdr.status);
13153 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13157 * This is an internal copy of an I/O, and should not go through
13158 * the normal done processing logic.
13160 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13163 #ifdef CTL_IO_DELAY
13164 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13165 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13167 struct ctl_lun *lun = CTL_LUN(io);
13170 && (lun->delay_info.done_delay > 0)) {
13172 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13173 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13174 callout_reset(&io->io_hdr.delay_callout,
13175 lun->delay_info.done_delay * hz,
13176 ctl_done_timer_wakeup, io);
13177 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13178 lun->delay_info.done_delay = 0;
13182 #endif /* CTL_IO_DELAY */
13184 ctl_enqueue_done(io);
13188 ctl_work_thread(void *arg)
13190 struct ctl_thread *thr = (struct ctl_thread *)arg;
13191 struct ctl_softc *softc = thr->ctl_softc;
13195 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13196 thread_lock(curthread);
13197 sched_prio(curthread, PUSER - 1);
13198 thread_unlock(curthread);
13200 while (!softc->shutdown) {
13202 * We handle the queues in this order:
13204 * - done queue (to free up resources, unblock other commands)
13208 * If those queues are empty, we break out of the loop and
13211 mtx_lock(&thr->queue_lock);
13212 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13214 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13215 mtx_unlock(&thr->queue_lock);
13216 ctl_handle_isc(io);
13219 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13221 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13222 /* clear any blocked commands, call fe_done */
13223 mtx_unlock(&thr->queue_lock);
13224 ctl_process_done(io);
13227 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13229 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13230 mtx_unlock(&thr->queue_lock);
13231 if (io->io_hdr.io_type == CTL_IO_TASK)
13234 ctl_scsiio_precheck(softc, &io->scsiio);
13237 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13239 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13240 mtx_unlock(&thr->queue_lock);
13241 retval = ctl_scsiio(&io->scsiio);
13242 if (retval != CTL_RETVAL_COMPLETE)
13243 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13247 /* Sleep until we have something to do. */
13248 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0);
13250 thr->thread = NULL;
13255 ctl_lun_thread(void *arg)
13257 struct ctl_softc *softc = (struct ctl_softc *)arg;
13258 struct ctl_be_lun *be_lun;
13260 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13261 thread_lock(curthread);
13262 sched_prio(curthread, PUSER - 1);
13263 thread_unlock(curthread);
13265 while (!softc->shutdown) {
13266 mtx_lock(&softc->ctl_lock);
13267 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13268 if (be_lun != NULL) {
13269 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13270 mtx_unlock(&softc->ctl_lock);
13271 ctl_create_lun(be_lun);
13275 /* Sleep until we have something to do. */
13276 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13279 softc->lun_thread = NULL;
13284 ctl_thresh_thread(void *arg)
13286 struct ctl_softc *softc = (struct ctl_softc *)arg;
13287 struct ctl_lun *lun;
13288 struct ctl_logical_block_provisioning_page *page;
13290 union ctl_ha_msg msg;
13291 uint64_t thres, val;
13294 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13295 thread_lock(curthread);
13296 sched_prio(curthread, PUSER - 1);
13297 thread_unlock(curthread);
13299 while (!softc->shutdown) {
13300 mtx_lock(&softc->ctl_lock);
13301 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13302 if ((lun->flags & CTL_LUN_DISABLED) ||
13303 (lun->flags & CTL_LUN_NO_MEDIA) ||
13304 lun->backend->lun_attr == NULL)
13306 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13307 softc->ha_mode == CTL_HA_MODE_XFER)
13309 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13312 page = &lun->MODE_LBP;
13313 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13314 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13316 thres = scsi_4btoul(page->descr[i].count);
13317 thres <<= CTL_LBP_EXPONENT;
13318 switch (page->descr[i].resource) {
13320 attr = "blocksavail";
13323 attr = "blocksused";
13326 attr = "poolblocksavail";
13329 attr = "poolblocksused";
13334 mtx_unlock(&softc->ctl_lock); // XXX
13335 val = lun->backend->lun_attr(
13336 lun->be_lun->be_lun, attr);
13337 mtx_lock(&softc->ctl_lock);
13338 if (val == UINT64_MAX)
13340 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13341 == SLBPPD_ARMING_INC)
13342 e = (val >= thres);
13344 e = (val <= thres);
13348 mtx_lock(&lun->lun_lock);
13350 scsi_u64to8b((uint8_t *)&page->descr[i] -
13351 (uint8_t *)page, lun->ua_tpt_info);
13352 if (lun->lasttpt == 0 ||
13353 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13354 lun->lasttpt = time_uptime;
13355 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13361 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13364 mtx_unlock(&lun->lun_lock);
13366 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13367 /* Send msg to other side. */
13368 bzero(&msg.ua, sizeof(msg.ua));
13369 msg.hdr.msg_type = CTL_MSG_UA;
13370 msg.hdr.nexus.initid = -1;
13371 msg.hdr.nexus.targ_port = -1;
13372 msg.hdr.nexus.targ_lun = lun->lun;
13373 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13375 msg.ua.ua_set = (set > 0);
13376 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13377 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13378 mtx_unlock(&softc->ctl_lock); // XXX
13379 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13380 sizeof(msg.ua), M_WAITOK);
13381 mtx_lock(&softc->ctl_lock);
13384 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13385 PDROP, "-", CTL_LBP_PERIOD * hz);
13387 softc->thresh_thread = NULL;
13392 ctl_enqueue_incoming(union ctl_io *io)
13394 struct ctl_softc *softc = CTL_SOFTC(io);
13395 struct ctl_thread *thr;
13398 idx = (io->io_hdr.nexus.targ_port * 127 +
13399 io->io_hdr.nexus.initid) % worker_threads;
13400 thr = &softc->threads[idx];
13401 mtx_lock(&thr->queue_lock);
13402 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13403 mtx_unlock(&thr->queue_lock);
13408 ctl_enqueue_rtr(union ctl_io *io)
13410 struct ctl_softc *softc = CTL_SOFTC(io);
13411 struct ctl_thread *thr;
13413 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13414 mtx_lock(&thr->queue_lock);
13415 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13416 mtx_unlock(&thr->queue_lock);
13421 ctl_enqueue_done(union ctl_io *io)
13423 struct ctl_softc *softc = CTL_SOFTC(io);
13424 struct ctl_thread *thr;
13426 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13427 mtx_lock(&thr->queue_lock);
13428 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13429 mtx_unlock(&thr->queue_lock);
13434 ctl_enqueue_isc(union ctl_io *io)
13436 struct ctl_softc *softc = CTL_SOFTC(io);
13437 struct ctl_thread *thr;
13439 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13440 mtx_lock(&thr->queue_lock);
13441 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13442 mtx_unlock(&thr->queue_lock);