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), SCSI Feature Sets (0x92),
452 * Block limits (0xB0), Block Device Characteristics (0xB1) and
453 * Logical Block Provisioning (0xB2)
455 #define SCSI_EVPD_NUM_SUPPORTED_PAGES 11
457 static void ctl_isc_event_handler(ctl_ha_channel chanel, ctl_ha_event event,
459 static void ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest);
460 static void ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest);
461 static int ctl_init(void);
462 static int ctl_shutdown(void);
463 static int ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td);
464 static int ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td);
465 static void ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio);
466 static void ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
467 struct ctl_ooa *ooa_hdr,
468 struct ctl_ooa_entry *kern_entries);
469 static int ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
471 static int ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *lun,
472 struct ctl_be_lun *be_lun);
473 static int ctl_free_lun(struct ctl_lun *lun);
474 static void ctl_create_lun(struct ctl_be_lun *be_lun);
476 static int ctl_do_mode_select(union ctl_io *io);
477 static int ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun,
478 uint64_t res_key, uint64_t sa_res_key,
479 uint8_t type, uint32_t residx,
480 struct ctl_scsiio *ctsio,
481 struct scsi_per_res_out *cdb,
482 struct scsi_per_res_out_parms* param);
483 static void ctl_pro_preempt_other(struct ctl_lun *lun,
484 union ctl_ha_msg *msg);
485 static void ctl_hndl_per_res_out_on_other_sc(union ctl_io *io);
486 static int ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len);
487 static int ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len);
488 static int ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len);
489 static int ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len);
490 static int ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len);
491 static int ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio,
493 static int ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len);
494 static int ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio,
496 static int ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len);
497 static int ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len);
498 static int ctl_inquiry_evpd(struct ctl_scsiio *ctsio);
499 static int ctl_inquiry_std(struct ctl_scsiio *ctsio);
500 static int ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len);
501 static ctl_action ctl_extent_check(union ctl_io *io1, union ctl_io *io2,
503 static ctl_action ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2);
504 static ctl_action ctl_check_for_blockage(struct ctl_lun *lun,
505 union ctl_io *pending_io, union ctl_io *ooa_io);
506 static ctl_action ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
507 union ctl_io **starting_io);
508 static void ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io,
510 static void ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *io,
512 static int ctl_scsiio_lun_check(struct ctl_lun *lun,
513 const struct ctl_cmd_entry *entry,
514 struct ctl_scsiio *ctsio);
515 static void ctl_failover_lun(union ctl_io *io);
516 static int ctl_scsiio_precheck(struct ctl_softc *ctl_softc,
517 struct ctl_scsiio *ctsio);
518 static int ctl_scsiio(struct ctl_scsiio *ctsio);
520 static int ctl_target_reset(union ctl_io *io);
521 static void ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx,
522 ctl_ua_type ua_type);
523 static int ctl_lun_reset(union ctl_io *io);
524 static int ctl_abort_task(union ctl_io *io);
525 static int ctl_abort_task_set(union ctl_io *io);
526 static int ctl_query_task(union ctl_io *io, int task_set);
527 static void ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
528 ctl_ua_type ua_type);
529 static int ctl_i_t_nexus_reset(union ctl_io *io);
530 static int ctl_query_async_event(union ctl_io *io);
531 static void ctl_run_task(union ctl_io *io);
533 static void ctl_datamove_timer_wakeup(void *arg);
534 static void ctl_done_timer_wakeup(void *arg);
535 #endif /* CTL_IO_DELAY */
537 static void ctl_send_datamove_done(union ctl_io *io, int have_lock);
538 static void ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq);
539 static int ctl_datamove_remote_dm_write_cb(union ctl_io *io);
540 static void ctl_datamove_remote_write(union ctl_io *io);
541 static int ctl_datamove_remote_dm_read_cb(union ctl_io *io);
542 static void ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq);
543 static int ctl_datamove_remote_sgl_setup(union ctl_io *io);
544 static int ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
545 ctl_ha_dt_cb callback);
546 static void ctl_datamove_remote_read(union ctl_io *io);
547 static void ctl_datamove_remote(union ctl_io *io);
548 static void ctl_process_done(union ctl_io *io);
549 static void ctl_lun_thread(void *arg);
550 static void ctl_thresh_thread(void *arg);
551 static void ctl_work_thread(void *arg);
552 static void ctl_enqueue_incoming(union ctl_io *io);
553 static void ctl_enqueue_rtr(union ctl_io *io);
554 static void ctl_enqueue_done(union ctl_io *io);
555 static void ctl_enqueue_isc(union ctl_io *io);
556 static const struct ctl_cmd_entry *
557 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa);
558 static const struct ctl_cmd_entry *
559 ctl_validate_command(struct ctl_scsiio *ctsio);
560 static int ctl_cmd_applicable(uint8_t lun_type,
561 const struct ctl_cmd_entry *entry);
562 static int ctl_ha_init(void);
563 static int ctl_ha_shutdown(void);
565 static uint64_t ctl_get_prkey(struct ctl_lun *lun, uint32_t residx);
566 static void ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx);
567 static void ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx);
568 static void ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key);
571 * Load the serialization table. This isn't very pretty, but is probably
572 * the easiest way to do it.
574 #include "ctl_ser_table.c"
577 * We only need to define open, close and ioctl routines for this driver.
579 static struct cdevsw ctl_cdevsw = {
580 .d_version = D_VERSION,
583 .d_close = ctl_close,
584 .d_ioctl = ctl_ioctl,
589 MALLOC_DEFINE(M_CTL, "ctlmem", "Memory used for CTL");
591 static int ctl_module_event_handler(module_t, int /*modeventtype_t*/, void *);
593 static moduledata_t ctl_moduledata = {
595 ctl_module_event_handler,
599 DECLARE_MODULE(ctl, ctl_moduledata, SI_SUB_CONFIGURE, SI_ORDER_THIRD);
600 MODULE_VERSION(ctl, 1);
602 static struct ctl_frontend ha_frontend =
606 .shutdown = ctl_ha_shutdown,
612 struct ctl_softc *softc = control_softc;
614 if (ctl_pool_create(softc, "othersc", CTL_POOL_ENTRIES_OTHER_SC,
615 &softc->othersc_pool) != 0)
617 if (ctl_ha_msg_init(softc) != CTL_HA_STATUS_SUCCESS) {
618 ctl_pool_free(softc->othersc_pool);
621 if (ctl_ha_msg_register(CTL_HA_CHAN_CTL, ctl_isc_event_handler)
622 != CTL_HA_STATUS_SUCCESS) {
623 ctl_ha_msg_destroy(softc);
624 ctl_pool_free(softc->othersc_pool);
631 ctl_ha_shutdown(void)
633 struct ctl_softc *softc = control_softc;
634 struct ctl_port *port;
636 ctl_ha_msg_shutdown(softc);
637 if (ctl_ha_msg_deregister(CTL_HA_CHAN_CTL) != CTL_HA_STATUS_SUCCESS)
639 if (ctl_ha_msg_destroy(softc) != CTL_HA_STATUS_SUCCESS)
641 ctl_pool_free(softc->othersc_pool);
642 while ((port = STAILQ_FIRST(&ha_frontend.port_list)) != NULL) {
643 ctl_port_deregister(port);
644 free(port->port_name, M_CTL);
651 ctl_ha_datamove(union ctl_io *io)
653 struct ctl_lun *lun = CTL_LUN(io);
654 struct ctl_sg_entry *sgl;
655 union ctl_ha_msg msg;
656 uint32_t sg_entries_sent;
657 int do_sg_copy, i, j;
659 memset(&msg.dt, 0, sizeof(msg.dt));
660 msg.hdr.msg_type = CTL_MSG_DATAMOVE;
661 msg.hdr.original_sc = io->io_hdr.remote_io;
662 msg.hdr.serializing_sc = io;
663 msg.hdr.nexus = io->io_hdr.nexus;
664 msg.hdr.status = io->io_hdr.status;
665 msg.dt.flags = io->io_hdr.flags;
668 * We convert everything into a S/G list here. We can't
669 * pass by reference, only by value between controllers.
670 * So we can't pass a pointer to the S/G list, only as many
671 * S/G entries as we can fit in here. If it's possible for
672 * us to get more than CTL_HA_MAX_SG_ENTRIES S/G entries,
673 * then we need to break this up into multiple transfers.
675 if (io->scsiio.kern_sg_entries == 0) {
676 msg.dt.kern_sg_entries = 1;
678 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
679 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
681 /* XXX KDM use busdma here! */
682 msg.dt.sg_list[0].addr =
683 (void *)vtophys(io->scsiio.kern_data_ptr);
686 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
687 ("HA does not support BUS_ADDR"));
688 msg.dt.sg_list[0].addr = io->scsiio.kern_data_ptr;
690 msg.dt.sg_list[0].len = io->scsiio.kern_data_len;
693 msg.dt.kern_sg_entries = io->scsiio.kern_sg_entries;
697 msg.dt.kern_data_len = io->scsiio.kern_data_len;
698 msg.dt.kern_total_len = io->scsiio.kern_total_len;
699 msg.dt.kern_data_resid = io->scsiio.kern_data_resid;
700 msg.dt.kern_rel_offset = io->scsiio.kern_rel_offset;
701 msg.dt.sg_sequence = 0;
704 * Loop until we've sent all of the S/G entries. On the
705 * other end, we'll recompose these S/G entries into one
706 * contiguous list before processing.
708 for (sg_entries_sent = 0; sg_entries_sent < msg.dt.kern_sg_entries;
709 msg.dt.sg_sequence++) {
710 msg.dt.cur_sg_entries = MIN((sizeof(msg.dt.sg_list) /
711 sizeof(msg.dt.sg_list[0])),
712 msg.dt.kern_sg_entries - sg_entries_sent);
713 if (do_sg_copy != 0) {
714 sgl = (struct ctl_sg_entry *)io->scsiio.kern_data_ptr;
715 for (i = sg_entries_sent, j = 0;
716 i < msg.dt.cur_sg_entries; i++, j++) {
718 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
719 msg.dt.sg_list[j].addr = sgl[i].addr;
721 /* XXX KDM use busdma here! */
722 msg.dt.sg_list[j].addr =
723 (void *)vtophys(sgl[i].addr);
726 KASSERT((io->io_hdr.flags &
727 CTL_FLAG_BUS_ADDR) == 0,
728 ("HA does not support BUS_ADDR"));
729 msg.dt.sg_list[j].addr = sgl[i].addr;
731 msg.dt.sg_list[j].len = sgl[i].len;
735 sg_entries_sent += msg.dt.cur_sg_entries;
736 msg.dt.sg_last = (sg_entries_sent >= msg.dt.kern_sg_entries);
737 if (ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
738 sizeof(msg.dt) - sizeof(msg.dt.sg_list) +
739 sizeof(struct ctl_sg_entry) * msg.dt.cur_sg_entries,
740 M_WAITOK) > CTL_HA_STATUS_SUCCESS) {
741 io->io_hdr.port_status = 31341;
742 io->scsiio.be_move_done(io);
745 msg.dt.sent_sg_entries = sg_entries_sent;
749 * Officially handover the request from us to peer.
750 * If failover has just happened, then we must return error.
751 * If failover happen just after, then it is not our problem.
754 mtx_lock(&lun->lun_lock);
755 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
757 mtx_unlock(&lun->lun_lock);
758 io->io_hdr.port_status = 31342;
759 io->scsiio.be_move_done(io);
762 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
763 io->io_hdr.flags |= CTL_FLAG_DMA_INPROG;
765 mtx_unlock(&lun->lun_lock);
769 ctl_ha_done(union ctl_io *io)
771 union ctl_ha_msg msg;
773 if (io->io_hdr.io_type == CTL_IO_SCSI) {
774 memset(&msg, 0, sizeof(msg));
775 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
776 msg.hdr.original_sc = io->io_hdr.remote_io;
777 msg.hdr.nexus = io->io_hdr.nexus;
778 msg.hdr.status = io->io_hdr.status;
779 msg.scsi.scsi_status = io->scsiio.scsi_status;
780 msg.scsi.tag_num = io->scsiio.tag_num;
781 msg.scsi.tag_type = io->scsiio.tag_type;
782 msg.scsi.sense_len = io->scsiio.sense_len;
783 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
784 io->scsiio.sense_len);
785 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
786 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
787 msg.scsi.sense_len, M_WAITOK);
793 ctl_isc_handler_finish_xfer(struct ctl_softc *ctl_softc,
794 union ctl_ha_msg *msg_info)
796 struct ctl_scsiio *ctsio;
798 if (msg_info->hdr.original_sc == NULL) {
799 printf("%s: original_sc == NULL!\n", __func__);
800 /* XXX KDM now what? */
804 ctsio = &msg_info->hdr.original_sc->scsiio;
805 ctsio->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
806 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
807 ctsio->io_hdr.status = msg_info->hdr.status;
808 ctsio->scsi_status = msg_info->scsi.scsi_status;
809 ctsio->sense_len = msg_info->scsi.sense_len;
810 memcpy(&ctsio->sense_data, &msg_info->scsi.sense_data,
811 msg_info->scsi.sense_len);
812 ctl_enqueue_isc((union ctl_io *)ctsio);
816 ctl_isc_handler_finish_ser_only(struct ctl_softc *ctl_softc,
817 union ctl_ha_msg *msg_info)
819 struct ctl_scsiio *ctsio;
821 if (msg_info->hdr.serializing_sc == NULL) {
822 printf("%s: serializing_sc == NULL!\n", __func__);
823 /* XXX KDM now what? */
827 ctsio = &msg_info->hdr.serializing_sc->scsiio;
828 ctsio->io_hdr.msg_type = CTL_MSG_FINISH_IO;
829 ctl_enqueue_isc((union ctl_io *)ctsio);
833 ctl_isc_announce_lun(struct ctl_lun *lun)
835 struct ctl_softc *softc = lun->ctl_softc;
836 union ctl_ha_msg *msg;
837 struct ctl_ha_msg_lun_pr_key pr_key;
840 if (softc->ha_link != CTL_HA_LINK_ONLINE)
842 mtx_lock(&lun->lun_lock);
843 i = sizeof(msg->lun);
845 i += lun->lun_devid->len;
846 i += sizeof(pr_key) * lun->pr_key_count;
848 mtx_unlock(&lun->lun_lock);
849 msg = malloc(i, M_CTL, M_WAITOK);
850 mtx_lock(&lun->lun_lock);
851 k = sizeof(msg->lun);
853 k += lun->lun_devid->len;
854 k += sizeof(pr_key) * lun->pr_key_count;
860 bzero(&msg->lun, sizeof(msg->lun));
861 msg->hdr.msg_type = CTL_MSG_LUN_SYNC;
862 msg->hdr.nexus.targ_lun = lun->lun;
863 msg->hdr.nexus.targ_mapped_lun = lun->lun;
864 msg->lun.flags = lun->flags;
865 msg->lun.pr_generation = lun->pr_generation;
866 msg->lun.pr_res_idx = lun->pr_res_idx;
867 msg->lun.pr_res_type = lun->pr_res_type;
868 msg->lun.pr_key_count = lun->pr_key_count;
870 if (lun->lun_devid) {
871 msg->lun.lun_devid_len = lun->lun_devid->len;
872 memcpy(&msg->lun.data[i], lun->lun_devid->data,
873 msg->lun.lun_devid_len);
874 i += msg->lun.lun_devid_len;
876 for (k = 0; k < CTL_MAX_INITIATORS; k++) {
877 if ((pr_key.pr_key = ctl_get_prkey(lun, k)) == 0)
880 memcpy(&msg->lun.data[i], &pr_key, sizeof(pr_key));
883 mtx_unlock(&lun->lun_lock);
884 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
888 if (lun->flags & CTL_LUN_PRIMARY_SC) {
889 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
890 ctl_isc_announce_mode(lun, -1,
891 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
892 lun->mode_pages.index[i].subpage);
898 ctl_isc_announce_port(struct ctl_port *port)
900 struct ctl_softc *softc = port->ctl_softc;
901 union ctl_ha_msg *msg;
904 if (port->targ_port < softc->port_min ||
905 port->targ_port >= softc->port_max ||
906 softc->ha_link != CTL_HA_LINK_ONLINE)
908 i = sizeof(msg->port) + strlen(port->port_name) + 1;
910 i += port->lun_map_size * sizeof(uint32_t);
911 if (port->port_devid)
912 i += port->port_devid->len;
913 if (port->target_devid)
914 i += port->target_devid->len;
915 if (port->init_devid)
916 i += port->init_devid->len;
917 msg = malloc(i, M_CTL, M_WAITOK);
918 bzero(&msg->port, sizeof(msg->port));
919 msg->hdr.msg_type = CTL_MSG_PORT_SYNC;
920 msg->hdr.nexus.targ_port = port->targ_port;
921 msg->port.port_type = port->port_type;
922 msg->port.physical_port = port->physical_port;
923 msg->port.virtual_port = port->virtual_port;
924 msg->port.status = port->status;
926 msg->port.name_len = sprintf(&msg->port.data[i],
927 "%d:%s", softc->ha_id, port->port_name) + 1;
928 i += msg->port.name_len;
930 msg->port.lun_map_len = port->lun_map_size * sizeof(uint32_t);
931 memcpy(&msg->port.data[i], port->lun_map,
932 msg->port.lun_map_len);
933 i += msg->port.lun_map_len;
935 if (port->port_devid) {
936 msg->port.port_devid_len = port->port_devid->len;
937 memcpy(&msg->port.data[i], port->port_devid->data,
938 msg->port.port_devid_len);
939 i += msg->port.port_devid_len;
941 if (port->target_devid) {
942 msg->port.target_devid_len = port->target_devid->len;
943 memcpy(&msg->port.data[i], port->target_devid->data,
944 msg->port.target_devid_len);
945 i += msg->port.target_devid_len;
947 if (port->init_devid) {
948 msg->port.init_devid_len = port->init_devid->len;
949 memcpy(&msg->port.data[i], port->init_devid->data,
950 msg->port.init_devid_len);
951 i += msg->port.init_devid_len;
953 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->port, sizeof(msg->port) + i,
959 ctl_isc_announce_iid(struct ctl_port *port, int iid)
961 struct ctl_softc *softc = port->ctl_softc;
962 union ctl_ha_msg *msg;
965 if (port->targ_port < softc->port_min ||
966 port->targ_port >= softc->port_max ||
967 softc->ha_link != CTL_HA_LINK_ONLINE)
969 mtx_lock(&softc->ctl_lock);
970 i = sizeof(msg->iid);
972 if (port->wwpn_iid[iid].name)
973 l = strlen(port->wwpn_iid[iid].name) + 1;
975 msg = malloc(i, M_CTL, M_NOWAIT);
977 mtx_unlock(&softc->ctl_lock);
980 bzero(&msg->iid, sizeof(msg->iid));
981 msg->hdr.msg_type = CTL_MSG_IID_SYNC;
982 msg->hdr.nexus.targ_port = port->targ_port;
983 msg->hdr.nexus.initid = iid;
984 msg->iid.in_use = port->wwpn_iid[iid].in_use;
985 msg->iid.name_len = l;
986 msg->iid.wwpn = port->wwpn_iid[iid].wwpn;
987 if (port->wwpn_iid[iid].name)
988 strlcpy(msg->iid.data, port->wwpn_iid[iid].name, l);
989 mtx_unlock(&softc->ctl_lock);
990 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg->iid, i, M_NOWAIT);
995 ctl_isc_announce_mode(struct ctl_lun *lun, uint32_t initidx,
996 uint8_t page, uint8_t subpage)
998 struct ctl_softc *softc = lun->ctl_softc;
999 union ctl_ha_msg msg;
1002 if (softc->ha_link != CTL_HA_LINK_ONLINE)
1004 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1005 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1006 page && lun->mode_pages.index[i].subpage == subpage)
1009 if (i == CTL_NUM_MODE_PAGES)
1012 /* Don't try to replicate pages not present on this device. */
1013 if (lun->mode_pages.index[i].page_data == NULL)
1016 bzero(&msg.mode, sizeof(msg.mode));
1017 msg.hdr.msg_type = CTL_MSG_MODE_SYNC;
1018 msg.hdr.nexus.targ_port = initidx / CTL_MAX_INIT_PER_PORT;
1019 msg.hdr.nexus.initid = initidx % CTL_MAX_INIT_PER_PORT;
1020 msg.hdr.nexus.targ_lun = lun->lun;
1021 msg.hdr.nexus.targ_mapped_lun = lun->lun;
1022 msg.mode.page_code = page;
1023 msg.mode.subpage = subpage;
1024 msg.mode.page_len = lun->mode_pages.index[i].page_len;
1025 memcpy(msg.mode.data, lun->mode_pages.index[i].page_data,
1027 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.mode, sizeof(msg.mode),
1032 ctl_isc_ha_link_up(struct ctl_softc *softc)
1034 struct ctl_port *port;
1035 struct ctl_lun *lun;
1036 union ctl_ha_msg msg;
1039 /* Announce this node parameters to peer for validation. */
1040 msg.login.msg_type = CTL_MSG_LOGIN;
1041 msg.login.version = CTL_HA_VERSION;
1042 msg.login.ha_mode = softc->ha_mode;
1043 msg.login.ha_id = softc->ha_id;
1044 msg.login.max_luns = ctl_max_luns;
1045 msg.login.max_ports = ctl_max_ports;
1046 msg.login.max_init_per_port = CTL_MAX_INIT_PER_PORT;
1047 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg.login, sizeof(msg.login),
1050 STAILQ_FOREACH(port, &softc->port_list, links) {
1051 ctl_isc_announce_port(port);
1052 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1053 if (port->wwpn_iid[i].in_use)
1054 ctl_isc_announce_iid(port, i);
1057 STAILQ_FOREACH(lun, &softc->lun_list, links)
1058 ctl_isc_announce_lun(lun);
1062 ctl_isc_ha_link_down(struct ctl_softc *softc)
1064 struct ctl_port *port;
1065 struct ctl_lun *lun;
1069 mtx_lock(&softc->ctl_lock);
1070 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1071 mtx_lock(&lun->lun_lock);
1072 if (lun->flags & CTL_LUN_PEER_SC_PRIMARY) {
1073 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1074 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1076 mtx_unlock(&lun->lun_lock);
1078 mtx_unlock(&softc->ctl_lock);
1079 io = ctl_alloc_io(softc->othersc_pool);
1080 mtx_lock(&softc->ctl_lock);
1082 io->io_hdr.msg_type = CTL_MSG_FAILOVER;
1083 io->io_hdr.nexus.targ_mapped_lun = lun->lun;
1084 ctl_enqueue_isc(io);
1087 STAILQ_FOREACH(port, &softc->port_list, links) {
1088 if (port->targ_port >= softc->port_min &&
1089 port->targ_port < softc->port_max)
1091 port->status &= ~CTL_PORT_STATUS_ONLINE;
1092 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1093 port->wwpn_iid[i].in_use = 0;
1094 free(port->wwpn_iid[i].name, M_CTL);
1095 port->wwpn_iid[i].name = NULL;
1098 mtx_unlock(&softc->ctl_lock);
1102 ctl_isc_ua(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1104 struct ctl_lun *lun;
1105 uint32_t iid = ctl_get_initindex(&msg->hdr.nexus);
1107 mtx_lock(&softc->ctl_lock);
1108 if (msg->hdr.nexus.targ_mapped_lun >= ctl_max_luns ||
1109 (lun = softc->ctl_luns[msg->hdr.nexus.targ_mapped_lun]) == NULL) {
1110 mtx_unlock(&softc->ctl_lock);
1113 mtx_lock(&lun->lun_lock);
1114 mtx_unlock(&softc->ctl_lock);
1115 if (msg->ua.ua_type == CTL_UA_THIN_PROV_THRES && msg->ua.ua_set)
1116 memcpy(lun->ua_tpt_info, msg->ua.ua_info, 8);
1117 if (msg->ua.ua_all) {
1119 ctl_est_ua_all(lun, iid, msg->ua.ua_type);
1121 ctl_clr_ua_all(lun, iid, msg->ua.ua_type);
1124 ctl_est_ua(lun, iid, msg->ua.ua_type);
1126 ctl_clr_ua(lun, iid, msg->ua.ua_type);
1128 mtx_unlock(&lun->lun_lock);
1132 ctl_isc_lun_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1134 struct ctl_lun *lun;
1135 struct ctl_ha_msg_lun_pr_key pr_key;
1137 ctl_lun_flags oflags;
1140 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1141 mtx_lock(&softc->ctl_lock);
1142 if (targ_lun >= ctl_max_luns ||
1143 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1144 mtx_unlock(&softc->ctl_lock);
1147 mtx_lock(&lun->lun_lock);
1148 mtx_unlock(&softc->ctl_lock);
1149 if (lun->flags & CTL_LUN_DISABLED) {
1150 mtx_unlock(&lun->lun_lock);
1153 i = (lun->lun_devid != NULL) ? lun->lun_devid->len : 0;
1154 if (msg->lun.lun_devid_len != i || (i > 0 &&
1155 memcmp(&msg->lun.data[0], lun->lun_devid->data, i) != 0)) {
1156 mtx_unlock(&lun->lun_lock);
1157 printf("%s: Received conflicting HA LUN %d\n",
1158 __func__, targ_lun);
1161 /* Record whether peer is primary. */
1162 oflags = lun->flags;
1163 if ((msg->lun.flags & CTL_LUN_PRIMARY_SC) &&
1164 (msg->lun.flags & CTL_LUN_DISABLED) == 0)
1165 lun->flags |= CTL_LUN_PEER_SC_PRIMARY;
1167 lun->flags &= ~CTL_LUN_PEER_SC_PRIMARY;
1168 if (oflags != lun->flags)
1169 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
1171 /* If peer is primary and we are not -- use data */
1172 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
1173 (lun->flags & CTL_LUN_PEER_SC_PRIMARY)) {
1174 lun->pr_generation = msg->lun.pr_generation;
1175 lun->pr_res_idx = msg->lun.pr_res_idx;
1176 lun->pr_res_type = msg->lun.pr_res_type;
1177 lun->pr_key_count = msg->lun.pr_key_count;
1178 for (k = 0; k < CTL_MAX_INITIATORS; k++)
1179 ctl_clr_prkey(lun, k);
1180 for (k = 0; k < msg->lun.pr_key_count; k++) {
1181 memcpy(&pr_key, &msg->lun.data[i],
1183 ctl_alloc_prkey(lun, pr_key.pr_iid);
1184 ctl_set_prkey(lun, pr_key.pr_iid,
1186 i += sizeof(pr_key);
1190 mtx_unlock(&lun->lun_lock);
1191 CTL_DEBUG_PRINT(("%s: Known LUN %d, peer is %s\n",
1193 (msg->lun.flags & CTL_LUN_PRIMARY_SC) ?
1194 "primary" : "secondary"));
1196 /* If we are primary but peer doesn't know -- notify */
1197 if ((lun->flags & CTL_LUN_PRIMARY_SC) &&
1198 (msg->lun.flags & CTL_LUN_PEER_SC_PRIMARY) == 0)
1199 ctl_isc_announce_lun(lun);
1204 ctl_isc_port_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1206 struct ctl_port *port;
1207 struct ctl_lun *lun;
1210 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1212 CTL_DEBUG_PRINT(("%s: New port %d\n", __func__,
1213 msg->hdr.nexus.targ_port));
1215 port = malloc(sizeof(*port), M_CTL, M_WAITOK | M_ZERO);
1216 port->frontend = &ha_frontend;
1217 port->targ_port = msg->hdr.nexus.targ_port;
1218 port->fe_datamove = ctl_ha_datamove;
1219 port->fe_done = ctl_ha_done;
1220 } else if (port->frontend == &ha_frontend) {
1221 CTL_DEBUG_PRINT(("%s: Updated port %d\n", __func__,
1222 msg->hdr.nexus.targ_port));
1225 printf("%s: Received conflicting HA port %d\n",
1226 __func__, msg->hdr.nexus.targ_port);
1229 port->port_type = msg->port.port_type;
1230 port->physical_port = msg->port.physical_port;
1231 port->virtual_port = msg->port.virtual_port;
1232 port->status = msg->port.status;
1234 free(port->port_name, M_CTL);
1235 port->port_name = strndup(&msg->port.data[i], msg->port.name_len,
1237 i += msg->port.name_len;
1238 if (msg->port.lun_map_len != 0) {
1239 if (port->lun_map == NULL ||
1240 port->lun_map_size * sizeof(uint32_t) <
1241 msg->port.lun_map_len) {
1242 port->lun_map_size = 0;
1243 free(port->lun_map, M_CTL);
1244 port->lun_map = malloc(msg->port.lun_map_len,
1247 memcpy(port->lun_map, &msg->port.data[i], msg->port.lun_map_len);
1248 port->lun_map_size = msg->port.lun_map_len / sizeof(uint32_t);
1249 i += msg->port.lun_map_len;
1251 port->lun_map_size = 0;
1252 free(port->lun_map, M_CTL);
1253 port->lun_map = NULL;
1255 if (msg->port.port_devid_len != 0) {
1256 if (port->port_devid == NULL ||
1257 port->port_devid->len < msg->port.port_devid_len) {
1258 free(port->port_devid, M_CTL);
1259 port->port_devid = malloc(sizeof(struct ctl_devid) +
1260 msg->port.port_devid_len, M_CTL, M_WAITOK);
1262 memcpy(port->port_devid->data, &msg->port.data[i],
1263 msg->port.port_devid_len);
1264 port->port_devid->len = msg->port.port_devid_len;
1265 i += msg->port.port_devid_len;
1267 free(port->port_devid, M_CTL);
1268 port->port_devid = NULL;
1270 if (msg->port.target_devid_len != 0) {
1271 if (port->target_devid == NULL ||
1272 port->target_devid->len < msg->port.target_devid_len) {
1273 free(port->target_devid, M_CTL);
1274 port->target_devid = malloc(sizeof(struct ctl_devid) +
1275 msg->port.target_devid_len, M_CTL, M_WAITOK);
1277 memcpy(port->target_devid->data, &msg->port.data[i],
1278 msg->port.target_devid_len);
1279 port->target_devid->len = msg->port.target_devid_len;
1280 i += msg->port.target_devid_len;
1282 free(port->target_devid, M_CTL);
1283 port->target_devid = NULL;
1285 if (msg->port.init_devid_len != 0) {
1286 if (port->init_devid == NULL ||
1287 port->init_devid->len < msg->port.init_devid_len) {
1288 free(port->init_devid, M_CTL);
1289 port->init_devid = malloc(sizeof(struct ctl_devid) +
1290 msg->port.init_devid_len, M_CTL, M_WAITOK);
1292 memcpy(port->init_devid->data, &msg->port.data[i],
1293 msg->port.init_devid_len);
1294 port->init_devid->len = msg->port.init_devid_len;
1295 i += msg->port.init_devid_len;
1297 free(port->init_devid, M_CTL);
1298 port->init_devid = NULL;
1301 if (ctl_port_register(port) != 0) {
1302 printf("%s: ctl_port_register() failed with error\n",
1306 mtx_lock(&softc->ctl_lock);
1307 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1308 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
1310 mtx_lock(&lun->lun_lock);
1311 ctl_est_ua_all(lun, -1, CTL_UA_INQ_CHANGE);
1312 mtx_unlock(&lun->lun_lock);
1314 mtx_unlock(&softc->ctl_lock);
1318 ctl_isc_iid_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1320 struct ctl_port *port;
1323 port = softc->ctl_ports[msg->hdr.nexus.targ_port];
1325 printf("%s: Received IID for unknown port %d\n",
1326 __func__, msg->hdr.nexus.targ_port);
1329 iid = msg->hdr.nexus.initid;
1330 if (port->wwpn_iid[iid].in_use != 0 &&
1331 msg->iid.in_use == 0)
1332 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
1333 port->wwpn_iid[iid].in_use = msg->iid.in_use;
1334 port->wwpn_iid[iid].wwpn = msg->iid.wwpn;
1335 free(port->wwpn_iid[iid].name, M_CTL);
1336 if (msg->iid.name_len) {
1337 port->wwpn_iid[iid].name = strndup(&msg->iid.data[0],
1338 msg->iid.name_len, M_CTL);
1340 port->wwpn_iid[iid].name = NULL;
1344 ctl_isc_login(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1347 if (msg->login.version != CTL_HA_VERSION) {
1348 printf("CTL HA peers have different versions %d != %d\n",
1349 msg->login.version, CTL_HA_VERSION);
1350 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1353 if (msg->login.ha_mode != softc->ha_mode) {
1354 printf("CTL HA peers have different ha_mode %d != %d\n",
1355 msg->login.ha_mode, softc->ha_mode);
1356 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1359 if (msg->login.ha_id == softc->ha_id) {
1360 printf("CTL HA peers have same ha_id %d\n", msg->login.ha_id);
1361 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1364 if (msg->login.max_luns != ctl_max_luns ||
1365 msg->login.max_ports != ctl_max_ports ||
1366 msg->login.max_init_per_port != CTL_MAX_INIT_PER_PORT) {
1367 printf("CTL HA peers have different limits\n");
1368 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1374 ctl_isc_mode_sync(struct ctl_softc *softc, union ctl_ha_msg *msg, int len)
1376 struct ctl_lun *lun;
1378 uint32_t initidx, targ_lun;
1380 targ_lun = msg->hdr.nexus.targ_mapped_lun;
1381 mtx_lock(&softc->ctl_lock);
1382 if (targ_lun >= ctl_max_luns ||
1383 (lun = softc->ctl_luns[targ_lun]) == NULL) {
1384 mtx_unlock(&softc->ctl_lock);
1387 mtx_lock(&lun->lun_lock);
1388 mtx_unlock(&softc->ctl_lock);
1389 if (lun->flags & CTL_LUN_DISABLED) {
1390 mtx_unlock(&lun->lun_lock);
1393 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
1394 if ((lun->mode_pages.index[i].page_code & SMPH_PC_MASK) ==
1395 msg->mode.page_code &&
1396 lun->mode_pages.index[i].subpage == msg->mode.subpage)
1399 if (i == CTL_NUM_MODE_PAGES) {
1400 mtx_unlock(&lun->lun_lock);
1403 memcpy(lun->mode_pages.index[i].page_data, msg->mode.data,
1404 lun->mode_pages.index[i].page_len);
1405 initidx = ctl_get_initindex(&msg->hdr.nexus);
1407 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
1408 mtx_unlock(&lun->lun_lock);
1412 * ISC (Inter Shelf Communication) event handler. Events from the HA
1413 * subsystem come in here.
1416 ctl_isc_event_handler(ctl_ha_channel channel, ctl_ha_event event, int param)
1418 struct ctl_softc *softc = control_softc;
1420 struct ctl_prio *presio;
1421 ctl_ha_status isc_status;
1423 CTL_DEBUG_PRINT(("CTL: Isc Msg event %d\n", event));
1424 if (event == CTL_HA_EVT_MSG_RECV) {
1425 union ctl_ha_msg *msg, msgbuf;
1427 if (param > sizeof(msgbuf))
1428 msg = malloc(param, M_CTL, M_WAITOK);
1431 isc_status = ctl_ha_msg_recv(CTL_HA_CHAN_CTL, msg, param,
1433 if (isc_status != CTL_HA_STATUS_SUCCESS) {
1434 printf("%s: Error receiving message: %d\n",
1435 __func__, isc_status);
1441 CTL_DEBUG_PRINT(("CTL: msg_type %d\n", msg->msg_type));
1442 switch (msg->hdr.msg_type) {
1443 case CTL_MSG_SERIALIZE:
1444 io = ctl_alloc_io(softc->othersc_pool);
1446 // populate ctsio from msg
1447 io->io_hdr.io_type = CTL_IO_SCSI;
1448 io->io_hdr.msg_type = CTL_MSG_SERIALIZE;
1449 io->io_hdr.remote_io = msg->hdr.original_sc;
1450 io->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC |
1453 * If we're in serialization-only mode, we don't
1454 * want to go through full done processing. Thus
1457 * XXX KDM add another flag that is more specific.
1459 if (softc->ha_mode != CTL_HA_MODE_XFER)
1460 io->io_hdr.flags |= CTL_FLAG_INT_COPY;
1461 io->io_hdr.nexus = msg->hdr.nexus;
1462 io->scsiio.tag_num = msg->scsi.tag_num;
1463 io->scsiio.tag_type = msg->scsi.tag_type;
1465 io->io_hdr.start_time = time_uptime;
1466 getbinuptime(&io->io_hdr.start_bt);
1467 #endif /* CTL_TIME_IO */
1468 io->scsiio.cdb_len = msg->scsi.cdb_len;
1469 memcpy(io->scsiio.cdb, msg->scsi.cdb,
1471 if (softc->ha_mode == CTL_HA_MODE_XFER) {
1472 const struct ctl_cmd_entry *entry;
1474 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
1475 io->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
1477 entry->flags & CTL_FLAG_DATA_MASK;
1479 ctl_enqueue_isc(io);
1482 /* Performed on the Originating SC, XFER mode only */
1483 case CTL_MSG_DATAMOVE: {
1484 struct ctl_sg_entry *sgl;
1487 io = msg->hdr.original_sc;
1489 printf("%s: original_sc == NULL!\n", __func__);
1490 /* XXX KDM do something here */
1493 io->io_hdr.msg_type = CTL_MSG_DATAMOVE;
1494 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1496 * Keep track of this, we need to send it back over
1497 * when the datamove is complete.
1499 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1500 if (msg->hdr.status == CTL_SUCCESS)
1501 io->io_hdr.status = msg->hdr.status;
1503 if (msg->dt.sg_sequence == 0) {
1505 getbinuptime(&io->io_hdr.dma_start_bt);
1507 i = msg->dt.kern_sg_entries +
1508 msg->dt.kern_data_len /
1509 CTL_HA_DATAMOVE_SEGMENT + 1;
1510 sgl = malloc(sizeof(*sgl) * i, M_CTL,
1513 CTL_LSGL(io) = &sgl[msg->dt.kern_sg_entries];
1515 io->scsiio.kern_data_ptr = (uint8_t *)sgl;
1517 io->scsiio.kern_sg_entries =
1518 msg->dt.kern_sg_entries;
1519 io->scsiio.rem_sg_entries =
1520 msg->dt.kern_sg_entries;
1521 io->scsiio.kern_data_len =
1522 msg->dt.kern_data_len;
1523 io->scsiio.kern_total_len =
1524 msg->dt.kern_total_len;
1525 io->scsiio.kern_data_resid =
1526 msg->dt.kern_data_resid;
1527 io->scsiio.kern_rel_offset =
1528 msg->dt.kern_rel_offset;
1529 io->io_hdr.flags &= ~CTL_FLAG_BUS_ADDR;
1530 io->io_hdr.flags |= msg->dt.flags &
1533 sgl = (struct ctl_sg_entry *)
1534 io->scsiio.kern_data_ptr;
1536 for (i = msg->dt.sent_sg_entries, j = 0;
1537 i < (msg->dt.sent_sg_entries +
1538 msg->dt.cur_sg_entries); i++, j++) {
1539 sgl[i].addr = msg->dt.sg_list[j].addr;
1540 sgl[i].len = msg->dt.sg_list[j].len;
1544 * If this is the last piece of the I/O, we've got
1545 * the full S/G list. Queue processing in the thread.
1546 * Otherwise wait for the next piece.
1548 if (msg->dt.sg_last != 0)
1549 ctl_enqueue_isc(io);
1552 /* Performed on the Serializing (primary) SC, XFER mode only */
1553 case CTL_MSG_DATAMOVE_DONE: {
1554 if (msg->hdr.serializing_sc == NULL) {
1555 printf("%s: serializing_sc == NULL!\n",
1557 /* XXX KDM now what? */
1561 * We grab the sense information here in case
1562 * there was a failure, so we can return status
1563 * back to the initiator.
1565 io = msg->hdr.serializing_sc;
1566 io->io_hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
1567 io->io_hdr.flags &= ~CTL_FLAG_DMA_INPROG;
1568 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1569 io->io_hdr.port_status = msg->scsi.port_status;
1570 io->scsiio.kern_data_resid = msg->scsi.kern_data_resid;
1571 if (msg->hdr.status != CTL_STATUS_NONE) {
1572 io->io_hdr.status = msg->hdr.status;
1573 io->scsiio.scsi_status = msg->scsi.scsi_status;
1574 io->scsiio.sense_len = msg->scsi.sense_len;
1575 memcpy(&io->scsiio.sense_data,
1576 &msg->scsi.sense_data,
1577 msg->scsi.sense_len);
1578 if (msg->hdr.status == CTL_SUCCESS)
1579 io->io_hdr.flags |= CTL_FLAG_STATUS_SENT;
1581 ctl_enqueue_isc(io);
1585 /* Preformed on Originating SC, SER_ONLY mode */
1587 io = msg->hdr.original_sc;
1589 printf("%s: original_sc == NULL!\n",
1593 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1594 io->io_hdr.msg_type = CTL_MSG_R2R;
1595 io->io_hdr.remote_io = msg->hdr.serializing_sc;
1596 ctl_enqueue_isc(io);
1600 * Performed on Serializing(i.e. primary SC) SC in SER_ONLY
1602 * Performed on the Originating (i.e. secondary) SC in XFER
1605 case CTL_MSG_FINISH_IO:
1606 if (softc->ha_mode == CTL_HA_MODE_XFER)
1607 ctl_isc_handler_finish_xfer(softc, msg);
1609 ctl_isc_handler_finish_ser_only(softc, msg);
1612 /* Preformed on Originating SC */
1613 case CTL_MSG_BAD_JUJU:
1614 io = msg->hdr.original_sc;
1616 printf("%s: Bad JUJU!, original_sc is NULL!\n",
1620 ctl_copy_sense_data(msg, io);
1622 * IO should have already been cleaned up on other
1623 * SC so clear this flag so we won't send a message
1624 * back to finish the IO there.
1626 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
1627 io->io_hdr.flags |= CTL_FLAG_IO_ACTIVE;
1629 /* io = msg->hdr.serializing_sc; */
1630 io->io_hdr.msg_type = CTL_MSG_BAD_JUJU;
1631 ctl_enqueue_isc(io);
1634 /* Handle resets sent from the other side */
1635 case CTL_MSG_MANAGE_TASKS: {
1636 struct ctl_taskio *taskio;
1637 taskio = (struct ctl_taskio *)ctl_alloc_io(
1638 softc->othersc_pool);
1639 ctl_zero_io((union ctl_io *)taskio);
1640 taskio->io_hdr.io_type = CTL_IO_TASK;
1641 taskio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1642 taskio->io_hdr.nexus = msg->hdr.nexus;
1643 taskio->task_action = msg->task.task_action;
1644 taskio->tag_num = msg->task.tag_num;
1645 taskio->tag_type = msg->task.tag_type;
1647 taskio->io_hdr.start_time = time_uptime;
1648 getbinuptime(&taskio->io_hdr.start_bt);
1649 #endif /* CTL_TIME_IO */
1650 ctl_run_task((union ctl_io *)taskio);
1653 /* Persistent Reserve action which needs attention */
1654 case CTL_MSG_PERS_ACTION:
1655 presio = (struct ctl_prio *)ctl_alloc_io(
1656 softc->othersc_pool);
1657 ctl_zero_io((union ctl_io *)presio);
1658 presio->io_hdr.msg_type = CTL_MSG_PERS_ACTION;
1659 presio->io_hdr.flags |= CTL_FLAG_FROM_OTHER_SC;
1660 presio->io_hdr.nexus = msg->hdr.nexus;
1661 presio->pr_msg = msg->pr;
1662 ctl_enqueue_isc((union ctl_io *)presio);
1665 ctl_isc_ua(softc, msg, param);
1667 case CTL_MSG_PORT_SYNC:
1668 ctl_isc_port_sync(softc, msg, param);
1670 case CTL_MSG_LUN_SYNC:
1671 ctl_isc_lun_sync(softc, msg, param);
1673 case CTL_MSG_IID_SYNC:
1674 ctl_isc_iid_sync(softc, msg, param);
1677 ctl_isc_login(softc, msg, param);
1679 case CTL_MSG_MODE_SYNC:
1680 ctl_isc_mode_sync(softc, msg, param);
1683 printf("Received HA message of unknown type %d\n",
1685 ctl_ha_msg_abort(CTL_HA_CHAN_CTL);
1690 } else if (event == CTL_HA_EVT_LINK_CHANGE) {
1691 printf("CTL: HA link status changed from %d to %d\n",
1692 softc->ha_link, param);
1693 if (param == softc->ha_link)
1695 if (softc->ha_link == CTL_HA_LINK_ONLINE) {
1696 softc->ha_link = param;
1697 ctl_isc_ha_link_down(softc);
1699 softc->ha_link = param;
1700 if (softc->ha_link == CTL_HA_LINK_ONLINE)
1701 ctl_isc_ha_link_up(softc);
1705 printf("ctl_isc_event_handler: Unknown event %d\n", event);
1711 ctl_copy_sense_data(union ctl_ha_msg *src, union ctl_io *dest)
1714 memcpy(&dest->scsiio.sense_data, &src->scsi.sense_data,
1715 src->scsi.sense_len);
1716 dest->scsiio.scsi_status = src->scsi.scsi_status;
1717 dest->scsiio.sense_len = src->scsi.sense_len;
1718 dest->io_hdr.status = src->hdr.status;
1722 ctl_copy_sense_data_back(union ctl_io *src, union ctl_ha_msg *dest)
1725 memcpy(&dest->scsi.sense_data, &src->scsiio.sense_data,
1726 src->scsiio.sense_len);
1727 dest->scsi.scsi_status = src->scsiio.scsi_status;
1728 dest->scsi.sense_len = src->scsiio.sense_len;
1729 dest->hdr.status = src->io_hdr.status;
1733 ctl_est_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1735 struct ctl_softc *softc = lun->ctl_softc;
1738 if (initidx < softc->init_min || initidx >= softc->init_max)
1740 mtx_assert(&lun->lun_lock, MA_OWNED);
1741 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1744 pu[initidx % CTL_MAX_INIT_PER_PORT] |= ua;
1748 ctl_est_ua_port(struct ctl_lun *lun, int port, uint32_t except, ctl_ua_type ua)
1752 mtx_assert(&lun->lun_lock, MA_OWNED);
1753 if (lun->pending_ua[port] == NULL)
1755 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
1756 if (port * CTL_MAX_INIT_PER_PORT + i == except)
1758 lun->pending_ua[port][i] |= ua;
1763 ctl_est_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1765 struct ctl_softc *softc = lun->ctl_softc;
1768 mtx_assert(&lun->lun_lock, MA_OWNED);
1769 for (i = softc->port_min; i < softc->port_max; i++)
1770 ctl_est_ua_port(lun, i, except, ua);
1774 ctl_clr_ua(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua)
1776 struct ctl_softc *softc = lun->ctl_softc;
1779 if (initidx < softc->init_min || initidx >= softc->init_max)
1781 mtx_assert(&lun->lun_lock, MA_OWNED);
1782 pu = lun->pending_ua[initidx / CTL_MAX_INIT_PER_PORT];
1785 pu[initidx % CTL_MAX_INIT_PER_PORT] &= ~ua;
1789 ctl_clr_ua_all(struct ctl_lun *lun, uint32_t except, ctl_ua_type ua)
1791 struct ctl_softc *softc = lun->ctl_softc;
1794 mtx_assert(&lun->lun_lock, MA_OWNED);
1795 for (i = softc->port_min; i < softc->port_max; i++) {
1796 if (lun->pending_ua[i] == NULL)
1798 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
1799 if (i * CTL_MAX_INIT_PER_PORT + j == except)
1801 lun->pending_ua[i][j] &= ~ua;
1807 ctl_clr_ua_allluns(struct ctl_softc *ctl_softc, uint32_t initidx,
1808 ctl_ua_type ua_type)
1810 struct ctl_lun *lun;
1812 mtx_assert(&ctl_softc->ctl_lock, MA_OWNED);
1813 STAILQ_FOREACH(lun, &ctl_softc->lun_list, links) {
1814 mtx_lock(&lun->lun_lock);
1815 ctl_clr_ua(lun, initidx, ua_type);
1816 mtx_unlock(&lun->lun_lock);
1821 ctl_ha_role_sysctl(SYSCTL_HANDLER_ARGS)
1823 struct ctl_softc *softc = (struct ctl_softc *)arg1;
1824 struct ctl_lun *lun;
1825 struct ctl_lun_req ireq;
1828 value = (softc->flags & CTL_FLAG_ACTIVE_SHELF) ? 0 : 1;
1829 error = sysctl_handle_int(oidp, &value, 0, req);
1830 if ((error != 0) || (req->newptr == NULL))
1833 mtx_lock(&softc->ctl_lock);
1835 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1837 softc->flags &= ~CTL_FLAG_ACTIVE_SHELF;
1838 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1839 mtx_unlock(&softc->ctl_lock);
1840 bzero(&ireq, sizeof(ireq));
1841 ireq.reqtype = CTL_LUNREQ_MODIFY;
1842 ireq.reqdata.modify.lun_id = lun->lun;
1843 lun->backend->ioctl(NULL, CTL_LUN_REQ, (caddr_t)&ireq, 0,
1845 if (ireq.status != CTL_LUN_OK) {
1846 printf("%s: CTL_LUNREQ_MODIFY returned %d '%s'\n",
1847 __func__, ireq.status, ireq.error_str);
1849 mtx_lock(&softc->ctl_lock);
1851 mtx_unlock(&softc->ctl_lock);
1858 struct make_dev_args args;
1859 struct ctl_softc *softc;
1862 softc = control_softc = malloc(sizeof(*control_softc), M_DEVBUF,
1865 make_dev_args_init(&args);
1866 args.mda_devsw = &ctl_cdevsw;
1867 args.mda_uid = UID_ROOT;
1868 args.mda_gid = GID_OPERATOR;
1869 args.mda_mode = 0600;
1870 args.mda_si_drv1 = softc;
1871 args.mda_si_drv2 = NULL;
1872 error = make_dev_s(&args, &softc->dev, "cam/ctl");
1874 free(softc, M_DEVBUF);
1875 control_softc = NULL;
1879 sysctl_ctx_init(&softc->sysctl_ctx);
1880 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1881 SYSCTL_STATIC_CHILDREN(_kern_cam), OID_AUTO, "ctl",
1882 CTLFLAG_RD, 0, "CAM Target Layer");
1884 if (softc->sysctl_tree == NULL) {
1885 printf("%s: unable to allocate sysctl tree\n", __func__);
1886 destroy_dev(softc->dev);
1887 free(softc, M_DEVBUF);
1888 control_softc = NULL;
1892 mtx_init(&softc->ctl_lock, "CTL mutex", NULL, MTX_DEF);
1893 softc->io_zone = uma_zcreate("CTL IO", sizeof(union ctl_io),
1894 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1897 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1898 OID_AUTO, "ha_mode", CTLFLAG_RDTUN, (int *)&softc->ha_mode, 0,
1899 "HA mode (0 - act/stby, 1 - serialize only, 2 - xfer)");
1901 if (ctl_max_luns <= 0 || powerof2(ctl_max_luns) == 0) {
1902 printf("Bad value %d for kern.cam.ctl.max_luns, must be a power of two, using %d\n",
1903 ctl_max_luns, CTL_DEFAULT_MAX_LUNS);
1904 ctl_max_luns = CTL_DEFAULT_MAX_LUNS;
1906 softc->ctl_luns = malloc(sizeof(struct ctl_lun *) * ctl_max_luns,
1907 M_DEVBUF, M_WAITOK | M_ZERO);
1908 softc->ctl_lun_mask = malloc(sizeof(uint32_t) *
1909 ((ctl_max_luns + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1910 if (ctl_max_ports <= 0 || powerof2(ctl_max_ports) == 0) {
1911 printf("Bad value %d for kern.cam.ctl.max_ports, must be a power of two, using %d\n",
1912 ctl_max_ports, CTL_DEFAULT_MAX_PORTS);
1913 ctl_max_ports = CTL_DEFAULT_MAX_PORTS;
1915 softc->ctl_port_mask = malloc(sizeof(uint32_t) *
1916 ((ctl_max_ports + 31) / 32), M_DEVBUF, M_WAITOK | M_ZERO);
1917 softc->ctl_ports = malloc(sizeof(struct ctl_port *) * ctl_max_ports,
1918 M_DEVBUF, M_WAITOK | M_ZERO);
1922 * In Copan's HA scheme, the "master" and "slave" roles are
1923 * figured out through the slot the controller is in. Although it
1924 * is an active/active system, someone has to be in charge.
1926 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1927 OID_AUTO, "ha_id", CTLFLAG_RDTUN, &softc->ha_id, 0,
1928 "HA head ID (0 - no HA)");
1929 if (softc->ha_id == 0 || softc->ha_id > NUM_HA_SHELVES) {
1930 softc->flags |= CTL_FLAG_ACTIVE_SHELF;
1931 softc->is_single = 1;
1932 softc->port_cnt = ctl_max_ports;
1933 softc->port_min = 0;
1935 softc->port_cnt = ctl_max_ports / NUM_HA_SHELVES;
1936 softc->port_min = (softc->ha_id - 1) * softc->port_cnt;
1938 softc->port_max = softc->port_min + softc->port_cnt;
1939 softc->init_min = softc->port_min * CTL_MAX_INIT_PER_PORT;
1940 softc->init_max = softc->port_max * CTL_MAX_INIT_PER_PORT;
1942 SYSCTL_ADD_INT(&softc->sysctl_ctx, SYSCTL_CHILDREN(softc->sysctl_tree),
1943 OID_AUTO, "ha_link", CTLFLAG_RD, (int *)&softc->ha_link, 0,
1944 "HA link state (0 - offline, 1 - unknown, 2 - online)");
1946 STAILQ_INIT(&softc->lun_list);
1947 STAILQ_INIT(&softc->pending_lun_queue);
1948 STAILQ_INIT(&softc->fe_list);
1949 STAILQ_INIT(&softc->port_list);
1950 STAILQ_INIT(&softc->be_list);
1951 ctl_tpc_init(softc);
1953 if (worker_threads <= 0)
1954 worker_threads = max(1, mp_ncpus / 4);
1955 if (worker_threads > CTL_MAX_THREADS)
1956 worker_threads = CTL_MAX_THREADS;
1958 for (i = 0; i < worker_threads; i++) {
1959 struct ctl_thread *thr = &softc->threads[i];
1961 mtx_init(&thr->queue_lock, "CTL queue mutex", NULL, MTX_DEF);
1962 thr->ctl_softc = softc;
1963 STAILQ_INIT(&thr->incoming_queue);
1964 STAILQ_INIT(&thr->rtr_queue);
1965 STAILQ_INIT(&thr->done_queue);
1966 STAILQ_INIT(&thr->isc_queue);
1968 error = kproc_kthread_add(ctl_work_thread, thr,
1969 &softc->ctl_proc, &thr->thread, 0, 0, "ctl", "work%d", i);
1971 printf("error creating CTL work thread!\n");
1975 error = kproc_kthread_add(ctl_lun_thread, softc,
1976 &softc->ctl_proc, &softc->lun_thread, 0, 0, "ctl", "lun");
1978 printf("error creating CTL lun thread!\n");
1981 error = kproc_kthread_add(ctl_thresh_thread, softc,
1982 &softc->ctl_proc, &softc->thresh_thread, 0, 0, "ctl", "thresh");
1984 printf("error creating CTL threshold thread!\n");
1988 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1989 OID_AUTO, "ha_role", CTLTYPE_INT | CTLFLAG_RWTUN,
1990 softc, 0, ctl_ha_role_sysctl, "I", "HA role for this head");
1992 if (softc->is_single == 0) {
1993 if (ctl_frontend_register(&ha_frontend) != 0)
1994 softc->is_single = 1;
2002 struct ctl_softc *softc = control_softc;
2005 if (softc->is_single == 0)
2006 ctl_frontend_deregister(&ha_frontend);
2008 destroy_dev(softc->dev);
2010 /* Shutdown CTL threads. */
2011 softc->shutdown = 1;
2012 for (i = 0; i < worker_threads; i++) {
2013 struct ctl_thread *thr = &softc->threads[i];
2014 while (thr->thread != NULL) {
2016 if (thr->thread != NULL)
2017 pause("CTL thr shutdown", 1);
2019 mtx_destroy(&thr->queue_lock);
2021 while (softc->lun_thread != NULL) {
2022 wakeup(&softc->pending_lun_queue);
2023 if (softc->lun_thread != NULL)
2024 pause("CTL thr shutdown", 1);
2026 while (softc->thresh_thread != NULL) {
2027 wakeup(softc->thresh_thread);
2028 if (softc->thresh_thread != NULL)
2029 pause("CTL thr shutdown", 1);
2032 ctl_tpc_shutdown(softc);
2033 uma_zdestroy(softc->io_zone);
2034 mtx_destroy(&softc->ctl_lock);
2036 free(softc->ctl_luns, M_DEVBUF);
2037 free(softc->ctl_lun_mask, M_DEVBUF);
2038 free(softc->ctl_port_mask, M_DEVBUF);
2039 free(softc->ctl_ports, M_DEVBUF);
2041 sysctl_ctx_free(&softc->sysctl_ctx);
2043 free(softc, M_DEVBUF);
2044 control_softc = NULL;
2049 ctl_module_event_handler(module_t mod, int what, void *arg)
2054 return (ctl_init());
2056 return (ctl_shutdown());
2058 return (EOPNOTSUPP);
2063 * XXX KDM should we do some access checks here? Bump a reference count to
2064 * prevent a CTL module from being unloaded while someone has it open?
2067 ctl_open(struct cdev *dev, int flags, int fmt, struct thread *td)
2073 ctl_close(struct cdev *dev, int flags, int fmt, struct thread *td)
2079 * Remove an initiator by port number and initiator ID.
2080 * Returns 0 for success, -1 for failure.
2083 ctl_remove_initiator(struct ctl_port *port, int iid)
2085 struct ctl_softc *softc = port->ctl_softc;
2088 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2090 if (iid > CTL_MAX_INIT_PER_PORT) {
2091 printf("%s: initiator ID %u > maximun %u!\n",
2092 __func__, iid, CTL_MAX_INIT_PER_PORT);
2096 mtx_lock(&softc->ctl_lock);
2097 last = (--port->wwpn_iid[iid].in_use == 0);
2098 port->wwpn_iid[iid].last_use = time_uptime;
2099 mtx_unlock(&softc->ctl_lock);
2101 ctl_i_t_nexus_loss(softc, iid, CTL_UA_POWERON);
2102 ctl_isc_announce_iid(port, iid);
2108 * Add an initiator to the initiator map.
2109 * Returns iid for success, < 0 for failure.
2112 ctl_add_initiator(struct ctl_port *port, int iid, uint64_t wwpn, char *name)
2114 struct ctl_softc *softc = port->ctl_softc;
2118 mtx_assert(&softc->ctl_lock, MA_NOTOWNED);
2120 if (iid >= CTL_MAX_INIT_PER_PORT) {
2121 printf("%s: WWPN %#jx initiator ID %u > maximum %u!\n",
2122 __func__, wwpn, iid, CTL_MAX_INIT_PER_PORT);
2127 mtx_lock(&softc->ctl_lock);
2129 if (iid < 0 && (wwpn != 0 || name != NULL)) {
2130 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2131 if (wwpn != 0 && wwpn == port->wwpn_iid[i].wwpn) {
2135 if (name != NULL && port->wwpn_iid[i].name != NULL &&
2136 strcmp(name, port->wwpn_iid[i].name) == 0) {
2144 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2145 if (port->wwpn_iid[i].in_use == 0 &&
2146 port->wwpn_iid[i].wwpn == 0 &&
2147 port->wwpn_iid[i].name == NULL) {
2156 best_time = INT32_MAX;
2157 for (i = 0; i < CTL_MAX_INIT_PER_PORT; i++) {
2158 if (port->wwpn_iid[i].in_use == 0) {
2159 if (port->wwpn_iid[i].last_use < best_time) {
2161 best_time = port->wwpn_iid[i].last_use;
2169 mtx_unlock(&softc->ctl_lock);
2174 if (port->wwpn_iid[iid].in_use > 0 && (wwpn != 0 || name != NULL)) {
2176 * This is not an error yet.
2178 if (wwpn != 0 && wwpn == port->wwpn_iid[iid].wwpn) {
2180 printf("%s: port %d iid %u WWPN %#jx arrived"
2181 " again\n", __func__, port->targ_port,
2182 iid, (uintmax_t)wwpn);
2186 if (name != NULL && port->wwpn_iid[iid].name != NULL &&
2187 strcmp(name, port->wwpn_iid[iid].name) == 0) {
2189 printf("%s: port %d iid %u name '%s' arrived"
2190 " again\n", __func__, port->targ_port,
2197 * This is an error, but what do we do about it? The
2198 * driver is telling us we have a new WWPN for this
2199 * initiator ID, so we pretty much need to use it.
2201 printf("%s: port %d iid %u WWPN %#jx '%s' arrived,"
2202 " but WWPN %#jx '%s' is still at that address\n",
2203 __func__, port->targ_port, iid, wwpn, name,
2204 (uintmax_t)port->wwpn_iid[iid].wwpn,
2205 port->wwpn_iid[iid].name);
2208 free(port->wwpn_iid[iid].name, M_CTL);
2209 port->wwpn_iid[iid].name = name;
2210 port->wwpn_iid[iid].wwpn = wwpn;
2211 port->wwpn_iid[iid].in_use++;
2212 mtx_unlock(&softc->ctl_lock);
2213 ctl_isc_announce_iid(port, iid);
2219 ctl_create_iid(struct ctl_port *port, int iid, uint8_t *buf)
2223 switch (port->port_type) {
2226 struct scsi_transportid_fcp *id =
2227 (struct scsi_transportid_fcp *)buf;
2228 if (port->wwpn_iid[iid].wwpn == 0)
2230 memset(id, 0, sizeof(*id));
2231 id->format_protocol = SCSI_PROTO_FC;
2232 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->n_port_name);
2233 return (sizeof(*id));
2235 case CTL_PORT_ISCSI:
2237 struct scsi_transportid_iscsi_port *id =
2238 (struct scsi_transportid_iscsi_port *)buf;
2239 if (port->wwpn_iid[iid].name == NULL)
2242 id->format_protocol = SCSI_TRN_ISCSI_FORMAT_PORT |
2244 len = strlcpy(id->iscsi_name, port->wwpn_iid[iid].name, 252) + 1;
2245 len = roundup2(min(len, 252), 4);
2246 scsi_ulto2b(len, id->additional_length);
2247 return (sizeof(*id) + len);
2251 struct scsi_transportid_sas *id =
2252 (struct scsi_transportid_sas *)buf;
2253 if (port->wwpn_iid[iid].wwpn == 0)
2255 memset(id, 0, sizeof(*id));
2256 id->format_protocol = SCSI_PROTO_SAS;
2257 scsi_u64to8b(port->wwpn_iid[iid].wwpn, id->sas_address);
2258 return (sizeof(*id));
2262 struct scsi_transportid_spi *id =
2263 (struct scsi_transportid_spi *)buf;
2264 memset(id, 0, sizeof(*id));
2265 id->format_protocol = SCSI_PROTO_SPI;
2266 scsi_ulto2b(iid, id->scsi_addr);
2267 scsi_ulto2b(port->targ_port, id->rel_trgt_port_id);
2268 return (sizeof(*id));
2274 * Serialize a command that went down the "wrong" side, and so was sent to
2275 * this controller for execution. The logic is a little different than the
2276 * standard case in ctl_scsiio_precheck(). Errors in this case need to get
2277 * sent back to the other side, but in the success case, we execute the
2278 * command on this side (XFER mode) or tell the other side to execute it
2282 ctl_serialize_other_sc_cmd(struct ctl_scsiio *ctsio)
2284 struct ctl_softc *softc = CTL_SOFTC(ctsio);
2285 struct ctl_port *port = CTL_PORT(ctsio);
2286 union ctl_ha_msg msg_info;
2287 struct ctl_lun *lun;
2288 const struct ctl_cmd_entry *entry;
2292 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
2294 /* Make sure that we know about this port. */
2295 if (port == NULL || (port->status & CTL_PORT_STATUS_ONLINE) == 0) {
2296 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2301 /* Make sure that we know about this LUN. */
2302 mtx_lock(&softc->ctl_lock);
2303 if (targ_lun >= ctl_max_luns ||
2304 (lun = softc->ctl_luns[targ_lun]) == NULL) {
2305 mtx_unlock(&softc->ctl_lock);
2308 * The other node would not send this request to us unless
2309 * received announce that we are primary node for this LUN.
2310 * If this LUN does not exist now, it is probably result of
2311 * a race, so respond to initiator in the most opaque way.
2313 ctl_set_busy(ctsio);
2316 mtx_lock(&lun->lun_lock);
2317 mtx_unlock(&softc->ctl_lock);
2320 * If the LUN is invalid, pretend that it doesn't exist.
2321 * It will go away as soon as all pending I/Os completed.
2323 if (lun->flags & CTL_LUN_DISABLED) {
2324 mtx_unlock(&lun->lun_lock);
2325 ctl_set_busy(ctsio);
2329 entry = ctl_get_cmd_entry(ctsio, NULL);
2330 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
2331 mtx_unlock(&lun->lun_lock);
2335 CTL_LUN(ctsio) = lun;
2336 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
2339 * Every I/O goes into the OOA queue for a
2340 * particular LUN, and stays there until completion.
2343 if (TAILQ_EMPTY(&lun->ooa_queue))
2344 lun->idle_time += getsbinuptime() - lun->last_busy;
2346 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2348 bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links);
2349 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
2350 case CTL_ACTION_BLOCK:
2351 ctsio->io_hdr.blocker = bio;
2352 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
2354 mtx_unlock(&lun->lun_lock);
2356 case CTL_ACTION_PASS:
2357 case CTL_ACTION_SKIP:
2358 if (softc->ha_mode == CTL_HA_MODE_XFER) {
2359 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
2360 ctl_enqueue_rtr((union ctl_io *)ctsio);
2361 mtx_unlock(&lun->lun_lock);
2363 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
2364 mtx_unlock(&lun->lun_lock);
2366 /* send msg back to other side */
2367 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2368 msg_info.hdr.serializing_sc = (union ctl_io *)ctsio;
2369 msg_info.hdr.msg_type = CTL_MSG_R2R;
2370 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2371 sizeof(msg_info.hdr), M_WAITOK);
2374 case CTL_ACTION_OVERLAP:
2375 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2376 mtx_unlock(&lun->lun_lock);
2377 ctl_set_overlapped_cmd(ctsio);
2379 case CTL_ACTION_OVERLAP_TAG:
2380 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2381 mtx_unlock(&lun->lun_lock);
2382 ctl_set_overlapped_tag(ctsio, ctsio->tag_num);
2384 case CTL_ACTION_ERROR:
2386 TAILQ_REMOVE(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
2387 mtx_unlock(&lun->lun_lock);
2389 ctl_set_internal_failure(ctsio, /*sks_valid*/ 0,
2392 ctl_copy_sense_data_back((union ctl_io *)ctsio, &msg_info);
2393 msg_info.hdr.original_sc = ctsio->io_hdr.remote_io;
2394 msg_info.hdr.serializing_sc = NULL;
2395 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
2396 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
2397 sizeof(msg_info.scsi), M_WAITOK);
2398 ctl_free_io((union ctl_io *)ctsio);
2404 * Returns 0 for success, errno for failure.
2407 ctl_ioctl_fill_ooa(struct ctl_lun *lun, uint32_t *cur_fill_num,
2408 struct ctl_ooa *ooa_hdr, struct ctl_ooa_entry *kern_entries)
2412 mtx_lock(&lun->lun_lock);
2413 for (io = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); (io != NULL);
2414 (*cur_fill_num)++, io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr,
2416 struct ctl_ooa_entry *entry;
2419 * If we've got more than we can fit, just count the
2420 * remaining entries.
2422 if (*cur_fill_num >= ooa_hdr->alloc_num)
2425 entry = &kern_entries[*cur_fill_num];
2427 entry->tag_num = io->scsiio.tag_num;
2428 entry->lun_num = lun->lun;
2430 entry->start_bt = io->io_hdr.start_bt;
2432 bcopy(io->scsiio.cdb, entry->cdb, io->scsiio.cdb_len);
2433 entry->cdb_len = io->scsiio.cdb_len;
2434 if (io->io_hdr.blocker != NULL)
2435 entry->cmd_flags |= CTL_OOACMD_FLAG_BLOCKED;
2437 if (io->io_hdr.flags & CTL_FLAG_DMA_INPROG)
2438 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA;
2440 if (io->io_hdr.flags & CTL_FLAG_ABORT)
2441 entry->cmd_flags |= CTL_OOACMD_FLAG_ABORT;
2443 if (io->io_hdr.flags & CTL_FLAG_IS_WAS_ON_RTR)
2444 entry->cmd_flags |= CTL_OOACMD_FLAG_RTR;
2446 if (io->io_hdr.flags & CTL_FLAG_DMA_QUEUED)
2447 entry->cmd_flags |= CTL_OOACMD_FLAG_DMA_QUEUED;
2449 mtx_unlock(&lun->lun_lock);
2453 * Escape characters that are illegal or not recommended in XML.
2456 ctl_sbuf_printf_esc(struct sbuf *sb, char *str, int size)
2458 char *end = str + size;
2463 for (; *str && str < end; str++) {
2466 retval = sbuf_printf(sb, "&");
2469 retval = sbuf_printf(sb, ">");
2472 retval = sbuf_printf(sb, "<");
2475 retval = sbuf_putc(sb, *str);
2488 ctl_id_sbuf(struct ctl_devid *id, struct sbuf *sb)
2490 struct scsi_vpd_id_descriptor *desc;
2493 if (id == NULL || id->len < 4)
2495 desc = (struct scsi_vpd_id_descriptor *)id->data;
2496 switch (desc->id_type & SVPD_ID_TYPE_MASK) {
2497 case SVPD_ID_TYPE_T10:
2498 sbuf_printf(sb, "t10.");
2500 case SVPD_ID_TYPE_EUI64:
2501 sbuf_printf(sb, "eui.");
2503 case SVPD_ID_TYPE_NAA:
2504 sbuf_printf(sb, "naa.");
2506 case SVPD_ID_TYPE_SCSI_NAME:
2509 switch (desc->proto_codeset & SVPD_ID_CODESET_MASK) {
2510 case SVPD_ID_CODESET_BINARY:
2511 for (i = 0; i < desc->length; i++)
2512 sbuf_printf(sb, "%02x", desc->identifier[i]);
2514 case SVPD_ID_CODESET_ASCII:
2515 sbuf_printf(sb, "%.*s", (int)desc->length,
2516 (char *)desc->identifier);
2518 case SVPD_ID_CODESET_UTF8:
2519 sbuf_printf(sb, "%s", (char *)desc->identifier);
2525 ctl_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag,
2528 struct ctl_softc *softc = dev->si_drv1;
2529 struct ctl_port *port;
2530 struct ctl_lun *lun;
2537 retval = ctl_ioctl_io(dev, cmd, addr, flag, td);
2539 case CTL_ENABLE_PORT:
2540 case CTL_DISABLE_PORT:
2541 case CTL_SET_PORT_WWNS: {
2542 struct ctl_port *port;
2543 struct ctl_port_entry *entry;
2545 entry = (struct ctl_port_entry *)addr;
2547 mtx_lock(&softc->ctl_lock);
2548 STAILQ_FOREACH(port, &softc->port_list, links) {
2551 if (port->targ_port < softc->port_min ||
2552 port->targ_port >= softc->port_max)
2557 if ((entry->port_type == CTL_PORT_NONE)
2558 && (entry->targ_port == port->targ_port)) {
2560 * If the user only wants to enable or
2561 * disable or set WWNs on a specific port,
2562 * do the operation and we're done.
2566 } else if (entry->port_type & port->port_type) {
2568 * Compare the user's type mask with the
2569 * particular frontend type to see if we
2576 * Make sure the user isn't trying to set
2577 * WWNs on multiple ports at the same time.
2579 if (cmd == CTL_SET_PORT_WWNS) {
2580 printf("%s: Can't set WWNs on "
2581 "multiple ports\n", __func__);
2590 * XXX KDM we have to drop the lock here, because
2591 * the online/offline operations can potentially
2592 * block. We need to reference count the frontends
2593 * so they can't go away,
2595 if (cmd == CTL_ENABLE_PORT) {
2596 mtx_unlock(&softc->ctl_lock);
2597 ctl_port_online(port);
2598 mtx_lock(&softc->ctl_lock);
2599 } else if (cmd == CTL_DISABLE_PORT) {
2600 mtx_unlock(&softc->ctl_lock);
2601 ctl_port_offline(port);
2602 mtx_lock(&softc->ctl_lock);
2603 } else if (cmd == CTL_SET_PORT_WWNS) {
2604 ctl_port_set_wwns(port,
2605 (entry->flags & CTL_PORT_WWNN_VALID) ?
2607 (entry->flags & CTL_PORT_WWPN_VALID) ?
2608 1 : 0, entry->wwpn);
2613 mtx_unlock(&softc->ctl_lock);
2617 struct ctl_ooa *ooa_hdr;
2618 struct ctl_ooa_entry *entries;
2619 uint32_t cur_fill_num;
2621 ooa_hdr = (struct ctl_ooa *)addr;
2623 if ((ooa_hdr->alloc_len == 0)
2624 || (ooa_hdr->alloc_num == 0)) {
2625 printf("%s: CTL_GET_OOA: alloc len %u and alloc num %u "
2626 "must be non-zero\n", __func__,
2627 ooa_hdr->alloc_len, ooa_hdr->alloc_num);
2632 if (ooa_hdr->alloc_len != (ooa_hdr->alloc_num *
2633 sizeof(struct ctl_ooa_entry))) {
2634 printf("%s: CTL_GET_OOA: alloc len %u must be alloc "
2635 "num %d * sizeof(struct ctl_ooa_entry) %zd\n",
2636 __func__, ooa_hdr->alloc_len,
2637 ooa_hdr->alloc_num,sizeof(struct ctl_ooa_entry));
2642 entries = malloc(ooa_hdr->alloc_len, M_CTL, M_WAITOK | M_ZERO);
2643 if (entries == NULL) {
2644 printf("%s: could not allocate %d bytes for OOA "
2645 "dump\n", __func__, ooa_hdr->alloc_len);
2650 mtx_lock(&softc->ctl_lock);
2651 if ((ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) == 0 &&
2652 (ooa_hdr->lun_num >= ctl_max_luns ||
2653 softc->ctl_luns[ooa_hdr->lun_num] == NULL)) {
2654 mtx_unlock(&softc->ctl_lock);
2655 free(entries, M_CTL);
2656 printf("%s: CTL_GET_OOA: invalid LUN %ju\n",
2657 __func__, (uintmax_t)ooa_hdr->lun_num);
2664 if (ooa_hdr->flags & CTL_OOA_FLAG_ALL_LUNS) {
2665 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2666 ctl_ioctl_fill_ooa(lun, &cur_fill_num,
2670 lun = softc->ctl_luns[ooa_hdr->lun_num];
2671 ctl_ioctl_fill_ooa(lun, &cur_fill_num, ooa_hdr,
2674 mtx_unlock(&softc->ctl_lock);
2676 ooa_hdr->fill_num = min(cur_fill_num, ooa_hdr->alloc_num);
2677 ooa_hdr->fill_len = ooa_hdr->fill_num *
2678 sizeof(struct ctl_ooa_entry);
2679 retval = copyout(entries, ooa_hdr->entries, ooa_hdr->fill_len);
2681 printf("%s: error copying out %d bytes for OOA dump\n",
2682 __func__, ooa_hdr->fill_len);
2685 getbinuptime(&ooa_hdr->cur_bt);
2687 if (cur_fill_num > ooa_hdr->alloc_num) {
2688 ooa_hdr->dropped_num = cur_fill_num -ooa_hdr->alloc_num;
2689 ooa_hdr->status = CTL_OOA_NEED_MORE_SPACE;
2691 ooa_hdr->dropped_num = 0;
2692 ooa_hdr->status = CTL_OOA_OK;
2695 free(entries, M_CTL);
2698 case CTL_DELAY_IO: {
2699 struct ctl_io_delay_info *delay_info;
2701 delay_info = (struct ctl_io_delay_info *)addr;
2704 mtx_lock(&softc->ctl_lock);
2705 if (delay_info->lun_id >= ctl_max_luns ||
2706 (lun = softc->ctl_luns[delay_info->lun_id]) == NULL) {
2707 mtx_unlock(&softc->ctl_lock);
2708 delay_info->status = CTL_DELAY_STATUS_INVALID_LUN;
2711 mtx_lock(&lun->lun_lock);
2712 mtx_unlock(&softc->ctl_lock);
2713 delay_info->status = CTL_DELAY_STATUS_OK;
2714 switch (delay_info->delay_type) {
2715 case CTL_DELAY_TYPE_CONT:
2716 case CTL_DELAY_TYPE_ONESHOT:
2719 delay_info->status = CTL_DELAY_STATUS_INVALID_TYPE;
2722 switch (delay_info->delay_loc) {
2723 case CTL_DELAY_LOC_DATAMOVE:
2724 lun->delay_info.datamove_type = delay_info->delay_type;
2725 lun->delay_info.datamove_delay = delay_info->delay_secs;
2727 case CTL_DELAY_LOC_DONE:
2728 lun->delay_info.done_type = delay_info->delay_type;
2729 lun->delay_info.done_delay = delay_info->delay_secs;
2732 delay_info->status = CTL_DELAY_STATUS_INVALID_LOC;
2735 mtx_unlock(&lun->lun_lock);
2737 delay_info->status = CTL_DELAY_STATUS_NOT_IMPLEMENTED;
2738 #endif /* CTL_IO_DELAY */
2741 case CTL_ERROR_INJECT: {
2742 struct ctl_error_desc *err_desc, *new_err_desc;
2744 err_desc = (struct ctl_error_desc *)addr;
2746 new_err_desc = malloc(sizeof(*new_err_desc), M_CTL,
2748 bcopy(err_desc, new_err_desc, sizeof(*new_err_desc));
2750 mtx_lock(&softc->ctl_lock);
2751 if (err_desc->lun_id >= ctl_max_luns ||
2752 (lun = softc->ctl_luns[err_desc->lun_id]) == NULL) {
2753 mtx_unlock(&softc->ctl_lock);
2754 free(new_err_desc, M_CTL);
2755 printf("%s: CTL_ERROR_INJECT: invalid LUN %ju\n",
2756 __func__, (uintmax_t)err_desc->lun_id);
2760 mtx_lock(&lun->lun_lock);
2761 mtx_unlock(&softc->ctl_lock);
2764 * We could do some checking here to verify the validity
2765 * of the request, but given the complexity of error
2766 * injection requests, the checking logic would be fairly
2769 * For now, if the request is invalid, it just won't get
2770 * executed and might get deleted.
2772 STAILQ_INSERT_TAIL(&lun->error_list, new_err_desc, links);
2775 * XXX KDM check to make sure the serial number is unique,
2776 * in case we somehow manage to wrap. That shouldn't
2777 * happen for a very long time, but it's the right thing to
2780 new_err_desc->serial = lun->error_serial;
2781 err_desc->serial = lun->error_serial;
2782 lun->error_serial++;
2784 mtx_unlock(&lun->lun_lock);
2787 case CTL_ERROR_INJECT_DELETE: {
2788 struct ctl_error_desc *delete_desc, *desc, *desc2;
2791 delete_desc = (struct ctl_error_desc *)addr;
2794 mtx_lock(&softc->ctl_lock);
2795 if (delete_desc->lun_id >= ctl_max_luns ||
2796 (lun = softc->ctl_luns[delete_desc->lun_id]) == NULL) {
2797 mtx_unlock(&softc->ctl_lock);
2798 printf("%s: CTL_ERROR_INJECT_DELETE: invalid LUN %ju\n",
2799 __func__, (uintmax_t)delete_desc->lun_id);
2803 mtx_lock(&lun->lun_lock);
2804 mtx_unlock(&softc->ctl_lock);
2805 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
2806 if (desc->serial != delete_desc->serial)
2809 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc,
2814 mtx_unlock(&lun->lun_lock);
2815 if (delete_done == 0) {
2816 printf("%s: CTL_ERROR_INJECT_DELETE: can't find "
2817 "error serial %ju on LUN %u\n", __func__,
2818 delete_desc->serial, delete_desc->lun_id);
2824 case CTL_DUMP_STRUCTS: {
2826 struct ctl_port *port;
2827 struct ctl_frontend *fe;
2829 mtx_lock(&softc->ctl_lock);
2830 printf("CTL Persistent Reservation information start:\n");
2831 STAILQ_FOREACH(lun, &softc->lun_list, links) {
2832 mtx_lock(&lun->lun_lock);
2833 if ((lun->flags & CTL_LUN_DISABLED) != 0) {
2834 mtx_unlock(&lun->lun_lock);
2838 for (j = 0; j < ctl_max_ports; j++) {
2839 if (lun->pr_keys[j] == NULL)
2841 for (k = 0; k < CTL_MAX_INIT_PER_PORT; k++){
2842 if (lun->pr_keys[j][k] == 0)
2844 printf(" LUN %ju port %d iid %d key "
2845 "%#jx\n", lun->lun, j, k,
2846 (uintmax_t)lun->pr_keys[j][k]);
2849 mtx_unlock(&lun->lun_lock);
2851 printf("CTL Persistent Reservation information end\n");
2852 printf("CTL Ports:\n");
2853 STAILQ_FOREACH(port, &softc->port_list, links) {
2854 printf(" Port %d '%s' Frontend '%s' Type %u pp %d vp %d WWNN "
2855 "%#jx WWPN %#jx\n", port->targ_port, port->port_name,
2856 port->frontend->name, port->port_type,
2857 port->physical_port, port->virtual_port,
2858 (uintmax_t)port->wwnn, (uintmax_t)port->wwpn);
2859 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
2860 if (port->wwpn_iid[j].in_use == 0 &&
2861 port->wwpn_iid[j].wwpn == 0 &&
2862 port->wwpn_iid[j].name == NULL)
2865 printf(" iid %u use %d WWPN %#jx '%s'\n",
2866 j, port->wwpn_iid[j].in_use,
2867 (uintmax_t)port->wwpn_iid[j].wwpn,
2868 port->wwpn_iid[j].name);
2871 printf("CTL Port information end\n");
2872 mtx_unlock(&softc->ctl_lock);
2874 * XXX KDM calling this without a lock. We'd likely want
2875 * to drop the lock before calling the frontend's dump
2878 printf("CTL Frontends:\n");
2879 STAILQ_FOREACH(fe, &softc->fe_list, links) {
2880 printf(" Frontend '%s'\n", fe->name);
2881 if (fe->fe_dump != NULL)
2884 printf("CTL Frontend information end\n");
2888 struct ctl_lun_req *lun_req;
2889 struct ctl_backend_driver *backend;
2891 nvlist_t *tmp_args_nvl;
2894 lun_req = (struct ctl_lun_req *)addr;
2895 tmp_args_nvl = lun_req->args_nvl;
2897 backend = ctl_backend_find(lun_req->backend);
2898 if (backend == NULL) {
2899 lun_req->status = CTL_LUN_ERROR;
2900 snprintf(lun_req->error_str,
2901 sizeof(lun_req->error_str),
2902 "Backend \"%s\" not found.",
2907 if (lun_req->args != NULL) {
2908 packed = malloc(lun_req->args_len, M_CTL, M_WAITOK);
2909 if (copyin(lun_req->args, packed, lun_req->args_len) != 0) {
2910 free(packed, M_CTL);
2911 lun_req->status = CTL_LUN_ERROR;
2912 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2913 "Cannot copyin args.");
2916 lun_req->args_nvl = nvlist_unpack(packed,
2917 lun_req->args_len, 0);
2918 free(packed, M_CTL);
2920 if (lun_req->args_nvl == NULL) {
2921 lun_req->status = CTL_LUN_ERROR;
2922 snprintf(lun_req->error_str, sizeof(lun_req->error_str),
2923 "Cannot unpack args nvlist.");
2927 lun_req->args_nvl = nvlist_create(0);
2929 retval = backend->ioctl(dev, cmd, addr, flag, td);
2930 nvlist_destroy(lun_req->args_nvl);
2931 lun_req->args_nvl = tmp_args_nvl;
2933 if (lun_req->result_nvl != NULL) {
2934 if (lun_req->result != NULL) {
2935 packed = nvlist_pack(lun_req->result_nvl,
2937 if (packed == NULL) {
2938 lun_req->status = CTL_LUN_ERROR;
2939 snprintf(lun_req->error_str,
2940 sizeof(lun_req->error_str),
2941 "Cannot pack result nvlist.");
2945 if (packed_len > lun_req->result_len) {
2946 lun_req->status = CTL_LUN_ERROR;
2947 snprintf(lun_req->error_str,
2948 sizeof(lun_req->error_str),
2949 "Result nvlist too large.");
2950 free(packed, M_NVLIST);
2954 if (copyout(packed, lun_req->result, packed_len)) {
2955 lun_req->status = CTL_LUN_ERROR;
2956 snprintf(lun_req->error_str,
2957 sizeof(lun_req->error_str),
2958 "Cannot copyout() the result.");
2959 free(packed, M_NVLIST);
2963 lun_req->result_len = packed_len;
2964 free(packed, M_NVLIST);
2967 nvlist_destroy(lun_req->result_nvl);
2971 case CTL_LUN_LIST: {
2973 struct ctl_lun_list *list;
2974 const char *name, *value;
2978 list = (struct ctl_lun_list *)addr;
2981 * Allocate a fixed length sbuf here, based on the length
2982 * of the user's buffer. We could allocate an auto-extending
2983 * buffer, and then tell the user how much larger our
2984 * amount of data is than his buffer, but that presents
2987 * 1. The sbuf(9) routines use a blocking malloc, and so
2988 * we can't hold a lock while calling them with an
2989 * auto-extending buffer.
2991 * 2. There is not currently a LUN reference counting
2992 * mechanism, outside of outstanding transactions on
2993 * the LUN's OOA queue. So a LUN could go away on us
2994 * while we're getting the LUN number, backend-specific
2995 * information, etc. Thus, given the way things
2996 * currently work, we need to hold the CTL lock while
2997 * grabbing LUN information.
2999 * So, from the user's standpoint, the best thing to do is
3000 * allocate what he thinks is a reasonable buffer length,
3001 * and then if he gets a CTL_LUN_LIST_NEED_MORE_SPACE error,
3002 * double the buffer length and try again. (And repeat
3003 * that until he succeeds.)
3005 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3007 list->status = CTL_LUN_LIST_ERROR;
3008 snprintf(list->error_str, sizeof(list->error_str),
3009 "Unable to allocate %d bytes for LUN list",
3014 sbuf_printf(sb, "<ctllunlist>\n");
3016 mtx_lock(&softc->ctl_lock);
3017 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3018 mtx_lock(&lun->lun_lock);
3019 retval = sbuf_printf(sb, "<lun id=\"%ju\">\n",
3020 (uintmax_t)lun->lun);
3023 * Bail out as soon as we see that we've overfilled
3029 retval = sbuf_printf(sb, "\t<backend_type>%s"
3030 "</backend_type>\n",
3031 (lun->backend == NULL) ? "none" :
3032 lun->backend->name);
3037 retval = sbuf_printf(sb, "\t<lun_type>%d</lun_type>\n",
3038 lun->be_lun->lun_type);
3043 if (lun->backend == NULL) {
3044 retval = sbuf_printf(sb, "</lun>\n");
3050 retval = sbuf_printf(sb, "\t<size>%ju</size>\n",
3051 (lun->be_lun->maxlba > 0) ?
3052 lun->be_lun->maxlba + 1 : 0);
3057 retval = sbuf_printf(sb, "\t<blocksize>%u</blocksize>\n",
3058 lun->be_lun->blocksize);
3063 retval = sbuf_printf(sb, "\t<serial_number>");
3068 retval = ctl_sbuf_printf_esc(sb,
3069 lun->be_lun->serial_num,
3070 sizeof(lun->be_lun->serial_num));
3075 retval = sbuf_printf(sb, "</serial_number>\n");
3080 retval = sbuf_printf(sb, "\t<device_id>");
3085 retval = ctl_sbuf_printf_esc(sb,
3086 lun->be_lun->device_id,
3087 sizeof(lun->be_lun->device_id));
3092 retval = sbuf_printf(sb, "</device_id>\n");
3097 if (lun->backend->lun_info != NULL) {
3098 retval = lun->backend->lun_info(lun->be_lun->be_lun, sb);
3104 while ((name = nvlist_next(lun->be_lun->options, &type,
3105 &cookie)) != NULL) {
3106 sbuf_printf(sb, "\t<%s>", name);
3108 if (type == NV_TYPE_STRING) {
3109 value = dnvlist_get_string(
3110 lun->be_lun->options, name, NULL);
3112 sbuf_printf(sb, "%s", value);
3115 sbuf_printf(sb, "</%s>\n", name);
3118 retval = sbuf_printf(sb, "</lun>\n");
3122 mtx_unlock(&lun->lun_lock);
3125 mtx_unlock(&lun->lun_lock);
3126 mtx_unlock(&softc->ctl_lock);
3129 || ((retval = sbuf_printf(sb, "</ctllunlist>\n")) != 0)) {
3132 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3133 snprintf(list->error_str, sizeof(list->error_str),
3134 "Out of space, %d bytes is too small",
3141 retval = copyout(sbuf_data(sb), list->lun_xml,
3144 list->fill_len = sbuf_len(sb) + 1;
3145 list->status = CTL_LUN_LIST_OK;
3150 struct ctl_iscsi *ci;
3151 struct ctl_frontend *fe;
3153 ci = (struct ctl_iscsi *)addr;
3155 fe = ctl_frontend_find("iscsi");
3157 ci->status = CTL_ISCSI_ERROR;
3158 snprintf(ci->error_str, sizeof(ci->error_str),
3159 "Frontend \"iscsi\" not found.");
3163 retval = fe->ioctl(dev, cmd, addr, flag, td);
3166 case CTL_PORT_REQ: {
3167 struct ctl_req *req;
3168 struct ctl_frontend *fe;
3170 nvlist_t *tmp_args_nvl;
3173 req = (struct ctl_req *)addr;
3174 tmp_args_nvl = req->args_nvl;
3176 fe = ctl_frontend_find(req->driver);
3178 req->status = CTL_LUN_ERROR;
3179 snprintf(req->error_str, sizeof(req->error_str),
3180 "Frontend \"%s\" not found.", req->driver);
3184 if (req->args != NULL) {
3185 packed = malloc(req->args_len, M_CTL, M_WAITOK);
3186 if (copyin(req->args, packed, req->args_len) != 0) {
3187 free(packed, M_CTL);
3188 req->status = CTL_LUN_ERROR;
3189 snprintf(req->error_str, sizeof(req->error_str),
3190 "Cannot copyin args.");
3193 req->args_nvl = nvlist_unpack(packed,
3195 free(packed, M_CTL);
3197 if (req->args_nvl == NULL) {
3198 req->status = CTL_LUN_ERROR;
3199 snprintf(req->error_str, sizeof(req->error_str),
3200 "Cannot unpack args nvlist.");
3204 req->args_nvl = nvlist_create(0);
3207 retval = fe->ioctl(dev, cmd, addr, flag, td);
3211 nvlist_destroy(req->args_nvl);
3212 req->args_nvl = tmp_args_nvl;
3214 if (req->result_nvl != NULL) {
3215 if (req->result != NULL) {
3216 packed = nvlist_pack(req->result_nvl,
3218 if (packed == NULL) {
3219 req->status = CTL_LUN_ERROR;
3220 snprintf(req->error_str,
3221 sizeof(req->error_str),
3222 "Cannot pack result nvlist.");
3226 if (packed_len > req->result_len) {
3227 req->status = CTL_LUN_ERROR;
3228 snprintf(req->error_str,
3229 sizeof(req->error_str),
3230 "Result nvlist too large.");
3231 free(packed, M_NVLIST);
3235 if (copyout(packed, req->result, packed_len)) {
3236 req->status = CTL_LUN_ERROR;
3237 snprintf(req->error_str,
3238 sizeof(req->error_str),
3239 "Cannot copyout() the result.");
3240 free(packed, M_NVLIST);
3244 req->result_len = packed_len;
3245 free(packed, M_NVLIST);
3248 nvlist_destroy(req->result_nvl);
3252 case CTL_PORT_LIST: {
3254 struct ctl_port *port;
3255 struct ctl_lun_list *list;
3256 const char *name, *value;
3261 list = (struct ctl_lun_list *)addr;
3263 sb = sbuf_new(NULL, NULL, list->alloc_len, SBUF_FIXEDLEN);
3265 list->status = CTL_LUN_LIST_ERROR;
3266 snprintf(list->error_str, sizeof(list->error_str),
3267 "Unable to allocate %d bytes for LUN list",
3272 sbuf_printf(sb, "<ctlportlist>\n");
3274 mtx_lock(&softc->ctl_lock);
3275 STAILQ_FOREACH(port, &softc->port_list, links) {
3276 retval = sbuf_printf(sb, "<targ_port id=\"%ju\">\n",
3277 (uintmax_t)port->targ_port);
3280 * Bail out as soon as we see that we've overfilled
3286 retval = sbuf_printf(sb, "\t<frontend_type>%s"
3287 "</frontend_type>\n", port->frontend->name);
3291 retval = sbuf_printf(sb, "\t<port_type>%d</port_type>\n",
3296 retval = sbuf_printf(sb, "\t<online>%s</online>\n",
3297 (port->status & CTL_PORT_STATUS_ONLINE) ? "YES" : "NO");
3301 retval = sbuf_printf(sb, "\t<port_name>%s</port_name>\n",
3306 retval = sbuf_printf(sb, "\t<physical_port>%d</physical_port>\n",
3307 port->physical_port);
3311 retval = sbuf_printf(sb, "\t<virtual_port>%d</virtual_port>\n",
3312 port->virtual_port);
3316 if (port->target_devid != NULL) {
3317 sbuf_printf(sb, "\t<target>");
3318 ctl_id_sbuf(port->target_devid, sb);
3319 sbuf_printf(sb, "</target>\n");
3322 if (port->port_devid != NULL) {
3323 sbuf_printf(sb, "\t<port>");
3324 ctl_id_sbuf(port->port_devid, sb);
3325 sbuf_printf(sb, "</port>\n");
3328 if (port->port_info != NULL) {
3329 retval = port->port_info(port->onoff_arg, sb);
3335 while ((name = nvlist_next(port->options, &type,
3336 &cookie)) != NULL) {
3337 sbuf_printf(sb, "\t<%s>", name);
3339 if (type == NV_TYPE_STRING) {
3340 value = dnvlist_get_string(port->options,
3343 sbuf_printf(sb, "%s", value);
3346 sbuf_printf(sb, "</%s>\n", name);
3349 if (port->lun_map != NULL) {
3350 sbuf_printf(sb, "\t<lun_map>on</lun_map>\n");
3351 for (j = 0; j < port->lun_map_size; j++) {
3352 plun = ctl_lun_map_from_port(port, j);
3353 if (plun == UINT32_MAX)
3356 "\t<lun id=\"%u\">%u</lun>\n",
3361 for (j = 0; j < CTL_MAX_INIT_PER_PORT; j++) {
3362 if (port->wwpn_iid[j].in_use == 0 ||
3363 (port->wwpn_iid[j].wwpn == 0 &&
3364 port->wwpn_iid[j].name == NULL))
3367 if (port->wwpn_iid[j].name != NULL)
3368 retval = sbuf_printf(sb,
3369 "\t<initiator id=\"%u\">%s</initiator>\n",
3370 j, port->wwpn_iid[j].name);
3372 retval = sbuf_printf(sb,
3373 "\t<initiator id=\"%u\">naa.%08jx</initiator>\n",
3374 j, port->wwpn_iid[j].wwpn);
3381 retval = sbuf_printf(sb, "</targ_port>\n");
3385 mtx_unlock(&softc->ctl_lock);
3388 || ((retval = sbuf_printf(sb, "</ctlportlist>\n")) != 0)) {
3391 list->status = CTL_LUN_LIST_NEED_MORE_SPACE;
3392 snprintf(list->error_str, sizeof(list->error_str),
3393 "Out of space, %d bytes is too small",
3400 retval = copyout(sbuf_data(sb), list->lun_xml,
3403 list->fill_len = sbuf_len(sb) + 1;
3404 list->status = CTL_LUN_LIST_OK;
3409 struct ctl_lun_map *lm = (struct ctl_lun_map *)addr;
3410 struct ctl_port *port;
3412 mtx_lock(&softc->ctl_lock);
3413 if (lm->port < softc->port_min ||
3414 lm->port >= softc->port_max ||
3415 (port = softc->ctl_ports[lm->port]) == NULL) {
3416 mtx_unlock(&softc->ctl_lock);
3419 if (port->status & CTL_PORT_STATUS_ONLINE) {
3420 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3421 if (ctl_lun_map_to_port(port, lun->lun) ==
3424 mtx_lock(&lun->lun_lock);
3425 ctl_est_ua_port(lun, lm->port, -1,
3427 mtx_unlock(&lun->lun_lock);
3430 mtx_unlock(&softc->ctl_lock); // XXX: port_enable sleeps
3431 if (lm->plun != UINT32_MAX) {
3432 if (lm->lun == UINT32_MAX)
3433 retval = ctl_lun_map_unset(port, lm->plun);
3434 else if (lm->lun < ctl_max_luns &&
3435 softc->ctl_luns[lm->lun] != NULL)
3436 retval = ctl_lun_map_set(port, lm->plun, lm->lun);
3440 if (lm->lun == UINT32_MAX)
3441 retval = ctl_lun_map_deinit(port);
3443 retval = ctl_lun_map_init(port);
3445 if (port->status & CTL_PORT_STATUS_ONLINE)
3446 ctl_isc_announce_port(port);
3449 case CTL_GET_LUN_STATS: {
3450 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3454 * XXX KDM no locking here. If the LUN list changes,
3455 * things can blow up.
3458 stats->status = CTL_SS_OK;
3459 stats->fill_len = 0;
3460 STAILQ_FOREACH(lun, &softc->lun_list, links) {
3461 if (lun->lun < stats->first_item)
3463 if (stats->fill_len + sizeof(lun->stats) >
3465 stats->status = CTL_SS_NEED_MORE_SPACE;
3468 retval = copyout(&lun->stats, &stats->stats[i++],
3469 sizeof(lun->stats));
3472 stats->fill_len += sizeof(lun->stats);
3474 stats->num_items = softc->num_luns;
3475 stats->flags = CTL_STATS_FLAG_NONE;
3477 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3479 getnanouptime(&stats->timestamp);
3482 case CTL_GET_PORT_STATS: {
3483 struct ctl_get_io_stats *stats = (struct ctl_get_io_stats *)addr;
3487 * XXX KDM no locking here. If the LUN list changes,
3488 * things can blow up.
3491 stats->status = CTL_SS_OK;
3492 stats->fill_len = 0;
3493 STAILQ_FOREACH(port, &softc->port_list, links) {
3494 if (port->targ_port < stats->first_item)
3496 if (stats->fill_len + sizeof(port->stats) >
3498 stats->status = CTL_SS_NEED_MORE_SPACE;
3501 retval = copyout(&port->stats, &stats->stats[i++],
3502 sizeof(port->stats));
3505 stats->fill_len += sizeof(port->stats);
3507 stats->num_items = softc->num_ports;
3508 stats->flags = CTL_STATS_FLAG_NONE;
3510 stats->flags |= CTL_STATS_FLAG_TIME_VALID;
3512 getnanouptime(&stats->timestamp);
3516 /* XXX KDM should we fix this? */
3518 struct ctl_backend_driver *backend;
3525 * We encode the backend type as the ioctl type for backend
3526 * ioctls. So parse it out here, and then search for a
3527 * backend of this type.
3529 type = _IOC_TYPE(cmd);
3531 STAILQ_FOREACH(backend, &softc->be_list, links) {
3532 if (backend->type == type) {
3538 printf("ctl: unknown ioctl command %#lx or backend "
3543 retval = backend->ioctl(dev, cmd, addr, flag, td);
3553 ctl_get_initindex(struct ctl_nexus *nexus)
3555 return (nexus->initid + (nexus->targ_port * CTL_MAX_INIT_PER_PORT));
3559 ctl_lun_map_init(struct ctl_port *port)
3561 struct ctl_softc *softc = port->ctl_softc;
3562 struct ctl_lun *lun;
3563 int size = ctl_lun_map_size;
3566 if (port->lun_map == NULL || port->lun_map_size < size) {
3567 port->lun_map_size = 0;
3568 free(port->lun_map, M_CTL);
3569 port->lun_map = malloc(size * sizeof(uint32_t),
3572 if (port->lun_map == NULL)
3574 for (i = 0; i < size; i++)
3575 port->lun_map[i] = UINT32_MAX;
3576 port->lun_map_size = size;
3577 if (port->status & CTL_PORT_STATUS_ONLINE) {
3578 if (port->lun_disable != NULL) {
3579 STAILQ_FOREACH(lun, &softc->lun_list, links)
3580 port->lun_disable(port->targ_lun_arg, lun->lun);
3582 ctl_isc_announce_port(port);
3588 ctl_lun_map_deinit(struct ctl_port *port)
3590 struct ctl_softc *softc = port->ctl_softc;
3591 struct ctl_lun *lun;
3593 if (port->lun_map == NULL)
3595 port->lun_map_size = 0;
3596 free(port->lun_map, M_CTL);
3597 port->lun_map = NULL;
3598 if (port->status & CTL_PORT_STATUS_ONLINE) {
3599 if (port->lun_enable != NULL) {
3600 STAILQ_FOREACH(lun, &softc->lun_list, links)
3601 port->lun_enable(port->targ_lun_arg, lun->lun);
3603 ctl_isc_announce_port(port);
3609 ctl_lun_map_set(struct ctl_port *port, uint32_t plun, uint32_t glun)
3614 if (port->lun_map == NULL) {
3615 status = ctl_lun_map_init(port);
3619 if (plun >= port->lun_map_size)
3621 old = port->lun_map[plun];
3622 port->lun_map[plun] = glun;
3623 if ((port->status & CTL_PORT_STATUS_ONLINE) && old == UINT32_MAX) {
3624 if (port->lun_enable != NULL)
3625 port->lun_enable(port->targ_lun_arg, plun);
3626 ctl_isc_announce_port(port);
3632 ctl_lun_map_unset(struct ctl_port *port, uint32_t plun)
3636 if (port->lun_map == NULL || plun >= port->lun_map_size)
3638 old = port->lun_map[plun];
3639 port->lun_map[plun] = UINT32_MAX;
3640 if ((port->status & CTL_PORT_STATUS_ONLINE) && old != UINT32_MAX) {
3641 if (port->lun_disable != NULL)
3642 port->lun_disable(port->targ_lun_arg, plun);
3643 ctl_isc_announce_port(port);
3649 ctl_lun_map_from_port(struct ctl_port *port, uint32_t lun_id)
3653 return (UINT32_MAX);
3654 if (port->lun_map == NULL)
3656 if (lun_id > port->lun_map_size)
3657 return (UINT32_MAX);
3658 return (port->lun_map[lun_id]);
3662 ctl_lun_map_to_port(struct ctl_port *port, uint32_t lun_id)
3667 return (UINT32_MAX);
3668 if (port->lun_map == NULL)
3670 for (i = 0; i < port->lun_map_size; i++) {
3671 if (port->lun_map[i] == lun_id)
3674 return (UINT32_MAX);
3678 ctl_decode_lun(uint64_t encoded)
3681 uint32_t result = 0xffffffff;
3683 be64enc(lun, encoded);
3684 switch (lun[0] & RPL_LUNDATA_ATYP_MASK) {
3685 case RPL_LUNDATA_ATYP_PERIPH:
3686 if ((lun[0] & 0x3f) == 0 && lun[2] == 0 && lun[3] == 0 &&
3687 lun[4] == 0 && lun[5] == 0 && lun[6] == 0 && lun[7] == 0)
3690 case RPL_LUNDATA_ATYP_FLAT:
3691 if (lun[2] == 0 && lun[3] == 0 && lun[4] == 0 && lun[5] == 0 &&
3692 lun[6] == 0 && lun[7] == 0)
3693 result = ((lun[0] & 0x3f) << 8) + lun[1];
3695 case RPL_LUNDATA_ATYP_EXTLUN:
3696 switch (lun[0] & RPL_LUNDATA_EXT_EAM_MASK) {
3698 switch (lun[0] & RPL_LUNDATA_EXT_LEN_MASK) {
3703 result = (lun[1] << 16) + (lun[2] << 8) +
3707 if (lun[1] == 0 && lun[6] == 0 && lun[7] == 0)
3708 result = (lun[2] << 24) +
3709 (lun[3] << 16) + (lun[4] << 8) +
3714 case RPL_LUNDATA_EXT_EAM_NOT_SPEC:
3715 result = 0xffffffff;
3724 ctl_encode_lun(uint32_t decoded)
3726 uint64_t l = decoded;
3729 return (((uint64_t)RPL_LUNDATA_ATYP_PERIPH << 56) | (l << 48));
3731 return (((uint64_t)RPL_LUNDATA_ATYP_FLAT << 56) | (l << 48));
3733 return (((uint64_t)(RPL_LUNDATA_ATYP_EXTLUN | 0x12) << 56) |
3735 return ((((uint64_t)RPL_LUNDATA_ATYP_EXTLUN | 0x22) << 56) | (l << 16));
3739 ctl_ffz(uint32_t *mask, uint32_t first, uint32_t last)
3743 for (i = first; i < last; i++) {
3744 if ((mask[i / 32] & (1 << (i % 32))) == 0)
3751 ctl_set_mask(uint32_t *mask, uint32_t bit)
3753 uint32_t chunk, piece;
3756 piece = bit % (sizeof(uint32_t) * 8);
3758 if ((mask[chunk] & (1 << piece)) != 0)
3761 mask[chunk] |= (1 << piece);
3767 ctl_clear_mask(uint32_t *mask, uint32_t bit)
3769 uint32_t chunk, piece;
3772 piece = bit % (sizeof(uint32_t) * 8);
3774 if ((mask[chunk] & (1 << piece)) == 0)
3777 mask[chunk] &= ~(1 << piece);
3783 ctl_is_set(uint32_t *mask, uint32_t bit)
3785 uint32_t chunk, piece;
3788 piece = bit % (sizeof(uint32_t) * 8);
3790 if ((mask[chunk] & (1 << piece)) == 0)
3797 ctl_get_prkey(struct ctl_lun *lun, uint32_t residx)
3801 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3804 return (t[residx % CTL_MAX_INIT_PER_PORT]);
3808 ctl_clr_prkey(struct ctl_lun *lun, uint32_t residx)
3812 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3815 t[residx % CTL_MAX_INIT_PER_PORT] = 0;
3819 ctl_alloc_prkey(struct ctl_lun *lun, uint32_t residx)
3824 i = residx/CTL_MAX_INIT_PER_PORT;
3825 if (lun->pr_keys[i] != NULL)
3827 mtx_unlock(&lun->lun_lock);
3828 p = malloc(sizeof(uint64_t) * CTL_MAX_INIT_PER_PORT, M_CTL,
3830 mtx_lock(&lun->lun_lock);
3831 if (lun->pr_keys[i] == NULL)
3832 lun->pr_keys[i] = p;
3838 ctl_set_prkey(struct ctl_lun *lun, uint32_t residx, uint64_t key)
3842 t = lun->pr_keys[residx/CTL_MAX_INIT_PER_PORT];
3843 KASSERT(t != NULL, ("prkey %d is not allocated", residx));
3844 t[residx % CTL_MAX_INIT_PER_PORT] = key;
3848 * ctl_softc, pool_name, total_ctl_io are passed in.
3849 * npool is passed out.
3852 ctl_pool_create(struct ctl_softc *ctl_softc, const char *pool_name,
3853 uint32_t total_ctl_io, void **npool)
3855 struct ctl_io_pool *pool;
3857 pool = (struct ctl_io_pool *)malloc(sizeof(*pool), M_CTL,
3862 snprintf(pool->name, sizeof(pool->name), "CTL IO %s", pool_name);
3863 pool->ctl_softc = ctl_softc;
3865 pool->zone = uma_zsecond_create(pool->name, NULL,
3866 NULL, NULL, NULL, ctl_softc->io_zone);
3867 /* uma_prealloc(pool->zone, total_ctl_io); */
3869 pool->zone = ctl_softc->io_zone;
3877 ctl_pool_free(struct ctl_io_pool *pool)
3884 uma_zdestroy(pool->zone);
3890 ctl_alloc_io(void *pool_ref)
3892 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3895 io = uma_zalloc(pool->zone, M_WAITOK);
3897 io->io_hdr.pool = pool_ref;
3898 CTL_SOFTC(io) = pool->ctl_softc;
3899 TAILQ_INIT(&io->io_hdr.blocked_queue);
3905 ctl_alloc_io_nowait(void *pool_ref)
3907 struct ctl_io_pool *pool = (struct ctl_io_pool *)pool_ref;
3910 io = uma_zalloc(pool->zone, M_NOWAIT);
3912 io->io_hdr.pool = pool_ref;
3913 CTL_SOFTC(io) = pool->ctl_softc;
3914 TAILQ_INIT(&io->io_hdr.blocked_queue);
3920 ctl_free_io(union ctl_io *io)
3922 struct ctl_io_pool *pool;
3927 pool = (struct ctl_io_pool *)io->io_hdr.pool;
3928 uma_zfree(pool->zone, io);
3932 ctl_zero_io(union ctl_io *io)
3934 struct ctl_io_pool *pool;
3940 * May need to preserve linked list pointers at some point too.
3942 pool = io->io_hdr.pool;
3943 memset(io, 0, sizeof(*io));
3944 io->io_hdr.pool = pool;
3945 CTL_SOFTC(io) = pool->ctl_softc;
3946 TAILQ_INIT(&io->io_hdr.blocked_queue);
3950 ctl_expand_number(const char *buf, uint64_t *num)
3956 number = strtoq(buf, &endptr, 0);
3958 switch (tolower((unsigned char)*endptr)) {
3978 case '\0': /* No unit. */
3982 /* Unrecognized unit. */
3986 if ((number << shift) >> shift != number) {
3990 *num = number << shift;
3996 * This routine could be used in the future to load default and/or saved
3997 * mode page parameters for a particuar lun.
4000 ctl_init_page_index(struct ctl_lun *lun)
4003 struct ctl_page_index *page_index;
4007 memcpy(&lun->mode_pages.index, page_index_template,
4008 sizeof(page_index_template));
4010 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
4012 page_index = &lun->mode_pages.index[i];
4013 if (lun->be_lun->lun_type == T_DIRECT &&
4014 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4016 if (lun->be_lun->lun_type == T_PROCESSOR &&
4017 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4019 if (lun->be_lun->lun_type == T_CDROM &&
4020 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4023 page_code = page_index->page_code & SMPH_PC_MASK;
4024 switch (page_code) {
4025 case SMS_RW_ERROR_RECOVERY_PAGE: {
4026 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4027 ("subpage %#x for page %#x is incorrect!",
4028 page_index->subpage, page_code));
4029 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CURRENT],
4030 &rw_er_page_default,
4031 sizeof(rw_er_page_default));
4032 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_CHANGEABLE],
4033 &rw_er_page_changeable,
4034 sizeof(rw_er_page_changeable));
4035 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_DEFAULT],
4036 &rw_er_page_default,
4037 sizeof(rw_er_page_default));
4038 memcpy(&lun->mode_pages.rw_er_page[CTL_PAGE_SAVED],
4039 &rw_er_page_default,
4040 sizeof(rw_er_page_default));
4041 page_index->page_data =
4042 (uint8_t *)lun->mode_pages.rw_er_page;
4045 case SMS_FORMAT_DEVICE_PAGE: {
4046 struct scsi_format_page *format_page;
4048 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4049 ("subpage %#x for page %#x is incorrect!",
4050 page_index->subpage, page_code));
4053 * Sectors per track are set above. Bytes per
4054 * sector need to be set here on a per-LUN basis.
4056 memcpy(&lun->mode_pages.format_page[CTL_PAGE_CURRENT],
4057 &format_page_default,
4058 sizeof(format_page_default));
4059 memcpy(&lun->mode_pages.format_page[
4060 CTL_PAGE_CHANGEABLE], &format_page_changeable,
4061 sizeof(format_page_changeable));
4062 memcpy(&lun->mode_pages.format_page[CTL_PAGE_DEFAULT],
4063 &format_page_default,
4064 sizeof(format_page_default));
4065 memcpy(&lun->mode_pages.format_page[CTL_PAGE_SAVED],
4066 &format_page_default,
4067 sizeof(format_page_default));
4069 format_page = &lun->mode_pages.format_page[
4071 scsi_ulto2b(lun->be_lun->blocksize,
4072 format_page->bytes_per_sector);
4074 format_page = &lun->mode_pages.format_page[
4076 scsi_ulto2b(lun->be_lun->blocksize,
4077 format_page->bytes_per_sector);
4079 format_page = &lun->mode_pages.format_page[
4081 scsi_ulto2b(lun->be_lun->blocksize,
4082 format_page->bytes_per_sector);
4084 page_index->page_data =
4085 (uint8_t *)lun->mode_pages.format_page;
4088 case SMS_RIGID_DISK_PAGE: {
4089 struct scsi_rigid_disk_page *rigid_disk_page;
4090 uint32_t sectors_per_cylinder;
4094 #endif /* !__XSCALE__ */
4096 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4097 ("subpage %#x for page %#x is incorrect!",
4098 page_index->subpage, page_code));
4101 * Rotation rate and sectors per track are set
4102 * above. We calculate the cylinders here based on
4103 * capacity. Due to the number of heads and
4104 * sectors per track we're using, smaller arrays
4105 * may turn out to have 0 cylinders. Linux and
4106 * FreeBSD don't pay attention to these mode pages
4107 * to figure out capacity, but Solaris does. It
4108 * seems to deal with 0 cylinders just fine, and
4109 * works out a fake geometry based on the capacity.
4111 memcpy(&lun->mode_pages.rigid_disk_page[
4112 CTL_PAGE_DEFAULT], &rigid_disk_page_default,
4113 sizeof(rigid_disk_page_default));
4114 memcpy(&lun->mode_pages.rigid_disk_page[
4115 CTL_PAGE_CHANGEABLE],&rigid_disk_page_changeable,
4116 sizeof(rigid_disk_page_changeable));
4118 sectors_per_cylinder = CTL_DEFAULT_SECTORS_PER_TRACK *
4122 * The divide method here will be more accurate,
4123 * probably, but results in floating point being
4124 * used in the kernel on i386 (__udivdi3()). On the
4125 * XScale, though, __udivdi3() is implemented in
4128 * The shift method for cylinder calculation is
4129 * accurate if sectors_per_cylinder is a power of
4130 * 2. Otherwise it might be slightly off -- you
4131 * might have a bit of a truncation problem.
4134 cylinders = (lun->be_lun->maxlba + 1) /
4135 sectors_per_cylinder;
4137 for (shift = 31; shift > 0; shift--) {
4138 if (sectors_per_cylinder & (1 << shift))
4141 cylinders = (lun->be_lun->maxlba + 1) >> shift;
4145 * We've basically got 3 bytes, or 24 bits for the
4146 * cylinder size in the mode page. If we're over,
4147 * just round down to 2^24.
4149 if (cylinders > 0xffffff)
4150 cylinders = 0xffffff;
4152 rigid_disk_page = &lun->mode_pages.rigid_disk_page[
4154 scsi_ulto3b(cylinders, rigid_disk_page->cylinders);
4156 if ((value = dnvlist_get_string(lun->be_lun->options,
4157 "rpm", NULL)) != NULL) {
4158 scsi_ulto2b(strtol(value, NULL, 0),
4159 rigid_disk_page->rotation_rate);
4162 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_CURRENT],
4163 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4164 sizeof(rigid_disk_page_default));
4165 memcpy(&lun->mode_pages.rigid_disk_page[CTL_PAGE_SAVED],
4166 &lun->mode_pages.rigid_disk_page[CTL_PAGE_DEFAULT],
4167 sizeof(rigid_disk_page_default));
4169 page_index->page_data =
4170 (uint8_t *)lun->mode_pages.rigid_disk_page;
4173 case SMS_VERIFY_ERROR_RECOVERY_PAGE: {
4174 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4175 ("subpage %#x for page %#x is incorrect!",
4176 page_index->subpage, page_code));
4177 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CURRENT],
4178 &verify_er_page_default,
4179 sizeof(verify_er_page_default));
4180 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_CHANGEABLE],
4181 &verify_er_page_changeable,
4182 sizeof(verify_er_page_changeable));
4183 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_DEFAULT],
4184 &verify_er_page_default,
4185 sizeof(verify_er_page_default));
4186 memcpy(&lun->mode_pages.verify_er_page[CTL_PAGE_SAVED],
4187 &verify_er_page_default,
4188 sizeof(verify_er_page_default));
4189 page_index->page_data =
4190 (uint8_t *)lun->mode_pages.verify_er_page;
4193 case SMS_CACHING_PAGE: {
4194 struct scsi_caching_page *caching_page;
4196 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4197 ("subpage %#x for page %#x is incorrect!",
4198 page_index->subpage, page_code));
4199 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_DEFAULT],
4200 &caching_page_default,
4201 sizeof(caching_page_default));
4202 memcpy(&lun->mode_pages.caching_page[
4203 CTL_PAGE_CHANGEABLE], &caching_page_changeable,
4204 sizeof(caching_page_changeable));
4205 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4206 &caching_page_default,
4207 sizeof(caching_page_default));
4208 caching_page = &lun->mode_pages.caching_page[
4210 value = dnvlist_get_string(lun->be_lun->options,
4211 "writecache", NULL);
4212 if (value != NULL && strcmp(value, "off") == 0)
4213 caching_page->flags1 &= ~SCP_WCE;
4214 value = dnvlist_get_string(lun->be_lun->options,
4216 if (value != NULL && strcmp(value, "off") == 0)
4217 caching_page->flags1 |= SCP_RCD;
4218 memcpy(&lun->mode_pages.caching_page[CTL_PAGE_CURRENT],
4219 &lun->mode_pages.caching_page[CTL_PAGE_SAVED],
4220 sizeof(caching_page_default));
4221 page_index->page_data =
4222 (uint8_t *)lun->mode_pages.caching_page;
4225 case SMS_CONTROL_MODE_PAGE: {
4226 switch (page_index->subpage) {
4227 case SMS_SUBPAGE_PAGE_0: {
4228 struct scsi_control_page *control_page;
4230 memcpy(&lun->mode_pages.control_page[
4232 &control_page_default,
4233 sizeof(control_page_default));
4234 memcpy(&lun->mode_pages.control_page[
4235 CTL_PAGE_CHANGEABLE],
4236 &control_page_changeable,
4237 sizeof(control_page_changeable));
4238 memcpy(&lun->mode_pages.control_page[
4240 &control_page_default,
4241 sizeof(control_page_default));
4242 control_page = &lun->mode_pages.control_page[
4244 value = dnvlist_get_string(lun->be_lun->options,
4245 "reordering", NULL);
4246 if (value != NULL &&
4247 strcmp(value, "unrestricted") == 0) {
4248 control_page->queue_flags &=
4249 ~SCP_QUEUE_ALG_MASK;
4250 control_page->queue_flags |=
4251 SCP_QUEUE_ALG_UNRESTRICTED;
4253 memcpy(&lun->mode_pages.control_page[
4255 &lun->mode_pages.control_page[
4257 sizeof(control_page_default));
4258 page_index->page_data =
4259 (uint8_t *)lun->mode_pages.control_page;
4263 memcpy(&lun->mode_pages.control_ext_page[
4265 &control_ext_page_default,
4266 sizeof(control_ext_page_default));
4267 memcpy(&lun->mode_pages.control_ext_page[
4268 CTL_PAGE_CHANGEABLE],
4269 &control_ext_page_changeable,
4270 sizeof(control_ext_page_changeable));
4271 memcpy(&lun->mode_pages.control_ext_page[
4273 &control_ext_page_default,
4274 sizeof(control_ext_page_default));
4275 memcpy(&lun->mode_pages.control_ext_page[
4277 &lun->mode_pages.control_ext_page[
4279 sizeof(control_ext_page_default));
4280 page_index->page_data =
4281 (uint8_t *)lun->mode_pages.control_ext_page;
4284 panic("subpage %#x for page %#x is incorrect!",
4285 page_index->subpage, page_code);
4289 case SMS_INFO_EXCEPTIONS_PAGE: {
4290 switch (page_index->subpage) {
4291 case SMS_SUBPAGE_PAGE_0:
4292 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_CURRENT],
4294 sizeof(ie_page_default));
4295 memcpy(&lun->mode_pages.ie_page[
4296 CTL_PAGE_CHANGEABLE], &ie_page_changeable,
4297 sizeof(ie_page_changeable));
4298 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_DEFAULT],
4300 sizeof(ie_page_default));
4301 memcpy(&lun->mode_pages.ie_page[CTL_PAGE_SAVED],
4303 sizeof(ie_page_default));
4304 page_index->page_data =
4305 (uint8_t *)lun->mode_pages.ie_page;
4308 struct ctl_logical_block_provisioning_page *page;
4310 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_DEFAULT],
4312 sizeof(lbp_page_default));
4313 memcpy(&lun->mode_pages.lbp_page[
4314 CTL_PAGE_CHANGEABLE], &lbp_page_changeable,
4315 sizeof(lbp_page_changeable));
4316 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4318 sizeof(lbp_page_default));
4319 page = &lun->mode_pages.lbp_page[CTL_PAGE_SAVED];
4320 value = dnvlist_get_string(lun->be_lun->options,
4321 "avail-threshold", NULL);
4322 if (value != NULL &&
4323 ctl_expand_number(value, &ival) == 0) {
4324 page->descr[0].flags |= SLBPPD_ENABLED |
4326 if (lun->be_lun->blocksize)
4327 ival /= lun->be_lun->blocksize;
4330 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4331 page->descr[0].count);
4333 value = dnvlist_get_string(lun->be_lun->options,
4334 "used-threshold", NULL);
4335 if (value != NULL &&
4336 ctl_expand_number(value, &ival) == 0) {
4337 page->descr[1].flags |= SLBPPD_ENABLED |
4339 if (lun->be_lun->blocksize)
4340 ival /= lun->be_lun->blocksize;
4343 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4344 page->descr[1].count);
4346 value = dnvlist_get_string(lun->be_lun->options,
4347 "pool-avail-threshold", NULL);
4348 if (value != NULL &&
4349 ctl_expand_number(value, &ival) == 0) {
4350 page->descr[2].flags |= SLBPPD_ENABLED |
4352 if (lun->be_lun->blocksize)
4353 ival /= lun->be_lun->blocksize;
4356 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4357 page->descr[2].count);
4359 value = dnvlist_get_string(lun->be_lun->options,
4360 "pool-used-threshold", NULL);
4361 if (value != NULL &&
4362 ctl_expand_number(value, &ival) == 0) {
4363 page->descr[3].flags |= SLBPPD_ENABLED |
4365 if (lun->be_lun->blocksize)
4366 ival /= lun->be_lun->blocksize;
4369 scsi_ulto4b(ival >> CTL_LBP_EXPONENT,
4370 page->descr[3].count);
4372 memcpy(&lun->mode_pages.lbp_page[CTL_PAGE_CURRENT],
4373 &lun->mode_pages.lbp_page[CTL_PAGE_SAVED],
4374 sizeof(lbp_page_default));
4375 page_index->page_data =
4376 (uint8_t *)lun->mode_pages.lbp_page;
4380 panic("subpage %#x for page %#x is incorrect!",
4381 page_index->subpage, page_code);
4385 case SMS_CDDVD_CAPS_PAGE:{
4386 KASSERT(page_index->subpage == SMS_SUBPAGE_PAGE_0,
4387 ("subpage %#x for page %#x is incorrect!",
4388 page_index->subpage, page_code));
4389 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_DEFAULT],
4390 &cddvd_page_default,
4391 sizeof(cddvd_page_default));
4392 memcpy(&lun->mode_pages.cddvd_page[
4393 CTL_PAGE_CHANGEABLE], &cddvd_page_changeable,
4394 sizeof(cddvd_page_changeable));
4395 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4396 &cddvd_page_default,
4397 sizeof(cddvd_page_default));
4398 memcpy(&lun->mode_pages.cddvd_page[CTL_PAGE_CURRENT],
4399 &lun->mode_pages.cddvd_page[CTL_PAGE_SAVED],
4400 sizeof(cddvd_page_default));
4401 page_index->page_data =
4402 (uint8_t *)lun->mode_pages.cddvd_page;
4406 panic("invalid page code value %#x", page_code);
4410 return (CTL_RETVAL_COMPLETE);
4414 ctl_init_log_page_index(struct ctl_lun *lun)
4416 struct ctl_page_index *page_index;
4419 memcpy(&lun->log_pages.index, log_page_index_template,
4420 sizeof(log_page_index_template));
4423 for (i = 0, j = 0, k = 0; i < CTL_NUM_LOG_PAGES; i++) {
4425 page_index = &lun->log_pages.index[i];
4426 if (lun->be_lun->lun_type == T_DIRECT &&
4427 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
4429 if (lun->be_lun->lun_type == T_PROCESSOR &&
4430 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
4432 if (lun->be_lun->lun_type == T_CDROM &&
4433 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
4436 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING &&
4437 lun->backend->lun_attr == NULL)
4440 if (page_index->page_code != prev) {
4441 lun->log_pages.pages_page[j] = page_index->page_code;
4442 prev = page_index->page_code;
4445 lun->log_pages.subpages_page[k*2] = page_index->page_code;
4446 lun->log_pages.subpages_page[k*2+1] = page_index->subpage;
4449 lun->log_pages.index[0].page_data = &lun->log_pages.pages_page[0];
4450 lun->log_pages.index[0].page_len = j;
4451 lun->log_pages.index[1].page_data = &lun->log_pages.subpages_page[0];
4452 lun->log_pages.index[1].page_len = k * 2;
4453 lun->log_pages.index[2].page_data = (uint8_t *)&lun->log_pages.temp_page;
4454 lun->log_pages.index[2].page_len = sizeof(lun->log_pages.temp_page);
4455 lun->log_pages.index[3].page_data = &lun->log_pages.lbp_page[0];
4456 lun->log_pages.index[3].page_len = 12*CTL_NUM_LBP_PARAMS;
4457 lun->log_pages.index[4].page_data = (uint8_t *)&lun->log_pages.stat_page;
4458 lun->log_pages.index[4].page_len = sizeof(lun->log_pages.stat_page);
4459 lun->log_pages.index[5].page_data = (uint8_t *)&lun->log_pages.ie_page;
4460 lun->log_pages.index[5].page_len = sizeof(lun->log_pages.ie_page);
4462 return (CTL_RETVAL_COMPLETE);
4466 hex2bin(const char *str, uint8_t *buf, int buf_size)
4471 memset(buf, 0, buf_size);
4472 while (isspace(str[0]))
4474 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
4477 for (i = 0; str[i] != 0 && i < buf_size; i++) {
4478 while (str[i] == '-') /* Skip dashes in UUIDs. */
4483 else if (isalpha(c))
4484 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
4490 buf[i / 2] |= (c << 4);
4494 return ((i + 1) / 2);
4501 * - caller allocates and zeros LUN storage, or passes in a NULL LUN if he
4502 * wants us to allocate the LUN and he can block.
4503 * - ctl_softc is always set
4504 * - be_lun is set if the LUN has a backend (needed for disk LUNs)
4506 * Returns 0 for success, non-zero (errno) for failure.
4509 ctl_alloc_lun(struct ctl_softc *ctl_softc, struct ctl_lun *ctl_lun,
4510 struct ctl_be_lun *const be_lun)
4512 struct ctl_lun *nlun, *lun;
4513 struct scsi_vpd_id_descriptor *desc;
4514 struct scsi_vpd_id_t10 *t10id;
4515 const char *eui, *naa, *scsiname, *uuid, *vendor, *value;
4516 int lun_number, lun_malloced;
4517 int devidlen, idlen1, idlen2 = 0, len;
4523 * We currently only support Direct Access or Processor LUN types.
4525 switch (be_lun->lun_type) {
4533 be_lun->lun_config_status(be_lun->be_lun,
4534 CTL_LUN_CONFIG_FAILURE);
4537 if (ctl_lun == NULL) {
4538 lun = malloc(sizeof(*lun), M_CTL, M_WAITOK);
4545 memset(lun, 0, sizeof(*lun));
4547 lun->flags = CTL_LUN_MALLOCED;
4549 lun->pending_sense = malloc(sizeof(struct scsi_sense_data *) *
4550 ctl_max_ports, M_DEVBUF, M_WAITOK | M_ZERO);
4551 lun->pending_ua = malloc(sizeof(ctl_ua_type *) * ctl_max_ports,
4552 M_DEVBUF, M_WAITOK | M_ZERO);
4553 lun->pr_keys = malloc(sizeof(uint64_t *) * ctl_max_ports,
4554 M_DEVBUF, M_WAITOK | M_ZERO);
4556 /* Generate LUN ID. */
4557 devidlen = max(CTL_DEVID_MIN_LEN,
4558 strnlen(be_lun->device_id, CTL_DEVID_LEN));
4559 idlen1 = sizeof(*t10id) + devidlen;
4560 len = sizeof(struct scsi_vpd_id_descriptor) + idlen1;
4561 scsiname = dnvlist_get_string(be_lun->options, "scsiname", NULL);
4562 if (scsiname != NULL) {
4563 idlen2 = roundup2(strlen(scsiname) + 1, 4);
4564 len += sizeof(struct scsi_vpd_id_descriptor) + idlen2;
4566 eui = dnvlist_get_string(be_lun->options, "eui", NULL);
4568 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4570 naa = dnvlist_get_string(be_lun->options, "naa", NULL);
4572 len += sizeof(struct scsi_vpd_id_descriptor) + 16;
4574 uuid = dnvlist_get_string(be_lun->options, "uuid", NULL);
4576 len += sizeof(struct scsi_vpd_id_descriptor) + 18;
4578 lun->lun_devid = malloc(sizeof(struct ctl_devid) + len,
4579 M_CTL, M_WAITOK | M_ZERO);
4580 desc = (struct scsi_vpd_id_descriptor *)lun->lun_devid->data;
4581 desc->proto_codeset = SVPD_ID_CODESET_ASCII;
4582 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN | SVPD_ID_TYPE_T10;
4583 desc->length = idlen1;
4584 t10id = (struct scsi_vpd_id_t10 *)&desc->identifier[0];
4585 memset(t10id->vendor, ' ', sizeof(t10id->vendor));
4586 if ((vendor = dnvlist_get_string(be_lun->options, "vendor", NULL)) == NULL) {
4587 strncpy((char *)t10id->vendor, CTL_VENDOR, sizeof(t10id->vendor));
4589 strncpy(t10id->vendor, vendor,
4590 min(sizeof(t10id->vendor), strlen(vendor)));
4592 strncpy((char *)t10id->vendor_spec_id,
4593 (char *)be_lun->device_id, devidlen);
4594 if (scsiname != NULL) {
4595 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4597 desc->proto_codeset = SVPD_ID_CODESET_UTF8;
4598 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4599 SVPD_ID_TYPE_SCSI_NAME;
4600 desc->length = idlen2;
4601 strlcpy(desc->identifier, scsiname, idlen2);
4604 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4606 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4607 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4609 desc->length = hex2bin(eui, desc->identifier, 16);
4610 desc->length = desc->length > 12 ? 16 :
4611 (desc->length > 8 ? 12 : 8);
4612 len -= 16 - desc->length;
4615 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4617 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4618 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4620 desc->length = hex2bin(naa, desc->identifier, 16);
4621 desc->length = desc->length > 8 ? 16 : 8;
4622 len -= 16 - desc->length;
4625 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
4627 desc->proto_codeset = SVPD_ID_CODESET_BINARY;
4628 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_LUN |
4630 desc->identifier[0] = 0x10;
4631 hex2bin(uuid, &desc->identifier[2], 16);
4634 lun->lun_devid->len = len;
4636 mtx_lock(&ctl_softc->ctl_lock);
4638 * See if the caller requested a particular LUN number. If so, see
4639 * if it is available. Otherwise, allocate the first available LUN.
4641 if (be_lun->flags & CTL_LUN_FLAG_ID_REQ) {
4642 if ((be_lun->req_lun_id > (ctl_max_luns - 1))
4643 || (ctl_is_set(ctl_softc->ctl_lun_mask, be_lun->req_lun_id))) {
4644 mtx_unlock(&ctl_softc->ctl_lock);
4645 if (be_lun->req_lun_id > (ctl_max_luns - 1)) {
4646 printf("ctl: requested LUN ID %d is higher "
4647 "than ctl_max_luns - 1 (%d)\n",
4648 be_lun->req_lun_id, ctl_max_luns - 1);
4651 * XXX KDM return an error, or just assign
4652 * another LUN ID in this case??
4654 printf("ctl: requested LUN ID %d is already "
4655 "in use\n", be_lun->req_lun_id);
4658 free(lun->lun_devid, M_CTL);
4659 if (lun->flags & CTL_LUN_MALLOCED)
4661 be_lun->lun_config_status(be_lun->be_lun,
4662 CTL_LUN_CONFIG_FAILURE);
4665 lun_number = be_lun->req_lun_id;
4667 lun_number = ctl_ffz(ctl_softc->ctl_lun_mask, 0, ctl_max_luns);
4668 if (lun_number == -1) {
4669 mtx_unlock(&ctl_softc->ctl_lock);
4670 printf("ctl: can't allocate LUN, out of LUNs\n");
4674 ctl_set_mask(ctl_softc->ctl_lun_mask, lun_number);
4675 mtx_unlock(&ctl_softc->ctl_lock);
4677 mtx_init(&lun->lun_lock, "CTL LUN", NULL, MTX_DEF);
4678 lun->lun = lun_number;
4679 lun->be_lun = be_lun;
4681 * The processor LUN is always enabled. Disk LUNs come on line
4682 * disabled, and must be enabled by the backend.
4684 lun->flags |= CTL_LUN_DISABLED;
4685 lun->backend = be_lun->be;
4686 be_lun->ctl_lun = lun;
4687 be_lun->lun_id = lun_number;
4688 atomic_add_int(&be_lun->be->num_luns, 1);
4689 if (be_lun->flags & CTL_LUN_FLAG_EJECTED)
4690 lun->flags |= CTL_LUN_EJECTED;
4691 if (be_lun->flags & CTL_LUN_FLAG_NO_MEDIA)
4692 lun->flags |= CTL_LUN_NO_MEDIA;
4693 if (be_lun->flags & CTL_LUN_FLAG_STOPPED)
4694 lun->flags |= CTL_LUN_STOPPED;
4696 if (be_lun->flags & CTL_LUN_FLAG_PRIMARY)
4697 lun->flags |= CTL_LUN_PRIMARY_SC;
4699 value = dnvlist_get_string(be_lun->options, "removable", NULL);
4700 if (value != NULL) {
4701 if (strcmp(value, "on") == 0)
4702 lun->flags |= CTL_LUN_REMOVABLE;
4703 } else if (be_lun->lun_type == T_CDROM)
4704 lun->flags |= CTL_LUN_REMOVABLE;
4706 lun->ctl_softc = ctl_softc;
4708 lun->last_busy = getsbinuptime();
4710 TAILQ_INIT(&lun->ooa_queue);
4711 STAILQ_INIT(&lun->error_list);
4712 lun->ie_reported = 1;
4713 callout_init_mtx(&lun->ie_callout, &lun->lun_lock, 0);
4714 ctl_tpc_lun_init(lun);
4715 if (lun->flags & CTL_LUN_REMOVABLE) {
4716 lun->prevent = malloc((CTL_MAX_INITIATORS + 31) / 32 * 4,
4721 * Initialize the mode and log page index.
4723 ctl_init_page_index(lun);
4724 ctl_init_log_page_index(lun);
4726 /* Setup statistics gathering */
4727 lun->stats.item = lun_number;
4730 * Now, before we insert this lun on the lun list, set the lun
4731 * inventory changed UA for all other luns.
4733 mtx_lock(&ctl_softc->ctl_lock);
4734 STAILQ_FOREACH(nlun, &ctl_softc->lun_list, links) {
4735 mtx_lock(&nlun->lun_lock);
4736 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4737 mtx_unlock(&nlun->lun_lock);
4739 STAILQ_INSERT_TAIL(&ctl_softc->lun_list, lun, links);
4740 ctl_softc->ctl_luns[lun_number] = lun;
4741 ctl_softc->num_luns++;
4742 mtx_unlock(&ctl_softc->ctl_lock);
4744 lun->be_lun->lun_config_status(lun->be_lun->be_lun, CTL_LUN_CONFIG_OK);
4751 * - LUN has already been marked invalid and any pending I/O has been taken
4755 ctl_free_lun(struct ctl_lun *lun)
4757 struct ctl_softc *softc = lun->ctl_softc;
4758 struct ctl_lun *nlun;
4761 KASSERT(TAILQ_EMPTY(&lun->ooa_queue),
4762 ("Freeing a LUN %p with outstanding I/O!\n", lun));
4764 mtx_lock(&softc->ctl_lock);
4765 STAILQ_REMOVE(&softc->lun_list, lun, ctl_lun, links);
4766 ctl_clear_mask(softc->ctl_lun_mask, lun->lun);
4767 softc->ctl_luns[lun->lun] = NULL;
4769 STAILQ_FOREACH(nlun, &softc->lun_list, links) {
4770 mtx_lock(&nlun->lun_lock);
4771 ctl_est_ua_all(nlun, -1, CTL_UA_LUN_CHANGE);
4772 mtx_unlock(&nlun->lun_lock);
4774 mtx_unlock(&softc->ctl_lock);
4777 * Tell the backend to free resources, if this LUN has a backend.
4779 atomic_subtract_int(&lun->be_lun->be->num_luns, 1);
4780 lun->be_lun->lun_shutdown(lun->be_lun->be_lun);
4782 lun->ie_reportcnt = UINT32_MAX;
4783 callout_drain(&lun->ie_callout);
4784 ctl_tpc_lun_shutdown(lun);
4785 mtx_destroy(&lun->lun_lock);
4786 free(lun->lun_devid, M_CTL);
4787 for (i = 0; i < ctl_max_ports; i++)
4788 free(lun->pending_ua[i], M_CTL);
4789 free(lun->pending_ua, M_DEVBUF);
4790 for (i = 0; i < ctl_max_ports; i++)
4791 free(lun->pr_keys[i], M_CTL);
4792 free(lun->pr_keys, M_DEVBUF);
4793 free(lun->write_buffer, M_CTL);
4794 free(lun->prevent, M_CTL);
4795 if (lun->flags & CTL_LUN_MALLOCED)
4802 ctl_create_lun(struct ctl_be_lun *be_lun)
4806 * ctl_alloc_lun() should handle all potential failure cases.
4808 ctl_alloc_lun(control_softc, NULL, be_lun);
4812 ctl_add_lun(struct ctl_be_lun *be_lun)
4814 struct ctl_softc *softc = control_softc;
4816 mtx_lock(&softc->ctl_lock);
4817 STAILQ_INSERT_TAIL(&softc->pending_lun_queue, be_lun, links);
4818 mtx_unlock(&softc->ctl_lock);
4819 wakeup(&softc->pending_lun_queue);
4825 ctl_enable_lun(struct ctl_be_lun *be_lun)
4827 struct ctl_softc *softc;
4828 struct ctl_port *port, *nport;
4829 struct ctl_lun *lun;
4832 lun = (struct ctl_lun *)be_lun->ctl_lun;
4833 softc = lun->ctl_softc;
4835 mtx_lock(&softc->ctl_lock);
4836 mtx_lock(&lun->lun_lock);
4837 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
4839 * eh? Why did we get called if the LUN is already
4842 mtx_unlock(&lun->lun_lock);
4843 mtx_unlock(&softc->ctl_lock);
4846 lun->flags &= ~CTL_LUN_DISABLED;
4847 mtx_unlock(&lun->lun_lock);
4849 STAILQ_FOREACH_SAFE(port, &softc->port_list, links, nport) {
4850 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4851 port->lun_map != NULL || port->lun_enable == NULL)
4855 * Drop the lock while we call the FETD's enable routine.
4856 * This can lead to a callback into CTL (at least in the
4857 * case of the internal initiator frontend.
4859 mtx_unlock(&softc->ctl_lock);
4860 retval = port->lun_enable(port->targ_lun_arg, lun->lun);
4861 mtx_lock(&softc->ctl_lock);
4863 printf("%s: FETD %s port %d returned error "
4864 "%d for lun_enable on lun %jd\n",
4865 __func__, port->port_name, port->targ_port,
4866 retval, (intmax_t)lun->lun);
4870 mtx_unlock(&softc->ctl_lock);
4871 ctl_isc_announce_lun(lun);
4877 ctl_disable_lun(struct ctl_be_lun *be_lun)
4879 struct ctl_softc *softc;
4880 struct ctl_port *port;
4881 struct ctl_lun *lun;
4884 lun = (struct ctl_lun *)be_lun->ctl_lun;
4885 softc = lun->ctl_softc;
4887 mtx_lock(&softc->ctl_lock);
4888 mtx_lock(&lun->lun_lock);
4889 if (lun->flags & CTL_LUN_DISABLED) {
4890 mtx_unlock(&lun->lun_lock);
4891 mtx_unlock(&softc->ctl_lock);
4894 lun->flags |= CTL_LUN_DISABLED;
4895 mtx_unlock(&lun->lun_lock);
4897 STAILQ_FOREACH(port, &softc->port_list, links) {
4898 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0 ||
4899 port->lun_map != NULL || port->lun_disable == NULL)
4903 * Drop the lock before we call the frontend's disable
4904 * routine, to avoid lock order reversals.
4906 * XXX KDM what happens if the frontend list changes while
4907 * we're traversing it? It's unlikely, but should be handled.
4909 mtx_unlock(&softc->ctl_lock);
4910 retval = port->lun_disable(port->targ_lun_arg, lun->lun);
4911 mtx_lock(&softc->ctl_lock);
4913 printf("%s: FETD %s port %d returned error "
4914 "%d for lun_disable on lun %jd\n",
4915 __func__, port->port_name, port->targ_port,
4916 retval, (intmax_t)lun->lun);
4920 mtx_unlock(&softc->ctl_lock);
4921 ctl_isc_announce_lun(lun);
4927 ctl_start_lun(struct ctl_be_lun *be_lun)
4929 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4931 mtx_lock(&lun->lun_lock);
4932 lun->flags &= ~CTL_LUN_STOPPED;
4933 mtx_unlock(&lun->lun_lock);
4938 ctl_stop_lun(struct ctl_be_lun *be_lun)
4940 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4942 mtx_lock(&lun->lun_lock);
4943 lun->flags |= CTL_LUN_STOPPED;
4944 mtx_unlock(&lun->lun_lock);
4949 ctl_lun_no_media(struct ctl_be_lun *be_lun)
4951 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4953 mtx_lock(&lun->lun_lock);
4954 lun->flags |= CTL_LUN_NO_MEDIA;
4955 mtx_unlock(&lun->lun_lock);
4960 ctl_lun_has_media(struct ctl_be_lun *be_lun)
4962 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4963 union ctl_ha_msg msg;
4965 mtx_lock(&lun->lun_lock);
4966 lun->flags &= ~(CTL_LUN_NO_MEDIA | CTL_LUN_EJECTED);
4967 if (lun->flags & CTL_LUN_REMOVABLE)
4968 ctl_est_ua_all(lun, -1, CTL_UA_MEDIUM_CHANGE);
4969 mtx_unlock(&lun->lun_lock);
4970 if ((lun->flags & CTL_LUN_REMOVABLE) &&
4971 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
4972 bzero(&msg.ua, sizeof(msg.ua));
4973 msg.hdr.msg_type = CTL_MSG_UA;
4974 msg.hdr.nexus.initid = -1;
4975 msg.hdr.nexus.targ_port = -1;
4976 msg.hdr.nexus.targ_lun = lun->lun;
4977 msg.hdr.nexus.targ_mapped_lun = lun->lun;
4980 msg.ua.ua_type = CTL_UA_MEDIUM_CHANGE;
4981 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
4988 ctl_lun_ejected(struct ctl_be_lun *be_lun)
4990 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
4992 mtx_lock(&lun->lun_lock);
4993 lun->flags |= CTL_LUN_EJECTED;
4994 mtx_unlock(&lun->lun_lock);
4999 ctl_lun_primary(struct ctl_be_lun *be_lun)
5001 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5003 mtx_lock(&lun->lun_lock);
5004 lun->flags |= CTL_LUN_PRIMARY_SC;
5005 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5006 mtx_unlock(&lun->lun_lock);
5007 ctl_isc_announce_lun(lun);
5012 ctl_lun_secondary(struct ctl_be_lun *be_lun)
5014 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5016 mtx_lock(&lun->lun_lock);
5017 lun->flags &= ~CTL_LUN_PRIMARY_SC;
5018 ctl_est_ua_all(lun, -1, CTL_UA_ASYM_ACC_CHANGE);
5019 mtx_unlock(&lun->lun_lock);
5020 ctl_isc_announce_lun(lun);
5025 ctl_invalidate_lun(struct ctl_be_lun *be_lun)
5027 struct ctl_lun *lun;
5029 lun = (struct ctl_lun *)be_lun->ctl_lun;
5031 mtx_lock(&lun->lun_lock);
5034 * The LUN needs to be disabled before it can be marked invalid.
5036 if ((lun->flags & CTL_LUN_DISABLED) == 0) {
5037 mtx_unlock(&lun->lun_lock);
5041 * Mark the LUN invalid.
5043 lun->flags |= CTL_LUN_INVALID;
5046 * If there is nothing in the OOA queue, go ahead and free the LUN.
5047 * If we have something in the OOA queue, we'll free it when the
5048 * last I/O completes.
5050 if (TAILQ_EMPTY(&lun->ooa_queue)) {
5051 mtx_unlock(&lun->lun_lock);
5054 mtx_unlock(&lun->lun_lock);
5060 ctl_lun_capacity_changed(struct ctl_be_lun *be_lun)
5062 struct ctl_lun *lun = (struct ctl_lun *)be_lun->ctl_lun;
5063 union ctl_ha_msg msg;
5065 mtx_lock(&lun->lun_lock);
5066 ctl_est_ua_all(lun, -1, CTL_UA_CAPACITY_CHANGE);
5067 mtx_unlock(&lun->lun_lock);
5068 if (lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
5069 /* Send msg to other side. */
5070 bzero(&msg.ua, sizeof(msg.ua));
5071 msg.hdr.msg_type = CTL_MSG_UA;
5072 msg.hdr.nexus.initid = -1;
5073 msg.hdr.nexus.targ_port = -1;
5074 msg.hdr.nexus.targ_lun = lun->lun;
5075 msg.hdr.nexus.targ_mapped_lun = lun->lun;
5078 msg.ua.ua_type = CTL_UA_CAPACITY_CHANGE;
5079 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg, sizeof(msg.ua),
5085 * Backend "memory move is complete" callback for requests that never
5086 * make it down to say RAIDCore's configuration code.
5089 ctl_config_move_done(union ctl_io *io)
5093 CTL_DEBUG_PRINT(("ctl_config_move_done\n"));
5094 KASSERT(io->io_hdr.io_type == CTL_IO_SCSI,
5095 ("Config I/O type isn't CTL_IO_SCSI (%d)!", io->io_hdr.io_type));
5097 if ((io->io_hdr.port_status != 0) &&
5098 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5099 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5100 ctl_set_internal_failure(&io->scsiio, /*sks_valid*/ 1,
5101 /*retry_count*/ io->io_hdr.port_status);
5102 } else if (io->scsiio.kern_data_resid != 0 &&
5103 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT &&
5104 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5105 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5106 ctl_set_invalid_field_ciu(&io->scsiio);
5109 if (ctl_debug & CTL_DEBUG_CDB_DATA)
5111 if (((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN) ||
5112 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5113 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) ||
5114 ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0)) {
5116 * XXX KDM just assuming a single pointer here, and not a
5117 * S/G list. If we start using S/G lists for config data,
5118 * we'll need to know how to clean them up here as well.
5120 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5121 free(io->scsiio.kern_data_ptr, M_CTL);
5123 retval = CTL_RETVAL_COMPLETE;
5126 * XXX KDM now we need to continue data movement. Some
5128 * - call ctl_scsiio() again? We don't do this for data
5129 * writes, because for those at least we know ahead of
5130 * time where the write will go and how long it is. For
5131 * config writes, though, that information is largely
5132 * contained within the write itself, thus we need to
5133 * parse out the data again.
5135 * - Call some other function once the data is in?
5139 * XXX KDM call ctl_scsiio() again for now, and check flag
5140 * bits to see whether we're allocated or not.
5142 retval = ctl_scsiio(&io->scsiio);
5148 * This gets called by a backend driver when it is done with a
5149 * data_submit method.
5152 ctl_data_submit_done(union ctl_io *io)
5155 * If the IO_CONT flag is set, we need to call the supplied
5156 * function to continue processing the I/O, instead of completing
5159 * If there is an error, though, we don't want to keep processing.
5160 * Instead, just send status back to the initiator.
5162 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5163 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5164 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5165 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5166 io->scsiio.io_cont(io);
5173 * This gets called by a backend driver when it is done with a
5174 * configuration write.
5177 ctl_config_write_done(union ctl_io *io)
5182 * If the IO_CONT flag is set, we need to call the supplied
5183 * function to continue processing the I/O, instead of completing
5186 * If there is an error, though, we don't want to keep processing.
5187 * Instead, just send status back to the initiator.
5189 if ((io->io_hdr.flags & CTL_FLAG_IO_CONT) &&
5190 (io->io_hdr.flags & CTL_FLAG_ABORT) == 0 &&
5191 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_STATUS_NONE ||
5192 (io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)) {
5193 io->scsiio.io_cont(io);
5197 * Since a configuration write can be done for commands that actually
5198 * have data allocated, like write buffer, and commands that have
5199 * no data, like start/stop unit, we need to check here.
5201 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5202 buf = io->scsiio.kern_data_ptr;
5211 ctl_config_read_done(union ctl_io *io)
5216 * If there is some error -- we are done, skip data transfer.
5218 if ((io->io_hdr.flags & CTL_FLAG_ABORT) != 0 ||
5219 ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
5220 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)) {
5221 if (io->io_hdr.flags & CTL_FLAG_ALLOCATED)
5222 buf = io->scsiio.kern_data_ptr;
5232 * If the IO_CONT flag is set, we need to call the supplied
5233 * function to continue processing the I/O, instead of completing
5236 if (io->io_hdr.flags & CTL_FLAG_IO_CONT) {
5237 io->scsiio.io_cont(io);
5245 * SCSI release command.
5248 ctl_scsi_release(struct ctl_scsiio *ctsio)
5250 struct ctl_lun *lun = CTL_LUN(ctsio);
5253 CTL_DEBUG_PRINT(("ctl_scsi_release\n"));
5255 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5258 * XXX KDM right now, we only support LUN reservation. We don't
5259 * support 3rd party reservations, or extent reservations, which
5260 * might actually need the parameter list. If we've gotten this
5261 * far, we've got a LUN reservation. Anything else got kicked out
5262 * above. So, according to SPC, ignore the length.
5265 mtx_lock(&lun->lun_lock);
5268 * According to SPC, it is not an error for an intiator to attempt
5269 * to release a reservation on a LUN that isn't reserved, or that
5270 * is reserved by another initiator. The reservation can only be
5271 * released, though, by the initiator who made it or by one of
5272 * several reset type events.
5274 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == residx))
5275 lun->flags &= ~CTL_LUN_RESERVED;
5277 mtx_unlock(&lun->lun_lock);
5279 ctl_set_success(ctsio);
5280 ctl_done((union ctl_io *)ctsio);
5281 return (CTL_RETVAL_COMPLETE);
5285 ctl_scsi_reserve(struct ctl_scsiio *ctsio)
5287 struct ctl_lun *lun = CTL_LUN(ctsio);
5290 CTL_DEBUG_PRINT(("ctl_reserve\n"));
5292 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5295 * XXX KDM right now, we only support LUN reservation. We don't
5296 * support 3rd party reservations, or extent reservations, which
5297 * might actually need the parameter list. If we've gotten this
5298 * far, we've got a LUN reservation. Anything else got kicked out
5299 * above. So, according to SPC, ignore the length.
5302 mtx_lock(&lun->lun_lock);
5303 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx != residx)) {
5304 ctl_set_reservation_conflict(ctsio);
5308 /* SPC-3 exceptions to SPC-2 RESERVE and RELEASE behavior. */
5309 if (lun->flags & CTL_LUN_PR_RESERVED) {
5310 ctl_set_success(ctsio);
5314 lun->flags |= CTL_LUN_RESERVED;
5315 lun->res_idx = residx;
5316 ctl_set_success(ctsio);
5319 mtx_unlock(&lun->lun_lock);
5320 ctl_done((union ctl_io *)ctsio);
5321 return (CTL_RETVAL_COMPLETE);
5325 ctl_start_stop(struct ctl_scsiio *ctsio)
5327 struct ctl_lun *lun = CTL_LUN(ctsio);
5328 struct scsi_start_stop_unit *cdb;
5331 CTL_DEBUG_PRINT(("ctl_start_stop\n"));
5333 cdb = (struct scsi_start_stop_unit *)ctsio->cdb;
5335 if ((cdb->how & SSS_PC_MASK) == 0) {
5336 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
5337 (cdb->how & SSS_START) == 0) {
5340 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5341 if (ctl_get_prkey(lun, residx) == 0 ||
5342 (lun->pr_res_idx != residx && lun->pr_res_type < 4)) {
5344 ctl_set_reservation_conflict(ctsio);
5345 ctl_done((union ctl_io *)ctsio);
5346 return (CTL_RETVAL_COMPLETE);
5350 if ((cdb->how & SSS_LOEJ) &&
5351 (lun->flags & CTL_LUN_REMOVABLE) == 0) {
5352 ctl_set_invalid_field(ctsio,
5358 ctl_done((union ctl_io *)ctsio);
5359 return (CTL_RETVAL_COMPLETE);
5362 if ((cdb->how & SSS_START) == 0 && (cdb->how & SSS_LOEJ) &&
5363 lun->prevent_count > 0) {
5364 /* "Medium removal prevented" */
5365 ctl_set_sense(ctsio, /*current_error*/ 1,
5366 /*sense_key*/(lun->flags & CTL_LUN_NO_MEDIA) ?
5367 SSD_KEY_NOT_READY : SSD_KEY_ILLEGAL_REQUEST,
5368 /*asc*/ 0x53, /*ascq*/ 0x02, SSD_ELEM_NONE);
5369 ctl_done((union ctl_io *)ctsio);
5370 return (CTL_RETVAL_COMPLETE);
5374 retval = lun->backend->config_write((union ctl_io *)ctsio);
5379 ctl_prevent_allow(struct ctl_scsiio *ctsio)
5381 struct ctl_lun *lun = CTL_LUN(ctsio);
5382 struct scsi_prevent *cdb;
5386 CTL_DEBUG_PRINT(("ctl_prevent_allow\n"));
5388 cdb = (struct scsi_prevent *)ctsio->cdb;
5390 if ((lun->flags & CTL_LUN_REMOVABLE) == 0 || lun->prevent == NULL) {
5391 ctl_set_invalid_opcode(ctsio);
5392 ctl_done((union ctl_io *)ctsio);
5393 return (CTL_RETVAL_COMPLETE);
5396 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5397 mtx_lock(&lun->lun_lock);
5398 if ((cdb->how & PR_PREVENT) &&
5399 ctl_is_set(lun->prevent, initidx) == 0) {
5400 ctl_set_mask(lun->prevent, initidx);
5401 lun->prevent_count++;
5402 } else if ((cdb->how & PR_PREVENT) == 0 &&
5403 ctl_is_set(lun->prevent, initidx)) {
5404 ctl_clear_mask(lun->prevent, initidx);
5405 lun->prevent_count--;
5407 mtx_unlock(&lun->lun_lock);
5408 retval = lun->backend->config_write((union ctl_io *)ctsio);
5413 * We support the SYNCHRONIZE CACHE command (10 and 16 byte versions), but
5414 * we don't really do anything with the LBA and length fields if the user
5415 * passes them in. Instead we'll just flush out the cache for the entire
5419 ctl_sync_cache(struct ctl_scsiio *ctsio)
5421 struct ctl_lun *lun = CTL_LUN(ctsio);
5422 struct ctl_lba_len_flags *lbalen;
5423 uint64_t starting_lba;
5424 uint32_t block_count;
5428 CTL_DEBUG_PRINT(("ctl_sync_cache\n"));
5432 switch (ctsio->cdb[0]) {
5433 case SYNCHRONIZE_CACHE: {
5434 struct scsi_sync_cache *cdb;
5435 cdb = (struct scsi_sync_cache *)ctsio->cdb;
5437 starting_lba = scsi_4btoul(cdb->begin_lba);
5438 block_count = scsi_2btoul(cdb->lb_count);
5442 case SYNCHRONIZE_CACHE_16: {
5443 struct scsi_sync_cache_16 *cdb;
5444 cdb = (struct scsi_sync_cache_16 *)ctsio->cdb;
5446 starting_lba = scsi_8btou64(cdb->begin_lba);
5447 block_count = scsi_4btoul(cdb->lb_count);
5452 ctl_set_invalid_opcode(ctsio);
5453 ctl_done((union ctl_io *)ctsio);
5455 break; /* NOTREACHED */
5459 * We check the LBA and length, but don't do anything with them.
5460 * A SYNCHRONIZE CACHE will cause the entire cache for this lun to
5461 * get flushed. This check will just help satisfy anyone who wants
5462 * to see an error for an out of range LBA.
5464 if ((starting_lba + block_count) > (lun->be_lun->maxlba + 1)) {
5465 ctl_set_lba_out_of_range(ctsio,
5466 MAX(starting_lba, lun->be_lun->maxlba + 1));
5467 ctl_done((union ctl_io *)ctsio);
5471 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5472 lbalen->lba = starting_lba;
5473 lbalen->len = block_count;
5474 lbalen->flags = byte2;
5475 retval = lun->backend->config_write((union ctl_io *)ctsio);
5482 ctl_format(struct ctl_scsiio *ctsio)
5484 struct scsi_format *cdb;
5485 int length, defect_list_len;
5487 CTL_DEBUG_PRINT(("ctl_format\n"));
5489 cdb = (struct scsi_format *)ctsio->cdb;
5492 if (cdb->byte2 & SF_FMTDATA) {
5493 if (cdb->byte2 & SF_LONGLIST)
5494 length = sizeof(struct scsi_format_header_long);
5496 length = sizeof(struct scsi_format_header_short);
5499 if (((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0)
5501 ctsio->kern_data_ptr = malloc(length, M_CTL, M_WAITOK);
5502 ctsio->kern_data_len = length;
5503 ctsio->kern_total_len = length;
5504 ctsio->kern_rel_offset = 0;
5505 ctsio->kern_sg_entries = 0;
5506 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5507 ctsio->be_move_done = ctl_config_move_done;
5508 ctl_datamove((union ctl_io *)ctsio);
5510 return (CTL_RETVAL_COMPLETE);
5513 defect_list_len = 0;
5515 if (cdb->byte2 & SF_FMTDATA) {
5516 if (cdb->byte2 & SF_LONGLIST) {
5517 struct scsi_format_header_long *header;
5519 header = (struct scsi_format_header_long *)
5520 ctsio->kern_data_ptr;
5522 defect_list_len = scsi_4btoul(header->defect_list_len);
5523 if (defect_list_len != 0) {
5524 ctl_set_invalid_field(ctsio,
5533 struct scsi_format_header_short *header;
5535 header = (struct scsi_format_header_short *)
5536 ctsio->kern_data_ptr;
5538 defect_list_len = scsi_2btoul(header->defect_list_len);
5539 if (defect_list_len != 0) {
5540 ctl_set_invalid_field(ctsio,
5551 ctl_set_success(ctsio);
5554 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5555 free(ctsio->kern_data_ptr, M_CTL);
5556 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5559 ctl_done((union ctl_io *)ctsio);
5560 return (CTL_RETVAL_COMPLETE);
5564 ctl_read_buffer(struct ctl_scsiio *ctsio)
5566 struct ctl_lun *lun = CTL_LUN(ctsio);
5567 uint64_t buffer_offset;
5570 static uint8_t descr[4];
5571 static uint8_t echo_descr[4] = { 0 };
5573 CTL_DEBUG_PRINT(("ctl_read_buffer\n"));
5575 switch (ctsio->cdb[0]) {
5577 struct scsi_read_buffer *cdb;
5579 cdb = (struct scsi_read_buffer *)ctsio->cdb;
5580 buffer_offset = scsi_3btoul(cdb->offset);
5581 len = scsi_3btoul(cdb->length);
5585 case READ_BUFFER_16: {
5586 struct scsi_read_buffer_16 *cdb;
5588 cdb = (struct scsi_read_buffer_16 *)ctsio->cdb;
5589 buffer_offset = scsi_8btou64(cdb->offset);
5590 len = scsi_4btoul(cdb->length);
5594 default: /* This shouldn't happen. */
5595 ctl_set_invalid_opcode(ctsio);
5596 ctl_done((union ctl_io *)ctsio);
5597 return (CTL_RETVAL_COMPLETE);
5600 if (buffer_offset > CTL_WRITE_BUFFER_SIZE ||
5601 buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5602 ctl_set_invalid_field(ctsio,
5608 ctl_done((union ctl_io *)ctsio);
5609 return (CTL_RETVAL_COMPLETE);
5612 if ((byte2 & RWB_MODE) == RWB_MODE_DESCR) {
5614 scsi_ulto3b(CTL_WRITE_BUFFER_SIZE, &descr[1]);
5615 ctsio->kern_data_ptr = descr;
5616 len = min(len, sizeof(descr));
5617 } else if ((byte2 & RWB_MODE) == RWB_MODE_ECHO_DESCR) {
5618 ctsio->kern_data_ptr = echo_descr;
5619 len = min(len, sizeof(echo_descr));
5621 if (lun->write_buffer == NULL) {
5622 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5625 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5627 ctsio->kern_data_len = len;
5628 ctsio->kern_total_len = len;
5629 ctsio->kern_rel_offset = 0;
5630 ctsio->kern_sg_entries = 0;
5631 ctl_set_success(ctsio);
5632 ctsio->be_move_done = ctl_config_move_done;
5633 ctl_datamove((union ctl_io *)ctsio);
5634 return (CTL_RETVAL_COMPLETE);
5638 ctl_write_buffer(struct ctl_scsiio *ctsio)
5640 struct ctl_lun *lun = CTL_LUN(ctsio);
5641 struct scsi_write_buffer *cdb;
5642 int buffer_offset, len;
5644 CTL_DEBUG_PRINT(("ctl_write_buffer\n"));
5646 cdb = (struct scsi_write_buffer *)ctsio->cdb;
5648 len = scsi_3btoul(cdb->length);
5649 buffer_offset = scsi_3btoul(cdb->offset);
5651 if (buffer_offset + len > CTL_WRITE_BUFFER_SIZE) {
5652 ctl_set_invalid_field(ctsio,
5658 ctl_done((union ctl_io *)ctsio);
5659 return (CTL_RETVAL_COMPLETE);
5663 * If we've got a kernel request that hasn't been malloced yet,
5664 * malloc it and tell the caller the data buffer is here.
5666 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5667 if (lun->write_buffer == NULL) {
5668 lun->write_buffer = malloc(CTL_WRITE_BUFFER_SIZE,
5671 ctsio->kern_data_ptr = lun->write_buffer + buffer_offset;
5672 ctsio->kern_data_len = len;
5673 ctsio->kern_total_len = len;
5674 ctsio->kern_rel_offset = 0;
5675 ctsio->kern_sg_entries = 0;
5676 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5677 ctsio->be_move_done = ctl_config_move_done;
5678 ctl_datamove((union ctl_io *)ctsio);
5680 return (CTL_RETVAL_COMPLETE);
5683 ctl_set_success(ctsio);
5684 ctl_done((union ctl_io *)ctsio);
5685 return (CTL_RETVAL_COMPLETE);
5689 ctl_write_same(struct ctl_scsiio *ctsio)
5691 struct ctl_lun *lun = CTL_LUN(ctsio);
5692 struct ctl_lba_len_flags *lbalen;
5694 uint32_t num_blocks;
5698 CTL_DEBUG_PRINT(("ctl_write_same\n"));
5700 switch (ctsio->cdb[0]) {
5701 case WRITE_SAME_10: {
5702 struct scsi_write_same_10 *cdb;
5704 cdb = (struct scsi_write_same_10 *)ctsio->cdb;
5706 lba = scsi_4btoul(cdb->addr);
5707 num_blocks = scsi_2btoul(cdb->length);
5711 case WRITE_SAME_16: {
5712 struct scsi_write_same_16 *cdb;
5714 cdb = (struct scsi_write_same_16 *)ctsio->cdb;
5716 lba = scsi_8btou64(cdb->addr);
5717 num_blocks = scsi_4btoul(cdb->length);
5723 * We got a command we don't support. This shouldn't
5724 * happen, commands should be filtered out above us.
5726 ctl_set_invalid_opcode(ctsio);
5727 ctl_done((union ctl_io *)ctsio);
5729 return (CTL_RETVAL_COMPLETE);
5730 break; /* NOTREACHED */
5733 /* ANCHOR flag can be used only together with UNMAP */
5734 if ((byte2 & SWS_UNMAP) == 0 && (byte2 & SWS_ANCHOR) != 0) {
5735 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
5736 /*command*/ 1, /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
5737 ctl_done((union ctl_io *)ctsio);
5738 return (CTL_RETVAL_COMPLETE);
5742 * The first check is to make sure we're in bounds, the second
5743 * check is to catch wrap-around problems. If the lba + num blocks
5744 * is less than the lba, then we've wrapped around and the block
5745 * range is invalid anyway.
5747 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5748 || ((lba + num_blocks) < lba)) {
5749 ctl_set_lba_out_of_range(ctsio,
5750 MAX(lba, lun->be_lun->maxlba + 1));
5751 ctl_done((union ctl_io *)ctsio);
5752 return (CTL_RETVAL_COMPLETE);
5755 /* Zero number of blocks means "to the last logical block" */
5756 if (num_blocks == 0) {
5757 if ((lun->be_lun->maxlba + 1) - lba > UINT32_MAX) {
5758 ctl_set_invalid_field(ctsio,
5764 ctl_done((union ctl_io *)ctsio);
5765 return (CTL_RETVAL_COMPLETE);
5767 num_blocks = (lun->be_lun->maxlba + 1) - lba;
5770 len = lun->be_lun->blocksize;
5773 * If we've got a kernel request that hasn't been malloced yet,
5774 * malloc it and tell the caller the data buffer is here.
5776 if ((byte2 & SWS_NDOB) == 0 &&
5777 (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5778 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5779 ctsio->kern_data_len = len;
5780 ctsio->kern_total_len = len;
5781 ctsio->kern_rel_offset = 0;
5782 ctsio->kern_sg_entries = 0;
5783 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5784 ctsio->be_move_done = ctl_config_move_done;
5785 ctl_datamove((union ctl_io *)ctsio);
5787 return (CTL_RETVAL_COMPLETE);
5790 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5792 lbalen->len = num_blocks;
5793 lbalen->flags = byte2;
5794 retval = lun->backend->config_write((union ctl_io *)ctsio);
5800 ctl_unmap(struct ctl_scsiio *ctsio)
5802 struct ctl_lun *lun = CTL_LUN(ctsio);
5803 struct scsi_unmap *cdb;
5804 struct ctl_ptr_len_flags *ptrlen;
5805 struct scsi_unmap_header *hdr;
5806 struct scsi_unmap_desc *buf, *end, *endnz, *range;
5808 uint32_t num_blocks;
5812 CTL_DEBUG_PRINT(("ctl_unmap\n"));
5814 cdb = (struct scsi_unmap *)ctsio->cdb;
5815 len = scsi_2btoul(cdb->length);
5819 * If we've got a kernel request that hasn't been malloced yet,
5820 * malloc it and tell the caller the data buffer is here.
5822 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
5823 ctsio->kern_data_ptr = malloc(len, M_CTL, M_WAITOK);
5824 ctsio->kern_data_len = len;
5825 ctsio->kern_total_len = len;
5826 ctsio->kern_rel_offset = 0;
5827 ctsio->kern_sg_entries = 0;
5828 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
5829 ctsio->be_move_done = ctl_config_move_done;
5830 ctl_datamove((union ctl_io *)ctsio);
5832 return (CTL_RETVAL_COMPLETE);
5835 len = ctsio->kern_total_len - ctsio->kern_data_resid;
5836 hdr = (struct scsi_unmap_header *)ctsio->kern_data_ptr;
5837 if (len < sizeof (*hdr) ||
5838 len < (scsi_2btoul(hdr->length) + sizeof(hdr->length)) ||
5839 len < (scsi_2btoul(hdr->desc_length) + sizeof (*hdr)) ||
5840 scsi_2btoul(hdr->desc_length) % sizeof(*buf) != 0) {
5841 ctl_set_invalid_field(ctsio,
5849 len = scsi_2btoul(hdr->desc_length);
5850 buf = (struct scsi_unmap_desc *)(hdr + 1);
5851 end = buf + len / sizeof(*buf);
5854 for (range = buf; range < end; range++) {
5855 lba = scsi_8btou64(range->lba);
5856 num_blocks = scsi_4btoul(range->length);
5857 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
5858 || ((lba + num_blocks) < lba)) {
5859 ctl_set_lba_out_of_range(ctsio,
5860 MAX(lba, lun->be_lun->maxlba + 1));
5861 ctl_done((union ctl_io *)ctsio);
5862 return (CTL_RETVAL_COMPLETE);
5864 if (num_blocks != 0)
5869 * Block backend can not handle zero last range.
5870 * Filter it out and return if there is nothing left.
5872 len = (uint8_t *)endnz - (uint8_t *)buf;
5874 ctl_set_success(ctsio);
5878 mtx_lock(&lun->lun_lock);
5879 ptrlen = (struct ctl_ptr_len_flags *)
5880 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
5881 ptrlen->ptr = (void *)buf;
5883 ptrlen->flags = byte2;
5884 ctl_try_unblock_others(lun, (union ctl_io *)ctsio, FALSE);
5885 mtx_unlock(&lun->lun_lock);
5887 retval = lun->backend->config_write((union ctl_io *)ctsio);
5891 if (ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) {
5892 free(ctsio->kern_data_ptr, M_CTL);
5893 ctsio->io_hdr.flags &= ~CTL_FLAG_ALLOCATED;
5895 ctl_done((union ctl_io *)ctsio);
5896 return (CTL_RETVAL_COMPLETE);
5900 ctl_default_page_handler(struct ctl_scsiio *ctsio,
5901 struct ctl_page_index *page_index, uint8_t *page_ptr)
5903 struct ctl_lun *lun = CTL_LUN(ctsio);
5904 uint8_t *current_cp;
5908 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
5911 current_cp = (page_index->page_data + (page_index->page_len *
5914 mtx_lock(&lun->lun_lock);
5915 if (memcmp(current_cp, page_ptr, page_index->page_len)) {
5916 memcpy(current_cp, page_ptr, page_index->page_len);
5920 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
5921 mtx_unlock(&lun->lun_lock);
5923 ctl_isc_announce_mode(lun,
5924 ctl_get_initindex(&ctsio->io_hdr.nexus),
5925 page_index->page_code, page_index->subpage);
5927 return (CTL_RETVAL_COMPLETE);
5931 ctl_ie_timer(void *arg)
5933 struct ctl_lun *lun = arg;
5936 if (lun->ie_asc == 0)
5939 if (lun->MODE_IE.mrie == SIEP_MRIE_UA)
5940 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5942 lun->ie_reported = 0;
5944 if (lun->ie_reportcnt < scsi_4btoul(lun->MODE_IE.report_count)) {
5945 lun->ie_reportcnt++;
5946 t = scsi_4btoul(lun->MODE_IE.interval_timer);
5947 if (t == 0 || t == UINT32_MAX)
5948 t = 3000; /* 5 min */
5949 callout_schedule(&lun->ie_callout, t * hz / 10);
5954 ctl_ie_page_handler(struct ctl_scsiio *ctsio,
5955 struct ctl_page_index *page_index, uint8_t *page_ptr)
5957 struct ctl_lun *lun = CTL_LUN(ctsio);
5958 struct scsi_info_exceptions_page *pg;
5961 (void)ctl_default_page_handler(ctsio, page_index, page_ptr);
5963 pg = (struct scsi_info_exceptions_page *)page_ptr;
5964 mtx_lock(&lun->lun_lock);
5965 if (pg->info_flags & SIEP_FLAGS_TEST) {
5967 lun->ie_ascq = 0xff;
5968 if (pg->mrie == SIEP_MRIE_UA) {
5969 ctl_est_ua_all(lun, -1, CTL_UA_IE);
5970 lun->ie_reported = 1;
5972 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5973 lun->ie_reported = -1;
5975 lun->ie_reportcnt = 1;
5976 if (lun->ie_reportcnt < scsi_4btoul(pg->report_count)) {
5977 lun->ie_reportcnt++;
5978 t = scsi_4btoul(pg->interval_timer);
5979 if (t == 0 || t == UINT32_MAX)
5980 t = 3000; /* 5 min */
5981 callout_reset(&lun->ie_callout, t * hz / 10,
5987 lun->ie_reported = 1;
5988 ctl_clr_ua_all(lun, -1, CTL_UA_IE);
5989 lun->ie_reportcnt = UINT32_MAX;
5990 callout_stop(&lun->ie_callout);
5992 mtx_unlock(&lun->lun_lock);
5993 return (CTL_RETVAL_COMPLETE);
5997 ctl_do_mode_select(union ctl_io *io)
5999 struct ctl_lun *lun = CTL_LUN(io);
6000 struct scsi_mode_page_header *page_header;
6001 struct ctl_page_index *page_index;
6002 struct ctl_scsiio *ctsio;
6003 int page_len, page_len_offset, page_len_size;
6004 union ctl_modepage_info *modepage_info;
6005 uint16_t *len_left, *len_used;
6008 ctsio = &io->scsiio;
6012 modepage_info = (union ctl_modepage_info *)
6013 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6014 len_left = &modepage_info->header.len_left;
6015 len_used = &modepage_info->header.len_used;
6019 page_header = (struct scsi_mode_page_header *)
6020 (ctsio->kern_data_ptr + *len_used);
6022 if (*len_left == 0) {
6023 free(ctsio->kern_data_ptr, M_CTL);
6024 ctl_set_success(ctsio);
6025 ctl_done((union ctl_io *)ctsio);
6026 return (CTL_RETVAL_COMPLETE);
6027 } else if (*len_left < sizeof(struct scsi_mode_page_header)) {
6029 free(ctsio->kern_data_ptr, M_CTL);
6030 ctl_set_param_len_error(ctsio);
6031 ctl_done((union ctl_io *)ctsio);
6032 return (CTL_RETVAL_COMPLETE);
6034 } else if ((page_header->page_code & SMPH_SPF)
6035 && (*len_left < sizeof(struct scsi_mode_page_header_sp))) {
6037 free(ctsio->kern_data_ptr, M_CTL);
6038 ctl_set_param_len_error(ctsio);
6039 ctl_done((union ctl_io *)ctsio);
6040 return (CTL_RETVAL_COMPLETE);
6045 * XXX KDM should we do something with the block descriptor?
6047 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6048 page_index = &lun->mode_pages.index[i];
6049 if (lun->be_lun->lun_type == T_DIRECT &&
6050 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6052 if (lun->be_lun->lun_type == T_PROCESSOR &&
6053 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6055 if (lun->be_lun->lun_type == T_CDROM &&
6056 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6059 if ((page_index->page_code & SMPH_PC_MASK) !=
6060 (page_header->page_code & SMPH_PC_MASK))
6064 * If neither page has a subpage code, then we've got a
6067 if (((page_index->page_code & SMPH_SPF) == 0)
6068 && ((page_header->page_code & SMPH_SPF) == 0)) {
6069 page_len = page_header->page_length;
6074 * If both pages have subpages, then the subpage numbers
6077 if ((page_index->page_code & SMPH_SPF)
6078 && (page_header->page_code & SMPH_SPF)) {
6079 struct scsi_mode_page_header_sp *sph;
6081 sph = (struct scsi_mode_page_header_sp *)page_header;
6082 if (page_index->subpage == sph->subpage) {
6083 page_len = scsi_2btoul(sph->page_length);
6090 * If we couldn't find the page, or if we don't have a mode select
6091 * handler for it, send back an error to the user.
6093 if ((i >= CTL_NUM_MODE_PAGES)
6094 || (page_index->select_handler == NULL)) {
6095 ctl_set_invalid_field(ctsio,
6098 /*field*/ *len_used,
6101 free(ctsio->kern_data_ptr, M_CTL);
6102 ctl_done((union ctl_io *)ctsio);
6103 return (CTL_RETVAL_COMPLETE);
6106 if (page_index->page_code & SMPH_SPF) {
6107 page_len_offset = 2;
6111 page_len_offset = 1;
6115 * If the length the initiator gives us isn't the one we specify in
6116 * the mode page header, or if they didn't specify enough data in
6117 * the CDB to avoid truncating this page, kick out the request.
6119 if (page_len != page_index->page_len - page_len_offset - page_len_size) {
6120 ctl_set_invalid_field(ctsio,
6123 /*field*/ *len_used + page_len_offset,
6126 free(ctsio->kern_data_ptr, M_CTL);
6127 ctl_done((union ctl_io *)ctsio);
6128 return (CTL_RETVAL_COMPLETE);
6130 if (*len_left < page_index->page_len) {
6131 free(ctsio->kern_data_ptr, M_CTL);
6132 ctl_set_param_len_error(ctsio);
6133 ctl_done((union ctl_io *)ctsio);
6134 return (CTL_RETVAL_COMPLETE);
6138 * Run through the mode page, checking to make sure that the bits
6139 * the user changed are actually legal for him to change.
6141 for (i = 0; i < page_index->page_len; i++) {
6142 uint8_t *user_byte, *change_mask, *current_byte;
6146 user_byte = (uint8_t *)page_header + i;
6147 change_mask = page_index->page_data +
6148 (page_index->page_len * CTL_PAGE_CHANGEABLE) + i;
6149 current_byte = page_index->page_data +
6150 (page_index->page_len * CTL_PAGE_CURRENT) + i;
6153 * Check to see whether the user set any bits in this byte
6154 * that he is not allowed to set.
6156 if ((*user_byte & ~(*change_mask)) ==
6157 (*current_byte & ~(*change_mask)))
6161 * Go through bit by bit to determine which one is illegal.
6164 for (j = 7; j >= 0; j--) {
6165 if ((((1 << i) & ~(*change_mask)) & *user_byte) !=
6166 (((1 << i) & ~(*change_mask)) & *current_byte)) {
6171 ctl_set_invalid_field(ctsio,
6174 /*field*/ *len_used + i,
6177 free(ctsio->kern_data_ptr, M_CTL);
6178 ctl_done((union ctl_io *)ctsio);
6179 return (CTL_RETVAL_COMPLETE);
6183 * Decrement these before we call the page handler, since we may
6184 * end up getting called back one way or another before the handler
6185 * returns to this context.
6187 *len_left -= page_index->page_len;
6188 *len_used += page_index->page_len;
6190 retval = page_index->select_handler(ctsio, page_index,
6191 (uint8_t *)page_header);
6194 * If the page handler returns CTL_RETVAL_QUEUED, then we need to
6195 * wait until this queued command completes to finish processing
6196 * the mode page. If it returns anything other than
6197 * CTL_RETVAL_COMPLETE (e.g. CTL_RETVAL_ERROR), then it should have
6198 * already set the sense information, freed the data pointer, and
6199 * completed the io for us.
6201 if (retval != CTL_RETVAL_COMPLETE)
6202 goto bailout_no_done;
6205 * If the initiator sent us more than one page, parse the next one.
6210 ctl_set_success(ctsio);
6211 free(ctsio->kern_data_ptr, M_CTL);
6212 ctl_done((union ctl_io *)ctsio);
6216 return (CTL_RETVAL_COMPLETE);
6221 ctl_mode_select(struct ctl_scsiio *ctsio)
6223 struct ctl_lun *lun = CTL_LUN(ctsio);
6224 union ctl_modepage_info *modepage_info;
6225 int bd_len, i, header_size, param_len, rtd;
6228 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
6229 switch (ctsio->cdb[0]) {
6230 case MODE_SELECT_6: {
6231 struct scsi_mode_select_6 *cdb;
6233 cdb = (struct scsi_mode_select_6 *)ctsio->cdb;
6235 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6236 param_len = cdb->length;
6237 header_size = sizeof(struct scsi_mode_header_6);
6240 case MODE_SELECT_10: {
6241 struct scsi_mode_select_10 *cdb;
6243 cdb = (struct scsi_mode_select_10 *)ctsio->cdb;
6245 rtd = (cdb->byte2 & SMS_RTD) ? 1 : 0;
6246 param_len = scsi_2btoul(cdb->length);
6247 header_size = sizeof(struct scsi_mode_header_10);
6251 ctl_set_invalid_opcode(ctsio);
6252 ctl_done((union ctl_io *)ctsio);
6253 return (CTL_RETVAL_COMPLETE);
6257 if (param_len != 0) {
6258 ctl_set_invalid_field(ctsio, /*sks_valid*/ 0,
6259 /*command*/ 1, /*field*/ 0,
6260 /*bit_valid*/ 0, /*bit*/ 0);
6261 ctl_done((union ctl_io *)ctsio);
6262 return (CTL_RETVAL_COMPLETE);
6265 /* Revert to defaults. */
6266 ctl_init_page_index(lun);
6267 mtx_lock(&lun->lun_lock);
6268 ctl_est_ua_all(lun, initidx, CTL_UA_MODE_CHANGE);
6269 mtx_unlock(&lun->lun_lock);
6270 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6271 ctl_isc_announce_mode(lun, -1,
6272 lun->mode_pages.index[i].page_code & SMPH_PC_MASK,
6273 lun->mode_pages.index[i].subpage);
6275 ctl_set_success(ctsio);
6276 ctl_done((union ctl_io *)ctsio);
6277 return (CTL_RETVAL_COMPLETE);
6282 * "A parameter list length of zero indicates that the Data-Out Buffer
6283 * shall be empty. This condition shall not be considered as an error."
6285 if (param_len == 0) {
6286 ctl_set_success(ctsio);
6287 ctl_done((union ctl_io *)ctsio);
6288 return (CTL_RETVAL_COMPLETE);
6292 * Since we'll hit this the first time through, prior to
6293 * allocation, we don't need to free a data buffer here.
6295 if (param_len < header_size) {
6296 ctl_set_param_len_error(ctsio);
6297 ctl_done((union ctl_io *)ctsio);
6298 return (CTL_RETVAL_COMPLETE);
6302 * Allocate the data buffer and grab the user's data. In theory,
6303 * we shouldn't have to sanity check the parameter list length here
6304 * because the maximum size is 64K. We should be able to malloc
6305 * that much without too many problems.
6307 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
6308 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
6309 ctsio->kern_data_len = param_len;
6310 ctsio->kern_total_len = param_len;
6311 ctsio->kern_rel_offset = 0;
6312 ctsio->kern_sg_entries = 0;
6313 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6314 ctsio->be_move_done = ctl_config_move_done;
6315 ctl_datamove((union ctl_io *)ctsio);
6317 return (CTL_RETVAL_COMPLETE);
6320 switch (ctsio->cdb[0]) {
6321 case MODE_SELECT_6: {
6322 struct scsi_mode_header_6 *mh6;
6324 mh6 = (struct scsi_mode_header_6 *)ctsio->kern_data_ptr;
6325 bd_len = mh6->blk_desc_len;
6328 case MODE_SELECT_10: {
6329 struct scsi_mode_header_10 *mh10;
6331 mh10 = (struct scsi_mode_header_10 *)ctsio->kern_data_ptr;
6332 bd_len = scsi_2btoul(mh10->blk_desc_len);
6336 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6339 if (param_len < (header_size + bd_len)) {
6340 free(ctsio->kern_data_ptr, M_CTL);
6341 ctl_set_param_len_error(ctsio);
6342 ctl_done((union ctl_io *)ctsio);
6343 return (CTL_RETVAL_COMPLETE);
6347 * Set the IO_CONT flag, so that if this I/O gets passed to
6348 * ctl_config_write_done(), it'll get passed back to
6349 * ctl_do_mode_select() for further processing, or completion if
6352 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
6353 ctsio->io_cont = ctl_do_mode_select;
6355 modepage_info = (union ctl_modepage_info *)
6356 ctsio->io_hdr.ctl_private[CTL_PRIV_MODEPAGE].bytes;
6357 memset(modepage_info, 0, sizeof(*modepage_info));
6358 modepage_info->header.len_left = param_len - header_size - bd_len;
6359 modepage_info->header.len_used = header_size + bd_len;
6361 return (ctl_do_mode_select((union ctl_io *)ctsio));
6365 ctl_mode_sense(struct ctl_scsiio *ctsio)
6367 struct ctl_lun *lun = CTL_LUN(ctsio);
6368 int pc, page_code, dbd, subpage;
6369 int alloc_len, page_len, header_len, total_len;
6370 struct scsi_mode_block_descr *block_desc;
6371 struct ctl_page_index *page_index;
6376 CTL_DEBUG_PRINT(("ctl_mode_sense\n"));
6378 switch (ctsio->cdb[0]) {
6379 case MODE_SENSE_6: {
6380 struct scsi_mode_sense_6 *cdb;
6382 cdb = (struct scsi_mode_sense_6 *)ctsio->cdb;
6384 header_len = sizeof(struct scsi_mode_hdr_6);
6385 if (cdb->byte2 & SMS_DBD)
6388 header_len += sizeof(struct scsi_mode_block_descr);
6390 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6391 page_code = cdb->page & SMS_PAGE_CODE;
6392 subpage = cdb->subpage;
6393 alloc_len = cdb->length;
6396 case MODE_SENSE_10: {
6397 struct scsi_mode_sense_10 *cdb;
6399 cdb = (struct scsi_mode_sense_10 *)ctsio->cdb;
6401 header_len = sizeof(struct scsi_mode_hdr_10);
6403 if (cdb->byte2 & SMS_DBD)
6406 header_len += sizeof(struct scsi_mode_block_descr);
6407 pc = (cdb->page & SMS_PAGE_CTRL_MASK) >> 6;
6408 page_code = cdb->page & SMS_PAGE_CODE;
6409 subpage = cdb->subpage;
6410 alloc_len = scsi_2btoul(cdb->length);
6414 ctl_set_invalid_opcode(ctsio);
6415 ctl_done((union ctl_io *)ctsio);
6416 return (CTL_RETVAL_COMPLETE);
6417 break; /* NOTREACHED */
6421 * We have to make a first pass through to calculate the size of
6422 * the pages that match the user's query. Then we allocate enough
6423 * memory to hold it, and actually copy the data into the buffer.
6425 switch (page_code) {
6426 case SMS_ALL_PAGES_PAGE: {
6432 * At the moment, values other than 0 and 0xff here are
6433 * reserved according to SPC-3.
6435 if ((subpage != SMS_SUBPAGE_PAGE_0)
6436 && (subpage != SMS_SUBPAGE_ALL)) {
6437 ctl_set_invalid_field(ctsio,
6443 ctl_done((union ctl_io *)ctsio);
6444 return (CTL_RETVAL_COMPLETE);
6447 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6448 page_index = &lun->mode_pages.index[i];
6450 /* Make sure the page is supported for this dev type */
6451 if (lun->be_lun->lun_type == T_DIRECT &&
6452 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6454 if (lun->be_lun->lun_type == T_PROCESSOR &&
6455 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6457 if (lun->be_lun->lun_type == T_CDROM &&
6458 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6462 * We don't use this subpage if the user didn't
6463 * request all subpages.
6465 if ((page_index->subpage != 0)
6466 && (subpage == SMS_SUBPAGE_PAGE_0))
6469 page_len += page_index->page_len;
6478 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6479 page_index = &lun->mode_pages.index[i];
6481 /* Make sure the page is supported for this dev type */
6482 if (lun->be_lun->lun_type == T_DIRECT &&
6483 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6485 if (lun->be_lun->lun_type == T_PROCESSOR &&
6486 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6488 if (lun->be_lun->lun_type == T_CDROM &&
6489 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6492 /* Look for the right page code */
6493 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6496 /* Look for the right subpage or the subpage wildcard*/
6497 if ((page_index->subpage != subpage)
6498 && (subpage != SMS_SUBPAGE_ALL))
6501 page_len += page_index->page_len;
6504 if (page_len == 0) {
6505 ctl_set_invalid_field(ctsio,
6511 ctl_done((union ctl_io *)ctsio);
6512 return (CTL_RETVAL_COMPLETE);
6518 total_len = header_len + page_len;
6520 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6521 ctsio->kern_sg_entries = 0;
6522 ctsio->kern_rel_offset = 0;
6523 ctsio->kern_data_len = min(total_len, alloc_len);
6524 ctsio->kern_total_len = ctsio->kern_data_len;
6526 switch (ctsio->cdb[0]) {
6527 case MODE_SENSE_6: {
6528 struct scsi_mode_hdr_6 *header;
6530 header = (struct scsi_mode_hdr_6 *)ctsio->kern_data_ptr;
6532 header->datalen = MIN(total_len - 1, 254);
6533 if (lun->be_lun->lun_type == T_DIRECT) {
6534 header->dev_specific = 0x10; /* DPOFUA */
6535 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6536 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6537 header->dev_specific |= 0x80; /* WP */
6540 header->block_descr_len = 0;
6542 header->block_descr_len =
6543 sizeof(struct scsi_mode_block_descr);
6544 block_desc = (struct scsi_mode_block_descr *)&header[1];
6547 case MODE_SENSE_10: {
6548 struct scsi_mode_hdr_10 *header;
6551 header = (struct scsi_mode_hdr_10 *)ctsio->kern_data_ptr;
6553 datalen = MIN(total_len - 2, 65533);
6554 scsi_ulto2b(datalen, header->datalen);
6555 if (lun->be_lun->lun_type == T_DIRECT) {
6556 header->dev_specific = 0x10; /* DPOFUA */
6557 if ((lun->be_lun->flags & CTL_LUN_FLAG_READONLY) ||
6558 (lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0)
6559 header->dev_specific |= 0x80; /* WP */
6562 scsi_ulto2b(0, header->block_descr_len);
6564 scsi_ulto2b(sizeof(struct scsi_mode_block_descr),
6565 header->block_descr_len);
6566 block_desc = (struct scsi_mode_block_descr *)&header[1];
6570 panic("%s: Invalid CDB type %#x", __func__, ctsio->cdb[0]);
6574 * If we've got a disk, use its blocksize in the block
6575 * descriptor. Otherwise, just set it to 0.
6578 if (lun->be_lun->lun_type == T_DIRECT)
6579 scsi_ulto3b(lun->be_lun->blocksize,
6580 block_desc->block_len);
6582 scsi_ulto3b(0, block_desc->block_len);
6585 switch (page_code) {
6586 case SMS_ALL_PAGES_PAGE: {
6589 data_used = header_len;
6590 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6591 struct ctl_page_index *page_index;
6593 page_index = &lun->mode_pages.index[i];
6594 if (lun->be_lun->lun_type == T_DIRECT &&
6595 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6597 if (lun->be_lun->lun_type == T_PROCESSOR &&
6598 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6600 if (lun->be_lun->lun_type == T_CDROM &&
6601 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6605 * We don't use this subpage if the user didn't
6606 * request all subpages. We already checked (above)
6607 * to make sure the user only specified a subpage
6608 * of 0 or 0xff in the SMS_ALL_PAGES_PAGE case.
6610 if ((page_index->subpage != 0)
6611 && (subpage == SMS_SUBPAGE_PAGE_0))
6615 * Call the handler, if it exists, to update the
6616 * page to the latest values.
6618 if (page_index->sense_handler != NULL)
6619 page_index->sense_handler(ctsio, page_index,pc);
6621 memcpy(ctsio->kern_data_ptr + data_used,
6622 page_index->page_data +
6623 (page_index->page_len * pc),
6624 page_index->page_len);
6625 data_used += page_index->page_len;
6632 data_used = header_len;
6634 for (i = 0; i < CTL_NUM_MODE_PAGES; i++) {
6635 struct ctl_page_index *page_index;
6637 page_index = &lun->mode_pages.index[i];
6639 /* Look for the right page code */
6640 if ((page_index->page_code & SMPH_PC_MASK) != page_code)
6643 /* Look for the right subpage or the subpage wildcard*/
6644 if ((page_index->subpage != subpage)
6645 && (subpage != SMS_SUBPAGE_ALL))
6648 /* Make sure the page is supported for this dev type */
6649 if (lun->be_lun->lun_type == T_DIRECT &&
6650 (page_index->page_flags & CTL_PAGE_FLAG_DIRECT) == 0)
6652 if (lun->be_lun->lun_type == T_PROCESSOR &&
6653 (page_index->page_flags & CTL_PAGE_FLAG_PROC) == 0)
6655 if (lun->be_lun->lun_type == T_CDROM &&
6656 (page_index->page_flags & CTL_PAGE_FLAG_CDROM) == 0)
6660 * Call the handler, if it exists, to update the
6661 * page to the latest values.
6663 if (page_index->sense_handler != NULL)
6664 page_index->sense_handler(ctsio, page_index,pc);
6666 memcpy(ctsio->kern_data_ptr + data_used,
6667 page_index->page_data +
6668 (page_index->page_len * pc),
6669 page_index->page_len);
6670 data_used += page_index->page_len;
6676 ctl_set_success(ctsio);
6677 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6678 ctsio->be_move_done = ctl_config_move_done;
6679 ctl_datamove((union ctl_io *)ctsio);
6680 return (CTL_RETVAL_COMPLETE);
6684 ctl_temp_log_sense_handler(struct ctl_scsiio *ctsio,
6685 struct ctl_page_index *page_index,
6688 struct ctl_lun *lun = CTL_LUN(ctsio);
6689 struct scsi_log_temperature *data;
6692 data = (struct scsi_log_temperature *)page_index->page_data;
6694 scsi_ulto2b(SLP_TEMPERATURE, data->hdr.param_code);
6695 data->hdr.param_control = SLP_LBIN;
6696 data->hdr.param_len = sizeof(struct scsi_log_temperature) -
6697 sizeof(struct scsi_log_param_header);
6698 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature",
6700 data->temperature = strtol(value, NULL, 0);
6702 data->temperature = 0xff;
6705 scsi_ulto2b(SLP_REFTEMPERATURE, data->hdr.param_code);
6706 data->hdr.param_control = SLP_LBIN;
6707 data->hdr.param_len = sizeof(struct scsi_log_temperature) -
6708 sizeof(struct scsi_log_param_header);
6709 if ((value = dnvlist_get_string(lun->be_lun->options, "reftemperature",
6711 data->temperature = strtol(value, NULL, 0);
6713 data->temperature = 0xff;
6718 ctl_lbp_log_sense_handler(struct ctl_scsiio *ctsio,
6719 struct ctl_page_index *page_index,
6722 struct ctl_lun *lun = CTL_LUN(ctsio);
6723 struct scsi_log_param_header *phdr;
6727 data = page_index->page_data;
6729 if (lun->backend->lun_attr != NULL &&
6730 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksavail"))
6732 phdr = (struct scsi_log_param_header *)data;
6733 scsi_ulto2b(0x0001, phdr->param_code);
6734 phdr->param_control = SLP_LBIN | SLP_LP;
6735 phdr->param_len = 8;
6736 data = (uint8_t *)(phdr + 1);
6737 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6738 data[4] = 0x02; /* per-pool */
6739 data += phdr->param_len;
6742 if (lun->backend->lun_attr != NULL &&
6743 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "blocksused"))
6745 phdr = (struct scsi_log_param_header *)data;
6746 scsi_ulto2b(0x0002, phdr->param_code);
6747 phdr->param_control = SLP_LBIN | SLP_LP;
6748 phdr->param_len = 8;
6749 data = (uint8_t *)(phdr + 1);
6750 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6751 data[4] = 0x01; /* per-LUN */
6752 data += phdr->param_len;
6755 if (lun->backend->lun_attr != NULL &&
6756 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksavail"))
6758 phdr = (struct scsi_log_param_header *)data;
6759 scsi_ulto2b(0x00f1, phdr->param_code);
6760 phdr->param_control = SLP_LBIN | SLP_LP;
6761 phdr->param_len = 8;
6762 data = (uint8_t *)(phdr + 1);
6763 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6764 data[4] = 0x02; /* per-pool */
6765 data += phdr->param_len;
6768 if (lun->backend->lun_attr != NULL &&
6769 (val = lun->backend->lun_attr(lun->be_lun->be_lun, "poolblocksused"))
6771 phdr = (struct scsi_log_param_header *)data;
6772 scsi_ulto2b(0x00f2, phdr->param_code);
6773 phdr->param_control = SLP_LBIN | SLP_LP;
6774 phdr->param_len = 8;
6775 data = (uint8_t *)(phdr + 1);
6776 scsi_ulto4b(val >> CTL_LBP_EXPONENT, data);
6777 data[4] = 0x02; /* per-pool */
6778 data += phdr->param_len;
6781 page_index->page_len = data - page_index->page_data;
6786 ctl_sap_log_sense_handler(struct ctl_scsiio *ctsio,
6787 struct ctl_page_index *page_index,
6790 struct ctl_lun *lun = CTL_LUN(ctsio);
6791 struct stat_page *data;
6794 data = (struct stat_page *)page_index->page_data;
6796 scsi_ulto2b(SLP_SAP, data->sap.hdr.param_code);
6797 data->sap.hdr.param_control = SLP_LBIN;
6798 data->sap.hdr.param_len = sizeof(struct scsi_log_stat_and_perf) -
6799 sizeof(struct scsi_log_param_header);
6800 scsi_u64to8b(lun->stats.operations[CTL_STATS_READ],
6801 data->sap.read_num);
6802 scsi_u64to8b(lun->stats.operations[CTL_STATS_WRITE],
6803 data->sap.write_num);
6804 if (lun->be_lun->blocksize > 0) {
6805 scsi_u64to8b(lun->stats.bytes[CTL_STATS_WRITE] /
6806 lun->be_lun->blocksize, data->sap.recvieved_lba);
6807 scsi_u64to8b(lun->stats.bytes[CTL_STATS_READ] /
6808 lun->be_lun->blocksize, data->sap.transmitted_lba);
6810 t = &lun->stats.time[CTL_STATS_READ];
6811 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6812 data->sap.read_int);
6813 t = &lun->stats.time[CTL_STATS_WRITE];
6814 scsi_u64to8b((uint64_t)t->sec * 1000 + t->frac / (UINT64_MAX / 1000),
6815 data->sap.write_int);
6816 scsi_u64to8b(0, data->sap.weighted_num);
6817 scsi_u64to8b(0, data->sap.weighted_int);
6818 scsi_ulto2b(SLP_IT, data->it.hdr.param_code);
6819 data->it.hdr.param_control = SLP_LBIN;
6820 data->it.hdr.param_len = sizeof(struct scsi_log_idle_time) -
6821 sizeof(struct scsi_log_param_header);
6823 scsi_u64to8b(lun->idle_time / SBT_1MS, data->it.idle_int);
6825 scsi_ulto2b(SLP_TI, data->ti.hdr.param_code);
6826 data->it.hdr.param_control = SLP_LBIN;
6827 data->ti.hdr.param_len = sizeof(struct scsi_log_time_interval) -
6828 sizeof(struct scsi_log_param_header);
6829 scsi_ulto4b(3, data->ti.exponent);
6830 scsi_ulto4b(1, data->ti.integer);
6835 ctl_ie_log_sense_handler(struct ctl_scsiio *ctsio,
6836 struct ctl_page_index *page_index,
6839 struct ctl_lun *lun = CTL_LUN(ctsio);
6840 struct scsi_log_informational_exceptions *data;
6843 data = (struct scsi_log_informational_exceptions *)page_index->page_data;
6845 scsi_ulto2b(SLP_IE_GEN, data->hdr.param_code);
6846 data->hdr.param_control = SLP_LBIN;
6847 data->hdr.param_len = sizeof(struct scsi_log_informational_exceptions) -
6848 sizeof(struct scsi_log_param_header);
6849 data->ie_asc = lun->ie_asc;
6850 data->ie_ascq = lun->ie_ascq;
6851 if ((value = dnvlist_get_string(lun->be_lun->options, "temperature",
6853 data->temperature = strtol(value, NULL, 0);
6855 data->temperature = 0xff;
6860 ctl_log_sense(struct ctl_scsiio *ctsio)
6862 struct ctl_lun *lun = CTL_LUN(ctsio);
6863 int i, pc, page_code, subpage;
6864 int alloc_len, total_len;
6865 struct ctl_page_index *page_index;
6866 struct scsi_log_sense *cdb;
6867 struct scsi_log_header *header;
6869 CTL_DEBUG_PRINT(("ctl_log_sense\n"));
6871 cdb = (struct scsi_log_sense *)ctsio->cdb;
6872 pc = (cdb->page & SLS_PAGE_CTRL_MASK) >> 6;
6873 page_code = cdb->page & SLS_PAGE_CODE;
6874 subpage = cdb->subpage;
6875 alloc_len = scsi_2btoul(cdb->length);
6878 for (i = 0; i < CTL_NUM_LOG_PAGES; i++) {
6879 page_index = &lun->log_pages.index[i];
6881 /* Look for the right page code */
6882 if ((page_index->page_code & SL_PAGE_CODE) != page_code)
6885 /* Look for the right subpage or the subpage wildcard*/
6886 if (page_index->subpage != subpage)
6891 if (i >= CTL_NUM_LOG_PAGES) {
6892 ctl_set_invalid_field(ctsio,
6898 ctl_done((union ctl_io *)ctsio);
6899 return (CTL_RETVAL_COMPLETE);
6902 total_len = sizeof(struct scsi_log_header) + page_index->page_len;
6904 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
6905 ctsio->kern_sg_entries = 0;
6906 ctsio->kern_rel_offset = 0;
6907 ctsio->kern_data_len = min(total_len, alloc_len);
6908 ctsio->kern_total_len = ctsio->kern_data_len;
6910 header = (struct scsi_log_header *)ctsio->kern_data_ptr;
6911 header->page = page_index->page_code;
6912 if (page_index->page_code == SLS_LOGICAL_BLOCK_PROVISIONING)
6913 header->page |= SL_DS;
6914 if (page_index->subpage) {
6915 header->page |= SL_SPF;
6916 header->subpage = page_index->subpage;
6918 scsi_ulto2b(page_index->page_len, header->datalen);
6921 * Call the handler, if it exists, to update the
6922 * page to the latest values.
6924 if (page_index->sense_handler != NULL)
6925 page_index->sense_handler(ctsio, page_index, pc);
6927 memcpy(header + 1, page_index->page_data, page_index->page_len);
6929 ctl_set_success(ctsio);
6930 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6931 ctsio->be_move_done = ctl_config_move_done;
6932 ctl_datamove((union ctl_io *)ctsio);
6933 return (CTL_RETVAL_COMPLETE);
6937 ctl_read_capacity(struct ctl_scsiio *ctsio)
6939 struct ctl_lun *lun = CTL_LUN(ctsio);
6940 struct scsi_read_capacity *cdb;
6941 struct scsi_read_capacity_data *data;
6944 CTL_DEBUG_PRINT(("ctl_read_capacity\n"));
6946 cdb = (struct scsi_read_capacity *)ctsio->cdb;
6948 lba = scsi_4btoul(cdb->addr);
6949 if (((cdb->pmi & SRC_PMI) == 0)
6951 ctl_set_invalid_field(/*ctsio*/ ctsio,
6957 ctl_done((union ctl_io *)ctsio);
6958 return (CTL_RETVAL_COMPLETE);
6961 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
6962 data = (struct scsi_read_capacity_data *)ctsio->kern_data_ptr;
6963 ctsio->kern_data_len = sizeof(*data);
6964 ctsio->kern_total_len = sizeof(*data);
6965 ctsio->kern_rel_offset = 0;
6966 ctsio->kern_sg_entries = 0;
6969 * If the maximum LBA is greater than 0xfffffffe, the user must
6970 * issue a SERVICE ACTION IN (16) command, with the read capacity
6971 * serivce action set.
6973 if (lun->be_lun->maxlba > 0xfffffffe)
6974 scsi_ulto4b(0xffffffff, data->addr);
6976 scsi_ulto4b(lun->be_lun->maxlba, data->addr);
6979 * XXX KDM this may not be 512 bytes...
6981 scsi_ulto4b(lun->be_lun->blocksize, data->length);
6983 ctl_set_success(ctsio);
6984 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
6985 ctsio->be_move_done = ctl_config_move_done;
6986 ctl_datamove((union ctl_io *)ctsio);
6987 return (CTL_RETVAL_COMPLETE);
6991 ctl_read_capacity_16(struct ctl_scsiio *ctsio)
6993 struct ctl_lun *lun = CTL_LUN(ctsio);
6994 struct scsi_read_capacity_16 *cdb;
6995 struct scsi_read_capacity_data_long *data;
6999 CTL_DEBUG_PRINT(("ctl_read_capacity_16\n"));
7001 cdb = (struct scsi_read_capacity_16 *)ctsio->cdb;
7003 alloc_len = scsi_4btoul(cdb->alloc_len);
7004 lba = scsi_8btou64(cdb->addr);
7006 if ((cdb->reladr & SRC16_PMI)
7008 ctl_set_invalid_field(/*ctsio*/ ctsio,
7014 ctl_done((union ctl_io *)ctsio);
7015 return (CTL_RETVAL_COMPLETE);
7018 ctsio->kern_data_ptr = malloc(sizeof(*data), M_CTL, M_WAITOK | M_ZERO);
7019 data = (struct scsi_read_capacity_data_long *)ctsio->kern_data_ptr;
7020 ctsio->kern_rel_offset = 0;
7021 ctsio->kern_sg_entries = 0;
7022 ctsio->kern_data_len = min(sizeof(*data), alloc_len);
7023 ctsio->kern_total_len = ctsio->kern_data_len;
7025 scsi_u64to8b(lun->be_lun->maxlba, data->addr);
7026 /* XXX KDM this may not be 512 bytes... */
7027 scsi_ulto4b(lun->be_lun->blocksize, data->length);
7028 data->prot_lbppbe = lun->be_lun->pblockexp & SRC16_LBPPBE;
7029 scsi_ulto2b(lun->be_lun->pblockoff & SRC16_LALBA_A, data->lalba_lbp);
7030 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP)
7031 data->lalba_lbp[0] |= SRC16_LBPME | SRC16_LBPRZ;
7033 ctl_set_success(ctsio);
7034 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7035 ctsio->be_move_done = ctl_config_move_done;
7036 ctl_datamove((union ctl_io *)ctsio);
7037 return (CTL_RETVAL_COMPLETE);
7041 ctl_get_lba_status(struct ctl_scsiio *ctsio)
7043 struct ctl_lun *lun = CTL_LUN(ctsio);
7044 struct scsi_get_lba_status *cdb;
7045 struct scsi_get_lba_status_data *data;
7046 struct ctl_lba_len_flags *lbalen;
7048 uint32_t alloc_len, total_len;
7051 CTL_DEBUG_PRINT(("ctl_get_lba_status\n"));
7053 cdb = (struct scsi_get_lba_status *)ctsio->cdb;
7054 lba = scsi_8btou64(cdb->addr);
7055 alloc_len = scsi_4btoul(cdb->alloc_len);
7057 if (lba > lun->be_lun->maxlba) {
7058 ctl_set_lba_out_of_range(ctsio, lba);
7059 ctl_done((union ctl_io *)ctsio);
7060 return (CTL_RETVAL_COMPLETE);
7063 total_len = sizeof(*data) + sizeof(data->descr[0]);
7064 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7065 data = (struct scsi_get_lba_status_data *)ctsio->kern_data_ptr;
7066 ctsio->kern_rel_offset = 0;
7067 ctsio->kern_sg_entries = 0;
7068 ctsio->kern_data_len = min(total_len, alloc_len);
7069 ctsio->kern_total_len = ctsio->kern_data_len;
7071 /* Fill dummy data in case backend can't tell anything. */
7072 scsi_ulto4b(4 + sizeof(data->descr[0]), data->length);
7073 scsi_u64to8b(lba, data->descr[0].addr);
7074 scsi_ulto4b(MIN(UINT32_MAX, lun->be_lun->maxlba + 1 - lba),
7075 data->descr[0].length);
7076 data->descr[0].status = 0; /* Mapped or unknown. */
7078 ctl_set_success(ctsio);
7079 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7080 ctsio->be_move_done = ctl_config_move_done;
7082 lbalen = (struct ctl_lba_len_flags *)&ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
7084 lbalen->len = total_len;
7086 retval = lun->backend->config_read((union ctl_io *)ctsio);
7091 ctl_read_defect(struct ctl_scsiio *ctsio)
7093 struct scsi_read_defect_data_10 *ccb10;
7094 struct scsi_read_defect_data_12 *ccb12;
7095 struct scsi_read_defect_data_hdr_10 *data10;
7096 struct scsi_read_defect_data_hdr_12 *data12;
7097 uint32_t alloc_len, data_len;
7100 CTL_DEBUG_PRINT(("ctl_read_defect\n"));
7102 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7103 ccb10 = (struct scsi_read_defect_data_10 *)&ctsio->cdb;
7104 format = ccb10->format;
7105 alloc_len = scsi_2btoul(ccb10->alloc_length);
7106 data_len = sizeof(*data10);
7108 ccb12 = (struct scsi_read_defect_data_12 *)&ctsio->cdb;
7109 format = ccb12->format;
7110 alloc_len = scsi_4btoul(ccb12->alloc_length);
7111 data_len = sizeof(*data12);
7113 if (alloc_len == 0) {
7114 ctl_set_success(ctsio);
7115 ctl_done((union ctl_io *)ctsio);
7116 return (CTL_RETVAL_COMPLETE);
7119 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
7120 ctsio->kern_rel_offset = 0;
7121 ctsio->kern_sg_entries = 0;
7122 ctsio->kern_data_len = min(data_len, alloc_len);
7123 ctsio->kern_total_len = ctsio->kern_data_len;
7125 if (ctsio->cdb[0] == READ_DEFECT_DATA_10) {
7126 data10 = (struct scsi_read_defect_data_hdr_10 *)
7127 ctsio->kern_data_ptr;
7128 data10->format = format;
7129 scsi_ulto2b(0, data10->length);
7131 data12 = (struct scsi_read_defect_data_hdr_12 *)
7132 ctsio->kern_data_ptr;
7133 data12->format = format;
7134 scsi_ulto2b(0, data12->generation);
7135 scsi_ulto4b(0, data12->length);
7138 ctl_set_success(ctsio);
7139 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7140 ctsio->be_move_done = ctl_config_move_done;
7141 ctl_datamove((union ctl_io *)ctsio);
7142 return (CTL_RETVAL_COMPLETE);
7146 ctl_report_tagret_port_groups(struct ctl_scsiio *ctsio)
7148 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7149 struct ctl_lun *lun = CTL_LUN(ctsio);
7150 struct scsi_maintenance_in *cdb;
7152 int alloc_len, ext, total_len = 0, g, pc, pg, ts, os;
7153 int num_ha_groups, num_target_ports, shared_group;
7154 struct ctl_port *port;
7155 struct scsi_target_group_data *rtg_ptr;
7156 struct scsi_target_group_data_extended *rtg_ext_ptr;
7157 struct scsi_target_port_group_descriptor *tpg_desc;
7159 CTL_DEBUG_PRINT(("ctl_report_tagret_port_groups\n"));
7161 cdb = (struct scsi_maintenance_in *)ctsio->cdb;
7162 retval = CTL_RETVAL_COMPLETE;
7164 switch (cdb->byte2 & STG_PDF_MASK) {
7165 case STG_PDF_LENGTH:
7168 case STG_PDF_EXTENDED:
7172 ctl_set_invalid_field(/*ctsio*/ ctsio,
7178 ctl_done((union ctl_io *)ctsio);
7182 num_target_ports = 0;
7183 shared_group = (softc->is_single != 0);
7184 mtx_lock(&softc->ctl_lock);
7185 STAILQ_FOREACH(port, &softc->port_list, links) {
7186 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7188 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7191 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7194 mtx_unlock(&softc->ctl_lock);
7195 num_ha_groups = (softc->is_single) ? 0 : NUM_HA_SHELVES;
7198 total_len = sizeof(struct scsi_target_group_data_extended);
7200 total_len = sizeof(struct scsi_target_group_data);
7201 total_len += sizeof(struct scsi_target_port_group_descriptor) *
7202 (shared_group + num_ha_groups) +
7203 sizeof(struct scsi_target_port_descriptor) * num_target_ports;
7205 alloc_len = scsi_4btoul(cdb->length);
7207 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7208 ctsio->kern_sg_entries = 0;
7209 ctsio->kern_rel_offset = 0;
7210 ctsio->kern_data_len = min(total_len, alloc_len);
7211 ctsio->kern_total_len = ctsio->kern_data_len;
7214 rtg_ext_ptr = (struct scsi_target_group_data_extended *)
7215 ctsio->kern_data_ptr;
7216 scsi_ulto4b(total_len - 4, rtg_ext_ptr->length);
7217 rtg_ext_ptr->format_type = 0x10;
7218 rtg_ext_ptr->implicit_transition_time = 0;
7219 tpg_desc = &rtg_ext_ptr->groups[0];
7221 rtg_ptr = (struct scsi_target_group_data *)
7222 ctsio->kern_data_ptr;
7223 scsi_ulto4b(total_len - 4, rtg_ptr->length);
7224 tpg_desc = &rtg_ptr->groups[0];
7227 mtx_lock(&softc->ctl_lock);
7228 pg = softc->port_min / softc->port_cnt;
7229 if (lun->flags & (CTL_LUN_PRIMARY_SC | CTL_LUN_PEER_SC_PRIMARY)) {
7230 /* Some shelf is known to be primary. */
7231 if (softc->ha_link == CTL_HA_LINK_OFFLINE)
7232 os = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7233 else if (softc->ha_link == CTL_HA_LINK_UNKNOWN)
7234 os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7235 else if (softc->ha_mode == CTL_HA_MODE_ACT_STBY)
7236 os = TPG_ASYMMETRIC_ACCESS_STANDBY;
7238 os = TPG_ASYMMETRIC_ACCESS_NONOPTIMIZED;
7239 if (lun->flags & CTL_LUN_PRIMARY_SC) {
7240 ts = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7243 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7246 /* No known primary shelf. */
7247 if (softc->ha_link == CTL_HA_LINK_OFFLINE) {
7248 ts = TPG_ASYMMETRIC_ACCESS_UNAVAILABLE;
7249 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7250 } else if (softc->ha_link == CTL_HA_LINK_UNKNOWN) {
7251 ts = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7252 os = TPG_ASYMMETRIC_ACCESS_OPTIMIZED;
7254 ts = os = TPG_ASYMMETRIC_ACCESS_TRANSITIONING;
7258 tpg_desc->pref_state = ts;
7259 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7260 TPG_U_SUP | TPG_T_SUP;
7261 scsi_ulto2b(1, tpg_desc->target_port_group);
7262 tpg_desc->status = TPG_IMPLICIT;
7264 STAILQ_FOREACH(port, &softc->port_list, links) {
7265 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7267 if (!softc->is_single &&
7268 (port->status & CTL_PORT_STATUS_HA_SHARED) == 0)
7270 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7272 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7273 relative_target_port_identifier);
7276 tpg_desc->target_port_count = pc;
7277 tpg_desc = (struct scsi_target_port_group_descriptor *)
7278 &tpg_desc->descriptors[pc];
7280 for (g = 0; g < num_ha_groups; g++) {
7281 tpg_desc->pref_state = (g == pg) ? ts : os;
7282 tpg_desc->support = TPG_AO_SUP | TPG_AN_SUP | TPG_S_SUP |
7283 TPG_U_SUP | TPG_T_SUP;
7284 scsi_ulto2b(2 + g, tpg_desc->target_port_group);
7285 tpg_desc->status = TPG_IMPLICIT;
7287 STAILQ_FOREACH(port, &softc->port_list, links) {
7288 if (port->targ_port < g * softc->port_cnt ||
7289 port->targ_port >= (g + 1) * softc->port_cnt)
7291 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
7293 if (port->status & CTL_PORT_STATUS_HA_SHARED)
7295 if (ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
7297 scsi_ulto2b(port->targ_port, tpg_desc->descriptors[pc].
7298 relative_target_port_identifier);
7301 tpg_desc->target_port_count = pc;
7302 tpg_desc = (struct scsi_target_port_group_descriptor *)
7303 &tpg_desc->descriptors[pc];
7305 mtx_unlock(&softc->ctl_lock);
7307 ctl_set_success(ctsio);
7308 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7309 ctsio->be_move_done = ctl_config_move_done;
7310 ctl_datamove((union ctl_io *)ctsio);
7315 ctl_report_supported_opcodes(struct ctl_scsiio *ctsio)
7317 struct ctl_lun *lun = CTL_LUN(ctsio);
7318 struct scsi_report_supported_opcodes *cdb;
7319 const struct ctl_cmd_entry *entry, *sentry;
7320 struct scsi_report_supported_opcodes_all *all;
7321 struct scsi_report_supported_opcodes_descr *descr;
7322 struct scsi_report_supported_opcodes_one *one;
7324 int alloc_len, total_len;
7325 int opcode, service_action, i, j, num;
7327 CTL_DEBUG_PRINT(("ctl_report_supported_opcodes\n"));
7329 cdb = (struct scsi_report_supported_opcodes *)ctsio->cdb;
7330 retval = CTL_RETVAL_COMPLETE;
7332 opcode = cdb->requested_opcode;
7333 service_action = scsi_2btoul(cdb->requested_service_action);
7334 switch (cdb->options & RSO_OPTIONS_MASK) {
7335 case RSO_OPTIONS_ALL:
7337 for (i = 0; i < 256; i++) {
7338 entry = &ctl_cmd_table[i];
7339 if (entry->flags & CTL_CMD_FLAG_SA5) {
7340 for (j = 0; j < 32; j++) {
7341 sentry = &((const struct ctl_cmd_entry *)
7343 if (ctl_cmd_applicable(
7344 lun->be_lun->lun_type, sentry))
7348 if (ctl_cmd_applicable(lun->be_lun->lun_type,
7353 total_len = sizeof(struct scsi_report_supported_opcodes_all) +
7354 num * sizeof(struct scsi_report_supported_opcodes_descr);
7356 case RSO_OPTIONS_OC:
7357 if (ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) {
7358 ctl_set_invalid_field(/*ctsio*/ ctsio,
7364 ctl_done((union ctl_io *)ctsio);
7365 return (CTL_RETVAL_COMPLETE);
7367 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7369 case RSO_OPTIONS_OC_SA:
7370 if ((ctl_cmd_table[opcode].flags & CTL_CMD_FLAG_SA5) == 0 ||
7371 service_action >= 32) {
7372 ctl_set_invalid_field(/*ctsio*/ ctsio,
7378 ctl_done((union ctl_io *)ctsio);
7379 return (CTL_RETVAL_COMPLETE);
7382 case RSO_OPTIONS_OC_ASA:
7383 total_len = sizeof(struct scsi_report_supported_opcodes_one) + 32;
7386 ctl_set_invalid_field(/*ctsio*/ ctsio,
7392 ctl_done((union ctl_io *)ctsio);
7393 return (CTL_RETVAL_COMPLETE);
7396 alloc_len = scsi_4btoul(cdb->length);
7398 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7399 ctsio->kern_sg_entries = 0;
7400 ctsio->kern_rel_offset = 0;
7401 ctsio->kern_data_len = min(total_len, alloc_len);
7402 ctsio->kern_total_len = ctsio->kern_data_len;
7404 switch (cdb->options & RSO_OPTIONS_MASK) {
7405 case RSO_OPTIONS_ALL:
7406 all = (struct scsi_report_supported_opcodes_all *)
7407 ctsio->kern_data_ptr;
7409 for (i = 0; i < 256; i++) {
7410 entry = &ctl_cmd_table[i];
7411 if (entry->flags & CTL_CMD_FLAG_SA5) {
7412 for (j = 0; j < 32; j++) {
7413 sentry = &((const struct ctl_cmd_entry *)
7415 if (!ctl_cmd_applicable(
7416 lun->be_lun->lun_type, sentry))
7418 descr = &all->descr[num++];
7420 scsi_ulto2b(j, descr->service_action);
7421 descr->flags = RSO_SERVACTV;
7422 scsi_ulto2b(sentry->length,
7426 if (!ctl_cmd_applicable(lun->be_lun->lun_type,
7429 descr = &all->descr[num++];
7431 scsi_ulto2b(0, descr->service_action);
7433 scsi_ulto2b(entry->length, descr->cdb_length);
7437 num * sizeof(struct scsi_report_supported_opcodes_descr),
7440 case RSO_OPTIONS_OC:
7441 one = (struct scsi_report_supported_opcodes_one *)
7442 ctsio->kern_data_ptr;
7443 entry = &ctl_cmd_table[opcode];
7445 case RSO_OPTIONS_OC_SA:
7446 one = (struct scsi_report_supported_opcodes_one *)
7447 ctsio->kern_data_ptr;
7448 entry = &ctl_cmd_table[opcode];
7449 entry = &((const struct ctl_cmd_entry *)
7450 entry->execute)[service_action];
7452 if (ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
7454 scsi_ulto2b(entry->length, one->cdb_length);
7455 one->cdb_usage[0] = opcode;
7456 memcpy(&one->cdb_usage[1], entry->usage,
7461 case RSO_OPTIONS_OC_ASA:
7462 one = (struct scsi_report_supported_opcodes_one *)
7463 ctsio->kern_data_ptr;
7464 entry = &ctl_cmd_table[opcode];
7465 if (entry->flags & CTL_CMD_FLAG_SA5) {
7466 entry = &((const struct ctl_cmd_entry *)
7467 entry->execute)[service_action];
7468 } else if (service_action != 0) {
7475 ctl_set_success(ctsio);
7476 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7477 ctsio->be_move_done = ctl_config_move_done;
7478 ctl_datamove((union ctl_io *)ctsio);
7483 ctl_report_supported_tmf(struct ctl_scsiio *ctsio)
7485 struct scsi_report_supported_tmf *cdb;
7486 struct scsi_report_supported_tmf_ext_data *data;
7488 int alloc_len, total_len;
7490 CTL_DEBUG_PRINT(("ctl_report_supported_tmf\n"));
7492 cdb = (struct scsi_report_supported_tmf *)ctsio->cdb;
7494 retval = CTL_RETVAL_COMPLETE;
7496 if (cdb->options & RST_REPD)
7497 total_len = sizeof(struct scsi_report_supported_tmf_ext_data);
7499 total_len = sizeof(struct scsi_report_supported_tmf_data);
7500 alloc_len = scsi_4btoul(cdb->length);
7502 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7503 ctsio->kern_sg_entries = 0;
7504 ctsio->kern_rel_offset = 0;
7505 ctsio->kern_data_len = min(total_len, alloc_len);
7506 ctsio->kern_total_len = ctsio->kern_data_len;
7508 data = (struct scsi_report_supported_tmf_ext_data *)ctsio->kern_data_ptr;
7509 data->byte1 |= RST_ATS | RST_ATSS | RST_CTSS | RST_LURS | RST_QTS |
7511 data->byte2 |= RST_QAES | RST_QTSS | RST_ITNRS;
7512 data->length = total_len - 4;
7514 ctl_set_success(ctsio);
7515 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7516 ctsio->be_move_done = ctl_config_move_done;
7517 ctl_datamove((union ctl_io *)ctsio);
7522 ctl_report_timestamp(struct ctl_scsiio *ctsio)
7524 struct scsi_report_timestamp *cdb;
7525 struct scsi_report_timestamp_data *data;
7529 int alloc_len, total_len;
7531 CTL_DEBUG_PRINT(("ctl_report_timestamp\n"));
7533 cdb = (struct scsi_report_timestamp *)ctsio->cdb;
7535 retval = CTL_RETVAL_COMPLETE;
7537 total_len = sizeof(struct scsi_report_timestamp_data);
7538 alloc_len = scsi_4btoul(cdb->length);
7540 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7541 ctsio->kern_sg_entries = 0;
7542 ctsio->kern_rel_offset = 0;
7543 ctsio->kern_data_len = min(total_len, alloc_len);
7544 ctsio->kern_total_len = ctsio->kern_data_len;
7546 data = (struct scsi_report_timestamp_data *)ctsio->kern_data_ptr;
7547 scsi_ulto2b(sizeof(*data) - 2, data->length);
7548 data->origin = RTS_ORIG_OUTSIDE;
7550 timestamp = (int64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
7551 scsi_ulto4b(timestamp >> 16, data->timestamp);
7552 scsi_ulto2b(timestamp & 0xffff, &data->timestamp[4]);
7554 ctl_set_success(ctsio);
7555 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7556 ctsio->be_move_done = ctl_config_move_done;
7557 ctl_datamove((union ctl_io *)ctsio);
7562 ctl_persistent_reserve_in(struct ctl_scsiio *ctsio)
7564 struct ctl_softc *softc = CTL_SOFTC(ctsio);
7565 struct ctl_lun *lun = CTL_LUN(ctsio);
7566 struct scsi_per_res_in *cdb;
7567 int alloc_len, total_len = 0;
7568 /* struct scsi_per_res_in_rsrv in_data; */
7571 CTL_DEBUG_PRINT(("ctl_persistent_reserve_in\n"));
7573 cdb = (struct scsi_per_res_in *)ctsio->cdb;
7575 alloc_len = scsi_2btoul(cdb->length);
7578 mtx_lock(&lun->lun_lock);
7579 switch (cdb->action) {
7580 case SPRI_RK: /* read keys */
7581 total_len = sizeof(struct scsi_per_res_in_keys) +
7583 sizeof(struct scsi_per_res_key);
7585 case SPRI_RR: /* read reservation */
7586 if (lun->flags & CTL_LUN_PR_RESERVED)
7587 total_len = sizeof(struct scsi_per_res_in_rsrv);
7589 total_len = sizeof(struct scsi_per_res_in_header);
7591 case SPRI_RC: /* report capabilities */
7592 total_len = sizeof(struct scsi_per_res_cap);
7594 case SPRI_RS: /* read full status */
7595 total_len = sizeof(struct scsi_per_res_in_header) +
7596 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7600 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7602 mtx_unlock(&lun->lun_lock);
7604 ctsio->kern_data_ptr = malloc(total_len, M_CTL, M_WAITOK | M_ZERO);
7605 ctsio->kern_rel_offset = 0;
7606 ctsio->kern_sg_entries = 0;
7607 ctsio->kern_data_len = min(total_len, alloc_len);
7608 ctsio->kern_total_len = ctsio->kern_data_len;
7610 mtx_lock(&lun->lun_lock);
7611 switch (cdb->action) {
7612 case SPRI_RK: { // read keys
7613 struct scsi_per_res_in_keys *res_keys;
7616 res_keys = (struct scsi_per_res_in_keys*)ctsio->kern_data_ptr;
7619 * We had to drop the lock to allocate our buffer, which
7620 * leaves time for someone to come in with another
7621 * persistent reservation. (That is unlikely, though,
7622 * since this should be the only persistent reservation
7623 * command active right now.)
7625 if (total_len != (sizeof(struct scsi_per_res_in_keys) +
7626 (lun->pr_key_count *
7627 sizeof(struct scsi_per_res_key)))){
7628 mtx_unlock(&lun->lun_lock);
7629 free(ctsio->kern_data_ptr, M_CTL);
7630 printf("%s: reservation length changed, retrying\n",
7635 scsi_ulto4b(lun->pr_generation, res_keys->header.generation);
7637 scsi_ulto4b(sizeof(struct scsi_per_res_key) *
7638 lun->pr_key_count, res_keys->header.length);
7640 for (i = 0, key_count = 0; i < CTL_MAX_INITIATORS; i++) {
7641 if ((key = ctl_get_prkey(lun, i)) == 0)
7645 * We used lun->pr_key_count to calculate the
7646 * size to allocate. If it turns out the number of
7647 * initiators with the registered flag set is
7648 * larger than that (i.e. they haven't been kept in
7649 * sync), we've got a problem.
7651 if (key_count >= lun->pr_key_count) {
7655 scsi_u64to8b(key, res_keys->keys[key_count].key);
7660 case SPRI_RR: { // read reservation
7661 struct scsi_per_res_in_rsrv *res;
7662 int tmp_len, header_only;
7664 res = (struct scsi_per_res_in_rsrv *)ctsio->kern_data_ptr;
7666 scsi_ulto4b(lun->pr_generation, res->header.generation);
7668 if (lun->flags & CTL_LUN_PR_RESERVED)
7670 tmp_len = sizeof(struct scsi_per_res_in_rsrv);
7671 scsi_ulto4b(sizeof(struct scsi_per_res_in_rsrv_data),
7672 res->header.length);
7675 tmp_len = sizeof(struct scsi_per_res_in_header);
7676 scsi_ulto4b(0, res->header.length);
7681 * We had to drop the lock to allocate our buffer, which
7682 * leaves time for someone to come in with another
7683 * persistent reservation. (That is unlikely, though,
7684 * since this should be the only persistent reservation
7685 * command active right now.)
7687 if (tmp_len != total_len) {
7688 mtx_unlock(&lun->lun_lock);
7689 free(ctsio->kern_data_ptr, M_CTL);
7690 printf("%s: reservation status changed, retrying\n",
7696 * No reservation held, so we're done.
7698 if (header_only != 0)
7702 * If the registration is an All Registrants type, the key
7703 * is 0, since it doesn't really matter.
7705 if (lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
7706 scsi_u64to8b(ctl_get_prkey(lun, lun->pr_res_idx),
7707 res->data.reservation);
7709 res->data.scopetype = lun->pr_res_type;
7712 case SPRI_RC: //report capabilities
7714 struct scsi_per_res_cap *res_cap;
7717 res_cap = (struct scsi_per_res_cap *)ctsio->kern_data_ptr;
7718 scsi_ulto2b(sizeof(*res_cap), res_cap->length);
7719 res_cap->flags1 = SPRI_CRH;
7720 res_cap->flags2 = SPRI_TMV | SPRI_ALLOW_5;
7721 type_mask = SPRI_TM_WR_EX_AR |
7727 scsi_ulto2b(type_mask, res_cap->type_mask);
7730 case SPRI_RS: { // read full status
7731 struct scsi_per_res_in_full *res_status;
7732 struct scsi_per_res_in_full_desc *res_desc;
7733 struct ctl_port *port;
7736 res_status = (struct scsi_per_res_in_full*)ctsio->kern_data_ptr;
7739 * We had to drop the lock to allocate our buffer, which
7740 * leaves time for someone to come in with another
7741 * persistent reservation. (That is unlikely, though,
7742 * since this should be the only persistent reservation
7743 * command active right now.)
7745 if (total_len < (sizeof(struct scsi_per_res_in_header) +
7746 (sizeof(struct scsi_per_res_in_full_desc) + 256) *
7747 lun->pr_key_count)){
7748 mtx_unlock(&lun->lun_lock);
7749 free(ctsio->kern_data_ptr, M_CTL);
7750 printf("%s: reservation length changed, retrying\n",
7755 scsi_ulto4b(lun->pr_generation, res_status->header.generation);
7757 res_desc = &res_status->desc[0];
7758 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7759 if ((key = ctl_get_prkey(lun, i)) == 0)
7762 scsi_u64to8b(key, res_desc->res_key.key);
7763 if ((lun->flags & CTL_LUN_PR_RESERVED) &&
7764 (lun->pr_res_idx == i ||
7765 lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS)) {
7766 res_desc->flags = SPRI_FULL_R_HOLDER;
7767 res_desc->scopetype = lun->pr_res_type;
7769 scsi_ulto2b(i / CTL_MAX_INIT_PER_PORT,
7770 res_desc->rel_trgt_port_id);
7772 port = softc->ctl_ports[i / CTL_MAX_INIT_PER_PORT];
7774 len = ctl_create_iid(port,
7775 i % CTL_MAX_INIT_PER_PORT,
7776 res_desc->transport_id);
7777 scsi_ulto4b(len, res_desc->additional_length);
7778 res_desc = (struct scsi_per_res_in_full_desc *)
7779 &res_desc->transport_id[len];
7781 scsi_ulto4b((uint8_t *)res_desc - (uint8_t *)&res_status->desc[0],
7782 res_status->header.length);
7786 panic("%s: Invalid PR type %#x", __func__, cdb->action);
7788 mtx_unlock(&lun->lun_lock);
7790 ctl_set_success(ctsio);
7791 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
7792 ctsio->be_move_done = ctl_config_move_done;
7793 ctl_datamove((union ctl_io *)ctsio);
7794 return (CTL_RETVAL_COMPLETE);
7798 * Returns 0 if ctl_persistent_reserve_out() should continue, non-zero if
7802 ctl_pro_preempt(struct ctl_softc *softc, struct ctl_lun *lun, uint64_t res_key,
7803 uint64_t sa_res_key, uint8_t type, uint32_t residx,
7804 struct ctl_scsiio *ctsio, struct scsi_per_res_out *cdb,
7805 struct scsi_per_res_out_parms* param)
7807 union ctl_ha_msg persis_io;
7810 mtx_lock(&lun->lun_lock);
7811 if (sa_res_key == 0) {
7812 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
7813 /* validate scope and type */
7814 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7816 mtx_unlock(&lun->lun_lock);
7817 ctl_set_invalid_field(/*ctsio*/ ctsio,
7823 ctl_done((union ctl_io *)ctsio);
7827 if (type>8 || type==2 || type==4 || type==0) {
7828 mtx_unlock(&lun->lun_lock);
7829 ctl_set_invalid_field(/*ctsio*/ ctsio,
7835 ctl_done((union ctl_io *)ctsio);
7840 * Unregister everybody else and build UA for
7843 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7844 if (i == residx || ctl_get_prkey(lun, i) == 0)
7847 ctl_clr_prkey(lun, i);
7848 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7850 lun->pr_key_count = 1;
7851 lun->pr_res_type = type;
7852 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7853 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
7854 lun->pr_res_idx = residx;
7855 lun->pr_generation++;
7856 mtx_unlock(&lun->lun_lock);
7858 /* send msg to other side */
7859 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7860 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7861 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7862 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7863 persis_io.pr.pr_info.res_type = type;
7864 memcpy(persis_io.pr.pr_info.sa_res_key,
7865 param->serv_act_res_key,
7866 sizeof(param->serv_act_res_key));
7867 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7868 sizeof(persis_io.pr), M_WAITOK);
7870 /* not all registrants */
7871 mtx_unlock(&lun->lun_lock);
7872 free(ctsio->kern_data_ptr, M_CTL);
7873 ctl_set_invalid_field(ctsio,
7879 ctl_done((union ctl_io *)ctsio);
7882 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
7883 || !(lun->flags & CTL_LUN_PR_RESERVED)) {
7886 if (res_key == sa_res_key) {
7889 * The spec implies this is not good but doesn't
7890 * say what to do. There are two choices either
7891 * generate a res conflict or check condition
7892 * with illegal field in parameter data. Since
7893 * that is what is done when the sa_res_key is
7894 * zero I'll take that approach since this has
7895 * to do with the sa_res_key.
7897 mtx_unlock(&lun->lun_lock);
7898 free(ctsio->kern_data_ptr, M_CTL);
7899 ctl_set_invalid_field(ctsio,
7905 ctl_done((union ctl_io *)ctsio);
7909 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
7910 if (ctl_get_prkey(lun, i) != sa_res_key)
7914 ctl_clr_prkey(lun, i);
7915 lun->pr_key_count--;
7916 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7919 mtx_unlock(&lun->lun_lock);
7920 free(ctsio->kern_data_ptr, M_CTL);
7921 ctl_set_reservation_conflict(ctsio);
7922 ctl_done((union ctl_io *)ctsio);
7923 return (CTL_RETVAL_COMPLETE);
7925 lun->pr_generation++;
7926 mtx_unlock(&lun->lun_lock);
7928 /* send msg to other side */
7929 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
7930 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
7931 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
7932 persis_io.pr.pr_info.residx = lun->pr_res_idx;
7933 persis_io.pr.pr_info.res_type = type;
7934 memcpy(persis_io.pr.pr_info.sa_res_key,
7935 param->serv_act_res_key,
7936 sizeof(param->serv_act_res_key));
7937 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
7938 sizeof(persis_io.pr), M_WAITOK);
7940 /* Reserved but not all registrants */
7941 /* sa_res_key is res holder */
7942 if (sa_res_key == ctl_get_prkey(lun, lun->pr_res_idx)) {
7943 /* validate scope and type */
7944 if ((cdb->scope_type & SPR_SCOPE_MASK) !=
7946 mtx_unlock(&lun->lun_lock);
7947 ctl_set_invalid_field(/*ctsio*/ ctsio,
7953 ctl_done((union ctl_io *)ctsio);
7957 if (type>8 || type==2 || type==4 || type==0) {
7958 mtx_unlock(&lun->lun_lock);
7959 ctl_set_invalid_field(/*ctsio*/ ctsio,
7965 ctl_done((union ctl_io *)ctsio);
7971 * if sa_res_key != res_key remove all
7972 * registrants w/sa_res_key and generate UA
7973 * for these registrants(Registrations
7974 * Preempted) if it wasn't an exclusive
7975 * reservation generate UA(Reservations
7976 * Preempted) for all other registered nexuses
7977 * if the type has changed. Establish the new
7978 * reservation and holder. If res_key and
7979 * sa_res_key are the same do the above
7980 * except don't unregister the res holder.
7983 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
7984 if (i == residx || ctl_get_prkey(lun, i) == 0)
7987 if (sa_res_key == ctl_get_prkey(lun, i)) {
7988 ctl_clr_prkey(lun, i);
7989 lun->pr_key_count--;
7990 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
7991 } else if (type != lun->pr_res_type &&
7992 (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
7993 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
7994 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
7997 lun->pr_res_type = type;
7998 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
7999 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8000 lun->pr_res_idx = residx;
8002 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8003 lun->pr_generation++;
8004 mtx_unlock(&lun->lun_lock);
8006 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8007 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8008 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8009 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8010 persis_io.pr.pr_info.res_type = type;
8011 memcpy(persis_io.pr.pr_info.sa_res_key,
8012 param->serv_act_res_key,
8013 sizeof(param->serv_act_res_key));
8014 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8015 sizeof(persis_io.pr), M_WAITOK);
8018 * sa_res_key is not the res holder just
8019 * remove registrants
8023 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8024 if (sa_res_key != ctl_get_prkey(lun, i))
8028 ctl_clr_prkey(lun, i);
8029 lun->pr_key_count--;
8030 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8034 mtx_unlock(&lun->lun_lock);
8035 free(ctsio->kern_data_ptr, M_CTL);
8036 ctl_set_reservation_conflict(ctsio);
8037 ctl_done((union ctl_io *)ctsio);
8040 lun->pr_generation++;
8041 mtx_unlock(&lun->lun_lock);
8043 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8044 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8045 persis_io.pr.pr_info.action = CTL_PR_PREEMPT;
8046 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8047 persis_io.pr.pr_info.res_type = type;
8048 memcpy(persis_io.pr.pr_info.sa_res_key,
8049 param->serv_act_res_key,
8050 sizeof(param->serv_act_res_key));
8051 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8052 sizeof(persis_io.pr), M_WAITOK);
8059 ctl_pro_preempt_other(struct ctl_lun *lun, union ctl_ha_msg *msg)
8061 uint64_t sa_res_key;
8064 sa_res_key = scsi_8btou64(msg->pr.pr_info.sa_res_key);
8066 if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS
8067 || lun->pr_res_idx == CTL_PR_NO_RESERVATION
8068 || sa_res_key != ctl_get_prkey(lun, lun->pr_res_idx)) {
8069 if (sa_res_key == 0) {
8071 * Unregister everybody else and build UA for
8074 for(i = 0; i < CTL_MAX_INITIATORS; i++) {
8075 if (i == msg->pr.pr_info.residx ||
8076 ctl_get_prkey(lun, i) == 0)
8079 ctl_clr_prkey(lun, i);
8080 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8083 lun->pr_key_count = 1;
8084 lun->pr_res_type = msg->pr.pr_info.res_type;
8085 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8086 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8087 lun->pr_res_idx = msg->pr.pr_info.residx;
8089 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8090 if (sa_res_key == ctl_get_prkey(lun, i))
8093 ctl_clr_prkey(lun, i);
8094 lun->pr_key_count--;
8095 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8099 for (i = 0; i < CTL_MAX_INITIATORS; i++) {
8100 if (i == msg->pr.pr_info.residx ||
8101 ctl_get_prkey(lun, i) == 0)
8104 if (sa_res_key == ctl_get_prkey(lun, i)) {
8105 ctl_clr_prkey(lun, i);
8106 lun->pr_key_count--;
8107 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8108 } else if (msg->pr.pr_info.res_type != lun->pr_res_type
8109 && (lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8110 lun->pr_res_type == SPR_TYPE_EX_AC_RO)) {
8111 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8114 lun->pr_res_type = msg->pr.pr_info.res_type;
8115 if (lun->pr_res_type != SPR_TYPE_WR_EX_AR &&
8116 lun->pr_res_type != SPR_TYPE_EX_AC_AR)
8117 lun->pr_res_idx = msg->pr.pr_info.residx;
8119 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8121 lun->pr_generation++;
8127 ctl_persistent_reserve_out(struct ctl_scsiio *ctsio)
8129 struct ctl_softc *softc = CTL_SOFTC(ctsio);
8130 struct ctl_lun *lun = CTL_LUN(ctsio);
8132 u_int32_t param_len;
8133 struct scsi_per_res_out *cdb;
8134 struct scsi_per_res_out_parms* param;
8136 uint64_t res_key, sa_res_key, key;
8138 union ctl_ha_msg persis_io;
8141 CTL_DEBUG_PRINT(("ctl_persistent_reserve_out\n"));
8143 cdb = (struct scsi_per_res_out *)ctsio->cdb;
8144 retval = CTL_RETVAL_COMPLETE;
8147 * We only support whole-LUN scope. The scope & type are ignored for
8148 * register, register and ignore existing key and clear.
8149 * We sometimes ignore scope and type on preempts too!!
8150 * Verify reservation type here as well.
8152 type = cdb->scope_type & SPR_TYPE_MASK;
8153 if ((cdb->action == SPRO_RESERVE)
8154 || (cdb->action == SPRO_RELEASE)) {
8155 if ((cdb->scope_type & SPR_SCOPE_MASK) != SPR_LU_SCOPE) {
8156 ctl_set_invalid_field(/*ctsio*/ ctsio,
8162 ctl_done((union ctl_io *)ctsio);
8163 return (CTL_RETVAL_COMPLETE);
8166 if (type>8 || type==2 || type==4 || type==0) {
8167 ctl_set_invalid_field(/*ctsio*/ ctsio,
8173 ctl_done((union ctl_io *)ctsio);
8174 return (CTL_RETVAL_COMPLETE);
8178 param_len = scsi_4btoul(cdb->length);
8180 if ((ctsio->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0) {
8181 ctsio->kern_data_ptr = malloc(param_len, M_CTL, M_WAITOK);
8182 ctsio->kern_data_len = param_len;
8183 ctsio->kern_total_len = param_len;
8184 ctsio->kern_rel_offset = 0;
8185 ctsio->kern_sg_entries = 0;
8186 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
8187 ctsio->be_move_done = ctl_config_move_done;
8188 ctl_datamove((union ctl_io *)ctsio);
8190 return (CTL_RETVAL_COMPLETE);
8193 param = (struct scsi_per_res_out_parms *)ctsio->kern_data_ptr;
8195 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
8196 res_key = scsi_8btou64(param->res_key.key);
8197 sa_res_key = scsi_8btou64(param->serv_act_res_key);
8200 * Validate the reservation key here except for SPRO_REG_IGNO
8201 * This must be done for all other service actions
8203 if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REG_IGNO) {
8204 mtx_lock(&lun->lun_lock);
8205 if ((key = ctl_get_prkey(lun, residx)) != 0) {
8206 if (res_key != key) {
8208 * The current key passed in doesn't match
8209 * the one the initiator previously
8212 mtx_unlock(&lun->lun_lock);
8213 free(ctsio->kern_data_ptr, M_CTL);
8214 ctl_set_reservation_conflict(ctsio);
8215 ctl_done((union ctl_io *)ctsio);
8216 return (CTL_RETVAL_COMPLETE);
8218 } else if ((cdb->action & SPRO_ACTION_MASK) != SPRO_REGISTER) {
8220 * We are not registered
8222 mtx_unlock(&lun->lun_lock);
8223 free(ctsio->kern_data_ptr, M_CTL);
8224 ctl_set_reservation_conflict(ctsio);
8225 ctl_done((union ctl_io *)ctsio);
8226 return (CTL_RETVAL_COMPLETE);
8227 } else if (res_key != 0) {
8229 * We are not registered and trying to register but
8230 * the register key isn't zero.
8232 mtx_unlock(&lun->lun_lock);
8233 free(ctsio->kern_data_ptr, M_CTL);
8234 ctl_set_reservation_conflict(ctsio);
8235 ctl_done((union ctl_io *)ctsio);
8236 return (CTL_RETVAL_COMPLETE);
8238 mtx_unlock(&lun->lun_lock);
8241 switch (cdb->action & SPRO_ACTION_MASK) {
8243 case SPRO_REG_IGNO: {
8246 * We don't support any of these options, as we report in
8247 * the read capabilities request (see
8248 * ctl_persistent_reserve_in(), above).
8250 if ((param->flags & SPR_SPEC_I_PT)
8251 || (param->flags & SPR_ALL_TG_PT)
8252 || (param->flags & SPR_APTPL)) {
8255 if (param->flags & SPR_APTPL)
8257 else if (param->flags & SPR_ALL_TG_PT)
8259 else /* SPR_SPEC_I_PT */
8262 free(ctsio->kern_data_ptr, M_CTL);
8263 ctl_set_invalid_field(ctsio,
8269 ctl_done((union ctl_io *)ctsio);
8270 return (CTL_RETVAL_COMPLETE);
8273 mtx_lock(&lun->lun_lock);
8276 * The initiator wants to clear the
8279 if (sa_res_key == 0) {
8281 && (cdb->action & SPRO_ACTION_MASK) == SPRO_REGISTER)
8282 || ((cdb->action & SPRO_ACTION_MASK) == SPRO_REG_IGNO
8283 && ctl_get_prkey(lun, residx) == 0)) {
8284 mtx_unlock(&lun->lun_lock);
8288 ctl_clr_prkey(lun, residx);
8289 lun->pr_key_count--;
8291 if (residx == lun->pr_res_idx) {
8292 lun->flags &= ~CTL_LUN_PR_RESERVED;
8293 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8295 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8296 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8297 lun->pr_key_count) {
8299 * If the reservation is a registrants
8300 * only type we need to generate a UA
8301 * for other registered inits. The
8302 * sense code should be RESERVATIONS
8306 for (i = softc->init_min; i < softc->init_max; i++){
8307 if (ctl_get_prkey(lun, i) == 0)
8310 CTL_UA_RES_RELEASE);
8313 lun->pr_res_type = 0;
8314 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8315 if (lun->pr_key_count==0) {
8316 lun->flags &= ~CTL_LUN_PR_RESERVED;
8317 lun->pr_res_type = 0;
8318 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8321 lun->pr_generation++;
8322 mtx_unlock(&lun->lun_lock);
8324 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8325 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8326 persis_io.pr.pr_info.action = CTL_PR_UNREG_KEY;
8327 persis_io.pr.pr_info.residx = residx;
8328 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8329 sizeof(persis_io.pr), M_WAITOK);
8330 } else /* sa_res_key != 0 */ {
8333 * If we aren't registered currently then increment
8334 * the key count and set the registered flag.
8336 ctl_alloc_prkey(lun, residx);
8337 if (ctl_get_prkey(lun, residx) == 0)
8338 lun->pr_key_count++;
8339 ctl_set_prkey(lun, residx, sa_res_key);
8340 lun->pr_generation++;
8341 mtx_unlock(&lun->lun_lock);
8343 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8344 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8345 persis_io.pr.pr_info.action = CTL_PR_REG_KEY;
8346 persis_io.pr.pr_info.residx = residx;
8347 memcpy(persis_io.pr.pr_info.sa_res_key,
8348 param->serv_act_res_key,
8349 sizeof(param->serv_act_res_key));
8350 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8351 sizeof(persis_io.pr), M_WAITOK);
8357 mtx_lock(&lun->lun_lock);
8358 if (lun->flags & CTL_LUN_PR_RESERVED) {
8360 * if this isn't the reservation holder and it's
8361 * not a "all registrants" type or if the type is
8362 * different then we have a conflict
8364 if ((lun->pr_res_idx != residx
8365 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS)
8366 || lun->pr_res_type != type) {
8367 mtx_unlock(&lun->lun_lock);
8368 free(ctsio->kern_data_ptr, M_CTL);
8369 ctl_set_reservation_conflict(ctsio);
8370 ctl_done((union ctl_io *)ctsio);
8371 return (CTL_RETVAL_COMPLETE);
8373 mtx_unlock(&lun->lun_lock);
8374 } else /* create a reservation */ {
8376 * If it's not an "all registrants" type record
8377 * reservation holder
8379 if (type != SPR_TYPE_WR_EX_AR
8380 && type != SPR_TYPE_EX_AC_AR)
8381 lun->pr_res_idx = residx; /* Res holder */
8383 lun->pr_res_idx = CTL_PR_ALL_REGISTRANTS;
8385 lun->flags |= CTL_LUN_PR_RESERVED;
8386 lun->pr_res_type = type;
8388 mtx_unlock(&lun->lun_lock);
8390 /* send msg to other side */
8391 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8392 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8393 persis_io.pr.pr_info.action = CTL_PR_RESERVE;
8394 persis_io.pr.pr_info.residx = lun->pr_res_idx;
8395 persis_io.pr.pr_info.res_type = type;
8396 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8397 sizeof(persis_io.pr), M_WAITOK);
8402 mtx_lock(&lun->lun_lock);
8403 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0) {
8404 /* No reservation exists return good status */
8405 mtx_unlock(&lun->lun_lock);
8409 * Is this nexus a reservation holder?
8411 if (lun->pr_res_idx != residx
8412 && lun->pr_res_idx != CTL_PR_ALL_REGISTRANTS) {
8414 * not a res holder return good status but
8417 mtx_unlock(&lun->lun_lock);
8421 if (lun->pr_res_type != type) {
8422 mtx_unlock(&lun->lun_lock);
8423 free(ctsio->kern_data_ptr, M_CTL);
8424 ctl_set_illegal_pr_release(ctsio);
8425 ctl_done((union ctl_io *)ctsio);
8426 return (CTL_RETVAL_COMPLETE);
8429 /* okay to release */
8430 lun->flags &= ~CTL_LUN_PR_RESERVED;
8431 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8432 lun->pr_res_type = 0;
8435 * If this isn't an exclusive access reservation and NUAR
8436 * is not set, generate UA for all other registrants.
8438 if (type != SPR_TYPE_EX_AC && type != SPR_TYPE_WR_EX &&
8439 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8440 for (i = softc->init_min; i < softc->init_max; i++) {
8441 if (i == residx || ctl_get_prkey(lun, i) == 0)
8443 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8446 mtx_unlock(&lun->lun_lock);
8448 /* Send msg to other side */
8449 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8450 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8451 persis_io.pr.pr_info.action = CTL_PR_RELEASE;
8452 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8453 sizeof(persis_io.pr), M_WAITOK);
8457 /* send msg to other side */
8459 mtx_lock(&lun->lun_lock);
8460 lun->flags &= ~CTL_LUN_PR_RESERVED;
8461 lun->pr_res_type = 0;
8462 lun->pr_key_count = 0;
8463 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8465 ctl_clr_prkey(lun, residx);
8466 for (i = 0; i < CTL_MAX_INITIATORS; i++)
8467 if (ctl_get_prkey(lun, i) != 0) {
8468 ctl_clr_prkey(lun, i);
8469 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8471 lun->pr_generation++;
8472 mtx_unlock(&lun->lun_lock);
8474 persis_io.hdr.nexus = ctsio->io_hdr.nexus;
8475 persis_io.hdr.msg_type = CTL_MSG_PERS_ACTION;
8476 persis_io.pr.pr_info.action = CTL_PR_CLEAR;
8477 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &persis_io,
8478 sizeof(persis_io.pr), M_WAITOK);
8482 case SPRO_PRE_ABO: {
8485 nretval = ctl_pro_preempt(softc, lun, res_key, sa_res_key, type,
8486 residx, ctsio, cdb, param);
8488 return (CTL_RETVAL_COMPLETE);
8492 panic("%s: Invalid PR type %#x", __func__, cdb->action);
8496 free(ctsio->kern_data_ptr, M_CTL);
8497 ctl_set_success(ctsio);
8498 ctl_done((union ctl_io *)ctsio);
8504 * This routine is for handling a message from the other SC pertaining to
8505 * persistent reserve out. All the error checking will have been done
8506 * so only perorming the action need be done here to keep the two
8510 ctl_hndl_per_res_out_on_other_sc(union ctl_io *io)
8512 struct ctl_softc *softc = CTL_SOFTC(io);
8513 union ctl_ha_msg *msg = (union ctl_ha_msg *)&io->presio.pr_msg;
8514 struct ctl_lun *lun;
8516 uint32_t residx, targ_lun;
8518 targ_lun = msg->hdr.nexus.targ_mapped_lun;
8519 mtx_lock(&softc->ctl_lock);
8520 if (targ_lun >= ctl_max_luns ||
8521 (lun = softc->ctl_luns[targ_lun]) == NULL) {
8522 mtx_unlock(&softc->ctl_lock);
8525 mtx_lock(&lun->lun_lock);
8526 mtx_unlock(&softc->ctl_lock);
8527 if (lun->flags & CTL_LUN_DISABLED) {
8528 mtx_unlock(&lun->lun_lock);
8531 residx = ctl_get_initindex(&msg->hdr.nexus);
8532 switch(msg->pr.pr_info.action) {
8533 case CTL_PR_REG_KEY:
8534 ctl_alloc_prkey(lun, msg->pr.pr_info.residx);
8535 if (ctl_get_prkey(lun, msg->pr.pr_info.residx) == 0)
8536 lun->pr_key_count++;
8537 ctl_set_prkey(lun, msg->pr.pr_info.residx,
8538 scsi_8btou64(msg->pr.pr_info.sa_res_key));
8539 lun->pr_generation++;
8542 case CTL_PR_UNREG_KEY:
8543 ctl_clr_prkey(lun, msg->pr.pr_info.residx);
8544 lun->pr_key_count--;
8546 /* XXX Need to see if the reservation has been released */
8547 /* if so do we need to generate UA? */
8548 if (msg->pr.pr_info.residx == lun->pr_res_idx) {
8549 lun->flags &= ~CTL_LUN_PR_RESERVED;
8550 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8552 if ((lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
8553 lun->pr_res_type == SPR_TYPE_EX_AC_RO) &&
8554 lun->pr_key_count) {
8556 * If the reservation is a registrants
8557 * only type we need to generate a UA
8558 * for other registered inits. The
8559 * sense code should be RESERVATIONS
8563 for (i = softc->init_min; i < softc->init_max; i++) {
8564 if (ctl_get_prkey(lun, i) == 0)
8567 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8570 lun->pr_res_type = 0;
8571 } else if (lun->pr_res_idx == CTL_PR_ALL_REGISTRANTS) {
8572 if (lun->pr_key_count==0) {
8573 lun->flags &= ~CTL_LUN_PR_RESERVED;
8574 lun->pr_res_type = 0;
8575 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8578 lun->pr_generation++;
8581 case CTL_PR_RESERVE:
8582 lun->flags |= CTL_LUN_PR_RESERVED;
8583 lun->pr_res_type = msg->pr.pr_info.res_type;
8584 lun->pr_res_idx = msg->pr.pr_info.residx;
8588 case CTL_PR_RELEASE:
8590 * If this isn't an exclusive access reservation and NUAR
8591 * is not set, generate UA for all other registrants.
8593 if (lun->pr_res_type != SPR_TYPE_EX_AC &&
8594 lun->pr_res_type != SPR_TYPE_WR_EX &&
8595 (lun->MODE_CTRL.queue_flags & SCP_NUAR) == 0) {
8596 for (i = softc->init_min; i < softc->init_max; i++) {
8597 if (i == residx || ctl_get_prkey(lun, i) == 0)
8599 ctl_est_ua(lun, i, CTL_UA_RES_RELEASE);
8603 lun->flags &= ~CTL_LUN_PR_RESERVED;
8604 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8605 lun->pr_res_type = 0;
8608 case CTL_PR_PREEMPT:
8609 ctl_pro_preempt_other(lun, msg);
8612 lun->flags &= ~CTL_LUN_PR_RESERVED;
8613 lun->pr_res_type = 0;
8614 lun->pr_key_count = 0;
8615 lun->pr_res_idx = CTL_PR_NO_RESERVATION;
8617 for (i=0; i < CTL_MAX_INITIATORS; i++) {
8618 if (ctl_get_prkey(lun, i) == 0)
8620 ctl_clr_prkey(lun, i);
8621 ctl_est_ua(lun, i, CTL_UA_REG_PREEMPT);
8623 lun->pr_generation++;
8627 mtx_unlock(&lun->lun_lock);
8631 ctl_read_write(struct ctl_scsiio *ctsio)
8633 struct ctl_lun *lun = CTL_LUN(ctsio);
8634 struct ctl_lba_len_flags *lbalen;
8636 uint32_t num_blocks;
8640 CTL_DEBUG_PRINT(("ctl_read_write: command: %#x\n", ctsio->cdb[0]));
8643 isread = ctsio->cdb[0] == READ_6 || ctsio->cdb[0] == READ_10
8644 || ctsio->cdb[0] == READ_12 || ctsio->cdb[0] == READ_16;
8645 switch (ctsio->cdb[0]) {
8648 struct scsi_rw_6 *cdb;
8650 cdb = (struct scsi_rw_6 *)ctsio->cdb;
8652 lba = scsi_3btoul(cdb->addr);
8653 /* only 5 bits are valid in the most significant address byte */
8655 num_blocks = cdb->length;
8657 * This is correct according to SBC-2.
8659 if (num_blocks == 0)
8665 struct scsi_rw_10 *cdb;
8667 cdb = (struct scsi_rw_10 *)ctsio->cdb;
8668 if (cdb->byte2 & SRW10_FUA)
8669 flags |= CTL_LLF_FUA;
8670 if (cdb->byte2 & SRW10_DPO)
8671 flags |= CTL_LLF_DPO;
8672 lba = scsi_4btoul(cdb->addr);
8673 num_blocks = scsi_2btoul(cdb->length);
8676 case WRITE_VERIFY_10: {
8677 struct scsi_write_verify_10 *cdb;
8679 cdb = (struct scsi_write_verify_10 *)ctsio->cdb;
8680 flags |= CTL_LLF_FUA;
8681 if (cdb->byte2 & SWV_DPO)
8682 flags |= CTL_LLF_DPO;
8683 lba = scsi_4btoul(cdb->addr);
8684 num_blocks = scsi_2btoul(cdb->length);
8689 struct scsi_rw_12 *cdb;
8691 cdb = (struct scsi_rw_12 *)ctsio->cdb;
8692 if (cdb->byte2 & SRW12_FUA)
8693 flags |= CTL_LLF_FUA;
8694 if (cdb->byte2 & SRW12_DPO)
8695 flags |= CTL_LLF_DPO;
8696 lba = scsi_4btoul(cdb->addr);
8697 num_blocks = scsi_4btoul(cdb->length);
8700 case WRITE_VERIFY_12: {
8701 struct scsi_write_verify_12 *cdb;
8703 cdb = (struct scsi_write_verify_12 *)ctsio->cdb;
8704 flags |= CTL_LLF_FUA;
8705 if (cdb->byte2 & SWV_DPO)
8706 flags |= CTL_LLF_DPO;
8707 lba = scsi_4btoul(cdb->addr);
8708 num_blocks = scsi_4btoul(cdb->length);
8713 struct scsi_rw_16 *cdb;
8715 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8716 if (cdb->byte2 & SRW12_FUA)
8717 flags |= CTL_LLF_FUA;
8718 if (cdb->byte2 & SRW12_DPO)
8719 flags |= CTL_LLF_DPO;
8720 lba = scsi_8btou64(cdb->addr);
8721 num_blocks = scsi_4btoul(cdb->length);
8724 case WRITE_ATOMIC_16: {
8725 struct scsi_write_atomic_16 *cdb;
8727 if (lun->be_lun->atomicblock == 0) {
8728 ctl_set_invalid_opcode(ctsio);
8729 ctl_done((union ctl_io *)ctsio);
8730 return (CTL_RETVAL_COMPLETE);
8733 cdb = (struct scsi_write_atomic_16 *)ctsio->cdb;
8734 if (cdb->byte2 & SRW12_FUA)
8735 flags |= CTL_LLF_FUA;
8736 if (cdb->byte2 & SRW12_DPO)
8737 flags |= CTL_LLF_DPO;
8738 lba = scsi_8btou64(cdb->addr);
8739 num_blocks = scsi_2btoul(cdb->length);
8740 if (num_blocks > lun->be_lun->atomicblock) {
8741 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1,
8742 /*command*/ 1, /*field*/ 12, /*bit_valid*/ 0,
8744 ctl_done((union ctl_io *)ctsio);
8745 return (CTL_RETVAL_COMPLETE);
8749 case WRITE_VERIFY_16: {
8750 struct scsi_write_verify_16 *cdb;
8752 cdb = (struct scsi_write_verify_16 *)ctsio->cdb;
8753 flags |= CTL_LLF_FUA;
8754 if (cdb->byte2 & SWV_DPO)
8755 flags |= CTL_LLF_DPO;
8756 lba = scsi_8btou64(cdb->addr);
8757 num_blocks = scsi_4btoul(cdb->length);
8762 * We got a command we don't support. This shouldn't
8763 * happen, commands should be filtered out above us.
8765 ctl_set_invalid_opcode(ctsio);
8766 ctl_done((union ctl_io *)ctsio);
8768 return (CTL_RETVAL_COMPLETE);
8769 break; /* NOTREACHED */
8773 * The first check is to make sure we're in bounds, the second
8774 * check is to catch wrap-around problems. If the lba + num blocks
8775 * is less than the lba, then we've wrapped around and the block
8776 * range is invalid anyway.
8778 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8779 || ((lba + num_blocks) < lba)) {
8780 ctl_set_lba_out_of_range(ctsio,
8781 MAX(lba, lun->be_lun->maxlba + 1));
8782 ctl_done((union ctl_io *)ctsio);
8783 return (CTL_RETVAL_COMPLETE);
8787 * According to SBC-3, a transfer length of 0 is not an error.
8788 * Note that this cannot happen with WRITE(6) or READ(6), since 0
8789 * translates to 256 blocks for those commands.
8791 if (num_blocks == 0) {
8792 ctl_set_success(ctsio);
8793 ctl_done((union ctl_io *)ctsio);
8794 return (CTL_RETVAL_COMPLETE);
8797 /* Set FUA and/or DPO if caches are disabled. */
8799 if ((lun->MODE_CACHING.flags1 & SCP_RCD) != 0)
8800 flags |= CTL_LLF_FUA | CTL_LLF_DPO;
8802 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8803 flags |= CTL_LLF_FUA;
8806 lbalen = (struct ctl_lba_len_flags *)
8807 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8809 lbalen->len = num_blocks;
8810 lbalen->flags = (isread ? CTL_LLF_READ : CTL_LLF_WRITE) | flags;
8812 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
8813 ctsio->kern_rel_offset = 0;
8815 CTL_DEBUG_PRINT(("ctl_read_write: calling data_submit()\n"));
8817 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8822 ctl_cnw_cont(union ctl_io *io)
8824 struct ctl_lun *lun = CTL_LUN(io);
8825 struct ctl_scsiio *ctsio;
8826 struct ctl_lba_len_flags *lbalen;
8829 ctsio = &io->scsiio;
8830 ctsio->io_hdr.status = CTL_STATUS_NONE;
8831 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_CONT;
8832 lbalen = (struct ctl_lba_len_flags *)
8833 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8834 lbalen->flags &= ~CTL_LLF_COMPARE;
8835 lbalen->flags |= CTL_LLF_WRITE;
8837 CTL_DEBUG_PRINT(("ctl_cnw_cont: calling data_submit()\n"));
8838 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8843 ctl_cnw(struct ctl_scsiio *ctsio)
8845 struct ctl_lun *lun = CTL_LUN(ctsio);
8846 struct ctl_lba_len_flags *lbalen;
8848 uint32_t num_blocks;
8851 CTL_DEBUG_PRINT(("ctl_cnw: command: %#x\n", ctsio->cdb[0]));
8854 switch (ctsio->cdb[0]) {
8855 case COMPARE_AND_WRITE: {
8856 struct scsi_compare_and_write *cdb;
8858 cdb = (struct scsi_compare_and_write *)ctsio->cdb;
8859 if (cdb->byte2 & SRW10_FUA)
8860 flags |= CTL_LLF_FUA;
8861 if (cdb->byte2 & SRW10_DPO)
8862 flags |= CTL_LLF_DPO;
8863 lba = scsi_8btou64(cdb->addr);
8864 num_blocks = cdb->length;
8869 * We got a command we don't support. This shouldn't
8870 * happen, commands should be filtered out above us.
8872 ctl_set_invalid_opcode(ctsio);
8873 ctl_done((union ctl_io *)ctsio);
8875 return (CTL_RETVAL_COMPLETE);
8876 break; /* NOTREACHED */
8880 * The first check is to make sure we're in bounds, the second
8881 * check is to catch wrap-around problems. If the lba + num blocks
8882 * is less than the lba, then we've wrapped around and the block
8883 * range is invalid anyway.
8885 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8886 || ((lba + num_blocks) < lba)) {
8887 ctl_set_lba_out_of_range(ctsio,
8888 MAX(lba, lun->be_lun->maxlba + 1));
8889 ctl_done((union ctl_io *)ctsio);
8890 return (CTL_RETVAL_COMPLETE);
8894 * According to SBC-3, a transfer length of 0 is not an error.
8896 if (num_blocks == 0) {
8897 ctl_set_success(ctsio);
8898 ctl_done((union ctl_io *)ctsio);
8899 return (CTL_RETVAL_COMPLETE);
8902 /* Set FUA if write cache is disabled. */
8903 if ((lun->MODE_CACHING.flags1 & SCP_WCE) == 0)
8904 flags |= CTL_LLF_FUA;
8906 ctsio->kern_total_len = 2 * num_blocks * lun->be_lun->blocksize;
8907 ctsio->kern_rel_offset = 0;
8910 * Set the IO_CONT flag, so that if this I/O gets passed to
8911 * ctl_data_submit_done(), it'll get passed back to
8912 * ctl_ctl_cnw_cont() for further processing.
8914 ctsio->io_hdr.flags |= CTL_FLAG_IO_CONT;
8915 ctsio->io_cont = ctl_cnw_cont;
8917 lbalen = (struct ctl_lba_len_flags *)
8918 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
8920 lbalen->len = num_blocks;
8921 lbalen->flags = CTL_LLF_COMPARE | flags;
8923 CTL_DEBUG_PRINT(("ctl_cnw: calling data_submit()\n"));
8924 retval = lun->backend->data_submit((union ctl_io *)ctsio);
8929 ctl_verify(struct ctl_scsiio *ctsio)
8931 struct ctl_lun *lun = CTL_LUN(ctsio);
8932 struct ctl_lba_len_flags *lbalen;
8934 uint32_t num_blocks;
8938 CTL_DEBUG_PRINT(("ctl_verify: command: %#x\n", ctsio->cdb[0]));
8941 flags = CTL_LLF_FUA;
8942 switch (ctsio->cdb[0]) {
8944 struct scsi_verify_10 *cdb;
8946 cdb = (struct scsi_verify_10 *)ctsio->cdb;
8947 if (cdb->byte2 & SVFY_BYTCHK)
8949 if (cdb->byte2 & SVFY_DPO)
8950 flags |= CTL_LLF_DPO;
8951 lba = scsi_4btoul(cdb->addr);
8952 num_blocks = scsi_2btoul(cdb->length);
8956 struct scsi_verify_12 *cdb;
8958 cdb = (struct scsi_verify_12 *)ctsio->cdb;
8959 if (cdb->byte2 & SVFY_BYTCHK)
8961 if (cdb->byte2 & SVFY_DPO)
8962 flags |= CTL_LLF_DPO;
8963 lba = scsi_4btoul(cdb->addr);
8964 num_blocks = scsi_4btoul(cdb->length);
8968 struct scsi_rw_16 *cdb;
8970 cdb = (struct scsi_rw_16 *)ctsio->cdb;
8971 if (cdb->byte2 & SVFY_BYTCHK)
8973 if (cdb->byte2 & SVFY_DPO)
8974 flags |= CTL_LLF_DPO;
8975 lba = scsi_8btou64(cdb->addr);
8976 num_blocks = scsi_4btoul(cdb->length);
8981 * We got a command we don't support. This shouldn't
8982 * happen, commands should be filtered out above us.
8984 ctl_set_invalid_opcode(ctsio);
8985 ctl_done((union ctl_io *)ctsio);
8986 return (CTL_RETVAL_COMPLETE);
8990 * The first check is to make sure we're in bounds, the second
8991 * check is to catch wrap-around problems. If the lba + num blocks
8992 * is less than the lba, then we've wrapped around and the block
8993 * range is invalid anyway.
8995 if (((lba + num_blocks) > (lun->be_lun->maxlba + 1))
8996 || ((lba + num_blocks) < lba)) {
8997 ctl_set_lba_out_of_range(ctsio,
8998 MAX(lba, lun->be_lun->maxlba + 1));
8999 ctl_done((union ctl_io *)ctsio);
9000 return (CTL_RETVAL_COMPLETE);
9004 * According to SBC-3, a transfer length of 0 is not an error.
9006 if (num_blocks == 0) {
9007 ctl_set_success(ctsio);
9008 ctl_done((union ctl_io *)ctsio);
9009 return (CTL_RETVAL_COMPLETE);
9012 lbalen = (struct ctl_lba_len_flags *)
9013 &ctsio->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
9015 lbalen->len = num_blocks;
9017 lbalen->flags = CTL_LLF_COMPARE | flags;
9018 ctsio->kern_total_len = num_blocks * lun->be_lun->blocksize;
9020 lbalen->flags = CTL_LLF_VERIFY | flags;
9021 ctsio->kern_total_len = 0;
9023 ctsio->kern_rel_offset = 0;
9025 CTL_DEBUG_PRINT(("ctl_verify: calling data_submit()\n"));
9026 retval = lun->backend->data_submit((union ctl_io *)ctsio);
9031 ctl_report_luns(struct ctl_scsiio *ctsio)
9033 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9034 struct ctl_port *port = CTL_PORT(ctsio);
9035 struct ctl_lun *lun, *request_lun = CTL_LUN(ctsio);
9036 struct scsi_report_luns *cdb;
9037 struct scsi_report_luns_data *lun_data;
9038 int num_filled, num_luns, num_port_luns, retval;
9039 uint32_t alloc_len, lun_datalen;
9040 uint32_t initidx, targ_lun_id, lun_id;
9042 retval = CTL_RETVAL_COMPLETE;
9043 cdb = (struct scsi_report_luns *)ctsio->cdb;
9045 CTL_DEBUG_PRINT(("ctl_report_luns\n"));
9048 num_port_luns = port->lun_map ? port->lun_map_size : ctl_max_luns;
9049 mtx_lock(&softc->ctl_lock);
9050 for (targ_lun_id = 0; targ_lun_id < num_port_luns; targ_lun_id++) {
9051 if (ctl_lun_map_from_port(port, targ_lun_id) != UINT32_MAX)
9054 mtx_unlock(&softc->ctl_lock);
9056 switch (cdb->select_report) {
9057 case RPL_REPORT_DEFAULT:
9058 case RPL_REPORT_ALL:
9059 case RPL_REPORT_NONSUBSID:
9061 case RPL_REPORT_WELLKNOWN:
9062 case RPL_REPORT_ADMIN:
9063 case RPL_REPORT_CONGLOM:
9067 ctl_set_invalid_field(ctsio,
9073 ctl_done((union ctl_io *)ctsio);
9075 break; /* NOTREACHED */
9078 alloc_len = scsi_4btoul(cdb->length);
9080 * The initiator has to allocate at least 16 bytes for this request,
9081 * so he can at least get the header and the first LUN. Otherwise
9082 * we reject the request (per SPC-3 rev 14, section 6.21).
9084 if (alloc_len < (sizeof(struct scsi_report_luns_data) +
9085 sizeof(struct scsi_report_luns_lundata))) {
9086 ctl_set_invalid_field(ctsio,
9092 ctl_done((union ctl_io *)ctsio);
9096 lun_datalen = sizeof(*lun_data) +
9097 (num_luns * sizeof(struct scsi_report_luns_lundata));
9099 ctsio->kern_data_ptr = malloc(lun_datalen, M_CTL, M_WAITOK | M_ZERO);
9100 lun_data = (struct scsi_report_luns_data *)ctsio->kern_data_ptr;
9101 ctsio->kern_sg_entries = 0;
9103 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9105 mtx_lock(&softc->ctl_lock);
9106 for (targ_lun_id = 0, num_filled = 0;
9107 targ_lun_id < num_port_luns && num_filled < num_luns;
9109 lun_id = ctl_lun_map_from_port(port, targ_lun_id);
9110 if (lun_id == UINT32_MAX)
9112 lun = softc->ctl_luns[lun_id];
9116 be64enc(lun_data->luns[num_filled++].lundata,
9117 ctl_encode_lun(targ_lun_id));
9120 * According to SPC-3, rev 14 section 6.21:
9122 * "The execution of a REPORT LUNS command to any valid and
9123 * installed logical unit shall clear the REPORTED LUNS DATA
9124 * HAS CHANGED unit attention condition for all logical
9125 * units of that target with respect to the requesting
9126 * initiator. A valid and installed logical unit is one
9127 * having a PERIPHERAL QUALIFIER of 000b in the standard
9128 * INQUIRY data (see 6.4.2)."
9130 * If request_lun is NULL, the LUN this report luns command
9131 * was issued to is either disabled or doesn't exist. In that
9132 * case, we shouldn't clear any pending lun change unit
9135 if (request_lun != NULL) {
9136 mtx_lock(&lun->lun_lock);
9137 ctl_clr_ua(lun, initidx, CTL_UA_LUN_CHANGE);
9138 mtx_unlock(&lun->lun_lock);
9141 mtx_unlock(&softc->ctl_lock);
9144 * It's quite possible that we've returned fewer LUNs than we allocated
9145 * space for. Trim it.
9147 lun_datalen = sizeof(*lun_data) +
9148 (num_filled * sizeof(struct scsi_report_luns_lundata));
9149 ctsio->kern_rel_offset = 0;
9150 ctsio->kern_sg_entries = 0;
9151 ctsio->kern_data_len = min(lun_datalen, alloc_len);
9152 ctsio->kern_total_len = ctsio->kern_data_len;
9155 * We set this to the actual data length, regardless of how much
9156 * space we actually have to return results. If the user looks at
9157 * this value, he'll know whether or not he allocated enough space
9158 * and reissue the command if necessary. We don't support well
9159 * known logical units, so if the user asks for that, return none.
9161 scsi_ulto4b(lun_datalen - 8, lun_data->length);
9164 * We can only return SCSI_STATUS_CHECK_COND when we can't satisfy
9167 ctl_set_success(ctsio);
9168 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9169 ctsio->be_move_done = ctl_config_move_done;
9170 ctl_datamove((union ctl_io *)ctsio);
9175 ctl_request_sense(struct ctl_scsiio *ctsio)
9177 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9178 struct ctl_lun *lun = CTL_LUN(ctsio);
9179 struct scsi_request_sense *cdb;
9180 struct scsi_sense_data *sense_ptr, *ps;
9183 u_int sense_len = SSD_FULL_SIZE;
9184 scsi_sense_data_type sense_format;
9185 ctl_ua_type ua_type;
9186 uint8_t asc = 0, ascq = 0;
9188 cdb = (struct scsi_request_sense *)ctsio->cdb;
9190 CTL_DEBUG_PRINT(("ctl_request_sense\n"));
9193 * Determine which sense format the user wants.
9195 if (cdb->byte2 & SRS_DESC)
9196 sense_format = SSD_TYPE_DESC;
9198 sense_format = SSD_TYPE_FIXED;
9200 ctsio->kern_data_ptr = malloc(sizeof(*sense_ptr), M_CTL, M_WAITOK);
9201 sense_ptr = (struct scsi_sense_data *)ctsio->kern_data_ptr;
9202 ctsio->kern_sg_entries = 0;
9203 ctsio->kern_rel_offset = 0;
9206 * struct scsi_sense_data, which is currently set to 256 bytes, is
9207 * larger than the largest allowed value for the length field in the
9208 * REQUEST SENSE CDB, which is 252 bytes as of SPC-4.
9210 ctsio->kern_data_len = cdb->length;
9211 ctsio->kern_total_len = cdb->length;
9214 * If we don't have a LUN, we don't have any pending sense.
9217 ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
9218 softc->ha_link < CTL_HA_LINK_UNKNOWN)) {
9219 /* "Logical unit not supported" */
9220 ctl_set_sense_data(sense_ptr, &sense_len, NULL, sense_format,
9221 /*current_error*/ 1,
9222 /*sense_key*/ SSD_KEY_ILLEGAL_REQUEST,
9230 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
9232 * Check for pending sense, and then for pending unit attentions.
9233 * Pending sense gets returned first, then pending unit attentions.
9235 mtx_lock(&lun->lun_lock);
9236 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
9238 ps += initidx % CTL_MAX_INIT_PER_PORT;
9239 if (ps != NULL && ps->error_code != 0) {
9240 scsi_sense_data_type stored_format;
9243 * Check to see which sense format was used for the stored
9246 stored_format = scsi_sense_type(ps);
9249 * If the user requested a different sense format than the
9250 * one we stored, then we need to convert it to the other
9251 * format. If we're going from descriptor to fixed format
9252 * sense data, we may lose things in translation, depending
9253 * on what options were used.
9255 * If the stored format is SSD_TYPE_NONE (i.e. invalid),
9256 * for some reason we'll just copy it out as-is.
9258 if ((stored_format == SSD_TYPE_FIXED)
9259 && (sense_format == SSD_TYPE_DESC))
9260 ctl_sense_to_desc((struct scsi_sense_data_fixed *)
9261 ps, (struct scsi_sense_data_desc *)sense_ptr);
9262 else if ((stored_format == SSD_TYPE_DESC)
9263 && (sense_format == SSD_TYPE_FIXED))
9264 ctl_sense_to_fixed((struct scsi_sense_data_desc *)
9265 ps, (struct scsi_sense_data_fixed *)sense_ptr);
9267 memcpy(sense_ptr, ps, sizeof(*sense_ptr));
9272 ua_type = ctl_build_ua(lun, initidx, sense_ptr, &sense_len,
9274 if (ua_type != CTL_UA_NONE)
9277 if (have_error == 0) {
9279 * Report informational exception if have one and allowed.
9281 if (lun->MODE_IE.mrie != SIEP_MRIE_NO) {
9283 ascq = lun->ie_ascq;
9285 ctl_set_sense_data(sense_ptr, &sense_len, lun, sense_format,
9286 /*current_error*/ 1,
9287 /*sense_key*/ SSD_KEY_NO_SENSE,
9292 mtx_unlock(&lun->lun_lock);
9296 * We report the SCSI status as OK, since the status of the command
9297 * itself is OK. We're reporting sense as parameter data.
9299 ctl_set_success(ctsio);
9300 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9301 ctsio->be_move_done = ctl_config_move_done;
9302 ctl_datamove((union ctl_io *)ctsio);
9303 return (CTL_RETVAL_COMPLETE);
9307 ctl_tur(struct ctl_scsiio *ctsio)
9310 CTL_DEBUG_PRINT(("ctl_tur\n"));
9312 ctl_set_success(ctsio);
9313 ctl_done((union ctl_io *)ctsio);
9315 return (CTL_RETVAL_COMPLETE);
9319 * SCSI VPD page 0x00, the Supported VPD Pages page.
9322 ctl_inquiry_evpd_supported(struct ctl_scsiio *ctsio, int alloc_len)
9324 struct ctl_lun *lun = CTL_LUN(ctsio);
9325 struct scsi_vpd_supported_pages *pages;
9329 sup_page_size = sizeof(struct scsi_vpd_supported_pages) *
9330 SCSI_EVPD_NUM_SUPPORTED_PAGES;
9331 ctsio->kern_data_ptr = malloc(sup_page_size, M_CTL, M_WAITOK | M_ZERO);
9332 pages = (struct scsi_vpd_supported_pages *)ctsio->kern_data_ptr;
9333 ctsio->kern_rel_offset = 0;
9334 ctsio->kern_sg_entries = 0;
9335 ctsio->kern_data_len = min(sup_page_size, alloc_len);
9336 ctsio->kern_total_len = ctsio->kern_data_len;
9339 * The control device is always connected. The disk device, on the
9340 * other hand, may not be online all the time. Need to change this
9341 * to figure out whether the disk device is actually online or not.
9344 pages->device = (SID_QUAL_LU_CONNECTED << 5) |
9345 lun->be_lun->lun_type;
9347 pages->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9350 /* Supported VPD pages */
9351 pages->page_list[p++] = SVPD_SUPPORTED_PAGES;
9353 pages->page_list[p++] = SVPD_UNIT_SERIAL_NUMBER;
9354 /* Device Identification */
9355 pages->page_list[p++] = SVPD_DEVICE_ID;
9356 /* Extended INQUIRY Data */
9357 pages->page_list[p++] = SVPD_EXTENDED_INQUIRY_DATA;
9358 /* Mode Page Policy */
9359 pages->page_list[p++] = SVPD_MODE_PAGE_POLICY;
9361 pages->page_list[p++] = SVPD_SCSI_PORTS;
9362 /* Third-party Copy */
9363 pages->page_list[p++] = SVPD_SCSI_TPC;
9364 /* SCSI Feature Sets */
9365 pages->page_list[p++] = SVPD_SCSI_SFS;
9366 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9368 pages->page_list[p++] = SVPD_BLOCK_LIMITS;
9369 /* Block Device Characteristics */
9370 pages->page_list[p++] = SVPD_BDC;
9371 /* Logical Block Provisioning */
9372 pages->page_list[p++] = SVPD_LBP;
9376 ctl_set_success(ctsio);
9377 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9378 ctsio->be_move_done = ctl_config_move_done;
9379 ctl_datamove((union ctl_io *)ctsio);
9380 return (CTL_RETVAL_COMPLETE);
9384 * SCSI VPD page 0x80, the Unit Serial Number page.
9387 ctl_inquiry_evpd_serial(struct ctl_scsiio *ctsio, int alloc_len)
9389 struct ctl_lun *lun = CTL_LUN(ctsio);
9390 struct scsi_vpd_unit_serial_number *sn_ptr;
9393 data_len = 4 + CTL_SN_LEN;
9394 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9395 sn_ptr = (struct scsi_vpd_unit_serial_number *)ctsio->kern_data_ptr;
9396 ctsio->kern_rel_offset = 0;
9397 ctsio->kern_sg_entries = 0;
9398 ctsio->kern_data_len = min(data_len, alloc_len);
9399 ctsio->kern_total_len = ctsio->kern_data_len;
9402 * The control device is always connected. The disk device, on the
9403 * other hand, may not be online all the time. Need to change this
9404 * to figure out whether the disk device is actually online or not.
9407 sn_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9408 lun->be_lun->lun_type;
9410 sn_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9412 sn_ptr->page_code = SVPD_UNIT_SERIAL_NUMBER;
9413 sn_ptr->length = CTL_SN_LEN;
9415 * If we don't have a LUN, we just leave the serial number as
9419 strncpy((char *)sn_ptr->serial_num,
9420 (char *)lun->be_lun->serial_num, CTL_SN_LEN);
9422 memset(sn_ptr->serial_num, 0x20, CTL_SN_LEN);
9424 ctl_set_success(ctsio);
9425 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9426 ctsio->be_move_done = ctl_config_move_done;
9427 ctl_datamove((union ctl_io *)ctsio);
9428 return (CTL_RETVAL_COMPLETE);
9433 * SCSI VPD page 0x86, the Extended INQUIRY Data page.
9436 ctl_inquiry_evpd_eid(struct ctl_scsiio *ctsio, int alloc_len)
9438 struct ctl_lun *lun = CTL_LUN(ctsio);
9439 struct scsi_vpd_extended_inquiry_data *eid_ptr;
9442 data_len = sizeof(struct scsi_vpd_extended_inquiry_data);
9443 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9444 eid_ptr = (struct scsi_vpd_extended_inquiry_data *)ctsio->kern_data_ptr;
9445 ctsio->kern_sg_entries = 0;
9446 ctsio->kern_rel_offset = 0;
9447 ctsio->kern_data_len = min(data_len, alloc_len);
9448 ctsio->kern_total_len = ctsio->kern_data_len;
9451 * The control device is always connected. The disk device, on the
9452 * other hand, may not be online all the time.
9455 eid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9456 lun->be_lun->lun_type;
9458 eid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9459 eid_ptr->page_code = SVPD_EXTENDED_INQUIRY_DATA;
9460 scsi_ulto2b(data_len - 4, eid_ptr->page_length);
9462 * We support head of queue, ordered and simple tags.
9464 eid_ptr->flags2 = SVPD_EID_HEADSUP | SVPD_EID_ORDSUP | SVPD_EID_SIMPSUP;
9466 * Volatile cache supported.
9468 eid_ptr->flags3 = SVPD_EID_V_SUP;
9471 * This means that we clear the REPORTED LUNS DATA HAS CHANGED unit
9472 * attention for a particular IT nexus on all LUNs once we report
9473 * it to that nexus once. This bit is required as of SPC-4.
9475 eid_ptr->flags4 = SVPD_EID_LUICLR;
9478 * We support revert to defaults (RTD) bit in MODE SELECT.
9480 eid_ptr->flags5 = SVPD_EID_RTD_SUP;
9483 * XXX KDM in order to correctly answer this, we would need
9484 * information from the SIM to determine how much sense data it
9485 * can send. So this would really be a path inquiry field, most
9486 * likely. This can be set to a maximum of 252 according to SPC-4,
9487 * but the hardware may or may not be able to support that much.
9488 * 0 just means that the maximum sense data length is not reported.
9490 eid_ptr->max_sense_length = 0;
9492 ctl_set_success(ctsio);
9493 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9494 ctsio->be_move_done = ctl_config_move_done;
9495 ctl_datamove((union ctl_io *)ctsio);
9496 return (CTL_RETVAL_COMPLETE);
9500 ctl_inquiry_evpd_mpp(struct ctl_scsiio *ctsio, int alloc_len)
9502 struct ctl_lun *lun = CTL_LUN(ctsio);
9503 struct scsi_vpd_mode_page_policy *mpp_ptr;
9506 data_len = sizeof(struct scsi_vpd_mode_page_policy) +
9507 sizeof(struct scsi_vpd_mode_page_policy_descr);
9509 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9510 mpp_ptr = (struct scsi_vpd_mode_page_policy *)ctsio->kern_data_ptr;
9511 ctsio->kern_rel_offset = 0;
9512 ctsio->kern_sg_entries = 0;
9513 ctsio->kern_data_len = min(data_len, alloc_len);
9514 ctsio->kern_total_len = ctsio->kern_data_len;
9517 * The control device is always connected. The disk device, on the
9518 * other hand, may not be online all the time.
9521 mpp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9522 lun->be_lun->lun_type;
9524 mpp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9525 mpp_ptr->page_code = SVPD_MODE_PAGE_POLICY;
9526 scsi_ulto2b(data_len - 4, mpp_ptr->page_length);
9527 mpp_ptr->descr[0].page_code = 0x3f;
9528 mpp_ptr->descr[0].subpage_code = 0xff;
9529 mpp_ptr->descr[0].policy = SVPD_MPP_SHARED;
9531 ctl_set_success(ctsio);
9532 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9533 ctsio->be_move_done = ctl_config_move_done;
9534 ctl_datamove((union ctl_io *)ctsio);
9535 return (CTL_RETVAL_COMPLETE);
9539 * SCSI VPD page 0x83, the Device Identification page.
9542 ctl_inquiry_evpd_devid(struct ctl_scsiio *ctsio, int alloc_len)
9544 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9545 struct ctl_port *port = CTL_PORT(ctsio);
9546 struct ctl_lun *lun = CTL_LUN(ctsio);
9547 struct scsi_vpd_device_id *devid_ptr;
9548 struct scsi_vpd_id_descriptor *desc;
9552 data_len = sizeof(struct scsi_vpd_device_id) +
9553 sizeof(struct scsi_vpd_id_descriptor) +
9554 sizeof(struct scsi_vpd_id_rel_trgt_port_id) +
9555 sizeof(struct scsi_vpd_id_descriptor) +
9556 sizeof(struct scsi_vpd_id_trgt_port_grp_id);
9557 if (lun && lun->lun_devid)
9558 data_len += lun->lun_devid->len;
9559 if (port && port->port_devid)
9560 data_len += port->port_devid->len;
9561 if (port && port->target_devid)
9562 data_len += port->target_devid->len;
9564 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9565 devid_ptr = (struct scsi_vpd_device_id *)ctsio->kern_data_ptr;
9566 ctsio->kern_sg_entries = 0;
9567 ctsio->kern_rel_offset = 0;
9568 ctsio->kern_sg_entries = 0;
9569 ctsio->kern_data_len = min(data_len, alloc_len);
9570 ctsio->kern_total_len = ctsio->kern_data_len;
9573 * The control device is always connected. The disk device, on the
9574 * other hand, may not be online all the time.
9577 devid_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9578 lun->be_lun->lun_type;
9580 devid_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9581 devid_ptr->page_code = SVPD_DEVICE_ID;
9582 scsi_ulto2b(data_len - 4, devid_ptr->length);
9584 if (port && port->port_type == CTL_PORT_FC)
9585 proto = SCSI_PROTO_FC << 4;
9586 else if (port && port->port_type == CTL_PORT_SAS)
9587 proto = SCSI_PROTO_SAS << 4;
9588 else if (port && port->port_type == CTL_PORT_ISCSI)
9589 proto = SCSI_PROTO_ISCSI << 4;
9591 proto = SCSI_PROTO_SPI << 4;
9592 desc = (struct scsi_vpd_id_descriptor *)devid_ptr->desc_list;
9595 * We're using a LUN association here. i.e., this device ID is a
9596 * per-LUN identifier.
9598 if (lun && lun->lun_devid) {
9599 memcpy(desc, lun->lun_devid->data, lun->lun_devid->len);
9600 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9601 lun->lun_devid->len);
9605 * This is for the WWPN which is a port association.
9607 if (port && port->port_devid) {
9608 memcpy(desc, port->port_devid->data, port->port_devid->len);
9609 desc = (struct scsi_vpd_id_descriptor *)((uint8_t *)desc +
9610 port->port_devid->len);
9614 * This is for the Relative Target Port(type 4h) identifier
9616 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9617 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9618 SVPD_ID_TYPE_RELTARG;
9620 scsi_ulto2b(ctsio->io_hdr.nexus.targ_port, &desc->identifier[2]);
9621 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9622 sizeof(struct scsi_vpd_id_rel_trgt_port_id));
9625 * This is for the Target Port Group(type 5h) identifier
9627 desc->proto_codeset = proto | SVPD_ID_CODESET_BINARY;
9628 desc->id_type = SVPD_ID_PIV | SVPD_ID_ASSOC_PORT |
9629 SVPD_ID_TYPE_TPORTGRP;
9631 if (softc->is_single ||
9632 (port && port->status & CTL_PORT_STATUS_HA_SHARED))
9635 g = 2 + ctsio->io_hdr.nexus.targ_port / softc->port_cnt;
9636 scsi_ulto2b(g, &desc->identifier[2]);
9637 desc = (struct scsi_vpd_id_descriptor *)(&desc->identifier[0] +
9638 sizeof(struct scsi_vpd_id_trgt_port_grp_id));
9641 * This is for the Target identifier
9643 if (port && port->target_devid) {
9644 memcpy(desc, port->target_devid->data, port->target_devid->len);
9647 ctl_set_success(ctsio);
9648 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9649 ctsio->be_move_done = ctl_config_move_done;
9650 ctl_datamove((union ctl_io *)ctsio);
9651 return (CTL_RETVAL_COMPLETE);
9655 ctl_inquiry_evpd_scsi_ports(struct ctl_scsiio *ctsio, int alloc_len)
9657 struct ctl_softc *softc = CTL_SOFTC(ctsio);
9658 struct ctl_lun *lun = CTL_LUN(ctsio);
9659 struct scsi_vpd_scsi_ports *sp;
9660 struct scsi_vpd_port_designation *pd;
9661 struct scsi_vpd_port_designation_cont *pdc;
9662 struct ctl_port *port;
9663 int data_len, num_target_ports, iid_len, id_len;
9665 num_target_ports = 0;
9668 mtx_lock(&softc->ctl_lock);
9669 STAILQ_FOREACH(port, &softc->port_list, links) {
9670 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9673 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9676 if (port->init_devid)
9677 iid_len += port->init_devid->len;
9678 if (port->port_devid)
9679 id_len += port->port_devid->len;
9681 mtx_unlock(&softc->ctl_lock);
9683 data_len = sizeof(struct scsi_vpd_scsi_ports) +
9684 num_target_ports * (sizeof(struct scsi_vpd_port_designation) +
9685 sizeof(struct scsi_vpd_port_designation_cont)) + iid_len + id_len;
9686 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
9687 sp = (struct scsi_vpd_scsi_ports *)ctsio->kern_data_ptr;
9688 ctsio->kern_sg_entries = 0;
9689 ctsio->kern_rel_offset = 0;
9690 ctsio->kern_sg_entries = 0;
9691 ctsio->kern_data_len = min(data_len, alloc_len);
9692 ctsio->kern_total_len = ctsio->kern_data_len;
9695 * The control device is always connected. The disk device, on the
9696 * other hand, may not be online all the time. Need to change this
9697 * to figure out whether the disk device is actually online or not.
9700 sp->device = (SID_QUAL_LU_CONNECTED << 5) |
9701 lun->be_lun->lun_type;
9703 sp->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9705 sp->page_code = SVPD_SCSI_PORTS;
9706 scsi_ulto2b(data_len - sizeof(struct scsi_vpd_scsi_ports),
9708 pd = &sp->design[0];
9710 mtx_lock(&softc->ctl_lock);
9711 STAILQ_FOREACH(port, &softc->port_list, links) {
9712 if ((port->status & CTL_PORT_STATUS_ONLINE) == 0)
9715 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
9717 scsi_ulto2b(port->targ_port, pd->relative_port_id);
9718 if (port->init_devid) {
9719 iid_len = port->init_devid->len;
9720 memcpy(pd->initiator_transportid,
9721 port->init_devid->data, port->init_devid->len);
9724 scsi_ulto2b(iid_len, pd->initiator_transportid_length);
9725 pdc = (struct scsi_vpd_port_designation_cont *)
9726 (&pd->initiator_transportid[iid_len]);
9727 if (port->port_devid) {
9728 id_len = port->port_devid->len;
9729 memcpy(pdc->target_port_descriptors,
9730 port->port_devid->data, port->port_devid->len);
9733 scsi_ulto2b(id_len, pdc->target_port_descriptors_length);
9734 pd = (struct scsi_vpd_port_designation *)
9735 ((uint8_t *)pdc->target_port_descriptors + id_len);
9737 mtx_unlock(&softc->ctl_lock);
9739 ctl_set_success(ctsio);
9740 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9741 ctsio->be_move_done = ctl_config_move_done;
9742 ctl_datamove((union ctl_io *)ctsio);
9743 return (CTL_RETVAL_COMPLETE);
9747 ctl_inquiry_evpd_sfs(struct ctl_scsiio *ctsio, int alloc_len)
9749 struct ctl_lun *lun = CTL_LUN(ctsio);
9750 struct scsi_vpd_sfs *sfs_ptr;
9751 int sfs_page_size, n;
9753 sfs_page_size = sizeof(*sfs_ptr) + 5 * 2;
9754 ctsio->kern_data_ptr = malloc(sfs_page_size, M_CTL, M_WAITOK | M_ZERO);
9755 sfs_ptr = (struct scsi_vpd_sfs *)ctsio->kern_data_ptr;
9756 ctsio->kern_sg_entries = 0;
9757 ctsio->kern_rel_offset = 0;
9758 ctsio->kern_sg_entries = 0;
9759 ctsio->kern_data_len = min(sfs_page_size, alloc_len);
9760 ctsio->kern_total_len = ctsio->kern_data_len;
9763 * The control device is always connected. The disk device, on the
9764 * other hand, may not be online all the time. Need to change this
9765 * to figure out whether the disk device is actually online or not.
9768 sfs_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9769 lun->be_lun->lun_type;
9771 sfs_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9773 sfs_ptr->page_code = SVPD_SCSI_SFS;
9775 /* Discovery 2016 */
9776 scsi_ulto2b(0x0001, &sfs_ptr->codes[2 * n++]);
9777 if (lun != NULL && lun->be_lun->lun_type == T_DIRECT) {
9779 scsi_ulto2b(0x0101, &sfs_ptr->codes[2 * n++]);
9781 scsi_ulto2b(0x0102, &sfs_ptr->codes[2 * n++]);
9782 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9783 /* Basic Provisioning 2016 */
9784 scsi_ulto2b(0x0103, &sfs_ptr->codes[2 * n++]);
9786 /* Drive Maintenance 2016 */
9787 //scsi_ulto2b(0x0104, &sfs_ptr->codes[2 * n++]);
9789 scsi_ulto2b(4 + 2 * n, sfs_ptr->page_length);
9791 ctl_set_success(ctsio);
9792 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9793 ctsio->be_move_done = ctl_config_move_done;
9794 ctl_datamove((union ctl_io *)ctsio);
9795 return (CTL_RETVAL_COMPLETE);
9799 ctl_inquiry_evpd_block_limits(struct ctl_scsiio *ctsio, int alloc_len)
9801 struct ctl_lun *lun = CTL_LUN(ctsio);
9802 struct scsi_vpd_block_limits *bl_ptr;
9806 ctsio->kern_data_ptr = malloc(sizeof(*bl_ptr), M_CTL, M_WAITOK | M_ZERO);
9807 bl_ptr = (struct scsi_vpd_block_limits *)ctsio->kern_data_ptr;
9808 ctsio->kern_sg_entries = 0;
9809 ctsio->kern_rel_offset = 0;
9810 ctsio->kern_sg_entries = 0;
9811 ctsio->kern_data_len = min(sizeof(*bl_ptr), alloc_len);
9812 ctsio->kern_total_len = ctsio->kern_data_len;
9815 * The control device is always connected. The disk device, on the
9816 * other hand, may not be online all the time. Need to change this
9817 * to figure out whether the disk device is actually online or not.
9820 bl_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9821 lun->be_lun->lun_type;
9823 bl_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9825 bl_ptr->page_code = SVPD_BLOCK_LIMITS;
9826 scsi_ulto2b(sizeof(*bl_ptr) - 4, bl_ptr->page_length);
9827 bl_ptr->max_cmp_write_len = 0xff;
9828 scsi_ulto4b(0xffffffff, bl_ptr->max_txfer_len);
9830 scsi_ulto4b(lun->be_lun->opttxferlen, bl_ptr->opt_txfer_len);
9831 if (lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9833 val = dnvlist_get_string(lun->be_lun->options,
9834 "unmap_max_lba", NULL);
9836 ctl_expand_number(val, &ival);
9837 scsi_ulto4b(ival, bl_ptr->max_unmap_lba_cnt);
9839 val = dnvlist_get_string(lun->be_lun->options,
9840 "unmap_max_descr", NULL);
9842 ctl_expand_number(val, &ival);
9843 scsi_ulto4b(ival, bl_ptr->max_unmap_blk_cnt);
9844 if (lun->be_lun->ublockexp != 0) {
9845 scsi_ulto4b((1 << lun->be_lun->ublockexp),
9846 bl_ptr->opt_unmap_grain);
9847 scsi_ulto4b(0x80000000 | lun->be_lun->ublockoff,
9848 bl_ptr->unmap_grain_align);
9851 scsi_ulto4b(lun->be_lun->atomicblock,
9852 bl_ptr->max_atomic_transfer_length);
9853 scsi_ulto4b(0, bl_ptr->atomic_alignment);
9854 scsi_ulto4b(0, bl_ptr->atomic_transfer_length_granularity);
9855 scsi_ulto4b(0, bl_ptr->max_atomic_transfer_length_with_atomic_boundary);
9856 scsi_ulto4b(0, bl_ptr->max_atomic_boundary_size);
9858 val = dnvlist_get_string(lun->be_lun->options,
9859 "write_same_max_lba", NULL);
9861 ctl_expand_number(val, &ival);
9862 scsi_u64to8b(ival, bl_ptr->max_write_same_length);
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 ctl_inquiry_evpd_bdc(struct ctl_scsiio *ctsio, int alloc_len)
9875 struct ctl_lun *lun = CTL_LUN(ctsio);
9876 struct scsi_vpd_block_device_characteristics *bdc_ptr;
9880 ctsio->kern_data_ptr = malloc(sizeof(*bdc_ptr), M_CTL, M_WAITOK | M_ZERO);
9881 bdc_ptr = (struct scsi_vpd_block_device_characteristics *)ctsio->kern_data_ptr;
9882 ctsio->kern_sg_entries = 0;
9883 ctsio->kern_rel_offset = 0;
9884 ctsio->kern_data_len = min(sizeof(*bdc_ptr), alloc_len);
9885 ctsio->kern_total_len = ctsio->kern_data_len;
9888 * The control device is always connected. The disk device, on the
9889 * other hand, may not be online all the time. Need to change this
9890 * to figure out whether the disk device is actually online or not.
9893 bdc_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9894 lun->be_lun->lun_type;
9896 bdc_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9897 bdc_ptr->page_code = SVPD_BDC;
9898 scsi_ulto2b(sizeof(*bdc_ptr) - 4, bdc_ptr->page_length);
9900 (value = dnvlist_get_string(lun->be_lun->options, "rpm", NULL)) != NULL)
9901 i = strtol(value, NULL, 0);
9903 i = CTL_DEFAULT_ROTATION_RATE;
9904 scsi_ulto2b(i, bdc_ptr->medium_rotation_rate);
9906 (value = dnvlist_get_string(lun->be_lun->options, "formfactor", NULL)) != NULL)
9907 i = strtol(value, NULL, 0);
9910 bdc_ptr->wab_wac_ff = (i & 0x0f);
9911 bdc_ptr->flags = SVPD_RBWZ | SVPD_FUAB | SVPD_VBULS;
9913 ctl_set_success(ctsio);
9914 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9915 ctsio->be_move_done = ctl_config_move_done;
9916 ctl_datamove((union ctl_io *)ctsio);
9917 return (CTL_RETVAL_COMPLETE);
9921 ctl_inquiry_evpd_lbp(struct ctl_scsiio *ctsio, int alloc_len)
9923 struct ctl_lun *lun = CTL_LUN(ctsio);
9924 struct scsi_vpd_logical_block_prov *lbp_ptr;
9927 ctsio->kern_data_ptr = malloc(sizeof(*lbp_ptr), M_CTL, M_WAITOK | M_ZERO);
9928 lbp_ptr = (struct scsi_vpd_logical_block_prov *)ctsio->kern_data_ptr;
9929 ctsio->kern_sg_entries = 0;
9930 ctsio->kern_rel_offset = 0;
9931 ctsio->kern_data_len = min(sizeof(*lbp_ptr), alloc_len);
9932 ctsio->kern_total_len = ctsio->kern_data_len;
9935 * The control device is always connected. The disk device, on the
9936 * other hand, may not be online all the time. Need to change this
9937 * to figure out whether the disk device is actually online or not.
9940 lbp_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
9941 lun->be_lun->lun_type;
9943 lbp_ptr->device = (SID_QUAL_LU_OFFLINE << 5) | T_DIRECT;
9945 lbp_ptr->page_code = SVPD_LBP;
9946 scsi_ulto2b(sizeof(*lbp_ptr) - 4, lbp_ptr->page_length);
9947 lbp_ptr->threshold_exponent = CTL_LBP_EXPONENT;
9948 if (lun != NULL && lun->be_lun->flags & CTL_LUN_FLAG_UNMAP) {
9949 lbp_ptr->flags = SVPD_LBP_UNMAP | SVPD_LBP_WS16 |
9950 SVPD_LBP_WS10 | SVPD_LBP_RZ | SVPD_LBP_ANC_SUP;
9951 value = dnvlist_get_string(lun->be_lun->options,
9952 "provisioning_type", NULL);
9953 if (value != NULL) {
9954 if (strcmp(value, "resource") == 0)
9955 lbp_ptr->prov_type = SVPD_LBP_RESOURCE;
9956 else if (strcmp(value, "thin") == 0)
9957 lbp_ptr->prov_type = SVPD_LBP_THIN;
9959 lbp_ptr->prov_type = SVPD_LBP_THIN;
9962 ctl_set_success(ctsio);
9963 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
9964 ctsio->be_move_done = ctl_config_move_done;
9965 ctl_datamove((union ctl_io *)ctsio);
9966 return (CTL_RETVAL_COMPLETE);
9970 * INQUIRY with the EVPD bit set.
9973 ctl_inquiry_evpd(struct ctl_scsiio *ctsio)
9975 struct ctl_lun *lun = CTL_LUN(ctsio);
9976 struct scsi_inquiry *cdb;
9977 int alloc_len, retval;
9979 cdb = (struct scsi_inquiry *)ctsio->cdb;
9980 alloc_len = scsi_2btoul(cdb->length);
9982 switch (cdb->page_code) {
9983 case SVPD_SUPPORTED_PAGES:
9984 retval = ctl_inquiry_evpd_supported(ctsio, alloc_len);
9986 case SVPD_UNIT_SERIAL_NUMBER:
9987 retval = ctl_inquiry_evpd_serial(ctsio, alloc_len);
9989 case SVPD_DEVICE_ID:
9990 retval = ctl_inquiry_evpd_devid(ctsio, alloc_len);
9992 case SVPD_EXTENDED_INQUIRY_DATA:
9993 retval = ctl_inquiry_evpd_eid(ctsio, alloc_len);
9995 case SVPD_MODE_PAGE_POLICY:
9996 retval = ctl_inquiry_evpd_mpp(ctsio, alloc_len);
9998 case SVPD_SCSI_PORTS:
9999 retval = ctl_inquiry_evpd_scsi_ports(ctsio, alloc_len);
10001 case SVPD_SCSI_TPC:
10002 retval = ctl_inquiry_evpd_tpc(ctsio, alloc_len);
10004 case SVPD_SCSI_SFS:
10005 retval = ctl_inquiry_evpd_sfs(ctsio, alloc_len);
10007 case SVPD_BLOCK_LIMITS:
10008 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10010 retval = ctl_inquiry_evpd_block_limits(ctsio, alloc_len);
10013 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10015 retval = ctl_inquiry_evpd_bdc(ctsio, alloc_len);
10018 if (lun == NULL || lun->be_lun->lun_type != T_DIRECT)
10020 retval = ctl_inquiry_evpd_lbp(ctsio, alloc_len);
10024 ctl_set_invalid_field(ctsio,
10030 ctl_done((union ctl_io *)ctsio);
10031 retval = CTL_RETVAL_COMPLETE;
10039 * Standard INQUIRY data.
10042 ctl_inquiry_std(struct ctl_scsiio *ctsio)
10044 struct ctl_softc *softc = CTL_SOFTC(ctsio);
10045 struct ctl_port *port = CTL_PORT(ctsio);
10046 struct ctl_lun *lun = CTL_LUN(ctsio);
10047 struct scsi_inquiry_data *inq_ptr;
10048 struct scsi_inquiry *cdb;
10050 uint32_t alloc_len, data_len;
10051 ctl_port_type port_type;
10053 port_type = port->port_type;
10054 if (port_type == CTL_PORT_IOCTL || port_type == CTL_PORT_INTERNAL)
10055 port_type = CTL_PORT_SCSI;
10057 cdb = (struct scsi_inquiry *)ctsio->cdb;
10058 alloc_len = scsi_2btoul(cdb->length);
10061 * We malloc the full inquiry data size here and fill it
10062 * in. If the user only asks for less, we'll give him
10065 data_len = offsetof(struct scsi_inquiry_data, vendor_specific1);
10066 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10067 inq_ptr = (struct scsi_inquiry_data *)ctsio->kern_data_ptr;
10068 ctsio->kern_sg_entries = 0;
10069 ctsio->kern_rel_offset = 0;
10070 ctsio->kern_data_len = min(data_len, alloc_len);
10071 ctsio->kern_total_len = ctsio->kern_data_len;
10074 if ((lun->flags & CTL_LUN_PRIMARY_SC) ||
10075 softc->ha_link >= CTL_HA_LINK_UNKNOWN) {
10076 inq_ptr->device = (SID_QUAL_LU_CONNECTED << 5) |
10077 lun->be_lun->lun_type;
10079 inq_ptr->device = (SID_QUAL_LU_OFFLINE << 5) |
10080 lun->be_lun->lun_type;
10082 if (lun->flags & CTL_LUN_REMOVABLE)
10083 inq_ptr->dev_qual2 |= SID_RMB;
10085 inq_ptr->device = (SID_QUAL_BAD_LU << 5) | T_NODEVICE;
10087 /* RMB in byte 2 is 0 */
10088 inq_ptr->version = SCSI_REV_SPC5;
10091 * According to SAM-3, even if a device only supports a single
10092 * level of LUN addressing, it should still set the HISUP bit:
10094 * 4.9.1 Logical unit numbers overview
10096 * All logical unit number formats described in this standard are
10097 * hierarchical in structure even when only a single level in that
10098 * hierarchy is used. The HISUP bit shall be set to one in the
10099 * standard INQUIRY data (see SPC-2) when any logical unit number
10100 * format described in this standard is used. Non-hierarchical
10101 * formats are outside the scope of this standard.
10103 * Therefore we set the HiSup bit here.
10105 * The response format is 2, per SPC-3.
10107 inq_ptr->response_format = SID_HiSup | 2;
10109 inq_ptr->additional_length = data_len -
10110 (offsetof(struct scsi_inquiry_data, additional_length) + 1);
10111 CTL_DEBUG_PRINT(("additional_length = %d\n",
10112 inq_ptr->additional_length));
10114 inq_ptr->spc3_flags = SPC3_SID_3PC | SPC3_SID_TPGS_IMPLICIT;
10115 if (port_type == CTL_PORT_SCSI)
10116 inq_ptr->spc2_flags = SPC2_SID_ADDR16;
10117 inq_ptr->spc2_flags |= SPC2_SID_MultiP;
10118 inq_ptr->flags = SID_CmdQue;
10119 if (port_type == CTL_PORT_SCSI)
10120 inq_ptr->flags |= SID_WBus16 | SID_Sync;
10123 * Per SPC-3, unused bytes in ASCII strings are filled with spaces.
10124 * We have 8 bytes for the vendor name, and 16 bytes for the device
10125 * name and 4 bytes for the revision.
10127 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10128 "vendor", NULL)) == NULL) {
10129 strncpy(inq_ptr->vendor, CTL_VENDOR, sizeof(inq_ptr->vendor));
10131 memset(inq_ptr->vendor, ' ', sizeof(inq_ptr->vendor));
10132 strncpy(inq_ptr->vendor, val,
10133 min(sizeof(inq_ptr->vendor), strlen(val)));
10136 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10137 sizeof(inq_ptr->product));
10138 } else if ((val = dnvlist_get_string(lun->be_lun->options, "product",
10140 switch (lun->be_lun->lun_type) {
10142 strncpy(inq_ptr->product, CTL_DIRECT_PRODUCT,
10143 sizeof(inq_ptr->product));
10146 strncpy(inq_ptr->product, CTL_PROCESSOR_PRODUCT,
10147 sizeof(inq_ptr->product));
10150 strncpy(inq_ptr->product, CTL_CDROM_PRODUCT,
10151 sizeof(inq_ptr->product));
10154 strncpy(inq_ptr->product, CTL_UNKNOWN_PRODUCT,
10155 sizeof(inq_ptr->product));
10159 memset(inq_ptr->product, ' ', sizeof(inq_ptr->product));
10160 strncpy(inq_ptr->product, val,
10161 min(sizeof(inq_ptr->product), strlen(val)));
10165 * XXX make this a macro somewhere so it automatically gets
10166 * incremented when we make changes.
10168 if (lun == NULL || (val = dnvlist_get_string(lun->be_lun->options,
10169 "revision", NULL)) == NULL) {
10170 strncpy(inq_ptr->revision, "0001", sizeof(inq_ptr->revision));
10172 memset(inq_ptr->revision, ' ', sizeof(inq_ptr->revision));
10173 strncpy(inq_ptr->revision, val,
10174 min(sizeof(inq_ptr->revision), strlen(val)));
10178 * For parallel SCSI, we support double transition and single
10179 * transition clocking. We also support QAS (Quick Arbitration
10180 * and Selection) and Information Unit transfers on both the
10181 * control and array devices.
10183 if (port_type == CTL_PORT_SCSI)
10184 inq_ptr->spi3data = SID_SPI_CLOCK_DT_ST | SID_SPI_QAS |
10187 /* SAM-6 (no version claimed) */
10188 scsi_ulto2b(0x00C0, inq_ptr->version1);
10189 /* SPC-5 (no version claimed) */
10190 scsi_ulto2b(0x05C0, inq_ptr->version2);
10191 if (port_type == CTL_PORT_FC) {
10192 /* FCP-2 ANSI INCITS.350:2003 */
10193 scsi_ulto2b(0x0917, inq_ptr->version3);
10194 } else if (port_type == CTL_PORT_SCSI) {
10195 /* SPI-4 ANSI INCITS.362:200x */
10196 scsi_ulto2b(0x0B56, inq_ptr->version3);
10197 } else if (port_type == CTL_PORT_ISCSI) {
10198 /* iSCSI (no version claimed) */
10199 scsi_ulto2b(0x0960, inq_ptr->version3);
10200 } else if (port_type == CTL_PORT_SAS) {
10201 /* SAS (no version claimed) */
10202 scsi_ulto2b(0x0BE0, inq_ptr->version3);
10203 } else if (port_type == CTL_PORT_UMASS) {
10204 /* USB Mass Storage Class Bulk-Only Transport, Revision 1.0 */
10205 scsi_ulto2b(0x1730, inq_ptr->version3);
10209 /* SBC-4 (no version claimed) */
10210 scsi_ulto2b(0x0600, inq_ptr->version4);
10212 switch (lun->be_lun->lun_type) {
10214 /* SBC-4 (no version claimed) */
10215 scsi_ulto2b(0x0600, inq_ptr->version4);
10220 /* MMC-6 (no version claimed) */
10221 scsi_ulto2b(0x04E0, inq_ptr->version4);
10228 ctl_set_success(ctsio);
10229 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10230 ctsio->be_move_done = ctl_config_move_done;
10231 ctl_datamove((union ctl_io *)ctsio);
10232 return (CTL_RETVAL_COMPLETE);
10236 ctl_inquiry(struct ctl_scsiio *ctsio)
10238 struct scsi_inquiry *cdb;
10241 CTL_DEBUG_PRINT(("ctl_inquiry\n"));
10243 cdb = (struct scsi_inquiry *)ctsio->cdb;
10244 if (cdb->byte2 & SI_EVPD)
10245 retval = ctl_inquiry_evpd(ctsio);
10246 else if (cdb->page_code == 0)
10247 retval = ctl_inquiry_std(ctsio);
10249 ctl_set_invalid_field(ctsio,
10255 ctl_done((union ctl_io *)ctsio);
10256 return (CTL_RETVAL_COMPLETE);
10263 ctl_get_config(struct ctl_scsiio *ctsio)
10265 struct ctl_lun *lun = CTL_LUN(ctsio);
10266 struct scsi_get_config_header *hdr;
10267 struct scsi_get_config_feature *feature;
10268 struct scsi_get_config *cdb;
10269 uint32_t alloc_len, data_len;
10272 cdb = (struct scsi_get_config *)ctsio->cdb;
10273 rt = (cdb->rt & SGC_RT_MASK);
10274 starting = scsi_2btoul(cdb->starting_feature);
10275 alloc_len = scsi_2btoul(cdb->length);
10277 data_len = sizeof(struct scsi_get_config_header) +
10278 sizeof(struct scsi_get_config_feature) + 8 +
10279 sizeof(struct scsi_get_config_feature) + 8 +
10280 sizeof(struct scsi_get_config_feature) + 4 +
10281 sizeof(struct scsi_get_config_feature) + 4 +
10282 sizeof(struct scsi_get_config_feature) + 8 +
10283 sizeof(struct scsi_get_config_feature) +
10284 sizeof(struct scsi_get_config_feature) + 4 +
10285 sizeof(struct scsi_get_config_feature) + 4 +
10286 sizeof(struct scsi_get_config_feature) + 4 +
10287 sizeof(struct scsi_get_config_feature) + 4 +
10288 sizeof(struct scsi_get_config_feature) + 4 +
10289 sizeof(struct scsi_get_config_feature) + 4;
10290 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10291 ctsio->kern_sg_entries = 0;
10292 ctsio->kern_rel_offset = 0;
10294 hdr = (struct scsi_get_config_header *)ctsio->kern_data_ptr;
10295 if (lun->flags & CTL_LUN_NO_MEDIA)
10296 scsi_ulto2b(0x0000, hdr->current_profile);
10298 scsi_ulto2b(0x0010, hdr->current_profile);
10299 feature = (struct scsi_get_config_feature *)(hdr + 1);
10301 if (starting > 0x003b)
10303 if (starting > 0x003a)
10305 if (starting > 0x002b)
10307 if (starting > 0x002a)
10309 if (starting > 0x001f)
10311 if (starting > 0x001e)
10313 if (starting > 0x001d)
10315 if (starting > 0x0010)
10317 if (starting > 0x0003)
10319 if (starting > 0x0002)
10321 if (starting > 0x0001)
10323 if (starting > 0x0000)
10327 scsi_ulto2b(0x0000, feature->feature_code);
10328 feature->flags = SGC_F_PERSISTENT | SGC_F_CURRENT;
10329 feature->add_length = 8;
10330 scsi_ulto2b(0x0008, &feature->feature_data[0]); /* CD-ROM */
10331 feature->feature_data[2] = 0x00;
10332 scsi_ulto2b(0x0010, &feature->feature_data[4]); /* DVD-ROM */
10333 feature->feature_data[6] = 0x01;
10334 feature = (struct scsi_get_config_feature *)
10335 &feature->feature_data[feature->add_length];
10338 scsi_ulto2b(0x0001, feature->feature_code);
10339 feature->flags = 0x08 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10340 feature->add_length = 8;
10341 scsi_ulto4b(0x00000000, &feature->feature_data[0]);
10342 feature->feature_data[4] = 0x03;
10343 feature = (struct scsi_get_config_feature *)
10344 &feature->feature_data[feature->add_length];
10347 scsi_ulto2b(0x0002, feature->feature_code);
10348 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10349 feature->add_length = 4;
10350 feature->feature_data[0] = 0x02;
10351 feature = (struct scsi_get_config_feature *)
10352 &feature->feature_data[feature->add_length];
10354 f3: /* Removable Medium */
10355 scsi_ulto2b(0x0003, feature->feature_code);
10356 feature->flags = 0x04 | SGC_F_PERSISTENT | SGC_F_CURRENT;
10357 feature->add_length = 4;
10358 feature->feature_data[0] = 0x39;
10359 feature = (struct scsi_get_config_feature *)
10360 &feature->feature_data[feature->add_length];
10362 if (rt == SGC_RT_CURRENT && (lun->flags & CTL_LUN_NO_MEDIA))
10365 f10: /* Random Read */
10366 scsi_ulto2b(0x0010, feature->feature_code);
10367 feature->flags = 0x00;
10368 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10369 feature->flags |= SGC_F_CURRENT;
10370 feature->add_length = 8;
10371 scsi_ulto4b(lun->be_lun->blocksize, &feature->feature_data[0]);
10372 scsi_ulto2b(1, &feature->feature_data[4]);
10373 feature->feature_data[6] = 0x00;
10374 feature = (struct scsi_get_config_feature *)
10375 &feature->feature_data[feature->add_length];
10377 f1d: /* Multi-Read */
10378 scsi_ulto2b(0x001D, feature->feature_code);
10379 feature->flags = 0x00;
10380 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10381 feature->flags |= SGC_F_CURRENT;
10382 feature->add_length = 0;
10383 feature = (struct scsi_get_config_feature *)
10384 &feature->feature_data[feature->add_length];
10387 scsi_ulto2b(0x001E, feature->feature_code);
10388 feature->flags = 0x00;
10389 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10390 feature->flags |= SGC_F_CURRENT;
10391 feature->add_length = 4;
10392 feature->feature_data[0] = 0x00;
10393 feature = (struct scsi_get_config_feature *)
10394 &feature->feature_data[feature->add_length];
10396 f1f: /* DVD Read */
10397 scsi_ulto2b(0x001F, feature->feature_code);
10398 feature->flags = 0x08;
10399 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10400 feature->flags |= SGC_F_CURRENT;
10401 feature->add_length = 4;
10402 feature->feature_data[0] = 0x01;
10403 feature->feature_data[2] = 0x03;
10404 feature = (struct scsi_get_config_feature *)
10405 &feature->feature_data[feature->add_length];
10408 scsi_ulto2b(0x002A, feature->feature_code);
10409 feature->flags = 0x04;
10410 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10411 feature->flags |= SGC_F_CURRENT;
10412 feature->add_length = 4;
10413 feature->feature_data[0] = 0x00;
10414 feature->feature_data[1] = 0x00;
10415 feature = (struct scsi_get_config_feature *)
10416 &feature->feature_data[feature->add_length];
10419 scsi_ulto2b(0x002B, feature->feature_code);
10420 feature->flags = 0x00;
10421 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10422 feature->flags |= SGC_F_CURRENT;
10423 feature->add_length = 4;
10424 feature->feature_data[0] = 0x00;
10425 feature = (struct scsi_get_config_feature *)
10426 &feature->feature_data[feature->add_length];
10428 f3a: /* DVD+RW Dual Layer */
10429 scsi_ulto2b(0x003A, feature->feature_code);
10430 feature->flags = 0x00;
10431 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10432 feature->flags |= SGC_F_CURRENT;
10433 feature->add_length = 4;
10434 feature->feature_data[0] = 0x00;
10435 feature->feature_data[1] = 0x00;
10436 feature = (struct scsi_get_config_feature *)
10437 &feature->feature_data[feature->add_length];
10439 f3b: /* DVD+R Dual Layer */
10440 scsi_ulto2b(0x003B, feature->feature_code);
10441 feature->flags = 0x00;
10442 if ((lun->flags & CTL_LUN_NO_MEDIA) == 0)
10443 feature->flags |= SGC_F_CURRENT;
10444 feature->add_length = 4;
10445 feature->feature_data[0] = 0x00;
10446 feature = (struct scsi_get_config_feature *)
10447 &feature->feature_data[feature->add_length];
10450 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10451 if (rt == SGC_RT_SPECIFIC && data_len > 4) {
10452 feature = (struct scsi_get_config_feature *)(hdr + 1);
10453 if (scsi_2btoul(feature->feature_code) == starting)
10454 feature = (struct scsi_get_config_feature *)
10455 &feature->feature_data[feature->add_length];
10456 data_len = (uint8_t *)feature - (uint8_t *)hdr;
10458 scsi_ulto4b(data_len - 4, hdr->data_length);
10459 ctsio->kern_data_len = min(data_len, alloc_len);
10460 ctsio->kern_total_len = ctsio->kern_data_len;
10462 ctl_set_success(ctsio);
10463 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10464 ctsio->be_move_done = ctl_config_move_done;
10465 ctl_datamove((union ctl_io *)ctsio);
10466 return (CTL_RETVAL_COMPLETE);
10470 ctl_get_event_status(struct ctl_scsiio *ctsio)
10472 struct scsi_get_event_status_header *hdr;
10473 struct scsi_get_event_status *cdb;
10474 uint32_t alloc_len, data_len;
10476 cdb = (struct scsi_get_event_status *)ctsio->cdb;
10477 if ((cdb->byte2 & SGESN_POLLED) == 0) {
10478 ctl_set_invalid_field(ctsio, /*sks_valid*/ 1, /*command*/ 1,
10479 /*field*/ 1, /*bit_valid*/ 1, /*bit*/ 0);
10480 ctl_done((union ctl_io *)ctsio);
10481 return (CTL_RETVAL_COMPLETE);
10483 alloc_len = scsi_2btoul(cdb->length);
10485 data_len = sizeof(struct scsi_get_event_status_header);
10486 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10487 ctsio->kern_sg_entries = 0;
10488 ctsio->kern_rel_offset = 0;
10489 ctsio->kern_data_len = min(data_len, alloc_len);
10490 ctsio->kern_total_len = ctsio->kern_data_len;
10492 hdr = (struct scsi_get_event_status_header *)ctsio->kern_data_ptr;
10493 scsi_ulto2b(0, hdr->descr_length);
10494 hdr->nea_class = SGESN_NEA;
10495 hdr->supported_class = 0;
10497 ctl_set_success(ctsio);
10498 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10499 ctsio->be_move_done = ctl_config_move_done;
10500 ctl_datamove((union ctl_io *)ctsio);
10501 return (CTL_RETVAL_COMPLETE);
10505 ctl_mechanism_status(struct ctl_scsiio *ctsio)
10507 struct scsi_mechanism_status_header *hdr;
10508 struct scsi_mechanism_status *cdb;
10509 uint32_t alloc_len, data_len;
10511 cdb = (struct scsi_mechanism_status *)ctsio->cdb;
10512 alloc_len = scsi_2btoul(cdb->length);
10514 data_len = sizeof(struct scsi_mechanism_status_header);
10515 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10516 ctsio->kern_sg_entries = 0;
10517 ctsio->kern_rel_offset = 0;
10518 ctsio->kern_data_len = min(data_len, alloc_len);
10519 ctsio->kern_total_len = ctsio->kern_data_len;
10521 hdr = (struct scsi_mechanism_status_header *)ctsio->kern_data_ptr;
10522 hdr->state1 = 0x00;
10523 hdr->state2 = 0xe0;
10524 scsi_ulto3b(0, hdr->lba);
10525 hdr->slots_num = 0;
10526 scsi_ulto2b(0, hdr->slots_length);
10528 ctl_set_success(ctsio);
10529 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10530 ctsio->be_move_done = ctl_config_move_done;
10531 ctl_datamove((union ctl_io *)ctsio);
10532 return (CTL_RETVAL_COMPLETE);
10536 ctl_ultomsf(uint32_t lba, uint8_t *buf)
10541 buf[1] = bin2bcd((lba / 75) / 60);
10542 buf[2] = bin2bcd((lba / 75) % 60);
10543 buf[3] = bin2bcd(lba % 75);
10547 ctl_read_toc(struct ctl_scsiio *ctsio)
10549 struct ctl_lun *lun = CTL_LUN(ctsio);
10550 struct scsi_read_toc_hdr *hdr;
10551 struct scsi_read_toc_type01_descr *descr;
10552 struct scsi_read_toc *cdb;
10553 uint32_t alloc_len, data_len;
10556 cdb = (struct scsi_read_toc *)ctsio->cdb;
10557 msf = (cdb->byte2 & CD_MSF) != 0;
10558 format = cdb->format;
10559 alloc_len = scsi_2btoul(cdb->data_len);
10561 data_len = sizeof(struct scsi_read_toc_hdr);
10563 data_len += 2 * sizeof(struct scsi_read_toc_type01_descr);
10565 data_len += sizeof(struct scsi_read_toc_type01_descr);
10566 ctsio->kern_data_ptr = malloc(data_len, M_CTL, M_WAITOK | M_ZERO);
10567 ctsio->kern_sg_entries = 0;
10568 ctsio->kern_rel_offset = 0;
10569 ctsio->kern_data_len = min(data_len, alloc_len);
10570 ctsio->kern_total_len = ctsio->kern_data_len;
10572 hdr = (struct scsi_read_toc_hdr *)ctsio->kern_data_ptr;
10574 scsi_ulto2b(0x12, hdr->data_length);
10577 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10578 descr->addr_ctl = 0x14;
10579 descr->track_number = 1;
10581 ctl_ultomsf(0, descr->track_start);
10583 scsi_ulto4b(0, descr->track_start);
10585 descr->addr_ctl = 0x14;
10586 descr->track_number = 0xaa;
10588 ctl_ultomsf(lun->be_lun->maxlba+1, descr->track_start);
10590 scsi_ulto4b(lun->be_lun->maxlba+1, descr->track_start);
10592 scsi_ulto2b(0x0a, hdr->data_length);
10595 descr = (struct scsi_read_toc_type01_descr *)(hdr + 1);
10596 descr->addr_ctl = 0x14;
10597 descr->track_number = 1;
10599 ctl_ultomsf(0, descr->track_start);
10601 scsi_ulto4b(0, descr->track_start);
10604 ctl_set_success(ctsio);
10605 ctsio->io_hdr.flags |= CTL_FLAG_ALLOCATED;
10606 ctsio->be_move_done = ctl_config_move_done;
10607 ctl_datamove((union ctl_io *)ctsio);
10608 return (CTL_RETVAL_COMPLETE);
10612 * For known CDB types, parse the LBA and length.
10615 ctl_get_lba_len(union ctl_io *io, uint64_t *lba, uint64_t *len)
10617 if (io->io_hdr.io_type != CTL_IO_SCSI)
10620 switch (io->scsiio.cdb[0]) {
10621 case COMPARE_AND_WRITE: {
10622 struct scsi_compare_and_write *cdb;
10624 cdb = (struct scsi_compare_and_write *)io->scsiio.cdb;
10626 *lba = scsi_8btou64(cdb->addr);
10627 *len = cdb->length;
10632 struct scsi_rw_6 *cdb;
10634 cdb = (struct scsi_rw_6 *)io->scsiio.cdb;
10636 *lba = scsi_3btoul(cdb->addr);
10637 /* only 5 bits are valid in the most significant address byte */
10639 *len = cdb->length;
10644 struct scsi_rw_10 *cdb;
10646 cdb = (struct scsi_rw_10 *)io->scsiio.cdb;
10648 *lba = scsi_4btoul(cdb->addr);
10649 *len = scsi_2btoul(cdb->length);
10652 case WRITE_VERIFY_10: {
10653 struct scsi_write_verify_10 *cdb;
10655 cdb = (struct scsi_write_verify_10 *)io->scsiio.cdb;
10657 *lba = scsi_4btoul(cdb->addr);
10658 *len = scsi_2btoul(cdb->length);
10663 struct scsi_rw_12 *cdb;
10665 cdb = (struct scsi_rw_12 *)io->scsiio.cdb;
10667 *lba = scsi_4btoul(cdb->addr);
10668 *len = scsi_4btoul(cdb->length);
10671 case WRITE_VERIFY_12: {
10672 struct scsi_write_verify_12 *cdb;
10674 cdb = (struct scsi_write_verify_12 *)io->scsiio.cdb;
10676 *lba = scsi_4btoul(cdb->addr);
10677 *len = scsi_4btoul(cdb->length);
10682 struct scsi_rw_16 *cdb;
10684 cdb = (struct scsi_rw_16 *)io->scsiio.cdb;
10686 *lba = scsi_8btou64(cdb->addr);
10687 *len = scsi_4btoul(cdb->length);
10690 case WRITE_ATOMIC_16: {
10691 struct scsi_write_atomic_16 *cdb;
10693 cdb = (struct scsi_write_atomic_16 *)io->scsiio.cdb;
10695 *lba = scsi_8btou64(cdb->addr);
10696 *len = scsi_2btoul(cdb->length);
10699 case WRITE_VERIFY_16: {
10700 struct scsi_write_verify_16 *cdb;
10702 cdb = (struct scsi_write_verify_16 *)io->scsiio.cdb;
10704 *lba = scsi_8btou64(cdb->addr);
10705 *len = scsi_4btoul(cdb->length);
10708 case WRITE_SAME_10: {
10709 struct scsi_write_same_10 *cdb;
10711 cdb = (struct scsi_write_same_10 *)io->scsiio.cdb;
10713 *lba = scsi_4btoul(cdb->addr);
10714 *len = scsi_2btoul(cdb->length);
10717 case WRITE_SAME_16: {
10718 struct scsi_write_same_16 *cdb;
10720 cdb = (struct scsi_write_same_16 *)io->scsiio.cdb;
10722 *lba = scsi_8btou64(cdb->addr);
10723 *len = scsi_4btoul(cdb->length);
10727 struct scsi_verify_10 *cdb;
10729 cdb = (struct scsi_verify_10 *)io->scsiio.cdb;
10731 *lba = scsi_4btoul(cdb->addr);
10732 *len = scsi_2btoul(cdb->length);
10736 struct scsi_verify_12 *cdb;
10738 cdb = (struct scsi_verify_12 *)io->scsiio.cdb;
10740 *lba = scsi_4btoul(cdb->addr);
10741 *len = scsi_4btoul(cdb->length);
10745 struct scsi_verify_16 *cdb;
10747 cdb = (struct scsi_verify_16 *)io->scsiio.cdb;
10749 *lba = scsi_8btou64(cdb->addr);
10750 *len = scsi_4btoul(cdb->length);
10758 case SERVICE_ACTION_IN: { /* GET LBA STATUS */
10759 struct scsi_get_lba_status *cdb;
10761 cdb = (struct scsi_get_lba_status *)io->scsiio.cdb;
10762 *lba = scsi_8btou64(cdb->addr);
10768 break; /* NOTREACHED */
10775 ctl_extent_check_lba(uint64_t lba1, uint64_t len1, uint64_t lba2, uint64_t len2,
10778 uint64_t endlba1, endlba2;
10780 endlba1 = lba1 + len1 - (seq ? 0 : 1);
10781 endlba2 = lba2 + len2 - 1;
10783 if ((endlba1 < lba2) || (endlba2 < lba1))
10784 return (CTL_ACTION_PASS);
10786 return (CTL_ACTION_BLOCK);
10790 ctl_extent_check_unmap(union ctl_io *io, uint64_t lba2, uint64_t len2)
10792 struct ctl_ptr_len_flags *ptrlen;
10793 struct scsi_unmap_desc *buf, *end, *range;
10797 /* If not UNMAP -- go other way. */
10798 if (io->io_hdr.io_type != CTL_IO_SCSI ||
10799 io->scsiio.cdb[0] != UNMAP)
10800 return (CTL_ACTION_ERROR);
10802 /* If UNMAP without data -- block and wait for data. */
10803 ptrlen = (struct ctl_ptr_len_flags *)
10804 &io->io_hdr.ctl_private[CTL_PRIV_LBA_LEN];
10805 if ((io->io_hdr.flags & CTL_FLAG_ALLOCATED) == 0 ||
10806 ptrlen->ptr == NULL)
10807 return (CTL_ACTION_BLOCK);
10809 /* UNMAP with data -- check for collision. */
10810 buf = (struct scsi_unmap_desc *)ptrlen->ptr;
10811 end = buf + ptrlen->len / sizeof(*buf);
10812 for (range = buf; range < end; range++) {
10813 lba = scsi_8btou64(range->lba);
10814 len = scsi_4btoul(range->length);
10815 if ((lba < lba2 + len2) && (lba + len > lba2))
10816 return (CTL_ACTION_BLOCK);
10818 return (CTL_ACTION_PASS);
10822 ctl_extent_check(union ctl_io *io1, union ctl_io *io2, bool seq)
10824 uint64_t lba1, lba2;
10825 uint64_t len1, len2;
10828 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10829 return (CTL_ACTION_ERROR);
10831 retval = ctl_extent_check_unmap(io1, lba2, len2);
10832 if (retval != CTL_ACTION_ERROR)
10835 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10836 return (CTL_ACTION_ERROR);
10838 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10840 return (ctl_extent_check_lba(lba1, len1, lba2, len2, seq));
10844 ctl_extent_check_seq(union ctl_io *io1, union ctl_io *io2)
10846 uint64_t lba1, lba2;
10847 uint64_t len1, len2;
10849 if (io1->io_hdr.flags & CTL_FLAG_SERSEQ_DONE)
10850 return (CTL_ACTION_PASS);
10851 if (ctl_get_lba_len(io1, &lba1, &len1) != 0)
10852 return (CTL_ACTION_ERROR);
10853 if (ctl_get_lba_len(io2, &lba2, &len2) != 0)
10854 return (CTL_ACTION_ERROR);
10856 if (lba1 + len1 == lba2)
10857 return (CTL_ACTION_BLOCK);
10858 return (CTL_ACTION_PASS);
10862 ctl_check_for_blockage(struct ctl_lun *lun, union ctl_io *pending_io,
10863 union ctl_io *ooa_io)
10865 const struct ctl_cmd_entry *pending_entry, *ooa_entry;
10866 const ctl_serialize_action *serialize_row;
10869 * Aborted commands are not going to be executed and may even
10870 * not report completion, so we don't care about their order.
10871 * Let them complete ASAP to clean the OOA queue.
10873 if (pending_io->io_hdr.flags & CTL_FLAG_ABORT)
10874 return (CTL_ACTION_SKIP);
10877 * The initiator attempted multiple untagged commands at the same
10878 * time. Can't do that.
10880 if ((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10881 && (ooa_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10882 && ((pending_io->io_hdr.nexus.targ_port ==
10883 ooa_io->io_hdr.nexus.targ_port)
10884 && (pending_io->io_hdr.nexus.initid ==
10885 ooa_io->io_hdr.nexus.initid))
10886 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10887 CTL_FLAG_STATUS_SENT)) == 0))
10888 return (CTL_ACTION_OVERLAP);
10891 * The initiator attempted to send multiple tagged commands with
10892 * the same ID. (It's fine if different initiators have the same
10895 * Even if all of those conditions are true, we don't kill the I/O
10896 * if the command ahead of us has been aborted. We won't end up
10897 * sending it to the FETD, and it's perfectly legal to resend a
10898 * command with the same tag number as long as the previous
10899 * instance of this tag number has been aborted somehow.
10901 if ((pending_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10902 && (ooa_io->scsiio.tag_type != CTL_TAG_UNTAGGED)
10903 && (pending_io->scsiio.tag_num == ooa_io->scsiio.tag_num)
10904 && ((pending_io->io_hdr.nexus.targ_port ==
10905 ooa_io->io_hdr.nexus.targ_port)
10906 && (pending_io->io_hdr.nexus.initid ==
10907 ooa_io->io_hdr.nexus.initid))
10908 && ((ooa_io->io_hdr.flags & (CTL_FLAG_ABORT |
10909 CTL_FLAG_STATUS_SENT)) == 0))
10910 return (CTL_ACTION_OVERLAP_TAG);
10913 * If we get a head of queue tag, SAM-3 says that we should
10914 * immediately execute it.
10916 * What happens if this command would normally block for some other
10917 * reason? e.g. a request sense with a head of queue tag
10918 * immediately after a write. Normally that would block, but this
10919 * will result in its getting executed immediately...
10921 * We currently return "pass" instead of "skip", so we'll end up
10922 * going through the rest of the queue to check for overlapped tags.
10924 * XXX KDM check for other types of blockage first??
10926 if (pending_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10927 return (CTL_ACTION_PASS);
10930 * Ordered tags have to block until all items ahead of them
10931 * have completed. If we get called with an ordered tag, we always
10932 * block, if something else is ahead of us in the queue.
10934 if (pending_io->scsiio.tag_type == CTL_TAG_ORDERED)
10935 return (CTL_ACTION_BLOCK);
10938 * Simple tags get blocked until all head of queue and ordered tags
10939 * ahead of them have completed. I'm lumping untagged commands in
10940 * with simple tags here. XXX KDM is that the right thing to do?
10942 if (((pending_io->scsiio.tag_type == CTL_TAG_UNTAGGED)
10943 || (pending_io->scsiio.tag_type == CTL_TAG_SIMPLE))
10944 && ((ooa_io->scsiio.tag_type == CTL_TAG_HEAD_OF_QUEUE)
10945 || (ooa_io->scsiio.tag_type == CTL_TAG_ORDERED)))
10946 return (CTL_ACTION_BLOCK);
10948 pending_entry = ctl_get_cmd_entry(&pending_io->scsiio, NULL);
10949 KASSERT(pending_entry->seridx < CTL_SERIDX_COUNT,
10950 ("%s: Invalid seridx %d for pending CDB %02x %02x @ %p",
10951 __func__, pending_entry->seridx, pending_io->scsiio.cdb[0],
10952 pending_io->scsiio.cdb[1], pending_io));
10953 ooa_entry = ctl_get_cmd_entry(&ooa_io->scsiio, NULL);
10954 if (ooa_entry->seridx == CTL_SERIDX_INVLD)
10955 return (CTL_ACTION_PASS); /* Unsupported command in OOA queue */
10956 KASSERT(ooa_entry->seridx < CTL_SERIDX_COUNT,
10957 ("%s: Invalid seridx %d for ooa CDB %02x %02x @ %p",
10958 __func__, ooa_entry->seridx, ooa_io->scsiio.cdb[0],
10959 ooa_io->scsiio.cdb[1], ooa_io));
10961 serialize_row = ctl_serialize_table[ooa_entry->seridx];
10963 switch (serialize_row[pending_entry->seridx]) {
10964 case CTL_SER_BLOCK:
10965 return (CTL_ACTION_BLOCK);
10966 case CTL_SER_EXTENT:
10967 return (ctl_extent_check(ooa_io, pending_io,
10968 (lun->be_lun && lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10969 case CTL_SER_EXTENTOPT:
10970 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10971 SCP_QUEUE_ALG_UNRESTRICTED)
10972 return (ctl_extent_check(ooa_io, pending_io,
10974 lun->be_lun->serseq == CTL_LUN_SERSEQ_ON)));
10975 return (CTL_ACTION_PASS);
10976 case CTL_SER_EXTENTSEQ:
10977 if (lun->be_lun && lun->be_lun->serseq != CTL_LUN_SERSEQ_OFF)
10978 return (ctl_extent_check_seq(ooa_io, pending_io));
10979 return (CTL_ACTION_PASS);
10981 return (CTL_ACTION_PASS);
10982 case CTL_SER_BLOCKOPT:
10983 if ((lun->MODE_CTRL.queue_flags & SCP_QUEUE_ALG_MASK) !=
10984 SCP_QUEUE_ALG_UNRESTRICTED)
10985 return (CTL_ACTION_BLOCK);
10986 return (CTL_ACTION_PASS);
10988 return (CTL_ACTION_SKIP);
10990 panic("%s: Invalid serialization value %d for %d => %d",
10991 __func__, serialize_row[pending_entry->seridx],
10992 pending_entry->seridx, ooa_entry->seridx);
10995 return (CTL_ACTION_ERROR);
10999 * Check for blockage or overlaps against the OOA (Order Of Arrival) queue.
11001 * - pending_io is generally either incoming, or on the blocked queue
11002 * - starting I/O is the I/O we want to start the check with.
11005 ctl_check_ooa(struct ctl_lun *lun, union ctl_io *pending_io,
11006 union ctl_io **starting_io)
11008 union ctl_io *ooa_io;
11011 mtx_assert(&lun->lun_lock, MA_OWNED);
11014 * Run back along the OOA queue, starting with the current
11015 * blocked I/O and going through every I/O before it on the
11016 * queue. If starting_io is NULL, we'll just end up returning
11019 for (ooa_io = *starting_io; ooa_io != NULL;
11020 ooa_io = (union ctl_io *)TAILQ_PREV(&ooa_io->io_hdr, ctl_ooaq,
11022 action = ctl_check_for_blockage(lun, pending_io, ooa_io);
11023 if (action != CTL_ACTION_PASS) {
11024 *starting_io = ooa_io;
11029 *starting_io = NULL;
11030 return (CTL_ACTION_PASS);
11034 * Try to unblock the specified I/O.
11036 * skip parameter allows explicitly skip present blocker of the I/O,
11037 * starting from the previous one on OOA queue. It can be used when
11038 * we know for sure that the blocker I/O does no longer count.
11041 ctl_try_unblock_io(struct ctl_lun *lun, union ctl_io *io, bool skip)
11043 struct ctl_softc *softc = lun->ctl_softc;
11044 union ctl_io *bio, *obio;
11045 const struct ctl_cmd_entry *entry;
11046 union ctl_ha_msg msg_info;
11049 mtx_assert(&lun->lun_lock, MA_OWNED);
11051 if (io->io_hdr.blocker == NULL)
11054 obio = bio = io->io_hdr.blocker;
11056 bio = (union ctl_io *)TAILQ_PREV(&bio->io_hdr, ctl_ooaq,
11058 action = ctl_check_ooa(lun, io, &bio);
11059 if (action == CTL_ACTION_BLOCK) {
11060 /* Still blocked, but may be by different I/O now. */
11062 TAILQ_REMOVE(&obio->io_hdr.blocked_queue,
11063 &io->io_hdr, blocked_links);
11064 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue,
11065 &io->io_hdr, blocked_links);
11066 io->io_hdr.blocker = bio;
11071 /* No longer blocked, one way or another. */
11072 TAILQ_REMOVE(&obio->io_hdr.blocked_queue, &io->io_hdr, blocked_links);
11073 io->io_hdr.blocker = NULL;
11076 case CTL_ACTION_OVERLAP:
11077 ctl_set_overlapped_cmd(&io->scsiio);
11079 case CTL_ACTION_OVERLAP_TAG:
11080 ctl_set_overlapped_tag(&io->scsiio,
11081 io->scsiio.tag_num & 0xff);
11083 case CTL_ACTION_PASS:
11084 case CTL_ACTION_SKIP:
11086 /* Serializing commands from the other SC retire there. */
11087 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11088 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11089 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11090 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11091 msg_info.hdr.serializing_sc = io;
11092 msg_info.hdr.msg_type = CTL_MSG_R2R;
11093 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11094 sizeof(msg_info.hdr), M_NOWAIT);
11099 * Check this I/O for LUN state changes that may have happened
11100 * while this command was blocked. The LUN state may have been
11101 * changed by a command ahead of us in the queue.
11103 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
11104 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
11109 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11110 ctl_enqueue_rtr(io);
11112 case CTL_ACTION_ERROR:
11114 ctl_set_internal_failure(&io->scsiio,
11116 /*retry_count*/ 0);
11119 /* Serializing commands from the other SC are done here. */
11120 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) &&
11121 (softc->ha_mode != CTL_HA_MODE_XFER)) {
11122 ctl_try_unblock_others(lun, io, TRUE);
11123 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
11125 ctl_copy_sense_data_back(io, &msg_info);
11126 msg_info.hdr.original_sc = io->io_hdr.remote_io;
11127 msg_info.hdr.serializing_sc = NULL;
11128 msg_info.hdr.msg_type = CTL_MSG_BAD_JUJU;
11129 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11130 sizeof(msg_info.scsi), M_WAITOK);
11141 * Try to unblock I/Os blocked by the specified I/O.
11143 * skip parameter allows explicitly skip the specified I/O as blocker,
11144 * starting from the previous one on the OOA queue. It can be used when
11145 * we know for sure that the specified I/O does no longer count (done).
11146 * It has to be still on OOA queue though so that we know where to start.
11149 ctl_try_unblock_others(struct ctl_lun *lun, union ctl_io *bio, bool skip)
11151 union ctl_io *io, *next_io;
11153 mtx_assert(&lun->lun_lock, MA_OWNED);
11155 for (io = (union ctl_io *)TAILQ_FIRST(&bio->io_hdr.blocked_queue);
11156 io != NULL; io = next_io) {
11157 next_io = (union ctl_io *)TAILQ_NEXT(&io->io_hdr, blocked_links);
11159 KASSERT(io->io_hdr.blocker != NULL,
11160 ("I/O %p on blocked list without blocker", io));
11161 ctl_try_unblock_io(lun, io, skip);
11163 KASSERT(!skip || TAILQ_EMPTY(&bio->io_hdr.blocked_queue),
11164 ("blocked_queue is not empty after skipping %p", bio));
11168 * This routine (with one exception) checks LUN flags that can be set by
11169 * commands ahead of us in the OOA queue. These flags have to be checked
11170 * when a command initially comes in, and when we pull a command off the
11171 * blocked queue and are preparing to execute it. The reason we have to
11172 * check these flags for commands on the blocked queue is that the LUN
11173 * state may have been changed by a command ahead of us while we're on the
11176 * Ordering is somewhat important with these checks, so please pay
11177 * careful attention to the placement of any new checks.
11180 ctl_scsiio_lun_check(struct ctl_lun *lun,
11181 const struct ctl_cmd_entry *entry, struct ctl_scsiio *ctsio)
11183 struct ctl_softc *softc = lun->ctl_softc;
11189 mtx_assert(&lun->lun_lock, MA_OWNED);
11192 * If this shelf is a secondary shelf controller, we may have to
11193 * reject some commands disallowed by HA mode and link state.
11195 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0) {
11196 if (softc->ha_link == CTL_HA_LINK_OFFLINE &&
11197 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11198 ctl_set_lun_unavail(ctsio);
11202 if ((lun->flags & CTL_LUN_PEER_SC_PRIMARY) == 0 &&
11203 (entry->flags & CTL_CMD_FLAG_OK_ON_UNAVAIL) == 0) {
11204 ctl_set_lun_transit(ctsio);
11208 if (softc->ha_mode == CTL_HA_MODE_ACT_STBY &&
11209 (entry->flags & CTL_CMD_FLAG_OK_ON_STANDBY) == 0) {
11210 ctl_set_lun_standby(ctsio);
11215 /* The rest of checks are only done on executing side */
11216 if (softc->ha_mode == CTL_HA_MODE_XFER)
11220 if (entry->pattern & CTL_LUN_PAT_WRITE) {
11222 lun->be_lun->flags & CTL_LUN_FLAG_READONLY) {
11223 ctl_set_hw_write_protected(ctsio);
11227 if ((lun->MODE_CTRL.eca_and_aen & SCP_SWP) != 0) {
11228 ctl_set_sense(ctsio, /*current_error*/ 1,
11229 /*sense_key*/ SSD_KEY_DATA_PROTECT,
11230 /*asc*/ 0x27, /*ascq*/ 0x02, SSD_ELEM_NONE);
11237 * Check for a reservation conflict. If this command isn't allowed
11238 * even on reserved LUNs, and if this initiator isn't the one who
11239 * reserved us, reject the command with a reservation conflict.
11241 residx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11242 if ((lun->flags & CTL_LUN_RESERVED)
11243 && ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_RESV) == 0)) {
11244 if (lun->res_idx != residx) {
11245 ctl_set_reservation_conflict(ctsio);
11251 if ((lun->flags & CTL_LUN_PR_RESERVED) == 0 ||
11252 (entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_RESV)) {
11253 /* No reservation or command is allowed. */;
11254 } else if ((entry->flags & CTL_CMD_FLAG_ALLOW_ON_PR_WRESV) &&
11255 (lun->pr_res_type == SPR_TYPE_WR_EX ||
11256 lun->pr_res_type == SPR_TYPE_WR_EX_RO ||
11257 lun->pr_res_type == SPR_TYPE_WR_EX_AR)) {
11258 /* The command is allowed for Write Exclusive resv. */;
11261 * if we aren't registered or it's a res holder type
11262 * reservation and this isn't the res holder then set a
11265 if (ctl_get_prkey(lun, residx) == 0 ||
11266 (residx != lun->pr_res_idx && lun->pr_res_type < 4)) {
11267 ctl_set_reservation_conflict(ctsio);
11273 if ((entry->flags & CTL_CMD_FLAG_OK_ON_NO_MEDIA) == 0) {
11274 if (lun->flags & CTL_LUN_EJECTED)
11275 ctl_set_lun_ejected(ctsio);
11276 else if (lun->flags & CTL_LUN_NO_MEDIA) {
11277 if (lun->flags & CTL_LUN_REMOVABLE)
11278 ctl_set_lun_no_media(ctsio);
11280 ctl_set_lun_int_reqd(ctsio);
11281 } else if (lun->flags & CTL_LUN_STOPPED)
11282 ctl_set_lun_stopped(ctsio);
11294 ctl_failover_io(union ctl_io *io, int have_lock)
11296 ctl_set_busy(&io->scsiio);
11301 ctl_failover_lun(union ctl_io *rio)
11303 struct ctl_softc *softc = CTL_SOFTC(rio);
11304 struct ctl_lun *lun;
11305 struct ctl_io_hdr *io, *next_io;
11308 targ_lun = rio->io_hdr.nexus.targ_mapped_lun;
11309 CTL_DEBUG_PRINT(("FAILOVER for lun %ju\n", targ_lun));
11311 /* Find and lock the LUN. */
11312 mtx_lock(&softc->ctl_lock);
11313 if (targ_lun > ctl_max_luns ||
11314 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11315 mtx_unlock(&softc->ctl_lock);
11318 mtx_lock(&lun->lun_lock);
11319 mtx_unlock(&softc->ctl_lock);
11320 if (lun->flags & CTL_LUN_DISABLED) {
11321 mtx_unlock(&lun->lun_lock);
11325 if (softc->ha_mode == CTL_HA_MODE_XFER) {
11326 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11327 /* We are master */
11328 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11329 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11330 io->flags |= CTL_FLAG_ABORT;
11331 io->flags |= CTL_FLAG_FAILOVER;
11332 ctl_try_unblock_io(lun,
11333 (union ctl_io *)io, FALSE);
11334 } else { /* This can be only due to DATAMOVE */
11335 io->msg_type = CTL_MSG_DATAMOVE_DONE;
11336 io->flags &= ~CTL_FLAG_DMA_INPROG;
11337 io->flags |= CTL_FLAG_IO_ACTIVE;
11338 io->port_status = 31340;
11339 ctl_enqueue_isc((union ctl_io *)io);
11343 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11344 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11345 if (io->flags & CTL_FLAG_IO_ACTIVE) {
11346 io->flags |= CTL_FLAG_FAILOVER;
11348 ctl_set_busy(&((union ctl_io *)io)->
11350 ctl_done((union ctl_io *)io);
11354 } else { /* SERIALIZE modes */
11355 TAILQ_FOREACH_SAFE(io, &lun->ooa_queue, ooa_links, next_io) {
11356 /* We are master */
11357 if (io->flags & CTL_FLAG_FROM_OTHER_SC) {
11358 if (io->blocker != NULL) {
11359 TAILQ_REMOVE(&io->blocker->io_hdr.blocked_queue,
11360 io, blocked_links);
11361 io->blocker = NULL;
11363 ctl_try_unblock_others(lun, (union ctl_io *)io,
11365 TAILQ_REMOVE(&lun->ooa_queue, io, ooa_links);
11366 ctl_free_io((union ctl_io *)io);
11369 if (io->flags & CTL_FLAG_SENT_2OTHER_SC) {
11370 io->flags &= ~CTL_FLAG_SENT_2OTHER_SC;
11371 if (!(io->flags & CTL_FLAG_IO_ACTIVE)) {
11372 ctl_set_busy(&((union ctl_io *)io)->
11374 ctl_done((union ctl_io *)io);
11379 mtx_unlock(&lun->lun_lock);
11383 ctl_scsiio_precheck(struct ctl_softc *softc, struct ctl_scsiio *ctsio)
11385 struct ctl_lun *lun;
11386 const struct ctl_cmd_entry *entry;
11388 uint32_t initidx, targ_lun;
11392 targ_lun = ctsio->io_hdr.nexus.targ_mapped_lun;
11393 if (targ_lun < ctl_max_luns)
11394 lun = softc->ctl_luns[targ_lun];
11397 * If the LUN is invalid, pretend that it doesn't exist.
11398 * It will go away as soon as all pending I/O has been
11401 mtx_lock(&lun->lun_lock);
11402 if (lun->flags & CTL_LUN_DISABLED) {
11403 mtx_unlock(&lun->lun_lock);
11407 CTL_LUN(ctsio) = lun;
11409 CTL_BACKEND_LUN(ctsio) = lun->be_lun;
11412 * Every I/O goes into the OOA queue for a particular LUN,
11413 * and stays there until completion.
11416 if (TAILQ_EMPTY(&lun->ooa_queue))
11417 lun->idle_time += getsbinuptime() - lun->last_busy;
11419 TAILQ_INSERT_TAIL(&lun->ooa_queue, &ctsio->io_hdr, ooa_links);
11422 /* Get command entry and return error if it is unsuppotyed. */
11423 entry = ctl_validate_command(ctsio);
11424 if (entry == NULL) {
11426 mtx_unlock(&lun->lun_lock);
11430 ctsio->io_hdr.flags &= ~CTL_FLAG_DATA_MASK;
11431 ctsio->io_hdr.flags |= entry->flags & CTL_FLAG_DATA_MASK;
11434 * Check to see whether we can send this command to LUNs that don't
11435 * exist. This should pretty much only be the case for inquiry
11436 * and request sense. Further checks, below, really require having
11437 * a LUN, so we can't really check the command anymore. Just put
11438 * it on the rtr queue.
11441 if (entry->flags & CTL_CMD_FLAG_OK_ON_NO_LUN) {
11442 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11443 ctl_enqueue_rtr((union ctl_io *)ctsio);
11447 ctl_set_unsupported_lun(ctsio);
11448 ctl_done((union ctl_io *)ctsio);
11449 CTL_DEBUG_PRINT(("ctl_scsiio_precheck: bailing out due to invalid LUN\n"));
11453 * Make sure we support this particular command on this LUN.
11454 * e.g., we don't support writes to the control LUN.
11456 if (!ctl_cmd_applicable(lun->be_lun->lun_type, entry)) {
11457 mtx_unlock(&lun->lun_lock);
11458 ctl_set_invalid_opcode(ctsio);
11459 ctl_done((union ctl_io *)ctsio);
11464 initidx = ctl_get_initindex(&ctsio->io_hdr.nexus);
11467 * If we've got a request sense, it'll clear the contingent
11468 * allegiance condition. Otherwise, if we have a CA condition for
11469 * this initiator, clear it, because it sent down a command other
11470 * than request sense.
11472 if (ctsio->cdb[0] != REQUEST_SENSE) {
11473 struct scsi_sense_data *ps;
11475 ps = lun->pending_sense[initidx / CTL_MAX_INIT_PER_PORT];
11477 ps[initidx % CTL_MAX_INIT_PER_PORT].error_code = 0;
11481 * If the command has this flag set, it handles its own unit
11482 * attention reporting, we shouldn't do anything. Otherwise we
11483 * check for any pending unit attentions, and send them back to the
11484 * initiator. We only do this when a command initially comes in,
11485 * not when we pull it off the blocked queue.
11487 * According to SAM-3, section 5.3.2, the order that things get
11488 * presented back to the host is basically unit attentions caused
11489 * by some sort of reset event, busy status, reservation conflicts
11490 * or task set full, and finally any other status.
11492 * One issue here is that some of the unit attentions we report
11493 * don't fall into the "reset" category (e.g. "reported luns data
11494 * has changed"). So reporting it here, before the reservation
11495 * check, may be technically wrong. I guess the only thing to do
11496 * would be to check for and report the reset events here, and then
11497 * check for the other unit attention types after we check for a
11498 * reservation conflict.
11500 * XXX KDM need to fix this
11502 if ((entry->flags & CTL_CMD_FLAG_NO_SENSE) == 0) {
11503 ctl_ua_type ua_type;
11504 u_int sense_len = 0;
11506 ua_type = ctl_build_ua(lun, initidx, &ctsio->sense_data,
11507 &sense_len, SSD_TYPE_NONE);
11508 if (ua_type != CTL_UA_NONE) {
11509 mtx_unlock(&lun->lun_lock);
11510 ctsio->scsi_status = SCSI_STATUS_CHECK_COND;
11511 ctsio->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
11512 ctsio->sense_len = sense_len;
11513 ctl_done((union ctl_io *)ctsio);
11519 if (ctl_scsiio_lun_check(lun, entry, ctsio) != 0) {
11520 mtx_unlock(&lun->lun_lock);
11521 ctl_done((union ctl_io *)ctsio);
11526 * XXX CHD this is where we want to send IO to other side if
11527 * this LUN is secondary on this SC. We will need to make a copy
11528 * of the IO and flag the IO on this side as SENT_2OTHER and the flag
11529 * the copy we send as FROM_OTHER.
11530 * We also need to stuff the address of the original IO so we can
11531 * find it easily. Something similar will need be done on the other
11532 * side so when we are done we can find the copy.
11534 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
11535 (lun->flags & CTL_LUN_PEER_SC_PRIMARY) != 0 &&
11536 (entry->flags & CTL_CMD_FLAG_RUN_HERE) == 0) {
11537 union ctl_ha_msg msg_info;
11540 ctsio->io_hdr.flags |= CTL_FLAG_SENT_2OTHER_SC;
11541 ctsio->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
11542 mtx_unlock(&lun->lun_lock);
11544 msg_info.hdr.msg_type = CTL_MSG_SERIALIZE;
11545 msg_info.hdr.original_sc = (union ctl_io *)ctsio;
11546 msg_info.hdr.serializing_sc = NULL;
11547 msg_info.hdr.nexus = ctsio->io_hdr.nexus;
11548 msg_info.scsi.tag_num = ctsio->tag_num;
11549 msg_info.scsi.tag_type = ctsio->tag_type;
11550 msg_info.scsi.cdb_len = ctsio->cdb_len;
11551 memcpy(msg_info.scsi.cdb, ctsio->cdb, CTL_MAX_CDBLEN);
11553 if ((isc_retval = ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11554 sizeof(msg_info.scsi) - sizeof(msg_info.scsi.sense_data),
11555 M_WAITOK)) > CTL_HA_STATUS_SUCCESS) {
11556 ctl_set_busy(ctsio);
11557 ctl_done((union ctl_io *)ctsio);
11563 bio = (union ctl_io *)TAILQ_PREV(&ctsio->io_hdr, ctl_ooaq, ooa_links);
11564 switch (ctl_check_ooa(lun, (union ctl_io *)ctsio, &bio)) {
11565 case CTL_ACTION_BLOCK:
11566 ctsio->io_hdr.blocker = bio;
11567 TAILQ_INSERT_TAIL(&bio->io_hdr.blocked_queue, &ctsio->io_hdr,
11569 mtx_unlock(&lun->lun_lock);
11571 case CTL_ACTION_PASS:
11572 case CTL_ACTION_SKIP:
11573 ctsio->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
11574 mtx_unlock(&lun->lun_lock);
11575 ctl_enqueue_rtr((union ctl_io *)ctsio);
11577 case CTL_ACTION_OVERLAP:
11578 mtx_unlock(&lun->lun_lock);
11579 ctl_set_overlapped_cmd(ctsio);
11580 ctl_done((union ctl_io *)ctsio);
11582 case CTL_ACTION_OVERLAP_TAG:
11583 mtx_unlock(&lun->lun_lock);
11584 ctl_set_overlapped_tag(ctsio, ctsio->tag_num & 0xff);
11585 ctl_done((union ctl_io *)ctsio);
11587 case CTL_ACTION_ERROR:
11589 mtx_unlock(&lun->lun_lock);
11590 ctl_set_internal_failure(ctsio,
11592 /*retry_count*/ 0);
11593 ctl_done((union ctl_io *)ctsio);
11599 const struct ctl_cmd_entry *
11600 ctl_get_cmd_entry(struct ctl_scsiio *ctsio, int *sa)
11602 const struct ctl_cmd_entry *entry;
11603 int service_action;
11605 entry = &ctl_cmd_table[ctsio->cdb[0]];
11607 *sa = ((entry->flags & CTL_CMD_FLAG_SA5) != 0);
11608 if (entry->flags & CTL_CMD_FLAG_SA5) {
11609 service_action = ctsio->cdb[1] & SERVICE_ACTION_MASK;
11610 entry = &((const struct ctl_cmd_entry *)
11611 entry->execute)[service_action];
11616 const struct ctl_cmd_entry *
11617 ctl_validate_command(struct ctl_scsiio *ctsio)
11619 const struct ctl_cmd_entry *entry;
11623 entry = ctl_get_cmd_entry(ctsio, &sa);
11624 if (entry->execute == NULL) {
11626 ctl_set_invalid_field(ctsio,
11633 ctl_set_invalid_opcode(ctsio);
11634 ctl_done((union ctl_io *)ctsio);
11637 KASSERT(entry->length > 0,
11638 ("Not defined length for command 0x%02x/0x%02x",
11639 ctsio->cdb[0], ctsio->cdb[1]));
11640 for (i = 1; i < entry->length; i++) {
11641 diff = ctsio->cdb[i] & ~entry->usage[i - 1];
11644 ctl_set_invalid_field(ctsio,
11649 /*bit*/ fls(diff) - 1);
11650 ctl_done((union ctl_io *)ctsio);
11657 ctl_cmd_applicable(uint8_t lun_type, const struct ctl_cmd_entry *entry)
11660 switch (lun_type) {
11662 if ((entry->flags & CTL_CMD_FLAG_OK_ON_DIRECT) == 0)
11666 if ((entry->flags & CTL_CMD_FLAG_OK_ON_PROC) == 0)
11670 if ((entry->flags & CTL_CMD_FLAG_OK_ON_CDROM) == 0)
11680 ctl_scsiio(struct ctl_scsiio *ctsio)
11683 const struct ctl_cmd_entry *entry;
11685 retval = CTL_RETVAL_COMPLETE;
11687 CTL_DEBUG_PRINT(("ctl_scsiio cdb[0]=%02X\n", ctsio->cdb[0]));
11689 entry = ctl_get_cmd_entry(ctsio, NULL);
11692 * If this I/O has been aborted, just send it straight to
11693 * ctl_done() without executing it.
11695 if (ctsio->io_hdr.flags & CTL_FLAG_ABORT) {
11696 ctl_done((union ctl_io *)ctsio);
11701 * All the checks should have been handled by ctl_scsiio_precheck().
11702 * We should be clear now to just execute the I/O.
11704 retval = entry->execute(ctsio);
11711 ctl_target_reset(union ctl_io *io)
11713 struct ctl_softc *softc = CTL_SOFTC(io);
11714 struct ctl_port *port = CTL_PORT(io);
11715 struct ctl_lun *lun;
11717 ctl_ua_type ua_type;
11719 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11720 union ctl_ha_msg msg_info;
11722 msg_info.hdr.nexus = io->io_hdr.nexus;
11723 msg_info.task.task_action = io->taskio.task_action;
11724 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11725 msg_info.hdr.original_sc = NULL;
11726 msg_info.hdr.serializing_sc = NULL;
11727 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11728 sizeof(msg_info.task), M_WAITOK);
11731 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11732 if (io->taskio.task_action == CTL_TASK_TARGET_RESET)
11733 ua_type = CTL_UA_TARG_RESET;
11735 ua_type = CTL_UA_BUS_RESET;
11736 mtx_lock(&softc->ctl_lock);
11737 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11738 if (port != NULL &&
11739 ctl_lun_map_to_port(port, lun->lun) == UINT32_MAX)
11741 ctl_do_lun_reset(lun, initidx, ua_type);
11743 mtx_unlock(&softc->ctl_lock);
11744 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11749 * The LUN should always be set. The I/O is optional, and is used to
11750 * distinguish between I/Os sent by this initiator, and by other
11751 * initiators. We set unit attention for initiators other than this one.
11752 * SAM-3 is vague on this point. It does say that a unit attention should
11753 * be established for other initiators when a LUN is reset (see section
11754 * 5.7.3), but it doesn't specifically say that the unit attention should
11755 * be established for this particular initiator when a LUN is reset. Here
11756 * is the relevant text, from SAM-3 rev 8:
11758 * 5.7.2 When a SCSI initiator port aborts its own tasks
11760 * When a SCSI initiator port causes its own task(s) to be aborted, no
11761 * notification that the task(s) have been aborted shall be returned to
11762 * the SCSI initiator port other than the completion response for the
11763 * command or task management function action that caused the task(s) to
11764 * be aborted and notification(s) associated with related effects of the
11765 * action (e.g., a reset unit attention condition).
11767 * XXX KDM for now, we're setting unit attention for all initiators.
11770 ctl_do_lun_reset(struct ctl_lun *lun, uint32_t initidx, ctl_ua_type ua_type)
11775 mtx_lock(&lun->lun_lock);
11777 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11778 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11779 xio->io_hdr.flags |= CTL_FLAG_ABORT | CTL_FLAG_ABORT_STATUS;
11780 ctl_try_unblock_io(lun, xio, FALSE);
11783 for (i = 0; i < ctl_max_ports; i++) {
11784 free(lun->pending_sense[i], M_CTL);
11785 lun->pending_sense[i] = NULL;
11787 /* Clear reservation. */
11788 lun->flags &= ~CTL_LUN_RESERVED;
11789 /* Clear prevent media removal. */
11790 if (lun->prevent) {
11791 for (i = 0; i < CTL_MAX_INITIATORS; i++)
11792 ctl_clear_mask(lun->prevent, i);
11793 lun->prevent_count = 0;
11795 /* Clear TPC status */
11796 ctl_tpc_lun_clear(lun, -1);
11797 /* Establish UA. */
11799 ctl_est_ua_all(lun, initidx, ua_type);
11801 ctl_est_ua_all(lun, -1, ua_type);
11803 mtx_unlock(&lun->lun_lock);
11807 ctl_lun_reset(union ctl_io *io)
11809 struct ctl_softc *softc = CTL_SOFTC(io);
11810 struct ctl_lun *lun;
11811 uint32_t targ_lun, initidx;
11813 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11814 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11815 mtx_lock(&softc->ctl_lock);
11816 if (targ_lun >= ctl_max_luns ||
11817 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11818 mtx_unlock(&softc->ctl_lock);
11819 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11822 ctl_do_lun_reset(lun, initidx, CTL_UA_LUN_RESET);
11823 mtx_unlock(&softc->ctl_lock);
11824 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11826 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0) {
11827 union ctl_ha_msg msg_info;
11829 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11830 msg_info.hdr.nexus = io->io_hdr.nexus;
11831 msg_info.task.task_action = CTL_TASK_LUN_RESET;
11832 msg_info.hdr.original_sc = NULL;
11833 msg_info.hdr.serializing_sc = NULL;
11834 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11835 sizeof(msg_info.task), M_WAITOK);
11841 ctl_abort_tasks_lun(struct ctl_lun *lun, uint32_t targ_port, uint32_t init_id,
11846 mtx_assert(&lun->lun_lock, MA_OWNED);
11849 * Run through the OOA queue and attempt to find the given I/O.
11850 * The target port, initiator ID, tag type and tag number have to
11851 * match the values that we got from the initiator. If we have an
11852 * untagged command to abort, simply abort the first untagged command
11853 * we come to. We only allow one untagged command at a time of course.
11855 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
11856 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
11858 if ((targ_port == UINT32_MAX ||
11859 targ_port == xio->io_hdr.nexus.targ_port) &&
11860 (init_id == UINT32_MAX ||
11861 init_id == xio->io_hdr.nexus.initid)) {
11862 if (targ_port != xio->io_hdr.nexus.targ_port ||
11863 init_id != xio->io_hdr.nexus.initid)
11864 xio->io_hdr.flags |= CTL_FLAG_ABORT_STATUS;
11865 xio->io_hdr.flags |= CTL_FLAG_ABORT;
11866 if (!other_sc && !(lun->flags & CTL_LUN_PRIMARY_SC)) {
11867 union ctl_ha_msg msg_info;
11869 msg_info.hdr.nexus = xio->io_hdr.nexus;
11870 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
11871 msg_info.task.tag_num = xio->scsiio.tag_num;
11872 msg_info.task.tag_type = xio->scsiio.tag_type;
11873 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11874 msg_info.hdr.original_sc = NULL;
11875 msg_info.hdr.serializing_sc = NULL;
11876 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11877 sizeof(msg_info.task), M_NOWAIT);
11879 ctl_try_unblock_io(lun, xio, FALSE);
11885 ctl_abort_task_set(union ctl_io *io)
11887 struct ctl_softc *softc = CTL_SOFTC(io);
11888 struct ctl_lun *lun;
11894 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11895 mtx_lock(&softc->ctl_lock);
11896 if (targ_lun >= ctl_max_luns ||
11897 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11898 mtx_unlock(&softc->ctl_lock);
11899 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11903 mtx_lock(&lun->lun_lock);
11904 mtx_unlock(&softc->ctl_lock);
11905 if (io->taskio.task_action == CTL_TASK_ABORT_TASK_SET) {
11906 ctl_abort_tasks_lun(lun, io->io_hdr.nexus.targ_port,
11907 io->io_hdr.nexus.initid,
11908 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11909 } else { /* CTL_TASK_CLEAR_TASK_SET */
11910 ctl_abort_tasks_lun(lun, UINT32_MAX, UINT32_MAX,
11911 (io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) != 0);
11913 mtx_unlock(&lun->lun_lock);
11914 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11919 ctl_i_t_nexus_loss(struct ctl_softc *softc, uint32_t initidx,
11920 ctl_ua_type ua_type)
11922 struct ctl_lun *lun;
11923 struct scsi_sense_data *ps;
11926 p = initidx / CTL_MAX_INIT_PER_PORT;
11927 i = initidx % CTL_MAX_INIT_PER_PORT;
11928 mtx_lock(&softc->ctl_lock);
11929 STAILQ_FOREACH(lun, &softc->lun_list, links) {
11930 mtx_lock(&lun->lun_lock);
11932 ctl_abort_tasks_lun(lun, p, i, 1);
11934 ps = lun->pending_sense[p];
11936 ps[i].error_code = 0;
11937 /* Clear reservation. */
11938 if ((lun->flags & CTL_LUN_RESERVED) && (lun->res_idx == initidx))
11939 lun->flags &= ~CTL_LUN_RESERVED;
11940 /* Clear prevent media removal. */
11941 if (lun->prevent && ctl_is_set(lun->prevent, initidx)) {
11942 ctl_clear_mask(lun->prevent, initidx);
11943 lun->prevent_count--;
11945 /* Clear TPC status */
11946 ctl_tpc_lun_clear(lun, initidx);
11947 /* Establish UA. */
11948 ctl_est_ua(lun, initidx, ua_type);
11949 mtx_unlock(&lun->lun_lock);
11951 mtx_unlock(&softc->ctl_lock);
11955 ctl_i_t_nexus_reset(union ctl_io *io)
11957 struct ctl_softc *softc = CTL_SOFTC(io);
11960 if (!(io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC)) {
11961 union ctl_ha_msg msg_info;
11963 msg_info.hdr.nexus = io->io_hdr.nexus;
11964 msg_info.task.task_action = CTL_TASK_I_T_NEXUS_RESET;
11965 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
11966 msg_info.hdr.original_sc = NULL;
11967 msg_info.hdr.serializing_sc = NULL;
11968 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
11969 sizeof(msg_info.task), M_WAITOK);
11972 initidx = ctl_get_initindex(&io->io_hdr.nexus);
11973 ctl_i_t_nexus_loss(softc, initidx, CTL_UA_I_T_NEXUS_LOSS);
11974 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
11979 ctl_abort_task(union ctl_io *io)
11981 struct ctl_softc *softc = CTL_SOFTC(io);
11983 struct ctl_lun *lun;
11989 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
11990 mtx_lock(&softc->ctl_lock);
11991 if (targ_lun >= ctl_max_luns ||
11992 (lun = softc->ctl_luns[targ_lun]) == NULL) {
11993 mtx_unlock(&softc->ctl_lock);
11994 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
11998 mtx_lock(&lun->lun_lock);
11999 mtx_unlock(&softc->ctl_lock);
12001 * Run through the OOA queue and attempt to find the given I/O.
12002 * The target port, initiator ID, tag type and tag number have to
12003 * match the values that we got from the initiator. If we have an
12004 * untagged command to abort, simply abort the first untagged command
12005 * we come to. We only allow one untagged command at a time of course.
12007 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12008 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12010 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12011 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12012 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12016 * If the abort says that the task is untagged, the
12017 * task in the queue must be untagged. Otherwise,
12018 * we just check to see whether the tag numbers
12019 * match. This is because the QLogic firmware
12020 * doesn't pass back the tag type in an abort
12024 if (((xio->scsiio.tag_type == CTL_TAG_UNTAGGED)
12025 && (io->taskio.tag_type == CTL_TAG_UNTAGGED))
12026 || (xio->scsiio.tag_num == io->taskio.tag_num)) {
12029 * XXX KDM we've got problems with FC, because it
12030 * doesn't send down a tag type with aborts. So we
12031 * can only really go by the tag number...
12032 * This may cause problems with parallel SCSI.
12033 * Need to figure that out!!
12035 if (xio->scsiio.tag_num == io->taskio.tag_num) {
12037 xio->io_hdr.flags |= CTL_FLAG_ABORT;
12038 if ((io->io_hdr.flags & CTL_FLAG_FROM_OTHER_SC) == 0 &&
12039 !(lun->flags & CTL_LUN_PRIMARY_SC)) {
12040 union ctl_ha_msg msg_info;
12042 msg_info.hdr.nexus = io->io_hdr.nexus;
12043 msg_info.task.task_action = CTL_TASK_ABORT_TASK;
12044 msg_info.task.tag_num = io->taskio.tag_num;
12045 msg_info.task.tag_type = io->taskio.tag_type;
12046 msg_info.hdr.msg_type = CTL_MSG_MANAGE_TASKS;
12047 msg_info.hdr.original_sc = NULL;
12048 msg_info.hdr.serializing_sc = NULL;
12049 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg_info,
12050 sizeof(msg_info.task), M_NOWAIT);
12052 ctl_try_unblock_io(lun, xio, FALSE);
12055 mtx_unlock(&lun->lun_lock);
12056 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12061 ctl_query_task(union ctl_io *io, int task_set)
12063 struct ctl_softc *softc = CTL_SOFTC(io);
12065 struct ctl_lun *lun;
12069 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12070 mtx_lock(&softc->ctl_lock);
12071 if (targ_lun >= ctl_max_luns ||
12072 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12073 mtx_unlock(&softc->ctl_lock);
12074 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12077 mtx_lock(&lun->lun_lock);
12078 mtx_unlock(&softc->ctl_lock);
12079 for (xio = (union ctl_io *)TAILQ_FIRST(&lun->ooa_queue); xio != NULL;
12080 xio = (union ctl_io *)TAILQ_NEXT(&xio->io_hdr, ooa_links)) {
12082 if ((xio->io_hdr.nexus.targ_port != io->io_hdr.nexus.targ_port)
12083 || (xio->io_hdr.nexus.initid != io->io_hdr.nexus.initid)
12084 || (xio->io_hdr.flags & CTL_FLAG_ABORT))
12087 if (task_set || xio->scsiio.tag_num == io->taskio.tag_num) {
12092 mtx_unlock(&lun->lun_lock);
12094 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12096 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12101 ctl_query_async_event(union ctl_io *io)
12103 struct ctl_softc *softc = CTL_SOFTC(io);
12104 struct ctl_lun *lun;
12106 uint32_t targ_lun, initidx;
12108 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12109 mtx_lock(&softc->ctl_lock);
12110 if (targ_lun >= ctl_max_luns ||
12111 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12112 mtx_unlock(&softc->ctl_lock);
12113 io->taskio.task_status = CTL_TASK_LUN_DOES_NOT_EXIST;
12116 mtx_lock(&lun->lun_lock);
12117 mtx_unlock(&softc->ctl_lock);
12118 initidx = ctl_get_initindex(&io->io_hdr.nexus);
12119 ua = ctl_build_qae(lun, initidx, io->taskio.task_resp);
12120 mtx_unlock(&lun->lun_lock);
12121 if (ua != CTL_UA_NONE)
12122 io->taskio.task_status = CTL_TASK_FUNCTION_SUCCEEDED;
12124 io->taskio.task_status = CTL_TASK_FUNCTION_COMPLETE;
12129 ctl_run_task(union ctl_io *io)
12133 CTL_DEBUG_PRINT(("ctl_run_task\n"));
12134 KASSERT(io->io_hdr.io_type == CTL_IO_TASK,
12135 ("ctl_run_task: Unextected io_type %d\n", io->io_hdr.io_type));
12136 io->taskio.task_status = CTL_TASK_FUNCTION_NOT_SUPPORTED;
12137 bzero(io->taskio.task_resp, sizeof(io->taskio.task_resp));
12138 switch (io->taskio.task_action) {
12139 case CTL_TASK_ABORT_TASK:
12140 retval = ctl_abort_task(io);
12142 case CTL_TASK_ABORT_TASK_SET:
12143 case CTL_TASK_CLEAR_TASK_SET:
12144 retval = ctl_abort_task_set(io);
12146 case CTL_TASK_CLEAR_ACA:
12148 case CTL_TASK_I_T_NEXUS_RESET:
12149 retval = ctl_i_t_nexus_reset(io);
12151 case CTL_TASK_LUN_RESET:
12152 retval = ctl_lun_reset(io);
12154 case CTL_TASK_TARGET_RESET:
12155 case CTL_TASK_BUS_RESET:
12156 retval = ctl_target_reset(io);
12158 case CTL_TASK_PORT_LOGIN:
12160 case CTL_TASK_PORT_LOGOUT:
12162 case CTL_TASK_QUERY_TASK:
12163 retval = ctl_query_task(io, 0);
12165 case CTL_TASK_QUERY_TASK_SET:
12166 retval = ctl_query_task(io, 1);
12168 case CTL_TASK_QUERY_ASYNC_EVENT:
12169 retval = ctl_query_async_event(io);
12172 printf("%s: got unknown task management event %d\n",
12173 __func__, io->taskio.task_action);
12177 io->io_hdr.status = CTL_SUCCESS;
12179 io->io_hdr.status = CTL_ERROR;
12184 * For HA operation. Handle commands that come in from the other
12188 ctl_handle_isc(union ctl_io *io)
12190 struct ctl_softc *softc = CTL_SOFTC(io);
12191 struct ctl_lun *lun;
12192 const struct ctl_cmd_entry *entry;
12195 targ_lun = io->io_hdr.nexus.targ_mapped_lun;
12196 switch (io->io_hdr.msg_type) {
12197 case CTL_MSG_SERIALIZE:
12198 ctl_serialize_other_sc_cmd(&io->scsiio);
12200 case CTL_MSG_R2R: /* Only used in SER_ONLY mode. */
12201 entry = ctl_get_cmd_entry(&io->scsiio, NULL);
12202 if (targ_lun >= ctl_max_luns ||
12203 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12207 mtx_lock(&lun->lun_lock);
12208 if (ctl_scsiio_lun_check(lun, entry, &io->scsiio) != 0) {
12209 mtx_unlock(&lun->lun_lock);
12213 io->io_hdr.flags |= CTL_FLAG_IS_WAS_ON_RTR;
12214 mtx_unlock(&lun->lun_lock);
12215 ctl_enqueue_rtr(io);
12217 case CTL_MSG_FINISH_IO:
12218 if (softc->ha_mode == CTL_HA_MODE_XFER) {
12222 if (targ_lun >= ctl_max_luns ||
12223 (lun = softc->ctl_luns[targ_lun]) == NULL) {
12227 mtx_lock(&lun->lun_lock);
12228 ctl_try_unblock_others(lun, io, TRUE);
12229 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
12230 mtx_unlock(&lun->lun_lock);
12233 case CTL_MSG_PERS_ACTION:
12234 ctl_hndl_per_res_out_on_other_sc(io);
12237 case CTL_MSG_BAD_JUJU:
12240 case CTL_MSG_DATAMOVE: /* Only used in XFER mode */
12241 ctl_datamove_remote(io);
12243 case CTL_MSG_DATAMOVE_DONE: /* Only used in XFER mode */
12244 io->scsiio.be_move_done(io);
12246 case CTL_MSG_FAILOVER:
12247 ctl_failover_lun(io);
12251 printf("%s: Invalid message type %d\n",
12252 __func__, io->io_hdr.msg_type);
12261 * Returns the match type in the case of a match, or CTL_LUN_PAT_NONE if
12262 * there is no match.
12264 static ctl_lun_error_pattern
12265 ctl_cmd_pattern_match(struct ctl_scsiio *ctsio, struct ctl_error_desc *desc)
12267 const struct ctl_cmd_entry *entry;
12268 ctl_lun_error_pattern filtered_pattern, pattern;
12270 pattern = desc->error_pattern;
12273 * XXX KDM we need more data passed into this function to match a
12274 * custom pattern, and we actually need to implement custom pattern
12277 if (pattern & CTL_LUN_PAT_CMD)
12278 return (CTL_LUN_PAT_CMD);
12280 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_ANY)
12281 return (CTL_LUN_PAT_ANY);
12283 entry = ctl_get_cmd_entry(ctsio, NULL);
12285 filtered_pattern = entry->pattern & pattern;
12288 * If the user requested specific flags in the pattern (e.g.
12289 * CTL_LUN_PAT_RANGE), make sure the command supports all of those
12292 * If the user did not specify any flags, it doesn't matter whether
12293 * or not the command supports the flags.
12295 if ((filtered_pattern & ~CTL_LUN_PAT_MASK) !=
12296 (pattern & ~CTL_LUN_PAT_MASK))
12297 return (CTL_LUN_PAT_NONE);
12300 * If the user asked for a range check, see if the requested LBA
12301 * range overlaps with this command's LBA range.
12303 if (filtered_pattern & CTL_LUN_PAT_RANGE) {
12309 retval = ctl_get_lba_len((union ctl_io *)ctsio, &lba1, &len1);
12311 return (CTL_LUN_PAT_NONE);
12313 action = ctl_extent_check_lba(lba1, len1, desc->lba_range.lba,
12314 desc->lba_range.len, FALSE);
12316 * A "pass" means that the LBA ranges don't overlap, so
12317 * this doesn't match the user's range criteria.
12319 if (action == CTL_ACTION_PASS)
12320 return (CTL_LUN_PAT_NONE);
12323 return (filtered_pattern);
12327 ctl_inject_error(struct ctl_lun *lun, union ctl_io *io)
12329 struct ctl_error_desc *desc, *desc2;
12331 mtx_assert(&lun->lun_lock, MA_OWNED);
12333 STAILQ_FOREACH_SAFE(desc, &lun->error_list, links, desc2) {
12334 ctl_lun_error_pattern pattern;
12336 * Check to see whether this particular command matches
12337 * the pattern in the descriptor.
12339 pattern = ctl_cmd_pattern_match(&io->scsiio, desc);
12340 if ((pattern & CTL_LUN_PAT_MASK) == CTL_LUN_PAT_NONE)
12343 switch (desc->lun_error & CTL_LUN_INJ_TYPE) {
12344 case CTL_LUN_INJ_ABORTED:
12345 ctl_set_aborted(&io->scsiio);
12347 case CTL_LUN_INJ_MEDIUM_ERR:
12348 ctl_set_medium_error(&io->scsiio,
12349 (io->io_hdr.flags & CTL_FLAG_DATA_MASK) !=
12350 CTL_FLAG_DATA_OUT);
12352 case CTL_LUN_INJ_UA:
12353 /* 29h/00h POWER ON, RESET, OR BUS DEVICE RESET
12355 ctl_set_ua(&io->scsiio, 0x29, 0x00);
12357 case CTL_LUN_INJ_CUSTOM:
12359 * We're assuming the user knows what he is doing.
12360 * Just copy the sense information without doing
12363 bcopy(&desc->custom_sense, &io->scsiio.sense_data,
12364 MIN(sizeof(desc->custom_sense),
12365 sizeof(io->scsiio.sense_data)));
12366 io->scsiio.scsi_status = SCSI_STATUS_CHECK_COND;
12367 io->scsiio.sense_len = SSD_FULL_SIZE;
12368 io->io_hdr.status = CTL_SCSI_ERROR | CTL_AUTOSENSE;
12370 case CTL_LUN_INJ_NONE:
12373 * If this is an error injection type we don't know
12374 * about, clear the continuous flag (if it is set)
12375 * so it will get deleted below.
12377 desc->lun_error &= ~CTL_LUN_INJ_CONTINUOUS;
12381 * By default, each error injection action is a one-shot
12383 if (desc->lun_error & CTL_LUN_INJ_CONTINUOUS)
12386 STAILQ_REMOVE(&lun->error_list, desc, ctl_error_desc, links);
12392 #ifdef CTL_IO_DELAY
12394 ctl_datamove_timer_wakeup(void *arg)
12398 io = (union ctl_io *)arg;
12402 #endif /* CTL_IO_DELAY */
12405 ctl_datamove(union ctl_io *io)
12407 void (*fe_datamove)(union ctl_io *io);
12409 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12411 CTL_DEBUG_PRINT(("ctl_datamove\n"));
12413 /* No data transferred yet. Frontend must update this when done. */
12414 io->scsiio.kern_data_resid = io->scsiio.kern_data_len;
12417 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12422 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12423 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12425 sbuf_cat(&sb, path_str);
12426 switch (io->io_hdr.io_type) {
12428 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12429 sbuf_printf(&sb, "\n");
12430 sbuf_cat(&sb, path_str);
12431 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12432 io->scsiio.tag_num, io->scsiio.tag_type);
12435 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12436 "Tag Type: %d\n", io->taskio.task_action,
12437 io->taskio.tag_num, io->taskio.tag_type);
12440 panic("%s: Invalid CTL I/O type %d\n",
12441 __func__, io->io_hdr.io_type);
12443 sbuf_cat(&sb, path_str);
12444 sbuf_printf(&sb, "ctl_datamove: %jd seconds\n",
12445 (intmax_t)time_uptime - io->io_hdr.start_time);
12447 printf("%s", sbuf_data(&sb));
12449 #endif /* CTL_TIME_IO */
12451 #ifdef CTL_IO_DELAY
12452 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
12453 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
12455 struct ctl_lun *lun;
12459 && (lun->delay_info.datamove_delay > 0)) {
12461 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
12462 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
12463 callout_reset(&io->io_hdr.delay_callout,
12464 lun->delay_info.datamove_delay * hz,
12465 ctl_datamove_timer_wakeup, io);
12466 if (lun->delay_info.datamove_type ==
12467 CTL_DELAY_TYPE_ONESHOT)
12468 lun->delay_info.datamove_delay = 0;
12475 * This command has been aborted. Set the port status, so we fail
12478 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12479 printf("ctl_datamove: tag 0x%04x on (%u:%u:%u) aborted\n",
12480 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12481 io->io_hdr.nexus.targ_port,
12482 io->io_hdr.nexus.targ_lun);
12483 io->io_hdr.port_status = 31337;
12485 * Note that the backend, in this case, will get the
12486 * callback in its context. In other cases it may get
12487 * called in the frontend's interrupt thread context.
12489 io->scsiio.be_move_done(io);
12493 /* Don't confuse frontend with zero length data move. */
12494 if (io->scsiio.kern_data_len == 0) {
12495 io->scsiio.be_move_done(io);
12499 fe_datamove = CTL_PORT(io)->fe_datamove;
12504 ctl_send_datamove_done(union ctl_io *io, int have_lock)
12506 union ctl_ha_msg msg;
12508 struct bintime cur_bt;
12511 memset(&msg, 0, sizeof(msg));
12512 msg.hdr.msg_type = CTL_MSG_DATAMOVE_DONE;
12513 msg.hdr.original_sc = io;
12514 msg.hdr.serializing_sc = io->io_hdr.remote_io;
12515 msg.hdr.nexus = io->io_hdr.nexus;
12516 msg.hdr.status = io->io_hdr.status;
12517 msg.scsi.kern_data_resid = io->scsiio.kern_data_resid;
12518 msg.scsi.tag_num = io->scsiio.tag_num;
12519 msg.scsi.tag_type = io->scsiio.tag_type;
12520 msg.scsi.scsi_status = io->scsiio.scsi_status;
12521 memcpy(&msg.scsi.sense_data, &io->scsiio.sense_data,
12522 io->scsiio.sense_len);
12523 msg.scsi.sense_len = io->scsiio.sense_len;
12524 msg.scsi.port_status = io->io_hdr.port_status;
12525 io->io_hdr.flags &= ~CTL_FLAG_IO_ACTIVE;
12526 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12527 ctl_failover_io(io, /*have_lock*/ have_lock);
12530 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
12531 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data) +
12532 msg.scsi.sense_len, M_WAITOK);
12535 getbinuptime(&cur_bt);
12536 bintime_sub(&cur_bt, &io->io_hdr.dma_start_bt);
12537 bintime_add(&io->io_hdr.dma_bt, &cur_bt);
12539 io->io_hdr.num_dmas++;
12543 * The DMA to the remote side is done, now we need to tell the other side
12544 * we're done so it can continue with its data movement.
12547 ctl_datamove_remote_write_cb(struct ctl_ha_dt_req *rq)
12554 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12555 printf("%s: ISC DMA write failed with error %d", __func__,
12557 ctl_set_internal_failure(&io->scsiio,
12559 /*retry_count*/ rq->ret);
12562 ctl_dt_req_free(rq);
12564 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12565 free(CTL_LSGLT(io)[i].addr, M_CTL);
12566 free(CTL_RSGL(io), M_CTL);
12567 CTL_RSGL(io) = NULL;
12568 CTL_LSGL(io) = NULL;
12571 * The data is in local and remote memory, so now we need to send
12572 * status (good or back) back to the other side.
12574 ctl_send_datamove_done(io, /*have_lock*/ 0);
12578 * We've moved the data from the host/controller into local memory. Now we
12579 * need to push it over to the remote controller's memory.
12582 ctl_datamove_remote_dm_write_cb(union ctl_io *io)
12586 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_WRITE,
12587 ctl_datamove_remote_write_cb);
12592 ctl_datamove_remote_write(union ctl_io *io)
12595 void (*fe_datamove)(union ctl_io *io);
12598 * - Get the data from the host/HBA into local memory.
12599 * - DMA memory from the local controller to the remote controller.
12600 * - Send status back to the remote controller.
12603 retval = ctl_datamove_remote_sgl_setup(io);
12607 /* Switch the pointer over so the FETD knows what to do */
12608 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12611 * Use a custom move done callback, since we need to send completion
12612 * back to the other controller, not to the backend on this side.
12614 io->scsiio.be_move_done = ctl_datamove_remote_dm_write_cb;
12616 fe_datamove = CTL_PORT(io)->fe_datamove;
12621 ctl_datamove_remote_dm_read_cb(union ctl_io *io)
12625 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12626 free(CTL_LSGLT(io)[i].addr, M_CTL);
12627 free(CTL_RSGL(io), M_CTL);
12628 CTL_RSGL(io) = NULL;
12629 CTL_LSGL(io) = NULL;
12632 * The read is done, now we need to send status (good or bad) back
12633 * to the other side.
12635 ctl_send_datamove_done(io, /*have_lock*/ 0);
12641 ctl_datamove_remote_read_cb(struct ctl_ha_dt_req *rq)
12644 void (*fe_datamove)(union ctl_io *io);
12648 if (rq->ret != CTL_HA_STATUS_SUCCESS) {
12649 printf("%s: ISC DMA read failed with error %d\n", __func__,
12651 ctl_set_internal_failure(&io->scsiio,
12653 /*retry_count*/ rq->ret);
12656 ctl_dt_req_free(rq);
12658 /* Switch the pointer over so the FETD knows what to do */
12659 io->scsiio.kern_data_ptr = (uint8_t *)CTL_LSGL(io);
12662 * Use a custom move done callback, since we need to send completion
12663 * back to the other controller, not to the backend on this side.
12665 io->scsiio.be_move_done = ctl_datamove_remote_dm_read_cb;
12667 /* XXX KDM add checks like the ones in ctl_datamove? */
12669 fe_datamove = CTL_PORT(io)->fe_datamove;
12674 ctl_datamove_remote_sgl_setup(union ctl_io *io)
12676 struct ctl_sg_entry *local_sglist;
12677 uint32_t len_to_go;
12682 local_sglist = CTL_LSGL(io);
12683 len_to_go = io->scsiio.kern_data_len;
12686 * The difficult thing here is that the size of the various
12687 * S/G segments may be different than the size from the
12688 * remote controller. That'll make it harder when DMAing
12689 * the data back to the other side.
12691 for (i = 0; len_to_go > 0; i++) {
12692 local_sglist[i].len = MIN(len_to_go, CTL_HA_DATAMOVE_SEGMENT);
12693 local_sglist[i].addr =
12694 malloc(local_sglist[i].len, M_CTL, M_WAITOK);
12696 len_to_go -= local_sglist[i].len;
12699 * Reset the number of S/G entries accordingly. The original
12700 * number of S/G entries is available in rem_sg_entries.
12702 io->scsiio.kern_sg_entries = i;
12708 ctl_datamove_remote_xfer(union ctl_io *io, unsigned command,
12709 ctl_ha_dt_cb callback)
12711 struct ctl_ha_dt_req *rq;
12712 struct ctl_sg_entry *remote_sglist, *local_sglist;
12713 uint32_t local_used, remote_used, total_used;
12716 rq = ctl_dt_req_alloc();
12719 * If we failed to allocate the request, and if the DMA didn't fail
12720 * anyway, set busy status. This is just a resource allocation
12724 && ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12725 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS))
12726 ctl_set_busy(&io->scsiio);
12728 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_STATUS_NONE &&
12729 (io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
12732 ctl_dt_req_free(rq);
12735 * The data move failed. We need to return status back
12736 * to the other controller. No point in trying to DMA
12737 * data to the remote controller.
12740 ctl_send_datamove_done(io, /*have_lock*/ 0);
12745 local_sglist = CTL_LSGL(io);
12746 remote_sglist = CTL_RSGL(io);
12752 * Pull/push the data over the wire from/to the other controller.
12753 * This takes into account the possibility that the local and
12754 * remote sglists may not be identical in terms of the size of
12755 * the elements and the number of elements.
12757 * One fundamental assumption here is that the length allocated for
12758 * both the local and remote sglists is identical. Otherwise, we've
12759 * essentially got a coding error of some sort.
12761 isc_ret = CTL_HA_STATUS_SUCCESS;
12762 for (i = 0, j = 0; total_used < io->scsiio.kern_data_len; ) {
12766 rq->command = command;
12770 * Both pointers should be aligned. But it is possible
12771 * that the allocation length is not. They should both
12772 * also have enough slack left over at the end, though,
12773 * to round up to the next 8 byte boundary.
12775 cur_len = MIN(local_sglist[i].len - local_used,
12776 remote_sglist[j].len - remote_used);
12777 rq->size = cur_len;
12779 tmp_ptr = (uint8_t *)local_sglist[i].addr;
12780 tmp_ptr += local_used;
12783 /* Use physical addresses when talking to ISC hardware */
12784 if ((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0) {
12785 /* XXX KDM use busdma */
12786 rq->local = vtophys(tmp_ptr);
12788 rq->local = tmp_ptr;
12790 KASSERT((io->io_hdr.flags & CTL_FLAG_BUS_ADDR) == 0,
12791 ("HA does not support BUS_ADDR"));
12792 rq->local = tmp_ptr;
12795 tmp_ptr = (uint8_t *)remote_sglist[j].addr;
12796 tmp_ptr += remote_used;
12797 rq->remote = tmp_ptr;
12799 rq->callback = NULL;
12801 local_used += cur_len;
12802 if (local_used >= local_sglist[i].len) {
12807 remote_used += cur_len;
12808 if (remote_used >= remote_sglist[j].len) {
12812 total_used += cur_len;
12814 if (total_used >= io->scsiio.kern_data_len)
12815 rq->callback = callback;
12817 isc_ret = ctl_dt_single(rq);
12818 if (isc_ret > CTL_HA_STATUS_SUCCESS)
12821 if (isc_ret != CTL_HA_STATUS_WAIT) {
12830 ctl_datamove_remote_read(union ctl_io *io)
12836 * This will send an error to the other controller in the case of a
12839 retval = ctl_datamove_remote_sgl_setup(io);
12843 retval = ctl_datamove_remote_xfer(io, CTL_HA_DT_CMD_READ,
12844 ctl_datamove_remote_read_cb);
12847 * Make sure we free memory if there was an error.. The
12848 * ctl_datamove_remote_xfer() function will send the
12849 * datamove done message, or call the callback with an
12850 * error if there is a problem.
12852 for (i = 0; i < io->scsiio.kern_sg_entries; i++)
12853 free(CTL_LSGLT(io)[i].addr, M_CTL);
12854 free(CTL_RSGL(io), M_CTL);
12855 CTL_RSGL(io) = NULL;
12856 CTL_LSGL(io) = NULL;
12861 * Process a datamove request from the other controller. This is used for
12862 * XFER mode only, not SER_ONLY mode. For writes, we DMA into local memory
12863 * first. Once that is complete, the data gets DMAed into the remote
12864 * controller's memory. For reads, we DMA from the remote controller's
12865 * memory into our memory first, and then move it out to the FETD.
12868 ctl_datamove_remote(union ctl_io *io)
12871 mtx_assert(&((struct ctl_softc *)CTL_SOFTC(io))->ctl_lock, MA_NOTOWNED);
12873 if (io->io_hdr.flags & CTL_FLAG_FAILOVER) {
12874 ctl_failover_io(io, /*have_lock*/ 0);
12879 * Note that we look for an aborted I/O here, but don't do some of
12880 * the other checks that ctl_datamove() normally does.
12881 * We don't need to run the datamove delay code, since that should
12882 * have been done if need be on the other controller.
12884 if (io->io_hdr.flags & CTL_FLAG_ABORT) {
12885 printf("%s: tag 0x%04x on (%u:%u:%u) aborted\n", __func__,
12886 io->scsiio.tag_num, io->io_hdr.nexus.initid,
12887 io->io_hdr.nexus.targ_port,
12888 io->io_hdr.nexus.targ_lun);
12889 io->io_hdr.port_status = 31338;
12890 ctl_send_datamove_done(io, /*have_lock*/ 0);
12894 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_OUT)
12895 ctl_datamove_remote_write(io);
12896 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) == CTL_FLAG_DATA_IN)
12897 ctl_datamove_remote_read(io);
12899 io->io_hdr.port_status = 31339;
12900 ctl_send_datamove_done(io, /*have_lock*/ 0);
12905 ctl_process_done(union ctl_io *io)
12907 struct ctl_softc *softc = CTL_SOFTC(io);
12908 struct ctl_port *port = CTL_PORT(io);
12909 struct ctl_lun *lun = CTL_LUN(io);
12910 void (*fe_done)(union ctl_io *io);
12911 union ctl_ha_msg msg;
12913 CTL_DEBUG_PRINT(("ctl_process_done\n"));
12914 fe_done = port->fe_done;
12917 if ((time_uptime - io->io_hdr.start_time) > ctl_time_io_secs) {
12922 ctl_scsi_path_string(io, path_str, sizeof(path_str));
12923 sbuf_new(&sb, str, sizeof(str), SBUF_FIXEDLEN);
12925 sbuf_cat(&sb, path_str);
12926 switch (io->io_hdr.io_type) {
12928 ctl_scsi_command_string(&io->scsiio, NULL, &sb);
12929 sbuf_printf(&sb, "\n");
12930 sbuf_cat(&sb, path_str);
12931 sbuf_printf(&sb, "Tag: 0x%04x, type %d\n",
12932 io->scsiio.tag_num, io->scsiio.tag_type);
12935 sbuf_printf(&sb, "Task I/O type: %d, Tag: 0x%04x, "
12936 "Tag Type: %d\n", io->taskio.task_action,
12937 io->taskio.tag_num, io->taskio.tag_type);
12940 panic("%s: Invalid CTL I/O type %d\n",
12941 __func__, io->io_hdr.io_type);
12943 sbuf_cat(&sb, path_str);
12944 sbuf_printf(&sb, "ctl_process_done: %jd seconds\n",
12945 (intmax_t)time_uptime - io->io_hdr.start_time);
12947 printf("%s", sbuf_data(&sb));
12949 #endif /* CTL_TIME_IO */
12951 switch (io->io_hdr.io_type) {
12955 if (ctl_debug & CTL_DEBUG_INFO)
12956 ctl_io_error_print(io, NULL);
12960 panic("%s: Invalid CTL I/O type %d\n",
12961 __func__, io->io_hdr.io_type);
12965 CTL_DEBUG_PRINT(("NULL LUN for lun %d\n",
12966 io->io_hdr.nexus.targ_mapped_lun));
12970 mtx_lock(&lun->lun_lock);
12973 * Check to see if we have any informational exception and status
12974 * of this command can be modified to report it in form of either
12975 * RECOVERED ERROR or NO SENSE, depending on MRIE mode page field.
12977 if (lun->ie_reported == 0 && lun->ie_asc != 0 &&
12978 io->io_hdr.status == CTL_SUCCESS &&
12979 (io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0) {
12980 uint8_t mrie = lun->MODE_IE.mrie;
12981 uint8_t per = ((lun->MODE_RWER.byte3 & SMS_RWER_PER) ||
12982 (lun->MODE_VER.byte3 & SMS_VER_PER));
12983 if (((mrie == SIEP_MRIE_REC_COND && per) ||
12984 mrie == SIEP_MRIE_REC_UNCOND ||
12985 mrie == SIEP_MRIE_NO_SENSE) &&
12986 (ctl_get_cmd_entry(&io->scsiio, NULL)->flags &
12987 CTL_CMD_FLAG_NO_SENSE) == 0) {
12988 ctl_set_sense(&io->scsiio,
12989 /*current_error*/ 1,
12990 /*sense_key*/ (mrie == SIEP_MRIE_NO_SENSE) ?
12991 SSD_KEY_NO_SENSE : SSD_KEY_RECOVERED_ERROR,
12992 /*asc*/ lun->ie_asc,
12993 /*ascq*/ lun->ie_ascq,
12995 lun->ie_reported = 1;
12997 } else if (lun->ie_reported < 0)
12998 lun->ie_reported = 0;
13001 * Check to see if we have any errors to inject here. We only
13002 * inject errors for commands that don't already have errors set.
13004 if (!STAILQ_EMPTY(&lun->error_list) &&
13005 ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) &&
13006 ((io->io_hdr.flags & CTL_FLAG_STATUS_SENT) == 0))
13007 ctl_inject_error(lun, io);
13010 * XXX KDM how do we treat commands that aren't completed
13013 * XXX KDM should we also track I/O latency?
13015 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS &&
13016 io->io_hdr.io_type == CTL_IO_SCSI) {
13022 bintime_sub(&bt, &io->io_hdr.start_bt);
13024 if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13026 type = CTL_STATS_READ;
13027 else if ((io->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
13029 type = CTL_STATS_WRITE;
13031 type = CTL_STATS_NO_IO;
13033 lun->stats.bytes[type] += io->scsiio.kern_total_len;
13034 lun->stats.operations[type] ++;
13035 lun->stats.dmas[type] += io->io_hdr.num_dmas;
13037 bintime_add(&lun->stats.dma_time[type], &io->io_hdr.dma_bt);
13038 bintime_add(&lun->stats.time[type], &bt);
13041 mtx_lock(&port->port_lock);
13042 port->stats.bytes[type] += io->scsiio.kern_total_len;
13043 port->stats.operations[type] ++;
13044 port->stats.dmas[type] += io->io_hdr.num_dmas;
13046 bintime_add(&port->stats.dma_time[type], &io->io_hdr.dma_bt);
13047 bintime_add(&port->stats.time[type], &bt);
13049 mtx_unlock(&port->port_lock);
13053 * Run through the blocked queue of this I/O and see if anything
13054 * can be unblocked, now that this I/O is done and will be removed.
13055 * We need to do it before removal to have OOA position to start.
13057 ctl_try_unblock_others(lun, io, TRUE);
13060 * Remove this from the OOA queue.
13062 TAILQ_REMOVE(&lun->ooa_queue, &io->io_hdr, ooa_links);
13064 if (TAILQ_EMPTY(&lun->ooa_queue))
13065 lun->last_busy = getsbinuptime();
13069 * If the LUN has been invalidated, free it if there is nothing
13070 * left on its OOA queue.
13072 if ((lun->flags & CTL_LUN_INVALID)
13073 && TAILQ_EMPTY(&lun->ooa_queue)) {
13074 mtx_unlock(&lun->lun_lock);
13077 mtx_unlock(&lun->lun_lock);
13082 * If this command has been aborted, make sure we set the status
13083 * properly. The FETD is responsible for freeing the I/O and doing
13084 * whatever it needs to do to clean up its state.
13086 if (io->io_hdr.flags & CTL_FLAG_ABORT)
13087 ctl_set_task_aborted(&io->scsiio);
13090 * If enabled, print command error status.
13092 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS &&
13093 (ctl_debug & CTL_DEBUG_INFO) != 0)
13094 ctl_io_error_print(io, NULL);
13097 * Tell the FETD or the other shelf controller we're done with this
13098 * command. Note that only SCSI commands get to this point. Task
13099 * management commands are completed above.
13101 if ((softc->ha_mode != CTL_HA_MODE_XFER) &&
13102 (io->io_hdr.flags & CTL_FLAG_SENT_2OTHER_SC)) {
13103 memset(&msg, 0, sizeof(msg));
13104 msg.hdr.msg_type = CTL_MSG_FINISH_IO;
13105 msg.hdr.serializing_sc = io->io_hdr.remote_io;
13106 msg.hdr.nexus = io->io_hdr.nexus;
13107 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13108 sizeof(msg.scsi) - sizeof(msg.scsi.sense_data),
13116 * Front end should call this if it doesn't do autosense. When the request
13117 * sense comes back in from the initiator, we'll dequeue this and send it.
13120 ctl_queue_sense(union ctl_io *io)
13122 struct ctl_softc *softc = CTL_SOFTC(io);
13123 struct ctl_port *port = CTL_PORT(io);
13124 struct ctl_lun *lun;
13125 struct scsi_sense_data *ps;
13126 uint32_t initidx, p, targ_lun;
13128 CTL_DEBUG_PRINT(("ctl_queue_sense\n"));
13130 targ_lun = ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13133 * LUN lookup will likely move to the ctl_work_thread() once we
13134 * have our new queueing infrastructure (that doesn't put things on
13135 * a per-LUN queue initially). That is so that we can handle
13136 * things like an INQUIRY to a LUN that we don't have enabled. We
13137 * can't deal with that right now.
13138 * If we don't have a LUN for this, just toss the sense information.
13140 mtx_lock(&softc->ctl_lock);
13141 if (targ_lun >= ctl_max_luns ||
13142 (lun = softc->ctl_luns[targ_lun]) == NULL) {
13143 mtx_unlock(&softc->ctl_lock);
13146 mtx_lock(&lun->lun_lock);
13147 mtx_unlock(&softc->ctl_lock);
13149 initidx = ctl_get_initindex(&io->io_hdr.nexus);
13150 p = initidx / CTL_MAX_INIT_PER_PORT;
13151 if (lun->pending_sense[p] == NULL) {
13152 lun->pending_sense[p] = malloc(sizeof(*ps) * CTL_MAX_INIT_PER_PORT,
13153 M_CTL, M_NOWAIT | M_ZERO);
13155 if ((ps = lun->pending_sense[p]) != NULL) {
13156 ps += initidx % CTL_MAX_INIT_PER_PORT;
13157 memset(ps, 0, sizeof(*ps));
13158 memcpy(ps, &io->scsiio.sense_data, io->scsiio.sense_len);
13160 mtx_unlock(&lun->lun_lock);
13164 return (CTL_RETVAL_COMPLETE);
13168 * Primary command inlet from frontend ports. All SCSI and task I/O
13169 * requests must go through this function.
13172 ctl_queue(union ctl_io *io)
13174 struct ctl_port *port = CTL_PORT(io);
13176 CTL_DEBUG_PRINT(("ctl_queue cdb[0]=%02X\n", io->scsiio.cdb[0]));
13179 io->io_hdr.start_time = time_uptime;
13180 getbinuptime(&io->io_hdr.start_bt);
13181 #endif /* CTL_TIME_IO */
13183 /* Map FE-specific LUN ID into global one. */
13184 io->io_hdr.nexus.targ_mapped_lun =
13185 ctl_lun_map_from_port(port, io->io_hdr.nexus.targ_lun);
13187 switch (io->io_hdr.io_type) {
13190 if (ctl_debug & CTL_DEBUG_CDB)
13192 ctl_enqueue_incoming(io);
13195 printf("ctl_queue: unknown I/O type %d\n", io->io_hdr.io_type);
13199 return (CTL_RETVAL_COMPLETE);
13202 #ifdef CTL_IO_DELAY
13204 ctl_done_timer_wakeup(void *arg)
13208 io = (union ctl_io *)arg;
13211 #endif /* CTL_IO_DELAY */
13214 ctl_serseq_done(union ctl_io *io)
13216 struct ctl_lun *lun = CTL_LUN(io);;
13218 if (lun->be_lun == NULL ||
13219 lun->be_lun->serseq == CTL_LUN_SERSEQ_OFF)
13221 mtx_lock(&lun->lun_lock);
13222 io->io_hdr.flags |= CTL_FLAG_SERSEQ_DONE;
13223 ctl_try_unblock_others(lun, io, FALSE);
13224 mtx_unlock(&lun->lun_lock);
13228 ctl_done(union ctl_io *io)
13232 * Enable this to catch duplicate completion issues.
13235 if (io->io_hdr.flags & CTL_FLAG_ALREADY_DONE) {
13236 printf("%s: type %d msg %d cdb %x iptl: "
13237 "%u:%u:%u tag 0x%04x "
13238 "flag %#x status %x\n",
13240 io->io_hdr.io_type,
13241 io->io_hdr.msg_type,
13243 io->io_hdr.nexus.initid,
13244 io->io_hdr.nexus.targ_port,
13245 io->io_hdr.nexus.targ_lun,
13246 (io->io_hdr.io_type ==
13248 io->taskio.tag_num :
13249 io->scsiio.tag_num,
13251 io->io_hdr.status);
13253 io->io_hdr.flags |= CTL_FLAG_ALREADY_DONE;
13257 * This is an internal copy of an I/O, and should not go through
13258 * the normal done processing logic.
13260 if (io->io_hdr.flags & CTL_FLAG_INT_COPY)
13263 #ifdef CTL_IO_DELAY
13264 if (io->io_hdr.flags & CTL_FLAG_DELAY_DONE) {
13265 io->io_hdr.flags &= ~CTL_FLAG_DELAY_DONE;
13267 struct ctl_lun *lun = CTL_LUN(io);
13270 && (lun->delay_info.done_delay > 0)) {
13272 callout_init(&io->io_hdr.delay_callout, /*mpsafe*/ 1);
13273 io->io_hdr.flags |= CTL_FLAG_DELAY_DONE;
13274 callout_reset(&io->io_hdr.delay_callout,
13275 lun->delay_info.done_delay * hz,
13276 ctl_done_timer_wakeup, io);
13277 if (lun->delay_info.done_type == CTL_DELAY_TYPE_ONESHOT)
13278 lun->delay_info.done_delay = 0;
13282 #endif /* CTL_IO_DELAY */
13284 ctl_enqueue_done(io);
13288 ctl_work_thread(void *arg)
13290 struct ctl_thread *thr = (struct ctl_thread *)arg;
13291 struct ctl_softc *softc = thr->ctl_softc;
13295 CTL_DEBUG_PRINT(("ctl_work_thread starting\n"));
13296 thread_lock(curthread);
13297 sched_prio(curthread, PUSER - 1);
13298 thread_unlock(curthread);
13300 while (!softc->shutdown) {
13302 * We handle the queues in this order:
13304 * - done queue (to free up resources, unblock other commands)
13308 * If those queues are empty, we break out of the loop and
13311 mtx_lock(&thr->queue_lock);
13312 io = (union ctl_io *)STAILQ_FIRST(&thr->isc_queue);
13314 STAILQ_REMOVE_HEAD(&thr->isc_queue, links);
13315 mtx_unlock(&thr->queue_lock);
13316 ctl_handle_isc(io);
13319 io = (union ctl_io *)STAILQ_FIRST(&thr->done_queue);
13321 STAILQ_REMOVE_HEAD(&thr->done_queue, links);
13322 /* clear any blocked commands, call fe_done */
13323 mtx_unlock(&thr->queue_lock);
13324 ctl_process_done(io);
13327 io = (union ctl_io *)STAILQ_FIRST(&thr->incoming_queue);
13329 STAILQ_REMOVE_HEAD(&thr->incoming_queue, links);
13330 mtx_unlock(&thr->queue_lock);
13331 if (io->io_hdr.io_type == CTL_IO_TASK)
13334 ctl_scsiio_precheck(softc, &io->scsiio);
13337 io = (union ctl_io *)STAILQ_FIRST(&thr->rtr_queue);
13339 STAILQ_REMOVE_HEAD(&thr->rtr_queue, links);
13340 mtx_unlock(&thr->queue_lock);
13341 retval = ctl_scsiio(&io->scsiio);
13342 if (retval != CTL_RETVAL_COMPLETE)
13343 CTL_DEBUG_PRINT(("ctl_scsiio failed\n"));
13347 /* Sleep until we have something to do. */
13348 mtx_sleep(thr, &thr->queue_lock, PDROP, "-", 0);
13350 thr->thread = NULL;
13355 ctl_lun_thread(void *arg)
13357 struct ctl_softc *softc = (struct ctl_softc *)arg;
13358 struct ctl_be_lun *be_lun;
13360 CTL_DEBUG_PRINT(("ctl_lun_thread starting\n"));
13361 thread_lock(curthread);
13362 sched_prio(curthread, PUSER - 1);
13363 thread_unlock(curthread);
13365 while (!softc->shutdown) {
13366 mtx_lock(&softc->ctl_lock);
13367 be_lun = STAILQ_FIRST(&softc->pending_lun_queue);
13368 if (be_lun != NULL) {
13369 STAILQ_REMOVE_HEAD(&softc->pending_lun_queue, links);
13370 mtx_unlock(&softc->ctl_lock);
13371 ctl_create_lun(be_lun);
13375 /* Sleep until we have something to do. */
13376 mtx_sleep(&softc->pending_lun_queue, &softc->ctl_lock,
13379 softc->lun_thread = NULL;
13384 ctl_thresh_thread(void *arg)
13386 struct ctl_softc *softc = (struct ctl_softc *)arg;
13387 struct ctl_lun *lun;
13388 struct ctl_logical_block_provisioning_page *page;
13390 union ctl_ha_msg msg;
13391 uint64_t thres, val;
13394 CTL_DEBUG_PRINT(("ctl_thresh_thread starting\n"));
13395 thread_lock(curthread);
13396 sched_prio(curthread, PUSER - 1);
13397 thread_unlock(curthread);
13399 while (!softc->shutdown) {
13400 mtx_lock(&softc->ctl_lock);
13401 STAILQ_FOREACH(lun, &softc->lun_list, links) {
13402 if ((lun->flags & CTL_LUN_DISABLED) ||
13403 (lun->flags & CTL_LUN_NO_MEDIA) ||
13404 lun->backend->lun_attr == NULL)
13406 if ((lun->flags & CTL_LUN_PRIMARY_SC) == 0 &&
13407 softc->ha_mode == CTL_HA_MODE_XFER)
13409 if ((lun->MODE_RWER.byte8 & SMS_RWER_LBPERE) == 0)
13412 page = &lun->MODE_LBP;
13413 for (i = 0; i < CTL_NUM_LBP_THRESH; i++) {
13414 if ((page->descr[i].flags & SLBPPD_ENABLED) == 0)
13416 thres = scsi_4btoul(page->descr[i].count);
13417 thres <<= CTL_LBP_EXPONENT;
13418 switch (page->descr[i].resource) {
13420 attr = "blocksavail";
13423 attr = "blocksused";
13426 attr = "poolblocksavail";
13429 attr = "poolblocksused";
13434 mtx_unlock(&softc->ctl_lock); // XXX
13435 val = lun->backend->lun_attr(
13436 lun->be_lun->be_lun, attr);
13437 mtx_lock(&softc->ctl_lock);
13438 if (val == UINT64_MAX)
13440 if ((page->descr[i].flags & SLBPPD_ARMING_MASK)
13441 == SLBPPD_ARMING_INC)
13442 e = (val >= thres);
13444 e = (val <= thres);
13448 mtx_lock(&lun->lun_lock);
13450 scsi_u64to8b((uint8_t *)&page->descr[i] -
13451 (uint8_t *)page, lun->ua_tpt_info);
13452 if (lun->lasttpt == 0 ||
13453 time_uptime - lun->lasttpt >= CTL_LBP_UA_PERIOD) {
13454 lun->lasttpt = time_uptime;
13455 ctl_est_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13461 ctl_clr_ua_all(lun, -1, CTL_UA_THIN_PROV_THRES);
13464 mtx_unlock(&lun->lun_lock);
13466 lun->ctl_softc->ha_mode == CTL_HA_MODE_XFER) {
13467 /* Send msg to other side. */
13468 bzero(&msg.ua, sizeof(msg.ua));
13469 msg.hdr.msg_type = CTL_MSG_UA;
13470 msg.hdr.nexus.initid = -1;
13471 msg.hdr.nexus.targ_port = -1;
13472 msg.hdr.nexus.targ_lun = lun->lun;
13473 msg.hdr.nexus.targ_mapped_lun = lun->lun;
13475 msg.ua.ua_set = (set > 0);
13476 msg.ua.ua_type = CTL_UA_THIN_PROV_THRES;
13477 memcpy(msg.ua.ua_info, lun->ua_tpt_info, 8);
13478 mtx_unlock(&softc->ctl_lock); // XXX
13479 ctl_ha_msg_send(CTL_HA_CHAN_CTL, &msg,
13480 sizeof(msg.ua), M_WAITOK);
13481 mtx_lock(&softc->ctl_lock);
13484 mtx_sleep(&softc->thresh_thread, &softc->ctl_lock,
13485 PDROP, "-", CTL_LBP_PERIOD * hz);
13487 softc->thresh_thread = NULL;
13492 ctl_enqueue_incoming(union ctl_io *io)
13494 struct ctl_softc *softc = CTL_SOFTC(io);
13495 struct ctl_thread *thr;
13498 idx = (io->io_hdr.nexus.targ_port * 127 +
13499 io->io_hdr.nexus.initid) % worker_threads;
13500 thr = &softc->threads[idx];
13501 mtx_lock(&thr->queue_lock);
13502 STAILQ_INSERT_TAIL(&thr->incoming_queue, &io->io_hdr, links);
13503 mtx_unlock(&thr->queue_lock);
13508 ctl_enqueue_rtr(union ctl_io *io)
13510 struct ctl_softc *softc = CTL_SOFTC(io);
13511 struct ctl_thread *thr;
13513 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13514 mtx_lock(&thr->queue_lock);
13515 STAILQ_INSERT_TAIL(&thr->rtr_queue, &io->io_hdr, links);
13516 mtx_unlock(&thr->queue_lock);
13521 ctl_enqueue_done(union ctl_io *io)
13523 struct ctl_softc *softc = CTL_SOFTC(io);
13524 struct ctl_thread *thr;
13526 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13527 mtx_lock(&thr->queue_lock);
13528 STAILQ_INSERT_TAIL(&thr->done_queue, &io->io_hdr, links);
13529 mtx_unlock(&thr->queue_lock);
13534 ctl_enqueue_isc(union ctl_io *io)
13536 struct ctl_softc *softc = CTL_SOFTC(io);
13537 struct ctl_thread *thr;
13539 thr = &softc->threads[io->io_hdr.nexus.targ_mapped_lun % worker_threads];
13540 mtx_lock(&thr->queue_lock);
13541 STAILQ_INSERT_TAIL(&thr->isc_queue, &io->io_hdr, links);
13542 mtx_unlock(&thr->queue_lock);